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

myStructureComputer

introduction

This is an example where we want to fill a structure called MySimpleMesh containing a list of vertices in the world coordinates and a list of faces, each of it being represented by a list of vertex indexes. We define a processor over the MESH scene graph, that transforms all the visited vertices into the world coordinates and stores the indexes of the faces.

MySimpleMesh

We define the necessary methods to fill the structure.

MESH::MyStructureComputerStateVariables

We store a pointer to the MySimpleMesh structure as well as the matrix stack to enable the transformation of the vertices in the world coordinates.

MESH::MyStructureComputerCoreVisitor

We define three enter functions for visited nodes. enterTransform and leaveX3DGroupingNode push and pop the transform matrices. enterMesh fills the MySimpleMesh structure thanks to the MESH::MyStructureComputerStateVariables access.

MESH::MyStructureComputer

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

code

MySimpleMesh.h




#ifndef MYSIMPLEMESH_H
#define MYSIMPLEMESH_H

#include <vector>
#include <list>

typedef struct _Vertex
{
  float x;
  float y;
  float z;
} Vertex;

typedef std::list<unsigned int> Face;

class MySimpleMesh
{
public:
  MySimpleMesh();
  ~MySimpleMesh();
  
  void addVertex(float x, float y, float z);
  void addFace(const std::list<unsigned int> &indexes);
  void print() const;
  
  inline std::vector<Vertex> &getVertexArray() {return _vertexArray;};
  inline std::vector<Face> &getIndexArray() {return _indexArray;};
  
private:
  std::vector<Vertex> _vertexArray;
  std::vector<Face> _indexArray;
};

#endif

MySimpleMesh.cpp

#include "MySimpleMesh.h"

#include <iostream>

using namespace std;

MySimpleMesh::MySimpleMesh()
{
}

MySimpleMesh::~MySimpleMesh()
{
}
  
void MySimpleMesh::addVertex(float x, float y, float z)
{
  Vertex v;
  v.x = x;
  v.y = y;
  v.z = z;
  
  _vertexArray.push_back(v);
}

void MySimpleMesh::addFace(const std::list<unsigned int> &indexes)
{
  _indexArray.push_back(indexes);
}

void MySimpleMesh::print() const
{
  cout << "Vertices: " << endl;
  for (vector<Vertex>::const_iterator it = _vertexArray.begin(); it != _vertexArray.end(); ++it)
    cout << "  " << (*it).x << ", " << (*it).y << ", " << (*it).z << endl;
  
  cout << "Indexes: " << endl;
  for (vector<Face>::const_iterator it = _indexArray.begin(); it != _indexArray.end(); ++it)
  {
    Face face = *it;
    cout << "  ";
    for (Face::const_iterator itIndex = face.begin(); itIndex != face.end(); ++itIndex)
      cout << *itIndex << " ";
    cout << endl;
  }
}

MESH_MyStructureComputerStateVariables.h




#ifndef MYSTRUCTURECOMPUTERGLOBALVARIABLES_H
#define MYSTRUCTURECOMPUTERGLOBALVARIABLES_H

#include "MySimpleMesh.h"

#include <X3DTK/kernel.h>

#include <list>

namespace X3DTK {
namespace MESH {


class MyStructureComputerStateVariables : public StateVariables
{
public:
  MyStructureComputerStateVariables();
  virtual ~MyStructureComputerStateVariables();

  void init();
  void finish();

  void beginNewMesh();
  inline void addVertex(float x, float y, float z) {_mesh->addVertex(x, y, z);};
  void addFace(const Face &face);
  void pushMatrix(const SFMatrix34f &transformation);  
  void popMatrix();

  inline MySimpleMesh *getMesh() const {return _mesh;};  
  SFMatrix34f getMatrix() const {return _matrixStack.front();};

private: 
  MySimpleMesh *_mesh;
  unsigned int _decal;
  std::list<SFMatrix34f> _matrixStack;
};

}
}

#endif

MESH_MyStructureComputerStateVariables.cpp

#include "MESH_MyStructureComputerStateVariables.h"

namespace X3DTK {
namespace MESH {

MyStructureComputerStateVariables::MyStructureComputerStateVariables()
: StateVariables(), _mesh(0), _decal(0)
{
}

MyStructureComputerStateVariables::~MyStructureComputerStateVariables()
{
}

void MyStructureComputerStateVariables::init()
{
  _mesh = new MySimpleMesh();
  _matrixStack.push_front(SFMatrix34f::identity);
}

void MyStructureComputerStateVariables::finish()
{
  _matrixStack.clear();
}

void MyStructureComputerStateVariables::beginNewMesh()
{
  _decal = _mesh->getVertexArray().size();
}

void MyStructureComputerStateVariables::addFace(const Face &face)
{
  Face decalFace = face;
  for (Face::iterator it = decalFace.begin(); it != decalFace.end(); ++it)
    *it = _decal + *it;
    
  _mesh->addFace(decalFace);
};

void MyStructureComputerStateVariables::pushMatrix(const SFMatrix34f &transformation)
{
  _matrixStack.push_front(_matrixStack.front()*transformation);
}

void MyStructureComputerStateVariables::popMatrix()
{
  _matrixStack.pop_front();
}

}
}

MESH_MyStructureComputerCoreVisitor.h




#ifndef MYSTRUCTURECOMPUTERCOREVISITOR_H
#define MYSTRUCTURECOMPUTERCOREVISITOR_H

#include <X3DTK/mesh_scenegraph.h>

