Skip to content

Commit 6d7a335

Browse files
Merge pull request #320 from vsg-dev/PolytopeIntersector
Added vsg::PolytopeIntersector tests to vsgintersection example
2 parents 1b818aa + 0c79763 commit 6d7a335

1 file changed

Lines changed: 72 additions & 8 deletions

File tree

examples/utils/vsgintersection/vsgintersection.cpp

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class IntersectionHandler : public vsg::Inherit<vsg::Visitor, IntersectionHandle
2828
ellipsoidModel(in_ellipsoidModel),
2929
scale(in_scale)
3030
{
31+
geom.cullNode = true;
3132
builder->verbose = verbose;
3233
if (scale > 10.0) scale = 10.0;
3334
}
@@ -36,7 +37,7 @@ class IntersectionHandler : public vsg::Inherit<vsg::Visitor, IntersectionHandle
3637
{
3738
if (lastPointerEvent)
3839
{
39-
intersection(*lastPointerEvent);
40+
intersection_LineSegmentIntersector(*lastPointerEvent);
4041
if (!lastIntersection) return;
4142

4243
vsg::info("keyPress.keyModifier = ", keyPress.keyModifier, " keyPress.keyBase = ", keyPress.keyBase);
@@ -86,10 +87,6 @@ class IntersectionHandler : public vsg::Inherit<vsg::Visitor, IntersectionHandle
8687
{
8788
scenegraph->addChild(builder->createCone(geom, state));
8889
}
89-
else if (keyPress.keyBase == 'o')
90-
{
91-
vsg::write(scenegraph, "builder.vsgt");
92-
}
9390
}
9491

9592
if (state.billboard)
@@ -98,6 +95,11 @@ class IntersectionHandler : public vsg::Inherit<vsg::Visitor, IntersectionHandle
9895
state.billboard = false;
9996
geom.positions = {};
10097
}
98+
99+
if (keyPress.keyBase == 'o')
100+
{
101+
vsg::write(scenegraph, "builder.vsgt");
102+
}
101103
}
102104

103105
void apply(vsg::ButtonPressEvent& buttonPressEvent) override
@@ -106,7 +108,11 @@ class IntersectionHandler : public vsg::Inherit<vsg::Visitor, IntersectionHandle
106108

107109
if (buttonPressEvent.button == 1)
108110
{
109-
intersection(buttonPressEvent);
111+
intersection_LineSegmentIntersector(buttonPressEvent);
112+
}
113+
else if (buttonPressEvent.button == 2)
114+
{
115+
intersection_PolytopeIntersector(buttonPressEvent);
110116
}
111117
}
112118

@@ -115,12 +121,12 @@ class IntersectionHandler : public vsg::Inherit<vsg::Visitor, IntersectionHandle
115121
lastPointerEvent = &pointerEvent;
116122
}
117123

118-
void intersection(vsg::PointerEvent& pointerEvent)
124+
void intersection_LineSegmentIntersector(vsg::PointerEvent& pointerEvent)
119125
{
120126
auto intersector = vsg::LineSegmentIntersector::create(*camera, pointerEvent.x, pointerEvent.y);
121127
scenegraph->accept(*intersector);
122128

123-
if (verbose) std::cout << "intersection(" << pointerEvent.x << ", " << pointerEvent.y << ") " << intersector->intersections.size() << ")" << std::endl;
129+
if (verbose) std::cout << "intersection_LineSegmentIntersector(" << pointerEvent.x << ", " << pointerEvent.y << ") " << intersector->intersections.size() << ")" << std::endl;
124130

125131
if (intersector->intersections.empty()) return;
126132

@@ -171,6 +177,63 @@ class IntersectionHandler : public vsg::Inherit<vsg::Visitor, IntersectionHandle
171177
lastIntersection = intersector->intersections.front();
172178
}
173179

180+
void intersection_PolytopeIntersector(vsg::PointerEvent& pointerEvent)
181+
{
182+
double size = 5.0;
183+
double xMin = static_cast<double>(pointerEvent.x) - size;
184+
double xMax = static_cast<double>(pointerEvent.x) + size;
185+
double yMin = static_cast<double>(pointerEvent.y) - size;
186+
double yMax = static_cast<double>(pointerEvent.y) + size;
187+
188+
auto intersector = vsg::PolytopeIntersector::create(*camera, xMin, yMin, xMax, yMax);
189+
scenegraph->accept(*intersector);
190+
191+
if (verbose) std::cout << "intersection_PolytopeIntersector(" << pointerEvent.x << ", " << pointerEvent.y << ") " << intersector->intersections.size() << ")" << std::endl;
192+
193+
if (intersector->intersections.empty()) return;
194+
195+
for (auto& intersection : intersector->intersections)
196+
{
197+
if (verbose) std::cout << "intersection = world(" << intersection->worldIntersection << "), instanceIndex " << intersection->instanceIndex;
198+
199+
if (ellipsoidModel)
200+
{
201+
std::cout.precision(10);
202+
auto location = ellipsoidModel->convertECEFToLatLongAltitude(intersection->worldIntersection);
203+
if (verbose) std::cout << " lat = " << location[0] << ", long = " << location[1] << ", height = " << location[2];
204+
}
205+
206+
if (lastIntersection)
207+
{
208+
if (verbose) std::cout << ", distance from previous intersection = " << vsg::length(intersection->worldIntersection - lastIntersection->worldIntersection);
209+
}
210+
211+
if (verbose)
212+
{
213+
std::string name;
214+
for (auto& node : intersection->nodePath)
215+
{
216+
std::cout << ", " << node->className();
217+
if (node->getValue("name", name)) std::cout << ":name=" << name;
218+
}
219+
220+
std::cout << ", Arrays[ ";
221+
for (auto& array : intersection->arrays)
222+
{
223+
std::cout << array << " ";
224+
}
225+
std::cout << "] [";
226+
for (auto& index : intersection->indices)
227+
{
228+
std::cout << index<<" ";
229+
}
230+
std::cout << "]";
231+
232+
std::cout << std::endl;
233+
}
234+
}
235+
}
236+
174237
protected:
175238
vsg::ref_ptr<vsg::PointerEvent> lastPointerEvent;
176239
vsg::ref_ptr<vsg::LineSegmentIntersector::Intersection> lastIntersection;
@@ -229,6 +292,7 @@ int main(int argc, char** argv)
229292
if (scene->children.empty())
230293
{
231294
vsg::GeometryInfo info;
295+
info.cullNode = true;
232296
info.dx.set(100.0f, 0.0f, 0.0f);
233297
info.dy.set(0.0f, 100.0f, 0.0f);
234298
info.dz.set(0.0f, 0.0f, 100.0f);

0 commit comments

Comments
 (0)