StandardShapes.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "../include/aiTypes.h"
  36. #include "../include/DefaultLogger.h"
  37. #include "../include/aiAssert.h"
  38. #include "StandardShapes.h"
  39. namespace Assimp {
  40. // note - flip the face ordering
  41. #define ADD_TRIANGLE(n0,n1,n2) \
  42. positions.push_back(n2); \
  43. positions.push_back(n1); \
  44. positions.push_back(n0);
  45. #ifdef AI_STANDARD_SHAPES_OUTPUT_POLYGONS
  46. # define ADD_PENTAGON(n0,n1,n2,n3,n4) \
  47. positions.push_back(n0); \
  48. positions.push_back(n1); \
  49. positions.push_back(n2); \
  50. positions.push_back(n3); \
  51. positions.push_back(n4);
  52. # define ADD_QUAD(n0,n1,n2,n3) \
  53. positions.push_back(n0); \
  54. positions.push_back(n1); \
  55. positions.push_back(n2); \
  56. positions.push_back(n3);
  57. #else
  58. # define ADD_PENTAGON(n0,n1,n2,n3,n4) \
  59. ADD_TRIANGLE(n0, n1, n2) \
  60. ADD_TRIANGLE(n0, n2, n3) \
  61. ADD_TRIANGLE(n0, n3, n4)
  62. # define ADD_QUAD(n0,n1,n2,n3) \
  63. ADD_TRIANGLE(n0, n1, n2) \
  64. ADD_TRIANGLE(n0, n2, n3)
  65. #endif
  66. // ------------------------------------------------------------------------------------------------
  67. void Subdivide(std::vector<aiVector3D>& positions)
  68. {
  69. // assume this to be constant - input must be a Platonic primitive!
  70. const float fl1 = positions[0].Length();
  71. unsigned int origSize = (unsigned int)positions.size();
  72. for (unsigned int i = 0 ; i < origSize ; i+=3)
  73. {
  74. aiVector3D& tv0 = positions[i];
  75. aiVector3D& tv1 = positions[i+1];
  76. aiVector3D& tv2 = positions[i+2];
  77. aiVector3D a = tv0, b = tv1, c = tv2;
  78. aiVector3D v1 = aiVector3D(a.x+b.x, a.y+b.y, a.z+b.z).Normalize()*fl1;
  79. aiVector3D v2 = aiVector3D(a.x+c.x, a.y+c.y, a.z+c.z).Normalize()*fl1;
  80. aiVector3D v3 = aiVector3D(b.x+c.x, b.y+c.y, b.z+c.z).Normalize()*fl1;
  81. tv0 = v1; tv1 = v3; tv2 = v2; // overwrite the original
  82. ADD_TRIANGLE(v2, v1, a);
  83. ADD_TRIANGLE(v3, v2, c);
  84. ADD_TRIANGLE(v1, v3, b);
  85. }
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. void StandardShapes::MakeIcosahedron(aiVector3D& center,const aiVector3D& length,
  89. std::vector<aiVector3D>& positions)
  90. {
  91. positions.reserve(positions.size()+60);
  92. const float t = (1.f + 2.236067977f)/2.f;
  93. const aiVector3D s = aiVector3D( sqrt(1.f + t*t) ) / length;
  94. aiVector3D v0 = aiVector3D(t,1.f, 0.f)/s;
  95. aiVector3D v1 = aiVector3D(-t,1.f, 0.f)/s;
  96. aiVector3D v2 = aiVector3D(t,-1.f, 0.f)/s;
  97. aiVector3D v3 = aiVector3D(-t,-1.f, 0.f)/s;
  98. aiVector3D v4 = aiVector3D(1.f, 0.f, t)/s;
  99. aiVector3D v5 = aiVector3D(1.f, 0.f,-t)/s;
  100. aiVector3D v6 = aiVector3D(-1.f, 0.f,t)/s;
  101. aiVector3D v7 = aiVector3D(-1.f, 0.f,-t)/s;
  102. aiVector3D v8 = aiVector3D(0.f, t, 1.f)/s;
  103. aiVector3D v9 = aiVector3D(0.f,-t, 1.f)/s;
  104. aiVector3D v10 = aiVector3D(0.f, t,-1.f)/s;
  105. aiVector3D v11 = aiVector3D(0.f,-t,-1.f)/s;
  106. ADD_TRIANGLE(v0,v8,v4);
  107. ADD_TRIANGLE(v0,v5,v10);
  108. ADD_TRIANGLE(v2,v4,v9);
  109. ADD_TRIANGLE(v2,v11,v5);
  110. ADD_TRIANGLE(v1,v6,v8);
  111. ADD_TRIANGLE(v1,v10,v7);
  112. ADD_TRIANGLE(v3,v9,v6);
  113. ADD_TRIANGLE(v3,v7,v11);
  114. ADD_TRIANGLE(v0,v10,v8);
  115. ADD_TRIANGLE(v1,v8,v10);
  116. ADD_TRIANGLE(v2,v9,v11);
  117. ADD_TRIANGLE(v3,v11,v9);
  118. ADD_TRIANGLE(v4,v2,v0);
  119. ADD_TRIANGLE(v5,v0,v2);
  120. ADD_TRIANGLE(v6,v1,v3);
  121. ADD_TRIANGLE(v7,v3,v1);
  122. ADD_TRIANGLE(v8,v6,v4);
  123. ADD_TRIANGLE(v9,v4,v6);
  124. ADD_TRIANGLE(v10,v5,v7);
  125. ADD_TRIANGLE(v11,v7,v5);
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. void StandardShapes::MakeDodecahedron(aiVector3D& center,const aiVector3D& length,
  129. std::vector<aiVector3D>& positions)
  130. {
  131. positions.reserve(positions.size()+108);
  132. const float a = 1.f / 1.7320508f;
  133. const float b = sqrt((3.f-2.23606797f)/6.f);
  134. const float c = sqrt((3.f+2.23606797f)/6.f);
  135. aiVector3D v0 = aiVector3D(a,a,a).SymMul(length);
  136. aiVector3D v1 = aiVector3D(a,a,-a).SymMul(length);
  137. aiVector3D v2 = aiVector3D(a,-a,a).SymMul(length);
  138. aiVector3D v3 = aiVector3D(a,-a,-a).SymMul(length);
  139. aiVector3D v4 = aiVector3D(-a,a,a).SymMul(length);
  140. aiVector3D v5 = aiVector3D(-a,a,-a).SymMul(length);
  141. aiVector3D v6 = aiVector3D(-a,-a,a).SymMul(length);
  142. aiVector3D v7 = aiVector3D(-a,-a,-a).SymMul(length);
  143. aiVector3D v8 = aiVector3D(b,c,0.f).SymMul(length);
  144. aiVector3D v9 = aiVector3D(-b,c,0.f).SymMul(length);
  145. aiVector3D v10 = aiVector3D(b,-c,0.f).SymMul(length);
  146. aiVector3D v11 = aiVector3D(-b,-c,0.f).SymMul(length);
  147. aiVector3D v12 = aiVector3D(c, 0.f, b).SymMul(length);
  148. aiVector3D v13 = aiVector3D(c, 0.f, -b).SymMul(length);
  149. aiVector3D v14 = aiVector3D(-c, 0.f, b).SymMul(length);
  150. aiVector3D v15 = aiVector3D(-c, 0.f, -b).SymMul(length);
  151. aiVector3D v16 = aiVector3D(0.f, b, c).SymMul(length);
  152. aiVector3D v17 = aiVector3D(0.f, -b, c).SymMul(length);
  153. aiVector3D v18 = aiVector3D(0.f, b, -c).SymMul(length);
  154. aiVector3D v19 = aiVector3D(0.f, -b, -c).SymMul(length);
  155. ADD_PENTAGON(v0, v8, v9, v4, v16);
  156. ADD_PENTAGON(v0, v12, v13, v1, v8);
  157. ADD_PENTAGON(v0, v16, v17, v2, v12);
  158. ADD_PENTAGON(v8, v1, v18, v5, v9);
  159. ADD_PENTAGON(v12, v2, v10, v3, v13);
  160. ADD_PENTAGON(v16, v4, v14, v6, v17);
  161. ADD_PENTAGON(v9, v5, v15, v14, v4);
  162. ADD_PENTAGON(v6, v11, v10, v2, v17);
  163. ADD_PENTAGON(v3, v19, v18, v1, v13);
  164. ADD_PENTAGON(v7, v15, v5, v18, v19);
  165. ADD_PENTAGON(v7, v11, v6, v14, v15);
  166. ADD_PENTAGON(v7, v19, v3, v10, v11);
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. void StandardShapes::MakeOctahedron(aiVector3D& center,const aiVector3D& length,
  170. std::vector<aiVector3D>& positions)
  171. {
  172. positions.reserve(positions.size()+24);
  173. aiVector3D v0 = aiVector3D(length.x, 0.f, 0.f) ;
  174. aiVector3D v1 = aiVector3D(-length.x, 0.f, 0.f);
  175. aiVector3D v2 = aiVector3D(0.f, length.y, 0.f);
  176. aiVector3D v3 = aiVector3D(0.f, -length.y, 0.f);
  177. aiVector3D v4 = aiVector3D(0.f, 0.f, length.z);
  178. aiVector3D v5 = aiVector3D(0.f, 0.f, -length.z);
  179. ADD_TRIANGLE(v4,v0,v2);
  180. ADD_TRIANGLE(v4,v2,v1);
  181. ADD_TRIANGLE(v4,v1,v3);
  182. ADD_TRIANGLE(v4,v3,v0);
  183. ADD_TRIANGLE(v5,v2,v0);
  184. ADD_TRIANGLE(v5,v1,v2);
  185. ADD_TRIANGLE(v5,v3,v1);
  186. ADD_TRIANGLE(v5,v0,v3);
  187. }
  188. // ------------------------------------------------------------------------------------------------
  189. void StandardShapes::MakeTetrahedron(aiVector3D& center,const aiVector3D& length,
  190. std::vector<aiVector3D>& positions)
  191. {
  192. positions.reserve(positions.size()+9);
  193. const float a = 1.41421f/3.f;
  194. const float b = 2.4494f/3.f;
  195. aiVector3D v0 = aiVector3D(0.f,0.f,1.f).SymMul(length);
  196. aiVector3D v1 = aiVector3D(2*a,0,-1.f/3.f).SymMul(length);
  197. aiVector3D v2 = aiVector3D(-a,b,-1.f/3.f).SymMul(length);
  198. aiVector3D v3 = aiVector3D(-a,-b,-1.f/3.f).SymMul(length);
  199. ADD_TRIANGLE(v0,v1,v2);
  200. ADD_TRIANGLE(v0,v2,v3);
  201. ADD_TRIANGLE(v0,v3,v1);
  202. ADD_TRIANGLE(v1,v3,v2);
  203. }
  204. // ------------------------------------------------------------------------------------------------
  205. void StandardShapes::MakeHexahedron(aiVector3D& center,const aiVector3D& length,
  206. std::vector<aiVector3D>& positions)
  207. {
  208. positions.reserve(positions.size()+36);
  209. aiVector3D _length = length * (1.f/1.73205080f);
  210. aiVector3D v0 = aiVector3D(-1.f,-1.f,-1.f).SymMul(_length) ;
  211. aiVector3D v1 = aiVector3D(1.f,-1.f,-1.f).SymMul(_length) ;
  212. aiVector3D v2 = aiVector3D(1.f,1.f,-1.f) .SymMul(_length) ;
  213. aiVector3D v3 = aiVector3D(-1.f,1.f,-1.f) .SymMul(_length) ;
  214. aiVector3D v4 = aiVector3D(-1.f,-1.f,1.f) .SymMul(_length) ;
  215. aiVector3D v5 = aiVector3D(1.f,-1.f,1.f) .SymMul(_length) ;
  216. aiVector3D v6 = aiVector3D(1.f,1.f,1.f) .SymMul(_length) ;
  217. aiVector3D v7 = aiVector3D(-1.f,1.f,1.f) .SymMul(_length) ;
  218. ADD_QUAD(v0,v3,v2,v1);
  219. ADD_QUAD(v0,v1,v5,v4);
  220. ADD_QUAD(v0,v4,v7,v3);
  221. ADD_QUAD(v6,v5,v1,v2);
  222. ADD_QUAD(v6,v2,v3,v7);
  223. ADD_QUAD(v6,v7,v4,v5);
  224. }
  225. #undef ADD_TRIANGLE
  226. // ------------------------------------------------------------------------------------------------
  227. void StandardShapes::MakeSphere(
  228. aiVector3D& center,
  229. float radius,
  230. unsigned int tess,
  231. std::vector<aiVector3D>& positions)
  232. {
  233. MakeIcosahedron(center,radius,positions);
  234. for (unsigned int i = 0; i<tess;++i)
  235. Subdivide(positions);
  236. }
  237. // ------------------------------------------------------------------------------------------------
  238. void StandardShapes::MakeCone(
  239. aiVector3D& center1,
  240. float radius1,
  241. aiVector3D& center2,
  242. float radius2,
  243. unsigned int tess,
  244. std::vector<aiVector3D>& positions,
  245. bool bOpened /*= false*/)
  246. {
  247. }
  248. // ------------------------------------------------------------------------------------------------
  249. void StandardShapes::MakeCircle(
  250. aiVector3D& center,
  251. aiVector3D& normal,
  252. float radius,
  253. unsigned int tess,
  254. std::vector<aiVector3D>& positions)
  255. {
  256. }
  257. } // ! Assimp