summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Utility/NXVectorTest.cpp
blob: 96abcc1d8e7b5dc7566c738497124a6b3d0515f7 (plain)
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
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#include "NXVectorTest.h"

CPPUNIT_TEST_SUITE_REGISTRATION(NXVectorTest);
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(NXVectorTest,
                                      "NXVectorTestSuite");

// ---------------------------------------------------------------------
/* Helper comparison functions  */


template<typename T, int N, template<typename,int> class NXVectorDerived>
void cppunit_assert_vectors_equal(NXVectorBase<T,N,NXVectorDerived> const& pRef,
                                  T answer[N],
                                  T const& delta = 1.0e-8)
{
	for(int n=0; n<N; ++n) {
		CPPUNIT_ASSERT_DOUBLES_EQUAL(double(pRef[n]),
		                             double(answer[n]),
		                             double(delta));
	}
}

template<typename T, int N, template<typename,int> class NXVectorDerived>
void cppunit_assert_points_unequal(NXVectorBase<T,N,NXVectorDerived> const& pRef,
                                   T answer[N],
                                   T const& delta = 1.0e-8)
{
	for(int n=0; n<N; ++n) {
		CPPUNIT_ASSERT(fabs(double(pRef[n])-double(answer[n])) > double(delta));
	}
}

template<typename T, int N,
template<typename,int> class NXVectorDerived,
template<typename,int> class NXVectorDerived2
>
void cppunit_assert_vectors_equal(NXVectorBase<T,N,NXVectorDerived> const& p1Ref,
                                  NXVectorBase<T,N,NXVectorDerived2> const& p2Ref,
                                  T const& delta = 1.0e-8)
{
	for(int n=0; n<N; ++n) {
		CPPUNIT_ASSERT_DOUBLES_EQUAL(double(p1Ref[n]),
		                             double(p2Ref[n]),
		                             double(delta));
	}
}


// ---------------------------------------------------------------------
/* Test-suite */

void NXVectorTest::accessTest(void)
{
	NXVector4d p;
	double answer[4] = { 0.0, 1.0, 2.0, 3.0 };
	p[0] = answer[0]; p[1] = answer[1]; p[2] = answer[2]; p[3] = answer[3];
	double const *const ptr = p.data();
	CPPUNIT_ASSERT(ptr[0] == 0.0);
	CPPUNIT_ASSERT(ptr[1] == 1.0);
	CPPUNIT_ASSERT(ptr[2] == 2.0);
	CPPUNIT_ASSERT(ptr[3] == 3.0);
	cppunit_assert_vectors_equal(p, answer, 0.0);
}

void NXVectorTest::dataTest(void)
{ 
	NXVector4d p;
	NXVectorRef4d pRef;
	pRef = p;
	CPPUNIT_ASSERT(p.data() == pRef.data());
	
    // test copy constructor for deep-copy semantics
	double init[4] = { 2.0, 3.0, -1.0, 6.0 };
	NXVectorRef4d initRef(init);
	NXVector4d initCopy(initRef);
	CPPUNIT_ASSERT(initRef.data() != initCopy.data());
	cppunit_assert_vectors_equal(initRef, initCopy);
    // test copy assignment for deep-copy
	initCopy.zero();
	initCopy = initRef;
	CPPUNIT_ASSERT(initRef.data() != initCopy.data());
	cppunit_assert_vectors_equal(initRef, initCopy);
}

void NXVectorTest::sizeTest(void)
{
	NXVector4d p;
	CPPUNIT_ASSERT(p.size() == 4);
	NXVectorRef3f pRef;
	CPPUNIT_ASSERT(pRef.size() == 3);
}

void NXVectorTest::zeroTest(void)
{
	NXVector4d p;
	p.zero();
	CPPUNIT_ASSERT(p[0] == 0.0);
	CPPUNIT_ASSERT(p[1] == 0.0);
	CPPUNIT_ASSERT(p[2] == 0.0);
	CPPUNIT_ASSERT(p[3] == 0.0);
	
	NXVector<char,2> cp;
	cp.zero();
	CPPUNIT_ASSERT(cp[0] == '\0');
	CPPUNIT_ASSERT(cp[1] == '\0');
}

void NXVectorTest::incrementTest(void)
{
	NXVector4d p;
	double init[4] = { 0.0, 1.0, 2.0, 3.0 };
	p[0] = init[0]; p[1] = init[1]; p[2] = init[2]; p[3] = init[3];
	NXVector4d p2;
	p2.zero();
	p2 += p;
	cppunit_assert_vectors_equal(p2, init);
	double twice[4] = { 0.0, 2.0, 4.0, 6.0 };
	p2 += p2;
	cppunit_assert_vectors_equal(p2, twice, 0.0);
	
}

void NXVectorTest::decrementTest(void)
{
	double zeros[4] = { 0.0, 0.0, 0.0, 0.0 };
	NXVector4d p1;
	p1.zero();
	NXVector4d p2;
	p2.zero();
	p1 -= p2;
	cppunit_assert_vectors_equal(p1, zeros, 0.0);
	
	double init[4] = { 1.0, -2.0, 13.0, -27.0 };
	double answer[4] = { -1.0, 2.0, -13.0, 27.0 };
	NXVectorRef4d initRef(init);
	NXVector4d initCopy = initRef;
	cppunit_assert_vectors_equal(initRef, initCopy);
	initCopy -= initRef;
	cppunit_assert_vectors_equal(initCopy, zeros, 0.0);
	initCopy -= initRef;
	cppunit_assert_vectors_equal(initCopy, answer, 0.0);
}

