Discussion:
[osg-users] RayIntersector with KdTree causes a crash
Ale Maro
2018-08-29 08:17:08 UTC
Permalink
Hi Robert,

sorry for asking again. Did you have time to take a look to my example?

Thank you.

Ale

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74616#74616
Ale Maro
2018-08-29 08:18:17 UTC
Permalink
Hi Robert,

sorry for asking again. Did you have time to take a look to my example?

Thank you.

Ale

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74617#74617
Robert Osfield
2018-08-29 08:20:03 UTC
Permalink
Post by Ale Maro
sorry for asking again. Did you have time to take a look to my example?
Not yet. Still busy wrapping up the last week of the Exploration Phase
of the VSG project. Next week I'll spend time on the OSG side and
will have a look.

Robert.
Robert Osfield
2018-09-03 10:04:31 UTC
Permalink
Hi Ale,

I have now built and run your test and it works fine for me.

I have run with the --relative-camera-scene command line option and I
don't see any cube or sphere, but I do see reports of hits.

This is testing with the 3.6 branch under Kubuntu 18.04.

At this point I don't have anything to go any further. Ideally I need
a test case that I can reproduce a problem, and if this isn't straight
forward then I need stack traces of what is happening on your system.

Cheers,
Robert,
Ale Maro
2018-09-03 13:41:25 UTC
Permalink
Hi Robert,

thanks for the update.

Have you built in release?
We are seeing now that the release version has the behaviour you described (that is also strange becouse I expect to see something… if you do not build the KdTree in the code you can see the sphere).

The debug version crashes (the assert Windows says there is a call to front() of an empty vector).

It is correct you run with the --relative-camera-scene command line option.

If needed I can send you the Windows binaries of the example.


Cheers,
Ale

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74632#74632
Robert Osfield
2018-09-03 15:35:25 UTC
Permalink
Hi Ale,

I built with release.

I will try with a debug build. A change in behaviour between release
a debug would suggest an unintialized variable.

Could you provide the stack trace.

A binary for Windows isn't required at this point.

Cheers,
Robert.
Post by Ale Maro
Hi Robert,
thanks for the update.
Have you built in release?
We are seeing now that the release version has the behaviour you described (that is also strange becouse I expect to see something… if you do not build the KdTree in the code you can see the sphere).
The debug version crashes (the assert Windows says there is a call to front() of an empty vector).
It is correct you run with the --relative-camera-scene command line option.
If needed I can send you the Windows binaries of the example.
Cheers,
Ale
------------------
http://forum.openscenegraph.org/viewtopic.php?p=74632#74632
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Ale Maro
2018-09-04 07:06:47 UTC
Permalink
Hi Robert,


I attached the stack trace for the modified osgPick example.

We analyzed the LineSegmentIntersector::intersect code (see below) and we saw that if we build KDTree AND the transofmation matrix has negative scale then "geometry->getVertexArray()" returns an empty array and "settings._vertices" becomes empty.
In debug an assert stops the execution (see stack trace), in release the execution continue but the geometry is not shown


Code:

