-
Notifications
You must be signed in to change notification settings - Fork 0
MET-30: Implement Geometry Primitives #12
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
Changes from all commits
f1fdff1
f143eb9
827b5b1
e5876f6
b6de85f
17ff36c
8fb9bcd
19f9d12
0427c87
49ac9c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,39 @@ | ||
| #include <cmath> | ||
|
|
||
| #include "geometry.cuh" | ||
|
|
||
| Vec3D operator+(const Vec3D a, const Vec3D b) { | ||
| return {a.x + b.x, a.y + b.y, a.z + b.z}; | ||
| // NOLINTNEXTLINE(readability-convert-member-functions-to-static) | ||
| CUDA_CALLABLE Rotation Rotation::from_quat(float x, float y, float z, float w) { | ||
| auto modulus = std::sqrt(x * x + y * y + z * z + w * w); | ||
| return Rotation{{x / modulus, y / modulus, z / modulus, w / modulus}}; | ||
| } | ||
|
|
||
| CUDA_CALLABLE Vec3D Rotation::apply(const Vec3D vec) const { | ||
| // v' = q * v * q^(-1) for unit quaternions | ||
| // where q^(-1) = (-x, -y, -z, w) | ||
| Vec3D q = {unit_quat_.x, unit_quat_.y, unit_quat_.z}; | ||
| float w = unit_quat_.w; | ||
|
|
||
| // v' = 2*(q·v)*q + (w²-|q|²)*v + 2*w*(q×v) | ||
| float d = dot(q, vec); | ||
| Vec3D c = cross(q, vec); | ||
|
|
||
| return 2.0f * d * q + (w * w - dot(q, q)) * vec + 2.0f * w * c; | ||
| } | ||
|
|
||
| // NOLINTNEXTLINE(readability-convert-member-functions-to-static) | ||
| CUDA_CALLABLE Rotation Rotation::compose(const Rotation& rot) const { | ||
| // Quaternion multiplication: q1 * q2 | ||
| float4 q1 = unit_quat_; | ||
| float4 q2 = rot.unit_quat_; | ||
|
|
||
| return Rotation{{q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y, | ||
| q1.w * q2.y - q1.x * q2.z + q1.y * q2.w + q1.z * q2.x, | ||
| q1.w * q2.z + q1.x * q2.y - q1.y * q2.x + q1.z * q2.w, | ||
| q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z}}; | ||
| } | ||
|
|
||
| Vec3D operator-(const Vec3D a, const Vec3D b) { | ||
| return {a.x - b.x, a.y - b.y, a.z - b.z}; | ||
| CUDA_CALLABLE Rotation Rotation::inv() const { | ||
| // For unit quaternions, inverse = conjugate: (-x, -y, -z, w) | ||
| return Rotation{{-unit_quat_.x, -unit_quat_.y, -unit_quat_.z, unit_quat_.w}}; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,30 +2,101 @@ | |
|
|
||
| #include <cuda_runtime.h> | ||
|
|
||
| #include "utils.cuh" | ||
|
|
||
| using Vec3D = float3; | ||
|
|
||
| Vec3D operator+(const Vec3D a, const Vec3D b); | ||
| Vec3D operator-(const Vec3D a, const Vec3D b); | ||
| CUDA_CALLABLE inline Vec3D operator-(const Vec3D a) { | ||
| return {-a.x, -a.y, -a.z}; | ||
| } | ||
|
|
||
| class Rotation { | ||
| CUDA_CALLABLE inline Vec3D operator+(const Vec3D a, const Vec3D b) { | ||
| return {a.x + b.x, a.y + b.y, a.z + b.z}; | ||
| } | ||
|
|
||
| CUDA_CALLABLE inline Vec3D operator-(const Vec3D a, const Vec3D b) { | ||
| return {a.x - b.x, a.y - b.y, a.z - b.z}; | ||
| } | ||
|
|
||
| CUDA_CALLABLE inline Vec3D operator*(const float scalar, const Vec3D a) { | ||
| return {a.x * scalar, a.y * scalar, a.z * scalar}; | ||
| } | ||
|
|
||
| CUDA_CALLABLE inline Vec3D operator*(const Vec3D a, const float scalar) { | ||
| return {a.x * scalar, a.y * scalar, a.z * scalar}; | ||
| } | ||
|
|
||
| CUDA_CALLABLE inline Vec3D operator/(const Vec3D a, const float scalar) { | ||
| return {a.x / scalar, a.y / scalar, a.z / scalar}; | ||
| } | ||
|
|
||
| CUDA_CALLABLE inline float dot(const Vec3D a, const Vec3D b) { | ||
| return a.x * b.x + a.y * b.y + a.z * b.z; | ||
| } | ||
|
|
||
| CUDA_CALLABLE inline Vec3D cross(const Vec3D a, const Vec3D b) { | ||
| return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; | ||
| } | ||
|
|
||
| class Rotation { | ||
| private: | ||
| // ... | ||
| float rotmat_[9]; | ||
| float4 unit_quat_; | ||
|
|
||
| CUDA_CALLABLE Rotation(float4 unit_quat) : unit_quat_{unit_quat} {}; | ||
|
|
||
| public: | ||
| Vec3D apply(const Vec3D vec) const; | ||
| Rotation compose(const Rotation& rot) const; | ||
| Rotation inv() const; | ||
| CUDA_CALLABLE Rotation() : unit_quat_{0.0f, 0.0f, 0.0f, 1.0f} {}; | ||
|
|
||
| static CUDA_CALLABLE Rotation from_quat(float x, float y, float z, float w); | ||
|
|
||
| CUDA_CALLABLE Vec3D apply(const Vec3D vec) const; | ||
|
|
||
| CUDA_CALLABLE Rotation compose(const Rotation& rot) const; | ||
|
|
||
| CUDA_CALLABLE Rotation inv() const; | ||
| }; | ||
|
|
||
| struct Pose { | ||
| Rotation rot; | ||
| Vec3D tran; | ||
| class Pose { | ||
| private: | ||
| Rotation rot_; | ||
| Vec3D tran_; | ||
|
|
||
| CUDA_CALLABLE Pose(const Rotation rot, const Vec3D tran) : rot_{rot}, tran_{tran} {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is really minor but I wonder if we can accept references here? 🤔
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this like 7 floats? Would a reference make a difference?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not by much tbh, but I guess it wouldn't hurt either? ;) |
||
|
|
||
| public: | ||
| // these member functions are defined in class body to allow for possible inlining | ||
|
|
||
| CUDA_CALLABLE Pose() : rot_{Rotation()}, tran_{0.0f, 0.0f, 0.0f} {} | ||
|
|
||
| static CUDA_CALLABLE Pose from_components(const Rotation rot, const Vec3D tran) { | ||
| return {rot, tran}; | ||
| } | ||
|
|
||
| CUDA_CALLABLE Rotation get_rot() const { | ||
| return rot_; | ||
| } | ||
|
|
||
| CUDA_CALLABLE Vec3D get_tran() const { | ||
| return tran_; | ||
| } | ||
|
|
||
| CUDA_CALLABLE Vec3D apply(const Vec3D vec) const { | ||
| return tran_ + rot_.apply(vec); | ||
| } | ||
|
|
||
| CUDA_CALLABLE Pose compose(const Pose& pose) const { | ||
| /* | ||
| * If $A_i$ is the matrix corresponding to pose object `p_i`, then | ||
| * $A_1A_2$ is the matrix corresponding to the pose object | ||
| * `p_1.compose(p2)`. | ||
| */ | ||
| return {rot_.compose(pose.rot_), rot_.apply(pose.tran_) + tran_}; | ||
| } | ||
|
|
||
| Vec3D apply(const Vec3D vec) const; | ||
| Pose compose(const Pose& pose) const; | ||
| Pose inv() const; | ||
| CUDA_CALLABLE Pose inv() const { | ||
| auto rotinv = rot_.inv(); | ||
| return {rotinv, -rotinv.apply(tran_)}; | ||
| } | ||
| }; | ||
|
|
||
| struct Ray { | ||
|
|
||
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.
I bet this linting is getting annoying haha, i'm also always adding suppressions here just to get over the dumb linting checks. Good addition!