vhacdMesh.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #define _CRT_SECURE_NO_WARNINGS
  10. #include "btConvexHullComputer.h"
  11. #include "vhacdMesh.h"
  12. #include <fstream>
  13. #include <iosfwd>
  14. #include <iostream>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string>
  18. namespace VHACD {
  19. Mesh::Mesh()
  20. {
  21. m_diag = 1.0;
  22. }
  23. Mesh::~Mesh()
  24. {
  25. }
  26. double Mesh::ComputeVolume() const
  27. {
  28. const size_t nV = GetNPoints();
  29. const size_t nT = GetNTriangles();
  30. if (nV == 0 || nT == 0) {
  31. return 0.0;
  32. }
  33. Vec3<double> bary(0.0, 0.0, 0.0);
  34. for (size_t v = 0; v < nV; v++) {
  35. bary += GetPoint(v);
  36. }
  37. bary /= static_cast<double>(nV);
  38. Vec3<double> ver0, ver1, ver2;
  39. double totalVolume = 0.0;
  40. for (int t = 0; t < nT; t++) {
  41. const Vec3<int>& tri = GetTriangle(t);
  42. ver0 = GetPoint(tri[0]);
  43. ver1 = GetPoint(tri[1]);
  44. ver2 = GetPoint(tri[2]);
  45. totalVolume += ComputeVolume4(ver0, ver1, ver2, bary);
  46. }
  47. return totalVolume / 6.0;
  48. }
  49. void Mesh::ComputeConvexHull(const double* const pts,
  50. const size_t nPts)
  51. {
  52. ResizePoints(0);
  53. ResizeTriangles(0);
  54. btConvexHullComputer ch;
  55. ch.compute(pts, 3 * sizeof(double), (int)nPts, -1.0, -1.0);
  56. for (int v = 0; v < ch.vertices.size(); v++) {
  57. AddPoint(Vec3<double>(ch.vertices[v].getX(), ch.vertices[v].getY(), ch.vertices[v].getZ()));
  58. }
  59. const int nt = ch.faces.size();
  60. for (int t = 0; t < nt; ++t) {
  61. const btConvexHullComputer::Edge* sourceEdge = &(ch.edges[ch.faces[t]]);
  62. int a = sourceEdge->getSourceVertex();
  63. int b = sourceEdge->getTargetVertex();
  64. const btConvexHullComputer::Edge* edge = sourceEdge->getNextEdgeOfFace();
  65. int c = edge->getTargetVertex();
  66. while (c != a) {
  67. AddTriangle(Vec3<int>(a, b, c));
  68. edge = edge->getNextEdgeOfFace();
  69. b = c;
  70. c = edge->getTargetVertex();
  71. }
  72. }
  73. }
  74. void Mesh::Clip(const Plane& plane,
  75. SArray<Vec3<double> >& positivePart,
  76. SArray<Vec3<double> >& negativePart) const
  77. {
  78. const size_t nV = GetNPoints();
  79. if (nV == 0) {
  80. return;
  81. }
  82. double d;
  83. for (size_t v = 0; v < nV; v++) {
  84. const Vec3<double>& pt = GetPoint(v);
  85. d = plane.m_a * pt[0] + plane.m_b * pt[1] + plane.m_c * pt[2] + plane.m_d;
  86. if (d > 0.0) {
  87. positivePart.PushBack(pt);
  88. }
  89. else if (d < 0.0) {
  90. negativePart.PushBack(pt);
  91. }
  92. else {
  93. positivePart.PushBack(pt);
  94. negativePart.PushBack(pt);
  95. }
  96. }
  97. }
  98. bool Mesh::IsInside(const Vec3<double>& pt) const
  99. {
  100. const size_t nV = GetNPoints();
  101. const size_t nT = GetNTriangles();
  102. if (nV == 0 || nT == 0) {
  103. return false;
  104. }
  105. Vec3<double> ver0, ver1, ver2;
  106. double volume;
  107. for (int t = 0; t < nT; t++) {
  108. const Vec3<int>& tri = GetTriangle(t);
  109. ver0 = GetPoint(tri[0]);
  110. ver1 = GetPoint(tri[1]);
  111. ver2 = GetPoint(tri[2]);
  112. volume = ComputeVolume4(ver0, ver1, ver2, pt);
  113. if (volume < 0.0) {
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. double Mesh::ComputeDiagBB()
  120. {
  121. const size_t nPoints = GetNPoints();
  122. if (nPoints == 0)
  123. return 0.0;
  124. Vec3<double> minBB = m_points[0];
  125. Vec3<double> maxBB = m_points[0];
  126. double x, y, z;
  127. for (size_t v = 1; v < nPoints; v++) {
  128. x = m_points[v][0];
  129. y = m_points[v][1];
  130. z = m_points[v][2];
  131. if (x < minBB[0])
  132. minBB[0] = x;
  133. else if (x > maxBB[0])
  134. maxBB[0] = x;
  135. if (y < minBB[1])
  136. minBB[1] = y;
  137. else if (y > maxBB[1])
  138. maxBB[1] = y;
  139. if (z < minBB[2])
  140. minBB[2] = z;
  141. else if (z > maxBB[2])
  142. maxBB[2] = z;
  143. }
  144. return (m_diag = (maxBB - minBB).GetNorm());
  145. }
  146. #ifdef VHACD_DEBUG_MESH
  147. bool Mesh::SaveVRML2(const std::string& fileName) const
  148. {
  149. std::ofstream fout(fileName.c_str());
  150. if (fout.is_open()) {
  151. const Material material;
  152. if (SaveVRML2(fout, material)) {
  153. fout.close();
  154. return true;
  155. }
  156. return false;
  157. }
  158. return false;
  159. }
  160. bool Mesh::SaveVRML2(std::ofstream& fout, const Material& material) const
  161. {
  162. if (fout.is_open()) {
  163. fout.setf(std::ios::fixed, std::ios::floatfield);
  164. fout.setf(std::ios::showpoint);
  165. fout.precision(6);
  166. size_t nV = m_points.Size();
  167. size_t nT = m_triangles.Size();
  168. fout << "#VRML V2.0 utf8" << std::endl;
  169. fout << "" << std::endl;
  170. fout << "# Vertices: " << nV << std::endl;
  171. fout << "# Triangles: " << nT << std::endl;
  172. fout << "" << std::endl;
  173. fout << "Group {" << std::endl;
  174. fout << " children [" << std::endl;
  175. fout << " Shape {" << std::endl;
  176. fout << " appearance Appearance {" << std::endl;
  177. fout << " material Material {" << std::endl;
  178. fout << " diffuseColor " << material.m_diffuseColor[0] << " "
  179. << material.m_diffuseColor[1] << " "
  180. << material.m_diffuseColor[2] << std::endl;
  181. fout << " ambientIntensity " << material.m_ambientIntensity << std::endl;
  182. fout << " specularColor " << material.m_specularColor[0] << " "
  183. << material.m_specularColor[1] << " "
  184. << material.m_specularColor[2] << std::endl;
  185. fout << " emissiveColor " << material.m_emissiveColor[0] << " "
  186. << material.m_emissiveColor[1] << " "
  187. << material.m_emissiveColor[2] << std::endl;
  188. fout << " shininess " << material.m_shininess << std::endl;
  189. fout << " transparency " << material.m_transparency << std::endl;
  190. fout << " }" << std::endl;
  191. fout << " }" << std::endl;
  192. fout << " geometry IndexedFaceSet {" << std::endl;
  193. fout << " ccw TRUE" << std::endl;
  194. fout << " solid TRUE" << std::endl;
  195. fout << " convex TRUE" << std::endl;
  196. if (nV > 0) {
  197. fout << " coord DEF co Coordinate {" << std::endl;
  198. fout << " point [" << std::endl;
  199. for (size_t v = 0; v < nV; v++) {
  200. fout << " " << m_points[v][0] << " "
  201. << m_points[v][1] << " "
  202. << m_points[v][2] << "," << std::endl;
  203. }
  204. fout << " ]" << std::endl;
  205. fout << " }" << std::endl;
  206. }
  207. if (nT > 0) {
  208. fout << " coordIndex [ " << std::endl;
  209. for (size_t f = 0; f < nT; f++) {
  210. fout << " " << m_triangles[f][0] << ", "
  211. << m_triangles[f][1] << ", "
  212. << m_triangles[f][2] << ", -1," << std::endl;
  213. }
  214. fout << " ]" << std::endl;
  215. }
  216. fout << " }" << std::endl;
  217. fout << " }" << std::endl;
  218. fout << " ]" << std::endl;
  219. fout << "}" << std::endl;
  220. return true;
  221. }
  222. return false;
  223. }
  224. bool Mesh::SaveOFF(const std::string& fileName) const
  225. {
  226. std::ofstream fout(fileName.c_str());
  227. if (fout.is_open()) {
  228. size_t nV = m_points.Size();
  229. size_t nT = m_triangles.Size();
  230. fout << "OFF" << std::endl;
  231. fout << nV << " " << nT << " " << 0 << std::endl;
  232. for (size_t v = 0; v < nV; v++) {
  233. fout << m_points[v][0] << " "
  234. << m_points[v][1] << " "
  235. << m_points[v][2] << std::endl;
  236. }
  237. for (size_t f = 0; f < nT; f++) {
  238. fout << "3 " << m_triangles[f][0] << " "
  239. << m_triangles[f][1] << " "
  240. << m_triangles[f][2] << std::endl;
  241. }
  242. fout.close();
  243. return true;
  244. }
  245. return false;
  246. }
  247. bool Mesh::LoadOFF(const std::string& fileName, bool invert)
  248. {
  249. FILE* fid = fopen(fileName.c_str(), "r");
  250. if (fid) {
  251. const std::string strOFF("OFF");
  252. char temp[1024];
  253. fscanf(fid, "%s", temp);
  254. if (std::string(temp) != strOFF) {
  255. fclose(fid);
  256. return false;
  257. }
  258. else {
  259. int nv = 0;
  260. int nf = 0;
  261. int ne = 0;
  262. fscanf(fid, "%i", &nv);
  263. fscanf(fid, "%i", &nf);
  264. fscanf(fid, "%i", &ne);
  265. m_points.Resize(nv);
  266. m_triangles.Resize(nf);
  267. Vec3<double> coord;
  268. float x, y, z;
  269. for (int p = 0; p < nv; p++) {
  270. fscanf(fid, "%f", &x);
  271. fscanf(fid, "%f", &y);
  272. fscanf(fid, "%f", &z);
  273. m_points[p][0] = x;
  274. m_points[p][1] = y;
  275. m_points[p][2] = z;
  276. }
  277. int i, j, k, s;
  278. for (int t = 0; t < nf; ++t) {
  279. fscanf(fid, "%i", &s);
  280. if (s == 3) {
  281. fscanf(fid, "%i", &i);
  282. fscanf(fid, "%i", &j);
  283. fscanf(fid, "%i", &k);
  284. m_triangles[t][0] = i;
  285. if (invert) {
  286. m_triangles[t][1] = k;
  287. m_triangles[t][2] = j;
  288. }
  289. else {
  290. m_triangles[t][1] = j;
  291. m_triangles[t][2] = k;
  292. }
  293. }
  294. else // Fix me: support only triangular meshes
  295. {
  296. for (int h = 0; h < s; ++h)
  297. fscanf(fid, "%i", &s);
  298. }
  299. }
  300. fclose(fid);
  301. }
  302. }
  303. else {
  304. return false;
  305. }
  306. return true;
  307. }
  308. #endif // VHACD_DEBUG_MESH
  309. }