123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #pragma once
- #include <Mathematics/Vector3.h>
- #include <memory>
- #include <set>
- #include <vector>
- namespace WwiseGTE
- {
- template <typename Real>
- class Polyhedron3
- {
- public:
-
-
-
-
-
-
-
-
- Polyhedron3(std::shared_ptr<std::vector<Vector3<Real>>> const& vertexPool,
- int numIndices, int const* indices, bool counterClockwise)
- :
- mVertexPool(vertexPool),
- mCounterClockwise(counterClockwise)
- {
- if (vertexPool && indices && numIndices >= 12 && (numIndices % 3) == 0)
- {
- for (int i = 0; i < numIndices; ++i)
- {
- mUniqueIndices.insert(indices[i]);
- }
- mIndices.resize(numIndices);
- std::copy(indices, indices + numIndices, mIndices.begin());
- }
- else
- {
-
- mVertexPool = nullptr;
- mCounterClockwise = false;
- }
- }
-
-
-
- inline operator bool() const
- {
- return mVertexPool != nullptr;
- }
-
- inline std::shared_ptr<std::vector<Vector3<Real>>> const& GetVertexPool() const
- {
- return mVertexPool;
- }
- inline std::vector<Vector3<Real>> const& GetVertices() const
- {
- return *mVertexPool.get();
- }
- inline std::set<int> const& GetUniqueIndices() const
- {
- return mUniqueIndices;
- }
- inline std::vector<int> const& GetIndices() const
- {
- return mIndices;
- }
- inline bool CounterClockwise() const
- {
- return mCounterClockwise;
- }
-
- Vector3<Real> ComputeVertexAverage() const
- {
- Vector3<Real> average = Vector3<Real>::Zero();
- if (mVertexPool)
- {
- auto vertexPool = GetVertices();
- for (int index : mUniqueIndices)
- {
- average += vertexPool[index];
- }
- average /= static_cast<Real>(mUniqueIndices.size());
- }
- return average;
- }
- Real ComputeSurfaceArea() const
- {
- Real surfaceArea(0);
- if (mVertexPool)
- {
- auto vertexPool = GetVertices();
- int const numTriangles = static_cast<int>(mIndices.size()) / 3;
- int const* indices = mIndices.data();
- for (int t = 0; t < numTriangles; ++t)
- {
- int v0 = *indices++;
- int v1 = *indices++;
- int v2 = *indices++;
- Vector3<Real> edge0 = vertexPool[v1] - vertexPool[v0];
- Vector3<Real> edge1 = vertexPool[v2] - vertexPool[v0];
- Vector3<Real> cross = Cross(edge0, edge1);
- surfaceArea += Length(cross);
- }
- surfaceArea *= (Real)0.5;
- }
- return surfaceArea;
- }
- Real ComputeVolume() const
- {
- Real volume(0);
- if (mVertexPool)
- {
- auto vertexPool = GetVertices();
- int const numTriangles = static_cast<int>(mIndices.size()) / 3;
- int const* indices = mIndices.data();
- for (int t = 0; t < numTriangles; ++t)
- {
- int v0 = *indices++;
- int v1 = *indices++;
- int v2 = *indices++;
- volume += DotCross(vertexPool[v0], vertexPool[v1], vertexPool[v2]);
- }
- volume /= (Real)6;
- }
- return std::fabs(volume);
- }
- private:
- std::shared_ptr<std::vector<Vector3<Real>>> mVertexPool;
- std::set<int> mUniqueIndices;
- std::vector<int> mIndices;
- bool mCounterClockwise;
- };
- }
|