Skip to content

Commit

Permalink
improve (src): reformat implementation of Firefly::Vector member fu…
Browse files Browse the repository at this point in the history
…nctions
  • Loading branch information
tbhaxor committed Jul 23, 2023
1 parent 18d2e75 commit 867535c
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 23 deletions.
11 changes: 8 additions & 3 deletions src/vector/add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ std::unique_ptr<Vector> Vector::Add(Vector const &_fvec) const {
std::unique_ptr<Vector> Vector::Add(Real const &_scalar) const {
std::unique_ptr<Vector> sum = std::make_unique<Vector>(*this);

std::transform(sum->m_vec->cbegin(), sum->m_vec->cend(), sum->m_vec->begin(), [&](Real const &_el) { return _el + _scalar; });
std::transform(sum->m_vec->cbegin(), sum->m_vec->cend(), sum->m_vec->begin(),
[&](Real const &_el) { return _el + _scalar; });

return sum;
}

std::unique_ptr<Vector> Vector::operator+(Real const &_scalar) const { return this->Add(_scalar); }
std::unique_ptr<Vector> Vector::operator+(Real const &_scalar) const {
return this->Add(_scalar);
}

std::unique_ptr<Vector> Vector::operator+(Vector const &_fvec) const { return this->Add(_fvec); }
std::unique_ptr<Vector> Vector::operator+(Vector const &_fvec) const {
return this->Add(_fvec);
}

std::unique_ptr<Vector> Vector::operator++() const { return this->Add(1); }
} // namespace Firefly
3 changes: 2 additions & 1 deletion src/vector/angle_with.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

