Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
Phyth::Vector< 3, T > Struct Template Reference

3D vector with Quantity components More...

#include <Vector3.hpp>

Public Member Functions

constexpr Vector ()
 Construct a zero vector (0, 0, 0)
 
constexpr Vector (T x_, T y_, T z_)
 Construct a vector with specified components.
 
auto ToPolar () const
 Convert to 2D polar coordinates (z component is ignored)
 
auto ToSpherical () const
 Convert to spherical coordinates.
 
constexpr T & operator[] (const size_t i)
 Index-based access (non-const)
 
constexpr const T & operator[] (const size_t i) const
 Index-based access (const)
 
template<typename U >
constexpr auto operator+ (const Vector3< U > &other) const
 Vector addition.
 
template<typename U >
constexpr auto operator- (const Vector3< U > &other) const
 Vector subtraction.
 
constexpr auto operator- () const
 Unary negation.
 
template<typename U , typename = std::enable_if_t<!std::is_same_v<std::decay_t<U>, Vector>>>
constexpr auto operator* (U scalar) const
 Scalar multiplication (vector * scalar)
 
template<typename U >
constexpr auto operator/ (U scalar) const
 Scalar division (vector / scalar)
 
template<typename U >
constexpr Vector & operator+= (const Vector3< U > &other)
 Addition assignment.
 
template<typename U >
constexpr Vector & operator-= (const Vector3< U > &other)
 Subtraction assignment.
 
template<typename U >
constexpr Vector & operator*= (U scalar)
 Scalar multiplication assignment.
 
template<typename U >
constexpr Vector & operator/= (U scalar)
 Scalar division assignment.
 
template<typename U >
constexpr bool operator== (const Vector3< U > &other) const
 Equality comparison.
 
template<typename U >
constexpr bool operator!= (const Vector3< U > &other) const
 Inequality comparison.
 
template<typename U >
constexpr auto Dot (const Vector3< U > &other) const -> decltype(x *other.x)
 Dot product.
 
template<typename U >
constexpr auto Cross (const Vector3< U > &other) const
 Cross product.
 
auto Length () const
 Euclidean length (magnitude)
 
auto LengthSquared () const
 Squared Euclidean length.
 
Vector3< Scalar > Normalized () const
 Normalized vector (unit vector in the same direction)
 
template<typename U >
auto Project (const Vector3< U > &other) const
 Project this vector onto another vector.
 
template<typename UnitT >
Vector3< Quantity< UnitT > > As () const
 Convert all components to a different unit of the same dimension.
 

Static Public Member Functions

static constexpr Vector FromPolar (T r, const Scalar theta_rad)
 Construct a 2D polar vector (z = 0)
 
static constexpr Vector FromSpherical (T r, const Scalar theta_rad, const Scalar phi_rad)
 Construct a 3D spherical vector.
 

Public Attributes

T x
 
T y
 
T z
 

Friends

std::ostream & operator<< (std::ostream &os, const Vector &v)
 Stream insertion operator for Vector3.
 

Detailed Description

template<typename T>
struct Phyth::Vector< 3, T >

3D vector with Quantity components

Vector3 is a homogeneous 3D vector where all components share the same Quantity type. This ensures that vector operations preserve physical dimensions correctly.

Template Parameters
TComponent type (must be a Quantity instantiation)

Example: Vector3<Quantity<Meter>> position(1.0_m, 2.0_m, 3.0_m); Vector3<Quantity<MeterPerSecond>> velocity(1.0_m/s, 0.0_m/s, 0.0_m/s); auto displacement = velocity * 2.0_s; // Vector3<Quantity<Meter>>

Definition at line 28 of file Vector3.hpp.

Constructor & Destructor Documentation

◆ Vector() [1/2]

template<typename T >
constexpr Phyth::Vector< 3, T >::Vector ( )
inlineconstexpr

Construct a zero vector (0, 0, 0)

Definition at line 39 of file Vector3.hpp.

◆ Vector() [2/2]

