FaceGen 3 SDKs Reference
Loading...
Searching...
No Matches
matrixCTH.hpp
1//
2// Copyright (c) Singular Inversions Inc. 2000
3//
4// Authors: Andrew Beatty
5// Created: 30/06/00
6//
7// Square matrices for representing transforms of projective space using
8// so-called homogeneous coordinates in which RP^N is represented by the
9// projection of an R^(N+1) vector onto the hyper-plane x_(N+1) = 1.
10//
11// Vectors in this representation are of length N with the N+1'th component
12// implied and equal to one.
13//
14// The matrix representation is (N+1) by (N+1) and is only unique to within
15// a scalar multiple.
16//
17// Multiplication of an RP^N matrix by an RP^N vector is performed by adding
18// the N+1'th component (1) to the vector then multiplying the (N+1)x(N+1)
19// matrix by that vector and re-projecting the vector.
20//
21// Matrix-matrix multiplication is not re-projected.
22//
23
24#ifndef FUT_MATRIXCTH_HPP
25#define FUT_MATRIXCTH_HPP
26
27#include "FgMatrixC.hpp"
28#include "matrixCT.hpp"
29#include "vect3.hpp"
30
31namespace Fg {
32
33template <class T,const int size>
35{
37
38 FutMatrixCTHC() {};
39 FutMatrixCTHC(const FutMatrixCTHC& m) : m_mat(m.m_mat) {};
40
41 FutMatrixCTHC(const Mat<T,size,size> & m)
42 {
43 for (uint ii=0; ii<size; ++ii)
44 for (uint jj=0; jj<size; ++jj)
45 m_mat.data[ii][jj] = m.rc(ii,jj);
46 }
47
48 FutMatrixCTHC(const FR2MatrixCTC<T,size-1,size-1>&, // Construct from
49 const FR2MatrixCTC<T,size-1,1>&); // linear plus trans.
50
51 inline T* operator[](size_t i) { return m_mat[i]; }
52 inline T const* operator[](size_t i) const { return m_mat[i]; }
53 inline void setZero() {m_mat.setZero();}
54 inline void setIdentity() {m_mat.setIdentity();}
55 inline FutMatrixCTHC transpose() const;
56
57 FutMatrixCTHC operator*(const FutMatrixCTHC&) const;
58 FR2MatrixCTC<T,size-1,1> operator*(const FR2MatrixCTC<T,size-1,1>&) const;
59 bool operator==(const FutMatrixCTHC& v) const
60 {return (m_mat == v.m_mat);}
61};
62
63// ***********************************************************************
64// ***********************************************************************
65
70
71// ***********************************************************************
72// ***********************************************************************
73
74template<class T,const int size>
76 const FR2MatrixCTC<T,size-1,size-1>& lin, // Linear component
77 const FR2MatrixCTC<T,size-1,1>& tran) // Translational component
78{
79 int row,col;
80 m_mat.setZero();
81 for (row=0; row<size-1; row++) {
82 for (col=0; col<size-1; col++) {
83 m_mat[row][col] = lin[row][col]; // Copy linear
84 }
85 m_mat[row][size-1] = tran[row][0]; // Copy translational
86 }
87 m_mat[size-1][size-1] = T(1);
88}
89
90template <class T,const int size>
91inline FutMatrixCTHC<T,size> FutMatrixCTHC<T,size>::operator*(
92 const FutMatrixCTHC& m) const
93{
94 FutMatrixCTHC<T,size> retval;
95 retval.m_mat = m_mat * m.m_mat;
96 return retval;
97}
98
99template <class T,const int size>
100FR2MatrixCTC<T,size-1,1>
101FutMatrixCTHC<T,size>::operator*(const FR2MatrixCTC<T,size-1,1> & vec) const
102{
103 FR2MatrixCTC<T,size-1,1> retval;
104 int row,col;
105 T acc;
106 for (row=0; row<size-1; row++) {
107 acc = T(0);
108 for (col=0; col<size-1; col++) {
109 acc += m_mat[row][col] * vec[col][0];
110 }
111 acc += m_mat[row][size-1];
112 retval[row][0] = acc;
113 }
114 acc = T(0);
115 for (col=0; col<size-1; col++)
116 acc += m_mat[size-1][col] * vec[col][0];
117 acc += m_mat[size-1][size-1];
118 if (acc != T(0)) { // Used to be an ASSERT but this was occasionally killing photofit:
119 for (col=0; col<size-1; col++)
120 retval[col][0] /= acc;
121 }
122 return retval;
123}
124
125// The operator* for the old FR2Vect3FC classes must be global since
126// they are defined only for specific instantiations of FutMatrixCTHC.
127// The example for FR2Vect3FC below can be used to create similar ones
128// as needed.
129template <class T>
130inline FR2Vect3TC<T> operator*(
131 const FutMatrixCTHC<T,4>& mat,
132 const FR2Vect3TC<T>& vec)
133{
134 FR2Vect3TC<T> retval;
135 FR2MatrixCTC<T,3,1> in, // Homogeneous vector representation
136 out;
137 in[0][0] = vec.x1;
138 in[1][0] = vec.x2;
139 in[2][0] = vec.x3;
140 out = mat * in;
141 retval.x1 = out[0][0];
142 retval.x2 = out[1][0];
143 retval.x3 = out[2][0];
144 return retval;
145}
146
147template <class T,const int size>
148std::ostream& operator<<(std::ostream& s,FutMatrixCTHC<T,size> m)
149{
150 for (int i=0; i<size; i++) {
151 for (int j=0; j<size; j++) {
152 s << m[i][j] << " "; }
153 s << "\n"; }
154 return s;
155}
156
157}
158
159#endif