|
310 | 310 | #include <cmath>
|
311 | 311 | #include <iostream>
|
312 | 312 |
|
313 |
| - using std::sqrt; |
314 |
| - |
315 | 313 | class vec3 {
|
316 | 314 | public:
|
317 | 315 | double e[3];
|
|
346 | 344 | }
|
347 | 345 |
|
348 | 346 | double length() const {
|
349 |
| - return sqrt(length_squared()); |
| 347 | + return std::sqrt(length_squared()); |
350 | 348 | }
|
351 | 349 |
|
352 | 350 | double length_squared() const {
|
|
968 | 966 | if (discriminant < 0) {
|
969 | 967 | return -1.0;
|
970 | 968 | } else {
|
971 |
| - return (-b - sqrt(discriminant) ) / (2.0*a); |
| 969 | + return (-b - std::sqrt(discriminant) ) / (2.0*a); |
972 | 970 | }
|
973 | 971 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
974 | 972 | }
|
|
1013 | 1011 | if (discriminant < 0) {
|
1014 | 1012 | return -1.0;
|
1015 | 1013 | } else {
|
1016 |
| - return (-b - sqrt(discriminant) ) / (2.0*a); |
| 1014 | + return (-b - std::sqrt(discriminant) ) / (2.0*a); |
1017 | 1015 | }
|
1018 | 1016 | }
|
1019 | 1017 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
1055 | 1053 | return -1.0;
|
1056 | 1054 | } else {
|
1057 | 1055 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1058 |
| - return (h - sqrt(discriminant)) / a; |
| 1056 | + return (h - std::sqrt(discriminant)) / a; |
1059 | 1057 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1060 | 1058 | }
|
1061 | 1059 | }
|
|
1120 | 1118 |
|
1121 | 1119 | class sphere : public hittable {
|
1122 | 1120 | public:
|
1123 |
| - sphere(const point3& center, double radius) : center(center), radius(fmax(0,radius)) {} |
| 1121 | + sphere(const point3& center, double radius) : center(center), radius(std::fmax(0,radius)) {} |
1124 | 1122 |
|
1125 | 1123 | bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override {
|
1126 | 1124 | vec3 oc = center - r.origin();
|
|
1132 | 1130 | if (discriminant < 0)
|
1133 | 1131 | return false;
|
1134 | 1132 |
|
1135 |
| - auto sqrtd = sqrt(discriminant); |
| 1133 | + auto sqrtd = std::sqrt(discriminant); |
1136 | 1134 |
|
1137 | 1135 | // Find the nearest root that lies in the acceptable range.
|
1138 | 1136 | auto root = (h - sqrtd) / a;
|
|
1158 | 1156 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
1159 | 1157 | [Listing [sphere-initial]: <kbd>[sphere.h]</kbd> The sphere class]
|
1160 | 1158 |
|
| 1159 | +(Note here that we use the C++ standard function `std::fmax()`, which returns the maximum of the two |
| 1160 | +floating-point arguments. Similarly, we will later use `std::fmin()`, which returns the minimum of |
| 1161 | +the two floating-point arguments.) |
1161 | 1162 | </div>
|
1162 | 1163 |
|
1163 | 1164 |
|
|
1334 | 1335 |
|
1335 | 1336 | Some New C++ Features
|
1336 | 1337 | ----------------------
|
1337 |
| -The `hittable_list` class code uses two C++ features that may trip you up if you're not normally a |
1338 |
| -C++ programmer: `vector` and `shared_ptr`. |
| 1338 | +The `hittable_list` class code uses some C++ features that may trip you up if you're not normally a |
| 1339 | +C++ programmer: `vector`, `shared_ptr`, and `make_shared`. |
1339 | 1340 |
|
1340 | 1341 | `shared_ptr<type>` is a pointer to some allocated type, with reference-counting semantics. Every
|
1341 | 1342 | time you assign its value to another shared pointer (usually with a simple assignment), the
|
|
1410 | 1411 |
|
1411 | 1412 | using std::make_shared;
|
1412 | 1413 | using std::shared_ptr;
|
1413 |
| - using std::sqrt; |
1414 | 1414 |
|
1415 | 1415 | // Constants
|
1416 | 1416 |
|
|
1476 | 1476 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
|
1477 | 1477 | #include <cmath>
|
1478 | 1478 | #include <iostream>
|
1479 |
| - |
1480 |
| - using std::sqrt; |
1481 | 1479 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
1482 | 1480 | [Listing [assume-rtw-vec3]: <kbd>[vec3.h]</kbd> Assume rtweekend.h inclusion for vec3.h]
|
1483 | 1481 |
|
|
1998 | 1996 | return a canonical random number, which by convention falls in the range $0 ≤ n < 1$. The “less
|
1999 | 1997 | than” before the 1 is important, as we will sometimes take advantage of that.
|
2000 | 1998 |
|
2001 |
| -A simple approach to this is to use the `rand()` function that can be found in `<cstdlib>`, which |
2002 |
| -returns a random integer in the range 0 and `RAND_MAX`. Hence we can get a real random number as |
2003 |
| -desired with the following code snippet, added to `rtweekend.h`: |
| 1999 | +A simple approach to this is to use the `std::rand()` function that can be found in `<cstdlib>`, |
| 2000 | +which returns a random integer in the range 0 and `RAND_MAX`. Hence we can get a real random number |
| 2001 | +as desired with the following code snippet, added to `rtweekend.h`: |
2004 | 2002 |
|
2005 | 2003 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2006 | 2004 | #include <cmath>
|
|
2022 | 2020 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ Highlight
|
2023 | 2021 | inline double random_double() {
|
2024 | 2022 | // Returns a random real in [0,1).
|
2025 |
| - return rand() / (RAND_MAX + 1.0); |
| 2023 | + return std::rand() / (RAND_MAX + 1.0); |
2026 | 2024 | }
|
2027 | 2025 |
|
2028 | 2026 | inline double random_double(double min, double max) {
|
|
2742 | 2740 | inline double linear_to_gamma(double linear_component)
|
2743 | 2741 | {
|
2744 | 2742 | if (linear_component > 0)
|
2745 |
| - return sqrt(linear_component); |
| 2743 | + return std::sqrt(linear_component); |
2746 | 2744 |
|
2747 | 2745 | return 0;
|
2748 | 2746 | }
|
|
2869 | 2867 | class sphere : public hittable {
|
2870 | 2868 | public:
|
2871 | 2869 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
2872 |
| - sphere(const point3& center, double radius) : center(center), radius(fmax(0,radius)) { |
| 2870 | + sphere(const point3& center, double radius) : center(center), radius(std::fmax(0,radius)) { |
2873 | 2871 | // TODO: Initialize the material pointer `mat`.
|
2874 | 2872 | }
|
2875 | 2873 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
|
2953 | 2951 | the vector is very close to zero in all dimensions.
|
2954 | 2952 |
|
2955 | 2953 | The following changes will use the C++ standard library function `std::fabs`, which returns the
|
2956 |
| -absolute value of its input, so add this to our standard header. |
| 2954 | +absolute value of its input. |
2957 | 2955 |
|
2958 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2959 |
| - // C++ Std Usings |
2960 |
| - |
2961 |
| - |
2962 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
2963 |
| - using std::fabs; |
2964 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2965 |
| - using std::make_shared; |
2966 |
| - using std::shared_ptr; |
2967 |
| - using std::sqrt; |
2968 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2969 |
| - [Listing [declare-fabs]: <kbd>[rtweekend.h]</kbd> Declaring std::fabs()] |
2970 |
| - |
2971 |
| - |
2972 | 2956 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2973 | 2957 | class vec3 {
|
2974 | 2958 | ...
|
|
2982 | 2966 | bool near_zero() const {
|
2983 | 2967 | // Return true if the vector is close to zero in all dimensions.
|
2984 | 2968 | auto s = 1e-8;
|
2985 |
| - return (fabs(e[0]) < s) && (fabs(e[1]) < s) && (fabs(e[2]) < s); |
| 2969 | + return (std::fabs(e[0]) < s) && (std::fabs(e[1]) < s) && (std::fabs(e[2]) < s); |
2986 | 2970 | }
|
2987 | 2971 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2988 | 2972 |
|
|
3135 | 3119 | public:
|
3136 | 3120 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
3137 | 3121 | sphere(const point3& center, double radius, shared_ptr<material> mat)
|
3138 |
| - : center(center), radius(fmax(0,radius)), mat(mat) {} |
| 3122 | + : center(center), radius(std::fmax(0,radius)), mat(mat) {} |
3139 | 3123 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
3140 | 3124 |
|
3141 | 3125 | ...
|
|
3362 | 3346 |
|
3363 | 3347 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ Highlight
|
3364 | 3348 | inline vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) {
|
3365 |
| - auto cos_theta = fmin(dot(-uv, n), 1.0); |
| 3349 | + auto cos_theta = std::fmin(dot(-uv, n), 1.0); |
3366 | 3350 | vec3 r_out_perp = etai_over_etat * (uv + cos_theta*n);
|
3367 |
| - vec3 r_out_parallel = -sqrt(fabs(1.0 - r_out_perp.length_squared())) * n; |
| 3351 | + vec3 r_out_parallel = -std::sqrt(std::fabs(1.0 - r_out_perp.length_squared())) * n; |
3368 | 3352 | return r_out_perp + r_out_parallel;
|
3369 | 3353 | }
|
3370 | 3354 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
3371 | 3355 | [Listing [refract]: <kbd>[vec3.h]</kbd> Refraction function]
|
3372 | 3356 |
|
3373 | 3357 | </div>
|
3374 | 3358 |
|
3375 |
| -(Note here that we use the C++ standard function `fmin()`, which returns the minimum of the two |
3376 |
| -arguments. Similarly, we will later use `fmax()`, which returns the maximum of the two arguments.) |
3377 |
| - |
3378 | 3359 | <div class='together'>
|
3379 | 3360 | And the dielectric material that always refracts is:
|
3380 | 3361 |
|
|
3489 | 3470 | $$ \cos\theta = \mathbf{R} \cdot \mathbf{n} $$
|
3490 | 3471 |
|
3491 | 3472 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
3492 |
| - double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0); |
3493 |
| - double sin_theta = sqrt(1.0 - cos_theta*cos_theta); |
| 3473 | + double cos_theta = std::fmin(dot(-unit_direction, rec.normal), 1.0); |
| 3474 | + double sin_theta = std::sqrt(1.0 - cos_theta*cos_theta); |
3494 | 3475 |
|
3495 | 3476 | if (ri * sin_theta > 1.0) {
|
3496 | 3477 | // Must Reflect
|
|
3517 | 3498 |
|
3518 | 3499 | vec3 unit_direction = unit_vector(r_in.direction());
|
3519 | 3500 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
3520 |
| - double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0); |
3521 |
| - double sin_theta = sqrt(1.0 - cos_theta*cos_theta); |
| 3501 | + double cos_theta = std::fmin(dot(-unit_direction, rec.normal), 1.0); |
| 3502 | + double sin_theta = std::sqrt(1.0 - cos_theta*cos_theta); |
3522 | 3503 |
|
3523 | 3504 | bool cannot_refract = ri * sin_theta > 1.0;
|
3524 | 3505 | vec3 direction;
|
|
3603 | 3584 | double ri = rec.front_face ? (1.0/refraction_index) : refraction_index;
|
3604 | 3585 |
|
3605 | 3586 | vec3 unit_direction = unit_vector(r_in.direction());
|
3606 |
| - double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0); |
3607 |
| - double sin_theta = sqrt(1.0 - cos_theta*cos_theta); |
| 3587 | + double cos_theta = std::fmin(dot(-unit_direction, rec.normal), 1.0); |
| 3588 | + double sin_theta = std::sqrt(1.0 - cos_theta*cos_theta); |
3608 | 3589 |
|
3609 | 3590 | bool cannot_refract = ri * sin_theta > 1.0;
|
3610 | 3591 | vec3 direction;
|
|
3745 | 3726 | auto focal_length = 1.0;
|
3746 | 3727 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
3747 | 3728 | auto theta = degrees_to_radians(vfov);
|
3748 |
| - auto h = tan(theta/2); |
| 3729 | + auto h = std::tan(theta/2); |
3749 | 3730 | auto viewport_height = 2 * h * focal_length;
|
3750 | 3731 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
3751 | 3732 | auto viewport_width = viewport_height * (double(image_width)/image_height);
|
|
3780 | 3761 |
|
3781 | 3762 |
|
3782 | 3763 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
3783 |
| - auto R = cos(pi/4); |
| 3764 | + auto R = std::cos(pi/4); |
3784 | 3765 |
|
3785 | 3766 | auto material_left = make_shared<lambertian>(color(0,0,1));
|
3786 | 3767 | auto material_right = make_shared<lambertian>(color(1,0,0));
|
|
3889 | 3870 | auto focal_length = (lookfrom - lookat).length();
|
3890 | 3871 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
3891 | 3872 | auto theta = degrees_to_radians(vfov);
|
3892 |
| - auto h = tan(theta/2); |
| 3873 | + auto h = std::tan(theta/2); |
3893 | 3874 | auto viewport_height = 2 * h * focal_length;
|
3894 | 3875 | auto viewport_width = viewport_height * (double(image_width)/image_height);
|
3895 | 3876 |
|
|
4140 | 4121 | auto focal_length = (lookfrom - lookat).length();
|
4141 | 4122 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
4142 | 4123 | auto theta = degrees_to_radians(vfov);
|
4143 |
| - auto h = tan(theta/2); |
| 4124 | + auto h = std::tan(theta/2); |
4144 | 4125 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
4145 | 4126 | auto viewport_height = 2 * h * focus_dist;
|
4146 | 4127 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
|
4168 | 4149 |
|
4169 | 4150 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
4170 | 4151 | // Calculate the camera defocus disk basis vectors.
|
4171 |
| - auto defocus_radius = focus_dist * tan(degrees_to_radians(defocus_angle / 2)); |
| 4152 | + auto defocus_radius = focus_dist * std::tan(degrees_to_radians(defocus_angle / 2)); |
4172 | 4153 | defocus_disk_u = u * defocus_radius;
|
4173 | 4154 | defocus_disk_v = v * defocus_radius;
|
4174 | 4155 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
|
0 commit comments