OpenWalnut  1.5.0dev
WDataCreatorConstant.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 <cstdlib>
26 #include <ctime>
27 #include <memory>
28 #include <vector>
29 
30 #include "WDataCreatorConstant.h"
31 #include "core/common/WLogger.h"
32 #include "core/dataHandler/WDataHandlerEnums.h"
33 #include "core/dataHandler/WValueSet.h"
34 
36  WObjectNDIP< WDataSetSingleCreatorInterface >( "Constant", "Creates a volume containing only one value." )
37 {
38  m_value = m_properties->addProperty( "Value", "The value in the data.", 0.0 );
39 }
40 
42 {
43 }
44 
46  WGridRegular3D::ConstSPtr grid, unsigned char order, unsigned char dimension,
47  dataType /*type*/ )
48 {
49  std::srand( time( 0 ) );
50 
51  // currently, the type is fixed. This will come soon.
52  typedef double ValueType;
53  typedef WValueSet< ValueType > ValueSetType;
54 
55  ValueType val = m_value->get( true );
56 
57  // create some memory for the data
58  std::shared_ptr< std::vector< ValueType > > data( new std::vector< ValueType > );
59  // for scalar data we need only as much space as we have voxels
60  size_t valuesPerVoxel = ValueSetType::getRequiredRawSizePerVoxel( order, dimension );
61  data->resize( valuesPerVoxel * grid->size() );
62 
63  // iterate the data and fill in some conatnt values
64  for( size_t x = 0; x < grid->getNbCoordsX(); ++x )
65  {
66  for( size_t y = 0; y < grid->getNbCoordsY(); ++y )
67  {
68  for( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
69  {
70  // each voxels might need multiple values
71  for( size_t v = 0; v < valuesPerVoxel; ++v )
72  {
73  data->operator[]( ( valuesPerVoxel * grid->getVoxelNum( x, y, z ) ) + v ) =
74  static_cast< ValueType >( val );
75  }
76  }
77 
78  // updating progress for each voxel is not needed. It is enough to update each slice
79  progress->increment( grid->getNbCoordsZ() );
80  }
81  }
82 
83  // finally, create the value set and return it
84  // We have scalar data (order = 0 ) in 3D
85  return ValueSetType::SPtr( new ValueSetType( order, dimension , data ) );
86 }
87 
WDataCreatorConstant()
Default constructor.
virtual ~WDataCreatorConstant()
Destructor.
virtual WValueSetBase::SPtr operator()(WProgress::SPtr progress, WGridRegular3D::ConstSPtr grid, unsigned char order=0, unsigned char dimension=1, dataType type=W_DT_FLOAT)
Create the dataset.
WPropDouble m_value
The value in the dataset.
Define the interface which is injected into an WObjectNDIP.
std::shared_ptr< const WGridRegular3DTemplate > ConstSPtr
Convenience typedef for a std::shared_ptr< const WGridRegular3DTemplate >.
This is a base class for everything which has a Name,Description,Icon and Properties (=NDIP).
Definition: WObjectNDIP.h:42
WProperties::SPtr m_properties
the properties of the object.
Definition: WObjectNDIP.h:99
std::shared_ptr< WProgress > SPtr
Shared pointer on a WProgress.
Definition: WProgress.h:48
std::shared_ptr< WValueSetBase > SPtr
Shared pointer to an instance of this class.
Definition: WValueSetBase.h:65
Base Class for all value set types.
Definition: WValueSet.h:47
dataType
Data types and number values taken from the nifti1.h, at this point it's unknown if it makes sense to...