Skip to content

Instantly share code, notes, and snippets.

@mnn
Created September 9, 2014 09:43
Show Gist options
  • Save mnn/57cc01980312289f39e9 to your computer and use it in GitHub Desktop.
Save mnn/57cc01980312289f39e9 to your computer and use it in GitHub Desktop.
import scala.runtime.ScalaRunTime.stringOf
import scala.collection.JavaConverters._
// stuff for verifying results, skip to case defs (comment -- 1 --)
def doVerify[A](name: String, origRes: A, myRes: A) {
val equals = origRes match {
case a: Array[_] => a.sameElements(myRes.asInstanceOf[Array[_]])
case _ => origRes.equals(myRes)
}
if (equals) println(s"Case '$name' fine: ${stringOf(myRes)}")
else println(s"Case '$name' wrong:\n${stringOf(origRes)}\nvs.\n${stringOf(myRes)}")
}
def verify[A, R](name: String, orig: (A) => R, mine: (A) => R, testVal: A) {
doVerify(name, orig(testVal), mine(testVal))
}
def verify[R](name: String, orig: () => R, mine: () => R) {
doVerify(name, orig(), mine())
}
// -- 1 --
def case1orig(oBounds: Seq[Int]) = {
var boxes = Seq(oBounds(6))
for (s <- 0 until 6) boxes :+= oBounds(s)
boxes
}
def case1mine(oBounds: Seq[Int]) = oBounds.last +: oBounds.init
verify("1", case1orig, case1mine, 0 to 6)
// -------
// -- 2 --
def case2orig() = {
val w = "someExpression"
w
}
def case2mine() = "someExpression"
verify("2", case2orig, case2mine)
// same with returning in generateBoxes, generateCollisionBoxes
// -------
// -- 3 --
// Does getOcclusionBoxes even compile? There is no return value in "if" branch.
/*
override def getOcclusionBoxes = {
import mlb.technica.experience.PipeBoxes._
if (true)
else Seq(oBounds(6)).asJava
}
*/
// Possibly what you meant:
def case3orig(oBounds: Seq[Int]) = {
if (true) Seq().asJava
else Seq(oBounds(6)).asJava
}
def case3mine(oBounds: Seq[Int]) = Seq().asJava
verify("3", case3orig, case3mine, 0 to 6)
// -------
// -- 4 --
// Some implementation of used classes in snippet
class Cuboid6(var a: Double, var b: Double, var c: Double, var d: Double, var e: Double, var f: Double) {
def apply(r: Rotation): Cuboid6 = {
a -= r.x
b -= r.y
c -= r.z
d -= 7 * r.x
e -= 9 * r.y
f -= 13 * r.x
this
}
override def equals(that: Any) =
that match {
case t: Cuboid6 => t.a == a && t.b == b && t.c == c && t.d == d && t.e == e && t.f == f
case _ => false
}
override def toString() = s"Cuboid6($a, $b, $c, $d, $e, $f)"
}
class Rotation(val x: Double, val y: Double, val z: Double) {
def at(v: Vector3): Rotation = new Rotation(x + v.x, y + 2 * v.y, z + 3 * v.z)
}
object Rotation {
val sideRotations = (0 to 6).map(v => new Rotation(1.0 / (v + .1), 5.0 / (v + 2), 5 * (v - 3)))
}
class Vector3(val x: Double, val y: Double, val z: Double)
object Vector3 {
lazy val center = new Vector3(11, 23, 27)
}
// PipeBoxes.oBounds
def case4orig() = {
val boxes = new Array[Cuboid6](7)
val w = 2 / 8D
boxes(6) = new Cuboid6(0.5 - w, 0.5 - w, 0.5 - w, 0.5 + w, 0.5 + w, 0.5 + w)
for (s <- 0 until 6)
boxes(s) = new Cuboid6(0.5 - w, 0, 0.5 - w, 0.5 + w, 0.5 - w, 0.5 + w).apply(Rotation.sideRotations(s).at(Vector3.center))
boxes
}
def case4mine() = {
val w = 2 / 8D
val (m, p) = (.5 - w, .5 + w)
((for (s <- 0 until 6) yield new Cuboid6(m, 0, m, p, m, p).apply(Rotation.sideRotations(s).at(Vector3.center)))
:+ new Cuboid6(m, m, m, p, p, p)).toArray
}
verify("4", case4orig, case4mine)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment