hacdManifoldMesh.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  4. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  5. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  6. 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
  7. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8. */
  9. #include "hacdManifoldMesh.h"
  10. using namespace std;
  11. namespace HACD
  12. {
  13. Material::Material(void)
  14. {
  15. m_diffuseColor.X() = 0.5;
  16. m_diffuseColor.Y() = 0.5;
  17. m_diffuseColor.Z() = 0.5;
  18. m_specularColor.X() = 0.5;
  19. m_specularColor.Y() = 0.5;
  20. m_specularColor.Z() = 0.5;
  21. m_ambientIntensity = 0.4;
  22. m_emissiveColor.X() = 0.0;
  23. m_emissiveColor.Y() = 0.0;
  24. m_emissiveColor.Z() = 0.0;
  25. m_shininess = 0.4;
  26. m_transparency = 0.0;
  27. }
  28. TMMVertex::TMMVertex(void)
  29. {
  30. m_name = 0;
  31. m_id = 0;
  32. m_duplicate = 0;
  33. m_onHull = false;
  34. m_tag = false;
  35. }
  36. TMMVertex::~TMMVertex(void)
  37. {
  38. }
  39. TMMEdge::TMMEdge(void)
  40. {
  41. m_id = 0;
  42. m_triangles[0] = m_triangles[1] = m_newFace = 0;
  43. m_vertices[0] = m_vertices[1] = 0;
  44. }
  45. TMMEdge::~TMMEdge(void)
  46. {
  47. }
  48. TMMTriangle::TMMTriangle(void)
  49. {
  50. m_id = 0;
  51. for(int i = 0; i < 3; i++)
  52. {
  53. m_edges[i] = 0;
  54. m_vertices[0] = 0;
  55. }
  56. m_visible = false;
  57. }
  58. TMMTriangle::~TMMTriangle(void)
  59. {
  60. }
  61. TMMesh::TMMesh(void)
  62. {
  63. m_barycenter = Vec3<Real>(0,0,0);
  64. m_diag = 1;
  65. }
  66. TMMesh::~TMMesh(void)
  67. {
  68. }
  69. void TMMesh::Print()
  70. {
  71. size_t nV = m_vertices.GetSize();
  72. std::cout << "-----------------------------" << std::endl;
  73. std::cout << "vertices (" << nV << ")" << std::endl;
  74. for(size_t v = 0; v < nV; v++)
  75. {
  76. const TMMVertex & currentVertex = m_vertices.GetData();
  77. std::cout << currentVertex.m_id << ", "
  78. << currentVertex.m_pos.X() << ", "
  79. << currentVertex.m_pos.Y() << ", "
  80. << currentVertex.m_pos.Z() << std::endl;
  81. m_vertices.Next();
  82. }
  83. size_t nE = m_edges.GetSize();
  84. std::cout << "edges (" << nE << ")" << std::endl;
  85. for(size_t e = 0; e < nE; e++)
  86. {
  87. const TMMEdge & currentEdge = m_edges.GetData();
  88. const CircularListElement<TMMVertex> * v0 = currentEdge.m_vertices[0];
  89. const CircularListElement<TMMVertex> * v1 = currentEdge.m_vertices[1];
  90. const CircularListElement<TMMTriangle> * f0 = currentEdge.m_triangles[0];
  91. const CircularListElement<TMMTriangle> * f1 = currentEdge.m_triangles[1];
  92. std::cout << "-> (" << v0->GetData().m_name << ", " << v1->GetData().m_name << ")" << std::endl;
  93. std::cout << "-> F0 (" << f0->GetData().m_vertices[0]->GetData().m_name << ", "
  94. << f0->GetData().m_vertices[1]->GetData().m_name << ", "
  95. << f0->GetData().m_vertices[2]->GetData().m_name <<")" << std::endl;
  96. std::cout << "-> F1 (" << f1->GetData().m_vertices[0]->GetData().m_name << ", "
  97. << f1->GetData().m_vertices[1]->GetData().m_name << ", "
  98. << f1->GetData().m_vertices[2]->GetData().m_name << ")" << std::endl;
  99. m_edges.Next();
  100. }
  101. size_t nT = m_triangles.GetSize();
  102. std::cout << "triangles (" << nT << ")" << std::endl;
  103. for(size_t t = 0; t < nT; t++)
  104. {
  105. const TMMTriangle & currentTriangle = m_triangles.GetData();
  106. const CircularListElement<TMMVertex> * v0 = currentTriangle.m_vertices[0];
  107. const CircularListElement<TMMVertex> * v1 = currentTriangle.m_vertices[1];
  108. const CircularListElement<TMMVertex> * v2 = currentTriangle.m_vertices[2];
  109. const CircularListElement<TMMEdge> * e0 = currentTriangle.m_edges[0];
  110. const CircularListElement<TMMEdge> * e1 = currentTriangle.m_edges[1];
  111. const CircularListElement<TMMEdge> * e2 = currentTriangle.m_edges[2];
  112. std::cout << "-> (" << v0->GetData().m_name << ", " << v1->GetData().m_name << ", "<< v2->GetData().m_name << ")" << std::endl;
  113. std::cout << "-> E0 (" << e0->GetData().m_vertices[0]->GetData().m_name << ", "
  114. << e0->GetData().m_vertices[1]->GetData().m_name << ")" << std::endl;
  115. std::cout << "-> E1 (" << e1->GetData().m_vertices[0]->GetData().m_name << ", "
  116. << e1->GetData().m_vertices[1]->GetData().m_name << ")" << std::endl;
  117. std::cout << "-> E2 (" << e2->GetData().m_vertices[0]->GetData().m_name << ", "
  118. << e2->GetData().m_vertices[1]->GetData().m_name << ")" << std::endl;
  119. m_triangles.Next();
  120. }
  121. }
  122. bool TMMesh::Save(const char *fileName)
  123. {
  124. std::ofstream fout(fileName);
  125. std::cout << "Saving " << fileName << std::endl;
  126. if (SaveVRML2(fout))
  127. {
  128. fout.close();
  129. return true;
  130. }
  131. return false;
  132. }
  133. bool TMMesh::SaveVRML2(std::ofstream &fout)
  134. {
  135. return SaveVRML2(fout, Material());
  136. }
  137. bool TMMesh::SaveVRML2(std::ofstream &fout, const Material & material)
  138. {
  139. if (fout.is_open())
  140. {
  141. size_t nV = m_vertices.GetSize();
  142. size_t nT = m_triangles.GetSize();
  143. fout <<"#VRML V2.0 utf8" << std::endl;
  144. fout <<"" << std::endl;
  145. fout <<"# Vertices: " << nV << std::endl;
  146. fout <<"# Triangles: " << nT << std::endl;
  147. fout <<"" << std::endl;
  148. fout <<"Group {" << std::endl;
  149. fout <<" children [" << std::endl;
  150. fout <<" Shape {" << std::endl;
  151. fout <<" appearance Appearance {" << std::endl;
  152. fout <<" material Material {" << std::endl;
  153. fout <<" diffuseColor " << material.m_diffuseColor.X() << " "
  154. << material.m_diffuseColor.Y() << " "
  155. << material.m_diffuseColor.Z() << std::endl;
  156. fout <<" ambientIntensity " << material.m_ambientIntensity << std::endl;
  157. fout <<" specularColor " << material.m_specularColor.X() << " "
  158. << material.m_specularColor.Y() << " "
  159. << material.m_specularColor.Z() << std::endl;
  160. fout <<" emissiveColor " << material.m_emissiveColor.X() << " "
  161. << material.m_emissiveColor.Y() << " "
  162. << material.m_emissiveColor.Z() << std::endl;
  163. fout <<" shininess " << material.m_shininess << std::endl;
  164. fout <<" transparency " << material.m_transparency << std::endl;
  165. fout <<" }" << std::endl;
  166. fout <<" }" << std::endl;
  167. fout <<" geometry IndexedFaceSet {" << std::endl;
  168. fout <<" ccw TRUE" << std::endl;
  169. fout <<" solid TRUE" << std::endl;
  170. fout <<" convex TRUE" << std::endl;
  171. if (GetNVertices() > 0) {
  172. fout <<" coord DEF co Coordinate {" << std::endl;
  173. fout <<" point [" << std::endl;
  174. for(size_t v = 0; v < nV; v++)
  175. {
  176. TMMVertex & currentVertex = m_vertices.GetData();
  177. fout <<" " << currentVertex.m_pos.X() << " "
  178. << currentVertex.m_pos.Y() << " "
  179. << currentVertex.m_pos.Z() << "," << std::endl;
  180. currentVertex.m_id = v;
  181. m_vertices.Next();
  182. }
  183. fout <<" ]" << std::endl;
  184. fout <<" }" << std::endl;
  185. }
  186. if (GetNTriangles() > 0) {
  187. fout <<" coordIndex [ " << std::endl;
  188. for(size_t f = 0; f < nT; f++)
  189. {
  190. TMMTriangle & currentTriangle = m_triangles.GetData();
  191. fout <<" " << currentTriangle.m_vertices[0]->GetData().m_id << ", "
  192. << currentTriangle.m_vertices[1]->GetData().m_id << ", "
  193. << currentTriangle.m_vertices[2]->GetData().m_id << ", -1," << std::endl;
  194. m_triangles.Next();
  195. }
  196. fout <<" ]" << std::endl;
  197. }
  198. fout <<" }" << std::endl;
  199. fout <<" }" << std::endl;
  200. fout <<" ]" << std::endl;
  201. fout <<"}" << std::endl;
  202. }
  203. return true;
  204. }
  205. void TMMesh::GetIFS(Vec3<Real> * const points, Vec3<long> * const triangles)
  206. {
  207. size_t nV = m_vertices.GetSize();
  208. size_t nT = m_triangles.GetSize();
  209. for(size_t v = 0; v < nV; v++)
  210. {
  211. points[v] = m_vertices.GetData().m_pos;
  212. m_vertices.GetData().m_id = v;
  213. m_vertices.Next();
  214. }
  215. for(size_t f = 0; f < nT; f++)
  216. {
  217. TMMTriangle & currentTriangle = m_triangles.GetData();
  218. triangles[f].X() = static_cast<long>(currentTriangle.m_vertices[0]->GetData().m_id);
  219. triangles[f].Y() = static_cast<long>(currentTriangle.m_vertices[1]->GetData().m_id);
  220. triangles[f].Z() = static_cast<long>(currentTriangle.m_vertices[2]->GetData().m_id);
  221. m_triangles.Next();
  222. }
  223. }
  224. void TMMesh::Clear()
  225. {
  226. m_vertices.Clear();
  227. m_edges.Clear();
  228. m_triangles.Clear();
  229. }
  230. void TMMesh::Copy(TMMesh & mesh)
  231. {
  232. Clear();
  233. // updating the id's
  234. size_t nV = mesh.m_vertices.GetSize();
  235. size_t nE = mesh. m_edges.GetSize();
  236. size_t nT = mesh.m_triangles.GetSize();
  237. for(size_t v = 0; v < nV; v++)
  238. {
  239. mesh.m_vertices.GetData().m_id = v;
  240. mesh.m_vertices.Next();
  241. }
  242. for(size_t e = 0; e < nE; e++)
  243. {
  244. mesh.m_edges.GetData().m_id = e;
  245. mesh.m_edges.Next();
  246. }
  247. for(size_t f = 0; f < nT; f++)
  248. {
  249. mesh.m_triangles.GetData().m_id = f;
  250. mesh.m_triangles.Next();
  251. }
  252. // copying data
  253. m_vertices = mesh.m_vertices;
  254. m_edges = mesh.m_edges;
  255. m_triangles = mesh.m_triangles;
  256. // generating mapping
  257. CircularListElement<TMMVertex> ** vertexMap = new CircularListElement<TMMVertex> * [nV];
  258. CircularListElement<TMMEdge> ** edgeMap = new CircularListElement<TMMEdge> * [nE];
  259. CircularListElement<TMMTriangle> ** triangleMap = new CircularListElement<TMMTriangle> * [nT];
  260. for(size_t v = 0; v < nV; v++)
  261. {
  262. vertexMap[v] = m_vertices.GetHead();
  263. m_vertices.Next();
  264. }
  265. for(size_t e = 0; e < nE; e++)
  266. {
  267. edgeMap[e] = m_edges.GetHead();
  268. m_edges.Next();
  269. }
  270. for(size_t f = 0; f < nT; f++)
  271. {
  272. triangleMap[f] = m_triangles.GetHead();
  273. m_triangles.Next();
  274. }
  275. // updating pointers
  276. for(size_t v = 0; v < nV; v++)
  277. {
  278. if (vertexMap[v]->GetData().m_duplicate)
  279. {
  280. vertexMap[v]->GetData().m_duplicate = edgeMap[vertexMap[v]->GetData().m_duplicate->GetData().m_id];
  281. }
  282. }
  283. for(size_t e = 0; e < nE; e++)
  284. {
  285. if (edgeMap[e]->GetData().m_newFace)
  286. {
  287. edgeMap[e]->GetData().m_newFace = triangleMap[edgeMap[e]->GetData().m_newFace->GetData().m_id];
  288. }
  289. if (nT > 0)
  290. {
  291. for(int f = 0; f < 2; f++)
  292. {
  293. if (edgeMap[e]->GetData().m_triangles[f])
  294. {
  295. edgeMap[e]->GetData().m_triangles[f] = triangleMap[edgeMap[e]->GetData().m_triangles[f]->GetData().m_id];
  296. }
  297. }
  298. }
  299. for(int v = 0; v < 2; v++)
  300. {
  301. if (edgeMap[e]->GetData().m_vertices[v])
  302. {
  303. edgeMap[e]->GetData().m_vertices[v] = vertexMap[edgeMap[e]->GetData().m_vertices[v]->GetData().m_id];
  304. }
  305. }
  306. }
  307. for(size_t f = 0; f < nT; f++)
  308. {
  309. if (nE > 0)
  310. {
  311. for(int e = 0; e < 3; e++)
  312. {
  313. if (triangleMap[f]->GetData().m_edges[e])
  314. {
  315. triangleMap[f]->GetData().m_edges[e] = edgeMap[triangleMap[f]->GetData().m_edges[e]->GetData().m_id];
  316. }
  317. }
  318. }
  319. for(int v = 0; v < 3; v++)
  320. {
  321. if (triangleMap[f]->GetData().m_vertices[v])
  322. {
  323. triangleMap[f]->GetData().m_vertices[v] = vertexMap[triangleMap[f]->GetData().m_vertices[v]->GetData().m_id];
  324. }
  325. }
  326. }
  327. delete [] vertexMap;
  328. delete [] edgeMap;
  329. delete [] triangleMap;
  330. }
  331. long IntersectRayTriangle(const Vec3<double> & P0, const Vec3<double> & dir,
  332. const Vec3<double> & V0, const Vec3<double> & V1,
  333. const Vec3<double> & V2, double &t)
  334. {
  335. Vec3<double> edge1, edge2, edge3;
  336. double det, invDet;
  337. edge1 = V1 - V2;
  338. edge2 = V2 - V0;
  339. Vec3<double> pvec = dir ^ edge2;
  340. det = edge1 * pvec;
  341. if (det == 0.0)
  342. return 0;
  343. invDet = 1.0/det;
  344. Vec3<double> tvec = P0 - V0;
  345. Vec3<double> qvec = tvec ^ edge1;
  346. t = (edge2 * qvec) * invDet;
  347. if (t < 0.0)
  348. {
  349. return 0;
  350. }
  351. edge3 = V0 - V1;
  352. Vec3<double> I(P0 + t * dir);
  353. Vec3<double> s0 = (I-V0) ^ edge3;
  354. Vec3<double> s1 = (I-V1) ^ edge1;
  355. Vec3<double> s2 = (I-V2) ^ edge2;
  356. if (s0*s1 > -1e-9 && s2*s1 > -1e-9)
  357. {
  358. return 1;
  359. }
  360. return 0;
  361. }
  362. bool IntersectLineLine(const Vec3<double> & p1, const Vec3<double> & p2,
  363. const Vec3<double> & p3, const Vec3<double> & p4,
  364. Vec3<double> & pa, Vec3<double> & pb,
  365. double & mua, double & mub)
  366. {
  367. Vec3<double> p13,p43,p21;
  368. double d1343,d4321,d1321,d4343,d2121;
  369. double numer,denom;
  370. p13.X() = p1.X() - p3.X();
  371. p13.Y() = p1.Y() - p3.Y();
  372. p13.Z() = p1.Z() - p3.Z();
  373. p43.X() = p4.X() - p3.X();
  374. p43.Y() = p4.Y() - p3.Y();
  375. p43.Z() = p4.Z() - p3.Z();
  376. if (p43.X()==0.0 && p43.Y()==0.0 && p43.Z()==0.0)
  377. return false;
  378. p21.X() = p2.X() - p1.X();
  379. p21.Y() = p2.Y() - p1.Y();
  380. p21.Z() = p2.Z() - p1.Z();
  381. if (p21.X()==0.0 && p21.Y()==0.0 && p21.Z()==0.0)
  382. return false;
  383. d1343 = p13.X() * p43.X() + p13.Y() * p43.Y() + p13.Z() * p43.Z();
  384. d4321 = p43.X() * p21.X() + p43.Y() * p21.Y() + p43.Z() * p21.Z();
  385. d1321 = p13.X() * p21.X() + p13.Y() * p21.Y() + p13.Z() * p21.Z();
  386. d4343 = p43.X() * p43.X() + p43.Y() * p43.Y() + p43.Z() * p43.Z();
  387. d2121 = p21.X() * p21.X() + p21.Y() * p21.Y() + p21.Z() * p21.Z();
  388. denom = d2121 * d4343 - d4321 * d4321;
  389. if (denom==0.0)
  390. return false;
  391. numer = d1343 * d4321 - d1321 * d4343;
  392. mua = numer / denom;
  393. mub = (d1343 + d4321 * (mua)) / d4343;
  394. pa.X() = p1.X() + mua * p21.X();
  395. pa.Y() = p1.Y() + mua * p21.Y();
  396. pa.Z() = p1.Z() + mua * p21.Z();
  397. pb.X() = p3.X() + mub * p43.X();
  398. pb.Y() = p3.Y() + mub * p43.Y();
  399. pb.Z() = p3.Z() + mub * p43.Z();
  400. return true;
  401. }
  402. long IntersectRayTriangle2(const Vec3<double> & P0, const Vec3<double> & dir,
  403. const Vec3<double> & V0, const Vec3<double> & V1,
  404. const Vec3<double> & V2, double &r)
  405. {
  406. Vec3<double> u, v, n; // triangle vectors
  407. Vec3<double> w0, w; // ray vectors
  408. double a, b; // params to calc ray-plane intersect
  409. // get triangle edge vectors and plane normal
  410. u = V1 - V0;
  411. v = V2 - V0;
  412. n = u ^ v; // cross product
  413. if (n.GetNorm() == 0.0) // triangle is degenerate
  414. return -1; // do not deal with this case
  415. w0 = P0 - V0;
  416. a = - n * w0;
  417. b = n * dir;
  418. if (fabs(b) <= 0.0) { // ray is parallel to triangle plane
  419. if (a == 0.0) // ray lies in triangle plane
  420. return 2;
  421. else return 0; // ray disjoint from plane
  422. }
  423. // get intersect point of ray with triangle plane
  424. r = a / b;
  425. if (r < 0.0) // ray goes away from triangle
  426. return 0; // => no intersect
  427. // for a segment, also test if (r > 1.0) => no intersect
  428. Vec3<double> I = P0 + r * dir; // intersect point of ray and plane
  429. // is I inside T?
  430. double uu, uv, vv, wu, wv, D;
  431. uu = u * u;
  432. uv = u * v;
  433. vv = v * v;
  434. w = I - V0;
  435. wu = w * u;
  436. wv = w * v;
  437. D = uv * uv - uu * vv;
  438. // get and test parametric coords
  439. double s, t;
  440. s = (uv * wv - vv * wu) / D;
  441. if (s < 0.0 || s > 1.0) // I is outside T
  442. return 0;
  443. t = (uv * wu - uu * wv) / D;
  444. if (t < 0.0 || (s + t) > 1.0) // I is outside T
  445. return 0;
  446. return 1; // I is in T
  447. }
  448. bool TMMesh::CheckConsistancy()
  449. {
  450. size_t nE = m_edges.GetSize();
  451. size_t nT = m_triangles.GetSize();
  452. for(size_t e = 0; e < nE; e++)
  453. {
  454. for(int f = 0; f < 2; f++)
  455. {
  456. if (!m_edges.GetHead()->GetData().m_triangles[f])
  457. {
  458. return false;
  459. }
  460. }
  461. m_edges.Next();
  462. }
  463. for(size_t f = 0; f < nT; f++)
  464. {
  465. for(int e = 0; e < 3; e++)
  466. {
  467. int found = 0;
  468. for(int k = 0; k < 2; k++)
  469. {
  470. if (m_triangles.GetHead()->GetData().m_edges[e]->GetData().m_triangles[k] == m_triangles.GetHead())
  471. {
  472. found++;
  473. }
  474. }
  475. if (found != 1)
  476. {
  477. return false;
  478. }
  479. }
  480. m_triangles.Next();
  481. }
  482. return true;
  483. }
  484. bool TMMesh::Normalize()
  485. {
  486. size_t nV = m_vertices.GetSize();
  487. if (nV == 0)
  488. {
  489. return false;
  490. }
  491. m_barycenter = m_vertices.GetHead()->GetData().m_pos;
  492. Vec3<Real> min = m_barycenter;
  493. Vec3<Real> max = m_barycenter;
  494. Real x, y, z;
  495. for(size_t v = 1; v < nV; v++)
  496. {
  497. m_barycenter += m_vertices.GetHead()->GetData().m_pos;
  498. x = m_vertices.GetHead()->GetData().m_pos.X();
  499. y = m_vertices.GetHead()->GetData().m_pos.Y();
  500. z = m_vertices.GetHead()->GetData().m_pos.Z();
  501. if ( x < min.X()) min.X() = x;
  502. else if ( x > max.X()) max.X() = x;
  503. if ( y < min.Y()) min.Y() = y;
  504. else if ( y > max.Y()) max.Y() = y;
  505. if ( z < min.Z()) min.Z() = z;
  506. else if ( z > max.Z()) max.Z() = z;
  507. m_vertices.Next();
  508. }
  509. m_barycenter /= static_cast<Real>(nV);
  510. m_diag = static_cast<Real>(0.001 * (max-min).GetNorm());
  511. const Real invDiag = static_cast<Real>(1.0 / m_diag);
  512. if (m_diag != 0.0)
  513. {
  514. for(size_t v = 0; v < nV; v++)
  515. {
  516. m_vertices.GetHead()->GetData().m_pos = (m_vertices.GetHead()->GetData().m_pos - m_barycenter) * invDiag;
  517. m_vertices.Next();
  518. }
  519. }
  520. return true;
  521. }
  522. bool TMMesh::Denormalize()
  523. {
  524. size_t nV = m_vertices.GetSize();
  525. if (nV == 0)
  526. {
  527. return false;
  528. }
  529. if (m_diag != 0.0)
  530. {
  531. for(size_t v = 0; v < nV; v++)
  532. {
  533. m_vertices.GetHead()->GetData().m_pos = m_vertices.GetHead()->GetData().m_pos * m_diag + m_barycenter;
  534. m_vertices.Next();
  535. }
  536. }
  537. return false;
  538. }
  539. }