@@ -336,6 +336,7 @@ int r_off_clip_3D_mod(r_intersection* t, Coords a, Coords b,
336336 pol .npol = faceArray [i ]; //nb vertex of polygon
337337 pol .p = popol ;
338338 pol .normal = coords_set (0 ,0 ,1 );
339+ pol .D = 1 ;
339340 unsigned long indVertP1 = faceArray [++ i ]; //polygon's first vertex index in vtxTable
340341 int j = 1 ;
341342 /*check whether vertex is left or right of plane*/
@@ -758,7 +759,7 @@ long r_off_init( char *offfile, double xwidth, double yheight, double zdepth,
758759 face_W_Array = malloc (polySize * sizeof (double ));
759760
760761
761- if (!normalArray || !faceArray || !DArray ) return (0 );
762+ if (!normalArray || !faceArray || !DArray || ! face_m_Array || ! face_alpha_Array || ! face_W_Array ) return (0 );
762763
763764 // fill faces
764765 faceSize = 0 ;
@@ -812,6 +813,10 @@ long r_off_init( char *offfile, double xwidth, double yheight, double zdepth,
812813 {
813814 int nbVertex = faceArray [i ];//nb of vertices of this polygon
814815 double * vertices = malloc (3 * nbVertex * sizeof (double ));
816+ if (!vertices ) {
817+ fprintf (stderr ,"Error allocating vertex array sized %i\n" ,nbVertex );
818+ exit (-1 );
819+ }
815820 int j ;
816821
817822 for (j = 0 ; j < nbVertex ; ++ j )
@@ -825,6 +830,7 @@ long r_off_init( char *offfile, double xwidth, double yheight, double zdepth,
825830 r_polygon p ;
826831 p .p = vertices ;
827832 p .npol = nbVertex ;
833+ p .D = 1 ;
828834 r_off_normal (& (p .normal ),p );
829835
830836 normalArray [indNormal ]= p .normal ;
@@ -1238,7 +1244,60 @@ int r_off_x_intersect(double *l0,double *l3,
12381244*******************************************************************************/
12391245void r_off_display (r_off_struct data )
12401246{
1241- #ifndef OPENACC
1247+ if (mcdotrace == 2 ){
1248+ // Estimate size of the JSON string
1249+ const int VERTEX_OVERHEAD = 30 ;
1250+ const int FACE_OVERHEAD_BASE = 20 ;
1251+ const int FACE_INDEX_OVERHEAD = 15 ;
1252+ int estimated_size = 256 ; // Base size
1253+ estimated_size += data .vtxSize * VERTEX_OVERHEAD ;
1254+
1255+ for (int i = 0 ; i < data .faceSize ;) {
1256+ int num_indices = data .faceArray [i ];
1257+ estimated_size += FACE_OVERHEAD_BASE + num_indices * FACE_INDEX_OVERHEAD ;
1258+ i += num_indices + 1 ;
1259+ }
1260+
1261+ char * json_string = malloc (estimated_size );
1262+ if (json_string == NULL ) {
1263+ fprintf (stderr , "Memory allocation failed.\n" );
1264+ return ;
1265+ }
1266+
1267+ char * ptr = json_string ;
1268+ ptr += sprintf (ptr , "{ \"vertices\": [" );
1269+
1270+ for (int i = 0 ; i < data .vtxSize ; i ++ ) {
1271+ ptr += sprintf (ptr , "[%g, %g, %g]" , data .vtxArray [i ].x , data .vtxArray [i ].y , data .vtxArray [i ].z );
1272+ if (i < data .vtxSize - 1 ) {
1273+ ptr += sprintf (ptr , ", " );
1274+ }
1275+ }
1276+
1277+ ptr += sprintf (ptr , "], \"faces\": [" );
1278+
1279+ for (int i = 0 ; i < data .faceSize ;) {
1280+ int num = data .faceArray [i ];
1281+ ptr += sprintf (ptr , "{ \"face\": [" );
1282+ for (int j = 1 ; j <= num ; j ++ ) {
1283+ ptr += sprintf (ptr , "%lu" , data .faceArray [i + j ]);
1284+ if (j < num ) {
1285+ ptr += sprintf (ptr , ", " );
1286+ }
1287+ }
1288+ ptr += sprintf (ptr , "]}" );
1289+ i += num + 1 ;
1290+ if (i < data .faceSize ){
1291+ ptr += sprintf (ptr , ", " );
1292+ }
1293+ }
1294+
1295+ ptr += sprintf (ptr , "]}" );
1296+ mcdis_polyhedron (json_string );
1297+
1298+ free (json_string );
1299+ }
1300+ else {
12421301 unsigned int i ;
12431302 double ratio = (double )(N_VERTEX_DISPLAYED )/(double )data .faceSize ;
12441303 unsigned int pixel = 0 ;
@@ -1251,7 +1310,7 @@ void r_off_display(r_off_struct data)
12511310 z0 = data .vtxArray [data .faceArray [i + 1 ]].z ;
12521311 double x1 = x0 ,y1 = y0 ,z1 = z0 ;
12531312 double cmx = 0 ,cmy = 0 ,cmz = 0 ;
1254-
1313+
12551314 int drawthis = rand01 () < ratio ;
12561315 // First pass, calculate center of mass location...
12571316 for (j = 1 ; j <=nbVertex ; j ++ ) {
@@ -1262,15 +1321,17 @@ void r_off_display(r_off_struct data)
12621321 cmx /= nbVertex ;
12631322 cmy /= nbVertex ;
12641323 cmz /= nbVertex ;
1265-
1266- char pixelinfo [1024 ];
1267- sprintf (pixelinfo , "%lu,%lu,%lu,%i,%g,%g,%g,%g,%g,%g" , data .mantidoffset + pixel , data .mantidoffset , data .mantidoffset + data .polySize - 1 , nbVertex , cmx , cmy , cmz , x1 - cmx , y1 - cmy , z1 - cmz );
1324+
1325+ char pixelinfo [1024 ];
1326+ char pixelinfotmp [1024 ];
1327+ sprintf (pixelinfo , "%li,%li,%li,%i,%g,%g,%g,%g,%g,%g" , data .mantidoffset + pixel , data .mantidoffset , data .mantidoffset + data .polySize - 1 , nbVertex , cmx , cmy , cmz , x1 - cmx , y1 - cmy , z1 - cmz );
12681328 for (j = 2 ; j <=nbVertex ; j ++ ) {
12691329 double x2 ,y2 ,z2 ;
12701330 x2 = data .vtxArray [data .faceArray [i + j ]].x ;
12711331 y2 = data .vtxArray [data .faceArray [i + j ]].y ;
12721332 z2 = data .vtxArray [data .faceArray [i + j ]].z ;
1273- sprintf (pixelinfo , "%s,%g,%g,%g" , pixelinfo , x2 - cmx , y2 - cmy , z2 - cmz );
1333+ sprintf (pixelinfotmp , "%s,%g,%g,%g" , pixelinfo , x2 - cmx , y2 - cmy , z2 - cmz );
1334+ sprintf (pixelinfo ,"%s" ,pixelinfotmp );
12741335 if (ratio > 1 || drawthis ) {
12751336 mcdis_line (x1 ,y1 ,z1 ,x2 ,y2 ,z2 );
12761337 }
@@ -1285,7 +1346,7 @@ void r_off_display(r_off_struct data)
12851346 }
12861347 i += nbVertex ;
12871348 }
1288- #endif
1349+ }
12891350} /* r_off_display */
12901351
12911352/* end of r-interoff-lib.c */
0 commit comments