diff --git a/src/base/geometry/Bounds.cpp b/src/base/geometry/Bounds.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/base/geometry/CMakeLists.txt b/src/base/geometry/CMakeLists.txt index 385631c..840a36b 100644 --- a/src/base/geometry/CMakeLists.txt +++ b/src/base/geometry/CMakeLists.txt @@ -33,6 +33,7 @@ list(APPEND HEADERS list(APPEND SOURCES Rotation.cpp + Bounds.cpp Transform.cpp grid/AbstractGrid.cpp math/Linalg.cpp diff --git a/src/base/geometry/math/Matrix.h b/src/base/geometry/math/Matrix.h index a308115..4fd6b70 100644 --- a/src/base/geometry/math/Matrix.h +++ b/src/base/geometry/math/Matrix.h @@ -1,5 +1,6 @@ #pragma once +#include "Vector.h" #include class Matrix diff --git a/src/base/geometry/math/Vector.cpp b/src/base/geometry/math/Vector.cpp index eca12bd..ce6a50a 100644 --- a/src/base/geometry/math/Vector.cpp +++ b/src/base/geometry/math/Vector.cpp @@ -1,17 +1,49 @@ #include "Vector.h" +#include +#include + +Vector::Vector(Primitive p) +{ + switch(p) + { + case Primitive::UNIT_X: + mX = 1.0; + break; + case Primitive::UNIT_Y: + mY = 1.0; + break; + case Primitive::UNIT_Z: + mZ = 1.0; + break; + case Primitive::NEGATIVE_X: + mX = -1.0; + break; + case Primitive::NEGATIVE_Y: + mY = -1.0; + break; + case Primitive::NEGATIVE_Z: + mZ = -1.0; + break; + } +} + Vector::Vector(double x, double y, double z) : mX(x), mY(y), mZ(z) { - updateLength(); } Vector::~Vector() { }; +void Vector::reverseDirection() +{ + (*this) *= -1.0; +} + double Vector::getX() const { return mX; @@ -29,7 +61,7 @@ double Vector::getZ() const double Vector::getLength() const { - return mLength; + return std::sqrt(mX * mX + mY * mY + mZ * mZ); } double Vector::dotProduct(const Vector& v) const @@ -44,7 +76,12 @@ Vector Vector::crossProduct(const Vector& v) const Vector Vector::getNormalized() const { - return Vector(mX / mLength, mY / mLength, mZ / mLength); + const auto length = getLength(); + if (length == 0.0) + { + throw std::logic_error("Divide by zero getting normalizing vector"); + } + return Vector(mX / length, mY / length, mZ / length); } void Vector::scale(double x, double y, double z) @@ -52,10 +89,4 @@ void Vector::scale(double x, double y, double z) mX = x * mX; mY = y * mY; mZ = z * mZ; - updateLength(); } - -void Vector::updateLength() -{ - mLength = std::sqrt(mX * mX + mY * mY + mZ * mZ); -} \ No newline at end of file diff --git a/src/base/geometry/math/Vector.h b/src/base/geometry/math/Vector.h index 098c752..46dfe44 100644 --- a/src/base/geometry/math/Vector.h +++ b/src/base/geometry/math/Vector.h @@ -1,15 +1,30 @@ #pragma once -#include -#include - class Vector { public: + enum class Primitive + { + UNIT_X, + UNIT_Y, + UNIT_Z, + NEGATIVE_X, + NEGATIVE_Y, + NEGATIVE_Z + }; + + Vector(Primitive p); + Vector(double x = 0, double y = 0, double z = 0); ~Vector(); + double dotProduct(const Vector& v) const; + + Vector crossProduct(const Vector& v) const; + + void reverseDirection(); + double getX() const; double getY() const; @@ -20,11 +35,21 @@ public: Vector getNormalized() const; - void scale(double x, double y, double z = 1.0); + void scale(double x, double y = 1.0, double z = 1.0); - double dotProduct(const Vector& v) const; + void operator*=(double d) + { + mX *= d; + mY *= d; + mZ *= d; + } - Vector crossProduct(const Vector& v) const; + void operator+=(const Vector& vector) + { + mX += vector.mX; + mY += vector.mY; + mZ += vector.mZ; + } bool operator==(const Vector& rhs) const { @@ -39,10 +64,7 @@ public: } private: - void updateLength(); - - double mLength{ 0 }; double mX{ 0 }; double mY{ 0 }; double mZ{ 0 }; -}; \ No newline at end of file +}; diff --git a/src/base/geometry/points/Point.cpp b/src/base/geometry/points/Point.cpp index 2ced173..65a1d1d 100644 --- a/src/base/geometry/points/Point.cpp +++ b/src/base/geometry/points/Point.cpp @@ -31,11 +31,6 @@ Point::~Point() { }; -std::unique_ptr Point::Create(double x, double y, double z) -{ - return std::make_unique(x, y, z); -} - double Point::getX() const { return mX; @@ -101,3 +96,8 @@ void Point::move(double x, double y, double z) mY += y; mZ += z; } + +void Point::move(const Vector& vector) +{ + move(vector.getX(), vector.getY(), vector.getZ()); +} diff --git a/src/base/geometry/points/Point.h b/src/base/geometry/points/Point.h index c5c2ffb..8efa5f2 100644 --- a/src/base/geometry/points/Point.h +++ b/src/base/geometry/points/Point.h @@ -18,14 +18,8 @@ public: ~Point(); - static std::unique_ptr Create(double x, double y, double z = 0); - void apply(const Transform& transform); - bool isAtOrigin() const; - - void move(double x, double y, double z = 0); - double getX() const; double getY() const; @@ -42,6 +36,17 @@ public: Vector getDelta(const Point& point) const; + bool isAtOrigin() const; + + void move(const Vector& vector); + + void move(double x, double y = 0.0, double z = 0.0); + + void operator+=(const Vector& vector) + { + move(vector); + } + bool operator==(const Point& rhs) const { return (mX == rhs.mX) diff --git a/src/base/geometry/points/PointParser.h b/src/base/geometry/points/PointParser.h index 4c2f1b1..3ab83ce 100644 --- a/src/base/geometry/points/PointParser.h +++ b/src/base/geometry/points/PointParser.h @@ -8,11 +8,11 @@ class PointParser { public: - static std::string toString(const Point& p, std::size_t dimensions = 3, const std::string& delimiter = " ", std::size_t precision = 0); + static std::string toString(const Point& p, std::size_t dimensions = 3, const std::string& delimiter = " ", std::size_t precision = 0); - static std::string toStringRelative(const Point& p, const Point& relativeTo, std::size_t dimensions = 3, const std::string& delimiter = " ", std::size_t precision = 0); + static std::string toStringRelative(const Point& p, const Point& relativeTo, std::size_t dimensions = 3, const std::string& delimiter = " ", std::size_t precision = 0); - static std::string toString(double x, double y, double z, std::size_t dimensions = 3, const std::string& delimiter = " ", std::size_t precision = 0); + static std::string toString(double x, double y, double z, std::size_t dimensions = 3, const std::string& delimiter = " ", std::size_t precision = 0); - static std::string toString(double x, std::size_t precision = 0); + static std::string toString(double x, std::size_t precision = 0); };