Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / ligo / zone_edge.cpp
blobf34d43cd1af7c122e2977297ccc220bae9e10c5c
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdligo.h"
18 // Ligo include
19 #include "nel/ligo/zone_edge.h"
20 #include "nel/ligo/ligo_config.h"
21 #include "nel/ligo/ligo_error.h"
23 // NeL include
24 #include "nel/misc/matrix.h"
26 using namespace NLMISC;
28 #ifdef DEBUG_NEW
29 #define new DEBUG_NEW
30 #endif
32 namespace NLLIGO
35 // ***************************************************************************
37 bool CZoneEdge::build (const std::vector<NLMISC::CVector> &theEdge, const std::vector<uint32> &theId, uint rotation,
38 sint32 offsetX, sint32 offsetY, const CLigoConfig &config, CLigoError &errors)
40 // Some checks
41 // no need, it s an uint nlassert (rotation>=0);
42 nlassert (rotation<=3);
43 nlassert (theEdge.size() == theId.size());
45 // Cancels errors
46 errors.clear ();
48 // Errors ?
49 bool ok = true;
51 // Check first position
52 CVector toCheck (theEdge[0].x, theEdge[0].y, 0);
53 if ((float)fabs (toCheck.norm())>config.Snap)
55 // Vertex error
56 errors.pushVertexError (CLigoError::UnknownError, 0);
57 ok = false;
60 // Check last position
61 uint lastIndex = (uint)theEdge.size()-1;
62 toCheck = CVector (theEdge[lastIndex].x, theEdge[lastIndex].y, 0);
63 if (((toCheck-CVector (config.CellSize, 0, 0)).norm())>config.Snap)
65 // Vertex error
66 errors.pushVertexError (CLigoError::UnknownError, 0);
67 ok = false;
70 // No error ? Build!
71 if (ok)
73 _TheEdge = theEdge;
74 _Rotation = rotation;
75 _OffsetX = offsetX;
76 _OffsetY = offsetY;
77 _Id = theId;
80 return ok;
83 // ***************************************************************************
85 bool CZoneEdge::isSymetrical (const CLigoConfig &config, CLigoError &errors) const
87 // Cancels errors
88 errors.clear ();
90 // Errors ?
91 bool ok = true;
93 // For each internal vertices
94 uint vert;
95 for (vert=0; vert<_TheEdge.size(); vert++)
97 // Symmetrical value
98 CVector sym = CVector (config.CellSize-_TheEdge[vert].x, _TheEdge[vert].y, _TheEdge[vert].z);
100 // Others vertices
101 uint vert2;
102 for (vert2=0; vert2<_TheEdge.size(); vert2++)
104 // Not the same ?
105 if (vert != vert2)
107 // Snapped ?
108 if ((_TheEdge[vert2]-sym).norm() <= config.Snap)
110 // Good, next one
111 break;
116 // Not found ?
117 if (vert2>=_TheEdge.size())
119 // Error
120 ok = false;
122 // Push error message
123 errors.pushVertexError (CLigoError::NotSymetrical, _Id[vert]);
124 errors.MainError = CLigoError::NotSymetrical;
128 // Return error code
129 return ok;
132 // ***************************************************************************
134 bool CZoneEdge::isTheSame (const CZoneEdge &other, const CLigoConfig &config, CLigoError &errors) const
136 // Same vertex count ?
137 if (_TheEdge.size() != other._TheEdge.size())
139 // Error
140 errors.MainError = CLigoError::NotSameVerticesNumber;
141 return false;
144 // Errors ?
145 bool ok = true;
147 // For each internal vertices
148 uint vert;
149 for (vert=0; vert<_TheEdge.size(); vert++)
151 // The same ?
152 const CVector &pos0 = _TheEdge[vert];
153 const CVector &pos1 = other._TheEdge[vert];
154 if ((pos0-pos1).norm() > config.Snap)
156 // Error
157 ok = false;
159 // Push error message
160 errors.pushVertexError (CLigoError::NotSameVertex, other._Id[vert]);
161 errors.MainError = CLigoError::NotSameVertex;
165 // Return error code
166 return ok;
169 // ***************************************************************************
171 void CZoneEdge::serial (NLMISC::IStream& s)
173 // Serial the version
174 /*sint ver =*/ s.serialVersion (0);
176 s.xmlPush ("VERTICES");
177 s.serialCont (_TheEdge);
178 s.xmlPop ();
180 s.xmlPush ("VERTICES_ID");
181 s.serialCont (_Id);
182 s.xmlPop ();
184 s.xmlSerial (_Rotation, "ROTATION");
186 s.xmlSerial (_OffsetX, _OffsetY, "OFFSET");
189 // ***************************************************************************
191 void CZoneEdge::invert (const CLigoConfig &config)
193 // Copy the array
194 const std::vector<NLMISC::CVector> copy = _TheEdge;
196 // For each internal vertices
197 uint vert;
198 for (vert=0; vert<_TheEdge.size(); vert++)
200 // Invert
201 const CVector &pos = copy[_TheEdge.size()-vert-1];
202 _TheEdge[vert] = CVector (config.CellSize - pos.x, pos.y, pos.z);
206 // ***************************************************************************
208 void CZoneEdge::buildMatrix (NLMISC::CMatrix& mat, const CLigoConfig &config) const
210 // Build a transformation matrix
211 mat.identity();
212 mat.rotateZ ((float)Pi*(float)_Rotation/2.f);
213 mat.setPos (CVector (config.CellSize*(float)_OffsetX, config.CellSize*(float)_OffsetY, 0));
216 // ***************************************************************************