Small cleaning.

This commit is contained in:
James Grogan 2023-01-28 20:48:04 +00:00
parent 9e1d951520
commit 9f036d6438
8 changed files with 94 additions and 34 deletions

View file

View file

@ -33,6 +33,7 @@ list(APPEND HEADERS
list(APPEND SOURCES list(APPEND SOURCES
Rotation.cpp Rotation.cpp
Bounds.cpp
Transform.cpp Transform.cpp
grid/AbstractGrid.cpp grid/AbstractGrid.cpp
math/Linalg.cpp math/Linalg.cpp

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Vector.h"
#include <vector> #include <vector>
class Matrix class Matrix

View file

@ -1,17 +1,49 @@
#include "Vector.h" #include "Vector.h"
#include <cmath>
#include <stdexcept>
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) Vector::Vector(double x, double y, double z)
: mX(x), : mX(x),
mY(y), mY(y),
mZ(z) mZ(z)
{ {
updateLength();
} }
Vector::~Vector() Vector::~Vector()
{ {
}; };
void Vector::reverseDirection()
{
(*this) *= -1.0;
}
double Vector::getX() const double Vector::getX() const
{ {
return mX; return mX;
@ -29,7 +61,7 @@ double Vector::getZ() const
double Vector::getLength() const double Vector::getLength() const
{ {
return mLength; return std::sqrt(mX * mX + mY * mY + mZ * mZ);
} }
double Vector::dotProduct(const Vector& v) const double Vector::dotProduct(const Vector& v) const
@ -44,7 +76,12 @@ Vector Vector::crossProduct(const Vector& v) const
Vector Vector::getNormalized() 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) 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; mX = x * mX;
mY = y * mY; mY = y * mY;
mZ = z * mZ; mZ = z * mZ;
updateLength();
}
void Vector::updateLength()
{
mLength = std::sqrt(mX * mX + mY * mY + mZ * mZ);
} }

View file

@ -1,15 +1,30 @@
#pragma once #pragma once
#include <memory>
#include <cmath>
class Vector class Vector
{ {
public: 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 x = 0, double y = 0, double z = 0);
~Vector(); ~Vector();
double dotProduct(const Vector& v) const;
Vector crossProduct(const Vector& v) const;
void reverseDirection();
double getX() const; double getX() const;
double getY() const; double getY() const;
@ -20,11 +35,21 @@ public:
Vector getNormalized() const; 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 bool operator==(const Vector& rhs) const
{ {
@ -39,9 +64,6 @@ public:
} }
private: private:
void updateLength();
double mLength{ 0 };
double mX{ 0 }; double mX{ 0 };
double mY{ 0 }; double mY{ 0 };
double mZ{ 0 }; double mZ{ 0 };

View file

@ -31,11 +31,6 @@ Point::~Point()
{ {
}; };
std::unique_ptr<Point> Point::Create(double x, double y, double z)
{
return std::make_unique<Point>(x, y, z);
}
double Point::getX() const double Point::getX() const
{ {
return mX; return mX;
@ -101,3 +96,8 @@ void Point::move(double x, double y, double z)
mY += y; mY += y;
mZ += z; mZ += z;
} }
void Point::move(const Vector& vector)
{
move(vector.getX(), vector.getY(), vector.getZ());
}

View file

@ -18,14 +18,8 @@ public:
~Point(); ~Point();
static std::unique_ptr<Point> Create(double x, double y, double z = 0);
void apply(const Transform& transform); void apply(const Transform& transform);
bool isAtOrigin() const;
void move(double x, double y, double z = 0);
double getX() const; double getX() const;
double getY() const; double getY() const;
@ -42,6 +36,17 @@ public:
Vector getDelta(const Point& point) const; 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 bool operator==(const Point& rhs) const
{ {
return (mX == rhs.mX) return (mX == rhs.mX)