StandardShapes.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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 "StandardShapes.h"
  40. #include "StringComparison.h"
  41. #include "Defines.h"
  42. #include <stddef.h>
  43. #include <assimp/mesh.h>
  44. namespace Assimp {
  45. # define ADD_TRIANGLE(n0,n1,n2) \
  46. positions.push_back(n0); \
  47. positions.push_back(n1); \
  48. positions.push_back(n2);
  49. # define ADD_PENTAGON(n0,n1,n2,n3,n4) \
  50. if (polygons) \
  51. { \
  52. positions.push_back(n0); \
  53. positions.push_back(n1); \
  54. positions.push_back(n2); \
  55. positions.push_back(n3); \
  56. positions.push_back(n4); \
  57. } \
  58. else \
  59. { \
  60. ADD_TRIANGLE(n0, n1, n2) \
  61. ADD_TRIANGLE(n0, n2, n3) \
  62. ADD_TRIANGLE(n0, n3, n4) \
  63. }
  64. # define ADD_QUAD(n0,n1,n2,n3) \
  65. if (polygons) \
  66. { \
  67. positions.push_back(n0); \
  68. positions.push_back(n1); \
  69. positions.push_back(n2); \
  70. positions.push_back(n3); \
  71. } \
  72. else \
  73. { \
  74. ADD_TRIANGLE(n0, n1, n2) \
  75. ADD_TRIANGLE(n0, n2, n3) \
  76. }
  77. // ------------------------------------------------------------------------------------------------
  78. // Fast subdivision for a mesh whose verts have a magnitude of 1
  79. void Subdivide(std::vector<aiVector3D>& positions)
  80. {
  81. // assume this to be constant - (fixme: must be 1.0? I think so)
  82. const ai_real fl1 = positions[0].Length();
  83. unsigned int origSize = (unsigned int)positions.size();
  84. for (unsigned int i = 0 ; i < origSize ; i+=3)
  85. {
  86. aiVector3D& tv0 = positions[i];
  87. aiVector3D& tv1 = positions[i+1];
  88. aiVector3D& tv2 = positions[i+2];
  89. aiVector3D a = tv0, b = tv1, c = tv2;
  90. aiVector3D v1 = aiVector3D(a.x+b.x, a.y+b.y, a.z+b.z).Normalize()*fl1;
  91. aiVector3D v2 = aiVector3D(a.x+c.x, a.y+c.y, a.z+c.z).Normalize()*fl1;
  92. aiVector3D v3 = aiVector3D(b.x+c.x, b.y+c.y, b.z+c.z).Normalize()*fl1;
  93. tv0 = v1; tv1 = v3; tv2 = v2; // overwrite the original
  94. ADD_TRIANGLE(v1, v2, a);
  95. ADD_TRIANGLE(v2, v3, c);
  96. ADD_TRIANGLE(v3, v1, b);
  97. }
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. // Construct a mesh from given vertex positions
  101. aiMesh* StandardShapes::MakeMesh(const std::vector<aiVector3D>& positions,
  102. unsigned int numIndices)
  103. {
  104. if (positions.empty() || !numIndices) return NULL;
  105. // Determine which kinds of primitives the mesh consists of
  106. aiMesh* out = new aiMesh();
  107. switch (numIndices)
  108. {
  109. case 1:
  110. out->mPrimitiveTypes = aiPrimitiveType_POINT;
  111. break;
  112. case 2:
  113. out->mPrimitiveTypes = aiPrimitiveType_LINE;
  114. break;
  115. case 3:
  116. out->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  117. break;
  118. default:
  119. out->mPrimitiveTypes = aiPrimitiveType_POLYGON;
  120. break;
  121. };
  122. out->mNumFaces = (unsigned int)positions.size() / numIndices;
  123. out->mFaces = new aiFace[out->mNumFaces];
  124. for (unsigned int i = 0, a = 0; i < out->mNumFaces;++i)
  125. {
  126. aiFace& f = out->mFaces[i];
  127. f.mNumIndices = numIndices;
  128. f.mIndices = new unsigned int[numIndices];
  129. for (unsigned int i = 0; i < numIndices;++i,++a)
  130. f.mIndices[i] = a;
  131. }
  132. out->mNumVertices = (unsigned int)positions.size();
  133. out->mVertices = new aiVector3D[out->mNumVertices];
  134. ::memcpy(out->mVertices,&positions[0],out->mNumVertices*sizeof(aiVector3D));
  135. return out;
  136. }
  137. // ------------------------------------------------------------------------------------------------
  138. // Construct a mesh with a specific shape (callback)
  139. aiMesh* StandardShapes::MakeMesh ( unsigned int (*GenerateFunc)(
  140. std::vector<aiVector3D>&))
  141. {
  142. std::vector<aiVector3D> temp;
  143. unsigned num = (*GenerateFunc)(temp);
  144. return MakeMesh(temp,num);
  145. }
  146. // ------------------------------------------------------------------------------------------------
  147. // Construct a mesh with a specific shape (callback)
  148. aiMesh* StandardShapes::MakeMesh ( unsigned int (*GenerateFunc)(
  149. std::vector<aiVector3D>&, bool))
  150. {
  151. std::vector<aiVector3D> temp;
  152. unsigned num = (*GenerateFunc)(temp,true);
  153. return MakeMesh(temp,num);
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. // Construct a mesh with a specific shape (callback)
  157. aiMesh* StandardShapes::MakeMesh (unsigned int num, void (*GenerateFunc)(
  158. unsigned int,std::vector<aiVector3D>&))
  159. {
  160. std::vector<aiVector3D> temp;
  161. (*GenerateFunc)(num,temp);
  162. return MakeMesh(temp,3);
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. // Build an incosahedron with points.magnitude == 1
  166. unsigned int StandardShapes::MakeIcosahedron(std::vector<aiVector3D>& positions)
  167. {
  168. positions.reserve(positions.size()+60);
  169. const ai_real t = ( ai_real( 1.0 )+ ai_real( 2.236067977 ) ) / ai_real( 2.0 );
  170. const ai_real s = std::sqrt(ai_real(1.0) + t*t);
  171. const aiVector3D v0 = aiVector3D(t,1.0, 0.0)/s;
  172. const aiVector3D v1 = aiVector3D(-t,1.0, 0.0)/s;
  173. const aiVector3D v2 = aiVector3D(t,-1.0, 0.0)/s;
  174. const aiVector3D v3 = aiVector3D(-t,-1.0, 0.0)/s;
  175. const aiVector3D v4 = aiVector3D(1.0, 0.0, t)/s;
  176. const aiVector3D v5 = aiVector3D(1.0, 0.0,-t)/s;
  177. const aiVector3D v6 = aiVector3D(-1.0, 0.0,t)/s;
  178. const aiVector3D v7 = aiVector3D(-1.0, 0.0,-t)/s;
  179. const aiVector3D v8 = aiVector3D(0.0, t, 1.0)/s;
  180. const aiVector3D v9 = aiVector3D(0.0,-t, 1.0)/s;
  181. const aiVector3D v10 = aiVector3D(0.0, t,-1.0)/s;
  182. const aiVector3D v11 = aiVector3D(0.0,-t,-1.0)/s;
  183. ADD_TRIANGLE(v0,v8,v4);
  184. ADD_TRIANGLE(v0,v5,v10);
  185. ADD_TRIANGLE(v2,v4,v9);
  186. ADD_TRIANGLE(v2,v11,v5);
  187. ADD_TRIANGLE(v1,v6,v8);
  188. ADD_TRIANGLE(v1,v10,v7);
  189. ADD_TRIANGLE(v3,v9,v6);
  190. ADD_TRIANGLE(v3,v7,v11);
  191. ADD_TRIANGLE(v0,v10,v8);
  192. ADD_TRIANGLE(v1,v8,v10);
  193. ADD_TRIANGLE(v2,v9,v11);
  194. ADD_TRIANGLE(v3,v11,v9);
  195. ADD_TRIANGLE(v4,v2,v0);
  196. ADD_TRIANGLE(v5,v0,v2);
  197. ADD_TRIANGLE(v6,v1,v3);
  198. ADD_TRIANGLE(v7,v3,v1);
  199. ADD_TRIANGLE(v8,v6,v4);
  200. ADD_TRIANGLE(v9,v4,v6);
  201. ADD_TRIANGLE(v10,v5,v7);
  202. ADD_TRIANGLE(v11,v7,v5);
  203. return 3;
  204. }
  205. // ------------------------------------------------------------------------------------------------
  206. // Build a dodecahedron with points.magnitude == 1
  207. unsigned int StandardShapes::MakeDodecahedron(std::vector<aiVector3D>& positions,
  208. bool polygons /*= false*/)
  209. {
  210. positions.reserve(positions.size()+108);
  211. const ai_real a = ai_real( 1.0 ) / ai_real(1.7320508);
  212. const ai_real b = std::sqrt(( ai_real( 3.0 )- ai_real( 2.23606797))/ ai_real( 6.0) );
  213. const ai_real c = std::sqrt(( ai_real( 3.0 )+ ai_real( 2.23606797f))/ ai_real( 6.0) );
  214. const aiVector3D v0 = aiVector3D(a,a,a);
  215. const aiVector3D v1 = aiVector3D(a,a,-a);
  216. const aiVector3D v2 = aiVector3D(a,-a,a);
  217. const aiVector3D v3 = aiVector3D(a,-a,-a);
  218. const aiVector3D v4 = aiVector3D(-a,a,a);
  219. const aiVector3D v5 = aiVector3D(-a,a,-a);
  220. const aiVector3D v6 = aiVector3D(-a,-a,a);
  221. const aiVector3D v7 = aiVector3D(-a,-a,-a);
  222. const aiVector3D v8 = aiVector3D(b,c,0.0);
  223. const aiVector3D v9 = aiVector3D(-b,c,0.0);
  224. const aiVector3D v10 = aiVector3D(b,-c,0.0);
  225. const aiVector3D v11 = aiVector3D(-b,-c,0.0);
  226. const aiVector3D v12 = aiVector3D(c, 0.0, b);
  227. const aiVector3D v13 = aiVector3D(c, 0.0, -b);
  228. const aiVector3D v14 = aiVector3D(-c, 0.0, b);
  229. const aiVector3D v15 = aiVector3D(-c, 0.0, -b);
  230. const aiVector3D v16 = aiVector3D(0.0, b, c);
  231. const aiVector3D v17 = aiVector3D(0.0, -b, c);
  232. const aiVector3D v18 = aiVector3D(0.0, b, -c);
  233. const aiVector3D v19 = aiVector3D(0.0, -b, -c);
  234. ADD_PENTAGON(v0, v8, v9, v4, v16);
  235. ADD_PENTAGON(v0, v12, v13, v1, v8);
  236. ADD_PENTAGON(v0, v16, v17, v2, v12);
  237. ADD_PENTAGON(v8, v1, v18, v5, v9);
  238. ADD_PENTAGON(v12, v2, v10, v3, v13);
  239. ADD_PENTAGON(v16, v4, v14, v6, v17);
  240. ADD_PENTAGON(v9, v5, v15, v14, v4);
  241. ADD_PENTAGON(v6, v11, v10, v2, v17);
  242. ADD_PENTAGON(v3, v19, v18, v1, v13);
  243. ADD_PENTAGON(v7, v15, v5, v18, v19);
  244. ADD_PENTAGON(v7, v11, v6, v14, v15);
  245. ADD_PENTAGON(v7, v19, v3, v10, v11);
  246. return (polygons ? 5 : 3);
  247. }
  248. // ------------------------------------------------------------------------------------------------
  249. // Build an octahedron with points.magnitude == 1
  250. unsigned int StandardShapes::MakeOctahedron(std::vector<aiVector3D>& positions)
  251. {
  252. positions.reserve(positions.size()+24);
  253. const aiVector3D v0 = aiVector3D(1.0, 0.0, 0.0) ;
  254. const aiVector3D v1 = aiVector3D(-1.0, 0.0, 0.0);
  255. const aiVector3D v2 = aiVector3D(0.0, 1.0, 0.0);
  256. const aiVector3D v3 = aiVector3D(0.0, -1.0, 0.0);
  257. const aiVector3D v4 = aiVector3D(0.0, 0.0, 1.0);
  258. const aiVector3D v5 = aiVector3D(0.0, 0.0, -1.0);
  259. ADD_TRIANGLE(v4,v0,v2);
  260. ADD_TRIANGLE(v4,v2,v1);
  261. ADD_TRIANGLE(v4,v1,v3);
  262. ADD_TRIANGLE(v4,v3,v0);
  263. ADD_TRIANGLE(v5,v2,v0);
  264. ADD_TRIANGLE(v5,v1,v2);
  265. ADD_TRIANGLE(v5,v3,v1);
  266. ADD_TRIANGLE(v5,v0,v3);
  267. return 3;
  268. }
  269. // ------------------------------------------------------------------------------------------------
  270. // Build a tetrahedron with points.magnitude == 1
  271. unsigned int StandardShapes::MakeTetrahedron(std::vector<aiVector3D>& positions)
  272. {
  273. positions.reserve(positions.size()+9);
  274. const ai_real invThree = ai_real( 1.0 ) / ai_real( 3.0 );
  275. const ai_real a = ai_real( 1.41421 ) * invThree;
  276. const ai_real b = ai_real( 2.4494 ) * invThree;
  277. const aiVector3D v0 = aiVector3D(0.0,0.0,1.0);
  278. const aiVector3D v1 = aiVector3D(2*a,0,-invThree );
  279. const aiVector3D v2 = aiVector3D(-a,b,-invThree );
  280. const aiVector3D v3 = aiVector3D(-a,-b,-invThree );
  281. ADD_TRIANGLE(v0,v1,v2);
  282. ADD_TRIANGLE(v0,v2,v3);
  283. ADD_TRIANGLE(v0,v3,v1);
  284. ADD_TRIANGLE(v1,v3,v2);
  285. return 3;
  286. }
  287. // ------------------------------------------------------------------------------------------------
  288. // Build a hexahedron with points.magnitude == 1
  289. unsigned int StandardShapes::MakeHexahedron(std::vector<aiVector3D>& positions,
  290. bool polygons /*= false*/)
  291. {
  292. positions.reserve(positions.size()+36);
  293. const ai_real length = ai_real(1.0)/ai_real(1.73205080);
  294. const aiVector3D v0 = aiVector3D(-1.0,-1.0,-1.0)*length;
  295. const aiVector3D v1 = aiVector3D(1.0,-1.0,-1.0)*length;
  296. const aiVector3D v2 = aiVector3D(1.0,1.0,-1.0)*length;
  297. const aiVector3D v3 = aiVector3D(-1.0,1.0,-1.0)*length;
  298. const aiVector3D v4 = aiVector3D(-1.0,-1.0,1.0)*length;
  299. const aiVector3D v5 = aiVector3D(1.0,-1.0,1.0)*length;
  300. const aiVector3D v6 = aiVector3D(1.0,1.0,1.0)*length;
  301. const aiVector3D v7 = aiVector3D(-1.0,1.0,1.0)*length;
  302. ADD_QUAD(v0,v3,v2,v1);
  303. ADD_QUAD(v0,v1,v5,v4);
  304. ADD_QUAD(v0,v4,v7,v3);
  305. ADD_QUAD(v6,v5,v1,v2);
  306. ADD_QUAD(v6,v2,v3,v7);
  307. ADD_QUAD(v6,v7,v4,v5);
  308. return (polygons ? 4 : 3);
  309. }
  310. // Cleanup ...
  311. #undef ADD_TRIANGLE
  312. #undef ADD_QUAD
  313. #undef ADD_PENTAGON
  314. // ------------------------------------------------------------------------------------------------
  315. // Create a subdivision sphere
  316. void StandardShapes::MakeSphere(unsigned int tess,
  317. std::vector<aiVector3D>& positions)
  318. {
  319. // Reserve enough storage. Every subdivision
  320. // splits each triangle in 4, the icosahedron consists of 60 verts
  321. positions.reserve(positions.size()+60 * integer_pow(4, tess));
  322. // Construct an icosahedron to start with
  323. MakeIcosahedron(positions);
  324. // ... and subdivide it until the requested output
  325. // tesselation is reached
  326. for (unsigned int i = 0; i<tess;++i)
  327. Subdivide(positions);
  328. }
  329. // ------------------------------------------------------------------------------------------------
  330. // Build a cone
  331. void StandardShapes::MakeCone(ai_real height,ai_real radius1,
  332. ai_real radius2,unsigned int tess,
  333. std::vector<aiVector3D>& positions,bool bOpen /*= false */)
  334. {
  335. // Sorry, a cone with less than 3 segments makes ABSOLUTELY NO SENSE
  336. if (tess < 3 || !height)
  337. return;
  338. size_t old = positions.size();
  339. // No negative radii
  340. radius1 = std::fabs(radius1);
  341. radius2 = std::fabs(radius2);
  342. ai_real halfHeight = height / ai_real(2.0);
  343. // radius1 is always the smaller one
  344. if (radius2 > radius1)
  345. {
  346. std::swap(radius2,radius1);
  347. halfHeight = -halfHeight;
  348. }
  349. else old = SIZE_MAX;
  350. // Use a large epsilon to check whether the cone is pointy
  351. if (radius1 < (radius2-radius1)*10e-3)radius1 = 0.0;
  352. // We will need 3*2 verts per segment + 3*2 verts per segment
  353. // if the cone is closed
  354. const unsigned int mem = tess*6 + (!bOpen ? tess*3 * (radius1 ? 2 : 1) : 0);
  355. positions.reserve(positions.size () + mem);
  356. // Now construct all segments
  357. const ai_real angle_delta = (ai_real)AI_MATH_TWO_PI / tess;
  358. const ai_real angle_max = (ai_real)AI_MATH_TWO_PI;
  359. ai_real s = 1.0; // std::cos(angle == 0);
  360. ai_real t = 0.0; // std::sin(angle == 0);
  361. for (ai_real angle = 0.0; angle < angle_max; )
  362. {
  363. const aiVector3D v1 = aiVector3D (s * radius1, -halfHeight, t * radius1 );
  364. const aiVector3D v2 = aiVector3D (s * radius2, halfHeight, t * radius2 );
  365. const ai_real next = angle + angle_delta;
  366. ai_real s2 = std::cos(next);
  367. ai_real t2 = std::sin(next);
  368. const aiVector3D v3 = aiVector3D (s2 * radius2, halfHeight, t2 * radius2 );
  369. const aiVector3D v4 = aiVector3D (s2 * radius1, -halfHeight, t2 * radius1 );
  370. positions.push_back(v1);
  371. positions.push_back(v2);
  372. positions.push_back(v3);
  373. positions.push_back(v4);
  374. positions.push_back(v1);
  375. positions.push_back(v3);
  376. if (!bOpen)
  377. {
  378. // generate the end 'cap'
  379. positions.push_back(aiVector3D(s * radius2, halfHeight, t * radius2 ));
  380. positions.push_back(aiVector3D(s2 * radius2, halfHeight, t2 * radius2 ));
  381. positions.push_back(aiVector3D(0.0, halfHeight, 0.0));
  382. if (radius1)
  383. {
  384. // generate the other end 'cap'
  385. positions.push_back(aiVector3D(s * radius1, -halfHeight, t * radius1 ));
  386. positions.push_back(aiVector3D(s2 * radius1, -halfHeight, t2 * radius1 ));
  387. positions.push_back(aiVector3D(0.0, -halfHeight, 0.0));
  388. }
  389. }
  390. s = s2;
  391. t = t2;
  392. angle = next;
  393. }
  394. // Need to flip face order?
  395. if ( SIZE_MAX != old ) {
  396. for (size_t s = old; s < positions.size();s += 3) {
  397. std::swap(positions[s],positions[s+1]);
  398. }
  399. }
  400. }
  401. // ------------------------------------------------------------------------------------------------
  402. // Build a circle
  403. void StandardShapes::MakeCircle(ai_real radius, unsigned int tess,
  404. std::vector<aiVector3D>& positions)
  405. {
  406. // Sorry, a circle with less than 3 segments makes ABSOLUTELY NO SENSE
  407. if (tess < 3 || !radius)
  408. return;
  409. radius = std::fabs(radius);
  410. // We will need 3 vertices per segment
  411. positions.reserve(positions.size()+tess*3);
  412. const ai_real angle_delta = (ai_real)AI_MATH_TWO_PI / tess;
  413. const ai_real angle_max = (ai_real)AI_MATH_TWO_PI;
  414. ai_real s = 1.0; // std::cos(angle == 0);
  415. ai_real t = 0.0; // std::sin(angle == 0);
  416. for (ai_real angle = 0.0; angle < angle_max; )
  417. {
  418. positions.push_back(aiVector3D(s * radius,0.0,t * radius));
  419. angle += angle_delta;
  420. s = std::cos(angle);
  421. t = std::sin(angle);
  422. positions.push_back(aiVector3D(s * radius,0.0,t * radius));
  423. positions.push_back(aiVector3D(0.0,0.0,0.0));
  424. }
  425. }
  426. } // ! Assimp