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
Rotation.cpp
Bounds.cpp
Transform.cpp
grid/AbstractGrid.cpp
math/Linalg.cpp

View file

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

View file

@ -1,17 +1,49 @@
#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)
: 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);
}

View file

@ -1,15 +1,30 @@
#pragma once
#include <memory>
#include <cmath>
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 };
};
};

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
{
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());
}

View file

@ -18,14 +18,8 @@ public:
~Point();
static std::unique_ptr<Point> 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)

View file

@ -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);
};