36 typedef uint64_t uint64; |
37 typedef uint64_t uint64; |
37 |
38 |
38 template<class T> using initlist = std::initializer_list<T>; |
39 template<class T> using initlist = std::initializer_list<T>; |
39 using std::vector; |
40 using std::vector; |
40 |
41 |
41 class matrix; |
|
42 |
|
43 enum Axis { X, Y, Z }; |
42 enum Axis { X, Y, Z }; |
44 static const Axis g_Axes[3] = {X, Y, Z}; |
43 static const Axis g_Axes[3] = {X, Y, Z}; |
|
44 |
|
45 // ============================================================================= |
|
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
47 // ============================================================================= |
|
48 // matrix |
|
49 // |
|
50 // A templated, mathematical N x N matrix |
|
51 // ============================================================================= |
|
52 template<int N> class matrix { |
|
53 public: |
|
54 // Constructors |
|
55 matrix () {} |
|
56 matrix (initlist<double> vals) { |
|
57 assert (vals.size() == N * N); |
|
58 memcpy (&m_vals[0], &(*vals.begin ()), sizeof m_vals); |
|
59 } |
|
60 |
|
61 matrix (double fillval) { |
|
62 for (short i = 0; i < (N * N); ++i) |
|
63 m_vals[i] = fillval; |
|
64 } |
|
65 |
|
66 matrix (double vals[]) { |
|
67 for (short i = 0; i < (N * N); ++i) |
|
68 m_vals[i] = vals[i]; |
|
69 } |
|
70 |
|
71 template<int M> matrix (matrix<M> other) { |
|
72 assert (M >= N); |
|
73 |
|
74 for (short i = 0; i < M; ++i) |
|
75 for (short j = 0; j < M; ++j) { |
|
76 const short idx = (i * M) + j; |
|
77 m_vals[idx] = other[idx]; |
|
78 } |
|
79 } |
|
80 |
|
81 matrix<N> mult (matrix<N> other) { |
|
82 matrix val; |
|
83 val.zero (); |
|
84 |
|
85 for (short i = 0; i < N; ++i) |
|
86 for (short j = 0; j < N; ++j) |
|
87 for (short k = 0; k < N; ++k) |
|
88 val[(i * N) + j] += m_vals[(i * N) + k] * other[(k * N) + j]; |
|
89 |
|
90 return val; |
|
91 } |
|
92 |
|
93 matrix<N>& operator= (matrix<N> other) { |
|
94 memcpy (&m_vals[0], &other.m_vals[0], sizeof (double) * N * N); |
|
95 return *this; |
|
96 } |
|
97 |
|
98 matrix<N> operator* (matrix<N> other) { |
|
99 return mult (other); |
|
100 } |
|
101 |
|
102 double& operator[] (const uint idx) { |
|
103 return m_vals[idx]; |
|
104 } |
|
105 |
|
106 const double& operator[] (const uint idx) const { |
|
107 return m_vals[idx]; |
|
108 } |
|
109 |
|
110 void zero () { |
|
111 memset (&m_vals[0], 0, sizeof (double) * N * N); |
|
112 } |
|
113 |
|
114 void puts () const { |
|
115 for (short i = 0; i < N; ++i) { |
|
116 for (short j = 0; j < N; ++j) |
|
117 printf ("%*f\t", 10, m_vals[(i * 3) + j]); |
|
118 |
|
119 printf ("\n"); |
|
120 } |
|
121 } |
|
122 |
|
123 str stringRep () const { |
|
124 str val; |
|
125 for (short i = 0; i < N * N; ++i) { |
|
126 if (i > 0) |
|
127 val += ' '; |
|
128 |
|
129 val.appendformat ("%s", ftoa (m_vals[i]).chars()); |
|
130 } |
|
131 |
|
132 return val; |
|
133 } |
|
134 |
|
135 private: |
|
136 double m_vals[N * N]; |
|
137 }; |
45 |
138 |
46 // ============================================================================= |
139 // ============================================================================= |
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
48 // ============================================================================= |
141 // ============================================================================= |
49 // vertex |
142 // vertex |
60 m_coords[X] = x; |
153 m_coords[X] = x; |
61 m_coords[Y] = y; |
154 m_coords[Y] = y; |
62 m_coords[Z] = z; |
155 m_coords[Z] = z; |
63 } |
156 } |
64 |
157 |
65 // ========================================================================= |
|
66 void move (vertex other) { |
158 void move (vertex other) { |
67 for (const Axis ax : g_Axes) |
159 for (const Axis ax : g_Axes) |
68 m_coords[ax] += other[ax]; |
160 m_coords[ax] += other[ax]; |
69 } |
161 } |
70 |
162 |
71 // ========================================================================= |
|
72 vertex& operator+= (vertex other) { |
163 vertex& operator+= (vertex other) { |
73 move (other); |
164 move (other); |
74 return *this; |
165 return *this; |
75 } |
166 } |
76 |
167 |
77 // ========================================================================= |
168 vertex operator/ (const double d) const { |
78 vertex operator/ (const double d) { |
|
79 vertex other (*this); |
169 vertex other (*this); |
80 return other /= d; |
170 return other /= d; |
81 } |
171 } |
82 |
172 |
83 // ========================================================================= |
|
84 vertex& operator/= (const double d) { |
173 vertex& operator/= (const double d) { |
85 for (const Axis ax : g_Axes) |
174 for (const Axis ax : g_Axes) |
86 m_coords[ax] /= d; |
175 m_coords[ax] /= d; |
87 return *this; |
176 return *this; |
88 } |
177 } |
89 |
178 |
90 // ========================================================================= |
|
91 vertex operator- () const { |
179 vertex operator- () const { |
92 return vertex (-m_coords[X], -m_coords[Y], -m_coords[Z]); |
180 return vertex (-m_coords[X], -m_coords[Y], -m_coords[Z]); |
93 } |
181 } |
94 |
182 |
95 // ========================================================================= |
183 double& coord (const ushort n) { |
96 double& coord (const ushort n) const { |
184 return m_coords[n]; |
97 return const_cast<double&> (m_coords[n]); |
185 } |
98 } |
186 |
99 |
187 const double& coord (const ushort n) const { |
100 double& operator[] (const Axis ax) const { |
188 return m_coords[n]; |
|
189 } |
|
190 |
|
191 double& operator[] (const Axis ax) { |
101 return coord ((ushort) ax); |
192 return coord ((ushort) ax); |
102 } |
193 } |
103 |
194 |
104 double& operator[] (const ushort n) const { |
195 const double& operator[] (const Axis ax) const { |
105 return coord (n); |
196 return coord ((ushort) ax); |
106 } |
197 } |
107 |
198 |
108 vertex midpoint (vertex& other); |
199 vertex midpoint (vertex& other); |
109 str stringRep (const bool mangled); |
200 str stringRep (const bool mangled); |
110 void transform (matrix mMatrix, vertex pos); |
201 void transform (matrix<3> matr, vertex pos); |
111 }; |
202 }; |
112 |
203 |
113 // ============================================================================= |
|
114 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
115 // ============================================================================= |
|
116 // matrix |
|
117 // |
|
118 // A mathematical 3x3 matrix |
|
119 // ============================================================================= |
|
120 class matrix { |
|
121 public: |
|
122 // Constructors |
|
123 matrix () {} |
|
124 matrix (std::vector<double> vals); |
|
125 matrix (double fillval); |
|
126 matrix (double a, double b, double c, |
|
127 double d, double e, double f, |
|
128 double g, double h, double i); |
|
129 |
|
130 matrix mult (matrix mOther); |
|
131 |
|
132 matrix& operator= (matrix mOther) { |
|
133 memcpy (&m_vals[0], &mOther.m_vals[0], sizeof m_vals); |
|
134 return *this; |
|
135 } |
|
136 |
|
137 matrix operator* (matrix mOther) { |
|
138 return mult (mOther); |
|
139 } |
|
140 |
|
141 inline double& operator[] (const uint uIndex) { |
|
142 return m_vals[uIndex]; |
|
143 } |
|
144 |
|
145 void zero (); |
|
146 void testOutput (); |
|
147 str stringRep (); |
|
148 |
|
149 private: |
|
150 double m_vals[9]; |
|
151 }; |
|
152 |
|
153 #endif // TYPES_H |
204 #endif // TYPES_H |