candlewick 0.6.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
Collision.h
Go to the documentation of this file.
1#pragma once
2#include "math_types.h"
3#include <coal/BV/AABB.h>
4#include <coal/BV/OBB.h>
5
6namespace candlewick {
7
8using coal::AABB;
9
10inline Mat4f toTransformationMatrix(const AABB &aabb) {
11 Mat4f T = Mat4f::Identity();
12 Float3 halfExtents = 0.5f * (aabb.max_ - aabb.min_).cast<float>();
13 T.block<3, 3>(0, 0) = halfExtents.asDiagonal();
14 T.topRightCorner<3, 1>() = aabb.center().cast<float>();
15 return T;
16}
17
18inline Mat4f toTransformationMatrix(const coal::OBB &obb) {
19 Mat4f T = Mat4f::Identity();
20 auto D = obb.extent.asDiagonal();
21 T.block<3, 3>(0, 0) = (obb.axes * D).cast<float>();
22 T.topRightCorner<3, 1>() = obb.center().cast<float>();
23 return T;
24}
25
26inline std::array<Float3, 8> getAABBCorners(const AABB &aabb) {
27 const Float3 min = aabb.min_.cast<float>();
28 const Float3 max = aabb.max_.cast<float>();
29
30 return {
31 Float3{min.x(), min.y(), min.z()}, // 000
32 Float3{max.x(), min.y(), min.z()}, // 100
33 Float3{min.x(), max.y(), min.z()}, // 010
34 Float3{max.x(), max.y(), min.z()}, // 110
35 Float3{min.x(), min.y(), max.z()}, // 001
36 Float3{max.x(), min.y(), max.z()}, // 101
37 Float3{min.x(), max.y(), max.z()}, // 011
38 Float3{max.x(), max.y(), max.z()}, // 111
39 };
40}
41
42inline AABB applyTransformToAABB(const AABB &aabb, const Mat4f &tr_) {
43 using coal::CoalScalar;
44 auto tr = tr_.cast<CoalScalar>();
45 coal::Matrix3s R = tr.topLeftCorner<3, 3>();
46 coal::Vec3s t = tr.topRightCorner<3, 1>();
47 return coal::translate(coal::rotate(aabb, R), t);
48}
49
50} // namespace candlewick
Definition Camera.h:8
Eigen::Vector3f Float3
Definition math_types.h:8
AABB applyTransformToAABB(const AABB &aabb, const Mat4f &tr_)
Definition Collision.h:42
std::array< Float3, 8 > getAABBCorners(const AABB &aabb)
Definition Collision.h:26
Mat4f toTransformationMatrix(const AABB &aabb)
Definition Collision.h:10
Eigen::Matrix4f Mat4f
Definition math_types.h:11