MESH.C 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  1. /****************************************************************************
  2. * mesh.c
  3. *
  4. * This module implements primitives for mesh objects.
  5. *
  6. * This module was written by Dieter Bayer [DB].
  7. *
  8. * from Persistence of Vision(tm) Ray Tracer
  9. * Copyright 1996,1999 Persistence of Vision Team
  10. *---------------------------------------------------------------------------
  11. * NOTICE: This source code file is provided so that users may experiment
  12. * with enhancements to POV-Ray and to port the software to platforms other
  13. * than those supported by the POV-Ray Team. There are strict rules under
  14. * which you are permitted to use this file. The rules are in the file
  15. * named POVLEGAL.DOC which should be distributed with this file.
  16. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  17. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  18. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  19. *
  20. * This program is based on the popular DKB raytracer version 2.12.
  21. * DKBTrace was originally written by David K. Buck.
  22. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  23. *
  24. *****************************************************************************/
  25. /****************************************************************************
  26. *
  27. * Explanation:
  28. *
  29. * -
  30. *
  31. * Syntax:
  32. *
  33. * mesh
  34. * {
  35. * triangle { <CORNER1>, <CORNER2>, <CORNER3>, texture { NAME } }
  36. * smooth_triangle { <CORNER1>, <NORMAL1>, <CORNER2>, <NORMAL2>, <CORNER3>, <NORMAL3>, texture { NAME } }
  37. * ...
  38. * [ hierarchy FLAG ]
  39. * }
  40. *
  41. * ---
  42. *
  43. * Feb 1995 : Creation. [DB]
  44. *
  45. *****************************************************************************/
  46. #include "frame.h"
  47. #include "vector.h"
  48. #include "povproto.h"
  49. #include "bbox.h"
  50. #include "matrices.h"
  51. #include "objects.h"
  52. #include "mesh.h"
  53. #include "texture.h"
  54. #include "povray.h"
  55. /*****************************************************************************
  56. * Local preprocessor defines
  57. ******************************************************************************/
  58. #define DEPTH_TOLERANCE 1e-6
  59. #define max3_coordinate(x,y,z) ((x > y) ? ((x > z) ? X : Z) : ((y > z) ? Y : Z))
  60. #define HASH_SIZE 1000
  61. #define INITIAL_NUMBER_OF_ENTRIES 256
  62. /*****************************************************************************
  63. * Local typedefs
  64. ******************************************************************************/
  65. typedef struct Hash_Table_Struct HASH_TABLE;
  66. struct Hash_Table_Struct
  67. {
  68. int Index;
  69. SNGL_VECT P;
  70. HASH_TABLE *Next;
  71. };
  72. /*****************************************************************************
  73. * Static functions
  74. ******************************************************************************/
  75. static int Intersect_Mesh (RAY *Ray, MESH *Mesh, ISTACK *Depth_Stack);
  76. static int All_Mesh_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack);
  77. static int Inside_Mesh (VECTOR IPoint, OBJECT *Object);
  78. static void Mesh_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter);
  79. static MESH *Copy_Mesh (OBJECT *Object);
  80. static void Translate_Mesh (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  81. static void Rotate_Mesh (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  82. static void Scale_Mesh (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  83. static void Transform_Mesh (OBJECT *Object, TRANSFORM *Trans);
  84. static void Invert_Mesh (OBJECT *Object);
  85. static void Destroy_Mesh (OBJECT *Object);
  86. static void compute_smooth_triangle (MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3);
  87. static int intersect_mesh_triangle (RAY *Ray, MESH *Mesh, MESH_TRIANGLE *Triangle, DBL *Depth);
  88. static int test_hit (MESH_TRIANGLE *Triangle, MESH *Mesh, RAY *Ray, DBL Depth, ISTACK *Depth_Stack);
  89. static void smooth_mesh_normal (MESH *Mesh, VECTOR Result, MESH_TRIANGLE *Triangle, VECTOR IPoint);
  90. static void get_triangle_bbox (MESH *Mesh, MESH_TRIANGLE *Triangle, BBOX *BBox);
  91. static int intersect_bbox_tree (MESH *Mesh, RAY *Ray, RAY *Orig_Ray, DBL len, ISTACK *Depth_Stack);
  92. static void get_triangle_vertices (MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3);
  93. static void get_triangle_normals (MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR N1, VECTOR N2, VECTOR N3);
  94. static int mesh_hash (HASH_TABLE **Hash_Table,
  95. int *Number, int *Max, SNGL_VECT **Elements, VECTOR aPoint);
  96. /*****************************************************************************
  97. * Local variables
  98. ******************************************************************************/
  99. METHODS Mesh_Methods =
  100. {
  101. All_Mesh_Intersections,
  102. Inside_Mesh, Mesh_Normal,
  103. (COPY_METHOD)Copy_Mesh,
  104. Translate_Mesh, Rotate_Mesh,
  105. Scale_Mesh, Transform_Mesh, Invert_Mesh, Destroy_Mesh
  106. };
  107. static HASH_TABLE **Vertex_Hash_Table, **Normal_Hash_Table;
  108. static PRIORITY_QUEUE *Mesh_Queue;
  109. /*****************************************************************************
  110. *
  111. * FUNCTION
  112. *
  113. * All_Mesh_Intersections
  114. *
  115. * INPUT
  116. *
  117. * OUTPUT
  118. *
  119. * RETURNS
  120. *
  121. * AUTHOR
  122. *
  123. * Dieter Bayer
  124. *
  125. * DESCRIPTION
  126. *
  127. * -
  128. *
  129. * CHANGES
  130. *
  131. * Feb 1995 : Creation.
  132. *
  133. ******************************************************************************/
  134. static int All_Mesh_Intersections(OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack)
  135. {
  136. Increase_Counter(stats[Ray_Mesh_Tests]);
  137. if (Intersect_Mesh(Ray, (MESH *)Object, Depth_Stack))
  138. {
  139. Increase_Counter(stats[Ray_Mesh_Tests_Succeeded]);
  140. return(TRUE);
  141. }
  142. return(FALSE);
  143. }
  144. /*****************************************************************************
  145. *
  146. * FUNCTION
  147. *
  148. * Intersect_Mesh
  149. *
  150. * INPUT
  151. *
  152. * OUTPUT
  153. *
  154. * RETURNS
  155. *
  156. * AUTHOR
  157. *
  158. * Dieter Bayer
  159. *
  160. * DESCRIPTION
  161. *
  162. * -
  163. *
  164. * CHANGES
  165. *
  166. * Feb 1995 : Creation.
  167. *
  168. ******************************************************************************/
  169. static int Intersect_Mesh(RAY *Ray, MESH *Mesh, ISTACK *Depth_Stack)
  170. {
  171. int i;
  172. unsigned found;
  173. DBL len, t;
  174. RAY New_Ray;
  175. /* Transform the ray into mesh space. */
  176. if (Mesh->Trans != NULL)
  177. {
  178. MInvTransPoint(New_Ray.Initial, Ray->Initial, Mesh->Trans);
  179. MInvTransDirection(New_Ray.Direction, Ray->Direction, Mesh->Trans);
  180. VLength(len, New_Ray.Direction);
  181. VInverseScaleEq(New_Ray.Direction, len);
  182. }
  183. else
  184. {
  185. New_Ray = *Ray;
  186. len = 1.0;
  187. }
  188. found = FALSE;
  189. if (Mesh->Data->Tree == NULL)
  190. {
  191. /* There's no bounding hierarchy so just step through all elements. */
  192. for (i = 0; i < Mesh->Data->Number_Of_Triangles; i++)
  193. {
  194. if (intersect_mesh_triangle(&New_Ray, Mesh, &Mesh->Data->Triangles[i], &t))
  195. {
  196. if (test_hit(&Mesh->Data->Triangles[i], Mesh, Ray, t / len, Depth_Stack))
  197. {
  198. found = TRUE;
  199. }
  200. }
  201. }
  202. }
  203. else
  204. {
  205. /* Use the mesh's bounding hierarchy. */
  206. return(intersect_bbox_tree(Mesh, &New_Ray, Ray, len, Depth_Stack));
  207. }
  208. return(found);
  209. }
  210. /*****************************************************************************
  211. *
  212. * FUNCTION
  213. *
  214. * Inside_Mesh
  215. *
  216. * INPUT
  217. *
  218. * OUTPUT
  219. *
  220. * RETURNS
  221. *
  222. * AUTHOR
  223. *
  224. * Dieter Bayer
  225. *
  226. * DESCRIPTION
  227. *
  228. * -
  229. *
  230. * CHANGES
  231. *
  232. * Feb 1995 : Creation.
  233. *
  234. ******************************************************************************/
  235. static int Inside_Mesh(VECTOR IPoint, OBJECT *Object)
  236. {
  237. return(FALSE);
  238. }
  239. /*****************************************************************************
  240. *
  241. * FUNCTION
  242. *
  243. * Mesh_Normal
  244. *
  245. * INPUT
  246. *
  247. * OUTPUT
  248. *
  249. * RETURNS
  250. *
  251. * AUTHOR
  252. *
  253. * Dieter Bayer
  254. *
  255. * DESCRIPTION
  256. *
  257. * Return the normalized normal in the given point.
  258. *
  259. * CHANGES
  260. *
  261. * Feb 1995 : Creation.
  262. *
  263. ******************************************************************************/
  264. static void Mesh_Normal(VECTOR Result, OBJECT *Object, INTERSECTION *Inter)
  265. {
  266. VECTOR IPoint;
  267. MESH_TRIANGLE *Triangle;
  268. MESH *Mesh = (MESH *)Object;
  269. Triangle = (MESH_TRIANGLE *)Inter->Pointer;
  270. if (Triangle->Smooth)
  271. {
  272. if (Mesh->Trans != NULL)
  273. {
  274. MInvTransPoint(IPoint, Inter->IPoint, Mesh->Trans);
  275. }
  276. else
  277. {
  278. Assign_Vector(IPoint, Inter->IPoint);
  279. }
  280. smooth_mesh_normal(Mesh, Result, Triangle, IPoint);
  281. if (Mesh->Trans != NULL)
  282. {
  283. MTransNormal(Result, Result, Mesh->Trans);
  284. }
  285. VNormalize(Result, Result);
  286. }
  287. else
  288. {
  289. Assign_SNGL_Vect(Result, Mesh->Data->Normals[Triangle->Normal_Ind]);
  290. if (Mesh->Trans != NULL)
  291. {
  292. MTransNormal(Result, Result, Mesh->Trans);
  293. VNormalize(Result, Result);
  294. }
  295. }
  296. }
  297. /*****************************************************************************
  298. *
  299. * FUNCTION
  300. *
  301. * smooth_mesh_normal
  302. *
  303. * INPUT
  304. *
  305. * OUTPUT
  306. *
  307. * RETURNS
  308. *
  309. * AUTHOR
  310. *
  311. * Dieter Bayer
  312. *
  313. * DESCRIPTION
  314. *
  315. * Remove the un-normalized normal of a smoothed triangle.
  316. *
  317. * CHANGES
  318. *
  319. * Feb 1995 : Creation. (Derived from TRIANGLE.C)
  320. *
  321. ******************************************************************************/
  322. static void smooth_mesh_normal(MESH *Mesh, VECTOR Result, MESH_TRIANGLE *Triangle, VECTOR IPoint)
  323. {
  324. int axis;
  325. DBL u, v;
  326. DBL k1, k2, k3;
  327. VECTOR PIMinusP1, N1, N2, N3;
  328. get_triangle_normals(Mesh, Triangle, N1, N2, N3);
  329. VSub(PIMinusP1, IPoint, Mesh->Data->Vertices[Triangle->P1]);
  330. VDot(u, PIMinusP1, Triangle->Perp);
  331. if (u < EPSILON)
  332. {
  333. Assign_Vector(Result, N1);
  334. }
  335. else
  336. {
  337. axis = Triangle->vAxis;
  338. k1 = Mesh->Data->Vertices[Triangle->P1][axis];
  339. k2 = Mesh->Data->Vertices[Triangle->P2][axis];
  340. k3 = Mesh->Data->Vertices[Triangle->P3][axis];
  341. v = (PIMinusP1[axis] / u + k1 - k2) / (k3 - k2);
  342. Result[X] = N1[X] + u * (N2[X] - N1[X] + v * (N3[X] - N2[X]));
  343. Result[Y] = N1[Y] + u * (N2[Y] - N1[Y] + v * (N3[Y] - N2[Y]));
  344. Result[Z] = N1[Z] + u * (N2[Z] - N1[Z] + v * (N3[Z] - N2[Z]));
  345. }
  346. }
  347. /*****************************************************************************
  348. *
  349. * FUNCTION
  350. *
  351. * Translate_Mesh
  352. *
  353. * INPUT
  354. *
  355. * OUTPUT
  356. *
  357. * RETURNS
  358. *
  359. * AUTHOR
  360. *
  361. * Dieter Bayer
  362. *
  363. * DESCRIPTION
  364. *
  365. * -
  366. *
  367. * CHANGES
  368. *
  369. * Feb 1995 : Creation.
  370. *
  371. ******************************************************************************/
  372. static void Translate_Mesh(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  373. {
  374. Transform_Mesh(Object, Trans);
  375. }
  376. /*****************************************************************************
  377. *
  378. * FUNCTION
  379. *
  380. * Rotate_Mesh
  381. *
  382. * INPUT
  383. *
  384. * OUTPUT
  385. *
  386. * RETURNS
  387. *
  388. * AUTHOR
  389. *
  390. * Dieter Bayer
  391. *
  392. * DESCRIPTION
  393. *
  394. * -
  395. *
  396. * CHANGES
  397. *
  398. * Feb 1995 : Creation.
  399. *
  400. ******************************************************************************/
  401. static void Rotate_Mesh(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  402. {
  403. Transform_Mesh(Object, Trans);
  404. }
  405. /*****************************************************************************
  406. *
  407. * FUNCTION
  408. *
  409. * Scale_Mesh
  410. *
  411. * INPUT
  412. *
  413. * OUTPUT
  414. *
  415. * RETURNS
  416. *
  417. * AUTHOR
  418. *
  419. * Dieter Bayer
  420. *
  421. * DESCRIPTION
  422. *
  423. * -
  424. *
  425. * CHANGES
  426. *
  427. * Feb 1995 : Creation.
  428. *
  429. ******************************************************************************/
  430. static void Scale_Mesh(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  431. {
  432. Transform_Mesh(Object, Trans);
  433. }
  434. /*****************************************************************************
  435. *
  436. * FUNCTION
  437. *
  438. * Transfrom_Mesh
  439. *
  440. * INPUT
  441. *
  442. * OUTPUT
  443. *
  444. * RETURNS
  445. *
  446. * AUTHOR
  447. *
  448. * Dieter Bayer
  449. *
  450. * DESCRIPTION
  451. *
  452. * -
  453. *
  454. * CHANGES
  455. *
  456. * Feb 1995 : Creation.
  457. *
  458. ******************************************************************************/
  459. static void Transform_Mesh(OBJECT *Object, TRANSFORM *Trans)
  460. {
  461. int i;
  462. if (((MESH *)Object)->Trans == NULL)
  463. {
  464. ((MESH *)Object)->Trans = Create_Transform();
  465. }
  466. Recompute_BBox(&Object->BBox, Trans);
  467. Compose_Transforms(((MESH *)Object)->Trans, Trans);
  468. for (i=0; i<((MESH *)Object)->Data->Number_Of_Textures; i++)
  469. {
  470. Transform_Textures(((MESH *)Object)->Data->Textures[i], Trans);
  471. }
  472. }
  473. /*****************************************************************************
  474. *
  475. * FUNCTION
  476. *
  477. * Invert_Mesh
  478. *
  479. * INPUT
  480. *
  481. * OUTPUT
  482. *
  483. * RETURNS
  484. *
  485. * AUTHOR
  486. *
  487. * Dieter Bayer
  488. *
  489. * DESCRIPTION
  490. *
  491. * -
  492. *
  493. * CHANGES
  494. *
  495. * Feb 1995 : Creation.
  496. *
  497. ******************************************************************************/
  498. static void Invert_Mesh(OBJECT *Object)
  499. {
  500. }
  501. /*****************************************************************************
  502. *
  503. * FUNCTION
  504. *
  505. * Create_Mesh
  506. *
  507. * INPUT
  508. *
  509. * OUTPUT
  510. *
  511. * RETURNS
  512. *
  513. * AUTHOR
  514. *
  515. * Dieter Bayer
  516. *
  517. * DESCRIPTION
  518. *
  519. * -
  520. *
  521. * CHANGES
  522. *
  523. * Feb 1995 : Creation.
  524. *
  525. ******************************************************************************/
  526. MESH *Create_Mesh()
  527. {
  528. MESH *New;
  529. New = (MESH *)POV_MALLOC(sizeof(MESH), "mesh");
  530. INIT_OBJECT_FIELDS(New,MESH_OBJECT,&Mesh_Methods)
  531. Set_Flag(New, HIERARCHY_FLAG);
  532. New->Trans = NULL;
  533. New->Data = NULL;
  534. return(New);
  535. }
  536. /*****************************************************************************
  537. *
  538. * FUNCTION
  539. *
  540. * Copy_Mesh
  541. *
  542. * INPUT
  543. *
  544. * OUTPUT
  545. *
  546. * RETURNS
  547. *
  548. * AUTHOR
  549. *
  550. * Dieter Bayer
  551. *
  552. * DESCRIPTION
  553. *
  554. * Copy a mesh.
  555. *
  556. * NOTE: The components are not copied, only the number of references is
  557. * counted, so that Destroy_Mesh() knows if they can be destroyed.
  558. *
  559. * -
  560. *
  561. * CHANGES
  562. *
  563. * Feb 1995 : Creation.
  564. *
  565. ******************************************************************************/
  566. static MESH *Copy_Mesh(OBJECT *Object)
  567. {
  568. MESH *New;
  569. New = Create_Mesh();
  570. /* Copy mesh. */
  571. *New = *((MESH *)Object);
  572. New->Trans = Copy_Transform(New->Trans);
  573. New->Data->References++;
  574. return(New);
  575. }
  576. /*****************************************************************************
  577. *
  578. * FUNCTION
  579. *
  580. * Destroy_Mesh
  581. *
  582. * INPUT
  583. *
  584. * OUTPUT
  585. *
  586. * RETURNS
  587. *
  588. * AUTHOR
  589. *
  590. * Dieter Bayer
  591. *
  592. * DESCRIPTION
  593. *
  594. * -
  595. *
  596. * CHANGES
  597. *
  598. * Feb 1995 : Creation.
  599. *
  600. ******************************************************************************/
  601. static void Destroy_Mesh(OBJECT *Object)
  602. {
  603. int i;
  604. MESH *Mesh = (MESH *)Object;
  605. Destroy_Transform(Mesh->Trans);
  606. if (--(Mesh->Data->References) == 0)
  607. {
  608. Destroy_BBox_Tree(Mesh->Data->Tree);
  609. if (Mesh->Data->Normals != NULL)
  610. {
  611. POV_FREE(Mesh->Data->Normals);
  612. }
  613. if (Mesh->Data->Vertices != NULL)
  614. {
  615. POV_FREE(Mesh->Data->Vertices);
  616. }
  617. if (Mesh->Data->Triangles != NULL)
  618. {
  619. POV_FREE(Mesh->Data->Triangles);
  620. }
  621. if (Mesh->Data->Textures != NULL)
  622. {
  623. for (i = 0; i < Mesh->Data->Number_Of_Textures; i++)
  624. {
  625. Destroy_Textures(Mesh->Data->Textures[i]);
  626. }
  627. POV_FREE(Mesh->Data->Textures);
  628. }
  629. POV_FREE(Mesh->Data);
  630. }
  631. POV_FREE(Object);
  632. }
  633. /*****************************************************************************
  634. *
  635. * FUNCTION
  636. *
  637. * Compute_Mesh_BBox
  638. *
  639. * INPUT
  640. *
  641. * Mesh - Mesh
  642. *
  643. * OUTPUT
  644. *
  645. * Mesh
  646. *
  647. * RETURNS
  648. *
  649. * AUTHOR
  650. *
  651. * Dieter Bayer
  652. *
  653. * DESCRIPTION
  654. *
  655. * Calculate the bounding box of a triangle.
  656. *
  657. * CHANGES
  658. *
  659. * Feb 1995 : Creation.
  660. *
  661. ******************************************************************************/
  662. void Compute_Mesh_BBox(MESH *Mesh)
  663. {
  664. int i;
  665. VECTOR P1, P2, P3;
  666. VECTOR mins, maxs;
  667. Make_Vector(mins, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  668. Make_Vector(maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  669. for (i = 0; i < Mesh->Data->Number_Of_Triangles; i++)
  670. {
  671. get_triangle_vertices(Mesh, &Mesh->Data->Triangles[i], P1, P2, P3);
  672. mins[X] = min(mins[X], min3(P1[X], P2[X], P3[X]));
  673. mins[Y] = min(mins[Y], min3(P1[Y], P2[Y], P3[Y]));
  674. mins[Z] = min(mins[Z], min3(P1[Z], P2[Z], P3[Z]));
  675. maxs[X] = max(maxs[X], max3(P1[X], P2[X], P3[X]));
  676. maxs[Y] = max(maxs[Y], max3(P1[Y], P2[Y], P3[Y]));
  677. maxs[Z] = max(maxs[Z], max3(P1[Z], P2[Z], P3[Z]));
  678. }
  679. Make_BBox_from_min_max(Mesh->BBox, mins, maxs);
  680. }
  681. /*****************************************************************************
  682. *
  683. * FUNCTION
  684. *
  685. * Compute_Mesh
  686. *
  687. * INPUT
  688. *
  689. * OUTPUT
  690. *
  691. * RETURNS
  692. *
  693. * AUTHOR
  694. *
  695. * Dieter Bayer
  696. *
  697. * DESCRIPTION
  698. *
  699. * -
  700. *
  701. * CHANGES
  702. *
  703. * Feb 1995 : Creation.
  704. *
  705. ******************************************************************************/
  706. int Compute_Mesh_Triangle(MESH_TRIANGLE *Triangle, int Smooth, VECTOR P1, VECTOR P2, VECTOR P3, VECTOR S_Normal)
  707. {
  708. int temp, swap;
  709. DBL x, y, z;
  710. VECTOR V1, V2, T1;
  711. DBL Length;
  712. VSub(V1, P2, P1);
  713. VSub(V2, P3, P1);
  714. VCross(S_Normal, V2, V1);
  715. VLength(Length, S_Normal);
  716. /* Set up a flag so we can ignore degenerate triangles */
  717. if (Length == 0.0)
  718. {
  719. return(FALSE);
  720. }
  721. /* Normalize the normal vector. */
  722. VInverseScaleEq(S_Normal, Length);
  723. VDot(Triangle->Distance, S_Normal, P1);
  724. Triangle->Distance *= -1.0;
  725. /* Find triangle's dominant axis. */
  726. x = fabs(S_Normal[X]);
  727. y = fabs(S_Normal[Y]);
  728. z = fabs(S_Normal[Z]);
  729. Triangle->Dominant_Axis = max3_coordinate(x, y, z);
  730. swap = FALSE;
  731. switch (Triangle->Dominant_Axis)
  732. {
  733. case X:
  734. if ((P2[Y] - P3[Y])*(P2[Z] - P1[Z]) < (P2[Z] - P3[Z])*(P2[Y] - P1[Y]))
  735. {
  736. swap = TRUE;
  737. }
  738. break;
  739. case Y:
  740. if ((P2[X] - P3[X])*(P2[Z] - P1[Z]) < (P2[Z] - P3[Z])*(P2[X] - P1[X]))
  741. {
  742. swap = TRUE;
  743. }
  744. break;
  745. case Z:
  746. if ((P2[X] - P3[X])*(P2[Y] - P1[Y]) < (P2[Y] - P3[Y])*(P2[X] - P1[X]))
  747. {
  748. swap = TRUE;
  749. }
  750. break;
  751. }
  752. if (swap)
  753. {
  754. temp = Triangle->P2;
  755. Triangle->P2 = Triangle->P1;
  756. Triangle->P1 = temp;
  757. Assign_Vector(T1, P1);
  758. Assign_Vector(P1, P2);
  759. Assign_Vector(P2, T1);
  760. if (Smooth)
  761. {
  762. temp = Triangle->N2;
  763. Triangle->N2 = Triangle->N1;
  764. Triangle->N1 = temp;
  765. }
  766. }
  767. if (Smooth)
  768. {
  769. compute_smooth_triangle(Triangle, P1, P2, P3);
  770. }
  771. return(TRUE);
  772. }
  773. /*****************************************************************************
  774. *
  775. * FUNCTION
  776. *
  777. * compute_smooth_triangle
  778. *
  779. * INPUT
  780. *
  781. * OUTPUT
  782. *
  783. * RETURNS
  784. *
  785. * AUTHOR
  786. *
  787. * Dieter Bayer
  788. *
  789. * DESCRIPTION
  790. *
  791. * -
  792. *
  793. * CHANGES
  794. *
  795. * Feb 1995 : Creation.
  796. *
  797. ******************************************************************************/
  798. static void compute_smooth_triangle(MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3)
  799. {
  800. VECTOR P3MinusP2, VTemp1, VTemp2;
  801. DBL x, y, z, uDenominator, Proj;
  802. Triangle->Smooth = TRUE;
  803. VSub(P3MinusP2, P3, P2);
  804. x = fabs(P3MinusP2[X]);
  805. y = fabs(P3MinusP2[Y]);
  806. z = fabs(P3MinusP2[Z]);
  807. Triangle->vAxis = max3_coordinate(x, y, z);
  808. VSub(VTemp1, P2, P3);
  809. VNormalize(VTemp1, VTemp1);
  810. VSub(VTemp2, P1, P3);
  811. VDot(Proj, VTemp2, VTemp1);
  812. VScaleEq(VTemp1, Proj);
  813. VSub(Triangle->Perp, VTemp1, VTemp2);
  814. VNormalize(Triangle->Perp, Triangle->Perp);
  815. VDot(uDenominator, VTemp2, Triangle->Perp);
  816. VInverseScaleEq(Triangle->Perp, -uDenominator);
  817. }
  818. /*****************************************************************************
  819. *
  820. * FUNCTION
  821. *
  822. * intersect_mesh_triangle
  823. *
  824. * INPUT
  825. *
  826. * OUTPUT
  827. *
  828. * RETURNS
  829. *
  830. * AUTHOR
  831. *
  832. * Dieter Bayer
  833. *
  834. * DESCRIPTION
  835. *
  836. * -
  837. *
  838. * CHANGES
  839. *
  840. * Feb 1995 : Creation.
  841. *
  842. ******************************************************************************/
  843. static int intersect_mesh_triangle(RAY *Ray, MESH *Mesh, MESH_TRIANGLE *Triangle, DBL *Depth)
  844. {
  845. DBL NormalDotOrigin, NormalDotDirection;
  846. DBL s, t;
  847. VECTOR P1, P2, P3, S_Normal;
  848. Assign_SNGL_Vect(S_Normal, Mesh->Data->Normals[Triangle->Normal_Ind]);
  849. VDot(NormalDotDirection, S_Normal, Ray->Direction);
  850. if (fabs(NormalDotDirection) < EPSILON)
  851. {
  852. return(FALSE);
  853. }
  854. VDot(NormalDotOrigin, S_Normal, Ray->Initial);
  855. *Depth = -(Triangle->Distance + NormalDotOrigin) / NormalDotDirection;
  856. if ((*Depth < DEPTH_TOLERANCE) || (*Depth > Max_Distance))
  857. {
  858. return(FALSE);
  859. }
  860. get_triangle_vertices(Mesh, Triangle, P1, P2, P3);
  861. switch (Triangle->Dominant_Axis)
  862. {
  863. case X:
  864. s = Ray->Initial[Y] + *Depth * Ray->Direction[Y];
  865. t = Ray->Initial[Z] + *Depth * Ray->Direction[Z];
  866. if ((P2[Y] - s) * (P2[Z] - P1[Z]) < (P2[Z] - t) * (P2[Y] - P1[Y]))
  867. {
  868. return(FALSE);
  869. }
  870. if ((P3[Y] - s) * (P3[Z] - P2[Z]) < (P3[Z] - t) * (P3[Y] - P2[Y]))
  871. {
  872. return(FALSE);
  873. }
  874. if ((P1[Y] - s) * (P1[Z] - P3[Z]) < (P1[Z] - t) * (P1[Y] - P3[Y]))
  875. {
  876. return(FALSE);
  877. }
  878. return(TRUE);
  879. case Y:
  880. s = Ray->Initial[X] + *Depth * Ray->Direction[X];
  881. t = Ray->Initial[Z] + *Depth * Ray->Direction[Z];
  882. if ((P2[X] - s) * (P2[Z] - P1[Z]) < (P2[Z] - t) * (P2[X] - P1[X]))
  883. {
  884. return(FALSE);
  885. }
  886. if ((P3[X] - s) * (P3[Z] - P2[Z]) < (P3[Z] - t) * (P3[X] - P2[X]))
  887. {
  888. return(FALSE);
  889. }
  890. if ((P1[X] - s) * (P1[Z] - P3[Z]) < (P1[Z] - t) * (P1[X] - P3[X]))
  891. {
  892. return(FALSE);
  893. }
  894. return(TRUE);
  895. case Z:
  896. s = Ray->Initial[X] + *Depth * Ray->Direction[X];
  897. t = Ray->Initial[Y] + *Depth * Ray->Direction[Y];
  898. if ((P2[X] - s) * (P2[Y] - P1[Y]) < (P2[Y] - t) * (P2[X] - P1[X]))
  899. {
  900. return(FALSE);
  901. }
  902. if ((P3[X] - s) * (P3[Y] - P2[Y]) < (P3[Y] - t) * (P3[X] - P2[X]))
  903. {
  904. return(FALSE);
  905. }
  906. if ((P1[X] - s) * (P1[Y] - P3[Y]) < (P1[Y] - t) * (P1[X] - P3[X]))
  907. {
  908. return(FALSE);
  909. }
  910. return(TRUE);
  911. }
  912. return(FALSE);
  913. }
  914. /*****************************************************************************
  915. *
  916. * FUNCTION
  917. *
  918. * test_hit
  919. *
  920. * INPUT
  921. *
  922. * OUTPUT
  923. *
  924. * RETURNS
  925. *
  926. * AUTHOR
  927. *
  928. * Dieter Bayer
  929. *
  930. * DESCRIPTION
  931. *
  932. * Test if a hit is valid and push if on the intersection depth.
  933. *
  934. * CHANGES
  935. *
  936. * Feb 1995 : Creation.
  937. *
  938. ******************************************************************************/
  939. static int test_hit(MESH_TRIANGLE *Triangle, MESH *Mesh, RAY *Ray, DBL Depth, ISTACK *Depth_Stack)
  940. {
  941. VECTOR IPoint;
  942. OBJECT *Object = (OBJECT *)Mesh;
  943. VEvaluateRay(IPoint, Ray->Initial, Depth, Ray->Direction);
  944. if (Point_In_Clip(IPoint, Object->Clip))
  945. {
  946. push_entry_pointer(Depth, IPoint, Object, Triangle, Depth_Stack);
  947. return(TRUE);
  948. }
  949. return(FALSE);
  950. }
  951. /*****************************************************************************
  952. *
  953. * FUNCTION
  954. *
  955. * Init_Mesh_Triangle
  956. *
  957. * INPUT
  958. *
  959. * OUTPUT
  960. *
  961. * RETURNS
  962. *
  963. * AUTHOR
  964. *
  965. * Dieter Bayer
  966. *
  967. * DESCRIPTION
  968. *
  969. * -
  970. *
  971. * CHANGES
  972. *
  973. * Feb 1995 : Creation.
  974. *
  975. ******************************************************************************/
  976. void Init_Mesh_Triangle(MESH_TRIANGLE *Triangle)
  977. {
  978. Triangle->Smooth = FALSE;
  979. Triangle->Dominant_Axis = 0;
  980. Triangle->vAxis = 0;
  981. Triangle->P1 =
  982. Triangle->P2 =
  983. Triangle->P3 = -1;
  984. Triangle->Normal_Ind = -1;
  985. Triangle->Texture = -1;
  986. Triangle->N1 =
  987. Triangle->N2 =
  988. Triangle->N3 = -1;
  989. Make_Vector(Triangle->Perp, 0.0, 0.0, 0.0);
  990. Triangle->Distance = 0.0;
  991. }
  992. /*****************************************************************************
  993. *
  994. * FUNCTION
  995. *
  996. * get_triangle_bbox
  997. *
  998. * INPUT
  999. *
  1000. * Triangle - Pointer to triangle
  1001. *
  1002. * OUTPUT
  1003. *
  1004. * BBox - Bounding box
  1005. *
  1006. * RETURNS
  1007. *
  1008. * AUTHOR
  1009. *
  1010. * Dieter Bayer
  1011. *
  1012. * DESCRIPTION
  1013. *
  1014. * Calculate the bounding box of a triangle.
  1015. *
  1016. * CHANGES
  1017. *
  1018. * Sep 1994 : Creation.
  1019. *
  1020. ******************************************************************************/
  1021. static void get_triangle_bbox(MESH *Mesh, MESH_TRIANGLE *Triangle, BBOX *BBox)
  1022. {
  1023. VECTOR P1, P2, P3;
  1024. VECTOR Min, Max;
  1025. get_triangle_vertices(Mesh, Triangle, P1, P2, P3);
  1026. Min[X] = min3(P1[X], P2[X], P3[X]);
  1027. Min[Y] = min3(P1[Y], P2[Y], P3[Y]);
  1028. Min[Z] = min3(P1[Z], P2[Z], P3[Z]);
  1029. Max[X] = max3(P1[X], P2[X], P3[X]);
  1030. Max[Y] = max3(P1[Y], P2[Y], P3[Y]);
  1031. Max[Z] = max3(P1[Z], P2[Z], P3[Z]);
  1032. Make_BBox_from_min_max(*BBox, Min, Max);
  1033. }
  1034. /*****************************************************************************
  1035. *
  1036. * FUNCTION
  1037. *
  1038. * Build_Mesh_BBox_Tree
  1039. *
  1040. * INPUT
  1041. *
  1042. * OUTPUT
  1043. *
  1044. * RETURNS
  1045. *
  1046. * AUTHOR
  1047. *
  1048. * Dieter Bayer
  1049. *
  1050. * DESCRIPTION
  1051. *
  1052. * Create the bounding box hierarchy.
  1053. *
  1054. * CHANGES
  1055. *
  1056. * Feb 1995 : Creation. (Derived from the bounding slab creation code)
  1057. *
  1058. ******************************************************************************/
  1059. void Build_Mesh_BBox_Tree(MESH *Mesh)
  1060. {
  1061. int i, nElem, maxelements;
  1062. BBOX_TREE **Triangles;
  1063. if (!Test_Flag(Mesh, HIERARCHY_FLAG))
  1064. {
  1065. return;
  1066. }
  1067. nElem = (int)Mesh->Data->Number_Of_Triangles;
  1068. maxelements = 2 * nElem;
  1069. /* Now allocate an array to hold references to these elements. */
  1070. Triangles = (BBOX_TREE **)POV_MALLOC(maxelements*sizeof(BBOX_TREE *), "mesh bbox tree");
  1071. /* Init list with mesh elements. */
  1072. for (i = 0; i < nElem; i++)
  1073. {
  1074. Triangles[i] = (BBOX_TREE *)POV_MALLOC(sizeof(BBOX_TREE), "mesh bbox tree");
  1075. Triangles[i]->Infinite = FALSE;
  1076. Triangles[i]->Entries = 0;
  1077. Triangles[i]->Node = (BBOX_TREE **)&Mesh->Data->Triangles[i];
  1078. get_triangle_bbox(Mesh, &Mesh->Data->Triangles[i], &Triangles[i]->BBox);
  1079. }
  1080. Build_BBox_Tree(&Mesh->Data->Tree, nElem, Triangles, 0, NULL);
  1081. /* Get rid of the Triangles array. */
  1082. POV_FREE(Triangles);
  1083. }
  1084. /*****************************************************************************
  1085. *
  1086. * FUNCTION
  1087. *
  1088. * intersect_bbox_tree
  1089. *
  1090. * INPUT
  1091. *
  1092. * Mesh - Mesh object
  1093. * Ray - Current ray
  1094. * Orig_Ray - Original, untransformed ray
  1095. * len - Length of the transformed ray direction
  1096. *
  1097. * OUTPUT
  1098. *
  1099. * Depth_Stack - Stack of intersections
  1100. *
  1101. * RETURNS
  1102. *
  1103. * int - TRUE if an intersection was found
  1104. *
  1105. * AUTHOR
  1106. *
  1107. * Dieter Bayer
  1108. *
  1109. * DESCRIPTION
  1110. *
  1111. * Intersect a ray with the bounding box tree of a mesh.
  1112. *
  1113. * CHANGES
  1114. *
  1115. * Feb 1995 : Creation.
  1116. *
  1117. ******************************************************************************/
  1118. static int intersect_bbox_tree(MESH *Mesh, RAY *Ray, RAY *Orig_Ray, DBL len, ISTACK *Depth_Stack)
  1119. {
  1120. int i, found;
  1121. DBL Best, Depth;
  1122. RAYINFO rayinfo;
  1123. BBOX_TREE *Node, *Root;
  1124. /* Create the direction vectors for this ray. */
  1125. Create_Rayinfo(Ray, &rayinfo);
  1126. /* Start with an empty priority queue. */
  1127. found = 0;
  1128. Mesh_Queue->QSize = 0;
  1129. Best = BOUND_HUGE;
  1130. #ifdef BBOX_EXTRA_STATS
  1131. Increase_Counter(stats[totalQueueResets]);
  1132. #endif
  1133. /* Check top node. */
  1134. Root = Mesh->Data->Tree;
  1135. /* Set the root object infinite to avoid a test. */
  1136. Check_And_Enqueue(Mesh_Queue, Root, &Root->BBox, &rayinfo);
  1137. /* Check elements in the priority queue. */
  1138. while (Mesh_Queue->QSize > 0)
  1139. {
  1140. Priority_Queue_Delete(Mesh_Queue, &Depth, &Node);
  1141. /*
  1142. * If current intersection is larger than the best intersection found
  1143. * so far our task is finished, because all other bounding boxes in
  1144. * the priority queue are further away.
  1145. */
  1146. if (Depth > Best)
  1147. {
  1148. break;
  1149. }
  1150. /* Check current node. */
  1151. if (Node->Entries)
  1152. {
  1153. /* This is a node containing leaves to be checked. */
  1154. for (i = 0; i < Node->Entries; i++)
  1155. {
  1156. Check_And_Enqueue(Mesh_Queue, Node->Node[i], &Node->Node[i]->BBox, &rayinfo);
  1157. }
  1158. }
  1159. else
  1160. {
  1161. /* This is a leaf so test the contained triangle. */
  1162. if (intersect_mesh_triangle(Ray, Mesh, (MESH_TRIANGLE *)Node->Node, &Depth))
  1163. {
  1164. if (test_hit((MESH_TRIANGLE *)Node->Node, Mesh, Orig_Ray, Depth / len, Depth_Stack))
  1165. {
  1166. found = TRUE;
  1167. Best = Depth;
  1168. }
  1169. }
  1170. }
  1171. }
  1172. return(found);
  1173. }
  1174. /*****************************************************************************
  1175. *
  1176. * FUNCTION
  1177. *
  1178. * mesh_hash
  1179. *
  1180. * INPUT
  1181. *
  1182. * aPoint - Normal/Vertex to store
  1183. *
  1184. * OUTPUT
  1185. *
  1186. * Hash_Table - Normal/Vertex hash table
  1187. * Number - Number of normals/vertices
  1188. * Max - Max. number of normals/vertices
  1189. * Elements - List of normals/vertices
  1190. *
  1191. * RETURNS
  1192. *
  1193. * int - Index of normal/vertex into the normals/vertices list
  1194. *
  1195. * AUTHOR
  1196. *
  1197. * Dieter Bayer
  1198. *
  1199. * DESCRIPTION
  1200. *
  1201. * Try to locate a triangle normal/vertex in the normal/vertex list.
  1202. * If the vertex is not found its stored in the normal/vertex list.
  1203. *
  1204. * CHANGES
  1205. *
  1206. * Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1207. *
  1208. ******************************************************************************/
  1209. static int mesh_hash(HASH_TABLE **Hash_Table, int *Number, int *Max, SNGL_VECT **Elements, VECTOR aPoint)
  1210. {
  1211. int hash;
  1212. SNGL_VECT D, P;
  1213. HASH_TABLE *p;
  1214. Assign_SNGL_Vect(P, aPoint);
  1215. /* Get hash value. */
  1216. hash = (unsigned)((int)(326.0*P[X])^(int)(694.7*P[Y])^(int)(1423.6*P[Z])) % HASH_SIZE;
  1217. /* Try to find normal/vertex. */
  1218. for (p = Hash_Table[hash]; p != NULL; p = p->Next)
  1219. {
  1220. VSub(D, p->P, P);
  1221. if ((fabs(D[X]) < EPSILON) && (fabs(D[Y]) < EPSILON) && (fabs(D[Z]) < EPSILON))
  1222. {
  1223. break;
  1224. }
  1225. }
  1226. if ((p != NULL) && (p->Index >= 0))
  1227. {
  1228. return(p->Index);
  1229. }
  1230. /* Add new normal/vertex to the list and hash table. */
  1231. if ((*Number) >= (*Max))
  1232. {
  1233. if ((*Max) >= INT_MAX/2)
  1234. {
  1235. Error("Too many normals/vertices in mesh.\n");
  1236. }
  1237. (*Max) *= 2;
  1238. (*Elements) = (SNGL_VECT *)POV_REALLOC((*Elements), (*Max)*sizeof(SNGL_VECT), "mesh data");
  1239. }
  1240. Assign_SNGL_Vect((*Elements)[*Number], P);
  1241. p = (HASH_TABLE *)POV_MALLOC(sizeof(HASH_TABLE), "mesh data");
  1242. Assign_SNGL_Vect(p->P, P);
  1243. p->Index = *Number;
  1244. p->Next = Hash_Table[hash];
  1245. Hash_Table[hash] = p;
  1246. return((*Number)++);
  1247. }
  1248. /*****************************************************************************
  1249. *
  1250. * FUNCTION
  1251. *
  1252. * Mesh_Hash_Vertex
  1253. *
  1254. * INPUT
  1255. *
  1256. * Vertex - Vertex to store
  1257. *
  1258. * OUTPUT
  1259. *
  1260. * Number_Of_Vertices - Number of vertices
  1261. * Max_Vertices - Max. number of vertices
  1262. * Vertices - List of vertices
  1263. *
  1264. * RETURNS
  1265. *
  1266. * int - Index of vertex into the vertices list
  1267. *
  1268. * AUTHOR
  1269. *
  1270. * Dieter Bayer
  1271. *
  1272. * DESCRIPTION
  1273. *
  1274. * Try to locate a triangle vertex in the vertex list.
  1275. * If the vertex is not found its stored in the vertex list.
  1276. *
  1277. * CHANGES
  1278. *
  1279. * Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1280. *
  1281. ******************************************************************************/
  1282. int Mesh_Hash_Vertex(int *Number_Of_Vertices, int *Max_Vertices, SNGL_VECT **Vertices, VECTOR Vertex)
  1283. {
  1284. return(mesh_hash(Vertex_Hash_Table, Number_Of_Vertices, Max_Vertices, Vertices, Vertex));
  1285. }
  1286. /*****************************************************************************
  1287. *
  1288. * FUNCTION
  1289. *
  1290. * Mesh_Hash_Normal
  1291. *
  1292. * INPUT
  1293. *
  1294. * Normal - Normal to store
  1295. *
  1296. * OUTPUT
  1297. *
  1298. * Number_Of_Normals - Number of normals
  1299. * Max_Normals - Max. number of normals
  1300. * Normals - List of normals
  1301. *
  1302. * RETURNS
  1303. *
  1304. * int - Index of normal into the normals list
  1305. *
  1306. * AUTHOR
  1307. *
  1308. * Dieter Bayer
  1309. *
  1310. * DESCRIPTION
  1311. *
  1312. * Try to locate a triangle normal in the normal list.
  1313. * If the normal is not found its stored in the normal list.
  1314. *
  1315. * CHANGES
  1316. *
  1317. * Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1318. *
  1319. ******************************************************************************/
  1320. int Mesh_Hash_Normal(int *Number_Of_Normals, int *Max_Normals, SNGL_VECT **Normals, VECTOR S_Normal)
  1321. {
  1322. return(mesh_hash(Normal_Hash_Table, Number_Of_Normals, Max_Normals, Normals, S_Normal));
  1323. }
  1324. /*****************************************************************************
  1325. *
  1326. * FUNCTION
  1327. *
  1328. * Mesh_Hash_Texture
  1329. *
  1330. * INPUT
  1331. *
  1332. * Texture - Texture to store
  1333. *
  1334. * OUTPUT
  1335. *
  1336. * Number_Of_Textures - Number of textures
  1337. * Max_Textures - Max. number of textures
  1338. * Textures - List of textures
  1339. *
  1340. * RETURNS
  1341. *
  1342. * int - Index of texture into the texture list
  1343. *
  1344. * AUTHOR
  1345. *
  1346. * Dieter Bayer
  1347. *
  1348. * DESCRIPTION
  1349. *
  1350. * Try to locate a texture in the texture list.
  1351. * If the texture is not found its stored in the texture list.
  1352. *
  1353. * CHANGES
  1354. *
  1355. * Feb 1995 : Creation.
  1356. *
  1357. ******************************************************************************/
  1358. int Mesh_Hash_Texture(int *Number_Of_Textures, int *Max_Textures, TEXTURE ***Textures, TEXTURE *Texture)
  1359. {
  1360. int i;
  1361. if (Texture == NULL)
  1362. {
  1363. return(-1);
  1364. }
  1365. /* Just do a linear search. */
  1366. for (i = 0; i < *Number_Of_Textures; i++)
  1367. {
  1368. if ((*Textures)[i] == Texture)
  1369. {
  1370. break;
  1371. }
  1372. }
  1373. if (i == *Number_Of_Textures)
  1374. {
  1375. if ((*Number_Of_Textures) >= (*Max_Textures))
  1376. {
  1377. if ((*Max_Textures) >= INT_MAX/2)
  1378. {
  1379. Error("Too many textures in mesh.\n");
  1380. }
  1381. (*Max_Textures) *= 2;
  1382. (*Textures) = (TEXTURE **)POV_REALLOC((*Textures), (*Max_Textures)*sizeof(TEXTURE *), "mesh data");
  1383. }
  1384. (*Textures)[(*Number_Of_Textures)++] = Copy_Texture_Pointer(Texture);
  1385. }
  1386. return(i);
  1387. }
  1388. /*****************************************************************************
  1389. *
  1390. * FUNCTION
  1391. *
  1392. * Create_Mesh_Hash_Tables
  1393. *
  1394. * INPUT
  1395. *
  1396. * OUTPUT
  1397. *
  1398. * RETURNS
  1399. *
  1400. * AUTHOR
  1401. *
  1402. * Dieter Bayer
  1403. *
  1404. * DESCRIPTION
  1405. *
  1406. * -
  1407. *
  1408. * CHANGES
  1409. *
  1410. * Feb 1995 : Creation.
  1411. *
  1412. ******************************************************************************/
  1413. void Create_Mesh_Hash_Tables()
  1414. {
  1415. int i;
  1416. Vertex_Hash_Table = (HASH_TABLE **)POV_MALLOC(HASH_SIZE*sizeof(HASH_TABLE *), "mesh hash table");
  1417. for (i = 0; i < HASH_SIZE; i++)
  1418. {
  1419. Vertex_Hash_Table[i] = NULL;
  1420. }
  1421. Normal_Hash_Table = (HASH_TABLE **)POV_MALLOC(HASH_SIZE*sizeof(HASH_TABLE *), "mesh hash table");
  1422. for (i = 0; i < HASH_SIZE; i++)
  1423. {
  1424. Normal_Hash_Table[i] = NULL;
  1425. }
  1426. }
  1427. /*****************************************************************************
  1428. *
  1429. * FUNCTION
  1430. *
  1431. * Destroy_Mesh_Hash_Tables
  1432. *
  1433. * INPUT
  1434. *
  1435. * OUTPUT
  1436. *
  1437. * RETURNS
  1438. *
  1439. * AUTHOR
  1440. *
  1441. * Dieter Bayer
  1442. *
  1443. * DESCRIPTION
  1444. *
  1445. * -
  1446. *
  1447. * CHANGES
  1448. *
  1449. * Feb 1995 : Creation.
  1450. *
  1451. ******************************************************************************/
  1452. void Destroy_Mesh_Hash_Tables()
  1453. {
  1454. int i;
  1455. HASH_TABLE *Temp;
  1456. for (i = 0; i < HASH_SIZE; i++)
  1457. {
  1458. while (Vertex_Hash_Table[i] != NULL)
  1459. {
  1460. Temp = Vertex_Hash_Table[i];
  1461. Vertex_Hash_Table[i] = Temp->Next;
  1462. POV_FREE(Temp);
  1463. }
  1464. }
  1465. POV_FREE(Vertex_Hash_Table);
  1466. for (i = 0; i < HASH_SIZE; i++)
  1467. {
  1468. while (Normal_Hash_Table[i] != NULL)
  1469. {
  1470. Temp = Normal_Hash_Table[i];
  1471. Normal_Hash_Table[i] = Temp->Next;
  1472. POV_FREE(Temp);
  1473. }
  1474. }
  1475. POV_FREE(Normal_Hash_Table);
  1476. }
  1477. /*****************************************************************************
  1478. *
  1479. * FUNCTION
  1480. *
  1481. * get_triangle_vertices
  1482. *
  1483. * INPUT
  1484. *
  1485. * Mesh - Mesh object
  1486. * Triangle - Triangle
  1487. *
  1488. * OUTPUT
  1489. *
  1490. * RETURNS
  1491. *
  1492. * P1, P2, P3 - Vertices of the triangle
  1493. *
  1494. * AUTHOR
  1495. *
  1496. * Dieter Bayer
  1497. *
  1498. * DESCRIPTION
  1499. *
  1500. * -
  1501. *
  1502. * CHANGES
  1503. *
  1504. * Feb 1995 : Creation.
  1505. *
  1506. ******************************************************************************/
  1507. static void get_triangle_vertices(MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3)
  1508. {
  1509. Assign_SNGL_Vect(P1, Mesh->Data->Vertices[Triangle->P1]);
  1510. Assign_SNGL_Vect(P2, Mesh->Data->Vertices[Triangle->P2]);
  1511. Assign_SNGL_Vect(P3, Mesh->Data->Vertices[Triangle->P3]);
  1512. }
  1513. /*****************************************************************************
  1514. *
  1515. * FUNCTION
  1516. *
  1517. * get_triangle_normals
  1518. *
  1519. * INPUT
  1520. *
  1521. * Mesh - Mesh object
  1522. * Triangle - Triangle
  1523. *
  1524. * OUTPUT
  1525. *
  1526. * RETURNS
  1527. *
  1528. * N1, N2, N3 - Normals of the triangle
  1529. *
  1530. * AUTHOR
  1531. *
  1532. * Dieter Bayer
  1533. *
  1534. * DESCRIPTION
  1535. *
  1536. * -
  1537. *
  1538. * CHANGES
  1539. *
  1540. * Feb 1995 : Creation.
  1541. *
  1542. ******************************************************************************/
  1543. static void get_triangle_normals(MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR N1, VECTOR N2, VECTOR N3)
  1544. {
  1545. Assign_SNGL_Vect(N1, Mesh->Data->Normals[Triangle->N1]);
  1546. Assign_SNGL_Vect(N2, Mesh->Data->Normals[Triangle->N2]);
  1547. Assign_SNGL_Vect(N3, Mesh->Data->Normals[Triangle->N3]);
  1548. }
  1549. /*****************************************************************************
  1550. *
  1551. * FUNCTION
  1552. *
  1553. * Mesh_Degenerate
  1554. *
  1555. * INPUT
  1556. *
  1557. * P1, P2, P3 - Triangle's vertices
  1558. *
  1559. * OUTPUT
  1560. *
  1561. * RETURNS
  1562. *
  1563. * int - TRUE if degenerate
  1564. *
  1565. * AUTHOR
  1566. *
  1567. * Dieter Bayer
  1568. *
  1569. * DESCRIPTION
  1570. *
  1571. * Test if a triangle is degenerate.
  1572. *
  1573. * CHANGES
  1574. *
  1575. * Feb 1995 : Creation.
  1576. *
  1577. ******************************************************************************/
  1578. int Mesh_Degenerate(VECTOR P1, VECTOR P2, VECTOR P3)
  1579. {
  1580. VECTOR V1, V2, Temp;
  1581. DBL Length;
  1582. VSub(V1, P1, P2);
  1583. VSub(V2, P3, P2);
  1584. VCross(Temp, V1, V2);
  1585. VLength(Length, Temp);
  1586. return(Length == 0.0);
  1587. }
  1588. /*****************************************************************************
  1589. *
  1590. * FUNCTION
  1591. *
  1592. * Initialize_Mesh_Code
  1593. *
  1594. * INPUT
  1595. *
  1596. * OUTPUT
  1597. *
  1598. * RETURNS
  1599. *
  1600. * AUTHOR
  1601. *
  1602. * Dieter Bayer
  1603. *
  1604. * DESCRIPTION
  1605. *
  1606. * Initialize mesh specific variables.
  1607. *
  1608. * CHANGES
  1609. *
  1610. * Jul 1995 : Creation.
  1611. *
  1612. ******************************************************************************/
  1613. void Initialize_Mesh_Code()
  1614. {
  1615. Mesh_Queue = Create_Priority_Queue(INITIAL_NUMBER_OF_ENTRIES);
  1616. }
  1617. /*****************************************************************************
  1618. *
  1619. * FUNCTION
  1620. *
  1621. * Deinitialize_Mesh_Code
  1622. *
  1623. * INPUT
  1624. *
  1625. * OUTPUT
  1626. *
  1627. * RETURNS
  1628. *
  1629. * AUTHOR
  1630. *
  1631. * Dieter Bayer
  1632. *
  1633. * DESCRIPTION
  1634. *
  1635. * Deinitialize mesh specific variables.
  1636. *
  1637. * CHANGES
  1638. *
  1639. * Jul 1995 : Creation.
  1640. *
  1641. ******************************************************************************/
  1642. void Deinitialize_Mesh_Code()
  1643. {
  1644. if (Mesh_Queue != NULL)
  1645. {
  1646. Destroy_Priority_Queue(Mesh_Queue);
  1647. }
  1648. Mesh_Queue = NULL;
  1649. }
  1650. /*****************************************************************************
  1651. *
  1652. * FUNCTION
  1653. *
  1654. * Test_Mesh_Opacity
  1655. *
  1656. * INPUT
  1657. *
  1658. * Mesh - Pointer to mesh structure
  1659. *
  1660. * OUTPUT
  1661. *
  1662. * Mesh
  1663. *
  1664. * RETURNS
  1665. *
  1666. * AUTHOR
  1667. *
  1668. * Dieter Bayer
  1669. *
  1670. * DESCRIPTION
  1671. *
  1672. * Set the opacity flag of the mesh according to the opacity
  1673. * of the mesh's texture(s).
  1674. *
  1675. * CHANGES
  1676. *
  1677. * Apr 1996 : Creation.
  1678. *
  1679. ******************************************************************************/
  1680. void Test_Mesh_Opacity(MESH *Mesh)
  1681. {
  1682. int i;
  1683. /* Initialize opacity flag to the opacity of the object's texture. */
  1684. if ((Mesh->Texture == NULL) || (Test_Opacity(Mesh->Texture)))
  1685. {
  1686. Set_Flag(Mesh, OPAQUE_FLAG);
  1687. }
  1688. if (Test_Flag(Mesh, MULTITEXTURE_FLAG))
  1689. {
  1690. for (i = 0; i < Mesh->Data->Number_Of_Textures; i++)
  1691. {
  1692. if (Mesh->Data->Textures[i] != NULL)
  1693. {
  1694. /* If component's texture isn't opaque the mesh is neither. */
  1695. if (!Test_Opacity(Mesh->Data->Textures[i]))
  1696. {
  1697. Clear_Flag(Mesh, OPAQUE_FLAG);
  1698. }
  1699. }
  1700. }
  1701. }
  1702. }