OpenWalnut  1.3.1
WDataSetSphericalHarmonics.cpp
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 #include <stdint.h>
26 
27 #include <cmath>
28 #include <string>
29 #include <vector>
30 
31 #include "../common/WAssert.h"
32 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
33 #include "WDataSetSingle.h"
34 #include "WDataSetSphericalHarmonics.h"
35 
36 // prototype instance as singleton
37 boost::shared_ptr< WPrototyped > WDataSetSphericalHarmonics::m_prototype = boost::shared_ptr< WPrototyped >();
38 
39 WDataSetSphericalHarmonics::WDataSetSphericalHarmonics( boost::shared_ptr< WValueSetBase > newValueSet,
40  boost::shared_ptr< WGrid > newGrid ) :
41  WDataSetSingle( newValueSet, newGrid ), m_valueSet( newValueSet )
42 {
43  m_gridRegular3D = boost::shared_dynamic_cast< WGridRegular3D >( newGrid );
44  WAssert( newValueSet, "No value set given." );
45  WAssert( newGrid, "No grid given." );
46 }
47 
49  : WDataSetSingle()
50 {
51 }
52 
54 {
55 }
56 
57 WDataSetSingle::SPtr WDataSetSphericalHarmonics::clone( boost::shared_ptr< WValueSetBase > newValueSet ) const
58 {
59  return WDataSetSingle::SPtr( new WDataSetSphericalHarmonics( newValueSet, getGrid() ) );
60 }
61 
62 WDataSetSingle::SPtr WDataSetSphericalHarmonics::clone( boost::shared_ptr< WGrid > newGrid ) const
63 {
65 }
66 
68 {
70 }
71 
72 boost::shared_ptr< WPrototyped > WDataSetSphericalHarmonics::getPrototype()
73 {
74  if( !m_prototype )
75  {
76  m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetSphericalHarmonics() );
77  }
78 
79  return m_prototype;
80 }
81 
83 {
84  *success = m_gridRegular3D->encloses( pos );
85 
86  bool isInside = true;
87  size_t cellId = m_gridRegular3D->getCellId( pos, &isInside );
88 
89  if( !isInside )
90  {
91  *success = false;
93  }
94 
95  // ids of vertices for interpolation
96  WGridRegular3D::CellVertexArray vertexIds = m_gridRegular3D->getCellVertexIds( cellId );
97 
98  WPosition localPos = pos - m_gridRegular3D->getPosition( vertexIds[0] );
99 
100  double lambdaX = localPos[0] / m_gridRegular3D->getOffsetX();
101  double lambdaY = localPos[1] / m_gridRegular3D->getOffsetY();
102  double lambdaZ = localPos[2] / m_gridRegular3D->getOffsetZ();
103  WValue< double > h( 8 );
104 // lZ lY
105 // | /
106 // | 6___/_7
107 // |/: /|
108 // 4_:___5 |
109 // | :...|.|
110 // |.2 | 3
111 // |_____|/ ____lX
112 // 0 1
113  h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
114  h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
115  h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
116  h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
117  h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
118  h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
119  h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
120  h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
121 
122  // take
123  WValue<double> interpolatedCoefficients( m_valueSet->dimension() );
124  for( size_t i = 0; i < 8; ++i )
125  {
126  interpolatedCoefficients += h[i] * m_valueSet->getWValueDouble( vertexIds[i] );
127  }
128 
129  *success = true;
130 
131  return WSymmetricSphericalHarmonic( interpolatedCoefficients );
132 }
133 
135 {
136  if( index < m_valueSet->size() ) return WSymmetricSphericalHarmonic( m_valueSet->getWValueDouble( index ) );
138 }
139 
140 const std::string WDataSetSphericalHarmonics::getName() const
141 {
142  return "WDataSetSphericalHarmonics";
143 }
144 
146 {
147  return "Contains factors for spherical harmonics.";
148 }
149 
151 {
152  return false;
153 }
154