Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe9554c

Browse files
AlBuSoft_RTX3070AlBuSoft_RTX3070
authored andcommitted
#87 lerp functions for the vectors
1 parent 3ce7762 commit fe9554c

File tree

5 files changed

+292
-0
lines changed

5 files changed

+292
-0
lines changed

Libs/ecm.math.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
</ItemDefinitionGroup>
179179
<ItemGroup>
180180
<ClInclude Include="ecm\ecm_math.hpp" />
181+
<ClInclude Include="ecm\math\ext\vector_ext.hpp" />
181182
<ClInclude Include="ecm\math\functions.hpp" />
182183
<ClInclude Include="ecm\math\functions_simd.hpp" />
183184
<ClInclude Include="ecm\math\matrix.hpp" />
@@ -188,6 +189,7 @@
188189
<ClInclude Include="ecm\math\vector4.hpp" />
189190
</ItemGroup>
190191
<ItemGroup>
192+
<None Include="ecm\math\ext\vector_ext.inl" />
191193
<None Include="ecm\math\functions.inl" />
192194
<None Include="ecm\math\functions_simd.inl" />
193195
<None Include="ecm\math\matrix4x4.inl" />

Libs/ecm/math/ext/vector_ext.hpp

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/**
2+
* \file vector2.h
3+
*
4+
* \brief This header defines a two dimensional vector and functionalities.
5+
*/
6+
7+
#pragma once
8+
#ifndef _ECM_VECTOR_EXT_H_
9+
#define _ECM_VECTOR_EXT_H_
10+
11+
#include <ecm/ecm_api.h>
12+
#include <ecm/ecm_types.hpp>
13+
#include <ecm/math/vector2.hpp>
14+
#include <ecm/math/vector3.hpp>
15+
#include <ecm/math/vector4.hpp>
16+
17+
#include <type_traits>
18+
19+
namespace ecm::math
20+
{
21+
// Lerp
22+
23+
/**
24+
* Linearly interpolates between two 2D vectors, \p x and \p y, using a
25+
* scalar factor \p t.
26+
*
27+
* This function computes the interpolation component-wise according to:
28+
* \f[
29+
* \text{Lerp}(x, y, t) = x + t \cdot (y - x)
30+
* \f]
31+
* where \p x and \p y are 2D vectors, and \p t is a scalar.
32+
*
33+
* \tparam T The type of the elements in vector \p x.
34+
* \tparam U The type of the elements in vector \p y.
35+
* \tparam W The type of the interpolation factor \p t (must be arithmetic).
36+
*
37+
* \param x The starting 2D vector.
38+
* \param y The ending 2D vector.
39+
* \param t The interpolation factor, typically in the range \[0, 1\].
40+
*
41+
* \returns A 2D vector that is the result of the linear interpolation.
42+
*
43+
* \since v1.0.0
44+
*/
45+
template<typename T, typename U, typename W, typename = std::enable_if_t<std::is_arithmetic<W>::value>>
46+
ECM_NODISCARD constexpr Vector2_Base<T> ECM_CALL Lerp(const Vector2_Base<T>& x, const Vector2_Base<U>& y, W t);
47+
48+
/**
49+
* Linearly interpolates between two 3D vectors, \p x and \p y, using a
50+
* scalar factor \p t.
51+
*
52+
* This function computes the interpolation component-wise according to:
53+
* \f[
54+
* \text{Lerp}(x, y, t) = x + t \cdot (y - x)
55+
* \f]
56+
* where \p x and \p y are 3D vectors, and \p t is a scalar.
57+
*
58+
* \tparam T The type of the elements in vector \p x.
59+
* \tparam U The type of the elements in vector \p y.
60+
* \tparam W The type of the interpolation factor \p t (must be arithmetic).
61+
*
62+
* \param x The starting 3D vector.
63+
* \param y The ending 3D vector.
64+
* \param t The interpolation factor, typically in the range \[0, 1\].
65+
*
66+
* \returns A 3D vector that is the result of the linear interpolation.
67+
*
68+
* \since v1.0.0
69+
*/
70+
template<typename T, typename U, typename W, typename = std::enable_if_t<std::is_arithmetic<W>::value>>
71+
ECM_NODISCARD constexpr Vector3_Base<T> ECM_CALL Lerp(const Vector3_Base<T>& x, const Vector3_Base<U>& y, W t);
72+
73+
/**
74+
* Linearly interpolates between two 4D vectors, \p x and \p y, using a
75+
* scalar factor \p t.
76+
*
77+
* This function computes the interpolation component-wise according to:
78+
* \f[
79+
* \text{Lerp}(x, y, t) = x + t \cdot (y - x)
80+
* \f]
81+
* where \p x and \p y are 4D vectors, and \p t is a scalar.
82+
*
83+
* \tparam T The type of the elements in vector \p x.
84+
* \tparam U The type of the elements in vector \p y.
85+
* \tparam W The type of the interpolation factor \p t (must be arithmetic).
86+
*
87+
* \param x The starting 4D vector.
88+
* \param y The ending 4D vector.
89+
* \param t The interpolation factor, typically in the range \[0, 1\].
90+
*
91+
* \returns A 4D vector that is the result of the linear interpolation.
92+
*
93+
* \since v1.0.0
94+
*/
95+
template<typename T, typename U, typename W, typename = std::enable_if_t<std::is_arithmetic<W>::value>>
96+
ECM_NODISCARD constexpr Vector4_Base<T> ECM_CALL Lerp(const Vector4_Base<T>& x, const Vector4_Base<U>& y, W t);
97+
98+
/**
99+
* Linearly interpolates between two 2D vectors, \p x and \p y, using a 2D
100+
* vector factor \p t.
101+
*
102+
* This function computes the interpolation component-wise according to:
103+
* \f[
104+
* \text{Lerp}(x, y, t) = x + t \cdot (y - x)
105+
* \f]
106+
* where \p x and \p y are 2D vectors, and \p t is another 2D vector,
107+
* allowing for separate interpolation factors per component.
108+
*
109+
* \tparam T The type of the elements in vector \p x.
110+
* \tparam U The type of the elements in vector \p y.
111+
* \tparam W The type of the elements in vector \p t.
112+
*
113+
* \param x The starting 2D vector.
114+
* \param y The ending 2D vector.
115+
* \param t The 2D interpolation factors, one for each component.
116+
*
117+
* \returns A 2D vector that is the result of the component-wise linear
118+
* interpolation.
119+
*
120+
* \since v1.0.0
121+
*/
122+
template<typename T, typename U, typename W>
123+
ECM_NODISCARD constexpr Vector2_Base<T> ECM_CALL Lerp(const Vector2_Base<T>& x, const Vector2_Base<U>& y, const Vector2_Base<W>& t);
124+
125+
/**
126+
* Linearly interpolates between two 3D vectors, \p x and \p y, using a 3D
127+
* vector factor \p t.
128+
*
129+
* This function computes the interpolation component-wise according to:
130+
* \f[
131+
* \text{Lerp}(x, y, t) = x + t \cdot (y - x)
132+
* \f]
133+
* where \p x and \p y are 3D vectors, and \p t is another 3D vector,
134+
* allowing for separate interpolation factors per component.
135+
*
136+
* \tparam T The type of the elements in vector \p x.
137+
* \tparam U The type of the elements in vector \p y.
138+
* \tparam W The type of the elements in vector \p t.
139+
*
140+
* \param x The starting 3D vector.
141+
* \param y The ending 3D vector.
142+
* \param t The 3D interpolation factors, one for each component.
143+
*
144+
* \returns A 3D vector that is the result of the component-wise linear
145+
* interpolation.
146+
*
147+
* \since v1.0.0
148+
*/
149+
template<typename T, typename U, typename W>
150+
ECM_NODISCARD constexpr Vector3_Base<T> ECM_CALL Lerp(const Vector3_Base<T>& x, const Vector3_Base<U>& y, const Vector3_Base<W>& t);
151+
152+
/**
153+
* Linearly interpolates between two 4D vectors, \p x and \p y, using a 4D
154+
* vector factor \p t.
155+
*
156+
* This function computes the interpolation component-wise according to:
157+
* \f[
158+
* \text{Lerp}(x, y, t) = x + t \cdot (y - x)
159+
* \f]
160+
* where \p x and \p y are 4D vectors, and \p t is another 4D vector,
161+
* allowing for separate interpolation factors per component.
162+
*
163+
* \tparam T The type of the elements in vector \p x.
164+
* \tparam U The type of the elements in vector \p y.
165+
* \tparam W The type of the elements in vector \p t.
166+
*
167+
* \param x The starting 4D vector.
168+
* \param y The ending 4D vector.
169+
* \param t The 4D interpolation factors, one for each component.
170+
*
171+
* \returns A 4D vector that is the result of the component-wise linear
172+
* interpolation.
173+
*
174+
* \since v1.0.0
175+
*/
176+
template<typename T, typename U, typename W>
177+
ECM_NODISCARD constexpr Vector4_Base<T> ECM_CALL Lerp(const Vector4_Base<T>& x, const Vector4_Base<U>& y, const Vector4_Base<W>& t);
178+
} // namespace ecm::math
179+
180+
#include "vector_ext.inl"
181+
182+
#endif // !_ECM_VECTOR_EXT_H_

