ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvCircle.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 "ecvCircle.h"
9 
10 // CV_db
11 #include "ecvPointCloud.h"
12 
13 // CV_CORE_LIB
14 #include <CVTools.h>
15 #include <Logging.h>
16 
17 ccCircle::ccCircle(double radius /*=0.0*/,
18  unsigned resolution /*=48*/,
19  unsigned uniqueID /*=ccUniqueIDGenerator::InvalidUniqueID*/)
20  : ccPolyline(new ccPointCloud("vertices")),
21  m_radius(std::max(0.0, radius)),
22  m_resolution(std::max(resolution, 4u)) {
23  if (radius > 0.0) {
24  updateInternalRepresentation();
25  }
26 
27  ccPointCloud* vertices = dynamic_cast<ccPointCloud*>(m_theAssociatedCloud);
28  if (vertices) {
29  vertices->setEnabled(false);
30  addChild(vertices);
31  } else {
32  assert(false);
33  }
34 
35  setName("Circle");
36 }
37 
39  : ccCircle(circle.m_radius, circle.m_resolution) {
40  updateInternalRepresentation();
41 }
42 
44  ccCircle* clonedCircle = new ccCircle(*this);
45  clonedCircle->setLocked(
46  false); // there's no reason to keep the clone locked
47 
48  return clonedCircle;
49 }
50 
52  // we call the ccHObject method instead of the ccPolyline one,
53  // to only update the transformation history matrix, and not
54  // trigger any coordinate modification
56 
57  // now we can update the vertices
58  updateInternalRepresentation();
59 
60  // invalidate the bounding-box
62 }
63 
64 void ccCircle::setRadius(double radius) {
65  if (m_radius != radius) {
66  m_radius = radius;
67  updateInternalRepresentation();
68  }
69 }
70 
71 void ccCircle::setResolution(unsigned resolution) {
72  if (m_resolution != resolution) {
73  m_resolution = resolution;
74  updateInternalRepresentation();
75  }
76 }
77 
78 bool ccCircle::toFile_MeOnly(QFile& out, short dataVersion) const {
79  assert(out.isOpen() && (out.openMode() & QIODevice::WriteOnly));
80  if (dataVersion < 56) {
81  assert(false);
82  return false;
83  }
84 
85  if (!ccPolyline::toFile_MeOnly(out, dataVersion)) {
86  return false;
87  }
88 
89  QDataStream outStream(&out);
90 
91  // Radius (dataVersion>=56)
92  outStream << m_radius;
93  // Resolution (dataVersion>=56)
94  outStream << m_resolution;
95 
96  return true;
97 }
98 
100  short minVersion = 56;
101  return std::max(minVersion, ccHObject::minimumFileVersion_MeOnly());
102 }
103 
105  short dataVersion,
106  int flags,
107  LoadedIDMap& oldToNewIDMap) {
108  CVLog::PrintVerbose(QString("Loading circle %1...").arg(getName()));
109 
110  ccPointCloud* vertices = dynamic_cast<ccPointCloud*>(m_theAssociatedCloud);
111  if (vertices) {
112  removeChild(vertices);
113  m_theAssociatedCloud = nullptr;
114  }
115 
116  if (!ccPolyline::fromFile_MeOnly(in, dataVersion, flags, oldToNewIDMap)) {
117  return false;
118  }
119 
120  if (dataVersion < 56) {
121  return false;
122  }
123 
124  QDataStream inStream(&in);
125 
126  // Radius (dataVersion>=56)
127  inStream >> m_radius;
128  // Resolution (dataVersion>=56)
129  inStream >> m_resolution;
130 
131  return true;
132 }
133 
134 void ccCircle::updateInternalRepresentation() {
135  if (m_resolution < 4) {
137  "[ccCircle::updateInternalRepresentation] Resolution is too "
138  "small");
139  return;
140  }
141 
142  ccPointCloud* vertices = dynamic_cast<ccPointCloud*>(m_theAssociatedCloud);
143  if (!vertices) {
145  "[ccCircle::updateInternalRepresentation] Invalid vertices");
146  return;
147  }
148 
149  vertices->clear();
150  resize(0); // should never fail
151 
152  // reset the vertices transformation history
153  ccGLMatrix Id;
154  Id.toIdentity();
155  vertices->setGLTransformationHistory(Id);
156 
157  if (!vertices->reserve(m_resolution)) {
159  "[ccCircle::updateInternalRepresentation] Not enough memory");
160  return;
161  }
162 
163  double angleStep_rad = (2 * M_PI) / m_resolution;
164  for (unsigned i = 0; i < m_resolution; ++i) {
166  CCVector3d(cos(i * angleStep_rad) * m_radius,
167  sin(i * angleStep_rad) * m_radius, 0)
168  .u);
169  vertices->addPoint(P);
170  }
171 
173  setClosed(true);
174 
176 }
constexpr double M_PI
Pi.
Definition: CVConst.h:19
Vector3Tpl< double > CCVector3d
Double 3D Vector.
Definition: CVGeom.h:804
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
static bool PrintVerbose(const char *format,...)
Prints out a verbose formatted message in console.
Definition: CVLog.cpp:103
static Vector3Tpl fromArray(const int a[3])
Constructor from an int array.
Definition: CVGeom.h:268
Circle (as a polyline)
Definition: ecvCircle.h:16
double m_radius
Radius of the circle.
Definition: ecvCircle.h:73
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
Definition: ecvCircle.cpp:78
void applyGLTransformation(const ccGLMatrix &trans) override
Applies a GL transformation to the entity.
Definition: ecvCircle.cpp:51
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
Definition: ecvCircle.cpp:104
void setRadius(double radius)
Sets the radius of the circle.
Definition: ecvCircle.cpp:64
unsigned m_resolution
Resolution of the displayed circle.
Definition: ecvCircle.h:76
ccCircle * clone() const
Clones this circle.
Definition: ecvCircle.cpp:43
ccCircle(double radius=0.0, unsigned resolution=48, unsigned uniqueID=ccUniqueIDGenerator::InvalidUniqueID)
Default constructor.
Definition: ecvCircle.cpp:17
short minimumFileVersion_MeOnly() const override
Definition: ecvCircle.cpp:99
void setResolution(unsigned resolution)
Sets the resolution of the displayed circle.
Definition: ecvCircle.cpp:71
virtual void toIdentity()
Sets matrix to identity.
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
ccGLMatrix m_glTransHistory
Cumulative GL transformation.
Definition: ecvHObject.h:727
virtual short minimumFileVersion_MeOnly() const
void applyGLTransformation_recursive(const ccGLMatrix *trans=nullptr)
Applies the active OpenGL transformation to the entity (recursive)
Definition: ecvHObject.cpp:948
virtual void setGLTransformationHistory(const ccGLMatrix &mat)
Sets the transformation 'history' matrix (handle with care!)
Definition: ecvHObject.h:635
virtual void applyGLTransformation(const ccGLMatrix &trans)
Applies a GL transformation to the entity.
Definition: ecvHObject.cpp:944
void removeChild(ccHObject *child)
virtual bool addChild(ccHObject *child, int dependencyFlags=DP_PARENT_OF_OTHER, int insertIndex=-1)
Adds a child.
Definition: ecvHObject.cpp:534
virtual void setLocked(bool state)
Sets the "enabled" property.
Definition: ecvObject.h:117
virtual QString getName() const
Returns object name.
Definition: ecvObject.h:72
virtual void setName(const QString &name)
Sets object name.
Definition: ecvObject.h:75
virtual void setEnabled(bool state)
Sets the "enabled" property.
Definition: ecvObject.h:102
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
bool reserve(unsigned numberOfPoints) override
Reserves memory for all the active features.
void clear() override
Clears the entity from all its points and features.
Colored polyline.
Definition: ecvPolyline.h:24
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.
QMultiMap< unsigned, unsigned > LoadedIDMap
Map of loaded unique IDs (old ID --> new ID)
void addPoint(const CCVector3 &P)
Adds a 3D point to the database.
void setClosed(bool state)
Sets whether the polyline is closed or not.
Definition: Polyline.h:29
virtual bool addPointIndex(unsigned globalIndex)
Point global index insertion mechanism.
void invalidateBoundingBox()
Invalidates the bounding-box.
GenericIndexedCloudPersist * m_theAssociatedCloud
Associated cloud.
virtual bool resize(unsigned n)
Presets the size of the vector used to store point references.
Definition: Eigen.h:85