Skip to content

Commit

Permalink
define colors soley by the physical properties of the scenery
Browse files Browse the repository at this point in the history
  • Loading branch information
relleums committed Sep 13, 2024
1 parent c8a7f5c commit 81f0d66
Show file tree
Hide file tree
Showing 25 changed files with 397 additions and 175 deletions.
15 changes: 14 additions & 1 deletion libs/mli/src/mliAccelerator.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ CASE("mliAccelerator, init")
struct mliRay ray;
struct mliPrng prng = mliPrng_init_MT19937(0);
struct mliTracerConfig tracer_config = mliTracerConfig_init();
struct mliColorObserver color_observer = mliColorObserver_init();
struct mliColorMaterials color_materials = mliColorMaterials_init();
struct mliTracer tracer = mliTracer_init();

CHECK(mliScenery_malloc_from_path_tar(
&scenery,
Expand All @@ -26,14 +29,24 @@ CASE("mliAccelerator, init")
"sceneries/"
"001.tar"));

CHECK(mliColorObserver_malloc_cie1931(&color_observer));
CHECK(mliColorMaterials_malloc_from_Materials(
&color_materials, &scenery.materials, &color_observer));

tracer.scenery = &scenery;
tracer.scenery_color_materials = &color_materials;
tracer.config = &tracer_config;

ray = mliRay_set(
mliVec_init(0.0, 0.0, -5.0), mliVec_init(0.0, 0.0, 1.0));

color = mli_trace(&scenery, ray, &tracer_config, &prng);
color = mliTracer_trace_ray(&tracer, ray, &prng);

CHECK_MARGIN(color.r, 11.0, 1.0);
CHECK_MARGIN(color.g, 45.5, 1.0);
CHECK_MARGIN(color.b, 74.5, 1.0);

