tcbspline.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWMath *
  23. * *
  24. * $Archive:: /VSS_Sync/wwmath/tcbspline.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 6/13/01 2:18p $*
  29. * *
  30. * $Revision:: 6 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "tcbspline.h"
  36. #include "wwdebug.h"
  37. #include "persistfactory.h"
  38. #include "wwmathids.h"
  39. #include "wwhack.h"
  40. /*
  41. ** Force-Link this module because the linker can't detect that we actually need it...
  42. */
  43. DECLARE_FORCE_LINK(tcbspline);
  44. /*
  45. ** Save-Load stuff
  46. */
  47. SimplePersistFactoryClass<TCBSpline3DClass,WWMATH_CHUNKID_TCBSPLINE3D> _TCBSpline3DFactory;
  48. enum
  49. {
  50. // ID's used by TCBSpline3D
  51. TCB3D_CHUNK_HERMITE3D = 0x02071009,
  52. TCB3D_CHUNK_PARAMS,
  53. };
  54. /*
  55. ** TCBSpline3DClass Implemenation
  56. */
  57. int TCBSpline3DClass::Add_Key(const Vector3 & point,float t)
  58. {
  59. int index;
  60. index = HermiteSpline3DClass::Add_Key(point,t);
  61. TCBClass params;
  62. params.Tension = 0.0f;
  63. params.Continuity = 0.0f;
  64. params.Bias = 0.0f;
  65. Params.Insert(index,params);
  66. return index;
  67. }
  68. void TCBSpline3DClass::Remove_Key(int i)
  69. {
  70. HermiteSpline3DClass::Remove_Key(i);
  71. Params.Delete(i);
  72. }
  73. void TCBSpline3DClass::Clear_Keys(void)
  74. {
  75. HermiteSpline3DClass::Clear_Keys();
  76. Params.Clear();
  77. }
  78. void TCBSpline3DClass::Set_TCB_Params(int i,float tension,float continuity,float bias)
  79. {
  80. WWASSERT(i >= 0);
  81. WWASSERT(i < Params.Count());
  82. Params[i].Tension = tension;
  83. Params[i].Continuity = continuity;
  84. Params[i].Bias = bias;
  85. TangentsDirty = true;
  86. }
  87. void TCBSpline3DClass::Get_TCB_Params(int i,float *tension,float *continuity,float *bias)
  88. {
  89. if (tension) *tension = Params[i].Tension;
  90. if (continuity) *continuity = Params[i].Continuity;
  91. if (bias) *bias = Params[i].Bias;
  92. }
  93. void TCBSpline3DClass::Update_Tangents(void)
  94. {
  95. if (Keys.Count() < 2) {
  96. for (int i=0; i<Keys.Count(); i++) {
  97. Tangents[0].InTangent.Set(0,0,0);
  98. Tangents[0].OutTangent.Set(0,0,0);
  99. }
  100. }
  101. // First and Last Key:
  102. // Only need to compute the OutTangent for key[0] and the InTangent for key[end]
  103. int end = Keys.Count() - 1;
  104. Tangents[0].InTangent.Set(0,0,0);
  105. Tangents[end].OutTangent.Set(0,0,0);
  106. if (IsLooping) {
  107. // This really only works if the start and end points have the same position...
  108. // Also just using the TCB params from p0 for both
  109. float k0 = 0.5f * ((1-Params[0].Tension) * (1-Params[0].Continuity) * (1-Params[0].Bias));
  110. float k1 = 0.5f * ((1-Params[0].Tension) * (1+Params[0].Continuity) * (1+Params[0].Bias));
  111. float k2 = 0.5f * ((1-Params[0].Tension) * (1+Params[0].Continuity) * (1-Params[0].Bias));
  112. float k3 = 0.5f * ((1-Params[0].Tension) * (1-Params[0].Continuity) * (1+Params[0].Bias));
  113. Vector3 dp_in;
  114. Vector3 dp_out;
  115. Vector3::Subtract(Keys[0].Point,Keys[end-1].Point,&dp_in);
  116. Vector3::Subtract(Keys[1].Point,Keys[0].Point,&dp_out);
  117. Vector3::Add(k0*dp_in, k1*dp_out, &Tangents[end].InTangent);
  118. Vector3::Add(k2*dp_out, k3*dp_in, &Tangents[0].OutTangent);
  119. } else {
  120. float k2 = 0.25f * ((1-Params[0].Tension) * (1+Params[0].Continuity) * (1-Params[0].Bias));
  121. float k3 = 0.25f * ((1-Params[0].Tension) * (1-Params[0].Continuity) * (1+Params[0].Bias));
  122. Vector3 dp_in;
  123. Vector3 dp_out;
  124. Vector3::Subtract(Keys[1].Point,Keys[0].Point,&dp_out);
  125. dp_in = dp_out;
  126. Vector3::Add(k2*dp_out, k3*dp_in, &Tangents[0].OutTangent);
  127. float k0 = 0.25f * ((1-Params[0].Tension) * (1-Params[0].Continuity) * (1-Params[0].Bias));
  128. float k1 = 0.25f * ((1-Params[0].Tension) * (1+Params[0].Continuity) * (1+Params[0].Bias));
  129. Vector3::Subtract(Keys[end].Point,Keys[end-1].Point,&dp_in);
  130. dp_out = dp_in;
  131. Vector3::Add(k0*dp_out, k1*dp_in, &Tangents[end].InTangent);
  132. }
  133. float total_time = (Keys[1].Time - Keys[0].Time) + (Keys[end].Time - Keys[end-1].Time);
  134. float in_factor = 2.0f * (Keys[end].Time - Keys[end-1].Time) / total_time;
  135. float out_factor = 2.0f * (Keys[1].Time - Keys[0].Time) / total_time;
  136. Tangents[end].InTangent *= in_factor;
  137. Tangents[0].OutTangent *= out_factor;
  138. // Now compute the tangents of all of the normal keys...
  139. for (int pi=1;pi<Keys.Count() - 1; pi++) {
  140. float k0 = 0.5f * ((1-Params[pi].Tension) * (1-Params[pi].Continuity) * (1-Params[pi].Bias));
  141. float k1 = 0.5f * ((1-Params[pi].Tension) * (1+Params[pi].Continuity) * (1+Params[pi].Bias));
  142. float k2 = 0.5f * ((1-Params[pi].Tension) * (1+Params[pi].Continuity) * (1-Params[pi].Bias));
  143. float k3 = 0.5f * ((1-Params[pi].Tension) * (1-Params[pi].Continuity) * (1+Params[pi].Bias));
  144. Vector3 dp_in;
  145. Vector3 dp_out;
  146. Vector3::Subtract(Keys[pi].Point,Keys[pi-1].Point,&dp_in);
  147. Vector3::Subtract(Keys[pi+1].Point,Keys[pi].Point,&dp_out);
  148. Vector3::Add(k0*dp_out, k1*dp_in, &Tangents[pi].InTangent);
  149. Vector3::Add(k2*dp_out, k3*dp_in, &Tangents[pi].OutTangent);
  150. float total_time = (Keys[pi+1].Time - Keys[pi-1].Time);
  151. float in_factor = 2.0f * (Keys[pi].Time - Keys[pi-1].Time) / total_time;
  152. float out_factor = 2.0f * (Keys[pi+1].Time - Keys[pi].Time) / total_time;
  153. Tangents[pi].InTangent *= in_factor; // compensating for un-even keys
  154. Tangents[pi].OutTangent *= out_factor;
  155. }
  156. TangentsDirty = false;
  157. }
  158. const PersistFactoryClass & TCBSpline3DClass::Get_Factory(void) const
  159. {
  160. return _TCBSpline3DFactory;
  161. }
  162. bool TCBSpline3DClass::Save(ChunkSaveClass &csave)
  163. {
  164. csave.Begin_Chunk(TCB3D_CHUNK_HERMITE3D);
  165. HermiteSpline3DClass::Save(csave);
  166. csave.End_Chunk();
  167. csave.Begin_Chunk(TCB3D_CHUNK_PARAMS);
  168. for (int i=0; i<Params.Count(); i++) {
  169. csave.Write(&(Params[i].Tension),sizeof(Params[i].Tension));
  170. csave.Write(&(Params[i].Continuity),sizeof(Params[i].Continuity));
  171. csave.Write(&(Params[i].Bias),sizeof(Params[i].Bias));
  172. }
  173. csave.End_Chunk();
  174. return true;
  175. }
  176. bool TCBSpline3DClass::Load(ChunkLoadClass &cload)
  177. {
  178. int i;
  179. TCBClass param;
  180. // reset the keys
  181. Params.Delete_All();
  182. // read in the chunks
  183. while (cload.Open_Chunk()) {
  184. switch(cload.Cur_Chunk_ID())
  185. {
  186. case TCB3D_CHUNK_HERMITE3D:
  187. HermiteSpline3DClass::Load(cload);
  188. break;
  189. case TCB3D_CHUNK_PARAMS:
  190. for (i=0; i<Keys.Count(); i++) {
  191. cload.Read(&(param.Tension),sizeof(param.Tension));
  192. cload.Read(&(param.Continuity),sizeof(param.Continuity));
  193. cload.Read(&(param.Bias),sizeof(param.Bias));
  194. Params.Add(param);
  195. }
  196. break;
  197. default:
  198. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",__FILE__,__LINE__));
  199. break;
  200. }
  201. cload.Close_Chunk();
  202. }
  203. return true;
  204. }