OpenWalnut  1.4.0
WValueSetBase.h
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 #ifndef WVALUESETBASE_H
00026 #define WVALUESETBASE_H
00027 
00028 #include <cstddef>
00029 #include <cmath>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 #include <boost/variant.hpp>
00033 
00034 #include "../common/math/WValue.h"
00035 #include "WDataHandlerEnums.h"
00036 
00037 
00038 //! forward declaration
00039 template< typename T >
00040 class WValueSet;
00041 
00042 //! declare a boost::variant of all possible valuesets
00043 typedef boost::variant< WValueSet< uint8_t > const*,
00044                         WValueSet< int8_t > const*,
00045                         WValueSet< uint16_t > const*,
00046                         WValueSet< int16_t > const*,
00047                         WValueSet< uint32_t > const*,
00048                         WValueSet< int32_t > const*,
00049                         WValueSet< uint64_t > const*,
00050                         WValueSet< int64_t > const*,
00051                         WValueSet< float > const*,
00052                         WValueSet< double > const*,
00053                         WValueSet< long double > const* > WValueSetVariant;
00054 
00055 /**
00056  * Abstract base class to all WValueSets. This class doesn't provide any genericness.
00057  * \ingroup dataHandler
00058  */
00059 class WValueSetBase // NOLINT
00060 {
00061 public:
00062     /**
00063      * Shared pointer to an instance of this class.
00064      */
00065     typedef boost::shared_ptr< WValueSetBase > SPtr;
00066 
00067     /**
00068      * Shared pointer to an const instance of this class.
00069      */
00070     typedef boost::shared_ptr< const WValueSetBase > ConstSPtr;
00071 
00072     /**
00073      * Despite this is an abstract class all subclasses should have an order
00074      * and dimension.
00075      * \param order the tensor order of the values stored in this WValueSet
00076      * \param dimension the tensor dimension of the values stored in this WValueSet
00077      * \param inDataType indication of the primitive data type used to store the values
00078      */
00079     WValueSetBase( size_t order, size_t dimension, dataType inDataType );
00080 
00081     /**
00082      * Dummy since each class with virtual member functions needs one.
00083      */
00084     virtual ~WValueSetBase() = 0;
00085 
00086     /**
00087      * \return The number of tensors in this ValueSet.
00088      */
00089     virtual size_t size() const = 0;
00090 
00091     /**
00092      * \return The number of integrals (POD like int, double) in this ValueSet.
00093      */
00094     virtual size_t rawSize() const = 0;
00095 
00096     /**
00097      * \param i id of the scalar to retrieve
00098      * \return The i-th scalar stored in this value set. There are rawSize() such scalars.
00099      */
00100     virtual double getScalarDouble( size_t i ) const = 0;
00101 
00102     /**
00103      * \param i id of the WValue to retrieve
00104      * \return The i-th WValue stored in this value set. There are size() such scalars.
00105      */
00106     virtual WValue< double > getWValueDouble( size_t i ) const = 0;
00107 
00108     /**
00109      * \return Dimension of the values in this ValueSet
00110      */
00111     virtual size_t dimension() const
00112     {
00113         return m_dimension;
00114     }
00115 
00116     /**
00117      * \return Order of the values in this ValueSet
00118      */
00119     virtual size_t order() const
00120     {
00121         return m_order;
00122     }
00123 
00124     /**
00125      * Returns the number of elements of type T per value.
00126      * \note this is dimension to the power of order.
00127      * \return number of elements per value
00128      */
00129     virtual size_t elementsPerValue() const
00130     {
00131         return static_cast< size_t >( std::pow( static_cast< double >( m_dimension ), static_cast< int >( m_order ) ) );
00132     }
00133 
00134     /**
00135      * \return Dimension of the values in this ValueSet
00136      */
00137     virtual dataType getDataType() const
00138     {
00139         return m_dataType;
00140     }
00141 
00142     /**
00143      * This method returns the smallest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
00144      * smallest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
00145      *
00146      * \return the smallest value in the data.
00147      */
00148     virtual double getMinimumValue() const = 0;
00149 
00150     /**
00151      * This method returns the largest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
00152      * largest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
00153      *
00154      * \return the largest value in the data.
00155      */
00156     virtual double getMaximumValue() const = 0;
00157 
00158     /**
00159      * Apply a function object to this valueset.
00160      *
00161      * \tparam Func_T The type of the function object, should be derived from the boost::static_visitor template.
00162      *
00163      * \param func The function object to apply.
00164      * \return The result of the operation.
00165      */
00166     template< typename Func_T >
00167     typename Func_T::result_type applyFunction( Func_T const& func )
00168     {
00169         return boost::apply_visitor( func, getVariant() );
00170     }
00171 
00172 protected:
00173     /**
00174      * The order of the tensors for this ValueSet
00175      */
00176     const size_t m_order;
00177 
00178     /**
00179      * The dimension of the tensors for this ValueSet
00180      */
00181     const size_t m_dimension;
00182 
00183     /**
00184      * The data type of the values' elements.
00185      */
00186     const dataType m_dataType;
00187 
00188 private:
00189     /**
00190      * Creates a boost::variant reference.
00191      *
00192      * \return var A pointer to a variant reference to the valueset.
00193      */
00194     virtual WValueSetVariant const getVariant() const
00195     {
00196         return WValueSetVariant();
00197     }
00198 };
00199 
00200 #endif  // WVALUESETBASE_H