MEDIA.C 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /****************************************************************************
  2. * media.c
  3. *
  4. * This module contains all functions for participating media.
  5. *
  6. * from Persistence of Vision(tm) Ray Tracer
  7. * Copyright 1996,1999 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. * NOTICE: This source code file is provided so that users may experiment
  10. * with enhancements to POV-Ray and to port the software to platforms other
  11. * than those supported by the POV-Ray Team. There are strict rules under
  12. * which you are permitted to use this file. The rules are in the file
  13. * named POVLEGAL.DOC which should be distributed with this file.
  14. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  16. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23. #include "frame.h"
  24. #include "vector.h"
  25. #include "povproto.h"
  26. #include "chi2.h"
  27. #include "colour.h"
  28. #include "povray.h"
  29. #include "texture.h"
  30. #include "pigment.h"
  31. #include "objects.h"
  32. #include "lighting.h"
  33. #include "matrices.h"
  34. #include "media.h"
  35. #include "pattern.h"
  36. #include "povray.h"
  37. #include "point.h"
  38. #include "texture.h"
  39. #include "ray.h"
  40. /*****************************************************************************
  41. * Local preprocessor defines
  42. ******************************************************************************/
  43. /*****************************************************************************
  44. * Local typedefs
  45. ******************************************************************************/
  46. typedef struct Light_List_Struct LIGHT_LIST;
  47. typedef struct Media_Interval_Struct MEDIA_INTERVAL;
  48. typedef struct Lit_Interval_Struct LIT_INTERVAL;
  49. struct Lit_Interval_Struct
  50. {
  51. int lit;
  52. DBL s0, s1, ds;
  53. };
  54. struct Media_Interval_Struct
  55. {
  56. int lit;
  57. int samples;
  58. DBL s0, s1, ds;
  59. COLOUR od;
  60. COLOUR te;
  61. COLOUR te2;
  62. };
  63. struct Light_List_Struct
  64. {
  65. int active;
  66. DBL s0, s1;
  67. LIGHT_SOURCE *Light;
  68. };
  69. /*****************************************************************************
  70. * Local variables
  71. ******************************************************************************/
  72. /*****************************************************************************
  73. * Static functions
  74. ******************************************************************************/
  75. static void sample_media (LIGHT_LIST *, RAY *, IMEDIA **, MEDIA_INTERVAL *, int );
  76. static void get_light_list (LIGHT_LIST *, RAY *, INTERSECTION *);
  77. static void get_lit_interval (int *, LIT_INTERVAL *, int, LIGHT_LIST *, INTERSECTION *);
  78. static int set_up_sampling_intervals (MEDIA_INTERVAL *,
  79. int, LIT_INTERVAL *, IMEDIA *);
  80. static int intersect_spotlight (RAY *Ray, LIGHT_SOURCE *Light, DBL *d1, DBL *d2);
  81. static int intersect_cylinderlight (RAY *Ray, LIGHT_SOURCE *Light, DBL *d1, DBL *d2);
  82. static int CDECL compdoubles (CONST void *in_a, CONST void *in_b);
  83. static void evaluate_density_pattern (IMEDIA *, VECTOR , COLOUR);
  84. /*****************************************************************************
  85. *
  86. * FUNCTION
  87. *
  88. * Simulate_Media
  89. *
  90. * INPUT
  91. *
  92. * Ray - Current ray, start point P0
  93. * Inter - Current intersection, end point P1
  94. * Colour - Color emitted at P1 towards P0
  95. * light_ray - TRUE if we are looking at a light source ray
  96. *
  97. * OUTPUT
  98. *
  99. * Colour - Color arriving at the end point
  100. *
  101. * RETURNS
  102. *
  103. * AUTHOR
  104. *
  105. * Dieter Bayer
  106. *
  107. * DESCRIPTION
  108. *
  109. * Simulate participating media using volume sampling.
  110. *
  111. * The effects of participating media on the light emitted at P1
  112. * towards P0 are determined using Monte Carlo integration.
  113. *
  114. * The effects include: emission, absoprtion and scattering.
  115. *
  116. * Currently one global medium with constant coefficients is implemented.
  117. *
  118. * Ideas for the atmospheric scattering were taken from:
  119. *
  120. * - M. Inakage, "An Illumination Model for Atmospheric Environments", ..
  121. *
  122. * - Nishita, T., Miyawaki, Y. and Nakamae, E., "A Shading Model for
  123. * Atmospheric Scattering Considering Luminous Intensity Distribution
  124. * of Light Sources", Computer Graphics, 21, 4, 1987, 303-310
  125. *
  126. * CHANGES
  127. *
  128. * Nov 1994 : Creation.
  129. *
  130. * Jan 1995 : Added support of cylindrical light sources. [DB]
  131. *
  132. * Jun 1995 : Added code for alpha channel support. [DB]
  133. *
  134. ******************************************************************************/
  135. void Simulate_Media(IMEDIA **Media_List, RAY *Ray, INTERSECTION *Inter, COLOUR Colour, int light_ray)
  136. {
  137. int i, j, intervals, use_extinction;
  138. int lit_interval_entries;
  139. DBL n;
  140. COLOUR Od, Te, Va;
  141. LIGHT_LIST *Light_List = NULL;
  142. LIT_INTERVAL *Lit_Interval;
  143. IMEDIA *IMedia, **Tmp, *Local;
  144. MEDIA_INTERVAL *Media_Interval, *curr;
  145. /* Why are we here? */
  146. if ((Media_List == NULL) || (Media_List[0] == NULL))
  147. {
  148. return;
  149. }
  150. /* Find media with the largest number of intervals. */
  151. intervals = 0;
  152. use_extinction = FALSE;
  153. IMedia = Media_List[0];
  154. for (Tmp = Media_List; (*Tmp) != NULL; Tmp++)
  155. {
  156. for (Local = *Tmp; Local != NULL; Local = Local->Next_Media)
  157. {
  158. if (Local->Intervals > intervals)
  159. {
  160. intervals = Local->Intervals;
  161. IMedia = Local;
  162. }
  163. use_extinction |= Local->use_extinction;
  164. }
  165. }
  166. /* If this is a light ray and no extinction is used we can return. */
  167. if ((light_ray) && (!use_extinction))
  168. {
  169. return;
  170. }
  171. /*
  172. * Prepare the Monte Carlo integration along the ray from P0 to P1.
  173. */
  174. if (light_ray || (Frame.Number_Of_Light_Sources==0))
  175. {
  176. Lit_Interval = (LIT_INTERVAL *)POV_MALLOC(sizeof(LIT_INTERVAL), "lit interval");
  177. lit_interval_entries = 1;
  178. Lit_Interval[0].lit = FALSE;
  179. Lit_Interval[0].s0 = 0.0;
  180. Lit_Interval[0].s1 =
  181. Lit_Interval[0].ds = Inter->Depth;
  182. }
  183. else
  184. {
  185. /* Get light list. */
  186. Light_List = (LIGHT_LIST *)POV_MALLOC(Frame.Number_Of_Light_Sources*sizeof(LIGHT_LIST), "light list");
  187. get_light_list(Light_List, Ray, Inter);
  188. /* Get lit intervals. */
  189. Lit_Interval = (LIT_INTERVAL *)POV_MALLOC((2*Frame.Number_Of_Light_Sources+1)*sizeof(LIT_INTERVAL), "lit interval");
  190. get_lit_interval(&lit_interval_entries, Lit_Interval, Frame.Number_Of_Light_Sources, Light_List, Inter);
  191. }
  192. /* Set up sampling intervals. */
  193. Media_Interval = (MEDIA_INTERVAL *)POV_MALLOC(IMedia->Intervals*sizeof(MEDIA_INTERVAL), "media intervals");
  194. intervals = set_up_sampling_intervals(Media_Interval, lit_interval_entries, Lit_Interval, IMedia);
  195. /* Sample all intervals. */
  196. for (i = 0; i < intervals; i++)
  197. {
  198. /* Sample current interval. */
  199. Increase_Counter(stats[Media_Intervals]);
  200. for (j = 0; j < IMedia->Min_Samples; j++)
  201. {
  202. sample_media(Light_List, Ray, Media_List, &Media_Interval[i], light_ray);
  203. }
  204. }
  205. /* Cast additional samples if necessary. */
  206. if ((!light_ray) && (IMedia->Max_Samples > IMedia->Min_Samples))
  207. {
  208. curr = &Media_Interval[0];
  209. for (i = 0; i < intervals; i++)
  210. {
  211. if (curr->samples < IMedia->Max_Samples)
  212. {
  213. /* Get variance of samples. */
  214. n = (DBL)curr->samples;
  215. Va[0] = (curr->te2[0] / n - Sqr(curr->te[0] / n)) / n;
  216. Va[1] = (curr->te2[1] / n - Sqr(curr->te[1] / n)) / n;
  217. Va[2] = (curr->te2[2] / n - Sqr(curr->te[2] / n)) / n;
  218. /* Take additional samples until variance is small enough. */
  219. while ((Va[0] >= IMedia->Sample_Threshold[curr->samples-1]) ||
  220. (Va[1] >= IMedia->Sample_Threshold[curr->samples-1]) ||
  221. (Va[2] >= IMedia->Sample_Threshold[curr->samples-1]))
  222. {
  223. /* Sample current interval again. */
  224. sample_media(Light_List, Ray, Media_List, curr, light_ray);
  225. /* Have we reached maximum number of samples. */
  226. if (curr->samples > IMedia->Max_Samples)
  227. {
  228. break;
  229. }
  230. /* Get variance of samples. */
  231. n = (DBL)curr->samples;
  232. Va[0] = (curr->te2[0] / n - Sqr(curr->te[0] / n)) / n;
  233. Va[1] = (curr->te2[1] / n - Sqr(curr->te[1] / n)) / n;
  234. Va[2] = (curr->te2[2] / n - Sqr(curr->te[2] / n)) / n;
  235. }
  236. }
  237. curr++;
  238. }
  239. }
  240. /* Sum the influences of all intervals. */
  241. Make_Colour(Od, 0.0, 0.0, 0.0);
  242. Make_Colour(Te, 0.0, 0.0, 0.0);
  243. curr = &Media_Interval[0];
  244. for (i = 0; i < intervals; i++)
  245. {
  246. /* Add total emission. */
  247. Te[0] += curr->te[0] / (DBL)curr->samples * exp(-Od[0]);
  248. Te[1] += curr->te[1] / (DBL)curr->samples * exp(-Od[1]);
  249. Te[2] += curr->te[2] / (DBL)curr->samples * exp(-Od[2]);
  250. /* Add optical depth of current interval. */
  251. Od[0] += curr->od[0] / (DBL)curr->samples;
  252. Od[1] += curr->od[1] / (DBL)curr->samples;
  253. Od[2] += curr->od[2] / (DBL)curr->samples;
  254. curr++;
  255. }
  256. /* Add contribution estimated for the participating media. */
  257. Colour[0] = Colour[0] * exp(-Od[0]) + Te[0];
  258. Colour[1] = Colour[1] * exp(-Od[1]) + Te[1];
  259. Colour[2] = Colour[2] * exp(-Od[2]) + Te[2];
  260. if (!(light_ray || (Frame.Number_Of_Light_Sources==0)))
  261. {
  262. POV_FREE(Light_List);
  263. }
  264. POV_FREE(Lit_Interval);
  265. POV_FREE(Media_Interval);
  266. }
  267. /*****************************************************************************
  268. *
  269. * FUNCTION
  270. *
  271. * sample_media
  272. *
  273. * INPUT
  274. *
  275. * dist - distance of current sample
  276. * Ray - pointer to ray
  277. * IMedia - pointer to media to use
  278. *
  279. * OUTPUT
  280. *
  281. * Col - color of current sample
  282. *
  283. * RETURNS
  284. *
  285. * AUTHOR
  286. *
  287. * Dieter Bayer
  288. *
  289. * DESCRIPTION
  290. *
  291. * Calculate the color of the current media sample.
  292. *
  293. * CHANGES
  294. *
  295. * Nov 1994 : Creation.
  296. *
  297. ******************************************************************************/
  298. static void sample_media(LIGHT_LIST *Light_List, RAY *Ray, IMEDIA **Media_List, MEDIA_INTERVAL *Interval, int light_ray)
  299. {
  300. int i, n, use_scattering;
  301. DBL alpha, d0, d1, len, k, g, g2;
  302. VECTOR P, H;
  303. COLOUR C0, Light_Colour, Te;
  304. COLOUR Em, Ex, Sc;
  305. RAY Light_Ray;
  306. IMEDIA **Tmp, *Local;
  307. Increase_Counter(stats[Media_Samples]);
  308. /* Set up sampling location. */
  309. d0 = Interval->ds * FRAND();
  310. d1 = Interval->s0 + d0;
  311. VEvaluateRay(H, Ray->Initial, d1, Ray->Direction);
  312. /* Get coefficients in current sample location. */
  313. Make_Colour(Em, 0.0, 0.0, 0.0);
  314. Make_Colour(Ex, 0.0, 0.0, 0.0);
  315. Make_Colour(Sc, 0.0, 0.0, 0.0);
  316. use_scattering = FALSE;
  317. for (Tmp = Media_List; (*Tmp) != NULL; Tmp++)
  318. {
  319. for (Local = *Tmp; Local != NULL; Local = Local->Next_Media)
  320. {
  321. Assign_Vector(P, H);
  322. evaluate_density_pattern(Local, P, C0);
  323. Ex[0] += C0[0] * Local->Extinction[0];
  324. Ex[1] += C0[1] * Local->Extinction[1];
  325. Ex[2] += C0[2] * Local->Extinction[2];
  326. if (!light_ray)
  327. {
  328. Em[0] += C0[0] * Local->Emission[0];
  329. Em[1] += C0[1] * Local->Emission[1];
  330. Em[2] += C0[2] * Local->Emission[2];
  331. Sc[0] += C0[0] * Local->Scattering[0];
  332. Sc[1] += C0[1] * Local->Scattering[1];
  333. Sc[2] += C0[2] * Local->Scattering[2];
  334. }
  335. use_scattering |= Local->use_scattering;
  336. }
  337. }
  338. /* Get estimate for the total optical depth of the current interval. */
  339. Interval->od[0] += Ex[0] * Interval->ds;
  340. Interval->od[1] += Ex[1] * Interval->ds;
  341. Interval->od[2] += Ex[2] * Interval->ds;
  342. /* Get estimate for the total emission of the current interval. */
  343. Te[0] = Em[0];
  344. Te[1] = Em[1];
  345. Te[2] = Em[2];
  346. if ((!light_ray) && (use_scattering) && (Interval->lit))
  347. {
  348. /* Process all light sources. */
  349. for (i = 0; i < Frame.Number_Of_Light_Sources; i++)
  350. {
  351. /* Use light only if active and within it's boundaries. */
  352. if ((Light_List[i].active) && (d1 >= Light_List[i].s0) && (d1 <= Light_List[i].s1))
  353. {
  354. if (!(Test_Shadow(Light_List[i].Light, &len, &Light_Ray, Ray, P, Light_Colour)))
  355. {
  356. /* Get attenuation due to scattering. */
  357. k = 0.0;
  358. for (n = 0, Tmp = Media_List; (*Tmp) != NULL; n++, Tmp++)
  359. {
  360. for (Local = *Tmp; Local != NULL; Local = Local->Next_Media)
  361. {
  362. switch (Local->Type)
  363. {
  364. case RAYLEIGH_SCATTERING:
  365. VDot(alpha, Light_Ray.Direction, Ray->Direction);
  366. k += 0.799372013 * (1.0 + Sqr(alpha));
  367. break;
  368. case MIE_HAZY_SCATTERING:
  369. VDot(alpha, Light_Ray.Direction, Ray->Direction);
  370. k += 0.576655375 * (1.0 + 9.0 * pow(0.5 * (1.0 + alpha), 8.0));
  371. break;
  372. case MIE_MURKY_SCATTERING:
  373. VDot(alpha, Light_Ray.Direction, Ray->Direction);
  374. k += 0.495714547 * (1.0 + 50.0 * pow(0.5 * (1.0 + alpha), 32.0));
  375. break;
  376. case HENYEY_GREENSTEIN_SCATTERING:
  377. VDot(alpha, Light_Ray.Direction, Ray->Direction);
  378. g = Local->Eccentricity;
  379. g2 = Sqr(g);
  380. k += (1.0 - g2) / pow(1.0 + g2 - 2.0 * g * alpha, 1.5);
  381. break;
  382. case ISOTROPIC_SCATTERING:
  383. default:
  384. k += 1.0;
  385. break;
  386. }
  387. }
  388. }
  389. k /= (DBL)n;
  390. Te[0] += k * Sc[0] * Light_Colour[0];
  391. Te[1] += k * Sc[1] * Light_Colour[1];
  392. Te[2] += k * Sc[2] * Light_Colour[2];
  393. }
  394. }
  395. }
  396. }
  397. Te[0] *= Interval->ds * exp(-Ex[0] * d0);
  398. Te[1] *= Interval->ds * exp(-Ex[1] * d0);
  399. Te[2] *= Interval->ds * exp(-Ex[2] * d0);
  400. /* Add emission. */
  401. Interval->te[0] += Te[0];
  402. Interval->te[1] += Te[1];
  403. Interval->te[2] += Te[2];
  404. Interval->te2[0] += Sqr(Te[0]);
  405. Interval->te2[1] += Sqr(Te[1]);
  406. Interval->te2[2] += Sqr(Te[2]);
  407. Interval->samples++;
  408. }
  409. /*****************************************************************************
  410. *
  411. * FUNCTION
  412. *
  413. * evaluate_density_pattern
  414. *
  415. * INPUT
  416. *
  417. * OUTPUT
  418. *
  419. * RETURNS
  420. *
  421. * AUTHOR
  422. *
  423. * Dieter Bayer
  424. *
  425. * DESCRIPTION
  426. *
  427. * CHANGES
  428. *
  429. * Dec 1996 : Creation.
  430. *
  431. ******************************************************************************/
  432. static void evaluate_density_pattern(IMEDIA *IMedia, VECTOR P, COLOUR C)
  433. {
  434. COLOUR Local_Color;
  435. PIGMENT *Temp=IMedia->Density;
  436. Make_Colour (C, 1.0, 1.0, 1.0);
  437. while (Temp != NULL)
  438. {
  439. Make_Colour (Local_Color, 0.0, 0.0, 0.0);
  440. Compute_Pigment (Local_Color, Temp, P);
  441. C[0] *= Local_Color[0];
  442. C[1] *= Local_Color[1];
  443. C[2] *= Local_Color[2];
  444. Temp=(PIGMENT *)Temp->Next;
  445. }
  446. }
  447. /*****************************************************************************
  448. *
  449. * FUNCTION
  450. *
  451. * get_light_list
  452. *
  453. * INPUT
  454. *
  455. * Light_List - array containing light source information
  456. * dist - distance of current sample
  457. * Ray - pointer to ray
  458. * IMedia - pointer to media to use
  459. *
  460. * OUTPUT
  461. *
  462. * Col - color of current sample
  463. *
  464. * RETURNS
  465. *
  466. * AUTHOR
  467. *
  468. * Dieter Bayer
  469. *
  470. * DESCRIPTION
  471. *
  472. * CHANGES
  473. *
  474. * Nov 1994 : Creation.
  475. *
  476. ******************************************************************************/
  477. static void get_light_list(LIGHT_LIST *Light_List, RAY *Ray, INTERSECTION *Inter)
  478. {
  479. int i, insert;
  480. DBL t1, t2;
  481. LIGHT_SOURCE *Light;
  482. /* Get depths for all light sources and disconnected sampling intervals. */
  483. t1 = t2 = 0.0;
  484. for (i = 0, Light = Frame.Light_Sources; Light != NULL; Light = Light->Next_Light_Source, i++)
  485. {
  486. /* Init interval. */
  487. Light_List[i].active = FALSE;
  488. Light_List[i].s0 = 0.0;
  489. Light_List[i].s1 = Max_Distance;
  490. Light_List[i].Light = NULL;
  491. insert = FALSE;
  492. Light_List[i].Light = Light;
  493. if (!Light->Media_Interaction)
  494. {
  495. continue;
  496. }
  497. switch (Light->Light_Type)
  498. {
  499. case CYLINDER_SOURCE:
  500. if (intersect_cylinderlight(Ray, Light, &t1, &t2))
  501. {
  502. if ((t1 < Inter->Depth) && (t2 > Small_Tolerance))
  503. {
  504. insert = TRUE;
  505. }
  506. }
  507. break;
  508. case POINT_SOURCE:
  509. t1 = 0.0;
  510. t2 = Inter->Depth;
  511. insert = TRUE;
  512. break;
  513. case SPOT_SOURCE:
  514. if (intersect_spotlight(Ray, Light, &t1, &t2))
  515. {
  516. if ((t1 < Inter->Depth) && (t2 > Small_Tolerance))
  517. {
  518. insert = TRUE;
  519. }
  520. }
  521. break;
  522. }
  523. /* Insert distances into sampling interval list. */
  524. if (insert)
  525. {
  526. /* Insert light source intersections into light list. */
  527. t1 = max(t1, 0.0);
  528. t2 = min(t2, Inter->Depth);
  529. Light_List[i].active = TRUE;
  530. Light_List[i].s0 = t1;
  531. Light_List[i].s1 = t2;
  532. }
  533. }
  534. }
  535. /*****************************************************************************
  536. *
  537. * FUNCTION
  538. *
  539. * get_lit_interval
  540. *
  541. * INPUT
  542. *
  543. * Light_List - array containing light source information
  544. * dist - distance of current sample
  545. * Ray - pointer to ray
  546. * IMedia - pointer to media to use
  547. *
  548. * OUTPUT
  549. *
  550. * Col - color of current sample
  551. *
  552. * RETURNS
  553. *
  554. * AUTHOR
  555. *
  556. * Dieter Bayer
  557. *
  558. * DESCRIPTION
  559. *
  560. * CHANGES
  561. *
  562. * Nov 1994 : Creation.
  563. *
  564. ******************************************************************************/
  565. static void get_lit_interval(int *number, LIT_INTERVAL *Lit_Interval, int entries, LIGHT_LIST *Light_List, INTERSECTION *Inter)
  566. {
  567. int a, i, n;
  568. DBL *s0, *s1;
  569. LIT_INTERVAL *curr, *prev;
  570. s0 = (DBL *)POV_MALLOC(Frame.Number_Of_Light_Sources*sizeof(DBL), "temp data");
  571. s1 = (DBL *)POV_MALLOC(Frame.Number_Of_Light_Sources*sizeof(DBL), "temp data");
  572. for (i = a = 0; i < entries; i++)
  573. {
  574. if (Light_List[i].active)
  575. {
  576. s0[a] = Light_List[i].s0;
  577. s1[a] = Light_List[i].s1;
  578. a++;
  579. }
  580. }
  581. n = 0;
  582. curr = &Lit_Interval[0];
  583. if (a)
  584. {
  585. QSORT((void *)(&s0[0]), (unsigned long)a, sizeof(DBL), compdoubles);
  586. QSORT((void *)(&s1[0]), (unsigned long)a, sizeof(DBL), compdoubles);
  587. if (s0[0] > 0.0)
  588. {
  589. curr->lit = FALSE;
  590. curr->s0 = 0.0;
  591. curr->s1 = s0[0];
  592. curr++;
  593. n++;
  594. }
  595. curr->lit = TRUE;
  596. curr->s0 = s0[0];
  597. curr->s1 = s1[0];
  598. prev = curr;
  599. curr++;
  600. n++;
  601. for (i = 1; i < a; i++)
  602. {
  603. if (s0[i] > prev->s1)
  604. {
  605. curr->lit = FALSE;
  606. curr->s0 = prev->s1;
  607. curr->s1 = s0[i];
  608. prev++;
  609. curr++;
  610. n++;
  611. curr->lit = TRUE;
  612. curr->s0 = s0[i];
  613. curr->s1 = s1[i];
  614. prev++;
  615. curr++;
  616. n++;
  617. }
  618. else
  619. {
  620. if (s1[i] > prev->s1)
  621. {
  622. prev->s1 = s1[i];
  623. }
  624. }
  625. }
  626. if (prev->s1 < Inter->Depth)
  627. {
  628. curr->lit = FALSE;
  629. curr->s0 = prev->s1;
  630. curr->s1 = Inter->Depth;
  631. curr++;
  632. n++;
  633. }
  634. }
  635. else
  636. {
  637. curr->lit = FALSE;
  638. curr->s0 = 0.0;
  639. curr->s1 = Inter->Depth;
  640. curr++;
  641. n++;
  642. }
  643. curr = &Lit_Interval[0];
  644. for (i = 0; i < n; i++)
  645. {
  646. curr->ds = curr->s1 - curr->s0;
  647. curr++;
  648. }
  649. POV_FREE(s0);
  650. POV_FREE(s1);
  651. *number = n;
  652. }
  653. /*****************************************************************************
  654. *
  655. * FUNCTION
  656. *
  657. * set_up_sampling_intervals
  658. *
  659. * INPUT
  660. *
  661. * interval - array containing media intervals
  662. * number - number of lit intervals
  663. * list - array of lit intervals
  664. * media - media to use
  665. *
  666. * OUTPUT
  667. *
  668. * interval - array containing media intervals
  669. *
  670. * RETURNS
  671. *
  672. * int - number of media intervals created
  673. *
  674. * AUTHOR
  675. *
  676. * Dieter Bayer
  677. *
  678. * DESCRIPTION
  679. *
  680. * Distribute samples along an interval according to the maximum
  681. * number of samples and the ratio of samples in lit and unlit
  682. * areas as given by the participating media.
  683. *
  684. * CHANGES
  685. *
  686. * Nov 1994 : Creation.
  687. *
  688. ******************************************************************************/
  689. static int set_up_sampling_intervals(MEDIA_INTERVAL *interval, int number, LIT_INTERVAL *list, IMEDIA *media)
  690. {
  691. int i, j, n, r, remaining, intervals;
  692. DBL delta, sum, weight;
  693. MEDIA_INTERVAL *curr;
  694. LIT_INTERVAL *entry;
  695. /* Set up sampling intervals. */
  696. intervals = media->Intervals;
  697. /* Use one interval if no lit intervals and constant media. */
  698. if ((number == 0) && (media->is_constant))
  699. {
  700. intervals = 1;
  701. delta = list[0].ds;
  702. curr = interval;
  703. curr->lit = TRUE;
  704. curr->samples = 0;
  705. curr->s0 = list[0].s0;
  706. curr->s1 = list[0].s0 + delta;
  707. curr->ds = delta;
  708. Make_Colour(curr->od, 0.0, 0.0, 0.0);
  709. Make_Colour(curr->te, 0.0, 0.0, 0.0);
  710. Make_Colour(curr->te2, 0.0, 0.0, 0.0);
  711. return(intervals);
  712. }
  713. /* Choose intervals. */
  714. if (number == 1)
  715. {
  716. /* Use uniform intervals. */
  717. delta = list[0].ds / (DBL)intervals;
  718. curr = interval;
  719. for (i = 0; i < intervals; i++)
  720. {
  721. curr->lit = TRUE;
  722. curr->samples = 0;
  723. curr->s0 = list[0].s0 + delta * (DBL)i;
  724. curr->s1 = list[0].s0 + delta * (DBL)(i + 1);
  725. curr->ds = delta;
  726. Make_Colour(curr->od, 0.0, 0.0, 0.0);
  727. Make_Colour(curr->te, 0.0, 0.0, 0.0);
  728. Make_Colour(curr->te2, 0.0, 0.0, 0.0);
  729. curr++;
  730. }
  731. }
  732. else
  733. {
  734. /* Choose intervals according to the specified ratio. */
  735. if (number > intervals)
  736. {
  737. Error("Too few sampling intervals.\n");
  738. }
  739. sum = 0.0;
  740. entry = list;
  741. for (i = 0; i < number; i++)
  742. {
  743. /*
  744. sum += ((entry->lit) ? (0.9) : (0.1)) * entry->ds;
  745. */
  746. sum += ((entry->lit) ? (media->Ratio) : (1.0 - media->Ratio));
  747. entry++;
  748. }
  749. remaining = intervals;
  750. curr = interval;
  751. entry = list;
  752. for (i = 0; i < number; i++)
  753. {
  754. /*
  755. weight = ((entry->lit) ? (0.9) : (0.1)) * entry->ds;
  756. */
  757. weight = ((entry->lit) ? (media->Ratio) : (1.0 - media->Ratio));
  758. n = (int)(weight / sum * (DBL)intervals) + 1;
  759. r = remaining - number + i + 1;
  760. if (n > r)
  761. {
  762. n = r;
  763. }
  764. delta = entry->ds / (DBL)n;
  765. for (j = 0; j < n; j++)
  766. {
  767. curr->lit = entry->lit;
  768. curr->samples = 0;
  769. curr->s0 = entry->s0 + delta * (DBL)j;
  770. curr->s1 = entry->s0 + delta * (DBL)(j + 1);
  771. curr->ds = delta;
  772. Make_Colour(curr->od, 0.0, 0.0, 0.0);
  773. Make_Colour(curr->te, 0.0, 0.0, 0.0);
  774. Make_Colour(curr->te2, 0.0, 0.0, 0.0);
  775. curr++;
  776. }
  777. remaining -= n;
  778. entry++;
  779. }
  780. }
  781. return(intervals);
  782. }
  783. /*****************************************************************************
  784. *
  785. * FUNCTION
  786. *
  787. * intersect_spotlight
  788. *
  789. * INPUT
  790. *
  791. * Ray - current ray
  792. * Light - current light source
  793. *
  794. * OUTPUT
  795. *
  796. * d1, d2 - intersection depths
  797. *
  798. * RETURNS
  799. *
  800. * int - TRUE, if hit
  801. *
  802. * AUTHOR
  803. *
  804. * Dieter Bayer
  805. *
  806. * DESCRIPTION
  807. *
  808. * Intersect a ray with the light cone of a spotlight.
  809. *
  810. * CHANGES
  811. *
  812. * Nov 1994 : Creation.
  813. *
  814. ******************************************************************************/
  815. static int intersect_spotlight(RAY *Ray, LIGHT_SOURCE *Light, DBL *d1, DBL *d2)
  816. {
  817. int viewpoint_is_in_cone;
  818. DBL a, b, c, d, m, l, l1, l2, t, t1, t2, k1, k2, k3, k4;
  819. VECTOR V1;
  820. /* Get cone's slope. Note that cos(falloff) is stored in Falloff! */
  821. a = acos(Light->Falloff);
  822. /* This only works for a < 180 degrees! */
  823. m = tan(a);
  824. m = 1.0 + Sqr(m);
  825. VSub(V1, Ray->Initial, Light->Center);
  826. VDot(k1, Ray->Direction, Light->Direction);
  827. VDot(k2, V1, Light->Direction);
  828. VLength(l, V1);
  829. if (l > EPSILON)
  830. {
  831. viewpoint_is_in_cone = (k2 / l >= Light->Falloff);
  832. }
  833. else
  834. {
  835. viewpoint_is_in_cone = FALSE;
  836. }
  837. if ((k1 <= 0.0) && (k2 < 0.0))
  838. {
  839. return (FALSE);
  840. }
  841. VDot(k3, V1, Ray->Direction);
  842. VDot(k4, V1, V1);
  843. a = 1.0 - Sqr(k1) * m;
  844. b = k3 - k1 * k2 * m;
  845. c = k4 - Sqr(k2) * m;
  846. if (a != 0.0)
  847. {
  848. d = Sqr(b) - a * c;
  849. if (d > EPSILON)
  850. {
  851. d = sqrt(d);
  852. t1 = (-b + d) / a;
  853. t2 = (-b - d) / a;
  854. if (t1 > t2)
  855. {
  856. t = t1; t1 = t2; t2 = t;
  857. }
  858. l1 = k2 + t1 * k1;
  859. l2 = k2 + t2 * k1;
  860. if ((l1 <= 0.0) && (l2 <= 0.0))
  861. {
  862. return (FALSE);
  863. }
  864. if ((l1 <= 0.0) || (l2 <= 0.0))
  865. {
  866. if (l1 <= 0.0)
  867. {
  868. if (viewpoint_is_in_cone)
  869. {
  870. t1 = 0.0;
  871. t2 = (t2 > 0.0) ? (t2) : (Max_Distance);
  872. }
  873. else
  874. {
  875. t1 = t2;
  876. t2 = Max_Distance;
  877. }
  878. }
  879. else
  880. {
  881. if (viewpoint_is_in_cone)
  882. {
  883. t2 = t1;
  884. t1 = 0.0;
  885. }
  886. else
  887. {
  888. t2 = Max_Distance;
  889. }
  890. }
  891. }
  892. *d1 = t1;
  893. *d2 = t2;
  894. return (TRUE);
  895. }
  896. else
  897. {
  898. if (d > -EPSILON)
  899. {
  900. if (viewpoint_is_in_cone)
  901. {
  902. *d1 = 0.0;
  903. *d2 = -b / a;
  904. }
  905. else
  906. {
  907. *d1 = -b / a;
  908. *d2 = Max_Distance;
  909. }
  910. return(TRUE);
  911. }
  912. }
  913. }
  914. else
  915. {
  916. if (viewpoint_is_in_cone)
  917. {
  918. *d1 = 0.0;
  919. *d2 = -c/b;
  920. return(TRUE);
  921. }
  922. }
  923. return (FALSE);
  924. }
  925. /*****************************************************************************
  926. *
  927. * FUNCTION
  928. *
  929. * intersect_cylinderlight
  930. *
  931. * INPUT
  932. *
  933. * Ray - current ray
  934. * Light - current light source
  935. *
  936. * OUTPUT
  937. *
  938. * d1, d2 - intersection depths
  939. *
  940. * RETURNS
  941. *
  942. * int - TRUE, if hit
  943. *
  944. * AUTHOR
  945. *
  946. * Dieter Bayer
  947. *
  948. * DESCRIPTION
  949. *
  950. * Intersect a ray with the light cylinder of a cylinderlight.
  951. *
  952. * CHANGES
  953. *
  954. * Jan 1995 : Creation.
  955. *
  956. ******************************************************************************/
  957. static int intersect_cylinderlight(RAY *Ray, LIGHT_SOURCE *Light, DBL *d1, DBL *d2)
  958. {
  959. DBL a, b, c, d, l1, l2, t, t1, t2, k1, k2, k3, k4;
  960. VECTOR V1;
  961. VSub(V1, Ray->Initial, Light->Center);
  962. VDot(k1, Ray->Direction, Light->Direction);
  963. VDot(k2, V1, Light->Direction);
  964. if ((k1 <= 0.0) && (k2 < 0.0))
  965. {
  966. return (FALSE);
  967. }
  968. a = 1.0 - Sqr(k1);
  969. if (a != 0.0)
  970. {
  971. VDot(k3, V1, Ray->Direction);
  972. VDot(k4, V1, V1);
  973. b = k3 - k1 * k2;
  974. c = k4 - Sqr(k2) - Sqr(Light->Falloff);
  975. d = Sqr(b) - a * c;
  976. if (d > EPSILON)
  977. {
  978. d = sqrt(d);
  979. t1 = (-b + d) / a;
  980. t2 = (-b - d) / a;
  981. if (t1 > t2)
  982. {
  983. t = t1; t1 = t2; t2 = t;
  984. }
  985. l1 = k2 + t1 * k1;
  986. l2 = k2 + t2 * k1;
  987. if ((l1 <= 0.0) && (l2 <= 0.0))
  988. {
  989. return (FALSE);
  990. }
  991. if ((l1 <= 0.0) || (l2 <= 0.0))
  992. {
  993. if (l1 <= 0.0)
  994. {
  995. t1 = 0.0;
  996. }
  997. else
  998. {
  999. t2 = (Max_Distance - k2) / k1;
  1000. }
  1001. }
  1002. *d1 = t1;
  1003. *d2 = t2;
  1004. return (TRUE);
  1005. }
  1006. }
  1007. return (FALSE);
  1008. }
  1009. /*****************************************************************************
  1010. *
  1011. * FUNCTION
  1012. *
  1013. * Post_Media
  1014. *
  1015. * INPUT
  1016. *
  1017. * OUTPUT
  1018. *
  1019. * RETURNS
  1020. *
  1021. * AUTHOR
  1022. *
  1023. * Dieter Bayer
  1024. *
  1025. * DESCRIPTION
  1026. *
  1027. * Initialize media.
  1028. *
  1029. * CHANGES
  1030. *
  1031. * Dec 1996 : Creation.
  1032. *
  1033. ******************************************************************************/
  1034. void Post_Media(IMEDIA *IMedia)
  1035. {
  1036. int i;
  1037. DBL t;
  1038. if (IMedia == NULL)
  1039. {
  1040. return;
  1041. }
  1042. /* Get extinction coefficient. */
  1043. CLinComb2(IMedia->Extinction, 1.0, IMedia->Absorption, IMedia->sc_ext, IMedia->Scattering);
  1044. /* Determine used effects. */
  1045. IMedia->use_absorption = (IMedia->Absorption[0] != 0.0) ||
  1046. (IMedia->Absorption[1] != 0.0) ||
  1047. (IMedia->Absorption[2] != 0.0);
  1048. IMedia->use_emission = (IMedia->Emission[0] != 0.0) ||
  1049. (IMedia->Emission[1] != 0.0) ||
  1050. (IMedia->Emission[2] != 0.0);
  1051. IMedia->use_scattering = (IMedia->Scattering[0] != 0.0) ||
  1052. (IMedia->Scattering[1] != 0.0) ||
  1053. (IMedia->Scattering[2] != 0.0);
  1054. IMedia->use_extinction = IMedia->use_absorption || IMedia->use_scattering;
  1055. IMedia->is_constant = (IMedia->Density == NULL);
  1056. /* Init sample threshold array. */
  1057. if (IMedia->Sample_Threshold != NULL)
  1058. {
  1059. POV_FREE(IMedia->Sample_Threshold);
  1060. }
  1061. /* Create list of thresholds for confidence test. */
  1062. IMedia->Sample_Threshold = (DBL *)POV_MALLOC(IMedia->Max_Samples*sizeof(DBL), "sample threshold list");
  1063. if (IMedia->Max_Samples > 1)
  1064. {
  1065. t = chdtri((DBL)(IMedia->Max_Samples-1), IMedia->Confidence);
  1066. if (t > 0.0)
  1067. {
  1068. t = IMedia->Variance / t;
  1069. }
  1070. else
  1071. {
  1072. t = IMedia->Variance * EPSILON;
  1073. }
  1074. for (i = 0; i < IMedia->Max_Samples; i++)
  1075. {
  1076. IMedia->Sample_Threshold[i] = t * chdtri((DBL)(i+1), IMedia->Confidence);
  1077. /*
  1078. fprintf(stderr, "threshold for n = %3d: %f\n", i+1, IMedia->Sample_Threshold[i]);
  1079. */
  1080. }
  1081. }
  1082. else
  1083. {
  1084. IMedia->Sample_Threshold[0] = 0.0;
  1085. }
  1086. if (IMedia->Density != NULL)
  1087. {
  1088. Post_Pigment(IMedia->Density);
  1089. }
  1090. Post_Media(IMedia->Next_Media);
  1091. }
  1092. /*****************************************************************************
  1093. *
  1094. * FUNCTION
  1095. *
  1096. * Create_Media
  1097. *
  1098. * INPUT
  1099. *
  1100. * OUTPUT
  1101. *
  1102. * RETURNS
  1103. *
  1104. * IMEDIA * - created media
  1105. *
  1106. * AUTHOR
  1107. *
  1108. * Dieter Bayer
  1109. *
  1110. * DESCRIPTION
  1111. *
  1112. * Create a media.
  1113. *
  1114. * CHANGES
  1115. *
  1116. * Dec 1994 : Creation.
  1117. *
  1118. ******************************************************************************/
  1119. IMEDIA *Create_Media()
  1120. {
  1121. IMEDIA *New;
  1122. New = (IMEDIA *)POV_MALLOC(sizeof(IMEDIA), "media");
  1123. New->Type = ISOTROPIC_SCATTERING;
  1124. New->Intervals = 10;
  1125. New->Min_Samples = 1;
  1126. New->Max_Samples = 1;
  1127. New->Eccentricity = 0.0;
  1128. Make_Colour(New->Absorption, 0.0, 0.0, 0.0);
  1129. Make_Colour(New->Emission, 0.0, 0.0, 0.0);
  1130. Make_Colour(New->Extinction, 0.0, 0.0, 0.0);
  1131. Make_Colour(New->Scattering, 0.0, 0.0, 0.0);
  1132. New->use_absorption = FALSE;
  1133. New->use_emission = FALSE;
  1134. New->use_extinction = FALSE;
  1135. New->use_scattering = FALSE;
  1136. New->is_constant = FALSE;
  1137. New->sc_ext = 1.0;
  1138. New->Ratio = 0.9;
  1139. New->Confidence = 0.9;
  1140. New->Variance = 1.0 / 128.0;
  1141. New->Sample_Threshold = NULL;
  1142. New->Density = NULL;
  1143. New->Next_Media = NULL;
  1144. return (New);
  1145. }
  1146. /*****************************************************************************
  1147. *
  1148. * FUNCTION
  1149. *
  1150. * Copy_Media
  1151. *
  1152. * INPUT
  1153. *
  1154. * Old - media to copy
  1155. *
  1156. * OUTPUT
  1157. *
  1158. * RETURNS
  1159. *
  1160. * IMEDIA * - new media
  1161. *
  1162. * AUTHOR
  1163. *
  1164. * Dieter Bayer
  1165. *
  1166. * DESCRIPTION
  1167. *
  1168. * Copy an media.
  1169. *
  1170. * CHANGES
  1171. *
  1172. * Dec 1994 : Creation.
  1173. *
  1174. ******************************************************************************/
  1175. IMEDIA *Copy_Media(IMEDIA *Old)
  1176. {
  1177. int i;
  1178. IMEDIA *New, *First, *Previous, *Local_Media;
  1179. Previous = First = NULL;
  1180. if (Old != NULL)
  1181. {
  1182. for (Local_Media = Old; Local_Media != NULL; Local_Media = Local_Media->Next_Media)
  1183. {
  1184. New = Create_Media();
  1185. *New = *Local_Media;
  1186. if (Local_Media->Sample_Threshold != NULL)
  1187. {
  1188. if (New->Intervals > 0)
  1189. {
  1190. New->Sample_Threshold = (DBL *)POV_MALLOC(New->Intervals*sizeof(DBL), "sample threshold list");
  1191. for (i = 0; i < New->Intervals; i++)
  1192. {
  1193. New->Sample_Threshold[i] = Local_Media->Sample_Threshold[i];
  1194. }
  1195. }
  1196. }
  1197. New->Density = Copy_Pigment(Local_Media->Density);
  1198. if (First == NULL)
  1199. {
  1200. First = New;
  1201. }
  1202. if (Previous != NULL)
  1203. {
  1204. Previous->Next_Media = New;
  1205. }
  1206. Previous = New;
  1207. }
  1208. }
  1209. return(First);
  1210. }
  1211. /*****************************************************************************
  1212. *
  1213. * FUNCTION
  1214. *
  1215. * Destroy_Media
  1216. *
  1217. * INPUT
  1218. *
  1219. * IMedia - media to destroy
  1220. *
  1221. * OUTPUT
  1222. *
  1223. * RETURNS
  1224. *
  1225. * AUTHOR
  1226. *
  1227. * Dieter Bayer
  1228. *
  1229. * DESCRIPTION
  1230. *
  1231. * Destroy an media.
  1232. *
  1233. * CHANGES
  1234. *
  1235. * Dec 1994 : Creation.
  1236. *
  1237. ******************************************************************************/
  1238. void Destroy_Media(IMEDIA *IMedia)
  1239. {
  1240. IMEDIA *Local_Media, *Temp;
  1241. if (IMedia != NULL)
  1242. {
  1243. Local_Media = IMedia;
  1244. while (Local_Media != NULL)
  1245. {
  1246. if (Local_Media->Sample_Threshold != NULL)
  1247. {
  1248. POV_FREE(Local_Media->Sample_Threshold);
  1249. }
  1250. /* Note Destroy_Pigment also handles Density->Next */
  1251. Destroy_Pigment(Local_Media->Density);
  1252. Temp = Local_Media->Next_Media;
  1253. POV_FREE(Local_Media);
  1254. Local_Media = Temp;
  1255. }
  1256. }
  1257. }
  1258. /*****************************************************************************
  1259. *
  1260. * FUNCTION
  1261. *
  1262. * compdoubles
  1263. *
  1264. * INPUT
  1265. *
  1266. * in_a, in_b - Elements to compare
  1267. *
  1268. * OUTPUT
  1269. *
  1270. * RETURNS
  1271. *
  1272. * int - result of comparison
  1273. *
  1274. * AUTHOR
  1275. *
  1276. * Dieter Bayer
  1277. *
  1278. * DESCRIPTION
  1279. *
  1280. * -
  1281. *
  1282. * CHANGES
  1283. *
  1284. * Dec 1996 : Creation.
  1285. *
  1286. ******************************************************************************/
  1287. static int CDECL compdoubles(CONST void *in_a, CONST void *in_b)
  1288. {
  1289. DBL *a, *b;
  1290. a = (DBL *)in_a;
  1291. b = (DBL *)in_b;
  1292. if (*a < *b)
  1293. {
  1294. return (-1);
  1295. }
  1296. else
  1297. {
  1298. if (*a == *b)
  1299. {
  1300. return (0);
  1301. }
  1302. else
  1303. {
  1304. return (1);
  1305. }
  1306. }
  1307. }
  1308. /*****************************************************************************
  1309. *
  1310. * FUNCTION
  1311. *
  1312. * Transform_Media
  1313. *
  1314. * INPUT
  1315. *
  1316. * OUTPUT
  1317. *
  1318. * RETURNS
  1319. *
  1320. * AUTHOR
  1321. *
  1322. * Dieter Bayer
  1323. *
  1324. * DESCRIPTION
  1325. *
  1326. * Transform media.
  1327. *
  1328. * CHANGES
  1329. *
  1330. * Dec 1996 : Creation.
  1331. *
  1332. ******************************************************************************/
  1333. void Transform_Media(IMEDIA *IMedia, TRANSFORM *Trans)
  1334. {
  1335. IMEDIA *Temp;
  1336. if (IMedia != NULL)
  1337. {
  1338. for (Temp = IMedia; Temp != NULL; Temp = Temp->Next_Media)
  1339. {
  1340. Transform_Density(Temp->Density, Trans);
  1341. }
  1342. }
  1343. }
  1344. void Transform_Density(PIGMENT *Density, TRANSFORM *Trans)
  1345. {
  1346. TPATTERN *Temp = (TPATTERN *)Density;
  1347. while (Temp != NULL)
  1348. {
  1349. Transform_Tpattern(Temp, Trans);
  1350. Temp = Temp->Next;
  1351. }
  1352. }