Reference documentation for deal.II version 8.5.1
point.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__point_h
17 #define dealii__point_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/tensor.h>
23 #include <cmath>
24 
25 DEAL_II_NAMESPACE_OPEN
26 
88 template <int dim, typename Number = double>
89 class Point : public Tensor<1,dim,Number>
90 {
91 public:
96  Point ();
97 
101  explicit Point (const Tensor<1,dim,Number> &);
102 
109  explicit Point (const Number x);
110 
118  Point (const Number x,
119  const Number y);
120 
128  Point (const Number x,
129  const Number y,
130  const Number z);
131 
137  static Point<dim,Number> unit_vector(const unsigned int i);
138 
142  Number operator () (const unsigned int index) const;
143 
147  Number &operator () (const unsigned int index);
148 
149  /*
150  * @name Addition and subtraction of points.
151  * @{
152  */
153 
158 
167 
175 
180 
185  /*
186  * @name Multiplication and scaling of points. Dot products. Norms.
187  * @{
188  */
189 
195  template <typename OtherNumber>
197  operator * (const OtherNumber) const;
198 
202  template <typename OtherNumber>
204  operator / (const OtherNumber) const;
205 
209  Number operator * (const Tensor<1,dim,Number> &p) const;
210 
222 
229 
238  template <class Archive>
239  void serialize(Archive &ar, const unsigned int version);
240 };
241 
242 /*------------------------------- Inline functions: Point ---------------------------*/
243 
244 #ifndef DOXYGEN
245 
246 template <int dim, typename Number>
247 inline
249 {}
250 
251 
252 
253 template <int dim, typename Number>
254 inline
256  :
257  Tensor<1,dim,Number>(t)
258 {}
259 
260 
261 
262 template <int dim, typename Number>
263 inline
264 Point<dim,Number>::Point (const Number x)
265 {
266  Assert (dim==1,
267  ExcMessage ("You can only initialize Point<1> objects using the constructor "
268  "that takes only one argument. Point<dim> objects with dim!=1 "
269  "require initialization with the constructor that takes 'dim' "
270  "arguments."));
271 
272  // we can only get here if we pass the assertion. use the switch anyway so
273  // as to avoid compiler warnings about uninitialized elements or writing
274  // beyond the end of the 'values' array
275  switch (dim)
276  {
277  case 1:
278  this->values[0] = x;
279  break;
280 
281  default:
282  ;
283  }
284 }
285 
286 
287 
288 template <int dim, typename Number>
289 inline
290 Point<dim,Number>::Point (const Number x,
291  const Number y)
292 {
293  Assert (dim==2,
294  ExcMessage ("You can only initialize Point<2> objects using the constructor "
295  "that takes two arguments. Point<dim> objects with dim!=2 "
296  "require initialization with the constructor that takes 'dim' "
297  "arguments."));
298  // we can only get here if we pass the assertion. use the switch anyway so
299  // as to avoid compiler warnings about uninitialized elements or writing
300  // beyond the end of the 'values' array
301  switch (dim)
302  {
303  case 2:
304  this->values[0] = x;
305  this->values[1] = y;
306  break;
307 
308  default:
309  ;
310  }
311 }
312 
313 
314 
315 template <int dim, typename Number>
316 inline
317 Point<dim,Number>::Point (const Number x,
318  const Number y,
319  const Number z)
320 {
321  Assert (dim==3,
322  ExcMessage ("You can only initialize Point<3> objects using the constructor "
323  "that takes three arguments. Point<dim> objects with dim!=3 "
324  "require initialization with the constructor that takes 'dim' "
325  "arguments."));
326 
327  // we can only get here if we pass the assertion. use the switch anyway so
328  // as to avoid compiler warnings about uninitialized elements or writing
329  // beyond the end of the 'values' array
330  switch (dim)
331  {
332  case 3:
333  this->values[0] = x;
334  this->values[1] = y;
335  this->values[2] = z;
336  break;
337 
338  default:
339  ;
340  }
341 }
342 
343 
344 template <int dim, typename Number>
345 inline
347 Point<dim,Number>::unit_vector(unsigned int i)
348 {
350  p[i] = 1.;
351  return p;
352 }
353 
354 
355 template <int dim, typename Number>
356 inline
357 Number
358 Point<dim,Number>::operator () (const unsigned int index) const
359 {
360  AssertIndexRange((int) index, dim);
361  return this->values[index];
362 }
363 
364 
365 
366 template <int dim, typename Number>
367 inline
368 Number &
369 Point<dim,Number>::operator () (const unsigned int index)
370 {
371  AssertIndexRange((int) index, dim);
372  return this->values[index];
373 }
374 
375 
376 
377 template <int dim, typename Number>
378 inline
381 {
382  Point<dim,Number> tmp = *this;
383  tmp += p;
384  return tmp;
385 }
386 
387 
388 
389 template <int dim, typename Number>
390 inline
393 {
394  return (Tensor<1,dim,Number>(*this) -= p);
395 }
396 
397 
398 
399 template <int dim, typename Number>
400 inline
403 {
404  Point<dim,Number> tmp = *this;
405  tmp -= p;
406  return tmp;
407 }
408 
409 
410 
411 template <int dim, typename Number>
412 inline
415 {
416  Point<dim,Number> result;
417  for (unsigned int i=0; i<dim; ++i)
418  result.values[i] = -this->values[i];
419  return result;
420 }
421 
422 
423 
424 template <int dim, typename Number>
425 template<typename OtherNumber>
426 inline
428 Point<dim,Number>::operator * (const OtherNumber factor) const
429 {
431  for (unsigned int i=0; i<dim; ++i)
432  tmp[i] = this->operator[](i) * factor;
433  return tmp;
434 }
435 
436 
437 
438 template <int dim, typename Number>
439 template<typename OtherNumber>
440 inline
442 Point<dim,Number>::operator / (const OtherNumber factor) const
443 {
445  for (unsigned int i=0; i<dim; ++i)
446  tmp[i] = this->operator[](i) / factor;
447  return tmp;
448 }
449 
450 
451 
452 template <int dim, typename Number>
453 inline
454 Number
456 {
457  Number res = Number();
458  for (unsigned int i=0; i<dim; ++i)
459  res += this->operator[](i) * p[i];
460  return res;
461 }
462 
463 
464 template <int dim, typename Number>
465 inline
468 {
469  return this->norm_square();
470 }
471 
472 
473 
474 template <int dim, typename Number>
475 inline
478 {
479  Number sum = Number();
480  for (unsigned int i=0; i<dim; ++i)
481  {
482  const Number diff=this->values[i]-p(i);
484  }
485 
486  return std::sqrt(sum);
487 }
488 
489 
490 
491 template <int dim, typename Number>
492 template <class Archive>
493 inline
494 void
495 Point<dim,Number>::serialize(Archive &ar, const unsigned int)
496 {
497  // forward to serialization
498  // function in the base class
499  ar &static_cast<Tensor<1,dim,Number> &>(*this);
500 }
501 
502 #endif // DOXYGEN
503 
504 
505 /*------------------------------- Global functions: Point ---------------------------*/
506 
507 
514 template <int dim, typename Number, typename OtherNumber>
515 inline
517 operator * (const OtherNumber factor,
518  const Point<dim,Number> &p)
519 {
520  return p * factor;
521 }
522 
523 
524 
530 template <int dim, typename Number>
531 inline
532 std::ostream &operator << (std::ostream &out,
533  const Point<dim,Number> &p)
534 {
535  for (unsigned int i=0; i<dim-1; ++i)
536  out << p[i] << ' ';
537  out << p[dim-1];
538 
539  return out;
540 }
541 
542 
543 
549 template <int dim, typename Number>
550 inline
551 std::istream &operator >> (std::istream &in,
553 {
554  for (unsigned int i=0; i<dim; ++i)
555  in >> p[i];
556 
557  return in;
558 }
559 
560 
561 #ifndef DOXYGEN
562 
568 template <typename Number>
569 inline
570 std::ostream &operator << (std::ostream &out,
571  const Point<1,Number> &p)
572 {
573  out << p[0];
574 
575  return out;
576 }
577 
578 #endif // DOXYGEN
579 DEAL_II_NAMESPACE_CLOSE
580 
581 #endif
Point< dim, Number > operator-() const
Point< dim, Number > operator+(const Tensor< 1, dim, Number > &) const
#define AssertIndexRange(index, range)
Definition: exceptions.h:1170
Number operator()(const unsigned int index) const
Definition: point.h:89
static ::ExceptionBase & ExcMessage(std::string arg1)
static real_type abs_square(const number &x)
Definition: numbers.h:333
#define Assert(cond, exc)
Definition: exceptions.h:313
static Point< dim, Number > unit_vector(const unsigned int i)
void serialize(Archive &ar, const unsigned int version)
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
SymmetricTensor< rank, dim, Number > sum(const SymmetricTensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
Definition: mpi.h:41
numbers::NumberTraits< Number >::real_type square() const
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber) const
std::istream & operator>>(std::istream &in, Point< dim, Number > &p)
Definition: point.h:551
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const OtherNumber) const