Skip to content

Commit

Permalink
[共通] ベクトルの正規化仕様変更 #1237 #1152
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Jun 22, 2024
1 parent ec5c603 commit 4b8e927
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Siv3D/include/Siv3D/Vector2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ namespace s3d
/// @return *this
Vector2D& normalize() noexcept;

/// @brief 正規化した(大きさを 1 にした)ベクトルを返します。ゼロベクトルの場合は valueIfZero を返します。
/// @param valueIfZero ゼロベクトルの場合に返すベクトル
/// @return 正規化した(大きさを 1 にした)ベクトル、または valueIfZero
[[nodiscard]]
Vector2D normalized_or(Vector2D valueIfZero) const noexcept;

[[nodiscard]]
Vector2D rotated(value_type angle) const noexcept;

Expand Down
6 changes: 6 additions & 0 deletions Siv3D/include/Siv3D/Vector3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ namespace s3d
/// @return *this
Vector3D& normalize() noexcept;

/// @brief 正規化した(大きさを 1 にした)ベクトルを返します。ゼロベクトルの場合は valueIfZero を返します。
/// @param valueIfZero ゼロベクトルの場合に返すベクトル
/// @return 正規化した(大きさを 1 にした)ベクトル、または valueIfZero
[[nodiscard]]
Vector3D normalized_or(Vector3D valueIfZero) const noexcept;

[[nodiscard]]
constexpr Vector3D getMidpoint(Vector3D other) const noexcept;

Expand Down
6 changes: 6 additions & 0 deletions Siv3D/include/Siv3D/Vector4D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ namespace s3d

Vector4D& normalize() noexcept;

/// @brief 正規化した(大きさを 1 にした)ベクトルを返します。ゼロベクトルの場合は valueIfZero を返します。
/// @param valueIfZero ゼロベクトルの場合に返すベクトル
/// @return 正規化した(大きさを 1 にした)ベクトル、または valueIfZero
[[nodiscard]]
Vector4D normalized_or(Vector4D valueIfZero) const noexcept;

[[nodiscard]]
constexpr Vector4D getMidpoint(Vector4D other) const noexcept;

Expand Down
39 changes: 37 additions & 2 deletions Siv3D/include/Siv3D/detail/Vector2D.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,48 @@ namespace s3d
template <class Type>
inline Vector2D<Type> Vector2D<Type>::normalized() const noexcept
{
return (*this * invLength());
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
return *this;
}

const value_type invLen = (static_cast<value_type>(1.0) / std::sqrt(lenSq));

return{ (x * invLen), (y * invLen) };
}

template <class Type>
inline Vector2D<Type>& Vector2D<Type>::normalize() noexcept
{
return (*this *= invLength());
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
x = y = 0;
}
else
{
const value_type invLen = (static_cast<value_type>(1.0) / std::sqrt(lenSq));
x *= invLen;
y *= invLen;
}

return *this;
}

template <class Type>
inline Vector2D<Type> Vector2D<Type>::normalized_or(const Vector2D<Type> valueIfZero) const noexcept
{
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
return valueIfZero;
}

return (*this * (static_cast<value_type>(1.0) / std::sqrt(lenSq)));
}

template <class Type>
Expand Down
40 changes: 38 additions & 2 deletions Siv3D/include/Siv3D/detail/Vector3D.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,49 @@ namespace s3d
template <class Type>
inline Vector3D<Type> Vector3D<Type>::normalized() const noexcept
{
return (*this * invLength());
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
return *this;
}

const value_type invLen = (static_cast<value_type>(1.0) / std::sqrt(lenSq));

return{ (x * invLen), (y * invLen), (z * invLen) };
}

template <class Type>
inline Vector3D<Type>& Vector3D<Type>::normalize() noexcept
{
return (*this *= invLength());
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
x = y = z = 0;
}
else
{
const value_type invLen = (static_cast<value_type>(1.0) / std::sqrt(lenSq));
x *= invLen;
y *= invLen;
z *= invLen;
}

return *this;
}

template <class Type>
inline Vector3D<Type> Vector3D<Type>::normalized_or(const Vector3D<Type> valueIfZero) const noexcept
{
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
return valueIfZero;
}

return (*this * (static_cast<value_type>(1.0) / std::sqrt(lenSq)));
}

template <class Type>
Expand Down
41 changes: 39 additions & 2 deletions Siv3D/include/Siv3D/detail/Vector4D.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,50 @@ namespace s3d
template <class Type>
inline Vector4D<Type> Vector4D<Type>::normalized() const noexcept
{
return (*this * invLength());
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
return *this;
}

const value_type invLen = (static_cast<value_type>(1.0) / std::sqrt(lenSq));

return{ (x * invLen), (y * invLen), (z * invLen), (w * invLen) };
}

template <class Type>
inline Vector4D<Type>& Vector4D<Type>::normalize() noexcept
{
return (*this *= invLength());
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
x = y = z = w = 0;
}
else
{
const value_type invLen = (static_cast<value_type>(1.0) / std::sqrt(lenSq));
x *= invLen;
y *= invLen;
z *= invLen;
w *= invLen;
}

return *this;
}

template <class Type>
inline Vector4D<Type> Vector4D<Type>::normalized_or(const Vector4D<Type> valueIfZero) const noexcept
{
const value_type lenSq = lengthSq();

if (lenSq == 0)
{
return valueIfZero;
}

return (*this * (static_cast<value_type>(1.0) / std::sqrt(lenSq)));
}

template <class Type>
Expand Down

0 comments on commit 4b8e927

Please sign in to comment.