Libs/ecm/math/ext/vector_ext.inl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#pragma once
2+
3+
#include "vector_ext.hpp"
4+
5+
namespace ecm::math
6+
{
7+
// Lerp
8+
9+
template<typename T, typename U, typename W, typename>
10+
constexpr Vector2_Base<T> Lerp(const Vector2_Base<T>& x, const Vector2_Base<U>& y, W t)
11+
{
12+
const auto dx{ y.x - x.x };
13+
const auto dy{ y.y - x.y };
14+
return Vector2_Base<T>(
15+
static_cast<T>(x.x + dx * t),
16+
static_cast<T>(x.y + dy * t));
17+
}
18+
19+
template<typename T, typename U, typename W, typename>
20+
constexpr Vector3_Base<T> Lerp(const Vector3_Base<T>& x, const Vector3_Base<U>& y, W t)
21+
{
22+
const auto dx{ y.x - x.x };
23+
const auto dy{ y.y - x.y };
24+
const auto dz{ y.z - x.z };
25+
return Vector3_Base<T>(
26+
static_cast<T>(x.x + dx * t),
27+
static_cast<T>(x.y + dy * t),
28+
static_cast<T>(x.z + dz * t));
29+
}
30+
31+
template<typename T, typename U, typename W, typename>
32+
constexpr Vector4_Base<T> Lerp(const Vector4_Base<T>& x, const Vector4_Base<U>& y, W t)
33+
{
34+
const auto dx{ y.x - x.x };
35+
const auto dy{ y.y - x.y };
36+
const auto dz{ y.z - x.z };
37+
const auto dw{ y.w - x.w };
38+
return Vector4_Base<T>(
39+
static_cast<T>(x.x + dx * t),
40+
static_cast<T>(x.y + dy * t),
41+
static_cast<T>(x.z + dz * t),
42+
static_cast<T>(x.w + dw * t));
43+
}
44+
45+
template<typename T, typename U, typename W>
46+
constexpr Vector2_Base<T> Lerp(const Vector2_Base<T>& x, const Vector2_Base<U>& y, const Vector2_Base<W>& t)
47+
{
48+
const auto dx{ y.x - x.x };
49+
const auto dy{ y.y - x.y };
50+
return Vector2_Base<T>(
51+
static_cast<T>(x.x + dx * t.x),
52+
static_cast<T>(x.y + dy * t.y));
53+
}
54+
55+
template<typename T, typename U, typename W>
56+
constexpr Vector3_Base<T> Lerp(const Vector3_Base<T>& x, const Vector3_Base<U>& y, const Vector3_Base<W>& t)
57+
{
58+
const auto dx{ y.x - x.x };
59+
const auto dy{ y.y - x.y };
60+
const auto dz{ y.z - x.z };
61+
return Vector3_Base<T>(
62+
static_cast<T>(x.x + dx * t.x),
63+
static_cast<T>(x.y + dy * t.y),
64+
static_cast<T>(x.z + dz * t.z));
65+
}
66+
67+
template<typename T, typename U, typename W>
68+
constexpr Vector4_Base<T> Lerp(const Vector4_Base<T>& x, const Vector4_Base<U>& y, const Vector4_Base<W>& t)
69+
{
70+
const auto dx{ y.x - x.x };
71+
const auto dy{ y.y - x.y };
72+
const auto dz{ y.z - x.z };
73+
const auto dw{ y.w - x.w };
74+
return Vector4_Base<T>(
75+
static_cast<T>(x.x + dx * t.x),
76+
static_cast<T>(x.y + dy * t.y),
77+
static_cast<T>(x.z + dz * t.z),
78+
static_cast<T>(x.w + dw * t.w));
79+
}
80+
} // namespace ecm::math

