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 MeshTransformComputerStateVariables : public StateVariables { public: MeshTransformComputerStateVariables(); virtual ~MeshTransformComputerStateVariables(); void init(); void finish(); void pushMatrix(const SFMatrix34f &transformation); void popMatrix(); SFMatrix34f getMatrix() const {return _matrixStack.front();}; private: std::list<SFMatrix34f> _matrixStack; }; } } #endif
#include "MESH_TransformComputerStateVariables.h" namespace X3DTK { namespace MESH { MeshTransformComputerStateVariables::MeshTransformComputerStateVariables() : StateVariables() { } MeshTransformComputerStateVariables::~MeshTransformComputerStateVariables() { } void MeshTransformComputerStateVariables::init() { _matrixStack.push_front(SFMatrix34f::identity); } void MeshTransformComputerStateVariables::finish() { _matrixStack.clear(); } void MeshTransformComputerStateVariables::pushMatrix(const SFMatrix34f &transformation) { _matrixStack.push_front(_matrixStack.front()*transformation); } void MeshTransformComputerStateVariables::popMatrix() { _matrixStack.pop_front(); } } }
#ifndef MESHTRANSFORMCOMPUTERCOREVISITOR_H #define MESHTRANSFORMCOMPUTERCOREVISITOR_H #include <X3DTK/mesh_scenegraph.h> #include "MESH_TransformComputerStateVariables.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: MeshTransformComputerStateVariables *stateVariables; }; } } #endif
#include "MESH_TransformComputerCoreVisitor.h" #include <iostream> using namespace std; namespace X3DTK { namespace MESH { MeshTransformComputerCoreVisitor::MeshTransformComputerCoreVisitor() { // Enter functions. defineEnterFunction(&MeshTransformComputerCoreVisitor::enterMesh); defineEnterFunction(&MeshTransformComputerCoreVisitor::enterTransform); // Leave functions. defineLeaveFunction(&MeshTransformComputerCoreVisitor::leaveX3DGroupingNode); // StateVariables assignation. stateVariables = GraphTraversal::getInstanceOf<MeshTransformComputerStateVariables>(); } MeshTransformComputerCoreVisitor::~MeshTransformComputerCoreVisitor() { } void MeshTransformComputerCoreVisitor::enterMesh(Mesh *M) const { cout << "enter Mesh" << endl; cout << "Transform:" << endl << stateVariables->getMatrix() << endl; cout << " number of vertices = " << M->getVertices().size() << endl; for (Mesh::MFVertex::const_iterator it = M->getVertices().begin(); it != M->getVertices().end(); ++it) cout << (*it)->data().getPoint() << endl; } void MeshTransformComputerCoreVisitor::enterTransform(Transform *T) const { stateVariables->pushMatrix(T->getTransform()); } void MeshTransformComputerCoreVisitor::leaveX3DGroupingNode(X3DGroupingNode *N) const { stateVariables->popMatrix(); } } }
#ifndef MESHTRANSFORMCOMPUTER_H #define MESHTRANSFORMCOMPUTER_H #include "MESH_TransformComputerStateVariables.h" #include <X3DTK/kernel.h> #include <X3DTK/mesh_scenegraph.h> namespace X3DTK { namespace MESH { class MeshTransformComputer : public X3DOnePassProcessor { public: MeshTransformComputer(); virtual ~MeshTransformComputer(); virtual void print(SFNode N); protected: MeshTransformComputerStateVariables *stateVariables; }; } } #endif
#include "MESH_TransformComputer.h" #include "MESH_TransformComputerCoreVisitor.h" namespace X3DTK { namespace MESH { MeshTransformComputer::MeshTransformComputer() { stateVariables = GraphTraversal::getInstanceOf<MeshTransformComputerStateVariables>(); graphTraversal = new DFSGraphTraversal(); graphTraversal->setComponentVisitor(new MeshTransformComputerCoreVisitor()); } MeshTransformComputer::~MeshTransformComputer() { GraphTraversal::removeInstanceOf<MeshTransformComputerStateVariables>(); delete graphTraversal; } void MeshTransformComputer::print(SFNode N) { stateVariables->init(); graphTraversal->traverse(N); stateVariables->finish(); } } }
#include "MESH_TransformComputer.h" #include <X3DTK/x3d_scenegraph.h> #include <X3DTK/x3d_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 = X3DLoader::getInstanceOf<X3D::Loader>(); // Instanciation of the new MeshBuilder. X3D::MeshBuilder *meshbuilder = X3DProcessor::getInstanceOf<X3D::MeshBuilder>(); // Instanciation of the new MeshTransformComputer. MESH::MeshTransformComputer *mtc = X3DProcessor::getInstanceOf<MESH::MeshTransformComputer>(); // Loads the scene. X3D::Scene *s = loader->load(argv[1], false); MESH::Scene *ms = meshbuilder->build(s); // Prints the scene. mtc->print(ms); // removes all instances. X3DProcessor::removeAllInstances(); X3DLoader::removeInstanceOf<X3D::Loader>(); return 1; }