libs/ck-libs/datatransfer/mapbasic.h

Go to the documentation of this file.
00001 // $Id: mapbasic.h,v 1.1 2007-06-14 20:55:24 wilmarth Exp $
00002 
00003 #ifndef __MAP_BASIC_H_
00004 #define __MAP_BASIC_H_
00005 
00006 #define MAP_BEGIN_NAMESPACE  namespace MAP {
00007 #define MAP_END_NAMESPACE    }
00008 #define USE_MAP_NAMESPACE    using namespace MAP;
00009 
00010 #include <cstdlib>
00011 #include <iostream>
00012 #include <cmath>
00013 //#include "Element_accessors.h"
00014 
00015 MAP_BEGIN_NAMESPACE
00016 
00017 /*
00018 using COM::Element_node_enumerator;
00019 using COM::Element_node_enumerator_str_2;
00020 using COM::Element_node_enumerator_uns;
00021 using COM::Facet_node_enumerator;
00022 using COM::Element_vectors_k_const;
00023 using COM::Element_vectors_k;
00024 using COM::Element_node_vectors_k_const;
00025 using COM::Element_node_vectors_k;
00026 */
00027 
00028 class Origin {};
00029 class Null_vector {};
00030 
00032 template <class Type>
00033 class Vector_3 { 
00034 public:
00035   Vector_3() {}
00036   explicit Vector_3( Type t) : _x(t), _y(t), _z(t) {}
00037   Vector_3( Null_vector) : _x(0), _y(0), _z(0) {}
00038   Vector_3( Type a, Type b, Type c) : _x(a), _y(b), _z(c) {}
00039   Type &operator[](const int i) { return (&_x)[i]; }
00040   const Type &operator[](const int i) const { return (&_x)[i]; }
00041 
00042   Type x() const { return _x; }
00043   Type y() const { return _y; }
00044   Type z() const { return _z; }
00045 
00047   Vector_3 &operator+=( const Type t)
00048   { _x+=t; _y+=t; _z+=t; return *this; }
00049   Vector_3 &operator+=( const Vector_3 &v)
00050   { _x+=v._x; _y+=v._y; _z+=v._z; return *this; }
00051   Vector_3 &operator-=( const Type t)
00052   { _x-=t; _y-=t; _z-=t; return *this; }
00053   Vector_3 &operator-=( const Vector_3 &v)
00054   { _x-=v._x; _y-=v._y; _z-=v._z; return *this; }
00055   Vector_3 &operator*=( const Type t)
00056   { _x*=t; _y*=t; _z*=t; return *this; }
00057   Vector_3 &operator*=( const Vector_3 &v)
00058   { _x*=v._x; _y*=v._y; _z*=v._z; return *this; }
00059   Vector_3 &operator/=( const Type t)
00060   { _x/=t; _y/=t; _z/=t; return *this; }
00061   Vector_3 &operator/=( const Vector_3 &v)
00062   { _x/=v._x; _y/=v._y; _z/=v._z; return *this; }
00063 
00065   Vector_3 operator+( const Type t) const 
00066   { return Vector_3(_x+t, _y+t, _z+t); }
00067   Vector_3 operator+( const Vector_3 &v) const 
00068   { return Vector_3(_x+v._x, _y+v._y, _z+v._z); }
00069   Vector_3 operator-( const Type t) const 
00070   { return Vector_3(_x-t, _y-t, _z-t); }
00071   Vector_3 operator-( const Vector_3 &v) const 
00072   { return Vector_3(_x-v._x, _y-v._y, _z-v._z); }
00073   Vector_3 operator*( const Type t) const 
00074   { return Vector_3(_x*t, _y*t, _z*t); }
00075   Vector_3 operator/( const Type t) const 
00076   { return Vector_3(_x/t, _y/t, _z/t); }
00077   Type operator*( const Vector_3 &v) const 
00078   { return _x*v._x + _y*v._y + _z*v._z; }
00079 
00080   Vector_3 operator-() const
00081   { return Vector_3(-_x, -_y, -_z); }
00082 
00083   static Vector_3 cross_product( const Vector_3 &v, const Vector_3 &w) {
00084     return Vector_3( v._y * w._z - v._z * w._y ,
00085                      v._z * w._x - v._x * w._z ,
00086                      v._x * w._y - v._y * w._x );
00087   }
00088   
00089   Type squared_norm() const { return *this * *this; }
00090 
00091   Type norm() const { return std::sqrt(*this * *this); }
00092 
00093   Vector_3 &normalize() { 
00094     Type s=squared_norm();
00095     if ( s != Type(0)) { s=std::sqrt(s); _x/=s; _y/=s; _z/=s; }
00096     return *this;
00097   }
00098   
00099   Vector_3 &neg() { 
00100     _x = -_x; _y = -_y; _z = -_z; return *this;
00101   }
00102   
00103   bool operator==( const Vector_3 &p) const 
00104   { return x()==p.x() && y()==p.y() && z()==p.z(); }
00105   bool operator!=( const Vector_3 &p) const 
00106   { return x()!=p.x() || y()!=p.y() || z()!=p.z(); }
00107 
00108   bool operator<( const Vector_3 &v) const
00109   { return _x<v._x || _x==v._x && _y<v._y || 
00110       _x==v._x && _y==v._y && _z<v._z; }
00111 
00112   bool is_null() const { return _x==0&&_y==0&&_z==0; }
00113 protected:
00114   Type _x, _y, _z;
00115 };
00116 
00117 template <class T>
00118 Vector_3<T> operator*( T t, const Vector_3<T> &v) { return v*t; }
00119 
00120 template <class Type>
00121 std::ostream &operator<<( std::ostream &os, const Vector_3<Type> &p) {
00122   return os << '(' << p.x() << ',' << p.y() << ',' << p.z() << ')';
00123 }
00124 
00125 template <class T>
00126 class Point_3 : protected Vector_3 <T> { 
00127 public:
00128   Point_3() {}
00129   explicit Point_3( T t) : Vector_3<T>(t) {}
00130   Point_3( Origin) : Vector_3<T>(0, 0, 0) {}
00131 
00132   Point_3( T a, T b, T c) : Vector_3<T>(a, b, c) {}
00133   T &operator[](const int i) { return (&_x)[i]; }
00134   const T &operator[](const int i) const { return (&_x)[i]; }
00135   using Vector_3<T>::x;
00136   using Vector_3<T>::y;
00137   using Vector_3<T>::z;
00138 
00140   Point_3 &operator+=( const Vector_3<T> &v)
00141   { _x+=v.x(); _y+=v.y(); _z+=v.z(); return *this; }
00142   Point_3 &operator-=( const Vector_3<T> &v)
00143   { _x-=v.x(); _y-=v.y(); _z-=v.z(); return *this; }
00144   
00146   Point_3 operator+( const Vector_3<T> &v) const 
00147   { return Point_3(_x+v.x(), _y+v.y(), _z+v.z()); }
00148   Point_3 operator-( const Vector_3<T> &v) const 
00149   { return Point_3(_x-v.x(), _y-v.y(), _z-v.z()); }
00150   Vector_3<T> operator-( const Point_3 &v) const 
00151   { return Vector_3<T>(_x-v.x(), _y-v.y(), _z-v.z()); }
00152 
00153   bool operator==( const Point_3 &p) const 
00154   { return x()==p.x() && y()==p.y() && z()==p.z(); }
00155 
00156   bool operator!=( const Point_3 &p) const 
00157   { return x()!=p.x() || y()!=p.y() || z()!=p.z(); }
00158 
00159   bool operator<( const Point_3 &v) const
00160   { return Vector_3<T>::operator<(v); }
00161 
00162   bool is_origin() const { return _x==0&&_y==0&&_z==0; }
00163 
00164 #ifndef __SUNPRO_CC
00165 protected:
00166   using Vector_3<T>::_x;
00167   using Vector_3<T>::_y;
00168   using Vector_3<T>::_z;
00169 #endif
00170 };
00171 
00172 template <class Type>
00173 std::ostream &operator<<( std::ostream &os, const Point_3<Type> &p) {
00174   return os << '(' << p.x() << ',' << p.y() << ',' << p.z() << ')';
00175 }
00176 
00177 template <class Type>
00178 class Vector_2 { 
00179 public:
00180   Vector_2() {}
00181   explicit Vector_2( Type t) : _x(t), _y(t) {}
00182   Vector_2( Null_vector) : _x(0), _y(0) {}
00183   Vector_2( Type a, Type b) : _x(a), _y(b) {}
00184   Type &operator[](const int i) { return i==0?_x:_y; }
00185   const Type &operator[](const int& i) const { return i==0?_x:_y; }
00186 
00187   Type x() const { return _x; }
00188   Type y() const { return _y; }
00189 
00191   Vector_2 &operator+=( const Vector_2 &v)
00192   { _x+=v._x; _y+=v._y; return *this; }
00193   Vector_2 &operator-=( const Vector_2 &v)
00194   { _x-=v._x; _y-=v._y; return *this; }
00195   Vector_2 &operator*=( const Type t)
00196   { _x*=t; _y*=t; return *this; }
00197   Vector_2 &operator/=( const Type t)
00198   { _x/=t; _y/=t; return *this; }
00199 
00201   Vector_2 operator+( const Vector_2 &v) const 
00202   { return Vector_2(_x+v._x, _y+v._y); }
00203   Vector_2 operator-( const Vector_2 &v) const 
00204   { return Vector_2(_x-v._x, _y-v._y); }
00205   Vector_2 operator*( const Type t) const 
00206   { return Vector_2(_x*t, _y*t); }
00207   Vector_2 operator/( const Type t) const 
00208   { return Vector_2(_x/t, _y/t); }
00209   Type operator*( const Vector_2 &v) const 
00210   { return _x*v._x + _y*v._y; }
00211 
00212   Vector_2 operator-() const
00213   { return Vector_2(-_x, -_y); }
00214  
00215   Type squared_norm() const { return *this * *this; }
00216 
00217   Type norm() const { return std::sqrt(*this * *this); }
00218 
00219   Vector_2 &normalize() { 
00220     Type s=squared_norm();
00221     if ( s != Type(0)) { s=std::sqrt(s); _x/=s; _y/=s; }
00222     return *this;
00223   }
00224   
00225   Vector_2 &neg() { 
00226     _x = -_x; _y = -_y; return *this;
00227   }
00228   
00229   bool operator==( const Vector_2 &p) const 
00230   { return x()==p.x() && y()==p.y(); }
00231 
00232   bool operator!=( const Vector_2 &p) const 
00233   { return x()!=p.x() || y()!=p.y(); }
00234 
00235   bool operator<( const Vector_2 &v) const
00236   { return _x<v._x || _x==v._x && _y<v._y; }
00237 
00238   bool is_null() const { return _x==0&&_y==0; }
00239   
00240 protected:
00241   Type _x, _y; 
00242 };
00243 
00244 template <class T>
00245 Vector_2<T> operator*( T t, const Vector_2<T> &v) { return v*t; }
00246 
00247 template <class Type>
00248 std::ostream &operator<<( std::ostream &os, const Vector_2<Type> &p) {
00249   return os << '(' << p.x() << ',' << p.y() << ')';
00250 }
00251 
00252 template <class T>
00253 class Point_2 : protected Vector_2<T> { 
00254 public:
00255   Point_2() {}
00256   Point_2( Origin) : Vector_2<T>( 0, 0) {}
00257   explicit Point_2( T t) : Vector_2<T>(t) {}
00258   Point_2( T a, T b) : Vector_2<T>(a, b) {}
00259   T &operator[](const int i) { return (&_x)[i]; }
00260   const T &operator[](const int i) const { return (&_x)[i]; }
00261   using Vector_2<T>::x;
00262   using Vector_2<T>::y;
00263   
00265   Point_2 &operator+=( const Vector_2<T> &v)
00266   { _x+=v.x(); _y+=v.y(); return *this; }
00267   Point_2 &operator-=( const Vector_2<T> &v)
00268   { _x-=v.x(); _y-=v.y(); return *this; }
00269   
00271   Point_2 operator+( const Vector_2<T> &v) const 
00272   { return Point_2(_x+v.x(), _y+v.y()); }
00273   Point_2 operator-( const Vector_2<T> &v) const 
00274   { return Point_2(_x-v.x(), _y-v.y()); }
00275   Vector_2<T> operator-( const Point_2 &v) const 
00276   { return Vector_2<T>(_x-v.x(), _y-v.y()); }
00277 
00278   bool operator==( const Point_2 &p) const 
00279   { return x()==p.x() && y()==p.y(); }
00280   
00281   bool operator!=( const Point_2 &p) const 
00282   { return x()!=p.x() || y()!=p.y(); }
00283   
00284   bool operator<( const Point_2 &v) const
00285   { return Vector_2<T>::operator<(v); }
00286 
00287   bool is_origin() const { return _x==0&&_y==0; }
00288 
00289 #ifndef __SUNPRO_CC
00290 protected:
00291   using Vector_2<T>::_x;
00292   using Vector_2<T>::_y;
00293 #endif
00294 };
00295 
00296 template <class Type>
00297 std::ostream &operator<<( std::ostream &os, const Point_2<Type> &p) {
00298   return os << '(' << p.x() << ',' << p.y() << ')';
00299 }
00300 
00301 typedef double Real;
00302 
00303 // Modes of element_to_nodes.
00304 enum { E2N_USER=0, E2N_ONE=1, E2N_AREA=2, E2N_ANGLE=3};
00305 
00306 MAP_END_NAMESPACE
00307 
00308 #endif

Generated on Sun Jun 29 13:29:19 2008 for Charm++ by  doxygen 1.5.1