Skip to content

Commit 1b8b4e4

Browse files
committed
From C++ static_cast to ctor-like conversion
Instead of static_cast<double>(x) switch to double(x) for easier readability for non-C++ readers and simple for C++ readers alike. In the end, these are the same thing. Resolves #1222
1 parent 2f1c0f2 commit 1b8b4e4

File tree

16 files changed

+86
-86
lines changed

16 files changed

+86
-86
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@
122122
auto g = double(j) / (image_height-1);
123123
auto b = 0;
124124

125-
int ir = static_cast<int>(255.999 * r);
126-
int ig = static_cast<int>(255.999 * g);
127-
int ib = static_cast<int>(255.999 * b);
125+
int ir = int(255.999 * r);
126+
int ig = int(255.999 * g);
127+
int ib = int(255.999 * b);
128128

129129
std::cout << ir << ' ' << ig << ' ' << ib << '\n';
130130
}
@@ -240,9 +240,9 @@
240240
auto g = double(j) / (image_height-1);
241241
auto b = 0;
242242

243-
int ir = static_cast<int>(255.999 * r);
244-
int ig = static_cast<int>(255.999 * g);
245-
int ib = static_cast<int>(255.999 * b);
243+
int ir = int(255.999 * r);
244+
int ig = int(255.999 * g);
245+
int ib = int(255.999 * b);
246246

247247
std::cout << ir << ' ' << ig << ' ' << ib << '\n';
248248
}
@@ -405,9 +405,9 @@
405405

406406
void write_color(std::ostream &out, color pixel_color) {
407407
// Write the translated [0,255] value of each color component.
408-
out << static_cast<int>(255.999 * pixel_color.x()) << ' '
409-
<< static_cast<int>(255.999 * pixel_color.y()) << ' '
410-
<< static_cast<int>(255.999 * pixel_color.z()) << '\n';
408+
out << int(255.999 * pixel_color.x()) << ' '
409+
<< int(255.999 * pixel_color.y()) << ' '
410+
<< int(255.999 * pixel_color.z()) << '\n';
411411
}
412412

413413
#endif
@@ -548,12 +548,12 @@
548548
int image_width = 400;
549549

550550
// Calculate the image height, and ensure that it's at least 1.
551-
int image_height = static_cast<int>(image_width / aspect_ratio);
551+
int image_height = int(image_width / aspect_ratio);
552552
image_height = (image_height < 1) ? 1 : image_height;
553553

554554
// Viewport widths less than one are ok since they are real valued.
555555
auto viewport_height = 2.0;
556-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
556+
auto viewport_width = viewport_height * (double(image_width)/image_height);
557557
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
558558
[Listing [image-setup]: Rendered image setup]
559559

@@ -634,14 +634,14 @@
634634
int image_width = 400;
635635

636636
// Calculate the image height, and ensure that it's at least 1.
637-
int image_height = static_cast<int>(image_width / aspect_ratio);
637+
int image_height = int(image_width / aspect_ratio);
638638
image_height = (image_height < 1) ? 1 : image_height;
639639

640640
// Camera
641641

642642
auto focal_length = 1.0;
643643
auto viewport_height = 2.0;
644-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
644+
auto viewport_width = viewport_height * (double(image_width)/image_height);
645645
auto camera_center = point3(0, 0, 0);
646646

647647
// Calculate the vectors across the horizontal and down the vertical viewport edges.
@@ -1426,7 +1426,7 @@
14261426
int image_width = 400;
14271427

14281428
// Calculate the image height, and ensure that it's at least 1.
1429-
int image_height = static_cast<int>(image_width / aspect_ratio);
1429+
int image_height = int(image_width / aspect_ratio);
14301430
image_height = (image_height < 1) ? 1 : image_height;
14311431

14321432

@@ -1443,7 +1443,7 @@
14431443

14441444
auto focal_length = 1.0;
14451445
auto viewport_height = 2.0;
1446-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
1446+
auto viewport_width = viewport_height * (double(image_width)/image_height);
14471447
auto camera_center = point3(0, 0, 0);
14481448

14491449
// Calculate the vectors across the horizontal and down the vertical viewport edges.
@@ -1781,15 +1781,15 @@
17811781
vec3 pixel_delta_v; // Offset to pixel below
17821782

