rheolef  7.0
point.h
Go to the documentation of this file.
1 # ifndef _RHEO_BASIC_POINT_H
2 # define _RHEO_BASIC_POINT_H
3 // point_basic<T> : set of coordinates of type T
4 #include "rheolef/compiler.h"
5 #include <sstream>
6 #ifdef _RHEOLEF_USE_NEW_CODE
7 #include "rheolef/distributed.h"
8 #endif // _RHEOLEF_USE_NEW_CODE
9 
10 namespace rheolef {
11 
21 template <class T>
22 class point_basic {
23  public:
24 
25 
26  typedef size_t size_type;
27  typedef T element_type;
28  typedef T scalar_type;
29  typedef T float_type;
30 
31 
32  explicit point_basic () { _x[0] = T(); _x[1] = T(); _x[2] = T(); }
33 
34  explicit point_basic (
35  const T& x0,
36  const T& x1 = 0,
37  const T& x2 = 0)
38  { _x[0] = x0; _x[1] = x1; _x[2] = x2; }
39 
40  template <class T1>
42  { _x[0] = p._x[0]; _x[1] = p._x[1]; _x[2] = p._x[2]; }
43 
44  template <class T1>
46  { _x[0] = p._x[0]; _x[1] = p._x[1]; _x[2] = p._x[2]; return *this; }
47 
48 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
49  point_basic (const std::initializer_list<T>& il);
50 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
51 
52 
53  T& operator[](int i_coord) { return _x[i_coord%3]; }
54  const T& operator[](int i_coord) const { return _x[i_coord%3]; }
55  T& operator()(int i_coord) { return _x[i_coord%3]; }
56  const T& operator()(int i_coord) const { return _x[i_coord%3]; }
57 
58  const T& x() const { return _x[0]; }
59  const T& y() const { return _x[1]; }
60  const T& z() const { return _x[2]; }
61  T& x(){ return _x[0]; }
62  T& y(){ return _x[1]; }
63  T& z(){ return _x[2]; }
64 
65 // inputs/outputs:
66 
67  std::istream& get (std::istream& s, int d = 3)
68  {
69  switch (d) {
70  case 0 : _x[0] = _x[1] = _x[2] = T(0); return s;
71  case 1 : _x[1] = _x[2] = T(0); return s >> _x[0];
72  case 2 : _x[2] = T(0); return s >> _x[0] >> _x[1];
73  default: return s >> _x[0] >> _x[1] >> _x[2];
74  }
75  }
76  std::ostream& put (std::ostream& s, int d = 3) const;
77 
78 
79  bool operator== (const point_basic<T>& v) const
80  { return _x[0] == v[0] && _x[1] == v[1] && _x[2] == v[2]; }
81 
82  bool operator!= (const point_basic<T>& v) const
83  { return !operator==(v); }
84 
86  { _x[0] += v[0]; _x[1] += v[1]; _x[2] += v[2]; return *this; }
87 
89  { _x[0] -= v[0]; _x[1] -= v[1]; _x[2] -= v[2]; return *this; }
90 
92  { _x[0] *= a; _x[1] *= a; _x[2] *= a; return *this; }
93 
95  { _x[0] /= a; _x[1] /= a; _x[2] /= a; return *this; }
96 
98  { return point_basic<T> (_x[0]+v[0], _x[1]+v[1], _x[2]+v[2]); }
99 
101  { return point_basic<T> (-_x[0], -_x[1], -_x[2]); }
102 
104  { return point_basic<T> (_x[0]-v[0], _x[1]-v[1], _x[2]-v[2]); }
105 
106  template <class U>
107  typename
108  std::enable_if<
111  >::type
112  operator* (const U& a) const
113  { return point_basic<T> (_x[0]*a, _x[1]*a, _x[2]*a); }
114  point_basic<T> operator/ (const T& a) const
115  { return operator* (T(1)/T(a)); }
117  { return point_basic<T> (_x[0]/v[0], _x[1]/v[1], _x[2]/v[2]); }
118 
119  T _x[3];
120  static T _my_abs(const T& x) { return (x > T(0)) ? x : -x; }
121 };
123 
124 template <class T, class U>
125 inline
126 typename
127 std::enable_if<
130 >::type
131 operator* (const U& a, const point_basic<T>& u)
132 {
133  return point_basic<T> (a*u[0], a*u[1], a*u[2]);
134 }
135 template<class T>
136 inline
138 vect (const point_basic<T>& v, const point_basic<T>& w)
139 {
140  return point_basic<T> (
141  v[1]*w[2]-v[2]*w[1],
142  v[2]*w[0]-v[0]*w[2],
143  v[0]*w[1]-v[1]*w[0]);
144 }
145 template<class T>
146 inline
147 T dot (const point_basic<T>& x, const point_basic<T>& y)
148 {
149  return x[0]*y[0]+x[1]*y[1]+x[2]*y[2];
150 }
151 template<class T>
152 inline
154 {
155  return dot(x,x);
156 }
157 template<class T>
158 inline
160 {
161  return sqrt(norm2(x));
162 }
163 template<class T>
164 inline
166 {
167  return norm2(x-y);
168 }
169 template<class T>
170 inline
172 {
173  return norm(x-y);
174 }
175 template<class T>
176 inline
178 {
179  return max(point_basic<T>::_my_abs(x[0]-y[0]),
180  max(point_basic<T>::_my_abs(x[1]-y[1]),
181  point_basic<T>::_my_abs(x[2]-y[2])));
182 }
183 template <class T>
184 T vect2d (const point_basic<T>& v, const point_basic<T>& w);
185 
186 template <class T>
187 T mixt (const point_basic<T>& u, const point_basic<T>& v, const point_basic<T>& w);
188 
189 // robust(exact) floating point predicates: return the sign of the value as (0, > 0, < 0)
190 template <class T>
191 int
193  const point_basic<T>& a,
194  const point_basic<T>& b,
195  const point_basic<T>& c);
196 
197 template <class T>
198 int
200  const point_basic<T>& a,
201  const point_basic<T>& b,
202  const point_basic<T>& c,
203  const point_basic<T>& d);
204 
205 template <class T>
206 T orient2d(
207  const point_basic<T>& a,
208  const point_basic<T>& b,
209  const point_basic<T>& c);
210 
211 template <class T>
212 T orient3d(
213  const point_basic<T>& a,
214  const point_basic<T>& b,
215  const point_basic<T>& c,
216  const point_basic<T>& d);
217 
218 template <class T>
219 std::string ptos (const point_basic<T>& x, int d = 3);
220 
221 template<class T, size_t d>
222 bool
224 {
225  for (typename point_basic<T>::size_type i = 0; i < d; i++) {
226  if (a[i] < b[i]) return true;
227  if (a[i] > b[i]) return false;
228  }
229  return false; // equality
230 }
231 @endcode
232 
234 template<class T> struct scalar_traits { typedef T type; };
235 template<class T> struct scalar_traits<point_basic<T> > { typedef T type; };
236 template<class T> struct float_traits<point_basic<T> > { typedef typename float_traits<T>::type type; };
237 
238 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
239 template<class T>
240 point_basic<T>::point_basic (const std::initializer_list<T>& il) : _x() {
241 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
242  typedef typename std::initializer_list<T>::const_iterator const_iterator;
243 #else // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
244  typedef const T* const_iterator;
245 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
246  check_macro (il.size() <= 3, "unexpected initializer list size=" << il.size() << " > 3");
247  size_type i = 0;
248  for (const_iterator iter = il.begin(); iter != il.end(); ++iter, ++i) {
249  _x[i] = *iter;
250  }
251  for (i = il.size(); i < 3; ++i) {
252  _x[i] = T();
253  }
254 }
255 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
256 
257 // input/output
258 template<class T>
259 inline
260 std::ostream&
261 point_basic<T>::put (std::ostream& s, int d) const
262 {
263  switch (d) {
264  case 0 : return s;
265  case 1 : return s << _x[0];
266  case 2 : return s << _x[0] << " " << _x[1];
267  default: return s << _x[0] << " " << _x[1] << " " << _x[2];
268  }
269 }
270 template<class T>
271 inline
272 std::istream&
273 operator >> (std::istream& s, point_basic<T>& p)
274 {
275  return s >> p[0] >> p[1] >> p[2];
276 }
277 template<class T>
278 inline
279 std::ostream&
280 operator << (std::ostream& s, const point_basic<T>& p)
281 {
282  return s << p[0] << " " << p[1] << " " << p[2];
283 }
284 template<class T>
285 std::string
286 ptos (const point_basic<T>& x, int d)
287 {
288  std::ostringstream ostrstr;
289  x.put (ostrstr, d);
290  return ostrstr.str();
291 }
292 #define def_point_function2(f,F) \
293 template<class T> \
294 inline \
295 point_basic<T> \
296 f (const point_basic<T>& x) \
297 { \
298  point_basic<T> y; \
299  for (size_t i = 0; i < 3; i++) \
300  y[i] = F(x[i]); \
301  return y; \
302 }
303 
304 #define def_point_function(f) def_point_function2(f,f)
305 
307 def_point_function(sqrt)
309 def_point_function(log10)
311 #undef def_point_function2
312 #undef def_point_function
313 
314 template<class T1, class T2>
315 inline
316 point_basic<T1>
317 operator/ (const T2& a, const point_basic<T1>& x)
318 {
320  for (size_t i = 0; i < 3; i++)
321  if (x[i] != 0) y[i] = a/x[i];
322  return y;
323 }
324 template <class T>
325 inline
326 T
328 {
329  return orient2d (point_basic<T>(), v, w);
330 }
331 template <class T>
332 inline
333 T
334 mixt (const point_basic<T>& u, const point_basic<T>& v, const point_basic<T>& w)
335 {
336  return orient3d (point_basic<T>(), u, v, w);
337 }
338 
339 }// namespace rheolef
340 #ifdef _RHEOLEF_USE_NEW_CODE
341 #ifdef _RHEOLEF_HAVE_MPI
342 namespace boost {
343  namespace serialization {
344  template<class Archive, class T>
345  void serialize (Archive& ar, class rheolef::point_basic<T>& x, const unsigned int version) {
346  ar & x[0];
347  ar & x[1];
348  ar & x[2];
349  }
350  } // namespace serialization
351 } // namespace boost
352 
353 namespace boost {
354  namespace mpi {
355  // TODO: when point_basic<T> is not a simple type, such as T=bigfloat or T=gmp, etc
356  template <>
357  struct is_mpi_datatype<rheolef::point_basic<double> > : mpl::true_ { };
358  } // namespace mpi
359 } // namespace boost
360 #endif // _RHEOLEF_HAVE_MPI
361 #endif // _RHEOLEF_USE_NEW_CODE
362 
363 #endif /* _RHEO_BASIC_POINT_H */
point_basic< T > vect(const point_basic< T > &v, const point_basic< T > &w)
Definition: point.h:138
const T & y() const
Definition: point.h:59
point_basic< T > operator-() const
Definition: point.h:100
point_basic< T > & operator+=(const point_basic< T > &v)
Definition: point.h:85
T dist2(const point_basic< T > &x, const point_basic< T > &y)
Definition: point.h:165
T & operator()(int i_coord)
Definition: point.h:55
size_t size_type
Definition: point.h:26
point_basic< T > & operator*=(const T &a)
Definition: point.h:91
std::enable_if< details::is_rheolef_arithmetic< U >::value,point_basic< T > >::type operator*(const U &a) const
Definition: point.h:112
const T & operator()(int i_coord) const
Definition: point.h:56
T orient2d(const point_basic< T > &a, const point_basic< T > &b, const point_basic< T > &c)
point - vertex of a mesh
Definition: point.h:22
rheolef::std enable_if ::type dot const Expr1 expr1, const Expr2 expr2 dot(const Expr1 &expr1, const Expr2 &expr2)
Definition: vec_expr_v2.h:335
static double sqr(const double &x)
Definition: compiler.h:203
irheostream, orheostream - large data streams
Definition: compiler.h:7
point_basic< T > & operator-=(const point_basic< T > &v)
Definition: point.h:88
const T & operator[](int i_coord) const
Definition: point.h:54
bool operator==(const point_basic< T > &v) const
Definition: point.h:79
T norm2(const vec< T, M > &x)
Definition: vec.h:334
T mixt(const point_basic< T > &u, const point_basic< T > &v, const point_basic< T > &w)
Definition: point.h:334
T norm(const vec< T, M > &x)
Definition: vec.h:341
#define check_macro(ok_condition, message)
Definition: compiler.h:104
size_t d
T dist_infty(const point_basic< T > &x, const point_basic< T > &y)
Definition: point.h:177
const T & x() const
Definition: point.h:58
bool operator!=(const point_basic< T > &v) const
Definition: point.h:82
tensor_basic< T > exp(const tensor_basic< T > &a, size_t d)
Definition: tensor-exp.cc:147
int sign_orient3d(const point_basic< T > &a, const point_basic< T > &b, const point_basic< T > &c, const point_basic< T > &d)
point_basic< T > operator/(const T &a) const
Definition: point.h:114
Definition: rotating-hill.h:1
T vect2d(const point_basic< T > &v, const point_basic< T > &w)
Definition: point.h:327
std::string ptos(const point_basic< T > &x, int d=3)
Definition: point.h:286
int sign_orient2d(const point_basic< T > &a, const point_basic< T > &b, const point_basic< T > &c)
point_basic< T > & operator/=(const T &a)
Definition: point.h:94
T orient3d(const point_basic< T > &a, const point_basic< T > &b, const point_basic< T > &c, const point_basic< T > &d)
point_basic< Float > point
Definition: point.h:122
point_basic(const T &x0, const T &x1=0, const T &x2=0)
Definition: point.h:34
helper for point_basic<T> & tensor_basic<T>: get basic T type
Definition: point.h:234
float_traits< T >::type type
Definition: point.h:236
bool lexicographically_less(const point_basic< T > &a, const point_basic< T > &b)
Definition: point.h:223
std::istream & operator>>(std::istream &is, const catchmark &m)
Definition: catchmark.h:48
rheolef::std type
point_basic< T > operator+(const point_basic< T > &v) const
Definition: point.h:97
def_point_function(sqr) def_point_function(sqrt) def_point_function(log) def_point_function(log10) def_point_function(exp) template< class T1
point_basic< T > & operator=(const point_basic< T1 > &p)
Definition: point.h:45
std::ostream & put(std::ostream &s, int d=3) const
Definition: point.h:261
static T _my_abs(const T &x)
Definition: point.h:120
const T & z() const
Definition: point.h:60
T dist(const point_basic< T > &x, const point_basic< T > &y)
Definition: point.h:171
T & operator[](int i_coord)
Definition: point.h:53