File: vtkMatrix4x4.h

package info (click to toggle)
vtk9 9.5.2%2Bdfsg4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 206,640 kB
  • sloc: cpp: 2,340,827; ansic: 327,116; python: 114,881; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; javascript: 1,261; makefile: 194; objc: 153; tcl: 59
file content (312 lines) | stat: -rw-r--r-- 9,339 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/**
 * @class   vtkMatrix4x4
 * @brief   represent and manipulate 4x4 transformation matrices
 *
 * vtkMatrix4x4 is a class to represent and manipulate 4x4 matrices.
 * Specifically, it is designed to work on 4x4 transformation matrices
 * found in 3D rendering using homogeneous coordinates [x y z w].
 * Many of the methods take an array of 16 doubles in row-major format.
 * Note that OpenGL stores matrices in column-major format, so the matrix
 * contents must be transposed when they are moved between OpenGL and VTK.
 * @sa
 * vtkTransform
 */

#ifndef vtkMatrix4x4_h
#define vtkMatrix4x4_h

#include "vtkCommonMathModule.h" // For export macro
#include "vtkObject.h"
#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO

VTK_ABI_NAMESPACE_BEGIN
class VTKCOMMONMATH_EXPORT VTK_MARSHALAUTO vtkMatrix4x4 : public vtkObject
{
public:
  /// The internal data is public for historical reasons. Do not use!
  double Element[4][4];

  /**
   * Construct a 4x4 identity matrix.
   */
  static vtkMatrix4x4* New();

  vtkTypeMacro(vtkMatrix4x4, vtkObject);
  void PrintSelf(ostream& os, vtkIndent indent) override;

  /**
   * Set the elements of the matrix to the same values as the elements
   * of the given source matrix.
   */
  void DeepCopy(const vtkMatrix4x4* source)
  {
    vtkMatrix4x4::DeepCopy(*this->Element, source);
    this->Modified();
  }

  /**
   * Set the elements of the given destination buffer to the same values
   * as the elements of the given source matrix.
   */
  static void DeepCopy(double destination[16], const vtkMatrix4x4* source)
  {
    vtkMatrix4x4::DeepCopy(destination, *source->Element);
  }

  /**
   * Copies the given source buffer to the given destination buffer.
   * The memory ranges must not overlap.  Does not affect any matrix.
   */
  static void DeepCopy(double destination[16], const double source[16]);

  /**
   * Non-static member function. Assigns *to* the matrix *from*
   * the given elements array.
   */
  void DeepCopy(const double elements[16])
  {
    vtkMatrix4x4::DeepCopy(*this->Element, elements);
    this->Modified();
  }

  /**
   * Set all of the elements to zero.
   */
  void Zero()
  {
    vtkMatrix4x4::Zero(*this->Element);
    this->Modified();
  }
  static void Zero(double elements[16]);

  /**
   * Set equal to Identity matrix
   */
  void Identity()
  {
    vtkMatrix4x4::Identity(*this->Element);
    this->Modified();
  }
  static void Identity(double elements[16]);

  /**
   * Returns true if this matrix is equal to the identity matrix.
   */
  bool IsIdentity();

  /**
   * Matrix Inversion (adapted from Richard Carling in "Graphics Gems,"
   * Academic Press, 1990).
   */
  static void Invert(const vtkMatrix4x4* in, vtkMatrix4x4* out)
  {
    vtkMatrix4x4::Invert(*in->Element, *out->Element);
    out->Modified();
  }
  void Invert() { vtkMatrix4x4::Invert(this, this); }
  static void Invert(const double inElements[16], double outElements[16]);

  /**
   * Transpose the matrix and put it into out.
   */
  static void Transpose(const vtkMatrix4x4* in, vtkMatrix4x4* out)
  {
    vtkMatrix4x4::Transpose(*in->Element, *out->Element);
    out->Modified();
  }
  void Transpose() { vtkMatrix4x4::Transpose(this, this); }
  static void Transpose(const double inElements[16], double outElements[16]);

  ///@{
  /**
   * Construct a matrix from a rotation
   */
  static void MatrixFromRotation(double angle, double x, double y, double z, vtkMatrix4x4* result);
  static void MatrixFromRotation(double angle, double x, double y, double z, double matrix[16]);
  ///@}

  /**
   * Given an orientation and position this function will fill in a matrix
   * representing the transformation from the pose to whatever space the pose was
   * defined in. For example if the position and orientation are in world
   * coordinates then this method would set the matrix to be PoseToWorld
   */
  static void PoseToMatrix(double pos[3], double ori[4], vtkMatrix4x4* mat);

  /**
   * Multiply a homogeneous coordinate by this matrix, i.e. out = A*in.
   * The in[4] and out[4] can be the same array.
   */
  void MultiplyPoint(const float in[4], float out[4])
  {
    vtkMatrix4x4::MultiplyPoint(*this->Element, in, out);
  }
  void MultiplyPoint(const double in[4], double out[4])
  {
    vtkMatrix4x4::MultiplyPoint(*this->Element, in, out);
  }

  static void MultiplyPoint(const double elements[16], const float in[4], float out[4]);
  static void MultiplyPoint(const double elements[16], const double in[4], double out[4]);

  /**
   * For use in Java or Python.
   */
  float* MultiplyPoint(const float in[4]) VTK_SIZEHINT(4) { return this->MultiplyFloatPoint(in); }
  double* MultiplyPoint(const double in[4]) VTK_SIZEHINT(4)
  {
    return this->MultiplyDoublePoint(in);
  }
  float* MultiplyFloatPoint(const float in[4]) VTK_SIZEHINT(4)
  {
    this->MultiplyPoint(in, this->FloatPoint);
    return this->FloatPoint;
  }
  double* MultiplyDoublePoint(const double in[4]) VTK_SIZEHINT(4)
  {
    this->MultiplyPoint(in, this->DoublePoint);
    return this->DoublePoint;
  }

  ///@{
  /**
   * Multiplies matrices a and b and stores the result in c.
   */
  static void Multiply4x4(const vtkMatrix4x4* a, const vtkMatrix4x4* b, vtkMatrix4x4* c);
  static void Multiply4x4(const double a[16], const double b[16], double c[16]);
  static void Multiply4x4(const double a[16], const double b[16], float c[16]);
  static void MultiplyAndTranspose4x4(const double a[16], const double b[16], float c[16]);
  ///@}

  /**
   * Compute adjoint of the matrix and put it into out.
   */
  void Adjoint(const vtkMatrix4x4* in, vtkMatrix4x4* out)
  {
    vtkMatrix4x4::Adjoint(*in->Element, *out->Element);
  }
  static void Adjoint(const double inElements[16], double outElements[16]);

  /**
   * Compute the determinant of the matrix and return it.
   */
  double Determinant() { return vtkMatrix4x4::Determinant(*this->Element); }
  static double Determinant(const double elements[16]);

  /**
   * Sets the element i,j in the matrix.
   */
  void SetElement(int i, int j, double value);

  /**
   * Returns the element i,j from the matrix.
   */
  double GetElement(int i, int j) const { return this->Element[i][j]; }

  /**
   * Returns the raw double array holding the matrix.
   */
  double* GetData() VTK_SIZEHINT(16) { return *this->Element; }

  /**
   * Returns the raw double array holding the matrix.
   */
  const double* GetData() const { return *this->Element; }

  /**
   * Copies data into the matrix.
   */
  void SetData(const double data[16]) { vtkMatrix4x4::DeepCopy(data); }

protected:
  vtkMatrix4x4() { vtkMatrix4x4::Identity(*this->Element); }
  ~vtkMatrix4x4() override = default;

  float FloatPoint[4];
  double DoublePoint[4];

private:
  vtkMatrix4x4(const vtkMatrix4x4&) = delete;
  void operator=(const vtkMatrix4x4&) = delete;
};

//----------------------------------------------------------------------------
// Multiplies matrices a and b and stores the result in c.
inline void vtkMatrix4x4::Multiply4x4(const double a[16], const double b[16], double c[16])
{
  double tmp[16];

  for (int i = 0; i < 16; i += 4)
  {
    for (int j = 0; j < 4; j++)
    {
      tmp[i + j] =
        a[i + 0] * b[j + 0] + a[i + 1] * b[j + 4] + a[i + 2] * b[j + 8] + a[i + 3] * b[j + 12];
    }
  }

  for (int k = 0; k < 16; k++)
  {
    c[k] = tmp[k];
  }
}

//----------------------------------------------------------------------------
// Multiplies matrices a and b and stores the result in c.
inline void vtkMatrix4x4::Multiply4x4(const double a[16], const double b[16], float c[16])
{
  for (int i = 0; i < 16; i += 4)
  {
    for (int j = 0; j < 4; j++)
    {
      c[i + j] =
        a[i + 0] * b[j + 0] + a[i + 1] * b[j + 4] + a[i + 2] * b[j + 8] + a[i + 3] * b[j + 12];
    }
  }
}

//----------------------------------------------------------------------------
// Multiplies matrices a and b and stores the result in c.
inline void vtkMatrix4x4::MultiplyAndTranspose4x4(
  const double a[16], const double b[16], float c[16])
{
  for (int i = 0; i < 4; i++)
  {
    for (int j = 0; j < 4; j++)
    {
      int it4 = i * 4;
      c[i + j * 4] = a[it4 + 0] * b[j + 0] + a[it4 + 1] * b[j + 4] + a[it4 + 2] * b[j + 8] +
        a[it4 + 3] * b[j + 12];
    }
  }
}

//----------------------------------------------------------------------------
inline void vtkMatrix4x4::Multiply4x4(const vtkMatrix4x4* a, const vtkMatrix4x4* b, vtkMatrix4x4* c)
{
  vtkMatrix4x4::Multiply4x4(*a->Element, *b->Element, *c->Element);
}

//----------------------------------------------------------------------------
inline void vtkMatrix4x4::SetElement(int i, int j, double value)
{
  if (this->Element[i][j] != value)
  {
    this->Element[i][j] = value;
    this->Modified();
  }
}

//----------------------------------------------------------------------------
inline bool vtkMatrix4x4::IsIdentity()
{
  double* M = *this->Element;
  return M[0] == 1.0 && M[1] == 0.0 && M[2] == 0.0 && M[3] == 0.0 && M[4] == 0.0 && M[5] == 1.0 &&
    M[6] == 0.0 && M[7] == 0.0 && M[8] == 0.0 && M[9] == 0.0 && M[10] == 1.0 && M[11] == 0.0 &&
    M[12] == 0.0 && M[13] == 0.0 && M[14] == 0.0 && M[15] == 1.0;
}

VTK_ABI_NAMESPACE_END
#endif