@@ -3890,8 +3890,23 @@ int r_within_mesh(Coords pos,struct geometry_struct *geometry) {
38903890
38913891 return 0 ;
38923892 };
3893+
38933894
3894-
3895+ // Type for holding intersection and normal
3896+ typedef struct {
3897+ double t ;
3898+ double nx , ny , nz ;
3899+ int surface_index ;
3900+ } Intersection ;
3901+
3902+ // Function to sort intersection structs according to time
3903+ int compare_intersections (const void * a , const void * b ) {
3904+ const Intersection * ia = a ;
3905+ const Intersection * ib = b ;
3906+ if (ia -> t < ib -> t ) return -1 ;
3907+ if (ia -> t > ib -> t ) return 1 ;
3908+ return 0 ;
3909+ }
38953910
38963911int sample_mesh_intersect (double * t ,
38973912 double * nx , double * ny , double * nz ,
@@ -3983,58 +3998,64 @@ int sample_mesh_intersect(double *t,
39833998 //if (a > -UNION_EPSILON && a < UNION_EPSILON){
39843999 ////printf("\n UNION_EPSILON fail");
39854000 //}
3986- f = 1.0 /a ;
3987- s = coords_sub (rotated_coordinates , coords_set (* (v1_x + iter ),* (v1_y + iter ),* (v1_z + iter )));
3988- u = f * (Dot (s ,h ));
3989- if (u < 0.0 || u > 1.0 ){
3990- }else {
3991- //q = vec_prod(s,edge1);
3992- vec_prod (q .x ,q .y ,q .z ,s .x ,s .y ,s .z ,edge1 .x ,edge1 .y ,edge1 .z );
3993- V = f * Dot (rotated_velocity ,q );
3994- if (V < 0.0 || u + V > 1.0 ){
3995- } else {
3996- // At this stage we can compute t to find out where the intersection point is on the line.
3997- //tmp = Dot(q,edge2)
3998- if (f * Dot (q ,edge2 ) > 0 ){
3999-
4000- t_intersect [counter ] = f * Dot (q ,edge2 );
4001- facet_index [counter ] = iter ;
4002- //printf("\nIntersects at time: t= %f\n",t_intersect[counter] );
4003- counter ++ ;
4004- }
4005- }
4001+ f = 1.0 /a ;
4002+ s = coords_sub (rotated_coordinates , coords_set (* (v1_x + iter ),* (v1_y + iter ),* (v1_z + iter )));
4003+ u = f * (Dot (s ,h ));
4004+ if (u < 0.0 || u > 1.0 ){
4005+ } else {
4006+ //q = vec_prod(s,edge1);
4007+ vec_prod (q .x ,q .y ,q .z ,s .x ,s .y ,s .z ,edge1 .x ,edge1 .y ,edge1 .z );
4008+ V = f * Dot (rotated_velocity ,q );
4009+ if (V < 0.0 || u + V > 1.0 ){
4010+ } else {
4011+ // At this stage we can compute t to find out where the intersection point is on the line.
4012+ t_intersect [counter ] = f * Dot (q ,edge2 );
4013+ facet_index [counter ] = iter ;
4014+ //printf("\nIntersects at time: t= %f\n",t_intersect[counter] );
4015+ counter ++ ;
40064016 }
4017+ }
40074018 }
4008-
4009- // Return all t
4010- int counter2 = 0 ;
4011- double x_intersect , y_intersect , z_intersect ;
4012- * num_solutions = 0 ;
4013- for (iter = 0 ; iter < counter ; iter ++ ){
4014- if (t_intersect [iter ] > 0.0 ){
4015- t [counter2 ] = t_intersect [iter ];
4016- x_intersect = t [counter2 ]* v [0 ] + x_new ;
4017- y_intersect = t [counter2 ]* v [1 ] + y_new ;
4018- z_intersect = t [counter2 ]* v [2 ] + z_new ;
4019- coordinates = coords_set (x_intersect ,y_intersect ,z_intersect );
4020- NORM (coordinates .x , coordinates .y , coordinates .z );
4021- nx [counter2 ] = normal_x [facet_index [iter ]];
4022- ny [counter2 ] = normal_x [facet_index [iter ]];
4023- nz [counter2 ] = normal_x [facet_index [iter ]];
4024- surface_index [counter2 ] = 0 ;
4025- counter2 ++ ;
4026- * num_solutions = counter2 ;
4027- }
4028- }
4029- // Sort t:
4019+
4020+ * num_solutions = counter ;
4021+
4022+ // Early exit if there are not solutions
40304023 if (* num_solutions == 0 ){
40314024 free (t_intersect );
40324025 free (facet_index );
40334026 return 0 ;
40344027 }
4035- qsort (t ,* num_solutions ,sizeof (double ), Sample_compare_doubles );
4028+
4029+ // Move times and normal's into structs to be sorted
4030+ Intersection * hits = malloc (* num_solutions * sizeof (Intersection ));
4031+ if (!hits ) {
4032+ fprintf (stderr ,"Failure allocating Intersection list struct in Union function sample_mesh_intersect - Exit!\n" );
4033+ exit (EXIT_FAILURE );
4034+ }
4035+
4036+ for (iter = 0 ; iter < * num_solutions ; iter ++ ){
4037+ hits [iter ].t = t_intersect [iter ];;
4038+ hits [iter ].nx = normal_x [facet_index [iter ]];
4039+ hits [iter ].ny = normal_y [facet_index [iter ]];
4040+ hits [iter ].nz = normal_z [facet_index [iter ]];
4041+ surface_index [iter ] = 0 ;
4042+ }
4043+
4044+ // Sort structs according to time
4045+ qsort (hits , * num_solutions , sizeof (Intersection ), compare_intersections );
4046+
4047+ // Place the solutions into the pointers given in the function parameters for return
4048+ for (int i = 0 ; i < * num_solutions ; i ++ ) {
4049+ t [i ] = hits [i ].t ;
4050+ nx [i ] = hits [i ].nx ;
4051+ ny [i ] = hits [i ].ny ;
4052+ nz [i ] = hits [i ].nz ;
4053+ surface_index [i ] = hits [i ].surface_index ;
4054+ }
4055+
40364056 free (facet_index );
40374057 free (t_intersect );
4058+ free (hits );
40384059 return 1 ;
40394060};
40404061
0 commit comments