Firefly 3.1.0
Standalone library for vector calculations
Loading...
Searching...
No Matches
utilities.hpp
Go to the documentation of this file.
1#include "firefly/vector.hpp"
2
4
24template <vector_type T, vector_type U, std::size_t Length>
25[[nodiscard]] constexpr auto angle_between(firefly::vector<T, Length> const &v1, firefly::vector<U, Length> const &v2,
26 double delta = 1e-6) {
27 static_assert(std::is_arithmetic_v<T> && std::is_arithmetic_v<U>, "Only arithmetic types are allowed.");
28 if (v1.norm() == 0 || v2.norm() == 0) {
29 return M_PI_2;
30 }
31
32 auto rad = std::acos(std::clamp((v1.to_normalized() * v2.to_normalized()) / 1, -1.0, 1.0));
33 return rad < delta ? 0.0 : rad;
34}
35
53template <vector_type T, vector_type U, std::size_t Length>
55 static_assert(std::is_arithmetic_v<T> && std::is_arithmetic_v<U>, "Only arithmetic types are allowed.");
56 return v1.to_normalized() == -v2.to_normalized();
57}
58
76template <vector_type T, vector_type U, std::size_t Length>
78 static_assert(std::is_arithmetic_v<T> && std::is_arithmetic_v<U>, "Only arithmetic types are allowed.");
79 return (v1.to_normalized() == v2.to_normalized()) || are_anti_parallel(v1, v2);
80}
81
100template <vector_type T, vector_type U, std::size_t Length>
101bool are_orthogonal(firefly::vector<T, Length> const &v1, firefly::vector<U, Length> const &v2, double delta = 1e-6) {
102 static_assert(std::is_arithmetic_v<T> && std::is_arithmetic_v<U>, "Only arithmetic types are allowed.");
103 return std::fabs(angle_between(v1, v2) - M_PI_2) < delta;
104}
105
120template <vector_type T, vector_type U, std::size_t Length>
121[[nodiscard]] constexpr auto area_parallelogram(firefly::vector<T, Length> const &v1,
122 firefly::vector<U, Length> const &v2) {
123 static_assert(std::is_arithmetic_v<T> && std::is_arithmetic_v<U>, "Only arithmetic types are allowed.");
124 return v1.cross(v2).norm();
125}
126
141template <vector_type T, vector_type U, std::size_t Length>
142[[nodiscard]] constexpr auto area_triangle(firefly::vector<T, Length> const &v1, firefly::vector<U, Length> const &v2) {
143 static_assert(std::is_arithmetic_v<T> && std::is_arithmetic_v<U>, "Only arithmetic types are allowed.");
144 return area_parallelogram(v1, v2) / 2;
145}
146
159template <vector_type T, vector_type U, std::size_t Length>
160[[nodiscard]] constexpr auto projection(firefly::vector<T, Length> const &source_vector,
161 firefly::vector<U, Length> const &target_vector) {
162 return target_vector * ((source_vector * target_vector) / (target_vector * target_vector));
163}
164
177template <vector_type T, vector_type U, std::size_t Length>
178[[nodiscard]] constexpr auto rejection(firefly::vector<T, Length> const &source_vector,
179 firefly::vector<U, Length> const &target_vector) {
180 return source_vector - projection(source_vector, target_vector);
181}
182
195template <vector_type T, vector_type U, std::size_t Length>
196[[nodiscard]] constexpr auto distance(firefly::vector<T, Length> const &vector_a,
197 firefly::vector<U, Length> const &vector_b) {
198 return (vector_a - vector_b).norm();
199}
200
213template <vector_type T, vector_type U, std::size_t Length>
214[[nodiscard]] constexpr auto reflection(firefly::vector<T, Length> const &source_vector,
215 firefly::vector<U, Length> const &target_vector) {
216 return projection(source_vector, target_vector) * 2 - source_vector;
217}
218
229template <vector_type T>
230[[nodiscard]] constexpr auto rotate_2d(firefly::vector<T, 2> const &vector, double angle_rad) {
231 T x = vector[0] * std::cos(angle_rad) - vector[1] * std::sin(angle_rad);
232 T y = vector[0] * std::sin(angle_rad) + vector[1] * std::cos(angle_rad);
233 return firefly::vector<T, 2>{x, y};
234}
235
249template <vector_type T, vector_type U, std::size_t Length>
250[[nodiscard]] constexpr auto scalar_projection(firefly::vector<T, Length> const &source_vector,
251 firefly::vector<U, Length> const &target_vector) {
252 return source_vector.dot(target_vector) / target_vector.norm();
253}
254
268template <vector_type T, vector_type U, std::size_t Length>
269[[nodiscard]] constexpr auto lerp(firefly::vector<T, Length> const &vector_a,
270 firefly::vector<U, Length> const &vector_b, double t) {
271 return vector_a * (1 - t) + vector_b * t;
272}
273
274} // namespace firefly::utilities::vector
Represents a mathematical vector in n-dimensional space.
Definition vector.hpp:148
constexpr auto norm() const
Computes the Euclidean magnitude (Length) of the vector.
Definition vector.hpp:630
constexpr auto dot(vector< U, Length > const &other) const
Calculates the dot product of two vectors.
Definition vector.hpp:447
constexpr auto to_normalized() const
Normalizes the vector.
Definition vector.hpp:649
constexpr auto cross(vector< U, Length > const &other) const
Calculates the cross product of two 3D vectors.
Definition vector.hpp:465
Definition utilities.hpp:3
constexpr auto scalar_projection(firefly::vector< T, Length > const &source_vector, firefly::vector< U, Length > const &target_vector)
Computes the scalar projection of a source vector onto a target vector.
Definition utilities.hpp:250
constexpr auto angle_between(firefly::vector< T, Length > const &v1, firefly::vector< U, Length > const &v2, double delta=1e-6)
Calculates the angle between two vectors in radians.
Definition utilities.hpp:25
constexpr auto rotate_2d(firefly::vector< T, 2 > const &vector, double angle_rad)
Rotates a 2D vector by a given angle (in radians).
Definition utilities.hpp:230
constexpr auto reflection(firefly::vector< T, Length > const &source_vector, firefly::vector< U, Length > const &target_vector)
Reflects a source vector across a target vector.
Definition utilities.hpp:214
constexpr auto projection(firefly::vector< T, Length > const &source_vector, firefly::vector< U, Length > const &target_vector)
Projects a source vector onto a target vector.
Definition utilities.hpp:160
bool are_parallel(firefly::vector< T, Length > const &v1, firefly::vector< U, Length > const &v2)
Checks if two vectors are parallel.
Definition utilities.hpp:77
constexpr auto distance(firefly::vector< T, Length > const &vector_a, firefly::vector< U, Length > const &vector_b)
Computes the Euclidean distance between two vectors.
Definition utilities.hpp:196
bool are_orthogonal(firefly::vector< T, Length > const &v1, firefly::vector< U, Length > const &v2, double delta=1e-6)
Checks if two- vectors are orthogonal.
Definition utilities.hpp:101
constexpr auto rejection(firefly::vector< T, Length > const &source_vector, firefly::vector< U, Length > const &target_vector)
Rejects a source vector from a target vector.
Definition utilities.hpp:178
constexpr auto area_triangle(firefly::vector< T, Length > const &v1, firefly::vector< U, Length > const &v2)
Computes the area of the triangle formed by two vectors.
Definition utilities.hpp:142
constexpr auto area_parallelogram(firefly::vector< T, Length > const &v1, firefly::vector< U, Length > const &v2)
Computes the area of the parallelogram formed by two vectors.
Definition utilities.hpp:121
constexpr auto lerp(firefly::vector< T, Length > const &vector_a, firefly::vector< U, Length > const &vector_b, double t)
Performs linear interpolation (Lerp) between two vectors.
Definition utilities.hpp:269
bool are_anti_parallel(firefly::vector< T, Length > const &v1, firefly::vector< U, Length > const &v2)
Checks if two vectors are anti-parallel.
Definition utilities.hpp:54