vhacdVolume.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #pragma once
  10. #ifndef VHACD_VOLUME_H
  11. #define VHACD_VOLUME_H
  12. #include "vhacdMesh.h"
  13. #include "vhacdVector.h"
  14. #include <assert.h>
  15. #ifdef _MSC_VER
  16. #pragma warning(push)
  17. #pragma warning(disable:4456 4701)
  18. #endif
  19. namespace VHACD {
  20. enum VOXEL_VALUE {
  21. PRIMITIVE_UNDEFINED = 0,
  22. PRIMITIVE_OUTSIDE_SURFACE = 1,
  23. PRIMITIVE_INSIDE_SURFACE = 2,
  24. PRIMITIVE_ON_SURFACE = 3
  25. };
  26. struct Voxel {
  27. public:
  28. short m_coord[3];
  29. short m_data;
  30. };
  31. class PrimitiveSet {
  32. public:
  33. virtual ~PrimitiveSet(){};
  34. virtual PrimitiveSet* Create() const = 0;
  35. virtual const size_t GetNPrimitives() const = 0;
  36. virtual const size_t GetNPrimitivesOnSurf() const = 0;
  37. virtual const size_t GetNPrimitivesInsideSurf() const = 0;
  38. virtual const double GetEigenValue(AXIS axis) const = 0;
  39. virtual const double ComputeMaxVolumeError() const = 0;
  40. virtual const double ComputeVolume() const = 0;
  41. virtual void Clip(const Plane& plane, PrimitiveSet* const positivePart,
  42. PrimitiveSet* const negativePart) const = 0;
  43. virtual void Intersect(const Plane& plane, SArray<Vec3<double> >* const positivePts,
  44. SArray<Vec3<double> >* const negativePts, const size_t sampling) const = 0;
  45. virtual void ComputeExteriorPoints(const Plane& plane, const Mesh& mesh,
  46. SArray<Vec3<double> >* const exteriorPts) const = 0;
  47. virtual void ComputeClippedVolumes(const Plane& plane, double& positiveVolume,
  48. double& negativeVolume) const = 0;
  49. virtual void SelectOnSurface(PrimitiveSet* const onSurfP) const = 0;
  50. virtual void ComputeConvexHull(Mesh& meshCH, const size_t sampling = 1) const = 0;
  51. virtual void ComputeBB() = 0;
  52. virtual void ComputePrincipalAxes() = 0;
  53. virtual void AlignToPrincipalAxes() = 0;
  54. virtual void RevertAlignToPrincipalAxes() = 0;
  55. virtual void Convert(Mesh& mesh, const VOXEL_VALUE value) const = 0;
  56. const Mesh& GetConvexHull() const { return m_convexHull; };
  57. Mesh& GetConvexHull() { return m_convexHull; };
  58. private:
  59. Mesh m_convexHull;
  60. };
  61. //!
  62. class VoxelSet : public PrimitiveSet {
  63. friend class Volume;
  64. public:
  65. //! Destructor.
  66. ~VoxelSet(void);
  67. //! Constructor.
  68. VoxelSet();
  69. const size_t GetNPrimitives() const { return m_voxels.Size(); }
  70. const size_t GetNPrimitivesOnSurf() const { return m_numVoxelsOnSurface; }
  71. const size_t GetNPrimitivesInsideSurf() const { return m_numVoxelsInsideSurface; }
  72. const double GetEigenValue(AXIS axis) const { return m_D[axis][axis]; }
  73. const double ComputeVolume() const { return m_unitVolume * m_voxels.Size(); }
  74. const double ComputeMaxVolumeError() const { return m_unitVolume * m_numVoxelsOnSurface; }
  75. const Vec3<short>& GetMinBBVoxels() const { return m_minBBVoxels; }
  76. const Vec3<short>& GetMaxBBVoxels() const { return m_maxBBVoxels; }
  77. const Vec3<double>& GetMinBB() const { return m_minBB; }
  78. const double& GetScale() const { return m_scale; }
  79. const double& GetUnitVolume() const { return m_unitVolume; }
  80. Vec3<double> GetPoint(Vec3<short> voxel) const
  81. {
  82. return Vec3<double>(voxel[0] * m_scale + m_minBB[0],
  83. voxel[1] * m_scale + m_minBB[1],
  84. voxel[2] * m_scale + m_minBB[2]);
  85. }
  86. Vec3<double> GetPoint(const Voxel& voxel) const
  87. {
  88. return Vec3<double>(voxel.m_coord[0] * m_scale + m_minBB[0],
  89. voxel.m_coord[1] * m_scale + m_minBB[1],
  90. voxel.m_coord[2] * m_scale + m_minBB[2]);
  91. }
  92. Vec3<double> GetPoint(Vec3<double> voxel) const
  93. {
  94. return Vec3<double>(voxel[0] * m_scale + m_minBB[0],
  95. voxel[1] * m_scale + m_minBB[1],
  96. voxel[2] * m_scale + m_minBB[2]);
  97. }
  98. void GetPoints(const Voxel& voxel, Vec3<double>* const pts) const;
  99. void ComputeConvexHull(Mesh& meshCH, const size_t sampling = 1) const;
  100. void Clip(const Plane& plane, PrimitiveSet* const positivePart, PrimitiveSet* const negativePart) const;
  101. void Intersect(const Plane& plane, SArray<Vec3<double> >* const positivePts,
  102. SArray<Vec3<double> >* const negativePts, const size_t sampling) const;
  103. void ComputeExteriorPoints(const Plane& plane, const Mesh& mesh,
  104. SArray<Vec3<double> >* const exteriorPts) const;
  105. void ComputeClippedVolumes(const Plane& plane, double& positiveVolume, double& negativeVolume) const;
  106. void SelectOnSurface(PrimitiveSet* const onSurfP) const;
  107. void ComputeBB();
  108. void Convert(Mesh& mesh, const VOXEL_VALUE value) const;
  109. void ComputePrincipalAxes();
  110. PrimitiveSet* Create() const
  111. {
  112. return new VoxelSet();
  113. }
  114. void AlignToPrincipalAxes(){};
  115. void RevertAlignToPrincipalAxes(){};
  116. Voxel* const GetVoxels() { return m_voxels.Data(); }
  117. const Voxel* const GetVoxels() const { return m_voxels.Data(); }
  118. private:
  119. size_t m_numVoxelsOnSurface;
  120. size_t m_numVoxelsInsideSurface;
  121. Vec3<double> m_minBB;
  122. double m_scale;
  123. SArray<Voxel, 8> m_voxels;
  124. double m_unitVolume;
  125. Vec3<double> m_minBBPts;
  126. Vec3<double> m_maxBBPts;
  127. Vec3<short> m_minBBVoxels;
  128. Vec3<short> m_maxBBVoxels;
  129. Vec3<short> m_barycenter;
  130. double m_Q[3][3];
  131. double m_D[3][3];
  132. Vec3<double> m_barycenterPCA;
  133. };
  134. struct Tetrahedron {
  135. public:
  136. Vec3<double> m_pts[4];
  137. unsigned char m_data;
  138. };
  139. //!
  140. class TetrahedronSet : public PrimitiveSet {
  141. friend class Volume;
  142. public:
  143. //! Destructor.
  144. ~TetrahedronSet(void);
  145. //! Constructor.
  146. TetrahedronSet();
  147. const size_t GetNPrimitives() const { return m_tetrahedra.Size(); }
  148. const size_t GetNPrimitivesOnSurf() const { return m_numTetrahedraOnSurface; }
  149. const size_t GetNPrimitivesInsideSurf() const { return m_numTetrahedraInsideSurface; }
  150. const Vec3<double>& GetMinBB() const { return m_minBB; }
  151. const Vec3<double>& GetMaxBB() const { return m_maxBB; }
  152. const Vec3<double>& GetBarycenter() const { return m_barycenter; }
  153. const double GetEigenValue(AXIS axis) const { return m_D[axis][axis]; }
  154. const double GetSacle() const { return m_scale; }
  155. const double ComputeVolume() const;
  156. const double ComputeMaxVolumeError() const;
  157. void ComputeConvexHull(Mesh& meshCH, const size_t sampling = 1) const;
  158. void ComputePrincipalAxes();
  159. void AlignToPrincipalAxes();
  160. void RevertAlignToPrincipalAxes();
  161. void Clip(const Plane& plane, PrimitiveSet* const positivePart, PrimitiveSet* const negativePart) const;
  162. void Intersect(const Plane& plane, SArray<Vec3<double> >* const positivePts,
  163. SArray<Vec3<double> >* const negativePts, const size_t sampling) const;
  164. void ComputeExteriorPoints(const Plane& plane, const Mesh& mesh,
  165. SArray<Vec3<double> >* const exteriorPts) const;
  166. void ComputeClippedVolumes(const Plane& plane, double& positiveVolume, double& negativeVolume) const;
  167. void SelectOnSurface(PrimitiveSet* const onSurfP) const;
  168. void ComputeBB();
  169. void Convert(Mesh& mesh, const VOXEL_VALUE value) const;
  170. inline bool Add(Tetrahedron& tetrahedron);
  171. PrimitiveSet* Create() const
  172. {
  173. return new TetrahedronSet();
  174. }
  175. static const double EPS;
  176. private:
  177. void AddClippedTetrahedra(const Vec3<double> (&pts)[10], const int32_t nPts);
  178. size_t m_numTetrahedraOnSurface;
  179. size_t m_numTetrahedraInsideSurface;
  180. double m_scale;
  181. Vec3<double> m_minBB;
  182. Vec3<double> m_maxBB;
  183. Vec3<double> m_barycenter;
  184. SArray<Tetrahedron, 8> m_tetrahedra;
  185. double m_Q[3][3];
  186. double m_D[3][3];
  187. };
  188. //!
  189. class Volume {
  190. public:
  191. //! Destructor.
  192. ~Volume(void);
  193. //! Constructor.
  194. Volume();
  195. //! Voxelize
  196. template <class T>
  197. void Voxelize(const T* const points, const uint32_t stridePoints, const uint32_t nPoints,
  198. const int32_t* const triangles, const uint32_t strideTriangles, const uint32_t nTriangles,
  199. const size_t dim, const Vec3<double>& barycenter, const double (&rot)[3][3]);
  200. unsigned char& GetVoxel(const size_t i, const size_t j, const size_t k)
  201. {
  202. assert(i < m_dim[0] || i >= 0);
  203. assert(j < m_dim[0] || j >= 0);
  204. assert(k < m_dim[0] || k >= 0);
  205. return m_data[i + j * m_dim[0] + k * m_dim[0] * m_dim[1]];
  206. }
  207. const unsigned char& GetVoxel(const size_t i, const size_t j, const size_t k) const
  208. {
  209. assert(i < m_dim[0] || i >= 0);
  210. assert(j < m_dim[0] || j >= 0);
  211. assert(k < m_dim[0] || k >= 0);
  212. return m_data[i + j * m_dim[0] + k * m_dim[0] * m_dim[1]];
  213. }
  214. const size_t GetNPrimitivesOnSurf() const { return m_numVoxelsOnSurface; }
  215. const size_t GetNPrimitivesInsideSurf() const { return m_numVoxelsInsideSurface; }
  216. void Convert(Mesh& mesh, const VOXEL_VALUE value) const;
  217. void Convert(VoxelSet& vset) const;
  218. void Convert(TetrahedronSet& tset) const;
  219. void AlignToPrincipalAxes(double (&rot)[3][3]) const;
  220. private:
  221. void FillOutsideSurface(const size_t i0, const size_t j0, const size_t k0, const size_t i1,
  222. const size_t j1, const size_t k1);
  223. void FillInsideSurface();
  224. template <class T>
  225. void ComputeBB(const T* const points, const uint32_t stridePoints, const uint32_t nPoints,
  226. const Vec3<double>& barycenter, const double (&rot)[3][3]);
  227. void Allocate();
  228. void Free();
  229. Vec3<double> m_minBB;
  230. Vec3<double> m_maxBB;
  231. double m_scale;
  232. size_t m_dim[3]; //>! dim
  233. size_t m_numVoxelsOnSurface;
  234. size_t m_numVoxelsInsideSurface;
  235. size_t m_numVoxelsOutsideSurface;
  236. unsigned char* m_data;
  237. };
  238. int32_t TriBoxOverlap(const Vec3<double>& boxcenter, const Vec3<double>& boxhalfsize, const Vec3<double>& triver0,
  239. const Vec3<double>& triver1, const Vec3<double>& triver2);
  240. template <class T>
  241. inline void ComputeAlignedPoint(const T* const points, const uint32_t idx, const Vec3<double>& barycenter,
  242. const double (&rot)[3][3], Vec3<double>& pt){};
  243. template <>
  244. inline void ComputeAlignedPoint<float>(const float* const points, const uint32_t idx, const Vec3<double>& barycenter, const double (&rot)[3][3], Vec3<double>& pt)
  245. {
  246. double x = points[idx + 0] - barycenter[0];
  247. double y = points[idx + 1] - barycenter[1];
  248. double z = points[idx + 2] - barycenter[2];
  249. pt[0] = rot[0][0] * x + rot[1][0] * y + rot[2][0] * z;
  250. pt[1] = rot[0][1] * x + rot[1][1] * y + rot[2][1] * z;
  251. pt[2] = rot[0][2] * x + rot[1][2] * y + rot[2][2] * z;
  252. }
  253. template <>
  254. inline void ComputeAlignedPoint<double>(const double* const points, const uint32_t idx, const Vec3<double>& barycenter, const double (&rot)[3][3], Vec3<double>& pt)
  255. {
  256. double x = points[idx + 0] - barycenter[0];
  257. double y = points[idx + 1] - barycenter[1];
  258. double z = points[idx + 2] - barycenter[2];
  259. pt[0] = rot[0][0] * x + rot[1][0] * y + rot[2][0] * z;
  260. pt[1] = rot[0][1] * x + rot[1][1] * y + rot[2][1] * z;
  261. pt[2] = rot[0][2] * x + rot[1][2] * y + rot[2][2] * z;
  262. }
  263. template <class T>
  264. void Volume::ComputeBB(const T* const points, const uint32_t stridePoints, const uint32_t nPoints,
  265. const Vec3<double>& barycenter, const double (&rot)[3][3])
  266. {
  267. Vec3<double> pt;
  268. ComputeAlignedPoint(points, 0, barycenter, rot, pt);
  269. m_maxBB = pt;
  270. m_minBB = pt;
  271. for (uint32_t v = 1; v < nPoints; ++v) {
  272. ComputeAlignedPoint(points, v * stridePoints, barycenter, rot, pt);
  273. for (int32_t i = 0; i < 3; ++i) {
  274. if (pt[i] < m_minBB[i])
  275. m_minBB[i] = pt[i];
  276. else if (pt[i] > m_maxBB[i])
  277. m_maxBB[i] = pt[i];
  278. }
  279. }
  280. }
  281. template <class T>
  282. void Volume::Voxelize(const T* const points, const uint32_t stridePoints, const uint32_t nPoints,
  283. const int32_t* const triangles, const uint32_t strideTriangles, const uint32_t nTriangles,
  284. const size_t dim, const Vec3<double>& barycenter, const double (&rot)[3][3])
  285. {
  286. if (nPoints == 0) {
  287. return;
  288. }
  289. ComputeBB(points, stridePoints, nPoints, barycenter, rot);
  290. double d[3] = { m_maxBB[0] - m_minBB[0], m_maxBB[1] - m_minBB[1], m_maxBB[2] - m_minBB[2] };
  291. double r;
  292. if (d[0] >= d[1] && d[0] >= d[2]) {
  293. r = d[0];
  294. m_dim[0] = dim;
  295. m_dim[1] = 2 + static_cast<size_t>(dim * d[1] / d[0]);
  296. m_dim[2] = 2 + static_cast<size_t>(dim * d[2] / d[0]);
  297. }
  298. else if (d[1] >= d[0] && d[1] >= d[2]) {
  299. r = d[1];
  300. m_dim[1] = dim;
  301. m_dim[0] = 2 + static_cast<size_t>(dim * d[0] / d[1]);
  302. m_dim[2] = 2 + static_cast<size_t>(dim * d[2] / d[1]);
  303. }
  304. else {
  305. r = d[2];
  306. m_dim[2] = dim;
  307. m_dim[0] = 2 + static_cast<size_t>(dim * d[0] / d[2]);
  308. m_dim[1] = 2 + static_cast<size_t>(dim * d[1] / d[2]);
  309. }
  310. m_scale = r / (dim - 1);
  311. double invScale = (dim - 1) / r;
  312. Allocate();
  313. m_numVoxelsOnSurface = 0;
  314. m_numVoxelsInsideSurface = 0;
  315. m_numVoxelsOutsideSurface = 0;
  316. Vec3<double> p[3];
  317. size_t i, j, k;
  318. size_t i0, j0, k0;
  319. size_t i1, j1, k1;
  320. Vec3<double> boxcenter;
  321. Vec3<double> pt;
  322. const Vec3<double> boxhalfsize(0.5, 0.5, 0.5);
  323. for (size_t t = 0, ti = 0; t < nTriangles; ++t, ti += strideTriangles) {
  324. Vec3<int32_t> tri(triangles[ti + 0],
  325. triangles[ti + 1],
  326. triangles[ti + 2]);
  327. for (int32_t c = 0; c < 3; ++c) {
  328. ComputeAlignedPoint(points, tri[c] * stridePoints, barycenter, rot, pt);
  329. p[c][0] = (pt[0] - m_minBB[0]) * invScale;
  330. p[c][1] = (pt[1] - m_minBB[1]) * invScale;
  331. p[c][2] = (pt[2] - m_minBB[2]) * invScale;
  332. i = static_cast<size_t>(p[c][0] + 0.5);
  333. j = static_cast<size_t>(p[c][1] + 0.5);
  334. k = static_cast<size_t>(p[c][2] + 0.5);
  335. assert(i < m_dim[0] && i >= 0 && j < m_dim[1] && j >= 0 && k < m_dim[2] && k >= 0);
  336. if (c == 0) {
  337. i0 = i1 = i;
  338. j0 = j1 = j;
  339. k0 = k1 = k;
  340. }
  341. else {
  342. if (i < i0)
  343. i0 = i;
  344. if (j < j0)
  345. j0 = j;
  346. if (k < k0)
  347. k0 = k;
  348. if (i > i1)
  349. i1 = i;
  350. if (j > j1)
  351. j1 = j;
  352. if (k > k1)
  353. k1 = k;
  354. }
  355. }
  356. if (i0 > 0)
  357. --i0;
  358. if (j0 > 0)
  359. --j0;
  360. if (k0 > 0)
  361. --k0;
  362. if (i1 < m_dim[0])
  363. ++i1;
  364. if (j1 < m_dim[1])
  365. ++j1;
  366. if (k1 < m_dim[2])
  367. ++k1;
  368. for (size_t i = i0; i < i1; ++i) {
  369. boxcenter[0] = (double)i;
  370. for (size_t j = j0; j < j1; ++j) {
  371. boxcenter[1] = (double)j;
  372. for (size_t k = k0; k < k1; ++k) {
  373. boxcenter[2] = (double)k;
  374. int32_t res = TriBoxOverlap(boxcenter, boxhalfsize, p[0], p[1], p[2]);
  375. unsigned char& value = GetVoxel(i, j, k);
  376. if (res == 1 && value == PRIMITIVE_UNDEFINED) {
  377. value = PRIMITIVE_ON_SURFACE;
  378. ++m_numVoxelsOnSurface;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. FillOutsideSurface(0, 0, 0, m_dim[0], m_dim[1], 1);
  385. FillOutsideSurface(0, 0, m_dim[2] - 1, m_dim[0], m_dim[1], m_dim[2]);
  386. FillOutsideSurface(0, 0, 0, m_dim[0], 1, m_dim[2]);
  387. FillOutsideSurface(0, m_dim[1] - 1, 0, m_dim[0], m_dim[1], m_dim[2]);
  388. FillOutsideSurface(0, 0, 0, 1, m_dim[1], m_dim[2]);
  389. FillOutsideSurface(m_dim[0] - 1, 0, 0, m_dim[0], m_dim[1], m_dim[2]);
  390. FillInsideSurface();
  391. }
  392. }
  393. #ifdef _MSC_VER
  394. #pragma warning(pop)
  395. #endif
  396. #endif // VHACD_VOLUME_H