void NXVectorTest::scalingTest(void)
{
	double init[4] = { 5.0, 8.0, -2.0, 3.0 };
	double const factor = -2.0;
	double answer[4] = { -10.0, -16.0, 4.0, -6.0 };
	NXVectorRef4d p(init);
	p *= factor;
	cppunit_assert_vectors_equal(p, answer, 0.0);
    // check to see if the reference updated the actual array
	for(int n=0; n<4; ++n)
		CPPUNIT_ASSERT(init[n] == answer[n]);
}

void NXVectorTest::invScalingTest(void)
{
	double init[4] = { -10.0, -16.0, 4.0, -6.0 };
	double answer[4] = { 5.0, 8.0, -2.0, 3.0 };
	double const factor = -2.0;
	NXVectorRef4d p(init);
	p /= factor;
	cppunit_assert_vectors_equal(p, answer, 0.0);
    // check to see if the reference updated the actual array
	for(int n=0; n<4; ++n)
		CPPUNIT_ASSERT(init[n] == answer[n]);
}

void NXVectorTest::dotTest(void)
{
	double p1Data[4] = { 5.0, 6.0, 7.0, 8.0 };
	double p2Data[4] = { 8.0, 7.0, 6.0, 5.0 };
	NXVectorRef4d p1(p1Data);
	NXVectorRef4d p2(p2Data);
	double const p1_dot_p2 = dot(p1, p2);
	double const answer = (p1Data[0] * p2Data[0] +
	                       p1Data[1] * p2Data[1] +
	                       p1Data[2] * p2Data[2] +
	                       p1Data[3] * p2Data[3]);
	CPPUNIT_ASSERT(p1_dot_p2 == answer);
}

void NXVectorTest::squared_normTest(void)
{
	double pData[4] = { 5.0, 6.0, 7.0, 8.0 };
	NXVectorRef4d p(pData);
	double const pSquaredNorm = squared_norm(p);
	double const answer = (pData[0] * pData[0] +
	                       pData[1] * pData[1] +
	                       pData[2] * pData[2] +
	                       pData[3] * pData[3]);
	CPPUNIT_ASSERT(pSquaredNorm == answer);
}

void NXVectorTest::normTest(void)
{
	double pData[5] = { 3.0, 4.0, 5.0, 5.0, 5.0 };
	NXVectorRef<double,5> p(pData);
	double const pNorm = norm(p);
	double const answer = 10.0;
	CPPUNIT_ASSERT_DOUBLES_EQUAL(pNorm, answer, 1.0e-8);
}

void NXVectorTest::normalizeSelfTest(void)
{
	double init[4] = { 1.0, 1.0, 1.0, 1.0 };
	NXVector4d initCopy(init);
	initCopy.normalizeSelf();
	double answer[4] = { 0.5, 0.5, 0.5, 0.5 };
	cppunit_assert_vectors_equal(initCopy, answer, 1.0e-8);
	
	NXVector<float,2> pythagoreanPair;
	pythagoreanPair[0] = 3.0f;
	pythagoreanPair[1] = 4.0f;
	pythagoreanPair.normalizeSelf();
	float pythagoreanPairNormalizedValues[2] = { 0.6f, 0.8f };
	NXVectorRef2f pythagoreanPairNormalized(pythagoreanPairNormalizedValues);
	cppunit_assert_vectors_equal(pythagoreanPair,
	                             pythagoreanPairNormalized,
	                             1.0e-8f);
}

void NXVectorTest::normalizedTest(void)
{
	double init[4] = { 1.0, 1.0, 1.0, 1.0 };
	NXVector4d initCopy(init);
	NXVector4d initNormalized = initCopy.normalized();
	double answer[4] = { 0.5, 0.5, 0.5, 0.5 };
	cppunit_assert_vectors_equal(initNormalized, answer, 1.0e-8);
	cppunit_assert_points_unequal(initNormalized, init, 1.0e-8);
}

void NXVectorTest::crossTest(void)
{
	double iData[3] = { 1.0, 0.0, 0.0 };
	double jData[3] = { 0.0, 1.0, 0.0 };
	double kData[3] = { 0.0, 0.0, 1.0 };
	NXVectorRef3d iUnit(iData), jUnit(jData), kUnit(kData);
	NXVector3d iCross = cross(jUnit, kUnit);
	cppunit_assert_vectors_equal(iCross, iUnit);
	NXVector3d jCross = cross(kUnit, iUnit);
	cppunit_assert_vectors_equal(jCross, jUnit);
	NXVector3d kCross = cross(iUnit, jUnit);
	cppunit_assert_vectors_equal(kCross, kUnit);
	
	double pData[3] = {  2.0, 1.0, -1.0 };
	double qData[3] = { -3.0, 4.0,  1.0 };
	double pcrossqData [3] = { 5.0, 1.0, 11.0 };
	NXVectorRef3d p(pData), q(qData);
	NXVector3d r = cross(p,q);
	cppunit_assert_vectors_equal(NXVectorRef3d(pcrossqData), r.data());
	
	int lData[3] = { 0,  1, 1 };
	int mData[3] = { 1, -1, 3 };
	int minus_lcrossm_data[3] = { -4, -1, 1 };
	NXVectorRef<int,3> l(lData), m(mData);
	NXVector<int,3> n = cross(m,l);
	cppunit_assert_vectors_equal(n, minus_lcrossm_data, 0);
}