OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEUtils.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 <algorithm>
26 #include <vector>
27 
28 #include <osg/Array>
29 
30 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
31 
32 #include "WGETexture.h"
33 
34 #include "WGEUtils.h"
35 
36 osg::ref_ptr< osg::Vec3Array > wge::osgVec3Array( const std::vector< WPosition >& posArray )
37 {
38  osg::ref_ptr< osg::Vec3Array > result = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
39  result->reserve( posArray.size() );
40  std::vector< WPosition >::const_iterator cit;
41  for( cit = posArray.begin(); cit != posArray.end(); ++cit )
42  {
43  result->push_back( *cit );
44  }
45  return result;
46 }
47 
48 osg::Vec3 wge::unprojectFromScreen( const osg::Vec3 screen, osg::ref_ptr< osg::Camera > camera )
49 {
50  return screen * osg::Matrix::inverse( camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix() );
51 }
52 
53 WColor wge::createColorFromIndex( int index )
54 {
55  float r = 0.0;
56  float g = 0.0;
57  float b = 0.0;
58  float mult = 1.0;
59 
60  if( index == 0 )
61  {
62  return WColor( 0.0, 0.0, 0.0, 1.0 );
63  }
64 
65  if( ( index & 1 ) == 1 )
66  {
67  b = 1.0;
68  }
69  if( ( index & 2 ) == 2 )
70  {
71  g = 1.0;
72  }
73  if( ( index & 4 ) == 4 )
74  {
75  r = 1.0;
76  }
77  if( ( index & 8 ) == 8 )
78  {
79  mult -= 0.15;
80  if( r < 1.0 && g < 1.0 && b < 1.0 )
81  {
82  r = 1.0;
83  g = 1.0;
84  }
85  }
86  if( ( index & 16 ) == 16 )
87  {
88  mult -= 0.15;
89  if( r < 1.0 && g < 1.0 && b < 1.0 )
90  {
91  b = 1.0;
92  g = 1.0;
93  }
94  }
95  if( ( index & 32 ) == 32 )
96  {
97  mult -= 0.15;
98  if( r < 1.0 && g < 1.0 && b < 1.0 )
99  {
100  r = 1.0;
101  b = 1.0;
102  }
103  }
104  if( ( index & 64 ) == 64 )
105  {
106  mult -= 0.15;
107  if( r < 1.0 && g < 1.0 && b < 1.0 )
108  {
109  g = 1.0;
110  }
111  }
112  if( ( index & 128 ) == 128 )
113  {
114  mult -= 0.15;
115  if( r < 1.0 && g < 1.0 && b < 1.0 )
116  {
117  r = 1.0;
118  }
119  }
120  r *= mult;
121  g *= mult;
122  b *= mult;
123 
124  return WColor( r, g, b, 1.0 );
125 }
126 
127 WColor wge::createColorFromHSV( int h, float s, float v )
128 {
129  h = h % 360;
130 
131  int hi = h / 60;
132  float f = ( static_cast<float>( h ) / 60.0 ) - hi;
133 
134  float p = v * ( 1.0 - s );
135  float q = v * ( 1.0 - s * f );
136  float t = v * ( 1.0 - s * ( 1.0 - f ) );
137 
138  switch ( hi )
139  {
140  case 0:
141  return WColor( v, t, p, 1.0 );
142  case 1:
143  return WColor( q, v, p, 1.0 );
144  case 2:
145  return WColor( p, v, t, 1.0 );
146  case 3:
147  return WColor( p, q, v, 1.0 );
148  case 4:
149  return WColor( t, p, v, 1.0 );
150  case 5:
151  return WColor( v, p, q, 1.0 );
152  case 6:
153  return WColor( v, t, p, 1.0 );
154  default:
155  return WColor( v, t, p, 1.0 );
156  }
157 }
158 
159 WColor wge::getNthHSVColor( int n )
160 {
161  int h = 0;
162  float s = 1.0;
163  float v = 1.0;
164 
165  if( ( n & 1 ) == 1 )
166  {
167  h += 180;
168  }
169  if( ( n & 2 ) == 2 )
170  {
171  h += 90;
172  }
173  if( ( n & 4 ) == 4 )
174  {
175  h += 45;
176  }
177  if( ( n & 8 ) == 8 )
178  {
179  h += 202;
180  h = h % 360;
181  }
182  if( ( n & 16 ) == 16 )
183  {
184  v -= .25;
185  }
186  if( ( n & 32 ) == 32 )
187  {
188  s -= .25;
189  }
190  if( ( n & 64 ) == 64 )
191  {
192  v -= .25;
193  }
194  if( ( n & 128 ) == 128 )
195  {
196  s -= 0.25;
197  }
198  if( ( n & 256 ) == 256 )
199  {
200  v -= 0.25;
201  }
202 
203  return createColorFromHSV( h, s, v );
204 }
205