-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOBJECT.CPP
More file actions
288 lines (257 loc) · 8.03 KB
/
OBJECT.CPP
File metadata and controls
288 lines (257 loc) · 8.03 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// Copyright (C) 2004 by Lakin Wecker And Corey Auger
// nikal@nucleus.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#include "Object.h"
// Overloaded matrix multiplication function. (friend).
Affine::Object Affine::operator*( const std::valarray<Affine::Scalar> &matrix, const Affine::Object &aObj ) {
assert( matrix.size() == 16 );
Scalar nAffine[4];
nAffine[0] = matrix[0] * aObj.myAffine[0] +
matrix[1]*aObj.myAffine[1] +
matrix[2]*aObj.myAffine[2] +
matrix[3]*aObj.myAffine[3];
nAffine[1] = matrix[4] * aObj.myAffine[0] +
matrix[5]*aObj.myAffine[1] +
matrix[6]*aObj.myAffine[2] +
matrix[7]*aObj.myAffine[3];
nAffine[2] = matrix[8] * aObj.myAffine[0] +
matrix[9]*aObj.myAffine[1] +
matrix[10]*aObj.myAffine[2] +
matrix[11]*aObj.myAffine[3];
nAffine[3] = matrix[12] * aObj.myAffine[0] +
matrix[13]*aObj.myAffine[1] +
matrix[14]*aObj.myAffine[2] +
matrix[15]*aObj.myAffine[3];
return Affine::Object( nAffine );
}
// Overloaded scalar multiplication operator.
Affine::Object Affine::operator*( const Affine::Scalar &s, const Affine::Object &obj ) {
Scalar nAffine[4];
nAffine[0] = obj.myAffine[0] * s;
nAffine[1] = obj.myAffine[1] * s;
nAffine[2] = obj.myAffine[2] * s;
nAffine[3] = obj.myAffine[3] * s;
return Object( nAffine );
}
//All the following methods are part of the Affine::Object class.
//Hence they are all in it's namespace.
namespace Affine {
//------------------------------------------------------------------------------
// Object Constructors and Destructors
//------------------------------------------------------------------------------
Object::Object(const Scalar x, const Scalar y, const Scalar z, const Scalar w, Scalar *mem)
:
myShared( true ),
myAffine( mem )
{
if( !mem ) {
myShared = false;
myAffine = new Scalar[4];
}
myAffine[0] = x;
myAffine[1] = y;
myAffine[2] = z;
myAffine[3] = w;
}
Object::Object( Scalar a[] ) :
myShared( false ),
myAffine( new Scalar[4] )
{
myAffine[0] = a[0];
myAffine[1] = a[1];
myAffine[2] = a[2];
myAffine[3] = a[3];
}
Object::Object( const Object &source )
:
myShared( source.myShared ),
myAffine( source.myAffine )
{
if( !myShared ) {
myAffine = new Scalar[4];
myAffine[0] = source[0];
myAffine[1] = source[1];
myAffine[2] = source[2];
myAffine[3] = source[3];
}
}
Object::~Object() {
if( !myShared ) delete[] myAffine;
}
Scalar Object::dot( const Object &other ) const {
return myAffine[0]*other[0] +
myAffine[1]*other[1] +
myAffine[2]*other[2] +
myAffine[3]*other[3];
}
Object Object::cross( const Object &other ) const {
Object c;
c[0] = myAffine[1]*other[2] - myAffine[2]*other[1];//v1.y*v2.z - v1.z*v2.y;
c[1] = myAffine[2]*other[0] - myAffine[0]*other[2];//v1.z*v2.x - v1.x*v2.z;
c[2] = myAffine[0]*other[1] - myAffine[1]*other[0];//v1.x*v2.y - v1.y*v2.x;
c[3] = 0.0; // A vector
return c;
}
void Object::transform( const std::valarray<Scalar> &matrix ) {
assert( matrix.size() == 16 );
Scalar nAffine[4];
nAffine[0] = matrix[0] * myAffine[0] +
matrix[1]*myAffine[1] +
matrix[2]*myAffine[2] +
matrix[3]*myAffine[3];
nAffine[1] = matrix[4] * myAffine[0] +
matrix[5]*myAffine[1] +
matrix[6]*myAffine[2] +
matrix[7]*myAffine[3];
nAffine[2] = matrix[8] * myAffine[0] +
matrix[9]*myAffine[1] +
matrix[10]*myAffine[2] +
matrix[11]*myAffine[3];
nAffine[3] = matrix[12] * myAffine[0] +
matrix[13]*myAffine[1] +
matrix[14]*myAffine[2] +
matrix[15]*myAffine[3];
myAffine[0] = nAffine[0];
myAffine[1] = nAffine[1];
myAffine[2] = nAffine[2];
myAffine[3] = nAffine[3];
}
void Object::normalize() {
Scalar len = length();
myAffine[0] = myAffine[0]/len;
myAffine[1] = myAffine[1]/len;
myAffine[2] = myAffine[2]/len;
//I'm normalzing the homogenous
//coordinate, as it will have no affect
//on a vector but will mess with a point
//and we don't want to normalize points.
myAffine[3] = myAffine[3]/len;
}
Scalar Object::length() const {
return sqrt( dot(*this) );
}
void Object::set( const Scalar x, const Scalar y, const Scalar z ) {
myAffine[0] = x;
myAffine[1] = y;
myAffine[2] = z;
}
//------------------------------------------------------------------------------
// Overloaded operators
//------------------------------------------------------------------------------
Object &Object::operator=( const Object &source ) {
if( !myShared ) delete[] myAffine;
myShared = source.myShared;
if( !myShared ) myAffine = new Scalar[4];
myAffine[0] = source[0];
myAffine[1] = source[1];
myAffine[2] = source[2];
myAffine[3] = source[3];
return *this;
}
bool Object::operator==( const Object &other ) const {
if( myAffine[0] != other[0] ) return false;
if( myAffine[1] != other[1] ) return false;
if( myAffine[2] != other[2] ) return false;
if( myAffine[3] != other[3] ) return false;
return true;
}
Object Object::operator+( const Object &addend) const {
Scalar affine[4];
affine[0] = myAffine[0] + addend[0];
affine[1] = myAffine[1] + addend[1];
affine[2] = myAffine[2] + addend[2];
affine[3] = myAffine[3] + addend[3];
return Object( affine );
}
void Object::operator+=( const Object &addend) {
myAffine[0] += addend[0];
myAffine[1] += addend[1];
myAffine[2] += addend[2];
myAffine[3] += addend[3];
}
Object Object::operator-( const Object &minuend) const {
Scalar affine[4];
affine[0] = myAffine[0] - minuend[0];
affine[1] = myAffine[1] - minuend[1];
affine[2] = myAffine[2] - minuend[2];
affine[3] = myAffine[3] - minuend[3];
return Object( affine );
}
void Object::operator-=( const Object &minuend) {
myAffine[0] -= minuend[0];
myAffine[1] -= minuend[1];
myAffine[2] -= minuend[2];
myAffine[3] -= minuend[3];
}
Object Object::operator*( const Scalar &other ) const {
Scalar affine[4];
affine[0] = myAffine[0] * other;
affine[1] = myAffine[1] * other;
affine[2] = myAffine[2] * other;
affine[3] = myAffine[3] * other;
return Object( affine );
}
void Object::operator*=( const Scalar &other ) {
myAffine[0] *= other;
myAffine[1] *= other;
myAffine[2] *= other;
myAffine[3] *= other;
}
Object Object::operator/( const Scalar &other ) const {
Scalar affine[4];
affine[0] = myAffine[0] / other;
affine[1] = myAffine[1] / other;
affine[2] = myAffine[2] / other;
affine[3] = myAffine[3] / other;
return Object( affine );
}
void Object::operator/=( const Scalar &other ) {
myAffine[0] /= other;
myAffine[1] /= other;
myAffine[2] /= other;
myAffine[3] /= other;
}
Object Object::operator-( ) const {
Scalar affine[4];
affine[0] = -myAffine[0];
affine[1] = -myAffine[1];
affine[2] = -myAffine[2];
affine[3] = -myAffine[3];
return Object( affine);
}
Object Object::operator*( const Object &other ) const {
Scalar affine[4];
affine[0] = myAffine[0] * other[0];
affine[1] = myAffine[1] * other[1];
affine[2] = myAffine[2] * other[2];
affine[3] = myAffine[3] * other[3];
return Object( affine );
}
Object Object::operator%( const Object &other ) const {
return cross( other );
}
Scalar &Object::operator[]( const unsigned int i ) {
assert( i < 4 && "Scalar &Object::operator[]...");
return myAffine[i];
}
const Scalar &Object::operator[]( const unsigned int i ) const {
assert( i < 4 && "const Scalar &Object::operator[]...");
return myAffine[i];
}
}//Namespace Affine