SpectralSampleSet.h
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @class SpectralSampleSet 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @author Matthew Jacobs (jacobs.mh@gmail.com) 00005 @date Fall 2008 00006 00007 @brief 00008 Templated set of spectral values (wavelength-dependeht), sampled at N 00009 distinct wavelengths. Specific SpectralSampleSet instances include 00010 SpectralRadianceSet, in which each sample represents the radiance at a single 00011 wavelength with units Watts / (m^2 sr), and SpectralScalarSet, where each 00012 wavelength-dependent sample represents a unitless scalar that can be used 00013 to attenuate SpectralRadianceSet. 00014 00015 @note 00016 Throughout Milton, currently only RGB spectra are used, but this 00017 abstraction is in place and used ubiquitously s.t. we can eventually come 00018 back and implement spectrally-aware rendering with the majority of the 00019 changes taking place in this one class. 00020 <!-------------------------------------------------------------------->**/ 00021 00022 #ifndef SPECTRAL_SAMPLE_SET_H_ 00023 #define SPECTRAL_SAMPLE_SET_H_ 00024 00025 #include <common/common.h> 00026 #include <common/image/Rgba.h> 00027 00028 #include <ostream> 00029 #include <limits> 00030 00031 #define NO_DEFAULT_WAVELENGTHS 3 00032 00033 struct SpectralSample { 00034 /// point-sampled, wavelength-dependent value, with generic units that are 00035 /// dependent on the user's interpretation 00036 real_t value; 00037 00038 /// wavelength of this spectral sample in nm 00039 /// note: we're restricting simulation to integer wavelengths to allow 00040 /// array indexing by wavelength (for efficiency reasons) 00041 unsigned wavelength; 00042 00043 inline SpectralSample(real_t value_, unsigned wavelength_) 00044 : value(value_), wavelength(wavelength_) 00045 { } 00046 00047 inline SpectralSample(const SpectralSample &r) 00048 : value(r.value), wavelength(r.wavelength) 00049 { } 00050 00051 inline SpectralSample() 00052 { } 00053 00054 inline bool operator==(const SpectralSample &r) const { 00055 return (wavelength == r.wavelength && EQ(value, r.value)); 00056 } 00057 00058 inline bool operator!=(const SpectralSample &r) const { 00059 return (wavelength != r.wavelength || NEQ(value, r.value)); 00060 } 00061 00062 inline operator real_t() const { 00063 return value; 00064 } 00065 }; 00066 00067 class SpectralSampleSet { 00068 public: 00069 ///@name Constructors 00070 //@{----------------------------------------------------------------- 00071 00072 /// Constructs a SpectralSampleSet sampled at the three canonical RGB 00073 /// wavelengths, with the sample values specified in the given 3- 00074 /// element array 00075 inline explicit SpectralSampleSet(const real_t *data); 00076 00077 /// Constructs a SpectralSampleSet sampled at the three canonical RGB 00078 /// wavelengths, with zero values 00079 inline explicit SpectralSampleSet(); 00080 00081 /// Constructs a SpectralSampleSet sampled at the three canonical RGB 00082 /// wavelengths, with the sample values given 00083 inline explicit SpectralSampleSet(const real_t &r, const real_t &g, 00084 const real_t &b); 00085 00086 /// Constructs a SpectralSampleSet sampled at the three canonical RGB 00087 /// wavelengths, with the sample values specified in the given 3- 00088 /// element vector 00089 inline SpectralSampleSet(const Vector3 &v); 00090 00091 /// Constructs a SpectralSampleSet sampled at the three canonical RGB 00092 /// wavelengths, with the sample values specified by the given RgbaHDR 00093 inline SpectralSampleSet(const RgbaHDR &rgba); 00094 00095 /// Copy Constructor 00096 inline SpectralSampleSet(const SpectralSampleSet &v); 00097 00098 ~SpectralSampleSet(); 00099 00100 00101 //@}----------------------------------------------------------------- 00102 ///@name Static convenience constructors to generate common spectra 00103 //@{----------------------------------------------------------------- 00104 00105 /// Generates a SpectralSampleSet sampled at the three canonical RGB 00106 /// wavelengths, filled with zeros 00107 static inline SpectralSampleSet black() { 00108 return SpectralSampleSet(); 00109 } 00110 00111 /// Generates a SpectralSampleSet sampled at the three canonical RGB 00112 /// wavelengths, filled with ones (multiplicative identity) 00113 static inline SpectralSampleSet identity() { 00114 return SpectralSampleSet::fill(1); 00115 } 00116 00117 /// Generates a SpectralSampleSet sampled at the three canonical RGB 00118 /// wavelengths, filled with the specified value 00119 static inline SpectralSampleSet fill(const real_t &value) { 00120 SpectralSampleSet ret; 00121 00122 for(unsigned i = NO_DEFAULT_WAVELENGTHS; i--;) 00123 ret[i].value = value; 00124 00125 return ret; 00126 } 00127 00128 00129 //@}----------------------------------------------------------------- 00130 ///@name Accessor Operators 00131 //@{----------------------------------------------------------------- 00132 00133 /// @returns a reference to the element at the given index 00134 inline const SpectralSample &operator[](const unsigned index) const; 00135 00136 /// @returns a reference to the element at the given index 00137 /// @note changes to the returned element will affect this spectrum 00138 inline SpectralSample &operator[](const unsigned index); 00139 00140 /// @returns the number of spectral samples contained in this set 00141 inline unsigned getN() const; 00142 00143 00144 //@}----------------------------------------------------------------- 00145 ///@name Equality Operators 00146 //@{----------------------------------------------------------------- 00147 00148 inline bool operator==(const SpectralSampleSet &v) const; 00149 inline bool operator!=(const SpectralSampleSet &v) const; 00150 00151 00152 //@}----------------------------------------------------------------- 00153 ///@name Relational Operators 00154 //@{----------------------------------------------------------------- 00155 00156 inline bool operator>=(const SpectralSampleSet &v) const; 00157 inline bool operator<=(const SpectralSampleSet &v) const; 00158 00159 00160 //@}----------------------------------------------------------------- 00161 ///@name Mutator Operators 00162 //@{----------------------------------------------------------------- 00163 00164 inline SpectralSampleSet &operator =(const SpectralSampleSet &v); 00165 inline SpectralSampleSet &operator+=(const SpectralSampleSet &rhs); 00166 inline SpectralSampleSet &operator-=(const SpectralSampleSet &rhs); 00167 00168 00169 //@}----------------------------------------------------------------- 00170 ///@name Scalar Mutator Operators 00171 //@{----------------------------------------------------------------- 00172 00173 inline SpectralSampleSet &operator*=(const real_t &scale); 00174 inline SpectralSampleSet &operator/=(const real_t &scale); 00175 00176 00177 //@}----------------------------------------------------------------- 00178 ///@name Arithmetic Operators 00179 //@{----------------------------------------------------------------- 00180 00181 inline SpectralSampleSet operator+ (const SpectralSampleSet &rhs) const; 00182 inline SpectralSampleSet operator- (const SpectralSampleSet &rhs) const; 00183 00184 /// @returns element-wise multiplication 00185 inline SpectralSampleSet operator* (const SpectralSampleSet &rhs) const; 00186 /// @returns element-wise division 00187 inline SpectralSampleSet operator/ (const SpectralSampleSet &rhs) const; 00188 00189 00190 00191 //@}----------------------------------------------------------------- 00192 ///@name Scalar Arithmetic Operators 00193 //@{----------------------------------------------------------------- 00194 00195 inline SpectralSampleSet operator* (const real_t &scale) const; 00196 inline SpectralSampleSet operator/ (const real_t &scale) const; 00197 00198 00199 //@}----------------------------------------------------------------- 00200 ///@name Misc Functionality 00201 //@{----------------------------------------------------------------- 00202 00203 /// @returns whether or not all samples in this set are zero 00204 inline bool isZero() const; 00205 00206 /// @returns the sum of the components in this SpectralSampleSet 00207 inline real_t getSum() const; 00208 00209 /// @returns the average of the components in this SpectralSampleSet 00210 inline real_t getAverage() const; 00211 00212 /// @returns dimension (0,1,...,N) of maximum value 00213 inline unsigned getMaxSample() const; 00214 00215 /// @returns dimension (0,1,...,N) of minimum value 00216 inline unsigned getMinSample() const; 00217 00218 00219 //@}----------------------------------------------------------------- 00220 ///@name Conversions 00221 //@{----------------------------------------------------------------- 00222 00223 /// @returns this SpectralSampleSet converted to RGB format 00224 inline RgbaHDR getRGB() const; 00225 00226 /// @returns this SpectralSampleSet as a HDR floating-point rgba 00227 /// struct (implicit conversion) 00228 inline operator RgbaHDR() const; 00229 00230 00231 //@}----------------------------------------------------------------- 00232 00233 private: 00234 SpectralSample *m_data; 00235 unsigned m_n; 00236 00237 static unsigned s_defaultWavelengths[3]; 00238 }; 00239 00240 00241 ///@name Extra operators where SpectralSampleSet is on right-hand side 00242 //@{--------------------------------------------------------------------- 00243 00244 /// @returns the @p v, with each spectral sample scaled by @p scale 00245 inline SpectralSampleSet operator* (const real_t &scale, 00246 const SpectralSampleSet &v); 00247 00248 /// Prints a SpectralSampleSet to an output stream 00249 inline std::ostream &operator<<(std::ostream &os, 00250 const SpectralSampleSet &v); 00251 00252 //@}--------------------------------------------------------------------- 00253 00254 /* include inline implementations */ 00255 #include <utils/SpectralSampleSet.inl> 00256 00257 /// spectral radiance with units Watts / (m^2 sr) 00258 typedef SpectralSample SpectralRadiance; 00259 00260 /// set of spectral radiance samples, where each sample represents the radiance 00261 /// at a single wavelength with units Watts / (m^2 sr) 00262 typedef SpectralSampleSet SpectralRadianceSet; 00263 00264 #endif // SPECTRAL_SAMPLE_SET_H_ 00265
Generated on 28 Feb 2009 for Milton by
1.5.6