00001 /* 00002 liban - base function to do usefull things related to simulation and scientific work 00003 Copyright (C) 1999-2005 Stephane Magnenat <stephane at magnenat dot net> 00004 Copyright (c) 2004-2005 Antoine Beyeler <antoine dot beyeler at epfl dot ch> 00005 Copyright (C) 2005 Laboratory of Intelligent Systems, EPFL, Lausanne 00006 See AUTHORS for details 00007 00008 This program is free software. You can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 #ifndef __AN_TYPES_H 00024 #define __AN_TYPES_H 00025 00026 #include <valarray> 00027 #include <cassert> 00028 00055 namespace An 00056 { 00058 00059 struct Color 00060 { 00062 double components[4]; 00063 00065 Color(double r = 0.0, double g = 0.0, double b = 0.0, double a = 1.0) 00066 { 00067 components[0] = r; 00068 components[1] = g; 00069 components[2] = b; 00070 components[3] = a; 00071 } 00072 00074 const double& operator[](size_t i) const { assert((i >= 0) && (i < 4)); return components[i]; } 00076 double& operator[](size_t i) { assert((i >= 0) && (i < 4)); return components[i]; } 00077 00078 // operations with scalar 00080 void operator +=(double d) { for (size_t i=0; i<3; i++) components[i] += d; } 00082 Color operator +(double d) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] + d; return c; } 00083 00085 void operator -=(double d) { for (size_t i=0; i<3; i++) components[i] -= d; } 00087 Color operator -(double d) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] - d; return c; } 00088 00090 void operator *=(double d) { for (size_t i=0; i<3; i++) components[i] *= d; } 00092 Color operator *(double d) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] * d; return c; } 00093 00095 void operator /=(double d) { for (size_t i=0; i<3; i++) components[i] /= d; } 00097 Color operator /(double d) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] / d; return c; } 00098 00099 // operation with another color 00101 void operator +=(const Color &oc) { for (size_t i=0; i<3; i++) components[i] += oc.components[i]; } 00103 Color operator +(const Color &oc) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] + oc.components[i]; return c; } 00104 00106 void operator -=(const Color &oc) { for (size_t i=0; i<3; i++) components[i] -= oc.components[i]; } 00108 Color operator -(const Color &oc) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] - oc.components[i]; return c; } 00109 00111 bool operator ==(const Color &c) const { for (size_t i=0; i<4; i++) if (components[i] != c.components[i]) return false; return true; } 00113 bool operator !=(const Color &c) const { return !(*this == c); } 00115 void threshold(const Color &limit) { for (size_t i=0; i<3; i++) components[i] = components[i] > limit.components[i] ? components[i] : 0; } 00117 double gray() const { return (components[0] + components[1] + components[2]) / 3; } 00118 00120 static const Color black; 00122 static const Color white; 00124 static const Color red; 00126 static const Color green; 00128 static const Color blue; 00129 }; 00130 00132 00133 typedef std::valarray<Color> Texture; 00134 } 00135 00136 #endif