Skip to content

Commit

Permalink
[KT] Box2d idiomatic - enable assert, pt 2
Browse files Browse the repository at this point in the history
`assert` is now available and we're retaining it so that binary size is still comparable.

PiperOrigin-RevId: 721470758
  • Loading branch information
Googler authored and copybara-github committed Jan 30, 2025
1 parent 378f88b commit 38afe3a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ class Distance {
proxyB: DistanceProxy,
transformB: Transform,
) {
// assert is not supported in KMP.
// assert(cache.count <= 3)
assert(cache.count <= 3)

// Copy data from cache.
count = cache.count
Expand Down Expand Up @@ -193,8 +192,7 @@ class Distance {
}
}
else -> {
// assert is not supported in KMP.
// assert(false)
assert(false)
out.setZero()
}
}
Expand All @@ -208,8 +206,7 @@ class Distance {
fun getClosestPoint(out: Vec2) {
when (count) {
0 -> {
// assert is not supported in KMP.
// assert(false)
assert(false)
out.setZero()
}
1 -> {
Expand All @@ -224,8 +221,7 @@ class Distance {
out.setZero()
}
else -> {
// assert is not supported in KMP.
// assert(false)
assert(false)
out.setZero()
}
}
Expand All @@ -234,8 +230,7 @@ class Distance {
fun getWitnessPoints(pA: Vec2, pB: Vec2) {
when (count) {
0 -> {
// assert is not supported in KMP.
// assert(false)
assert(false)
}
1 -> {
pA.set(v1.wA)
Expand All @@ -257,8 +252,7 @@ class Distance {
pB.set(pA)
}
else -> {
// assert is not supported in KMP.
// assert(false)
assert(false)
}
}
}
Expand All @@ -267,8 +261,7 @@ class Distance {
fun getMetric(): Float {
when (count) {
0 -> {
// assert is not supported in KMP.
// assert(false)
assert(false)
return 0.0f
}
1 -> return 0.0f
Expand All @@ -280,8 +273,7 @@ class Distance {
return case3 cross case33
}
else -> {
// assert is not supported in KMP.
// assert(false)
assert(false)
return 0.0f
}
}
Expand Down Expand Up @@ -484,8 +476,7 @@ class Distance {
}
ShapeType.CHAIN -> {
val chain = shape as ChainShape
// assert is not supported in KMP.
// assert(0 <= index && index < chain.m_count)
assert(0 <= index && index < chain.count)
buffer[0] = chain.vertices!![index]
if (index + 1 < chain.count) {
buffer[1] = chain.vertices!![index + 1]
Expand Down Expand Up @@ -589,8 +580,7 @@ class Distance {
2 -> simplex.solve2()
3 -> simplex.solve3()
else -> {
// assert is not supported in KMP.
// assert(false)
assert(false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,17 @@ class DynamicTree : BroadPhaseStrategy {
}

override fun destroyProxy(proxyId: Int) {
// assert is not supported in KMP.
// assert(0 <= proxyId && proxyId < nodeCapacity)
assert(0 <= proxyId && proxyId < nodeCapacity)
val node = treeNodes[proxyId]
// assert is not supported in KMP.
// assert(node.isLeaf())
assert(node.isLeaf())
removeLeaf(node)
freeNode(node)
}

override fun moveProxy(proxyId: Int, aabb: AABB, displacement: Vec2): Boolean {
// assert is not supported in KMP.
// assert(0 <= proxyId && proxyId < nodeCapacity)
assert(0 <= proxyId && proxyId < nodeCapacity)
val node = treeNodes[proxyId]
// assert is not supported in KMP.
// assert(node.isLeaf())
assert(node.isLeaf())
val nodeAABB = node.aabb
// if (nodeAABB.contains(aabb)) {
if (
Expand Down Expand Up @@ -170,8 +166,7 @@ class DynamicTree : BroadPhaseStrategy {
var tempy: Float
r.x = p2x - p1x
r.y = p2y - p1y
// assert is not supported in KMP.
// assert(r.x * r.x + r.y * r.y > 0f)
assert(r.x * r.x + r.y * r.y > 0f)
r.normalize()
val rx: Float = r.x
val ry: Float = r.y
Expand Down Expand Up @@ -257,8 +252,7 @@ class DynamicTree : BroadPhaseStrategy {
override fun computeHeight(): Int = computeHeight(root!!)

private fun computeHeight(node: DynamicTreeNode): Int {
// assert is not supported in KMP.
// assert(0 <= node!!.id && node.id < nodeCapacity)
assert(0 <= node.id && node.id < nodeCapacity)
if (node.isLeaf()) {
return 0
}
Expand All @@ -274,15 +268,13 @@ class DynamicTree : BroadPhaseStrategy {
var freeCount = 0
var freeNode = if (freeList != NULL_NODE) treeNodes[freeList] else null
while (freeNode != null) {
// assert is not supported in KMP.
// assert(0 <= freeNode.id && freeNode.id < nodeCapacity)
// assert(freeNode === treeNodes[freeNode.id])
assert(0 <= freeNode.id && freeNode.id < nodeCapacity)
assert(freeNode === treeNodes[freeNode.id])
freeNode = freeNode.parent
++freeCount
}
// assert is not supported in KMP.
// assert(getHeight() == computeHeight())
// assert(nodeCount + freeCount == nodeCapacity)
assert(getHeight() == computeHeight())
assert(nodeCount + freeCount == nodeCapacity)
}

override fun getHeight(): Int {
Expand All @@ -299,8 +291,7 @@ class DynamicTree : BroadPhaseStrategy {
if (node.height <= 1) {
continue
}
// assert is not supported in KMP.
// assert(node.isLeaf() == false)
assert(!node.isLeaf())
val child1 = node.child1
val child2 = node.child2
val balance = MathUtils.abs(child2!!.height - child1!!.height)
Expand Down Expand Up @@ -388,8 +379,7 @@ class DynamicTree : BroadPhaseStrategy {

private fun allocateNode(): DynamicTreeNode {
if (freeList == NULL_NODE) {
// assert is not supported in KMP.
// assert(nodeCount == nodeCapacity)
assert(nodeCount == nodeCapacity)
val old = treeNodes
nodeCapacity *= 2
treeNodes = Array(nodeCapacity) { DynamicTreeNode(it) }
Expand All @@ -416,9 +406,7 @@ class DynamicTree : BroadPhaseStrategy {

/** returns a node to the pool */
private fun freeNode(node: DynamicTreeNode) {
// assert is not supported in KMP.
// assert(node != null)
// assert(0 < nodeCount)
assert(0 < nodeCount)
node.parent = if (freeList != NULL_NODE) treeNodes[freeList] else null
node.height = -1
freeList = node.id
Expand Down Expand Up @@ -521,9 +509,8 @@ class DynamicTree : BroadPhaseStrategy {
index = balance(index)
val child1 = index.child1
val child2 = index.child2
// assert is not supported in KMP.
// assert(child1 != null)
// assert(child2 != null)
assert(child1 != null)
assert(child2 != null)
index.height = 1 + MathUtils.max(child1!!.height, child2!!.height)
index.aabb.combine(child1.aabb, child2.aabb)
index = index.parent
Expand Down Expand Up @@ -577,28 +564,24 @@ class DynamicTree : BroadPhaseStrategy {
// Perform a left or right rotation if node A is imbalanced.
// Returns the new root index.
private fun balance(iA: DynamicTreeNode): DynamicTreeNode {
// assert is not supported in KMP.
// assert(iA != null)
if (iA.isLeaf() || iA.height < 2) {
return iA
}
val iB = iA.child1
val iC = iA.child2
// assert is not supported in KMP.
// assert(0 <= iB!!.id && iB.id < nodeCapacity)
// assert(0 <= iC!!.id && iC.id < nodeCapacity)
assert(0 <= iB!!.id && iB.id < nodeCapacity)
assert(0 <= iC!!.id && iC.id < nodeCapacity)
val balance = iC!!.height - iB!!.height

// Rotate C up
if (balance > 1) {
val iF = iC.child1
val iG = iC.child2

// assert is not supported in KMP.
// assert(iF != null)
// assert(iG != null)
// assert(0 <= iF!!.id && iF.id < nodeCapacity)
// assert(0 <= iG!!.id && iG.id < nodeCapacity)
assert(iF != null)
assert(iG != null)
assert(0 <= iF!!.id && iF.id < nodeCapacity)
assert(0 <= iG!!.id && iG.id < nodeCapacity)

// Swap A and C
iC.child1 = iA
Expand All @@ -610,8 +593,7 @@ class DynamicTree : BroadPhaseStrategy {
if (iC.parent!!.child1 === iA) {
iC.parent!!.child1 = iC
} else {
// assert is not supported in KMP.
// assert(iC.parent!!.child2 === iA)
assert(iC.parent!!.child2 === iA)
iC.parent!!.child2 = iC
}
} else {
Expand Down Expand Up @@ -643,9 +625,8 @@ class DynamicTree : BroadPhaseStrategy {
if (balance < -1) {
val iD = iB.child1
val iE = iB.child2
// assert is not supported in KMP.
// assert(0 <= iD!!.id && iD.id < nodeCapacity)
// assert(0 <= iE!!.id && iE.id < nodeCapacity)
assert(0 <= iD!!.id && iD.id < nodeCapacity)
assert(0 <= iE!!.id && iE.id < nodeCapacity)

// Swap A and B
iB.child1 = iA
Expand All @@ -658,8 +639,7 @@ class DynamicTree : BroadPhaseStrategy {
if (it.child1 === iA) {
it.child1 = iB
} else {
// assert is not supported in KMP.
// assert(iB.parent!!.child2 === iA)
assert(iB.parent!!.child2 === iA)
it.child2 = iB
}
}
Expand Down Expand Up @@ -694,26 +674,23 @@ class DynamicTree : BroadPhaseStrategy {
if (node == null) {
return
}
// assert is not supported in KMP.
// assert(node === treeNodes[node.id])
assert(node === treeNodes[node.id])
// if (node === root) {
// assert(node.parent == null)
// }
val child1 = node.child1
val child2 = node.child2
if (node.isLeaf()) {
// assert is not supported in KMP.
// assert(child1 == null)
// assert(child2 == null)
// assert(node.height == 0)
assert(child1 == null)
assert(child2 == null)
assert(node.height == 0)
return
}

// assert is not supported in KMP.
// assert(child1 != null && 0 <= child1.id && child1.id < nodeCapacity)
// assert(child2 != null && 0 <= child2.id && child2.id < nodeCapacity)
// assert(child1!!.parent === node)
// assert(child2!!.parent === node)
assert(child1 != null && 0 <= child1.id && child1.id < nodeCapacity)
assert(child2 != null && 0 <= child2.id && child2.id < nodeCapacity)
assert(child1!!.parent === node)
assert(child2!!.parent === node)
validateStructure(child1)
validateStructure(child2)
}
Expand All @@ -725,25 +702,21 @@ class DynamicTree : BroadPhaseStrategy {
val child1 = node.child1
val child2 = node.child2
if (node.isLeaf()) {
// assert is not supported in KMP.
// assert(child1 == null)
// assert(child2 == null)
// assert(node.height == 0)
assert(child1 == null)
assert(child2 == null)
assert(node.height == 0)
return
}
// assert is not supported in KMP.
// assert(child1 != null && 0 <= child1.id && child1.id < nodeCapacity)
// assert(child2 != null && 0 <= child2.id && child2.id < nodeCapacity)
assert(child1 != null && 0 <= child1.id && child1.id < nodeCapacity)
assert(child2 != null && 0 <= child2.id && child2.id < nodeCapacity)
val height1 = child1!!.height
val height2 = child2!!.height
@Suppress("UNUSED_VARIABLE") val height: Int = 1 + MathUtils.max(height1, height2)
// assert is not supported in KMP.
// assert(node.height == height)
val height: Int = 1 + MathUtils.max(height1, height2)
assert(node.height == height)
val aabb = AABB()
aabb.combine(child1.aabb, child2.aabb)
// assert is not supported in KMP.
// assert(aabb.lowerBound == node.aabb.lowerBound)
// assert(aabb.upperBound == node.aabb.upperBound)
assert(aabb.lowerBound == node.aabb.lowerBound)
assert(aabb.upperBound == node.aabb.upperBound)
validateMetrics(child1)
validateMetrics(child2)
}
Expand All @@ -765,7 +738,7 @@ class DynamicTree : BroadPhaseStrategy {
textVec.x,
textVec.y,
node.id.toString() + "-" + (spot + 1) + "/" + height,
color
color,
)
if (node.child1 != null) {
drawTree(argDraw, node.child1!!, spot + 1, height)
Expand Down

0 comments on commit 38afe3a

Please sign in to comment.