~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/core/dataHandler/test/WDataSetVector_test.h

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
10
// OpenWalnut is free software: you can redistribute it and/or modify
 
11
// it under the terms of the GNU Lesser General Public License as published by
 
12
// the Free Software Foundation, either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// OpenWalnut is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Lesser General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Lesser General Public License
 
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#ifndef WDATASETVECTOR_TEST_H
 
26
#define WDATASETVECTOR_TEST_H
 
27
 
 
28
#include <vector>
 
29
 
 
30
#include <boost/array.hpp>
 
31
 
 
32
#include <cxxtest/TestSuite.h>
 
33
 
 
34
#include "../../common/WLogger.h"
 
35
#include "../WDataSetVector.h"
 
36
 
 
37
/**
 
38
 * Test basic functionality of WDataSetVector.
 
39
 */
 
40
class WDataSetVectorTest : public CxxTest::TestSuite
 
41
{
 
42
public:
 
43
    /**
 
44
     * Constructs unit test environment.
 
45
     */
 
46
    void setUp( void )
 
47
    {
 
48
        WLogger::startup();
 
49
    }
 
50
 
 
51
    /**
 
52
     * An interpolate of an vector is as if every components were interpolated
 
53
     */
 
54
    void testInterpolate( void )
 
55
    {
 
56
        boost::shared_ptr< WGrid > grid = boost::shared_ptr< WGrid >( new WGridRegular3D( 5, 3, 3 ) );
 
57
        boost::shared_ptr< std::vector< double > > data = boost::shared_ptr< std::vector< double > >( new std::vector< double >( grid->size() * 3 ) );
 
58
        for( size_t i = 0; i < grid->size() * 3; ++i )
 
59
        {
 
60
            ( *data )[i] = i;
 
61
        }
 
62
        boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 1, 3, data, W_DT_DOUBLE ) );
 
63
        WDataSetVector ds( valueSet, grid );
 
64
 
 
65
        bool success = false;
 
66
 
 
67
        TS_ASSERT_EQUALS( ds.interpolate( WPosition( 0, 0, 0 ), &success )[0], ( *data )[0] );
 
68
        TS_ASSERT_EQUALS( ds.interpolate( WPosition( 0, 0, 0 ), &success )[1], ( *data )[1] );
 
69
        TS_ASSERT_EQUALS( ds.interpolate( WPosition( 0, 0, 0 ), &success )[2], ( *data )[2] );
 
70
        TS_ASSERT( success );
 
71
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 0 ), &success )[0], ( *data )[3], 1e-9 );
 
72
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 0 ), &success )[1], ( *data )[4], 1e-9 );
 
73
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 0 ), &success )[2], ( *data )[5], 1e-9 );
 
74
        TS_ASSERT( success );
 
75
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 0 ), &success )[0], ( *data )[15], 1e-9 );
 
76
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 0 ), &success )[1], ( *data )[16], 1e-9 );
 
77
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 0 ), &success )[2], ( *data )[17], 1e-9 );
 
78
        TS_ASSERT( success );
 
79
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 0 ), &success )[0], ( *data )[18], 1e-9 );
 
80
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 0 ), &success )[1], ( *data )[19], 1e-9 );
 
81
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 0 ), &success )[2], ( *data )[20], 1e-9 );
 
82
        TS_ASSERT( success );
 
83
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 0, 1 ), &success )[0], ( *data )[45], 1e-9 );
 
84
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 0, 1 ), &success )[1], ( *data )[46], 1e-9 );
 
85
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 0, 1 ), &success )[2], ( *data )[47], 1e-9 );
 
86
        TS_ASSERT( success );
 
87
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 1 ), &success )[0], ( *data )[48], 1e-9 );
 
88
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 1 ), &success )[1], ( *data )[49], 1e-9 );
 
89
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 1 ), &success )[2], ( *data )[50], 1e-9 );
 
90
        TS_ASSERT( success );
 
91
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 1 ), &success )[0], ( *data )[60], 1e-9 );
 
92
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 1 ), &success )[1], ( *data )[61], 1e-9 );
 
93
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 1 ), &success )[2], ( *data )[62], 1e-9 );
 
94
        TS_ASSERT( success );
 
95
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 1 ), &success )[0], ( *data )[63], 1e-9 );
 
96
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 1 ), &success )[1], ( *data )[64], 1e-9 );
 
97
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 1 ), &success )[2], ( *data )[65], 1e-9 );
 
98
        TS_ASSERT( success );
 
99
 
 
100
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.3, 0.4, 0.5 ), &success )[0], 29.4, 1e-9 );
 
101
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.3, 0.4, 0.5 ), &success )[1], 30.4, 1e-9 );
 
102
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.3, 0.4, 0.5 ), &success )[2], 31.4, 1e-9 );
 
103
        TS_ASSERT( success );
 
104
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.5, 0.5, 0.5 ), &success )[0], 31.5, 1e-9 );
 
105
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.5, 0.5, 0.5 ), &success )[1], 32.5, 1e-9 );
 
106
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.5, 0.5, 0.5 ), &success )[2], 33.5, 1e-9 );
 
107
        TS_ASSERT( success );
 
108
    }
 