17831783
void initialize() {
1784-
image_height = static_cast<int>(image_width / aspect_ratio);
1784+
image_height = int(image_width / aspect_ratio);
17851785
image_height = (image_height < 1) ? 1 : image_height;
17861786

17871787
center = point3(0, 0, 0);
17881788

17891789
// Determine viewport dimensions.
17901790
auto focal_length = 1.0;
17911791
auto viewport_height = 2.0;
1792-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
1792+
auto viewport_width = viewport_height * (double(image_width)/image_height);
17931793

17941794
// Calculate the vectors across the horizontal and down the vertical viewport edges.
17951795
auto viewport_u = vec3(viewport_width, 0, 0);
@@ -1986,9 +1986,9 @@
19861986

19871987
// Write the translated [0,255] value of each color component.
19881988
static const interval intensity(0.000, 0.999);
1989-
out << static_cast<int>(256 * intensity.clamp(r)) << ' '
1990-
<< static_cast<int>(256 * intensity.clamp(g)) << ' '
1991-
<< static_cast<int>(256 * intensity.clamp(b)) << '\n';
1989+
out << int(256 * intensity.clamp(r)) << ' '
1990+
<< int(256 * intensity.clamp(g)) << ' '
1991+
<< int(256 * intensity.clamp(b)) << '\n';
19921992
}
19931993
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19941994
[Listing [write-color-clamped]: <kbd>[color.h]</kbd> The multi-sample write_color() function]
@@ -2632,9 +2632,9 @@
26322632

26332633
// Write the translated [0,255] value of each color component.
26342634
static const interval intensity(0.000, 0.999);
2635-
out << static_cast<int>(256 * intensity.clamp(r)) << ' '
2636-
<< static_cast<int>(256 * intensity.clamp(g)) << ' '
2637-
<< static_cast<int>(256 * intensity.clamp(b)) << '\n';
2635+
out << int(256 * intensity.clamp(r)) << ' '
2636+
<< int(256 * intensity.clamp(g)) << ' '
2637+
<< int(256 * intensity.clamp(b)) << '\n';
26382638
}
26392639
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26402640
[Listing [write-color-gamma]: <kbd>[color.h]</kbd> write_color(), with gamma correction]
@@ -3484,7 +3484,7 @@
34843484
...
34853485

