chain.h

Go to the documentation of this file.
00001 
00005 /* Copyright, 2001 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #ifndef NL_CHAIN_H
00025 #define NL_CHAIN_H
00026 
00027 #include <vector>
00028 #include "nel/misc/types_nl.h"
00029 #include "nel/misc/vector.h"
00030 #include "vector_2s.h"
00031 #include "nel/misc/file.h"
00032 
00033 namespace NLPACS
00034 {
00035 class COrderedChain;
00036 
00046 class COrderedChain3f
00047 {
00048 protected:
00049     friend class CChain;
00050     friend class CRetrievableSurface;
00051 
00053     std::vector<NLMISC::CVector>        _Vertices;
00054 
00056     bool                                _Forward;
00057 
00059     uint16                              _ParentId;
00060 
00062     uint16                              _IndexInParent;
00063 
00064 public:
00066     const std::vector<NLMISC::CVector>  &getVertices() const { return _Vertices; }
00067 
00069     bool                                isForward() const { return _Forward; }
00070 
00072     uint16                              getParentId() const { return _ParentId; }
00073 
00075     uint16                              getIndexInParent() const { return _IndexInParent; }
00076 
00078     const NLMISC::CVector               &operator[] (uint n) const { return _Vertices[n]; }
00079 
00081     void                                unpack(const COrderedChain &ochain);
00082 
00084     void                                translate(const NLMISC::CVector &translation)
00085     {
00086         uint    i;
00087         for (i=0; i<_Vertices.size(); ++i)
00088             _Vertices[i] += translation;
00089     }
00090 
00091     void                                serial(NLMISC::IStream &f);
00092 };
00093 
00105 class COrderedChain
00106 {
00107 protected:
00108     friend class CChain;
00109     friend class CRetrievableSurface;
00110 
00112     std::vector<CVector2s>              _Vertices;
00113 
00115     bool                                _Forward;
00116 
00118     uint16                              _ParentId;
00119 
00121     uint16                              _IndexInParent;
00122 
00124     float                               _Length;
00125 
00127     CVector2s                           _Min, _Max;
00128 
00129 public:
00131     const std::vector<CVector2s>        &getVertices() const { return _Vertices; }
00132 
00134     bool                                isForward() const { return _Forward; }
00135 
00137     uint16                              getParentId() const { return _ParentId; }
00138 
00140     uint16                              getIndexInParent() const { return _IndexInParent; }
00141 
00143     float                               getLength() const { return _Length; }
00144 
00146     const CVector2s                     &operator[] (uint n) const { return _Vertices[n]; }
00147 
00149     const CVector2s                     &getMin() const { return _Min; };
00150 
00152     const CVector2s                     &getMax() const { return _Max; };
00153 
00154 
00156     void                                translate(const NLMISC::CVector &translation);
00157 
00159     void                                pack(const COrderedChain3f &chain)
00160     {
00161         uint    i;
00162         const std::vector<NLMISC::CVector>  &vertices = chain.getVertices();
00163         _Vertices.resize(vertices.size());
00164         _Forward = chain.isForward();
00165         _ParentId = chain.getParentId();
00166         _IndexInParent = chain.getIndexInParent();
00167         for (i=0; i<vertices.size(); ++i)
00168         {
00169             _Vertices[i] = CVector2s(vertices[i]);
00170             _Min.minof(_Min, _Vertices[i]);
00171             _Max.maxof(_Max, _Vertices[i]);
00172         }
00173     }
00174 
00176     void                                computeMinMax()
00177     {
00178         _Min = _Max = CVector2s(0, 0);
00179 
00180         if (_Vertices.empty())
00181             return;
00182 
00183         _Min = _Max = _Vertices[0];
00184         uint    i;
00185         for (i=1; i<_Vertices.size(); ++i)
00186         {
00187             _Min.minof(_Min, _Vertices[i]);
00188             _Max.maxof(_Max, _Vertices[i]);
00189         }
00190     }
00191 
00193     void                                traverse(sint from, sint to, bool forward, std::vector<CVector2s> &path) const;
00194 
00196     float                               distance(const NLMISC::CVector &position) const;
00197 
00199     void                                serial(NLMISC::IStream &f);
00200 };
00201 
00208 class CChain
00209 {
00210 protected:
00211     friend class CRetrievableSurface;
00212     friend class CLocalRetriever;
00213 
00215     std::vector<uint16>                 _SubChains;
00216 
00218     sint32                              _Left;
00219 
00221     sint32                              _Right;
00222 
00224     uint16                              _StartTip;
00225     uint16                              _StopTip;
00226 
00228     float                               _Length;
00229 
00230     uint8                               _LeftLoop, _LeftLoopIndex;
00231     uint8                               _RightLoop, _RightLoopIndex;
00232 
00233 protected:
00235     void                                make(const std::vector<NLMISC::CVector> &vertices, sint32 left, sint32 right, std::vector<COrderedChain> &chains, uint16 thisId,
00236                                              std::vector<COrderedChain3f> &fullChains, std::vector<uint> &useOChainId);
00237 
00238     void                                setLoopIndexes(sint32 surface, uint loop, uint loopIndex)
00239     {
00240         if (_Left == surface)
00241         {
00242             _LeftLoop = uint8(loop);
00243             _LeftLoopIndex = uint8(loopIndex);
00244         }
00245         else
00246         {
00247             _RightLoop = uint8(loop);
00248             _RightLoopIndex = uint8(loopIndex);
00249         }
00250     }
00251 
00252     void                                setBorderChainIndex(sint32 id)  { _Right = convertBorderChainId(id); }
00253 
00254 public:
00255 
00257     CChain() :  _Left(-1), _Right(-1), _StartTip(0xffff), _StopTip(0xffff), _Length(0.0f),
00258                 _LeftLoop(0), _LeftLoopIndex(0), _RightLoop(0), _RightLoopIndex(0) {}
00259 
00261     const std::vector<uint16>           &getSubChains() const { return _SubChains; }
00262 
00264     uint16                              getSubChain(uint n) const { return _SubChains[n]; }
00265 
00267     sint32                              getLeft() const { return _Left; }
00268     uint8                               getLeftLoop() const { return _LeftLoop; }
00269     uint8                               getLeftLoopIndex() const { return _LeftLoopIndex; }
00270 
00272     sint32                              getRight() const { return _Right; }
00273     uint8                               getRightLoop() const { return _RightLoop; }
00274     uint8                               getRightLoopIndex() const { return _RightLoopIndex; }
00275 
00277     float                               getLength() const { return _Length; }
00278 
00280     sint32                              getBorderChainIndex() const
00281     {
00282         return (isBorderChainId(_Right)) ? convertBorderChainId(_Right) : -1;
00283     }
00284 
00286     static bool                         isBorderChainId(sint32 id) { return id <= -256; }
00287 
00289     static sint32                       convertBorderChainId(sint32 id) { return -(id+256); }
00290 
00292     bool                                isBorderChain() const { return isBorderChainId(_Right); }
00293 
00295     static sint32                       getDummyBorderChainId() { return convertBorderChainId(0); }
00296 
00298     uint16                              getStartTip() const { return _StartTip; }
00299 
00301     uint16                              getStopTip() const { return _StopTip; }
00302 
00304     void                                serial(NLMISC::IStream &f);
00305 
00306 protected:
00308     void                                unify(std::vector<COrderedChain> &ochains);
00309 
00311     void                                setStartVector(const CVector2s &v, std::vector<COrderedChain> &ochains);
00312 
00314     void                                setStopVector(const CVector2s &v, std::vector<COrderedChain> &ochains);
00315 
00317     CVector2s                           getStartVector(std::vector<COrderedChain> &ochains);
00318 
00319     //
00320     CVector2s                           getStopVector(std::vector<COrderedChain> &ochains);
00321 };
00322 
00323 
00324 
00325 
00326 
00327 //
00328 inline void                             COrderedChain3f::unpack(const COrderedChain &chain)
00329 {
00330     uint    i, mx;
00331     const std::vector<CVector2s>    &vertices = chain.getVertices();
00332     mx = _Vertices.size();
00333     _Vertices.resize(vertices.size());
00334     _Forward = chain.isForward();
00335     _ParentId = chain.getParentId();
00336     _IndexInParent = chain.getIndexInParent();
00337     for (i=0; i<vertices.size(); ++i)
00338     {
00339         _Vertices[i] = vertices[i].unpack3f(i >= mx ? 0.0f : _Vertices[i].z);
00340     }
00341 }
00342 
00343 }; // NLPACS
00344 
00345 #endif // NL_CHAIN_H
00346 
00347 /* End of chain.h */

Generated on Thu Jan 7 08:26:36 2010 for NeL by  doxygen 1.6.1