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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
 * storing and accessing finite element mesh data.
 *
 * Copyright 2004 Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
 * retains certain rights in this software.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 */

#include "moab/HomXform.hpp"
#include <cassert>
#include <iostream>

using namespace moab;

// unit test for the HomCoord and HomXform classes
//
int test_get_set();
int test_coord_operators();
int test_xform_operators();
int test_xform_functions();
int test_coord_xform_operators();

int main( int /*argc*/, char** /*argv*/ )
{
    // first test HomCoord

    // constructors
    // following shouldn't compile, since bare constructor is private

    int errors = 0;

    errors += test_get_set();
    errors += test_coord_operators();
    errors += test_xform_operators();
    errors += test_xform_functions();
    errors += test_coord_xform_operators();

    if( errors > 0 )
        std::cout << errors << " errors found." << std::endl;
    else
        std::cout << "All tests passed." << std::endl;

    return errors;
}

int test_get_set()
{
    int errors = 0;

    // other constructors
    int coordsa[4] = { 1, 2, 3, 1 };
    int coordsb[4] = { 4, 3, 2, 1 };

    HomCoord coords1( coordsa );
    HomCoord coords2( 4, 3, 2, 1 );
    HomCoord coords3( 4, 3, 2 );
    HomCoord coords4( 1, 1, 1, 1 );

    // set
    coords2.set( coordsb );

    // hom_coord()
    for( int i = 0; i < 4; i++ )
    {
        if( coords2.hom_coord()[i] != coordsb[i] )
        {
            std::cout << "Get test failed." << std::endl;
            errors++;
        }
    }

    // ijkh
    if( coords2.i() != coordsb[0] || coords2.j() != coordsb[1] || coords2.k() != coordsb[2] ||
        coords2.h() != coordsb[3] )
    {
        std::cout << "ijkh test failed." << std::endl;
        errors++;
    }

    // set
    coords2.set( 3, 3, 3 );

    // hom_coord()
    if( coords2.hom_coord()[0] != 3 || coords2.hom_coord()[1] != 3 || coords2.hom_coord()[2] != 3 ||
        coords2.hom_coord()[3] != 1 )
    {
        std::cout << "Set (int) test failed." << std::endl;
        errors++;
    }

    return errors;
}

int test_coord_operators()
{
    int errors = 0;

    HomCoord coords1( 1, 2, 3, 1 );
    HomCoord coords2( 4, 3, 2, 1 );
    HomCoord coords3( 4, 3, 2 );
    HomCoord coords4( 1, 1, 1, 1 );

    // operator>=
    bool optest = ( coords2 >= coords4 && coords2 >= coords3 && coords3 >= coords2 );
    if( !optest )
    {
        std::cout << "Test failed for operator >=." << std::endl;
        errors++;
    }

    optest = ( coords4 <= coords2 && coords2 <= coords3 && coords3 <= coords2 );
    if( !optest )
    {
        std::cout << "Test failed for operator <=." << std::endl;
        errors++;
    }

    // operator>
    optest = ( coords2 > coords4 && !( coords2 > coords3 ) && !( coords3 > coords2 ) );
    if( !optest )
    {
        std::cout << "Test failed for operator >." << std::endl;
        errors++;
    }

    optest = ( coords4 < coords2 && !( coords2 < coords3 ) && !( coords3 < coords2 ) );
    if( !optest )
    {
        std::cout << "Test failed for operator <." << std::endl;
        errors++;
    }

    // operator[]
    for( int i = 0; i < 3; i++ )
    {
        if( coords1[i] != coords2[3 - i] )
        {
            std::cout << "Test failed for operator[]." << std::endl;
            errors++;
        }
    }

    // operator+
    HomCoord coords5( 2 * coords1[0], 2 * coords1[1], 2 * coords1[2] );
    HomCoord coords6 = coords1 + coords1;
    if( coords5 != coords6 )
    {
        std::cout << "Test failed for operator+." << std::endl;
        errors++;
    }

    // operator-
    if( coords5 - coords1 != coords1 )
    {
        std::cout << "Test failed for operator-." << std::endl;
        errors++;
    }

    return errors;
}