mliColorObserver_free(&color_observer);
mliColorMaterials_free(&color_materials);
mliScenery_free(&scenery);
}
14 changes: 5 additions & 9 deletions libs/mli/src/mliApertureCamera.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,9 @@ void mliApertureCamera_aquire_pixels(
const struct mliApertureCamera camera,
const struct mliImage *image,
const struct mliHomTraComp camera2root_comp,
const struct mliScenery *scenery,
const struct mliTracer *tracer,
const struct mliPixels *pixels_to_do,
struct mliImage *colors,
const struct mliTracerConfig *tracer_config,
struct mliPrng *prng)
{
uint64_t i;
Expand All @@ -195,7 +194,7 @@ void mliApertureCamera_aquire_pixels(
mliHomTra_ray(&camera2root, ray_wrt_camera);

struct mliColor set_color =
mli_trace(scenery, ray_wrt_root, tracer_config, prng);
mliTracer_trace_ray(tracer, ray_wrt_root, prng);

mliImage_set(colors, i, 0u, set_color);
}
Expand Down Expand Up @@ -228,9 +227,8 @@ void mliApertureCamera_assign_pixel_colors_to_sum_and_exposure_image(
int mliApertureCamera_render_image(
const struct mliApertureCamera camera,
const struct mliHomTraComp camera2root_comp,
const struct mliScenery *scenery,
const struct mliTracer *tracer,
struct mliImage *image,
const struct mliTracerConfig *tracer_config,
struct mliPrng *prng)
{
float noise_threshold = 0.05 * 255.0;
Expand Down Expand Up @@ -289,10 +287,9 @@ int mliApertureCamera_render_image(
camera,
image,
camera2root_comp,
scenery,
tracer,
&pixels_to_do,
&colors,
tracer_config,
prng);

mliApertureCamera_assign_pixel_colors_to_sum_and_exposure_image(
Expand Down Expand Up @@ -325,10 +322,9 @@ int mliApertureCamera_render_image(
camera,
image,
camera2root_comp,
scenery,
tracer,
&pixels_to_do,
&colors,
tracer_config,
prng);

mliApertureCamera_assign_pixel_colors_to_sum_and_exposure_image(
Expand Down
6 changes: 2 additions & 4 deletions libs/mli/src/mliApertureCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,17 @@ struct mliApertureCamera mliApertureCamera_init(void);
int mliApertureCamera_render_image(
const struct mliApertureCamera camera,
const struct mliHomTraComp camera2root_comp,
const struct mliScenery *scenery,
const struct mliTracer *tracer,
struct mliImage *image,
const struct mliTracerConfig *tracer_config,
struct mliPrng *prng);

void mliApertureCamera_aquire_pixels(
const struct mliApertureCamera camera,
const struct mliImage *image,
const struct mliHomTraComp camera2root_comp,
const struct mliScenery *scenery,
const struct mliTracer *tracer,
const struct mliPixels *pixels_to_do,
struct mliImage *colors,
const struct mliTracerConfig *tracer_config,
struct mliPrng *prng);

void mliApertureCamera_assign_pixel_colors_to_sum_and_exposure_image(
Expand Down
86 changes: 86 additions & 0 deletions libs/mli/src/mliColorMaterials.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Copyright 2018-2020 Sebastian Achim Mueller */
#include "mliColorMaterials.h"
#include <stdlib.h>
#include "chk.h"

struct mliColorMaterials mliColorMaterials_init(void)
{
struct mliColorMaterials colmat;

colmat.num_media = 0u;
colmat.media = NULL;

colmat.num_surfaces = 0u;
colmat.surfaces = NULL;
return colmat;
}

void mliColorMaterials_free(struct mliColorMaterials *colmat)
{
free(colmat->media);
free(colmat->surfaces);
(*colmat) = mliColorMaterials_init();
}

int mliColorMaterials_malloc(
struct mliColorMaterials *colmat,
const struct mliMaterialsCapacity rescap)
{
mliColorMaterials_free(colmat);
colmat->num_surfaces = rescap.num_surfaces;
colmat->num_media = rescap.num_media;

chk_malloc(
colmat->surfaces, struct mliColorSurface, colmat->num_surfaces);
chk_malloc(colmat->media, struct mliColorMedium, colmat->num_media);

return 1;
chk_error:
mliColorMaterials_free(colmat);
return 0;
}

int mliColorMaterials_malloc_from_Materials(
struct mliColorMaterials *colmat,
const struct mliMaterials *mat,
const struct mliColorObserver *colobs)
{
uint64_t i;
struct mliMaterialsCapacity cap;
cap.num_media = mat->num_media;
cap.num_surfaces = mat->num_surfaces;

chk_msg(mliColorMaterials_malloc(colmat, cap),
"Can't malloc ColorMaterials from Materials.");

for (i = 0; i < mat->num_surfaces; i++) {
chk_msg(mliColorObserver_evaluate(
colobs,
&mat->surfaces[i].specular_reflection,
&colmat->surfaces[i].specular_reflection),
"Can't evaluate specular_reflection colors.");
chk_msg(mliColorObserver_evaluate(
colobs,
&mat->surfaces[i].diffuse_reflection,
&colmat->surfaces[i].diffuse_reflection),
"Can't evaluate diffuse_reflection colors.");
}

for (i = 0; i < mat->num_media; i++) {
chk_msg(mliColorObserver_evaluate(
colobs,
&mat->media[i].refraction,
&colmat->media[i].refraction),
"Can't evaluate refraction colors.");
chk_msg(mliColorObserver_evaluate(
colobs,
&mat->media[i].absorbtion,
&colmat->media[i].absorbtion),
"Can't evaluate absorbtion colors.");
}

return 1;
chk_error:
mliColorMaterials_free(colmat);
return 0;
}
36 changes: 36 additions & 0 deletions libs/mli/src/mliColorMaterials.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2018-2020 Sebastian Achim Mueller */
#ifndef MLICOLORMATERIALS_H_
#define MLICOLORMATERIALS_H_

#include <stdint.h>
#include "mliMaterials.h"
#include "mliColor.h"

struct mliColorSurface {
struct mliColor specular_reflection;
struct mliColor diffuse_reflection;
};

struct mliColorMedium {
struct mliColor refraction;
struct mliColor absorbtion;
};

struct mliColorMaterials {
uint64_t num_surfaces;
struct mliColorSurface *surfaces;

uint64_t num_media;
struct mliColorMedium *media;
};

struct mliColorMaterials mliColorMaterials_init(void);
int mliColorMaterials_malloc(
struct mliColorMaterials *colmat,
const struct mliMaterialsCapacity rescap);
int mliColorMaterials_malloc_from_Materials(
struct mliColorMaterials *colmat,
const struct mliMaterials *mat,
const struct mliColorObserver *colobs);
void mliColorMaterials_free(struct mliColorMaterials *colmat);
#endif
11 changes: 6 additions & 5 deletions libs/mli/src/mliColorObserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ int mliColorObserver_malloc_cie1931(struct mliColorObserver *colobs)
colobs->b.y[i] = blue[i][1];
}

return 1;
chk_error:
mliColorObserver_free(colobs);
return 0;
Expand All @@ -114,15 +115,15 @@ int mliColorObserver_evaluate(
chk_msg(func->num_points > 1, "Expected function's num_points > 1");
wavelength_range = func->x[func->num_points - 1] - func->x[0];
chk_msg(wavelength_range > 0.0, "Expected wavelength range > 0nm");
chk_msg(mliFunc_fold_numeric(&colobs->r, func, &r),
chk_msg(mliFunc_fold_numeric_default_zero(&colobs->r, func, &r),
"Can't fold red channel.");
chk_msg(mliFunc_fold_numeric(&colobs->g, func, &g),
chk_msg(mliFunc_fold_numeric_default_zero(&colobs->g, func, &g),
"Can't fold green channel.");
chk_msg(mliFunc_fold_numeric(&colobs->b, func, &g),
chk_msg(mliFunc_fold_numeric_default_zero(&colobs->b, func, &b),
"Can't fold blue channel.");
color->r = (float)(r / wavelength_range);
color->b = (float)(g / wavelength_range);
color->g = (float)(b / wavelength_range);
color->g = (float)(g / wavelength_range);
color->b = (float)(b / wavelength_range);
return 1;
chk_error:
return 0;
Expand Down
65 changes: 65 additions & 0 deletions libs/mli/src/mliFunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ int mliFunc_evaluate(const struct mliFunc *f, const double xarg, double *out)
return 0;
}

double mliFunc_evaluate_with_default_when_out_of_range(
const struct mliFunc *f,
const double xarg,
const double default_value)
{
double y1, y0, x1, x0;
uint32_t idx = mli_upper_compare_double(f->x, f->num_points, xarg);
if (idx == 0) {
/* mliFunc argument below lower bound */
return default_value;
} else if (idx == f->num_points) {
/* mliFunc argument above upper bound */
return default_value;
} else {
y1 = f->y[idx];
y0 = f->y[idx - 1u];
x1 = f->x[idx];
x0 = f->x[idx - 1u];
return mli_linear_interpolate_2d(xarg, x0, y0, x1, y1);
}
}

int mliFunc_fold_numeric(
const struct mliFunc *a,
const struct mliFunc *b,
Expand Down Expand Up @@ -93,6 +115,49 @@ int mliFunc_fold_numeric(
return 0;
}

int mliFunc_fold_numeric_default_zero(
const struct mliFunc *a,
const struct mliFunc *b,
double *fold)
{
(*fold) = 0.0;
double x_start, x_stop, x_step, x_range, x_weight;
uint64_t i;
const uint64_t NUM_STEPS = 1024 * 8;

chk_msg(a->num_points >= 2u, "Expect a->num_points >= 2.");
chk_msg(b->num_points >= 2u, "Expect b->num_points >= 2.");

chk_msg(mliFunc_x_is_strictly_increasing(a),
"Expected function a to be strictly_increasing.");
chk_msg(mliFunc_x_is_strictly_increasing(b),
"Expected function b to be strictly_increasing.");

x_start = MLI_MAX2(a->x[0], b->x[0]);
x_stop = MLI_MIN2(a->x[a->num_points - 1], b->x[b->num_points - 1]);
x_range = x_stop - x_start;
x_step = (x_range) / (double)NUM_STEPS;
x_weight = x_step / x_range;

(*fold) = 0.0;
if (x_start < x_stop) {
for (i = 0; i < NUM_STEPS; i++) {
double ra = MLI_NAN;
double rb = MLI_NAN;
double x = x_start + (double)i * x_step;
ra = mliFunc_evaluate_with_default_when_out_of_range(
a, x, 0.0);
rb = mliFunc_evaluate_with_default_when_out_of_range(
b, x, 0.0);
(*fold) += (ra * rb) * x_weight;
}
}

return 1;
chk_error:
return 0;
}

int mliFunc_equal(const struct mliFunc a, const struct mliFunc b)
{
uint64_t i;
Expand Down
8 changes: 8 additions & 0 deletions libs/mli/src/mliFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ int mliFunc_fold_numeric(
const struct mliFunc *a,
const struct mliFunc *b,
double *fold);
int mliFunc_fold_numeric_default_zero(
const struct mliFunc *a,
const struct mliFunc *b,
double *fold);
int mliFunc_evaluate(const struct mliFunc *f, const double xarg, double *out);
double mliFunc_evaluate_with_default_when_out_of_range(
const struct mliFunc *f,
const double xarg,
const double default_value);
int mliFunc_x_is_strictly_increasing(const struct mliFunc *f);
int mliFunc_malloc(struct mliFunc *f, const uint32_t num_points);
void mliFunc_free(struct mliFunc *f);
Expand Down
6 changes: 1 addition & 5 deletions libs/mli/src/mliMaterials.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ void mliMaterials_info_fprint(FILE *f, const struct mliMaterials *res)
fprintf(f,
"%6d ",
res->surfaces[i].diffuse_reflection.num_points);
fprintf(f,
" %3d,%3d,%3d ",
(int)res->surfaces[i].color.r,
(int)res->surfaces[i].color.g,
(int)res->surfaces[i].color.b);
fprintf(f, " %3d,%3d,%3d ", 0, 0, 0);
fprintf(f, "\n");
}
fprintf(f, "\n");
Expand Down
1 change: 0 additions & 1 deletion libs/mli/src/mliMaterials.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <stdint.h>
#include "mliObject.h"
#include "mliColor.h"
#include "mliFunc.h"
#include "mliSurface.h"
#include "mliMedium.h"
Expand Down
3 changes: 0 additions & 3 deletions libs/mli/src/mliMaterials_valid.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ int mliMaterials_valid_surfaces(const struct mliMaterials *materials)
chk_msg(mliFunc_is_valid(
&materials->surfaces[i].diffuse_reflection),
"Expected diffuse_reflection of surface to be valid.");
chk_msg(mliColor_is_in_range(
materials->surfaces[i].color, 0.0, 256.0),
"Expected 0.0 <= color < 256.0.");
}
return 1;
chk_error:
Expand Down
Loading

0 comments on commit 81f0d66

Please sign in to comment.