StandardShapes.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the StandardShapes class
  34. */
  35. #include "AssimpPCH.h"
  36. #include "StandardShapes.h"
  37. namespace Assimp {
  38. // note - flip the face order
  39. #define ADD_TRIANGLE(n0,n1,n2) \
  40. positions.push_back(n2); \
  41. positions.push_back(n1); \
  42. positions.push_back(n0);
  43. # define ADD_PENTAGON(n0,n1,n2,n3,n4) \
  44. if (polygons) \
  45. { \
  46. positions.push_back(n0); \
  47. positions.push_back(n1); \
  48. positions.push_back(n2); \
  49. positions.push_back(n3); \
  50. positions.push_back(n4); \
  51. } \
  52. else \
  53. { \
  54. ADD_TRIANGLE(n0, n1, n2) \
  55. ADD_TRIANGLE(n0, n2, n3) \
  56. ADD_TRIANGLE(n0, n3, n4) \
  57. }
  58. # define ADD_QUAD(n0,n1,n2,n3) \
  59. if (polygons) \
  60. { \
  61. positions.push_back(n0); \
  62. positions.push_back(n1); \
  63. positions.push_back(n2); \
  64. positions.push_back(n3); \
  65. } \
  66. else \
  67. { \
  68. ADD_TRIANGLE(n0, n1, n2) \
  69. ADD_TRIANGLE(n0, n2, n3) \
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. void Subdivide(std::vector<aiVector3D>& positions)
  73. {
  74. // assume this to be constant - input must be a Platonic primitive!
  75. const float fl1 = positions[0].Length();
  76. unsigned int origSize = (unsigned int)positions.size();
  77. for (unsigned int i = 0 ; i < origSize ; i+=3)
  78. {
  79. aiVector3D& tv0 = positions[i];
  80. aiVector3D& tv1 = positions[i+1];
  81. aiVector3D& tv2 = positions[i+2];
  82. aiVector3D a = tv0, b = tv1, c = tv2;
  83. aiVector3D v1 = aiVector3D(a.x+b.x, a.y+b.y, a.z+b.z).Normalize()*fl1;
  84. aiVector3D v2 = aiVector3D(a.x+c.x, a.y+c.y, a.z+c.z).Normalize()*fl1;
  85. aiVector3D v3 = aiVector3D(b.x+c.x, b.y+c.y, b.z+c.z).Normalize()*fl1;
  86. tv0 = v1; tv1 = v3; tv2 = v2; // overwrite the original
  87. ADD_TRIANGLE(v2, v1, a);
  88. ADD_TRIANGLE(v3, v2, c);
  89. ADD_TRIANGLE(v1, v3, b);
  90. }
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. aiMesh* StandardShapes::MakeMesh(const std::vector<aiVector3D>& positions,
  94. unsigned int numIndices)
  95. {
  96. if (positions.size() & numIndices || positions.empty() || !numIndices)return NULL;
  97. aiMesh* out = new aiMesh();
  98. switch (numIndices)
  99. {
  100. case 1:
  101. out->mPrimitiveTypes = aiPrimitiveType_POINT;
  102. break;
  103. case 2:
  104. out->mPrimitiveTypes = aiPrimitiveType_LINE;
  105. break;
  106. case 3:
  107. out->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  108. break;
  109. default:
  110. out->mPrimitiveTypes = aiPrimitiveType_POLYGON;
  111. break;
  112. };
  113. out->mNumFaces = (unsigned int)positions.size() / numIndices;
  114. out->mFaces = new aiFace[out->mNumFaces];
  115. for (unsigned int i = 0, a = 0; i < out->mNumFaces;++i)
  116. {
  117. aiFace& f = out->mFaces[i];
  118. f.mNumIndices = numIndices;
  119. f.mIndices = new unsigned int[numIndices];
  120. for (unsigned int i = 0; i < numIndices;++i,++a)
  121. f.mIndices[i] = a;
  122. }
  123. out->mNumVertices = (unsigned int)positions.size();
  124. out->mVertices = new aiVector3D[out->mNumVertices];
  125. ::memcpy(out->mVertices,&positions[0],out->mNumVertices*sizeof(aiVector3D));
  126. return out;
  127. }
  128. // ------------------------------------------------------------------------------------------------
  129. aiMesh* StandardShapes::MakeMesh ( unsigned int (*GenerateFunc)(
  130. std::vector<aiVector3D>&))
  131. {
  132. std::vector<aiVector3D> temp;
  133. unsigned num = (*GenerateFunc)(temp);
  134. return MakeMesh(temp,num);
  135. }
  136. // ------------------------------------------------------------------------------------------------
  137. aiMesh* StandardShapes::MakeMesh ( unsigned int (*GenerateFunc)(
  138. std::vector<aiVector3D>&, bool))
  139. {
  140. std::vector<aiVector3D> temp;
  141. unsigned num = (*GenerateFunc)(temp,true);
  142. return MakeMesh(temp,num);
  143. }
  144. // ------------------------------------------------------------------------------------------------
  145. aiMesh* StandardShapes::MakeMesh (unsigned int num, void (*GenerateFunc)(
  146. unsigned int,std::vector<aiVector3D>&))
  147. {
  148. std::vector<aiVector3D> temp;
  149. (*GenerateFunc)(num,temp);
  150. return MakeMesh(temp,3);
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. unsigned int StandardShapes::MakeIcosahedron(std::vector<aiVector3D>& positions)
  154. {
  155. positions.reserve(positions.size()+60);
  156. const float t = (1.f + 2.236067977f)/2.f;
  157. const float s = sqrt(1.f + t*t);
  158. aiVector3D v0 = aiVector3D(t,1.f, 0.f)/s;
  159. aiVector3D v1 = aiVector3D(-t,1.f, 0.f)/s;
  160. aiVector3D v2 = aiVector3D(t,-1.f, 0.f)/s;
  161. aiVector3D v3 = aiVector3D(-t,-1.f, 0.f)/s;
  162. aiVector3D v4 = aiVector3D(1.f, 0.f, t)/s;
  163. aiVector3D v5 = aiVector3D(1.f, 0.f,-t)/s;
  164. aiVector3D v6 = aiVector3D(-1.f, 0.f,t)/s;
  165. aiVector3D v7 = aiVector3D(-1.f, 0.f,-t)/s;
  166. aiVector3D v8 = aiVector3D(0.f, t, 1.f)/s;
  167. aiVector3D v9 = aiVector3D(0.f,-t, 1.f)/s;
  168. aiVector3D v10 = aiVector3D(0.f, t,-1.f)/s;
  169. aiVector3D v11 = aiVector3D(0.f,-t,-1.f)/s;
  170. ADD_TRIANGLE(v0,v8,v4);
  171. ADD_TRIANGLE(v0,v5,v10);
  172. ADD_TRIANGLE(v2,v4,v9);
  173. ADD_TRIANGLE(v2,v11,v5);
  174. ADD_TRIANGLE(v1,v6,v8);
  175. ADD_TRIANGLE(v1,v10,v7);
  176. ADD_TRIANGLE(v3,v9,v6);
  177. ADD_TRIANGLE(v3,v7,v11);
  178. ADD_TRIANGLE(v0,v10,v8);
  179. ADD_TRIANGLE(v1,v8,v10);
  180. ADD_TRIANGLE(v2,v9,v11);
  181. ADD_TRIANGLE(v3,v11,v9);
  182. ADD_TRIANGLE(v4,v2,v0);
  183. ADD_TRIANGLE(v5,v0,v2);
  184. ADD_TRIANGLE(v6,v1,v3);
  185. ADD_TRIANGLE(v7,v3,v1);
  186. ADD_TRIANGLE(v8,v6,v4);
  187. ADD_TRIANGLE(v9,v4,v6);
  188. ADD_TRIANGLE(v10,v5,v7);
  189. ADD_TRIANGLE(v11,v7,v5);
  190. return 3;
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. unsigned int StandardShapes::MakeDodecahedron(std::vector<aiVector3D>& positions,
  194. bool polygons /*= false*/)
  195. {
  196. positions.reserve(positions.size()+108);
  197. const float a = 1.f / 1.7320508f;
  198. const float b = sqrt((3.f-2.23606797f)/6.f);
  199. const float c = sqrt((3.f+2.23606797f)/6.f);
  200. aiVector3D v0 = aiVector3D(a,a,a);
  201. aiVector3D v1 = aiVector3D(a,a,-a);
  202. aiVector3D v2 = aiVector3D(a,-a,a);
  203. aiVector3D v3 = aiVector3D(a,-a,-a);
  204. aiVector3D v4 = aiVector3D(-a,a,a);
  205. aiVector3D v5 = aiVector3D(-a,a,-a);
  206. aiVector3D v6 = aiVector3D(-a,-a,a);
  207. aiVector3D v7 = aiVector3D(-a,-a,-a);
  208. aiVector3D v8 = aiVector3D(b,c,0.f);
  209. aiVector3D v9 = aiVector3D(-b,c,0.f);
  210. aiVector3D v10 = aiVector3D(b,-c,0.f);
  211. aiVector3D v11 = aiVector3D(-b,-c,0.f);
  212. aiVector3D v12 = aiVector3D(c, 0.f, b);
  213. aiVector3D v13 = aiVector3D(c, 0.f, -b);
  214. aiVector3D v14 = aiVector3D(-c, 0.f, b);
  215. aiVector3D v15 = aiVector3D(-c, 0.f, -b);
  216. aiVector3D v16 = aiVector3D(0.f, b, c);
  217. aiVector3D v17 = aiVector3D(0.f, -b, c);
  218. aiVector3D v18 = aiVector3D(0.f, b, -c);
  219. aiVector3D v19 = aiVector3D(0.f, -b, -c);
  220. ADD_PENTAGON(v0, v8, v9, v4, v16);
  221. ADD_PENTAGON(v0, v12, v13, v1, v8);
  222. ADD_PENTAGON(v0, v16, v17, v2, v12);
  223. ADD_PENTAGON(v8, v1, v18, v5, v9);
  224. ADD_PENTAGON(v12, v2, v10, v3, v13);
  225. ADD_PENTAGON(v16, v4, v14, v6, v17);
  226. ADD_PENTAGON(v9, v5, v15, v14, v4);
  227. ADD_PENTAGON(v6, v11, v10, v2, v17);
  228. ADD_PENTAGON(v3, v19, v18, v1, v13);
  229. ADD_PENTAGON(v7, v15, v5, v18, v19);
  230. ADD_PENTAGON(v7, v11, v6, v14, v15);
  231. ADD_PENTAGON(v7, v19, v3, v10, v11);
  232. return (polygons ? 5 : 3);
  233. }
  234. // ------------------------------------------------------------------------------------------------
  235. unsigned int StandardShapes::MakeOctahedron(std::vector<aiVector3D>& positions)
  236. {
  237. positions.reserve(positions.size()+24);
  238. aiVector3D v0 = aiVector3D(1.0f, 0.f, 0.f) ;
  239. aiVector3D v1 = aiVector3D(-1.0f, 0.f, 0.f);
  240. aiVector3D v2 = aiVector3D(0.f, 1.0f, 0.f);
  241. aiVector3D v3 = aiVector3D(0.f, -1.0f, 0.f);
  242. aiVector3D v4 = aiVector3D(0.f, 0.f, 1.0f);
  243. aiVector3D v5 = aiVector3D(0.f, 0.f, -1.0f);
  244. ADD_TRIANGLE(v4,v0,v2);
  245. ADD_TRIANGLE(v4,v2,v1);
  246. ADD_TRIANGLE(v4,v1,v3);
  247. ADD_TRIANGLE(v4,v3,v0);
  248. ADD_TRIANGLE(v5,v2,v0);
  249. ADD_TRIANGLE(v5,v1,v2);
  250. ADD_TRIANGLE(v5,v3,v1);
  251. ADD_TRIANGLE(v5,v0,v3);
  252. return 3;
  253. }
  254. // ------------------------------------------------------------------------------------------------
  255. unsigned int StandardShapes::MakeTetrahedron(std::vector<aiVector3D>& positions)
  256. {
  257. positions.reserve(positions.size()+9);
  258. const float a = 1.41421f/3.f;
  259. const float b = 2.4494f/3.f;
  260. aiVector3D v0 = aiVector3D(0.f,0.f,1.f);
  261. aiVector3D v1 = aiVector3D(2*a,0,-1.f/3.f);
  262. aiVector3D v2 = aiVector3D(-a,b,-1.f/3.f);
  263. aiVector3D v3 = aiVector3D(-a,-b,-1.f/3.f);
  264. ADD_TRIANGLE(v0,v1,v2);
  265. ADD_TRIANGLE(v0,v2,v3);
  266. ADD_TRIANGLE(v0,v3,v1);
  267. ADD_TRIANGLE(v1,v3,v2);
  268. return 3;
  269. }
  270. // ------------------------------------------------------------------------------------------------
  271. unsigned int StandardShapes::MakeHexahedron(std::vector<aiVector3D>& positions,
  272. bool polygons /*= false*/)
  273. {
  274. positions.reserve(positions.size()+36);
  275. float length = 1.f/1.73205080f;
  276. aiVector3D v0 = aiVector3D(-1.f,-1.f,-1.f)*length;
  277. aiVector3D v1 = aiVector3D(1.f,-1.f,-1.f)*length;
  278. aiVector3D v2 = aiVector3D(1.f,1.f,-1.f)*length;
  279. aiVector3D v3 = aiVector3D(-1.f,1.f,-1.f)*length;
  280. aiVector3D v4 = aiVector3D(-1.f,-1.f,1.f)*length;
  281. aiVector3D v5 = aiVector3D(1.f,-1.f,1.f)*length;
  282. aiVector3D v6 = aiVector3D(1.f,1.f,1.f)*length;
  283. aiVector3D v7 = aiVector3D(-1.f,1.f,1.f)*length;
  284. ADD_QUAD(v0,v3,v2,v1);
  285. ADD_QUAD(v0,v1,v5,v4);
  286. ADD_QUAD(v0,v4,v7,v3);
  287. ADD_QUAD(v6,v5,v1,v2);
  288. ADD_QUAD(v6,v2,v3,v7);
  289. ADD_QUAD(v6,v7,v4,v5);
  290. return (polygons ? 4 : 3);
  291. }
  292. // Cleanup ...
  293. #undef ADD_TRIANGLE
  294. #undef ADD_QUAD
  295. #undef ADD_PENTAGON
  296. // ------------------------------------------------------------------------------------------------
  297. void StandardShapes::MakeSphere(unsigned int tess,
  298. std::vector<aiVector3D>& positions)
  299. {
  300. MakeIcosahedron(positions);
  301. for (unsigned int i = 0; i<tess;++i)
  302. Subdivide(positions);
  303. }
  304. // ------------------------------------------------------------------------------------------------
  305. void StandardShapes::MakeCone(
  306. aiVector3D& center1,
  307. float radius1,
  308. aiVector3D& center2,
  309. float radius2,
  310. unsigned int tess,
  311. std::vector<aiVector3D>& positions,
  312. bool bOpened /*= false*/)
  313. {
  314. }
  315. // ------------------------------------------------------------------------------------------------
  316. void StandardShapes::MakeCircle(
  317. const aiVector3D& center,
  318. const aiVector3D& normal,
  319. float radius,
  320. unsigned int tess,
  321. std::vector<aiVector3D>& positions)
  322. {
  323. //aiVector3D current = aiVector3D ( normal.x,
  324. }
  325. } // ! Assimp