Reference documentation for deal.II version GIT 0980a66d4b 2023-03-23 20:20:03+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
point.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2021 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.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_point_h
17 #define dealii_point_h
18 
19 
20 #include <deal.II/base/config.h>
21 
23 #include <deal.II/base/tensor.h>
24 
25 #include <boost/geometry/core/cs.hpp>
26 #include <boost/geometry/geometries/point.hpp>
27 
28 #include <cmath>
29 
31 
107 template <int dim, typename Number = double>
108 DEAL_II_CXX20_REQUIRES(dim >= 0)
109 class Point : public Tensor<1, dim, Number>
110 {
111 public:
119  Point();
120 
124  explicit DEAL_II_HOST_DEVICE
126 
135  explicit DEAL_II_HOST_DEVICE
136  Point(const Number x);
137 
148  Point(const Number x, const Number y);
149 
160  Point(const Number x, const Number y, const Number z);
161 
165  template <std::size_t dummy_dim,
166  std::enable_if_t<(dim == dummy_dim) && (dummy_dim != 0), int> = 0>
167  Point(const boost::geometry::model::
168  point<Number, dummy_dim, boost::geometry::cs::cartesian> &boost_pt);
169 
178  unit_vector(const unsigned int i);
179 
185  DEAL_II_HOST_DEVICE Number
186  operator()(const unsigned int index) const;
187 
193  DEAL_II_HOST_DEVICE Number &
194  operator()(const unsigned int index);
195 
201  template <typename OtherNumber>
204 
217 
229 
240 
247  operator-() const;
248 
265  template <typename OtherNumber>
267  dim,
268  typename ProductType<Number,
269  typename EnableIfScalar<OtherNumber>::type>::type>
270  operator*(const OtherNumber) const;
271 
277  template <typename OtherNumber>
279  dim,
280  typename ProductType<Number,
281  typename EnableIfScalar<OtherNumber>::type>::type>
282  operator/(const OtherNumber) const;
283 
289  DEAL_II_HOST_DEVICE Number
291 
305  square() const;
306 
315  distance(const Point<dim, Number> &p) const;
316 
325 
335  template <class Archive>
336  void
337  serialize(Archive &ar, const unsigned int version);
338 };
339 
340 /*--------------------------- Inline functions: Point -----------------------*/
341 
342 #ifndef DOXYGEN
343 
344 // At least clang-3.7 requires us to have a user-defined constructor
345 // and we can't use 'Point<dim,Number>::Point () = default' here.
346 template <int dim, typename Number>
347 DEAL_II_CXX20_REQUIRES(dim >= 0)
348 inline DEAL_II_HOST_DEVICE Point<dim, Number>::Point() // NOLINT
349 {}
350 
351 
352 
353 template <int dim, typename Number>
354 DEAL_II_CXX20_REQUIRES(dim >= 0)
355 inline DEAL_II_HOST_DEVICE Point<dim, Number>::Point(
356  const Tensor<1, dim, Number> &t)
357  : Tensor<1, dim, Number>(t)
358 {}
359 
360 
361 
362 template <int dim, typename Number>
363 DEAL_II_CXX20_REQUIRES(dim >= 0)
364 inline DEAL_II_HOST_DEVICE Point<dim, Number>::Point(const Number x)
365 {
366  Assert(dim == 1,
367  ExcMessage(
368  "You can only initialize Point<1> objects using the constructor "
369  "that takes only one argument. Point<dim> objects with dim!=1 "
370  "require initialization with the constructor that takes 'dim' "
371  "arguments."));
372 
373  // we can only get here if we pass the assertion. use the switch anyway so
374  // as to avoid compiler warnings about uninitialized elements or writing
375  // beyond the end of the 'values' array
376  switch (dim)
377  {
378  case 1:
379  this->values[0] = x;
380  break;
381 
382  default:;
383  }
384 }
385 
386 
387 
388 template <int dim, typename Number>
389 DEAL_II_CXX20_REQUIRES(dim >= 0)
390 inline DEAL_II_HOST_DEVICE Point<dim, Number>::Point(const Number x,
391  const Number y)
392 {
393  Assert(dim == 2,
394  ExcMessage(
395  "You can only initialize Point<2> objects using the constructor "
396  "that takes two arguments. Point<dim> objects with dim!=2 "
397  "require initialization with the constructor that takes 'dim' "
398  "arguments."));
399 
400  // we can only get here if we pass the assertion. use the indirection anyway
401  // so as to avoid compiler warnings about uninitialized elements or writing
402  // beyond the end of the 'values' array
403  constexpr unsigned int y_index = (dim < 2) ? 0 : 1;
404  this->values[0] = x;
405  this->values[y_index] = y;
406 }
407 
408 
409 
410 template <int dim, typename Number>
411 DEAL_II_CXX20_REQUIRES(dim >= 0)
412 inline DEAL_II_HOST_DEVICE Point<dim, Number>::Point(const Number x,
413  const Number y,
414  const Number z)
415 {
416  Assert(dim == 3,
417  ExcMessage(
418  "You can only initialize Point<3> objects using the constructor "
419  "that takes three arguments. Point<dim> objects with dim!=3 "
420  "require initialization with the constructor that takes 'dim' "
421  "arguments."));
422 
423  // we can only get here if we pass the assertion. use the indirection anyway
424  // so as to avoid compiler warnings about uninitialized elements or writing
425  // beyond the end of the 'values' array
426  constexpr unsigned int y_index = (dim < 2) ? 0 : 1;
427  constexpr unsigned int z_index = (dim < 3) ? 0 : 2;
428  this->values[0] = x;
429  this->values[y_index] = y;
430  this->values[z_index] = z;
431 }
432 
433 
434 
435 template <int dim, typename Number>
436 DEAL_II_CXX20_REQUIRES(dim >= 0)
437 template <std::size_t dummy_dim,
438  std::enable_if_t<(dim == dummy_dim) && (dummy_dim != 0), int>>
439 inline Point<dim, Number>::Point(
440  const boost::geometry::model::
441  point<Number, dummy_dim, boost::geometry::cs::cartesian> &boost_pt)
442 {
443  Assert(dim <= 3, ExcNotImplemented());
444  this->values[0] = boost::geometry::get<0>(boost_pt);
445  constexpr unsigned int y_index = (dim < 2) ? 0 : 1;
446  constexpr unsigned int z_index = (dim < 3) ? 0 : 2;
447 
448  if (dim >= 2)
449  this->values[y_index] = boost::geometry::get<y_index>(boost_pt);
450 
451  if (dim >= 3)
452  this->values[z_index] = boost::geometry::get<z_index>(boost_pt);
453 }
454 
455 
456 
457 template <int dim, typename Number>
458 DEAL_II_CXX20_REQUIRES(dim >= 0)
459 inline DEAL_II_HOST_DEVICE Point<dim, Number> Point<dim, Number>::unit_vector(
460  unsigned int i)
461 {
463  p[i] = 1.;
464  return p;
465 }
466 
467 
468 template <int dim, typename Number>
469 DEAL_II_CXX20_REQUIRES(dim >= 0)
470 inline DEAL_II_HOST_DEVICE Number Point<dim, Number>::operator()(
471  const unsigned int index) const
472 {
473  AssertIndexRange(static_cast<int>(index), dim);
474  return this->values[index];
475 }
476 
477 
478 
479 template <int dim, typename Number>
480 DEAL_II_CXX20_REQUIRES(dim >= 0)
481 inline DEAL_II_HOST_DEVICE Number &Point<dim, Number>::operator()(
482  const unsigned int index)
483 {
484  AssertIndexRange(static_cast<int>(index), dim);
485  return this->values[index];
486 }
487 
488 
489 
490 template <int dim, typename Number>
491 DEAL_II_CXX20_REQUIRES(dim >= 0)
492 template <typename OtherNumber>
493 inline DEAL_II_ALWAYS_INLINE Point<dim, Number> &Point<dim, Number>::operator=(
494  const Tensor<1, dim, OtherNumber> &p)
495 {
497  return *this;
498 }
499 
500 
501 
502 template <int dim, typename Number>
503 DEAL_II_CXX20_REQUIRES(dim >= 0)
504 inline DEAL_II_HOST_DEVICE Point<dim, Number> Point<dim, Number>::operator+(
505  const Tensor<1, dim, Number> &p) const
506 {
507  Point<dim, Number> tmp = *this;
508  tmp += p;
509  return tmp;
510 }
511 
512 
513 
514 template <int dim, typename Number>
515 DEAL_II_CXX20_REQUIRES(dim >= 0)
516 inline DEAL_II_HOST_DEVICE Tensor<1, dim, Number> Point<dim, Number>::operator-(
517  const Point<dim, Number> &p) const
518 {
519  return (Tensor<1, dim, Number>(*this) -= p);
520 }
521 
522 
523 
524 template <int dim, typename Number>
525 DEAL_II_CXX20_REQUIRES(dim >= 0)
526 inline DEAL_II_HOST_DEVICE Point<dim, Number> Point<dim, Number>::operator-(
527  const Tensor<1, dim, Number> &p) const
528 {
529  Point<dim, Number> tmp = *this;
530  tmp -= p;
531  return tmp;
532 }
533 
534 
535 
536 template <int dim, typename Number>
537 DEAL_II_CXX20_REQUIRES(dim >= 0)
538 inline DEAL_II_HOST_DEVICE Point<dim, Number> Point<dim, Number>::operator-()
539  const
540 {
541  Point<dim, Number> result;
542  for (unsigned int i = 0; i < dim; ++i)
543  result.values[i] = -this->values[i];
544  return result;
545 }
546 
547 
548 
549 template <int dim, typename Number>
550 DEAL_II_CXX20_REQUIRES(dim >= 0)
551 template <typename OtherNumber>
553  dim,
554  typename ProductType<Number, typename EnableIfScalar<OtherNumber>::type>::
555  type> Point<dim, Number>::operator*(const OtherNumber factor) const
556 {
558  for (unsigned int i = 0; i < dim; ++i)
559  tmp[i] = this->operator[](i) * factor;
560  return tmp;
561 }
562 
563 
564 
565 template <int dim, typename Number>
566 DEAL_II_CXX20_REQUIRES(dim >= 0)
567 template <typename OtherNumber>
569  dim,
570  typename ProductType<Number, typename EnableIfScalar<OtherNumber>::type>::
571  type> Point<dim, Number>::operator/(const OtherNumber factor) const
572 {
573  const Tensor<1, dim, Number> &base_object = *this;
574  return Point<
575  dim,
576  typename ProductType<Number,
577  typename EnableIfScalar<OtherNumber>::type>::type>(
578  ::operator/(base_object, factor));
579 }
580 
581 
582 
583 template <int dim, typename Number>
584 DEAL_II_CXX20_REQUIRES(dim >= 0)
585 inline DEAL_II_HOST_DEVICE Number Point<dim, Number>::operator*(
586  const Tensor<1, dim, Number> &p) const
587 {
588  Number res = Number();
589  for (unsigned int i = 0; i < dim; ++i)
590  res += this->operator[](i) * p[i];
591  return res;
592 }
593 
594 
595 template <int dim, typename Number>
596 DEAL_II_CXX20_REQUIRES(dim >= 0)
597 inline DEAL_II_HOST_DEVICE typename numbers::NumberTraits<Number>::real_type
598  Point<dim, Number>::square() const
599 {
600  return this->norm_square();
601 }
602 
603 
604 
605 template <int dim, typename Number>
606 DEAL_II_CXX20_REQUIRES(dim >= 0)
607 inline DEAL_II_HOST_DEVICE typename numbers::NumberTraits<Number>::real_type
608  Point<dim, Number>::distance(const Point<dim, Number> &p) const
609 {
610  return std::sqrt(distance_square(p));
611 }
612 
613 
614 
615 template <int dim, typename Number>
616 DEAL_II_CXX20_REQUIRES(dim >= 0)
617 inline DEAL_II_HOST_DEVICE typename numbers::NumberTraits<Number>::real_type
618  Point<dim, Number>::distance_square(const Point<dim, Number> &p) const
619 {
621  for (unsigned int i = 0; i < dim; ++i)
622  {
623  const Number diff = static_cast<Number>(this->values[i]) - p(i);
625  }
626 
627  return sum;
628 }
629 
630 
631 
632 template <int dim, typename Number>
633 DEAL_II_CXX20_REQUIRES(dim >= 0)
634 template <class Archive>
635 inline void Point<dim, Number>::serialize(Archive &ar, const unsigned int)
636 {
637  // forward to serialization
638  // function in the base class
639  ar &static_cast<Tensor<1, dim, Number> &>(*this);
640 }
641 
642 #endif // DOXYGEN
643 
644 
645 /*--------------------------- Global functions: Point -----------------------*/
646 
647 
655 template <int dim, typename Number, typename OtherNumber>
656 inline DEAL_II_HOST_DEVICE
657  Point<dim,
658  typename ProductType<Number,
659  typename EnableIfScalar<OtherNumber>::type>::type>
660  operator*(const OtherNumber factor, const Point<dim, Number> &p)
661 {
662  return p * factor;
663 }
664 
665 
666 
672 template <int dim, typename Number>
673 DEAL_II_CXX20_REQUIRES(dim >= 0)
674 inline std::ostream &
675 operator<<(std::ostream &out, const Point<dim, Number> &p)
676 {
677  for (unsigned int i = 0; i < dim - 1; ++i)
678  out << p[i] << ' ';
679  out << p[dim - 1];
680 
681  return out;
682 }
683 
684 
685 
690 template <int dim, typename Number>
691 DEAL_II_CXX20_REQUIRES(dim >= 0)
692 inline std::istream &
693 operator>>(std::istream &in, Point<dim, Number> &p)
694 {
695  for (unsigned int i = 0; i < dim; ++i)
696  in >> p[i];
697 
698  return in;
699 }
700 
701 
702 #ifndef DOXYGEN
703 
709 template <typename Number>
710 inline std::ostream &
711 operator<<(std::ostream &out, const Point<1, Number> &p)
712 {
713  out << p[0];
714 
715  return out;
716 }
717 
718 #endif // DOXYGEN
720 
721 #endif
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:165
Definition: point.h:110
numbers::NumberTraits< Number >::real_type distance_square(const Point< dim, Number > &p) const
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const OtherNumber) const
Tensor< 1, dim, Number > operator-(const Point< dim, Number > &) const
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber) const
Number operator*(const Tensor< 1, dim, Number > &p) const
Number & operator()(const unsigned int index)
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
Point(const boost::geometry::model::point< Number, dummy_dim, boost::geometry::cs::cartesian > &boost_pt)
Point(const Tensor< 1, dim, Number > &)
Point(const Number x, const Number y, const Number z)
Point< dim, Number > & operator=(const Tensor< 1, dim, OtherNumber > &p)
Point< dim, Number > operator-(const Tensor< 1, dim, Number > &) const
Point(const Number x)
void serialize(Archive &ar, const unsigned int version)
numbers::NumberTraits< Number >::real_type square() const
Point< dim, Number > operator-() const
Point< dim, Number > operator+(const Tensor< 1, dim, Number > &) const
Point(const Number x, const Number y)
static Point< dim, Number > unit_vector(const unsigned int i)
Number operator()(const unsigned int index) const
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber factor, const Point< dim, Number > &p)
Definition: point.h:660
SymmetricTensor< rank, dim, Number > sum(const SymmetricTensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
Definition: tensor.h:516
constexpr Tensor & operator=(const Tensor< rank_, dim, OtherNumber > &rhs)
Tensor< rank_ - 1, dim, Number > values[(dim !=0) ? dim :1]
Definition: tensor.h:885
#define DEAL_II_ALWAYS_INLINE
Definition: config.h:108
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:474
#define DEAL_II_CXX20_REQUIRES(condition)
Definition: config.h:162
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:475
#define Assert(cond, exc)
Definition: exceptions.h:1586
static ::ExceptionBase & ExcNotImplemented()
#define AssertIndexRange(index, range)
Definition: exceptions.h:1827
static ::ExceptionBase & ExcMessage(std::string arg1)
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition: utilities.cc:189
#define DEAL_II_HOST_DEVICE
Definition: numbers.h:35
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
static constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE const T & value(const T &t)
Definition: numbers.h:702
static constexpr real_type abs_square(const number &x)
Definition: numbers.h:584