Documentation


morphingNode.cpp

Go to the documentation of this file.
00001 #include "morphingNode.h"
00002 #include <animal/engine/morphingEngine.inl>
00003 #include <animal/engine/AnimalEngineNode.cpp>  //template class
00004 
00005 namespace X3DTK
00006 {
00007 
00008 
00009   namespace X3D
00010   {
00011 
00012     template class AnimalEngineNode<AMorphingEngine>;
00013 
00015     //template class animal::MorphingEngine<Morphings, X3DTK::SFFloat>;
00016     MorphingNode::MorphingNode()
00017         : _coordinate(0)
00018     {
00019       // Node
00020       define(Recorder<MorphingNode>::getTypeName("MorphingNode"));
00021 
00022       // Attributes
00023       define(Recorder<MorphingNode, int>::getAttribute("methodID", &MorphingNode::_interpolationID, 0));
00024       define(Recorder<MorphingNode, float>::getAttribute("initialKey", &MorphingNode::_initialKey, 0.0));
00025       define(Recorder<MorphingNode, float>::getAttribute("velocity", &MorphingNode::_velocity, 0.0));
00026       define(Recorder<MorphingNode, MFFloat>::getAttribute("keys", &MorphingNode::_keys, MFFloat()));
00027       define(Recorder<MorphingNode, MFVec3f>::getAttribute("keyValues", &MorphingNode::_keyValues, MFVec3f()));
00028 
00029       // Children
00030       define(Recorder<MorphingNode, Coordinate>::getSFNode("Coordinate", &MorphingNode::_coordinate));
00031     }
00032     
00033     MorphingNode::~MorphingNode()
00034     {
00035       removeParentFromChild(this, _coordinate);
00036     }
00037 
00039     // Init the engine inherited by the engine node
00040     void MorphingNode::init()
00041     {
00042       
00043       
00044       // Verify the sizes of the datas
00045       Keys::iterator i = _keys.begin ();
00046       KeyValues::iterator j = _keyValues.begin ();
00047       unsigned int nbValues = 0;
00048       if (_keys.size ())
00049         nbValues = _keyValues.size () / _keys.size ();
00050 
00051       if (!_keys.size () || nbValues * _keys.size () != _keyValues.size ())
00052       {
00053         //std::cerr <<"nbValues = "<<nbValues<<" ; _keys.size() = "<<_keys.size()<<" ; _keyValues.size() = "<< _keyValues.size()<<std::endl;
00054         std::cerr <<"Warning: impossible to init a key frame engine. In file.x3d, the number of the keys or of the keyValues is wrong."    << std::endl;
00055         return;
00056       }
00057       // Copy datas in the morphingNode into the morphings
00058       if(!_morphings) _morphings = new Morphings();
00059 
00060       Morphings::Values values;
00061       for (; i != _keys.end (); ++i)
00062       {
00063         for (unsigned int k = 0; j != _keyValues.end () && k < nbValues;  ++j, ++k)
00064           values.push_back ((*j));
00065         _morphings->insert (std::pair < Morphings::Key, Morphings::Values > ((*i), values));
00066         values.clear ();
00067       }
00068     
00069 //       MFNode childList = getChildList();
00070 //       std::cerr<<"childList.size() = "<<childList.size()<<std::endl;
00071 
00072       if (!_coordinate)
00073       {
00074         std::cerr <<"Warning: impossible to init a key frame engine. In the file .x3d, coordinate is NULL."    << std::endl;
00075         return;
00076       }
00077 
00078       // Get the positions
00079       const MFVec3f * positionsTmp = &(_coordinate->getPoint ());
00080       MFVec3f * positions = const_cast < MFVec3f * >(positionsTmp);
00081 
00082       if (!positions || positions->size () != nbValues)
00083       {
00084         //std::cerr <<"nbValues = "<<nbValues<<" ; _keys.size() = "<<_keys.size()<<" ; _keyValues.size() = "<< _keyValues.size()<< " ; positions->size() = "<<positions->size()<<std::endl;
00085         std::cerr <<"Warning: impossible to init a key frame engine. In the file .x3d, the number of the keys or of the keyValues is wrong."    << std::endl;
00086         return;
00087       }
00088 
00089       // init some attributes
00090       _positions = positions;
00091 
00092       // Init the AMorphingEngine
00093       AMorphingEngine::init();
00094     }
00095 
00097     void MorphingNode::setKeys(const Keys &keys)
00098     {
00099       _keys= keys;
00100     }
00101 
00102     void MorphingNode::setKeyValues(const KeyValues &keyValues)
00103     {
00104       _keyValues= keyValues;
00105     }
00106 
00107 
00108     void MorphingNode::close()
00109     {
00110       // Copy _morphings in _keys and _keyValues
00111       Keys * keys = const_cast<Keys *>(&_keys);
00112       keys->clear();
00113       KeyValues * keyValues = const_cast<KeyValues *>(&_keyValues);
00114       keyValues->clear();
00115       for (  Morphings::const_iterator it = _morphings->begin();
00116              it != _morphings->end();
00117              ++it)
00118       {
00119         keys->push_back((*it).first);
00120         for (  Morphings::Values::const_iterator itV = ((*it).second).begin();
00121                itV != ((*it).second).end();
00122                ++itV)
00123           keyValues->push_back((*itV));
00124       }
00125     }
00126   }//X3D
00127 }//X3DTK
00128 
00129 
00133 void X3DTK::X3D::MorphingNode::declareOutputs( X3D_X3DNodeList& list )
00134 {
00135   list.push_back(_coordinate);
00136 }

Generated on Thu Dec 23 13:52:26 2004 by doxygen 1.3.6