void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable,
const osg::Vec3d& s, const osg::Vec3d& e)
{
if (reachedLimit()) return;

LineSegmentIntersectorUtils::Settings settings;
settings._lineSegIntersector = this;
settings._iv = &iv;
settings._drawable = drawable;
settings._limitOneIntersection = (_intersectionLimit == LIMIT_ONE_PER_DRAWABLE || _intersectionLimit == LIMIT_ONE);

osg::Geometry* geometry = drawable->asGeometry();
if (geometry)
{
settings._vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
}






Cheers,
Ale

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74635#74635




Attachments:
http://forum.openscenegraph.org//files/stacktrace_370.txt
Robert Osfield
2018-09-04 14:38:35 UTC
Permalink
Hi Ale,

I have invested the issue further, focusing on why the sphere appears
fine when the KdTree isn't build, but disappears when it is. I
tracked this issue down to the ShapeDrawable::setShape() being invoked
by the KdTreeBuilder that creates a KdTree for a ShapeDrawable then
assigns this KdTree to the ShapeDrawable via the Drawable::setShape()
method which in turn invokes the ShapeDrawable::build() method that
attempts to build a geometry from the KdTree. It's a bit of circular
reference - KdTree wasn't ever meant to work with ShapeDrawable, it's
only now working because of the change of ShapeDrawable from
subclassing from Drawable to subclassing from Geometry.

To prevent the unintended rebuild I have disable the
ShapeDrawable::build() from doing anything when the shape assigned is
a KdTree.

https://github.com/openscenegraph/OpenSceneGraph/commit/5f9c6a01704e26e6760cc023e09ab1dcf0da1a15

There is still a bit of mismatch in that KdTree is a special case of a
Shape, but still a valid use. Shape's were always intended to be used
for intersection testing and physics, but never originally intended to
support a ShapeDrawable. The ShapeDrawable was a quick and easy thing
to add to help visualize primitive shapes but in hindsight I should
never have implemented it as it's misused too much by the community as
a serious means for drawing objects. It's not possible to remove
ShapeDrawable without breaking lots of user applications so it's still
part of the OSG. Please consider it just there for backwards
compatibility.

The above change is now checked into master and the OpenSceneGraph-3.6
branch. so it will be part of the 3.6.3 release I'll make this month.

Testing of these changes would be appreciated.

Robert.
Ale Maro
2018-09-05 13:51:37 UTC
Permalink
Hi Robert,

thanks for the update.
The issue with ShapeDrawable take us to the wrong way.

So at the end we find the cause of the problem is different.
I will describe it again:

1 - Take a geode (e.g. we loaded "cow.osg") and build a kdtree
2 - Make a clone with DEEP_COPY_ALL
3 - Applying LineSegmentIntersection to the clone we get the crash

If we clone with SHALLOW_COPY it works fine

Attached is the modified osgPick example that reproduces the crash.
You just need to set the correct path to the file to be loaded and specify the --relative-camera-scene option as before

Here is an extract of the code. If you use a SHALLOW_COPY instead of DEEP_COPY_ALL it does not crash.

[code]
osg::MatrixTransform* xform = new osg::MatrixTransform();
osg::MatrixTransform* xform2 = new osg::MatrixTransform(osg::Matrix::translate(10.0,0.0,0.));

osg::ref_ptr<osg::KdTreeBuilder> kdtreeBuilder = NULL;
kdtreeBuilder = osgDB::Registry::instance()->getKdTreeBuilder()->clone();

xform->addChild(cow);
// xform->accept(*kdtreeBuilder);

osg::Object * cow2 = cow->clone(osg::CopyOp::DEEP_COPY_ALL);

xform2->addChild(cow2->asNode());

group->addChild(xform);
group->addChild(xform2);
[\code]




Cheers,
Ale

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74652#74652




Attachments:
http://forum.openscenegraph.org//files/osgpick_not_working_865.cpp
Robert Osfield
2018-09-05 17:07:40 UTC
Permalink
Hi Ale,

Thanks for the updating test program, this illustrates a separate bug
in the KdTree copy constructor. I have added missing copying of the
indices:

~/OpenSceneGraph/src/osg$ git diff
diff --git a/src/osg/KdTree.cpp b/src/osg/KdTree.cpp
index 709c12f99..3f7a4a34a 100644
--- a/src/osg/KdTree.cpp
+++ b/src/osg/KdTree.cpp
@@ -492,6 +492,8 @@ KdTree::KdTree(const KdTree& rhs, const
osg::CopyOp& copyop):
Shape(rhs, copyop),
_degenerateCount(rhs._degenerateCount),
_vertices(rhs._vertices),
+ _primitiveIndices(rhs._primitiveIndices),
+ _vertexIndices(rhs._vertexIndices),
_kdNodes(rhs._kdNodes)
{
}

Could you test out the commit?

https://github.com/openscenegraph/OpenSceneGraph/commit/34d58d447d7f0b35d7bb20572a2f5d4fd26b7961

Cheers.
Robert.
Ale Maro
2018-09-06 06:41:24 UTC
Permalink
Hi Robert,

now it works fine! Thank you very much for the update.

Cheers,
Ale

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74672#74672

Loading...