Thanks alot for your help Robert I had become pretty confused but I think I 
understand it now.  The proof will be when I implement something :)
You must have been a pretty strong runner when you were a kid (or maybe still 
are) HW is good for stuff like that even though we are pretty much in Edinburgh 
you can be in the countryside if you go out of the other side of the campus.

All the best

Fred

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Robert Osfield
Sent: 12 March 2010 10:57
To: OpenSceneGraph Users
Subject: Re: [osg-users] Need help to understand this code snippet withaccept 
and operator

Hi Fred,

The TriangleIndexFunctor is a template helper class for making it
easier to access the triangles held in osg::Drawable.  The actual
geometry primitives held within different osg::Drawable subclasses
could be of any type and any arrangement, so casting osg::Drawable to
osg::Geometry etc. and then accessing the primitives directly and then
working out how to interpret the triangles from this is rather
complex, tedious and prone to poor performance unless you are very
careful.

To address the tight coupling of accessors to implementations, the
osg::Drawable has an accept(osg::Drawable::PrimitiveFunctor&) method
exists to allow a subclass from osg::Drawable to pass details on the
geometry primitives that it has to the functor in a generic way - thus
hiding the local implementation details of that Drawable and enabling
your own custom PrimitiveFunctor to work with a wide range of Drawable
without needing to know the implementation details.  While this
achieves good decoupling the PrimitiveFunctor still has to handle all
the different types of primitives - polygons, tri strips, quads, qaud
strips, lines etc, which is still pretty complicated to implement.

To address the complexity of handling all the different types of
primitives the TriangleIndexFunctor template class exists to decompose
all the descriptions of generic primitives into the simple triangles
marked by their corner indices.  For you the app developer all you
then need to do is create you little functor that implements the void
operator()(const int v1, const int v2, const int v3) method as per the
example, and then pass the resulting templated class to the drawable
to get all the triangle information.  The use of templates also
ensures good performance.

If it wasn't for PrimtiiveFunctor and
TriangleFunctor/TriangleIndexFunctor accessing geometry would be very
tedious and error prone task - lots of casts, switch statements and
book keeping.  These helper classes might seem a bit convoluted at
first look, they really make life much easier.  Go have a search
through the OSG code base, there are plenty of examples of
TriangleFunctor/TriangleIndexFunctor in action - especially in
src/osgUtil.

Cheers,
Robert.

ps. I spotted your signature and it brings back memories - I ran in
the Scottish Cross Country Nationals at Herriot-Watt when I was kid
;-)

On Fri, Mar 12, 2010 at 10:01 AM, Fletcher, Craig A <[email protected]> wrote:
> Hi
>
> I was wondering if someone could help explain this piece of code or at least
> point me in the right direction.  It was posted by Yuen Helbig in 2008
>
>
>
> osg::TriangleIndexFunctor<TriangleIndexVisitor> tif;
>
> tempGeode.geode->getDrawable(j)->accept(tif);
>
>
>
> With TriangleIndexVisitor defined as:
>
>
>
> class TriangleIndexVisitor
>
> {
>
> public:
>
>         CArray<int,int> indices;
>
>
>
>         void operator()(const int v1, const int v2, const int v3)
>
>         {
>
>                // toss the computed indices into the indices array
>
>                indices.Add( v1 );
>
>                indices.Add( v2 );
>
>                indices.Add( v3 );
>
>         }
>
> };
>
>
>
> I think I am struggling with the concept of how accept and operator work.
>
>
>
> All the best
>
>
>
> Fred
>
> ________________________________
> Heriot-Watt University is a Scottish charity registered under charity number
> SC000278.
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


-- 
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to