34863486
void initialize() {
3487-
image_height = static_cast<int>(image_width / aspect_ratio);
3487+
image_height = int(image_width / aspect_ratio);
34883488
image_height = (image_height < 1) ? 1 : image_height;
34893489

34903490
center = point3(0, 0, 0);
@@ -3496,7 +3496,7 @@
34963496
auto h = tan(theta/2);
34973497
auto viewport_height = 2 * h * focal_length;
34983498
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
3499-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
3499+
auto viewport_width = viewport_height * (double(image_width)/image_height);
35003500

35013501
// Calculate the vectors across the horizontal and down the vertical viewport edges.
35023502
auto viewport_u = vec3(viewport_width, 0, 0);
@@ -3621,7 +3621,7 @@
36213621
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
36223622

36233623
void initialize() {
3624-
image_height = static_cast<int>(image_width / aspect_ratio);
3624+
image_height = int(image_width / aspect_ratio);
36253625
image_height = (image_height < 1) ? 1 : image_height;
36263626

36273627

@@ -3636,7 +3636,7 @@
36363636
auto theta = degrees_to_radians(vfov);
36373637
auto h = tan(theta/2);
36383638
auto viewport_height = 2 * h * focal_length;
3639-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
3639+
auto viewport_width = viewport_height * (double(image_width)/image_height);
36403640

36413641

36423642
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -3866,7 +3866,7 @@
38663866
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
38673867

38683868
void initialize() {
3869-
image_height = static_cast<int>(image_width / aspect_ratio);
3869+
image_height = int(image_width / aspect_ratio);
38703870
image_height = (image_height < 1) ? 1 : image_height;
38713871

38723872
center = lookfrom;
@@ -3880,7 +3880,7 @@
38803880
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
38813881
auto viewport_height = 2 * h * focus_dist;
38823882
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
3883-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
3883+
auto viewport_width = viewport_height * (double(image_width)/image_height);
38843884

38853885
// Calculate the u,v,w unit basis vectors for the camera coordinate frame.
38863886
w = unit_vector(lookfrom - lookat);

books/RayTracingTheNextWeek.html

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@
969969
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
970970
inline int random_int(int min, int max) {
971971
// Returns a random integer in [min,max].
972-
return static_cast<int>(random_double(min, max+1));
972+
return int(random_double(min, max+1));
973973
}
974974
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
975975
[Listing [random-int]: <kbd>[rtweekend.h]</kbd> A function to return random integers in a range]
@@ -1271,9 +1271,9 @@
12711271
{}
12721272

12731273
color value(double u, double v, const point3& p) const override {
1274-
auto xInteger = static_cast<int>(std::floor(inv_scale * p.x()));
1275-
auto yInteger = static_cast<int>(std::floor(inv_scale * p.y()));
1276-
auto zInteger = static_cast<int>(std::floor(inv_scale * p.z()));
1274+
auto xInteger = int(std::floor(inv_scale * p.x()));
1275+
auto yInteger = int(std::floor(inv_scale * p.y()));
1276+
auto zInteger = int(std::floor(inv_scale * p.z()));
12771277

12781278
bool isEven = (xInteger + yInteger + zInteger) % 2 == 0;
12791279

@@ -1739,8 +1739,8 @@
17391739
u = interval(0,1).clamp(u);
17401740
v = 1.0 - interval(0,1).clamp(v); // Flip V to image coordinates
17411741

1742-
auto i = static_cast<int>(u * image.width());
1743-
auto j = static_cast<int>(v * image.height());
1742+
auto i = int(u * image.width());
1743+
auto j = int(v * image.height());
17441744
auto pixel = image.pixel_data(i,j);
17451745

17461746
auto color_scale = 1.0 / 255.0;
@@ -1877,9 +1877,9 @@
18771877
}
18781878

18791879
double noise(const point3& p) const {
1880-
auto i = static_cast<int>(4*p.x()) & 255;
1881-
auto j = static_cast<int>(4*p.y()) & 255;
1882-
auto k = static_cast<int>(4*p.z()) & 255;
1880+
auto i = int(4*p.x()) & 255;
1881+
auto j = int(4*p.y()) & 255;
1882+
auto k = int(4*p.z()) & 255;
18831883

18841884
return ranfloat[perm_x[i] ^ perm_y[j] ^ perm_z[k]];
18851885
}
@@ -2010,9 +2010,9 @@
20102010
auto v = p.y() - floor(p.y());
20112011
auto w = p.z() - floor(p.z());
20122012

2013-
auto i = static_cast<int>(floor(p.x()));
2014-
auto j = static_cast<int>(floor(p.y()));
2015-
auto k = static_cast<int>(floor(p.z()));
2013+
auto i = int(floor(p.x()));
2014+
auto j = int(floor(p.y()));
2015+
auto k = int(floor(p.z()));
20162016
double c[2][2][2];
20172017

20182018
for (int di=0; di < 2; di++)
@@ -2078,9 +2078,9 @@
20782078
w = w*w*(3-2*w);
20792079
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
20802080

2081-
auto i = static_cast<int>(floor(p.x()));
2082-
auto j = static_cast<int>(floor(p.y()));
2083-
auto k = static_cast<int>(floor(p.z()));
2081+
auto i = int(floor(p.x()));
2082+
auto j = int(floor(p.y()));
2083+
auto k = int(floor(p.z()));
20842084
...
20852085
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20862086
[Listing [perlin-smoothed]: <kbd>[perlin.h]</kbd> Perlin smoothed]
@@ -2214,9 +2214,9 @@
22142214
auto v = p.y() - floor(p.y());
22152215
auto w = p.z() - floor(p.z());
22162216
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2217-
auto i = static_cast<int>(floor(p.x()));
2218-
auto j = static_cast<int>(floor(p.y()));
2219-
auto k = static_cast<int>(floor(p.z()));
2217+
auto i = int(floor(p.x()));
2218+
auto j = int(floor(p.y()));
2219+
auto k = int(floor(p.z()));
22202220
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
22212221
vec3 c[2][2][2];
22222222
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

books/RayTracingTheRestOfYourLife.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,7 +3596,7 @@
35963596
}
35973597

35983598
vec3 random(const vec3& o) const override {
3599-
auto int_size = static_cast<int>(objects.size());
3599+
auto int_size = int(objects.size());
36003600
return objects[random_int(0, int_size-1)]->random(o);
36013601
}
36023602
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -3680,9 +3680,9 @@
36803680

36813681
// Write the translated [0,255] value of each color component.
36823682
static const interval intensity(0.000, 0.999);
3683-
out << static_cast<int>(256 * intensity.clamp(r)) << ' '
3684-
<< static_cast<int>(256 * intensity.clamp(g)) << ' '
3685-
<< static_cast<int>(256 * intensity.clamp(b)) << '\n';
3683+
out << int(256 * intensity.clamp(r)) << ' '
3684+
<< int(256 * intensity.clamp(g)) << ' '
3685+
<< int(256 * intensity.clamp(b)) << '\n';
36863686
}
36873687
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36883688
[Listing [write-color-nan]: <kbd>[color.h]</kbd> NaN-tolerant write_color function]

src/InOneWeekend/camera.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class camera {
6666
vec3 defocus_disk_v; // Defocus disk vertical radius
6767

6868
void initialize() {
69-
image_height = static_cast<int>(image_width / aspect_ratio);
69+
image_height = int(image_width / aspect_ratio);
7070
image_height = (image_height < 1) ? 1 : image_height;
7171

7272
center = lookfrom;
@@ -75,7 +75,7 @@ class camera {
7575
auto theta = degrees_to_radians(vfov);
7676
auto h = tan(theta/2);
7777
auto viewport_height = 2 * h * focus_dist;
78-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
78+
auto viewport_width = viewport_height * (double(image_width)/image_height);
7979

8080
// Calculate the u,v,w unit basis vectors for the camera coordinate frame.
8181
w = unit_vector(lookfrom - lookat);

src/InOneWeekend/color.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
4343

4444
// Write the translated [0,255] value of each color component.
4545
static const interval intensity(0.000, 0.999);
46-
out << static_cast<int>(256 * intensity.clamp(r)) << ' '
47-
<< static_cast<int>(256 * intensity.clamp(g)) << ' '
48-
<< static_cast<int>(256 * intensity.clamp(b)) << '\n';
46+
out << int(256 * intensity.clamp(r)) << ' '
47+
<< int(256 * intensity.clamp(g)) << ' '
48+
<< int(256 * intensity.clamp(b)) << '\n';
4949
}
5050

5151

src/TheNextWeek/camera.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class camera {
6767
vec3 defocus_disk_v; // Defocus disk vertical radius
6868

6969
void initialize() {
70-
image_height = static_cast<int>(image_width / aspect_ratio);
70+
image_height = int(image_width / aspect_ratio);
7171
image_height = (image_height < 1) ? 1 : image_height;
7272

7373
center = lookfrom;
@@ -76,7 +76,7 @@ class camera {
7676
auto theta = degrees_to_radians(vfov);
7777
auto h = tan(theta/2);
7878
auto viewport_height = 2 * h * focus_dist;
79-
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
79+
auto viewport_width = viewport_height * (double(image_width)/image_height);
8080

8181
// Calculate the u,v,w unit basis vectors for the camera coordinate frame.
8282
w = unit_vector(lookfrom - lookat);

src/TheNextWeek/color.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
4343

4444
// Write the translated [0,255] value of each color component.
4545
static const interval intensity(0.000, 0.999);
46-
out << static_cast<int>(256 * intensity.clamp(r)) << ' '
47-
<< static_cast<int>(256 * intensity.clamp(g)) << ' '
48-
<< static_cast<int>(256 * intensity.clamp(b)) << '\n';
46+
out << int(256 * intensity.clamp(r)) << ' '
47+
<< int(256 * intensity.clamp(g)) << ' '
48+
<< int(256 * intensity.clamp(b)) << '\n';
4949
}
5050

5151

src/TheNextWeek/perlin.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class perlin {
3838
auto u = p.x() - floor(p.x());
3939
auto v = p.y() - floor(p.y());
4040
auto w = p.z() - floor(p.z());
41-
auto i = static_cast<int>(floor(p.x()));
42-
auto j = static_cast<int>(floor(p.y()));
43-
auto k = static_cast<int>(floor(p.z()));
41+
auto i = int(floor(p.x()));
42+
auto j = int(floor(p.y()));
43+
auto k = int(floor(p.z()));
4444
vec3 c[2][2][2];
4545

4646
for (int di=0; di < 2; di++)

src/TheNextWeek/rtweekend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ inline double random_double(double min, double max) {
4444

4545
inline int random_int(int min, int max) {
4646
// Returns a random integer in [min,max].
47-
return static_cast<int>(random_double(min, max+1));
47+
return int(random_double(min, max+1));
4848
}
4949

5050
// Common Headers

0 commit comments

Comments
 (0)