1/* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
2 All rights reserved.
3
4
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
9 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.
10
11 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12
13 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.
14 */
15#pragma once
16#ifndef VHACD_H
17#define VHACD_H
18
19#define VHACD_VERSION_MAJOR 2
20#define VHACD_VERSION_MINOR 3
21
22// Changes for version 2.3
23//
24// m_gamma : Has been removed. This used to control the error metric to merge convex hulls. Now it uses the 'm_maxConvexHulls' value instead.
25// m_maxConvexHulls : This is the maximum number of convex hulls to produce from the merge operation; replaces 'm_gamma'.
26//
27// Note that decomposition depth is no longer a user provided value. It is now derived from the
28// maximum number of hulls requested.
29//
30// As a convenience to the user, each convex hull produced now includes the volume of the hull as well as it's center.
31//
32// This version supports a convenience method to automatically make V-HACD run asynchronously in a background thread.
33// To get a fully asynchronous version, call 'CreateVHACD_ASYNC' instead of 'CreateVHACD'. You get the same interface however,
34// now when computing convex hulls, it is no longer a blocking operation. All callback messages are still returned
35// in the application's thread so you don't need to worry about mutex locks or anything in that case.
36// To tell if the operation is complete, the application should call 'IsReady'. This will return true if
37// the last approximation operation is complete and will dispatch any pending messages.
38// If you call 'Compute' while a previous operation was still running, it will automatically cancel the last request
39// and begin a new one. To cancel a currently running approximation just call 'Cancel'.
40#include <stdint.h>
41
42namespace VHACD {
43class IVHACD {
44public:
45 class IUserCallback {
46 public:
47 virtual ~IUserCallback(){};
48 virtual void Update(const double overallProgress,
49 const double stageProgress,
50 const double operationProgress,
51 const char* const stage,
52 const char* const operation)
53 = 0;
54 };
55
56 class IUserLogger {
57 public:
58 virtual ~IUserLogger(){};
59 virtual void Log(const char* const msg) = 0;
60 };
61
62 class ConvexHull {
63 public:
64 double* m_points;
65 uint32_t* m_triangles;
66 uint32_t m_nPoints;
67 uint32_t m_nTriangles;
68 double m_volume;
69 double m_center[3];
70 };
71
72 class Parameters {
73 public:
74 Parameters(void) { Init(); }
75 void Init(void)
76 {
77 m_resolution = 100000;
78 m_concavity = 0.001;
79 m_planeDownsampling = 4;
80 m_convexhullDownsampling = 4;
81 m_alpha = 0.05;
82 m_beta = 0.05;
83 m_pca = 0;
84 m_mode = 0; // 0: voxel-based (recommended), 1: tetrahedron-based
85 m_maxNumVerticesPerCH = 64;
86 m_minVolumePerCH = 0.0001;
87 m_callback = 0;
88 m_logger = 0;
89 m_convexhullApproximation = true;
90 m_oclAcceleration = true;
91 m_maxConvexHulls = 1024;
92 m_projectHullVertices = true; // This will project the output convex hull vertices onto the original source mesh to increase the floating point accuracy of the results
93 }
94 double m_concavity;
95 double m_alpha;
96 double m_beta;
97 double m_minVolumePerCH;
98 IUserCallback* m_callback;
99 IUserLogger* m_logger;
100 uint32_t m_resolution;
101 uint32_t m_maxNumVerticesPerCH;
102 uint32_t m_planeDownsampling;
103 uint32_t m_convexhullDownsampling;
104 uint32_t m_pca;
105 uint32_t m_mode;
106 uint32_t m_convexhullApproximation;
107 uint32_t m_oclAcceleration;
108 uint32_t m_maxConvexHulls;
109 bool m_projectHullVertices;
110 };
111
112 virtual void Cancel() = 0;
113 virtual bool Compute(const float* const points,
114 const uint32_t countPoints,
115 const uint32_t* const triangles,
116 const uint32_t countTriangles,
117 const Parameters& params)
118 = 0;
119 virtual bool Compute(const double* const points,
120 const uint32_t countPoints,
121 const uint32_t* const triangles,
122 const uint32_t countTriangles,
123 const Parameters& params)
124 = 0;
125 virtual uint32_t GetNConvexHulls() const = 0;
126 virtual void GetConvexHull(const uint32_t index, ConvexHull& ch) const = 0;
127 virtual void Clean(void) = 0; // release internally allocated memory
128 virtual void Release(void) = 0; // release IVHACD
129 virtual bool OCLInit(void* const oclDevice,
130 IUserLogger* const logger = 0)
131 = 0;
132 virtual bool OCLRelease(IUserLogger* const logger = 0) = 0;
133
134 // Will compute the center of mass of the convex hull decomposition results and return it
135 // in 'centerOfMass'. Returns false if the center of mass could not be computed.
136 virtual bool ComputeCenterOfMass(double centerOfMass[3]) const = 0;
137
138 // In synchronous mode (non-multi-threaded) the state is always 'ready'
139 // In asynchronous mode, this returns true if the background thread is not still actively computing
140 // a new solution. In an asynchronous config the 'IsReady' call will report any update or log
141 // messages in the caller's current thread.
142 virtual bool IsReady(void) const
143 {
144 return true;
145 }
146
147protected:
148 virtual ~IVHACD(void) {}
149};
150IVHACD* CreateVHACD(void);
151IVHACD* CreateVHACD_ASYNC(void);
152}
153#endif // VHACD_H
154