Reference documentation for deal.II version 8.1.0
point.h
1 // ---------------------------------------------------------------------
2 // @f$Id: point.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 1998 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__point_h
18 #define __deal2__point_h
19 
20 
21 #include <deal.II/base/config.h>
23 #include <deal.II/base/tensor_base.h>
24 #include <cmath>
25 
27 
55 template <int dim, typename Number>
56 class Point : public Tensor<1,dim,Number>
57 {
58 public:
64  Point ();
65 
73  explicit Point (const bool initialize);
74 
78  Point (const Tensor<1,dim,Number> &);
79 
88  explicit Point (const Number x);
89 
99  Point (const Number x,
100  const Number y);
101 
111  Point (const Number x,
112  const Number y,
113  const Number z);
114 
119  static Point<dim,Number> unit_vector(const unsigned int i);
120 
125  Number operator () (const unsigned int index) const;
126 
131  Number &operator () (const unsigned int index);
132 
133  /*
134  * Plus and minus operators are re-implemented from Tensor<1,dim>
135  * to avoid additional casting.
136  */
137 
145 
153 
158 
168  Point<dim,Number> operator * (const Number) const;
169 
174  Number operator * (const Tensor<1,dim,Number> &) const;
175 
182  Point<dim,Number> operator / (const Number) const;
183 
189  Number square () const;
190 
198  Number distance (const Point<dim,Number> &p) const;
199 
204  template <class Archive>
205  void serialize(Archive &ar, const unsigned int version);
206 };
207 
208 /*------------------------------- Inline functions: Point ---------------------------*/
209 
210 #ifndef DOXYGEN
211 
212 template <int dim, typename Number>
213 inline
215 {}
216 
217 
218 
219 template <int dim, typename Number>
220 inline
221 Point<dim,Number>::Point (const bool initialize)
222  :
223  Tensor<1,dim,Number>(initialize)
224 {}
225 
226 
227 
228 template <int dim, typename Number>
229 inline
231  :
232  Tensor<1,dim,Number>(t)
233 {}
234 
235 
236 
237 template <int dim, typename Number>
238 inline
239 Point<dim,Number>::Point (const Number x)
240 {
241  switch (dim)
242  {
243  case 1:
244  this->values[0] = x;
245  default:
247  }
248 }
249 
250 
251 
252 template <int dim, typename Number>
253 inline
254 Point<dim,Number>::Point (const Number x, const Number y)
255 {
256  switch (dim)
257  {
258  case 2:
259  this->values[0] = x;
260  this->values[1] = y;
261  default:
263  }
264 }
265 
266 
267 
268 template <int dim, typename Number>
269 inline
270 Point<dim,Number>::Point (const Number x, const Number y, const Number z)
271 {
272  switch (dim)
273  {
274  case 3:
275  this->values[0] = x;
276  this->values[1] = y;
277  this->values[2] = z;
278  default:
280  }
281 }
282 
283 
284 template <int dim, typename Number>
285 inline
287 Point<dim,Number>::unit_vector(unsigned int i)
288 {
290  p[i] = 1.;
291  return p;
292 }
293 
294 
295 template <int dim, typename Number>
296 inline
297 Number
298 Point<dim,Number>::operator () (const unsigned int index) const
299 {
300  AssertIndexRange((int) index, dim);
301  return this->values[index];
302 }
303 
304 
305 
306 template <int dim, typename Number>
307 inline
308 Number &
309 Point<dim,Number>::operator () (const unsigned int index)
310 {
311  AssertIndexRange((int) index, dim);
312  return this->values[index];
313 }
314 
315 
316 
317 template <int dim, typename Number>
318 inline
321 {
322  return (Point<dim,Number>(*this) += p);
323 }
324 
325 
326 
327 template <int dim, typename Number>
328 inline
331 {
332  return (Point<dim,Number>(*this) -= p);
333 }
334 
335 
336 
337 template <int dim, typename Number>
338 inline
341 {
342  Point<dim,Number> result;
343  for (unsigned int i=0; i<dim; ++i)
344  result.values[i] = -this->values[i];
345  return result;
346 }
347 
348 
349 
350 template <int dim, typename Number>
351 inline
353 Point<dim,Number>::operator * (const Number factor) const
354 {
355  return (Point<dim,Number>(*this) *= factor);
356 }
357 
358 
359 
360 template <int dim, typename Number>
361 inline
362 Number
364 {
365  // simply pass down
367 }
368 
369 
370 template <int dim, typename Number>
371 inline
372 Number
374 {
375  Number q = Number();
376  for (unsigned int i=0; i<dim; ++i)
377  q += this->values[i] * this->values[i];
378  return q;
379 }
380 
381 
382 
383 template <int dim, typename Number>
384 inline
385 Number
387 {
388  Number sum=0;
389  for (unsigned int i=0; i<dim; ++i)
390  {
391  const double diff=this->values[i]-p(i);
392  sum += diff*diff;
393  }
394 
395  return std::sqrt(sum);
396 }
397 
398 
399 
400 template <int dim, typename Number>
401 inline
402 Point<dim,Number> Point<dim,Number>::operator / (const Number factor) const
403 {
404  return (Point<dim,Number>(*this) /= factor);
405 }
406 
407 
408 
409 template <int dim, typename Number>
410 template <class Archive>
411 inline
412 void
413 Point<dim,Number>::serialize(Archive &ar, const unsigned int)
414 {
415  // forward to serialization
416  // function in the base class
417  ar &static_cast<Tensor<1,dim,Number> &>(*this);
418 }
419 
420 #endif // DOXYGEN
421 
422 
423 /*------------------------------- Global functions: Point ---------------------------*/
424 
425 
430 template <int dim, typename Number>
431 inline
432 Point<dim,Number> operator * (const Number factor,
433  const Point<dim,Number> &p)
434 {
435  return p*factor;
436 }
437 
438 
439 
444 template <int dim>
445 inline
446 Point<dim,double> operator * (const double factor,
447  const Point<dim,double> &p)
448 {
449  return p*factor;
450 }
451 
452 
453 
459 template <int dim, typename Number>
460 inline
461 std::ostream &operator << (std::ostream &out,
462  const Point<dim,Number> &p)
463 {
464  for (unsigned int i=0; i<dim-1; ++i)
465  out << p[i] << ' ';
466  out << p[dim-1];
467 
468  return out;
469 }
470 
471 
472 
478 template <int dim, typename Number>
479 inline
480 std::istream &operator >> (std::istream &in,
482 {
483  for (unsigned int i=0; i<dim; ++i)
484  in >> p[i];
485 
486  return in;
487 }
488 
489 
490 #ifndef DOXYGEN
491 
497 template <typename Number>
498 inline
499 std::ostream &operator << (std::ostream &out,
500  const Point<1,Number> &p)
501 {
502  out << p[0];
503 
504  return out;
505 }
506 
507 #endif // DOXYGEN
508 DEAL_II_NAMESPACE_CLOSE
509 
510 #endif
Point< dim, Number > operator/(const Number) const
Number operator()(const unsigned int index) const
Point< dim, Number > operator-() const
Tensor< 1, dim, Number > operator*(const Tensor< 1, dim, Number > &t, const Number factor)
Definition: tensor_base.h:1361
#define AssertIndexRange(index, range)
Definition: exceptions.h:888
Number square() const
T sum(const T &t, const MPI_Comm &mpi_communicator)
Definition: mpi.h:447
#define Assert(cond, exc)
Definition: exceptions.h:299
static Point< dim, Number > unit_vector(const unsigned int i)
void serialize(Archive &ar, const unsigned int version)
Point< dim, Number > operator+(const Tensor< 1, dim, Number > &) const
::ExceptionBase & ExcInvalidConstructorCall()
std::ostream & operator<<(std::ostream &os, const Vector< number > &v)
Definition: vector.h:1546
Number distance(const Point< dim, Number > &p) const
Point< dim, Number > operator*(const Number) const