Commit 8f59fa4b authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[ADD #172] we now have WPropInterval. Also added code to allow WPropInterval...

[ADD #172] we now have WPropInterval. Also added code to allow WPropInterval to be used comfortably as uniforms.
parent 365ac1aa
......@@ -37,6 +37,7 @@
#include "math/linearAlgebra/WLinearAlgebra.h"
#include "math/linearAlgebra/WMatrixFixed.h"
#include "math/linearAlgebra/WVectorFixed.h"
#include "math/WInterval.h"
#include "WAssert.h"
#include "WColor.h"
#include "WItemSelector.h"
......@@ -74,7 +75,8 @@ typedef enum
PV_MATRIX4X4, //!< for 4x4 matrices
PV_TRANSFERFUNCTION, //!< for transfer function textures
PV_STRUCT, //!< for complex, structured properties (used by \ref WPropertyStruct)
PV_LIST //!< for a dynamic list of properties of the same type (see \ref WPropertyList)
PV_LIST, //!< for a dynamic list of properties of the same type (see \ref WPropertyList)
PV_INTERVAL //!< for defining intervals (min and max values)
}
PROPERTY_TYPE;
......@@ -109,6 +111,7 @@ namespace WPVBaseTypes
typedef WColor PV_COLOR; //!< base type used for every WPVColor
typedef WMatrix4d PV_MATRIX4X4; //!< base type used for every WPVMatrix4X4
typedef WTransferFunction PV_TRANSFERFUNCTION; //!< base type for every transfer function
typedef WIntervalDouble PV_INTERVAL; //!< base type used for every PV_INTERVAL
/**
* Enum denoting the possible trigger states. It is used for trigger properties.
......@@ -216,6 +219,11 @@ typedef WPropertyVariable< WPVBaseTypes::PV_MATRIX4X4 > WPVMatrix4X4;
*/
typedef WPropertyVariable< WPVBaseTypes::PV_TRANSFERFUNCTION > WPVTransferFunction;
/**
* Interval properties
*/
typedef WPropertyVariable< WPVBaseTypes::PV_INTERVAL > WPVInterval;
/**
* Some convenience type alias for a even more easy usage of WPropertyVariable.
* These typdefs define some pointer alias.
......@@ -281,6 +289,11 @@ typedef boost::shared_ptr< WPVMatrix4X4 > WPropMatrix4X4;
*/
typedef boost::shared_ptr< WPVTransferFunction > WPropTransferFunction;
/**
* Alias for the interval properties
*/
typedef boost::shared_ptr< WPVInterval > WPropInterval;
/**
* This namespace contains several helper classes which translate their template type to an enum.
*/
......@@ -552,6 +565,24 @@ namespace PROPERTY_TYPE_HELPER
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
template<>
class WTypeIdentifier< WPVBaseTypes::PV_INTERVAL >
{
public:
/**
* Get type identifier of the template type T.
*
* \return type identifier-
*/
PROPERTY_TYPE getType()
{
return PV_INTERVAL;
}
};
/**
* Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
* serializable content which needs to be acquired from its predecessor instance.
......@@ -701,6 +732,48 @@ namespace PROPERTY_TYPE_HELPER
return out.str();
}
};
/**
* Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
* serializable content which needs to be acquired from its predecessor instance.
*/
template<>
class WStringConversion< WPVBaseTypes::PV_INTERVAL >
{
public:
/**
* Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
*
* \param str the new value as string
*
* \return the new instance
*/
WPVBaseTypes::PV_INTERVAL create( const WPVBaseTypes::PV_INTERVAL& /*old*/, const std::string str )
{
std::vector< std::string > tokens;
tokens = string_utils::tokenize( str, ";" );
WAssert( tokens.size() >= 2, "There weren't 2 values for an interval" );
WPVBaseTypes::PV_INTERVAL c( string_utils::fromString< double >( tokens[ 0 ] ),
string_utils::fromString< double >( tokens[ 1 ] ) );
return c;
}
/**
* Creates a string from the specified value.
*
* \param v the value to convert
*
* \return the string representation
*/
std::string asString( const WPVBaseTypes::PV_INTERVAL& v )
{
std::ostringstream out;
out << v.getLower() << ";" << v.getUpper();
return out.str();
}
};
}
#endif // WPROPERTYTYPES_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WInterval.h"
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WINTERVAL_H
#define WINTERVAL_H
#include <utility>
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include "../WTypeTraits.h"
/**
* Basic class for encapsulating a std::pair to be interpreted as interval. This class intentionally does not include a parameter telling whether
* the interval is open or not (mathematically: [],][,[[,]])
*
* \tparam T the type used for this interval
*/
template< typename T >
class WInterval
{
public:
/**
* Convenience typedef for a boost::shared_ptr< WInterval >.
*/
typedef boost::shared_ptr< WInterval< T > > SPtr;
/**
* Convenience typedef for a boost::shared_ptr< const WInterval >.
*/
typedef boost::shared_ptr< const WInterval< T > > ConstSPtr;
/**
* Type used to store the information
*/
typedef std::pair< T, T > StoreType;
/**
* My own type.
*/
typedef WInterval< T > Type;
/**
* Copy constructor to create a WInterval using a std::pair
*
* \param c the pair to use
*/
WInterval( const StoreType& c );
/**
* Copy constructor.
*
* \param c the interval to copy
*/
WInterval( const Type& c );
/**
* Create a new interval instance using the given values.
*
* \param l the lower border
* \param u the upper border
*/
WInterval( const T& l, const T& u );
/**
* Destructor.
*/
virtual ~WInterval();
/**
* Convert the WInterval instance to a std::pair again.
*
* \return the pair
*/
operator const StoreType& () const;
/**
* Get the lower value of the interval.
*
* \return the lower value
*/
const T& getLower() const;
/**
* Return the upper value of the interval
*
* \return the upper value
*/
const T& getUpper() const;
/**
* Compare this interval with another one
*
* \param interval the other one
*
* \return true if lower and upper bounds are equal
*/
bool operator==( Type interval ) const;
protected:
private:
/**
* The interval itself.
*/
StoreType m_interval;
};
/**
* Abbreviation for an double interval.
*/
typedef WInterval< double > WIntervalDouble;
/**
* Abbreviation for an integer interval
*/
typedef WInterval< int > WIntervalInt;
/**
* Create an interval instance similar to make_pair.
*
* \tparam T1 the lower bound type
* \tparam T2 the upper bound type
* \param l lower bound
* \param u upper bound
*
* \return the interval
*/
template < typename T1, typename T2 >
WInterval< typename WTypeTraits::TypePromotion< T1, T2 >::Result > make_interval( T1 l, T2 u )
{
return WInterval< typename WTypeTraits::TypePromotion< T1, T2 >::Result >( l, u );
}
/**
* Check whether a value is in the interval or not. This function interprets the interval as closed at both bounds.
*
* \tparam IntervalType type if the interval
* \tparam T type of the value to use for checking
* \param interval the interval to check against
* \param value the value
*
* \return true if inside
*/
template < typename IntervalType, typename T >
bool isInClosed( const IntervalType& interval, const T& value )
{
return ( ( interval.getLower() <= value ) && ( interval.getUpper() >= value ) );
}
/**
* Check whether a value is in the interval or not. This function interprets the interval as open at both bounds.
*
* \tparam IntervalType type if the interval
* \tparam T type of the value to use for checking
* \param interval the interval to check against
* \param value the value
*
* \return true if inside
*/
template < typename IntervalType, typename T >
bool isInOpen( const IntervalType& interval, const T& value )
{
return ( ( interval.getLower() < value ) && ( interval.getUpper() > value ) );
}
/**
* Check whether a value is in the interval or not. This function interprets the interval as open at the lower bound and closed at the upper one.
*
* \tparam IntervalType type if the interval
* \tparam T type of the value to use for checking
* \param interval the interval to check against
* \param value the value
*
* \return true if inside
*/
template < typename IntervalType, typename T >
bool isInOpenClosed( const IntervalType& interval, const T& value )
{
return ( ( interval.getLower() < value ) && ( interval.getUpper() >= value ) );
}
/**
* Check whether a value is in the interval or not. This function interprets the interval as closed at the lower bound and open at the upper one.
*
* \tparam IntervalType type if the interval
* \tparam T type of the value to use for checking
* \param interval the interval to check against
* \param value the value
*
* \return true if inside
*/
template < typename IntervalType, typename T >
bool isInClosedOpen( const IntervalType& interval, const T& value )
{
return ( ( interval.getLower() <= value ) && ( interval.getUpper() > value ) );
}
template < typename T >
WInterval< T >::WInterval( const StoreType& c )
{
// ensure order
m_interval.first = std::min( c.first, c.second );
m_interval.second = std::min( c.first, c.second );
}
template < typename T >
WInterval< T >::WInterval( const Type& c ):
m_interval( c.m_interval )
{
// nothing else to do
}
template < typename T >
WInterval< T >::WInterval( const T& l, const T& u ):
m_interval( std::min( l, u ), std::max( l, u ) )
{
// nothing else to do
}
template < typename T >
WInterval< T >::~WInterval()
{
// nothing else to do
}
template < typename T >
WInterval< T >::operator const StoreType& () const
{
return m_interval;
}
template < typename T >
const T& WInterval< T >::getLower() const
{
return m_interval.first;
}
template < typename T >
const T& WInterval< T >::getUpper() const
{
return m_interval.second;
}
template < typename T >
bool WInterval< T >::operator==( Type interval ) const
{
return ( ( interval.getLower() == getLower() ) && ( interval.getUpper() == getUpper() ) );
}
#endif // WINTERVAL_H
......@@ -153,6 +153,13 @@ public:
*/
WPropBool active() const;
/**
* Returns the window level definition for the colormap. The property can be changed. A change affects all colormaps using this texture.
*
* \return window colormap
*/
WPropInterval window() const;
/**
* Returns the texture transformation matrix. The property can be changed. A change affects all colormaps using this texture. This matrix
* converts an world-space coordinate to an texture coordinate! This can be seen as a scaled inverse matrix of the grid's transformation.
......@@ -351,6 +358,11 @@ private:
* The texture transformation matrix.
*/
WPropMatrix4X4 m_texMatrix;
/**
* Window level setting for the current colormap
*/
WPropInterval m_window;
};
// Some convenience typedefs
......@@ -446,6 +458,9 @@ void WGETexture< TextureType >::setupProperties( double scale, double min )
m_active = m_properties->addProperty( "Active", "Can dis-enable a texture.", true );
m_window = m_properties->addProperty( "Window Level", "Define the interval in the data which is mapped to the colormap.",
make_interval( 0.0, 1.0 ) );
WMatrix4d m = WMatrix4d::identity();
m_texMatrix = m_properties->addProperty( "Texture Transformation", "Usable to transform the texture.", m );
m_texMatrix->setPurpose( PV_PURPOSE_INFORMATION );
......@@ -532,6 +547,12 @@ inline WPropBool WGETexture< TextureType >::active() const
return m_active;
}
template < typename TextureType >
inline WPropInterval WGETexture< TextureType >::window() const
{
return m_window;
}
template < typename TextureType >
inline WPropMatrix4X4 WGETexture< TextureType >::transformation() const
{
......@@ -558,6 +579,7 @@ void WGETexture< TextureType >::applyUniforms( std::string prefix, osg::StateSe
states->addUniform( new WGEPropertyUniform< WPropDouble >( prefix + "Threshold", threshold() ) );
states->addUniform( new WGEPropertyUniform< WPropSelection >( prefix + "Colormap", colormap() ) );
states->addUniform( new WGEPropertyUniform< WPropBool >( prefix + "Active", active() ) );
states->addUniform( new WGEPropertyUniform< WPropInterval >( prefix + "Window", window() ) );
}
template < typename TextureType >
......
......@@ -94,7 +94,7 @@ WGEPropertyUniformCallback< T >::~WGEPropertyUniformCallback()
template< typename T >
void WGEPropertyUniformCallback< T >::operator()( osg::Uniform* uniform, osg::NodeVisitor* /*nv*/ )
{
uniform->set( static_cast< typename WGEPropertyUniformCallback< T >::UniformType >( m_property->get() ) );
uniform->set( wge::toUniformType( m_property->get() ) );
}
#endif // WGEPROPERTYUNIFORMCALLBACK_H
......
......@@ -87,7 +87,7 @@ private:
template < typename T >
WGEPropertyUniform< T >::WGEPropertyUniform( std::string name, T property ):
osg::Uniform( name.c_str(), static_cast< typename WGEPropertyUniform< T >::UniformType >( property->get() ) ),
osg::Uniform( name.c_str(), typename WGEPropertyUniform< T >::UniformType() ),
m_property( property ),
m_name( name )
{
......
......@@ -24,3 +24,10 @@
#include "WGEUniformTypeTraits.h"
namespace wge
{
osg::Vec2 toUniformType( const WIntervalDouble& in )
{
return osg::Vec2( in.getLower(), in.getUpper() );
}
}
......@@ -26,9 +26,10 @@
#define WGEUNIFORMTYPETRAITS_H
#include <osg/Vec3>
#include "../../common/math/linearAlgebra/WLinearAlgebra.h"
#include <osg/Vec2>
#include "../../common/math/linearAlgebra/WLinearAlgebra.h"
#include "../../common/math/WInterval.h"
class WItemSelector;
......@@ -88,6 +89,42 @@ namespace wge
*/
typedef int Type;
};
/**
* Maps Selection Properties to ints.
*/
template<>
class UniformType< WIntervalDouble >
{
public:
/**
* The best matching GLSL uniform type for the specified template parameter.
*/
typedef osg::Vec2 Type;
};
/**
* Create an instance of the uniform target type given some input type.
*
* \tparam InType the input type
* \param in the input value
*
* \return the resulting uniform type
*/
template< typename InType >
typename UniformType< InType >::Type toUniformType( const InType& in )
{
return static_cast< typename UniformType< InType >::Type >( in );
}
/**
* Creates an OSG vec2 when an interval is inserted.
*
* \param in the interval to convert
*
* \return the vector
*/
osg::Vec2 toUniformType( const WIntervalDouble& in );
}
#endif // WGEUNIFORMTYPETRAITS_H
......@@ -258,6 +258,11 @@ void WMTemplate::properties()
m_aSingleSelectionUsingTypes = m_properties->addProperty( "Choose one", "Choose on of these and watch the console output.",
m_possibleSelectionsUsingTypes->getSelectorFirst(), m_propCondition );
// There are more property types available. Check out core/common/WPropertyTypes.h. One last to mention here is the WPropInterval. This
// property type allows you to define intervals. You can create one in the same way you created the other properties:
m_anInterval = m_properties->addProperty( "Interval", "Choose some interval please", WIntervalDouble( -1.0, 100.0 ), m_propCondition );
// Important to know is that you can only use WIntervalDouble. You can also use make_interval, which works similar to std::make_pair.
// Adding a lot of properties might confuse the user. Using WPropGroup, you have the possibility to group your properties together. A
// WPropGroup needs a name and can provide a description. As with properties, the name should not contain any "/" and must be unique.
......@@ -713,6 +718,15 @@ void WMTemplate::moduleMain()
// which shows a dialog or something similar. Additionally, the m_isCrashed flag is set to true. Once a module is crahsed, it cannot
// be "restored".
}
// Check if the interval was modified.
if( m_anInterval->changed() )
{
// Grab the value as you have learned above:
WIntervalDouble i = m_anInterval->get( true );
// And use it somehow:
infoLog() << "You have selected this interval: [" << i.getLower() << ", " << i.getUpper() << "].";
}
}
// At this point, the container managing this module signaled to shutdown. The main loop has ended and you should clean up:
......
......@@ -292,6 +292,11 @@ private:
*/
WPropTrigger m_exceptionTrigger;
/**
* A property allowing you to define whole intervals.
*/
WPropInterval m_anInterval;
/**
* The callback triggering the hide flag of m_aColor for demonstration.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment