feat(cpp-object-oriented-modular-refactoring): step 8 - element-geometry-vector3

This commit is contained in:
KOKO\Mimi
2026-08-16 07:27:51 +09:00
parent cbad5c3592
commit a9ff75b3fa
9 changed files with 320 additions and 233 deletions
+26 -1
View File
@@ -21,6 +21,10 @@ class Vector3 {
constexpr Vector3(double x, double y, double z) noexcept
: components_{{x, y, z}} {}
/// @brief Copies components from an existing array-backed carrier.
explicit constexpr Vector3(const std::array<double, 3>& components) noexcept
: components_{components} {}
/// @brief Returns the first component.
constexpr double X() const noexcept { return components_[0]; }
@@ -36,6 +40,11 @@ class Vector3 {
return components_[index];
}
/// @brief Returns the immutable array-backed component carrier.
constexpr const std::array<double, 3>& Components() const noexcept {
return components_;
}
/// @brief Adds corresponding vector components.
constexpr Vector3 operator+(const Vector3& rhs) const noexcept {
return Vector3{X() + rhs.X(), Y() + rhs.Y(), Z() + rhs.Z()};
@@ -51,6 +60,22 @@ class Vector3 {
return Vector3{X() * scalar, Y() * scalar, Z() * scalar};
}
/// @brief Divides every component by a scalar.
constexpr Vector3 operator/(double scalar) const noexcept {
return Vector3{X() / scalar, Y() / scalar, Z() / scalar};
}
/// @brief Multiplies every component with the scalar as the left operand.
friend constexpr Vector3 operator*(double scalar,
const Vector3& rhs) noexcept {
return Vector3{scalar * rhs.X(), scalar * rhs.Y(), scalar * rhs.Z()};
}
/// @brief Compares every component exactly.
constexpr bool operator==(const Vector3& rhs) const noexcept {
return X() == rhs.X() && Y() == rhs.Y() && Z() == rhs.Z();
}
/// @brief Computes the Euclidean dot product with rhs.
double Dot(const Vector3& rhs) const noexcept {
return X() * rhs.X() + Y() * rhs.Y() + Z() * rhs.Z();
@@ -63,7 +88,7 @@ class Vector3 {
}
/// @brief Computes the Euclidean norm.
double Norm() const noexcept { return std::sqrt(Dot(*this)); }
double Norm() const noexcept { return std::hypot(X(), Y(), Z()); }
/// @brief Returns a unit vector when the norm is usable.
/// @return Empty when the norm is exactly zero or nonfinite.