RectanglePatchMesh.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Mesh.h>
  9. #include <Mathematics/ParametricSurface.h>
  10. #include <memory>
  11. namespace WwiseGTE
  12. {
  13. template <typename Real>
  14. class RectanglePatchMesh : public Mesh<Real>
  15. {
  16. public:
  17. // Create a mesh (x(u,v),y(u,v),z(u,v)) defined by the specified
  18. // surface. It is required that surface->IsRectangular() return
  19. // 'true'.
  20. RectanglePatchMesh(MeshDescription const& description,
  21. std::shared_ptr<ParametricSurface<3, Real>> const& surface)
  22. :
  23. Mesh<Real>(description, { MeshTopology::RECTANGLE }),
  24. mSurface(surface)
  25. {
  26. if (!this->mDescription.constructed)
  27. {
  28. // The logger system will report these errors in the Mesh
  29. // constructor.
  30. mSurface = nullptr;
  31. return;
  32. }
  33. LogAssert(mSurface != nullptr && mSurface->IsRectangular(),
  34. "A nonnull rectangular surface is required.");
  35. if (!this->mTCoords)
  36. {
  37. mDefaultTCoords.resize(this->mDescription.numVertices);
  38. this->mTCoords = mDefaultTCoords.data();
  39. this->mTCoordStride = sizeof(Vector2<Real>);
  40. this->mDescription.allowUpdateFrame = this->mDescription.wantDynamicTangentSpaceUpdate;
  41. if (this->mDescription.allowUpdateFrame)
  42. {
  43. if (!this->mDescription.hasTangentSpaceVectors)
  44. {
  45. this->mDescription.allowUpdateFrame = false;
  46. }
  47. if (!this->mNormals)
  48. {
  49. this->mDescription.allowUpdateFrame = false;
  50. }
  51. }
  52. }
  53. this->ComputeIndices();
  54. InitializeTCoords();
  55. InitializePositions();
  56. if (this->mDescription.allowUpdateFrame)
  57. {
  58. InitializeFrame();
  59. }
  60. else if (this->mNormals)
  61. {
  62. InitializeNormals();
  63. }
  64. }
  65. // Member access.
  66. inline std::shared_ptr<ParametricSurface<3, Real>> const& GetSurface() const
  67. {
  68. return mSurface;
  69. }
  70. protected:
  71. void InitializeTCoords()
  72. {
  73. Real uMin = mSurface->GetUMin();
  74. Real uDelta = (mSurface->GetUMax() - uMin) / static_cast<Real>(this->mDescription.numCols - 1);
  75. Real vMin = mSurface->GetVMin();
  76. Real vDelta = (mSurface->GetVMax() - vMin) / static_cast<Real>(this->mDescription.numRows - 1);
  77. Vector2<Real> tcoord;
  78. for (uint32_t r = 0, i = 0; r < this->mDescription.numRows; ++r)
  79. {
  80. tcoord[1] = vMin + vDelta * (Real)r;
  81. for (uint32_t c = 0; c < this->mDescription.numCols; ++c, ++i)
  82. {
  83. tcoord[0] = uMin + uDelta * (Real)c;
  84. this->TCoord(i) = tcoord;
  85. }
  86. }
  87. }
  88. void InitializePositions()
  89. {
  90. for (uint32_t r = 0, i = 0; r < this->mDescription.numRows; ++r)
  91. {
  92. for (uint32_t c = 0; c < this->mDescription.numCols; ++c, ++i)
  93. {
  94. Vector2<Real> tcoord = this->TCoord(i);
  95. this->Position(i) = mSurface->GetPosition(tcoord[0], tcoord[1]);
  96. }
  97. }
  98. }
  99. void InitializeNormals()
  100. {
  101. for (uint32_t r = 0, i = 0; r < this->mDescription.numRows; ++r)
  102. {
  103. for (uint32_t c = 0; c < this->mDescription.numCols; ++c, ++i)
  104. {
  105. Vector2<Real> tcoord = this->TCoord(i);
  106. Vector3<Real> values[6];
  107. mSurface->Evaluate(tcoord[0], tcoord[1], 1, values);
  108. Normalize(values[1], true);
  109. Normalize(values[2], true);
  110. this->Normal(i) = UnitCross(values[1], values[2], true);
  111. }
  112. }
  113. }
  114. void InitializeFrame()
  115. {
  116. for (unsigned int r = 0, i = 0; r < this->mDescription.numRows; ++r)
  117. {
  118. for (unsigned int c = 0; c < this->mDescription.numCols; ++c, ++i)
  119. {
  120. Vector2<Real> tcoord = this->TCoord(i);
  121. Vector3<Real> values[6];
  122. mSurface->Evaluate(tcoord[0], tcoord[1], 1, values);
  123. Normalize(values[1], true);
  124. Normalize(values[2], true);
  125. if (this->mDPDUs)
  126. {
  127. this->DPDU(i) = values[1];
  128. }
  129. if (this->mDPDVs)
  130. {
  131. this->DPDV(i) = values[2];
  132. }
  133. ComputeOrthogonalComplement(2, &values[1], true);
  134. if (this->mNormals)
  135. {
  136. this->Normal(i) = values[3];
  137. }
  138. if (this->mTangents)
  139. {
  140. this->Tangent(i) = values[1];
  141. }
  142. if (this->mBitangents)
  143. {
  144. this->Bitangent(i) = values[2];
  145. }
  146. }
  147. }
  148. }
  149. virtual void UpdatePositions() override
  150. {
  151. if (mSurface)
  152. {
  153. InitializePositions();
  154. }
  155. }
  156. virtual void UpdateNormals() override
  157. {
  158. if (mSurface)
  159. {
  160. InitializeNormals();
  161. }
  162. }
  163. virtual void UpdateFrame() override
  164. {
  165. if (mSurface)
  166. {
  167. InitializeFrame();
  168. }
  169. }
  170. std::shared_ptr<ParametricSurface<3, Real>> mSurface;
  171. // If the client does not request texture coordinates, they will be
  172. // computed internally for use in evaluation of the surface geometry.
  173. std::vector<Vector2<Real>> mDefaultTCoords;
  174. };
  175. }