OpenWalnut 1.3.1
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      * \param i id of the WVector to retrieve
00110      * \return The i-th WValue (stored in this value set) as WVector. There are size() such scalars.
00111      */
00112     virtual WVector_2 getWVector( size_t i ) const = 0;
00113 
00114     /**
00115      * \return Dimension of the values in this ValueSet
00116      */
00117     virtual size_t dimension() const
00118     {
00119         return m_dimension;
00120     }
00121 
00122     /**
00123      * \return Order of the values in this ValueSet
00124      */
00125     virtual size_t order() const
00126     {
00127         return m_order;
00128     }
00129 
00130     /**
00131      * Returns the number of elements of type T per value.
00132      * \note this is dimension to the power of order.
00133      * \return number of elements per value
00134      */
00135     virtual size_t elementsPerValue() const
00136     {
00137         return static_cast< size_t >( std::pow( static_cast< double >( m_dimension ), static_cast< int >( m_order ) ) );
00138     }
00139 
00140     /**
00141      * \return Dimension of the values in this ValueSet
00142      */
00143     virtual dataType getDataType() const
00144     {
00145         return m_dataType;
00146     }
00147 
00148     /**
00149      * This method returns the smallest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
00150      * smallest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
00151      *
00152      * \return the smallest value in the data.
00153      */
00154     virtual double getMinimumValue() const = 0;
00155 
00156     /**
00157      * This method returns the largest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
00158      * largest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
00159      *
00160      * \return the largest value in the data.
00161      */
00162     virtual double getMaximumValue() const = 0;
00163 
00164     /**
00165      * Apply a function object to this valueset.
00166      *
00167      * \tparam Func_T The type of the function object, should be derived from the boost::static_visitor template.
00168      *
00169      * \param func The function object to apply.
00170      * \return The result of the operation.
00171      */
00172     template< typename Func_T >
00173     typename Func_T::result_type applyFunction( Func_T const& func )
00174     {
00175         return boost::apply_visitor( func, getVariant() );
00176     }
00177 
00178 protected:
00179     /**
00180      * The order of the tensors for this ValueSet
00181      */
00182     const size_t m_order;
00183 
00184     /**
00185      * The dimension of the tensors for this ValueSet
00186      */
00187     const size_t m_dimension;
00188 
00189     /**
00190      * The data type of the values' elements.
00191      */
00192     const dataType m_dataType;
00193 
00194 private:
00195     /**
00196      * Creates a boost::variant reference.
00197      *
00198      * \return var A pointer to a variant reference to the valueset.
00199      */
00200     virtual WValueSetVariant const getVariant() const
00201     {
00202         return WValueSetVariant();
00203     }
00204 };
00205 
00206 #endif  // WVALUESETBASE_H