123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- #pragma once
- #include <Mathematics/Mesh.h>
- #include <Mathematics/ParametricCurve.h>
- #include <memory>
- namespace WwiseGTE
- {
- template <typename Real>
- class RevolutionMesh : public Mesh<Real>
- {
- public:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- RevolutionMesh(MeshDescription const& description,
- std::shared_ptr<ParametricCurve<2, Real>> const& curve,
- bool sampleByArcLength = false)
- :
- Mesh<Real>(description,
- { MeshTopology::CYLINDER, MeshTopology::TORUS, MeshTopology::DISK, MeshTopology::SPHERE }),
- mCurve(curve),
- mSampleByArcLength(sampleByArcLength)
- {
- if (!this->mDescription.constructed)
- {
-
-
- mCurve = nullptr;
- return;
- }
- LogAssert(mCurve != nullptr, "A nonnull revolution curve is required.");
-
-
- mCosAngle.resize(this->mDescription.numCols + 1);
- mSinAngle.resize(this->mDescription.numCols + 1);
- Real invRadialSamples = (Real)1 / (Real)this->mDescription.numCols;
- for (unsigned int c = 0; c < this->mDescription.numCols; ++c)
- {
- Real angle = c * invRadialSamples * (Real)GTE_C_TWO_PI;
- mCosAngle[c] = std::cos(angle);
- mSinAngle[c] = std::sin(angle);
- }
- mCosAngle[this->mDescription.numCols] = mCosAngle[0];
- mSinAngle[this->mDescription.numCols] = mSinAngle[0];
- CreateSampler();
- if (!this->mTCoords)
- {
- mDefaultTCoords.resize(this->mDescription.numVertices);
- this->mTCoords = mDefaultTCoords.data();
- this->mTCoordStride = sizeof(Vector2<Real>);
- this->mDescription.allowUpdateFrame = this->mDescription.wantDynamicTangentSpaceUpdate;
- if (this->mDescription.allowUpdateFrame)
- {
- if (!this->mDescription.hasTangentSpaceVectors)
- {
- this->mDescription.allowUpdateFrame = false;
- }
- if (!this->mNormals)
- {
- this->mDescription.allowUpdateFrame = false;
- }
- }
- }
- this->ComputeIndices();
- InitializeTCoords();
- UpdatePositions();
- if (this->mDescription.allowUpdateFrame)
- {
- this->UpdateFrame();
- }
- else if (this->mNormals)
- {
- this->UpdateNormals();
- }
- }
-
- inline std::shared_ptr<ParametricCurve<2, Real>> const& GetCurve() const
- {
- return mCurve;
- }
- inline bool IsSampleByArcLength() const
- {
- return mSampleByArcLength;
- }
- private:
- void CreateSampler()
- {
- if (this->mDescription.topology == MeshTopology::CYLINDER
- || this->mDescription.topology == MeshTopology::TORUS)
- {
- mSamples.resize(this->mDescription.rMax + 1);
- }
- else if (this->mDescription.topology == MeshTopology::DISK)
- {
- mSamples.resize(this->mDescription.rMax + 2);
- }
- else if (this->mDescription.topology == MeshTopology::SPHERE)
- {
- mSamples.resize(this->mDescription.rMax + 3);
- }
- Real invDenom = ((Real)1) / (Real)(mSamples.size() - 1);
- if (mSampleByArcLength)
- {
- Real factor = mCurve->GetTotalLength() * invDenom;
- mTSampler = [this, factor](unsigned int i)
- {
- return mCurve->GetTime(i * factor);
- };
- }
- else
- {
- Real factor = (mCurve->GetTMax() - mCurve->GetTMin()) * invDenom;
- mTSampler = [this, factor](unsigned int i)
- {
- return mCurve->GetTMin() + i * factor;
- };
- }
- }
- void InitializeTCoords()
- {
- Vector2<Real>tcoord;
- switch (this->mDescription.topology)
- {
- case MeshTopology::CYLINDER:
- {
- for (unsigned int r = 0, i = 0; r < this->mDescription.numRows; ++r)
- {
- tcoord[1] = (Real)r / (Real)(this->mDescription.numRows - 1);
- for (unsigned int c = 0; c <= this->mDescription.numCols; ++c, ++i)
- {
- tcoord[0] = (Real)c / (Real)this->mDescription.numCols;
- this->TCoord(i) = tcoord;
- }
- }
- break;
- }
- case MeshTopology::TORUS:
- {
- for (unsigned int r = 0, i = 0; r <= this->mDescription.numRows; ++r)
- {
- tcoord[1] = (Real)r / (Real)this->mDescription.numRows;
- for (unsigned int c = 0; c <= this->mDescription.numCols; ++c, ++i)
- {
- tcoord[0] = (Real)c / (Real)this->mDescription.numCols;
- this->TCoord(i) = tcoord;
- }
- }
- break;
- }
- case MeshTopology::DISK:
- {
- Vector2<Real> origin{ (Real)0.5, (Real)0.5 };
- unsigned int i = 0;
- for (unsigned int r = 0; r < this->mDescription.numRows; ++r)
- {
- Real radius = (Real)(r + 1) / (Real)(2 * this->mDescription.numRows);
- radius = std::min(radius, (Real)0.5);
- for (unsigned int c = 0; c <= this->mDescription.numCols; ++c, ++i)
- {
- Real angle = (Real)GTE_C_TWO_PI * (Real)c / (Real)this->mDescription.numCols;
- this->TCoord(i) = { radius * std::cos(angle), radius * std::sin(angle) };
- }
- }
- this->TCoord(i) = origin;
- break;
- }
- case MeshTopology::SPHERE:
- {
- unsigned int i = 0;
- for (unsigned int r = 0; r < this->mDescription.numRows; ++r)
- {
- tcoord[1] = (Real)r / (Real)(this->mDescription.numRows - 1);
- for (unsigned int c = 0; c <= this->mDescription.numCols; ++c, ++i)
- {
- tcoord[0] = (Real)c / (Real)this->mDescription.numCols;
- this->TCoord(i) = tcoord;
- }
- }
- this->TCoord(i++) = { (Real)0.5, (Real)0 };
- this->TCoord(i) = { (Real)0.5, (Real)1 };
- break;
- }
- default:
-
-
- break;
- }
- }
- virtual void UpdatePositions() override
- {
- unsigned int const numSamples = static_cast<unsigned int>(mSamples.size());
- for (unsigned int i = 0; i < numSamples; ++i)
- {
- Real t = mTSampler(i);
- Vector2<Real> position = mCurve->GetPosition(t);
- mSamples[i][0] = position[0];
- mSamples[i][1] = (Real)0;
- mSamples[i][2] = position[1];
- }
- switch (this->mDescription.topology)
- {
- case MeshTopology::CYLINDER:
- UpdateCylinderPositions();
- break;
- case MeshTopology::TORUS:
- UpdateTorusPositions();
- break;
- case MeshTopology::DISK:
- UpdateDiskPositions();
- break;
- case MeshTopology::SPHERE:
- UpdateSpherePositions();
- break;
- default:
- break;
- }
- }
- void UpdateCylinderPositions()
- {
- for (unsigned int r = 0, i = 0; r <= this->mDescription.rMax; ++r)
- {
- Real radius = mSamples[r][0];
- for (unsigned int c = 0; c <= this->mDescription.cMax; ++c, ++i)
- {
- this->Position(i) = { radius * mCosAngle[c], radius * mSinAngle[c], mSamples[r][2] };
- }
- }
- }
- void UpdateTorusPositions()
- {
- for (unsigned int r = 0, i = 0; r <= this->mDescription.rMax; ++r)
- {
- Real radius = mSamples[r][0];
- for (unsigned int c = 0; c <= this->mDescription.cMax; ++c, ++i)
- {
- this->Position(i) = { radius * mCosAngle[c], radius * mSinAngle[c], mSamples[r][2] };
- }
- }
- }
- void UpdateDiskPositions()
- {
- for (unsigned int r = 0, rp1 = 1, i = 0; r <= this->mDescription.rMax; ++r, ++rp1)
- {
- Real radius = mSamples[rp1][0];
- for (unsigned int c = 0; c <= this->mDescription.cMax; ++c, ++i)
- {
- this->Position(i) = { radius * mCosAngle[c], radius * mSinAngle[c], mSamples[rp1][2] };
- }
- }
- this->Position(this->mDescription.numVertices - 1) = { (Real)0, (Real)0, mSamples.front()[2] };
- }
- void UpdateSpherePositions()
- {
- for (unsigned int r = 0, rp1 = 1, i = 0; r <= this->mDescription.rMax; ++r, ++rp1)
- {
- Real radius = mSamples[rp1][0];
- for (unsigned int c = 0; c <= this->mDescription.cMax; ++c, ++i)
- {
- this->Position(i) = { radius * mCosAngle[c], radius * mSinAngle[c], mSamples[rp1][2] };
- }
- }
- this->Position(this->mDescription.numVertices - 2) = { (Real)0, (Real)0, mSamples.front()[2] };
- this->Position(this->mDescription.numVertices - 1) = { (Real)0, (Real)0, mSamples.back()[2] };
- }
- std::shared_ptr<ParametricCurve<2, Real>> mCurve;
- bool mSampleByArcLength;
- std::vector<Real> mCosAngle, mSinAngle;
- std::function<Real(unsigned int)> mTSampler;
- std::vector<Vector3<Real>> mSamples;
-
-
- std::vector<Vector2<Real>> mDefaultTCoords;
- };
- }
|