StandardShapes.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #define ADD_TRIANGLE(n0,n1,n2) \
  41. positions.push_back(n0); \
  42. positions.push_back(n1); \
  43. positions.push_back(n2);
  44. // ------------------------------------------------------------------------------------------------
  45. void Subdivide(std::vector<aiVector3D>& positions)
  46. {
  47. // assume this to be constant - input must be a Platonic primitive!
  48. const float fl1 = positions[0].Length();
  49. unsigned int origSize = (unsigned int)positions.size();
  50. for (unsigned int i = 0 ; i < origSize ; i+=3)
  51. {
  52. aiVector3D& tv0 = positions[i];
  53. aiVector3D& tv1 = positions[i+1];
  54. aiVector3D& tv2 = positions[i+2];
  55. aiVector3D a = tv0, b = tv1, c = tv2;
  56. aiVector3D v1 = aiVector3D(a.x+b.x, a.y+b.y, a.z+b.z).Normalize()*fl1;
  57. aiVector3D v2 = aiVector3D(a.x+c.x, a.y+c.y, a.z+c.z).Normalize()*fl1;
  58. aiVector3D v3 = aiVector3D(b.x+c.x, b.y+c.y, b.z+c.z).Normalize()*fl1;
  59. tv0 = v1; tv1 = v3; tv2 = v2; // overwrite the original
  60. ADD_TRIANGLE(a, v1, v2);
  61. ADD_TRIANGLE(c, v2, v3);
  62. ADD_TRIANGLE(b, v3, v1);
  63. }
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. void StandardShapes::MakeIcosahedron(aiVector3D& center,float length,
  67. std::vector<aiVector3D>& positions)
  68. {
  69. positions.reserve(positions.size()+60);
  70. const float t = (1.f + 2.236067977f)/2.f;
  71. const float s = sqrt(1.f + t*t) / length;
  72. aiVector3D v0 = aiVector3D(t,1.f, 0.f)/s;
  73. aiVector3D v1 = aiVector3D(-t,1.f, 0.f)/s;
  74. aiVector3D v2 = aiVector3D(t,-1.f, 0.f)/s;
  75. aiVector3D v3 = aiVector3D(-t,-1.f, 0.f)/s;
  76. aiVector3D v4 = aiVector3D(1.f, 0.f, t)/s;
  77. aiVector3D v5 = aiVector3D(1.f, 0.f,-t)/s;
  78. aiVector3D v6 = aiVector3D(-1.f, 0.f,t)/s;
  79. aiVector3D v7 = aiVector3D(-1.f, 0.f,-t)/s;
  80. aiVector3D v8 = aiVector3D(0.f, t, 1.f)/s;
  81. aiVector3D v9 = aiVector3D(0.f,-t, 1.f)/s;
  82. aiVector3D v10 = aiVector3D(0.f, t,-1.f)/s;
  83. aiVector3D v11 = aiVector3D(0.f,-t,-1.f)/s;
  84. ADD_TRIANGLE(v0,v8,v4);
  85. ADD_TRIANGLE(v0,v5,v10);
  86. ADD_TRIANGLE(v2,v4,v9);
  87. ADD_TRIANGLE(v2,v11,v5);
  88. ADD_TRIANGLE(v1,v6,v8);
  89. ADD_TRIANGLE(v1,v10,v7);
  90. ADD_TRIANGLE(v3,v9,v6);
  91. ADD_TRIANGLE(v3,v7,v11);
  92. ADD_TRIANGLE(v0,v10,v8);
  93. ADD_TRIANGLE(v1,v8,v10);
  94. ADD_TRIANGLE(v2,v9,v11);
  95. ADD_TRIANGLE(v3,v11,v9);
  96. ADD_TRIANGLE(v4,v2,v0);
  97. ADD_TRIANGLE(v5,v0,v2);
  98. ADD_TRIANGLE(v6,v1,v3);
  99. ADD_TRIANGLE(v7,v3,v1);
  100. ADD_TRIANGLE(v8,v6,v4);
  101. ADD_TRIANGLE(v9,v4,v6);
  102. ADD_TRIANGLE(v10,v5,v7);
  103. ADD_TRIANGLE(v11,v7,v5);
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. void StandardShapes::MakeDodecahedron(aiVector3D& center,float length,
  107. std::vector<aiVector3D>& positions)
  108. {
  109. positions.reserve(positions.size()+60);
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. void StandardShapes::MakeOctahedron(aiVector3D& center,float length,
  113. std::vector<aiVector3D>& positions)
  114. {
  115. positions.reserve(positions.size()+24);
  116. aiVector3D v0 = aiVector3D(length, 0.f, 0.f) ;
  117. aiVector3D v1 = aiVector3D(-length, 0.f, 0.f);
  118. aiVector3D v2 = aiVector3D(0.f, length, 0.f);
  119. aiVector3D v3 = aiVector3D(0.f, -length, 0.f);
  120. aiVector3D v4 = aiVector3D(0.f, 0.f, length);
  121. aiVector3D v5 = aiVector3D(0.f, 0.f, -length);
  122. ADD_TRIANGLE(v4,v0,v2);
  123. ADD_TRIANGLE(v4,v2,v1);
  124. ADD_TRIANGLE(v4,v1,v3);
  125. ADD_TRIANGLE(v4,v3,v0);
  126. ADD_TRIANGLE(v5,v2,v0);
  127. ADD_TRIANGLE(v5,v1,v2);
  128. ADD_TRIANGLE(v5,v3,v1);
  129. ADD_TRIANGLE(v5,v0,v3);
  130. }
  131. #undef ADD_TRIANGLE
  132. // ------------------------------------------------------------------------------------------------
  133. void StandardShapes::MakeSphere(
  134. aiVector3D& center,
  135. float radius,
  136. unsigned int tess,
  137. std::vector<aiVector3D>& positions)
  138. {
  139. MakeIcosahedron(center,radius,positions);
  140. for (unsigned int i = 0; i<tess;++i)
  141. Subdivide(positions);
  142. }
  143. // ------------------------------------------------------------------------------------------------
  144. void StandardShapes::MakeCone(
  145. aiVector3D& center1,
  146. float radius1,
  147. aiVector3D& center2,
  148. float radius2,
  149. unsigned int tess,
  150. std::vector<aiVector3D>& positions,
  151. bool bOpened /*= false*/)
  152. {
  153. }
  154. // ------------------------------------------------------------------------------------------------
  155. void StandardShapes::MakeCircle(
  156. aiVector3D& center,
  157. aiVector3D& normal,
  158. float radius,
  159. unsigned int tess,
  160. std::vector<aiVector3D>& positions)
  161. {
  162. }
  163. } // ! Assimp