ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvSensor.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 "ecvSensor.h"
9 
10 #include "ecvDisplayTools.h"
11 
13  : ccHObject(name),
14  m_posBuffer(nullptr),
15  m_activeIndex(0),
16  m_color(ecvColor::green),
17  m_scale(PC_ONE) {
19 }
20 
22  : ccHObject(sensor),
23  m_posBuffer(nullptr),
24  m_rigidTransformation(sensor.m_rigidTransformation),
25  m_activeIndex(sensor.m_activeIndex),
26  m_color(sensor.m_color),
27  m_scale(sensor.m_scale) {
28  if (sensor.m_posBuffer)
30 }
31 
32 bool ccSensor::addPosition(ccGLMatrix& trans, double index) {
33  if (!m_posBuffer) {
36  m_posBuffer->setVisible(true);
37  m_posBuffer->setEnabled(false);
38  }
39 
40  bool sort =
41  (!m_posBuffer->empty() && m_posBuffer->back().getIndex() > index);
42  try {
43  m_posBuffer->emplace_back(trans, index);
44  } catch (const std::bad_alloc&) {
45  // not enough memory!
46  return false;
47  }
48 
49  if (sort) m_posBuffer->sort();
50 
51  return true;
52 }
53 
55 
57  context.viewID = this->getViewId();
59 }
60 
62  // transparent call
64 
65  // we update the rigid transformation
67 }
68 
69 void ccSensor::getIndexBounds(double& minIndex, double& maxIndex) const {
70  if (m_posBuffer && !m_posBuffer->empty()) {
71  minIndex = m_posBuffer->front().getIndex();
72  maxIndex = m_posBuffer->back().getIndex();
73  } else {
74  minIndex = maxIndex = 0;
75  }
76 }
77 
79  double index) const {
80  trans.toIdentity();
81  if (m_posBuffer)
82  if (!m_posBuffer->getInterpolatedTransformation(index, trans))
83  return false;
84 
85  trans *= m_rigidTransformation;
86 
87  return true;
88 }
89 
91  ccIndexedTransformation& trans) const {
94  "[ccSensor::getActiveAbsoluteTransformation] Failed to get a "
95  "valid transformation for active index!");
96  return false;
97  }
98 
99  return true;
100 }
101 
104 
105  if (!getActiveAbsoluteTransformation(trans)) return false;
106 
107  vec = trans.getTranslationAsVec3D();
108  return true;
109 }
110 
113 
114  if (!getActiveAbsoluteTransformation(trans)) return false;
115 
116  ccGLMatrix mat = trans;
117  mat.setTranslation(CCVector3(0.0, 0.0, 0.0));
118  rotation = mat;
119 
120  return true;
121 }
122 
123 bool ccSensor::toFile_MeOnly(QFile& out, short dataVersion) const {
124  assert(out.isOpen() && (out.openMode() & QIODevice::WriteOnly));
125  if (dataVersion < 35) {
126  assert(false);
127  return false;
128  }
129 
130  if (!ccHObject::toFile_MeOnly(out, dataVersion)) return false;
131 
132  // rigid transformation (dataVersion>=34)
133  if (!m_rigidTransformation.toFile(out, dataVersion)) return WriteError();
134 
135  // various parameters (dataVersion>=35)
136  QDataStream outStream(&out);
137  outStream << m_activeIndex; // active index
138  outStream << m_scale; // scale
139 
140  // color (dataVersion>=35)
141  if (out.write((const char*)&m_color.rgb, sizeof(ColorCompType) * 3) < 0)
142  return WriteError();
143 
144  // we can't save the associated position buffer (as it may be shared by
145  // multiple sensors) so instead we save it's unique ID (dataVersion>=34)
146  // WARNING: the buffer must be saved in the same BIN file! (responsibility
147  // of the caller)
148  uint32_t bufferUniqueID =
149  (m_posBuffer ? (uint32_t)m_posBuffer->getUniqueID() : 0);
150  if (out.write((const char*)&bufferUniqueID, 4) < 0) return WriteError();
151 
152  return true;
153 }
154 
156  short minVersion = std::max(static_cast<short>(35),
158  return std::max(minVersion, ccHObject::minimumFileVersion_MeOnly());
159 }
160 
162  short dataVersion,
163  int flags,
164  LoadedIDMap& oldToNewIDMap) {
165  if (!ccHObject::fromFile_MeOnly(in, dataVersion, flags, oldToNewIDMap))
166  return false;
167 
168  // serialization wasn't possible before v3.4!
169  if (dataVersion < 34) return false;
170 
171  // rigid transformation (dataVersion>=34)
172  if (!m_rigidTransformation.fromFile(in, dataVersion, flags, oldToNewIDMap))
173  return ReadError();
174 
175  // various parameters (dataVersion>=35)
176  QDataStream inStream(&in);
177  inStream >> m_activeIndex;
179 
180  // color (dataVersion>=35)
181  if (in.read((char*)&m_color.rgb, sizeof(ColorCompType) * 3) < 0)
182  return ReadError();
183 
184  // as the associated position buffer can't be saved directly (as it may be
185  // shared by multiple sensors) we only store its unique ID (dataVersion>=34)
186  // --> we hope we will find it at loading time (i.e. this is the
187  // responsibility of the caller to make sure that all dependencies are saved
188  // together)
189  uint32_t bufferUniqueID = 0;
190  if (in.read((char*)&bufferUniqueID, 4) < 0) return ReadError();
191  //[DIRTY] WARNING: temporarily, we set the vertices unique ID in the
192  //'m_posBuffer' pointer!!!
193  *(uint32_t*)(&m_posBuffer) = bufferUniqueID;
194 
195  return true;
196 }
197 
199  // not supported by default, must be reimplemented by the child class
200  CVLog::Warning("[ccSensor::applyViewport] Unhandled sensor type!");
201  return false;
202 }
constexpr PointCoordinateType PC_ONE
'1' as a PointCoordinateType value
Definition: CVConst.h:67
Vector3Tpl< PointCoordinateType > CCVector3
Default 3D Vector.
Definition: CVGeom.h:798
std::string name
Eigen::Matrix3d rotation
Definition: VoxelGridIO.cpp:27
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
virtual void setVisible(bool state)
Sets entity visibility.
Vector3Tpl< T > getTranslationAsVec3D() const
Returns a copy of the translation as a CCVector3.
bool fromFile(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads data from binary stream.
bool toFile(QFile &out, short dataVersion) const override
Saves data to binary stream.
void setTranslation(const Vector3Tpl< float > &Tr)
Sets translation (float version)
short minimumFileVersion() const override
Returns the minimum file version required to save this instance.
virtual void toIdentity()
Sets matrix to identity.
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
virtual short minimumFileVersion_MeOnly() const
virtual bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap)
Loads own object data.
int getIndex() const
Returns index relatively to its parent or -1 if no parent.
Definition: ecvHObject.cpp:695
virtual bool toFile_MeOnly(QFile &out, short dataVersion) const
Save own object data.
QString getViewId() const
Definition: ecvHObject.h:225
virtual void applyGLTransformation(const ccGLMatrix &trans)
Applies a GL transformation to the entity.
Definition: ecvHObject.cpp:944
virtual bool addChild(ccHObject *child, int dependencyFlags=DP_PARENT_OF_OTHER, int insertIndex=-1)
Adds a child.
Definition: ecvHObject.cpp:534
bool getInterpolatedTransformation(double index, ccIndexedTransformation &trans, double maxIndexDistForInterpolation=DBL_MAX) const
void sort()
Sorts transformations based on their index.
virtual unsigned getUniqueID() const
Returns object unique ID.
Definition: ecvObject.h:86
virtual void setEnabled(bool state)
Sets the "enabled" property.
Definition: ecvObject.h:102
Generic sensor interface.
Definition: ecvSensor.h:27
short minimumFileVersion_MeOnly() const override
Definition: ecvSensor.cpp:155
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
Definition: ecvSensor.cpp:161
PointCoordinateType m_scale
Sensor graphic representation scale.
Definition: ecvSensor.h:169
virtual void hideShowDrawings(CC_DRAW_CONTEXT &context)
Definition: ecvSensor.cpp:56
ccSensor(QString name)
Default constructor.
Definition: ecvSensor.cpp:12
virtual void applyGLTransformation(const ccGLMatrix &trans) override
Applies a GL transformation to the entity.
Definition: ecvSensor.cpp:61
virtual bool applyViewport()
Apply sensor 'viewport' to a 3D view.
Definition: ecvSensor.cpp:198
bool addPosition(ccGLMatrix &trans, double index)
Adds a new position (shortcut)
Definition: ecvSensor.cpp:32
bool getActiveAbsoluteCenter(CCVector3 &vec) const
Gets currently active absolute position.
Definition: ecvSensor.cpp:102
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
Definition: ecvSensor.cpp:123
virtual void clearDrawings()
Definition: ecvSensor.cpp:54
ccIndexedTransformationBuffer * m_posBuffer
Positions buffer (optional)
Definition: ecvSensor.h:152
ccGLMatrix m_rigidTransformation
Rigid transformation between this sensor and its associated positions.
Definition: ecvSensor.h:158
double m_activeIndex
Active index (for displayed position, etc.)
Definition: ecvSensor.h:161
bool getAbsoluteTransformation(ccIndexedTransformation &trans, double index) const
Definition: ecvSensor.cpp:78
void getIndexBounds(double &minIndex, double &maxIndex) const
Gets index boundaries (shortcut)
Definition: ecvSensor.cpp:69
bool getActiveAbsoluteTransformation(ccIndexedTransformation &trans) const
Gets currently active absolute transformation.
Definition: ecvSensor.cpp:90
ecvColor::Rgb m_color
Color of the sensor.
Definition: ecvSensor.h:166
bool getActiveAbsoluteRotation(ccGLMatrix &rotation) const
Gets currently active rotation matrix (without translation)
Definition: ecvSensor.cpp:111
QMultiMap< unsigned, unsigned > LoadedIDMap
Map of loaded unique IDs (old ID --> new ID)
static bool ReadError()
Sends a custom error message (read error) and returns 'false'.
static bool WriteError()
Sends a custom error message (write error) and returns 'false'.
static void CoordsFromDataStream(QDataStream &stream, int flags, PointCoordinateType *out, unsigned count=1)
static void RemoveEntities(const ccHObject *obj)
static bool HideShowEntities(const ccHObject *obj, bool visible)
unsigned char ColorCompType
Default color components type (R,G and B)
Definition: ecvColorTypes.h:29
ImGuiContext * context
Definition: Window.cpp:76
Colors namespace.
Definition: ecvColorTypes.h:32
constexpr Rgb green(0, MAX, 0)
Display context.