#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <limits>
#include <sstream>
#if _OPENMP
#include <omp.h>
#endif
#include "../public/VHACD.h"
#include "btConvexHullComputer.h"
#include "vhacdICHull.h"
#include "vhacdMesh.h"
#include "vhacdSArray.h"
#include "vhacdTimer.h"
#include "vhacdVHACD.h"
#include "vhacdVector.h"
#include "vhacdVolume.h"
#include "FloatMath.h"
#define MAX(a, b) …
#define MIN(a, b) …
#define ABS(a) …
#define ZSGN(a) …
#define MAX_DOUBLE …
#ifdef _MSC_VER
#pragma warning(disable:4267 4100 4244 4456)
#endif
#ifdef USE_SSE
#include <immintrin.h>
const int32_t SIMD_WIDTH = 4;
inline int32_t FindMinimumElement(const float* const d, float* const _, const int32_t n)
{
__m128 min_i = _mm_set1_ps(-1.0f);
__m128 min_v = _mm_set1_ps(std::numeric_limits<float>::max());
for (int32_t i = 0; i <= n - SIMD_WIDTH; i += SIMD_WIDTH) {
const __m128 data = _mm_load_ps(&d[i]);
const __m128 pred = _mm_cmplt_ps(data, min_v);
min_i = _mm_blendv_ps(min_i, _mm_set1_ps(i), pred);
min_v = _mm_min_ps(data, min_v);
}
const __m128 min1 = _mm_shuffle_ps(min_v, min_v, _MM_SHUFFLE(1, 0, 3, 2));
const __m128 min2 = _mm_min_ps(min_v, min1);
const __m128 min3 = _mm_shuffle_ps(min2, min2, _MM_SHUFFLE(0, 1, 0, 1));
const __m128 min4 = _mm_min_ps(min2, min3);
float min_d = _mm_cvtss_f32(min4);
const int32_t min_idx = __builtin_ctz(_mm_movemask_ps(_mm_cmpeq_ps(min_v, min4)));
int32_t ret = min_i[min_idx] + min_idx;
for (int32_t i = (n & ~(SIMD_WIDTH - 1)); i < n; ++i) {
if (d[i] < min_d) {
min_d = d[i];
ret = i;
}
}
*m = min_d;
return ret;
}
inline int32_t FindMinimumElement(const float* const d, float* const m, const int32_t begin, const int32_t end)
{
int32_t min_i = -1;
float min_d = std::numeric_limits<float>::max();
const int32_t aligned = (begin & ~(SIMD_WIDTH - 1)) + ((begin & (SIMD_WIDTH - 1)) ? SIMD_WIDTH : 0);
for (int32_t i = begin; i < std::min(end, aligned); ++i) {
if (d[i] < min_d) {
min_d = d[i];
min_i = i;
}
}
float r_m = std::numeric_limits<float>::max();
const int32_t n = end - aligned;
const int32_t r_i = (n > 0) ? FindMinimumElement(&d[aligned], &r_m, n) : 0;
if (r_m < min_d) {
*m = r_m;
return r_i + aligned;
}
else {
*m = min_d;
return min_i;
}
}
#else
inline int32_t FindMinimumElement(const float* const d, float* const m, const int32_t begin, const int32_t end)
{ … }
#endif
#ifndef OCL_SOURCE_FROM_FILE
const char* oclProgramSource = …;
#endif
namespace VHACD {
IVHACD* CreateVHACD(void)
{ … }
bool VHACD::OCLInit(void* const oclDevice, IUserLogger* const logger)
{ … }
bool VHACD::OCLRelease(IUserLogger* const logger)
{ … }
void VHACD::ComputePrimitiveSet(const Parameters& params)
{ … }
bool VHACD::Compute(const double* const points, const uint32_t nPoints,
const uint32_t* const triangles,const uint32_t nTriangles, const Parameters& params)
{ … }
bool VHACD::Compute(const float* const points,const uint32_t nPoints,
const uint32_t* const triangles,const uint32_t nTriangles, const Parameters& params)
{ … }
double ComputePreferredCuttingDirection(const PrimitiveSet* const tset, Vec3<double>& dir)
{ … }
void ComputeAxesAlignedClippingPlanes(const VoxelSet& vset, const short downsampling, SArray<Plane>& planes)
{ … }
void ComputeAxesAlignedClippingPlanes(const TetrahedronSet& tset, const short downsampling, SArray<Plane>& planes)
{ … }
void RefineAxesAlignedClippingPlanes(const VoxelSet& vset, const Plane& bestPlane, const short downsampling,
SArray<Plane>& planes)
{ … }
void RefineAxesAlignedClippingPlanes(const TetrahedronSet& tset, const Plane& bestPlane, const short downsampling,
SArray<Plane>& planes)
{ … }
inline double ComputeLocalConcavity(const double volume, const double volumeCH)
{ … }
inline double ComputeConcavity(const double volume, const double volumeCH, const double volume0)
{ … }
void VHACD::ComputeBestClippingPlane(const PrimitiveSet* inputPSet, const double volume, const SArray<Plane>& planes,
const Vec3<double>& preferredCuttingDirection, const double w, const double alpha, const double beta,
const int32_t convexhullDownsampling, const double progress0, const double progress1, Plane& bestPlane,
double& minConcavity, const Parameters& params)
{ … }
void VHACD::ComputeACD(const Parameters& params)
{ … }
void AddPoints(const Mesh* const mesh, SArray<Vec3<double> >& pts)
{ … }
void ComputeConvexHull(const Mesh* const ch1, const Mesh* const ch2, SArray<Vec3<double> >& pts, Mesh* const combinedCH)
{ … }
void VHACD::MergeConvexHulls(const Parameters& params)
{ … }
void VHACD::SimplifyConvexHull(Mesh* const ch, const size_t nvertices, const double minVolume)
{ … }
void VHACD::SimplifyConvexHulls(const Parameters& params)
{ … }
bool VHACD::ComputeCenterOfMass(double centerOfMass[3]) const
{ … }
}