API
impl::expression_base
defines methods callable on all vector expressionsimpl::expression
extendsimpl::expression_base
by adding methods callable on vector expressions of specific sizesimpl::operation
defines methods callable on all vector operationsimpl::value
defines methods callable on all vector values
Vector expressions
This class defines methods callable on any vector expression.
-
template<class Child>
struct impl::expression_base Provides all size-agnostic in-class functionality for vector expressions.
- param Child
The child vector expression type, for use in CRTP
Subclassed by impl::expression< operation< Op_fn, Operands… > >, impl::expression< value< Scalar, Size > >, impl::expression< Child, Child_size >, impl::expression< Child, 2 >, impl::expression< Child, 3 >
Public Functions
-
inline constexpr scalar_t at(const size_t index) const
Gets the component at specified index.
-
inline constexpr bool nonzero() const noexcept
Determines if at least one component is non-zero.
-
template<class T, traits::require<std::is_arithmetic_v<T>> = 1>
inline constexpr bool contains(const T value) const noexcept Determines if at least one component is equal to a scalar value.
-
inline constexpr scalar_t sum() const noexcept
Sums all components.
-
inline constexpr scalar_t product() const noexcept
Multiplies all components.
-
template<class Expr, traits::require<traits::is_same_size_v<Expr, Child>> = 1>
inline constexpr scalar_t dot(const Expr &expr) const noexcept Calculates the dot product with another vector expression.
-
inline constexpr scalar_t length2() const noexcept
Calculates the Euclidian length squared.
-
inline double length() const
Calculates the Euclidian length.
Analagous to writing
std::sqrt(vector.length2())
-
template<class Expr, traits::require<traits::is_same_size_v<Expr, Child>> = 1>
inline constexpr scalar_t distance2(const Expr &expr) const noexcept Calculates the Euclidian distance to another vector expression squared.
-
template<class Expr, traits::require<traits::is_same_size_v<Expr, Child>> = 1>
inline constexpr double distance(const Expr &expr) const Calculates the Euclidian distance to another vector expression.
Analagous to writing
std::sqrt(vector.distance2())
-
inline value<double, size> normalize() const
Calculates the normalized vector.
Analagous to writing
vector / vector.length()
-
inline value<double, size> set_length(const double length) const noexcept
Sets the Euclidian length.
Analogous to writing
vector.normalize() * length
-
template<class Expr, traits::require<traits::is_same_size_v<Expr, Child>> = 1>
inline double delta_angle(const Expr &expr) const Calculates the delta angle to another vector expression.
-
template<class Fn, traits::require<std::is_invocable_v<Fn, scalar_t>> = 1>
inline constexpr _DD_OPERATION_T apply(const Fn &fn) const noexcept Creates an operation to apply a function to all components.
-
inline constexpr _DD_OPERATION_T abs() const noexcept
Creates an operation to take the absolute value of each component.
-
inline constexpr _DD_OPERATION_T round() const noexcept
Creates an operation to round each component.
-
inline constexpr _DD_OPERATION_T floor() const noexcept
Creates an operation to floor each component.
-
inline constexpr _DD_OPERATION_T ceil() const noexcept
Creates an operation to ceil each component.
-
template<class Scalar>
inline constexpr _DD_OPERATION_T scalar_cast() const noexcept Creates an operation to cast each component.
-
inline std::string to_string(const std::string &name = "") const noexcept
Serializes the vector to a string.
- Parameters
name – Can be provided to differentiate between multiple vectors
This class defines methods callable vector expression of specific sizes.
- group Expressions
Provides all in-class functionality to vector expressions.
- param Child
The child vector expression type, for use in CRTP
-
template<class Child>
struct impl::expression<Child, 2> : public impl::expression_base<Child> - #include <dandy.h>
Adds functionality for 2D vector expressions.
Public Functions
-
inline double angle() const noexcept
Calculates the angle represented by the components.
Public Static Functions
-
static inline vector_t from_angle(const double angle) noexcept
Constructs the vector value representation of an angle.
-
inline double angle() const noexcept
-
template<class Child>
struct impl::expression<Child, 3> : public impl::expression_base<Child> - #include <dandy.h>
Adds functionality for 3D vector expressions.
Vector operations
This class defines the methods and properties available to all vector operations.
-
template<class Op_fn, class ...Operands>
struct impl::operation : public impl::expression<operation<Op_fn, Operands...>> Acts as an intermediary type for operations involving expressions.
- param Op_fn
The function type to apply to the operands
- param Operands
Types of the operands
Public Types
-
using scalar_t = typename base::scalar_t
The scalar type of the value.
-
using vector_t = typename base::vector_t
The vector result type of the operation.
Public Functions
Public Static Attributes
-
static constexpr size_t size = base::size
The vector size of the value.
Vector values
These classes define the methods and properties available to all vector values.
Note
It is not intended for the user to interface with vector values directly, but rather through one of the defined aliases
-
template<class Scalar, size_t Size>
struct impl::value : public impl::expression<value<Scalar, Size>>, public impl::value_data<Scalar, Size> A vector expression containing a single vector value.
- param Scalar
The scalar type of the vector
- param Size
The size of the vector
Public Types
-
using scalar_t = typename base::scalar_t
The scalar type of the value.
Public Functions
-
constexpr value() = default
Default constructs the vector value.
All components will be initialized to 0
-
template<class ...Scalars, traits::require<sizeof...(Scalars) == size && std::conjunction_v<std::is_convertible<Scalars, scalar_t>...>> = 1>
inline constexpr value(const Scalars&... scalars) noexcept Constructs a vector value from individual component values.
Expects as many arguments as the vector size and that each argument is convertible to the vector scalar type
-
inline explicit constexpr value(const scalar_t scalar) noexcept
Constructs a vector value from a single repeated value.
All components will be initialized to
scalar
-
template<class Other, traits::require<traits::is_same_size_v<value, Other>> = 1>
inline constexpr value(const Other &other) Copies component values from a different vector expression of the same size.
-
template<class Other, traits::require<traits::has_converter_v<value, Other>> = 1>
inline constexpr value(const Other &other) Create value from foreign type through converter.
Other
must have a converter specialization defined
-
template<class Expr, traits::require<traits::is_same_size_v<value, Expr>> = 1>
inline constexpr value &operator=(const Expr &expr) Copy assignment operator.
-
inline constexpr scalar_t operator[](const size_t index) const noexcept
Gets the component value at an index.
- group ValueData
Defines the vector value data.
The data is stored as a
Scalar data[Size]
. There are also specializations defined which add named components if a compatible compiler is used-
template<class Scalar, size_t Size>
struct value_data - #include <dandy.h>
Default case: only contains a
Scalar data[Size]
array.Subclassed by impl::value< Scalar, Size >
-
template<class Scalar>
struct value_data<Scalar, 2> - #include <dandy.h>
Specialization for 2D vectors: adds component names
x
,y
-
template<class Scalar, size_t Size>
STL integration
-
template<class Expr, dd::traits::require<dd::traits::is_expression_v<Expr>> = 1>
std::ostream &std::operator<<(std::ostream &stream, const Expr &e) Serializes vector expression to a
std::ostream
Allows for e.g.
std::cout << vector
-
template<class Scalar, size_t Size>
struct hash<dd::vector<Scalar, Size>> Hash specialization for use in
std::unordered_*
containers.
- group Iterators
These allow the use of vector values in STL algorithms and ranged-for loops.