Skip to content

Commit e824ff6

Browse files
authored
Merge pull request #508 from RayTracing/min-max-abs
Standardize on fmin/fmax/fabs
2 parents ab7e278 + 678bbed commit e824ff6

File tree

11 files changed

+39
-47
lines changed

11 files changed

+39
-47
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Change Log -- Ray Tracing in One Weekend
1010
- New: Add explanation for padding `aarect` in the zero dimension (#488)
1111
- Change: Minor change to use new `point3` and `color` type aliases for `vec3` (#422)
1212
- Change: Renamed `constant_texture` to `solid_color`, add RGB constructor (#452)
13+
- Change: Math notation to bold uppercase points, bold lowercase no-barb vectors (#412)
14+
- Change: Switch from `ffmin`/`ffmax` to standard `fmin`/`fmax` (#444, #491)
1315

1416
### _In One Weekend_
1517
- Fix: Update image and size for first PPM image

books/RayTracingInOneWeekend.html

+3-6
Original file line numberDiff line numberDiff line change
@@ -1150,9 +1150,6 @@
11501150
return degrees * pi / 180;
11511151
}
11521152

1153-
inline double ffmin(double a, double b) { return a <= b ? a : b; }
1154-
inline double ffmax(double a, double b) { return a >= b ? a : b; }
1155-
11561153
// Common Headers
11571154

11581155
#include "ray.h"
@@ -2389,7 +2386,7 @@
23892386
$$ \cos\theta = \mathbf{R} \cdot \mathbf{n} $$
23902387

23912388
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2392-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
2389+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
23932390
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
23942391
if(etai_over_etat * sin_theta > 1.0) {
23952392
// Must Reflect
@@ -2420,7 +2417,7 @@
24202417

24212418
vec3 unit_direction = unit_vector(r_in.direction());
24222419
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2423-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
2420+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
24242421
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
24252422
if (etai_over_etat * sin_theta > 1.0 ) {
24262423
vec3 reflected = reflect(unit_direction, rec.normal);
@@ -2499,7 +2496,7 @@
24992496
double etai_over_etat = (rec.front_face) ? (1.0 / ref_idx) : (ref_idx);
25002497

25012498
vec3 unit_direction = unit_vector(r_in.direction());
2502-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
2499+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
25032500
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
25042501
if (etai_over_etat * sin_theta > 1.0 ) {
25052502
vec3 reflected = reflect(unit_direction, rec.normal);

books/RayTracingTheNextWeek.html

+14-18
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,12 @@
584584

585585
bool hit(const ray& r, double tmin, double tmax) const {
586586
for (int a = 0; a < 3; a++) {
587-
auto t0 = ffmin((_min[a] - r.origin()[a]) / r.direction()[a],
588-
(_max[a] - r.origin()[a]) / r.direction()[a]);
589-
auto t1 = ffmax((_min[a] - r.origin()[a]) / r.direction()[a],
590-
(_max[a] - r.origin()[a]) / r.direction()[a]);
591-
tmin = ffmax(t0, tmin);
592-
tmax = ffmin(t1, tmax);
587+
auto t0 = fmin((_min[a] - r.origin()[a]) / r.direction()[a],
588+
(_max[a] - r.origin()[a]) / r.direction()[a]);
589+
auto t1 = fmax((_min[a] - r.origin()[a]) / r.direction()[a],
590+
(_max[a] - r.origin()[a]) / r.direction()[a]);
591+
tmin = fmax(t0, tmin);
592+
tmax = fmin(t1, tmax);
593593
if (tmax <= tmin)
594594
return false;
595595
}
@@ -603,10 +603,6 @@
603603
[Listing [aabb]: <kbd>[aabb.h]</kbd> Axis-aligned bounding box class]
604604
</div>
605605

606-
Note that we use the simple custom `ffmax()` function (defined in `rtweekend.h`) instead of the C++
607-
standard library `fmax()` utility. `ffmax()` is quite a bit faster because it doesn’t worry about
608-
`NaN`s and other exceptions.
609-
610606

611607
An Optimized AABB Hit Method
612608
-----------------------------
@@ -722,13 +718,13 @@
722718

723719
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
724720
aabb surrounding_box(aabb box0, aabb box1) {
725-
point3 small(ffmin(box0.min().x(), box1.min().x()),
726-
ffmin(box0.min().y(), box1.min().y()),
727-
ffmin(box0.min().z(), box1.min().z()));
721+
point3 small(fmin(box0.min().x(), box1.min().x()),
722+
fmin(box0.min().y(), box1.min().y()),
723+
fmin(box0.min().z(), box1.min().z()));
728724

729-
point3 big(ffmax(box0.max().x(), box1.max().x()),
730-
ffmax(box0.max().y(), box1.max().y()),
731-
ffmax(box0.max().z(), box1.max().z()));
725+
point3 big(fmax(box0.max().x(), box1.max().x()),
726+
fmax(box0.max().y(), box1.max().y()),
727+
fmax(box0.max().z(), box1.max().z()));
732728

733729
return aabb(small,big);
734730
}
@@ -2564,8 +2560,8 @@
25642560
vec3 tester(newx, y, newz);
25652561

25662562
for (int c = 0; c < 3; c++) {
2567-
min[c] = ffmin(min[c], tester[c]);
2568-
max[c] = ffmax(max[c], tester[c]);
2563+
min[c] = fmin(min[c], tester[c]);
2564+
max[c] = fmax(max[c], tester[c]);
25692565
}
25702566
}
25712567
}

src/InOneWeekend/material.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class dielectric : public material {
4343
double etai_over_etat = (rec.front_face) ? (1.0 / ref_idx) : (ref_idx);
4444

4545
vec3 unit_direction = unit_vector(r_in.direction());
46-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
46+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
4747
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
4848
if (etai_over_etat * sin_theta > 1.0 ) {
4949
vec3 reflected = reflect(unit_direction, rec.normal);

src/TheNextWeek/hittable.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ rotate_y::rotate_y(shared_ptr<hittable> p, double angle) : ptr(p) {
149149
vec3 tester(newx, y, newz);
150150

151151
for (int c = 0; c < 3; c++) {
152-
min[c] = ffmin(min[c], tester[c]);
153-
max[c] = ffmax(max[c], tester[c]);
152+
min[c] = fmin(min[c], tester[c]);
153+
max[c] = fmax(max[c], tester[c]);
154154
}
155155
}
156156
}

src/TheNextWeek/material.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class dielectric : public material {
4747
double etai_over_etat = (rec.front_face) ? (1.0 / ref_idx) : (ref_idx);
4848

4949
vec3 unit_direction = unit_vector(r_in.direction());
50-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
50+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
5151
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
5252
if (etai_over_etat * sin_theta > 1.0 ) {
5353
vec3 reflected = reflect(unit_direction, rec.normal);

src/TheRestOfYourLife/hittable.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ rotate_y::rotate_y(shared_ptr<hittable> p, double angle) : ptr(p) {
157157
vec3 tester(newx, y, newz);
158158

159159
for (int c = 0; c < 3; c++) {
160-
min[c] = ffmin(min[c], tester[c]);
161-
max[c] = ffmax(max[c], tester[c]);
160+
min[c] = fmin(min[c], tester[c]);
161+
max[c] = fmax(max[c], tester[c]);
162162
}
163163
}
164164
}

src/TheRestOfYourLife/material.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class dielectric : public material {
6868
double etai_over_etat = (rec.front_face) ? (1.0 / ref_idx) : (ref_idx);
6969

7070
vec3 unit_direction = unit_vector(r_in.direction());
71-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
71+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
7272
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
7373
if (etai_over_etat * sin_theta > 1.0 ) {
7474
vec3 reflected = reflect(unit_direction, rec.normal);

src/common/aabb.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ class aabb {
2424

2525
bool hit(const ray& r, double tmin, double tmax) const {
2626
for (int a = 0; a < 3; a++) {
27-
auto t0 = ffmin((_min[a] - r.origin()[a]) / r.direction()[a],
28-
(_max[a] - r.origin()[a]) / r.direction()[a]);
29-
auto t1 = ffmax((_min[a] - r.origin()[a]) / r.direction()[a],
30-
(_max[a] - r.origin()[a]) / r.direction()[a]);
31-
tmin = ffmax(t0, tmin);
32-
tmax = ffmin(t1, tmax);
27+
auto t0 = fmin((_min[a] - r.origin()[a]) / r.direction()[a],
28+
(_max[a] - r.origin()[a]) / r.direction()[a]);
29+
auto t1 = fmax((_min[a] - r.origin()[a]) / r.direction()[a],
30+
(_max[a] - r.origin()[a]) / r.direction()[a]);
31+
tmin = fmax(t0, tmin);
32+
tmax = fmin(t1, tmax);
3333
if (tmax <= tmin)
3434
return false;
3535
}
@@ -61,13 +61,13 @@ class aabb {
6161
};
6262

6363
aabb surrounding_box(aabb box0, aabb box1) {
64-
vec3 small(ffmin(box0.min().x(), box1.min().x()),
65-
ffmin(box0.min().y(), box1.min().y()),
66-
ffmin(box0.min().z(), box1.min().z()));
64+
vec3 small(fmin(box0.min().x(), box1.min().x()),
65+
fmin(box0.min().y(), box1.min().y()),
66+
fmin(box0.min().z(), box1.min().z()));
6767

68-
vec3 big (ffmax(box0.max().x(), box1.max().x()),
69-
ffmax(box0.max().y(), box1.max().y()),
70-
ffmax(box0.max().z(), box1.max().z()));
68+
vec3 big (fmax(box0.max().x(), box1.max().x()),
69+
fmax(box0.max().y(), box1.max().y()),
70+
fmax(box0.max().z(), box1.max().z()));
7171

7272
return aabb(small,big);
7373
}

src/common/rtweekend.h

-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ inline double degrees_to_radians(double degrees) {
3232
return degrees * pi / 180.0;
3333
}
3434

35-
inline double ffmin(double a, double b) { return a <= b ? a : b; }
36-
inline double ffmax(double a, double b) { return a >= b ? a : b; }
37-
3835
inline double clamp(double x, double min, double max) {
3936
if (x < min) return min;
4037
if (x > max) return max;

src/common/vec3.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ vec3 reflect(const vec3& v, const vec3& n) {
175175
}
176176

177177
vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) {
178-
auto cos_theta = ffmin(dot(-uv, n), 1.0);
178+
auto cos_theta = fmin(dot(-uv, n), 1.0);
179179
vec3 r_out_parallel = etai_over_etat * (uv + cos_theta*n);
180180
vec3 r_out_perp = -sqrt(1.0 - r_out_parallel.length_squared()) * n;
181181
return r_out_parallel + r_out_perp;

0 commit comments

Comments
 (0)