Ocean
Loading...
Searching...
No Matches
Grid.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_GEOMETRY_GRID_H
9#define META_OCEAN_GEOMETRY_GRID_H
10
12
17
18namespace Ocean
19{
20
21namespace Geometry
22{
23
24/**
25 * This class implements the basic data of an planar grid.
26 * There are 3 coordinate systems:
27 * (1) world coordinates:
28 * - describe points in the world system
29 * (2) plane coordinates:
30 * - describe points in the plane of the grid
31 * - the z coordinate is zero, if the point lies directly in the plane
32 * (3) grid coordinates:
33 * - describe points in the grid refer to the plane
34 * - the grid center must not be identical with the plane center
35 * - the following sketch shows a top-view of the grid:
36 * <pre>
37 * y
38 * ^
39 * | o - o - o - o - o
40 * | | | | | |
41 * | o - o - o - p - o
42 * | | | | | |
43 * | o - o - c - o - o
44 * | | | | | |
45 * | o - o - o - o - o
46 * | | | | | |
47 * | o - o - o - o - o
48 * |
49 * z---------------------> x
50 * </pre>
51 * - the z coordinate is always zero, so in following the grid coordinates are represent as a Vector2
52 *
53 * This class holds 3 transformations:
54 * (1) plane2world transformation:
55 * - transforms points defined in a coordinate system of the plane into points defined in the world coordinate system
56 * - all axes must be perpendicular to each other
57 * - the z axis always describe the normal of the plane
58 * (2) grid2plane transformation:
59 * - transforms points defined in a coordinate system of the grid into points defined in the plane coordinate system
60 * - all axes must be perpendicular to each other
61 * - the z axis is always (0, 0, 1)
62 * (3) virtualGrid2plane transformation
63 * - transforms points defined in a coordinate system of the virtual grid into points defined in the plane coordinate system
64 * - the z axis is always (0, 0, 1)
65 * - the x and y axes must be perpendicular to the z axis
66 * - the x axis must not be perpendicular to the y axis to skew the grid
67 *
68 * The grid holds an visualization bounding box defined in plane coordinates.
69 *
70 * This class implements convert functions to convert point from one coordinate system to an other.
71 * @ingroup geometry
72 */
73class OCEAN_GEOMETRY_EXPORT Grid
74{
75 public:
76
77 /**
78 * Creates a new invalid grid element.
79 */
80 inline Grid();
81
82 /**
83 * Creates a new Grid by a plane2world transformation.
84 * Whether the virtualGrid2plane transformation is set, the grid2plane transformation is set to the same matrix.
85 * If the grid2plane transformation should be set, the virtualGrid2plane transformation can be set to the appropriate matrix.
86 * @param plane2worldTransformation Transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system
87 * @param virtualGrid2planeTransformation Optional transformation that transforms points defined in a coordinate system of the virtual grid into points defined in the plane coordinate system
88 * @param lowerCorner Optional lower corner of the grid bounding box for visualization in plane coordinates
89 * @param upperCorner Optional upper corner of the grid bounding box for visualization in plane coordinates
90 */
91 inline explicit Grid(const HomogenousMatrix4& plane2worldTransformation, const HomogenousMatrix4& virtualGrid2planeTransformation = HomogenousMatrix4(true), const Vector2& lowerCorner = Vector2(-0.5, -0.5), const Vector2& upperCorner = Vector2(0.5, 0.5));
92
93 /**
94 * Creates a new Grid by a plane2world transformation and four image points defining a rectangle.
95 * The bounding box is adjusted, so the rectangle plus a border lies inside.
96 * The border has a size of the half average length of two neighboring projected plane points.
97 * If the virtualGrid2plane transformation is set, the grid2plane transformation is set to the same matrix.
98 * If the grid2plane transformation should be set, the virtualGrid2plane transformation can be set to the appropriate matrix.
99 * @param pinholeCamera The pinhole camera profile to be applied
100 * @param pose The pose to be applied
101 * @param imagePoints The image points to adjust the bounding box, size must be four
102 * @param plane2worldTransformation Transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system
103 * @param virtualGrid2planeTransformation Optional transformation that transforms points defined in a coordinate system of the virtual grid into points defined in the plane coordinate system
104 * @param clipBoundingBox Optional flag to clip the grid lower and upper grid corners by an image bounding box around the camera frame
105 */
106 Grid(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vectors2& imagePoints, const HomogenousMatrix4& plane2worldTransformation, const HomogenousMatrix4& virtualGrid2planeTransformation = HomogenousMatrix4(true), const bool clipBoundingBox = false);
107
108 /**
109 * Creates a new Grid by four given image points using vanishing projection.
110 * The four image points must define a convex polygon.
111 * If additionally four grid coordinates are given, the virtualGrid2plane transformation is set, too.
112 * The four grid coordinates must be given in the same order (clockwise or counter clockwise) like the four image points!
113 * The grid coordinates are only used for determine the shear of the virtualGrid2plane transformation, the lower and upper grid corners are not adjust!
114 * If no grid could be determined from the four image points, a invalid grid is create.
115 * @param pinholeCamera The pinhole camera profile to be applied
116 * @param pose The pose to be applied
117 * @param imagePoints The four image points (must be in clockwise or counterclockwise order)
118 * @param gridCoordinates Optional four grid coordinates (must be in clockwise or counterclockwise order)
119 * @param previousPlane2worldTransformation Optional Transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system to adapt the plane translation
120 * @param clipBoundingBox Optional flag to clip the grid lower and upper grid corners by an image bounding box around the camera frame
121 */
122 Grid(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vectors2& imagePoints, const Vector2* gridCoordinates = nullptr, const HomogenousMatrix4* previousPlane2worldTransformation = nullptr, const bool clipBoundingBox = false);
123
124 /**
125 * Creates a new Grid by a given transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system.
126 * The virtualGrid2plane transformation is adjusted by four given image points.
127 * The virtual grid is sheared that the four points define a rectangle on the virtual grid in the best case.
128 * @param pinholeCamera The pinhole camera profile to be applied
129 * @param pose The pose to be applied
130 * @param plane2worldTransformation Transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system
131 * @param imagePoints The four image points (must be in clockwise or counterclockwise order)
132 * @param clipBoundingBox Optional flag to clip the grid lower and upper grid corners by an image bounding box around the camera frame
133 */
134 Grid(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const HomogenousMatrix4& plane2worldTransformation, const Vectors2& imagePoints, const bool clipBoundingBox = false);
135
136 /**
137 * Returns the transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system.
138 * @return The plane transformation
139 */
140 inline const HomogenousMatrix4& plane2worldTransformation() const;
141
142 /**
143 * Returns the transformation that transforms points defined in a coordinate system of the grid into points defined in the plane coordinate system.
144 * @return The grid transformation.
145 */
146 inline const HomogenousMatrix4& grid2planeTransformation() const;
147
148 /**
149 * Returns the transformation that transform points defined in a coordinate system of the virtual grid into points defined in the plane coordinate system.
150 * @return The virtual grid transformation
151 */
152 inline const HomogenousMatrix4& virtualGrid2planeTransformation() const;
153
154 /**
155 * Returns a transformation that transforms points defined in a coordinate system of the virtual plane into points defined in the world coordinate system.
156 * The virtual plane has the orientation scale and depth of the normal plane while an optional shear component is extracted from the virtual grid if the virtual grid has a shear component.<br>
157 * @return The virtual plane transformation
158 */
160
161 /**
162 * Returns the transformation that transform points defined in a coordinate system of the grid into points defined in the world coordinate system.
163 * @return The grid transformation
164 */
165 inline HomogenousMatrix4 grid2worldTransformation() const;
166
167 /**
168 * Returns the transformation that transform points defined in a coordinate system of the virtual grid into points defined in the world coordinate system.
169 * @return The virtual grid transformation
170 */
171 inline HomogenousMatrix4 virtualGrid2worldTransformation() const;
172
173 /**
174 * Returns the lower grid corner defined in the grid coordinate system of the visualization bounding box of the grid.
175 * @return lower grid corner
176 */
177 inline const Vector2& lowerGridCorner() const;
178
179 /**
180 * Returns the upper grid corner defined in the grid coordinate system of the visualization bounding box of the grid.
181 * @return upper grid corner
182 */
183 inline const Vector2& upperGridCorner() const;
184
185 /**
186 * Sets the transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system.
187 * @param newTransformation New transformation to set
188 */
189 inline void setPlane2worldTransformation(const HomogenousMatrix4& newTransformation);
190
191 /**
192 * Sets the transformation that transforms points defined in a coordinate system of the grid into points defined in the plane coordinate system.
193 * The transformation that transforms points defined in a coordinate system of the virtual grid into points defined in the plane coordinate system is also set to the same matrix.
194 * @param newTransformation New transformation to set
195 */
196 inline void setGrid2planeTransformation(const HomogenousMatrix4& newTransformation);
197
198 /**
199 * Sets the transformation that transform points defined in a coordinate system of the virtual grid into points defined in the plane coordinate system.
200 * The transformation that transforms points defined in a coordinate system of the grid into points defined in the plane coordinate system is also set:
201 * If the given matrix is not valid, the grid2plane transformation will also be invalid.
202 * If the given matrix is valid, the grid2plane transformation will be set without the shear of the virtual transformation and a scale which is an average of the x and y scale.
203 * @param newTransformation New transformation to set
204 * @return True, if succeeded
205 */
207
208 /**
209 * Sets the lower grid corner defined in the plane coordinate system of the visualization bounding box of the grid.
210 * If the lower grid corner is set to a value higher than the upper corner, the grid will be invalid.
211 * @param newCorner New lower grid corner
212 */
213 inline void setLowerGridCorner(const Vector2& newCorner);
214
215 /**
216 * Sets the upper grid corner defined in the plane coordinate system of the visualization bounding box of the grid.
217 * If the upper grid corner is set to a value smaller than the lower corner, the grid will be invalid.
218 * @param newCorner New upper grid corner
219 */
220 inline void setUpperGridCorner(const Vector2& newCorner);
221
222 /**
223 * Returns whether the grid is valid.
224 * The grid is valid, whether the lower grid corner is smaller than the upper grid corner, the 3 transformation are valid matrices and:
225 * the x, y and z axis of the plane2world and grid2plane transformation are orthogonal to each other and the x and y axis of the virtualGrid2plane transformation are orthogonal to the z axis.
226 * @return True, if so
227 */
228 bool isValid() const;
229
230 /**
231 * Sets the grid to invalid.
232 * All transformations will be set to invalid transformations.
233 * The upper grid corner will be set higher as the lower grid corner.
234 */
236
237 /**
238 * Returns whether the physical and virtual grid transformations are identical.
239 * @return True, if so
240 */
242
243 /**
244 * Clips the grid bounding box and adjusts the lower and upper grid corner by an image bounding box.
245 * This bounding box is defined by a given camera, a pose and a border size factor.
246 * The image bounding box will have the following bounds:
247 * Left: -width * factor
248 * Right width + width * factor
249 * Top: -height * factor
250 * Bottom: height + height * factor
251 * @param pinholeCamera The pinhole camera profile to be applied
252 * @param pose The pose to be applied
253 * @param borderSizeFactor the factor for the width and height for the border around the camera frame, Range [0, infinity)
254 * @param insideGridCoordinates Optional grid coordinates that must lie inside the new clipped grid
255 */
256 void clipByImageBoundingBox(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Scalar borderSizeFactor = Scalar(2.0), const Vectors2& insideGridCoordinates = Vectors2());
257
258 /**
259 * Convert an point defined in a coordinate system of the camera image into an point on the plane defined in the world coordinate system.
260 * The grid must be valid!
261 * @param pinholeCamera The pinhole camera profile to be applied
262 * @param pose The pose to be applied
263 * @param imageCoordinate Image coordinate which will be converted
264 * @param worldCoordinate resulting world coordinate, if the conversion was successful
265 * @return True, if the conversion was successful
266 */
267 bool image2world(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector2& imageCoordinate, Vector3& worldCoordinate) const;
268
269 /**
270 * Convert an point defined in a coordinate system of the camera image into an point on the plane defined in the plane coordinate system.
271 * The grid must be valid!
272 * @param pinholeCamera The pinhole camera profile to be applied
273 * @param pose The pose to be applied
274 * @param imageCoordinate Image coordinate which will be converted
275 * @param planeCoordinate resulting plane coordinate, if the conversion was successful
276 * @return True, if the conversion was successful
277 */
278 bool image2plane(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector2& imageCoordinate, Vector3& planeCoordinate) const;
279
280 /**
281 * Convert an point defined in a coordinate system of the camera image into an point on the plane defined in the grid coordinate system.
282 * The grid must be valid!
283 * @param pinholeCamera The pinhole camera profile to be applied
284 * @param pose The pose to be applied
285 * @param imageCoordinate Image coordinate which will be converted
286 * @param gridCoordinate resulting grid coordinate, if the conversion was successful
287 * @tparam tVirtual Indicate whether the virtual grid is used
288 * @return True, if the conversion was successful
289 */
290 template <bool tVirtual>
291 bool image2grid(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector2& imageCoordinate, Vector2& gridCoordinate) const;
292
293 /**
294 * Convert an point on the plane defined in a coordinate system of the world into an point defined in the camera image coordinate system.
295 * The grid must be valid!
296 * @param pinholeCamera The pinhole camera profile to be applied
297 * @param pose The pose to be applied
298 * @param worldCoordinate World coordinate which will be converted
299 * @return resulting image coordinate
300 */
301 inline Vector2 world2image(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector3& worldCoordinate) const;
302
303 /**
304 * Convert an point on the plane defined in a coordinate system of the world into an point defined in the camera image coordinate system.
305 * The grid must be valid!
306 * @param pinholeCamera The pinhole camera profile to be applied
307 * @param poseIF Inverted flipped pose to be applied
308 * @param worldCoordinate World coordinate which will be converted
309 * @return resulting image coordinate
310 */
311 inline Vector2 world2imageIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& poseIF, const Vector3& worldCoordinate) const;
312
313 /**
314 * Convert an point on the plane defined in a coordinate system of the world into an point defined in the plane coordinate system.
315 * The grid must be valid!
316 * @param worldCoordinate World coordinate which will be converted
317 * @return resulting plane coordinate
318 */
319 inline Vector3 world2plane(const Vector3& worldCoordinate) const;
320
321 /**
322 * Convert an point on the plane defined in a coordinate system of the world into an point defined in the grid coordinate system.
323 * The grid must be valid!
324 * @param worldCoordinate World coordinate which will be converted
325 * @tparam tVirtual Indicate whether the virtual grid is used
326 * @return resulting grid coordinate
327 */
328 template <bool tVirtual>
329 inline Vector2 world2grid(const Vector3& worldCoordinate) const;
330
331 /**
332 * Convert an point on the plane defined in a coordinate system of the plane into an point defined in the camera image coordinate system.
333 * The grid must be valid!
334 * @param pinholeCamera The pinhole camera profile to be applied
335 * @param pose The pose to be applied
336 * @param planeCoordinate Plane coordinate which will be converted
337 * @return resulting image coordinate
338 */
339 inline Vector2 plane2image(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector3& planeCoordinate) const;
340
341 /**
342 * Convert an point on the plane defined in a coordinate system of the plane into an point defined in the camera image coordinate system.
343 * The grid must be valid!
344 * @param pinholeCamera The pinhole camera profile to be applied
345 * @param poseIF inverted flipped pose to be applied
346 * @param planeCoordinate Plane coordinate which will be converted
347 * @return resulting image coordinate
348 */
349 inline Vector2 plane2imageIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& poseIF, const Vector3& planeCoordinate) const;
350
351 /**
352 * Convert an point on the plane defined in a coordinate system of the plane into an point defined in the world coordinate system.
353 * The grid must be valid!
354 * @param planeCoordinate Plane coordinate which will be converted
355 * @return resulting world coordinate
356 */
357 inline Vector3 plane2world(const Vector3& planeCoordinate) const;
358
359 /**
360 * Convert an point on the plane defined in a coordinate system of the plane into an point defined in the grid coordinate system.
361 * The grid must be valid!
362 * @param planeCoordinate Plane coordinate which will be converted
363 * @tparam tVirtual Indicate whether the virtual grid is used
364 * @return resulting grid coordinate
365 */
366 template <bool tVirtual>
367 inline Vector2 plane2grid(const Vector3& planeCoordinate) const;
368
369 /**
370 * Convert an point on the plane defined in a coordinate system of the grid into an point defined in the camera image coordinate system.
371 * The grid must be valid!
372 * @param pinholeCamera The pinhole camera profile to be applied
373 * @param pose The pose to be applied
374 * @param gridCoordinate Grid coordinate which will be converted
375 * @tparam tVirtual Indicate whether the virtual grid is used
376 * @return resulting image coordinate
377 */
378 template <bool tVirtual>
379 inline Vector2 grid2image(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector2& gridCoordinate) const;
380
381 /**
382 * Convert an point on the plane defined in a coordinate system of the grid into an point defined in the camera image coordinate system.
383 * The grid must be valid!
384 * @param pinholeCamera The pinhole camera profile to be applied
385 * @param poseIF Inverted flipped pose to be applied
386 * @param gridCoordinate Grid coordinate which will be converted
387 * @tparam tVirtual Indicate whether the virtual grid is used
388 * @return resulting image coordinate
389 */
390 template <bool tVirtual>
391 inline Vector2 grid2imageIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& poseIF, const Vector2& gridCoordinate) const;
392
393 /**
394 * Convert an point on the plane defined in a coordinate system of the grid into an point defined in the world coordinate system.
395 * The grid must be valid!
396 * @param gridCoordinate Grid coordinate which will be converted
397 * @tparam tVirtual Indicate whether the virtual grid is used
398 * @return resulting world coordinate
399 */
400 template <bool tVirtual>
401 inline Vector3 grid2world(const Vector2& gridCoordinate) const;
402
403 /**
404 * Convert an point on the plane defined in a coordinate system of the grid into an point defined in the plane coordinate system.
405 * The grid must be valid!
406 * @param gridCoordinate Grid coordinate which will be converted
407 * @tparam tVirtual Indicate whether the virtual grid is used
408 * @return resulting plane coordinate
409 */
410 template <bool tVirtual>
411 inline Vector3 grid2plane(const Vector2& gridCoordinate) const;
412
413 /**
414 * Returns whether two grids are identical.
415 * @param grid Right grid
416 * @return True, if so
417 */
418 inline bool operator==(const Grid& grid) const;
419
420 /**
421 * Returns whether two grids are not identical.
422 * @param grid Right grid
423 * @return True, if so
424 */
425 inline bool operator!=(const Grid& grid) const;
426
427 /**
428 * Determine the transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system.
429 * If additionally four grid coordinates are given, the transformation that transforms points defined in a coordinate system of the grid/virtual grid into points defined in the plane coordinate system by four given image points are determined, too.
430 * The four grid coordinates must be given in the same order (clockwise or counter clockwise) like the four image points!
431 * If no grid coordinates are given, the resulting virtualGrid2plane transformation equals to the grid2plane transformation.
432 * If a previous plane2world transformation is given, the new one will be oriented at the same translation, otherwise the origin of the new plane2world transformation will be set to a distance of 1 from the camera.
433 * @param pinholeCamera The pinhole camera profile to be applied
434 * @param pose The pose to be applied
435 * @param imagePoints The four image points to determine the transformations (must be in clockwise or counterclockwise order)
436 * @param gridCoordinates Optional four grid coordinates (must be in clockwise or counterclockwise order)
437 * @param previousPlane2worldTransformation Optional previous plane2world transformation
438 * @param plane2worldTransformation Optional resulting plane2world transformation
439 * @param virtualGrid2planeTransformation Optional resulting virtualGrid2plane transformation
440 * @return True, if the Transformations could be determined
441 */
442 static bool determineVirtualGridTransformationsByFourImagePoints(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector2* imagePoints, const Vector2* gridCoordinates = nullptr, const HomogenousMatrix4* previousPlane2worldTransformation = nullptr, HomogenousMatrix4* plane2worldTransformation = nullptr, HomogenousMatrix4* virtualGrid2planeTransformation = nullptr);
443
444 /**
445 * Determines a virtualGrid2plane transformation by a plane2world transformation and four given image points.
446 * Furthermore an adjusted plane2world transformation is determined which is the previous plane2world transformation rotated around the plane normal
447 * that the bisection of the x and y axis of the virtual and non virtual grid transformation have the same orientation.
448 * @param pinholeCamera The pinhole camera profile to be applied
449 * @param pose The pose to be applied
450 * @param imagePoints The four image points to determine the transformations (must be in clockwise or counterclockwise order)
451 * @param previousPlane2worldTransformation Previous plane2world transformation
452 * @param plane2worldTransformation The new plane2world transformation that is a rotation of the previous plane2world transformation around the plane normal
453 * @param virtualGrid2planeTransformation The determined virtualGrid2plane transformation
454 * @return True, if succeeded
455 */
456 static bool determineVirtualGridTransformationByPlane2WorldTransformation(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vectors2& imagePoints, const HomogenousMatrix4& previousPlane2worldTransformation, HomogenousMatrix4& plane2worldTransformation, HomogenousMatrix4& virtualGrid2planeTransformation);
457
458 /**
459 * Determine the nearest integer grid node from a given grid coordinate.
460 * An x or y value from >= 2.5 to < 3.5 is rounded to 3.0.
461 * @param gridCoordinate Grid coordinate to get the nearest node from
462 * @return Nearest node in grid coordinates
463 */
464 static inline Vector2 grid2nearestNode(const Vector2& gridCoordinate);
465
466 /**
467 * Returns whether a given transformation has shear component in x/y direction.
468 * The x and y axes of the given transformation must be orthogonal to the z axis.
469 * @param transformation The transformation to check
470 * @return True, if so
471 */
472 static inline bool hasTransformationShearComponent(const HomogenousMatrix4& transformation);
473
474 /**
475 * Determines a transformation without a shear component from a given transformation with shear component.
476 * The x and y axes of the given transformation with shear component must be orthogonal to the z axis.
477 * The resulting transformation has the same z axis and translation like the given transformation.
478 * @param transformationWithShearComponent Transformation with shear component
479 * @param resultTransformation Resulting transformation without a shear component
480 * @return True, if succeeded
481 */
482 static bool determineTransformationWithoutShearComponent(const HomogenousMatrix4& transformationWithShearComponent, HomogenousMatrix4& resultTransformation);
483
484 /**
485 * Returns whether a given world coordinate is in front of the camera.
486 * @param pose Current camera pose
487 * @param worldCoordinate World coordinate to be checked
488 * @return True, if so
489 */
490 static inline bool isInFrontOfCamera(const HomogenousMatrix4& pose, const Vector3& worldCoordinate);
491
492 /**
493 * Returns whether a given world coordinate is in front of the camera.
494 * @param poseIF Current inverted flipped camera pose
495 * @param worldCoordinate World coordinate to be checked
496 * @return True, if so
497 */
498 static inline bool isInFrontOfCameraIF(const HomogenousMatrix4& poseIF, const Vector3& worldCoordinate);
499
500 protected:
501
502 /**
503 * Adjust the lower and upper grid corner in x direction by a given finite image line that is a projected grid edge in x direction and a finite image line of an image bounding box edge that clips the grid.
504 * Beware: The grid edge must be have a orientation of (1, 0) in grid coordinates
505 * @param pinholeCamera The pinhole camera profile to be applied
506 * @param pose The pose to be applied
507 * @param gridBoundingBoxEdgeImageCoordinate The finite image line of a grid edge in x direction
508 * @param imageBoundingBoxEdgeImageCoordinate The finite image line of image bounding box edge that clips the grid
509 */
510 void adjustGridCornersInXDirectionByImageLines(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const FiniteLine2& gridBoundingBoxEdgeImageCoordinate, const FiniteLine2& imageBoundingBoxEdgeImageCoordinate);
511
512 /**
513 * Adjust the lower and upper grid corner in y direction by a given finite image line that is a projected grid edge in y direction and a finite image line of an image bounding box edge that clips the grid.
514 * Beware: The grid edge must be have a orientation of (0, 1) in grid coordinates
515 * @param pinholeCamera The pinhole camera profile to be applied
516 * @param pose The pose to be applied
517 * @param gridBoundingBoxEdgeImageCoordinate The finite image line of a grid edge in y direction
518 * @param imageBoundingBoxEdgeImageCoordinate The finite image line of image bounding box edge that clips the grid
519 */
520 void adjustGridCornersInYDirectionByImageLines(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const FiniteLine2& gridBoundingBoxEdgeImageCoordinate, const FiniteLine2& imageBoundingBoxEdgeImageCoordinate);
521
522 /**
523 * Clips a finite line in world coordinates, that the start and end point lie in front of the camera.
524 * A binary search is determine the possible start and end point who are in front of the camera.
525 * If both points lie behind the camera, the line is not adjusted.
526 * @param poseIF Inverted flipped pose to be applied
527 * @param lineToClip The finite line to clip
528 * @param resultingLine The resulting clipped line if succeeded
529 * @return True, if succeeded
530 */
531 static bool clipWorldLineInFrontOfCameraBinarySearchIF(const HomogenousMatrix4& poseIF, const FiniteLine3& lineToClip, FiniteLine3& resultingLine);
532
533 protected:
534
535 /// Transformation that transforms points defined in a coordinate system of the plane into points defined in the world coordinate system.
537
538 /// Transformation that transforms points defined in a coordinate system of the grid into points defined in the plane coordinate system.
540
541 /// Transformation that transform points defined in a coordinate system of the virtual grid into points defined in the plane coordinate system.
543
544 /// Lower grid corner of the visualization bounding box of the grid defined in the grid coordinate system.
546
547 /// Upper grid corner of the visualization bounding box of the grid defined in the grid coordinate system .
549};
550
551inline Grid::Grid() :
552 worldTplane(false),
553 planeTgrid(false),
554 planeTvirtualGrid(false),
555 gridLowerCorner(1, 1),
556 gridUpperCorner(-1, -1)
557{
558 // nothing to do here
559}
560
561inline Grid::Grid(const HomogenousMatrix4& plane2worldTransformation, const HomogenousMatrix4& virtualGrid2planeTransformation, const Vector2& lowerCorner, const Vector2& upperCorner) :
562 worldTplane(plane2worldTransformation),
563 gridLowerCorner(lowerCorner),
564 gridUpperCorner(upperCorner)
565{
566 // sets the physical and virtual grid2plane transformation
568}
569
571{
572 return worldTplane;
573}
574
576{
577 return planeTgrid;
578}
579
584
589
594
595inline const Vector2& Grid::lowerGridCorner() const
596{
597 return gridLowerCorner;
598}
599
600inline const Vector2& Grid::upperGridCorner() const
601{
602 return gridUpperCorner;
603}
604
605inline void Grid::setPlane2worldTransformation(const HomogenousMatrix4& newTransformation)
606{
607 ocean_assert(newTransformation.isValid());
608
609 worldTplane = newTransformation;
610}
611
612inline void Grid::setGrid2planeTransformation(const HomogenousMatrix4& newTransformation)
613{
614 ocean_assert(newTransformation.isValid());
615
616 planeTgrid = newTransformation;
617 planeTvirtualGrid = newTransformation;
618}
619
620inline void Grid::setLowerGridCorner(const Vector2& newCorner)
621{
622 gridLowerCorner = newCorner;
623}
624
625inline void Grid::setUpperGridCorner(const Vector2& newCorner)
626{
627 gridUpperCorner = newCorner;
628}
629
630template <bool tVirtual>
631bool Grid::image2grid(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector2& imageCoordinate, Vector2& gridCoordinate) const
632{
633 ocean_assert(isValid());
634 ocean_assert(pinholeCamera.isValid() && pose.isValid());
635
636 Vector3 planeCoordinate;
637 if (!image2plane(pinholeCamera, pose, imageCoordinate, planeCoordinate))
638 return false;
639
640 gridCoordinate = plane2grid<tVirtual>(planeCoordinate);
641
642 return true;
643}
644
645inline Vector2 Grid::world2image(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector3& worldCoordinate) const
646{
647 ocean_assert(isValid());
648 ocean_assert(pinholeCamera.isValid() && pose.isValid());
649 //ocean_assert(Plane3(worldTplane).isInPlane(worldCoordinate, Numeric::eps()));
650
651 return pinholeCamera.projectToImageDamped(pose, worldCoordinate, pinholeCamera.hasDistortionParameters());
652}
653
654inline Vector2 Grid::world2imageIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& poseIF, const Vector3& worldCoordinate) const
655{
656 ocean_assert(isValid());
657 ocean_assert(pinholeCamera.isValid() && poseIF.isValid());
658 //ocean_assert(Plane3(worldTplane).isInPlane(worldCoordinate, Numeric::eps()));
659
660 return pinholeCamera.projectToImageDampedIF(poseIF, worldCoordinate, pinholeCamera.hasDistortionParameters());
661}
662
663inline Vector3 Grid::world2plane(const Vector3& worldCoordinate) const
664{
665 ocean_assert(isValid());
666 ocean_assert(Plane3(worldTplane).isInPlane(worldCoordinate, Numeric::eps()));
667
668 return worldTplane.inverted() * worldCoordinate;
669}
670
671template <bool tVirtual>
672inline Vector2 Grid::world2grid(const Vector3& worldCoordinate) const
673{
674 ocean_assert(isValid());
675
676 return plane2grid<tVirtual>(world2plane(worldCoordinate));
677}
678
679inline Vector2 Grid::plane2image(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector3& planeCoordinate) const
680{
681 ocean_assert(isValid());
682 ocean_assert(pinholeCamera.isValid() && pose.isValid());
683
684 return world2image(pinholeCamera, pose, plane2world(planeCoordinate));
685}
686
687inline Vector2 Grid::plane2imageIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& poseIF, const Vector3& planeCoordinate) const
688{
689 ocean_assert(isValid());
690 ocean_assert(pinholeCamera.isValid() && poseIF.isValid());
691
692 return world2imageIF(pinholeCamera, poseIF, plane2world(planeCoordinate));
693}
694
695inline Vector3 Grid::plane2world(const Vector3& planeCoordinate) const
696{
697 ocean_assert(isValid());
698
699 return worldTplane * planeCoordinate;
700}
701
702template <bool tVirtual>
703inline Vector2 Grid::plane2grid(const Vector3& planeCoordinate) const
704{
705 ocean_assert(isValid());
706
707 const Vector3 gridCoordinate = tVirtual ? planeTvirtualGrid.inverted() * planeCoordinate : planeTgrid.inverted() * planeCoordinate;
708 //ocean_assert(Numeric::isEqualEps(gridCoordinate.z()));
709
710 return Vector2(gridCoordinate.x(), gridCoordinate.y());
711}
712
713template <bool tVirtual>
714inline Vector2 Grid::grid2image(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& pose, const Vector2& gridCoordinate) const
715{
716 ocean_assert(isValid());
717 ocean_assert(pinholeCamera.isValid() && pose.isValid());
718
719 return world2image(pinholeCamera, pose, grid2world<tVirtual>(gridCoordinate));
720}
721
722template <bool tVirtual>
723inline Vector2 Grid::grid2imageIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& poseIF, const Vector2& gridCoordinate) const
724{
725 ocean_assert(isValid());
726 ocean_assert(pinholeCamera.isValid() && poseIF.isValid());
727
728 return world2imageIF(pinholeCamera, poseIF, grid2world<tVirtual>(gridCoordinate));
729}
730
731template <bool tVirtual>
732inline Vector3 Grid::grid2world(const Vector2& gridCoordinate) const
733{
734 ocean_assert(isValid());
735
736 return plane2world(grid2plane<tVirtual>(gridCoordinate));
737}
738
739template <bool tVirtual>
740inline Vector3 Grid::grid2plane(const Vector2& gridCoordinate) const
741{
742 ocean_assert(isValid());
743
744 if constexpr (tVirtual)
745 return planeTvirtualGrid * Vector3(gridCoordinate, 0);
746 else
747 return planeTgrid * Vector3(gridCoordinate, 0);
748}
749
750inline bool Grid::operator==(const Grid& grid) const
751{
754}
755
756inline bool Grid::operator!=(const Grid& grid) const
757{
758 return !(*this == grid);
759}
760
761inline Vector2 Grid::grid2nearestNode(const Vector2& gridCoordinate)
762{
763 return Vector2(Scalar(Numeric::round32(gridCoordinate.x())), Scalar(Numeric::round32(gridCoordinate.y())));
764}
765
767{
768 return !transformation.xAxis().isOrthogonal(transformation.yAxis());
769}
770
771inline bool Grid::isInFrontOfCamera(const HomogenousMatrix4& pose, const Vector3& worldCoordinate)
772{
774}
775
776inline bool Grid::isInFrontOfCameraIF(const HomogenousMatrix4& poseIF, const Vector3& worldCoordinate)
777{
778 return (poseIF * worldCoordinate).z() > Scalar(0.0001);
779}
780
781}
782
783}
784
785#endif // META_OCEAN_GEOMETRY_GRID_H
static HomogenousMatrixT4< U > standard2InvertedFlipped(const HomogenousMatrixT4< U > &world_T_camera)
Transforms a standard homogenous 4x4 viewing (extrinsic camera) matrix into an inverted and flipped c...
Definition Camera.h:734
This class implements an finite line in 3D space.
Definition FiniteLine3.h:66
This class implements the basic data of an planar grid.
Definition Grid.h:74
Vector3 grid2plane(const Vector2 &gridCoordinate) const
Convert an point on the plane defined in a coordinate system of the grid into an point defined in the...
Definition Grid.h:740
bool operator!=(const Grid &grid) const
Returns whether two grids are not identical.
Definition Grid.h:756
const HomogenousMatrix4 & virtualGrid2planeTransformation() const
Returns the transformation that transform points defined in a coordinate system of the virtual grid i...
Definition Grid.h:580
void adjustGridCornersInYDirectionByImageLines(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const FiniteLine2 &gridBoundingBoxEdgeImageCoordinate, const FiniteLine2 &imageBoundingBoxEdgeImageCoordinate)
Adjust the lower and upper grid corner in y direction by a given finite image line that is a projecte...
static bool isInFrontOfCameraIF(const HomogenousMatrix4 &poseIF, const Vector3 &worldCoordinate)
Returns whether a given world coordinate is in front of the camera.
Definition Grid.h:776
Grid()
Creates a new invalid grid element.
Definition Grid.h:551
Vector2 plane2grid(const Vector3 &planeCoordinate) const
Convert an point on the plane defined in a coordinate system of the plane into an point defined in th...
Definition Grid.h:703
static Vector2 grid2nearestNode(const Vector2 &gridCoordinate)
Determine the nearest integer grid node from a given grid coordinate.
Definition Grid.h:761
const HomogenousMatrix4 & plane2worldTransformation() const
Returns the transformation that transforms points defined in a coordinate system of the plane into po...
Definition Grid.h:570
bool image2plane(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vector2 &imageCoordinate, Vector3 &planeCoordinate) const
Convert an point defined in a coordinate system of the camera image into an point on the plane define...
void setInvalid()
Sets the grid to invalid.
static bool clipWorldLineInFrontOfCameraBinarySearchIF(const HomogenousMatrix4 &poseIF, const FiniteLine3 &lineToClip, FiniteLine3 &resultingLine)
Clips a finite line in world coordinates, that the start and end point lie in front of the camera.
Vector2 world2imageIF(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &poseIF, const Vector3 &worldCoordinate) const
Convert an point on the plane defined in a coordinate system of the world into an point defined in th...
Definition Grid.h:654
Vector3 world2plane(const Vector3 &worldCoordinate) const
Convert an point on the plane defined in a coordinate system of the world into an point defined in th...
Definition Grid.h:663
HomogenousMatrix4 virtualPlane2worldTransformation() const
Returns a transformation that transforms points defined in a coordinate system of the virtual plane i...
static bool determineTransformationWithoutShearComponent(const HomogenousMatrix4 &transformationWithShearComponent, HomogenousMatrix4 &resultTransformation)
Determines a transformation without a shear component from a given transformation with shear componen...
void setLowerGridCorner(const Vector2 &newCorner)
Sets the lower grid corner defined in the plane coordinate system of the visualization bounding box o...
Definition Grid.h:620
Vector3 plane2world(const Vector3 &planeCoordinate) const
Convert an point on the plane defined in a coordinate system of the plane into an point defined in th...
Definition Grid.h:695
Vector2 gridLowerCorner
Lower grid corner of the visualization bounding box of the grid defined in the grid coordinate system...
Definition Grid.h:545
Vector2 grid2imageIF(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &poseIF, const Vector2 &gridCoordinate) const
Convert an point on the plane defined in a coordinate system of the grid into an point defined in the...
Definition Grid.h:723
bool image2grid(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vector2 &imageCoordinate, Vector2 &gridCoordinate) const
Convert an point defined in a coordinate system of the camera image into an point on the plane define...
Definition Grid.h:631
bool areVirtualAndPhysicalGridIdentical() const
Returns whether the physical and virtual grid transformations are identical.
bool isValid() const
Returns whether the grid is valid.
Vector2 world2grid(const Vector3 &worldCoordinate) const
Convert an point on the plane defined in a coordinate system of the world into an point defined in th...
Definition Grid.h:672
static bool determineVirtualGridTransformationByPlane2WorldTransformation(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vectors2 &imagePoints, const HomogenousMatrix4 &previousPlane2worldTransformation, HomogenousMatrix4 &plane2worldTransformation, HomogenousMatrix4 &virtualGrid2planeTransformation)
Determines a virtualGrid2plane transformation by a plane2world transformation and four given image po...
static bool determineVirtualGridTransformationsByFourImagePoints(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vector2 *imagePoints, const Vector2 *gridCoordinates=nullptr, const HomogenousMatrix4 *previousPlane2worldTransformation=nullptr, HomogenousMatrix4 *plane2worldTransformation=nullptr, HomogenousMatrix4 *virtualGrid2planeTransformation=nullptr)
Determine the transformation that transforms points defined in a coordinate system of the plane into ...
void clipByImageBoundingBox(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Scalar borderSizeFactor=Scalar(2.0), const Vectors2 &insideGridCoordinates=Vectors2())
Clips the grid bounding box and adjusts the lower and upper grid corner by an image bounding box.
const Vector2 & lowerGridCorner() const
Returns the lower grid corner defined in the grid coordinate system of the visualization bounding box...
Definition Grid.h:595
void adjustGridCornersInXDirectionByImageLines(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const FiniteLine2 &gridBoundingBoxEdgeImageCoordinate, const FiniteLine2 &imageBoundingBoxEdgeImageCoordinate)
Adjust the lower and upper grid corner in x direction by a given finite image line that is a projecte...
HomogenousMatrix4 virtualGrid2worldTransformation() const
Returns the transformation that transform points defined in a coordinate system of the virtual grid i...
Definition Grid.h:590
Vector3 grid2world(const Vector2 &gridCoordinate) const
Convert an point on the plane defined in a coordinate system of the grid into an point defined in the...
Definition Grid.h:732
bool setVirtualGrid2planeTransformation(const HomogenousMatrix4 &newTransformation)
Sets the transformation that transform points defined in a coordinate system of the virtual grid into...
static bool isInFrontOfCamera(const HomogenousMatrix4 &pose, const Vector3 &worldCoordinate)
Returns whether a given world coordinate is in front of the camera.
Definition Grid.h:771
bool image2world(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vector2 &imageCoordinate, Vector3 &worldCoordinate) const
Convert an point defined in a coordinate system of the camera image into an point on the plane define...
Vector2 world2image(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vector3 &worldCoordinate) const
Convert an point on the plane defined in a coordinate system of the world into an point defined in th...
Definition Grid.h:645
Vector2 gridUpperCorner
Upper grid corner of the visualization bounding box of the grid defined in the grid coordinate system...
Definition Grid.h:548
void setGrid2planeTransformation(const HomogenousMatrix4 &newTransformation)
Sets the transformation that transforms points defined in a coordinate system of the grid into points...
Definition Grid.h:612
const Vector2 & upperGridCorner() const
Returns the upper grid corner defined in the grid coordinate system of the visualization bounding box...
Definition Grid.h:600
Grid(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const HomogenousMatrix4 &plane2worldTransformation, const Vectors2 &imagePoints, const bool clipBoundingBox=false)
Creates a new Grid by a given transformation that transforms points defined in a coordinate system of...
const HomogenousMatrix4 & grid2planeTransformation() const
Returns the transformation that transforms points defined in a coordinate system of the grid into poi...
Definition Grid.h:575
Grid(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vectors2 &imagePoints, const HomogenousMatrix4 &plane2worldTransformation, const HomogenousMatrix4 &virtualGrid2planeTransformation=HomogenousMatrix4(true), const bool clipBoundingBox=false)
Creates a new Grid by a plane2world transformation and four image points defining a rectangle.
void setPlane2worldTransformation(const HomogenousMatrix4 &newTransformation)
Sets the transformation that transforms points defined in a coordinate system of the plane into point...
Definition Grid.h:605
bool operator==(const Grid &grid) const
Returns whether two grids are identical.
Definition Grid.h:750
Grid(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vectors2 &imagePoints, const Vector2 *gridCoordinates=nullptr, const HomogenousMatrix4 *previousPlane2worldTransformation=nullptr, const bool clipBoundingBox=false)
Creates a new Grid by four given image points using vanishing projection.
void setUpperGridCorner(const Vector2 &newCorner)
Sets the upper grid corner defined in the plane coordinate system of the visualization bounding box o...
Definition Grid.h:625
HomogenousMatrix4 grid2worldTransformation() const
Returns the transformation that transform points defined in a coordinate system of the grid into poin...
Definition Grid.h:585
static bool hasTransformationShearComponent(const HomogenousMatrix4 &transformation)
Returns whether a given transformation has shear component in x/y direction.
Definition Grid.h:766
Vector2 plane2image(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vector3 &planeCoordinate) const
Convert an point on the plane defined in a coordinate system of the plane into an point defined in th...
Definition Grid.h:679
Vector2 plane2imageIF(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &poseIF, const Vector3 &planeCoordinate) const
Convert an point on the plane defined in a coordinate system of the plane into an point defined in th...
Definition Grid.h:687
HomogenousMatrix4 planeTvirtualGrid
Transformation that transform points defined in a coordinate system of the virtual grid into points d...
Definition Grid.h:542
HomogenousMatrix4 worldTplane
Transformation that transforms points defined in a coordinate system of the plane into points defined...
Definition Grid.h:536
Vector2 grid2image(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const Vector2 &gridCoordinate) const
Convert an point on the plane defined in a coordinate system of the grid into an point defined in the...
Definition Grid.h:714
HomogenousMatrix4 planeTgrid
Transformation that transforms points defined in a coordinate system of the grid into points defined ...
Definition Grid.h:539
HomogenousMatrixT4< T > inverted() const noexcept
Returns the inverted of this matrix.
Definition HomogenousMatrix4.h:1575
VectorT3< T > yAxis() const
Returns the y-axis of the transformation which is the second vector of the upper left 3x3 rotation ma...
Definition HomogenousMatrix4.h:1367
VectorT3< T > xAxis() const
Returns the x-axis of the transformation which is the first vector of the upper left 3x3 rotation mat...
Definition HomogenousMatrix4.h:1360
bool isValid() const
Returns whether this matrix is a valid homogeneous transformation.
Definition HomogenousMatrix4.h:1806
static constexpr int32_t round32(const T value)
Returns the rounded 32 bit integer value of a given value.
Definition Numeric.h:2064
static constexpr T eps()
Returns a small epsilon.
VectorT2< T > projectToImageDamped(const HomogenousMatrixT4< T > &extrinsic, const VectorT3< T > &objectPoint, const bool distortImagePoint, const T dampingFactor=T(1), const T zoom=T(1)) const
Projects a 3D object point to the 2D image plane of the camera by a given extrinsic camera matrix.
Definition PinholeCamera.h:2085
bool isValid() const
Returns whether this camera is valid.
Definition PinholeCamera.h:1572
bool hasDistortionParameters() const
Returns whether this camera object has specified distortion parameters.
Definition PinholeCamera.h:1293
VectorT2< T > projectToImageDampedIF(const HomogenousMatrixT4< T > &iFlippedExtrinsic, const VectorT3< T > &objectPoint, const bool distortImagePoint, const T dampingFactor=T(1), const T zoom=T(1)) const
Projects a 3D object point to the 2D image plane of the camera by a given inverse extrinsic camera ma...
const T & x() const noexcept
Returns the x value.
Definition Vector2.h:710
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:722
const T & y() const noexcept
Returns the y value.
Definition Vector3.h:824
bool isOrthogonal(const VectorT3< T > &right) const
Returns whether two vectors are orthogonal.
Definition Vector3.h:806
const T & x() const noexcept
Returns the x value.
Definition Vector3.h:812
float Scalar
Definition of a scalar type.
Definition Math.h:129
VectorT3< Scalar > Vector3
Definition of a 3D vector.
Definition Vector3.h:29
PlaneT3< Scalar > Plane3
Definition of the Plane3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single ...
Definition Plane3.h:31
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition Vector2.h:64
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition Vector2.h:28
The namespace covering the entire Ocean framework.
Definition Accessor.h:15