H3DTypes.h

00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00024 //
00026 
00027 #ifndef __H3DTYPES_H__
00028 #define __H3DTYPES_H__
00029 
00030 #include "Types/H3DBasicTypes.h"
00031 #include "Types/H3DTypeOperators.h"
00032 
00033 namespace H3D {
00034 
00035   // move the Arithmetic types into the H3D namespace.
00036   using ArithmeticTypes::Vec2f;
00037   using ArithmeticTypes::Vec2d;
00038   using ArithmeticTypes::Vec3f;
00039   using ArithmeticTypes::Vec3d;
00040   using ArithmeticTypes::Vec4f;
00041   using ArithmeticTypes::Vec4d;
00042   using ArithmeticTypes::Rotation;
00043   using ArithmeticTypes::Matrix3f;
00044   using ArithmeticTypes::Matrix3d;
00045   using ArithmeticTypes::Matrix4f;
00046   using ArithmeticTypes::Matrix4d;
00047   using ArithmeticTypes::Quaternion;
00048 #undef RGB
00052 
00053 
00054   struct RGB {
00056     inline RGB():
00057       r( (H3DFloat) 0 ),
00058       g( (H3DFloat) 0 ),
00059       b( (H3DFloat) 0 ) {}
00060 
00062     inline RGB( H3DFloat _r,
00063                 H3DFloat _g,
00064                 H3DFloat _b ): r(_r), g(_g), b(_b) {}
00065 
00067     inline Vec3f toHSV() const {
00068       int i;    
00069       H3DFloat v, x, f;         
00070       x = H3DMin(r, H3DMin( g, b ) );   
00071       v = H3DMax(r, H3DMax( g, b ) );   
00072       if(v == x) return Vec3f(0, 0, v);         
00073       f = (r == x) ? g - b : ((g == x) ? b - r : r - g);        
00074       i = (r == x) ? 3 : ((g == x) ? 5 : 1);    
00075       return Vec3f(i - f /(v - x), (v - x)/v, v);
00076     }
00077 
00079     inline void fromHSV( const Vec3f &hsv ) {
00080       H3DFloat h = hsv.x, s = hsv.y, v = hsv.z, m, n, f;        
00081       int i;    
00082       if( h == 0.0 ) r = g = b = v;
00083       i = (int) H3DFloor(h);    
00084       f = h - i;        
00085       if(!(i & 1)) f = 1 - f; // if i is even   
00086       m = v * (1 - s);  
00087       n = v * (1 - s * f);      
00088       switch (i) {      
00089       case 6:   
00090       case 0: r = v; g = n; b = m; break;
00091       case 1: r = n; g = v; b = m; break;
00092       case 2: r = m; g = v; b = n; break;
00093       case 3: r = m; g = n; b = v; break;
00094       case 4: r = n; g = m; b = v; break;
00095       case 5: r = v; g = m; b = n; break;
00096         // should never happen
00097       default: r = g = b = 0;
00098       }
00099     }
00100 
00101     H3DFloat r, g, b;
00102   }; 
00103     
00105   struct RGBA {
00107     inline RGBA():
00108       r( (H3DFloat) 0 ),
00109       g( (H3DFloat) 0 ),
00110       b( (H3DFloat) 0 ),
00111       a( (H3DFloat) 0 ) {}
00112 
00114     RGBA( H3DFloat _r,
00115           H3DFloat _g,
00116           H3DFloat _b,
00117           H3DFloat _a ) : r(_r), g(_g), b(_b), a(_a) {}
00118     H3DFloat r, g, b, a;
00119   };
00120 
00122   inline ostream& operator<<( ostream &os, const RGB &r ) {
00123     os << r.r << " " << r.g << " " << r.b;
00124     return os;
00125   }
00126   
00128   inline ostream& operator<<( ostream &os, const RGBA &r ) {
00129     os << r.r << " " << r.g << " " << r.b << " " << r.a;
00130     return os;
00131   }
00132 
00134   inline bool operator==( const RGB &r1, const RGB &r2 ) {
00135     return r1.r == r2.r && r1.g == r2.g && r1.b == r2.b;
00136   }
00137 
00139   inline bool operator==( const RGBA &r1, const RGBA &r2 ) {
00140     return r1.r == r2.r && r1.g == r2.g && r1.b == r2.b && r1.a == r2.a;
00141   }
00142 
00144   inline bool operator!=( const RGB &r1, const RGB &r2 ) {
00145     return !(r1==r2);
00146   }
00147 
00149   inline bool operator!=( const RGBA &r1, const RGBA &r2 ) {
00150     return !(r1==r2);
00151   }
00152 
00154 }
00155 
00156 #endif

Generated on Thu Aug 24 12:38:33 2006 for H3D API by  doxygen 1.4.5