opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
math_helpers.h
Go to the documentation of this file.
1#pragma once
2
10#include <liboscar/maths/rect.h>
15
16#include <array>
17#include <optional>
18#include <span>
19
20namespace osc { struct Circle; }
21namespace osc { struct Disc; }
22namespace osc { struct Plane; }
23namespace osc { struct LineSegment; }
24namespace osc { struct Tetrahedron; }
25
26// math helpers: generally handy math functions that aren't attached to a particular
27// osc struct
28namespace osc
29{
30 // computes horizontal FoV for a given vertical FoV + aspect ratio
32
33 // returns a normalized device coordinate-like (NDC-like) point converted from a point
34 // defined in a normalized y-points-down space.
35 //
36 // - input point should have origin in top-left, Y goes down
37 // - input point should have normalized range: (0, 0) is top-left, (+1, +1) is bottom-right
38 // - output point's origin will be in the middle of the input space, Y goes up
39 // - output point's range will be origin-centered: (-1, -1) is bottom-left, (+1, +1) is top-right
41
42 // returns a normalized y-points-down point converted from a point defined in a
43 // normalized device coordinate-like (NDC-like) space.
44 //
45 // - input point should have origin in the middle, Y goes up
46 // - input point should have an origin-centered range: (-1, -1) is bottom-left, (+1, +1) is top-right
47 // - output point origin will have an origin in the top-left, Y goes down
48 // - output point has range: (0, 0) for top-left, (1, 1) for bottom-right
50
51 // returns an NDC affine point (i.e. {x, y, z, 1.0}) converted from a point
52 // defined in a normalized y-points-down space.
53 //
54 // - input point should have origin in top-left, Y goes down
55 // - input point should have normalized range: (0, 0) is top-left, (+1, +1) is bottom-right
56 // - output point origin will have an origin in the top-left, Y goes down
57 // - output point has range: (0, 0) for top-left, (1, 1) for bottom-right
58 // - output point will have a `z` of `-1.0f` (i.e. nearest depth)
60
61 // "un-project" a point defined in a normalized y-points-down space into world
62 // space, assuming a perspective projection
63 //
64 // - input point should have origin in top-left, Y goes down
65 // - input point should have normalized range: (0, 0) is top-left, (+1, +1) is bottom-right
66 // - `camera_world_space_origin` is the position of the camera in world space
67 // - `camera_view_matrix` transforms points from world space to view space
68 // - `camera_proj_matrix` transforms points from view space to world space
74 );
75
76 // returns a rectangular region defined in, and bounded by, normalized device coordinates
77 // (NDCs) mapped from a region defined in a normalized y-points-down space bounded
78 // by `viewport`.
80
81 // returns the position where `world_space_position` would occur when projected via the
82 // given `view_matrix` and `projection_matrix`es onto `viewport_rect`.
85 const Matrix4x4& view_matrix,
86 const Matrix4x4& projection_matrix,
87 const Rect& viewport_rect
88 );
89
90
91 // ----- `Sphere` helpers -----
92
93 // returns a `Sphere` that loosely bounds the given `Vector3`s
94 std::optional<Sphere> bounding_sphere_of(std::span<const Vector3>);
95
96 // returns a `Sphere` that loosely bounds the given `AABB`
98
99 // returns an `AABB` that tightly bounds the `Sphere`
101
102
103 // ----- `Ray` helpers -----
104
105 // returns a `Ray` that has been transformed by the `Mat4`
107
108 // returns a `Ray` that has been transformed by the inverse of the supplied `Transform`
110
111
112 // ----- `Disc` helpers -----
113
114 // returns a `Mat4` that maps one `Disc` to another `Disc`
116
117
118 // ----- `Segment` helpers -----
119
120 // returns a transform matrix that maps a path segment to another path segment
122
123 // returns a `Transform` that maps a path segment to another path segment
125
126 // returns a `Transform` that maps a Y-to-Y (bottom-to-top) cylinder to a segment with the given radius
128
129 // returns a `Transform` that maps a Y-to-Y (bottom-to-top) cone to a segment with the given radius
131
132 // ----- Vector/Matrix helpers -----
133
134 // returns a transform matrix that rotates `dir1` to point in the same direction as `dir2`
136
137 // returns euler angles for performing an intrinsic, step-by-step, rotation about X, Y, and then Z
139
140 inline Vector3 transform_point(const Matrix4x4& mat, const Vector3& point)
141 {
142 return Vector3{mat * Vector4{point, 1.0f}};
143 }
144
146 {
147 return Vector3{mat * Vector4{vector, 0.0f}};
148 }
149
150 // returns a `Quaternion` equivalent to the given euler angles
152
153 // applies a world space rotation to the transform
158 );
159
160 // returns the volume of a given tetrahedron, defined as 4 points in space
161 float volume_of(const Tetrahedron&);
162
163 // returns arrays that transforms cube faces from world space to projection
164 // space such that the observer is looking at each face of the cube from
165 // the center of the cube
166 std::array<Matrix4x4, 6> calc_cubemap_view_proj_matrices(
167 const Matrix4x4& projection_matrix,
169 );
170}
Definition angle.h:25
Definition rect.h:17
Definition custom_decoration_generator.h:5
EulerAngles extract_eulers_xyz(const Quaternion &)
Transform cylinder_to_line_segment_transform(const LineSegment &, float radius)
Ray inverse_transform_ray(const Ray &, const Transform &)
Vector2 ndc_point_to_topleft_normalized(Vector2 ndc_point)
Radians vertical_to_horizontal_field_of_view(Radians vertical_field_of_view, float aspect_ratio)
Vector3 transform_point(const Matrix4x4 &mat, const Vector3 &point)
Definition math_helpers.h:140
std::optional< Sphere > bounding_sphere_of(const Mesh &)
Transform transform_between(const LineSegment &, const LineSegment &)
Vector3 transform_vector(const Matrix4x4 &mat, const Vector3 &vector)
Definition math_helpers.h:145
Vector2 project_onto_viewport_rect(const Vector3 &world_space_position, const Matrix4x4 &view_matrix, const Matrix4x4 &projection_matrix, const Rect &viewport_rect)
void apply_world_space_rotation(Transform &application_target, const EulerAngles &euler_angles, const Vector3 &rotation_center)
Matrix4x4 matrix4x4_transform_between(const Disc &, const Disc &)
Quaternion to_world_space_rotation_quaternion(const EulerAngles &)
std::array< Matrix4x4, 6 > calc_cubemap_view_proj_matrices(const Matrix4x4 &projection_matrix, Vector3 cube_center)
Matrix4x4 matrix4x4_transform_between_directions(const Vector3 &dir1, const Vector3 &dir2)
Vector2 topleft_normalized_point_to_ndc(Vector2 normalized_point)
Ray transform_ray(const Ray &, const Matrix4x4 &)
Ray perspective_unproject_topleft_normalized_pos_to_world(Vector2 normalized_point, Vector3 camera_world_space_origin, const Matrix4x4 &camera_view_matrix, const Matrix4x4 &camera_proj_matrix)
constexpr AABB bounding_aabb_of(const Vector3 &x)
Definition aabb_functions.h:66
Vector4 topleft_normalized_point_to_ndc_cube(Vector2 normalized_point)
Rect ndc_rect_to_topleft_viewport_rect(const Rect &ndc_rect, const Rect &viewport)
constexpr U to(T &&value)
Definition conversion.h:56
Transform y_to_y_cone_to_segment_transform(const LineSegment &, float radius)
constexpr float volume_of(const AABB &aabb)
Definition aabb_functions.h:38
Definition aabb.h:14
Definition disc.h:12
Definition line_segment.h:13
Definition ray.h:13
Definition sphere.h:9
Definition tetrahedron.h:9
Definition transform.h:11