vhacdVHACD.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_VHACD_H
  11. #define VHACD_VHACD_H
  12. #ifdef OPENCL_FOUND
  13. #ifdef __MACH__
  14. #include <OpenCL/cl.h>
  15. #else
  16. #include <CL/cl.h>
  17. #endif
  18. #endif //OPENCL_FOUND
  19. #include "vhacdMutex.h"
  20. #include "vhacdVolume.h"
  21. #include "vhacdRaycastMesh.h"
  22. #include <vector>
  23. #define USE_THREAD 1
  24. #define OCL_MIN_NUM_PRIMITIVES 4096
  25. #define CH_APP_MIN_NUM_PRIMITIVES 64000
  26. namespace VHACD {
  27. class VHACD : public IVHACD {
  28. public:
  29. //! Constructor.
  30. VHACD()
  31. {
  32. #if USE_THREAD == 1 && _OPENMP
  33. m_ompNumProcessors = 2 * omp_get_num_procs();
  34. omp_set_num_threads(m_ompNumProcessors);
  35. #else //USE_THREAD == 1 && _OPENMP
  36. m_ompNumProcessors = 1;
  37. #endif //USE_THREAD == 1 && _OPENMP
  38. #ifdef CL_VERSION_1_1
  39. m_oclWorkGroupSize = 0;
  40. m_oclDevice = 0;
  41. m_oclQueue = 0;
  42. m_oclKernelComputePartialVolumes = 0;
  43. m_oclKernelComputeSum = 0;
  44. #endif //CL_VERSION_1_1
  45. Init();
  46. }
  47. //! Destructor.
  48. ~VHACD(void)
  49. {
  50. }
  51. uint32_t GetNConvexHulls() const
  52. {
  53. return (uint32_t)m_convexHulls.Size();
  54. }
  55. void Cancel()
  56. {
  57. SetCancel(true);
  58. }
  59. void GetConvexHull(const uint32_t index, ConvexHull& ch) const
  60. {
  61. Mesh* mesh = m_convexHulls[index];
  62. ch.m_nPoints = (uint32_t)mesh->GetNPoints();
  63. ch.m_nTriangles = (uint32_t)mesh->GetNTriangles();
  64. ch.m_points = mesh->GetPoints();
  65. ch.m_triangles = (uint32_t *)mesh->GetTriangles();
  66. ch.m_volume = mesh->ComputeVolume();
  67. Vec3<double> &center = mesh->ComputeCenter();
  68. ch.m_center[0] = center.X();
  69. ch.m_center[1] = center.Y();
  70. ch.m_center[2] = center.Z();
  71. }
  72. void Clean(void)
  73. {
  74. if (mRaycastMesh)
  75. {
  76. mRaycastMesh->release();
  77. mRaycastMesh = nullptr;
  78. }
  79. delete m_volume;
  80. delete m_pset;
  81. size_t nCH = m_convexHulls.Size();
  82. for (size_t p = 0; p < nCH; ++p) {
  83. delete m_convexHulls[p];
  84. }
  85. m_convexHulls.Clear();
  86. Init();
  87. }
  88. void Release(void)
  89. {
  90. delete this;
  91. }
  92. bool Compute(const float* const points,
  93. const uint32_t nPoints,
  94. const uint32_t* const triangles,
  95. const uint32_t nTriangles,
  96. const Parameters& params);
  97. bool Compute(const double* const points,
  98. const uint32_t nPoints,
  99. const uint32_t* const triangles,
  100. const uint32_t nTriangles,
  101. const Parameters& params);
  102. bool OCLInit(void* const oclDevice,
  103. IUserLogger* const logger = 0);
  104. bool OCLRelease(IUserLogger* const logger = 0);
  105. virtual bool ComputeCenterOfMass(double centerOfMass[3]) const;
  106. private:
  107. void SetCancel(bool cancel)
  108. {
  109. m_cancelMutex.Lock();
  110. m_cancel = cancel;
  111. m_cancelMutex.Unlock();
  112. }
  113. bool GetCancel()
  114. {
  115. m_cancelMutex.Lock();
  116. bool cancel = m_cancel;
  117. m_cancelMutex.Unlock();
  118. return cancel;
  119. }
  120. void Update(const double stageProgress,
  121. const double operationProgress,
  122. const Parameters& params)
  123. {
  124. m_stageProgress = stageProgress;
  125. m_operationProgress = operationProgress;
  126. if (params.m_callback) {
  127. params.m_callback->Update(m_overallProgress,
  128. m_stageProgress,
  129. m_operationProgress,
  130. m_stage.c_str(),
  131. m_operation.c_str());
  132. }
  133. }
  134. void Init()
  135. {
  136. if (mRaycastMesh)
  137. {
  138. mRaycastMesh->release();
  139. mRaycastMesh = nullptr;
  140. }
  141. memset(m_rot, 0, sizeof(double) * 9);
  142. m_dim = 64;
  143. m_volume = 0;
  144. m_volumeCH0 = 0.0;
  145. m_pset = 0;
  146. m_overallProgress = 0.0;
  147. m_stageProgress = 0.0;
  148. m_operationProgress = 0.0;
  149. m_stage = "";
  150. m_operation = "";
  151. m_barycenter[0] = m_barycenter[1] = m_barycenter[2] = 0.0;
  152. m_rot[0][0] = m_rot[1][1] = m_rot[2][2] = 1.0;
  153. SetCancel(false);
  154. }
  155. void ComputePrimitiveSet(const Parameters& params);
  156. void ComputeACD(const Parameters& params);
  157. void MergeConvexHulls(const Parameters& params);
  158. void SimplifyConvexHull(Mesh* const ch, const size_t nvertices, const double minVolume);
  159. void SimplifyConvexHulls(const Parameters& params);
  160. void ComputeBestClippingPlane(const PrimitiveSet* inputPSet,
  161. const double volume,
  162. const SArray<Plane>& planes,
  163. const Vec3<double>& preferredCuttingDirection,
  164. const double w,
  165. const double alpha,
  166. const double beta,
  167. const int32_t convexhullDownsampling,
  168. const double progress0,
  169. const double progress1,
  170. Plane& bestPlane,
  171. double& minConcavity,
  172. const Parameters& params);
  173. template <class T>
  174. void AlignMesh(const T* const points,
  175. const uint32_t stridePoints,
  176. const uint32_t nPoints,
  177. const int32_t* const triangles,
  178. const uint32_t strideTriangles,
  179. const uint32_t nTriangles,
  180. const Parameters& params)
  181. {
  182. if (GetCancel() || !params.m_pca) {
  183. return;
  184. }
  185. m_timer.Tic();
  186. m_stage = "Align mesh";
  187. m_operation = "Voxelization";
  188. std::ostringstream msg;
  189. if (params.m_logger) {
  190. msg << "+ " << m_stage << std::endl;
  191. params.m_logger->Log(msg.str().c_str());
  192. }
  193. Update(0.0, 0.0, params);
  194. if (GetCancel()) {
  195. return;
  196. }
  197. m_dim = (size_t)(pow((double)params.m_resolution, 1.0 / 3.0) + 0.5);
  198. Volume volume;
  199. volume.Voxelize(points, stridePoints, nPoints,
  200. triangles, strideTriangles, nTriangles,
  201. m_dim, m_barycenter, m_rot);
  202. size_t n = volume.GetNPrimitivesOnSurf() + volume.GetNPrimitivesInsideSurf();
  203. Update(50.0, 100.0, params);
  204. if (params.m_logger) {
  205. msg.str("");
  206. msg << "\t dim = " << m_dim << "\t-> " << n << " voxels" << std::endl;
  207. params.m_logger->Log(msg.str().c_str());
  208. }
  209. if (GetCancel()) {
  210. return;
  211. }
  212. m_operation = "PCA";
  213. Update(50.0, 0.0, params);
  214. volume.AlignToPrincipalAxes(m_rot);
  215. m_overallProgress = 1.0;
  216. Update(100.0, 100.0, params);
  217. m_timer.Toc();
  218. if (params.m_logger) {
  219. msg.str("");
  220. msg << "\t time " << m_timer.GetElapsedTime() / 1000.0 << "s" << std::endl;
  221. params.m_logger->Log(msg.str().c_str());
  222. }
  223. }
  224. template <class T>
  225. void VoxelizeMesh(const T* const points,
  226. const uint32_t stridePoints,
  227. const uint32_t nPoints,
  228. const int32_t* const triangles,
  229. const uint32_t strideTriangles,
  230. const uint32_t nTriangles,
  231. const Parameters& params)
  232. {
  233. if (GetCancel()) {
  234. return;
  235. }
  236. m_timer.Tic();
  237. m_stage = "Voxelization";
  238. std::ostringstream msg;
  239. if (params.m_logger) {
  240. msg << "+ " << m_stage << std::endl;
  241. params.m_logger->Log(msg.str().c_str());
  242. }
  243. delete m_volume;
  244. m_volume = 0;
  245. int32_t iteration = 0;
  246. const int32_t maxIteration = 5;
  247. double progress = 0.0;
  248. while (iteration++ < maxIteration && !m_cancel) {
  249. msg.str("");
  250. msg << "Iteration " << iteration;
  251. m_operation = msg.str();
  252. progress = iteration * 100.0 / maxIteration;
  253. Update(progress, 0.0, params);
  254. m_volume = new Volume;
  255. m_volume->Voxelize(points, stridePoints, nPoints,
  256. triangles, strideTriangles, nTriangles,
  257. m_dim, m_barycenter, m_rot);
  258. Update(progress, 100.0, params);
  259. size_t n = m_volume->GetNPrimitivesOnSurf() + m_volume->GetNPrimitivesInsideSurf();
  260. if (params.m_logger) {
  261. msg.str("");
  262. msg << "\t dim = " << m_dim << "\t-> " << n << " voxels" << std::endl;
  263. params.m_logger->Log(msg.str().c_str());
  264. }
  265. double a = pow((double)(params.m_resolution) / n, 0.33);
  266. size_t dim_next = (size_t)(m_dim * a + 0.5);
  267. if (n < params.m_resolution && iteration < maxIteration && m_volume->GetNPrimitivesOnSurf() < params.m_resolution / 8 && m_dim != dim_next) {
  268. delete m_volume;
  269. m_volume = 0;
  270. m_dim = dim_next;
  271. }
  272. else {
  273. break;
  274. }
  275. }
  276. m_overallProgress = 10.0;
  277. Update(100.0, 100.0, params);
  278. m_timer.Toc();
  279. if (params.m_logger) {
  280. msg.str("");
  281. msg << "\t time " << m_timer.GetElapsedTime() / 1000.0 << "s" << std::endl;
  282. params.m_logger->Log(msg.str().c_str());
  283. }
  284. }
  285. template <class T>
  286. bool ComputeACD(const T* const points,
  287. const uint32_t nPoints,
  288. const uint32_t* const triangles,
  289. const uint32_t nTriangles,
  290. const Parameters& params)
  291. {
  292. Init();
  293. if (params.m_projectHullVertices)
  294. {
  295. mRaycastMesh = RaycastMesh::createRaycastMesh(nPoints, points, nTriangles, (const uint32_t *)triangles);
  296. }
  297. if (params.m_oclAcceleration) {
  298. // build kernels
  299. }
  300. AlignMesh(points, 3, nPoints, (int32_t *)triangles, 3, nTriangles, params);
  301. VoxelizeMesh(points, 3, nPoints, (int32_t *)triangles, 3, nTriangles, params);
  302. ComputePrimitiveSet(params);
  303. ComputeACD(params);
  304. MergeConvexHulls(params);
  305. SimplifyConvexHulls(params);
  306. if (params.m_oclAcceleration) {
  307. // Release kernels
  308. }
  309. if (GetCancel()) {
  310. Clean();
  311. return false;
  312. }
  313. return true;
  314. }
  315. private:
  316. RaycastMesh *mRaycastMesh{ nullptr };
  317. SArray<Mesh*> m_convexHulls;
  318. std::string m_stage;
  319. std::string m_operation;
  320. double m_overallProgress;
  321. double m_stageProgress;
  322. double m_operationProgress;
  323. double m_rot[3][3];
  324. double m_volumeCH0;
  325. Vec3<double> m_barycenter;
  326. Timer m_timer;
  327. size_t m_dim;
  328. Volume* m_volume;
  329. PrimitiveSet* m_pset;
  330. Mutex m_cancelMutex;
  331. bool m_cancel;
  332. int32_t m_ompNumProcessors;
  333. #ifdef CL_VERSION_1_1
  334. cl_device_id* m_oclDevice;
  335. cl_context m_oclContext;
  336. cl_program m_oclProgram;
  337. cl_command_queue* m_oclQueue;
  338. cl_kernel* m_oclKernelComputePartialVolumes;
  339. cl_kernel* m_oclKernelComputeSum;
  340. size_t m_oclWorkGroupSize;
  341. #endif //CL_VERSION_1_1
  342. };
  343. }
  344. #endif // VHACD_VHACD_H