ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvCoordinateSystem.cpp
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 #include "ecvCoordinateSystem.h"
9 
10 // CV_DB_LIB
11 #include "ecvDisplayTools.h"
12 #include "ecvPlane.h"
13 #include "ecvPointCloud.h"
14 
16  PointCoordinateType displayScale,
17  PointCoordinateType axisWidth,
18  const ccGLMatrix* transMat /*= 0*/,
19  QString name /*=QString("CoordinateSystem")*/)
20  : ccGenericPrimitive(name, transMat),
21  m_DisplayScale(displayScale),
22  m_width(axisWidth),
23  m_showAxisPlanes(true),
24  m_showAxisLines(true) {
26  showColors(true);
27 }
28 
30  const ccGLMatrix* transMat /*= 0*/,
31  QString name /*=QString("CoordinateSystem")*/)
32  : ccGenericPrimitive(name, transMat),
33  m_DisplayScale(DEFAULT_DISPLAY_SCALE),
34  m_width(AXIS_DEFAULT_WIDTH),
35  m_showAxisPlanes(true),
36  m_showAxisLines(true) {
38  showColors(true);
39 }
40 
42  QString name /*=QString("CoordinateSystem")*/)
44  m_DisplayScale(DEFAULT_DISPLAY_SCALE),
45  m_width(AXIS_DEFAULT_WIDTH),
46  m_showAxisPlanes(true),
47  m_showAxisLines(true) {
49  showColors(true);
50 }
51 
53 
55 
57  if (width == 0.0f) {
59  return;
60  }
62  m_width = width;
63  }
64 }
65 
67  if (scale >= MIN_DISPLAY_SCALE_F) {
68  m_DisplayScale = scale;
70  }
71 }
72 
73 std::shared_ptr<ccPlane> ccCoordinateSystem::getXYplane() const {
74  auto plane = std::make_shared<ccPlane>(createXYplane(&m_transformation));
75  plane->clearTriNormals();
76  plane->ComputeTriangleNormals();
77  return plane;
78 }
79 std::shared_ptr<ccPlane> ccCoordinateSystem::getYZplane() const {
80  auto plane = std::make_shared<ccPlane>(createYZplane(&m_transformation));
81  plane->clearTriNormals();
82  plane->ComputeTriangleNormals();
83  return plane;
84 }
85 std::shared_ptr<ccPlane> ccCoordinateSystem::getZXplane() const {
86  auto plane = std::make_shared<ccPlane>(createZXplane(&m_transformation));
87  plane->clearTriNormals();
88  plane->ComputeTriangleNormals();
89  return plane;
90 }
91 
95 }
96 
99  context.visible && m_showAxisPlanes);
101  context.visible && m_showAxisLines);
102 }
103 
105  ccGLMatrix xyPlane_mtrx;
106  xyPlane_mtrx.toIdentity();
107  xyPlane_mtrx.setTranslation(
108  CCVector3(m_DisplayScale / 2, m_DisplayScale / 2, 0.0));
109  if (transMat) {
110  xyPlane_mtrx = *transMat * xyPlane_mtrx;
111  }
112  ccPlane xyPlane(m_DisplayScale, m_DisplayScale, &xyPlane_mtrx);
113  xyPlane.setColor(ecvColor::red);
114  return xyPlane;
115 }
116 
118  ccGLMatrix yzPlane_mtrx;
119  yzPlane_mtrx.initFromParameters(
120  static_cast<PointCoordinateType>(1.57079633),
121  static_cast<PointCoordinateType>(0),
122  static_cast<PointCoordinateType>(1.57079633),
123  CCVector3(0.0, m_DisplayScale / 2, m_DisplayScale / 2));
124  if (transMat) {
125  yzPlane_mtrx = *transMat * yzPlane_mtrx;
126  }
127  ccPlane yzPlane(m_DisplayScale, m_DisplayScale, &yzPlane_mtrx);
128  yzPlane.setColor(ecvColor::yellow);
129  return yzPlane;
130 }
131 
133  ccGLMatrix zxPlane_mtrx;
134  zxPlane_mtrx.initFromParameters(
135  static_cast<PointCoordinateType>(0),
136  static_cast<PointCoordinateType>(-1.57079633),
137  static_cast<PointCoordinateType>(-1.57079633),
139  if (transMat) {
140  zxPlane_mtrx = *transMat * zxPlane_mtrx;
141  }
142  ccPlane zxPlane(m_DisplayScale, m_DisplayScale, &zxPlane_mtrx);
143  // zxPlane.setColor(ecvColor::FromRgbfToRgb(ecvColor::Rgbf(0.0f,
144  // 0.7f, 1.0f)));
145  zxPlane.setColor(ecvColor::green);
146  return zxPlane;
147 }
148 
150  // clear triangles indexes
151  if (m_triVertIndexes) {
153  }
154  // clear per triangle normals
156  if (m_triNormals) {
157  m_triNormals->clear();
158  }
159  // clear vertices
160  ccPointCloud* verts = vertices();
161  if (verts) {
162  verts->clear();
163  }
164 
165  *this += createXYplane();
166  *this += createYZplane();
167  *this += createZXplane();
168 
169  return (vertices() && vertices()->size() == 12 && this->size() == 6);
170 }
171 
175 }
176 
177 bool ccCoordinateSystem::toFile_MeOnly(QFile& out, short dataVersion) const {
178  assert(out.isOpen() && (out.openMode() & QIODevice::WriteOnly));
179  if (dataVersion < 52) {
180  assert(false);
181  return false;
182  }
183 
184  if (!ccGenericPrimitive::toFile_MeOnly(out, dataVersion)) return false;
185 
186  // parameters (dataVersion>=52)
187  QDataStream outStream(&out);
188  outStream << m_DisplayScale;
189  outStream << m_width;
190 
191  return true;
192 }
193 
195  return std::max(static_cast<short>(52),
197 }
198 
200  short dataVersion,
201  int flags,
202  LoadedIDMap& oldToNewIDMap) {
203  if (!ccGenericPrimitive::fromFile_MeOnly(in, dataVersion, flags,
204  oldToNewIDMap))
205  return false;
206 
207  // parameters (dataVersion>=52)
208  QDataStream inStream(&in);
210  &m_DisplayScale, 1);
212  return true;
213 }
214 
216  if (m_showAxisPlanes) {
217  // call parent method to draw the Planes
218  context.viewID = this->getViewId();
220  } else {
222  }
223 
224  // show axis
225  if (MACRO_Draw3D(context)) {
226  if (m_showAxisLines) {
227  if (ecvDisplayTools::GetCurrentScreen() == nullptr) return;
228 
229  // build-up the normal representation own 'context'
230  CC_DRAW_CONTEXT tempContext = context;
231  if (m_width != 0.0f) {
232  tempContext.currentLineWidth =
233  static_cast<unsigned char>(m_width);
234  }
235 
236  // coordinate system axis
237  m_axis.clear();
238  m_axis.points_.push_back(Eigen::Vector3d(0.0, 0.0, 0.0));
239  m_axis.points_.push_back(Eigen::Vector3d(
240  static_cast<double>(m_DisplayScale * 2), 0.0, 0.0));
241  m_axis.points_.push_back(Eigen::Vector3d(
242  0.0, static_cast<double>(m_DisplayScale * 2), 0.0));
243  m_axis.points_.push_back(Eigen::Vector3d(
244  0.0, 0.0, static_cast<double>(m_DisplayScale * 2)));
245 
246  // x axis
247  m_axis.lines_.push_back(Eigen::Vector2i(0, 1));
248  m_axis.colors_.push_back(Eigen::Vector3d(1.0, 0.0, 0.0));
249 
250  // y axis
251  m_axis.lines_.push_back(Eigen::Vector2i(0, 2));
252  m_axis.colors_.push_back(Eigen::Vector3d(1.0, 1.0, 0.0));
253 
254  // z axis
255  m_axis.lines_.push_back(Eigen::Vector2i(0, 3));
256  m_axis.colors_.push_back(Eigen::Vector3d(0.0, 1.0, 0.0));
258 
259  // transformation
260  {
261  Eigen::Matrix4d transformation =
263  m_axis.Transform(transformation);
264  }
265 
266  tempContext.viewID = m_axis.getViewId();
267  ecvDisplayTools::Draw(tempContext, &m_axis);
268  } else {
270  }
271  }
272 }
Vector3Tpl< PointCoordinateType > CCVector3
Default 3D Vector.
Definition: CVGeom.h:798
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
int width
std::string name
void clear(bool releaseMemory=false)
Definition: ecvArray.h:115
PointCoordinateType m_DisplayScale
CoordinateSystem options.
static constexpr float MIN_DISPLAY_SCALE_F
Minimum Display scale.
cloudViewer::geometry::LineSet m_axis
PointCoordinateType m_width
ccPlane createZXplane(const ccGLMatrix *transMat=nullptr) const
static constexpr float MIN_AXIS_WIDTH_F
Minimum Axis line width.
void ShowAxisLines(bool show)
short minimumFileVersion_MeOnly() const override
void ShowAxisPlanes(bool show)
static constexpr PointCoordinateType AXIS_DEFAULT_WIDTH
Default Axis line width.
std::shared_ptr< ccPlane > getXYplane() const
ccPlane createYZplane(const ccGLMatrix *transMat=nullptr) const
virtual ccGenericPrimitive * clone() const override
Clones primitive.
virtual void hideShowDrawings(CC_DRAW_CONTEXT &context) override
static constexpr float MAX_AXIS_WIDTH_F
Maximum Axis line width.
void setDisplayScale(PointCoordinateType scale)
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
ccPlane createXYplane(const ccGLMatrix *transMat=nullptr) const
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
std::shared_ptr< ccPlane > getZXplane() const
virtual void clearDrawings() override
ccCoordinateSystem(PointCoordinateType displayScale, PointCoordinateType axisWidth, const ccGLMatrix *transMat=nullptr, QString name=QString("CoordinateSystem"))
Default constructor.
void drawMeOnly(CC_DRAW_CONTEXT &context) override
Draws the entity only (not its children)
std::shared_ptr< ccPlane > getYZplane() const
void setAxisWidth(PointCoordinateType width)
virtual bool buildUp() override
Builds primitive.
virtual void showColors(bool state)
Sets colors visibility.
void setTranslation(const Vector3Tpl< float > &Tr)
Sets translation (float version)
void initFromParameters(T alpha_rad, const Vector3Tpl< T > &axis3D, const Vector3Tpl< T > &t3D)
Inits transformation from a rotation axis, an angle and a translation.
virtual void toIdentity()
Sets matrix to identity.
static Eigen::Matrix< double, 4, 4 > ToEigenMatrix4(const ccGLMatrixTpl< float > &mat)
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
Generic primitive interface.
virtual void setColor(const ecvColor::Rgb &col)
Sets primitive color (shortcut)
virtual bool updateRepresentation()
Updates internal representation (as a mesh)
ccGenericPrimitive * finishCloneJob(ccGenericPrimitive *primitive) const
Finished 'clone' job (vertices color, etc.)
ccPointCloud * vertices()
Returns vertices.
virtual void clearDrawings()
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
ccGLMatrix m_transformation
Associated transformation (applied to vertices)
short minimumFileVersion_MeOnly() const override
QString getViewId() const
Definition: ecvHObject.h:225
void setRedrawFlagRecursive(bool redraw=false)
Definition: ecvHObject.cpp:772
void drawMeOnly(CC_DRAW_CONTEXT &context) override
Enables (OpenGL) stipple mask.
Definition: ecvMesh.cpp:2635
void removePerTriangleNormalIndexes()
Removes any per-triangle triplets of normal indexes.
Definition: ecvMesh.cpp:3323
virtual unsigned size() const override
Returns the number of triangles.
Definition: ecvMesh.cpp:2143
NormsIndexesTableType * m_triNormals
Per-triangle normals.
Definition: ecvMesh.h:1493
triangleIndexesContainer * m_triVertIndexes
Triangles' vertices indexes (3 per triangle)
Definition: ecvMesh.h:1502
virtual QString getName() const
Returns object name.
Definition: ecvObject.h:72
Plane (primitive)
Definition: ecvPlane.h:18
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
void clear() override
Clears the entity from all its points and features.
QMultiMap< unsigned, unsigned > LoadedIDMap
Map of loaded unique IDs (old ID --> new ID)
static void CoordsFromDataStream(QDataStream &stream, int flags, PointCoordinateType *out, unsigned count=1)
std::vector< Eigen::Vector3d > points_
Points coordinates.
Definition: LineSet.h:156
std::vector< Eigen::Vector3d > colors_
RGB colors of lines.
Definition: LineSet.h:160
std::vector< Eigen::Vector2i > lines_
Lines denoted by the index of points forming the line.
Definition: LineSet.h:158
virtual LineSet & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
Definition: LineSet.cpp:67
static void RemoveEntities(const ccHObject *obj)
static void Draw(const CC_DRAW_CONTEXT &context, const ccHObject *obj)
static bool HideShowEntities(const ccHObject *obj, bool visible)
static QWidget * GetCurrentScreen()
#define MACRO_Draw3D(context)
ImGuiContext * context
Definition: Window.cpp:76
constexpr Rgb red(MAX, 0, 0)
constexpr Rgb green(0, MAX, 0)
constexpr Rgb yellow(MAX, MAX, 0)
Eigen::Matrix< Index, 2, 1 > Vector2i
Definition: knncpp.h:29
Display context.
unsigned char currentLineWidth