vtf-logo

DCoords.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 #ifndef _included_DCoords_h
00004 #define _included_DCoords_h
00005 
00011 #include "DAGHDefaults.h"
00012 #include "lparx_copyright.h"
00013 
00014 #include <iosfwd>
00015 #include <cstdlib>
00016 #include <cassert>
00017 
00018 #ifndef DCoordsMaxDim
00019 #define DCoordsMaxDim   (3)
00020 #endif
00021 
00022 #define DCoordsNULL     ((DCoords *)0)
00023 
00031 class DCoords
00032   {
00033    friend std::istream& operator >> (std::istream& s, DCoords& c);
00034    friend std::ostream& operator << (std::ostream& s, const DCoords& c);
00035 
00036    double c[DCoordsMaxDim];
00037 
00038 public:
00039    int rank;
00040    static class DCoords _empty_dcoords;
00041 
00042 public:
00043    inline DCoords(void) : rank(0) 
00044      { c[0] = 0.0; c[1] = 0.0; c[2] = 0.0; }
00045    inline DCoords(int const r) : rank(r) 
00046      { c[0] = 0.0; c[1] = 0.0; c[2] = 0.0; }
00047    inline DCoords(int const r, double const val) : rank(r)
00048      { c[0] = val; c[1] = val; c[2] = val; }
00049 
00050    inline DCoords(int const r, double const *val) : rank(r)
00051      { register int i; for (i=0; i<rank; i++) c[i] = val[i]; }
00052 
00053    inline DCoords(DCoords const &other) : rank(other.rank)
00054      { c[0] = other.c[0]; c[1] = other.c[1]; c[2] = other.c[2]; }
00055 
00056    /* Specific constructors for 2 & 3 D */
00057    inline DCoords(int const r, double const i, double const j) : rank(r) 
00058      { assert(r == 2); c[0] = i; c[1] = j; }
00059    inline DCoords(int const r, double const i, double const j, double const k) : rank(r) 
00060      { assert(r == 3); c[0] = i; c[1] = j; c[2] = k; }
00061 
00062    inline DCoords &operator = (DCoords const &rhs)
00063      {
00064        rank = rhs.rank;
00065        c[0] = rhs.c[0]; c[1] = rhs.c[1]; c[2] = rhs.c[2];
00066        return *this;
00067      }
00068 
00069    inline ~DCoords() {}
00070 
00071    /* Some simple operators on DCoords -- const and non-const ones */
00072    
00073    inline double& operator () (int const i) { return(c[i]); }
00074    inline double operator () (int const i) const { return(c[i]); }
00075 
00076    inline double * operator () (void) { return(c); }
00077    inline double const *operator () (void) const { return(c); }
00078 
00079    inline operator double * () { return(c); }
00080    inline operator const double * () const { return(c); }
00081 
00082    /* Define all of the operators between two DCoords */
00083 #define Point_Point_Operator(ope,op)                            \
00084    DCoords& operator ope (DCoords const &rhs)                   \
00085      {                                                          \
00086       c[0] ope rhs.c[0];                                        \
00087       if (rank>1) c[1] ope rhs.c[1];                            \
00088       if (rank>2) c[2] ope rhs.c[2];                            \
00089       return(*this);                                            \
00090      }                                                          \
00091    DCoords operator op (DCoords const &rhs) const               \
00092      {                                                          \
00093       DCoords coords(*this);                                    \
00094       coords ope rhs;                                           \
00095       return(coords);                                           \
00096      }
00097    Point_Point_Operator(+=,+)
00098    Point_Point_Operator(-=,-)
00099    Point_Point_Operator(*=,*)
00100    Point_Point_Operator(/=,/)
00101 #undef Point_Point_Operator
00102 
00103    /* Define all of the operators between a DCoords and an integer */
00104 #define Point_Scalar_Operator(ope,op)                           \
00105    DCoords& operator ope (double const rhs)                     \
00106      {                                                          \
00107       c[0] ope rhs;                                             \
00108       if (rank>1) c[1] ope rhs;                                 \
00109       if (rank>2) c[2] ope rhs;                                 \
00110       return(*this);                                            \
00111      }                                                          \
00112    DCoords operator op (double const rhs) const                 \
00113      {                                                          \
00114       DCoords coords(*this);                                    \
00115       coords ope rhs;                                           \
00116       return(coords);                                           \
00117      }
00118    Point_Scalar_Operator(+=,+)
00119    Point_Scalar_Operator(-=,-)
00120    Point_Scalar_Operator(*=,*)
00121    Point_Scalar_Operator(/=,/)
00122 #undef Point_Scalar_Operator
00123 
00124    /* Define the negation operator for a DCoords */
00125    inline DCoords operator - () const
00126      {
00127        DCoords coords(rank);
00128        coords.c[0] = -c[0]; coords.c[1] = -c[1]; coords.c[2] = -c[2];
00129        return(coords);
00130      }
00131 
00132    /* Define the equality and inequality operators for DCoords */
00133    inline int operator != (DCoords const &rhs) const
00134      {
00135       return ( ((rank > 0) && (c[0] != rhs.c[0])) ||
00136                ((rank > 1) && (c[1] != rhs.c[1])) ||
00137                ((rank > 2) && (c[2] != rhs.c[2]))
00138              ) ;
00139      }
00140    inline int operator == (DCoords const &rhs) const
00141      { return(!(*this != rhs)); }
00142 
00143    /* Define the greater-than & less-than operators for DCoords */
00144    inline int operator > (DCoords const &rhs) const
00145      {
00146       return (!(((rank > 0) && (c[0] <= rhs.c[0])) ||
00147                ((rank > 1) && (c[1] <= rhs.c[1])) ||
00148                ((rank > 2) && (c[2] <= rhs.c[2])))
00149              ) ;
00150      }
00151    inline int operator < (DCoords const &rhs) const
00152      {
00153       return (!(((rank > 0) && (c[0] >= rhs.c[0])) ||
00154                ((rank > 1) && (c[1] >= rhs.c[1])) ||
00155                ((rank > 2) && (c[2] >= rhs.c[2])))
00156              ) ;
00157      }
00158 
00159    inline void setval(double const val)
00160      { c[0] = val; c[1] = val; c[2] = val; }
00161    inline void setval(DCoords const &rhs)
00162      { c[0] = rhs.c[0]; c[1] = rhs.c[1]; c[2] = rhs.c[2]; }
00163 
00164    /* Define elementwise minimum and maximum functions for DCoords */
00165    inline void min(DCoords const &rhs)
00166      {
00167        c[0] = (rhs.c[0] < c[0]) ? rhs.c[0] : c[0];
00168        c[1] = (rank > 1 && rhs.c[1] < c[1]) ? rhs.c[1] : c[1];
00169        c[2] = (rank > 2 && rhs.c[2] < c[2]) ? rhs.c[2] : c[2];
00170      }
00171 
00172    inline void max(DCoords const &rhs)
00173      {
00174        c[0] = (rhs.c[0] > c[0]) ? rhs.c[0] : c[0];
00175        c[1] = (rank > 1 && rhs.c[1] > c[1]) ? rhs.c[1] : c[1];
00176        c[2] = (rank > 2 && rhs.c[2] > c[2]) ? rhs.c[2] : c[2];
00177      }
00178 
00179    inline DCoords getmin(DCoords const &rhs) const
00180      { DCoords other(*this); other.min(rhs); return other; }
00181    inline DCoords getmax(DCoords const &rhs) const
00182      { DCoords other(*this); other.max(rhs); return other; }
00183   };
00184 
00185 inline DCoords Max(const DCoords& a, const DCoords &b)
00186        { return(a.getmax(b)); }
00187 inline DCoords Min(const DCoords& a, const DCoords &b)
00188        { return(a.getmin(b)); }
00189 
00190 std::ostream& operator << (std::ostream& s, const DCoords& c);
00191 std::istream& operator >> (std::istream& s, DCoords& c);
00192 
00193 #endif

Generated on Fri Aug 24 13:00:29 2007 for AMROC's Hierachical Data Structures - by  doxygen 1.4.7