More timing functions

This commit is contained in:
David 2021-08-28 01:04:31 +02:00
commit 321c677da2
12 changed files with 184 additions and 129 deletions

View file

@ -5,12 +5,12 @@
struct vec3 {
/* Members */
double x;
double y;
double z;
float x;
float y;
float z;
// Constructor proper. Values default to 0
vec3(double x = 0, double y = 0, double z = 0)
vec3(float x = 0, float y = 0, float z = 0)
{
this->x = x;
this->y = y;
@ -35,7 +35,7 @@ struct vec3 {
}
// Scalar multiplication
vec3& operator*=(const double t)
vec3& operator*=(const float t)
{
x *= t;
y *= t;
@ -44,7 +44,7 @@ struct vec3 {
}
// Division by a scalar t
vec3& operator/=(const double t)
vec3& operator/=(const float t)
{
x /= t;
y /= t;
@ -54,13 +54,13 @@ struct vec3 {
/* Methods */
double length() const
float length() const
{
return sqrt(x*x + y*y + z*z);
}
// Length squared, useful for some calculations
double length_squared() const
float length_squared() const
{
return x*x + y*y + z*z;
}
@ -68,19 +68,19 @@ struct vec3 {
// Get a vec3 with random components in the range [0,1)
inline static vec3 random()
{
return vec3(random_double(), random_double(), random_double());
return vec3(random_float(), random_float(), random_float());
}
// Get a vec3 with random components in the range [min, max)
inline static vec3 random(double min, double max)
inline static vec3 random(float min, float max)
{
return vec3(random_double(min, max), random_double(min, max), random_double(min, max));
return vec3(random_float(min, max), random_float(min, max), random_float(min, max));
}
// Check if all vector components are near zero
bool near_zero() const
{
double s = 1e-8;
float s = 1e-8;
return (fabs(x) < s) && (fabs(y) < s) && (fabs(z) < s);
}
};
@ -117,24 +117,24 @@ inline vec3 operator*(const vec3 &u, const vec3 &v)
}
// Scalar product
inline vec3 operator*(double t,const vec3 &v)
inline vec3 operator*(float t,const vec3 &v)
{
return vec3(t*v.x, t*v.y, t*v.z);
}
inline vec3 operator*(const vec3 &v, double t)
inline vec3 operator*(const vec3 &v, float t)
{
return t * v;
}
// Vector division by scalar. Note that we redefine it as multiplying by 1/t to avoid division by 0
inline vec3 operator/(vec3 v, double t)
inline vec3 operator/(vec3 v, float t)
{
return 1/t * v;
}
// Straightforward dot product
inline double dot(const vec3 &u, const vec3 &v)
inline float dot(const vec3 &u, const vec3 &v)
{
return u.x*v.x + u.y*v.y + u.z*v.z;
@ -191,9 +191,9 @@ vec3 reflect(const vec3& v, const vec3 n)
return v - 2*dot(v,n)*n;
}
vec3 refract (const vec3& uv, const vec3& n, double etai_over_etat)
vec3 refract (const vec3& uv, const vec3& n, float etai_over_etat)
{
double cos_theta = fmin(dot(-uv, n), 1.0);
float cos_theta = fmin(dot(-uv, n), 1.0);
vec3 r_out_perp = etai_over_etat * (uv + cos_theta*n);
vec3 r_out_parallel = -sqrt(fabs(1.0 - r_out_perp.length_squared())) * n;
return r_out_perp + r_out_parallel;
@ -203,7 +203,7 @@ vec3 random_in_unit_disk()
{
while (true)
{
auto p = vec3(random_double(-1,1), random_double(-1,1), 0);
auto p = vec3(random_float(-1,1), random_float(-1,1), 0);
if (p.length_squared() >= 1) continue;
return p;
}