OpenWalnut  1.4.0
WPropertyTypes.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 "WStringUtils.h"
00029 #include "WPropertyTypes.h"
00030 #include "WTransferFunction.h"
00031 
00032 std::ostream& WPVBaseTypes::operator<<( std::ostream& out, const WPVBaseTypes::PV_TRIGGER& c )
00033 {
00034     // print it as nice string
00035     switch( c )
00036     {
00037         case PV_TRIGGER_TRIGGERED:
00038             out << "PV_TRIGGER_TRIGGERED";
00039             break;
00040         default:
00041             out << "PV_TRIGGER_READY";
00042     }
00043 
00044     return out;
00045 }
00046 
00047 std::istream& WPVBaseTypes::operator>>( std::istream& in, WPVBaseTypes::PV_TRIGGER& c )
00048 {
00049     std::string s;
00050     in >> s;
00051 
00052     // interpret string
00053     if( s == "PV_TRIGGER_TRIGGERED" )
00054     {
00055         c = PV_TRIGGER_TRIGGERED;
00056     }
00057     else
00058     {
00059         c = PV_TRIGGER_READY;
00060     }
00061 
00062     return in;
00063 }
00064 
00065 bool WPVBaseTypes::isPropertyGroup( PROPERTY_TYPE  type )
00066 {
00067     switch( type )
00068     {
00069     case PV_GROUP:
00070     case PV_STRUCT:
00071         return true;
00072     default:
00073         return false;
00074     }
00075 }
00076 
00077 namespace PROPERTY_TYPE_HELPER
00078 {
00079     WPVBaseTypes::PV_TRANSFERFUNCTION WStringConversion< WPVBaseTypes::PV_TRANSFERFUNCTION >::create(
00080             const WPVBaseTypes::PV_TRANSFERFUNCTION& /*old*/, const std::string str )
00081     {
00082         WTransferFunction tf;
00083         std::vector< std::string > tokens;
00084         tokens = string_utils::tokenize( str, ";" );
00085         //WAssert( tokens.size() >= 16, "There weren't 16 values for a 4x4 Matrix" );
00086         size_t idx = 0;
00087         while( idx < tokens.size() )
00088         {
00089             std::vector< std::string > innerTokens;
00090             innerTokens = string_utils::tokenize( tokens[ idx ], ":" );
00091             // evaluate inner tokens
00092             {
00093                 if( innerTokens[ 0 ] == "c" )
00094                 {
00095                     tf.addColor( string_utils::fromString< double >( innerTokens[ 1 ].c_str() ), // isovalue
00096                                  WColor( string_utils::fromString< double >( innerTokens[ 2 ].c_str() ), // red
00097                                          string_utils::fromString< double >( innerTokens[ 3 ].c_str() ), // green
00098                                          string_utils::fromString< double >( innerTokens[ 4 ].c_str() ), // blue
00099                                          1. )
00100                                );  // blue
00101                 }
00102                 else if( innerTokens[ 0 ] == "a" )
00103                 {
00104                     tf.addAlpha( string_utils::fromString< double >( innerTokens[ 1 ].c_str() ),
00105                                  string_utils::fromString< double >( innerTokens[ 2 ].c_str() ) );
00106                 }
00107                 idx++;
00108             }
00109         }
00110         return tf;
00111     }
00112 
00113     std::string WStringConversion< WPVBaseTypes::PV_TRANSFERFUNCTION >::asString( const WPVBaseTypes::PV_TRANSFERFUNCTION& tf )
00114     {
00115         std::ostringstream out;
00116         size_t numColors = tf.numColors();
00117         for( size_t i = 0; i < numColors; ++i )
00118         {
00119             double iso = tf.getColorIsovalue( i );
00120             WColor c = tf.getColor( i );
00121             out << "c:" << iso << ":" << c[ 0 ] << ":" << c[ 1 ] << ":" << c[ 2 ] << ";";
00122         }
00123         size_t numAlphas = tf.numAlphas();
00124         for( size_t i = 0; i < numAlphas; ++i )
00125         {
00126             double iso = tf.getAlphaIsovalue( i );
00127             double alpha = tf.getAlpha( i );
00128             out << "a:" << iso << ":" << alpha;
00129             if( i != numAlphas-1 )
00130             {
00131                 out << ";";
00132             }
00133         }
00134         return out.str();
00135     }
00136 }