Skip to content

From C++ static_cast to ctor-like conversion #1227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions books/RayTracingInOneWeekend.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
auto g = double(j) / (image_height-1);
auto b = 0;

int ir = static_cast<int>(255.999 * r);
int ig = static_cast<int>(255.999 * g);
int ib = static_cast<int>(255.999 * b);
int ir = int(255.999 * r);
int ig = int(255.999 * g);
int ib = int(255.999 * b);

std::cout << ir << ' ' << ig << ' ' << ib << '\n';
}
Expand Down Expand Up @@ -240,9 +240,9 @@
auto g = double(j) / (image_height-1);
auto b = 0;

int ir = static_cast<int>(255.999 * r);
int ig = static_cast<int>(255.999 * g);
int ib = static_cast<int>(255.999 * b);
int ir = int(255.999 * r);
int ig = int(255.999 * g);
int ib = int(255.999 * b);

std::cout << ir << ' ' << ig << ' ' << ib << '\n';
}
Expand Down Expand Up @@ -405,9 +405,9 @@

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

#endif
Expand Down Expand Up @@ -548,12 +548,12 @@
int image_width = 400;

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

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

Expand Down Expand Up @@ -634,14 +634,14 @@
int image_width = 400;

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

// Camera

auto focal_length = 1.0;
auto viewport_height = 2.0;
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
auto viewport_width = viewport_height * (double(image_width)/image_height);
auto camera_center = point3(0, 0, 0);

// Calculate the vectors across the horizontal and down the vertical viewport edges.
Expand Down Expand Up @@ -1426,7 +1426,7 @@
int image_width = 400;

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


Expand All @@ -1443,7 +1443,7 @@

auto focal_length = 1.0;
auto viewport_height = 2.0;
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
auto viewport_width = viewport_height * (double(image_width)/image_height);
auto camera_center = point3(0, 0, 0);

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

