-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mr::Quat implementation #19
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
84c9d86
Initial mr::Quaternion implementation
cone-forest 895da13
Fix PR comments
cone-forest 2e608e2
Add getters for mr::Quat
cone-forest 253a031
Add tests
cone-forest 288c921
Add default initializer for mr::Radians and mr::Degrees
cone-forest 0fad8cc
Merge branch 'master' into feature/quat
cone-forest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#ifndef __quat_hpp_ | ||
#define __quat_hpp_ | ||
|
||
#include "def.hpp" | ||
#include "mr-math/operators.hpp" | ||
#include "vec.hpp" | ||
#include "matr.hpp" | ||
#include "rot.hpp" | ||
|
||
namespace mr { | ||
template <ArithmeticT T> | ||
struct Quat { | ||
private: | ||
Radians<T> _angle {}; | ||
Vec3<T> _vec {}; | ||
|
||
public: | ||
constexpr Quat() noexcept = default; | ||
constexpr Quat(Vec4<T> v) noexcept : _angle(v.x()), _vec(v.y(), v.z(), v.w()) {} | ||
constexpr Quat(Radians<T> a, Vec3<T> v) noexcept : _angle(a), _vec(v) {} | ||
constexpr Quat(Radians<T> a, T x, T y, T z) noexcept : _angle(a), _vec(x, y, z) {} | ||
|
||
// getters | ||
[[nodiscard]] constexpr Vec3<T> vec() const noexcept { return _vec; } | ||
[[nodiscard]] constexpr T x() const noexcept { return _vec.x(); } | ||
[[nodiscard]] constexpr T y() const noexcept { return _vec.y(); } | ||
[[nodiscard]] constexpr T z() const noexcept { return _vec.z(); } | ||
[[nodiscard]] constexpr T w() const noexcept { return _angle._data; } | ||
|
||
explicit constexpr operator Vec4<T>() const noexcept { | ||
return Vec4<T>{_angle._data, _vec.x(), _vec.y(), _vec.z()}; | ||
} | ||
|
||
// normalize methods | ||
constexpr Quat & normalize() noexcept { | ||
auto len = _vec.length2() + w() * w(); | ||
if (len <= mr::Vec3<T>::_epsilon) [[unlikely]] return *this; | ||
_vec /= std::sqrt(len); | ||
_angle /= std::sqrt(len); | ||
return *this; | ||
}; | ||
|
||
constexpr std::optional<Quat> normalized() const noexcept { | ||
auto len = _vec.length2() + w() * w(); | ||
if (len <= mr::Vec3<T>::_epsilon) [[unlikely]] return std::nullopt; | ||
auto res = *this; | ||
res._vec /= sqrt(len); | ||
res._angle /= sqrt(len); | ||
return res; | ||
}; | ||
|
||
friend constexpr Quat | ||
operator+(const Quat &lhs, const Quat &rhs) noexcept { | ||
return Quat{mr::Radiansf(lhs.w() + rhs.w()), lhs.vec() + rhs.vec()}; | ||
} | ||
|
||
friend constexpr Quat | ||
operator-(const Quat &lhs, const Quat &rhs) noexcept { | ||
return Quat{mr::Radiansf(lhs.w() - rhs.w()), lhs.vec() - rhs.vec()}; | ||
} | ||
|
||
friend constexpr Quat & | ||
operator+=(Quat &lhs, const Quat &rhs) noexcept { | ||
lhs = lhs + rhs; | ||
return lhs; | ||
} | ||
|
||
friend constexpr Quat & | ||
operator-=(Quat &lhs, const Quat &rhs) noexcept { | ||
lhs = lhs - rhs; | ||
return lhs; | ||
} | ||
|
||
friend constexpr Quat operator*(const Quat &lhs, const Quat &rhs) noexcept { | ||
return { | ||
mr::Radiansf(lhs.w() * rhs.w() - lhs.vec().dot(rhs.vec())), | ||
lhs.w() * rhs.vec() + rhs.w() * lhs.vec() + lhs.vec() % rhs.vec() | ||
}; | ||
} | ||
friend constexpr Quat & operator*=(Quat &lhs, const Quat &rhs) noexcept { | ||
lhs = lhs * rhs; | ||
return lhs; | ||
} | ||
|
||
friend constexpr Vec<T, 3> operator*(const Vec<T, 3> &lhs, const Quat &rhs) noexcept { | ||
auto vq = rhs.vec() * std::cos(rhs.w() / 2); | ||
auto t = vq % lhs; | ||
auto u = std::sin(rhs.w() / 2) * t + vq % t; | ||
|
||
return {lhs + u + u}; | ||
} | ||
template <std::size_t N> | ||
friend constexpr Vec<T, N> & operator*=(Vec<T, N> &lhs, const Quat &rhs) noexcept { | ||
lhs = lhs * rhs; | ||
return lhs; | ||
} | ||
}; | ||
} | ||
|
||
#endif // __quat_hpp_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can return
Radians
fromw()
(and call itangle()
then), so such casts won't be necessary. That is also better, since user will be sure they dealing with radiansThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't you have to cast dot product to Radians or lhs.w() to T in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally,
Radians
would support arithmetic operations with primitive types. Since the conversion fromDegrees
is explicit (I hope) that wouldn't lead to an ambiguityThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually,
Radians
support + or - withRadians
and * or / with numbers (which makes sense). Then yep, we need to add the cast to the dot product