summaryrefslogtreecommitdiff
path: root/hw4/src/mat3d.cpp
blob: 767477794503ebf611b63949684551a4a8f9de0d (plain) (blame)
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
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527     */
/* without consent of either the College of William & Mary or     */
/* Pieter Peers.                                                  */
/******************************************************************/
#include <cassert>
#include <algorithm>

#include "mat3d.h"


/////////////////
// Constructor //
/////////////////
mat3d::mat3d(void)
{
  clear(0.0f);
}


mat3d::mat3d(mat3d::value_type diag)
  : mat3d()
{
  setDiagonal(diag);
}


mat3d::mat3d(const vec3d& X, const vec3d& Y, const vec3d& Z)
{
  for(size_type i=0; i < 3; i++)
  {
    this->operator()(i,0) = X[i];
    this->operator()(i,1) = Y[i];
    this->operator()(i,2) = Z[i];
  }
}


mat3d::mat3d(const mat3d& m)
{
  _data = m._data;
}


////////////////
// Inspectors //
////////////////
mat3d::iterator mat3d::begin(void)
{
  return _data.begin();
}


mat3d::const_iterator mat3d::begin(void) const
{
  return _data.begin();
}


mat3d::iterator mat3d::end(void)
{
  return _data.end();
}


mat3d::const_iterator mat3d::end(void) const
{
  return _data.end();
}


//////////////
// Mutators //
//////////////
void mat3d::clear(mat3d::value_type value)
{
  std::fill(begin(), end(), value);
}


void mat3d::setDiagonal(mat3d::value_type value)
{
  (*this)(0,0) = (*this)(1,1) = (*this)(2,2) = value;
}


mat3d& mat3d::transpose(void)
{
  for(size_type row=0; row < height(); row++)
    for(size_type column=row+1; column < width(); column++)
      std::swap( (*this)(row,column), (*this)(column,row) );
  return *this;
}



///////////////
// Operators //
///////////////
mat3d& mat3d::operator=(const mat3d& m)
{
  _assign(m);
  return *this;
}


mat3d::reference mat3d::operator()(mat3d::size_type row, mat3d::size_type col)
{
  assert(row < height() && col < width());
  return _data[ row*width() + col];
}


mat3d::const_reference mat3d::operator()(mat3d::size_type row, mat3d::size_type col) const
{
  assert(row < height() && col < width());
  return _data[ row*width() + col];
}


mat3d mat3d::operator+(const mat3d& m) const
{
  mat3d result;
  std::transform(begin(), end(), m.begin(), result.begin(), [](const_reference a, const_reference b)
  {
    return a+b;
  });
  return result;
}


mat3d mat3d::operator-(const mat3d& m) const 
{
  mat3d result;
  std::transform(begin(), end(), m.begin(), result.begin(), [](const_reference a, const_reference b)
  {
    return a-b;
  });
  return result;
}


mat3d mat3d::operator*(const mat3d& m) const 
{ 
  mat3d result;
  for(size_type i=0; i < result.height(); i++)
    for(size_type j=0; j < result.width(); j++)
      for(size_type k=0; k < width(); k++)
	result(i,j) += (*this)(i,k) * m(k, j);
  return result;
}


vec3d mat3d::operator*(const vec3d& v) const 
{ 
  vec3d result;
  for(size_type i=0; i < height(); i++)
    for(size_type j=0; j < width(); j++)
      result[i] += v[j] * (*this)(i,j);
  return result;
}


mat3d mat3d::operator*(mat3d::value_type scale) const 
{
  mat3d result;
  std::transform(begin(), end(), result.begin(), [&](const_reference v)
  {
    return v * scale;
  });
  return result;
}

mat3d mat3d::operator/(mat3d::value_type scale) const 
{
  mat3d result;
  std::transform(begin(), end(), result.begin(), [&](const_reference v)
  {
    return v / scale;
  });
  return result;
}


mat3d& mat3d::operator+=(const mat3d& m) 
{ 
  std::transform(begin(), end(), m.begin(), begin(), [](const_reference a, const_reference b)
  {
    return a+b;
  });
  return *this; 
}


mat3d& mat3d::operator-=(const mat3d& m) 
{
  std::transform(begin(), end(), m.begin(), begin(), [](const_reference a, const_reference b)
  {
    return a-b;
  });
  return *this; 
}


mat3d& mat3d::operator*=(const mat3d& m) 
{
  *this = *this * m;
  return *this;
}


mat3d& mat3d::operator*=(mat3d::value_type scale) 
{
  std::for_each(begin(), end(), [&](reference v)
  {
    v *= scale;
  });
  return *this; 
}


mat3d& mat3d::operator/=(mat3d::value_type scale) 
{
  std::for_each(begin(), end(), [&](reference v)
  {
    v /= scale;
  });
  return *this; 
}
  

/////////////////////
// Private Methods //
/////////////////////
void mat3d::_swap(mat3d& m)
{
  if(&m == this) return;
  std::swap(_data, m._data);
}


void mat3d::_assign(const mat3d& m)
{
  if(&m == this) return;
  _data = m._data;
}


vec3d mat3d::_premultiply(const vec3d& v) const
{
  // result = v * *this
  vec3d result;
  for(size_type i=0; i < height(); i++)
    for(size_type j=0; j < width(); j++)
      result[j] += v[i] * (*this)(i,j);
  return result;
}