/* See {rdo_finish.h}. */ #define rdo_finish_C_COPYRIGHT "Copyright © 2008 Danillo Pereira and J. Stolfi, UNICAMP" /* Last edited on 2023-02-12 07:48:23 by stolfi */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include vec_typeimpl(finish_vec_t, finish_vec, finish_t); void rdo_finish_check_max_albedo(finish_t *fn, double max_albedo) { int channel; for (channel =0; channel < 3; channel++) { double albedo = fn->diffusion.c[channel] + fn->polish.c[channel] + fn->reflection.c[channel]; demand(albedo <= max_albedo, "albedo total exceeds limit"); } } void rdo_finish_write(FILE *wr, finish_t *fn) { fprintf(wr, "%-12s ", fn->name); frgb_print(wr, "( ", &(fn->emission), 3, "%7.5f", " )"); frgb_print(wr, "( ", &(fn->diffusion), 3, "%7.5f", " )"); frgb_print(wr, "( ", &(fn->polish), 3, "%7.5f", " )"); frgb_print(wr, "( ", &(fn->reflection), 3, "%7.5f", " )"); } finish_t rdo_finish_read(FILE *rd) { finish_t fn; fn.name = fget_string(rd); fget_skip_spaces_and_match(rd, "("); fn.emission = frgb_read_color(rd); fget_skip_spaces_and_match(rd, ")"); fget_skip_spaces_and_match(rd, "("); fn.diffusion = frgb_read_color(rd); fget_skip_spaces_and_match(rd, ")"); fget_skip_spaces_and_match(rd, "("); fn.polish = frgb_read_color(rd); fget_skip_spaces_and_match(rd, ")"); fget_skip_spaces_and_match(rd, "("); fn.reflection = frgb_read_color(rd); fget_skip_spaces_and_match(rd, ")"); return fn; } #define finish_vec_FILE_VERSION "2008-07-14" /* Identifies the write-out format for a list of finishes. */ void rdo_finish_vec_write(FILE *wr, finish_vec_t *fns) { /* Writes the header line: */ filefmt_write_header(wr, "finish_vec_t", finish_vec_FILE_VERSION); /* Here we should write comments provided by the client. */ /* Writes the number of finishes: */ fprintf(wr, "num_finishes = %d\n", fns->ne); /* Writes the finishes: */ int i; for(i = 0; i < fns->ne; i++) { rdo_finish_write(wr, &(fns->e[i])); fprintf(wr, "\n"); } /* Writes the footer line, and synchronizes the file: */ filefmt_write_footer(wr, "finish_vec_t"); fflush(wr); } finish_vec_t rdo_finish_vec_read(FILE *rd) { /* Reads the header line, and comments (if any): */ filefmt_read_header(rd, "finish_vec_t", finish_vec_FILE_VERSION); char *cmt = filefmt_read_comment(rd, '#'); free(cmt); /* For now. */ /* Reads the number of finishes in the list: */ int n_fns = nget_int32(rd, "num_finishes"); fget_eol(rd); /* Reads the finishes: */ finish_vec_t fns = finish_vec_new(n_fns); int i; for(i = 0; i < n_fns; i++) { fns.e[i] = rdo_finish_read(rd); fget_eol(rd); } /* Reads the footer line: */ filefmt_read_footer(rd, "finish_vec_t"); return fns; } double rdo_finish_get_diffusion_coeff(finish_t *fn, int channel) { if (fn == NULL) { /* Null site, or site on no object, or object without finish: */ return NAN; } else { return fn->diffusion.c[channel]; } } double rdo_finish_get_emission_coeff(finish_t *fn, int channel) { if (fn == NULL) { /* Null site, or site on no object, or object without finish: */ return NAN; } else { return fn->emission.c[channel]; } }