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

Range.h

Go to the documentation of this file.
00001 
00002 //
00003 // $Id: Range.h,v 1.15 2005/07/21 20:26:36 bv Exp $
00004 //
00005 // Range
00006 //
00007 // Package: MIDAD
00008 //
00009 // Range: models a (templated) range (min/max) of values.  Triggers
00010 // "modified" signal when changed.
00011 //
00012 // RangePtr: ref counted handle used to share ranges.
00013 //
00014 // Contact: bv@bnl.gov
00015 //
00016 // Created on: Mon Nov  5 13:39:16 2001
00017 //
00019 
00020 #ifndef MIDAD_RANGE_H
00021 #define MIDAD_RANGE_H
00022 
00023 // include TMath.h so that it (and it's overloaded function Range)
00024 // is seen before the one below, lest the compiler get confused.
00025 #include "TMath.h"
00026 
00027 #include <Midad/Util/Signals.h>
00028 
00029 #include <string>
00030 
00031 template <class TYPE>
00032 class Range : public SigC::Object
00033 {
00034 public:
00035 
00036     struct RangeState {
00037         TYPE min, max;
00038         RangeState(TYPE mi, TYPE ma) : min(mi), max(ma) {};
00039         RangeState() : min(-0.0001), max(0.0001) {};
00040         virtual ~RangeState() {};
00041     };
00042     
00043     SigC::Signal0<void> modified;
00044 
00045     Range(TYPE min=0, TYPE max=0, const char* name = 0)
00046         : fState(min,max), fName(name) {}
00047     virtual ~Range() { /*cerr << "~Range()\n";*/}
00048 
00049     void Set(TYPE min, TYPE max) 
00050         { fState.min = min; fState.max = max; this->modified(); }
00051     void SetMin(TYPE min) { fState.min = min; this->modified(); }
00052     void SetMax(TYPE max) { fState.max = max; this->modified(); }
00053     void Get(TYPE& min, TYPE& max) const { min=fState.min, max=fState.max; }
00054     TYPE Min(void) const { return fState.min; }
00055     TYPE Max(void) const { return fState.max; }
00056     bool InRange(TYPE val) { return val >= fState.min && val <= fState.max; }
00057     TYPE Relative(TYPE val) { 
00058         val = this->RelativeUnclipped(val);
00059         if (val < 0) return 0.0;
00060         if (val > 1) return 1.0;
00061         return val;
00062     }
00063     TYPE RelativeUnclipped(TYPE val) { 
00064         return (val - fState.min) / (fState.max - fState.min); 
00065     }
00066     const char* GetName(void) const
00067         { return fName; }
00068     void SetName(const char* name) 
00069         { fName = name; }
00070     void Merge(const Range& r) {
00071         bool dirty = false;
00072         if (r.Min() < fState.min) { fState.min = r.Min(); dirty=true; }
00073         if (r.Max() > fState.max) { fState.max = r.Max(); dirty=true; }
00074         if (dirty) this->modified();
00075     }
00076     void SetFrom(const Range& r) { this->Set(r.Min(),r.Max()); }
00077     void SetState(RangeState rs) { fState = rs; this->modified(); }
00078     RangeState GetState(void) { return fState; }
00079 
00080 private:
00081 
00082     // copy constructor, assignment:
00083     Range(const Range& rhs) { SetFrom(rhs); } // copy constructor
00084     Range& operator=(const Range& rhs) { SetFrom(rhs); return *this; } // assignment
00085 
00086     RangeState fState;
00087     const char* fName;
00088 };                              // end of class Range
00089 
00090 typedef Range<double> RangeDouble;
00091 
00092 template <typename TYPE>
00093 struct Range2D {
00094     SigC::Ptr<Range<TYPE> > x, y;
00095 
00096     Range2D()
00097         : x(SigC::manage(new Range<TYPE>)), 
00098           y(SigC::manage(new Range<TYPE>)) 
00099     {};
00100 
00101     Range2D(TYPE x1, TYPE y1, TYPE x2, TYPE y2)
00102         : x(SigC::manage(new Range<TYPE>(x1,x2))), 
00103           y(SigC::manage(new Range<TYPE>(y1,y2))) 
00104     {};
00105 
00106     Range2D(const Range2D& rhs) 
00107         { this->x = rhs.x; this->y = rhs.y; };
00108     Range2D& operator=(Range2D& rhs) {
00109         if (this == &rhs) return rhs;
00110         this->x = rhs.x; this->y = rhs.y; return *this;
00111     };
00112 
00113 };
00114 
00115 typedef Range2D<double> RangeDouble2D;
00116 typedef Range2D<int> RangeInt2D;
00117 
00118 #endif  // MIDAD_RANGE_H

Generated on Sat Nov 7 01:27:15 2009 for loon by  doxygen 1.3.9.1