109
 
 
110
    /**
 
111
     * Checks if the reorientation of the vectors is applied in eigenVectorInterpolate().
 
112
     \verbatim
 
113
                         v_6( 1, 0, 0 )               v_7( 1, 0, 0 )
 
114
                       /----------------------------/
 
115
                 z A  /|                           /|
 
116
                   | / |                          / |
 
117
                   |/  |                         /  |
 
118
                   /---+------------------------/   |
 
119
            v_4( 1, 0, 0 )               v_5( 1, 0, 0 )
 
120
                   |   |                        |   |
 
121
                   |   |                        |   |
 
122
                   |   |                        |   |
 
123
                   |   |    y                   |   |
 
124
                   |   |   /                    |   |
 
125
                   |   |  /                     |   |
 
126
                   |   | /                      |   |
 
127
                   |   | v_2( 1, 0, 0 )         |   | v_3( 1, 0, 0 )
 
128
                   |   /------------------------+---/
 
129
                   |  /                         |  /
 
130
                   | /                          | /
 
131
                   |/                           |/
 
132
                   /----------------------------/------------------> x
 
133
            v_0( -1, 0, 0)                v_1( 1, 0, 0 )
 
134
 
 
135
     \endverbatim
 
136
     */
 
137
    void testEigenVectorInterpolate( void )
 
138
    {
 
139
        boost::shared_ptr< WGrid > grid = boost::shared_ptr< WGrid >( new WGridRegular3D( 2, 2, 2 ) );
 
140
        boost::shared_ptr< std::vector< double > > data( new std::vector< double > );
 
141
        boost::array< WPosition, 8 > d = { { WPosition( -1, 0, 0 ), // NOLINT braces
 
142
                                             WPosition(  1, 0, 0 ),
 
143
                                             WPosition(  1, 0, 0 ),
 
144
                                             WPosition(  1, 0, 0 ),
 
145
                                             WPosition(  1, 0, 0 ),
 
146
                                             WPosition(  1, 0, 0 ),
 
147
                                             WPosition(  1, 0, 0 ),
 
148
                                             WPosition(  1, 0, 0 ) } }; // NOLINT braces
 
149
 
 
150
        for( size_t i = 0; i < grid->size(); ++i )
 
151
        {
 
152
            data->push_back( d[i][0] );
 
153
            data->push_back( d[i][1] );
 
154
            data->push_back( d[i][2] );
 
155
        }
 
156
        boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 1, 3, data, W_DT_DOUBLE ) );
 
157
        WDataSetVector ds( valueSet, grid );
 
158
 
 
159
        bool success = false;
 
160
        TS_ASSERT_EQUALS( ds.interpolate( WPosition( 0.0, 0.0, 0.0 ), &success ), d[0] );
 
161
        TS_ASSERT( success );
 
162
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.9999, 0.9999, 0.9999 ), &success )[0], d[7][0], 1e-9 );
 
163
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.9999, 0.9999, 0.9999 ), &success )[1], d[7][1], 1e-9 );
 
164
        TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.9999, 0.9999, 0.9999 ), &success )[2], d[7][2], 1e-9 );
 
165
        TS_ASSERT( success );
 
166
        WPosition expected( 0.75, 0.0, 0.0 );
 
167
        TS_ASSERT_EQUALS( ds.interpolate( WPosition( 0.5, 0.5, 0.5 ), &success ), expected );
 
168
        TS_ASSERT( success );
 
169
        TS_ASSERT_EQUALS( ds.eigenVectorInterpolate( WPosition( 0.0, 0.0, 0.0 ), &success ), d[0] );
 
170
        TS_ASSERT( success );
 
171
        expected = WPosition( -1.0, 0.0, 0.0 );
 
172
        TS_ASSERT_DELTA( ds.eigenVectorInterpolate( WPosition( 0.9999, 0.9999, 0.9999 ), &success )[0], expected[0], 1e-9 );
 
173
        TS_ASSERT_DELTA( ds.eigenVectorInterpolate( WPosition( 0.9999, 0.9999, 0.9999 ), &success )[1], expected[1], 1e-9 );
 
174
        TS_ASSERT_DELTA( ds.eigenVectorInterpolate( WPosition( 0.9999, 0.9999, 0.9999 ), &success )[2], expected[2], 1e-9 );
 
175
        TS_ASSERT( success );
 
176
        expected = WPosition( -1.0, 0.0, 0.0 );
 
177
        TS_ASSERT_EQUALS( ds.eigenVectorInterpolate( WPosition( 0.5, 0.5, 0.5 ), &success ), expected );
 
178
        TS_ASSERT( success );
 
179
    }
 
180
 
 
181
    /**
 
182
     * Using interpolate on Positions on the boundary of the grid the success flag is true but there should not be any segfaults.
 
183
     * See ticket #313 for more informations.
 
184
     */
 
185
    void testBoundary_ticket313( void )
 
186
    {
 
187
        boost::shared_ptr< WGridRegular3D > grid = boost::shared_ptr< WGridRegular3D >( new WGridRegular3D( 3, 4, 5 ) );
 
188
        bool success = false;
 
189
        boost::shared_ptr< std::vector< double > > data = boost::shared_ptr< std::vector< double > >( new std::vector< double >( grid->size() * 3 ) );
 
190
        for( size_t i = 0; i < grid->size() * 3; ++i )
 
191
        {
 
192
            ( *data )[i] = i;
 
193
        }
 
194
        boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 1, 3, data, W_DT_DOUBLE ) );
 
195
        WDataSetVector ds( valueSet, grid );
 
196
        ds.interpolate( WPosition( 2.0, 3.0, 4.0 ), &success );
 
197
        TS_ASSERT( !success );
 
198
    }
 
199
};
 
200
 
 
201
#endif  // WDATASETVECTOR_TEST_H