File size: 5,863 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  
  Author(s):	Ofri Sadowsky
  Created on:	2004-04-20
  
  (C) Copyright 2004-2007 Johns Hopkins University (JHU), All Rights
  Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/


#include "vctDeterminantTest.h"
#include <cisstCommon/cmnRandomSequence.h>
#include <cisstCommon/cmnTypeTraits.h>

vctDeterminantTest::vctDeterminantTest()
: Vec2D(0)
, Mat2x2(0)
, Vec3D( vctFixedSizeVector<ElementType, 3>(0) )
, RandomSequence( cmnRandomSequence::GetInstance() )
{}

void vctDeterminantTest::setUp()
{
    const ElementType tolerance = cmnTypeTraits<ElementType>::Tolerance();
    const ElementType minRandom(-10);
    const ElementType maxRandom(10);

    while (Vec2D.Norm() < tolerance) {
        RandomSequence.ExtractRandomValueArray(minRandom, maxRandom, Vec2D.Pointer(), 2);
    }

    while (Mat2x2.Norm() < tolerance) {
        RandomSequence.ExtractRandomValueArray(minRandom, maxRandom, Mat2x2.Pointer(), 4);
    }

    while (Vec3D[0].Norm() < tolerance) {
        RandomSequence.ExtractRandomValueArray(minRandom, maxRandom, Vec3D[0].Pointer(), 3);
    }

    while (Vec3D[1].Norm() < tolerance) {
        RandomSequence.ExtractRandomValueArray(minRandom, maxRandom, Vec3D[1].Pointer(), 3);
    }
}


void vctDeterminantTest::TestDeterminant2x2ByInverse(const vctFixedSizeMatrix<ElementType, 2, 2> & inputMatrix)
{
    const ElementType determinant = vctDeterminant<2>::Compute(inputMatrix);

    const ElementType tolerance = cmnTypeTraits<ElementType>::Tolerance();

    if (fabs(determinant) < tolerance) {
        vctFixedSizeVector<ElementType, 2> rowRatio;
        rowRatio.ElementwiseRatioOf( inputMatrix.Row(0), inputMatrix.Row(1) );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( rowRatio[0], rowRatio[1], tolerance );

        vctFixedSizeVector<ElementType, 2> columnRatio;
        columnRatio.ElementwiseRatioOf( inputMatrix.Column(0), inputMatrix.Column(1) );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( columnRatio[0], columnRatio[1], tolerance );

        return;
    }

    const vctFixedSizeMatrix<ElementType, 2, 2> inverse(
        inputMatrix[1][1] / determinant, -inputMatrix[0][1] / determinant,
        -inputMatrix[1][0] / determinant, inputMatrix[0][0] / determinant
        );

    /* gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 crashes with internal error
       on ubuntu 64 - Ubuntu 10.04.1 LTS with const */
    /* const */ vctFixedSizeMatrix<ElementType, 2, 2> product( inputMatrix * inverse );
    const vctFixedSizeMatrix<ElementType, 2, 2> identity( ElementType(1), ElementType(0), ElementType(0), ElementType(1) );
    const vctFixedSizeMatrix<ElementType, 2, 2> difference = product - identity;
    ElementType diffNorm = difference.Norm();
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, diffNorm, tolerance );
}


void vctDeterminantTest::TestDeterminant2x2ByOrthogonal(const vctFixedSizeVector<ElementType, 2> & inputVector)
{
    const ElementType tolerance = cmnTypeTraits<ElementType>::Tolerance();
    const vctFixedSizeVector<ElementType, 2> normalizedInput = inputVector / inputVector.Norm();
    const vctFixedSizeMatrix<ElementType, 2, 2> orthogonalMatrix(
        normalizedInput[0], normalizedInput[1],
        -normalizedInput[1], normalizedInput[0]);

    const ElementType determinant = vctDeterminant<2>::Compute(orthogonalMatrix);
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, determinant, tolerance );

    TestDeterminant2x2ByInverse( orthogonalMatrix );
}


void vctDeterminantTest::TestDeterminant2x2ByLinearCombination(const vctFixedSizeVector<ElementType, 2> & row0)
{
    const ElementType tolerance = cmnTypeTraits<ElementType>::Tolerance();

    const vctFixedSizeVector<ElementType, 2> row1 = ElementType(2) * row0;
    const vctFixedSizeMatrix<ElementType, 2, 2> matrix2x2(
        row0[0], row0[1], row1[0], row1[1] );

    const ElementType determinant = vctDeterminant<2>::Compute(matrix2x2);

    CPPUNIT_ASSERT_DOUBLES_EQUAL(0, determinant, tolerance);

    TestDeterminant2x2ByInverse( matrix2x2 );
}


void vctDeterminantTest::TestDeterminant3x3ByCrossProduct(const vctFixedSizeVector<ElementType, 3> & row0,
                                                          const vctFixedSizeVector<ElementType, 3> & row1)
{
    const ElementType tolerance = cmnTypeTraits<ElementType>::Tolerance();

    vctFixedSizeVector<ElementType, 3> row2;
    row2.CrossProductOf( row0, row1 );

    const vctFixedSizeMatrix<ElementType, 3, 3> matrix3x3(
        row0[0], row0[1], row0[2],
        row1[0], row1[1], row1[2],
        row2[0], row2[1], row2[2] );

    const ElementType determinant = vctDeterminant<3>::Compute(matrix3x3);

    if (row2.Norm() < tolerance) {
        CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, determinant, tolerance);
    }
    else {
        CPPUNIT_ASSERT( fabs(determinant) > tolerance );
    }
}


void vctDeterminantTest::TestDeterminant3x3ByLinearCombination(const vctFixedSizeVector<ElementType, 3> & row0,
                                                               const vctFixedSizeVector<ElementType, 3> & row1)
{
    const ElementType tolerance = cmnTypeTraits<ElementType>::Tolerance();

    const vctFixedSizeVector<ElementType, 3> row2 = row0 + row1;

    const vctFixedSizeMatrix<ElementType, 3, 3> matrix3x3(
        row0[0], row0[1], row0[2],
        row1[0], row1[1], row1[2],
        row2[0], row2[1], row2[2] );

    const ElementType determinant = vctDeterminant<3>::Compute(matrix3x3);

    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, determinant, tolerance );
}


CPPUNIT_TEST_SUITE_REGISTRATION(vctDeterminantTest);