summaryrefslogtreecommitdiff
path: root/hw2/src/transformation3d.cpp
blob: bde9caa48d8601e3e12789ca78822a6baae7fd60 (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
/******************************************************************/
/* 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 "transformation3d.h"


//////////////////
// Constructors //
//////////////////
transformation3d::transformation3d(void)
{
  _translation = vec3d();
  _transformation = mat3d(1.0f);
  _inverseTransformation = mat3d(1.0f);
}


transformation3d::transformation3d(const vec3d& translation, const mat3d& transformation, const mat3d& inverseTransformation)
{
  _translation = translation;
  _transformation = transformation;
  _inverseTransformation = inverseTransformation;
}


transformation3d::transformation3d(const transformation3d& t)
{
  _translation = t._translation;
  _transformation = t._transformation;
  _inverseTransformation = t._inverseTransformation;
}


//////////////
// Operator //
//////////////
transformation3d& transformation3d::operator=(const transformation3d& t)
{
  _assign(t);
  return *this;
}


transformation3d transformation3d::operator*(const transformation3d& t) const
{
  return transformation3d( transformPoint(t._translation),
			   _transformation * t._transformation,
			   t._inverseTransformation * _inverseTransformation);
}


transformation3d& transformation3d::operator*=(const transformation3d& t)
{
  *this = *this * t;
  return *this;
}


//////////////
// Mutators //
//////////////
transformation3d& transformation3d::invert(void)
{
  _translation = _inverseTransformation * (-_translation);
  swap(_transformation, _inverseTransformation);
  return *this;
}

/////////////
// Methods //
/////////////
vec3d transformation3d::transformPoint(const vec3d& p) const
{
  // first transform, then translate
  vec3d transformed = _transformation * p;
  transformed += _translation;
  
  // Done.
  return transformed;
}


vec3d transformation3d::transformDirection(const vec3d& d) const
{
  // Only apply transformation
  vec3d transformed = _transformation * d;

  // Done.
  return transformed.normalize();
}


vec3d transformation3d::transformNormal(const vec3d& n) const
{
  // Don't apply translation.
  // n' = (_transformation^T)^-1 * n
  // n'^T = n^T * _transformation^-1
  // n'^T = n^T * _inverseTransformation
  vec3d transformed = n * _inverseTransformation;
  
  // Done.
  return transformed.normalize();
}


vec3d transformation3d::inverseTransformPoint(const vec3d& p) const
{
  // for undo translation, then invert the transformation
  vec3d transformed = p - _translation;
  transformed = _inverseTransformation * transformed;

  // Done.
  return transformed;
}


vec3d transformation3d::inverseTransformDirection(const vec3d& d) const
{
  // Only invert the transformation
  vec3d transformed = _inverseTransformation * d;

  // Done.
  return transformed.normalize();
}


vec3d transformation3d::inverseTransformNormal(const vec3d& n) const
{
  // Don't apply translation. Undo (transformation^T)^-1
  vec3d transformed = n * _transformation;

  // Done.
  return transformed.normalize();
}


///////////////////////
// Protected Methods //
///////////////////////
void transformation3d::_swap(transformation3d& t)
{
  std::swap(_translation, t._translation);
  std::swap(_transformation, t._transformation);
  std::swap(_inverseTransformation, t._inverseTransformation);
}


void transformation3d::_assign(const transformation3d& t)
{
  // avoid copying when self-assigning
  if(&t == this) return;

  // Copy
  _translation = t._translation;
  _transformation = t._transformation;
  _inverseTransformation = t._inverseTransformation;

  // Done.
}


void transformation3d::_print(std::ostream& s) const
{
  s << "{";
  s << "Translation = " << _translation << ", ";
  s << "Transformation = " << _transformation << ", ";
  s << "Inverse Trans. = " << _inverseTransformation;
  s << "}";
}