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

infoReader

Introduction

In icosahedronViewer, we defined a new simple X3DTK::X3D::X3DNode that had no child. This is an example on how to create a parent node, meaning a node that can have children. Here we define an X3DTK::X3D::Info node that can contain two nodes, X3DTK::X3D::Price and X3DTK::X3D::Modeler.

Defining X3D::Info

Like a node that can only be a child, the three special methods load, write and clone still have to be redefined. Nevertheless others methods must be redefined. For the X3DTK::X3D::Price child, the methods setPrice and getPrice are defined. Note in setPrice the calls to removeLink and addLink. Four other necessary methods are redefined: setChild, addChild and removeChild, and getChildList. This method returns a list of child nodes. The order of which they appeared is important because, it will be the traversal order.

Defining X3D::Price and X3D::Modeler

These nodes are simple child nodes and are defined like the nodes of the precedent examples.

Defining a new simple processor

To test our new scene graph, we define a new processor called X3DTK::X3D::InfoReader that extracts and displays the content of the nodes X3DTK::X3D::Info, X3DTK::X3D::Price and X3DTK::X3D::Modeler. You can refer to glNormalViewer for an example page of processor creation.

Code

X3D_Info.h

#ifndef INFO_H
#define INFO_H

#include <X3DTK/X3D/scenegraph.h>

namespace X3DTK {
namespace X3D {

class Price;
class Modeler;

// Info node.

class Info : public X3DChildNode
{
public:
  Info();
  Info(const SFString &date,
       SFNode price,
       SFNode modeler);
  virtual ~Info();
  virtual SFNode clone() const;  

  void setDate(const SFString &date);
  void setPrice(Price *price);
  void setModeler(Modeler *modeler);  

  inline const SFString &getDate() const {return _date;};
  inline SFNode getPrice() const {return _price;};
  inline SFNode getModeler() const {return _modeler;};  
  
  virtual MFAbstractNode getChildList() const;
  
  virtual bool addChild(const SFAbstractNode &N);
  virtual bool setChild(const SFAbstractNode &N);
  virtual bool removeChild(const SFAbstractNode &N);
  
  virtual void load(const X3DFileElement *element);
  virtual SFString write() const;

protected:
  Info(const Info &I);

private:
  SFString _date;
  
  SFNode _price;
  SFNode _modeler;
};

}
}

#endif

X3D_Info.cpp

#include "X3D_Info.h"
#include "X3D_Price.h"
#include "X3D_Modeler.h"

namespace X3DTK {
namespace X3D {

Info::Info()
: X3DChildNode(), _date("0/0/0"), _price(0), _modeler(0)
{
  // Defines the tag of the node. This string must be the same than the one 
  // entered for the creation function, otherwise the node won't be loaded.
  defineTypeName("Info");
}

Info::Info(const SFString &date, SFNode price, SFNode modeler)
: X3DChildNode(), _date(date), _price(price), _modeler(modeler)
{
  // Defines the tag of the node.
  defineTypeName("Info");
}

Info::Info(const Info &I)
: X3DChildNode(I), _date(I._date), _price(I._price), _modeler(I._modeler)
{
}

Info::~Info()
{
  removeParentFromChild(this, _price);
  removeParentFromChild(this, _modeler);
}

SFNode Info::clone() const
{
  return new Info(*this);
}

void Info::setDate(const SFString &date)
{
  _date = date;
}

void Info::setPrice(Price *price)
{
  removeParentFromChild(this, _price);
  _price = price;
  addParentToChild(this, _price);
}

void Info::setModeler(Modeler *modeler)
{
  removeParentFromChild(this, _modeler);
  _modeler = modeler;
  addParentToChild(this, _modeler);
}

MFAbstractNode Info::getChildList() const
{
  MFAbstractNode childrenList;
  if (_price != 0)
    childrenList.push_back(_price);
  if (_modeler != 0)
    childrenList.push_back(_modeler);  
    
  return childrenList; 
}

bool Info::addChild(const SFAbstractNode &N)
{
  if (dynamic_cast<Price *>(N) != 0)
  {
    if (_price != 0)
      return false;
      
    setPrice(static_cast<Price *>(N));
    return true;
  }
  
  if (dynamic_cast<Modeler *>(N) != 0)
  {
    if (_modeler != 0)
      return false;
      
    setModeler(static_cast<Modeler *>(N));
    return true;    
  }
  
  return false;
}

bool Info::setChild(const SFAbstractNode &N)
{
  if (dynamic_cast<Price *>(N) != 0)
  {
    setPrice(static_cast<Price *>(N));
    return true;
  }
  
  if (dynamic_cast<Modeler *>(N) != 0)
  {
    setModeler(static_cast<Modeler *>(N));
    return true;    
  }
  
  return false;
}

bool Info::removeChild(const SFAbstractNode &N)
{
  if (_price == N)
  {
    setPrice(0);
    return true;
  }
  
  if (_modeler == N)
  {
    setModeler(0);
    return true;
  }
  
  return false;
}

void Info::load(const X3DFileElement *element)
{
  int index;
  index = element->getIndexAttribute("date");
  if (index != -1)
    _date = element->getAttribute(index);
}

SFString Info::write() const
{
  SFString attr;
  if (_date != "0/0/0")    
    attr += " date=\"" + toSFString(_date) + "\"";
      
  return attr;
}

}
}

