cloudy-raytracer/common/vector2d.cpp

91 lines
2.9 KiB
C++
Raw Permalink Normal View History

2022-10-28 09:31:13 +02:00
#include "common/vector2d.h"
2022-11-11 14:27:43 +01:00
#include "common/common.h"
#include <algorithm>
2022-10-28 09:31:13 +02:00
#include <cassert>
// Access operators ////////////////////////////////////////////////////////////
float &Vector2d::operator[](int dimension) {
assert(0 <= dimension && dimension < 2);
switch (dimension) {
case Dimension::U:
return this->u;
case Dimension::V:
return this->v;
default: // This must never happen
return this->u;
}
}
float const &Vector2d::operator[](int dimension) const {
assert(0 <= dimension && dimension < 2);
switch (dimension) {
case Dimension::U:
return this->u;
case Dimension::V:
return this->v;
default: // This must never happen
return this->u;
}
}
// Comparison operators ////////////////////////////////////////////////////////
bool operator==(Vector2d const &left, Vector2d const &right) { return (left.u == right.u && left.v == right.v); }
bool operator!=(Vector2d const &left, Vector2d const &right) { return !(left == right); }
// Arithmetic operators ////////////////////////////////////////////////////////
Vector2d operator+(Vector2d const &left, Vector2d const &right) { return Vector2d(left.u + right.u, left.v + right.v); }
Vector2d operator-(Vector2d const &right) { return Vector2d(-right.u, -right.v); }
Vector2d operator-(Vector2d const &left, Vector2d const &right) { return Vector2d(left.u - right.u, left.v - right.v); }
Vector2d operator*(Vector2d const &left, float right) { return Vector2d(left.u * right, left.v * right); }
Vector2d operator*(float left, Vector2d const &right) { return Vector2d(left * right.u, left * right.v); }
Vector2d operator/(Vector2d const &left, float right) { return Vector2d(left.u / right, left.v / right); }
// Assignment operators ////////////////////////////////////////////////////////
Vector2d &operator+=(Vector2d &left, Vector2d const &right) {
left.u += right.u;
left.v += right.v;
return left;
}
Vector2d &operator-=(Vector2d &left, Vector2d const &right) {
left.u -= right.u;
left.v -= right.v;
return left;
}
Vector2d &operator*=(Vector2d &left, float right) {
left.u *= right;
left.v *= right;
return left;
}
Vector2d &operator/=(Vector2d &left, float right) {
left.u /= right;
left.v /= right;
return left;
}
// Useful functions ////////////////////////////////////////////////////////////
2022-11-11 14:27:43 +01:00
Vector2d componentProduct(const Vector2d &left, const Vector2d &right) { return Vector2d(left.u * right.u, left.v * right.v); }
2022-10-28 09:31:13 +02:00
2022-11-11 14:27:43 +01:00
Vector2d componentQuotient(const Vector2d &left, const Vector2d &right) { return Vector2d(left.u / right.u, left.v / right.v); }
2022-10-28 09:31:13 +02:00
float dotProduct(Vector2d const &left, Vector2d const &right) { return left.u * right.u + left.v * right.v; }
float length(Vector2d const &c) { return std::sqrt(dotProduct(c, c)); }
2022-11-11 14:27:43 +01:00
Vector2d normalized(Vector2d const &v) { return v / std::max(length(v), NORM_EPS); }
2022-10-28 09:31:13 +02:00
void normalize(Vector2d *v) { *v = normalized(*v); }