OpenWalnut  1.4.0
WDataSetSingle.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 #include <vector>
00027 
00028 #include "../common/WAssert.h"
00029 #include "../common/WException.h"
00030 #include "../common/WPrototyped.h"
00031 #include "WDataTexture3D.h"
00032 #include "WGrid.h"
00033 #include "WGridRegular3D.h"
00034 #include "WValueSet.h"
00035 
00036 #include "WDataSetSingle.h"
00037 
00038 // prototype instance as singleton
00039 boost::shared_ptr< WPrototyped > WDataSetSingle::m_prototype = boost::shared_ptr< WPrototyped >();
00040 
00041 WDataSetSingle::WDataSetSingle( boost::shared_ptr< WValueSetBase > newValueSet,
00042                                 boost::shared_ptr< WGrid > newGrid )
00043     : WDataSet(),
00044     m_texture()
00045 {
00046     WAssert( newValueSet, "Need a value set for new data set." );
00047     WAssert( newGrid, "Need a grid for new data set." );
00048     WAssert( newValueSet->size() == newGrid->size(),
00049              "Number of grid position unequal number of values in value set." );
00050 
00051     m_valueSet = newValueSet;
00052     m_grid = newGrid;
00053 
00054     m_infoProperties->addProperty( m_grid->getInformationProperties() );
00055 
00056     // technically this should be placed into the WDataSetScalar, WDataSetVector and so on
00057     boost::shared_ptr< WGridRegular3D > regGrid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
00058     if( regGrid && ( m_valueSet->dimension() < 5 ) && ( m_valueSet->dimension() != 0 ) )
00059     {
00060         m_texture = osg::ref_ptr< WDataTexture3D >( new WDataTexture3D( m_valueSet, regGrid ) );
00061     }
00062 }
00063 
00064 WDataSetSingle::WDataSetSingle()
00065     : WDataSet(),
00066     m_grid(),
00067     m_valueSet(),
00068     m_texture()
00069 {
00070     // default constructor used by the prototype mechanism
00071 }
00072 
00073 WDataSetSingle::~WDataSetSingle()
00074 {
00075 }
00076 
00077 WDataSetSingle::SPtr WDataSetSingle::clone( boost::shared_ptr< WValueSetBase > newValueSet ) const
00078 {
00079     return WDataSetSingle::SPtr( new WDataSetSingle( newValueSet, getGrid() ) );
00080 }
00081 
00082 WDataSetSingle::SPtr WDataSetSingle::clone( boost::shared_ptr< WGrid > newGrid ) const
00083 {
00084     return WDataSetSingle::SPtr( new WDataSetSingle( getValueSet(), newGrid ) );
00085 }
00086 
00087 WDataSetSingle::SPtr WDataSetSingle::clone() const
00088 {
00089     return WDataSetSingle::SPtr( new WDataSetSingle( getValueSet(), getGrid() ) );
00090 }
00091 
00092 boost::shared_ptr< WValueSetBase > WDataSetSingle::getValueSet() const
00093 {
00094     return m_valueSet;
00095 }
00096 
00097 boost::shared_ptr< WGrid > WDataSetSingle::getGrid() const
00098 {
00099     return m_grid;
00100 }
00101 
00102 bool WDataSetSingle::isTexture() const
00103 {
00104     // TODO(all): this is not sophisticated. This should depend on type of data (vectors? scalars? tensors?)
00105     return m_texture;
00106 }
00107 
00108 osg::ref_ptr< WDataTexture3D > WDataSetSingle::getTexture() const
00109 {
00110     return m_texture;
00111 }
00112 
00113 const std::string WDataSetSingle::getName() const
00114 {
00115     return "WDataSetSingle";
00116 }
00117 
00118 const std::string WDataSetSingle::getDescription() const
00119 {
00120     return "A single dataset containing a number of WValues on a structured"
00121         "grid. Single, in this case, means not time-dependent and not one type of"
00122         "data for several subjects.";
00123 }
00124 
00125 boost::shared_ptr< WPrototyped > WDataSetSingle::getPrototype()
00126 {
00127     if( !m_prototype )
00128     {
00129         m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetSingle() );
00130     }
00131 
00132     return m_prototype;
00133 }
00134 
00135 double WDataSetSingle::getValueAt( size_t id ) const
00136 {
00137     switch( getValueSet()->getDataType() )
00138     {
00139         case W_DT_UNSIGNED_CHAR:
00140         {
00141             return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< uint8_t > >( getValueSet() )->getScalar( id ) );
00142         }
00143         case W_DT_INT16:
00144         {
00145             return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< int16_t > >( getValueSet() )->getScalar( id ) );
00146         }
00147         case W_DT_SIGNED_INT:
00148         {
00149             return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< int32_t > >( getValueSet() )->getScalar( id ) );
00150         }
00151         case W_DT_FLOAT:
00152         {
00153             return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< float > >( getValueSet() )->getScalar( id ) );
00154         }
00155         case W_DT_DOUBLE:
00156         {
00157             return static_cast< double >( boost::dynamic_pointer_cast< WValueSet< double > >( getValueSet() )->getScalar( id ) );
00158         }
00159         default:
00160             WAssert( false, "Unknow data type in dataset." );
00161     }
00162 
00163     return 0.0; // should not be reached. Just there to quiet compiler.
00164 }
00165