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