RevolutionMesh.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/ParametricCurve.h>
  10. #include <memory>
  11. namespace WwiseGTE
  12. {
  13. template <typename Real>
  14. class RevolutionMesh : public Mesh<Real>
  15. {
  16. public:
  17. // The axis of revolution is the z-axis. The curve of revolution is
  18. // p(t) = (x(t),z(t)), where t in [tmin,tmax], x(t) > 0 for t in
  19. // (tmin,tmax), x(tmin) >= 0, and x(tmax) >= 0. The values tmin and
  20. // tmax are those for the curve object passed to the constructor. The
  21. // curve must be non-self-intersecting, except possibly at its
  22. // endpoints. The curve is closed when p(tmin) = p(tmax), in which
  23. // case the surface of revolution has torus topology. The curve is
  24. // open when p(tmin) != p(tmax). For an open curve, define
  25. // x0 = x(tmin) and x1 = x(tmax). The surface has cylinder topology
  26. // when x0 > 0 and x1 > 0, disk topology when exactly one of x0 or x1
  27. // is zero, or sphere topology when x0 and x1 are both zero. However,
  28. // to simplify the design, the mesh is always built using cylinder
  29. // topology. The row samples correspond to curve points and the
  30. // column samples correspond to the points on the circles of
  31. // revolution.
  32. RevolutionMesh(MeshDescription const& description,
  33. std::shared_ptr<ParametricCurve<2, Real>> const& curve,
  34. bool sampleByArcLength = false)
  35. :
  36. Mesh<Real>(description,
  37. { MeshTopology::CYLINDER, MeshTopology::TORUS, MeshTopology::DISK, MeshTopology::SPHERE }),
  38. mCurve(curve),
  39. mSampleByArcLength(sampleByArcLength)
  40. {
  41. if (!this->mDescription.constructed)
  42. {
  43. // The logger system will report these errors in the Mesh
  44. // constructor.
  45. mCurve = nullptr;
  46. return;
  47. }
  48. LogAssert(mCurve != nullptr, "A nonnull revolution curve is required.");
  49. // The four supported topologies all wrap around in the column
  50. // direction.
  51. mCosAngle.resize(this->mDescription.numCols + 1);
  52. mSinAngle.resize(this->mDescription.numCols + 1);
  53. Real invRadialSamples = (Real)1 / (Real)this->mDescription.numCols;
  54. for (unsigned int c = 0; c < this->mDescription.numCols; ++c)
  55. {
  56. Real angle = c * invRadialSamples * (Real)GTE_C_TWO_PI;
  57. mCosAngle[c] = std::cos(angle);
  58. mSinAngle[c] = std::sin(angle);
  59. }
  60. mCosAngle[this->mDescription.numCols] = mCosAngle[0];
  61. mSinAngle[this->mDescription.numCols] = mSinAngle[0];
  62. CreateSampler();
  63. if (!this->mTCoords)
  64. {
  65. mDefaultTCoords.resize(this->mDescription.numVertices);
  66. this->mTCoords = mDefaultTCoords.data();
  67. this->mTCoordStride = sizeof(Vector2<Real>);
  68. this->mDescription.allowUpdateFrame = this->mDescription.wantDynamicTangentSpaceUpdate;
  69. if (this->mDescription.allowUpdateFrame)
  70. {
  71. if (!this->mDescription.hasTangentSpaceVectors)
  72. {
  73. this->mDescription.allowUpdateFrame = false;
  74. }
  75. if (!this->mNormals)
  76. {
  77. this->mDescription.allowUpdateFrame = false;
  78. }
  79. }
  80. }
  81. this->ComputeIndices();
  82. InitializeTCoords();
  83. UpdatePositions();
  84. if (this->mDescription.allowUpdateFrame)
  85. {
  86. this->UpdateFrame();
  87. }
  88. else if (this->mNormals)
  89. {
  90. this->UpdateNormals();
  91. }
  92. }
  93. // Member access.
  94. inline std::shared_ptr<ParametricCurve<2, Real>> const& GetCurve() const
  95. {
  96. return mCurve;
  97. }
  98. inline bool IsSampleByArcLength() const
  99. {
  100. return mSampleByArcLength;
  101. }
  102. private:
  103. void CreateSampler()
  104. {
  105. if (this->mDescription.topology == MeshTopology::CYLINDER
  106. || this->mDescription.topology == MeshTopology::TORUS)
  107. {
  108. mSamples.resize(this->mDescription.rMax + 1);
  109. }
  110. else if (this->mDescription.topology == MeshTopology::DISK)
  111. {
  112. mSamples.resize(this->mDescription.rMax + 2);
  113. }
  114. else if (this->mDescription.topology == MeshTopology::SPHERE)
  115. {
  116. mSamples.resize(this->mDescription.rMax + 3);
  117. }
  118. Real invDenom = ((Real)1) / (Real)(mSamples.size() - 1);
  119. if (mSampleByArcLength)
  120. {
  121. Real factor = mCurve->GetTotalLength() * invDenom;
  122. mTSampler = [this, factor](unsigned int i)
  123. {
  124. return mCurve->GetTime(i * factor);
  125. };
  126. }
  127. else
  128. {
  129. Real factor = (mCurve->GetTMax() - mCurve->GetTMin()) * invDenom;
  130. mTSampler = [this, factor](unsigned int i)
  131. {
  132. return mCurve->GetTMin() + i * factor;
  133. };
  134. }
  135. }
  136. void InitializeTCoords()
  137. {
  138. Vector2<Real>tcoord;
  139. switch (this->mDescription.topology)
  140. {
  141. case MeshTopology::CYLINDER:
  142. {
  143. for (unsigned int r = 0, i = 0; r < this->mDescription.numRows; ++r)
  144. {
  145. tcoord[1] = (Real)r / (Real)(this->mDescription.numRows - 1);
  146. for (unsigned int c = 0; c <= this->mDescription.numCols; ++c, ++i)
  147. {
  148. tcoord[0] = (Real)c / (Real)this->mDescription.numCols;
  149. this->TCoord(i) = tcoord;
  150. }
  151. }
  152. break;
  153. }
  154. case MeshTopology::TORUS:
  155. {
  156. for (unsigned int r = 0, i = 0; r <= this->mDescription.numRows; ++r)
  157. {
  158. tcoord[1] = (Real)r / (Real)this->mDescription.numRows;
  159. for (unsigned int c = 0; c <= this->mDescription.numCols; ++c, ++i)
  160. {
  161. tcoord[0] = (Real)c / (Real)this->mDescription.numCols;
  162. this->TCoord(i) = tcoord;
  163. }
  164. }
  165. break;
  166. }
  167. case MeshTopology::DISK:
  168. {
  169. Vector2<Real> origin{ (Real)0.5, (Real)0.5 };
  170. unsigned int i = 0;
  171. for (unsigned int r = 0; r < this->mDescription.numRows; ++r)
  172. {
  173. Real radius = (Real)(r + 1) / (Real)(2 * this->mDescription.numRows);
  174. radius = std::min(radius, (Real)0.5);
  175. for (unsigned int c = 0; c <= this->mDescription.numCols; ++c, ++i)
  176. {
  177. Real angle = (Real)GTE_C_TWO_PI * (Real)c / (Real)this->mDescription.numCols;
  178. this->TCoord(i) = { radius * std::cos(angle), radius * std::sin(angle) };
  179. }
  180. }
  181. this->TCoord(i) = origin;
  182. break;
  183. }
  184. case MeshTopology::SPHERE:
  185. {
  186. unsigned int i = 0;
  187. for (unsigned int r = 0; r < this->mDescription.numRows; ++r)
  188. {
  189. tcoord[1] = (Real)r / (Real)(this->mDescription.numRows - 1);
  190. for (unsigned int c = 0; c <= this->mDescription.numCols; ++c, ++i)
  191. {
  192. tcoord[0] = (Real)c / (Real)this->mDescription.numCols;
  193. this->TCoord(i) = tcoord;
  194. }
  195. }
  196. this->TCoord(i++) = { (Real)0.5, (Real)0 };
  197. this->TCoord(i) = { (Real)0.5, (Real)1 };
  198. break;
  199. }
  200. default:
  201. // Invalid topology is reported by the Mesh constructor, so there is
  202. // no need to log a message here.
  203. break;
  204. }
  205. }
  206. virtual void UpdatePositions() override
  207. {
  208. unsigned int const numSamples = static_cast<unsigned int>(mSamples.size());
  209. for (unsigned int i = 0; i < numSamples; ++i)
  210. {
  211. Real t = mTSampler(i);
  212. Vector2<Real> position = mCurve->GetPosition(t);
  213. mSamples[i][0] = position[0];
  214. mSamples[i][1] = (Real)0;
  215. mSamples[i][2] = position[1];
  216. }
  217. switch (this->mDescription.topology)
  218. {
  219. case MeshTopology::CYLINDER:
  220. UpdateCylinderPositions();
  221. break;
  222. case MeshTopology::TORUS:
  223. UpdateTorusPositions();
  224. break;
  225. case MeshTopology::DISK:
  226. UpdateDiskPositions();
  227. break;
  228. case MeshTopology::SPHERE:
  229. UpdateSpherePositions();
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. void UpdateCylinderPositions()
  236. {
  237. for (unsigned int r = 0, i = 0; r <= this->mDescription.rMax; ++r)
  238. {
  239. Real radius = mSamples[r][0];
  240. for (unsigned int c = 0; c <= this->mDescription.cMax; ++c, ++i)
  241. {
  242. this->Position(i) = { radius * mCosAngle[c], radius * mSinAngle[c], mSamples[r][2] };
  243. }
  244. }
  245. }
  246. void UpdateTorusPositions()
  247. {
  248. for (unsigned int r = 0, i = 0; r <= this->mDescription.rMax; ++r)
  249. {
  250. Real radius = mSamples[r][0];
  251. for (unsigned int c = 0; c <= this->mDescription.cMax; ++c, ++i)
  252. {
  253. this->Position(i) = { radius * mCosAngle[c], radius * mSinAngle[c], mSamples[r][2] };
  254. }
  255. }
  256. }
  257. void UpdateDiskPositions()
  258. {
  259. for (unsigned int r = 0, rp1 = 1, i = 0; r <= this->mDescription.rMax; ++r, ++rp1)
  260. {
  261. Real radius = mSamples[rp1][0];
  262. for (unsigned int c = 0; c <= this->mDescription.cMax; ++c, ++i)
  263. {
  264. this->Position(i) = { radius * mCosAngle[c], radius * mSinAngle[c], mSamples[rp1][2] };
  265. }
  266. }
  267. this->Position(this->mDescription.numVertices - 1) = { (Real)0, (Real)0, mSamples.front()[2] };
  268. }
  269. void UpdateSpherePositions()
  270. {
  271. for (unsigned int r = 0, rp1 = 1, i = 0; r <= this->mDescription.rMax; ++r, ++rp1)
  272. {
  273. Real radius = mSamples[rp1][0];
  274. for (unsigned int c = 0; c <= this->mDescription.cMax; ++c, ++i)
  275. {
  276. this->Position(i) = { radius * mCosAngle[c], radius * mSinAngle[c], mSamples[rp1][2] };
  277. }
  278. }
  279. this->Position(this->mDescription.numVertices - 2) = { (Real)0, (Real)0, mSamples.front()[2] };
  280. this->Position(this->mDescription.numVertices - 1) = { (Real)0, (Real)0, mSamples.back()[2] };
  281. }
  282. std::shared_ptr<ParametricCurve<2, Real>> mCurve;
  283. bool mSampleByArcLength;
  284. std::vector<Real> mCosAngle, mSinAngle;
  285. std::function<Real(unsigned int)> mTSampler;
  286. std::vector<Vector3<Real>> mSamples;
  287. // If the client does not request texture coordinates, they will be
  288. // computed internally for use in evaluation of the surface geometry.
  289. std::vector<Vector2<Real>> mDefaultTCoords;
  290. };
  291. }