Skip to content

Commit

Permalink
Merge pull request #67 from mayurdb/OctreeFix
Browse files Browse the repository at this point in the history
Octree and Geometry Objects Bug Fixes
  • Loading branch information
JulienPeloton authored Jul 12, 2018
2 parents 8caa14a + c43b83f commit 5b78e70
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 80 deletions.
70 changes: 26 additions & 44 deletions src/main/scala/com/spark3d/geometryObjects/BoxEnvelope.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.astrolabsoftware.spark3d.geometryObjects

import com.astrolabsoftware.spark3d.geometryObjects.Shape3D._
import com.astrolabsoftware.spark3d.utils.Utils.sphericalToCartesian

import scala.math._

Expand Down Expand Up @@ -460,11 +459,11 @@ class BoxEnvelope private(
return false
}

!(env.minX > maxX ||
env.maxX < minX ||
env.minY > maxY ||
env.maxY < minY ||
env.minZ > maxZ ||
!(env.minX >= maxX ||
env.maxX < minX ||
env.minY >= maxY ||
env.maxY < minY ||
env.minZ >= maxZ ||
env.maxZ < minZ)
}

Expand All @@ -486,41 +485,24 @@ class BoxEnvelope private(
using Utils.euclideantoSpherical(p).
""")
}

if (isNull) {
return false
}

val envMinX = min(p1.x, min(p2.x, p3.x))
if (envMinX > maxX) {
return false
}

val envMaxX = max(p1.x, max(p2.x, p3.x))
if (envMaxX < minX) {
return false
}

val envMinY = min(p1.y, min(p2.y, p3.y))
if (envMinY > maxY) {
return false
}

val envMaxY = max(p1.y, max(p2.y, p3.y))
if (envMaxY < minY) {
return false
}

val envMinZ = min(p1.z, min(p2.z, p3.z))
if (envMinZ > maxZ) {
return false
}

val envMaxZ = max(p1.z, max(p2.z, p3.z))
if (envMaxZ < minZ) {
return false
}

true
!(envMinX >= maxX ||
envMaxX < minX ||
envMinY >= maxY ||
envMaxY < minY ||
envMinZ >= maxZ ||
envMaxZ < minZ)
}

/**
Expand All @@ -537,12 +519,12 @@ class BoxEnvelope private(
return false
}

!(x < minX ||
x > maxX ||
y < minY ||
y > maxY ||
z < minZ ||
z > maxZ)
!(x >= maxX ||
x < minX ||
y >= maxY ||
y < minY ||
z >= maxZ ||
z < minZ)
}

/**
Expand Down Expand Up @@ -624,12 +606,12 @@ class BoxEnvelope private(
return false
}

x >= minX &&
x <= maxX &&
x >= minX &&
x < maxX &&
y >= minY &&
y <= maxY &&
y < maxY &&
z >= minZ &&
z <= maxZ
z < maxZ

}

Expand All @@ -645,12 +627,12 @@ class BoxEnvelope private(
return false
}

env.minX >= minX &&
env.maxX <= maxX &&
env.minX >= minX &&
env.maxX < maxX &&
env.minY >= minY &&
env.maxY <= maxY &&
env.maxY < maxY &&
env.minZ >= minZ &&
env.maxZ <= maxZ
env.maxZ < maxZ
}

/**
Expand Down
56 changes: 30 additions & 26 deletions src/main/scala/com/spark3d/spatialPartitioning/Octree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,60 +73,64 @@ class Octree(
private def splitBox(): Unit = {
children = new Array[Octree](8)

val midX = (box.minX + box.maxX) / 2
val midY = (box.minY + box.maxY) / 2
val midZ = (box.minZ + box.maxZ) / 2

children(CHILD_L_SW) = new Octree(
BoxEnvelope.apply(
box.minX, (box.maxX - box.minX) / 2,
box.minY, (box.maxY - box.minY) / 2,
box.minZ, (box.maxZ - box.minZ) / 2),
box.minX, midX,
box.minY, midY,
box.minZ, midZ),
level + 1, this, maxItemsPerNode, maxLevel)

children(CHILD_L_SE) = new Octree(
BoxEnvelope.apply(
(box.maxX - box.minX) / 2, box.maxX,
box.minY, (box.maxY - box.minY) / 2,
box.minZ, (box.maxZ - box.minZ) / 2),
midX, box.maxX,
box.minY, midY,
box.minZ, midZ),
level + 1, this, maxItemsPerNode, maxLevel)

children(CHILD_L_NW) = new Octree(
BoxEnvelope.apply(
box.minX, (box.maxX - box.minX) / 2,
(box.maxY - box.minY) / 2, box.maxY,
box.minZ, (box.maxZ - box.minZ) / 2),
box.minX, midX,
midY, box.maxY,
box.minZ, midZ),
level + 1, this, maxItemsPerNode, maxLevel)

children(CHILD_L_NE) = new Octree(
BoxEnvelope.apply(
(box.maxX - box.minX) / 2, box.maxX,
(box.maxY - box.minY) / 2, box.maxY,
box.minZ, (box.maxZ - box.minZ) / 2),
midX, box.maxX,
midY, box.maxY,
box.minZ, midZ),
level + 1, this, maxItemsPerNode, maxLevel)

children(CHILD_U_SW) = new Octree(
BoxEnvelope.apply(
box.minX, (box.maxX - box.minX) / 2,
box.minY, (box.maxY - box.minY) / 2,
(box.maxZ - box.minZ) / 2, box.maxZ),
box.minX, midX,
box.minY, midY,
midZ, box.maxZ),
level + 1, this, maxItemsPerNode, maxLevel)

children(CHILD_U_SE) = new Octree(
BoxEnvelope.apply(
(box.maxX - box.minX) / 2, box.maxX,
box.minY, (box.maxY - box.minY) / 2,
(box.maxZ - box.minZ) / 2, box.maxZ),
midX, box.maxX,
box.minY, midY,
midZ, box.maxZ),
level + 1, this, maxItemsPerNode, maxLevel)

children(CHILD_U_NW) = new Octree(
BoxEnvelope.apply(
box.minX, (box.maxX - box.minX) / 2,
(box.maxY - box.minY) / 2, box.maxY,
(box.maxZ - box.minZ) / 2, box.maxZ),
box.minX, midX,
midY, box.maxY,
midZ, box.maxZ),
level + 1, this, maxItemsPerNode, maxLevel)

children(CHILD_U_NE) = new Octree(
BoxEnvelope.apply(
(box.maxX - box.minX) / 2, box.maxX,
(box.maxY - box.minY) / 2, box.maxY,
(box.maxZ - box.minZ) / 2, box.maxZ),
midX, box.maxX,
midY, box.maxY,
midZ, box.maxZ),
level + 1, this, maxItemsPerNode, maxLevel)

}
Expand Down Expand Up @@ -384,8 +388,8 @@ class Octree(
val matchedLeaves = new ListBuffer[Octree]
val traverseFunct: (Octree, BoxEnvelope) => Boolean = {
(node, obj) => node.isLeaf && (node.box.intersects(obj) ||
node.box.contains(obj) ||
obj.contains(node.box))
node.box.contains(obj))
// obj.contains(node.box))
}

dfsTraverse(traverseFunct, obj, matchedLeaves)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class OctreePartitionerTest extends FunSuite with BeforeAndAfterAll {
test ("Can you correctly place a Point3D inside the Octree space?") {

var valid_tree = new Octree(BoxEnvelope.apply(0.0, 4.0, 0.0, 4.0, 0.0, 4.0), 0, null, 2)
val element1 = BoxEnvelope.apply(0.0, 1.0, 0.0, 1.0, 0.0, 1.0)
val element1 = BoxEnvelope.apply(0.0, 0.9, 0.0, 0.9, 0.0, 0.9)
val element2 = BoxEnvelope.apply(1.0, 3.0, 1.0, 3.0, 1.0, 3.0)
val element3 = BoxEnvelope.apply(1.0, 2.0, 1.0, 2.0, 1.0, 2.0)
val element4 = BoxEnvelope.apply(0.0, 1.0, 1.0, 2.0, 0.0, 1.0)
val element3 = BoxEnvelope.apply(1.0, 1.9, 1.0, 1.9, 1.0, 1.9)
val element4 = BoxEnvelope.apply(0.0, 0.9, 1.0, 1.9, 0.0, 0.9)
val data = new ListBuffer[BoxEnvelope]
data += element1
data += element2
Expand All @@ -43,7 +43,7 @@ class OctreePartitionerTest extends FunSuite with BeforeAndAfterAll {
assert(result.next._1 == 13)

// case when object belongs to all partitions
spr = new ShellEnvelope(2, 2, 2, false, 1)
spr = new ShellEnvelope(2, 2, 2, false, 1.1)
result = partitioner.placeObject(spr)
var resultCount = 0
while (result.hasNext) {
Expand Down
12 changes: 6 additions & 6 deletions src/test/scala/com/spark3d/spatialPartitioning/OctreeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class OctreeTest extends FunSuite with BeforeAndAfterAll {
}

test ("Can you insert the elements into a Octree and verify its correctness?") {
val element1 = BoxEnvelope.apply(0.0, 1.0, 0.0, 1.0, 0.0, 1.0)
val element2 = BoxEnvelope.apply(1.0, 3.0, 1.0, 3.0, 1.0, 3.0)
val element1 = BoxEnvelope.apply(0.0, 0.9, 0.0, 0.9, 0.0, 0.9)
val element2 = BoxEnvelope.apply(1.0, 2.9, 1.0, 2.9, 1.0, 2.9)
valid_tree.insertElement(element1)
valid_tree.insertElement(element2)

val spr = new ShellEnvelope(1.0, 1.0, 1.0, false, 1.0)
val spr = new ShellEnvelope(1.0, 1.0, 1.0, false, 0.9)
var result = valid_tree.getElements(spr)
assert(valid_tree.isLeaf)
assert(result.size == 2)
Expand All @@ -59,9 +59,9 @@ class OctreeTest extends FunSuite with BeforeAndAfterAll {
assert(leafNodes.size == 1)
assert(containsElement(leafNodes, tree_space))

val element3 = BoxEnvelope.apply(1.0, 2.0, 1.0, 2.0, 1.0, 2.0)
val element3 = BoxEnvelope.apply(1.0, 1.9, 1.0, 1.9, 1.0, 1.9)
valid_tree.insertElement(element3)
result = valid_tree.getElements(BoxEnvelope.apply(3.0, 4.0, 3.0, 4.0, 3.0, 4.0))
result = valid_tree.getElements(BoxEnvelope.apply(3.0, 3.9, 3.0, 3.9, 3.0, 3.9))
assert(result.size == 1)
assert(containsElement(result, element2))
result = valid_tree.getElements(element1)
Expand All @@ -70,7 +70,7 @@ class OctreeTest extends FunSuite with BeforeAndAfterAll {
assert(containsElement(result, element2))
assert(containsElement(result, element3))

val element4 = BoxEnvelope.apply(0.0, 1.0, 1.0, 2.0, 0.0, 1.0)
val element4 = BoxEnvelope.apply(0.0, 0.9, 1.0, 1.9, 0.0, 0.9)
valid_tree.insertElement(element4)

result = valid_tree.getElements(element1)
Expand Down

0 comments on commit 5b78e70

Please sign in to comment.