template<typename T >
constexpr Phyth::Vector< 3, T >::Vector ( T  x_,
T  y_,
T  z_ 
)
inlineconstexpr

Construct a vector with specified components.

Parameters
x_X component
y_Y component
z_Z component

Definition at line 49 of file Vector3.hpp.

Member Function Documentation

◆ As()

template<typename T >
template<typename UnitT >
Vector3< Quantity< UnitT > > Phyth::Vector< 3, T >::As ( ) const
inline

Convert all components to a different unit of the same dimension.

Template Parameters
UnitTThe target unit type
Returns
Vector3 with each component converted to UnitT

Example: auto pos = Vector3<Quantity<Kilometer>>(1.0_km, 0_km, 0_km); auto in_meters = pos.as<Meter>(); // (1000 m, 0 m, 0 m)

Definition at line 419 of file Vector3.hpp.

◆ Cross()

template<typename T >
template<typename U >
constexpr auto Phyth::Vector< 3, T >::Cross ( const Vector3< U > &  other) const
inlineconstexpr

Cross product.

Template Parameters
UQuantity type of the other vector
Parameters
otherThe other vector
Returns
Vector3 containing the cross product

Example: auto a = Vector3<Quantity<Meter>>(1.0_m, 0_m, 0_m); auto b = Vector3<Quantity<Meter>>(0_m, 1.0_m, 0_m); auto cross = a.Cross(b); // (0 m^2, 0 m^2, 1 m^2)

Definition at line 345 of file Vector3.hpp.

◆ Dot()

template<typename T >
template<typename U >
constexpr auto Phyth::Vector< 3, T >::Dot ( const Vector3< U > &  other) const -> decltype(x * other.x)
inlineconstexpr

Dot product.

Template Parameters
UQuantity type of the other vector
Parameters
otherThe other vector
Returns
The dot product as a Quantity

Example: auto a = Vector3<Quantity<Meter>>(1.0_m, 0_m, 0_m); auto b = Vector3<Quantity<Meter>>(0_m, 2.0_m, 0_m); auto dot = a.Dot(b); // (0 m^2, 0 m^2, 0 m^2) (Meter*Meter = Area)

Definition at line 328 of file Vector3.hpp.

◆ FromPolar()

template<typename T >
static constexpr Vector Phyth::Vector< 3, T >::FromPolar ( T  r,
const Scalar  theta_rad 
)
inlinestaticconstexpr

Construct a 2D polar vector (z = 0)

Parameters
rRadial distance (must be a Quantity)
theta_radAngle from the positive X axis, in radians (dimensionless)
Returns
Vector3 with (r*cos(theta), r*sin(theta), 0)

Example: auto pos = Vector3<Quantity<Meter>>::FromPolar(5.0_m, 30.0_deg); // pos ~ (4.33_m, 2.5_m, 0_m)

Definition at line 63 of file Vector3.hpp.

◆ FromSpherical()

template<typename T >
static constexpr Vector Phyth::Vector< 3, T >::FromSpherical ( T  r,
const Scalar  theta_rad,
const Scalar  phi_rad 
)
inlinestaticconstexpr

Construct a 3D spherical vector.

Parameters
rRadial distance (must be a Quantity)
theta_radPolar angle from Z axis, in radians (dimensionless)
phi_radAzimuthal angle in XY plane from X axis, in radians
Returns
Vector3 with spherical coordinates converted to Cartesian

Example: auto pos = Vector3<Quantity<Meter>>::FromSpherical( 5.0_m, 60.0_deg, 45.0_deg );

Definition at line 84 of file Vector3.hpp.

◆ Length()

template<typename T >
auto Phyth::Vector< 3, T >::Length ( ) const
inline

Euclidean length (magnitude)

Returns
sqrt(x^2 + y^2 + z^2) as a Quantity

Definition at line 358 of file Vector3.hpp.

◆ LengthSquared()

template<typename T >
auto Phyth::Vector< 3, T >::LengthSquared ( ) const
inline

Squared Euclidean length.

Returns
x^2 + y^2 + z^2 as a Quantity

