Attempt to fix nightly build.
[AROS-Contrib.git] / gfx / povray / mesh.c
blobc1ddcaa5a4b8d4529678197959161cc16f334e4f
1 /****************************************************************************
2 * mesh.c
4 * This module implements primitives for mesh objects.
6 * This module was written by Dieter Bayer [DB].
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.
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.
24 *****************************************************************************/
26 /****************************************************************************
28 * Explanation:
30 * -
32 * Syntax:
34 * mesh
35 * {
36 * triangle { <CORNER1>, <CORNER2>, <CORNER3>, texture { NAME } }
37 * smooth_triangle { <CORNER1>, <NORMAL1>, <CORNER2>, <NORMAL2>, <CORNER3>, <NORMAL3>, texture { NAME } }
38 * ...
39 * [ hierarchy FLAG ]
40 * }
42 * ---
44 * Feb 1995 : Creation. [DB]
46 *****************************************************************************/
48 #include "frame.h"
49 #include "vector.h"
50 #include "povproto.h"
51 #include "bbox.h"
52 #include "matrices.h"
53 #include "objects.h"
54 #include "mesh.h"
55 #include "texture.h"
56 #include "povray.h"
60 /*****************************************************************************
61 * Local preprocessor defines
62 ******************************************************************************/
64 #define DEPTH_TOLERANCE 1e-6
66 #define max3_coordinate(x,y,z) ((x > y) ? ((x > z) ? X : Z) : ((y > z) ? Y : Z))
68 #define HASH_SIZE 1000
70 #define INITIAL_NUMBER_OF_ENTRIES 256
73 /*****************************************************************************
74 * Local typedefs
75 ******************************************************************************/
77 typedef struct Hash_Table_Struct HASH_TABLE;
79 struct Hash_Table_Struct
81 int Index;
82 SNGL_VECT P;
83 HASH_TABLE *Next;
88 /*****************************************************************************
89 * Static functions
90 ******************************************************************************/
92 static int Intersect_Mesh (RAY *Ray, MESH *Mesh, ISTACK *Depth_Stack);
93 static int All_Mesh_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack);
94 static int Inside_Mesh (VECTOR IPoint, OBJECT *Object);
95 static void Mesh_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter);
96 static MESH *Copy_Mesh (OBJECT *Object);
97 static void Translate_Mesh (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
98 static void Rotate_Mesh (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
99 static void Scale_Mesh (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
100 static void Transform_Mesh (OBJECT *Object, TRANSFORM *Trans);
101 static void Invert_Mesh (OBJECT *Object);
102 static void Destroy_Mesh (OBJECT *Object);
104 static void compute_smooth_triangle (MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3);
105 static int intersect_mesh_triangle (RAY *Ray, MESH *Mesh, MESH_TRIANGLE *Triangle, DBL *Depth);
106 static int test_hit (MESH_TRIANGLE *Triangle, MESH *Mesh, RAY *Ray, DBL Depth, ISTACK *Depth_Stack);
107 static void smooth_mesh_normal (MESH *Mesh, VECTOR Result, MESH_TRIANGLE *Triangle, VECTOR IPoint);
108 static void get_triangle_bbox (MESH *Mesh, MESH_TRIANGLE *Triangle, BBOX *BBox);
110 static int intersect_bbox_tree (MESH *Mesh, RAY *Ray, RAY *Orig_Ray, DBL len, ISTACK *Depth_Stack);
112 static void get_triangle_vertices (MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3);
113 static void get_triangle_normals (MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR N1, VECTOR N2, VECTOR N3);
115 static int mesh_hash (HASH_TABLE **Hash_Table,
116 int *Number, int *Max, SNGL_VECT **Elements, VECTOR aPoint);
120 /*****************************************************************************
121 * Local variables
122 ******************************************************************************/
124 METHODS Mesh_Methods =
126 All_Mesh_Intersections,
127 Inside_Mesh, Mesh_Normal,
128 (COPY_METHOD)Copy_Mesh,
129 Translate_Mesh, Rotate_Mesh,
130 Scale_Mesh, Transform_Mesh, Invert_Mesh, Destroy_Mesh
133 static HASH_TABLE **Vertex_Hash_Table, **Normal_Hash_Table;
135 static PRIORITY_QUEUE *Mesh_Queue;
139 /*****************************************************************************
141 * FUNCTION
143 * All_Mesh_Intersections
145 * INPUT
147 * OUTPUT
149 * RETURNS
151 * AUTHOR
153 * Dieter Bayer
155 * DESCRIPTION
159 * CHANGES
161 * Feb 1995 : Creation.
163 ******************************************************************************/
165 static int All_Mesh_Intersections(OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack)
167 Increase_Counter(stats[Ray_Mesh_Tests]);
169 if (Intersect_Mesh(Ray, (MESH *)Object, Depth_Stack))
171 Increase_Counter(stats[Ray_Mesh_Tests_Succeeded]);
173 return(TRUE);
176 return(FALSE);
181 /*****************************************************************************
183 * FUNCTION
185 * Intersect_Mesh
187 * INPUT
189 * OUTPUT
191 * RETURNS
193 * AUTHOR
195 * Dieter Bayer
197 * DESCRIPTION
201 * CHANGES
203 * Feb 1995 : Creation.
205 ******************************************************************************/
207 static int Intersect_Mesh(RAY *Ray, MESH *Mesh, ISTACK *Depth_Stack)
209 int i;
210 unsigned found;
211 DBL len, t;
212 RAY New_Ray;
214 /* Transform the ray into mesh space. */
216 if (Mesh->Trans != NULL)
218 MInvTransPoint(New_Ray.Initial, Ray->Initial, Mesh->Trans);
219 MInvTransDirection(New_Ray.Direction, Ray->Direction, Mesh->Trans);
221 VLength(len, New_Ray.Direction);
222 VInverseScaleEq(New_Ray.Direction, len);
224 else
226 New_Ray = *Ray;
228 len = 1.0;
231 found = FALSE;
233 if (Mesh->Data->Tree == NULL)
235 /* There's no bounding hierarchy so just step through all elements. */
237 for (i = 0; i < Mesh->Data->Number_Of_Triangles; i++)
239 if (intersect_mesh_triangle(&New_Ray, Mesh, &Mesh->Data->Triangles[i], &t))
241 if (test_hit(&Mesh->Data->Triangles[i], Mesh, Ray, t / len, Depth_Stack))
243 found = TRUE;
248 else
250 /* Use the mesh's bounding hierarchy. */
252 return(intersect_bbox_tree(Mesh, &New_Ray, Ray, len, Depth_Stack));
255 return(found);
260 /*****************************************************************************
262 * FUNCTION
264 * Inside_Mesh
266 * INPUT
268 * OUTPUT
270 * RETURNS
272 * AUTHOR
274 * Dieter Bayer
276 * DESCRIPTION
280 * CHANGES
282 * Feb 1995 : Creation.
284 ******************************************************************************/
286 static int Inside_Mesh(VECTOR IPoint, OBJECT *Object)
288 return(FALSE);
293 /*****************************************************************************
295 * FUNCTION
297 * Mesh_Normal
299 * INPUT
301 * OUTPUT
303 * RETURNS
305 * AUTHOR
307 * Dieter Bayer
309 * DESCRIPTION
311 * Return the normalized normal in the given point.
313 * CHANGES
315 * Feb 1995 : Creation.
317 ******************************************************************************/
319 static void Mesh_Normal(VECTOR Result, OBJECT *Object, INTERSECTION *Inter)
321 VECTOR IPoint;
322 MESH_TRIANGLE *Triangle;
323 MESH *Mesh = (MESH *)Object;
325 Triangle = (MESH_TRIANGLE *)Inter->Pointer;
327 if (Triangle->Smooth)
329 if (Mesh->Trans != NULL)
331 MInvTransPoint(IPoint, Inter->IPoint, Mesh->Trans);
333 else
335 Assign_Vector(IPoint, Inter->IPoint);
338 smooth_mesh_normal(Mesh, Result, Triangle, IPoint);
340 if (Mesh->Trans != NULL)
342 MTransNormal(Result, Result, Mesh->Trans);
345 VNormalize(Result, Result);
347 else
349 Assign_SNGL_Vect(Result, Mesh->Data->Normals[Triangle->Normal_Ind]);
351 if (Mesh->Trans != NULL)
353 MTransNormal(Result, Result, Mesh->Trans);
355 VNormalize(Result, Result);
362 /*****************************************************************************
364 * FUNCTION
366 * smooth_mesh_normal
368 * INPUT
370 * OUTPUT
372 * RETURNS
374 * AUTHOR
376 * Dieter Bayer
378 * DESCRIPTION
380 * Remove the un-normalized normal of a smoothed triangle.
382 * CHANGES
384 * Feb 1995 : Creation. (Derived from TRIANGLE.C)
386 ******************************************************************************/
388 static void smooth_mesh_normal(MESH *Mesh, VECTOR Result, MESH_TRIANGLE *Triangle, VECTOR IPoint)
390 int axis;
391 DBL u, v;
392 DBL k1, k2, k3;
393 VECTOR PIMinusP1, N1, N2, N3;
395 get_triangle_normals(Mesh, Triangle, N1, N2, N3);
397 VSub(PIMinusP1, IPoint, Mesh->Data->Vertices[Triangle->P1]);
399 VDot(u, PIMinusP1, Triangle->Perp);
401 if (u < EPSILON)
403 Assign_Vector(Result, N1);
405 else
407 axis = Triangle->vAxis;
409 k1 = Mesh->Data->Vertices[Triangle->P1][axis];
410 k2 = Mesh->Data->Vertices[Triangle->P2][axis];
411 k3 = Mesh->Data->Vertices[Triangle->P3][axis];
413 v = (PIMinusP1[axis] / u + k1 - k2) / (k3 - k2);
415 Result[X] = N1[X] + u * (N2[X] - N1[X] + v * (N3[X] - N2[X]));
416 Result[Y] = N1[Y] + u * (N2[Y] - N1[Y] + v * (N3[Y] - N2[Y]));
417 Result[Z] = N1[Z] + u * (N2[Z] - N1[Z] + v * (N3[Z] - N2[Z]));
423 /*****************************************************************************
425 * FUNCTION
427 * Translate_Mesh
429 * INPUT
431 * OUTPUT
433 * RETURNS
435 * AUTHOR
437 * Dieter Bayer
439 * DESCRIPTION
443 * CHANGES
445 * Feb 1995 : Creation.
447 ******************************************************************************/
449 static void Translate_Mesh(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
451 Transform_Mesh(Object, Trans);
456 /*****************************************************************************
458 * FUNCTION
460 * Rotate_Mesh
462 * INPUT
464 * OUTPUT
466 * RETURNS
468 * AUTHOR
470 * Dieter Bayer
472 * DESCRIPTION
476 * CHANGES
478 * Feb 1995 : Creation.
480 ******************************************************************************/
482 static void Rotate_Mesh(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
484 Transform_Mesh(Object, Trans);
489 /*****************************************************************************
491 * FUNCTION
493 * Scale_Mesh
495 * INPUT
497 * OUTPUT
499 * RETURNS
501 * AUTHOR
503 * Dieter Bayer
505 * DESCRIPTION
509 * CHANGES
511 * Feb 1995 : Creation.
513 ******************************************************************************/
515 static void Scale_Mesh(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
517 Transform_Mesh(Object, Trans);
522 /*****************************************************************************
524 * FUNCTION
526 * Transfrom_Mesh
528 * INPUT
530 * OUTPUT
532 * RETURNS
534 * AUTHOR
536 * Dieter Bayer
538 * DESCRIPTION
542 * CHANGES
544 * Feb 1995 : Creation.
546 ******************************************************************************/
548 static void Transform_Mesh(OBJECT *Object, TRANSFORM *Trans)
550 int i;
551 if (((MESH *)Object)->Trans == NULL)
553 ((MESH *)Object)->Trans = Create_Transform();
556 Recompute_BBox(&Object->BBox, Trans);
558 Compose_Transforms(((MESH *)Object)->Trans, Trans);
560 for (i=0; i<((MESH *)Object)->Data->Number_Of_Textures; i++)
562 Transform_Textures(((MESH *)Object)->Data->Textures[i], Trans);
568 /*****************************************************************************
570 * FUNCTION
572 * Invert_Mesh
574 * INPUT
576 * OUTPUT
578 * RETURNS
580 * AUTHOR
582 * Dieter Bayer
584 * DESCRIPTION
588 * CHANGES
590 * Feb 1995 : Creation.
592 ******************************************************************************/
594 static void Invert_Mesh(OBJECT *Object)
600 /*****************************************************************************
602 * FUNCTION
604 * Create_Mesh
606 * INPUT
608 * OUTPUT
610 * RETURNS
612 * AUTHOR
614 * Dieter Bayer
616 * DESCRIPTION
620 * CHANGES
622 * Feb 1995 : Creation.
624 ******************************************************************************/
626 MESH *Create_Mesh()
628 MESH *New;
630 New = (MESH *)POV_MALLOC(sizeof(MESH), "mesh");
632 INIT_OBJECT_FIELDS(New,MESH_OBJECT,&Mesh_Methods)
634 Set_Flag(New, HIERARCHY_FLAG);
636 New->Trans = NULL;
638 New->Data = NULL;
640 return(New);
645 /*****************************************************************************
647 * FUNCTION
649 * Copy_Mesh
651 * INPUT
653 * OUTPUT
655 * RETURNS
657 * AUTHOR
659 * Dieter Bayer
661 * DESCRIPTION
663 * Copy a mesh.
665 * NOTE: The components are not copied, only the number of references is
666 * counted, so that Destroy_Mesh() knows if they can be destroyed.
670 * CHANGES
672 * Feb 1995 : Creation.
674 ******************************************************************************/
676 static MESH *Copy_Mesh(OBJECT *Object)
678 MESH *New;
680 New = Create_Mesh();
682 /* Copy mesh. */
684 *New = *((MESH *)Object);
686 New->Trans = Copy_Transform(New->Trans);
688 New->Data->References++;
690 return(New);
695 /*****************************************************************************
697 * FUNCTION
699 * Destroy_Mesh
701 * INPUT
703 * OUTPUT
705 * RETURNS
707 * AUTHOR
709 * Dieter Bayer
711 * DESCRIPTION
715 * CHANGES
717 * Feb 1995 : Creation.
719 ******************************************************************************/
721 static void Destroy_Mesh(OBJECT *Object)
723 int i;
724 MESH *Mesh = (MESH *)Object;
726 Destroy_Transform(Mesh->Trans);
728 if (--(Mesh->Data->References) == 0)
730 Destroy_BBox_Tree(Mesh->Data->Tree);
732 if (Mesh->Data->Normals != NULL)
734 POV_FREE(Mesh->Data->Normals);
737 if (Mesh->Data->Vertices != NULL)
739 POV_FREE(Mesh->Data->Vertices);
742 if (Mesh->Data->Triangles != NULL)
744 POV_FREE(Mesh->Data->Triangles);
747 if (Mesh->Data->Textures != NULL)
749 for (i = 0; i < Mesh->Data->Number_Of_Textures; i++)
751 Destroy_Textures(Mesh->Data->Textures[i]);
754 POV_FREE(Mesh->Data->Textures);
757 POV_FREE(Mesh->Data);
760 POV_FREE(Object);
765 /*****************************************************************************
767 * FUNCTION
769 * Compute_Mesh_BBox
771 * INPUT
773 * Mesh - Mesh
775 * OUTPUT
777 * Mesh
779 * RETURNS
781 * AUTHOR
783 * Dieter Bayer
785 * DESCRIPTION
787 * Calculate the bounding box of a triangle.
789 * CHANGES
791 * Feb 1995 : Creation.
793 ******************************************************************************/
795 void Compute_Mesh_BBox(MESH *Mesh)
797 int i;
798 VECTOR P1, P2, P3;
799 VECTOR mins, maxs;
801 Make_Vector(mins, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
802 Make_Vector(maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
804 for (i = 0; i < Mesh->Data->Number_Of_Triangles; i++)
806 get_triangle_vertices(Mesh, &Mesh->Data->Triangles[i], P1, P2, P3);
808 mins[X] = min(mins[X], min3(P1[X], P2[X], P3[X]));
809 mins[Y] = min(mins[Y], min3(P1[Y], P2[Y], P3[Y]));
810 mins[Z] = min(mins[Z], min3(P1[Z], P2[Z], P3[Z]));
812 maxs[X] = max(maxs[X], max3(P1[X], P2[X], P3[X]));
813 maxs[Y] = max(maxs[Y], max3(P1[Y], P2[Y], P3[Y]));
814 maxs[Z] = max(maxs[Z], max3(P1[Z], P2[Z], P3[Z]));
817 Make_BBox_from_min_max(Mesh->BBox, mins, maxs);
822 /*****************************************************************************
824 * FUNCTION
826 * Compute_Mesh
828 * INPUT
830 * OUTPUT
832 * RETURNS
834 * AUTHOR
836 * Dieter Bayer
838 * DESCRIPTION
842 * CHANGES
844 * Feb 1995 : Creation.
846 ******************************************************************************/
848 int Compute_Mesh_Triangle(MESH_TRIANGLE *Triangle, int Smooth, VECTOR P1, VECTOR P2, VECTOR P3, VECTOR S_Normal)
850 int temp, swap;
851 DBL x, y, z;
852 VECTOR V1, V2, T1;
853 DBL Length;
855 VSub(V1, P2, P1);
856 VSub(V2, P3, P1);
858 VCross(S_Normal, V2, V1);
860 VLength(Length, S_Normal);
862 /* Set up a flag so we can ignore degenerate triangles */
864 if (Length == 0.0)
866 return(FALSE);
869 /* Normalize the normal vector. */
871 VInverseScaleEq(S_Normal, Length);
873 VDot(Triangle->Distance, S_Normal, P1);
875 Triangle->Distance *= -1.0;
877 /* Find triangle's dominant axis. */
879 x = fabs(S_Normal[X]);
880 y = fabs(S_Normal[Y]);
881 z = fabs(S_Normal[Z]);
883 Triangle->Dominant_Axis = max3_coordinate(x, y, z);
885 swap = FALSE;
887 switch (Triangle->Dominant_Axis)
889 case X:
891 if ((P2[Y] - P3[Y])*(P2[Z] - P1[Z]) < (P2[Z] - P3[Z])*(P2[Y] - P1[Y]))
893 swap = TRUE;
896 break;
898 case Y:
900 if ((P2[X] - P3[X])*(P2[Z] - P1[Z]) < (P2[Z] - P3[Z])*(P2[X] - P1[X]))
902 swap = TRUE;
905 break;
907 case Z:
909 if ((P2[X] - P3[X])*(P2[Y] - P1[Y]) < (P2[Y] - P3[Y])*(P2[X] - P1[X]))
911 swap = TRUE;
914 break;
917 if (swap)
919 temp = Triangle->P2;
920 Triangle->P2 = Triangle->P1;
921 Triangle->P1 = temp;
923 Assign_Vector(T1, P1);
924 Assign_Vector(P1, P2);
925 Assign_Vector(P2, T1);
927 if (Smooth)
929 temp = Triangle->N2;
930 Triangle->N2 = Triangle->N1;
931 Triangle->N1 = temp;
935 if (Smooth)
937 compute_smooth_triangle(Triangle, P1, P2, P3);
940 return(TRUE);
945 /*****************************************************************************
947 * FUNCTION
949 * compute_smooth_triangle
951 * INPUT
953 * OUTPUT
955 * RETURNS
957 * AUTHOR
959 * Dieter Bayer
961 * DESCRIPTION
965 * CHANGES
967 * Feb 1995 : Creation.
969 ******************************************************************************/
971 static void compute_smooth_triangle(MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3)
973 VECTOR P3MinusP2, VTemp1, VTemp2;
974 DBL x, y, z, uDenominator, Proj;
976 Triangle->Smooth = TRUE;
978 VSub(P3MinusP2, P3, P2);
980 x = fabs(P3MinusP2[X]);
981 y = fabs(P3MinusP2[Y]);
982 z = fabs(P3MinusP2[Z]);
984 Triangle->vAxis = max3_coordinate(x, y, z);
986 VSub(VTemp1, P2, P3);
988 VNormalize(VTemp1, VTemp1);
990 VSub(VTemp2, P1, P3);
992 VDot(Proj, VTemp2, VTemp1);
994 VScaleEq(VTemp1, Proj);
996 VSub(Triangle->Perp, VTemp1, VTemp2);
998 VNormalize(Triangle->Perp, Triangle->Perp);
1000 VDot(uDenominator, VTemp2, Triangle->Perp);
1002 VInverseScaleEq(Triangle->Perp, -uDenominator);
1007 /*****************************************************************************
1009 * FUNCTION
1011 * intersect_mesh_triangle
1013 * INPUT
1015 * OUTPUT
1017 * RETURNS
1019 * AUTHOR
1021 * Dieter Bayer
1023 * DESCRIPTION
1027 * CHANGES
1029 * Feb 1995 : Creation.
1031 ******************************************************************************/
1033 static int intersect_mesh_triangle(RAY *Ray, MESH *Mesh, MESH_TRIANGLE *Triangle, DBL *Depth)
1035 DBL NormalDotOrigin, NormalDotDirection;
1036 DBL s, t;
1037 VECTOR P1, P2, P3, S_Normal;
1039 Assign_SNGL_Vect(S_Normal, Mesh->Data->Normals[Triangle->Normal_Ind]);
1041 VDot(NormalDotDirection, S_Normal, Ray->Direction);
1043 if (fabs(NormalDotDirection) < EPSILON)
1045 return(FALSE);
1048 VDot(NormalDotOrigin, S_Normal, Ray->Initial);
1050 *Depth = -(Triangle->Distance + NormalDotOrigin) / NormalDotDirection;
1052 if ((*Depth < DEPTH_TOLERANCE) || (*Depth > Max_Distance))
1054 return(FALSE);
1057 get_triangle_vertices(Mesh, Triangle, P1, P2, P3);
1059 switch (Triangle->Dominant_Axis)
1061 case X:
1063 s = Ray->Initial[Y] + *Depth * Ray->Direction[Y];
1064 t = Ray->Initial[Z] + *Depth * Ray->Direction[Z];
1066 if ((P2[Y] - s) * (P2[Z] - P1[Z]) < (P2[Z] - t) * (P2[Y] - P1[Y]))
1068 return(FALSE);
1071 if ((P3[Y] - s) * (P3[Z] - P2[Z]) < (P3[Z] - t) * (P3[Y] - P2[Y]))
1073 return(FALSE);
1076 if ((P1[Y] - s) * (P1[Z] - P3[Z]) < (P1[Z] - t) * (P1[Y] - P3[Y]))
1078 return(FALSE);
1081 return(TRUE);
1083 case Y:
1085 s = Ray->Initial[X] + *Depth * Ray->Direction[X];
1086 t = Ray->Initial[Z] + *Depth * Ray->Direction[Z];
1088 if ((P2[X] - s) * (P2[Z] - P1[Z]) < (P2[Z] - t) * (P2[X] - P1[X]))
1090 return(FALSE);
1093 if ((P3[X] - s) * (P3[Z] - P2[Z]) < (P3[Z] - t) * (P3[X] - P2[X]))
1095 return(FALSE);
1098 if ((P1[X] - s) * (P1[Z] - P3[Z]) < (P1[Z] - t) * (P1[X] - P3[X]))
1100 return(FALSE);
1103 return(TRUE);
1105 case Z:
1107 s = Ray->Initial[X] + *Depth * Ray->Direction[X];
1108 t = Ray->Initial[Y] + *Depth * Ray->Direction[Y];
1110 if ((P2[X] - s) * (P2[Y] - P1[Y]) < (P2[Y] - t) * (P2[X] - P1[X]))
1112 return(FALSE);
1115 if ((P3[X] - s) * (P3[Y] - P2[Y]) < (P3[Y] - t) * (P3[X] - P2[X]))
1117 return(FALSE);
1120 if ((P1[X] - s) * (P1[Y] - P3[Y]) < (P1[Y] - t) * (P1[X] - P3[X]))
1122 return(FALSE);
1125 return(TRUE);
1128 return(FALSE);
1133 /*****************************************************************************
1135 * FUNCTION
1137 * test_hit
1139 * INPUT
1141 * OUTPUT
1143 * RETURNS
1145 * AUTHOR
1147 * Dieter Bayer
1149 * DESCRIPTION
1151 * Test if a hit is valid and push if on the intersection depth.
1153 * CHANGES
1155 * Feb 1995 : Creation.
1157 ******************************************************************************/
1159 static int test_hit(MESH_TRIANGLE *Triangle, MESH *Mesh, RAY *Ray, DBL Depth, ISTACK *Depth_Stack)
1161 VECTOR IPoint;
1162 OBJECT *Object = (OBJECT *)Mesh;
1164 VEvaluateRay(IPoint, Ray->Initial, Depth, Ray->Direction);
1166 if (Point_In_Clip(IPoint, Object->Clip))
1168 push_entry_pointer(Depth, IPoint, Object, Triangle, Depth_Stack);
1170 return(TRUE);
1173 return(FALSE);
1178 /*****************************************************************************
1180 * FUNCTION
1182 * Init_Mesh_Triangle
1184 * INPUT
1186 * OUTPUT
1188 * RETURNS
1190 * AUTHOR
1192 * Dieter Bayer
1194 * DESCRIPTION
1198 * CHANGES
1200 * Feb 1995 : Creation.
1202 ******************************************************************************/
1204 void Init_Mesh_Triangle(MESH_TRIANGLE *Triangle)
1206 Triangle->Smooth = FALSE;
1208 Triangle->Dominant_Axis = 0;
1209 Triangle->vAxis = 0;
1211 Triangle->P1 =
1212 Triangle->P2 =
1213 Triangle->P3 = -1;
1215 Triangle->Normal_Ind = -1;
1217 Triangle->Texture = -1;
1219 Triangle->N1 =
1220 Triangle->N2 =
1221 Triangle->N3 = -1;
1223 Make_Vector(Triangle->Perp, 0.0, 0.0, 0.0);
1225 Triangle->Distance = 0.0;
1230 /*****************************************************************************
1232 * FUNCTION
1234 * get_triangle_bbox
1236 * INPUT
1238 * Triangle - Pointer to triangle
1240 * OUTPUT
1242 * BBox - Bounding box
1244 * RETURNS
1246 * AUTHOR
1248 * Dieter Bayer
1250 * DESCRIPTION
1252 * Calculate the bounding box of a triangle.
1254 * CHANGES
1256 * Sep 1994 : Creation.
1258 ******************************************************************************/
1260 static void get_triangle_bbox(MESH *Mesh, MESH_TRIANGLE *Triangle, BBOX *BBox)
1262 VECTOR P1, P2, P3;
1263 VECTOR Min, Max;
1265 get_triangle_vertices(Mesh, Triangle, P1, P2, P3);
1267 Min[X] = min3(P1[X], P2[X], P3[X]);
1268 Min[Y] = min3(P1[Y], P2[Y], P3[Y]);
1269 Min[Z] = min3(P1[Z], P2[Z], P3[Z]);
1271 Max[X] = max3(P1[X], P2[X], P3[X]);
1272 Max[Y] = max3(P1[Y], P2[Y], P3[Y]);
1273 Max[Z] = max3(P1[Z], P2[Z], P3[Z]);
1275 Make_BBox_from_min_max(*BBox, Min, Max);
1280 /*****************************************************************************
1282 * FUNCTION
1284 * Build_Mesh_BBox_Tree
1286 * INPUT
1288 * OUTPUT
1290 * RETURNS
1292 * AUTHOR
1294 * Dieter Bayer
1296 * DESCRIPTION
1298 * Create the bounding box hierarchy.
1300 * CHANGES
1302 * Feb 1995 : Creation. (Derived from the bounding slab creation code)
1304 ******************************************************************************/
1306 void Build_Mesh_BBox_Tree(MESH *Mesh)
1308 int i, nElem, maxelements;
1309 BBOX_TREE **Triangles;
1311 if (!Test_Flag(Mesh, HIERARCHY_FLAG))
1313 return;
1316 nElem = (int)Mesh->Data->Number_Of_Triangles;
1318 maxelements = 2 * nElem;
1320 /* Now allocate an array to hold references to these elements. */
1322 Triangles = (BBOX_TREE **)POV_MALLOC(maxelements*sizeof(BBOX_TREE *), "mesh bbox tree");
1324 /* Init list with mesh elements. */
1326 for (i = 0; i < nElem; i++)
1328 Triangles[i] = (BBOX_TREE *)POV_MALLOC(sizeof(BBOX_TREE), "mesh bbox tree");
1330 Triangles[i]->Infinite = FALSE;
1331 Triangles[i]->Entries = 0;
1332 Triangles[i]->Node = (BBOX_TREE **)&Mesh->Data->Triangles[i];
1334 get_triangle_bbox(Mesh, &Mesh->Data->Triangles[i], &Triangles[i]->BBox);
1337 Build_BBox_Tree(&Mesh->Data->Tree, nElem, Triangles, 0, NULL);
1339 /* Get rid of the Triangles array. */
1341 POV_FREE(Triangles);
1346 /*****************************************************************************
1348 * FUNCTION
1350 * intersect_bbox_tree
1352 * INPUT
1354 * Mesh - Mesh object
1355 * Ray - Current ray
1356 * Orig_Ray - Original, untransformed ray
1357 * len - Length of the transformed ray direction
1359 * OUTPUT
1361 * Depth_Stack - Stack of intersections
1363 * RETURNS
1365 * int - TRUE if an intersection was found
1367 * AUTHOR
1369 * Dieter Bayer
1371 * DESCRIPTION
1373 * Intersect a ray with the bounding box tree of a mesh.
1375 * CHANGES
1377 * Feb 1995 : Creation.
1379 ******************************************************************************/
1381 static int intersect_bbox_tree(MESH *Mesh, RAY *Ray, RAY *Orig_Ray, DBL len, ISTACK *Depth_Stack)
1383 int i, found;
1384 DBL Best, Depth;
1385 RAYINFO rayinfo;
1386 BBOX_TREE *Node, *Root;
1388 /* Create the direction vectors for this ray. */
1390 Create_Rayinfo(Ray, &rayinfo);
1392 /* Start with an empty priority queue. */
1394 found = 0;
1396 Mesh_Queue->QSize = 0;
1398 Best = BOUND_HUGE;
1400 #ifdef BBOX_EXTRA_STATS
1401 Increase_Counter(stats[totalQueueResets]);
1402 #endif
1404 /* Check top node. */
1406 Root = Mesh->Data->Tree;
1408 /* Set the root object infinite to avoid a test. */
1410 Check_And_Enqueue(Mesh_Queue, Root, &Root->BBox, &rayinfo);
1412 /* Check elements in the priority queue. */
1414 while (Mesh_Queue->QSize > 0)
1416 Priority_Queue_Delete(Mesh_Queue, &Depth, &Node);
1419 * If current intersection is larger than the best intersection found
1420 * so far our task is finished, because all other bounding boxes in
1421 * the priority queue are further away.
1424 if (Depth > Best)
1426 break;
1429 /* Check current node. */
1431 if (Node->Entries)
1433 /* This is a node containing leaves to be checked. */
1435 for (i = 0; i < Node->Entries; i++)
1437 Check_And_Enqueue(Mesh_Queue, Node->Node[i], &Node->Node[i]->BBox, &rayinfo);
1440 else
1442 /* This is a leaf so test the contained triangle. */
1444 if (intersect_mesh_triangle(Ray, Mesh, (MESH_TRIANGLE *)Node->Node, &Depth))
1446 if (test_hit((MESH_TRIANGLE *)Node->Node, Mesh, Orig_Ray, Depth / len, Depth_Stack))
1448 found = TRUE;
1450 Best = Depth;
1456 return(found);
1461 /*****************************************************************************
1463 * FUNCTION
1465 * mesh_hash
1467 * INPUT
1469 * aPoint - Normal/Vertex to store
1471 * OUTPUT
1473 * Hash_Table - Normal/Vertex hash table
1474 * Number - Number of normals/vertices
1475 * Max - Max. number of normals/vertices
1476 * Elements - List of normals/vertices
1478 * RETURNS
1480 * int - Index of normal/vertex into the normals/vertices list
1482 * AUTHOR
1484 * Dieter Bayer
1486 * DESCRIPTION
1488 * Try to locate a triangle normal/vertex in the normal/vertex list.
1489 * If the vertex is not found its stored in the normal/vertex list.
1491 * CHANGES
1493 * Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
1495 ******************************************************************************/
1497 static int mesh_hash(HASH_TABLE **Hash_Table, int *Number, int *Max, SNGL_VECT **Elements, VECTOR aPoint)
1499 int hash;
1500 SNGL_VECT D, P;
1501 HASH_TABLE *p;
1503 Assign_SNGL_Vect(P, aPoint);
1505 /* Get hash value. */
1507 hash = (unsigned)((int)(326.0*P[X])^(int)(694.7*P[Y])^(int)(1423.6*P[Z])) % HASH_SIZE;
1509 /* Try to find normal/vertex. */
1511 for (p = Hash_Table[hash]; p != NULL; p = p->Next)
1513 VSub(D, p->P, P);
1515 if ((fabs(D[X]) < EPSILON) && (fabs(D[Y]) < EPSILON) && (fabs(D[Z]) < EPSILON))
1517 break;
1521 if ((p != NULL) && (p->Index >= 0))
1523 return(p->Index);
1526 /* Add new normal/vertex to the list and hash table. */
1528 if ((*Number) >= (*Max))
1530 if ((*Max) >= INT_MAX/2)
1532 Error("Too many normals/vertices in mesh.\n");
1535 (*Max) *= 2;
1537 (*Elements) = (SNGL_VECT *)POV_REALLOC((*Elements), (*Max)*sizeof(SNGL_VECT), "mesh data");
1540 Assign_SNGL_Vect((*Elements)[*Number], P);
1542 p = (HASH_TABLE *)POV_MALLOC(sizeof(HASH_TABLE), "mesh data");
1544 Assign_SNGL_Vect(p->P, P);
1546 p->Index = *Number;
1548 p->Next = Hash_Table[hash];
1550 Hash_Table[hash] = p;
1552 return((*Number)++);
1557 /*****************************************************************************
1559 * FUNCTION
1561 * Mesh_Hash_Vertex
1563 * INPUT
1565 * Vertex - Vertex to store
1567 * OUTPUT
1569 * Number_Of_Vertices - Number of vertices
1570 * Max_Vertices - Max. number of vertices
1571 * Vertices - List of vertices
1573 * RETURNS
1575 * int - Index of vertex into the vertices list
1577 * AUTHOR
1579 * Dieter Bayer
1581 * DESCRIPTION
1583 * Try to locate a triangle vertex in the vertex list.
1584 * If the vertex is not found its stored in the vertex list.
1586 * CHANGES
1588 * Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
1590 ******************************************************************************/
1592 int Mesh_Hash_Vertex(int *Number_Of_Vertices, int *Max_Vertices, SNGL_VECT **Vertices, VECTOR Vertex)
1594 return(mesh_hash(Vertex_Hash_Table, Number_Of_Vertices, Max_Vertices, Vertices, Vertex));
1599 /*****************************************************************************
1601 * FUNCTION
1603 * Mesh_Hash_Normal
1605 * INPUT
1607 * Normal - Normal to store
1609 * OUTPUT
1611 * Number_Of_Normals - Number of normals
1612 * Max_Normals - Max. number of normals
1613 * Normals - List of normals
1615 * RETURNS
1617 * int - Index of normal into the normals list
1619 * AUTHOR
1621 * Dieter Bayer
1623 * DESCRIPTION
1625 * Try to locate a triangle normal in the normal list.
1626 * If the normal is not found its stored in the normal list.
1628 * CHANGES
1630 * Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
1632 ******************************************************************************/
1634 int Mesh_Hash_Normal(int *Number_Of_Normals, int *Max_Normals, SNGL_VECT **Normals, VECTOR S_Normal)
1636 return(mesh_hash(Normal_Hash_Table, Number_Of_Normals, Max_Normals, Normals, S_Normal));
1641 /*****************************************************************************
1643 * FUNCTION
1645 * Mesh_Hash_Texture
1647 * INPUT
1649 * Texture - Texture to store
1651 * OUTPUT
1653 * Number_Of_Textures - Number of textures
1654 * Max_Textures - Max. number of textures
1655 * Textures - List of textures
1657 * RETURNS
1659 * int - Index of texture into the texture list
1661 * AUTHOR
1663 * Dieter Bayer
1665 * DESCRIPTION
1667 * Try to locate a texture in the texture list.
1668 * If the texture is not found its stored in the texture list.
1670 * CHANGES
1672 * Feb 1995 : Creation.
1674 ******************************************************************************/
1676 int Mesh_Hash_Texture(int *Number_Of_Textures, int *Max_Textures, TEXTURE ***Textures, TEXTURE *Texture)
1678 int i;
1680 if (Texture == NULL)
1682 return(-1);
1685 /* Just do a linear search. */
1687 for (i = 0; i < *Number_Of_Textures; i++)
1689 if ((*Textures)[i] == Texture)
1691 break;
1695 if (i == *Number_Of_Textures)
1697 if ((*Number_Of_Textures) >= (*Max_Textures))
1699 if ((*Max_Textures) >= INT_MAX/2)
1701 Error("Too many textures in mesh.\n");
1704 (*Max_Textures) *= 2;
1706 (*Textures) = (TEXTURE **)POV_REALLOC((*Textures), (*Max_Textures)*sizeof(TEXTURE *), "mesh data");
1709 (*Textures)[(*Number_Of_Textures)++] = Copy_Texture_Pointer(Texture);
1712 return(i);
1717 /*****************************************************************************
1719 * FUNCTION
1721 * Create_Mesh_Hash_Tables
1723 * INPUT
1725 * OUTPUT
1727 * RETURNS
1729 * AUTHOR
1731 * Dieter Bayer
1733 * DESCRIPTION
1737 * CHANGES
1739 * Feb 1995 : Creation.
1741 ******************************************************************************/
1743 void Create_Mesh_Hash_Tables()
1745 int i;
1747 Vertex_Hash_Table = (HASH_TABLE **)POV_MALLOC(HASH_SIZE*sizeof(HASH_TABLE *), "mesh hash table");
1749 for (i = 0; i < HASH_SIZE; i++)
1751 Vertex_Hash_Table[i] = NULL;
1754 Normal_Hash_Table = (HASH_TABLE **)POV_MALLOC(HASH_SIZE*sizeof(HASH_TABLE *), "mesh hash table");
1756 for (i = 0; i < HASH_SIZE; i++)
1758 Normal_Hash_Table[i] = NULL;
1764 /*****************************************************************************
1766 * FUNCTION
1768 * Destroy_Mesh_Hash_Tables
1770 * INPUT
1772 * OUTPUT
1774 * RETURNS
1776 * AUTHOR
1778 * Dieter Bayer
1780 * DESCRIPTION
1784 * CHANGES
1786 * Feb 1995 : Creation.
1788 ******************************************************************************/
1790 void Destroy_Mesh_Hash_Tables()
1792 int i;
1793 HASH_TABLE *Temp;
1795 for (i = 0; i < HASH_SIZE; i++)
1797 while (Vertex_Hash_Table[i] != NULL)
1799 Temp = Vertex_Hash_Table[i];
1801 Vertex_Hash_Table[i] = Temp->Next;
1803 POV_FREE(Temp);
1807 POV_FREE(Vertex_Hash_Table);
1809 for (i = 0; i < HASH_SIZE; i++)
1811 while (Normal_Hash_Table[i] != NULL)
1813 Temp = Normal_Hash_Table[i];
1815 Normal_Hash_Table[i] = Temp->Next;
1817 POV_FREE(Temp);
1821 POV_FREE(Normal_Hash_Table);
1826 /*****************************************************************************
1828 * FUNCTION
1830 * get_triangle_vertices
1832 * INPUT
1834 * Mesh - Mesh object
1835 * Triangle - Triangle
1837 * OUTPUT
1839 * RETURNS
1841 * P1, P2, P3 - Vertices of the triangle
1843 * AUTHOR
1845 * Dieter Bayer
1847 * DESCRIPTION
1851 * CHANGES
1853 * Feb 1995 : Creation.
1855 ******************************************************************************/
1857 static void get_triangle_vertices(MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3)
1859 Assign_SNGL_Vect(P1, Mesh->Data->Vertices[Triangle->P1]);
1860 Assign_SNGL_Vect(P2, Mesh->Data->Vertices[Triangle->P2]);
1861 Assign_SNGL_Vect(P3, Mesh->Data->Vertices[Triangle->P3]);
1866 /*****************************************************************************
1868 * FUNCTION
1870 * get_triangle_normals
1872 * INPUT
1874 * Mesh - Mesh object
1875 * Triangle - Triangle
1877 * OUTPUT
1879 * RETURNS
1881 * N1, N2, N3 - Normals of the triangle
1883 * AUTHOR
1885 * Dieter Bayer
1887 * DESCRIPTION
1891 * CHANGES
1893 * Feb 1995 : Creation.
1895 ******************************************************************************/
1897 static void get_triangle_normals(MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR N1, VECTOR N2, VECTOR N3)
1899 Assign_SNGL_Vect(N1, Mesh->Data->Normals[Triangle->N1]);
1900 Assign_SNGL_Vect(N2, Mesh->Data->Normals[Triangle->N2]);
1901 Assign_SNGL_Vect(N3, Mesh->Data->Normals[Triangle->N3]);
1906 /*****************************************************************************
1908 * FUNCTION
1910 * Mesh_Degenerate
1912 * INPUT
1914 * P1, P2, P3 - Triangle's vertices
1916 * OUTPUT
1918 * RETURNS
1920 * int - TRUE if degenerate
1922 * AUTHOR
1924 * Dieter Bayer
1926 * DESCRIPTION
1928 * Test if a triangle is degenerate.
1930 * CHANGES
1932 * Feb 1995 : Creation.
1934 ******************************************************************************/
1936 int Mesh_Degenerate(VECTOR P1, VECTOR P2, VECTOR P3)
1938 VECTOR V1, V2, Temp;
1939 DBL Length;
1941 VSub(V1, P1, P2);
1942 VSub(V2, P3, P2);
1944 VCross(Temp, V1, V2);
1946 VLength(Length, Temp);
1948 return(Length == 0.0);
1952 /*****************************************************************************
1954 * FUNCTION
1956 * Initialize_Mesh_Code
1958 * INPUT
1960 * OUTPUT
1962 * RETURNS
1964 * AUTHOR
1966 * Dieter Bayer
1968 * DESCRIPTION
1970 * Initialize mesh specific variables.
1972 * CHANGES
1974 * Jul 1995 : Creation.
1976 ******************************************************************************/
1978 void Initialize_Mesh_Code()
1980 Mesh_Queue = Create_Priority_Queue(INITIAL_NUMBER_OF_ENTRIES);
1985 /*****************************************************************************
1987 * FUNCTION
1989 * Deinitialize_Mesh_Code
1991 * INPUT
1993 * OUTPUT
1995 * RETURNS
1997 * AUTHOR
1999 * Dieter Bayer
2001 * DESCRIPTION
2003 * Deinitialize mesh specific variables.
2005 * CHANGES
2007 * Jul 1995 : Creation.
2009 ******************************************************************************/
2011 void Deinitialize_Mesh_Code()
2013 if (Mesh_Queue != NULL)
2015 Destroy_Priority_Queue(Mesh_Queue);
2018 Mesh_Queue = NULL;
2023 /*****************************************************************************
2025 * FUNCTION
2027 * Test_Mesh_Opacity
2029 * INPUT
2031 * Mesh - Pointer to mesh structure
2033 * OUTPUT
2035 * Mesh
2037 * RETURNS
2039 * AUTHOR
2041 * Dieter Bayer
2043 * DESCRIPTION
2045 * Set the opacity flag of the mesh according to the opacity
2046 * of the mesh's texture(s).
2048 * CHANGES
2050 * Apr 1996 : Creation.
2052 ******************************************************************************/
2054 void Test_Mesh_Opacity(MESH *Mesh)
2056 int i;
2058 /* Initialize opacity flag to the opacity of the object's texture. */
2060 if ((Mesh->Texture == NULL) || (Test_Opacity(Mesh->Texture)))
2062 Set_Flag(Mesh, OPAQUE_FLAG);
2065 if (Test_Flag(Mesh, MULTITEXTURE_FLAG))
2067 for (i = 0; i < Mesh->Data->Number_Of_Textures; i++)
2069 if (Mesh->Data->Textures[i] != NULL)
2071 /* If component's texture isn't opaque the mesh is neither. */
2073 if (!Test_Opacity(Mesh->Data->Textures[i]))
2075 Clear_Flag(Mesh, OPAQUE_FLAG);