opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
math_helpers.h
Go to the documentation of this file.
1#pragma once
2
13
14#include <array>
15
16namespace osc { struct Circle; }
17namespace osc { struct Disc; }
18namespace osc { struct Plane; }
19namespace osc { struct LineSegment; }
20namespace osc { struct Tetrahedron; }
21
22// math helpers: generally handy math functions that aren't attached to a particular
23// osc struct
24namespace osc
25{
26 // ----- Visualization helpers -----
27
28 // computes horizontal FoV for a given vertical FoV + aspect ratio
29 Radians vertical_to_horizontal_field_of_view(Radians vertical_field_of_view, float aspect_ratio);
30
31 // returns a normalized device coordinate-like (NDC-like) point converted from a point
32 // defined in a normalized y-points-down space.
33 //
34 // - input point should have origin in top-left, Y goes down
35 // - input point should have normalized range: (0, 0) is top-left, (+1, +1) is bottom-right
36 // - output point's origin will be in the middle of the input space, Y goes up
37 // - output point's range will be origin-centered: (-1, -1) is bottom-left, (+1, +1) is top-right
39
40 // returns a normalized y-points-down point converted from a point defined in a
41 // normalized device coordinate-like (NDC-like) space.
42 //
43 // - input point should have origin in the middle, Y goes up
44 // - input point should have an origin-centered range: (-1, -1) is bottom-left, (+1, +1) is top-right
45 // - output point origin will have an origin in the top-left, Y goes down
46 // - output point has range: (0, 0) for top-left, (1, 1) for bottom-right
48
49 // returns an NDC affine point (i.e. {x, y, z, 1.0}) converted from a point
50 // defined in a normalized y-points-down space.
51 //
52 // - input point should have origin in top-left, Y goes down
53 // - input point should have normalized range: (0, 0) is top-left, (+1, +1) is bottom-right
54 // - output point origin will have an origin in the top-left, Y goes down
55 // - output point has range: (0, 0) for top-left, (1, 1) for bottom-right
56 // - output point will have a `z` of `-1.0f` (i.e. nearest depth)
58
59 // Returns a point in world space converted from a point defined in a normalized
60 // y-points-down space.
61 //
62 // - input point should have origin in top-left, Y goes down
63 // - input point should have normalized range: (0, 0) is top-left, (+1, +1) is bottom-right
64 // - `camera_view_matrix` transforms points from world space to view space
65 // - `camera_proj_matrix` transforms points from view space to world space
67 Vector2 normalized_point,
68 const Matrix4x4& camera_view_matrix,
69 const Matrix4x4& camera_proj_matrix
70 );
71
72 // "un-project" a point defined in a normalized y-points-down space into world
73 // space, assuming a perspective projection
74 //
75 // - input point should have origin in top-left, Y goes down
76 // - input point should have normalized range: (0, 0) is top-left, (+1, +1) is bottom-right
77 // - `camera_world_space_origin` is the position of the camera in world space
78 // - `camera_view_matrix` transforms points from world space to view space
79 // - `camera_proj_matrix` transforms points from view space to world space
81 Vector2 normalized_point,
82 Vector3 camera_world_space_origin,
83 const Matrix4x4& camera_view_matrix,
84 const Matrix4x4& camera_proj_matrix
85 );
86
87 // returns a rectangular region defined in, and bounded by, normalized device coordinates
88 // (NDCs) mapped from a region defined in a normalized y-points-down space bounded
89 // by `viewport`.
90 Rect ndc_rect_to_topleft_viewport_rect(const Rect& ndc_rect, const Rect& viewport);
91
92 // returns the position where `world_space_position` would occur when projected via the
93 // given `view_matrix` and `projection_matrix`es onto `viewport_rect`.
95 const Vector3& world_space_position,
96 const Matrix4x4& view_matrix,
97 const Matrix4x4& projection_matrix,
98 const Rect& viewport_rect
99 );
100
101 // returns arrays that transforms cube faces from world space to projection
102 // space such that the observer is looking at each face of the cube from
103 // the center of the cube
104 std::array<Matrix4x4, 6> calc_cubemap_view_proj_matrices(
105 const Matrix4x4& projection_matrix,
106 Vector3 cube_center
107 );
108
109 // ----- `Disc` helpers -----
110
111 // returns a `Mat4` that maps one `Disc` to another `Disc`
113
114
115 // ----- `Segment` helpers -----
116
117 // returns a transform matrix that maps a path segment to another path segment
119
120 // returns a `Transform` that maps a path segment to another path segment
122
123 // returns a `Transform` that maps a Y-to-Y (bottom-to-top) cylinder to a segment with the given radius
125
126 // returns a `Transform` that maps a Y-to-Y (bottom-to-top) cone to a segment with the given radius
128
129 // ----- Vector/Matrix helpers -----
130
131 // returns euler angles for performing an intrinsic, step-by-step, rotation about X, Y, and then Z
133
134 // returns a `Quaternion` equivalent to the given euler angles
136
137 // applies a world space rotation to the transform
139 Transform& application_target,
140 const EulerAngles& euler_angles,
141 const Vector3& rotation_center
142 );
143
144 // returns the volume of a given tetrahedron, defined as 4 points in space
145 float volume_of(const Tetrahedron&);
146}
Definition angle.h:26
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)
Vector2 ndc_point_to_topleft_normalized(Vector2 ndc_point)
Radians vertical_to_horizontal_field_of_view(Radians vertical_field_of_view, float aspect_ratio)
Transform transform_between(const LineSegment &, const LineSegment &)
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)
Vector2 topleft_normalized_point_to_ndc(Vector2 normalized_point)
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)
Vector3 topleft_normalized_point_to_world_znear(Vector2 normalized_point, const Matrix4x4 &camera_view_matrix, const Matrix4x4 &camera_proj_matrix)
Vector4 topleft_normalized_point_to_ndc_cube(Vector2 normalized_point)
Rect ndc_rect_to_topleft_viewport_rect(const Rect &ndc_rect, const Rect &viewport)
Transform y_to_y_cone_to_segment_transform(const LineSegment &, float radius)
constexpr float volume_of(const AABB &aabb)
Definition aabb_functions.h:40
Definition disc.h:12
Definition line_segment.h:13
Definition ray.h:17
Definition tetrahedron.h:9
Definition transform.h:11