Use this instead of Length() when you only need to compare magnitudes, as it avoids the square root computation.

Definition at line 370 of file Vector3.hpp.

◆ Normalized()

template<typename T >
Vector3< Scalar > Phyth::Vector< 3, T >::Normalized ( ) const
inline

Normalized vector (unit vector in the same direction)

Returns
Vector3 with magnitude 1
Note
The component type becomes Quantity<Unit<DimDivT<DimT, DimT>>> which is dimensionless. This is intentional.

Definition at line 382 of file Vector3.hpp.

◆ operator!=()

template<typename T >
template<typename U >
constexpr bool Phyth::Vector< 3, T >::operator!= ( const Vector3< U > &  other) const
inlineconstexpr

Inequality comparison.

Template Parameters
UQuantity type of the other vector
Parameters
otherVector to compare against
Returns
true if any component differs

Definition at line 311 of file Vector3.hpp.

◆ operator*()

template<typename T >
template<typename U , typename = std::enable_if_t<!std::is_same_v<std::decay_t<U>, Vector>>>
constexpr auto Phyth::Vector< 3, T >::operator* ( U  scalar) const
inlineconstexpr

Scalar multiplication (vector * scalar)

Template Parameters
UScalar type (must not be a Vector3)
Parameters
scalarThe scalar multiplier
Returns
Vector3 with each component multiplied by scalar

Note: This is intentionally constrained to non-Vector3 types to avoid ambiguity with element-wise multiplication.

Definition at line 207 of file Vector3.hpp.

◆ operator*=()

template<typename T >
template<typename U >
constexpr Vector & Phyth::Vector< 3, T >::operator*= ( U  scalar)
inlineconstexpr

Scalar multiplication assignment.

Template Parameters
UScalar type
Parameters
scalarThe scalar multiplier
Returns
Reference to this vector

Definition at line 269 of file Vector3.hpp.

◆ operator+()

template<typename T >
template<typename U >
constexpr auto Phyth::Vector< 3, T >::operator+ ( const Vector3< U > &  other) const
inlineconstexpr

Vector addition.

Template Parameters
UQuantity type of the other vector
Parameters
otherVector to add
Returns
Vector3 with component type T + U (promoted if needed)

Definition at line 163 of file Vector3.hpp.

◆ operator+=()

template<typename T >
template<typename U >
constexpr Vector & Phyth::Vector< 3, T >::operator+= ( const Vector3< U > &  other)
inlineconstexpr

Addition assignment.

Template Parameters
UQuantity type of the other vector
Parameters
otherVector to add
Returns
Reference to this vector

Definition at line 239 of file Vector3.hpp.

◆ operator-() [1/2]

template<typename T >
constexpr auto Phyth::Vector< 3, T >::operator- ( ) const
inlineconstexpr

Unary negation.

Returns
Vector3 with all components negated

Definition at line 192 of file Vector3.hpp.

◆ operator-() [2/2]

template<typename T >
template<typename U >
constexpr auto Phyth::Vector< 3, T >::operator- ( const Vector3< U > &  other) const
inlineconstexpr

Vector subtraction.

Template Parameters
UQuantity type of the other vector
Parameters
otherVector to subtract
Returns
Vector3 with component type T - U (promoted if needed)

Definition at line 179 of file Vector3.hpp.

◆ operator-=()

template<typename T >
template<typename U >
constexpr Vector & Phyth::Vector< 3, T >::operator-= ( const Vector3< U > &  other)
inlineconstexpr

Subtraction assignment.

Template Parameters
UQuantity type of the other vector
Parameters
otherVector to subtract
Returns
Reference to this vector

Definition at line 254 of file Vector3.hpp.

◆ operator/()

template<typename T >
template<typename U >
constexpr auto Phyth::Vector< 3, T >::operator/ ( U  scalar) const
inlineconstexpr

Scalar division (vector / scalar)

Template Parameters
UScalar type
Parameters
scalarThe scalar divisor
Returns
Vector3 with each component divided by scalar

