Skip to content

Commit

Permalink
--add construction of volume edges for debug/visualizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jturner65 committed Jan 26, 2024
1 parent 797f863 commit b8b3e44
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 53 deletions.
59 changes: 15 additions & 44 deletions examples/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def __init__(self, sim_settings: Dict[str, Any]) -> None:
self.replay_renderer_cfg: Optional[ReplayRendererConfiguration] = None
self.replay_renderer: Optional[ReplayRenderer] = None
self.reconfigure_sim()
self.region_mesh_lines = None
self.region_colors = {}

# compute NavMesh if not already loaded by the scene.
Expand Down Expand Up @@ -225,54 +224,21 @@ def draw_contact_debug(self):
normal=camera_position - cp.position_on_b_in_ws,
)

def get_region_meshes(self):
self.region_mesh_lines = {}
self.region_colors = {}
for region in self.sim.semantic_scene.regions:
poly_loop_points = region.poly_loop_points
extrusion_height = region.extrusion_height
floor_height = region.floor_height
extrusion_vec = mn.Vector3(0, extrusion_height, 0)
if len(poly_loop_points) > 2:
this_region_mesh_lines = []
prev_pp_low = mn.Vector3(
poly_loop_points[-1][0], floor_height, poly_loop_points[-1][1]
)
for xz in poly_loop_points:
pp_low = mn.Vector3(xz[0], floor_height, xz[1])
pp_high = pp_low + extrusion_vec
prev_pp_high = prev_pp_low + extrusion_vec
this_region_mesh_lines.append((pp_low, pp_high))
this_region_mesh_lines.append((pp_low, prev_pp_low))
this_region_mesh_lines.append((pp_high, prev_pp_high))
this_region_mesh_lines.append((prev_pp_high, pp_low))
prev_pp_low = pp_low
self.region_mesh_lines[region.id] = this_region_mesh_lines
self.region_colors[region.id] = mn.Color4(
mn.Vector3(np.random.random(3))
)
else:
print(
f"Region {region.id} has too few poly loop points: {len(poly_loop_points)}."
)

def draw_region(self, region_id: str, color: Optional[mn.Color4] = None) -> None:
def draw_region(self, region: Any, color: Optional[mn.Color4] = None) -> None:
"""
Draw a region wireframe.
"""
if self.region_mesh_lines is None:
self.get_region_meshes()

if color is None:
color = mn.Color4.magenta()

if region_id in self.region_mesh_lines:
for line in self.region_mesh_lines[region_id]:
self.sim.get_debug_line_render().draw_transformed_line(
line[0],
line[1],
color,
)
debug_line_render = self.sim.get_debug_line_render()
for edge in region.volume_edges:
debug_line_render.draw_transformed_line(
edge[0],
edge[1],
color,
)

def debug_draw(self):
"""
Expand All @@ -285,12 +251,17 @@ def debug_draw(self):
if self.contact_debug_draw:
self.draw_contact_debug()

if len(self.region_colors) == 0:
for region in self.sim.semantic_scene.regions:
self.region_colors[region.id] = mn.Color4(
mn.Vector3(np.random.random(3))
)
for region in self.sim.semantic_scene.regions:
# if region.contains(self.default_agent.get_state().position+mn.Vector3(0,0.3,0)):
# print(region.id)
if region.id in self.region_colors:
self.draw_region(region.id, color=self.region_colors[region.id])
self.draw_region(region.id)
self.draw_region(region, color=self.region_colors[region.id])
# self.draw_region(region)

def draw_event(
self,
Expand Down
4 changes: 4 additions & 0 deletions src/esp/bindings/SceneBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ void initSceneBindings(py::module& m) {
&SemanticRegion::getPolyLoopPoints,
"The points making up the polyloop for this "
"region, coplanar and parallel to the floor.")
.def_property_readonly(
"volume_edges", &SemanticRegion::getVisEdges,
"The edges, as pairs of points, that determine "
"the boundaries of the region. For visualizations.")
.def_property_readonly("floor_height", &SemanticRegion::getFloorHeight,
"The height above the x-z plane for the floor of "
"the semantic region.")
Expand Down
37 changes: 30 additions & 7 deletions src/esp/scene/SemanticScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,39 @@ bool SemanticScene::
const Mn::Vector3 max = regionInstance->getMaxBounds();
regionPtr->setBBox(min, max);
// Set polyloop points and precalc polyloop edge vectors
const std::vector<Mn::Vector3> loopPoints =
const std::vector<Mn::Vector3>& loopPoints =
regionInstance->getPolyLoop();

std::size_t numPts = loopPoints.size();
regionPtr->polyLoopPoints_ = std::vector<Mn::Vector2>(numPts);
regionPtr->visEdges_ =
std::vector<std::vector<Mn::Vector3>>(4 * numPts);

// Save points and edges
int eIdx = 0;
for (std::size_t i = 0; i < numPts; ++i) {
Mn::Vector2 pt = {loopPoints[i].x(), loopPoints[i].z()};
regionPtr->polyLoopPoints_[i] = pt;
Mn::Vector3 currPoint = loopPoints[i];
regionPtr->polyLoopPoints_[i] = {currPoint.x(), currPoint.z()};
// Edges
Mn::Vector3 currExtPt = {
currPoint.x(), static_cast<float>(regionPtr->extrusionHeight_),
currPoint.z()};

std::size_t nextIdx = ((i + 1) % numPts);
Mn::Vector3 nextPoint = loopPoints[nextIdx];
Mn::Vector3 nextExtPt = {
nextPoint.x(), static_cast<float>(regionPtr->extrusionHeight_),
nextPoint.z()};
// Horizontal edge
regionPtr->visEdges_[eIdx++] = {currPoint, nextPoint};
// Vertical edge
regionPtr->visEdges_[eIdx++] = {currPoint, currExtPt};
// Extruded horizontal edge
regionPtr->visEdges_[eIdx++] = {currExtPt, nextExtPt};
// Diagonal edge
regionPtr->visEdges_[eIdx++] = {currPoint, nextExtPt};
}

scene.regions_.emplace_back(std::move(regionPtr));
}
} else { // if semantic attributes specifes region annotations
Expand All @@ -199,13 +222,13 @@ bool SemanticRegion::contains(const Mn::Vector3& pt) const {
return (y < y0) != (y < y1) && (x < x0 + interp * (x1 - x0));
};

// First check height
if ((pt.y() < floorHeight_) || (pt.y() > (floorHeight_ + extrusionHeight_))) {
// First check bbox
if (!bbox_.contains(Mn::EigenIntegration::cast<vec3f>(pt))) {
return false;
}

// next check bbox
if (!bbox_.contains(Mn::EigenIntegration::cast<vec3f>(pt))) {
// Next check height
if ((pt.y() < floorHeight_) || (pt.y() > (floorHeight_ + extrusionHeight_))) {
return false;
}

Expand Down
14 changes: 12 additions & 2 deletions src/esp/scene/SemanticScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,13 @@ class SemanticRegion {
return polyLoopPoints_;
}

/**
* @brief Return a
*/
const std::vector<std::vector<Mn::Vector3>>& getVisEdges() const {
return visEdges_;
}

double getExtrusionHeight() const { return extrusionHeight_; }

double getFloorHeight() const { return floorHeight_; }
Expand All @@ -505,14 +512,17 @@ class SemanticRegion {

std::string name_;

// Extrusion-based regions
// Height of extrusion for Extruded poly-loop-based volumes
double extrusionHeight_{};
// Floor height
double floorHeight_{};

// poly loop points
// poly loop points for base extrusion
std::vector<Mn::Vector2> polyLoopPoints_;

// Edges for visualization of volume
std::vector<std::vector<Mn::Vector3>> visEdges_;

std::vector<std::shared_ptr<SemanticObject>> objects_;
std::shared_ptr<SemanticLevel> level_;
friend SemanticScene;
Expand Down

0 comments on commit b8b3e44

Please sign in to comment.