summaryrefslogtreecommitdiff
path: root/hw2/include/mat3d.h
blob: e70ece406d2324fcf0bd9f8d90b0b2560dc6fc77 (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
/******************************************************************/
/* 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.                                                  */
/******************************************************************/
#ifndef _MAT3D_H_
#define _MAT3D_H_

#include <array>
#include <ostream>

#include "vec3d.h"

class mat3d {
 public:
  /////////////
  // Typedef //
  /////////////
  typedef float                                      value_type;
  typedef value_type&                                reference;
  typedef const value_type&                          const_reference;
  typedef std::array<value_type,9>::iterator         iterator;
  typedef std::array<value_type,9>::const_iterator   const_iterator;
  typedef std::size_t                                difference;
  typedef std::size_t                                size_type;

  /////////////////
  // Constructor //
  /////////////////
  mat3d(void);                                              // all elements are set to 0
  explicit mat3d(value_type diag);                          // all elements are set to 0, the diagonal elements are set to 'diag'
  mat3d(const vec3d& X, const vec3d& Y, const vec3d& Z);    // set columns to X, Y, and Z
  mat3d(const mat3d& m);                                    // copy constructor

  ////////////////
  // Inspectors //
  ////////////////
  static size_type width(void)  { return 3; }
  static size_type height(void) { return 3; }
  static size_type size(void)   { return 9; }

  iterator begin(void);
  const_iterator begin(void) const;
  iterator end(void);
  const_iterator end(void) const;

  //////////////
  // Mutators //
  //////////////
  void clear(value_type value=0.0f);
  void setDiagonal(value_type value=1.0f);

  mat3d& transpose(void);

  ///////////////
  // Operators //
  ///////////////
  mat3d& operator=(const mat3d& m);

  reference operator()(size_type row, size_type col);
  const_reference operator()(size_type row, size_type col) const;

  mat3d operator+(const mat3d& m) const;
  mat3d operator-(const mat3d& m) const;
  mat3d operator*(const mat3d& m) const;
  vec3d operator*(const vec3d& v) const;
  mat3d operator*(value_type scale) const;
  mat3d operator/(value_type scale) const;

  mat3d& operator+=(const mat3d& m);
  mat3d& operator-=(const mat3d& m);
  mat3d& operator*=(const mat3d& m);
  mat3d& operator*=(value_type scale);
  mat3d& operator/=(value_type scale);
  
  /////////////
  // Friends //
  /////////////
  friend void swap(mat3d& a, mat3d& b) { a._swap(b); }

  friend vec3d operator*(const vec3d& v, const mat3d& m)   { return m._premultiply(v); }
  friend mat3d operator*(value_type scale, const mat3d& m) { return (m * scale); }

  friend mat3d transpose(mat3d& m) { mat3d n(m); return n.transpose(); }

  friend std::ostream& operator<<(std::ostream& s, const mat3d& m)
  {
    s << "[[" << m(0,0) << ", " << m(0,1) << ", " << m(0,2) << "],[";
    s         << m(1,0) << ", " << m(1,1) << ", " << m(1,2) << "],[";
    s         << m(2,0) << ", " << m(2,1) << ", " << m(2,2) << "]]";
    return s;
  }
  
 private:
  /////////////////////
  // Private Methods //
  /////////////////////
  void _swap(mat3d& m);
  void _assign(const mat3d& m);

  vec3d _premultiply(const vec3d& v) const;

  //////////////////////////
  // Private Data Members //
  //////////////////////////
  std::array<value_type, 9> _data;
};

#endif /* _MAT3D_H_ */