ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvCameraSensor.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // local
11 #include "LineSet.h"
12 #include "ecvOctree.h"
13 #include "ecvSensor.h"
14 
15 // system
16 #include <unordered_set>
17 
18 class ccPointCloud;
19 class ccMesh;
20 class ccImage;
21 class QDir;
22 
25 public: // general
30 
34 
35  float vertFocal_pix;
37  float pixelSize_mm[2];
39  float skew;
40  float vFOV_rad;
41  float zNear_mm;
42  float zFar_mm;
43  int arrayWidth;
45  float principal_point[2];
48 
50  inline float horizFocal_pix() const {
51  assert(pixelSize_mm[1] > 0);
52  return (vertFocal_pix * pixelSize_mm[0]) / pixelSize_mm[1];
53  }
54  };
55 
58  NO_DISTORTION_MODEL = 0,
59  SIMPLE_RADIAL_DISTORTION =
60  1,
61  BROWN_DISTORTION =
62  2,
63  EXTENDED_RADIAL_DISTORTION =
64  3
65  };
66 
70  typedef QSharedPointer<LensDistortionParameters> Shared;
71 
74 
76  virtual DistortionModel getModel() const = 0;
77  };
78 
82  typedef QSharedPointer<RadialDistortionParameters> Shared;
83 
85  RadialDistortionParameters() : k1(0), k2(0) {}
86 
87  // inherited from LensDistortionParameters
88  inline virtual DistortionModel getModel() const override {
89  return SIMPLE_RADIAL_DISTORTION;
90  }
91 
93  float k1;
95  float k2;
96  };
97 
102  typedef QSharedPointer<RadialDistortionParameters> Shared;
103 
106  : RadialDistortionParameters(), k3(0) {}
107 
108  // inherited from LensDistortionParameters
109  inline virtual DistortionModel getModel() const override {
110  return EXTENDED_RADIAL_DISTORTION;
111  }
112 
114  float k3;
115  };
116 
118 
126  typedef QSharedPointer<BrownDistortionParameters> Shared;
127 
130 
131  // inherited from LensDistortionParameters
132  inline virtual DistortionModel getModel() const override {
133  return BROWN_DISTORTION;
134  }
135 
139 
140  float principalPointOffset[2];
142  float linearDisparityParams[2];
145  float K_BrownParams[3];
147  float P_BrownParams[2];
149  };
150 
152 
159 
161 
165 
169 
176  };
177 
185 
187  virtual ~ccCameraSensor() override;
188 
189  // inherited from ccHObject
190  virtual CV_CLASS_ENUM getClassID() const override {
192  }
193  virtual bool isSerializable() const override { return true; }
194  virtual ccBBox getOwnBB(bool withGLFeatures = false) override;
195  virtual ccBBox getOwnFitBB(ccGLMatrix& trans) override;
196 
197  // inherited from ccSensor
198  virtual bool applyViewport() override;
199 
200 public: // getters and setters
202 
204  void setVertFocal_pix(float vertFocal_pix);
206  inline float getVertFocal_pix() const {
207  return m_intrinsicParams.vertFocal_pix;
208  }
210  inline float getHorizFocal_pix() const {
211  return m_intrinsicParams.horizFocal_pix();
212  }
213 
215  void setVerticalFov_rad(float fov_rad);
217  inline float getVerticalFov_rad() const {
218  return m_intrinsicParams.vFOV_rad;
219  }
220 
223  return m_intrinsicParams;
224  }
227 
230  return m_distortionParams;
231  }
234  m_distortionParams = params;
235  }
236 
238 
243 
244 public: // frustum display
246  inline bool frustumIsDrawn() const { return m_frustumInfos.drawFrustum; }
247 
249  inline void drawFrustum(bool state) { m_frustumInfos.drawFrustum = state; }
250 
252  inline bool frustumPlanesAreDrawn() const {
253  return m_frustumInfos.drawSidePlanes;
254  }
255 
257  inline void drawFrustumPlanes(bool state) {
258  m_frustumInfos.drawSidePlanes = state;
259  }
260 
261 public: // coordinate systems conversion methods
264 
268  bool fromLocalCoordToGlobalCoord(const CCVector3& localCoord,
269  CCVector3& globalCoord) const;
270 
273 
277  bool fromGlobalCoordToLocalCoord(const CCVector3& globalCoord,
278  CCVector3& localCoord) const;
279 
282 
290  bool fromLocalCoordToImageCoord(const CCVector3& localCoord,
291  CCVector2& imageCoord,
292  bool withLensError = true) const;
293 
296 
305  bool fromImageCoordToLocalCoord(const CCVector2& imageCoord,
306  CCVector3& localCoord,
307  PointCoordinateType depth,
308  bool withLensCorrection = true) const;
309 
312 
320  bool fromGlobalCoordToImageCoord(const CCVector3& globalCoord,
321  CCVector2& imageCoord,
322  bool withLensError = true) const;
323 
326 
334  bool fromImageCoordToGlobalCoord(const CCVector2& imageCoord,
335  CCVector3& globalCoord,
337  bool withLensCorrection = true) const;
338 
341 
349  CCVector2& ideal) const;
350 
353 
358  // TODO
359  // bool fromIdealImCoordToRealImCoord(const CCVector2& ideal, CCVector2&
360  // real) const;
361 
362 public: // orthorectification tools
365  struct KeyPoint {
367  float x;
369  float y;
371  unsigned index;
372 
374  KeyPoint() : x(0), y(0), index(0) {}
375 
377  KeyPoint(float Px, float Py, unsigned indexInCloud)
378  : x(Px), y(Py), index(indexInCloud) {}
379  };
380 
382 
389  const ccImage* image,
391  std::vector<KeyPoint>& keypointsImage) const;
392 
394 
406  std::vector<KeyPoint>& keypointsImage,
407  double& pixelSize,
408  double* minCorner = nullptr,
409  double* maxCorner = nullptr,
410  double* realCorners = nullptr) const;
411 
413 
425  PointCoordinateType altitude,
426  double& pixelSize,
427  bool undistortImages = true,
428  double* minCorner = nullptr,
429  double* maxCorner = nullptr,
430  double* realCorners = nullptr) const;
431 
433 
443  static bool OrthoRectifyAsImages(
444  std::vector<ccImage*> images,
445  double a[],
446  double b[],
447  double c[],
448  unsigned maxSize,
449  QDir* outputDir = nullptr,
450  std::vector<ccImage*>* orthoRectifiedImages = nullptr,
451  std::vector<std::pair<double, double>>* relativePos = nullptr);
452 
454 
467  const ccImage* image,
469  std::vector<KeyPoint>& keypointsImage,
470  double a[3],
471  double b[3],
472  double c[3]) const;
473 
474 public: // misc
477 
486  bool computeUncertainty(const CCVector2& pixel,
487  const float depth,
488  Vector3Tpl<ScalarType>& sigma) const;
489 
492 
499  std::vector<Vector3Tpl<ScalarType>>&
500  accuracy /*, bool lensDistortion*/);
501 
503 
507  QImage undistort(const QImage& image) const;
508 
510 
516  ccImage* undistort(ccImage* image, bool inplace = true) const;
517 
519 
524  const CCVector3& globalCoord /*, bool withLensCorrection*/) const;
525 
529 
535  bool computeGlobalPlaneCoefficients(float planeCoefficients[6][4],
536  CCVector3 ptsFrustum[8],
537  CCVector3 edges[6],
538  CCVector3& center);
539 
541  return m_nearPlane;
542  }
544  return m_sideLines;
545  }
546  const cloudViewer::geometry::LineSet& getArrow() const { return m_arrow; }
547  const cloudViewer::geometry::LineSet& getAxis() const { return m_axis; }
548 
549  void setPlaneColor(ecvColor::Rgb color) { m_plane_color = color; }
550  const ecvColor::Rgb& getPlaneColor() const { return m_plane_color; }
551 
552  double getFocalLength() const { return m_focalLength; }
553 
554  virtual void clearDrawings() override;
555  virtual void hideShowDrawings(CC_DRAW_CONTEXT& context) override;
556 
557 public: // helpers
559  static float ConvertFocalPixToMM(float focal_pix, float ccdPixelSize_mm);
560 
562  static float ConvertFocalMMToPix(float focal_mm, float ccdPixelSize_mm);
563 
565  static float ComputeFovRadFromFocalPix(float focal_pix, int imageSize_pix);
566 
568  static float ComputeFovRadFromFocalMm(float focal_mm, float ccdSize_mm);
569 
570  void updateData();
571 
572 protected:
575 
578 
580 
583 
584  // Inherited from ccHObject
585  bool toFile_MeOnly(QFile& out, short dataVersion) const override;
586  short minimumFileVersion_MeOnly() const override;
587  bool fromFile_MeOnly(QFile& in,
588  short dataVersion,
589  int flags,
590  LoadedIDMap& oldToNewIDMap) override;
591  virtual void drawMeOnly(CC_DRAW_CONTEXT& context) override;
592 
594 
597 
600 
602 
605 
610 
616 };
617 
619 public:
621 
630  };
631 
634 
637 
640  cloudViewer::DgmOctree::CellCode truncatedCode,
641  unsigned char level) const {
642  assert(m_associatedOctree);
643 
644  std::unordered_set<cloudViewer::DgmOctree::CellCode>::const_iterator
645  got = m_cellsInFrustum[level].find(truncatedCode);
646  if (got != m_cellsInFrustum[level].end()) return CELL_INSIDE_FRUSTUM;
647  got = m_cellsIntersectFrustum[level].find(truncatedCode);
648  if (got != m_cellsIntersectFrustum[level].end())
649  return CELL_INTERSECT_FRUSTUM;
650  return CELL_OUTSIDE_FRUSTUM;
651  }
652 
655 
670  std::vector<std::pair<unsigned, CCVector3>>& pointsToTest,
671  std::vector<unsigned>& inCameraFrustum,
672  const float planesCoefficients[6][4],
673  const CCVector3 ptsFrustum[8],
674  const CCVector3 edges[6],
675  const CCVector3& center);
676 
679 
691  unsigned char level,
692  cloudViewer::DgmOctree::CellCode parentTruncatedCode,
693  OctreeCellVisibility parentResult,
694  const float planesCoefficients[6][4],
695  const CCVector3 ptsFrustum[8],
696  const CCVector3 edges[6],
697  const CCVector3& center);
698 
700 
714  const CCVector3& bbMin,
715  const CCVector3& bbMax,
716  const float planesCoefficients[6][4],
717  const CCVector3 frustumCorners[8],
718  const CCVector3 frustumEdges[6],
719  const CCVector3& frustumCenter);
720 
721 protected:
723 
724  // contains the truncated code of the cells built in the octree
725  std::unordered_set<cloudViewer::DgmOctree::CellCode>
727  // contains the truncated code of the cells INSIDE the frustum
728  std::unordered_set<cloudViewer::DgmOctree::CellCode>
730  // contains the truncated code of the cells INTERSECTING the frustum
731  std::unordered_set<cloudViewer::DgmOctree::CellCode>
733  1];
734 };
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
int64_t CV_CLASS_ENUM
Type of object type flags (64 bits)
Definition: CVTypes.h:97
#define CV_DB_LIB_API
Definition: CV_db.h:15
std::shared_ptr< core::Tensor > image
int points
math::float4 color
cmdLineReadable * params[]
Bounding box structure.
Definition: ecvBBox.h:25
Camera (projective) sensor.
const ecvColor::Rgb & getPlaneColor() const
ccImage * orthoRectifyAsImage(const ccImage *image, cloudViewer::GenericIndexedCloud *keypoints3D, std::vector< KeyPoint > &keypointsImage, double &pixelSize, double *minCorner=nullptr, double *maxCorner=nullptr, double *realCorners=nullptr) const
Projective ortho-rectification of an image (as image)
static float ConvertFocalPixToMM(float focal_pix, float ccdPixelSize_mm)
Helper: converts camera focal from pixels to mm.
bool computeGlobalPlaneCoefficients(float planeCoefficients[6][4], CCVector3 ptsFrustum[8], CCVector3 edges[6], CCVector3 &center)
ccCameraSensor()
Default constructor.
FrustumInformation m_frustumInfos
Frustum information structure.
bool fromImageCoordToLocalCoord(const CCVector2 &imageCoord, CCVector3 &localCoord, PointCoordinateType depth, bool withLensCorrection=true) const
ecvColor::Rgb m_plane_color
void computeProjectionMatrix()
Compute the projection matrix (from intrinsic parameters)
LensDistortionParameters::Shared m_distortionParams
Lens distortion parameters.
virtual CV_CLASS_ENUM getClassID() const override
Returns class ID.
void setVerticalFov_rad(float fov_rad)
Sets the (vertical) field of view in radians.
bool computeOrthoRectificationParams(const ccImage *image, cloudViewer::GenericIndexedCloud *keypoints3D, std::vector< KeyPoint > &keypointsImage, double a[3], double b[3], double c[3]) const
Computes ortho-rectification parameters for a given image.
float getVerticalFov_rad() const
Returns the (vertical) field of view in radians.
const cloudViewer::geometry::LineSet & getNearPlane() const
cloudViewer::geometry::LineSet m_arrow
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
ccPointCloud * orthoRectifyAsCloud(const ccImage *image, cloudViewer::GenericIndexedCloud *keypoints3D, std::vector< KeyPoint > &keypointsImage) const
Projective ortho-rectification of an image (as cloud)
bool computeFrustumCorners()
Computes the eight corners of the frustum.
bool frustumIsDrawn() const
Returns whether the frustum should be displayed or not.
ccImage * orthoRectifyAsImageDirect(const ccImage *image, PointCoordinateType altitude, double &pixelSize, bool undistortImages=true, double *minCorner=nullptr, double *maxCorner=nullptr, double *realCorners=nullptr) const
Direct ortho-rectification of an image (as image)
float getVertFocal_pix() const
Returns vertical focal (in pixels)
bool fromGlobalCoordToImageCoord(const CCVector3 &globalCoord, CCVector2 &imageCoord, bool withLensError=true) const
CCVector3 computeUpperLeftPoint() const
Used internally for display.
void drawFrustumPlanes(bool state)
Sets whether the frustum planes should be displayed or not.
const cloudViewer::geometry::LineSet & getSideLines() const
static float ConvertFocalMMToPix(float focal_mm, float ccdPixelSize_mm)
Helper: converts camera focal from mm to pixels.
void setVertFocal_pix(float vertFocal_pix)
Sets focal (in pixels)
bool frustumPlanesAreDrawn() const
Returns whether the frustum planes should be displayed or not.
short minimumFileVersion_MeOnly() const override
bool isGlobalCoordInFrustum(const CCVector3 &globalCoord) const
Tests if a 3D point is in the field of view of the camera.
void setIntrinsicParameters(const IntrinsicParameters &params)
Sets intrinsic parameters.
static float ComputeFovRadFromFocalMm(float focal_mm, float ccdSize_mm)
Helper: deduces camera f.o.v. (in radians) from focal (in mm)
const IntrinsicParameters & getIntrinsicParameters() const
Returns intrinsic parameters.
bool fromGlobalCoordToLocalCoord(const CCVector3 &globalCoord, CCVector3 &localCoord) const
cloudViewer::geometry::LineSet m_nearPlane
float getHorizFocal_pix() const
Returns horizontal focal (in pixels)
const cloudViewer::geometry::LineSet & getArrow() const
cloudViewer::geometry::LineSet m_axis
virtual ~ccCameraSensor() override
Destructor.
bool fromLocalCoordToGlobalCoord(const CCVector3 &localCoord, CCVector3 &globalCoord) const
void setPlaneColor(ecvColor::Rgb color)
static float ComputeFovRadFromFocalPix(float focal_pix, int imageSize_pix)
Helper: deduces camera f.o.v. (in radians) from focal (in pixels)
virtual void hideShowDrawings(CC_DRAW_CONTEXT &context) override
virtual ccBBox getOwnFitBB(ccGLMatrix &trans) override
Returns best-fit bounding-box (if available)
virtual void drawMeOnly(CC_DRAW_CONTEXT &context) override
Draws the entity only (not its children)
cloudViewer::geometry::LineSet m_sideLines
const cloudViewer::geometry::LineSet & getAxis() const
const LensDistortionParameters::Shared & getDistortionParameters() const
Returns uncertainty parameters.
virtual bool applyViewport() override
Apply sensor 'viewport' to a 3D view.
virtual ccBBox getOwnBB(bool withGLFeatures=false) override
Returns the entity's own bounding-box.
bool getProjectionMatrix(ccGLMatrix &matrix)
Returns the camera projection matrix.
ccImage * undistort(ccImage *image, bool inplace=true) const
Undistorts an image based on the sensor distortion parameters.
IntrinsicParameters m_intrinsicParams
Camera intrinsic parameters.
ccCameraSensor(const IntrinsicParameters &iParams)
bool computeUncertainty(const CCVector2 &pixel, const float depth, Vector3Tpl< ScalarType > &sigma) const
bool fromLocalCoordToImageCoord(const CCVector3 &localCoord, CCVector2 &imageCoord, bool withLensError=true) const
virtual bool isSerializable() const override
Returns whether object is serializable of not.
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
bool computeUncertainty(cloudViewer::ReferenceCloud *points, std::vector< Vector3Tpl< ScalarType >> &accuracy)
QImage undistort(const QImage &image) const
Undistorts an image based on the sensor distortion parameters.
bool fromImageCoordToGlobalCoord(const CCVector2 &imageCoord, CCVector3 &globalCoord, PointCoordinateType z0, bool withLensCorrection=true) const
bool m_projectionMatrixIsValid
Whether the intrinsic matrix is valid or not.
bool fromRealImCoordToIdealImCoord(const CCVector2 &real, CCVector2 &ideal) const
DistortionModel
Supported distortion models.
virtual void clearDrawings() override
double getFocalLength() const
ccCameraSensor(const ccCameraSensor &sensor)
Copy constructor.
void setDistortionParameters(LensDistortionParameters::Shared params)
Sets uncertainty parameters.
ccGLMatrix m_projectionMatrix
Intrinsic parameters matrix.
void drawFrustum(bool state)
Sets whether the frustum should be displayed or not.
static bool OrthoRectifyAsImages(std::vector< ccImage * > images, double a[], double b[], double c[], unsigned maxSize, QDir *outputDir=nullptr, std::vector< ccImage * > *orthoRectifiedImages=nullptr, std::vector< std::pair< double, double >> *relativePos=nullptr)
Projective ortho-rectification of multiple images (as image files)
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
Generic image.
Definition: ecvImage.h:19
Triangular mesh.
Definition: ecvMesh.h:35
void computeFrustumIntersectionWithOctree(std::vector< std::pair< unsigned, CCVector3 >> &pointsToTest, std::vector< unsigned > &inCameraFrustum, const float planesCoefficients[6][4], const CCVector3 ptsFrustum[8], const CCVector3 edges[6], const CCVector3 &center)
cloudViewer::DgmOctree * m_associatedOctree
OctreeCellVisibility
Definition of the state of a cell compared to a frustum.
bool build(cloudViewer::DgmOctree *octree)
Prepares structure for frustum filtering.
OctreeCellVisibility positionFromFrustum(cloudViewer::DgmOctree::CellCode truncatedCode, unsigned char level) const
Returns the cell visibility.
std::unordered_set< cloudViewer::DgmOctree::CellCode > m_cellsBuilt[cloudViewer::DgmOctree::MAX_OCTREE_LEVEL+1]
void computeFrustumIntersectionByLevel(unsigned char level, cloudViewer::DgmOctree::CellCode parentTruncatedCode, OctreeCellVisibility parentResult, const float planesCoefficients[6][4], const CCVector3 ptsFrustum[8], const CCVector3 edges[6], const CCVector3 &center)
OctreeCellVisibility separatingAxisTest(const CCVector3 &bbMin, const CCVector3 &bbMax, const float planesCoefficients[6][4], const CCVector3 frustumCorners[8], const CCVector3 frustumEdges[6], const CCVector3 &frustumCenter)
Separating Axis Test.
std::unordered_set< cloudViewer::DgmOctree::CellCode > m_cellsInFrustum[cloudViewer::DgmOctree::MAX_OCTREE_LEVEL+1]
ccOctreeFrustumIntersector()
Default constructor.
std::unordered_set< cloudViewer::DgmOctree::CellCode > m_cellsIntersectFrustum[cloudViewer::DgmOctree::MAX_OCTREE_LEVEL+1]
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
Generic sensor interface.
Definition: ecvSensor.h:27
QMultiMap< unsigned, unsigned > LoadedIDMap
Map of loaded unique IDs (old ID --> new ID)
The octree structure used throughout the library.
Definition: DgmOctree.h:39
unsigned CellCode
Type of the code of an octree cell.
Definition: DgmOctree.h:78
static const int MAX_OCTREE_LEVEL
Max octree subdivision level.
Definition: DgmOctree.h:67
A generic 3D point cloud with index-based point access.
A very simple point cloud (no point duplication)
LineSet define a sets of lines in 3D. A typical application is to display the point cloud corresponde...
Definition: LineSet.h:29
RGB color structure.
Definition: ecvColorTypes.h:49
ImGuiContext * context
Definition: Window.cpp:76
@ CAMERA_SENSOR
Definition: CVTypes.h:118
cloudViewer::DgmOctree * octree
Brown's distortion model + Linear Disparity.
static void GetKinectDefaults(BrownDistortionParameters &params)
QSharedPointer< BrownDistortionParameters > Shared
Shared pointer type.
virtual DistortionModel getModel() const override
Returns distortion model type.
BrownDistortionParameters()
Default initializer.
virtual DistortionModel getModel() const override
Returns distortion model type.
QSharedPointer< RadialDistortionParameters > Shared
Shared pointer type.
float k3
3rd radial distortion coefficient
Frustum information structure.
bool initFrustumCorners()
Reserves memory for the frustum corners cloud.
FrustumInformation()
Default initializer.
bool initFrustumHull()
Creates the frustum hull mesh.
Intrinsic parameters of the camera sensor.
IntrinsicParameters()
Default initializer.
static void GetKinectDefaults(IntrinsicParameters &params)
float horizFocal_pix() const
Returns the horizontal focal pix.
KeyPoint()
Default constructor.
unsigned index
Index in associated point cloud.
float y
2D 'y' coordinate (in pixels)
KeyPoint(float Px, float Py, unsigned indexInCloud)
Constructor from a pixel and its index in associated cloud.
float x
2D 'x' coordinate (in pixels)
Lens distortion parameters (interface)
virtual DistortionModel getModel() const =0
Returns distortion model type.
QSharedPointer< LensDistortionParameters > Shared
Shared pointer type.
virtual ~LensDistortionParameters()
Virtual destructor.
Simple radial distortion model.
virtual DistortionModel getModel() const override
Returns distortion model type.
float k2
2nd radial distortion coefficient
RadialDistortionParameters()
Default initializer.
float k1
1st radial distortion coefficient
QSharedPointer< RadialDistortionParameters > Shared
Shared pointer type.
Display context.