# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

zone_edge.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 // Ligo include
00027 #include "zone_edge.h"
00028 #include "ligo_config.h"
00029 #include "ligo_error.h"
00030 
00031 // NeL include
00032 #include "nel/misc/matrix.h"
00033 
00034 using namespace NLMISC;
00035 
00036 namespace NLLIGO
00037 {
00038 
00039 // ***************************************************************************
00040 
00041 bool CZoneEdge::build (const std::vector<NLMISC::CVector> &theEdge, const std::vector<uint32> &theId, uint rotation, 
00042                                 sint32 offsetX, sint32 offsetY, const CLigoConfig &config, CLigoError &errors)
00043 {
00044         // Some checks
00045         nlassert (rotation>=0);
00046         nlassert (rotation<=3);
00047         nlassert (theEdge.size() == theId.size());
00048 
00049         // Cancels errors
00050         errors.clear ();
00051 
00052         // Errors ?
00053         bool ok = true;
00054 
00055         // Check first position
00056         CVector toCheck (theEdge[0].x, theEdge[0].y, 0);
00057         if ((float)fabs (toCheck.norm())>config.Snap)
00058         {
00059                 // Vertex error
00060                 errors.pushVertexError (CLigoError::UnknownError, 0);
00061                 ok = false;
00062         }
00063 
00064         // Check last position
00065         uint lastIndex = theEdge.size()-1;
00066         toCheck  = CVector (theEdge[lastIndex].x, theEdge[lastIndex].y, 0);
00067         if (((toCheck-CVector (config.CellSize, 0, 0)).norm())>config.Snap)
00068         {
00069                 // Vertex error
00070                 errors.pushVertexError (CLigoError::UnknownError, 0);
00071                 ok = false;
00072         }
00073 
00074         // No error ? Build!
00075         if (ok)
00076         {
00077                 _TheEdge = theEdge;
00078                 _Rotation = rotation;
00079                 _OffsetX = offsetX;
00080                 _OffsetY = offsetY;
00081                 _Id = theId;
00082         }
00083 
00084         return ok;
00085 }
00086 
00087 // ***************************************************************************
00088 
00089 bool CZoneEdge::isSymetrical (const CLigoConfig &config, CLigoError &errors) const
00090 {
00091         // Cancels errors
00092         errors.clear ();
00093 
00094         // Errors ?
00095         bool ok = true;
00096 
00097         // For each internal vertices
00098         uint vert;
00099         for (vert=0; vert<_TheEdge.size(); vert++)
00100         {
00101                 // Symmetrical value
00102                 CVector sym = CVector (config.CellSize-_TheEdge[vert].x, _TheEdge[vert].y, _TheEdge[vert].z);
00103 
00104                 // Others vertices
00105                 uint vert2;
00106                 for (vert2=0; vert2<_TheEdge.size(); vert2++)
00107                 {
00108                         // Not the same ?
00109                         if (vert != vert2)
00110                         {
00111                                 // Snapped ?
00112                                 if ((_TheEdge[vert2]-sym).norm() <= config.Snap)
00113                                 {
00114                                         // Good, next one
00115                                         break;
00116                                 }
00117                         }
00118                 }
00119 
00120                 // Not found ?
00121                 if (vert2>=_TheEdge.size())
00122                 {
00123                         // Error
00124                         ok = false;
00125 
00126                         // Push error message
00127                         errors.pushVertexError (CLigoError::NotSymetrical, _Id[vert]);
00128                         errors.MainError = CLigoError::NotSymetrical;
00129                 }
00130         }
00131 
00132         // Return error code
00133         return ok;
00134 }
00135 
00136 // ***************************************************************************
00137 
00138 bool CZoneEdge::isTheSame (const CZoneEdge &other, const CLigoConfig &config, CLigoError &errors) const
00139 {
00140         // Same vertex count ?
00141         if (_TheEdge.size() != other._TheEdge.size())
00142         {
00143                 // Error
00144                 errors.MainError = CLigoError::NotSameVerticesNumber;
00145                 return false;
00146         }
00147 
00148         // Errors ?
00149         bool ok = true;
00150 
00151         // For each internal vertices
00152         uint vert;
00153         for (vert=0; vert<_TheEdge.size(); vert++)
00154         {
00155                 // The same ?
00156                 const CVector &pos0 = _TheEdge[vert];
00157                 const CVector &pos1 = other._TheEdge[vert];
00158                 if ((pos0-pos1).norm() > config.Snap)
00159                 {
00160                         // Error
00161                         ok = false;
00162 
00163                         // Push error message
00164                         errors.pushVertexError (CLigoError::NotSameVertex, other._Id[vert]);
00165                         errors.MainError = CLigoError::NotSameVertex;
00166                 }
00167         }
00168 
00169         // Return error code
00170         return ok;
00171 }
00172 
00173 // ***************************************************************************
00174 
00175 void CZoneEdge::serial (NLMISC::IStream& s)
00176 {
00177         // Serial the version
00178         sint ver = s.serialVersion (0);
00179 
00180         s.xmlPush ("VERTICES");
00181                 s.serialCont (_TheEdge);
00182         s.xmlPop ();
00183 
00184         s.xmlPush ("VERTICES_ID");
00185                 s.serialCont (_Id);
00186         s.xmlPop ();
00187 
00188         s.xmlSerial (_Rotation, "ROTATION");
00189 
00190         s.xmlSerial (_OffsetX, _OffsetY, "OFFSET");
00191 }
00192 
00193 // ***************************************************************************
00194 
00195 void CZoneEdge::invert (const CLigoConfig &config)
00196 {
00197         // Copy the array
00198         const std::vector<NLMISC::CVector>      copy = _TheEdge;
00199 
00200         // For each internal vertices
00201         uint vert;
00202         for (vert=0; vert<_TheEdge.size(); vert++)
00203         {
00204                 // Invert
00205                 const CVector &pos = copy[_TheEdge.size()-vert-1];
00206                 _TheEdge[vert] = CVector (config.CellSize - pos.x, pos.y, pos.z);
00207         }
00208 }
00209 
00210 // ***************************************************************************
00211 
00212 void CZoneEdge::buildMatrix (NLMISC::CMatrix& mat, const CLigoConfig &config) const
00213 {
00214         // Build a transformation matrix
00215         mat.identity();
00216         mat.rotateZ ((float)Pi*(float)_Rotation/2.f);
00217         mat.setPos (CVector (config.CellSize*(float)_OffsetX, config.CellSize*(float)_OffsetY, 0));
00218 }
00219 
00220 // ***************************************************************************
00221 
00222 }