#include "MESH_MyStructureComputerStateVariables.h"

namespace X3DTK {
namespace MESH {

class X3DGroupingNode;
class Transform;


class MyStructureComputerCoreVisitor : public CoreVisitor
{
public:
  MyStructureComputerCoreVisitor();
  virtual ~MyStructureComputerCoreVisitor();

  virtual void enterMesh(Mesh *M) const;

  virtual void enterTransform(Transform *T) const;

  virtual void leaveX3DGroupingNode(X3DGroupingNode *N) const;

protected:
  MyStructureComputerStateVariables *stateVariables;
};

}
}

#endif

MESH_MyStructureComputerCoreVisitor.cpp

#include "MESH_MyStructureComputerCoreVisitor.h"

#include <iostream>

using namespace std;

namespace X3DTK {
namespace MESH {

MyStructureComputerCoreVisitor::MyStructureComputerCoreVisitor()
{
  // Enter functions.
  defineEnterFunction(&MyStructureComputerCoreVisitor::enterMesh);
  defineEnterFunction(&MyStructureComputerCoreVisitor::enterTransform);
  
  // Leave functions.
  defineLeaveFunction(&MyStructureComputerCoreVisitor::leaveX3DGroupingNode);
  
  // StateVariables assignation.
  stateVariables = GraphTraversal::getInstanceOf<MyStructureComputerStateVariables>();
}

MyStructureComputerCoreVisitor::~MyStructureComputerCoreVisitor()
{
}

void MyStructureComputerCoreVisitor::enterMesh(Mesh *M) const
{
  SFMatrix34f T = stateVariables->getMatrix();

  stateVariables->beginNewMesh();
  
  // filling the vertices.
  for (Mesh::MFVertex::const_iterator it = M->getVertices().begin(); it != M->getVertices().end(); ++it)
  {
    SFPoint3f P = T*(*it)->data().getPoint();
    stateVariables->addVertex(P.x, P.y, P.z);
  }
  
  // filling the faces.
  for (Mesh::MFFace::const_iterator it = M->getFaces().begin(); it != M->getFaces().end(); ++it)
  {
    Mesh::MFEdge edges = (*it)->getEdges();
    list<unsigned int> indexList;
    for (Mesh::MFEdge::const_iterator itEdge = edges.begin(); itEdge != edges.end(); ++itEdge)
      indexList.push_back((*itEdge)->getFromVertex()->getIndex());
    
    stateVariables->addFace(indexList);
  }  
}

void MyStructureComputerCoreVisitor::enterTransform(Transform *T) const
{
  stateVariables->pushMatrix(T->getTransform());
}

void MyStructureComputerCoreVisitor::leaveX3DGroupingNode(X3DGroupingNode *N) const
{
  stateVariables->popMatrix();
}

}
}

MESH_MyStructureComputer.h




#ifndef MYSTRUCTURECOMPUTER_H
#define MYSTRUCTURECOMPUTER_H

#include "MESH_MyStructureComputerStateVariables.h"
#include "MySimpleMesh.h"

#include <X3DTK/kernel.h>
#include <X3DTK/mesh_scenegraph.h>

namespace X3DTK {
namespace MESH {


class MyStructureComputer : public X3DOnePassProcessor
{
public:
  MyStructureComputer();
  virtual ~MyStructureComputer();
  
  virtual MySimpleMesh *compute(SFNode N);
 
protected:
  MyStructureComputerStateVariables *stateVariables;
};

}
}

#endif

MESH_MyStructureComputer.cpp

#include "MESH_MyStructureComputer.h"
#include "MESH_MyStructureComputerCoreVisitor.h"

namespace X3DTK {
namespace MESH {

MyStructureComputer::MyStructureComputer()
{
  stateVariables = GraphTraversal::getInstanceOf<MyStructureComputerStateVariables>();
    
  graphTraversal = new DFSGraphTraversal();
  graphTraversal->setComponentVisitor(new MyStructureComputerCoreVisitor());
}

MyStructureComputer::~MyStructureComputer()
{
  GraphTraversal::removeInstanceOf<MyStructureComputerStateVariables>();

  delete graphTraversal;
}

MySimpleMesh *MyStructureComputer::compute(SFNode N)
{
  stateVariables->init();
  graphTraversal->traverse(N);
  stateVariables->finish();
  
  return stateVariables->getMesh();
}

}
}

main.cpp

#include "MESH_MyStructureComputer.h"

#include <X3DTK/x3d_scenegraph.h>
#include <X3DTK/x3d_meshbuilder.h>
#include <X3DTK/memreleaser.h>

#include <iostream>

using namespace X3DTK;
using namespace std;

int main(int argc, char *argv[])
{
  if (argc <= 1)
  {
    cerr << "usage: MyStructureComputer 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 MyStructureComputer.
  MESH::MyStructureComputer *msc = X3DProcessor::getInstanceOf<MESH::MyStructureComputer>();  
  
  // Loads the scene.
  X3D::Scene *s = loader->load(argv[1], false);
  MESH::Scene *ms = meshbuilder->build(s);
  MySimpleMesh *mesh = msc->compute(ms);  
 
  // prints the content.
  mesh->print();
  
  // removes all the instances.
  X3DProcessor::removeAllInstances();
  X3DLoader::removeInstanceOf<X3D::Loader>();
  
  return 1;
}


Generated on Thu Dec 4 13:25:53 2003 for X3DToolKit by doxygen1.2.18