Source: raytracer/colour.h
|
|
|
|
/* *************************************************************************
colour.h - description
-------------------
begin : Wed Oct 15 2002
copyright : (C) 2002 by Micha Riser
email : mriser@gmx.net
************************************************************************* */
/* *************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
************************************************************************* */
#ifndef COLOUR_H
#define COLOUR_H
#include "vector.h"
#include "types.h"
class Colour3;
/**Colour vector including red, green, blue, filter and transmit components.
*@author Micha Riser
*/
class Colour5 {
public: // Public constants
static const char TRANSMIT = 4;
static const char FILTER = 3;
public: // Public attributes
CLR c[5]; // colour RGBFT
public: // Constructors and destructor
/**Constructs new Colour5 given the individual components.
*/
Colour5(CLR r, CLR g, CLR b, CLR f, CLR t) {
c[0]=r; c[1]=g; c[2]=b; c[3]=f; c[4]=t;
}
/**Copy constructor.
*/
Colour5(const CLR& a) {
for(int i=0; i<5; i++) c[i]=a;
}
/**Construct Colour5 from Colour3. Transmit and filter component is
*set to 0.
*/
Colour5(const Colour3 a);
private: // Private constructor
Colour5() {};
public: // Public methods
/**Add Colour5.
*@param d colour to add
*/
void add(const Colour5 &d) {for(int i=0; i<5; i++) c[i] += d.c[i];}
/**Subtract Colour5.
*@param d colour to subtract
*/
void sub(const Colour5 &d) {for(int i=0; i<5; i++) c[i] -= d.c[i];}
/**Scale colour by float.
*@param s scaling coifficient
*/
void scale(CLR s) {for(int i=0; i<5; i++) c[i] *= s;}
/**Scale colour componentwise.
*@param d Colour5 holding the scaling coifficients
*/
void scale(const Colour5 d) {for(int i=0; i<5; i++) c[i] *= d.c[i];}
/**Scale rgb components componentwise
*@param d Colour3 holding the scaling coifficients
*/
void scale(const Colour3 d);
/**
*@returns colour scaled by factor
*@param s scaling factor
*/
Colour5 scaled(CLR s) const {Colour5 ret; for(int i=0; i<5; i++) ret.c[i]=c[i]*s; return ret;}
/**Colour norm.
*@returns a (unspecified) norm of the colour
*/
CLR norm() const {
// calculate maximum (infinity) norm
CLR ret = c[0];
for(int i=1; i<3; i++) if (c[i]>ret) ret = c[i];
return ret;
}
};
/**Colour vector including red, green and blue component.
*@author Micha Riser
*/
class Colour3 {
public: // Public constants
/**Use this constant if you do a loop over the rgb elements*/
static const char RGB = 3;
private: // Private constants
static const char RGB_LOOP = RGB;
public: // Public members
/** colour <Red, Green, Blue> */
CLR c[RGB];
public: // Constructors and destructor
/**Construct colour by individual components
*/
Colour3(CLR r, CLR g, CLR b) {c[0]=r; c[1]=g; c[2]=b;}
/**Construct gray-value colour.
*@param a value for all the rgb components
*/
Colour3(const CLR& a) {for(int i=0; i<RGB_LOOP; i++) c[i]=a;}
/**Construct rgb colour from Colour5. Filter and transmit value are
*ignored
*/
Colour3(const Colour5& a) {for(int i=0; i<RGB_LOOP; i++) c[i]=a.c[i];}
private:
/**Construct uninitialized colour.
*/
Colour3() {};
public: // Public methods
/**Add colour.
*@param d colour to add
*/
void add(const Colour3 &d) {for(int i=0; i<RGB_LOOP; i++) c[i] += d.c[i];}
/**Add colour.
*@param d colour to add
*/
void add(const Colour5 &d) {for(int i=0; i<RGB_LOOP; i++) c[i] += d.c[i];}
/**Subtract colour.
*@param d colour to subtract
*/
void sub(const Colour3 &d) {for(int i=0; i<RGB_LOOP; i++) c[i] -= d.c[i];}
/**Scale colour.
*@param s scaling factor
*/
void scale(CLR s) {for(int i=0; i<RGB_LOOP; i++) c[i] *= s;}
/**Scale colour componentwise.
*@param d scaling factors
*/
void scale(const Colour5& d) {for(int i=0; i<RGB_LOOP; i++) c[i] *= d.c[i];}
/**Scale colour componentwise.
*@param d scaling factors
*/
void scale(const Colour3& d) {for(int i=0; i<RGB_LOOP; i++) c[i] *= d.c[i];}
/**Calculate e^x for each colour components x.
*/
void exp() {for(int i=0; i<RGB_LOOP; i++) c[i] = std::exp(c[i]);}
/**Calculate e^(-x) for each colour components x.
*/
void negExp() {for(int i=0; i<RGB_LOOP; i++) c[i] = std::exp(-c[i]);}
/**
*@returns colour scaled by factor
*@param s scaling factor
*/
Colour3 scaled(CLR s) const {Colour3 ret; for(int i=0; i<RGB_LOOP; i++) ret.c[i]=c[i]*s; return ret;}
/**
*@returns componentwise scaled colour
*@param d scaling factors
*/
Colour3 scaled(const Colour3 &d) const {
Colour3 ret;
for(int i=0; i<RGB_LOOP; i++) ret.c[i] = c[i]*d.c[i];
return ret;
}
/**
*@returns componentwise scaled colour
*@param d scaling factors
*/
Colour3 scaled(const Colour5 &d) const {
Colour3 ret;
for(int i=0; i<RGB_LOOP; i++) ret.c[i] = c[i]*d.c[i];
return ret;
}
/**Colour norm
*@returns a (unspecified) norm of the colour components
*/
CLR norm() const {
CLR ret = fabs(c[0]);
for(int i=1; i<RGB_LOOP; i++) if (fabs(c[i])>ret) ret = fabs(c[i]);
return ret;
}
/**Minimum.
*@returns the minimal element of the colour vector
*/
CLR minElement() const {
CLR ret = c[0];
for(int i=1; i<RGB_LOOP; i++) if (c[1]<ret) ret = c[1];
return ret;
}
/**Maximum.
*@returns the maximal element of the colour vector
*/
CLR maxElement() const {
CLR ret = c[0];
for(int i=1; i<RGB_LOOP; i++) if (c[1]>ret) ret = c[1];
return ret;
}
public: // Static methods
/**Adds two colours (componentwise).
*/
static Colour3 add(const Colour3& c1, const Colour3& c2) {
Colour3 ret;
for(int i=0; i<RGB_LOOP; i++) ret.c[i]=c1.c[i]+c2.c[i];
return ret;
}
/**Subtracts colour c2 from colour c1 (componentwise).
*/
static Colour3 sub(const Colour3& c1, const Colour3& c2) {
Colour3 ret;
for(int i=0; i<RGB_LOOP; i++) ret.c[i]=c1.c[i]-c2.c[i];
return ret;
}
/**Calcluate the square root of a colour (componentwise).
*/
static Colour3 sqrt(const Colour3& d) {
Colour3 ret;
for(int i=0; i<RGB_LOOP; i++) ret.c[i]=std::sqrt(d.c[i]);
return ret;
}
/**Calculate e^x for each colour component.
*/
static Colour3 exp(const Colour3& d) {
Colour3 ret;
for(int i=0; i<RGB_LOOP; i++) ret.c[i]=std::exp(d.c[i]);
return ret;
}
};
#endif
| Generated by: micha@laptop on Thu Oct 24 20:25:24 2002, using kdoc 2.0a53. |