About nodevisitor visitor Mode

Source: Internet
Author: User

By using nodevisitor, OSG implements the gof visitor mode. in OSG implementation, this mode implements dual-distribution. therefore, a virtual method in node is apply (nodevisitor), while a method in nodevisitor is apply (node &). the two methods are called differently. we should always call node: accept instead of nodevisitor: Apply. because only the former can be called to enjoy the dual-distribution capability. (the specific types of node and the specific types of nodevisitor are involved in the visitor mode)

Then we can see from the node: accept method:

VoidNode::Accept(Nodevisitor&NV){If(NV.Validnodemask(*This)){NV.Pushontonodepath(This);NV.Apply(*This);NV.Popfromnodepath();}}

This method is simple,

1) First, NV checks whether the node needs to be passed to the current nodevisitor Based on nodemask.

2) process nodepath to make the currently applied node the last node in nodepath. nodepath will be a sequence from top-level root node to current node.

3) Call apply

4) Restore nodepath

The processing of nodemask and nodepath is involved here. When we learn more about OSG, we will analyze their functions.

We can see that to use nodevisitor to traverse the scenario graph, you only need to define a nodevisitor derivative and then call accept to the viewer node.

The following is a simple example: we write a nodevisitor to filter out non-geode in the system and restore the system to the original geometric world:

1. First define our class howtonodevisitor derived from nodevisitor:

 # Pragma once # include  "Stdafx. H"  Class  Howtonodevisitor : Public  Nodevisitor { Public : Howtonodevisitor ( Void );~ Howtonodevisitor ( Void ); Void  Apply ( Geode & Node ); Group * Getfilterresult (){ Return  Mnodegroup . Get ();} Private : Ref_ptr <Group > Mnodegroup ;};

2. ImplementCodeIt is also extremely simple:

 # Include  "Stdafx. H"  # Include  "Howtonodevisitor. H"  Howtonodevisitor :: Howtonodevisitor ( Void ): Nodevisitor ( Nodevisitor :: Traverse_all_children ){Mnodegroup = New  Group ();} Howtonodevisitor ::~ Howtonodevisitor ( Void ){} Void  Howtonodevisitor :: Apply ( Geode & Node ){ If ( This -> Mnodegroup -> Containsnode (& Node )){ Return ;} Else { This -> Mnodegroup -> Addchild (& Node );}}

3. the following code is used:

 # Include  "Stdafx. H"  # Include  "Howtonodevisitor. H"  Int _ Tmain ( Int  Argc , _ Tchar * Argv []) { Viewer * Viewer = New  Viewer (); // Node * rootnode = osgdb: readnodefile ("cow. OSG"); // node * rootnode = osgdb: readnodefile ("axes. OSG ");  Node * Rootnode = Osgdb ::Readnodefile ( "Spaceship. OSG" ); Howtonodevisitor visitor;    Rootnode->Accept(Visitor);    Rootnode=Visitor.Getfilterresult();    Viewer -> Setscenedata ( Rootnode ); Viewer -> Addeventhandler (New  Osgga :: Statesetmanipulator ( Viewer -> Getcamera ()-> Getorcreatestateset ())); Viewer -> Addeventhandler ( New  Statshandler ()); Viewer -> Realize (); Return Viewer -> Run ();}

If you comment out the three lines of code with underscores, you will see a space shuttle. however, after our code is added, the plane will not be sprayed. because the spark injection is not directly implemented by the geode node.

The purpose of this example is to demonstrate the nodevisitor mode. Actually, nodevisitor is not simple, so it is not easy to readSource codeThe class is large.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.