ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
ObjectAlign.h
Go to the documentation of this file.
1/**
2 * @file ObjectAlign.h
3 * @brief Provides the ObjectAlign class for aligning 3D objects within defined 2D camera bounds.
4 *
5 * The ObjectAlign class is useful for positioning, scaling, and orienting 3D objects
6 * (or collections of objects) relative to a 2D screen area (defined by min/max camera
7 * coordinates). It calculates transformations needed to align objects in a plane while
8 * providing options for mirroring and different justification modes.
9 */
10
11#pragma once
12
13#include "../../Utils/Math/Plane.h"
14#include "../../Scene/Objects/Object3D.h"
15
16/**
17 * @class ObjectAlign
18 * @brief Handles aligning and transforming 3D objects to fit within specified 2D camera bounds.
19 *
20 * This class is primarily concerned with projecting objects into a plane, scaling them
21 * to fit within camera bounds, and applying transformations (orientation, mirroring,
22 * edge offsets, etc.).
23 */
25public:
26 /**
27 * @enum Justification
28 * @brief Describes how the object(s) should be justified within the bounding box.
29 *
30 * For instance, LowerLeft places the object in the lower-left corner (with potential
31 * offsets), while Stretch attempts to fit or stretch objects to fill the entire area.
32 */
34 UpperLeft, ///< Justify to the upper-left corner of the bounding area.
35 UpperMiddle, ///< Justify to the top center of the bounding area.
36 UpperRight, ///< Justify to the upper-right corner of the bounding area.
37 MiddleLeft, ///< Justify to the middle-left side of the bounding area.
38 Middle, ///< Center the object within the bounding area.
39 MiddleRight, ///< Align to the middle-right side of the bounding area.
40 LowerLeft, ///< Justify to the lower-left corner of the bounding area.
41 LowerMiddle, ///< Align to the bottom center of the bounding area.
42 LowerRight, ///< Justify to the lower-right corner of the bounding area.
43 Stretch ///< Attempt to scale the object(s) to fill the entire area.
44 };
45
46private:
47 Justification jst = Middle; ///< Current justification mode.
48 Quaternion targetOrientation; ///< Target orientation for aligning the object(s).
49 Vector3D forwardVector = Vector3D(0.0f, 0.0f, 1.0f); ///< Default "forward" axis.
50 Vector2D cameraCenter; ///< Computed center between camMin and camMax.
51 Vector2D camMin; ///< Minimum 2D camera bounds (lower-left).
52 Vector2D camMax; ///< Maximum 2D camera bounds (upper-right).
53 float offsetPlaneAngle = 0.0f; ///< Additional rotation offset (plane offset angle in degrees or radians).
54 float edgeMargin = 10.0f; ///< Margin from the bounding edges, in screen-space units.
55 float scaleX = 1.0f; ///< Scaling factor on the X-axis.
56 float scaleY = 1.0f; ///< Scaling factor on the Y-axis.
57 bool mirrorX = false; ///< Whether to mirror objects along the X-axis.
58 bool mirrorY = false; ///< Whether to mirror objects along the Y-axis.
59
60 /**
61 * @brief Normalizes the orientation of multiple objects onto a plane.
62 *
63 * This function modifies the input objects so that they are oriented parallel
64 * to a plane defined by \p planeOrientation, also adjusting their centers
65 * to the specified \p center.
66 *
67 * @param objs Array of pointers to Object3D instances.
68 * @param numObjects Number of objects in \p objs.
69 * @param center Desired alignment center in 3D space.
70 * @param planeOrientation Orientation (Quaternion) defining the plane normal.
71 */
72 void NormalizeObjectPlane(Object3D** objs, uint8_t numObjects, Vector3D center, Quaternion planeOrientation);
73
74 /**
75 * @brief Translates the center of multiple objects so that their average center
76 * matches the specified \p center.
77 *
78 * @param objs Array of pointers to Object3D instances.
79 * @param numObjects Number of objects in \p objs.
80 * @param center The target center position in 3D space.
81 */
82 void NormalizeObjectCenter(Object3D** objs, uint8_t numObjects, Vector3D center);
83
84public:
85 /**
86 * @brief Constructor for ObjectAlign, setting up the camera bounds and orientation.
87 * @param camMin The lower-left corner of the 2D camera region.
88 * @param camMax The upper-right corner of the 2D camera region.
89 * @param targetOrientation Optional desired orientation for aligned objects.
90 */
92
93 /**
94 * @brief Computes the centroid of the given object's geometry.
95 * @param obj Pointer to the Object3D.
96 * @return A Vector3D representing the centroid position.
97 */
99
100 /**
101 * @brief Computes the collective centroid of multiple objects.
102 * @param objs Array of pointers to Object3D objects.
103 * @param numObjects Number of objects in \p objs.
104 * @return A Vector3D representing the combined centroid of all objects.
105 */
106 Vector3D GetCentroid(Object3D** objs, uint8_t numObjects);
107
108 /**
109 * @brief Computes the "center" of a single object. Generally the same as centroid,
110 * but could be defined differently if an object's pivot or bounding logic
111 * is distinct.
112 * @param obj Pointer to the Object3D.
113 * @return The object's center as a Vector3D.
114 */
116
117 /**
118 * @brief Computes the collective center for multiple objects.
119 * @param objs Array of pointers to Object3D.
120 * @param numObjects Number of objects in \p objs.
121 * @return A Vector3D representing the combined center of all objects.
122 */
123 Vector3D GetObjectCenter(Object3D** objs, uint8_t numObjects);
124
125 /**
126 * @brief Retrieves the bounding box size (width, height, depth) of a single object.
127 * @param obj Pointer to the Object3D.
128 * @return A Vector3D whose components represent the bounding box dimensions.
129 */
131
132 /**
133 * @brief Computes the combined bounding box size for multiple objects.
134 * @param objs Array of pointers to Object3D.
135 * @param numObjects Number of objects in \p objs.
136 * @return A Vector3D representing the bounding box that encloses all objects.
137 */
138 Vector3D GetObjectSize(Object3D** objs, uint8_t numObjects);
139
140 /**
141 * @brief Determines the plane normal (as a Quaternion) of the object based on its geometry.
142 *
143 * This might be computed from the surface normal if the object is considered planar,
144 * or from an average of normals if the object is more complex.
145 *
146 * @param obj Pointer to the Object3D.
147 * @return A Quaternion representing the plane's orientation (normal).
148 */
150
151 /**
152 * @brief Determines the average plane normal (as a Quaternion) for multiple objects.
153 * @param objs Array of pointers to Object3D.
154 * @param numObjects Number of objects in \p objs.
155 * @return A Quaternion representing the average plane orientation.
156 */
157 Quaternion GetPlaneNormal(Object3D** objs, uint8_t numObjects);
158
159 /**
160 * @brief Calculates the orientation needed to make the object's plane face the camera or
161 * align with a given centroid.
162 *
163 * @param obj Pointer to the Object3D.
164 * @param centroid The computed centroid of the object.
165 * @return A Quaternion representing the orientation for that plane.
166 */
168
169 /**
170 * @brief Calculates the orientation needed for multiple objects to share a consistent plane.
171 *
172 * @param objs Array of pointers to Object3D.
173 * @param numObjects Number of objects in \p objs.
174 * @param centroid The computed centroid of all objects.
175 * @return A Quaternion representing the plane orientation for the combined objects.
176 */
177 Quaternion GetPlaneOrientation(Object3D** objs, uint8_t numObjects, Vector3D centroid);
178
179 /**
180 * @brief Computes the final Transform for aligning a single Object3D within the camera bounds.
181 *
182 * The Transform includes translation, rotation, and scaling as determined by the justification,
183 * margins, and plane offset angle.
184 *
185 * @param obj Pointer to the Object3D to align.
186 * @return A Transform struct containing position, rotation, and scale.
187 */
189
190 /**
191 * @brief Computes the final Transform for aligning multiple objects as a group.
192 *
193 * @param objs Array of pointers to Object3D.
194 * @param numObjects Number of objects in \p objs.
195 * @return A Transform for the combined alignment of the group.
196 */
197 Transform GetTransform(Object3D** objs, uint8_t numObjects);
198
199 /**
200 * @brief Computes how planar (flat) a single object is, typically as a ratio (0.0 = not planar, 1.0 = perfectly planar).
201 * @param obj Pointer to the Object3D.
202 * @return A float representing the planarity ratio.
203 */
205
206 /**
207 * @brief Computes an average planarity ratio for multiple objects.
208 *
209 * @param objs Array of pointers to Object3D.
210 * @param numObjects Number of objects in \p objs.
211 * @return A float representing the combined or averaged planarity ratio.
212 */
213 float GetObjectPlanarityRatio(Object3D** objs, uint8_t numObjects);
214
215 /**
216 * @brief Sets the additional rotation offset (plane offset angle), in degrees or radians,
217 * that will be applied when calculating plane orientation.
218 *
219 * @param offsetPlaneAngle The rotation offset to apply.
220 */
222
223 /**
224 * @brief Sets the margin to keep from the edges when aligning objects.
225 * @param edgeMargin How many screen-space units to leave as margin.
226 */
227 void SetEdgeMargin(float edgeMargin);
228
229 /**
230 * @brief Sets the forward vector, i.e., which axis is considered "forward" for the alignment logic.
231 * @param forwardVector A Vector3D defining the forward axis.
232 */
234
235 /**
236 * @brief Updates the minimum bounds for the 2D camera region.
237 * @param camMin A Vector2D representing the lower-left corner.
238 */
240
241 /**
242 * @brief Updates the maximum bounds for the 2D camera region.
243 * @param camMax A Vector2D representing the upper-right corner.
244 */
246
247 /**
248 * @brief Enables or disables mirroring along the X-axis for the aligned objects.
249 * @param mirrorX True to mirror objects on X-axis, false to disable.
250 */
251 void SetMirrorX(bool mirrorX);
252
253 /**
254 * @brief Enables or disables mirroring along the Y-axis for the aligned objects.
255 * @param mirrorY True to mirror objects on Y-axis, false to disable.
256 */
257 void SetMirrorY(bool mirrorY);
258
259 /**
260 * @brief Sets the justification mode for alignment.
261 * @param jst One of the enum values (UpperLeft, Middle, Stretch, etc.).
262 */
264
265 /**
266 * @brief Sets the scaling factors that will be applied to objects during alignment.
267 * @param scaleX The multiplier for width.
268 * @param scaleY The multiplier for height.
269 */
270 void SetScale(float scaleX, float scaleY);
271
272 /**
273 * @brief Aligns a single Object3D within the camera bounds without applying the object's scaling factor.
274 *
275 * Only transforms the object to fit in the plane (translation + orientation); no scaling from the object itself.
276 * @param obj Pointer to the Object3D to align.
277 */
278 void AlignObjectNoScale(Object3D* obj);
279
280 /**
281 * @brief Aligns multiple objects without applying the object's scaling factor.
282 *
283 * @param objs Array of pointers to Object3D.
284 * @param numObjects Number of objects in \p objs.
285 */
286 void AlignObjectsNoScale(Object3D** objs, uint8_t numObjects);
287
288 /**
289 * @brief Aligns a single Object3D within the camera bounds, including applying scale factors as necessary.
290 * @param obj Pointer to the Object3D to align.
291 */
292 void AlignObject(Object3D* obj);
293
294 /**
295 * @brief Aligns multiple objects within the camera bounds, including scale factors.
296 * @param objs Array of pointers to Object3D.
297 * @param numObjects Number of objects in \p objs.
298 */
299 void AlignObjects(Object3D** objs, uint8_t numObjects);
300};
Represents a 3D object with geometry, material, and transformation data.
Definition Object3D.h:28
Handles aligning and transforming 3D objects to fit within specified 2D camera bounds.
Definition ObjectAlign.h:24
void SetEdgeMargin(float edgeMargin)
Sets the margin to keep from the edges when aligning objects.
void AlignObjectNoScale(Object3D *obj)
Aligns a single Object3D within the camera bounds without applying the object's scaling factor.
void SetForwardVector(Vector3D forwardVector)
Sets the forward vector, i.e., which axis is considered "forward" for the alignment logic.
float scaleY
Scaling factor on the Y-axis.
Definition ObjectAlign.h:56
float GetObjectPlanarityRatio(Object3D *obj)
Computes how planar (flat) a single object is, typically as a ratio (0.0 = not planar,...
float offsetPlaneAngle
Additional rotation offset (plane offset angle in degrees or radians).
Definition ObjectAlign.h:53
Vector3D GetObjectSize(Object3D *obj)
Retrieves the bounding box size (width, height, depth) of a single object.
Quaternion GetPlaneNormal(Object3D *obj)
Determines the plane normal (as a Quaternion) of the object based on its geometry.
void NormalizeObjectCenter(Object3D **objs, uint8_t numObjects, Vector3D center)
Translates the center of multiple objects so that their average center matches the specified center.
Justification
Describes how the object(s) should be justified within the bounding box.
Definition ObjectAlign.h:33
@ MiddleLeft
Justify to the middle-left side of the bounding area.
Definition ObjectAlign.h:37
@ LowerMiddle
Align to the bottom center of the bounding area.
Definition ObjectAlign.h:41
@ MiddleRight
Align to the middle-right side of the bounding area.
Definition ObjectAlign.h:39
@ UpperLeft
Justify to the upper-left corner of the bounding area.
Definition ObjectAlign.h:34
@ Stretch
Attempt to scale the object(s) to fill the entire area.
Definition ObjectAlign.h:43
@ UpperRight
Justify to the upper-right corner of the bounding area.
Definition ObjectAlign.h:36
@ Middle
Center the object within the bounding area.
Definition ObjectAlign.h:38
@ UpperMiddle
Justify to the top center of the bounding area.
Definition ObjectAlign.h:35
@ LowerRight
Justify to the lower-right corner of the bounding area.
Definition ObjectAlign.h:42
@ LowerLeft
Justify to the lower-left corner of the bounding area.
Definition ObjectAlign.h:40
Transform GetTransform(Object3D *obj)
Computes the final Transform for aligning a single Object3D within the camera bounds.
Quaternion targetOrientation
Target orientation for aligning the object(s).
Definition ObjectAlign.h:48
void SetJustification(Justification jst)
Sets the justification mode for alignment.
void NormalizeObjectPlane(Object3D **objs, uint8_t numObjects, Vector3D center, Quaternion planeOrientation)
Normalizes the orientation of multiple objects onto a plane.
Vector2D camMin
Minimum 2D camera bounds (lower-left).
Definition ObjectAlign.h:51
Vector3D GetCentroid(Object3D *obj)
Computes the centroid of the given object's geometry.
bool mirrorX
Whether to mirror objects along the X-axis.
Definition ObjectAlign.h:57
Justification jst
Current justification mode.
Definition ObjectAlign.h:47
Vector2D cameraCenter
Computed center between camMin and camMax.
Definition ObjectAlign.h:50
float edgeMargin
Margin from the bounding edges, in screen-space units.
Definition ObjectAlign.h:54
void SetPlaneOffsetAngle(float offsetPlaneAngle)
Sets the additional rotation offset (plane offset angle), in degrees or radians, that will be applied...
Vector3D GetObjectCenter(Object3D *obj)
Computes the "center" of a single object. Generally the same as centroid, but could be defined differ...
void AlignObjectsNoScale(Object3D **objs, uint8_t numObjects)
Aligns multiple objects without applying the object's scaling factor.
void AlignObject(Object3D *obj)
Aligns a single Object3D within the camera bounds, including applying scale factors as necessary.
Quaternion GetPlaneOrientation(Object3D *obj, Vector3D centroid)
Calculates the orientation needed to make the object's plane face the camera or align with a given ce...
void SetCameraMax(Vector2D camMax)
Updates the maximum bounds for the 2D camera region.
void AlignObjects(Object3D **objs, uint8_t numObjects)
Aligns multiple objects within the camera bounds, including scale factors.
bool mirrorY
Whether to mirror objects along the Y-axis.
Definition ObjectAlign.h:58
void SetMirrorX(bool mirrorX)
Enables or disables mirroring along the X-axis for the aligned objects.
Vector3D forwardVector
Default "forward" axis.
Definition ObjectAlign.h:49
float scaleX
Scaling factor on the X-axis.
Definition ObjectAlign.h:55
void SetMirrorY(bool mirrorY)
Enables or disables mirroring along the Y-axis for the aligned objects.
Vector2D camMax
Maximum 2D camera bounds (upper-right).
Definition ObjectAlign.h:52
void SetCameraMin(Vector2D camMin)
Updates the minimum bounds for the 2D camera region.
void SetScale(float scaleX, float scaleY)
Sets the scaling factors that will be applied to objects during alignment.
A mathematical construct representing a rotation in 3D space.
Definition Quaternion.h:30
Represents a 3D transformation including position, rotation, and scale.
Definition Transform.h:22
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26