Skip to content

Commit 958ba5a

Browse files
committed
Switch from ffmin/ffmax to C stdlib fmin/fmax
1 parent b363815 commit 958ba5a

File tree

10 files changed

+38
-48
lines changed

10 files changed

+38
-48
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,9 +1146,6 @@
11461146
return degrees * pi / 180;
11471147
}
11481148

1149-
inline double ffmin(double a, double b) { return a <= b ? a : b; }
1150-
inline double ffmax(double a, double b) { return a >= b ? a : b; }
1151-
11521149
// Common Headers
11531150

11541151
#include "ray.h"
@@ -2385,7 +2382,7 @@
23852382
$$ \cos\theta = \mathbf{R} \cdot \mathbf{n} $$
23862383

23872384
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2388-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
2385+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
23892386
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
23902387
if(etai_over_etat * sin_theta > 1.0) {
23912388
// Must Reflect
@@ -2416,7 +2413,7 @@
24162413

24172414
vec3 unit_direction = unit_vector(r_in.direction());
24182415
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2419-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
2416+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
24202417
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
24212418
if (etai_over_etat * sin_theta > 1.0 ) {
24222419
vec3 reflected = reflect(unit_direction, rec.normal);
@@ -2495,7 +2492,7 @@
24952492
double etai_over_etat = (rec.front_face) ? (1.0 / ref_idx) : (ref_idx);
24962493

24972494
vec3 unit_direction = unit_vector(r_in.direction());
2498-
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);
2495+
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
24992496
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
25002497
if (etai_over_etat * sin_theta > 1.0 ) {
25012498
vec3 reflected = reflect(unit_direction, rec.normal);

books/RayTracingTheNextWeek.html

Lines changed: 15 additions & 19 deletions
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
}
@@ -1595,7 +1591,7 @@
15951591
temp_p *= 2;
15961592
}
15971593

1598-
return fabs(accum);
1594+
return ffabs(accum);
15991595
}
16001596
...
16011597
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2553,8 +2549,8 @@
25532549
vec3 tester(newx, y, newz);
25542550

25552551
for (int c = 0; c < 3; c++) {
2556-
min[c] = ffmin(min[c], tester[c]);
2557-
max[c] = ffmax(max[c], tester[c]);
2552+
min[c] = fmin(min[c], tester[c]);
2553+
max[c] = fmax(max[c], tester[c]);
25582554
}
25592555
}
25602556
}

src/InOneWeekend/material.h

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 12 deletions
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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ inline double degrees_to_radians(double degrees) {
3131
return degrees * pi / 180.0;
3232
}
3333

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

src/common/vec3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ vec3 reflect(const vec3& v, const vec3& n) {
173173
}
174174

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

0 commit comments

Comments
 (0)