Skip to content

Commit

Permalink
3DPruningImprovements PR Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Vin committed Jan 12, 2024
1 parent 7738224 commit 0a75bfc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
18 changes: 7 additions & 11 deletions src/scenic/core/pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def bufferHelper(viewRegion):
if (
base is not ego.visibleRegion
and not needsSampling(ego.visibleRegion)
and not checkCyclical(scenario, base, ego.visibleRegion)
and not checkCyclical(base, ego.visibleRegion)
):
if verbosity >= 1:
print(
Expand All @@ -446,7 +446,7 @@ def bufferHelper(viewRegion):
if (
base is not obj._observingEntity.visibleRegion
and not needsSampling(obj._observingEntity.visibleRegion)
and not checkCyclical(scenario, base, obj._observingEntity.visibleRegion)
and not checkCyclical(base, obj._observingEntity.visibleRegion)
):
if verbosity >= 1:
print(
Expand Down Expand Up @@ -596,15 +596,11 @@ def percentagePruned(base, newBase):
return None


def checkCyclical(scenario, A, B):
def checkCyclical(A, B):
"""Check for a potential circular dependency
Returns True if the scenario would have a circular dependency
if A depended on B.
This function runs DFS on the scenario, with the dependency edges
reversed for convenience (this should not affect whether or not a
cycle exists).
"""
state = collections.defaultdict(lambda: 0)

Expand All @@ -621,10 +617,10 @@ def dfs(target):
# Recurse on children
deps = conditionedDeps(target)

if target is A:
deps.append(B)

for child in deps:
if child is A:
return True

if dfs(child):
return True

Expand All @@ -633,7 +629,7 @@ def dfs(target):

return False

return dfs(scenario)
return dfs(B)


def conditionedDeps(samp):
Expand Down
4 changes: 0 additions & 4 deletions src/scenic/core/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2088,10 +2088,6 @@ def __init__(self, voxelGrid, orientation=None, name=None, lazy=False):
self.voxel_points = self.voxelGrid.points
self.scale = self.voxelGrid.scale

# Initialize KD-Tree for containment checking if not lazy
if not lazy:
self.kdTree

@cached_property
def kdTree(self):
return scipy.spatial.KDTree(self.voxel_points)
Expand Down
28 changes: 27 additions & 1 deletion tests/syntax/test_pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from scenic.core.errors import InconsistentScenarioError
from scenic.core.pruning import checkCyclical
from scenic.core.vectors import Vector
from tests.utils import compileScenic, sampleEgo, sampleParamP

Expand Down Expand Up @@ -209,12 +210,37 @@ def test_visibility_pruning():


def test_visibility_pruning_cyclical():
"""A case where a cyclical dependency could be introduced if pruning is not done carefully."""
"""A case where a cyclical dependency could be introduced if pruning is not done carefully.
NOTE: We don't currently prune this case so this test is a sentinel for future behavior.
"""
scenario = compileScenic(
"""
workspace = Workspace(PolygonalRegion([0@0, 100@0, 100@100, 0@100]))
foo = new Object with requireVisible True, in workspace
ego = new Object visible from foo, in workspace
"""
)

sampleEgo(scenario, maxIterations=100)


def test_checkCyclical():
scenario = compileScenic(
"""
workspace = Workspace(PolygonalRegion([0@0, 100@0, 100@100, 0@100]))
foo = new Object in workspace
ego = new Object in workspace
"""
)
assert not checkCyclical(scenario.objects[1].position, scenario.objects[0].position)

scenario = compileScenic(
"""
workspace = Workspace(PolygonalRegion([0@0, 100@0, 100@100, 0@100]))
foo = new Object with requireVisible True, in workspace
ego = new Object visible from foo
"""
)

assert checkCyclical(scenario.objects[1].position, scenario.objects[0].visibleRegion)

0 comments on commit 0a75bfc

Please sign in to comment.