Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Representations/Cognition/SampleSet.h

Go to the documentation of this file.

    00001 /**
    00002  * @file SampleSet.h
    00003  *
    00004  * The file contains the definition of the class SampleSet and SampleSetProxy.
    00005  *
    00006  * @author <A href=mailto:roefer@tzi.de>Thomas Röfer</A>
    00007  */
    00008
    00009 #ifndef __SampleSet_h_
    00010 #define __SampleSet_h_
    00011
    00012 #include "Tools/Math/Pose2D.h"
    00013
    00014 /**
    00015 * The class represents a 2D sample.
    00016 */
    00017 class Vector2Sample : public Vector2<double>
    00018 {
    00019   public:
    00020     double probability;
    00021
    00022     /**
    00023     * Constructor.
    00024     */
    00025     Vector2Sample() {probability = 0.5;}
    00026     Vector2Sample(const Vector2<double>& v) : Vector2<double>(v) {probability = 0.5;}
    00027 };
    00028
    00029 /**
    00030 * The class represents a pose sample.
    00031 */
    00032 class PoseSample : public Pose2D
    00033 {
    00034   public:
    00035     double probability;
    00036
    00037     /**
    00038     * Constructor.
    00039     */
    00040     PoseSample() {probability = 0.5;}
    00041     PoseSample(const Pose2D& pose) : Pose2D(pose) {probability = 0.5;}
    00042 };
    00043
    00044 /**
    00045  * @class SampleSetProxyBase
    00046  * The base of class SampleSetProxy.
    00047  */
    00048 class SampleSetProxyBase
    00049 {
    00050   protected:
    00051     const char* data;
    00052     int numberOfSamples,
    00053         sizeOfEntries;
    00054
    00055   public:
    00056     /**
    00057      * Constructor.
    00058      */
    00059     SampleSetProxyBase() {numberOfSamples = 0;}
    00060
    00061     /**
    00062     * The function returns the number of samples in the set.
    00063     * @return The number of samples.
    00064     */
    00065     int getNumberOfSamples() const {return numberOfSamples;}
    00066
    00067     /**
    00068     * The function links the proxy to a sample set.
    00069     */
    00070     void link(const char* start, int number, int size)
    00071     {
    00072       data = (const char*) start;
    00073       numberOfSamples = number;
    00074       sizeOfEntries = size;
    00075     }
    00076 };
    00077
    00078 /**
    00079  * @class SampleSetProxy
    00080  * The SampleSetProxy can be linked to instances of the class SampleSet.
    00081  */
    00082 template<class T> class SampleSetProxy : public SampleSetProxyBase
    00083 {
    00084   public:
    00085     /**
    00086      * Constant access operator.
    00087      * @param index The index of the sample to access.
    00088      */
    00089     const T& operator[](int index) const {return *(const T*) (data + index * sizeOfEntries);}
    00090 };
    00091
    00092 /**
    00093  * @class SampleSet
    00094  * A container for samples. Two independant sets are maintained.
    00095  * As the sample set can be used by different modules that require
    00096  * a different number of samples, the size of the set can be changed
    00097  * at runtime.
    00098  */
    00099 template<class T, int N> class SampleSet
    00100 {
    00101   private:
    00102     T* current, /**< The actual sample set. */
    00103      * other; /**< The secondary sample set. */
    00104
    00105   public:
    00106     /**
    00107      * Constructor.
    00108      */
    00109     SampleSet()
    00110     {
    00111       current = new T[N];
    00112       other = new T[N];
    00113     }
    00114
    00115     /**
    00116      * Destructor.
    00117      */
    00118     ~SampleSet()
    00119     {
    00120       delete [] current;
    00121       delete [] other;
    00122     }
    00123
    00124     /**
    00125     * The function links a proxy to this sample set.
    00126     * @param sampleSetProxy The proxy.
    00127     */
    00128     void link(SampleSetProxyBase& sampleSetProxy) const
    00129     {
    00130       sampleSetProxy.link((const char*) current, N, sizeof(T));
    00131     }
    00132
    00133     /**
    00134     * The function returns the number of samples in the set.
    00135     * @return The number of samples.
    00136     */
    00137     int getNumberOfSamples() const {return N;}
    00138
    00139     /**
    00140      * Access operator.
    00141      * @param index The index of the sample to access.
    00142      */
    00143     T& operator[](int index) {return current[index];}
    00144
    00145     /**
    00146      * Constant access operator.
    00147      * @param index The index of the sample to access.
    00148      */
    00149     const T& operator[](int index) const {return current[index];}
    00150
    00151     /**
    00152      * The function swaps the primary and secondary sample set.
    00153      * @return The address of the previous sample set;
    00154      */
    00155     T* swap()
    00156     {
    00157       T* temp = current;
    00158       current = other;
    00159       other = temp;
    00160       return other;
    00161     }
    00162 };
    00163
    00164 #endif //__SampleSet_h_