Nori

include/nori/color.h

Go to the documentation of this file.
00001 /*
00002     This file is part of Nori, a simple educational ray tracer
00003 
00004     Copyright (c) 2012 by Wenzel Jakob and Steve Marschner.
00005 
00006     Nori is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License Version 3
00008     as published by the Free Software Foundation.
00009 
00010     Nori is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 #if !defined(__COLOR_H)
00020 #define __COLOR_H
00021 
00022 #include <nori/common.h>
00023 
00024 NORI_NAMESPACE_BEGIN
00025 
00026 /**
00027  * \brief Represents a linear RGB color value
00028  */
00029 struct Color3f : public Eigen::Array3f {
00030 public:
00031         typedef Eigen::Array3f Base;
00032 
00033         /// Initialize the color vector with a uniform value
00034         inline Color3f(float value = 0) : Base(value, value, value) { }
00035 
00036         /// Initialize the color vector with specific per-channel values
00037         inline Color3f(float r, float g, float b) : Base(r, g, b) { }
00038 
00039         /// Construct a color vector from ArrayBase (needed to play nice with Eigen)
00040         template <typename Derived> inline Color3f(const Eigen::ArrayBase<Derived>& p) 
00041                 : Base(p) { }
00042 
00043         /// Assign a color vector from ArrayBase (needed to play nice with Eigen)
00044     template <typename Derived> Color3f &operator=(const Eigen::ArrayBase<Derived>& p) {
00045                 this->Base::operator=(p);
00046                 return *this;
00047     }
00048 
00049         /// Return a reference to the red channel
00050         inline float &r() { return x(); }
00051         /// Return a reference to the red channel (const version)
00052         inline const float &r() const { return x(); }
00053         /// Return a reference to the green channel
00054         inline float &g() { return y(); }
00055         /// Return a reference to the green channel (const version)
00056         inline const float &g() const { return y(); }
00057         /// Return a reference to the blue channel
00058         inline float &b() { return z(); }
00059         /// Return a reference to the blue channel (const version)
00060         inline const float &b() const { return z(); }
00061 
00062         /// Check if the color vector contains a NaN/Inf/negative value
00063         bool isValid() const;
00064 
00065         /// Convert from sRGB to linear RGB
00066         Color3f toLinearRGB() const;
00067 
00068         /// Convert from linear RGB to sRGB
00069         Color3f toSRGB() const;
00070 
00071         /// Return the associated luminance
00072         float getLuminance() const;
00073 
00074         /// Return a human-readable string summary
00075         inline QString toString() const {
00076                 return QString("[%1, %2, %3]").arg(coeff(0)).arg(coeff(1)).arg(coeff(2));
00077         }
00078 };
00079 
00080 /**
00081  * \brief Represents a linear RGB color and a weight
00082  *
00083  * This is used by Nori's image reconstruction filter code
00084  */
00085 struct Color4f : public Eigen::Array4f {
00086 public:
00087         typedef Eigen::Array4f Base;
00088 
00089         /// Create an zero value
00090         inline Color4f() : Base(0.0f, 0.0f, 0.0f, 0.0f) { }
00091 
00092         /// Create from a 3-channel color
00093         inline Color4f(const Color3f &c) : Base(c.r(), c.g(), c.b(), 1.0f) { }
00094 
00095         /// Initialize the color vector with specific per-channel values
00096         inline Color4f(float r, float g, float b, float w) : Base(r, g, b, w) { }
00097 
00098         /// Construct a color vector from ArrayBase (needed to play nice with Eigen)
00099         template <typename Derived> inline Color4f(const Eigen::ArrayBase<Derived>& p) 
00100                 : Base(p) { }
00101 
00102         /// Assign a color vector from ArrayBase (needed to play nice with Eigen)
00103     template <typename Derived> Color4f &operator=(const Eigen::ArrayBase<Derived>& p) {
00104                 this->Base::operator=(p);
00105                 return *this;
00106     }
00107 
00108         /// Normalize and convert into a \ref Color3f value 
00109         inline Color3f normalized() const {
00110                 if (EXPECT_TAKEN(w() != 0))
00111                         return head<3>() / w();
00112                 else
00113                         return Color3f(0.0f);
00114         }
00115 
00116         /// Return a human-readable string summary
00117         inline QString toString() const {
00118                 return QString("[%1, %2, %3, %4]").arg(coeff(0)).arg(coeff(1)).arg(coeff(2)).arg(coeff(4));
00119         }
00120 };
00121 
00122 NORI_NAMESPACE_END
00123 
00124 #endif /* __COLOR_H */
00125 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines