Skip to content

Commit 08d2ccc

Browse files
committed
Refactors Vector operations for type safety
Ensures type safety in Vector2, Vector3, and Vector4 operations by using static_cast(0) instead of relying on implicit conversions. This prevents potential issues with different numeric types. Adds from_im_vec2 and from_im_vec4 methods for creating vectors from ImVec2/ImVec4 types.
1 parent 21ec23d commit 08d2ccc

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

include/omath/vector2.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ namespace omath
142142
[[nodiscard]] Vector2 normalized() const noexcept
143143
{
144144
const Type len = length();
145-
return len > 0.f ? *this / len : *this;
145+
return len > static_cast<Type>(0) ? *this / len : *this;
146146
}
147147
#endif
148148
[[nodiscard]] constexpr Type length_sqr() const noexcept
@@ -153,8 +153,8 @@ namespace omath
153153
constexpr Vector2& abs() noexcept
154154
{
155155
// FIXME: Replace with std::abs, if it will become constexprable
156-
x = x < 0 ? -x : x;
157-
y = y < 0 ? -y : y;
156+
x = x < static_cast<Type>(0) ? -x : x;
157+
y = y < static_cast<Type>(0) ? -y : y;
158158
return *this;
159159
}
160160

@@ -202,6 +202,11 @@ namespace omath
202202
{
203203
return {static_cast<float>(this->x), static_cast<float>(this->y)};
204204
}
205+
[[nodiscard]]
206+
static Vector3<float> from_im_vec2(const ImVec2& other) noexcept
207+
{
208+
return {static_cast<Type>(other.x), static_cast<Type>(other.y)};
209+
}
205210
#endif
206211
};
207212
} // namespace omath

include/omath/vector3.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ namespace omath
151151
{
152152
const Type len = this->length();
153153

154-
return len != 0 ? *this / len : *this;
154+
return len != static_cast<Type>(0) ? *this / len : *this;
155155
}
156156

157157
[[nodiscard]] Type length_2d() const noexcept
@@ -221,7 +221,7 @@ namespace omath
221221
{
222222
const auto bottom = length() * other.length();
223223

224-
if (bottom == 0.f)
224+
if (bottom == static_cast<Type>(0))
225225
return std::unexpected(Vector3Error::IMPOSSIBLE_BETWEEN_ANGLE);
226226

227227
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::from_radians(std::acos(dot(other) / bottom));
@@ -230,7 +230,7 @@ namespace omath
230230
[[nodiscard]] bool is_perpendicular(const Vector3& other) const noexcept
231231
{
232232
if (const auto angle = angle_between(other))
233-
return angle->as_degrees() == 90.f;
233+
return angle->as_degrees() == static_cast<Type>(90);
234234

235235
return false;
236236
}

include/omath/vector4.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace omath
1818
constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w): Vector3<Type>(x, y, z), w(w)
1919
{
2020
}
21-
constexpr Vector4() noexcept : Vector3<Type>(), w(0) {};
21+
constexpr Vector4() noexcept: Vector3<Type>(), w(static_cast<Type>(0)) {};
2222

2323
[[nodiscard]]
2424
constexpr bool operator==(const Vector4& other) const noexcept
@@ -169,6 +169,12 @@ namespace omath
169169
static_cast<float>(w),
170170
};
171171
}
172+
[[nodiscard]]
173+
static Vector4<float> from_im_vec4(const ImVec4& other) noexcept
174+
{
175+
return {static_cast<Type>(other.x), static_cast<Type>(other.y), static_cast<Type>(other.z)};
176+
}
177+
}
172178
#endif
173-
};
179+
};
174180
} // namespace omath

0 commit comments

Comments
 (0)