Reference documentation for deal.II version Git 2618e0f 2017-11-23 17:25:26 +0100
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 
103 template <int dim, typename Number = double>
104 class Point : public Tensor<1,dim,Number>
105 {
106 public:
111  Point ();
112 
116  explicit Point (const Tensor<1,dim,Number> &);
117 
124  explicit Point (const Number x);
125 
133  Point (const Number x,
134  const Number y);
135 
143  Point (const Number x,
144  const Number y,
145  const Number z);
146 
152  static Point<dim,Number> unit_vector(const unsigned int i);
153 
157  Number operator () (const unsigned int index) const;
158 
162  Number &operator () (const unsigned int index);
163 
164  /*
165  * @name Addition and subtraction of points.
166  * @{
167  */
168 
173 
182 
190 
195 
200  /*
201  * @name Multiplication and scaling of points. Dot products. Norms.
202  * @{
203  */
204 
210  template <typename OtherNumber>
212  operator * (const OtherNumber) const;
213 
217  template <typename OtherNumber>
219  operator / (const OtherNumber) const;
220 
224  Number operator * (const Tensor<1,dim,Number> &p) const;
225 
237 
244 
250 
259  template <class Archive>
260  void serialize(Archive &ar, const unsigned int version);
261 };
262 
263 /*------------------------------- Inline functions: Point ---------------------------*/
264 
265 #ifndef DOXYGEN
266 
267 // At least clang-3.7 requires us to have a user-defined constructor
268 // and we can't use 'Point<dim,Number>::Point () = default' here.
269 template <int dim, typename Number>
270 inline
272 {}
273 
274 
275 
276 template <int dim, typename Number>
277 inline
279  :
280  Tensor<1,dim,Number>(t)
281 {}
282 
283 
284 
285 template <int dim, typename Number>
286 inline
287 Point<dim,Number>::Point (const Number x)
288 {
289  Assert (dim==1,
290  ExcMessage ("You can only initialize Point<1> objects using the constructor "
291  "that takes only one argument. Point<dim> objects with dim!=1 "
292  "require initialization with the constructor that takes 'dim' "
293  "arguments."));
294 
295  // we can only get here if we pass the assertion. use the switch anyway so
296  // as to avoid compiler warnings about uninitialized elements or writing
297  // beyond the end of the 'values' array
298  switch (dim)
299  {
300  case 1:
301  this->values[0] = x;
302  break;
303 
304  default:
305  ;
306  }
307 }
308 
309 
310 
311 template <int dim, typename Number>
312 inline
313 Point<dim,Number>::Point (const Number x,
314  const Number y)
315 {
316  Assert (dim==2,
317  ExcMessage ("You can only initialize Point<2> objects using the constructor "
318  "that takes two arguments. Point<dim> objects with dim!=2 "
319  "require initialization with the constructor that takes 'dim' "
320  "arguments."));
321  // we can only get here if we pass the assertion. use the switch anyway so
322  // as to avoid compiler warnings about uninitialized elements or writing
323  // beyond the end of the 'values' array
324  switch (dim)
325  {
326  case 2:
327  this->values[0] = x;
328  this->values[1] = y;
329  break;
330 
331  default:
332  ;
333  }
334 }
335 
336 
337 
338 template <int dim, typename Number>
339 inline
340 Point<dim,Number>::Point (const Number x,
341  const Number y,
342  const Number z)
343 {
344  Assert (dim==3,
345  ExcMessage ("You can only initialize Point<3> objects using the constructor "
346  "that takes three arguments. Point<dim> objects with dim!=3 "
347  "require initialization with the constructor that takes 'dim' "
348  "arguments."));
349 
350  // we can only get here if we pass the assertion. use the switch anyway so
351  // as to avoid compiler warnings about uninitialized elements or writing
352  // beyond the end of the 'values' array
353  switch (dim)
354  {
355  case 3:
356  this->values[0] = x;
357  this->values[1] = y;
358  this->values[2] = z;
359  break;
360 
361  default:
362  ;
363  }
364 }
365 
366 
367 template <int dim, typename Number>
368 inline
370 Point<dim,Number>::unit_vector(unsigned int i)
371 {
373  p[i] = 1.;
374  return p;
375 }
376 
377 
378 template <int dim, typename Number>
379 inline
380 Number
381 Point<dim,Number>::operator () (const unsigned int index) const
382 {
383  AssertIndexRange((int) index, dim);
384  return this->values[index];
385 }
386 
387 
388 
389 template <int dim, typename Number>
390 inline
391 Number &
392 Point<dim,Number>::operator () (const unsigned int index)
393 {
394  AssertIndexRange((int) index, dim);
395  return this->values[index];
396 }
397 
398 
399 
400 template <int dim, typename Number>
401 inline
404 {
405  Point<dim,Number> tmp = *this;
406  tmp += p;
407  return tmp;
408 }
409 
410 
411 
412 template <int dim, typename Number>
413 inline
416 {
417  return (Tensor<1,dim,Number>(*this) -= p);
418 }
419 
420 
421 
422 template <int dim, typename Number>
423 inline
426 {
427  Point<dim,Number> tmp = *this;
428  tmp -= p;
429  return tmp;
430 }
431 
432 
433 
434 template <int dim, typename Number>
435 inline
438 {
439  Point<dim,Number> result;
440  for (unsigned int i=0; i<dim; ++i)
441  result.values[i] = -this->values[i];
442  return result;
443 }
444 
445 
446 
447 template <int dim, typename Number>
448 template <typename OtherNumber>
449 inline
451 Point<dim,Number>::operator * (const OtherNumber factor) const
452 {
454  for (unsigned int i=0; i<dim; ++i)
455  tmp[i] = this->operator[](i) * factor;
456  return tmp;
457 }
458 
459 
460 
461 template <int dim, typename Number>
462 template <typename OtherNumber>
463 inline
465 Point<dim,Number>::operator / (const OtherNumber factor) const
466 {
468  for (unsigned int i=0; i<dim; ++i)
469  tmp[i] = this->operator[](i) / factor;
470  return tmp;
471 }
472 
473 
474 
475 template <int dim, typename Number>
476 inline
477 Number
479 {
480  Number res = Number();
481  for (unsigned int i=0; i<dim; ++i)
482  res += this->operator[](i) * p[i];
483  return res;
484 }
485 
486 
487 template <int dim, typename Number>
488 inline
491 {
492  return this->norm_square();
493 }
494 
495 
496 
497 template <int dim, typename Number>
498 inline
501 {
502  return std::sqrt(distance_square(p));
503 }
504 
505 
506 
507 template <int dim, typename Number>
508 inline
511 {
513  for (unsigned int i=0; i<dim; ++i)
514  {
515  const Number diff=static_cast<Number>(this->values[i])-p(i);
517  }
518 
519  return sum;
520 }
521 
522 
523 
524 template <int dim, typename Number>
525 template <class Archive>
526 inline
527 void
528 Point<dim,Number>::serialize(Archive &ar, const unsigned int)
529 {
530  // forward to serialization
531  // function in the base class
532  ar &static_cast<Tensor<1,dim,Number> &>(*this);
533 }
534 
535 #endif // DOXYGEN
536 
537 
538 /*------------------------------- Global functions: Point ---------------------------*/
539 
540 
547 template <int dim, typename Number, typename OtherNumber>
548 inline
550 operator * (const OtherNumber factor,
551  const Point<dim,Number> &p)
552 {
553  return p * factor;
554 }
555 
556 
557 
563 template <int dim, typename Number>
564 inline
565 std::ostream &operator << (std::ostream &out,
566  const Point<dim,Number> &p)
567 {
568  for (unsigned int i=0; i<dim-1; ++i)
569  out << p[i] << ' ';
570  out << p[dim-1];
571 
572  return out;
573 }
574 
575 
576 
581 template <int dim, typename Number>
582 inline
583 std::istream &operator >> (std::istream &in,
585 {
586  for (unsigned int i=0; i<dim; ++i)
587  in >> p[i];
588 
589  return in;
590 }
591 
592 
593 #ifndef DOXYGEN
594 
600 template <typename Number>
601 inline
602 std::ostream &operator << (std::ostream &out,
603  const Point<1,Number> &p)
604 {
605  out << p[0];
606 
607  return out;
608 }
609 
610 #endif // DOXYGEN
611 DEAL_II_NAMESPACE_CLOSE
612 
613 #endif
Number operator()(const unsigned int index) const
Point< dim, Number > operator-() const
static DEAL_II_CUDA_HOST_DEV real_type abs_square(const number &x)
Definition: numbers.h:343
#define AssertIndexRange(index, range)
Definition: exceptions.h:1207
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
Definition: point.h:104
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:337
numbers::NumberTraits< Number >::real_type distance_square(const Point< dim, Number > &p) const
static Point< dim, Number > unit_vector(const unsigned int i)
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const OtherNumber) const
void serialize(Archive &ar, const unsigned int version)
Point< dim, Number > operator+(const Tensor< 1, dim, Number > &) const
SymmetricTensor< rank, dim, Number > sum(const SymmetricTensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
numbers::NumberTraits< Number >::real_type square() const
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:163
Definition: mpi.h:53
std::istream & operator>>(std::istream &in, Point< dim, Number > &p)
Definition: point.h:583
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber) const