OpenWalnut  1.4.0
WDataSetSingle.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 <string>
26 #include <vector>
27 
28 #include "../common/WAssert.h"
29 #include "../common/WException.h"
30 #include "../common/WPrototyped.h"
31 #include "WDataTexture3D.h"
32 #include "WGrid.h"
33 #include "WGridRegular3D.h"
34 #include "WValueSet.h"
35 
36 #include "WDataSetSingle.h"
37 
38 // prototype instance as singleton
39 boost::shared_ptr< WPrototyped > WDataSetSingle::m_prototype = boost::shared_ptr< WPrototyped >();
40 
41 WDataSetSingle::WDataSetSingle( boost::shared_ptr< WValueSetBase > newValueSet,
42  boost::shared_ptr< WGrid > newGrid )
43  : WDataSet(),
44  m_texture()
45 {
46  WAssert( newValueSet, "Need a value set for new data set." );
47  WAssert( newGrid, "Need a grid for new data set." );
48  WAssert( newValueSet->size() == newGrid->size(),
49  "Number of grid position unequal number of values in value set." );
50 
51  m_valueSet = newValueSet;
52  m_grid = newGrid;
53 
54  m_infoProperties->addProperty( m_grid->getInformationProperties() );
55 
56  // technically this should be placed into the WDataSetScalar, WDataSetVector and so on
57  boost::shared_ptr< WGridRegular3D > regGrid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
58  if( regGrid && ( m_valueSet->dimension() < 5 ) && ( m_valueSet->dimension() != 0 ) )
59  {
60  m_texture = osg::ref_ptr< WDataTexture3D >( new WDataTexture3D( m_valueSet, regGrid ) );
61  }
62 }
63 
65  : WDataSet(),
66  m_grid(),
67  m_valueSet(),
68  m_texture()
69 {
70  // default constructor used by the prototype mechanism
71 }
72 
74 {
75 }
76 
77 WDataSetSingle::SPtr WDataSetSingle::clone( boost::shared_ptr< WValueSetBase > newValueSet ) const
78 {
79  return WDataSetSingle::SPtr( new WDataSetSingle( newValueSet, getGrid() ) );
80 }
81 
82 WDataSetSingle::SPtr WDataSetSingle::clone( boost::shared_ptr< WGrid > newGrid ) const
83 {
84  return WDataSetSingle::SPtr( new WDataSetSingle( getValueSet(), newGrid ) );
85 }
86 
88 {
90 }
91 
92 boost::shared_ptr< WValueSetBase > WDataSetSingle::getValueSet() const
93 {
94  return m_valueSet;
95 }
96 
97 boost::shared_ptr< WGrid > WDataSetSingle::getGrid() const
98 {
99  return m_grid;
100 }
101 
103 {
104  // TODO(all): this is not sophisticated. This should depend on type of data (vectors? scalars? tensors?)
105  return m_texture;
106 }
107 
108 osg::ref_ptr< WDataTexture3D > WDataSetSingle::getTexture() const
109 {
110  return m_texture;
111 }
112 
113 const std::string WDataSetSingle::getName() const
114 {
115  return "WDataSetSingle";
116 }
117 
118 const std::string WDataSetSingle::getDescription() const
119 {
120  return "A single dataset containing a number of WValues on a structured"
121  "grid. Single, in this case, means not time-dependent and not one type of"
122  "data for several subjects.";
123 }
124 
125 boost::shared_ptr< WPrototyped > WDataSetSingle::getPrototype()
126 {
127  if( !m_prototype )
128  {
129  m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetSingle() );
130  }
131 
132  return m_prototype;
133 }
134 
135 double WDataSetSingle::getValueAt( size_t id ) const
136 {
137  switch( getValueSet()->getDataType() )
138  {
139  case W_DT_UNSIGNED_CHAR:
140  {
141  return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< uint8_t > >( getValueSet() )->getScalar( id ) );
142  }
143  case W_DT_INT16:
144  {
145  return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< int16_t > >( getValueSet() )->getScalar( id ) );
146  }
147  case W_DT_SIGNED_INT:
148  {
149  return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< int32_t > >( getValueSet() )->getScalar( id ) );
150  }
151  case W_DT_FLOAT:
152  {
153  return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< float > >( getValueSet() )->getScalar( id ) );
154  }
155  case W_DT_DOUBLE:
156  {
157  return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< double > >( getValueSet() )->getScalar( id ) );
158  }
159  default:
160  WAssert( false, "Unknow data type in dataset." );
161  }
162 
163  return 0.0; // should not be reached. Just there to quiet compiler.
164 }
165