123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- #pragma once
- #include <Mathematics/Logger.h>
- #include <array>
- #include <map>
- #include <memory>
- namespace WwiseGTE
- {
- class VEManifoldMesh
- {
- public:
-
- class Vertex;
- typedef std::shared_ptr<Vertex>(*VCreator)(int);
- typedef std::map<int, std::shared_ptr<Vertex>> VMap;
-
- class Edge;
- typedef std::shared_ptr<Edge>(*ECreator)(int, int);
- typedef std::map<std::pair<int, int>, std::shared_ptr<Edge>> EMap;
-
- class Vertex
- {
- public:
- virtual ~Vertex() = default;
- Vertex(int v)
- :
- V(v)
- {
- }
-
- int V;
-
- std::array<std::weak_ptr<Edge>, 2> E;
- };
-
- class Edge
- {
- public:
- virtual ~Edge() = default;
- Edge(int v0, int v1)
- :
- V{ v0, v1 }
- {
- }
-
- std::array<int, 2> V;
-
- std::array<std::weak_ptr<Edge>, 2> E;
- };
-
- virtual ~VEManifoldMesh() = default;
- VEManifoldMesh(VCreator vCreator = nullptr, ECreator eCreator = nullptr)
- :
- mVCreator(vCreator ? vCreator : CreateVertex),
- mECreator(eCreator ? eCreator : CreateEdge),
- mThrowOnNonmanifoldInsertion(true)
- {
- }
-
- inline VMap const& GetVertices() const
- {
- return mVMap;
- }
- inline EMap const& GetEdges() const
- {
- return mEMap;
- }
-
-
-
-
- void ThrowOnNonmanifoldInsertion(bool doException)
- {
- mThrowOnNonmanifoldInsertion = doException;
- }
-
-
-
-
- std::shared_ptr<Edge> Insert(int v0, int v1)
- {
- std::pair<int, int> ekey(v0, v1);
- if (mEMap.find(ekey) != mEMap.end())
- {
-
-
- return nullptr;
- }
-
- std::shared_ptr<Edge> edge = mECreator(v0, v1);
- mEMap[ekey] = edge;
-
- for (int i = 0; i < 2; ++i)
- {
- int v = edge->V[i];
- std::shared_ptr<Vertex> vertex;
- auto viter = mVMap.find(v);
- if (viter == mVMap.end())
- {
-
- vertex = mVCreator(v);
- mVMap[v] = vertex;
-
- vertex->E[0] = edge;
- }
- else
- {
-
- vertex = viter->second;
- LogAssert(vertex != nullptr, "Unexpected condition.");
-
- if (vertex->E[1].lock())
- {
- if (mThrowOnNonmanifoldInsertion)
- {
- LogError("The mesh must be manifold.");
- }
- else
- {
- return nullptr;
- }
- }
- vertex->E[1] = edge;
-
- auto adjacent = vertex->E[0].lock();
- LogAssert(adjacent != nullptr, "Unexpected condition.");
- for (int j = 0; j < 2; ++j)
- {
- if (adjacent->V[j] == v)
- {
- adjacent->E[j] = edge;
- break;
- }
- }
-
- edge->E[i] = adjacent;
- }
- }
- return edge;
- }
-
-
- bool Remove(int v0, int v1)
- {
- std::pair<int, int> ekey(v0, v1);
- auto eiter = mEMap.find(ekey);
- if (eiter == mEMap.end())
- {
-
- return false;
- }
-
- std::shared_ptr<Edge> edge = eiter->second;
-
- for (int i = 0; i < 2; ++i)
- {
-
- auto viter = mVMap.find(edge->V[i]);
- LogAssert(viter != mVMap.end(), "Unexpected condition.");
- std::shared_ptr<Vertex> vertex = viter->second;
- LogAssert(vertex != nullptr, "Unexpected condition.");
- if (vertex->E[0].lock() == edge)
- {
-
- vertex->E[0] = vertex->E[1];
- vertex->E[1].reset();
- }
- else if (vertex->E[1].lock() == edge)
- {
- vertex->E[1].reset();
- }
- else
- {
- LogError("Unexpected condition.");
- }
-
- if (!vertex->E[0].lock() && !vertex->E[1].lock())
- {
- mVMap.erase(vertex->V);
- }
-
- auto adjacent = edge->E[i].lock();
- if (adjacent)
- {
- for (int j = 0; j < 2; ++j)
- {
- if (adjacent->E[j].lock() == edge)
- {
- adjacent->E[j].reset();
- break;
- }
- }
- }
- }
- mEMap.erase(ekey);
- return true;
- }
-
- bool IsClosed() const
- {
- for (auto const& element : mVMap)
- {
- auto vertex = element.second;
- if (!vertex->E[0].lock() || !vertex->E[1].lock())
- {
- return false;
- }
- }
- return true;
- }
- protected:
-
- static std::shared_ptr<Vertex> CreateVertex(int v0)
- {
- return std::make_shared<Vertex>(v0);
- }
- VCreator mVCreator;
- VMap mVMap;
-
- static std::shared_ptr<Edge> CreateEdge(int v0, int v1)
- {
- return std::make_shared<Edge>(v0, v1);
- }
- ECreator mECreator;
- EMap mEMap;
- bool mThrowOnNonmanifoldInsertion;
- };
- }
|