afxCurve3D.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "afx/util/afxCurveEval.h"
  26. #include "afx/util/afxCurve3D.h"
  27. afxCurve3D::afxCurve3D() : usable( false ), default_vector( 0, 0, 0 ), flip(false)
  28. {
  29. evaluator = new afxHermiteEval();
  30. }
  31. afxCurve3D::~afxCurve3D()
  32. {
  33. delete evaluator;
  34. }
  35. void afxCurve3D::addPoint( F32 param, Point3F &v )
  36. {
  37. if( param < 0.0f || param > 1.0f )
  38. return;
  39. CurvePoint p;
  40. p.parameter = param;
  41. p.point.set( v );
  42. points.push_back( p );
  43. usable = false;
  44. }
  45. void afxCurve3D::setPoint( int index, Point3F &v )
  46. {
  47. if( ( index < 0 ) || ( index >= points.size() ) )
  48. return;
  49. CurvePoint &p = points[index];
  50. p.point = v;
  51. if( index == 0 )
  52. start_value = v;
  53. else if( index == points.size()-1 )
  54. final_value = v;
  55. }
  56. //bool afxCurve3D::compare_CurvePoint( const afxCurve3D::CurvePoint &a, const afxCurve3D::CurvePoint &b )
  57. //{
  58. // return a.parameter < b.parameter;
  59. //}
  60. S32 QSORT_CALLBACK afxCurve3D::compare_CurvePoint( const void* a, const void* b )
  61. {
  62. //CurvePoint *cp_a = *((CurvePoint **)a);
  63. //CurvePoint *cp_b = *((CurvePoint **)b);
  64. const CurvePoint *cp_a = (CurvePoint *)a;
  65. const CurvePoint *cp_b = (CurvePoint *)b;
  66. //Con::printf( "*** %f %f", cp_a->parameter, cp_b->parameter );
  67. //return cp_a->parameter < cp_b->parameter;
  68. //return 1;
  69. if (cp_a->parameter > cp_b->parameter)
  70. return 1;
  71. else if (cp_a->parameter < cp_b->parameter)
  72. return -1;
  73. else
  74. return 0;
  75. }
  76. void afxCurve3D::sort( )
  77. {
  78. if( points.size() == 0 )
  79. return;
  80. if( points.size() == 1 )
  81. {
  82. start_value = points[0].point;
  83. final_value = start_value;
  84. usable = true;
  85. return;
  86. }
  87. //Con::printf( "*** pre-sort" );
  88. //std::sort( points.begin(), points.end(), afxCurve3D::compare_CurvePoint );
  89. dQsort( points.address(), points.size(), sizeof(CurvePoint), afxCurve3D::compare_CurvePoint );
  90. //Con::printf( "*** post-sort" );
  91. start_value = points[0].point;
  92. final_value = points[points.size()-1].point;
  93. usable = true;
  94. start_tangent = evaluateTangent( 0.0f );
  95. final_tangent = evaluateTangent( 1.0f );
  96. }
  97. int afxCurve3D::numPoints()
  98. {
  99. return points.size();
  100. }
  101. F32 afxCurve3D::getParameter( int index )
  102. {
  103. if( ( index < 0 ) || ( index >= points.size() ) )
  104. return 0.0f;
  105. return points[index].parameter;
  106. }
  107. Point3F afxCurve3D::getPoint( int index )
  108. {
  109. if( ( index < 0 ) || ( index >= points.size() ) )
  110. return default_vector;
  111. return points[index].point;
  112. }
  113. Point3F afxCurve3D::evaluate( F32 param )
  114. {
  115. if( !usable )
  116. return default_vector;
  117. if( param <= 0.0f )
  118. return start_value;
  119. if( param >= 1.0f )
  120. return final_value;
  121. if( points.size() == 1 )
  122. return start_value;
  123. int start_index = 0;
  124. for( ; start_index < points.size()-1; start_index++ )
  125. {
  126. if( param < points[start_index+1].parameter )
  127. break;
  128. }
  129. int end_index = start_index+1;
  130. CurvePoint p0 = points[start_index];
  131. CurvePoint p1 = points[end_index];
  132. // Compute tangents
  133. //Point3F tan0 = computeTangentP0( p0.point, p1.point, start_index );
  134. //Point3F tan1 = computeTangentP1( p0.point, p1.point, end_index );
  135. F32 local_param = ( param - p0.parameter ) / ( p1.parameter - p0.parameter );
  136. //Point3F vnew = evaluator->evaluateCurve( p0.point,
  137. // p1.point,
  138. // tan0,
  139. // tan1,
  140. // local_param );
  141. Point3F vnew = evaluator->evaluateCurve( p0.point, p1.point,
  142. p0.tangent, p1.tangent,
  143. local_param );
  144. return vnew;
  145. }
  146. Point3F afxCurve3D::evaluateTangent( F32 param )
  147. {
  148. if( !usable )
  149. return default_vector;
  150. if( param < 0.0f )
  151. return start_tangent;
  152. if( param > 1.0f )
  153. return final_tangent;
  154. if( points.size() == 1 )
  155. return start_tangent;
  156. int start_index = 0;
  157. for( ; start_index < points.size()-1; start_index++ )
  158. {
  159. if( param < points[start_index+1].parameter )
  160. break;
  161. }
  162. int end_index = start_index+1;
  163. if( param == 1.0f )
  164. {
  165. end_index = points.size()-1;
  166. start_index = end_index - 1;
  167. }
  168. CurvePoint p0 = points[start_index];
  169. CurvePoint p1 = points[end_index];
  170. // Compute tangents
  171. //Point3F tan0 = computeTangentP0( p0.point, p1.point, start_index );
  172. //Point3F tan1 = computeTangentP1( p0.point, p1.point, end_index );
  173. F32 local_param = ( param - p0.parameter ) / ( p1.parameter - p0.parameter );
  174. //Point3F vnew = evaluator->evaluateCurveTangent( p0.point,
  175. // p1.point,
  176. // tan0,
  177. // tan1,
  178. // local_param );
  179. Point3F vnew = evaluator->evaluateCurveTangent( p0.point, p1.point,
  180. p0.tangent, p1.tangent,
  181. local_param );
  182. return vnew;
  183. }
  184. Point3F afxCurve3D::computeTangentP0( Point3F &p0, Point3F &p1, int start_index )
  185. {
  186. Point3F tan0;
  187. Point3F p_prev;
  188. Point3F p_next;
  189. // tangent for p0
  190. if( start_index == 0 )
  191. {
  192. p_prev = p0; // Setting previous point to p0, creating a hidden point in
  193. // the same spot
  194. p_next = p1;
  195. }
  196. else
  197. {
  198. CurvePoint &p = points[start_index-1];
  199. p_prev = p.point;
  200. p_next = p1;
  201. }
  202. tan0 = p_next-p_prev; //p_next.subtract( p_prev );
  203. tan0 *= .5f; //= tan0.scale( .5f );
  204. return tan0;
  205. }
  206. Point3F afxCurve3D::computeTangentP1( Point3F &p0, Point3F &p1, int end_index )
  207. {
  208. Point3F tan1;
  209. Point3F p_prev;
  210. Point3F p_next;
  211. // tangent for p1
  212. if( end_index == points.size()-1 )
  213. {
  214. p_prev = p0;
  215. p_next = p1; // Setting next point to p1, creating a hidden point in
  216. // the same spot
  217. }
  218. else
  219. {
  220. p_prev = p0;
  221. CurvePoint &p = points[end_index+1];
  222. p_next = p.point;
  223. }
  224. tan1 = p_next-p_prev; //p_next.subtract( p_prev );
  225. tan1 *= .5f; //= tan1.scale( .5f );
  226. //Con::printf("UPDATE");
  227. return tan1;
  228. }
  229. void afxCurve3D::computeTangents()
  230. {
  231. CurvePoint *p_prev;
  232. CurvePoint *p_next;
  233. for( int i = 0; i < points.size(); i++ )
  234. {
  235. CurvePoint *p = &points[i];
  236. if( i == 0 )
  237. {
  238. p_prev = p; // Setting previous point to p0, creating a hidden point in
  239. // the same spot
  240. p_next = &points[i+1];
  241. }
  242. else if( i == points.size()-1 )
  243. {
  244. p_prev = &points[i-1];
  245. p_next = p; // Setting next point to p1, creating a hidden point in
  246. // the same spot
  247. }
  248. else
  249. {
  250. p_prev = &points[i-1];
  251. p_next = &points[i+1];
  252. }
  253. p->tangent = p_next->point - p_prev->point;
  254. //(p->tangent).normalize();
  255. p->tangent *= .5f;
  256. //Con::printf( "%d: %f %f %f", i, p->tangent.x, p->tangent.y, p->tangent.z );
  257. }
  258. }
  259. void afxCurve3D::print()
  260. {
  261. Con::printf( "afxCurve3D -------------------------" );
  262. for( int i = 0; i < points.size(); i++ )
  263. {
  264. CurvePoint &p = points[i];
  265. Con::printf( "%f: %f %f %f", p.parameter, p.point.x, p.point.y, p.point.z );
  266. }
  267. Con::printf( "---------------------------------" );
  268. }