This is an example where we want to write a simple processor that traverses the MESH scene graph and prints some basic informations which are the matrix transformations to the world coordinates as well as the list of points (in the world coordinates) for a MESH::Mesh
.
We store the matrix stack to enable the transformation of the vertices in the world coordinates.
We define three enter functions for visited nodes. enterTransform
and leaveX3DGroupingNode
push and pop the transform matrices. enterMesh
prints the informations.
This is the facade of the processor which aggregates the visitors of the different components.
#ifndef MESHTRANSFORMCOMPUTERGLOBALVARIABLES_H #define MESHTRANSFORMCOMPUTERGLOBALVARIABLES_H #include <X3DTK/kernel.h> #include <list> namespace X3DTK { namespace MESH { class MeshTransformComputerGlobalVariables : public GlobalVariables { public: MeshTransformComputerGlobalVariables(); virtual ~MeshTransformComputerGlobalVariables(); void init(); void finish(); void pushMatrix(const SFMatrix34f &transformation); void popMatrix(); SFMatrix34f getMatrix() const {return _matrixStack.front();}; private: std::list<SFMatrix34f> _matrixStack; }; } } #endif
#include "MeshTransformComputerGlobalVariables.h" namespace X3DTK { namespace MESH { MeshTransformComputerGlobalVariables::MeshTransformComputerGlobalVariables() : GlobalVariables() { } MeshTransformComputerGlobalVariables::~MeshTransformComputerGlobalVariables() { } void MeshTransformComputerGlobalVariables::init() { _matrixStack.push_front(identity34()); } void MeshTransformComputerGlobalVariables::finish() { _matrixStack.clear(); } void MeshTransformComputerGlobalVariables::pushMatrix(const SFMatrix34f &transformation) { _matrixStack.push_front(_matrixStack.front()*transformation); } void MeshTransformComputerGlobalVariables::popMatrix() { _matrixStack.pop_front(); } } }
#ifndef MESHTRANSFORMCOMPUTERCOREVISITOR_H #define MESHTRANSFORMCOMPUTERCOREVISITOR_H #include <X3DTK/meshscenegraph.h> #include "MeshTransformComputerGlobalVariables.h" namespace X3DTK { namespace MESH { class X3DGroupingNode; class Transform; class MeshTransformComputerCoreVisitor : public CoreVisitor { public: MeshTransformComputerCoreVisitor(); virtual ~MeshTransformComputerCoreVisitor(); virtual void enterMesh(Mesh *M) const; virtual void enterTransform(Transform *T) const; virtual void leaveX3DGroupingNode(X3DGroupingNode *N) const; protected: MeshTransformComputerGlobalVariables *globalVariables; }; } } #endif
#include "MeshTransformComputerCoreVisitor.h" #include <iostream> using namespace std; namespace X3DTK { namespace MESH { MeshTransformComputerCoreVisitor::MeshTransformComputerCoreVisitor() { // Enter functions. defineNewEnterFunction(&MeshTransformComputerCoreVisitor::enterMesh); defineNewEnterFunction(&MeshTransformComputerCoreVisitor::enterTransform); // Leave functions. defineNewLeaveFunction(&MeshTransformComputerCoreVisitor::leaveX3DGroupingNode); // GlobalVariables assignation. globalVariables = GVManager::getInstanceOf<MeshTransformComputerGlobalVariables>(); } MeshTransformComputerCoreVisitor::~MeshTransformComputerCoreVisitor() { } void MeshTransformComputerCoreVisitor::enterMesh(Mesh *M) const { cout << "enter Mesh" << endl; cout << "Transform:" << endl << globalVariables->getMatrix() << endl; cout << " number of vertices = " << M->getVertices().size() << endl; for (Mesh::MVertex::const_iterator it = M->getVertices().begin(); it != M->getVertices().end(); ++it) cout << (*it)->data().getPoint() << endl; } void MeshTransformComputerCoreVisitor::enterTransform(Transform *T) const { globalVariables->pushMatrix(T->getTransform()); } void MeshTransformComputerCoreVisitor::leaveX3DGroupingNode(X3DGroupingNode *N) const { globalVariables->popMatrix(); } } }
#ifndef MESHTRANSFORMCOMPUTER_H #define MESHTRANSFORMCOMPUTER_H #include "MeshTransformComputerGlobalVariables.h" #include <X3DTK/kernel.h> #include <X3DTK/meshscenegraph.h> namespace X3DTK { namespace MESH { class MeshTransformComputer : public X3DOnePassProcessor { public: MeshTransformComputer(); virtual ~MeshTransformComputer(); virtual void print(SFNode N); protected: MeshTransformComputerGlobalVariables *globalVariables; }; } } #endif
#include "MeshTransformComputer.h" #include "MeshTransformComputerCoreVisitor.h" namespace X3DTK { namespace MESH { MeshTransformComputer::MeshTransformComputer() { globalVariables = GVManager::getInstanceOf<MeshTransformComputerGlobalVariables>(); graphTraversal = new DFSGraphTraversal(); graphTraversal->setComponentVisitor(new MeshTransformComputerCoreVisitor()); } MeshTransformComputer::~MeshTransformComputer() { delete graphTraversal; } void MeshTransformComputer::print(SFNode N) { globalVariables->init(); graphTraversal->traverse(N); globalVariables->finish(); } } }
#include "MeshTransformComputer.h" #include <X3DTK/x3dscenegraph.h> #include <X3DTK/meshbuilder.h> #include <iostream> using namespace X3DTK; using namespace std; int main(int argc, char *argv[]) { if (argc <= 1) { cerr << "usage: meshtransformcomputer input" << endl; exit(0); } // DefaultLoader to load the default X3D Nodes. X3D::Loader *loader = new X3D::Loader(); // Instanciation of the new MeshBuilder. X3D::MeshBuilder *meshbuilder = new X3D::MeshBuilder(); // Instanciation of the new MeshTransformComputer. MESH::MeshTransformComputer *mtc = new MESH::MeshTransformComputer(); // Loads the scene. X3D::Scene *s = loader->load(argv[1], false); MESH::Scene *ms = meshbuilder->build(s); mtc->print(ms); return 1; }