Libs/ecm/math/vector.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "vector3.hpp"
1313
#include "vector4.hpp"
1414

15+
#include "ext/vector_ext.hpp"
16+
1517
namespace ecm::math
1618
{
1719
// Vector2 definitions

TestApps/Math/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ void test_types()
5757
test_type(mat1, mat2);
5858

5959
ecm::math::Rotate(mat1, 4, ecm::math::Vector3(1, 1, 0));
60+
61+
{ // Vector2 lerp
62+
auto test1 = ecm::math::Vector2(238.f, 2398.f);
63+
auto test2 = ecm::math::Vector2i(328, 908);
64+
ecm::math::Lerp(test1, test2, 5);
65+
ecm::math::Lerp(test1, test2, 24.f);
66+
ecm::math::Lerp(test1, test2, test1);
67+
ecm::math::Lerp(test1, test2, test2);
68+
}
69+
{ // Vector3 lerp
70+
auto test1 = ecm::math::Vector3(238.f, 2398.f, 278.f);
71+
auto test2 = ecm::math::Vector3i(328, 908, 387);
72+
ecm::math::Lerp(test1, test2, 5);
73+
ecm::math::Lerp(test1, test2, 24.f);
74+
ecm::math::Lerp(test1, test2, test1);
75+
ecm::math::Lerp(test1, test2, test2);
76+
}
77+
{ // Vector4 lerp
78+
auto test1 = ecm::math::Vector4(238.f, 2398.f, 278.f, 78.f);
79+
auto test2 = ecm::math::Vector4i(328, 908, 387, 84);
80+
ecm::math::Lerp(test1, test2, 5);
81+
ecm::math::Lerp(test1, test2, 24.f);
82+
ecm::math::Lerp(test1, test2, test1);
83+
ecm::math::Lerp(test1, test2, test2);
84+
}
6085
}
6186

6287
void test_sin()
@@ -140,6 +165,7 @@ void test_basics()
140165
ecm::math::Pow(164.f, 2);
141166
ecm::math::Sqrt(164.f);
142167
ecm::math::Trunc(164.f);
168+
ecm::math::Lerp(421.f, 7603.f, 1.f);
143169

144170
ecm::math::Sin(12.f);
145171
ecm::math::Asin(12.f);

0 commit comments

Comments
 (0)