OpenWalnut  1.5.0dev
WDataCreatorSphere.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 <memory>
26 #include <vector>
27 
28 #include "WDataCreatorSphere.h"
29 #include "core/common/WAssert.h"
30 #include "core/common/WLimits.h"
31 
33  WObjectNDIP< WDataSetSingleCreatorInterface >( "Spherical", "Creates a spherical volume." )
34 {
35  // add some properties
36  m_center = m_properties->addProperty( "Center", "The center point in relative coordinates, where 0.5 is the center. At this point, "
37  "the value in the scalar field will be zero.",
38  WPosition( 0.5, 0.5, 0.5 ) );
39  m_radius = m_properties->addProperty( "Radius", "The radius in relative coordinates, where 0.5 creates a sphere in the size of the grid.",
40  0.5 );
41  m_radius->setMin( 0.0 );
42 
43  m_lowerClamp = m_properties->addProperty( "Lower Clamp Threshold", "Defines a threshold which allows all values below this value to be set to 0."
44  "This can be handy to create spheres which are 0 in the inside.",
45  0.0 );
46  m_lowerClamp->setMin( 0.0 );
47  m_upperClamp = m_properties->addProperty( "Upper Clamp Threshold", "Defines a threshold which allows all values above this value to be set to 0."
48  "This can be handy to create an empty field around the sphere",
50  m_upperClamp->setMin( 0.0 );
51 
52  m_lowerClampValue = m_properties->addProperty( "Lower Clamp Value", "Value to use when below lower clamp threshold.", 0.0 );
53  m_upperClampValue = m_properties->addProperty( "Upper Clamp Value", "Value to use when above upper clamp threshold.", 0.0 );
54 }
55 
57 {
58 }
59 
61  WGridRegular3D::ConstSPtr grid, unsigned char order, unsigned char dimension,
62  dataType /*type*/ )
63 {
64  // this creator only supports valuesets for scalar data.
65  WAssert( ( order == 0 ) && ( dimension == 1 ), "The sphere data creator only supports scalar data." );
66 
67  // currently, the type is fixed. This will come soon.
68  typedef double ValueType;
69  typedef WValueSet< ValueType > ValueSetType;
70 
71  // create some memory for the data
72  std::shared_ptr< std::vector< ValueType > > data( new std::vector< ValueType > );
73  // for scalar data we need only as much space as we have voxels
74  data->resize( grid->size() );
75 
76  double originX = m_center->get().x();
77  double originY = m_center->get().y();
78  double originZ = m_center->get().z();
79 
80  // the formular below calculates the stuff in -1,1 interval. The radius is meant to be used in -0.5 to 0.5 -> so scale up
81  double radius = 2.0 * m_radius->get();
82 
83  // clamp info
84  double lowClamp = m_lowerClamp->get();
85  double upClamp = m_upperClamp->get();
86  double upClampValue = m_upperClampValue->get();
87  double lowClampValue = m_lowerClampValue->get();
88 
89  // iterate the data and fill in some values
90  double xRel = 0.0;
91  double yRel = 0.0;
92  double zRel = 0.0;
93  for( size_t x = 0; x < grid->getNbCoordsX(); ++x )
94  {
95  xRel = static_cast< double >( x ) / static_cast< double >( grid->getNbCoordsX() - 1 );
96  xRel -= originX;
97  xRel *= 2.0;
98 
99  for( size_t y = 0; y < grid->getNbCoordsY(); ++y )
100  {
101  yRel = static_cast< double >( y ) / static_cast< double >( grid->getNbCoordsY() - 1 );
102  yRel -= originY;
103  yRel *= 2.0;
104 
105  for( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
106  {
107  zRel = static_cast< double >( z ) / static_cast< double >( grid->getNbCoordsZ() - 1 );
108  zRel -= originZ;
109  zRel *= 2.0;
110 
111  ValueType val = static_cast< ValueType >( ( 1.0 / ( radius * radius ) ) *
112  ( ( xRel * xRel ) +
113  ( yRel * yRel ) +
114  ( zRel * zRel ) ) );
115 
116  // set value
117  data->operator[]( grid->getVoxelNum( x, y, z ) ) = val < lowClamp ? lowClampValue : ( val > upClamp ? upClampValue : val );
118  }
119 
120  // updating progress for each voxel is not needed. It is enough to update each slice
121  progress->increment( grid->getNbCoordsZ() );
122  }
123  }
124 
125  // finally, create the value set and return it
126  // We have scalar data (order = 0 ) in 3d
127  return ValueSetType::SPtr( new ValueSetType( 0, 1, data ) );
128 }
WPropDouble m_lowerClampValue
To which value should be clamped?
WPropDouble m_upperClampValue
To which value should be clamped?
virtual ~WDataCreatorSphere()
Destructor.
WPropDouble m_upperClamp
Clamp to 0 for all values above.
WPropDouble m_lowerClamp
Clamp to 0 for all values below this one.
WDataCreatorSphere()
Default constructor.
WPropDouble m_radius
The radius of the sphere in relative coordinates.
WPropPosition m_center
Relative center coordinates.
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.
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
This only is a 3d double vector.
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...
const double MAX_DOUBLE
Maximum double value.
Definition: WLimits.cpp:31