CGM Objects Library
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Matrix.h
1 // Matrix.h: interface for the CMatrix class.
2 //
4 
5 #if !defined(LST_MATRIX_H__INCLUDED_)
6 #define LST_MATRIX_H__INCLUDED_
7 
8 #pragma once
9 
10 #include "Utility/DPoint.h"
11 #include <cmath>
12 
13 namespace Larson
14 {
16  class CMatrix
17  {
18  public:
22  CMatrix(double* mat) { setTransform(mat); };
24  CMatrix(double r0c0, double r0c1, double r1c0, double r1c1, double r2c0, double r2c1)
25  {
26  setTransform(r0c0, r0c1, r1c0, r1c1, r2c0, r2c1);
27  }
28 
29  ~CMatrix() { };
30 
32  void setToIdentity() { matrix[0] = 1.0; matrix[1] = 0.0;
33  matrix[2] = 0.0; matrix[3] = 1.0;
34  matrix[4] = 0.0; matrix[5] = 0.0;
35  };
36 
38  // a b c d e f
39  void setTransform(double r0c0, double r0c1, double r1c0, double r1c1, double r2c0, double r2c1)
40  {
41  matrix[0] = r0c0; matrix[1] = r0c1; // a b
42  matrix[2] = r1c0; matrix[3] = r1c1; // c d
43  matrix[4] = r2c0; matrix[5] = r2c1; // e f
44  }
45 
47  void setTransform(double* mat) {
48 
49  matrix[0] = mat[0];
50  matrix[1] = mat[1];
51  matrix[2] = mat[2];
52  matrix[3] = mat[3];
53  matrix[4] = mat[4];
54  matrix[5] = mat[5];
55  }
56 
58  void multiply(CMatrix &mat)
59  {
60  double a1 = matrix[0];
61  double b1 = matrix[1];
62  double c1 = matrix[2];
63  double d1 = matrix[3];
64 
65  matrix[0] = mat.matrix[0] * a1 + mat.matrix[1] * c1;
66  matrix[1] = mat.matrix[0] * b1 + mat.matrix[1] * d1;
67  matrix[2] = mat.matrix[2] * a1 + mat.matrix[3] * c1;
68  matrix[3] = mat.matrix[2] * b1 + mat.matrix[3] * d1;
69  matrix[4] = mat.matrix[4] * a1 + mat.matrix[5] * c1 + matrix[4];
70  matrix[5] = mat.matrix[4] * b1 + mat.matrix[5] * d1 + matrix[5];
71  }
72 
74  void scale(DPoint scl) { scale(scl.x, scl.y); };
75  void scale(double x, double y)
76  {
77  CMatrix scaleMatrix( x, 0.0, 0.0, y, 0.0, 0.0);
78 
79  multiply(scaleMatrix);
80  }
81 
83  void rotate(double radians)
84  {
85  CMatrix rotateMatrix(cos(radians), sin(radians), -sin(radians), cos(radians), 0.0, 0.0);
86 
87  multiply(rotateMatrix);
88  }
89 
91  void translate(DPoint trans) { translate(trans.x, trans.y); };
92  void translate(double x, double y)
93  {
94  CMatrix transMatrix(1.0, 0.0, 0.0, 1.0, x, y);
95 
96  multiply(transMatrix);
97  }
98 
100  bool operator==(CMatrix& mat)
101  {
102  int i;
103 
104  for (i = 0; i < 6; i++)
105  {
106  if (matrix[i] != mat.matrix[i])
107  return false;
108  }
109  return true;
110  }
111 
113  bool operator!=(CMatrix& mat)
114  {
115  int i;
116 
117  for (i = 0; i < 6; i++)
118  {
119  if (matrix[i] != mat.matrix[i])
120  return true;
121  }
122  return false;
123  }
124 
125  void operator*=(CMatrix& mat) { multiply(mat); };
126 
128  inline DPoint transformPt(DPoint point)
129  {
130  DPoint newPt;
131 
132  newPt.x = (point.x * matrix[0]) + (point.y * matrix[2]) + matrix[4]; // ax + cy + tx
133  newPt.y = (point.x * matrix[1]) + (point.y * matrix[3]) + matrix[5]; // bx + dy + ty
134 
135  return newPt;
136  }
137 
139  inline DPoint transformVec(DPoint vec)
140  {
141  DPoint newVec;
142 
143  newVec.x = (vec.x * matrix[0]) + (vec.y * matrix[2]);
144  newVec.y = (vec.x * matrix[1]) + (vec.y * matrix[3]);
145 
146  return newVec;
147  }
148 
150  double matrix[6];
151  };
152 };
153 
154 #endif // LST_MATRIX_H__INCLUDED_
void setTransform(double *mat)
set matrix to specified matrix
Definition: Matrix.h:47
void rotate(double radians)
rotate matrix
Definition: Matrix.h:83
void setTransform(double r0c0, double r0c1, double r1c0, double r1c1, double r2c0, double r2c1)
set matrix to specified values
Definition: Matrix.h:39
CMatrix(double *mat)
constructor - intialize to specified matrix
Definition: Matrix.h:22
DPoint transformPt(DPoint point)
translate a point
Definition: Matrix.h:128
double matrix[6]
matrix values
Definition: Matrix.h:150
void multiply(CMatrix &mat)
multiply matrix
Definition: Matrix.h:58
void scale(DPoint scl)
scale matrix
Definition: Matrix.h:74
bool operator==(CMatrix &mat)
matrix equals operator
Definition: Matrix.h:100
void translate(DPoint trans)
translate matrix
Definition: Matrix.h:91
void setToIdentity()
set indentity matrix
Definition: Matrix.h:32
CMatrix(double r0c0, double r0c1, double r1c0, double r1c1, double r2c0, double r2c1)
constructor - intialize to specified values
Definition: Matrix.h:24
CMatrix – transform matrix class.
Definition: Matrix.h:16
DPoint transformVec(DPoint vec)
translate a vector
Definition: Matrix.h:139
bool operator!=(CMatrix &mat)
matrix not-equals operator
Definition: Matrix.h:113
CMatrix()
Default constructor - intialize to indentity matrix.
Definition: Matrix.h:20