StandardShapes.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file StandardShapes.cpp
  34. * @brief Implementation of the StandardShapes class
  35. *
  36. * The primitive geometry data comes from
  37. * http://geometrictools.com/Documentation/PlatonicSolids.pdf.
  38. */
  39. #include <assimp/StandardShapes.h>
  40. #include <assimp/Defines.h>
  41. #include <assimp/StringComparison.h>
  42. #include <assimp/mesh.h>
  43. namespace Assimp {
  44. #define ADD_TRIANGLE(n0, n1, n2) \
  45. positions.push_back(n0); \
  46. positions.push_back(n1); \
  47. positions.push_back(n2);
  48. #define ADD_PENTAGON(n0, n1, n2, n3, n4) \
  49. if (polygons) { \
  50. positions.push_back(n0); \
  51. positions.push_back(n1); \
  52. positions.push_back(n2); \
  53. positions.push_back(n3); \
  54. positions.push_back(n4); \
  55. } else { \
  56. ADD_TRIANGLE(n0, n1, n2) \
  57. ADD_TRIANGLE(n0, n2, n3) \
  58. ADD_TRIANGLE(n0, n3, n4) \
  59. }
  60. #define ADD_QUAD(n0, n1, n2, n3) \
  61. if (polygons) { \
  62. positions.push_back(n0); \
  63. positions.push_back(n1); \
  64. positions.push_back(n2); \
  65. positions.push_back(n3); \
  66. } else { \
  67. ADD_TRIANGLE(n0, n1, n2) \
  68. ADD_TRIANGLE(n0, n2, n3) \
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Fast subdivision for a mesh whose verts have a magnitude of 1
  72. void Subdivide(std::vector<aiVector3D> &positions) {
  73. // assume this to be constant - (fixme: must be 1.0? I think so)
  74. const ai_real fl1 = positions[0].Length();
  75. unsigned int origSize = (unsigned int)positions.size();
  76. for (unsigned int i = 0; i < origSize; i += 3) {
  77. aiVector3D &tv0 = positions[i];
  78. aiVector3D &tv1 = positions[i + 1];
  79. aiVector3D &tv2 = positions[i + 2];
  80. aiVector3D a = tv0, b = tv1, c = tv2;
  81. aiVector3D v1 = aiVector3D(a.x + b.x, a.y + b.y, a.z + b.z).Normalize() * fl1;
  82. aiVector3D v2 = aiVector3D(a.x + c.x, a.y + c.y, a.z + c.z).Normalize() * fl1;
  83. aiVector3D v3 = aiVector3D(b.x + c.x, b.y + c.y, b.z + c.z).Normalize() * fl1;
  84. tv0 = v1;
  85. tv1 = v3;
  86. tv2 = v2; // overwrite the original
  87. ADD_TRIANGLE(v1, v2, a);
  88. ADD_TRIANGLE(v2, v3, c);
  89. ADD_TRIANGLE(v3, v1, b);
  90. }
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. // Construct a mesh from given vertex positions
  94. aiMesh *StandardShapes::MakeMesh(const std::vector<aiVector3D> &positions,
  95. unsigned int numIndices) {
  96. if (positions.empty() || !numIndices) {
  97. return nullptr;
  98. }
  99. // Determine which kinds of primitives the mesh consists of
  100. aiMesh *out = new aiMesh();
  101. switch (numIndices) {
  102. case 1:
  103. out->mPrimitiveTypes = aiPrimitiveType_POINT;
  104. break;
  105. case 2:
  106. out->mPrimitiveTypes = aiPrimitiveType_LINE;
  107. break;
  108. case 3:
  109. out->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  110. break;
  111. default:
  112. out->mPrimitiveTypes = aiPrimitiveType_POLYGON;
  113. break;
  114. };
  115. out->mNumFaces = (unsigned int)positions.size() / numIndices;
  116. out->mFaces = new aiFace[out->mNumFaces];
  117. for (unsigned int i = 0, a = 0; i < out->mNumFaces; ++i) {
  118. aiFace &f = out->mFaces[i];
  119. f.mNumIndices = numIndices;
  120. f.mIndices = new unsigned int[numIndices];
  121. for (unsigned int j = 0; j < numIndices; ++j, ++a) {
  122. f.mIndices[j] = a;
  123. }
  124. }
  125. out->mNumVertices = (unsigned int)positions.size();
  126. out->mVertices = new aiVector3D[out->mNumVertices];
  127. ::memcpy(out->mVertices, &positions[0], out->mNumVertices * sizeof(aiVector3D));
  128. return out;
  129. }
  130. // ------------------------------------------------------------------------------------------------
  131. // Construct a mesh with a specific shape (callback)
  132. aiMesh *StandardShapes::MakeMesh(unsigned int (*GenerateFunc)(
  133. std::vector<aiVector3D> &)) {
  134. std::vector<aiVector3D> temp;
  135. unsigned num = (*GenerateFunc)(temp);
  136. return MakeMesh(temp, num);
  137. }
  138. // ------------------------------------------------------------------------------------------------
  139. // Construct a mesh with a specific shape (callback)
  140. aiMesh *StandardShapes::MakeMesh(unsigned int (*GenerateFunc)(
  141. std::vector<aiVector3D> &, bool)) {
  142. std::vector<aiVector3D> temp;
  143. unsigned num = (*GenerateFunc)(temp, true);
  144. return MakeMesh(temp, num);
  145. }
  146. // ------------------------------------------------------------------------------------------------
  147. // Construct a mesh with a specific shape (callback)
  148. aiMesh *StandardShapes::MakeMesh(unsigned int num, void (*GenerateFunc)(
  149. unsigned int, std::vector<aiVector3D> &)) {
  150. std::vector<aiVector3D> temp;
  151. (*GenerateFunc)(num, temp);
  152. return MakeMesh(temp, 3);
  153. }
  154. // ------------------------------------------------------------------------------------------------
  155. // Build an incosahedron with points.magnitude == 1
  156. unsigned int StandardShapes::MakeIcosahedron(std::vector<aiVector3D> &positions) {
  157. positions.reserve(positions.size() + 60);
  158. const ai_real t = (ai_real(1.0) + ai_real(2.236067977)) / ai_real(2.0);
  159. const ai_real s = std::sqrt(ai_real(1.0) + t * t);
  160. const aiVector3D v0 = aiVector3D(t, 1.0, 0.0) / s;
  161. const aiVector3D v1 = aiVector3D(-t, 1.0, 0.0) / s;
  162. const aiVector3D v2 = aiVector3D(t, -1.0, 0.0) / s;
  163. const aiVector3D v3 = aiVector3D(-t, -1.0, 0.0) / s;
  164. const aiVector3D v4 = aiVector3D(1.0, 0.0, t) / s;
  165. const aiVector3D v5 = aiVector3D(1.0, 0.0, -t) / s;
  166. const aiVector3D v6 = aiVector3D(-1.0, 0.0, t) / s;
  167. const aiVector3D v7 = aiVector3D(-1.0, 0.0, -t) / s;
  168. const aiVector3D v8 = aiVector3D(0.0, t, 1.0) / s;
  169. const aiVector3D v9 = aiVector3D(0.0, -t, 1.0) / s;
  170. const aiVector3D v10 = aiVector3D(0.0, t, -1.0) / s;
  171. const aiVector3D v11 = aiVector3D(0.0, -t, -1.0) / s;
  172. ADD_TRIANGLE(v0, v8, v4);
  173. ADD_TRIANGLE(v0, v5, v10);
  174. ADD_TRIANGLE(v2, v4, v9);
  175. ADD_TRIANGLE(v2, v11, v5);
  176. ADD_TRIANGLE(v1, v6, v8);
  177. ADD_TRIANGLE(v1, v10, v7);
  178. ADD_TRIANGLE(v3, v9, v6);
  179. ADD_TRIANGLE(v3, v7, v11);
  180. ADD_TRIANGLE(v0, v10, v8);
  181. ADD_TRIANGLE(v1, v8, v10);
  182. ADD_TRIANGLE(v2, v9, v11);
  183. ADD_TRIANGLE(v3, v11, v9);
  184. ADD_TRIANGLE(v4, v2, v0);
  185. ADD_TRIANGLE(v5, v0, v2);
  186. ADD_TRIANGLE(v6, v1, v3);
  187. ADD_TRIANGLE(v7, v3, v1);
  188. ADD_TRIANGLE(v8, v6, v4);
  189. ADD_TRIANGLE(v9, v4, v6);
  190. ADD_TRIANGLE(v10, v5, v7);
  191. ADD_TRIANGLE(v11, v7, v5);
  192. return 3;
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. // Build a dodecahedron with points.magnitude == 1
  196. unsigned int StandardShapes::MakeDodecahedron(std::vector<aiVector3D> &positions,
  197. bool polygons /*= false*/) {
  198. positions.reserve(positions.size() + 108);
  199. const ai_real a = ai_real(1.0) / ai_real(1.7320508);
  200. const ai_real b = std::sqrt((ai_real(3.0) - ai_real(2.23606797)) / ai_real(6.0));
  201. const ai_real c = std::sqrt((ai_real(3.0) + ai_real(2.23606797f)) / ai_real(6.0));
  202. const aiVector3D v0 = aiVector3D(a, a, a);
  203. const aiVector3D v1 = aiVector3D(a, a, -a);
  204. const aiVector3D v2 = aiVector3D(a, -a, a);
  205. const aiVector3D v3 = aiVector3D(a, -a, -a);
  206. const aiVector3D v4 = aiVector3D(-a, a, a);
  207. const aiVector3D v5 = aiVector3D(-a, a, -a);
  208. const aiVector3D v6 = aiVector3D(-a, -a, a);
  209. const aiVector3D v7 = aiVector3D(-a, -a, -a);
  210. const aiVector3D v8 = aiVector3D(b, c, 0.0);
  211. const aiVector3D v9 = aiVector3D(-b, c, 0.0);
  212. const aiVector3D v10 = aiVector3D(b, -c, 0.0);
  213. const aiVector3D v11 = aiVector3D(-b, -c, 0.0);
  214. const aiVector3D v12 = aiVector3D(c, 0.0, b);
  215. const aiVector3D v13 = aiVector3D(c, 0.0, -b);
  216. const aiVector3D v14 = aiVector3D(-c, 0.0, b);
  217. const aiVector3D v15 = aiVector3D(-c, 0.0, -b);
  218. const aiVector3D v16 = aiVector3D(0.0, b, c);
  219. const aiVector3D v17 = aiVector3D(0.0, -b, c);
  220. const aiVector3D v18 = aiVector3D(0.0, b, -c);
  221. const aiVector3D v19 = aiVector3D(0.0, -b, -c);
  222. ADD_PENTAGON(v0, v8, v9, v4, v16);
  223. ADD_PENTAGON(v0, v12, v13, v1, v8);
  224. ADD_PENTAGON(v0, v16, v17, v2, v12);
  225. ADD_PENTAGON(v8, v1, v18, v5, v9);
  226. ADD_PENTAGON(v12, v2, v10, v3, v13);
  227. ADD_PENTAGON(v16, v4, v14, v6, v17);
  228. ADD_PENTAGON(v9, v5, v15, v14, v4);
  229. ADD_PENTAGON(v6, v11, v10, v2, v17);
  230. ADD_PENTAGON(v3, v19, v18, v1, v13);
  231. ADD_PENTAGON(v7, v15, v5, v18, v19);
  232. ADD_PENTAGON(v7, v11, v6, v14, v15);
  233. ADD_PENTAGON(v7, v19, v3, v10, v11);
  234. return (polygons ? 5 : 3);
  235. }
  236. // ------------------------------------------------------------------------------------------------
  237. // Build an octahedron with points.magnitude == 1
  238. unsigned int StandardShapes::MakeOctahedron(std::vector<aiVector3D> &positions) {
  239. positions.reserve(positions.size() + 24);
  240. const aiVector3D v0 = aiVector3D(1.0, 0.0, 0.0);
  241. const aiVector3D v1 = aiVector3D(-1.0, 0.0, 0.0);
  242. const aiVector3D v2 = aiVector3D(0.0, 1.0, 0.0);
  243. const aiVector3D v3 = aiVector3D(0.0, -1.0, 0.0);
  244. const aiVector3D v4 = aiVector3D(0.0, 0.0, 1.0);
  245. const aiVector3D v5 = aiVector3D(0.0, 0.0, -1.0);
  246. ADD_TRIANGLE(v4, v0, v2);
  247. ADD_TRIANGLE(v4, v2, v1);
  248. ADD_TRIANGLE(v4, v1, v3);
  249. ADD_TRIANGLE(v4, v3, v0);
  250. ADD_TRIANGLE(v5, v2, v0);
  251. ADD_TRIANGLE(v5, v1, v2);
  252. ADD_TRIANGLE(v5, v3, v1);
  253. ADD_TRIANGLE(v5, v0, v3);
  254. return 3;
  255. }
  256. // ------------------------------------------------------------------------------------------------
  257. // Build a tetrahedron with points.magnitude == 1
  258. unsigned int StandardShapes::MakeTetrahedron(std::vector<aiVector3D> &positions) {
  259. positions.reserve(positions.size() + 9);
  260. const ai_real invThree = ai_real(1.0) / ai_real(3.0);
  261. const ai_real a = ai_real(1.41421) * invThree;
  262. const ai_real b = ai_real(2.4494) * invThree;
  263. const aiVector3D v0 = aiVector3D(0.0, 0.0, 1.0);
  264. const aiVector3D v1 = aiVector3D(2 * a, 0, -invThree);
  265. const aiVector3D v2 = aiVector3D(-a, b, -invThree);
  266. const aiVector3D v3 = aiVector3D(-a, -b, -invThree);
  267. ADD_TRIANGLE(v0, v1, v2);
  268. ADD_TRIANGLE(v0, v2, v3);
  269. ADD_TRIANGLE(v0, v3, v1);
  270. ADD_TRIANGLE(v1, v3, v2);
  271. return 3;
  272. }
  273. // ------------------------------------------------------------------------------------------------
  274. // Build a hexahedron with points.magnitude == 1
  275. unsigned int StandardShapes::MakeHexahedron(std::vector<aiVector3D> &positions,
  276. bool polygons /*= false*/) {
  277. positions.reserve(positions.size() + 36);
  278. const ai_real length = ai_real(1.0) / ai_real(1.73205080);
  279. const aiVector3D v0 = aiVector3D(-1.0, -1.0, -1.0) * length;
  280. const aiVector3D v1 = aiVector3D(1.0, -1.0, -1.0) * length;
  281. const aiVector3D v2 = aiVector3D(1.0, 1.0, -1.0) * length;
  282. const aiVector3D v3 = aiVector3D(-1.0, 1.0, -1.0) * length;
  283. const aiVector3D v4 = aiVector3D(-1.0, -1.0, 1.0) * length;
  284. const aiVector3D v5 = aiVector3D(1.0, -1.0, 1.0) * length;
  285. const aiVector3D v6 = aiVector3D(1.0, 1.0, 1.0) * length;
  286. const aiVector3D v7 = aiVector3D(-1.0, 1.0, 1.0) * length;
  287. ADD_QUAD(v0, v3, v2, v1);
  288. ADD_QUAD(v0, v1, v5, v4);
  289. ADD_QUAD(v0, v4, v7, v3);
  290. ADD_QUAD(v6, v5, v1, v2);
  291. ADD_QUAD(v6, v2, v3, v7);
  292. ADD_QUAD(v6, v7, v4, v5);
  293. return (polygons ? 4 : 3);
  294. }
  295. // Cleanup ...
  296. #undef ADD_TRIANGLE
  297. #undef ADD_QUAD
  298. #undef ADD_PENTAGON
  299. // ------------------------------------------------------------------------------------------------
  300. // Create a subdivision sphere
  301. void StandardShapes::MakeSphere(unsigned int tess,
  302. std::vector<aiVector3D> &positions) {
  303. // Reserve enough storage. Every subdivision
  304. // splits each triangle in 4, the icosahedron consists of 60 verts
  305. positions.reserve(positions.size() + 60 * integer_pow(4, tess));
  306. // Construct an icosahedron to start with
  307. MakeIcosahedron(positions);
  308. // ... and subdivide it until the requested output
  309. // tessellation is reached
  310. for (unsigned int i = 0; i < tess; ++i)
  311. Subdivide(positions);
  312. }
  313. // ------------------------------------------------------------------------------------------------
  314. // Build a cone
  315. void StandardShapes::MakeCone(ai_real height, ai_real radius1,
  316. ai_real radius2, unsigned int tess,
  317. std::vector<aiVector3D> &positions, bool bOpen /*= false */) {
  318. // Sorry, a cone with less than 3 segments makes ABSOLUTELY NO SENSE
  319. if (tess < 3 || !height)
  320. return;
  321. size_t old = positions.size();
  322. // No negative radii
  323. radius1 = std::fabs(radius1);
  324. radius2 = std::fabs(radius2);
  325. ai_real halfHeight = height / ai_real(2.0);
  326. // radius1 is always the smaller one
  327. if (radius2 > radius1) {
  328. std::swap(radius2, radius1);
  329. halfHeight = -halfHeight;
  330. } else
  331. old = SIZE_MAX;
  332. // Use a large epsilon to check whether the cone is pointy
  333. if (radius1 < (radius2 - radius1) * 10e-3) radius1 = 0.0;
  334. // We will need 3*2 verts per segment + 3*2 verts per segment
  335. // if the cone is closed
  336. const unsigned int mem = tess * 6 + (!bOpen ? tess * 3 * (radius1 ? 2 : 1) : 0);
  337. positions.reserve(positions.size() + mem);
  338. // Now construct all segments
  339. const ai_real angle_delta = (ai_real)AI_MATH_TWO_PI / tess;
  340. const ai_real angle_max = (ai_real)AI_MATH_TWO_PI;
  341. ai_real s = 1.0; // std::cos(angle == 0);
  342. ai_real t = 0.0; // std::sin(angle == 0);
  343. for (ai_real angle = 0.0; angle < angle_max;) {
  344. const aiVector3D v1 = aiVector3D(s * radius1, -halfHeight, t * radius1);
  345. const aiVector3D v2 = aiVector3D(s * radius2, halfHeight, t * radius2);
  346. const ai_real next = angle + angle_delta;
  347. ai_real s2 = std::cos(next);
  348. ai_real t2 = std::sin(next);
  349. const aiVector3D v3 = aiVector3D(s2 * radius2, halfHeight, t2 * radius2);
  350. const aiVector3D v4 = aiVector3D(s2 * radius1, -halfHeight, t2 * radius1);
  351. positions.push_back(v1);
  352. positions.push_back(v2);
  353. positions.push_back(v3);
  354. positions.push_back(v4);
  355. positions.push_back(v1);
  356. positions.push_back(v3);
  357. if (!bOpen) {
  358. // generate the end 'cap'
  359. positions.push_back(aiVector3D(s * radius2, halfHeight, t * radius2));
  360. positions.push_back(aiVector3D(s2 * radius2, halfHeight, t2 * radius2));
  361. positions.push_back(aiVector3D(0.0, halfHeight, 0.0));
  362. if (radius1) {
  363. // generate the other end 'cap'
  364. positions.push_back(aiVector3D(s * radius1, -halfHeight, t * radius1));
  365. positions.push_back(aiVector3D(s2 * radius1, -halfHeight, t2 * radius1));
  366. positions.push_back(aiVector3D(0.0, -halfHeight, 0.0));
  367. }
  368. }
  369. s = s2;
  370. t = t2;
  371. angle = next;
  372. }
  373. // Need to flip face order?
  374. if (SIZE_MAX != old) {
  375. for (size_t p = old; p < positions.size(); p += 3) {
  376. std::swap(positions[p], positions[p + 1]);
  377. }
  378. }
  379. }
  380. // ------------------------------------------------------------------------------------------------
  381. // Build a circle
  382. void StandardShapes::MakeCircle(ai_real radius, unsigned int tess,
  383. std::vector<aiVector3D> &positions) {
  384. // Sorry, a circle with less than 3 segments makes ABSOLUTELY NO SENSE
  385. if (tess < 3 || !radius)
  386. return;
  387. radius = std::fabs(radius);
  388. // We will need 3 vertices per segment
  389. positions.reserve(positions.size() + tess * 3);
  390. const ai_real angle_delta = (ai_real)AI_MATH_TWO_PI / tess;
  391. const ai_real angle_max = (ai_real)AI_MATH_TWO_PI;
  392. ai_real s = 1.0; // std::cos(angle == 0);
  393. ai_real t = 0.0; // std::sin(angle == 0);
  394. for (ai_real angle = 0.0; angle < angle_max;) {
  395. positions.push_back(aiVector3D(s * radius, 0.0, t * radius));
  396. angle += angle_delta;
  397. s = std::cos(angle);
  398. t = std::sin(angle);
  399. positions.push_back(aiVector3D(s * radius, 0.0, t * radius));
  400. positions.push_back(aiVector3D(0.0, 0.0, 0.0));
  401. }
  402. }
  403. } // namespace Assimp