X3D_Price.h

#ifndef PRICE_H
#define PRICE_H

#include <X3DTK/X3D/scenegraph.h>

namespace X3DTK {
namespace X3D {

// Price node.

class Price : public X3DChildNode
{
public:
  Price();
  Price(const SFFloat &price,
        const SFString &money);
            
  virtual SFNode clone() const;  
  void setPrice(const SFFloat &price);
  void setMoney(const SFString &money);

  inline const SFFloat &getPrice() const {return _price;};
  inline const SFString &getMoney() const {return _money;};
 
  virtual void load(const X3DFileElement *element);
  virtual SFString write() const;

protected:
  Price(const Price &I);

private:
  SFFloat _price;
  SFString _money;
};

}
}

#endif

X3D_Price.cpp

#include "X3D_Price.h"

namespace X3DTK {
namespace X3D {

Price::Price()
: X3DChildNode(), _price(0.0f), _money("euro")
{
  // Defines the tag of the node. This string must be the same than the one 
  // entered for the creation function, otherwise the node won't be loaded.
  defineTypeName("Price");
}

Price::Price(const SFFloat &price, const SFString &money)
: X3DChildNode(), _price(price), _money(money)
{
  // Defines the tag of the node.
  defineTypeName("Price");
}

Price::Price(const Price &P)
: X3DChildNode(P), _price(P._price), _money(P._money)
{
}

SFNode Price::clone() const
{
  return new Price(*this);
}

void Price::setPrice(const SFFloat &price)
{
  _price = price;
}

void Price::setMoney(const SFString &money)
{
  _money = money;
}

void Price::load(const X3DFileElement *element)
{
  int index;
  index = element->getIndexAttribute("price");
  if (index != -1)
    _price = element->getAttribute(index).toFloat();
  
  index = element->getIndexAttribute("money");
  if (index != -1)
    _money = element->getAttribute(index);  
}

SFString Price::write() const
{
  SFString attr;
  if (_price != 0.0f)    
    attr += " price=\"" + toSFString(_price) + "\"";
  if (_money != "euro")
    attr += " money=\"" + _money + "\"";
      
  return attr;
}

}
}

X3D_Modeler.h

#ifndef MODELER_H
#define MODELER_H

#include <X3DTK/X3D/scenegraph.h>

namespace X3DTK {
namespace X3D {

// Modeler child node of Info.

class Modeler : public X3DChildNode
{
public:
  Modeler();
  Modeler(const SFString &name);
            
  virtual SFNode clone() const;  

  void setName(const SFString &name);

  inline const SFString &getName() const {return _name;};
 
  virtual void load(const X3DFileElement *element);
  virtual SFString write() const;

protected:
  Modeler(const Modeler &I);

private:
  SFString _name;
};

}
}

#endif

X3D_Modeler.cpp

#include "X3D_Modeler.h"

namespace X3DTK {
namespace X3D {

Modeler::Modeler()
: X3DChildNode(), _name("")
{
  // Defines the tag of the node. This string must be the same than the one 
  // entered for the creation function, otherwise the node won't be loaded.
  defineTypeName("Modeler");
}

Modeler::Modeler(const SFString &name)
: X3DChildNode(), _name(name)
{
  // Defines the tag of the node.
  defineTypeName("Modeler");
}

Modeler::Modeler(const Modeler &P)
: X3DChildNode(P), _name(P._name)
{
}

SFNode Modeler::clone() const
{
  return new Modeler(*this);
}

void Modeler::setName(const SFString &name)
{
  _name = name;
}

void Modeler::load(const X3DFileElement *element)
{
  int index;
  index = element->getIndexAttribute("name");
  if (index != -1)
    _name = element->getAttribute(index);  
}

SFString Modeler::write() const
{
  SFString attr;
  if (_name != "")    
    attr += " name=\"" + _name + "\"";
      
  return attr;
}

}
}

X3D_MyGroupingCreator.h

#ifndef MYGROUPINGCREATOR_H
#define MYGROUPINGCREATOR_H

#include <X3DTK/X3D/scenegraph.h>

