ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genglaze.c
(Generate patch)

Comparing ray/src/gen/genglaze.c (file contents):
Revision 2.1 by greg, Mon May 5 19:20:16 2025 UTC vs.
Revision 2.2 by greg, Tue Jun 10 23:13:13 2025 UTC

# Line 16 | Line 16 | enum {
16   };
17  
18   const double DBL_EPSILON = 1E-9;
19 < const double FLT_MAX = 3.40282347E+38;
19 > const double LARGE_DOUBE = 3.40282347E+38;
20   const double RAD_PER_DEG = 0.0174532925199;
21   const double COEFFS_TAU_CLEAR[] = {-0.0015, 3.355, -3.84, 1.46, 0.0288};
22   const double COEFFS_TAU_BRONZE[] = {0.002, 2.813, -2.341, -0.05725, 0.599};
# Line 53 | Line 53 | typedef struct {
53   } GlazingLayer;
54  
55  
56 < inline double polynomial_5(double x, const double coeffs[5]) {
56 > static inline double polynomial_5(double x, const double coeffs[5]) {
57          return x * ( x * ( x * (coeffs[4] * x + coeffs[3]) + coeffs[2]) + coeffs[1]) + coeffs[0];
58   }
59  
# Line 147 | Line 147 | void angular_monolithic(GlazingLayer *layer, const int
147                          if (rho_lambda > 1.0)
148                                  rho_lambda = 1.0;
149  
150 <                        double exp_arg = (cos_phi_prime < DBL_EPSILON) ? -FLT_MAX : (-abs_coeff[iwvl] * layer->thickness_m / cos_phi_prime);
150 >                        double exp_arg = (cos_phi_prime < DBL_EPSILON) ? -LARGE_DOUBE : (-abs_coeff[iwvl] * layer->thickness_m / cos_phi_prime);
151                          /* Clamp exponent argument to prevent overflow in exp() */
152                          if (exp_arg < -700.0)
153                                  exp_arg = -700.0;
# Line 178 | Line 178 | void angular_monolithic(GlazingLayer *layer, const int
178                          layer->rb_lambda_theta[flat_idx] = r_lambda;
179                  }
180          }
181 +        free(rho_0);
182 +        free(refrac);
183 +        free(abs_coeff);
184   }
185  
186  
# Line 403 | Line 406 | void print_usage() {
406          fprintf(stderr, "Output Files: prefix_t.dat, prefix_r.dat\n");
407   }
408  
409 +
410 + static void free_layer_data(GlazingLayer *layer) {
411 +    if (!layer) return;
412 +    
413 +    free(layer->filename);
414 +    free(layer->t_0);
415 +    free(layer->rf_0);
416 +    free(layer->rb_0);
417 +    free(layer->t_lambda_theta);
418 +    free(layer->rf_lambda_theta);
419 +    free(layer->rb_lambda_theta);
420 +    
421 +    // Set pointers to NULL to prevent double-free errors
422 +    layer->filename = NULL;
423 +    layer->t_0 = NULL;
424 +    layer->rf_0 = NULL;
425 +    layer->rb_0 = NULL;
426 +    layer->t_lambda_theta = NULL;
427 +    layer->rf_lambda_theta = NULL;
428 +    layer->rb_lambda_theta = NULL;
429 + }
430 +
431 +
432   void cleanup_layers(GlazingLayer *layers, int num_layers) {
433           if (!layers) return;
434           for (int i = 0; i < num_layers; ++i) {
435                  if (layers[i].filename)
436 <                        free(layers[i].filename);
436 >                        free_layer_data(&layers[i]);
437          }
438          free(layers);
439   }
# Line 440 | Line 466 | int main(int argc, char *argv[])
466                          filename = argv[++i];
467                          thickness_m = atof(argv[++i]);
468                          if (!add_layer(&layers, &num_layers, &layer_capacity, filename, thickness_m, 1)) {
469 <                                 cleanup_layers(layers, num_layers);
470 <                                 return EXIT_FAILURE;
469 >                                cleanup_layers(layers, num_layers);
470 >                                return EXIT_FAILURE;
471                          }
472                          break;
473                  case 'c':
# Line 460 | Line 486 | int main(int argc, char *argv[])
486                          wvl_interval_nm = atof(argv[++i]);
487                          if (wvl_start_nm > wvl_end_nm) {
488                                  fprintf(stderr, "Starting wavelength > End wavelength\n");
489 +                                cleanup_layers(layers, num_layers);
490                                  return EXIT_FAILURE;
491                          }
492                          if (((int)(wvl_end_nm - wvl_start_nm) % (int)wvl_interval_nm) > 0) {
493                                  fprintf(stderr,
494                                                  "Error: Wavelength range (%f to %f nm) must be evenly divisible by the interval (%f nm).\n",
495                                                  wvl_start_nm, wvl_end_nm, wvl_interval_nm);
496 +                                cleanup_layers(layers, num_layers);
497                                  return EXIT_FAILURE;
498                          }
499                          break;
# Line 537 | Line 565 | int main(int argc, char *argv[])
565                  for (int i = 0;i < argc; ++i) {
566                          printf("%s ", argv[i]);
567                  }
568 <                printf("\nvoid specdata refl_spec\n");
568 >                printf("\nvoid specdata refl_spec_%s\n", prefix);
569                  printf("4 noop %s . 'Acos(Rdot)/DEGREE'\n0\n0\n\n", file_r);
570 <                printf("void specdata trans_spec\n");
570 >                printf("void specdata trans_spec_%s\n", prefix);
571                  printf("4 noop %s . 'Acos(abs(Rdot))/DEGREE'\n0\n0\n\n", file_t);
572 <                printf("void WGMDfunc glaze_mat\n13\n\trefl_spec 1 0 0\n\ttrans_spec 1 0 0\n\tvoid\n\t0 0 1 .\n0\n9 0 0 0 0 0 0 0 0 0\n");
572 >                printf("void WGMDfunc glaze_mat_%s\n13\n\trefl_spec_%s 1 0 0\n\ttrans_spec_%s 1 0 0\n\tvoid\n\t0 0 1 .\n0\n9 0 0 0 0 0 0 0 0 0\n", prefix, prefix, prefix);
573          } else {
574                  fprintf(stderr, "Error: Failed to write one or more output files.\n");
575          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines