WARPS.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /**************************************************************************
  2. * warps.c
  3. *
  4. * This module implements functions that warp or modify the point at which
  5. * a texture pattern is evaluated.
  6. *
  7. * from Persistence of Vision(tm) Ray Tracer
  8. * Copyright 1996,1999 Persistence of Vision Team
  9. *---------------------------------------------------------------------------
  10. * NOTICE: This source code file is provided so that users may experiment
  11. * with enhancements to POV-Ray and to port the software to platforms other
  12. * than those supported by the POV-Ray Team. There are strict rules under
  13. * which you are permitted to use this file. The rules are in the file
  14. * named POVLEGAL.DOC which should be distributed with this file.
  15. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  16. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  17. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  18. *
  19. * This program is based on the popular DKB raytracer version 2.12.
  20. * DKBTrace was originally written by David K. Buck.
  21. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  22. *
  23. *****************************************************************************/
  24. #include "frame.h"
  25. #include "vector.h"
  26. #include "povproto.h"
  27. #include "matrices.h"
  28. #include "warps.h"
  29. #include "pattern.h"
  30. #include "texture.h"
  31. /*****************************************************************************
  32. * Local preprocessor defines
  33. ******************************************************************************/
  34. #define COORDINATE_LIMIT 1.0e17
  35. /*****************************************************************************
  36. * Static functions
  37. ******************************************************************************/
  38. /*****************************************************************************
  39. *
  40. * FUNCTION
  41. *
  42. * Warp_EPoint
  43. *
  44. * INPUT
  45. *
  46. * EPoint -- The original point in 3d space at which a pattern
  47. * is evaluated.
  48. * TPat -- Texture pattern struct
  49. *
  50. * OUTPUT
  51. *
  52. * TPoint -- Point after turbulence and transform
  53. * have been applied
  54. *
  55. * RETURNS
  56. *
  57. * AUTHOR
  58. *
  59. * POV-Ray Team
  60. *
  61. * DESCRIPTION
  62. *
  63. * CHANGES
  64. *
  65. ******************************************************************************/
  66. void Warp_EPoint (VECTOR TPoint, VECTOR EPoint, TPATTERN *TPat)
  67. {
  68. VECTOR PTurbulence,RP;
  69. int Axis,i,temp_rand;
  70. int blockX = 0, blockY = 0, blockZ = 0 ;
  71. SNGL BlkNum;
  72. DBL Length;
  73. DBL Strength;
  74. WARP *Warp=TPat->Warps;
  75. TURB *Turb;
  76. TRANS *Tr;
  77. REPEAT *Repeat;
  78. BLACK_HOLE *Black_Hole;
  79. VECTOR Delta, Center;
  80. Assign_Vector(TPoint, EPoint);
  81. while (Warp != NULL)
  82. {
  83. switch(Warp->Warp_Type)
  84. {
  85. case CLASSIC_TURB_WARP:
  86. if ((TPat->Type == MARBLE_PATTERN) ||
  87. (TPat->Type == NO_PATTERN) ||
  88. (TPat->Type == WOOD_PATTERN))
  89. {
  90. break;
  91. }
  92. /* If not a special type, fall through to next case */
  93. case EXTRA_TURB_WARP:
  94. Turb=(TURB *)Warp;
  95. DTurbulence (PTurbulence, TPoint, Turb);
  96. TPoint[X] += PTurbulence[X] * Turb->Turbulence[X];
  97. TPoint[Y] += PTurbulence[Y] * Turb->Turbulence[Y];
  98. TPoint[Z] += PTurbulence[Z] * Turb->Turbulence[Z];
  99. break;
  100. case NO_WARP:
  101. break;
  102. case TRANSFORM_WARP:
  103. Tr=(TRANS *)Warp;
  104. MInvTransPoint(TPoint, TPoint, &(Tr->Trans));
  105. break;
  106. case REPEAT_WARP:
  107. Repeat=(REPEAT *)Warp;
  108. Assign_Vector(RP,TPoint);
  109. Axis=Repeat->Axis;
  110. BlkNum=floor(TPoint[Axis]/Repeat->Width);
  111. RP[Axis]=TPoint[Axis]-BlkNum*Repeat->Width;
  112. if (((int)BlkNum) & 1)
  113. {
  114. VEvaluateEq(RP,Repeat->Flip);
  115. if ( Repeat->Flip[Axis] < 0 )
  116. {
  117. RP[Axis] = Repeat->Width+RP[Axis];
  118. }
  119. }
  120. VAddScaledEq(RP,BlkNum,Repeat->Offset);
  121. Assign_Vector(TPoint,RP);
  122. break;
  123. case BLACK_HOLE_WARP:
  124. Black_Hole = (BLACK_HOLE *) Warp ;
  125. Assign_Vector (Center, Black_Hole->Center) ;
  126. if (Black_Hole->Repeat)
  127. {
  128. /* first, get the block number we're in for each dimension */
  129. /* block numbers are (currently) calculated relative to 0 */
  130. /* we use floor () since it correctly returns -1 for the
  131. first block below 0 in each axis */
  132. /* one final point - we could run into overflow problems if
  133. the repeat vector was small and the scene very large. */
  134. if (Black_Hole->Repeat_Vector [X] >= Small_Tolerance)
  135. blockX = (int) floor (TPoint [X] / Black_Hole->Repeat_Vector [X]) ;
  136. if (Black_Hole->Repeat_Vector [Y] >= Small_Tolerance)
  137. blockY = (int) floor (TPoint [Y] / Black_Hole->Repeat_Vector [Y]) ;
  138. if (Black_Hole->Repeat_Vector [Z] >= Small_Tolerance)
  139. blockZ = (int) floor (TPoint [Z] / Black_Hole->Repeat_Vector [Z]) ;
  140. if (Black_Hole->Uncertain)
  141. {
  142. /* if the position is uncertain calculate the new one first */
  143. /* this will allow the same numbers to be returned by frand */
  144. temp_rand = POV_GET_OLD_RAND(); /*protect seed*/
  145. POV_SRAND (Hash3d (blockX, blockY, blockZ)) ;
  146. Center [X] += FRAND () * Black_Hole->Uncertainty_Vector [X] ;
  147. Center [Y] += FRAND () * Black_Hole->Uncertainty_Vector [Y] ;
  148. Center [Z] += FRAND () * Black_Hole->Uncertainty_Vector [Z] ;
  149. POV_SRAND (temp_rand) ; /*restore*/
  150. }
  151. Center [X] += Black_Hole->Repeat_Vector [X] * blockX ;
  152. Center [Y] += Black_Hole->Repeat_Vector [Y] * blockY ;
  153. Center [Z] += Black_Hole->Repeat_Vector [Z] * blockZ ;
  154. }
  155. VSub (Delta, TPoint, Center) ;
  156. VLength (Length, Delta) ;
  157. /* Length is the distance from the centre of the black hole */
  158. if (Length >= Black_Hole->Radius) break ;
  159. if (Black_Hole->Type == 0)
  160. {
  161. /* now convert the length to a proportion (0 to 1) that the point
  162. is from the edge of the black hole. a point on the perimeter
  163. of the black hole will be 0.0 ; a point at the centre will be
  164. 1.0 ; a point exactly halfway will be 0.5, and so forth. */
  165. Length = (Black_Hole->Radius - Length) / Black_Hole->Radius ;
  166. /* Strength is the magnitude of the transformation effect. firstly,
  167. apply the Power variable to Length. this is meant to provide a
  168. means of controlling how fast the power of the Black Hole falls
  169. off from its centre. if Power is 2.0, then the effect is inverse
  170. square. increasing power will cause the Black Hole to be a lot
  171. weaker in its effect towards its perimeter.
  172. finally we multiply Strength with the Black Hole's Strength
  173. variable. if the resultant value exceeds 1.0 we clip it to 1.0.
  174. this means a point will never be transformed by more than its
  175. original distance from the centre. the result of this clipping
  176. is that you will have an 'exclusion' area near the centre of
  177. the black hole where all points whose final value exceeded or
  178. equalled 1.0 were moved by a fixed amount. this only happens
  179. if the Strength value of the Black Hole was greater than one. */
  180. Strength = pow (Length, Black_Hole->Power) * Black_Hole->Strength ;
  181. if (Strength > 1.0) Strength = 1.0 ;
  182. /* if the Black Hole is inverted, it gives the impression of 'push-
  183. ing' the pattern away from its centre. otherwise it sucks. */
  184. VScaleEq (Delta, Black_Hole->Inverted ? -Strength : Strength) ;
  185. /* add the scaled Delta to the input point to end up with TPoint. */
  186. VAddEq (TPoint, Delta) ;
  187. }
  188. break;
  189. default:
  190. Error("Warp type %d not yet implemented",Warp->Warp_Type);
  191. }
  192. Warp=Warp->Next_Warp;
  193. }
  194. for (i=X; i<=Z; i++)
  195. if (TPoint[i] > COORDINATE_LIMIT)
  196. TPoint[i]= COORDINATE_LIMIT;
  197. else
  198. if (TPoint[i] < -COORDINATE_LIMIT)
  199. TPoint[i] = -COORDINATE_LIMIT;
  200. }
  201. /*****************************************************************************
  202. *
  203. * FUNCTION
  204. *
  205. * INPUT
  206. *
  207. * OUTPUT
  208. *
  209. * RETURNS
  210. *
  211. * AUTHOR
  212. *
  213. * DESCRIPTION
  214. *
  215. * CHANGES
  216. *
  217. ******************************************************************************/
  218. WARP *Create_Warp (int Warp_Type)
  219. {
  220. WARP *New;
  221. TURB *TNew;
  222. REPEAT *RNew;
  223. TRANS *TRNew;
  224. BLACK_HOLE *BNew;
  225. New = NULL;
  226. switch (Warp_Type)
  227. {
  228. case CLASSIC_TURB_WARP:
  229. case EXTRA_TURB_WARP:
  230. TNew = (TURB *)POV_MALLOC(sizeof(TURB),"turbulence struct");
  231. Make_Vector(TNew->Turbulence,0.0,0.0,0.0);
  232. TNew->Octaves = 6;
  233. TNew->Omega = 0.5;
  234. TNew->Lambda = 2.0;
  235. New = (WARP *)TNew;
  236. break;
  237. case REPEAT_WARP:
  238. RNew = (REPEAT *)POV_MALLOC(sizeof(REPEAT),"repeat warp");
  239. RNew->Axis = -1;
  240. RNew->Width = 0.0;
  241. Make_Vector(RNew->Offset,0.0,0.0,0.0);
  242. Make_Vector(RNew->Flip,1.0,1.0,1.0);
  243. New = (WARP *)RNew;
  244. break;
  245. case BLACK_HOLE_WARP:
  246. BNew = (BLACK_HOLE *)POV_MALLOC (sizeof (BLACK_HOLE), "black hole warp") ;
  247. Make_Vector (BNew->Center, 0.0, 0.0, 0.0) ;
  248. Make_Vector (BNew->Repeat_Vector, 0.0, 0.0, 0.0) ;
  249. Make_Vector (BNew->Uncertainty_Vector, 0.0, 0.0, 0.0) ;
  250. BNew->Strength = 1.0 ;
  251. BNew->Power = 2.0 ;
  252. BNew->Radius = 1.0 ;
  253. BNew->Radius_Squared = 1.0 ;
  254. BNew->Inverse_Radius = 1.0 ;
  255. BNew->Inverted = FALSE ;
  256. BNew->Type = 0 ;
  257. BNew->Repeat = FALSE ;
  258. BNew->Uncertain = FALSE ;
  259. New = (WARP *) BNew ;
  260. break ;
  261. case TRANSFORM_WARP:
  262. TRNew = (TRANS *)POV_MALLOC(sizeof(TRANS),"pattern transform");
  263. MIdentity (TRNew->Trans.matrix);
  264. MIdentity (TRNew->Trans.inverse);
  265. New = (WARP *)TRNew;
  266. break;
  267. default:
  268. Error("Unknown Warp type %d.",Warp_Type);
  269. }
  270. New->Warp_Type = Warp_Type;
  271. New->Next_Warp = NULL;
  272. return(New);
  273. }
  274. /*****************************************************************************
  275. *
  276. * FUNCTION
  277. *
  278. * INPUT
  279. *
  280. * OUTPUT
  281. *
  282. * RETURNS
  283. *
  284. * AUTHOR
  285. *
  286. * DESCRIPTION
  287. *
  288. * CHANGES
  289. *
  290. ******************************************************************************/
  291. void Destroy_Warps (WARP *Warps)
  292. {
  293. WARP *Temp1 = Warps;
  294. WARP *Temp2;
  295. while (Temp1!=NULL)
  296. {
  297. Temp2 = Temp1->Next_Warp;
  298. POV_FREE(Temp1);
  299. Temp1 = Temp2;
  300. }
  301. }
  302. /*****************************************************************************
  303. *
  304. * FUNCTION
  305. *
  306. * INPUT
  307. *
  308. * OUTPUT
  309. *
  310. * RETURNS
  311. *
  312. * AUTHOR
  313. *
  314. * DESCRIPTION
  315. *
  316. * CHANGES
  317. *
  318. ******************************************************************************/
  319. WARP *Copy_Warps (WARP *Old)
  320. {
  321. WARP *New;
  322. if (Old != NULL)
  323. {
  324. New=Create_Warp(Old->Warp_Type);
  325. switch (Old->Warp_Type)
  326. {
  327. case CLASSIC_TURB_WARP:
  328. case EXTRA_TURB_WARP:
  329. memcpy(New,Old,sizeof(TURB));
  330. break;
  331. case REPEAT_WARP:
  332. memcpy(New,Old,sizeof(REPEAT));
  333. break;
  334. case BLACK_HOLE_WARP:
  335. memcpy(New,Old,sizeof(BLACK_HOLE));
  336. break;
  337. case TRANSFORM_WARP:
  338. memcpy(New,Old,sizeof(TRANS));
  339. break;
  340. }
  341. New->Next_Warp=Copy_Warps(Old->Next_Warp);
  342. }
  343. else
  344. {
  345. New=NULL;
  346. }
  347. return(New);
  348. }