int test_xform_constructors()<--- The function 'test_xform_constructors' is never used.
{
    int errors = 0;

    // integer constructor
    int test_int[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    HomXform xform( test_int );
    for( int i = 0; i < 16; i++ )
    {
        if( xform[i] != i )
        {
            std::cout << "HomXform integer array constructor failed." << std::endl;
            errors++;
        }
    }

    HomXform xform3( test_int[0], test_int[1], test_int[2], test_int[3], test_int[4], test_int[5], test_int[6],
                     test_int[7], test_int[8], test_int[9], test_int[10], test_int[11], test_int[12], test_int[13],
                     test_int[14], test_int[15] );
    for( int i = 0; i < 16; i++ )
    {
        if( xform3[i] != i )
        {
            std::cout << "HomXform integer constructor failed." << std::endl;
            errors++;
        }
    }

    // sample rotation, translation, and scaling matrices/vectors
    int rotate[]    = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
    int translate[] = { 4, 5, 6 };
    int scale[]     = { 1, 1, 1 };

    // construct an xform matrix based on those
    HomXform xform2( rotate, scale, translate );

    // test where those went in the xform
    if( !( xform2[XFORM_INDEX( 0, 0 )] == rotate[0] && xform2[XFORM_INDEX( 0, 1 )] == rotate[1] &&
           xform2[XFORM_INDEX( 0, 2 )] == rotate[2] && xform2[XFORM_INDEX( 1, 0 )] == rotate[3] &&
           xform2[XFORM_INDEX( 1, 1 )] == rotate[4] && xform2[XFORM_INDEX( 1, 2 )] == rotate[5] &&
           xform2[XFORM_INDEX( 2, 0 )] == rotate[6] && xform2[XFORM_INDEX( 2, 1 )] == rotate[7] &&
           xform2[XFORM_INDEX( 2, 2 )] == rotate[8] && xform2[XFORM_INDEX( 3, 0 )] == translate[0] &&
           xform2[XFORM_INDEX( 3, 1 )] == translate[1] && xform2[XFORM_INDEX( 3, 2 )] == translate[2] &&
           xform2[XFORM_INDEX( 0, 3 )] == 0 && xform2[XFORM_INDEX( 1, 3 )] == 0 && xform2[XFORM_INDEX( 2, 3 )] == 0 &&
           xform2[XFORM_INDEX( 3, 3 )] == 1 ) )
    {
        std::cout << "HomXform rotate, scale, translate constructor failed." << std::endl;
        errors++;
    }

    return errors;
}

int test_xform_operators()
{
    int errors = 0;

    // sample rotation, translation, and scaling matrices/vectors
    int rotate[]    = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
    int translate[] = { 4, 5, 6 };
    int scale[]     = { 1, 1, 1 };

    // construct an xform matrix based on those
    HomXform xform1( rotate, scale, translate );
    HomXform xform1a( rotate, scale, translate );

    // test operator==
    if( !( xform1 == xform1a ) )
    {
        std::cout << "HomXform operator== failed." << std::endl;
        errors++;
    }

    // test operator!=
    xform1a[1] = 0;
    if( !( xform1 != xform1a ) )
    {
        std::cout << "HomXform operator!= failed." << std::endl;
        errors++;
    }

    // test operator=
    HomXform xform1c = xform1;
    if( !( xform1c == xform1 ) )
    {
        std::cout << "HomXform operator= failed." << std::endl;
        errors++;
    }

    HomXform xform3 = xform1 * HomXform::IDENTITY;
    if( xform3 != xform1 )
    {
        std::cout << "HomXform operator * failed." << std::endl;
        errors++;
    }

    // test operator*=
    xform3 *= HomXform::IDENTITY;
    if( xform3 != xform1 )
    {
        std::cout << "HomXform operator *= failed." << std::endl;
        errors++;
    }

    return errors;
}

int test_xform_functions()
{
    int errors = 0;

    // HomCoord functions
    // length() and length_squared()
    HomCoord coord1( 3, 4, 5 );
    if( coord1.length_squared() != 50 || coord1.length() != 7 )
    {
        std::cout << "HomCoord length() or length_squared() failed." << std::endl;
        errors++;
    }

    // normalize()
    coord1.normalize();
    HomCoord coord2( 3, 0, 0 );
    coord2.normalize();
    if( coord1.length_squared() != 0 || coord2.length_squared() != 1 )
    {
        std::cout << "HomCoord normalize failed." << std::endl;
        errors++;
    }

    // sample rotation, translation, and scaling matrices/vectors
    int inv_int[] = { 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 7, 5, 0, 1 };

    // construct an xform matrix based on those
    HomXform xform1( inv_int );

    HomXform xform1_inv = xform1.inverse();

    HomXform xform2 = xform1 * xform1_inv;
    if( xform2 != HomXform::IDENTITY )
    {
        std::cout << "HomXform inverse failed." << std::endl;
        errors++;
    }

    return errors;
}

int test_coord_xform_operators()
{
    int errors = 0;

    // sample pt
    HomCoord test_pt( 1, 2, 3 );

    HomCoord test_pt2 = test_pt * HomXform::IDENTITY;
    if( test_pt2 != test_pt )
    {
        std::cout << "Coord-xform operator* failed." << std::endl;
        errors++;
    }

    // get an inverse transform quickly
    int rotate[]    = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
    int translate[] = { 4, 5, 6 };
    int scale[]     = { 1, 1, 1 };
    HomXform xform2( rotate, scale, translate );

    // operator*
    HomCoord ident( 1, 1, 1, 1 );
    HomCoord test_pt3 = ident * HomXform::IDENTITY;
    if( test_pt3 != ident )
    {
        std::cout << "Coord-xform operator* failed." << std::endl;
        errors++;
    }

    // operator/
    test_pt2 = ( test_pt * xform2 ) / xform2;
    if( test_pt2 != test_pt )
    {
        std::cout << "Coord-xform operator/ failed." << std::endl;
        errors++;
    }

    // test three_pt_xform; use known transforms in most cases
    HomXform xform;

    // first test: i = j, j = -i, k = k, t = (7,5,0)
    xform.three_pt_xform( HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ), HomCoord( 0, 3, 0, 1 ),
                          HomCoord( 4, 5, 0, 1 ), HomCoord( 0, 0, 3, 1 ), HomCoord( 7, 5, 3, 1 ) );

    HomXform solution( 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 7, 5, 0, 1 );
    if( xform != solution )
    {
        std::cout << "Three-pt transform (general) test failed." << std::endl;
        errors++;
    }

    // now test 1d
    xform.three_pt_xform( HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ), HomCoord( 6, 0, 0, 1 ),
                          HomCoord( 7, 11, 0, 1 ), HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ) );

    solution = HomXform( 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 7, 5, 0, 1 );
    if( xform != solution )
    {
        std::cout << "Three-pt transform (1d) test failed." << std::endl;
        errors++;
    }

    return errors;
}