ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvIndexedTransformation.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 
9 
10 // Qt
11 #include <QtCompat.h>
12 
13 #include <QFile>
14 
15 // System
16 #include <assert.h>
17 
18 #include <string>
19 
21 
23  : ccGLMatrix(matrix), m_index(0) {}
24 
26  double index)
27  : ccGLMatrix(matrix), m_index(index) {}
28 
30  const ccIndexedTransformation& trans)
31  : ccGLMatrix(trans), m_index(trans.m_index) {}
32 
34  int precision /*=12*/) const {
35  QFile fp(filename);
36  if (!fp.open(QFile::WriteOnly | QFile::Text)) return false;
37 
38  QTextStream stream(&fp);
39  stream.setRealNumberNotation(QTextStream::FixedNotation);
40  stream.setRealNumberPrecision(precision);
41 
42  // save transformation first (so that it can be loaded as a ccGLMatrix!)
43  for (unsigned i = 0; i < 4; ++i) {
44  stream << m_mat[i] << " " << m_mat[i + 4] << " " << m_mat[i + 8] << " "
45  << m_mat[i + 12] << QtCompat::endl;
46  }
47  // save index next
48  stream << m_index;
49 
50  return (fp.error() == QFile::NoError);
51 }
52 
54  QFile fp(filename);
55  if (!fp.open(QFile::ReadOnly | QFile::Text)) return false;
56 
57  QTextStream stream(&fp);
58 
59  // read the transformation first
60  for (unsigned i = 0; i < 4; ++i) {
61  stream >> m_mat[i];
62  stream >> m_mat[i + 4];
63  stream >> m_mat[i + 8];
64  stream >> m_mat[i + 12];
65  }
66  // read index
67  stream >> m_index;
68 
69  return (fp.error() == QFile::NoError);
70 }
71 
73  const ccGLMatrix& M) const {
74  return ccIndexedTransformation(*static_cast<const ccGLMatrix*>(this) * M,
75  m_index);
76 }
77 
79  const ccGLMatrix& M) {
80  ccGLMatrix temp = (*this) * M;
81  (*this) = temp;
82 
83  return (*this);
84 }
85 
87  const CCVector3& T) {
88  *static_cast<ccGLMatrix*>(this) += T;
89 
90  return (*this);
91 }
92 
94  const CCVector3& T) {
95  *static_cast<ccGLMatrix*>(this) -= T;
96 
97  return (*this);
98 }
99 
101  ccIndexedTransformation t(*this);
102  t.transpose();
103 
104  return t;
105 }
106 
108  ccIndexedTransformation t(*this);
109  t.invert();
110 
111  return t;
112 }
113 
115  double index,
116  const ccIndexedTransformation& trans1,
117  const ccIndexedTransformation& trans2) {
118  double dt = trans2.getIndex() - trans1.getIndex();
119  if (dt == 0.0) {
120  assert(index == trans1.getIndex());
121  return trans1;
122  }
123 
124  // we compute the transformation matrix between trans1 and trans2
125  double t = (index - trans1.getIndex()) / dt;
126  assert(t >= 0 && t <= 1);
127 
129  static_cast<PointCoordinateType>(t), trans1, trans2);
130 
131  return ccIndexedTransformation(mat, index);
132 }
133 
134 bool ccIndexedTransformation::toFile(QFile& out, short dataVersion) const {
135  if (!ccGLMatrix::toFile(out, dataVersion)) return false;
136 
137  assert(out.isOpen() && (out.openMode() & QIODevice::WriteOnly));
138 
139  // index (dataVersion>=34)
140  if (dataVersion >= 34) {
141  if (out.write((const char*)&m_index, sizeof(double)) < 0)
142  return WriteError();
143  }
144 
145  return true;
146 }
147 
149  return std::max(static_cast<short>(34), ccGLMatrix::minimumFileVersion());
150 }
151 
153  short dataVersion,
154  int flags,
155  LoadedIDMap& oldToNewIDMap) {
156  if (!ccGLMatrix::fromFile(in, dataVersion, flags, oldToNewIDMap))
157  return false;
158 
159  assert(in.isOpen() && (in.openMode() & QIODevice::ReadOnly));
160 
161  if (dataVersion < 34) return CorruptError();
162 
163  // index (dataVersion>=34)
164  if (in.read((char*)&m_index, sizeof(double)) < 0) return ReadError();
165 
166  return true;
167 }
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
std::string filename
bool fromFile(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
bool toFile(QFile &out, short dataVersion) const override
void invert()
Inverts transformation.
static ccGLMatrixTpl Interpolate(float coef, const ccGLMatrixTpl< float > &glMat1, const ccGLMatrixTpl< float > &glMat2)
Interpolates two matrices at relative position 'coef'.
short minimumFileVersion() const override
void transpose()
Transposes matrix (in place)
float m_mat[OPENGL_MATRIX_SIZE]
Internal 4x4 GL-style matrix data.
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
double getIndex() const
Returns associated index (e.g. timestamp)
virtual bool toAsciiFile(QString filename, int precision=12) const
Saves matrix to an ASCII file.
static ccIndexedTransformation Interpolate(double interpIndex, const ccIndexedTransformation &trans1, const ccIndexedTransformation &trans2)
Interpolates two transformations at an absolute position (index)
ccIndexedTransformation transposed() const
Returns transposed transformation.
ccIndexedTransformation operator*(const ccGLMatrix &mat) const
Multiplication by a ccGLMatrix operator.
ccIndexedTransformation & operator*=(const ccGLMatrix &mat)
(in place) Multiplication by a ccGLMatrix operator
virtual bool fromAsciiFile(QString filename)
Loads matrix from an ASCII file.
bool toFile(QFile &out, short dataVersion) const override
Saves data to binary stream.
ccIndexedTransformation inverse() const
Returns inverse transformation.
ccIndexedTransformation()
Default constructor.
ccIndexedTransformation & operator+=(const CCVector3 &T)
(in place) Translation operator
short minimumFileVersion() const override
Returns the minimum file version required to save this instance.
ccIndexedTransformation & operator-=(const CCVector3 &T)
(in place) Translation operator
bool fromFile(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads data from binary stream.
double m_index
Associated index (e.g. timestamp)
static bool CorruptError()
Sends a custom error message (corrupted file) and returns 'false'.
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'.
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718