1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
28
#include "../common/WAssert.h"
29
#include "../common/WLimits.h"
30
#include "datastructures/WValueSetHistogram.h"
31
#include "WDataSetSingle.h"
33
#include "WDataSetScalar.h"
35
// prototype instance as singleton
36
boost::shared_ptr< WPrototyped > WDataSetScalar::m_prototype = boost::shared_ptr< WPrototyped >();
38
WDataSetScalar::WDataSetScalar( boost::shared_ptr< WValueSetBase > newValueSet,
39
boost::shared_ptr< WGrid > newGrid )
40
: WDataSetSingle( newValueSet, newGrid )
42
WAssert( newValueSet, "No value set given." );
43
WAssert( newGrid, "No grid given." );
44
WAssert( newValueSet->size() == newGrid->size(), "Number of values unequal number of positions in grid." );
45
WAssert( newValueSet->order() == 0, "The value set does not contain scalars." );
48
WDataSetScalar::WDataSetScalar()
51
// default constructor used by the prototype mechanism
54
WDataSetScalar::~WDataSetScalar()
58
WDataSetSingle::SPtr WDataSetScalar::clone( boost::shared_ptr< WValueSetBase > newValueSet ) const
60
return WDataSetSingle::SPtr( new WDataSetScalar( newValueSet, getGrid() ) );
63
WDataSetSingle::SPtr WDataSetScalar::clone( boost::shared_ptr< WGrid > newGrid ) const
65
return WDataSetSingle::SPtr( new WDataSetScalar( getValueSet(), newGrid ) );
68
WDataSetSingle::SPtr WDataSetScalar::clone() const
70
return WDataSetSingle::SPtr( new WDataSetScalar( getValueSet(), getGrid() ) );
73
double WDataSetScalar::getMax() const
75
return m_valueSet->getMaximumValue();
78
double WDataSetScalar::getMin() const
80
return m_valueSet->getMinimumValue();
83
boost::shared_ptr< WPrototyped > WDataSetScalar::getPrototype()
87
m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetScalar() );
93
double WDataSetScalar::interpolate( const WPosition& pos, bool* success ) const
95
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
97
WAssert( grid, "This data set has a grid whose type is not yet supported for interpolation." );
98
WAssert( ( m_valueSet->order() == 0 && m_valueSet->dimension() == 1 ),
99
"Only implemented for scalar values so far." );
101
bool isInside = true;
102
size_t cellId = grid->getCellId( pos, &isInside );
110
WGridRegular3D::CellVertexArray vertexIds = grid->getCellVertexIds( cellId );
112
WPosition localPos = grid->getTransform().positionToGridSpace( pos - grid->getPosition( vertexIds[0] ) );
114
double lambdaX = localPos[0];
115
double lambdaY = localPos[1];
116
double lambdaZ = localPos[2];
117
std::vector< double > h( 8 );
127
h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
128
h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
129
h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
130
h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
131
h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
132
h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
133
h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
134
h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
137
for( size_t i = 0; i < 8; ++i )
139
result += h[i] * WDataSetSingle::getValueAt( vertexIds[i] );
146
double WDataSetScalar::getValueAt( int x, int y, int z ) const
148
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
149
size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
151
return WDataSetSingle::getValueAt( id );
154
boost::shared_ptr< const WValueSetHistogram > WDataSetScalar::getHistogram( size_t buckets )
156
boost::lock_guard<boost::mutex> lock( m_histogramLock );
158
if( m_histograms.count( buckets ) != 0 )
160
return m_histograms[ buckets ];
163
// create if not yet existing
164
m_histograms[ buckets ] = boost::shared_ptr< WValueSetHistogram >( new WValueSetHistogram( m_valueSet, buckets ) );
166
return m_histograms[ buckets ];