Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

meshTransformComputer

introduction

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.

MESH::MeshTransformComputerGlobalVariables

We store the matrix stack to enable the transformation of the vertices in the world coordinates.

MESH::MeshTransformComputerCoreVisitor

We define three enter functions for visited nodes. enterTransform and leaveX3DGroupingNode push and pop the transform matrices. enterMesh prints the informations.

MESH::MeshTransformComputer

This is the facade of the processor which aggregates the visitors of the different components.

code

MeshTransformComputerGlobalVariables.h




#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

MeshTransformComputerGlobalVariables.cpp

#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();
}

}
}

MeshTransformComputerCoreVisitor.h




#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

MeshTransformComputerCoreVisitor.cpp

#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();
}

}
}

MeshTransformComputer.h




#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

MeshTransformComputer.cpp

#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();
}

}
}

main.cpp

#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;
}


Generated on Thu Oct 9 13:50:57 2003 for X3DToolKit by doxygen1.2.18