PATTERN.C 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  1. /**************************************************************************
  2. * pattern.c
  3. *
  4. * This module implements texturing functions that return a value to be
  5. * used in a pigment or normal.
  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. * Modifications by Hans-Detlev Fink, January 1999, used with permission
  24. * Modifications by Thomas Willhalm, March 1999, used with permission
  25. *
  26. *****************************************************************************/
  27. /*
  28. * Some texture ideas garnered from SIGGRAPH '85 Volume 19 Number 3,
  29. * "An Image Synthesizer" By Ken Perlin.
  30. * Further Ideas Garnered from "The RenderMan Companion" (Addison Wesley).
  31. */
  32. #include "frame.h"
  33. #include "vector.h"
  34. #include "povproto.h"
  35. #include "matrices.h"
  36. #include "pattern.h"
  37. #include "povray.h"
  38. #include "texture.h"
  39. #include "image.h"
  40. #include "txttest.h"
  41. #include "colour.h"
  42. /*****************************************************************************
  43. * Local preprocessor defines
  44. ******************************************************************************/
  45. /*****************************************************************************
  46. * Static functions
  47. ******************************************************************************/
  48. static DBL agate (VECTOR EPoint, TPATTERN *TPat);
  49. static DBL brick (VECTOR EPoint, TPATTERN *TPat);
  50. static DBL checker (VECTOR EPoint);
  51. static DBL crackle (VECTOR EPoint);
  52. static DBL gradient (VECTOR EPoint, TPATTERN *TPat);
  53. static DBL granite (VECTOR EPoint);
  54. static DBL leopard (VECTOR EPoint);
  55. static DBL mandel (VECTOR EPoint, TPATTERN *TPat);
  56. static DBL marble (VECTOR EPoint, TPATTERN *TPat);
  57. static DBL onion (VECTOR EPoint);
  58. static DBL radial (VECTOR EPoint);
  59. static DBL spiral1 (VECTOR EPoint, TPATTERN *TPat);
  60. static DBL spiral2 (VECTOR EPoint, TPATTERN *TPat);
  61. static DBL wood (VECTOR EPoint, TPATTERN *TPat);
  62. static DBL hexagon (VECTOR EPoint);
  63. static DBL planar_pattern (VECTOR EPoint);
  64. static DBL spherical (VECTOR EPoint);
  65. static DBL boxed (VECTOR EPoint);
  66. static DBL cylindrical (VECTOR EPoint);
  67. static DBL density_file (VECTOR EPoint, TPATTERN *TPat);
  68. static long PickInCube (VECTOR tv, VECTOR p1);
  69. static DBL ripples_pigm (VECTOR EPoint, TPATTERN *TPat);
  70. static DBL waves_pigm (VECTOR EPoint, TPATTERN *TPat);
  71. static DBL dents_pigm (VECTOR EPoint);
  72. static DBL wrinkles_pigm (VECTOR EPoint);
  73. static DBL quilted_pigm (VECTOR EPoint, TPATTERN *TPat);
  74. static TURB *Search_For_Turb (WARP *Warps);
  75. /* static TURB *Copy_Turb (TURB *Old); Unused function [AED] */
  76. static unsigned short readushort(FILE *infile);
  77. /*****************************************************************************
  78. *
  79. * FUNCTION
  80. *
  81. * agate
  82. *
  83. * INPUT
  84. *
  85. * EPoint -- The point in 3d space at which the pattern is evaluated.
  86. * TPat -- Texture pattern struct
  87. *
  88. * OUTPUT
  89. *
  90. * RETURNS
  91. *
  92. * DBL value in the range 0.0 to 1.0
  93. *
  94. * AUTHOR
  95. *
  96. * POV-Ray Team
  97. *
  98. * DESCRIPTION
  99. *
  100. * CHANGES
  101. * Oct 1994 : adapted from agate pigment by [CY]
  102. *
  103. ******************************************************************************/
  104. static DBL agate (VECTOR EPoint, TPATTERN *TPat)
  105. {
  106. register DBL noise, turb_val;
  107. TURB* Turb;
  108. Turb=Search_For_Turb(TPat->Warps);
  109. turb_val = TPat->Vals.Agate_Turb_Scale * Turbulence(EPoint,Turb);
  110. noise = 0.5 * (cycloidal(1.3 * turb_val + 1.1 * EPoint[Z]) + 1.0);
  111. if (noise < 0.0)
  112. {
  113. noise = 0.0;
  114. }
  115. else
  116. {
  117. noise = min(1.0, noise);
  118. noise = pow(noise, 0.77);
  119. }
  120. return(noise);
  121. }
  122. /*****************************************************************************
  123. *
  124. * FUNCTION
  125. *
  126. * brick
  127. *
  128. * INPUT
  129. *
  130. * EPoint -- The point in 3d space at which the pattern
  131. * is evaluated.
  132. * TPat -- Texture pattern struct
  133. *
  134. * OUTPUT
  135. *
  136. * RETURNS
  137. *
  138. * DBL value exactly 0.0 or 1.0
  139. *
  140. * AUTHOR
  141. *
  142. * Dan Farmer
  143. *
  144. * DESCRIPTION
  145. *
  146. * CHANGES
  147. * Oct 1994 : adapted from pigment by [CY]
  148. *
  149. ******************************************************************************/
  150. static DBL brick (VECTOR EPoint, TPATTERN *TPat)
  151. {
  152. int ibrickx, ibricky, ibrickz;
  153. DBL brickheight, brickwidth, brickdepth;
  154. DBL brickmortar, mortarheight, mortarwidth, mortardepth;
  155. DBL brickx, bricky, brickz;
  156. DBL x, y, z, fudgit;
  157. fudgit=Small_Tolerance+TPat->Vals.Brick.Mortar;
  158. x = EPoint[X]+fudgit;
  159. y = EPoint[Y]+fudgit;
  160. z = EPoint[Z]+fudgit;
  161. brickwidth = TPat->Vals.Brick.Size[X];
  162. brickheight = TPat->Vals.Brick.Size[Y];
  163. brickdepth = TPat->Vals.Brick.Size[Z];
  164. brickmortar = (DBL)TPat->Vals.Brick.Mortar;
  165. mortarwidth = brickmortar / brickwidth;
  166. mortarheight = brickmortar / brickheight;
  167. mortardepth = brickmortar / brickdepth;
  168. /* 1) Check mortar layers in the X-Z plane (ie: top view) */
  169. bricky = y / brickheight;
  170. ibricky = (int) bricky;
  171. bricky -= (DBL) ibricky;
  172. if (bricky < 0.0)
  173. {
  174. bricky += 1.0;
  175. }
  176. if (bricky <= mortarheight)
  177. {
  178. return(0.0);
  179. }
  180. bricky = (y / brickheight) * 0.5;
  181. ibricky = (int) bricky;
  182. bricky -= (DBL) ibricky;
  183. if (bricky < 0.0)
  184. {
  185. bricky += 1.0;
  186. }
  187. /* 2) Check ODD mortar layers in the Y-Z plane (ends) */
  188. brickx = (x / brickwidth);
  189. ibrickx = (int) brickx;
  190. brickx -= (DBL) ibrickx;
  191. if (brickx < 0.0)
  192. {
  193. brickx += 1.0;
  194. }
  195. if ((brickx <= mortarwidth) && (bricky <= 0.5))
  196. {
  197. return(0.0);
  198. }
  199. /* 3) Check EVEN mortar layers in the Y-Z plane (ends) */
  200. brickx = (x / brickwidth) + 0.5;
  201. ibrickx = (int) brickx;
  202. brickx -= (DBL) ibrickx;
  203. if (brickx < 0.0)
  204. {
  205. brickx += 1.0;
  206. }
  207. if ((brickx <= mortarwidth) && (bricky > 0.5))
  208. {
  209. return(0.0);
  210. }
  211. /* 4) Check ODD mortar layers in the Y-X plane (facing) */
  212. brickz = (z / brickdepth);
  213. ibrickz = (int) brickz;
  214. brickz -= (DBL) ibrickz;
  215. if (brickz < 0.0)
  216. {
  217. brickz += 1.0;
  218. }
  219. if ((brickz <= mortardepth) && (bricky > 0.5))
  220. {
  221. return(0.0);
  222. }
  223. /* 5) Check EVEN mortar layers in the X-Y plane (facing) */
  224. brickz = (z / brickdepth) + 0.5;
  225. ibrickz = (int) brickz;
  226. brickz -= (DBL) ibrickz;
  227. if (brickz < 0.0)
  228. {
  229. brickz += 1.0;
  230. }
  231. if ((brickz <= mortardepth) && (bricky <= 0.5))
  232. {
  233. return(0.0);
  234. }
  235. /* If we've gotten this far, color me brick. */
  236. return(1.0);
  237. }
  238. /*****************************************************************************
  239. *
  240. * FUNCTION
  241. *
  242. * checker
  243. *
  244. * INPUT
  245. *
  246. * EPoint -- The point in 3d space at which the pattern
  247. * is evaluated.
  248. *
  249. * OUTPUT
  250. *
  251. * RETURNS
  252. *
  253. * DBL value exactly 0.0 or 1.0
  254. *
  255. * AUTHOR
  256. *
  257. * POV-Team
  258. *
  259. * DESCRIPTION
  260. *
  261. * CHANGES
  262. * Oct 1994 : adapted from pigment by [CY]
  263. *
  264. ******************************************************************************/
  265. static DBL checker (VECTOR EPoint)
  266. {
  267. int value;
  268. value = (int)(floor(EPoint[X]+Small_Tolerance) +
  269. floor(EPoint[Y]+Small_Tolerance) +
  270. floor(EPoint[Z]+Small_Tolerance));
  271. if (value & 1)
  272. {
  273. return (1.0);
  274. }
  275. else
  276. {
  277. return (0.0);
  278. }
  279. }
  280. /*****************************************************************************
  281. *
  282. * FUNCTION
  283. *
  284. * crackle
  285. *
  286. * INPUT
  287. *
  288. * EPoint -- The point in 3d space at which the pattern
  289. * is evaluated.
  290. * OUTPUT
  291. *
  292. * RETURNS
  293. *
  294. * DBL value in the range 0.0 to 1.0
  295. *
  296. * AUTHOR
  297. *
  298. * Jim McElhiney
  299. *
  300. * DESCRIPTION
  301. *
  302. * "crackle":
  303. *
  304. * New colour function by Jim McElhiney,
  305. * CompuServe 71201,1326, aka mcelhiney@acm.org
  306. *
  307. * Large scale, without turbulence, makes a pretty good stone wall.
  308. * Small scale, without turbulence, makes a pretty good crackle ceramic glaze.
  309. * Highly turbulent (with moderate displacement) makes a good marble, solving
  310. * the problem of apparent parallel layers in Perlin's method.
  311. * 2 octaves of full-displacement turbulence make a great "drizzled paint"
  312. * pattern, like a 1950's counter top.
  313. * Rule of thumb: put a single colour transition near 0 in your colour map.
  314. *
  315. * Mathematically, the set crackle(p)=0 is a 3D Voronoi diagram of a field of
  316. * semirandom points, and crackle(p)>0 is distance from set along shortest path.
  317. * (A Voronoi diagram is the locus of points equidistant from their 2 nearest
  318. * neighbours from a set of disjoint points, like the membranes in suds are
  319. * to the centres of the bubbles).
  320. *
  321. * All "crackle" specific source code and examples are in the public domain.
  322. *
  323. * CHANGES
  324. * Oct 1994 : adapted from pigment by [CY]
  325. *
  326. ******************************************************************************/
  327. static DBL crackle (VECTOR EPoint)
  328. {
  329. int i;
  330. long thisseed;
  331. DBL sum, minsum, minsum2, tf;
  332. VECTOR sv, tv, dv, t1, add;
  333. static int cvc;
  334. static long lastseed = 0x80000000;
  335. static VECTOR cv[81];
  336. Assign_Vector(tv,EPoint);
  337. /*
  338. * Check to see if the input point is in the same unit cube as the last
  339. * call to this function, to use cache of cubelets for speed.
  340. */
  341. thisseed = PickInCube(tv, t1);
  342. if (thisseed != lastseed)
  343. {
  344. /*
  345. * No, not same unit cube. Calculate the random points for this new
  346. * cube and its 80 neighbours which differ in any axis by 1 or 2.
  347. * Why distance of 2? If there is 1 point in each cube, located
  348. * randomly, it is possible for the closest random point to be in the
  349. * cube 2 over, or the one two over and one up. It is NOT possible
  350. * for it to be two over and two up. Picture a 3x3x3 cube with 9 more
  351. * cubes glued onto each face.
  352. */
  353. /* Now store a points for this cube and each of the 80 neighbour cubes. */
  354. cvc = 0;
  355. for (add[X] = -2.0; add[X] < 2.5; add[X] +=1.0)
  356. {
  357. for (add[Y] = -2.0; add[Y] < 2.5; add[Y] += 1.0)
  358. {
  359. for (add[Z] = -2.0; add[Z] < 2.5; add[Z] += 1.0)
  360. {
  361. /* For each cubelet in a 5x5 cube. */
  362. if ((fabs(add[X])>1.5)+(fabs(add[Y])>1.5)+(fabs(add[Z])>1.5) <= 1.0)
  363. {
  364. /* Yes, it's within a 3d knight move away. */
  365. VAdd(sv, tv, add);
  366. PickInCube(sv, t1);
  367. cv[cvc][X] = t1[X];
  368. cv[cvc][Y] = t1[Y];
  369. cv[cvc][Z] = t1[Z];
  370. cvc++;
  371. }
  372. }
  373. }
  374. }
  375. lastseed = thisseed;
  376. }
  377. /*
  378. * Find the 2 points with the 2 shortest distances from the input point.
  379. * Loop invariant: minsum is shortest dist, minsum2 is 2nd shortest
  380. */
  381. /* Set up the loop so the invariant is true: minsum <= minsum2 */
  382. VSub(dv, cv[0], tv); minsum = VSumSqr(dv);
  383. VSub(dv, cv[1], tv); minsum2 = VSumSqr(dv);
  384. if (minsum2 < minsum)
  385. {
  386. tf = minsum; minsum = minsum2; minsum2 = tf;
  387. }
  388. /* Loop for the 81 cubelets to find closest and 2nd closest. */
  389. for (i = 2; i < cvc; i++)
  390. {
  391. VSub(dv, cv[i], tv);
  392. sum = VSumSqr(dv);
  393. if (sum < minsum)
  394. {
  395. minsum2 = minsum;
  396. minsum = sum;
  397. }
  398. else
  399. {
  400. if (sum < minsum2)
  401. {
  402. minsum2 = sum;
  403. }
  404. }
  405. }
  406. /* Crackle value is absolute value of diff in dist to closest 2 points. */
  407. tf = sqrt(minsum2) - sqrt(minsum); /* minsum is known <= minsum2 */
  408. /*
  409. * Note that the theoretical range of this function is 0 to root 3.
  410. * In practice, it rarely exceeds 0.9, and only very rarely 1.0
  411. */
  412. return min(tf, 1.);
  413. }
  414. /*****************************************************************************
  415. *
  416. * FUNCTION
  417. *
  418. * gradient
  419. *
  420. * INPUT
  421. *
  422. * EPoint -- The point in 3d space at which the pattern
  423. * is evaluated.
  424. *
  425. * OUTPUT
  426. *
  427. * RETURNS
  428. *
  429. * DBL value in the range 0.0 to 1.0
  430. *
  431. * AUTHOR
  432. *
  433. * POV-Ray Team
  434. *
  435. * DESCRIPTION
  436. *
  437. * Gradient Pattern - gradient based on the fractional values of
  438. * x, y or z, based on whether or not the given directional vector is
  439. * a 1.0 or a 0.0.
  440. * The basic concept of this is from DBW Render, but Dave Wecker's
  441. * only supports simple Y axis gradients.
  442. *
  443. * CHANGES
  444. * Oct 1994 : adapted from pigment by [CY]
  445. *
  446. ******************************************************************************/
  447. static DBL gradient (VECTOR EPoint, TPATTERN *TPat)
  448. {
  449. register int i;
  450. register DBL temp;
  451. DBL value = 0.0;
  452. for (i=X; i<=Z; i++)
  453. {
  454. if (TPat->Vals.Gradient[i] != 0.0)
  455. {
  456. temp = fabs(EPoint[i]);
  457. value += fmod(temp,1.0);
  458. }
  459. }
  460. /* Clamp to 1.0. */
  461. value = ((value > 1.0) ? fmod(value, 1.0) : value);
  462. return(value);
  463. }
  464. /*****************************************************************************
  465. *
  466. * FUNCTION
  467. *
  468. * granite
  469. *
  470. * INPUT
  471. *
  472. * EPoint -- The point in 3d space at which the pattern
  473. * is evaluated.
  474. *
  475. * OUTPUT
  476. *
  477. * RETURNS
  478. *
  479. * DBL value in the range 0.0 to 1.0
  480. *
  481. * AUTHOR
  482. *
  483. * POV-Ray Team
  484. *
  485. * DESCRIPTION
  486. *
  487. * Granite - kind of a union of the "spotted" and the "dented" textures,
  488. * using a 1/f fractal noise function for color values. Typically used
  489. * with small scaling values. Should work with colour maps for pink granite.
  490. *
  491. * CHANGES
  492. * Oct 1994 : adapted from pigment by [CY]
  493. *
  494. ******************************************************************************/
  495. static DBL granite (VECTOR EPoint)
  496. {
  497. register int i;
  498. register DBL temp, noise = 0.0, freq = 1.0;
  499. VECTOR tv1,tv2;
  500. VScale(tv1,EPoint,4.0);
  501. for (i = 0; i < 6 ; freq *= 2.0, i++)
  502. {
  503. VScale(tv2,tv1,freq);
  504. temp = 0.5 - Noise (tv2);
  505. temp = fabs(temp);
  506. noise += temp / freq;
  507. }
  508. return(noise);
  509. }
  510. /*****************************************************************************
  511. *
  512. * FUNCTION
  513. *
  514. * leopard
  515. *
  516. * INPUT
  517. *
  518. * EPoint -- The point in 3d space at which the pattern
  519. * is evaluated.
  520. *
  521. * OUTPUT
  522. *
  523. * RETURNS
  524. *
  525. * DBL value in the range 0.0 to 1.0
  526. *
  527. * AUTHOR
  528. *
  529. * Scott Taylor
  530. *
  531. * DESCRIPTION
  532. *
  533. * CHANGES
  534. * Jul 1991 : Creation.
  535. * Oct 1994 : adapted from pigment by [CY]
  536. *
  537. ******************************************************************************/
  538. static DBL leopard (VECTOR EPoint)
  539. {
  540. register DBL value, temp1, temp2, temp3;
  541. /* This form didn't work with Zortech 386 compiler */
  542. /* value = Sqr((sin(x)+sin(y)+sin(z))/3); */
  543. /* So we break it down. */
  544. temp1 = sin(EPoint[X]);
  545. temp2 = sin(EPoint[Y]);
  546. temp3 = sin(EPoint[Z]);
  547. value = Sqr((temp1 + temp2 + temp3) / 3.0);
  548. return(value);
  549. }
  550. /*****************************************************************************
  551. *
  552. * FUNCTION
  553. *
  554. * mandel
  555. *
  556. * INPUT
  557. *
  558. * EPoint -- The point in 3d space at which the pattern
  559. * is evaluated.
  560. *
  561. * OUTPUT
  562. *
  563. * RETURNS
  564. *
  565. * DBL value in the range 0.0 to 1.0
  566. *
  567. * AUTHOR
  568. *
  569. * submitted by user, name lost (sorry)
  570. *
  571. * DESCRIPTION
  572. * The mandel pattern computes the standard Mandelbrot fractal pattern and
  573. * projects it onto the X-Y plane. It uses the X and Y coordinates to compute
  574. * the Mandelbrot set.
  575. *
  576. * CHANGES
  577. * Oct 1994 : adapted from pigment by [CY]
  578. *
  579. ******************************************************************************/
  580. static DBL mandel (VECTOR EPoint, TPATTERN *TPat)
  581. {
  582. int it_max, col;
  583. DBL a, b, cf, a2, b2, x, y;
  584. a = x = EPoint[X]; a2 = Sqr(a);
  585. b = y = EPoint[Y]; b2 = Sqr(b);
  586. it_max = TPat->Vals.Iterations;
  587. for (col = 0; col < it_max; col++)
  588. {
  589. b = 2.0 * a * b + y;
  590. a = a2 - b2 + x;
  591. a2 = Sqr(a);
  592. b2 = Sqr(b);
  593. if ((a2 + b2) > 4.0)
  594. {
  595. break;
  596. }
  597. }
  598. cf = (DBL)col / (DBL)it_max;
  599. return(cf);
  600. }
  601. /*****************************************************************************
  602. *
  603. * FUNCTION
  604. *
  605. * marble
  606. *
  607. * INPUT
  608. *
  609. * EPoint -- The point in 3d space at which the pattern
  610. * is evaluated.
  611. * TPat -- Texture pattern struct
  612. *
  613. * OUTPUT
  614. *
  615. * RETURNS
  616. *
  617. * DBL value in the range 0.0 to 1.0
  618. *
  619. * AUTHOR
  620. *
  621. * POV-Ray Team
  622. *
  623. * DESCRIPTION
  624. *
  625. * CHANGES
  626. * Oct 1994 : adapted from pigment by [CY]
  627. *
  628. ******************************************************************************/
  629. static DBL marble (VECTOR EPoint, TPATTERN *TPat)
  630. {
  631. register DBL turb_val;
  632. TURB *Turb;
  633. if ((Turb=Search_For_Turb(TPat->Warps)) != NULL)
  634. {
  635. turb_val = Turb->Turbulence[X] * Turbulence(EPoint,Turb);
  636. }
  637. else
  638. {
  639. turb_val = 0.0;
  640. }
  641. return(EPoint[X] + turb_val);
  642. }
  643. /*****************************************************************************
  644. *
  645. * FUNCTION
  646. *
  647. * onion
  648. *
  649. * INPUT
  650. *
  651. * EPoint -- The point in 3d space at which the pattern
  652. * is evaluated.
  653. *
  654. * OUTPUT
  655. *
  656. * RETURNS
  657. *
  658. * DBL value in the range 0.0 to 1.0
  659. *
  660. * AUTHOR
  661. *
  662. * Scott Taylor
  663. *
  664. * DESCRIPTION
  665. *
  666. * CHANGES
  667. * Jul 1991 : Creation.
  668. * Oct 1994 : adapted from pigment by [CY]
  669. *
  670. ******************************************************************************/
  671. static DBL onion (VECTOR EPoint)
  672. {
  673. /* The variable noise is not used as noise in this function */
  674. register DBL noise;
  675. /*
  676. This ramp goes 0-1,1-0,0-1,1-0...
  677. noise = (fmod(sqrt(Sqr(x)+Sqr(y)+Sqr(z)),2.0)-1.0);
  678. if (noise<0.0) {noise = 0.0-noise;}
  679. */
  680. /* This ramp goes 0-1, 0-1, 0-1, 0-1 ... */
  681. noise = (fmod(sqrt(Sqr(EPoint[X])+Sqr(EPoint[Y])+Sqr(EPoint[Z])), 1.0));
  682. return(noise);
  683. }
  684. /*****************************************************************************
  685. *
  686. * FUNCTION
  687. *
  688. * radial
  689. *
  690. * INPUT
  691. *
  692. * EPoint -- The point in 3d space at which the pattern
  693. * is evaluated.
  694. *
  695. * OUTPUT
  696. *
  697. * RETURNS
  698. *
  699. * DBL value in the range 0.0 to 1.0
  700. *
  701. * AUTHOR
  702. *
  703. * Chris Young -- new in vers 2.0
  704. *
  705. * DESCRIPTION
  706. *
  707. * CHANGES
  708. * Oct 1994 : adapted from pigment by [CY]
  709. *
  710. ******************************************************************************/
  711. static DBL radial (VECTOR EPoint)
  712. {
  713. register DBL value;
  714. if ((fabs(EPoint[X])<0.001) && (fabs(EPoint[Z])<0.001))
  715. {
  716. value = 0.25;
  717. }
  718. else
  719. {
  720. value = 0.25 + (atan2(EPoint[X],EPoint[Z]) + M_PI) / TWO_M_PI;
  721. }
  722. return(value);
  723. }
  724. /*****************************************************************************
  725. *
  726. * FUNCTION
  727. *
  728. * spiral1
  729. *
  730. * INPUT
  731. *
  732. * EPoint -- The point in 3d space at which the pattern
  733. * is evaluated.
  734. * TPat -- Texture pattern struct
  735. *
  736. * OUTPUT
  737. *
  738. * RETURNS
  739. *
  740. * DBL value in the range 0.0 to 1.0
  741. *
  742. * AUTHOR
  743. *
  744. * Dieter Bayer
  745. *
  746. * DESCRIPTION
  747. * Spiral whirles around z-axis.
  748. * The number of "arms" is defined in the TPat.
  749. *
  750. * CHANGES
  751. * Aug 1994 : Creation.
  752. * Oct 1994 : adapted from pigment by [CY]
  753. *
  754. ******************************************************************************/
  755. static DBL spiral1(VECTOR EPoint, TPATTERN *TPat)
  756. {
  757. DBL rad, phi, turb_val;
  758. DBL x = EPoint[X];
  759. DBL y = EPoint[Y];
  760. DBL z = EPoint[Z];
  761. TURB *Turb;
  762. if ((Turb=Search_For_Turb(TPat->Warps)) != NULL)
  763. {
  764. turb_val = Turb->Turbulence[X] * Turbulence(EPoint,Turb);
  765. }
  766. else
  767. {
  768. turb_val = 0.0;
  769. }
  770. /* Get distance from z-axis. */
  771. rad = sqrt(x * x + y * y);
  772. /* Get angle in x,y-plane (0...2 PI). */
  773. if (rad == 0.0)
  774. {
  775. phi = 0.0;
  776. }
  777. else
  778. {
  779. if (x < 0.0)
  780. {
  781. phi = 3.0 * M_PI_2 - asin(y / rad);
  782. }
  783. else
  784. {
  785. phi = M_PI_2 + asin(y / rad);
  786. }
  787. }
  788. return(z + rad + (DBL)TPat->Vals.Arms * phi / TWO_M_PI + turb_val);
  789. }
  790. /*****************************************************************************
  791. *
  792. * FUNCTION
  793. *
  794. * spiral2
  795. *
  796. * INPUT
  797. *
  798. * EPoint -- The point in 3d space at which the pattern
  799. * is evaluated.
  800. * TPat -- Texture pattern struct
  801. *
  802. * OUTPUT
  803. *
  804. * RETURNS
  805. *
  806. * DBL value in the range 0.0 to 1.0
  807. *
  808. * AUTHOR
  809. *
  810. * Dieter Bayer
  811. *
  812. * DESCRIPTION
  813. * Spiral whirles around z-axis.
  814. * The number of "arms" is defined in the TPat.
  815. *
  816. * CHANGES
  817. * Aug 1994 : Creation.
  818. * Oct 1994 : adapted from pigment by [CY]
  819. *
  820. ******************************************************************************/
  821. static DBL spiral2(VECTOR EPoint, TPATTERN *TPat)
  822. {
  823. DBL rad, phi, turb_val;
  824. DBL x = EPoint[X];
  825. DBL y = EPoint[Y];
  826. DBL z = EPoint[Z];
  827. TURB *Turb;
  828. if ((Turb=Search_For_Turb(TPat->Warps)) != NULL)
  829. {
  830. turb_val = Turb->Turbulence[X] * Turbulence(EPoint,Turb);
  831. }
  832. else
  833. {
  834. turb_val = 0.0;
  835. }
  836. /* Get distance from z-axis. */
  837. rad = sqrt(x * x + y * y);
  838. /* Get angle in x,y-plane (0...2 PI) */
  839. if (rad == 0.0)
  840. {
  841. phi = 0.0;
  842. }
  843. else
  844. {
  845. if (x < 0.0)
  846. {
  847. phi = 3.0 * M_PI_2 - asin(y / rad);
  848. }
  849. else
  850. {
  851. phi = M_PI_2 + asin(y / rad);
  852. }
  853. }
  854. turb_val = Triangle_Wave(z + rad + (DBL)TPat->Vals.Arms * phi / TWO_M_PI +
  855. turb_val);
  856. return(Triangle_Wave(rad) + turb_val);
  857. }
  858. /*****************************************************************************
  859. *
  860. * FUNCTION
  861. *
  862. * wood
  863. *
  864. * INPUT
  865. *
  866. * EPoint -- The point in 3d space at which the pattern
  867. * is evaluated.
  868. * TPat -- Texture pattern struct
  869. *
  870. * OUTPUT
  871. *
  872. * RETURNS
  873. *
  874. * DBL value in the range 0.0 to 1.0
  875. *
  876. * AUTHOR
  877. *
  878. * POV-Ray Team
  879. *
  880. * DESCRIPTION
  881. *
  882. * CHANGES
  883. * Oct 1994 : adapted from pigment by [CY]
  884. *
  885. ******************************************************************************/
  886. static DBL wood (VECTOR EPoint, TPATTERN *TPat)
  887. {
  888. register DBL length;
  889. VECTOR WoodTurbulence;
  890. VECTOR point;
  891. DBL x=EPoint[X];
  892. DBL y=EPoint[Y];
  893. TURB *Turb;
  894. if ((Turb=Search_For_Turb(TPat->Warps)) != NULL)
  895. {
  896. DTurbulence (WoodTurbulence, EPoint,Turb);
  897. point[X] = cycloidal((x + WoodTurbulence[X]) * Turb->Turbulence[X]);
  898. point[Y] = cycloidal((y + WoodTurbulence[Y]) * Turb->Turbulence[Y]);
  899. }
  900. else
  901. {
  902. point[X] = 0.0;
  903. point[Y] = 0.0;
  904. }
  905. point[Z] = 0.0;
  906. point[X] += x;
  907. point[Y] += y;
  908. /* point[Z] += z; Deleted per David Buck -- BP 7/91 */
  909. VLength (length, point);
  910. return(length);
  911. }
  912. /*****************************************************************************
  913. *
  914. * FUNCTION
  915. *
  916. * hexagon
  917. *
  918. * INPUT
  919. *
  920. * EPoint -- The point in 3d space at which the pattern
  921. * is evaluated.
  922. *
  923. * OUTPUT
  924. *
  925. * RETURNS
  926. *
  927. * DBL value exactly 0.0, 1.0 or 2.0
  928. *
  929. * AUTHOR
  930. *
  931. * Ernest MacDougal Campbell III
  932. *
  933. * DESCRIPTION
  934. *
  935. * TriHex pattern -- Ernest MacDougal Campbell III (EMC3) 11/23/92
  936. *
  937. * Creates a hexagon pattern in the XZ plane.
  938. *
  939. * This algorithm is hard to explain. First it scales the point to make
  940. * a few of the later calculations easier, then maps some points to be
  941. * closer to the Origin. A small area in the first quadrant is subdivided
  942. * into a 6 x 6 grid. The position of the point mapped into that grid
  943. * determines its color. For some points, just the grid location is enough,
  944. * but for others, we have to calculate which half of the block it's in
  945. * (this is where the atan2() function comes in handy).
  946. *
  947. * CHANGES
  948. * Nov 1992 : Creation.
  949. * Oct 1994 : adapted from pigment by [CY]
  950. *
  951. ******************************************************************************/
  952. #define xfactor 0.5; /* each triangle is split in half for the grid */
  953. #define zfactor 0.866025404; /* sqrt(3)/2 -- Height of an equilateral triangle */
  954. static DBL hexagon (VECTOR EPoint)
  955. {
  956. int xm, zm;
  957. int brkindx;
  958. DBL xs, zs, xl, zl, value = 0.0;
  959. DBL x=EPoint[X];
  960. DBL z=EPoint[Z];
  961. /* Keep all numbers positive. Also, if z is negative, map it in such a
  962. * way as to avoid mirroring across the x-axis. The value 5.196152424
  963. * is (sqrt(3)/2) * 6 (because the grid is 6 blocks high)
  964. */
  965. x = fabs(x);
  966. /* Avoid mirroring across x-axis. */
  967. z = z < 0.0 ? 5.196152424 - fabs(z) : z;
  968. /* Scale point to make calcs easier. */
  969. xs = x/xfactor;
  970. zs = z/zfactor;
  971. /* Map points into the 6 x 6 grid where the basic formula works. */
  972. xs -= floor(xs/6.0) * 6.0;
  973. zs -= floor(zs/6.0) * 6.0;
  974. /* Get a block in the 6 x 6 grid. */
  975. xm = (int) FLOOR(xs) % 6;
  976. zm = (int) FLOOR(zs) % 6;
  977. switch (xm)
  978. {
  979. /* These are easy cases: Color depends only on xm and zm. */
  980. case 0:
  981. case 5:
  982. switch (zm)
  983. {
  984. case 0:
  985. case 5: value = 0; break;
  986. case 1:
  987. case 2: value = 1; break;
  988. case 3:
  989. case 4: value = 2; break;
  990. }
  991. break;
  992. case 2:
  993. case 3:
  994. switch (zm)
  995. {
  996. case 0:
  997. case 1: value = 2; break;
  998. case 2:
  999. case 3: value = 0; break;
  1000. case 4:
  1001. case 5: value = 1; break;
  1002. }
  1003. break;
  1004. /* These cases are harder. These blocks are divided diagonally
  1005. * by the angled edges of the hexagons. Some slope positive, and
  1006. * others negative. We flip the x value of the negatively sloped
  1007. * pieces. Then we check to see if the point in question falls
  1008. * in the upper or lower half of the block. That info, plus the
  1009. * z status of the block determines the color.
  1010. */
  1011. case 1:
  1012. case 4:
  1013. /* Map the point into the block at the origin. */
  1014. xl = xs-xm;
  1015. zl = zs-zm;
  1016. /* These blocks have negative slopes so we flip it horizontally. */
  1017. if (((xm + zm) % 2) == 1)
  1018. {
  1019. xl = 1.0 - xl;
  1020. }
  1021. /* Avoid a divide-by-zero error. */
  1022. if (xl == 0.0)
  1023. {
  1024. xl = 0.0001;
  1025. }
  1026. /* Is the angle less-than or greater-than 45 degrees? */
  1027. brkindx = (zl / xl) < 1.0;
  1028. /* was...
  1029. * brkindx = (atan2(zl,xl) < (45 * M_PI_180));
  1030. * ...but because of the mapping, it's easier and cheaper,
  1031. * CPU-wise, to just use a good ol' slope.
  1032. */
  1033. switch (brkindx)
  1034. {
  1035. case TRUE:
  1036. switch (zm)
  1037. {
  1038. case 0:
  1039. case 3: value = 0; break;
  1040. case 2:
  1041. case 5: value = 1; break;
  1042. case 1:
  1043. case 4: value = 2; break;
  1044. }
  1045. break;
  1046. case FALSE:
  1047. switch (zm)
  1048. {
  1049. case 0:
  1050. case 3: value = 2; break;
  1051. case 2:
  1052. case 5: value = 0; break;
  1053. case 1:
  1054. case 4: value = 1; break;
  1055. }
  1056. break;
  1057. }
  1058. }
  1059. value = fmod(value, 3.0);
  1060. return(value);
  1061. }
  1062. /* In addition to clipping the value to
  1063. lie between 0.0 to 1.0, it also fudges 1.0-value.
  1064. */
  1065. #define CLIP_DENSITY(r) if((r)<0.0){(r)=1.0;}else{if((r)>1.0){(r)=0.0;}else{(r)=1.0-(r);}}
  1066. static DBL planar_pattern (VECTOR EPoint)
  1067. {
  1068. register DBL value;
  1069. value = fabs(EPoint[Y]);
  1070. CLIP_DENSITY(value);
  1071. return(value);
  1072. }
  1073. static DBL spherical (VECTOR EPoint)
  1074. {
  1075. register DBL value;
  1076. VLength(value, EPoint);
  1077. CLIP_DENSITY(value);
  1078. return(value);
  1079. }
  1080. static DBL boxed (VECTOR EPoint)
  1081. {
  1082. register DBL value;
  1083. value = max(fabs(EPoint[X]), max(fabs(EPoint[Y]), fabs(EPoint[Z])));
  1084. CLIP_DENSITY(value);
  1085. return(value);
  1086. }
  1087. static DBL cylindrical (VECTOR EPoint)
  1088. {
  1089. register DBL value;
  1090. value = sqrt(Sqr(EPoint[X]) + Sqr(EPoint[Z]));
  1091. CLIP_DENSITY(value);
  1092. return(value);
  1093. }
  1094. /*****************************************************************************
  1095. *
  1096. * FUNCTION
  1097. *
  1098. * PickInCube(tv, p1)
  1099. *
  1100. * INPUT
  1101. *
  1102. * ?
  1103. *
  1104. * OUTPUT
  1105. *
  1106. * RETURNS
  1107. *
  1108. * long integer hash function used, to speed up cacheing.
  1109. *
  1110. * AUTHOR
  1111. *
  1112. * Jim McElhiney
  1113. *
  1114. * DESCRIPTION
  1115. *
  1116. * A subroutine to go with crackle.
  1117. *
  1118. * Pick a random point in the same unit-sized cube as tv, in a
  1119. * predictable way, so that when called again with another point in
  1120. * the same unit cube, p1 is picked to be the same.
  1121. *
  1122. * CHANGES
  1123. *
  1124. ******************************************************************************/
  1125. static long PickInCube(VECTOR tv, VECTOR p1)
  1126. {
  1127. int seed, temp;
  1128. VECTOR flo;
  1129. /*
  1130. * This uses floor() not FLOOR, so it will not be a mirror
  1131. * image about zero in the range -1.0 to 1.0. The viewer
  1132. * won't see an artefact around the origin.
  1133. */
  1134. flo[X] = floor(tv[X] - EPSILON);
  1135. flo[Y] = floor(tv[Y] - EPSILON);
  1136. flo[Z] = floor(tv[Z] - EPSILON);
  1137. seed = Hash3d((int)flo[X], (int)flo[Y], (int)flo[Z]);
  1138. temp = POV_GET_OLD_RAND(); /* save current seed */
  1139. POV_SRAND(seed);
  1140. p1[X] = flo[X] + FRAND();
  1141. p1[Y] = flo[Y] + FRAND();
  1142. p1[Z] = flo[Z] + FRAND();
  1143. POV_SRAND(temp); /* restore */
  1144. return((long)seed);
  1145. }
  1146. /*****************************************************************************
  1147. *
  1148. * FUNCTION
  1149. *
  1150. * Evaluate_Pattern
  1151. *
  1152. * INPUT
  1153. *
  1154. * EPoint -- The point in 3d space at which the pattern
  1155. * is evaluated.
  1156. * TPat -- Texture pattern struct
  1157. *
  1158. * OUTPUT
  1159. *
  1160. * RETURNS
  1161. *
  1162. * DBL result usual 0.0 to 1.0 but may be 2.0 in hexagon
  1163. *
  1164. * AUTHOR
  1165. *
  1166. * adapted from Add_Pigment by Chris Young
  1167. *
  1168. * DESCRIPTION
  1169. *
  1170. * CHANGES
  1171. *
  1172. ******************************************************************************/
  1173. DBL Evaluate_TPat (TPATTERN *TPat, VECTOR EPoint)
  1174. {
  1175. DBL value = 0.0;
  1176. VECTOR TPoint;
  1177. Warp_EPoint (TPoint, EPoint, TPat);
  1178. switch (TPat->Type)
  1179. {
  1180. case AGATE_PATTERN: value = agate (TPoint, TPat); break;
  1181. case BOZO_PATTERN:
  1182. case SPOTTED_PATTERN:
  1183. case BUMPS_PATTERN: value = Noise (TPoint); break;
  1184. case BRICK_PATTERN: value = brick (TPoint, TPat); break;
  1185. case CHECKER_PATTERN: value = checker (TPoint); break;
  1186. case CRACKLE_PATTERN: value = crackle (TPoint); break;
  1187. case GRADIENT_PATTERN: value = gradient (TPoint, TPat); break;
  1188. case GRANITE_PATTERN: value = granite (TPoint); break;
  1189. case HEXAGON_PATTERN: value = hexagon (TPoint); break;
  1190. case LEOPARD_PATTERN: value = leopard (TPoint); break;
  1191. case MANDEL_PATTERN: value = mandel (TPoint, TPat); break;
  1192. case MARBLE_PATTERN: value = marble (TPoint, TPat); break;
  1193. case ONION_PATTERN: value = onion (TPoint); break;
  1194. case RADIAL_PATTERN: value = radial (TPoint); break;
  1195. case SPIRAL1_PATTERN: value = spiral1 (TPoint, TPat); break;
  1196. case SPIRAL2_PATTERN: value = spiral2 (TPoint, TPat); break;
  1197. case WOOD_PATTERN: value = wood (TPoint, TPat); break;
  1198. case WAVES_PATTERN: value = waves_pigm (TPoint, TPat); break;
  1199. case RIPPLES_PATTERN: value = ripples_pigm (TPoint, TPat); break;
  1200. case WRINKLES_PATTERN: value = wrinkles_pigm (TPoint); break;
  1201. case DENTS_PATTERN: value = dents_pigm (TPoint); break;
  1202. case QUILTED_PATTERN: value = quilted_pigm (TPoint, TPat); break;
  1203. case PLANAR_PATTERN: value = planar_pattern (TPoint); break;
  1204. case BOXED_PATTERN: value = boxed (TPoint); break;
  1205. case SPHERICAL_PATTERN: value = spherical (TPoint); break;
  1206. case CYLINDRICAL_PATTERN: value = cylindrical (TPoint); break;
  1207. case DENSITY_FILE_PATTERN:value = density_file (TPoint, TPat); break;
  1208. default: Error("Problem in Evaluate_TPat.");
  1209. }
  1210. if (TPat->Frequency !=0.0)
  1211. {
  1212. value = fmod(value * TPat->Frequency + TPat->Phase, 1.00001);
  1213. }
  1214. /* allow negative Frequency */
  1215. if (value < 0.0)
  1216. {
  1217. value -= floor(value);
  1218. }
  1219. switch (TPat->Wave_Type)
  1220. {
  1221. case RAMP_WAVE:
  1222. break;
  1223. case SINE_WAVE:
  1224. value = (1.0+cycloidal(value))*0.5;
  1225. break;
  1226. case TRIANGLE_WAVE:
  1227. value = Triangle_Wave(value);
  1228. break;
  1229. case SCALLOP_WAVE:
  1230. value = fabs(cycloidal(value*0.5));
  1231. break;
  1232. case CUBIC_WAVE:
  1233. value = Sqr(value)*((-2.0 * value) + 3.0);
  1234. break;
  1235. case POLY_WAVE:
  1236. value = pow(value, TPat->Exponent);
  1237. break;
  1238. default: Error("Unknown Wave Type %d.",TPat->Wave_Type);
  1239. }
  1240. return(value);
  1241. }
  1242. /*****************************************************************************
  1243. *
  1244. * FUNCTION
  1245. *
  1246. * INPUT
  1247. *
  1248. * OUTPUT
  1249. *
  1250. * RETURNS
  1251. *
  1252. * AUTHOR
  1253. *
  1254. * DESCRIPTION
  1255. *
  1256. * CHANGES
  1257. *
  1258. ******************************************************************************/
  1259. void Init_TPat_Fields (TPATTERN *Tpat)
  1260. {
  1261. Tpat->Type = NO_PATTERN;
  1262. Tpat->Wave_Type = RAMP_WAVE;
  1263. Tpat->Flags = NO_FLAGS;
  1264. Tpat->References = 1;
  1265. Tpat->Exponent = 1.0;
  1266. Tpat->Frequency = 1.0;
  1267. Tpat->Phase = 0.0;
  1268. Tpat->Warps = NULL;
  1269. Tpat->Next = NULL;
  1270. Tpat->Blend_Map = NULL;
  1271. }
  1272. /*****************************************************************************
  1273. *
  1274. * FUNCTION
  1275. *
  1276. * INPUT
  1277. *
  1278. * OUTPUT
  1279. *
  1280. * RETURNS
  1281. *
  1282. * AUTHOR
  1283. *
  1284. * DESCRIPTION
  1285. *
  1286. * CHANGES
  1287. *
  1288. ******************************************************************************/
  1289. void Copy_TPat_Fields (TPATTERN *New, TPATTERN *Old)
  1290. {
  1291. *New = *Old;
  1292. /* Copy warp chain */
  1293. New->Warps = Copy_Warps(Old->Warps);
  1294. New->Blend_Map = Copy_Blend_Map(Old->Blend_Map);
  1295. /* Note, cannot copy Old->Next because we don't know what kind of
  1296. thing this is. It must be copied by Copy_Pigment, Copy_Tnormal etc.
  1297. */
  1298. if (Old->Type == BITMAP_PATTERN)
  1299. {
  1300. New->Vals.Image = Copy_Image(Old->Vals.Image);
  1301. }
  1302. if (Old->Type == DENSITY_FILE_PATTERN)
  1303. {
  1304. New->Vals.Density_File = Copy_Density_File(Old->Vals.Density_File);
  1305. }
  1306. }
  1307. /*****************************************************************************
  1308. *
  1309. * FUNCTION
  1310. *
  1311. * INPUT
  1312. *
  1313. * OUTPUT
  1314. *
  1315. * RETURNS
  1316. *
  1317. * AUTHOR
  1318. *
  1319. * DESCRIPTION
  1320. *
  1321. * CHANGES
  1322. *
  1323. ******************************************************************************/
  1324. void Destroy_TPat_Fields(TPATTERN *Tpat)
  1325. {
  1326. Destroy_Warps(Tpat->Warps);
  1327. Destroy_Blend_Map(Tpat->Blend_Map);
  1328. /* Note, cannot destroy Tpat->Next nor pattern itself because we don't
  1329. know what kind of thing this is. It must be destroied by Destroy_Pigment, etc.
  1330. */
  1331. if (Tpat->Type == BITMAP_PATTERN)
  1332. {
  1333. Destroy_Image(Tpat->Vals.Image);
  1334. }
  1335. if (Tpat->Type == DENSITY_FILE_PATTERN)
  1336. {
  1337. Destroy_Density_File(Tpat->Vals.Density_File);
  1338. }
  1339. }
  1340. /*****************************************************************************
  1341. *
  1342. * FUNCTION
  1343. *
  1344. * INPUT
  1345. *
  1346. * OUTPUT
  1347. *
  1348. * RETURNS
  1349. *
  1350. * AUTHOR
  1351. *
  1352. * DESCRIPTION
  1353. *
  1354. * CHANGES
  1355. *
  1356. ******************************************************************************/
  1357. TURB *Create_Turb()
  1358. {
  1359. TURB *New;
  1360. New = (TURB *)POV_MALLOC(sizeof(TURB),"turbulence struct");
  1361. Make_Vector(New->Turbulence, 0.0, 0.0, 0.0);
  1362. New->Octaves = 6;
  1363. New->Omega = 0.5;
  1364. New->Lambda = 2.0;
  1365. return(New);
  1366. }
  1367. /*****************************************************************************
  1368. *
  1369. * FUNCTION
  1370. *
  1371. * INPUT
  1372. *
  1373. * OUTPUT
  1374. *
  1375. * RETURNS
  1376. *
  1377. * AUTHOR
  1378. *
  1379. * DESCRIPTION
  1380. *
  1381. * CHANGES
  1382. *
  1383. ******************************************************************************/
  1384. #if 0 /* Unused function [AED] */
  1385. static TURB *Copy_Turb(TURB *Old)
  1386. {
  1387. TURB *New;
  1388. if (Old != NULL)
  1389. {
  1390. New = Create_Turb();
  1391. *New = *Old;
  1392. }
  1393. else
  1394. {
  1395. New=NULL;
  1396. }
  1397. return(New);
  1398. }
  1399. #endif
  1400. /*****************************************************************************
  1401. *
  1402. * FUNCTION
  1403. *
  1404. * Translate_Tpattern
  1405. *
  1406. * INPUT
  1407. *
  1408. * OUTPUT
  1409. *
  1410. * RETURNS
  1411. *
  1412. * AUTHOR
  1413. *
  1414. * POV-Ray Team
  1415. *
  1416. * DESCRIPTION
  1417. *
  1418. * CHANGES
  1419. *
  1420. ******************************************************************************/
  1421. void Translate_Tpattern(TPATTERN *Tpattern,VECTOR Vector)
  1422. {
  1423. TRANSFORM Trans;
  1424. if (Tpattern != NULL)
  1425. {
  1426. Compute_Translation_Transform (&Trans, Vector);
  1427. Transform_Tpattern (Tpattern, &Trans);
  1428. }
  1429. }
  1430. /*****************************************************************************
  1431. *
  1432. * FUNCTION
  1433. *
  1434. * Rotate_Tpattern
  1435. *
  1436. * INPUT
  1437. *
  1438. * OUTPUT
  1439. *
  1440. * RETURNS
  1441. *
  1442. * AUTHOR
  1443. *
  1444. * POV-Ray Team
  1445. *
  1446. * DESCRIPTION
  1447. *
  1448. * CHANGES
  1449. *
  1450. ******************************************************************************/
  1451. void Rotate_Tpattern(TPATTERN *Tpattern,VECTOR Vector)
  1452. {
  1453. TRANSFORM Trans;
  1454. if (Tpattern != NULL)
  1455. {
  1456. Compute_Rotation_Transform (&Trans, Vector);
  1457. Transform_Tpattern (Tpattern, &Trans);
  1458. }
  1459. }
  1460. /*****************************************************************************
  1461. *
  1462. * FUNCTION
  1463. *
  1464. * Scale_Tpattern
  1465. *
  1466. * INPUT
  1467. *
  1468. * OUTPUT
  1469. *
  1470. * RETURNS
  1471. *
  1472. * AUTHOR
  1473. *
  1474. * POV-Ray Team
  1475. *
  1476. * DESCRIPTION
  1477. *
  1478. * CHANGES
  1479. *
  1480. ******************************************************************************/
  1481. void Scale_Tpattern(TPATTERN *Tpattern,VECTOR Vector)
  1482. {
  1483. TRANSFORM Trans;
  1484. if (Tpattern != NULL)
  1485. {
  1486. Compute_Scaling_Transform (&Trans, Vector);
  1487. Transform_Tpattern (Tpattern, &Trans);
  1488. }
  1489. }
  1490. /*****************************************************************************
  1491. *
  1492. * FUNCTION
  1493. *
  1494. * Transform_Tpattern
  1495. *
  1496. * INPUT
  1497. *
  1498. * OUTPUT
  1499. *
  1500. * RETURNS
  1501. *
  1502. * AUTHOR
  1503. *
  1504. * POV-Ray Team
  1505. *
  1506. * DESCRIPTION
  1507. *
  1508. * CHANGES
  1509. *
  1510. ******************************************************************************/
  1511. void Transform_Tpattern(TPATTERN *Tpattern,TRANSFORM *Trans)
  1512. {
  1513. WARP *Temp;
  1514. if (Tpattern != NULL)
  1515. {
  1516. if (Tpattern->Warps == NULL)
  1517. {
  1518. Tpattern->Warps=Create_Warp(TRANSFORM_WARP);
  1519. }
  1520. else
  1521. {
  1522. if (Tpattern->Warps->Warp_Type != TRANSFORM_WARP)
  1523. {
  1524. Temp=Tpattern->Warps;
  1525. Tpattern->Warps=Create_Warp(TRANSFORM_WARP);
  1526. Tpattern->Warps->Next_Warp=Temp;
  1527. }
  1528. }
  1529. Compose_Transforms (&( ((TRANS *)(Tpattern->Warps))->Trans), Trans);
  1530. }
  1531. }
  1532. /*****************************************************************************
  1533. *
  1534. * FUNCTION
  1535. *
  1536. * ripples_pigm
  1537. *
  1538. * INPUT
  1539. *
  1540. * EPoint -- The point in 3d space at which the pattern
  1541. * is evaluated.
  1542. * TPat -- Texture pattern struct
  1543. *
  1544. * OUTPUT
  1545. *
  1546. * RETURNS
  1547. *
  1548. * DBL value in the range 0.0 to 1.0
  1549. *
  1550. * AUTHOR
  1551. *
  1552. * POV-Ray Team
  1553. *
  1554. * DESCRIPTION : Note this pattern is only used for pigments and textures.
  1555. * Normals have a specialized pattern for this.
  1556. *
  1557. * CHANGES
  1558. * Nov 1994 : adapted from normal by [CY]
  1559. *
  1560. ******************************************************************************/
  1561. static DBL ripples_pigm (VECTOR EPoint, TPATTERN *TPat)
  1562. {
  1563. register unsigned int i;
  1564. register DBL length, index;
  1565. DBL scalar =0.0;
  1566. VECTOR point;
  1567. for (i = 0 ; i < Number_Of_Waves ; i++)
  1568. {
  1569. VSub (point, EPoint, Wave_Sources[i]);
  1570. VLength (length, point);
  1571. if (length == 0.0)
  1572. length = 1.0;
  1573. index = length * TPat->Frequency + TPat->Phase;
  1574. scalar += cycloidal(index);
  1575. }
  1576. scalar = 0.5*(1.0+(scalar / (DBL)Number_Of_Waves));
  1577. return(scalar);
  1578. }
  1579. /*****************************************************************************
  1580. *
  1581. * FUNCTION
  1582. *
  1583. * waves_pigm
  1584. *
  1585. * INPUT
  1586. *
  1587. * EPoint -- The point in 3d space at which the pattern
  1588. * is evaluated.
  1589. * TPat -- Texture pattern struct
  1590. *
  1591. * OUTPUT
  1592. *
  1593. * RETURNS
  1594. *
  1595. * DBL value in the range 0.0 to 1.0
  1596. *
  1597. * AUTHOR
  1598. *
  1599. * POV-Ray Team
  1600. *
  1601. * DESCRIPTION : Note this pattern is only used for pigments and textures.
  1602. * Normals have a specialized pattern for this.
  1603. *
  1604. * CHANGES
  1605. * Nov 1994 : adapted from normal by [CY]
  1606. *
  1607. ******************************************************************************/
  1608. static DBL waves_pigm (VECTOR EPoint, TPATTERN *TPat)
  1609. {
  1610. register unsigned int i;
  1611. register DBL length, index;
  1612. DBL scalar = 0.0;
  1613. VECTOR point;
  1614. for (i = 0 ; i < Number_Of_Waves ; i++)
  1615. {
  1616. VSub (point, EPoint, Wave_Sources[i]);
  1617. VLength (length, point);
  1618. if (length == 0.0)
  1619. {
  1620. length = 1.0;
  1621. }
  1622. index = length * TPat->Frequency * frequency[i] + TPat->Phase;
  1623. scalar += cycloidal(index)/frequency[i];
  1624. }
  1625. scalar = 0.2*(2.5+(scalar / (DBL)Number_Of_Waves));
  1626. return(scalar);
  1627. }
  1628. /*****************************************************************************
  1629. *
  1630. * FUNCTION
  1631. *
  1632. * dents_pigm
  1633. *
  1634. * INPUT
  1635. *
  1636. * EPoint -- The point in 3d space at which the pattern
  1637. * is evaluated.
  1638. *
  1639. * OUTPUT
  1640. *
  1641. * RETURNS
  1642. *
  1643. * DBL value in the range 0.0 to 1.0
  1644. *
  1645. * AUTHOR
  1646. *
  1647. * POV-Ray Team
  1648. *
  1649. * DESCRIPTION : Note this pattern is only used for pigments and textures.
  1650. * Normals have a specialized pattern for this.
  1651. *
  1652. * CHANGES
  1653. * Nov 1994 : adapted from normal by [CY]
  1654. *
  1655. ******************************************************************************/
  1656. static DBL dents_pigm (VECTOR EPoint)
  1657. {
  1658. DBL noise;
  1659. noise = Noise (EPoint);
  1660. return(noise * noise * noise);
  1661. }
  1662. /*****************************************************************************
  1663. *
  1664. * FUNCTION
  1665. *
  1666. * wrinkles_pigm
  1667. *
  1668. * INPUT
  1669. *
  1670. * EPoint -- The point in 3d space at which the pattern
  1671. * is evaluated.
  1672. *
  1673. * OUTPUT
  1674. *
  1675. * RETURNS
  1676. *
  1677. * DBL value in the range 0.0 to 1.0
  1678. *
  1679. * AUTHOR
  1680. *
  1681. * POV-Ray Team
  1682. *
  1683. * DESCRIPTION : Note this pattern is only used for pigments and textures.
  1684. * Normals have a specialized pattern for this.
  1685. *
  1686. * CHANGES
  1687. * Nov 1994 : adapted from normal by [CY]
  1688. *
  1689. ******************************************************************************/
  1690. static DBL wrinkles_pigm (VECTOR EPoint)
  1691. {
  1692. register int i;
  1693. DBL lambda = 2.0;
  1694. DBL omega = 0.5;
  1695. DBL value;
  1696. VECTOR temp;
  1697. value = Noise(EPoint);
  1698. for (i = 1; i < 10; i++)
  1699. {
  1700. VScale(temp,EPoint,lambda);
  1701. value += omega * Noise(temp);
  1702. lambda *= 2.0;
  1703. omega *= 0.5;
  1704. }
  1705. return(value/2.0);
  1706. }
  1707. /*****************************************************************************
  1708. *
  1709. * FUNCTION
  1710. *
  1711. * quilted_pigm
  1712. *
  1713. * INPUT
  1714. *
  1715. * OUTPUT
  1716. *
  1717. * RETURNS
  1718. *
  1719. * AUTHOR
  1720. *
  1721. * Dan Farmer & Chris Young
  1722. *
  1723. * DESCRIPTION
  1724. *
  1725. * CHANGES
  1726. *
  1727. ******************************************************************************/
  1728. static DBL quilted_pigm (VECTOR EPoint, TPATTERN *TPat)
  1729. {
  1730. VECTOR value;
  1731. DBL t;
  1732. value[X] = EPoint[X]-FLOOR(EPoint[X])-0.5;
  1733. value[Y] = EPoint[Y]-FLOOR(EPoint[Y])-0.5;
  1734. value[Z] = EPoint[Z]-FLOOR(EPoint[Z])-0.5;
  1735. t = sqrt(value[X]*value[X]+value[Y]*value[Y]+value[Z]*value[Z]);
  1736. t = quilt_cubic(t, TPat->Vals.Quilted.Control0, TPat->Vals.Quilted.Control1);
  1737. value[X] *= t;
  1738. value[Y] *= t;
  1739. value[Z] *= t;
  1740. return((fabs(value[X])+fabs(value[Y])+fabs(value[Z]))/3.0);
  1741. }
  1742. /*****************************************************************************
  1743. *
  1744. * FUNCTION
  1745. *
  1746. * INPUT
  1747. *
  1748. * OUTPUT
  1749. *
  1750. * RETURNS
  1751. *
  1752. * AUTHOR
  1753. *
  1754. * DESCRIPTION
  1755. *
  1756. * CHANGES
  1757. *
  1758. ******************************************************************************/
  1759. #define INV_SQRT_3_4 1.154700538
  1760. DBL quilt_cubic(DBL t,DBL p1,DBL p2)
  1761. {
  1762. DBL it=(1-t);
  1763. DBL itsqrd=it*it;
  1764. /* DBL itcubed=it*itsqrd; */
  1765. DBL tsqrd=t*t;
  1766. DBL tcubed=t*tsqrd;
  1767. DBL val;
  1768. /* Originally coded as...
  1769. val= (DBL)(itcubed*n1+(tcubed)*n2+3*t*(itsqrd)*p1+3*(tsqrd)*(it)*p2);
  1770. re-written by CEY to optimise because n1=0 n2=1 always.
  1771. */
  1772. val = (tcubed + 3.0*t*itsqrd*p1 + 3.0*tsqrd*it*p2) * INV_SQRT_3_4;
  1773. return(val);
  1774. }
  1775. /*****************************************************************************
  1776. *
  1777. * FUNCTION
  1778. *
  1779. * INPUT
  1780. *
  1781. * OUTPUT
  1782. *
  1783. * RETURNS
  1784. *
  1785. * AUTHOR
  1786. *
  1787. * DESCRIPTION
  1788. *
  1789. * CHANGES
  1790. *
  1791. ******************************************************************************/
  1792. void Search_Blend_Map (DBL value,BLEND_MAP *Blend_Map,BLEND_MAP_ENTRY **Prev,BLEND_MAP_ENTRY **Cur)
  1793. {
  1794. BLEND_MAP_ENTRY *P, *C;
  1795. int Max_Ent=Blend_Map->Number_Of_Entries-1;
  1796. /* if greater than last, use last. */
  1797. if (value >= Blend_Map->Blend_Map_Entries[Max_Ent].value)
  1798. {
  1799. P = C = &(Blend_Map->Blend_Map_Entries[Max_Ent]);
  1800. }
  1801. else
  1802. {
  1803. P = C = &(Blend_Map->Blend_Map_Entries[0]);
  1804. while (value > C->value)
  1805. {
  1806. P = C++;
  1807. }
  1808. }
  1809. if (value == C->value)
  1810. {
  1811. P = C;
  1812. }
  1813. *Prev = P;
  1814. *Cur = C;
  1815. }
  1816. /*****************************************************************************
  1817. *
  1818. * FUNCTION
  1819. *
  1820. * INPUT
  1821. *
  1822. * OUTPUT
  1823. *
  1824. * RETURNS
  1825. *
  1826. * AUTHOR
  1827. *
  1828. * DESCRIPTION
  1829. *
  1830. * CHANGES
  1831. *
  1832. ******************************************************************************/
  1833. static TURB *Search_For_Turb(WARP *Warps)
  1834. {
  1835. WARP* Temp=Warps;
  1836. if (Temp!=NULL)
  1837. {
  1838. while (Temp->Next_Warp != NULL)
  1839. {
  1840. Temp=Temp->Next_Warp;
  1841. }
  1842. if (Temp->Warp_Type != CLASSIC_TURB_WARP)
  1843. {
  1844. Temp=NULL;
  1845. }
  1846. }
  1847. return ((TURB *)Temp);
  1848. }
  1849. /*****************************************************************************
  1850. *
  1851. * FUNCTION
  1852. *
  1853. * density_file
  1854. *
  1855. * INPUT
  1856. *
  1857. * OUTPUT
  1858. *
  1859. * RETURNS
  1860. *
  1861. * AUTHOR
  1862. *
  1863. * Dieter Bayer
  1864. *
  1865. * DESCRIPTION
  1866. *
  1867. * CHANGES
  1868. *
  1869. * Dec 1996 : Creation.
  1870. *
  1871. ******************************************************************************/
  1872. static DBL density_file(VECTOR EPoint, TPATTERN *TPat)
  1873. {
  1874. int x, y, z;
  1875. int x1, y1, z1;
  1876. int x2, y2, z2;
  1877. DBL xx, yy, zz;
  1878. DBL xi, yi, zi;
  1879. DBL f111, f112, f121, f122, f211, f212, f221, f222;
  1880. DBL density = 0.0;
  1881. DENSITY_FILE_DATA *Data;
  1882. if ((TPat->Vals.Density_File != NULL) &&
  1883. ((Data = TPat->Vals.Density_File->Data) != NULL))
  1884. {
  1885. if ((EPoint[X] >= 0.0) && (EPoint[X] < 1.0) &&
  1886. (EPoint[Y] >= 0.0) && (EPoint[Y] < 1.0) &&
  1887. (EPoint[Z] >= 0.0) && (EPoint[Z] < 1.0))
  1888. {
  1889. switch (TPat->Vals.Density_File->Interpolation)
  1890. {
  1891. case NO_INTERPOLATION:
  1892. x = (int)(EPoint[X] * (DBL)Data->Sx);
  1893. y = (int)(EPoint[Y] * (DBL)Data->Sy);
  1894. z = (int)(EPoint[Z] * (DBL)Data->Sz);
  1895. if ((x < 0) || (x >= Data->Sx) ||
  1896. (y < 0) || (y >= Data->Sy) ||
  1897. (z < 0) || (z >= Data->Sz))
  1898. {
  1899. density = 0.0;
  1900. }
  1901. else
  1902. {
  1903. density = (DBL)Data->Density[z][y][x] / 255.0;
  1904. }
  1905. break;
  1906. case TRILINEAR_INTERPOLATION:
  1907. xx = EPoint[X] * (DBL)(Data->Sx - 1);
  1908. yy = EPoint[Y] * (DBL)(Data->Sy - 1);
  1909. zz = EPoint[Z] * (DBL)(Data->Sz - 1);
  1910. x1 = (int)xx;
  1911. y1 = (int)yy;
  1912. z1 = (int)zz;
  1913. x2 = x1 + 1;
  1914. y2 = y1 + 1;
  1915. z2 = z1 + 1;
  1916. xx -= floor(xx);
  1917. yy -= floor(yy);
  1918. zz -= floor(zz);
  1919. xi = 1.0 - xx;
  1920. yi = 1.0 - yy;
  1921. zi = 1.0 - zz;
  1922. f111 = (DBL)Data->Density[z1][y1][x1] / 255.0;
  1923. f112 = (DBL)Data->Density[z1][y1][x2] / 255.0;
  1924. f121 = (DBL)Data->Density[z1][y2][x1] / 255.0;
  1925. f122 = (DBL)Data->Density[z1][y2][x2] / 255.0;
  1926. f211 = (DBL)Data->Density[z2][y1][x1] / 255.0;
  1927. f212 = (DBL)Data->Density[z2][y1][x2] / 255.0;
  1928. f221 = (DBL)Data->Density[z2][y2][x1] / 255.0;
  1929. f222 = (DBL)Data->Density[z2][y2][x2] / 255.0;
  1930. density = f111 * zi * yi * xi +
  1931. f112 * zi * yi * xx +
  1932. f121 * zi * yy * xi +
  1933. f122 * zi * yy * xx +
  1934. f211 * zz * yi * xi +
  1935. f212 * zz * yi * xx +
  1936. f221 * zz * yy * xi +
  1937. f222 * zz * yy * xx;
  1938. break;
  1939. }
  1940. }
  1941. else
  1942. {
  1943. density = 0.0;
  1944. }
  1945. /*
  1946. fprintf(stderr, "x = %3d, y = %3d, z = %3d, density = %5.4f\n", x, y, z, density);
  1947. */
  1948. }
  1949. return(density);
  1950. }
  1951. /*****************************************************************************
  1952. *
  1953. * FUNCTION
  1954. *
  1955. * Create_Density_File
  1956. *
  1957. * INPUT
  1958. *
  1959. * OUTPUT
  1960. *
  1961. * RETURNS
  1962. *
  1963. * AUTHOR
  1964. *
  1965. * Dieter Bayer
  1966. *
  1967. * DESCRIPTION
  1968. *
  1969. * Create a density file structure.
  1970. *
  1971. * CHANGES
  1972. *
  1973. * Dec 1996 : Creation.
  1974. *
  1975. ******************************************************************************/
  1976. DENSITY_FILE *Create_Density_File()
  1977. {
  1978. DENSITY_FILE *New;
  1979. New = (DENSITY_FILE *)POV_MALLOC(sizeof(DENSITY_FILE), "density file");
  1980. New->Interpolation = NO_INTERPOLATION;
  1981. New->Data = (DENSITY_FILE_DATA *)POV_MALLOC(sizeof(DENSITY_FILE_DATA), "density file data");
  1982. New->Data->References = 1;
  1983. New->Data->Name = NULL;
  1984. New->Data->Sx =
  1985. New->Data->Sy =
  1986. New->Data->Sz = 0;
  1987. New->Data->Density = NULL;
  1988. return (New);
  1989. }
  1990. /*****************************************************************************
  1991. *
  1992. * FUNCTION
  1993. *
  1994. * Copy_Density_File
  1995. *
  1996. * INPUT
  1997. *
  1998. * OUTPUT
  1999. *
  2000. * RETURNS
  2001. *
  2002. * AUTHOR
  2003. *
  2004. * Dieter Bayer
  2005. *
  2006. * DESCRIPTION
  2007. *
  2008. * Copy a density file structure.
  2009. *
  2010. * CHANGES
  2011. *
  2012. * Dec 1996 : Creation.
  2013. *
  2014. ******************************************************************************/
  2015. DENSITY_FILE *Copy_Density_File(DENSITY_FILE *Old)
  2016. {
  2017. DENSITY_FILE *New;
  2018. if (Old != NULL)
  2019. {
  2020. New = (DENSITY_FILE *)POV_MALLOC(sizeof(DENSITY_FILE), "density file");
  2021. *New = *Old;
  2022. New->Data->References++;
  2023. }
  2024. else /* tw */
  2025. New = NULL; /* tw */
  2026. return(New);
  2027. }
  2028. /*****************************************************************************
  2029. *
  2030. * FUNCTION
  2031. *
  2032. * Destroy_Density_File
  2033. *
  2034. * INPUT
  2035. *
  2036. * OUTPUT
  2037. *
  2038. * RETURNS
  2039. *
  2040. * AUTHOR
  2041. *
  2042. * Dieter Bayer
  2043. *
  2044. * DESCRIPTION
  2045. *
  2046. * Destroy a density file structure.
  2047. *
  2048. * CHANGES
  2049. *
  2050. * Dec 1996 : Creation.
  2051. *
  2052. ******************************************************************************/
  2053. void Destroy_Density_File(DENSITY_FILE *Density_File)
  2054. {
  2055. int y, z;
  2056. if (Density_File != NULL)
  2057. {
  2058. if ((--(Density_File->Data->References)) == 0)
  2059. {
  2060. POV_FREE(Density_File->Data->Name);
  2061. for (z = 0; z < Density_File->Data->Sz; z++)
  2062. {
  2063. for (y = 0; y < Density_File->Data->Sy; y++)
  2064. {
  2065. POV_FREE(Density_File->Data->Density[z][y]);
  2066. }
  2067. POV_FREE(Density_File->Data->Density[z]);
  2068. }
  2069. POV_FREE(Density_File->Data->Density);
  2070. POV_FREE(Density_File->Data);
  2071. }
  2072. POV_FREE(Density_File);
  2073. }
  2074. }
  2075. void Read_Density_File(DENSITY_FILE *df)
  2076. {
  2077. unsigned char ***map;
  2078. int y, z, sx, sy, sz;
  2079. FILE *file;
  2080. if (df == NULL)
  2081. {
  2082. return;
  2083. }
  2084. /* Allocate and read density file. */
  2085. if ((df != NULL) && (df->Data->Name != NULL))
  2086. {
  2087. if ((file = Locate_File(df->Data->Name, READ_BINFILE_STRING, ".df3", ".DF3",NULL,TRUE)) == NULL)
  2088. {
  2089. Error("Cannot read media density file.\n");
  2090. }
  2091. sx = df->Data->Sx = readushort(file);
  2092. sy = df->Data->Sy = readushort(file);
  2093. sz = df->Data->Sz = readushort(file);
  2094. map = (unsigned char ***)POV_MALLOC(sz*sizeof(unsigned char **), "media density file data");
  2095. for (z = 0; z < sz; z++)
  2096. {
  2097. map[z] = (unsigned char **)POV_MALLOC(sy*sizeof(unsigned char *), "media density file data");
  2098. for (y = 0; y < sy; y++)
  2099. {
  2100. map[z][y] = (unsigned char *)POV_MALLOC(sx*sizeof(unsigned char), "media density file data");
  2101. fread(map[z][y], sizeof(unsigned char), (size_t)sx, file);
  2102. }
  2103. }
  2104. df->Data->Density = map;
  2105. if (file != NULL) /* -hdf99- */
  2106. {
  2107. fclose(file);
  2108. }
  2109. }
  2110. }
  2111. static unsigned short readushort(FILE *infile)
  2112. {
  2113. int i0, i1 = 0; /* To quiet warnings */
  2114. if ((i0 = fgetc(infile)) == EOF || (i1 = fgetc(infile)) == EOF)
  2115. {
  2116. Error("Error reading density_file\n");
  2117. }
  2118. return (unsigned short)((((unsigned short)i0) << 8) | ((unsigned short)i1));
  2119. }