OpenWalnut  1.5.0dev
WDataCreatorRandom.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 "WDataCreatorRandom.h"
31 #include "core/common/WLogger.h"
32 #include "core/dataHandler/WDataHandlerEnums.h"
33 #include "core/dataHandler/WValueSet.h"
34 
36  WObjectNDIP< WDataSetSingleCreatorInterface >( "Random", "Creates a noise volume." )
37 {
38  m_rangeMin = m_properties->addProperty( "Range Min", "The minimum value in the data.", 0.0 );
39  m_rangeMax = m_properties->addProperty( "Range Max", "The maximum value in the data.", 1.0 );
40 }
41 
43 {
44 }
45 
47  WGridRegular3D::ConstSPtr grid, unsigned char order, unsigned char dimension,
48  dataType /*type*/ )
49 {
50  std::srand( time( 0 ) );
51 
52  // currently, the type is fixed. This will come soon.
53  typedef double ValueType;
54  typedef WValueSet< ValueType > ValueSetType;
55 
56  // create some memory for the data
57  std::shared_ptr< std::vector< ValueType > > data( new std::vector< ValueType > );
58  // for scalar data we need only as much space as we have voxels
59  size_t valuesPerVoxel = ValueSetType::getRequiredRawSizePerVoxel( order, dimension );
60  data->resize( valuesPerVoxel * grid->size() );
61 
62  // iterate the data and fill in some random values
63  for( size_t x = 0; x < grid->getNbCoordsX(); ++x )
64  {
65  for( size_t y = 0; y < grid->getNbCoordsY(); ++y )
66  {
67  for( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
68  {
69  // each voxels might need multiple values
70  for( size_t v = 0; v < valuesPerVoxel; ++v )
71  {
72  // NOLINT: because we do not want to use rand_r.
73  double randD = static_cast< double >( std::rand() ) / static_cast< double >( RAND_MAX ); // NOLINT
74  data->operator[]( ( valuesPerVoxel * grid->getVoxelNum( x, y, z ) ) + v ) =
75  static_cast< ValueType >( m_rangeMin->get() + ( std::abs( m_rangeMin->get() - m_rangeMax->get() ) * randD ) );
76  }
77  }
78 
79  // updating progress for each voxel is not needed. It is enough to update each slice
80  progress->increment( grid->getNbCoordsZ() );
81  }
82  }
83 
84  // finally, create the value set and return it
85  // We have scalar data (order = 0 ) in 3D
86  return ValueSetType::SPtr( new ValueSetType( order, dimension , data ) );
87 }
88 
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_rangeMax
Maximum value in the dataset.
WDataCreatorRandom()
Default constructor.
virtual ~WDataCreatorRandom()
Destructor.
WPropDouble m_rangeMin
Minimum 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...