Event.h

Go to the documentation of this file.
00001 /**<!-------------------------------------------------------------------->
00002    @class  Event
00003    @author Travis Fischer (fisch0920@gmail.com)
00004    @author Matthew Jacobs (jacobs.mh@gmail.com)
00005    @date   Fall 2008
00006    
00007    @brief
00008       Represents a single event which was sampled from a Sampler. The 
00009    internal value of this event is stored as a variant using boost::any, 
00010    where the concrete value of type T can be extracted using 
00011    Event<T>::getValue()
00012    
00013       This makes it esay to have all Samplers conform to the same sampling 
00014    interface, which facilitates the implementation of more abstract sampling 
00015    algorithms without respect to specific Sampler implementations.  One 
00016    example of this generic Sampling interface being useful is generic 
00017    multiple importance sampling (see MultipleImportanceSampler)
00018    <!-------------------------------------------------------------------->**/
00019 
00020 #ifndef EVENT_H_
00021 #define EVENT_H_
00022 
00023 #include <common/common.h>
00024 #include <common/math/Vector.h>
00025 #include <utils/SpectralSampleSet.h>
00026 #include <boost/any.hpp>
00027 
00028 class Sampler;
00029 
00030 class Event {
00031    
00032    public:
00033       ///@name Constructors
00034       //@{-----------------------------------------------------------------
00035       
00036       /**
00037        * @param x is the value of this random event
00038        * @param randomVar is the sampler from which this random event was 
00039        *    generated
00040        * @param metadata is optional sampler-specific metadata associated 
00041        *    with this event
00042        * @param p is the probability with which this event occurred
00043        */
00044       inline Event(const boost::any &x, Sampler *randomVar = NULL, 
00045                    const boost::any &metadata = boost::any(), 
00046                    real_t p = -1)
00047          : m_x(x), m_metadata(metadata), m_sampler(randomVar), m_p(p)
00048       { }
00049       
00050       inline Event(const boost::any &x, const Event &event)
00051          : m_x(x), m_metadata(event.m_metadata), m_sampler(event.m_sampler), 
00052            m_p(-1)
00053       { }
00054       
00055       inline Event(Sampler *randomVar = NULL, 
00056                    const boost::any &metadata = boost::any())
00057          : m_x(), m_metadata(metadata), m_sampler(randomVar), m_p(-1)
00058       { }
00059       
00060       virtual ~Event()
00061       { }
00062       
00063       
00064       //@}-----------------------------------------------------------------
00065       ///@name Main usage interface
00066       //@{-----------------------------------------------------------------
00067       
00068       template<typename T>
00069       inline T getValue() const {
00070          ASSERT(hasValue());
00071          
00072          return boost::any_cast<T>(m_x);
00073       }
00074       
00075       template<typename T>
00076       inline T getMetadata() const {
00077          ASSERT(hasMetadata());
00078          
00079          return boost::any_cast<T>(m_metadata);
00080       }
00081       
00082       /**
00083        * @returns a cache of the probability with which this event occurred
00084        *    (initializes the cache of it hasn't been computed yet)
00085        */
00086       real_t getPdf();
00087       
00088       
00089       //@}-----------------------------------------------------------------
00090       ///@name Accessors/Mutators
00091       //@{-----------------------------------------------------------------
00092       
00093       inline boost::any getValue() const {
00094          return m_x;
00095       }
00096       
00097       inline void setValue(const boost::any &x) {
00098          m_x = x;
00099       }
00100       
00101       inline bool hasValue() const {
00102          return (!m_x.empty());
00103       }
00104       
00105       inline Sampler *getParent() {
00106          return m_sampler;
00107       }
00108       
00109       inline void setParent(Sampler *randomVar) {
00110          m_sampler = randomVar;
00111       }
00112       
00113       inline boost::any getMetadata() const {
00114          return m_metadata;
00115       }
00116       
00117       inline void setMetadata(const boost::any &metadata) {
00118          m_metadata = metadata;
00119       }
00120       
00121       inline bool hasMetadata() const {
00122          return (!m_metadata.empty());
00123       }
00124       
00125       
00126       //@}-----------------------------------------------------------------
00127       ///@name Convenience implicit conversions for common Event types
00128       //@{-----------------------------------------------------------------
00129       
00130       /// @returns this Event as a real_t (implicit conversion) for 
00131       ///    convenience purposes
00132       inline operator real_t() const {
00133          return getValue<real_t>();
00134       }
00135       
00136       /// @returns this Event as an int (implicit conversion) for 
00137       ///    convenience purposes
00138       inline operator int() const {
00139          return getValue<int>();
00140       }
00141       
00142       /// @returns this Event as a Vector3 (implicit conversion) for 
00143       ///    convenience purposes
00144       inline operator Vector3() const {
00145          return getValue<Vector3>();
00146       }
00147       
00148       /// @returns this Event as a SpectralSampleSet (implicit conversion) for 
00149       ///    convenience purposes
00150       inline operator SpectralSampleSet() const {
00151          return getValue<SpectralSampleSet>();
00152       }
00153       
00154       //@}-----------------------------------------------------------------
00155       
00156    protected:
00157       /// internal value variant
00158       boost::any m_x;
00159       
00160       /// internal sampler-specific metadata associated with this event
00161       /// note: may store information about how this event was generated 
00162       /// for determining the probability of the event
00163       boost::any m_metadata;
00164       
00165       /// parent random distribution from which this event was sampled
00166       Sampler   *m_sampler;
00167       
00168       /// probability with which this event occurred with respect to parent 
00169       /// sampling distribution
00170       real_t     m_p;
00171 };
00172 
00173 #endif // EVENT_H_
00174 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6