void initialize() {
image_height = static_cast<int>(image_width / aspect_ratio);
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

center = point3(0, 0, 0);

// Determine viewport dimensions.
auto focal_length = 1.0;
auto viewport_height = 2.0;
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
auto viewport_width = viewport_height * (double(image_width)/image_height);

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

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

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

void initialize() {
image_height = static_cast<int>(image_width / aspect_ratio);
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

center = point3(0, 0, 0);
Expand All @@ -3496,7 +3496,7 @@
auto h = tan(theta/2);
auto viewport_height = 2 * h * focal_length;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
auto viewport_width = viewport_height * (double(image_width)/image_height);

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

void initialize() {
image_height = static_cast<int>(image_width / aspect_ratio);
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;


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


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
Expand Down Expand Up @@ -3866,7 +3866,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

void initialize() {
image_height = static_cast<int>(image_width / aspect_ratio);
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

center = lookfrom;
Expand All @@ -3880,7 +3880,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
auto viewport_height = 2 * h * focus_dist;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
auto viewport_width = viewport_height * (static_cast<double>(image_width)/image_height);
auto viewport_width = viewport_height * (double(image_width)/image_height);

// Calculate the u,v,w unit basis vectors for the camera coordinate frame.
w = unit_vector(lookfrom - lookat);
Expand Down
36 changes: 18 additions & 18 deletions books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
inline int random_int(int min, int max) {
// Returns a random integer in [min,max].
return static_cast<int>(random_double(min, max+1));
return int(random_double(min, max+1));
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [random-int]: <kbd>[rtweekend.h]</kbd> A function to return random integers in a range]
Expand Down Expand Up @@ -1271,9 +1271,9 @@
{}

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

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

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

auto i = static_cast<int>(u * image.width());
auto j = static_cast<int>(v * image.height());
auto i = int(u * image.width());
auto j = int(v * image.height());
auto pixel = image.pixel_data(i,j);

auto color_scale = 1.0 / 255.0;
Expand Down Expand Up @@ -1877,9 +1877,9 @@
}

double noise(const point3& p) const {
auto i = static_cast<int>(4*p.x()) & 255;
auto j = static_cast<int>(4*p.y()) & 255;
auto k = static_cast<int>(4*p.z()) & 255;
auto i = int(4*p.x()) & 255;
auto j = int(4*p.y()) & 255;
auto k = int(4*p.z()) & 255;

return ranfloat[perm_x[i] ^ perm_y[j] ^ perm_z[k]];
}
Expand Down Expand Up @@ -2010,9 +2010,9 @@
auto v = p.y() - floor(p.y());
auto w = p.z() - floor(p.z());

auto i = static_cast<int>(floor(p.x()));
auto j = static_cast<int>(floor(p.y()));
auto k = static_cast<int>(floor(p.z()));
auto i = int(floor(p.x()));
auto j = int(floor(p.y()));
auto k = int(floor(p.z()));
double c[2][2][2];

for (int di=0; di < 2; di++)
Expand Down Expand Up @@ -2078,9 +2078,9 @@
w = w*w*(3-2*w);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

auto i = static_cast<int>(floor(p.x()));
auto j = static_cast<int>(floor(p.y()));
auto k = static_cast<int>(floor(p.z()));
auto i = int(floor(p.x()));
auto j = int(floor(p.y()));
auto k = int(floor(p.z()));
...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [perlin-smoothed]: <kbd>[perlin.h]</kbd> Perlin smoothed]
Expand Down Expand Up @@ -2214,9 +2214,9 @@
auto v = p.y() - floor(p.y());
auto w = p.z() - floor(p.z());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
auto i = static_cast<int>(floor(p.x()));
auto j = static_cast<int>(floor(p.y()));
auto k = static_cast<int>(floor(p.z()));
auto i = int(floor(p.x()));
auto j = int(floor(p.y()));
auto k = int(floor(p.z()));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
vec3 c[2][2][2];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
Expand Down
8 changes: 4 additions & 4 deletions books/RayTracingTheRestOfYourLife.html
Original file line number Diff line number Diff line change
Expand Up @@ -3597,7 +3597,7 @@
}

vec3 random(const vec3& o) const override {
auto int_size = static_cast<int>(objects.size());
auto int_size = int(objects.size());
return objects[random_int(0, int_size-1)]->random(o);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
Expand Down Expand Up @@ -3681,9 +3681,9 @@

// Write the translated [0,255] value of each color component.
static const interval intensity(0.000, 0.999);
out << static_cast<int>(256 * intensity.clamp(r)) << ' '
<< static_cast<int>(256 * intensity.clamp(g)) << ' '
<< static_cast<int>(256 * intensity.clamp(b)) << '\n';
out << int(256 * intensity.clamp(r)) << ' '
<< int(256 * intensity.clamp(g)) << ' '
<< int(256 * intensity.clamp(b)) << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [write-color-nan]: <kbd>[color.h]</kbd> NaN-tolerant write_color function]
Expand Down
4 changes: 2 additions & 2 deletions src/InOneWeekend/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class camera {
vec3 defocus_disk_v; // Defocus disk vertical radius

void initialize() {
image_height = static_cast<int>(image_width / aspect_ratio);
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

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

// Calculate the u,v,w unit basis vectors for the camera coordinate frame.
w = unit_vector(lookfrom - lookat);
Expand Down
6 changes: 3 additions & 3 deletions src/InOneWeekend/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {

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


Expand Down
4 changes: 2 additions & 2 deletions src/TheNextWeek/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class camera {
vec3 defocus_disk_v; // Defocus disk vertical radius

void initialize() {
image_height = static_cast<int>(image_width / aspect_ratio);
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

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

// Calculate the u,v,w unit basis vectors for the camera coordinate frame.
w = unit_vector(lookfrom - lookat);
Expand Down
6 changes: 3 additions & 3 deletions src/TheNextWeek/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {

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


Expand Down
6 changes: 3 additions & 3 deletions src/TheNextWeek/perlin.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class perlin {
auto u = p.x() - floor(p.x());
auto v = p.y() - floor(p.y());
auto w = p.z() - floor(p.z());
auto i = static_cast<int>(floor(p.x()));
auto j = static_cast<int>(floor(p.y()));
auto k = static_cast<int>(floor(p.z()));
auto i = int(floor(p.x()));
auto j = int(floor(p.y()));
auto k = int(floor(p.z()));
vec3 c[2][2][2];

for (int di=0; di < 2; di++)
Expand Down
2 changes: 1 addition & 1 deletion src/TheNextWeek/rtweekend.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ inline double random_double(double min, double max) {

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

// Common Headers
Expand Down
Loading