Definition at line 223 of file Vector3.hpp.

◆ operator/=()

template<typename T >
template<typename U >
constexpr Vector & Phyth::Vector< 3, T >::operator/= ( U  scalar)
inlineconstexpr

Scalar division assignment.

Template Parameters
UScalar type
Parameters
scalarThe scalar divisor
Returns
Reference to this vector

Definition at line 284 of file Vector3.hpp.

◆ operator==()

template<typename T >
template<typename U >
constexpr bool Phyth::Vector< 3, T >::operator== ( const Vector3< U > &  other) const
inlineconstexpr

Equality comparison.

Template Parameters
UQuantity type of the other vector
Parameters
otherVector to compare against
Returns
true if all components are equal

Definition at line 299 of file Vector3.hpp.

◆ operator[]() [1/2]

template<typename T >
constexpr T & Phyth::Vector< 3, T >::operator[] ( const size_t  i)
inlineconstexpr

Index-based access (non-const)

Parameters
iIndex: 0 = x, 1 = y, 2 = z
Returns
Reference to the component

Example: vec[0] = 1.0_m; // modifies x

Definition at line 133 of file Vector3.hpp.

◆ operator[]() [2/2]

template<typename T >
constexpr const T & Phyth::Vector< 3, T >::operator[] ( const size_t  i) const
inlineconstexpr

Index-based access (const)

Parameters
iIndex: 0 = x, 1 = y, 2 = z
Returns
Const reference to the component

Definition at line 147 of file Vector3.hpp.

◆ Project()

template<typename T >
template<typename U >
auto Phyth::Vector< 3, T >::Project ( const Vector3< U > &  other) const
inline

Project this vector onto another vector.

Template Parameters
UQuantity type of the target vector
Parameters
otherThe vector to project onto
Returns
The projection of this onto other

Example: auto a = Vector3<Quantity<Meter>>(1.0_m, 1.0_m, 0_m); auto b = Vector3<Quantity<Meter>>(1.0_m, 0_m, 0_m); auto proj = a.Project(b); // (1.0 m, 0 m, 0 m)

Definition at line 404 of file Vector3.hpp.

◆ ToPolar()

template<typename T >
auto Phyth::Vector< 3, T >::ToPolar ( ) const
inline

Convert to 2D polar coordinates (z component is ignored)

Returns
std::pair containing (r, theta) where:
  • r is the length in the XY plane (Quantity)
  • theta is the angle from X axis (Scalar, dimensionless)

Note: This function name might be misleading. It returns XY-plane polar coordinates, not a full 3D conversion. Consider using ToSpherical() for full 3D conversion.

Definition at line 103 of file Vector3.hpp.

◆ ToSpherical()

template<typename T >
auto Phyth::Vector< 3, T >::ToSpherical ( ) const
inline

Convert to spherical coordinates.

Returns
std::tuple containing (r, theta, phi) where:
  • r is the Euclidean length (Quantity)
  • theta is the polar angle from Z axis (Scalar, dimensionless)
  • phi is the azimuthal angle from X axis (Scalar, dimensionless)

Definition at line 117 of file Vector3.hpp.

Friends And Related Symbol Documentation

◆ operator<<

template<typename T >
std::ostream & operator<< ( std::ostream &  os,
const Vector< 3, T > &  v 
)
friend

Stream insertion operator for Vector3.

Output format: "(x, y, z)" where each component uses Quantity's stream formatting.

Example output: "(1 m, 2 m, 3 m)"

Definition at line 431 of file Vector3.hpp.

Member Data Documentation

◆ x

template<typename T >
T Phyth::Vector< 3, T >::x

X component

Definition at line 32 of file Vector3.hpp.

◆ y

template<typename T >
T Phyth::Vector< 3, T >::y

Y component

Definition at line 34 of file Vector3.hpp.

◆ z

template<typename T >
T Phyth::Vector< 3, T >::z

Z component

Definition at line 36 of file Vector3.hpp.


The documentation for this struct was generated from the following file: