Welcome to mirror list, hosted at ThFree Co, Russian Federation.

v3d_linear_utils.h « Math « ssba « third_party « libmv « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 969ec99694f6db7e27e9a28fac3208f9f240f7fb (plain)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// -*- C++ -*-
/*
Copyright (c) 2008 University of North Carolina at Chapel Hill

This file is part of SSBA (Simple Sparse Bundle Adjustment).

SSBA is free software: you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.

SSBA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License along
with SSBA. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef V3D_LINEAR_UTILS_H
#define V3D_LINEAR_UTILS_H

#include "Math/v3d_linear.h"

#include <iostream>

namespace V3D
{

   template <typename Elem, int Size>
   struct InlineVector : public InlineVectorBase<Elem, Size>
   {
   }; // end struct InlineVector

   template <typename Elem>
   struct Vector : public VectorBase<Elem>
   {
         Vector()
            : VectorBase<Elem>()
         { }

         Vector(unsigned int size)
            : VectorBase<Elem>(size)
         { }

         Vector(unsigned int size, Elem * values)
            : VectorBase<Elem>(size, values)
         { }

         Vector(Vector<Elem> const& a)
            : VectorBase<Elem>(a)
         { }

         Vector<Elem>& operator=(Vector<Elem> const& a)
         {
            (VectorBase<Elem>::operator=)(a);
            return *this;
         }

         Vector<Elem>& operator+=(Vector<Elem> const& rhs)
         {
            addVectorsIP(rhs, *this);
            return *this;
         }

         Vector<Elem>& operator*=(Elem scale)
         {
            scaleVectorsIP(scale, *this);
            return *this;
         }

         Vector<Elem> operator+(Vector<Elem> const& rhs) const
         {
            Vector<Elem> res(this->size());
            addVectors(*this, rhs, res);
            return res;
         }

         Vector<Elem> operator-(Vector<Elem> const& rhs) const
         {
            Vector<Elem> res(this->size());
            subtractVectors(*this, rhs, res);
            return res;
         }

         Elem operator*(Vector<Elem> const& rhs) const
         {
            return innerProduct(*this, rhs);
         }

   }; // end struct Vector

   template <typename Elem, int Rows, int Cols>
   struct InlineMatrix : public InlineMatrixBase<Elem, Rows, Cols>
   {
   }; // end struct InlineMatrix

   template <typename Elem>
   struct Matrix : public MatrixBase<Elem>
   {
         Matrix()
            : MatrixBase<Elem>()
         { }

         Matrix(unsigned int rows, unsigned int cols)
            : MatrixBase<Elem>(rows, cols)
         { }

         Matrix(unsigned int rows, unsigned int cols, Elem * values)
            : MatrixBase<Elem>(rows, cols, values)
         { }

         Matrix(Matrix<Elem> const& a)
            : MatrixBase<Elem>(a)
         { }

         Matrix<Elem>& operator=(Matrix<Elem> const& a)
         {
            (MatrixBase<Elem>::operator=)(a);
            return *this;
         }

         Matrix<Elem>& operator+=(Matrix<Elem> const& rhs)
         {
            addMatricesIP(rhs, *this);
            return *this;
         }

         Matrix<Elem>& operator*=(Elem scale)
         {
            scaleMatrixIP(scale, *this);
            return *this;
         }

         Matrix<Elem> operator+(Matrix<Elem> const& rhs) const
         {
            Matrix<Elem> res(this->num_rows(), this->num_cols());
            addMatrices(*this, rhs, res);
            return res;
         }

         Matrix<Elem> operator-(Matrix<Elem> const& rhs) const
         {
            Matrix<Elem> res(this->num_rows(), this->num_cols());
            subtractMatrices(*this, rhs, res);
            return res;
         }

   }; // end struct Matrix

//----------------------------------------------------------------------

   typedef InlineVector<float, 2>  Vector2f;
   typedef InlineVector<double, 2> Vector2d;
   typedef InlineVector<float, 3>  Vector3f;
   typedef InlineVector<double, 3> Vector3d;
   typedef InlineVector<float, 4>  Vector4f;
   typedef InlineVector<double, 4> Vector4d;

   typedef InlineMatrix<float, 2, 2>  Matrix2x2f;
   typedef InlineMatrix<double, 2, 2> Matrix2x2d;
   typedef InlineMatrix<float, 3, 3>  Matrix3x3f;
   typedef InlineMatrix<double, 3, 3> Matrix3x3d;
   typedef InlineMatrix<float, 4, 4>  Matrix4x4f;
   typedef InlineMatrix<double, 4, 4> Matrix4x4d;

   typedef InlineMatrix<float, 2, 3>  Matrix2x3f;
   typedef InlineMatrix<double, 2, 3> Matrix2x3d;
   typedef InlineMatrix<float, 3, 4>  Matrix3x4f;
   typedef InlineMatrix<double, 3, 4> Matrix3x4d;

   template <typename Elem>
   struct VectorArray
   {
         VectorArray(unsigned count, unsigned size)
            : _count(count), _size(size), _values(0), _vectors(0)
         {
            unsigned const nTotal = _count * _size;
            if (count > 0) _vectors = new Vector<Elem>[count];
            if (nTotal > 0) _values = new Elem[nTotal];
            for (unsigned i = 0; i < _count; ++i) new (&_vectors[i]) Vector<Elem>(_size, _values + i*_size);
         }

         VectorArray(unsigned count, unsigned size, Elem initVal)
            : _count(count), _size(size), _values(0), _vectors(0)
         {
            unsigned const nTotal = _count * _size;
            if (count > 0) _vectors = new Vector<Elem>[count];
            if (nTotal > 0) _values = new Elem[nTotal];
            for (unsigned i = 0; i < _count; ++i) new (&_vectors[i]) Vector<Elem>(_size, _values + i*_size);
            std::fill(_values, _values + nTotal, initVal);
         }

         ~VectorArray()
         {
            delete [] _values;
            delete [] _vectors;
         }

         unsigned count() const { return _count; }
         unsigned size()  const { return _size; }

         //! Get the submatrix at position ix
         Vector<Elem> const& operator[](unsigned ix) const
         {
            return _vectors[ix];
         }

         //! Get the submatrix at position ix
         Vector<Elem>& operator[](unsigned ix)
         {
            return _vectors[ix];
         }

      protected:
         unsigned       _count, _size;
         Elem         * _values;
         Vector<Elem> * _vectors;

      private:
         VectorArray(VectorArray const&);
         void operator=(VectorArray const&);
   };

   template <typename Elem>
   struct MatrixArray
   {
         MatrixArray(unsigned count, unsigned nRows, unsigned nCols)
            : _count(count), _rows(nRows), _columns(nCols), _values(0), _matrices(0)
         {
            unsigned const nTotal = _count * _rows * _columns;
            if (count > 0) _matrices = new Matrix<Elem>[count];
            if (nTotal > 0) _values = new double[nTotal];
            for (unsigned i = 0; i < _count; ++i)
               new (&_matrices[i]) Matrix<Elem>(_rows, _columns, _values + i*(_rows*_columns));
         }

         ~MatrixArray()
         {
            delete [] _matrices;
            delete [] _values;
         }

         //! Get the submatrix at position ix
         Matrix<Elem> const& operator[](unsigned ix) const
         {
            return _matrices[ix];
         }

         //! Get the submatrix at position ix
         Matrix<Elem>& operator[](unsigned ix)
         {
            return _matrices[ix];
         }

         unsigned count()    const { return _count; }
         unsigned num_rows() const { return _rows; }
         unsigned num_cols() const { return _columns; }

      protected:
         unsigned       _count, _rows, _columns;
         double       * _values;
         Matrix<Elem> * _matrices;

      private:
         MatrixArray(MatrixArray const&);
         void operator=(MatrixArray const&);
   };

//----------------------------------------------------------------------

   template <typename Elem, int Size>
   inline InlineVector<Elem, Size>
   operator+(InlineVector<Elem, Size> const& v, InlineVector<Elem, Size> const& w)
   {
      InlineVector<Elem, Size> res;
      addVectors(v, w, res);
      return res;
   }

   template <typename Elem, int Size>
   inline InlineVector<Elem, Size>
   operator-(InlineVector<Elem, Size> const& v, InlineVector<Elem, Size> const& w)
   {
      InlineVector<Elem, Size> res;
      subtractVectors(v, w, res);
      return res;
   }

   template <typename Elem, int Size>
   inline InlineVector<Elem, Size>
   operator*(Elem scale, InlineVector<Elem, Size> const& v)
   {
      InlineVector<Elem, Size> res;
      scaleVector(scale, v, res);
      return res;
   }

   template <typename Elem, int Rows, int Cols>
   inline InlineVector<Elem, Rows>
   operator*(InlineMatrix<Elem, Rows, Cols> const& A, InlineVector<Elem, Cols> const& v)
   {
      InlineVector<Elem, Rows> res;
      multiply_A_v(A, v, res);
      return res;
   }

   template <typename Elem, int RowsA, int ColsA, int ColsB>
   inline InlineMatrix<Elem, RowsA, ColsB>
   operator*(InlineMatrix<Elem, RowsA, ColsA> const& A, InlineMatrix<Elem, ColsA, ColsB> const& B)
   {
      InlineMatrix<Elem, RowsA, ColsB> res;
      multiply_A_B(A, B, res);
      return res;
   }

   template <typename Elem, int Rows, int Cols>
   inline InlineMatrix<Elem, Cols, Rows>
   transposedMatrix(InlineMatrix<Elem, Rows, Cols> const& A)
   {
      InlineMatrix<Elem, Cols, Rows> At;
      makeTransposedMatrix(A, At);
      return At;
   }

   template <typename Elem>
   inline InlineMatrix<Elem, 3, 3>
   invertedMatrix(InlineMatrix<Elem, 3, 3> const& A)
   {
      Elem a = A[0][0], b = A[0][1], c = A[0][2];
      Elem d = A[1][0], e = A[1][1], f = A[1][2];
      Elem g = A[2][0], h = A[2][1], i = A[2][2];

      Elem const det = a*e*i + b*f*g + c*d*h - c*e*g - b*d*i - a*f*h;

      InlineMatrix<Elem, 3, 3> res;
      res[0][0] = e*i-f*h; res[0][1] = c*h-b*i; res[0][2] = b*f-c*e;
      res[1][0] = f*g-d*i; res[1][1] = a*i-c*g; res[1][2] = c*d-a*f;
      res[2][0] = d*h-e*g; res[2][1] = b*g-a*h; res[2][2] = a*e-b*d;

      scaleMatrixIP(1.0/det, res);
      return res;
   }

   template <typename Elem>
   inline InlineVector<Elem, 2>
   makeVector2(Elem a, Elem b)
   {
      InlineVector<Elem, 2> res;
      res[0] = a; res[1] = b;
      return res;
   }

   template <typename Elem>
   inline InlineVector<Elem, 3>
   makeVector3(Elem a, Elem b, Elem c)
   {
      InlineVector<Elem, 3> res;
      res[0] = a; res[1] = b; res[2] = c;
      return res;
   }

   template <typename Vec>
   inline void
   displayVector(Vec const& v)
   {
      using namespace std;

      for (int r = 0; r < v.size(); ++r)
         cout << v[r] << " ";
      cout << endl;
   }

   template <typename Mat>
   inline void
   displayMatrix(Mat const& A)
   {
      using namespace std;

      for (int r = 0; r < A.num_rows(); ++r)
      {
         for (int c = 0; c < A.num_cols(); ++c)
            cout << A[r][c] << " ";
         cout << endl;
      }
   }

} // end namespace V3D

#endif