add VHACD, with the addition of Wavefront .obj export
This commit is contained in:
1433
Extras/VHACD/src/VHACD.cpp
Normal file
1433
Extras/VHACD/src/VHACD.cpp
Normal file
File diff suppressed because it is too large
Load Diff
176
Extras/VHACD/src/btAlignedAllocator.cpp
Normal file
176
Extras/VHACD/src/btAlignedAllocator.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include "btAlignedAllocator.h"
|
||||
|
||||
int gNumAlignedAllocs = 0;
|
||||
int gNumAlignedFree = 0;
|
||||
int gTotalBytesAlignedAllocs = 0; //detect memory leaks
|
||||
|
||||
static void* btAllocDefault(size_t size)
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
static void btFreeDefault(void* ptr)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static btAllocFunc* sAllocFunc = btAllocDefault;
|
||||
static btFreeFunc* sFreeFunc = btFreeDefault;
|
||||
|
||||
#if defined(BT_HAS_ALIGNED_ALLOCATOR)
|
||||
#include <malloc.h>
|
||||
static void* btAlignedAllocDefault(size_t size, int alignment)
|
||||
{
|
||||
return _aligned_malloc(size, (size_t)alignment);
|
||||
}
|
||||
|
||||
static void btAlignedFreeDefault(void* ptr)
|
||||
{
|
||||
_aligned_free(ptr);
|
||||
}
|
||||
#elif defined(__CELLOS_LV2__)
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void* btAlignedAllocDefault(size_t size, int alignment)
|
||||
{
|
||||
return memalign(alignment, size);
|
||||
}
|
||||
|
||||
static inline void btAlignedFreeDefault(void* ptr)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
#else
|
||||
static inline void* btAlignedAllocDefault(size_t size, int alignment)
|
||||
{
|
||||
void* ret;
|
||||
char* real;
|
||||
unsigned long offset;
|
||||
|
||||
real = (char*)sAllocFunc(size + sizeof(void*) + (alignment - 1));
|
||||
if (real) {
|
||||
offset = (alignment - (unsigned long)(real + sizeof(void*))) & (alignment - 1);
|
||||
ret = (void*)((real + sizeof(void*)) + offset);
|
||||
*((void**)(ret)-1) = (void*)(real);
|
||||
}
|
||||
else {
|
||||
ret = (void*)(real);
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
static inline void btAlignedFreeDefault(void* ptr)
|
||||
{
|
||||
void* real;
|
||||
|
||||
if (ptr) {
|
||||
real = *((void**)(ptr)-1);
|
||||
sFreeFunc(real);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static btAlignedAllocFunc* sAlignedAllocFunc = btAlignedAllocDefault;
|
||||
static btAlignedFreeFunc* sAlignedFreeFunc = btAlignedFreeDefault;
|
||||
|
||||
void btAlignedAllocSetCustomAligned(btAlignedAllocFunc* allocFunc, btAlignedFreeFunc* freeFunc)
|
||||
{
|
||||
sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
|
||||
sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
|
||||
}
|
||||
|
||||
void btAlignedAllocSetCustom(btAllocFunc* allocFunc, btFreeFunc* freeFunc)
|
||||
{
|
||||
sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
|
||||
sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
|
||||
}
|
||||
|
||||
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
|
||||
//this generic allocator provides the total allocated number of bytes
|
||||
#include <stdio.h>
|
||||
|
||||
void* btAlignedAllocInternal(size_t size, int alignment, int line, char* filename)
|
||||
{
|
||||
void* ret;
|
||||
char* real;
|
||||
unsigned long offset;
|
||||
|
||||
gTotalBytesAlignedAllocs += size;
|
||||
gNumAlignedAllocs++;
|
||||
|
||||
real = (char*)sAllocFunc(size + 2 * sizeof(void*) + (alignment - 1));
|
||||
if (real) {
|
||||
offset = (alignment - (unsigned long)(real + 2 * sizeof(void*))) & (alignment - 1);
|
||||
ret = (void*)((real + 2 * sizeof(void*)) + offset);
|
||||
*((void**)(ret)-1) = (void*)(real);
|
||||
*((int*)(ret)-2) = size;
|
||||
}
|
||||
else {
|
||||
ret = (void*)(real); //??
|
||||
}
|
||||
|
||||
printf("allocation#%d at address %x, from %s,line %d, size %d\n", gNumAlignedAllocs, real, filename, line, size);
|
||||
|
||||
int* ptr = (int*)ret;
|
||||
*ptr = 12;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void btAlignedFreeInternal(void* ptr, int line, char* filename)
|
||||
{
|
||||
|
||||
void* real;
|
||||
gNumAlignedFree++;
|
||||
|
||||
if (ptr) {
|
||||
real = *((void**)(ptr)-1);
|
||||
int size = *((int*)(ptr)-2);
|
||||
gTotalBytesAlignedAllocs -= size;
|
||||
|
||||
printf("free #%d at address %x, from %s,line %d, size %d\n", gNumAlignedFree, real, filename, line, size);
|
||||
|
||||
sFreeFunc(real);
|
||||
}
|
||||
else {
|
||||
printf("NULL ptr\n");
|
||||
}
|
||||
}
|
||||
|
||||
#else //BT_DEBUG_MEMORY_ALLOCATIONS
|
||||
|
||||
void* btAlignedAllocInternal(size_t size, int alignment)
|
||||
{
|
||||
gNumAlignedAllocs++;
|
||||
void* ptr;
|
||||
ptr = sAlignedAllocFunc(size, alignment);
|
||||
// printf("btAlignedAllocInternal %d, %x\n",size,ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void btAlignedFreeInternal(void* ptr)
|
||||
{
|
||||
if (!ptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
gNumAlignedFree++;
|
||||
// printf("btAlignedFreeInternal %x\n",ptr);
|
||||
sAlignedFreeFunc(ptr);
|
||||
}
|
||||
|
||||
#endif //BT_DEBUG_MEMORY_ALLOCATIONS
|
||||
2475
Extras/VHACD/src/btConvexHullComputer.cpp
Normal file
2475
Extras/VHACD/src/btConvexHullComputer.cpp
Normal file
File diff suppressed because it is too large
Load Diff
10
Extras/VHACD/src/premake4.lua
Normal file
10
Extras/VHACD/src/premake4.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
project "vhacd"
|
||||
|
||||
kind "StaticLib"
|
||||
includedirs {
|
||||
"../inc","../public",
|
||||
}
|
||||
files {
|
||||
"*.cpp",
|
||||
"*.h"
|
||||
}
|
||||
725
Extras/VHACD/src/vhacdICHull.cpp
Normal file
725
Extras/VHACD/src/vhacdICHull.cpp
Normal file
@@ -0,0 +1,725 @@
|
||||
/* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
|
||||
All rights reserved.
|
||||
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
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.
|
||||
|
||||
3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
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.
|
||||
*/
|
||||
#include "vhacdICHull.h"
|
||||
#include <limits>
|
||||
namespace VHACD {
|
||||
const double ICHull::sc_eps = 1.0e-15;
|
||||
const int ICHull::sc_dummyIndex = std::numeric_limits<int>::max();
|
||||
ICHull::ICHull()
|
||||
{
|
||||
m_isFlat = false;
|
||||
}
|
||||
bool ICHull::AddPoints(const Vec3<double>* points, size_t nPoints)
|
||||
{
|
||||
if (!points) {
|
||||
return false;
|
||||
}
|
||||
CircularListElement<TMMVertex>* vertex = NULL;
|
||||
for (size_t i = 0; i < nPoints; i++) {
|
||||
vertex = m_mesh.AddVertex();
|
||||
vertex->GetData().m_pos.X() = points[i].X();
|
||||
vertex->GetData().m_pos.Y() = points[i].Y();
|
||||
vertex->GetData().m_pos.Z() = points[i].Z();
|
||||
vertex->GetData().m_name = static_cast<int>(i);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool ICHull::AddPoint(const Vec3<double>& point, int id)
|
||||
{
|
||||
if (AddPoints(&point, 1)) {
|
||||
m_mesh.m_vertices.GetData().m_name = id;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ICHullError ICHull::Process()
|
||||
{
|
||||
unsigned int addedPoints = 0;
|
||||
if (m_mesh.GetNVertices() < 3) {
|
||||
return ICHullErrorNotEnoughPoints;
|
||||
}
|
||||
if (m_mesh.GetNVertices() == 3) {
|
||||
m_isFlat = true;
|
||||
CircularListElement<TMMTriangle>* t1 = m_mesh.AddTriangle();
|
||||
CircularListElement<TMMTriangle>* t2 = m_mesh.AddTriangle();
|
||||
CircularListElement<TMMVertex>* v0 = m_mesh.m_vertices.GetHead();
|
||||
CircularListElement<TMMVertex>* v1 = v0->GetNext();
|
||||
CircularListElement<TMMVertex>* v2 = v1->GetNext();
|
||||
// Compute the normal to the plane
|
||||
Vec3<double> p0 = v0->GetData().m_pos;
|
||||
Vec3<double> p1 = v1->GetData().m_pos;
|
||||
Vec3<double> p2 = v2->GetData().m_pos;
|
||||
m_normal = (p1 - p0) ^ (p2 - p0);
|
||||
m_normal.Normalize();
|
||||
t1->GetData().m_vertices[0] = v0;
|
||||
t1->GetData().m_vertices[1] = v1;
|
||||
t1->GetData().m_vertices[2] = v2;
|
||||
t2->GetData().m_vertices[0] = v1;
|
||||
t2->GetData().m_vertices[1] = v2;
|
||||
t2->GetData().m_vertices[2] = v2;
|
||||
return ICHullErrorOK;
|
||||
}
|
||||
if (m_isFlat) {
|
||||
m_mesh.m_edges.Clear();
|
||||
m_mesh.m_triangles.Clear();
|
||||
m_isFlat = false;
|
||||
}
|
||||
if (m_mesh.GetNTriangles() == 0) // we have to create the first polyhedron
|
||||
{
|
||||
ICHullError res = DoubleTriangle();
|
||||
if (res != ICHullErrorOK) {
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
addedPoints += 3;
|
||||
}
|
||||
}
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
// go to the first added and not processed vertex
|
||||
while (!(vertices.GetHead()->GetPrev()->GetData().m_tag)) {
|
||||
vertices.Prev();
|
||||
}
|
||||
while (!vertices.GetData().m_tag) // not processed
|
||||
{
|
||||
vertices.GetData().m_tag = true;
|
||||
if (ProcessPoint()) {
|
||||
addedPoints++;
|
||||
CleanUp(addedPoints);
|
||||
vertices.Next();
|
||||
if (!GetMesh().CheckConsistancy()) {
|
||||
size_t nV = m_mesh.GetNVertices();
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
for (size_t v = 0; v < nV; ++v) {
|
||||
if (vertices.GetData().m_name == sc_dummyIndex) {
|
||||
vertices.Delete();
|
||||
break;
|
||||
}
|
||||
vertices.Next();
|
||||
}
|
||||
return ICHullErrorInconsistent;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_isFlat) {
|
||||
SArray<CircularListElement<TMMTriangle>*> trianglesToDuplicate;
|
||||
size_t nT = m_mesh.GetNTriangles();
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
TMMTriangle& currentTriangle = m_mesh.m_triangles.GetHead()->GetData();
|
||||
if (currentTriangle.m_vertices[0]->GetData().m_name == sc_dummyIndex || currentTriangle.m_vertices[1]->GetData().m_name == sc_dummyIndex || currentTriangle.m_vertices[2]->GetData().m_name == sc_dummyIndex) {
|
||||
m_trianglesToDelete.PushBack(m_mesh.m_triangles.GetHead());
|
||||
for (int k = 0; k < 3; k++) {
|
||||
for (int h = 0; h < 2; h++) {
|
||||
if (currentTriangle.m_edges[k]->GetData().m_triangles[h] == m_mesh.m_triangles.GetHead()) {
|
||||
currentTriangle.m_edges[k]->GetData().m_triangles[h] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
trianglesToDuplicate.PushBack(m_mesh.m_triangles.GetHead());
|
||||
}
|
||||
m_mesh.m_triangles.Next();
|
||||
}
|
||||
size_t nE = m_mesh.GetNEdges();
|
||||
for (size_t e = 0; e < nE; e++) {
|
||||
TMMEdge& currentEdge = m_mesh.m_edges.GetHead()->GetData();
|
||||
if (currentEdge.m_triangles[0] == 0 && currentEdge.m_triangles[1] == 0) {
|
||||
m_edgesToDelete.PushBack(m_mesh.m_edges.GetHead());
|
||||
}
|
||||
m_mesh.m_edges.Next();
|
||||
}
|
||||
size_t nV = m_mesh.GetNVertices();
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
for (size_t v = 0; v < nV; ++v) {
|
||||
if (vertices.GetData().m_name == sc_dummyIndex) {
|
||||
vertices.Delete();
|
||||
}
|
||||
else {
|
||||
vertices.GetData().m_tag = false;
|
||||
vertices.Next();
|
||||
}
|
||||
}
|
||||
CleanEdges();
|
||||
CleanTriangles();
|
||||
CircularListElement<TMMTriangle>* newTriangle;
|
||||
for (size_t t = 0; t < trianglesToDuplicate.Size(); t++) {
|
||||
newTriangle = m_mesh.AddTriangle();
|
||||
newTriangle->GetData().m_vertices[0] = trianglesToDuplicate[t]->GetData().m_vertices[1];
|
||||
newTriangle->GetData().m_vertices[1] = trianglesToDuplicate[t]->GetData().m_vertices[0];
|
||||
newTriangle->GetData().m_vertices[2] = trianglesToDuplicate[t]->GetData().m_vertices[2];
|
||||
}
|
||||
}
|
||||
return ICHullErrorOK;
|
||||
}
|
||||
ICHullError ICHull::Process(const unsigned int nPointsCH,
|
||||
const double minVolume)
|
||||
{
|
||||
unsigned int addedPoints = 0;
|
||||
if (nPointsCH < 3 || m_mesh.GetNVertices() < 3) {
|
||||
return ICHullErrorNotEnoughPoints;
|
||||
}
|
||||
if (m_mesh.GetNVertices() == 3) {
|
||||
m_isFlat = true;
|
||||
CircularListElement<TMMTriangle>* t1 = m_mesh.AddTriangle();
|
||||
CircularListElement<TMMTriangle>* t2 = m_mesh.AddTriangle();
|
||||
CircularListElement<TMMVertex>* v0 = m_mesh.m_vertices.GetHead();
|
||||
CircularListElement<TMMVertex>* v1 = v0->GetNext();
|
||||
CircularListElement<TMMVertex>* v2 = v1->GetNext();
|
||||
// Compute the normal to the plane
|
||||
Vec3<double> p0 = v0->GetData().m_pos;
|
||||
Vec3<double> p1 = v1->GetData().m_pos;
|
||||
Vec3<double> p2 = v2->GetData().m_pos;
|
||||
m_normal = (p1 - p0) ^ (p2 - p0);
|
||||
m_normal.Normalize();
|
||||
t1->GetData().m_vertices[0] = v0;
|
||||
t1->GetData().m_vertices[1] = v1;
|
||||
t1->GetData().m_vertices[2] = v2;
|
||||
t2->GetData().m_vertices[0] = v1;
|
||||
t2->GetData().m_vertices[1] = v0;
|
||||
t2->GetData().m_vertices[2] = v2;
|
||||
return ICHullErrorOK;
|
||||
}
|
||||
|
||||
if (m_isFlat) {
|
||||
m_mesh.m_triangles.Clear();
|
||||
m_mesh.m_edges.Clear();
|
||||
m_isFlat = false;
|
||||
}
|
||||
|
||||
if (m_mesh.GetNTriangles() == 0) // we have to create the first polyhedron
|
||||
{
|
||||
ICHullError res = DoubleTriangle();
|
||||
if (res != ICHullErrorOK) {
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
addedPoints += 3;
|
||||
}
|
||||
}
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
while (!vertices.GetData().m_tag && addedPoints < nPointsCH) // not processed
|
||||
{
|
||||
if (!FindMaxVolumePoint((addedPoints > 4) ? minVolume : 0.0)) {
|
||||
break;
|
||||
}
|
||||
vertices.GetData().m_tag = true;
|
||||
if (ProcessPoint()) {
|
||||
addedPoints++;
|
||||
CleanUp(addedPoints);
|
||||
if (!GetMesh().CheckConsistancy()) {
|
||||
size_t nV = m_mesh.GetNVertices();
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
for (size_t v = 0; v < nV; ++v) {
|
||||
if (vertices.GetData().m_name == sc_dummyIndex) {
|
||||
vertices.Delete();
|
||||
break;
|
||||
}
|
||||
vertices.Next();
|
||||
}
|
||||
return ICHullErrorInconsistent;
|
||||
}
|
||||
vertices.Next();
|
||||
}
|
||||
}
|
||||
// delete remaining points
|
||||
while (!vertices.GetData().m_tag) {
|
||||
vertices.Delete();
|
||||
}
|
||||
if (m_isFlat) {
|
||||
SArray<CircularListElement<TMMTriangle>*> trianglesToDuplicate;
|
||||
size_t nT = m_mesh.GetNTriangles();
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
TMMTriangle& currentTriangle = m_mesh.m_triangles.GetHead()->GetData();
|
||||
if (currentTriangle.m_vertices[0]->GetData().m_name == sc_dummyIndex || currentTriangle.m_vertices[1]->GetData().m_name == sc_dummyIndex || currentTriangle.m_vertices[2]->GetData().m_name == sc_dummyIndex) {
|
||||
m_trianglesToDelete.PushBack(m_mesh.m_triangles.GetHead());
|
||||
for (int k = 0; k < 3; k++) {
|
||||
for (int h = 0; h < 2; h++) {
|
||||
if (currentTriangle.m_edges[k]->GetData().m_triangles[h] == m_mesh.m_triangles.GetHead()) {
|
||||
currentTriangle.m_edges[k]->GetData().m_triangles[h] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
trianglesToDuplicate.PushBack(m_mesh.m_triangles.GetHead());
|
||||
}
|
||||
m_mesh.m_triangles.Next();
|
||||
}
|
||||
size_t nE = m_mesh.GetNEdges();
|
||||
for (size_t e = 0; e < nE; e++) {
|
||||
TMMEdge& currentEdge = m_mesh.m_edges.GetHead()->GetData();
|
||||
if (currentEdge.m_triangles[0] == 0 && currentEdge.m_triangles[1] == 0) {
|
||||
m_edgesToDelete.PushBack(m_mesh.m_edges.GetHead());
|
||||
}
|
||||
m_mesh.m_edges.Next();
|
||||
}
|
||||
size_t nV = m_mesh.GetNVertices();
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
for (size_t v = 0; v < nV; ++v) {
|
||||
if (vertices.GetData().m_name == sc_dummyIndex) {
|
||||
vertices.Delete();
|
||||
}
|
||||
else {
|
||||
vertices.GetData().m_tag = false;
|
||||
vertices.Next();
|
||||
}
|
||||
}
|
||||
CleanEdges();
|
||||
CleanTriangles();
|
||||
CircularListElement<TMMTriangle>* newTriangle;
|
||||
for (size_t t = 0; t < trianglesToDuplicate.Size(); t++) {
|
||||
newTriangle = m_mesh.AddTriangle();
|
||||
newTriangle->GetData().m_vertices[0] = trianglesToDuplicate[t]->GetData().m_vertices[1];
|
||||
newTriangle->GetData().m_vertices[1] = trianglesToDuplicate[t]->GetData().m_vertices[0];
|
||||
newTriangle->GetData().m_vertices[2] = trianglesToDuplicate[t]->GetData().m_vertices[2];
|
||||
}
|
||||
}
|
||||
return ICHullErrorOK;
|
||||
}
|
||||
bool ICHull::FindMaxVolumePoint(const double minVolume)
|
||||
{
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
CircularListElement<TMMVertex>* vMaxVolume = 0;
|
||||
CircularListElement<TMMVertex>* vHeadPrev = vertices.GetHead()->GetPrev();
|
||||
|
||||
double maxVolume = minVolume;
|
||||
double volume = 0.0;
|
||||
while (!vertices.GetData().m_tag) // not processed
|
||||
{
|
||||
if (ComputePointVolume(volume, false)) {
|
||||
if (maxVolume < volume) {
|
||||
maxVolume = volume;
|
||||
vMaxVolume = vertices.GetHead();
|
||||
}
|
||||
vertices.Next();
|
||||
}
|
||||
}
|
||||
CircularListElement<TMMVertex>* vHead = vHeadPrev->GetNext();
|
||||
vertices.GetHead() = vHead;
|
||||
if (!vMaxVolume) {
|
||||
return false;
|
||||
}
|
||||
if (vMaxVolume != vHead) {
|
||||
Vec3<double> pos = vHead->GetData().m_pos;
|
||||
int id = vHead->GetData().m_name;
|
||||
vHead->GetData().m_pos = vMaxVolume->GetData().m_pos;
|
||||
vHead->GetData().m_name = vMaxVolume->GetData().m_name;
|
||||
vMaxVolume->GetData().m_pos = pos;
|
||||
vHead->GetData().m_name = id;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
ICHullError ICHull::DoubleTriangle()
|
||||
{
|
||||
// find three non colinear points
|
||||
m_isFlat = false;
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
CircularListElement<TMMVertex>* v0 = vertices.GetHead();
|
||||
while (Colinear(v0->GetData().m_pos,
|
||||
v0->GetNext()->GetData().m_pos,
|
||||
v0->GetNext()->GetNext()->GetData().m_pos)) {
|
||||
if ((v0 = v0->GetNext()) == vertices.GetHead()) {
|
||||
return ICHullErrorCoplanarPoints;
|
||||
}
|
||||
}
|
||||
CircularListElement<TMMVertex>* v1 = v0->GetNext();
|
||||
CircularListElement<TMMVertex>* v2 = v1->GetNext();
|
||||
// mark points as processed
|
||||
v0->GetData().m_tag = v1->GetData().m_tag = v2->GetData().m_tag = true;
|
||||
|
||||
// create two triangles
|
||||
CircularListElement<TMMTriangle>* f0 = MakeFace(v0, v1, v2, 0);
|
||||
MakeFace(v2, v1, v0, f0);
|
||||
|
||||
// find a fourth non-coplanar point to form tetrahedron
|
||||
CircularListElement<TMMVertex>* v3 = v2->GetNext();
|
||||
vertices.GetHead() = v3;
|
||||
|
||||
double vol = ComputeVolume4(v0->GetData().m_pos, v1->GetData().m_pos, v2->GetData().m_pos, v3->GetData().m_pos);
|
||||
while (fabs(vol) < sc_eps && !v3->GetNext()->GetData().m_tag) {
|
||||
v3 = v3->GetNext();
|
||||
vol = ComputeVolume4(v0->GetData().m_pos, v1->GetData().m_pos, v2->GetData().m_pos, v3->GetData().m_pos);
|
||||
}
|
||||
if (fabs(vol) < sc_eps) {
|
||||
// compute the barycenter
|
||||
Vec3<double> bary(0.0, 0.0, 0.0);
|
||||
CircularListElement<TMMVertex>* vBary = v0;
|
||||
do {
|
||||
bary += vBary->GetData().m_pos;
|
||||
} while ((vBary = vBary->GetNext()) != v0);
|
||||
bary /= static_cast<double>(vertices.GetSize());
|
||||
|
||||
// Compute the normal to the plane
|
||||
Vec3<double> p0 = v0->GetData().m_pos;
|
||||
Vec3<double> p1 = v1->GetData().m_pos;
|
||||
Vec3<double> p2 = v2->GetData().m_pos;
|
||||
m_normal = (p1 - p0) ^ (p2 - p0);
|
||||
m_normal.Normalize();
|
||||
// add dummy vertex placed at (bary + normal)
|
||||
vertices.GetHead() = v2;
|
||||
Vec3<double> newPt = bary + m_normal;
|
||||
AddPoint(newPt, sc_dummyIndex);
|
||||
m_isFlat = true;
|
||||
return ICHullErrorOK;
|
||||
}
|
||||
else if (v3 != vertices.GetHead()) {
|
||||
TMMVertex temp;
|
||||
temp.m_name = v3->GetData().m_name;
|
||||
temp.m_pos = v3->GetData().m_pos;
|
||||
v3->GetData().m_name = vertices.GetHead()->GetData().m_name;
|
||||
v3->GetData().m_pos = vertices.GetHead()->GetData().m_pos;
|
||||
vertices.GetHead()->GetData().m_name = temp.m_name;
|
||||
vertices.GetHead()->GetData().m_pos = temp.m_pos;
|
||||
}
|
||||
return ICHullErrorOK;
|
||||
}
|
||||
CircularListElement<TMMTriangle>* ICHull::MakeFace(CircularListElement<TMMVertex>* v0,
|
||||
CircularListElement<TMMVertex>* v1,
|
||||
CircularListElement<TMMVertex>* v2,
|
||||
CircularListElement<TMMTriangle>* fold)
|
||||
{
|
||||
CircularListElement<TMMEdge>* e0;
|
||||
CircularListElement<TMMEdge>* e1;
|
||||
CircularListElement<TMMEdge>* e2;
|
||||
int index = 0;
|
||||
if (!fold) // if first face to be created
|
||||
{
|
||||
e0 = m_mesh.AddEdge(); // create the three edges
|
||||
e1 = m_mesh.AddEdge();
|
||||
e2 = m_mesh.AddEdge();
|
||||
}
|
||||
else // otherwise re-use existing edges (in reverse order)
|
||||
{
|
||||
e0 = fold->GetData().m_edges[2];
|
||||
e1 = fold->GetData().m_edges[1];
|
||||
e2 = fold->GetData().m_edges[0];
|
||||
index = 1;
|
||||
}
|
||||
e0->GetData().m_vertices[0] = v0;
|
||||
e0->GetData().m_vertices[1] = v1;
|
||||
e1->GetData().m_vertices[0] = v1;
|
||||
e1->GetData().m_vertices[1] = v2;
|
||||
e2->GetData().m_vertices[0] = v2;
|
||||
e2->GetData().m_vertices[1] = v0;
|
||||
// create the new face
|
||||
CircularListElement<TMMTriangle>* f = m_mesh.AddTriangle();
|
||||
f->GetData().m_edges[0] = e0;
|
||||
f->GetData().m_edges[1] = e1;
|
||||
f->GetData().m_edges[2] = e2;
|
||||
f->GetData().m_vertices[0] = v0;
|
||||
f->GetData().m_vertices[1] = v1;
|
||||
f->GetData().m_vertices[2] = v2;
|
||||
// link edges to face f
|
||||
e0->GetData().m_triangles[index] = e1->GetData().m_triangles[index] = e2->GetData().m_triangles[index] = f;
|
||||
return f;
|
||||
}
|
||||
CircularListElement<TMMTriangle>* ICHull::MakeConeFace(CircularListElement<TMMEdge>* e, CircularListElement<TMMVertex>* p)
|
||||
{
|
||||
// create two new edges if they don't already exist
|
||||
CircularListElement<TMMEdge>* newEdges[2];
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
if (!(newEdges[i] = e->GetData().m_vertices[i]->GetData().m_duplicate)) { // if the edge doesn't exits add it and mark the vertex as duplicated
|
||||
newEdges[i] = m_mesh.AddEdge();
|
||||
newEdges[i]->GetData().m_vertices[0] = e->GetData().m_vertices[i];
|
||||
newEdges[i]->GetData().m_vertices[1] = p;
|
||||
e->GetData().m_vertices[i]->GetData().m_duplicate = newEdges[i];
|
||||
}
|
||||
}
|
||||
// make the new face
|
||||
CircularListElement<TMMTriangle>* newFace = m_mesh.AddTriangle();
|
||||
newFace->GetData().m_edges[0] = e;
|
||||
newFace->GetData().m_edges[1] = newEdges[0];
|
||||
newFace->GetData().m_edges[2] = newEdges[1];
|
||||
MakeCCW(newFace, e, p);
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
if (!newEdges[i]->GetData().m_triangles[j]) {
|
||||
newEdges[i]->GetData().m_triangles[j] = newFace;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return newFace;
|
||||
}
|
||||
bool ICHull::ComputePointVolume(double& totalVolume, bool markVisibleFaces)
|
||||
{
|
||||
// mark visible faces
|
||||
CircularListElement<TMMTriangle>* fHead = m_mesh.GetTriangles().GetHead();
|
||||
CircularListElement<TMMTriangle>* f = fHead;
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
CircularListElement<TMMVertex>* vertex0 = vertices.GetHead();
|
||||
bool visible = false;
|
||||
Vec3<double> pos0 = Vec3<double>(vertex0->GetData().m_pos.X(),
|
||||
vertex0->GetData().m_pos.Y(),
|
||||
vertex0->GetData().m_pos.Z());
|
||||
double vol = 0.0;
|
||||
totalVolume = 0.0;
|
||||
Vec3<double> ver0, ver1, ver2;
|
||||
do {
|
||||
ver0.X() = f->GetData().m_vertices[0]->GetData().m_pos.X();
|
||||
ver0.Y() = f->GetData().m_vertices[0]->GetData().m_pos.Y();
|
||||
ver0.Z() = f->GetData().m_vertices[0]->GetData().m_pos.Z();
|
||||
ver1.X() = f->GetData().m_vertices[1]->GetData().m_pos.X();
|
||||
ver1.Y() = f->GetData().m_vertices[1]->GetData().m_pos.Y();
|
||||
ver1.Z() = f->GetData().m_vertices[1]->GetData().m_pos.Z();
|
||||
ver2.X() = f->GetData().m_vertices[2]->GetData().m_pos.X();
|
||||
ver2.Y() = f->GetData().m_vertices[2]->GetData().m_pos.Y();
|
||||
ver2.Z() = f->GetData().m_vertices[2]->GetData().m_pos.Z();
|
||||
vol = ComputeVolume4(ver0, ver1, ver2, pos0);
|
||||
if (vol < -sc_eps) {
|
||||
vol = fabs(vol);
|
||||
totalVolume += vol;
|
||||
if (markVisibleFaces) {
|
||||
f->GetData().m_visible = true;
|
||||
m_trianglesToDelete.PushBack(f);
|
||||
}
|
||||
visible = true;
|
||||
}
|
||||
f = f->GetNext();
|
||||
} while (f != fHead);
|
||||
|
||||
if (m_trianglesToDelete.Size() == m_mesh.m_triangles.GetSize()) {
|
||||
for (size_t i = 0; i < m_trianglesToDelete.Size(); i++) {
|
||||
m_trianglesToDelete[i]->GetData().m_visible = false;
|
||||
}
|
||||
visible = false;
|
||||
}
|
||||
// if no faces visible from p then p is inside the hull
|
||||
if (!visible && markVisibleFaces) {
|
||||
vertices.Delete();
|
||||
m_trianglesToDelete.Resize(0);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool ICHull::ProcessPoint()
|
||||
{
|
||||
double totalVolume = 0.0;
|
||||
if (!ComputePointVolume(totalVolume, true)) {
|
||||
return false;
|
||||
}
|
||||
// Mark edges in interior of visible region for deletion.
|
||||
// Create a new face based on each border edge
|
||||
CircularListElement<TMMVertex>* v0 = m_mesh.GetVertices().GetHead();
|
||||
CircularListElement<TMMEdge>* eHead = m_mesh.GetEdges().GetHead();
|
||||
CircularListElement<TMMEdge>* e = eHead;
|
||||
CircularListElement<TMMEdge>* tmp = 0;
|
||||
int nvisible = 0;
|
||||
m_edgesToDelete.Resize(0);
|
||||
m_edgesToUpdate.Resize(0);
|
||||
do {
|
||||
tmp = e->GetNext();
|
||||
nvisible = 0;
|
||||
for (int k = 0; k < 2; k++) {
|
||||
if (e->GetData().m_triangles[k]->GetData().m_visible) {
|
||||
nvisible++;
|
||||
}
|
||||
}
|
||||
if (nvisible == 2) {
|
||||
m_edgesToDelete.PushBack(e);
|
||||
}
|
||||
else if (nvisible == 1) {
|
||||
e->GetData().m_newFace = MakeConeFace(e, v0);
|
||||
m_edgesToUpdate.PushBack(e);
|
||||
}
|
||||
e = tmp;
|
||||
} while (e != eHead);
|
||||
return true;
|
||||
}
|
||||
bool ICHull::MakeCCW(CircularListElement<TMMTriangle>* f,
|
||||
CircularListElement<TMMEdge>* e,
|
||||
CircularListElement<TMMVertex>* v)
|
||||
{
|
||||
// the visible face adjacent to e
|
||||
CircularListElement<TMMTriangle>* fv;
|
||||
if (e->GetData().m_triangles[0]->GetData().m_visible) {
|
||||
fv = e->GetData().m_triangles[0];
|
||||
}
|
||||
else {
|
||||
fv = e->GetData().m_triangles[1];
|
||||
}
|
||||
|
||||
// set vertex[0] and vertex[1] to have the same orientation as the corresponding vertices of fv.
|
||||
int i; // index of e->m_vertices[0] in fv
|
||||
CircularListElement<TMMVertex>* v0 = e->GetData().m_vertices[0];
|
||||
CircularListElement<TMMVertex>* v1 = e->GetData().m_vertices[1];
|
||||
for (i = 0; fv->GetData().m_vertices[i] != v0; i++)
|
||||
;
|
||||
|
||||
if (fv->GetData().m_vertices[(i + 1) % 3] != e->GetData().m_vertices[1]) {
|
||||
f->GetData().m_vertices[0] = v1;
|
||||
f->GetData().m_vertices[1] = v0;
|
||||
}
|
||||
else {
|
||||
f->GetData().m_vertices[0] = v0;
|
||||
f->GetData().m_vertices[1] = v1;
|
||||
// swap edges
|
||||
CircularListElement<TMMEdge>* tmp = f->GetData().m_edges[0];
|
||||
f->GetData().m_edges[0] = f->GetData().m_edges[1];
|
||||
f->GetData().m_edges[1] = tmp;
|
||||
}
|
||||
f->GetData().m_vertices[2] = v;
|
||||
return true;
|
||||
}
|
||||
bool ICHull::CleanUp(unsigned int& addedPoints)
|
||||
{
|
||||
bool r0 = CleanEdges();
|
||||
bool r1 = CleanTriangles();
|
||||
bool r2 = CleanVertices(addedPoints);
|
||||
return r0 && r1 && r2;
|
||||
}
|
||||
bool ICHull::CleanEdges()
|
||||
{
|
||||
// integrate the new faces into the data structure
|
||||
CircularListElement<TMMEdge>* e;
|
||||
const size_t ne_update = m_edgesToUpdate.Size();
|
||||
for (size_t i = 0; i < ne_update; ++i) {
|
||||
e = m_edgesToUpdate[i];
|
||||
if (e->GetData().m_newFace) {
|
||||
if (e->GetData().m_triangles[0]->GetData().m_visible) {
|
||||
e->GetData().m_triangles[0] = e->GetData().m_newFace;
|
||||
}
|
||||
else {
|
||||
e->GetData().m_triangles[1] = e->GetData().m_newFace;
|
||||
}
|
||||
e->GetData().m_newFace = 0;
|
||||
}
|
||||
}
|
||||
// delete edges maked for deletion
|
||||
CircularList<TMMEdge>& edges = m_mesh.GetEdges();
|
||||
const size_t ne_delete = m_edgesToDelete.Size();
|
||||
for (size_t i = 0; i < ne_delete; ++i) {
|
||||
edges.Delete(m_edgesToDelete[i]);
|
||||
}
|
||||
m_edgesToDelete.Resize(0);
|
||||
m_edgesToUpdate.Resize(0);
|
||||
return true;
|
||||
}
|
||||
bool ICHull::CleanTriangles()
|
||||
{
|
||||
CircularList<TMMTriangle>& triangles = m_mesh.GetTriangles();
|
||||
const size_t nt_delete = m_trianglesToDelete.Size();
|
||||
for (size_t i = 0; i < nt_delete; ++i) {
|
||||
triangles.Delete(m_trianglesToDelete[i]);
|
||||
}
|
||||
m_trianglesToDelete.Resize(0);
|
||||
return true;
|
||||
}
|
||||
bool ICHull::CleanVertices(unsigned int& addedPoints)
|
||||
{
|
||||
// mark all vertices incident to some undeleted edge as on the hull
|
||||
CircularList<TMMEdge>& edges = m_mesh.GetEdges();
|
||||
CircularListElement<TMMEdge>* e = edges.GetHead();
|
||||
size_t nE = edges.GetSize();
|
||||
for (size_t i = 0; i < nE; i++) {
|
||||
e->GetData().m_vertices[0]->GetData().m_onHull = true;
|
||||
e->GetData().m_vertices[1]->GetData().m_onHull = true;
|
||||
e = e->GetNext();
|
||||
}
|
||||
// delete all the vertices that have been processed but are not on the hull
|
||||
CircularList<TMMVertex>& vertices = m_mesh.GetVertices();
|
||||
CircularListElement<TMMVertex>* vHead = vertices.GetHead();
|
||||
CircularListElement<TMMVertex>* v = vHead;
|
||||
v = v->GetPrev();
|
||||
do {
|
||||
if (v->GetData().m_tag && !v->GetData().m_onHull) {
|
||||
CircularListElement<TMMVertex>* tmp = v->GetPrev();
|
||||
vertices.Delete(v);
|
||||
v = tmp;
|
||||
addedPoints--;
|
||||
}
|
||||
else {
|
||||
v->GetData().m_duplicate = 0;
|
||||
v->GetData().m_onHull = false;
|
||||
v = v->GetPrev();
|
||||
}
|
||||
} while (v->GetData().m_tag && v != vHead);
|
||||
return true;
|
||||
}
|
||||
void ICHull::Clear()
|
||||
{
|
||||
m_mesh.Clear();
|
||||
m_edgesToDelete.Resize(0);
|
||||
m_edgesToUpdate.Resize(0);
|
||||
m_trianglesToDelete.Resize(0);
|
||||
m_isFlat = false;
|
||||
}
|
||||
const ICHull& ICHull::operator=(ICHull& rhs)
|
||||
{
|
||||
if (&rhs != this) {
|
||||
m_mesh.Copy(rhs.m_mesh);
|
||||
m_edgesToDelete = rhs.m_edgesToDelete;
|
||||
m_edgesToUpdate = rhs.m_edgesToUpdate;
|
||||
m_trianglesToDelete = rhs.m_trianglesToDelete;
|
||||
m_isFlat = rhs.m_isFlat;
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
bool ICHull::IsInside(const Vec3<double>& pt0, const double eps)
|
||||
{
|
||||
const Vec3<double> pt(pt0.X(), pt0.Y(), pt0.Z());
|
||||
if (m_isFlat) {
|
||||
size_t nT = m_mesh.m_triangles.GetSize();
|
||||
Vec3<double> ver0, ver1, ver2, a, b, c;
|
||||
double u, v;
|
||||
for (size_t t = 0; t < nT; t++) {
|
||||
ver0.X() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[0]->GetData().m_pos.X();
|
||||
ver0.Y() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[0]->GetData().m_pos.Y();
|
||||
ver0.Z() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[0]->GetData().m_pos.Z();
|
||||
ver1.X() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[1]->GetData().m_pos.X();
|
||||
ver1.Y() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[1]->GetData().m_pos.Y();
|
||||
ver1.Z() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[1]->GetData().m_pos.Z();
|
||||
ver2.X() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[2]->GetData().m_pos.X();
|
||||
ver2.Y() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[2]->GetData().m_pos.Y();
|
||||
ver2.Z() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[2]->GetData().m_pos.Z();
|
||||
a = ver1 - ver0;
|
||||
b = ver2 - ver0;
|
||||
c = pt - ver0;
|
||||
u = c * a;
|
||||
v = c * b;
|
||||
if (u >= 0.0 && u <= 1.0 && v >= 0.0 && u + v <= 1.0) {
|
||||
return true;
|
||||
}
|
||||
m_mesh.m_triangles.Next();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
size_t nT = m_mesh.m_triangles.GetSize();
|
||||
Vec3<double> ver0, ver1, ver2;
|
||||
double vol;
|
||||
for (size_t t = 0; t < nT; t++) {
|
||||
ver0.X() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[0]->GetData().m_pos.X();
|
||||
ver0.Y() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[0]->GetData().m_pos.Y();
|
||||
ver0.Z() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[0]->GetData().m_pos.Z();
|
||||
ver1.X() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[1]->GetData().m_pos.X();
|
||||
ver1.Y() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[1]->GetData().m_pos.Y();
|
||||
ver1.Z() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[1]->GetData().m_pos.Z();
|
||||
ver2.X() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[2]->GetData().m_pos.X();
|
||||
ver2.Y() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[2]->GetData().m_pos.Y();
|
||||
ver2.Z() = m_mesh.m_triangles.GetHead()->GetData().m_vertices[2]->GetData().m_pos.Z();
|
||||
vol = ComputeVolume4(ver0, ver1, ver2, pt);
|
||||
if (vol < eps) {
|
||||
return false;
|
||||
}
|
||||
m_mesh.m_triangles.Next();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
202
Extras/VHACD/src/vhacdManifoldMesh.cpp
Normal file
202
Extras/VHACD/src/vhacdManifoldMesh.cpp
Normal file
@@ -0,0 +1,202 @@
|
||||
/* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
|
||||
All rights reserved.
|
||||
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
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.
|
||||
|
||||
3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
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.
|
||||
*/
|
||||
#include "vhacdManifoldMesh.h"
|
||||
namespace VHACD {
|
||||
TMMVertex::TMMVertex(void)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
void TMMVertex::Initialize()
|
||||
{
|
||||
m_name = 0;
|
||||
m_id = 0;
|
||||
m_duplicate = 0;
|
||||
m_onHull = false;
|
||||
m_tag = false;
|
||||
}
|
||||
|
||||
TMMVertex::~TMMVertex(void)
|
||||
{
|
||||
}
|
||||
TMMEdge::TMMEdge(void)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
void TMMEdge::Initialize()
|
||||
{
|
||||
m_id = 0;
|
||||
m_triangles[0] = m_triangles[1] = m_newFace = 0;
|
||||
m_vertices[0] = m_vertices[1] = 0;
|
||||
}
|
||||
TMMEdge::~TMMEdge(void)
|
||||
{
|
||||
}
|
||||
void TMMTriangle::Initialize()
|
||||
{
|
||||
m_id = 0;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
m_edges[i] = 0;
|
||||
m_vertices[0] = 0;
|
||||
}
|
||||
m_visible = false;
|
||||
}
|
||||
TMMTriangle::TMMTriangle(void)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
TMMTriangle::~TMMTriangle(void)
|
||||
{
|
||||
}
|
||||
TMMesh::TMMesh()
|
||||
{
|
||||
}
|
||||
TMMesh::~TMMesh(void)
|
||||
{
|
||||
}
|
||||
void TMMesh::GetIFS(Vec3<double>* const points, Vec3<int>* const triangles)
|
||||
{
|
||||
size_t nV = m_vertices.GetSize();
|
||||
size_t nT = m_triangles.GetSize();
|
||||
|
||||
for (size_t v = 0; v < nV; v++) {
|
||||
points[v] = m_vertices.GetData().m_pos;
|
||||
m_vertices.GetData().m_id = v;
|
||||
m_vertices.Next();
|
||||
}
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
TMMTriangle& currentTriangle = m_triangles.GetData();
|
||||
triangles[f].X() = static_cast<int>(currentTriangle.m_vertices[0]->GetData().m_id);
|
||||
triangles[f].Y() = static_cast<int>(currentTriangle.m_vertices[1]->GetData().m_id);
|
||||
triangles[f].Z() = static_cast<int>(currentTriangle.m_vertices[2]->GetData().m_id);
|
||||
m_triangles.Next();
|
||||
}
|
||||
}
|
||||
void TMMesh::Clear()
|
||||
{
|
||||
m_vertices.Clear();
|
||||
m_edges.Clear();
|
||||
m_triangles.Clear();
|
||||
}
|
||||
void TMMesh::Copy(TMMesh& mesh)
|
||||
{
|
||||
Clear();
|
||||
// updating the id's
|
||||
size_t nV = mesh.m_vertices.GetSize();
|
||||
size_t nE = mesh.m_edges.GetSize();
|
||||
size_t nT = mesh.m_triangles.GetSize();
|
||||
for (size_t v = 0; v < nV; v++) {
|
||||
mesh.m_vertices.GetData().m_id = v;
|
||||
mesh.m_vertices.Next();
|
||||
}
|
||||
for (size_t e = 0; e < nE; e++) {
|
||||
mesh.m_edges.GetData().m_id = e;
|
||||
mesh.m_edges.Next();
|
||||
}
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
mesh.m_triangles.GetData().m_id = f;
|
||||
mesh.m_triangles.Next();
|
||||
}
|
||||
// copying data
|
||||
m_vertices = mesh.m_vertices;
|
||||
m_edges = mesh.m_edges;
|
||||
m_triangles = mesh.m_triangles;
|
||||
|
||||
// generate mapping
|
||||
CircularListElement<TMMVertex>** vertexMap = new CircularListElement<TMMVertex>*[nV];
|
||||
CircularListElement<TMMEdge>** edgeMap = new CircularListElement<TMMEdge>*[nE];
|
||||
CircularListElement<TMMTriangle>** triangleMap = new CircularListElement<TMMTriangle>*[nT];
|
||||
for (size_t v = 0; v < nV; v++) {
|
||||
vertexMap[v] = m_vertices.GetHead();
|
||||
m_vertices.Next();
|
||||
}
|
||||
for (size_t e = 0; e < nE; e++) {
|
||||
edgeMap[e] = m_edges.GetHead();
|
||||
m_edges.Next();
|
||||
}
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
triangleMap[f] = m_triangles.GetHead();
|
||||
m_triangles.Next();
|
||||
}
|
||||
|
||||
// updating pointers
|
||||
for (size_t v = 0; v < nV; v++) {
|
||||
if (vertexMap[v]->GetData().m_duplicate) {
|
||||
vertexMap[v]->GetData().m_duplicate = edgeMap[vertexMap[v]->GetData().m_duplicate->GetData().m_id];
|
||||
}
|
||||
}
|
||||
for (size_t e = 0; e < nE; e++) {
|
||||
if (edgeMap[e]->GetData().m_newFace) {
|
||||
edgeMap[e]->GetData().m_newFace = triangleMap[edgeMap[e]->GetData().m_newFace->GetData().m_id];
|
||||
}
|
||||
if (nT > 0) {
|
||||
for (int f = 0; f < 2; f++) {
|
||||
if (edgeMap[e]->GetData().m_triangles[f]) {
|
||||
edgeMap[e]->GetData().m_triangles[f] = triangleMap[edgeMap[e]->GetData().m_triangles[f]->GetData().m_id];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int v = 0; v < 2; v++) {
|
||||
if (edgeMap[e]->GetData().m_vertices[v]) {
|
||||
edgeMap[e]->GetData().m_vertices[v] = vertexMap[edgeMap[e]->GetData().m_vertices[v]->GetData().m_id];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
if (nE > 0) {
|
||||
for (int e = 0; e < 3; e++) {
|
||||
if (triangleMap[f]->GetData().m_edges[e]) {
|
||||
triangleMap[f]->GetData().m_edges[e] = edgeMap[triangleMap[f]->GetData().m_edges[e]->GetData().m_id];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int v = 0; v < 3; v++) {
|
||||
if (triangleMap[f]->GetData().m_vertices[v]) {
|
||||
triangleMap[f]->GetData().m_vertices[v] = vertexMap[triangleMap[f]->GetData().m_vertices[v]->GetData().m_id];
|
||||
}
|
||||
}
|
||||
}
|
||||
delete[] vertexMap;
|
||||
delete[] edgeMap;
|
||||
delete[] triangleMap;
|
||||
}
|
||||
bool TMMesh::CheckConsistancy()
|
||||
{
|
||||
size_t nE = m_edges.GetSize();
|
||||
size_t nT = m_triangles.GetSize();
|
||||
for (size_t e = 0; e < nE; e++) {
|
||||
for (int f = 0; f < 2; f++) {
|
||||
if (!m_edges.GetHead()->GetData().m_triangles[f]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_edges.Next();
|
||||
}
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
for (int e = 0; e < 3; e++) {
|
||||
int found = 0;
|
||||
for (int k = 0; k < 2; k++) {
|
||||
if (m_triangles.GetHead()->GetData().m_edges[e]->GetData().m_triangles[k] == m_triangles.GetHead()) {
|
||||
found++;
|
||||
}
|
||||
}
|
||||
if (found != 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_triangles.Next();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
323
Extras/VHACD/src/vhacdMesh.cpp
Normal file
323
Extras/VHACD/src/vhacdMesh.cpp
Normal file
@@ -0,0 +1,323 @@
|
||||
/* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
|
||||
All rights reserved.
|
||||
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
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.
|
||||
|
||||
3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
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.
|
||||
*/
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include "btConvexHullComputer.h"
|
||||
#include "vhacdMesh.h"
|
||||
#include <fstream>
|
||||
#include <iosfwd>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
namespace VHACD {
|
||||
Mesh::Mesh()
|
||||
{
|
||||
m_diag = 1.0;
|
||||
}
|
||||
Mesh::~Mesh()
|
||||
{
|
||||
}
|
||||
double Mesh::ComputeVolume() const
|
||||
{
|
||||
const size_t nV = GetNPoints();
|
||||
const size_t nT = GetNTriangles();
|
||||
if (nV == 0 || nT == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
Vec3<double> bary(0.0, 0.0, 0.0);
|
||||
for (size_t v = 0; v < nV; v++) {
|
||||
bary += GetPoint(v);
|
||||
}
|
||||
bary /= static_cast<double>(nV);
|
||||
|
||||
Vec3<double> ver0, ver1, ver2;
|
||||
double totalVolume = 0.0;
|
||||
for (int t = 0; t < nT; t++) {
|
||||
const Vec3<int>& tri = GetTriangle(t);
|
||||
ver0 = GetPoint(tri[0]);
|
||||
ver1 = GetPoint(tri[1]);
|
||||
ver2 = GetPoint(tri[2]);
|
||||
totalVolume += ComputeVolume4(ver0, ver1, ver2, bary);
|
||||
}
|
||||
return totalVolume / 6.0;
|
||||
}
|
||||
|
||||
void Mesh::ComputeConvexHull(const double* const pts,
|
||||
const size_t nPts)
|
||||
{
|
||||
ResizePoints(0);
|
||||
ResizeTriangles(0);
|
||||
btConvexHullComputer ch;
|
||||
ch.compute(pts, 3 * sizeof(double), (int)nPts, -1.0, -1.0);
|
||||
for (int v = 0; v < ch.vertices.size(); v++) {
|
||||
AddPoint(Vec3<double>(ch.vertices[v].getX(), ch.vertices[v].getY(), ch.vertices[v].getZ()));
|
||||
}
|
||||
const int nt = ch.faces.size();
|
||||
for (int t = 0; t < nt; ++t) {
|
||||
const btConvexHullComputer::Edge* sourceEdge = &(ch.edges[ch.faces[t]]);
|
||||
int a = sourceEdge->getSourceVertex();
|
||||
int b = sourceEdge->getTargetVertex();
|
||||
const btConvexHullComputer::Edge* edge = sourceEdge->getNextEdgeOfFace();
|
||||
int c = edge->getTargetVertex();
|
||||
while (c != a) {
|
||||
AddTriangle(Vec3<int>(a, b, c));
|
||||
edge = edge->getNextEdgeOfFace();
|
||||
b = c;
|
||||
c = edge->getTargetVertex();
|
||||
}
|
||||
}
|
||||
}
|
||||
void Mesh::Clip(const Plane& plane,
|
||||
SArray<Vec3<double> >& positivePart,
|
||||
SArray<Vec3<double> >& negativePart) const
|
||||
{
|
||||
const size_t nV = GetNPoints();
|
||||
if (nV == 0) {
|
||||
return;
|
||||
}
|
||||
double d;
|
||||
for (size_t v = 0; v < nV; v++) {
|
||||
const Vec3<double>& pt = GetPoint(v);
|
||||
d = plane.m_a * pt[0] + plane.m_b * pt[1] + plane.m_c * pt[2] + plane.m_d;
|
||||
if (d > 0.0) {
|
||||
positivePart.PushBack(pt);
|
||||
}
|
||||
else if (d < 0.0) {
|
||||
negativePart.PushBack(pt);
|
||||
}
|
||||
else {
|
||||
positivePart.PushBack(pt);
|
||||
negativePart.PushBack(pt);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool Mesh::IsInside(const Vec3<double>& pt) const
|
||||
{
|
||||
const size_t nV = GetNPoints();
|
||||
const size_t nT = GetNTriangles();
|
||||
if (nV == 0 || nT == 0) {
|
||||
return false;
|
||||
}
|
||||
Vec3<double> ver0, ver1, ver2;
|
||||
double volume;
|
||||
for (int t = 0; t < nT; t++) {
|
||||
const Vec3<int>& tri = GetTriangle(t);
|
||||
ver0 = GetPoint(tri[0]);
|
||||
ver1 = GetPoint(tri[1]);
|
||||
ver2 = GetPoint(tri[2]);
|
||||
volume = ComputeVolume4(ver0, ver1, ver2, pt);
|
||||
if (volume < 0.0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
double Mesh::ComputeDiagBB()
|
||||
{
|
||||
const size_t nPoints = GetNPoints();
|
||||
if (nPoints == 0)
|
||||
return 0.0;
|
||||
Vec3<double> minBB = m_points[0];
|
||||
Vec3<double> maxBB = m_points[0];
|
||||
double x, y, z;
|
||||
for (size_t v = 1; v < nPoints; v++) {
|
||||
x = m_points[v][0];
|
||||
y = m_points[v][1];
|
||||
z = m_points[v][2];
|
||||
if (x < minBB[0])
|
||||
minBB[0] = x;
|
||||
else if (x > maxBB[0])
|
||||
maxBB[0] = x;
|
||||
if (y < minBB[1])
|
||||
minBB[1] = y;
|
||||
else if (y > maxBB[1])
|
||||
maxBB[1] = y;
|
||||
if (z < minBB[2])
|
||||
minBB[2] = z;
|
||||
else if (z > maxBB[2])
|
||||
maxBB[2] = z;
|
||||
}
|
||||
return (m_diag = (maxBB - minBB).GetNorm());
|
||||
}
|
||||
|
||||
#ifdef VHACD_DEBUG_MESH
|
||||
bool Mesh::SaveVRML2(const std::string& fileName) const
|
||||
{
|
||||
std::ofstream fout(fileName.c_str());
|
||||
if (fout.is_open()) {
|
||||
const Material material;
|
||||
|
||||
if (SaveVRML2(fout, material)) {
|
||||
fout.close();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool Mesh::SaveVRML2(std::ofstream& fout, const Material& material) const
|
||||
{
|
||||
if (fout.is_open()) {
|
||||
fout.setf(std::ios::fixed, std::ios::floatfield);
|
||||
fout.setf(std::ios::showpoint);
|
||||
fout.precision(6);
|
||||
size_t nV = m_points.Size();
|
||||
size_t nT = m_triangles.Size();
|
||||
fout << "#VRML V2.0 utf8" << std::endl;
|
||||
fout << "" << std::endl;
|
||||
fout << "# Vertices: " << nV << std::endl;
|
||||
fout << "# Triangles: " << nT << std::endl;
|
||||
fout << "" << std::endl;
|
||||
fout << "Group {" << std::endl;
|
||||
fout << " children [" << std::endl;
|
||||
fout << " Shape {" << std::endl;
|
||||
fout << " appearance Appearance {" << std::endl;
|
||||
fout << " material Material {" << std::endl;
|
||||
fout << " diffuseColor " << material.m_diffuseColor[0] << " "
|
||||
<< material.m_diffuseColor[1] << " "
|
||||
<< material.m_diffuseColor[2] << std::endl;
|
||||
fout << " ambientIntensity " << material.m_ambientIntensity << std::endl;
|
||||
fout << " specularColor " << material.m_specularColor[0] << " "
|
||||
<< material.m_specularColor[1] << " "
|
||||
<< material.m_specularColor[2] << std::endl;
|
||||
fout << " emissiveColor " << material.m_emissiveColor[0] << " "
|
||||
<< material.m_emissiveColor[1] << " "
|
||||
<< material.m_emissiveColor[2] << std::endl;
|
||||
fout << " shininess " << material.m_shininess << std::endl;
|
||||
fout << " transparency " << material.m_transparency << std::endl;
|
||||
fout << " }" << std::endl;
|
||||
fout << " }" << std::endl;
|
||||
fout << " geometry IndexedFaceSet {" << std::endl;
|
||||
fout << " ccw TRUE" << std::endl;
|
||||
fout << " solid TRUE" << std::endl;
|
||||
fout << " convex TRUE" << std::endl;
|
||||
if (nV > 0) {
|
||||
fout << " coord DEF co Coordinate {" << std::endl;
|
||||
fout << " point [" << std::endl;
|
||||
for (size_t v = 0; v < nV; v++) {
|
||||
fout << " " << m_points[v][0] << " "
|
||||
<< m_points[v][1] << " "
|
||||
<< m_points[v][2] << "," << std::endl;
|
||||
}
|
||||
fout << " ]" << std::endl;
|
||||
fout << " }" << std::endl;
|
||||
}
|
||||
if (nT > 0) {
|
||||
fout << " coordIndex [ " << std::endl;
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
fout << " " << m_triangles[f][0] << ", "
|
||||
<< m_triangles[f][1] << ", "
|
||||
<< m_triangles[f][2] << ", -1," << std::endl;
|
||||
}
|
||||
fout << " ]" << std::endl;
|
||||
}
|
||||
fout << " }" << std::endl;
|
||||
fout << " }" << std::endl;
|
||||
fout << " ]" << std::endl;
|
||||
fout << "}" << std::endl;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool Mesh::SaveOFF(const std::string& fileName) const
|
||||
{
|
||||
std::ofstream fout(fileName.c_str());
|
||||
if (fout.is_open()) {
|
||||
size_t nV = m_points.Size();
|
||||
size_t nT = m_triangles.Size();
|
||||
fout << "OFF" << std::endl;
|
||||
fout << nV << " " << nT << " " << 0 << std::endl;
|
||||
for (size_t v = 0; v < nV; v++) {
|
||||
fout << m_points[v][0] << " "
|
||||
<< m_points[v][1] << " "
|
||||
<< m_points[v][2] << std::endl;
|
||||
}
|
||||
for (size_t f = 0; f < nT; f++) {
|
||||
fout << "3 " << m_triangles[f][0] << " "
|
||||
<< m_triangles[f][1] << " "
|
||||
<< m_triangles[f][2] << std::endl;
|
||||
}
|
||||
fout.close();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Mesh::LoadOFF(const std::string& fileName, bool invert)
|
||||
{
|
||||
FILE* fid = fopen(fileName.c_str(), "r");
|
||||
if (fid) {
|
||||
const std::string strOFF("OFF");
|
||||
char temp[1024];
|
||||
fscanf(fid, "%s", temp);
|
||||
if (std::string(temp) != strOFF) {
|
||||
fclose(fid);
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
int nv = 0;
|
||||
int nf = 0;
|
||||
int ne = 0;
|
||||
fscanf(fid, "%i", &nv);
|
||||
fscanf(fid, "%i", &nf);
|
||||
fscanf(fid, "%i", &ne);
|
||||
m_points.Resize(nv);
|
||||
m_triangles.Resize(nf);
|
||||
Vec3<double> coord;
|
||||
float x, y, z;
|
||||
for (int p = 0; p < nv; p++) {
|
||||
fscanf(fid, "%f", &x);
|
||||
fscanf(fid, "%f", &y);
|
||||
fscanf(fid, "%f", &z);
|
||||
m_points[p][0] = x;
|
||||
m_points[p][1] = y;
|
||||
m_points[p][2] = z;
|
||||
}
|
||||
int i, j, k, s;
|
||||
for (int t = 0; t < nf; ++t) {
|
||||
fscanf(fid, "%i", &s);
|
||||
if (s == 3) {
|
||||
fscanf(fid, "%i", &i);
|
||||
fscanf(fid, "%i", &j);
|
||||
fscanf(fid, "%i", &k);
|
||||
m_triangles[t][0] = i;
|
||||
if (invert) {
|
||||
m_triangles[t][1] = k;
|
||||
m_triangles[t][2] = j;
|
||||
}
|
||||
else {
|
||||
m_triangles[t][1] = j;
|
||||
m_triangles[t][2] = k;
|
||||
}
|
||||
}
|
||||
else // Fix me: support only triangular meshes
|
||||
{
|
||||
for (int h = 0; h < s; ++h)
|
||||
fscanf(fid, "%i", &s);
|
||||
}
|
||||
}
|
||||
fclose(fid);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif // VHACD_DEBUG_MESH
|
||||
}
|
||||
1617
Extras/VHACD/src/vhacdVolume.cpp
Normal file
1617
Extras/VHACD/src/vhacdVolume.cpp
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user