vtf-logo

Coords.h

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

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