namespace X3DTK {
namespace X3D {

// Component creator deriving the Grouping component creator defining the default 
// X3D nodes, plus the new ones.

class MyGroupingCreator : public GroupingCreator
{
public:
  // Constructor.
  MyGroupingCreator();
};

}
}

#endif

X3D_MyGroupingCreator.cpp

#include "X3D_MyGroupingCreator.h"
#include "X3D_Info.h"
#include "X3D_Price.h"
#include "X3D_Modeler.h"

namespace X3DTK {
namespace X3D {

MyGroupingCreator::MyGroupingCreator()
: GroupingCreator()
{
  // Defines the new nodes.
  defineNode<Info>();
  defineNode<Price>();
  defineNode<Modeler>();  
}

}
}

X3D_InfoReaderGroupingVisitor.h

#ifndef INFOREADERGROUPINGVISITOR_H
#define INFOREADERGROUPINGVISITOR_H

#include <X3DTK/X3D/scenegraph.h>

namespace X3DTK {
namespace X3D {

class Info;
class Price;
class Modeler;

// Visitor for the Grouping component of the InfoReader processor.

class InfoReaderGroupingVisitor : public GroupingVisitor
{
public:
  InfoReaderGroupingVisitor();

  static void enterInfo(Info *I);
  static void enterPrice(Price *P);
  static void enterModeler(Modeler *M);
};

}
}

#endif

X3D_InfoReaderGroupingVisitor.cpp

#include "X3D_InfoReaderGroupingVisitor.h"
#include "X3D_Info.h"
#include "X3D_Price.h"
#include "X3D_Modeler.h"

#include <iostream>

using namespace std;

namespace X3DTK {
namespace X3D {

InfoReaderGroupingVisitor::InfoReaderGroupingVisitor()
: GroupingVisitor()
{
  // Enter functions.
  defineEnterFunction(&InfoReaderGroupingVisitor::enterInfo);
  defineEnterFunction(&InfoReaderGroupingVisitor::enterPrice);
  defineEnterFunction(&InfoReaderGroupingVisitor::enterModeler);    
}

void InfoReaderGroupingVisitor::enterInfo(Info *)
{
  cout << "Informations relative to the scene:"<< endl;
}

void InfoReaderGroupingVisitor::enterPrice(Price *P)
{
  cout << "  price = " << P->getPrice() << " " << P->getMoney() << endl;
}

void InfoReaderGroupingVisitor::enterModeler(Modeler *M)
{
  cout << "  realized on the " << M->getName() << " modeler" << endl;
}

}
}

X3D_InfoReader.h

#ifndef INFOREADER_H
#define INFOREADER_H

#include <X3DTK/kernel.h>
#include <X3DTK/X3D/scenegraph.h>

namespace X3DTK {
namespace X3D {

// InfoReader processor.

class InfoReader : public X3DOnePassProcessor
{
public:
  InfoReader();
  virtual ~InfoReader();
  
  virtual void read(SFNode N);
};

}
}

#endif

X3D_InfoReader.cpp

#include "X3D_InfoReader.h"
#include "X3D_InfoReaderGroupingVisitor.h"

namespace X3DTK {
namespace X3D {

InfoReader::InfoReader()
{
  // new GraphTraversal.
  graphTraversal = new DFSGraphTraversal();
  // Setting the new component visitor.
  graphTraversal->setComponentVisitor(new InfoReaderGroupingVisitor());
}

InfoReader::~InfoReader()
{
  delete graphTraversal;
}

void InfoReader::read(SFNode N)
{
  graphTraversal->traverse(N);
}

}
}

main.cpp

#include "X3D_InfoReader.h"
#include "X3D_MyGroupingCreator.h"

#include <X3DTK/X3D/scenegraph.h>
#include <iostream>

using namespace X3DTK;
using namespace std;

int main(int argc, char *argv[])
{
  if (argc <= 1)
  {
    cerr << "usage: infoReader input" << endl;
    exit(0);
  }
  
  // DefaultLoader to load the default X3D Nodes.
  X3D::Loader *loader = X3DLoader::getInstanceOf<X3D::Loader>();
  loader->setComponentCreator(new X3D::MyGroupingCreator());
  
  // Instanciation of the new InfoReader.
  X3D::InfoReader *infoReader = X3DProcessor::getInstanceOf<X3D::InfoReader>();
  
  // Loads the scene.
  X3D::Scene *s = loader->load(argv[1], false);
  // reads the graph.
  infoReader->read(s);
  
  // Removes the instances.
  X3DProcessor::removeInstanceOf<X3D::InfoReader>();
  X3DLoader::removeInstanceOf<X3D::Loader>();
      
  return 1;
}

Generated on Wed Apr 7 12:15:26 2004 for X3DToolKit by doxygen 1.3.3