ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
GrainsAsEllipsoids.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 // ECV_PLUGINS
11 #include <ecvMainAppInterface.h>
12 
13 // qCC_db
14 #include <ecvAdvancedTypes.h>
15 #include <ecvBBox.h>
16 #include <ecvColorTypes.h>
17 #include <ecvCustomObject.h>
18 #include <ecvDisplayTools.h>
19 #include <ecvHObject.h>
20 #include <ecvSerializableObject.h>
21 
22 #include <QObject>
23 #include <memory>
24 #include <set>
25 #include <vector>
26 
27 // Eigen
28 #include "Eigen/Dense"
29 
30 class ccPointCloud;
31 class ccMesh;
32 namespace cloudViewer {
33 namespace geometry {
34 class LineSet;
35 }
36 } // namespace cloudViewer
37 
38 class GrainsAsEllipsoids : public QObject, public ccCustomHObject {
39  Q_OBJECT
40 
41 public:
42  typedef Eigen::Array<bool, Eigen::Dynamic, Eigen::Dynamic> Xb;
43 
45 
48  const std::vector<std::vector<int>>& stacks,
51 
52  void setLocalMaximumIndexes(const Eigen::ArrayXi& localMaximumIndexes);
53 
54  void setGrainColorsTable(const RGBAColorsTableType& colorTable);
55 
56  bool exportResultsAsCloud();
57 
58  // INIT SPHERE
59 
60  void initSphereVertices();
61 
62  void initSphereIndexes();
63 
64  std::vector<float> vertices;
65  std::vector<float> normals;
66  std::vector<float> texCoords;
67 
68  std::vector<int> indices;
69  std::vector<int> lineIndices;
70 
71  int sectorCount{21};
72  int stackCount{21};
73 
74  // ELLIPSOID FITTING
75 
76  enum Method { DIRECT = 0 };
77 
78  void updateBBoxOnlyOne(int index);
79 
80  bool explicitToImplicit(const Eigen::Array3f& center,
81  const Eigen::Array3f& radii,
82  const Eigen::Matrix3f& rotationMatrix,
83  Eigen::ArrayXd& parameters);
84 
85  bool implicitToExplicit(const Eigen::ArrayXd& parameters,
86  Eigen::Array3f& center,
87  Eigen::Array3f& radii,
88  Eigen::Matrix3f& rotationMatrix);
89 
90  bool directFit(const Eigen::ArrayX3d& xyz, Eigen::ArrayXd& parameters);
91 
92  bool fitEllipsoidToGrain(const int grainIndex,
93  Eigen::Array3f& center,
94  Eigen::Array3f& radii,
95  Eigen::Matrix3f& rotationMatrix,
96  const Method& method = DIRECT);
97 
98  // DRAW
99 
101  void updateMeshAndLineSet();
102 
103  void setOnlyOne(int i);
104 
105  void showOnlyOne(bool state);
106 
107  void showAll(bool state);
108 
109  void setTransparency(double transparency) {
110  m_transparency = transparency;
111  m_meshNeedsUpdate = true;
112  redrawDisplay();
113  }
114  void drawSurfaces(bool state) {
115  m_drawSurfaces = state;
116  m_meshNeedsUpdate = true;
117  redrawDisplay();
118  }
119  void drawLines(bool state) {
120  m_drawLines = state;
121  m_meshNeedsUpdate = true;
122  redrawDisplay();
123  }
124  void drawPoints(bool state) {
125  m_drawPoints = state;
126  m_meshNeedsUpdate = true;
127  redrawDisplay();
128  }
129  void setGLPointSize(int size) {
131  m_meshNeedsUpdate = true;
132  redrawDisplay();
133  }
134 
139  void clearGeneratedObjects();
140 
141  // Inherited from ccHObject
142 
143  void draw(CC_DRAW_CONTEXT& context) override;
144 
145  ccBBox getOwnBB(bool withGLFeatures = false) override;
146 
147  bool toFile_MeOnly(QFile& out, short dataVersion) const override;
148  short minimumFileVersion_MeOnly() const override;
149 
150  bool fromFile_MeOnly(QFile& in,
151  short dataVersion,
152  int flags,
153  LoadedIDMap& oldToNewIDMap) override;
154 
157  std::vector<std::vector<int>> m_stacks;
158  std::vector<CCVector3f> m_grainColors;
162 
163  Eigen::ArrayX3f ellipsoidInstance;
164 
165  std::vector<Eigen::Array3f> m_center;
166  std::vector<Eigen::Array3f> m_radii;
167  std::vector<Eigen::Matrix3f> m_rotationMatrix;
168  std::set<int> m_fitNotOK;
169  double m_transparency = 1.0;
170  bool m_drawSurfaces = true;
171  bool m_drawLines = true;
172  bool m_drawPoints = false;
173 
174  int m_onlyOne = 0;
175  bool m_showAll{true};
176  int m_glPointSize = 3;
177 
178  // Mesh and LineSet representations for rendering
179  std::vector<ccMesh*> m_meshes;
180  std::vector<cloudViewer::geometry::LineSet*> m_lineSets;
181  std::vector<ccPointCloud*>
182  m_pointsClouds; // Point clouds for drawPoints mode
183  bool m_meshNeedsUpdate = true;
184 };
int size
bool fitEllipsoidToGrain(const int grainIndex, Eigen::Array3f &center, Eigen::Array3f &radii, Eigen::Matrix3f &rotationMatrix, const Method &method=DIRECT)
void updateBBoxOnlyOne(int index)
std::vector< Eigen::Array3f > m_center
std::vector< ccMesh * > m_meshes
std::vector< Eigen::Array3f > m_radii
bool implicitToExplicit(const Eigen::ArrayXd &parameters, Eigen::Array3f &center, Eigen::Array3f &radii, Eigen::Matrix3f &rotationMatrix)
void showOnlyOne(bool state)
void drawPoints(bool state)
void setGLPointSize(int size)
void updateMeshAndLineSet()
Update mesh and lineset representations for ellipsoids.
std::vector< ccPointCloud * > m_pointsClouds
std::vector< float > normals
std::vector< int > indices
void draw(CC_DRAW_CONTEXT &context) override
Draws entity and its children.
void showAll(bool state)
ecvMainAppInterface * m_app
std::vector< float > vertices
Eigen::ArrayX3f ellipsoidInstance
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
bool explicitToImplicit(const Eigen::Array3f &center, const Eigen::Array3f &radii, const Eigen::Matrix3f &rotationMatrix, Eigen::ArrayXd &parameters)
void drawSurfaces(bool state)
std::vector< CCVector3f > m_grainColors
std::vector< float > texCoords
Eigen::Array< bool, Eigen::Dynamic, Eigen::Dynamic > Xb
void drawLines(bool state)
ccPointCloud * m_cloud
bool directFit(const Eigen::ArrayX3d &xyz, Eigen::ArrayXd &parameters)
std::set< int > m_fitNotOK
void setTransparency(double transparency)
void setLocalMaximumIndexes(const Eigen::ArrayXi &localMaximumIndexes)
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
short minimumFileVersion_MeOnly() const override
GrainsAsEllipsoids(ecvMainAppInterface *app)
qCC_db
std::vector< cloudViewer::geometry::LineSet * > m_lineSets
std::vector< Eigen::Matrix3f > m_rotationMatrix
std::vector< std::vector< int > > m_stacks
ccBBox getOwnBB(bool withGLFeatures=false) override
Returns the entity's own bounding-box.
void setGrainColorsTable(const RGBAColorsTableType &colorTable)
std::vector< int > lineIndices
Array of RGBA colors for each point.
Bounding box structure.
Definition: ecvBBox.h:25
Custom hierarchy object.
virtual void redrawDisplay(bool forceRedraw=true, bool only2D=false)
Redraws associated display.
Triangular mesh.
Definition: ecvMesh.h:35
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
QMultiMap< unsigned, unsigned > LoadedIDMap
Map of loaded unique IDs (old ID --> new ID)
Main application interface (for plugins)
double colors[3]
ImGuiContext * context
Definition: Window.cpp:76
Generic file read and write utility for python interface.
std::vector< PointCoordinateType > radii
Definition: qM3C2Tools.cpp:42
Display context.