ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvDisc.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 "ecvDisc.h"
9 
10 // Local
11 #include <CVTools.h>
12 #include <Logging.h>
13 
14 #include "ecvNormalVectors.h"
15 #include "ecvPointCloud.h"
16 
18  const ccGLMatrix* transMat /*= nullptr*/,
19  QString name /*= QString("Disc")*/,
20  unsigned precision /*= DEFAULT_DRAWING_PRECISION*/)
21  : ccGenericPrimitive(name, transMat), m_radius(std::abs(radius)) {
22  setDrawingPrecision(std::max<unsigned>(
23  precision,
24  MIN_DRAWING_PRECISION)); // automatically calls buildUp &
25  // applyTransformationToVertices
26 }
27 
28 ccDisc::ccDisc(QString name /*="Disc"*/)
29  : ccGenericPrimitive(name), m_radius(0) {}
30 
34 }
35 
37  if (m_drawPrecision < MIN_DRAWING_PRECISION) return false;
38 
39  // invalid dimensions?
41  return false;
42  }
43 
44  unsigned steps = m_drawPrecision;
45 
46  // vertices
47  unsigned vertCount = steps + 1; // At least the center
48 
49  // normals
50  unsigned faceNormCounts = 1;
51  // faces
52  unsigned facesCount = steps;
53 
54  // allocate (& clear) structures
55  if (!init(vertCount, false, facesCount, faceNormCounts)) {
56  CVLog::Error("[ccDisc::buildUp] Not enough memory");
57  return false;
58  }
59 
60  ccPointCloud* verts = vertices();
61  assert(verts);
62  assert(m_triNormals);
63 
64  // first point: center of the disc
65  CCVector3 center = CCVector3(0, 0, 0);
66  // add center to the vertices
67  verts->addPoint(center);
68  CompressedNormType nIndex =
70  m_triNormals->addElement(nIndex);
71 
72  // then, angular sweep for the surface
73  PointCoordinateType angle_rad_step =
74  static_cast<PointCoordinateType>(2.0 * M_PI) / steps;
75  // bottom surface
76  for (unsigned i = 0; i < steps; ++i) {
77  CCVector3 P(center.x + cos(angle_rad_step * i) * m_radius,
78  center.y + sin(angle_rad_step * i) * m_radius, center.z);
79  verts->addPoint(P);
80  }
81 
82  // mesh faces
83  assert(m_triVertIndexes);
84 
85  // surface
86  for (unsigned i = 0; i < steps; ++i) {
87  unsigned i2 = 1 + i;
88  unsigned i3 = 1 + (i + 1) % steps;
89  addTriangle(0, i2, i3);
90  addTriangleNormalIndexes(0, 0, 0);
91  }
92 
94  showTriNorms(true);
95 
96  return true;
97 }
98 
100  if (m_radius == radius) return;
101 
102  assert(radius > 0);
103  m_radius = radius;
104 
105  buildUp();
107 }
108 
109 bool ccDisc::toFile_MeOnly(QFile& out, short dataVersion) const {
110  assert(out.isOpen() && (out.openMode() & QIODevice::WriteOnly));
111  if (dataVersion < 57) {
112  assert(false);
113  return false;
114  }
115 
116  if (!ccGenericPrimitive::toFile_MeOnly(out, dataVersion)) {
117  return false;
118  }
119 
120  // parameters (dataVersion>=57)
121  QDataStream outStream(&out);
122  outStream << m_radius;
123 
124  return true;
125 }
126 
128  return std::max(static_cast<short>(57),
130 }
131 
132 bool ccDisc::fromFile_MeOnly(QFile& in,
133  short dataVersion,
134  int flags,
135  LoadedIDMap& oldToNewIDMap) {
136  if (!ccGenericPrimitive::fromFile_MeOnly(in, dataVersion, flags,
137  oldToNewIDMap))
138  return false;
139 
140  if (dataVersion < 57) {
141  return false;
142  }
143 
144  // parameters (dataVersion>=57)
145  QDataStream inStream(&in);
147 
148  return true;
149 }
150 
152  trans = m_transformation;
153  // Disc is a 2D circle in XY plane, so bbox is a square centered at origin
154  // with side length = 2 * radius, and height = 0
155  return ccBBox(CCVector3(-m_radius, -m_radius, 0),
157 }
constexpr double M_PI
Pi.
Definition: CVConst.h:19
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
std::string name
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
void addElement(const Type &value)
Definition: ecvArray.h:105
Bounding box structure.
Definition: ecvBBox.h:25
ccDisc(PointCoordinateType radius, const ccGLMatrix *transMat=nullptr, QString name=QString("Disc"), unsigned precision=DEFAULT_DRAWING_PRECISION)
Default constructor.
Definition: ecvDisc.cpp:17
ccBBox getOwnFitBB(ccGLMatrix &trans) override
Returns best-fit bounding-box (if available)
Definition: ecvDisc.cpp:151
short minimumFileVersion_MeOnly() const override
Definition: ecvDisc.cpp:127
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
Definition: ecvDisc.cpp:109
void setRadius(PointCoordinateType radius)
Sets radius.
Definition: ecvDisc.cpp:99
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
Definition: ecvDisc.cpp:132
bool buildUp() override
Builds primitive.
Definition: ecvDisc.cpp:36
PointCoordinateType m_radius
Radius.
Definition: ecvDisc.h:70
ccGenericPrimitive * clone() const override
Clones primitive.
Definition: ecvDisc.cpp:31
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
virtual void showTriNorms(bool state)
Sets whether to show or not per-triangle normals.
Generic primitive interface.
void applyTransformationToVertices()
Applies associated transformation to vertices.
bool init(unsigned vertCount, bool vertNormals, unsigned faceCount, unsigned faceNormCount)
Inits internal structures.
virtual bool setDrawingPrecision(unsigned steps)
Sets drawing precision.
ccGenericPrimitive * finishCloneJob(ccGenericPrimitive *primitive) const
Finished 'clone' job (vertices color, etc.)
ccPointCloud * vertices()
Returns vertices.
unsigned m_drawPrecision
Drawing precision (for primitives that support this feature)
static const int MIN_DRAWING_PRECISION
Minimum drawing precision.
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
virtual void notifyGeometryUpdate()
Definition: ecvHObject.cpp:104
NormsIndexesTableType * m_triNormals
Per-triangle normals.
Definition: ecvMesh.h:1493
void addTriangle(unsigned i1, unsigned i2, unsigned i3)
Adds a triangle to the mesh.
Definition: ecvMesh.cpp:2428
void addTriangleNormalIndexes(int i1, int i2, int i3)
Adds a triplet of normal indexes for next triangle.
Definition: ecvMesh.cpp:3340
triangleIndexesContainer * m_triVertIndexes
Triangles' vertices indexes (3 per triangle)
Definition: ecvMesh.h:1502
static CompressedNormType GetNormIndex(const PointCoordinateType N[])
Returns the compressed index corresponding to a normal vector.
virtual QString getName() const
Returns object name.
Definition: ecvObject.h:72
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)
static void CoordsFromDataStream(QDataStream &stream, int flags, PointCoordinateType *out, unsigned count=1)
void addPoint(const CCVector3 &P)
Adds a 3D point to the database.
unsigned int CompressedNormType
Compressed normals type.
Definition: ecvBasicTypes.h:16
bool LessThanEpsilon(float x)
Test a floating point number against our epsilon (a very small number).
Definition: CVMath.h:23
Definition: Eigen.h:85