OpenWalnut  1.5.0dev
WPropertyTypes.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 #include <vector>
27 
28 #include "WStringUtils.h"
29 #include "WPropertyTypes.h"
30 #include "WTransferFunction.h"
31 
32 std::ostream& WPVBaseTypes::operator<<( std::ostream& out, const WPVBaseTypes::PV_TRIGGER& c )
33 {
34  // print it as nice string
35  switch( c )
36  {
38  out << "PV_TRIGGER_TRIGGERED";
39  break;
40  default:
41  out << "PV_TRIGGER_READY";
42  }
43 
44  return out;
45 }
46 
47 std::istream& WPVBaseTypes::operator>>( std::istream& in, WPVBaseTypes::PV_TRIGGER& c )
48 {
49  std::string s;
50  in >> s;
51 
52  // interpret string
53  if( s == "PV_TRIGGER_TRIGGERED" )
54  {
56  }
57  else
58  {
59  c = PV_TRIGGER_READY;
60  }
61 
62  return in;
63 }
64 
65 bool WPVBaseTypes::isPropertyGroup( PROPERTY_TYPE type )
66 {
67  switch( type )
68  {
69  case PV_GROUP:
70  case PV_STRUCT:
71  return true;
72  default:
73  return false;
74  }
75 }
76 
78 {
80  const WPVBaseTypes::PV_TRANSFERFUNCTION& /*old*/, const std::string str )
81  {
83  std::vector< std::string > tokens;
84  tokens = string_utils::tokenize( str, ";" );
85  //WAssert( tokens.size() >= 16, "There weren't 16 values for a 4x4 Matrix" );
86  size_t idx = 0;
87  while( idx < tokens.size() )
88  {
89  std::vector< std::string > innerTokens;
90  innerTokens = string_utils::tokenize( tokens[ idx ], ":" );
91  // evaluate inner tokens
92  {
93  if( innerTokens[ 0 ] == "c" )
94  {
95  tf.addColor( string_utils::fromString< double >( innerTokens[ 1 ].c_str() ), // isovalue
96  WColor( string_utils::fromString< double >( innerTokens[ 2 ].c_str() ), // red
97  string_utils::fromString< double >( innerTokens[ 3 ].c_str() ), // green
98  string_utils::fromString< double >( innerTokens[ 4 ].c_str() ), // blue
99  1. )
100  ); // blue
101  }
102  else if( innerTokens[ 0 ] == "a" )
103  {
104  tf.addAlpha( string_utils::fromString< double >( innerTokens[ 1 ].c_str() ),
105  string_utils::fromString< double >( innerTokens[ 2 ].c_str() ) );
106  }
107  idx++;
108  }
109  }
110  return tf;
111  }
112 
114  {
115  std::ostringstream out;
116  size_t numColors = tf.numColors();
117  for( size_t i = 0; i < numColors; ++i )
118  {
119  double iso = tf.getColorIsovalue( i );
120  WColor c = tf.getColor( i );
121  out << "c:" << iso << ":" << c[ 0 ] << ":" << c[ 1 ] << ":" << c[ 2 ] << ";";
122  }
123  size_t numAlphas = tf.numAlphas();
124  for( size_t i = 0; i < numAlphas; ++i )
125  {
126  double iso = tf.getAlphaIsovalue( i );
127  double alpha = tf.getAlpha( i );
128  out << "a:" << iso << ":" << alpha;
129  if( i != numAlphas-1 )
130  {
131  out << ";";
132  }
133  }
134  return out.str();
135  }
136 }
T create(const T &, const std::string str)
Creates a new instance of the type from a given string.
std::string asString(const T &v)
Creates a string from the specified value.
A class that stores a 1D transfer function which consists of a linear interpolation of alpha and colo...
double getColorIsovalue(size_t i) const
The isovalue of the color at a given index.
size_t numAlphas() const
Get the number of alphas.
double getAlphaIsovalue(size_t i) const
Get the isovalue at a given index in the alpha values.
size_t numColors() const
Get the number of colors.
double getAlpha(size_t i) const
Get alpha at given index.
void addColor(double iso, const WColor &color)
Insert a new color point.
const WColor & getColor(size_t i) const
Get color at given index.
void addAlpha(double iso, double alpha)
Insert a new alpha point.
This namespace contains several helper classes which translate their template type to an enum.
PV_TRIGGER
Enum denoting the possible trigger states.
@ PV_TRIGGER_TRIGGERED
Trigger property: got triggered.
@ PV_TRIGGER_READY
Trigger property: is ready to be triggered (again)
bool isPropertyGroup(PROPERTY_TYPE type)
Checks which property types are derived from WPropertyGroupBase.
std::ostream & operator<<(std::ostream &out, const PV_TRIGGER &c)
Write a PV_TRIGGER in string representation to the given output stream.
std::istream & operator>>(std::istream &in, PV_TRIGGER &c)
Write a PV_TRIGGER in string representation to the given input stream.
std::vector< std::string > tokenize(const std::string &source, const std::string &delim=WHITESPACE, bool compress=true)
Splits the given string into a vector of strings (so called tokens).