2017-02-02 21:58:40 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
|
|
|
* Copyright (c) 2010-2016 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "HalfEdgeStructure.h"
|
2018-01-26 16:34:26 +00:00
|
|
|
#include "containers/Map.h"
|
2018-02-05 06:41:02 +00:00
|
|
|
#include "containers/Pair.h"
|
2018-01-26 16:34:26 +00:00
|
|
|
#include "containers/containers_common.h"
|
2017-02-02 21:58:40 +00:00
|
|
|
|
|
|
|
using namespace reactphysics3d;
|
|
|
|
|
2017-03-22 18:07:31 +00:00
|
|
|
// Initialize the structure (when all vertices and faces have been added)
|
|
|
|
void HalfEdgeStructure::init() {
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2018-01-26 16:34:26 +00:00
|
|
|
Map<VerticesPair, Edge> edges(mAllocator);
|
|
|
|
Map<VerticesPair, VerticesPair> nextEdges(mAllocator);
|
|
|
|
Map<VerticesPair, uint> mapEdgeToStartVertex(mAllocator);
|
|
|
|
Map<VerticesPair, uint> mapEdgeToIndex(mAllocator);
|
|
|
|
Map<uint, VerticesPair> mapEdgeIndexToKey(mAllocator);
|
|
|
|
Map<uint, VerticesPair> mapFaceIndexToEdgeKey(mAllocator);
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2018-01-26 16:34:26 +00:00
|
|
|
List<VerticesPair> currentFaceEdges(mAllocator, mFaces[0].faceVertices.size());
|
2018-01-20 16:30:36 +00:00
|
|
|
|
2017-02-02 21:58:40 +00:00
|
|
|
// For each face
|
2017-03-22 18:07:31 +00:00
|
|
|
for (uint f=0; f<mFaces.size(); f++) {
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2017-03-22 18:07:31 +00:00
|
|
|
Face face = mFaces[f];
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2018-02-05 06:41:02 +00:00
|
|
|
VerticesPair firstEdgeKey(0, 0);
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2017-02-13 21:38:47 +00:00
|
|
|
// For each vertex of the face
|
2017-03-22 18:07:31 +00:00
|
|
|
for (uint v=0; v < face.faceVertices.size(); v++) {
|
|
|
|
uint v1Index = face.faceVertices[v];
|
|
|
|
uint v2Index = face.faceVertices[v == (face.faceVertices.size() - 1) ? 0 : v + 1];
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2018-01-26 16:34:26 +00:00
|
|
|
const VerticesPair pairV1V2 = VerticesPair(v1Index, v2Index);
|
2017-02-02 21:58:40 +00:00
|
|
|
|
|
|
|
// Create a new half-edge
|
|
|
|
Edge edge;
|
|
|
|
edge.faceIndex = f;
|
|
|
|
edge.vertexIndex = v1Index;
|
2017-02-13 21:38:47 +00:00
|
|
|
if (v == 0) {
|
2017-02-02 21:58:40 +00:00
|
|
|
firstEdgeKey = pairV1V2;
|
|
|
|
}
|
2017-02-13 21:38:47 +00:00
|
|
|
else if (v >= 1) {
|
2018-02-05 06:41:02 +00:00
|
|
|
nextEdges.add(Pair<VerticesPair, VerticesPair>(currentFaceEdges[currentFaceEdges.size() - 1], pairV1V2));
|
2017-02-02 21:58:40 +00:00
|
|
|
}
|
2017-03-22 18:07:31 +00:00
|
|
|
if (v == (face.faceVertices.size() - 1)) {
|
2018-02-05 06:41:02 +00:00
|
|
|
nextEdges.add(Pair<VerticesPair, VerticesPair>(pairV1V2, firstEdgeKey));
|
2017-02-02 21:58:40 +00:00
|
|
|
}
|
2018-02-05 06:41:02 +00:00
|
|
|
edges.add(Pair<VerticesPair, Edge>(pairV1V2, edge));
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2018-01-26 16:34:26 +00:00
|
|
|
const VerticesPair pairV2V1(v2Index, v1Index);
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2018-02-05 06:41:02 +00:00
|
|
|
mapEdgeToStartVertex.add(Pair<VerticesPair, uint>(pairV1V2, v1Index), true);
|
|
|
|
mapEdgeToStartVertex.add(Pair<VerticesPair, uint>(pairV2V1, v2Index), true);
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2018-02-05 06:41:02 +00:00
|
|
|
mapFaceIndexToEdgeKey.add(Pair<uint, VerticesPair>(f, pairV1V2), true);
|
2017-02-20 15:11:13 +00:00
|
|
|
|
2017-02-02 21:58:40 +00:00
|
|
|
auto itEdge = edges.find(pairV2V1);
|
|
|
|
if (itEdge != edges.end()) {
|
|
|
|
|
|
|
|
const uint edgeIndex = mEdges.size();
|
|
|
|
|
|
|
|
itEdge->second.twinEdgeIndex = edgeIndex + 1;
|
|
|
|
edge.twinEdgeIndex = edgeIndex;
|
|
|
|
|
2018-02-05 06:41:02 +00:00
|
|
|
mapEdgeIndexToKey.add(Pair<uint, VerticesPair>(edgeIndex, pairV2V1));
|
|
|
|
mapEdgeIndexToKey.add(Pair<uint, VerticesPair>(edgeIndex + 1, pairV1V2));
|
2017-02-20 15:11:13 +00:00
|
|
|
|
|
|
|
mVertices[v1Index].edgeIndex = edgeIndex + 1;
|
|
|
|
mVertices[v2Index].edgeIndex = edgeIndex;
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2018-02-05 06:41:02 +00:00
|
|
|
mapEdgeToIndex.add(Pair<VerticesPair, uint>(pairV1V2, edgeIndex + 1));
|
|
|
|
mapEdgeToIndex.add(Pair<VerticesPair, uint>(pairV2V1, edgeIndex));
|
2017-02-13 21:38:47 +00:00
|
|
|
|
2017-12-27 19:53:09 +00:00
|
|
|
mEdges.add(itEdge->second);
|
|
|
|
mEdges.add(edge);
|
2017-02-02 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 16:30:36 +00:00
|
|
|
currentFaceEdges.add(pairV1V2);
|
2017-02-02 21:58:40 +00:00
|
|
|
}
|
2018-01-20 16:30:36 +00:00
|
|
|
|
|
|
|
currentFaceEdges.clear();
|
2017-02-13 21:38:47 +00:00
|
|
|
}
|
2017-02-02 21:58:40 +00:00
|
|
|
|
2017-02-13 21:38:47 +00:00
|
|
|
// Set next edges
|
2017-02-20 15:11:13 +00:00
|
|
|
for (uint i=0; i < mEdges.size(); i++) {
|
|
|
|
mEdges[i].nextEdgeIndex = mapEdgeToIndex[nextEdges[mapEdgeIndexToKey[i]]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set face edge
|
2017-03-22 18:07:31 +00:00
|
|
|
for (uint f=0; f < mFaces.size(); f++) {
|
2017-02-20 15:11:13 +00:00
|
|
|
mFaces[f].edgeIndex = mapEdgeToIndex[mapFaceIndexToEdgeKey[f]];
|
2017-02-02 21:58:40 +00:00
|
|
|
}
|
|
|
|
}
|