Skip to content

Commit eb4bacc

Browse files
committed
Add vec3::random() utility functions
1 parent 85f1fe3 commit eb4bacc

File tree

7 files changed

+85
-110
lines changed

7 files changed

+85
-110
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,11 +1164,25 @@
11641164
range from -1 to +1. Reject this point and try again if the point is outside the sphere. A
11651165
do-while construct is perfect for that:
11661166

1167+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1168+
class vec3 {
1169+
public:
1170+
...
1171+
inline static vec3 random() {
1172+
return vec3(random_double(), random_double(), random_double());
1173+
}
1174+
1175+
inline static vec3 random(double min, double max) {
1176+
return vec3(random_double(min,max), random_double(min,max), random_double(min,max));
1177+
}
1178+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1179+
[Listing [vec-rand-util]: <kbd>[vec3.h]</kbd> `vec3` random utility functions]
1180+
11671181
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
11681182
vec3 random_in_unit_sphere() {
11691183
vec3 p;
11701184
do {
1171-
p = vec3(random_double(-1,1), random_double(-1,1), random_double(-1,1));
1185+
p = vec3::random(-1,1);
11721186
} while (p.squared_length() >= 1.0);
11731187
return p;
11741188
}
@@ -2311,26 +2325,17 @@
23112325
auto choose_mat = random_double();
23122326
vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
23132327
if ((center-vec3(4,0.2,0)).length() > 0.9) {
2314-
if (choose_mat < 0.8) { // diffuse
2315-
list[i++] = new sphere(center, 0.2,
2316-
new lambertian(vec3(random_double()*random_double(),
2317-
random_double()*random_double(),
2318-
random_double()*random_double())
2319-
)
2320-
);
2321-
}
2322-
else if (choose_mat < 0.95) { // metal
2323-
list[i++] = new sphere(
2324-
center, 0.2,
2325-
new metal(
2326-
vec3(random_double(.5, 1),
2327-
random_double(.5, 1),
2328-
random_double(.5, 1)),
2329-
random_double(0, .5)
2330-
)
2331-
);
2332-
}
2333-
else { // glass
2328+
if (choose_mat < 0.8) {
2329+
// diffuse
2330+
auto albedo = vec3::random() * vec3::random();
2331+
list[i++] = new sphere(center, 0.2, new lambertian(albedo));
2332+
} else if (choose_mat < 0.95) {
2333+
// metal
2334+
auto albedo = vec3::random(.5, 1);
2335+
auto fuzz = random_double(0, .5);
2336+
list[i++] = new sphere(center, 0.2, new metal(albedo, fuzz));
2337+
} else {
2338+
// glass
23342339
list[i++] = new sphere(center, 0.2, new dielectric(1.5));
23352340
}
23362341
}

books/RayTracingTheNextWeek.html

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -273,30 +273,20 @@
273273
auto choose_mat = random_double();
274274
vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
275275
if ((center - vec3(4, .2, 0)).length() > 0.9) {
276-
if (choose_mat < 0.8) { // diffuse
276+
if (choose_mat < 0.8) {
277+
// diffuse
278+
auto albedo = vec3::random() * vec3::random();
277279
list[i++] = new moving_sphere(
278-
center,
279-
center + vec3(0, random_double(0, .5), 0),
280-
0.0, 1.0, 0.2,
281-
new lambertian(
282-
vec3(random_double()*random_double(),
283-
random_double()*random_double(),
284-
random_double()*random_double())
285-
)
280+
center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2,
281+
new lambertian(new constant_texture(albedo))
286282
);
287-
}
288-
else if (choose_mat < 0.95) { // metal
289-
list[i++] = new sphere(
290-
center, 0.2,
291-
new metal(
292-
vec3(random_double(.5, 1),
293-
random_double(.5, 1),
294-
random_double(.5, 1)),
295-
random_double(0, .5)
296-
)
297-
);
298-
}
299-
else { // glass
283+
} else if (choose_mat < 0.95) {
284+
// metal
285+
auto albedo = vec3::random(.5, 1);
286+
auto fuzz = random_double(0, .5);
287+
list[i++] = new sphere(center, 0.2, new metal(albedo, fuzz));
288+
} else {
289+
// glass
300290
list[i++] = new sphere(center, 0.2, new dielectric(1.5));
301291
}
302292
}
@@ -677,12 +667,12 @@
677667

678668
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
679669
aabb surrounding_box(aabb box0, aabb box1) {
680-
vec3 small( ffmin(box0.min().x(), box1.min().x()),
681-
ffmin(box0.min().y(), box1.min().y()),
682-
ffmin(box0.min().z(), box1.min().z()));
683-
vec3 big ( ffmax(box0.max().x(), box1.max().x()),
684-
ffmax(box0.max().y(), box1.max().y()),
685-
ffmax(box0.max().z(), box1.max().z()));
670+
vec3 small(ffmin(box0.min().x(), box1.min().x()),
671+
ffmin(box0.min().y(), box1.min().y()),
672+
ffmin(box0.min().z(), box1.min().z()));
673+
vec3 big (ffmax(box0.max().x(), box1.max().x()),
674+
ffmax(box0.max().y(), box1.max().y()),
675+
ffmax(box0.max().z(), box1.max().z()));
686676
return aabb(small,big);
687677
}
688678
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1256,10 +1246,7 @@
12561246
static vec3* perlin_generate() {
12571247
vec3 *p = new vec3[256];
12581248
for (int i = 0; i < 256; ++i) {
1259-
double x_random = random_double(-1,1);
1260-
double y_random = random_double(-1,1);
1261-
double z_random = random_double(-1,1);
1262-
p[i] = unit_vector(vec3(x_random, y_random, z_random));
1249+
p[i] = unit_vector(vec3::random(-1,1));
12631250
}
12641251
return p;
12651252
}
@@ -2453,8 +2440,7 @@
24532440

24542441
int ns = 1000;
24552442
for (int j = 0; j < ns; j++) {
2456-
boxlist2[j] = new sphere(
2457-
vec3(random_double(0,165), random_double(0,165), random_double(0,165)), 10, white);
2443+
boxlist2[j] = new sphere(vec3::random(0,165), 10, white);
24582444
}
24592445

24602446
list[l++] = new translate(

src/InOneWeekend/main.cc

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,17 @@ hittable *random_scene() {
4646
auto choose_mat = random_double();
4747
vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
4848
if ((center - vec3(4, .2, 0)).length() > 0.9) {
49-
if (choose_mat < 0.8) { // diffuse
50-
list[i++] = new sphere(
51-
center, 0.2,
52-
new lambertian(vec3(random_double()*random_double(),
53-
random_double()*random_double(),
54-
random_double()*random_double()))
55-
);
56-
}
57-
else if (choose_mat < 0.95) { // metal
58-
list[i++] = new sphere(
59-
center, 0.2,
60-
new metal(
61-
vec3(random_double(.5, 1),
62-
random_double(.5, 1),
63-
random_double(.5, 1)),
64-
random_double(0, .5)
65-
)
66-
);
67-
}
68-
else { // glass
49+
if (choose_mat < 0.8) {
50+
// diffuse
51+
auto albedo = vec3::random() * vec3::random();
52+
list[i++] = new sphere(center, 0.2, new lambertian(albedo));
53+
} else if (choose_mat < 0.95) {
54+
// metal
55+
auto albedo = vec3::random(.5, 1);
56+
auto fuzz = random_double(0, .5);
57+
list[i++] = new sphere(center, 0.2, new metal(albedo, fuzz));
58+
} else {
59+
// glass
6960
list[i++] = new sphere(center, 0.2, new dielectric(1.5));
7061
}
7162
}

src/TheNextWeek/main.cc

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ hittable *final() {
102102

103103
int ns = 1000;
104104
for (int j = 0; j < ns; j++) {
105-
boxlist2[j] = new sphere(
106-
vec3(random_double(0,165), random_double(0,165), random_double(0,165)), 10, white);
105+
boxlist2[j] = new sphere(vec3::random(0,165), 10, white);
107106
}
108107

109108
list[l++] = new translate(
@@ -259,31 +258,20 @@ hittable *random_scene() {
259258
auto choose_mat = random_double();
260259
vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
261260
if ((center - vec3(4, .2, 0)).length() > 0.9) {
262-
if (choose_mat < 0.8) { // diffuse
261+
if (choose_mat < 0.8) {
262+
// diffuse
263+
auto albedo = vec3::random() * vec3::random();
263264
list[i++] = new moving_sphere(
264265
center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2,
265-
new lambertian(
266-
new constant_texture(
267-
vec3(random_double()*random_double(),
268-
random_double()*random_double(),
269-
random_double()*random_double()
270-
)
271-
)
272-
)
266+
new lambertian(new constant_texture(albedo))
273267
);
274-
}
275-
else if (choose_mat < 0.95) { // metal
276-
list[i++] = new sphere(
277-
center, 0.2,
278-
new metal(
279-
vec3(random_double(.5, 1),
280-
random_double(.5, 1),
281-
random_double(.5, 1)),
282-
random_double(0,.5)
283-
)
284-
);
285-
}
286-
else { // glass
268+
} else if (choose_mat < 0.95) {
269+
// metal
270+
auto albedo = vec3::random(.5, 1);
271+
auto fuzz = random_double(0, .5);
272+
list[i++] = new sphere(center, 0.2, new metal(albedo, fuzz));
273+
} else {
274+
// glass
287275
list[i++] = new sphere(center, 0.2, new dielectric(1.5));
288276
}
289277
}

src/common/aabb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ class aabb {
6060
};
6161

6262
aabb surrounding_box(aabb box0, aabb box1) {
63-
vec3 small( ffmin(box0.min().x(), box1.min().x()),
64-
ffmin(box0.min().y(), box1.min().y()),
65-
ffmin(box0.min().z(), box1.min().z()));
66-
vec3 big ( ffmax(box0.max().x(), box1.max().x()),
67-
ffmax(box0.max().y(), box1.max().y()),
68-
ffmax(box0.max().z(), box1.max().z()));
63+
vec3 small(ffmin(box0.min().x(), box1.min().x()),
64+
ffmin(box0.min().y(), box1.min().y()),
65+
ffmin(box0.min().z(), box1.min().z()));
66+
vec3 big (ffmax(box0.max().x(), box1.max().x()),
67+
ffmax(box0.max().y(), box1.max().y()),
68+
ffmax(box0.max().z(), box1.max().z()));
6969
return aabb(small,big);
7070
}
7171

src/common/perlin.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ class perlin {
6868
static vec3* perlin_generate() {
6969
vec3 *p = new vec3[256];
7070
for (int i = 0; i < 256; ++i) {
71-
double x_random = random_double(-1,1);
72-
double y_random = random_double(-1,1);
73-
double z_random = random_double(-1,1);
74-
p[i] = unit_vector(vec3(x_random, y_random, z_random));
71+
p[i] = unit_vector(vec3::random(-1,1));
7572
}
7673
return p;
7774
}

src/common/vec3.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class vec3 {
7373
<< static_cast<int>(255.999 * clamp(b, 0.0, 1.0)) << '\n';
7474
}
7575

76+
inline static vec3 random() {
77+
return vec3(random_double(), random_double(), random_double());
78+
}
79+
80+
inline static vec3 random(double min, double max) {
81+
return vec3(random_double(min,max), random_double(min,max), random_double(min,max));
82+
}
83+
7684
double e[3];
7785
};
7886

@@ -141,7 +149,7 @@ vec3 random_unit_vector() {
141149
vec3 random_in_unit_sphere() {
142150
vec3 p;
143151
do {
144-
p = vec3(random_double(-1,1), random_double(-1,1), random_double(-1,1));
152+
p = vec3::random(-1,1);
145153
} while (p.squared_length() >= 1.0);
146154
return p;
147155
}

0 commit comments

Comments
 (0)