namespace Firefly {
Real Vector::AngleWith(Vector const &_fvec) const {
auto angle_radians = std::acos(this->Dot(_fvec) / (this->Magnitude() * _fvec.Magnitude()));
auto angle_radians =
std::acos(this->Dot(_fvec) / (this->Magnitude() * _fvec.Magnitude()));
if (std::isnan(angle_radians)) {
throw std::domain_error("Can not find angle with zero vector.");
}
Expand Down
8 changes: 6 additions & 2 deletions src/vector/area.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "firefly/vector.hpp"

namespace Firefly {
Real Vector::ParallelogramArea(Vector const &_fvec) const { return this->Cross(_fvec)->Magnitude(); }
Real Vector::ParallelogramArea(Vector const &_fvec) const {
return this->Cross(_fvec)->Magnitude();
}

Real Vector::TriangleArea(Vector const &_fvec) const { return this->ParallelogramArea(_fvec) / 2; }
Real Vector::TriangleArea(Vector const &_fvec) const {
return this->ParallelogramArea(_fvec) / 2;
}
} // namespace Firefly
4 changes: 3 additions & 1 deletion src/vector/elem_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

namespace Firefly {

Real Vector::ElemSum() const { return std::reduce(m_vec->cbegin(), m_vec->cend(), 0x0, std::plus<Real>()); }
Real Vector::ElemSum() const {
return std::reduce(m_vec->cbegin(), m_vec->cend(), 0x0, std::plus<Real>());
}
} // namespace Firefly
5 changes: 3 additions & 2 deletions src/vector/indexers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ namespace Firefly {

Real Vector::At(std::size_t idx) const {
if (idx >= m_vec->size()) {
throw std::out_of_range("Index out of range. Requested index: " + std::to_string(idx) +
", Vector size: " + std::to_string(m_vec->size()));
throw std::out_of_range(
"Index out of range. Requested index: " + std::to_string(idx) +
", Vector size: " + std::to_string(m_vec->size()));
}

return m_vec->at(idx);
Expand Down
6 changes: 5 additions & 1 deletion src/vector/is_parallel.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include <cmath>

#include "firefly/vector.hpp"

namespace Firefly {
bool Vector::IsParallel(const Vector &_fvec) {
if (this->Magnitude() == 0 || _fvec.Magnitude() == 0) {
return true;
}
return this->AngleWith(_fvec) == 0;

auto angle_radians = this->AngleWith(_fvec);
return angle_radians == 0 || angle_radians == M_PI;
}
} // namespace Firefly
3 changes: 2 additions & 1 deletion src/vector/is_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Firefly {
bool Vector::IsZero() {
return std::all_of(m_vec->cbegin(), m_vec->cend(), [](Real const &_el) { return _el == 0; });
return std::all_of(m_vec->cbegin(), m_vec->cend(),
[](Real const &_el) { return _el == 0; });
}
} // namespace Firefly
4 changes: 3 additions & 1 deletion src/vector/magnitude.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
#include "firefly/vector.hpp"

namespace Firefly {
Real Vector::Magnitude() const { return static_cast<Real>(std::sqrt(this->Dot(*this))); }
Real Vector::Magnitude() const {
return static_cast<Real>(std::sqrt(this->Dot(*this)));
}
} // namespace Firefly
4 changes: 3 additions & 1 deletion src/vector/normalize.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "firefly/vector.hpp"

namespace Firefly {
std::unique_ptr<Vector> Vector::Normalize() const { return this->Scale(1 / this->Magnitude()); }
std::unique_ptr<Vector> Vector::Normalize() const {
return this->Scale(1 / this->Magnitude());
}
} // namespace Firefly
9 changes: 6 additions & 3 deletions src/vector/scale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ namespace Firefly {
std::unique_ptr<Vector> Vector::Scale(Real const &_scalar) const {
std::unique_ptr<Vector> scaled = std::make_unique<Vector>(*this);

std::transform(scaled->m_vec->cbegin(), scaled->m_vec->cend(), scaled->m_vec->begin(),
[&](Real const &_el) { return _el * _scalar; });
std::transform(scaled->m_vec->cbegin(), scaled->m_vec->cend(),
scaled->m_vec->begin(),
[&](Real const &_el) { return _el * _scalar; });

return scaled;
}

std::unique_ptr<Vector> Vector::operator*(Real const &_scalar) const { return this->Scale(_scalar); }
std::unique_ptr<Vector> Vector::operator*(Real const &_scalar) const {
return this->Scale(_scalar);
}
} // namespace Firefly
16 changes: 12 additions & 4 deletions src/vector/subtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
#include "firefly/vector.hpp"

namespace Firefly {
std::unique_ptr<Vector> Vector::Subtract(Vector const &_fvec) const { return this->Add(*_fvec.Scale(-1)); }
std::unique_ptr<Vector> Vector::Subtract(Real const &_scalar) const { return this->Add(-_scalar); }
std::unique_ptr<Vector> Vector::Subtract(Vector const &_fvec) const {
return this->Add(*_fvec.Scale(-1));
}
std::unique_ptr<Vector> Vector::Subtract(Real const &_scalar) const {
return this->Add(-_scalar);
}

std::unique_ptr<Vector> Vector::operator-(Vector const &_fvec) const { return this->Subtract(_fvec); }
std::unique_ptr<Vector> Vector::operator-(Real const &_scalar) const { return this->Subtract(_scalar); }
std::unique_ptr<Vector> Vector::operator-(Vector const &_fvec) const {
return this->Subtract(_fvec);
}
std::unique_ptr<Vector> Vector::operator-(Real const &_scalar) const {
return this->Subtract(_scalar);
}

std::unique_ptr<Vector> Vector::operator--() const { return this->Subtract(1); }
} // namespace Firefly
7 changes: 4 additions & 3 deletions src/vector/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

namespace Firefly {

Vector::Vector(std::size_t size) : m_vec{std::make_shared<VectorType>(size)} {}
Vector::Vector(std::size_t _size)
: m_vec{std::make_shared<VectorType>(_size)} {}

Vector::Vector(VectorType const &_vec)
: m_vec(std::make_shared<VectorType>(_vec)) {}

Vector::Vector(Vector const &_f)
: m_vec(std::make_shared<VectorType>(*_f.m_vec)) {}
Vector::Vector(Vector const &_fvec)
: m_vec(std::make_shared<VectorType>(*_fvec.m_vec)) {}

} // namespace Firefly

0 comments on commit 867535c

Please sign in to comment.