deal.II version GIT relicensing-2055-gd703fda1ad 2024-10-30 20:00:00+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\}}\)
Loading...
Searching...
No Matches
point.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 1998 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_point_h
16#define dealii_point_h
17
18
19#include <deal.II/base/config.h>
20
22#include <deal.II/base/tensor.h>
23
24#include <boost/geometry/core/cs.hpp>
25#include <boost/geometry/geometries/point.hpp>
26
27#include <cmath>
28
30
108template <int dim, typename Number = double>
110class Point : public Tensor<1, dim, Number>
111{
112public:
119 constexpr DEAL_II_HOST_DEVICE
121
125 constexpr explicit DEAL_II_HOST_DEVICE
127
136 constexpr explicit DEAL_II_HOST_DEVICE
137 Point(const Number x);
138
148 constexpr DEAL_II_HOST_DEVICE
149 Point(const Number x, const Number y);
150
160 constexpr DEAL_II_HOST_DEVICE
161 Point(const Number x, const Number y, const Number z);
162
166 template <std::size_t dummy_dim,
167 std::enable_if_t<(dim == dummy_dim) && (dummy_dim != 0), int> = 0>
168 constexpr Point(
169 const boost::geometry::model::
170 point<Number, dummy_dim, boost::geometry::cs::cartesian> &boost_pt);
171
180 unit_vector(const unsigned int i);
181
187 constexpr DEAL_II_HOST_DEVICE Number
188 operator()(const unsigned int index) const;
189
195 constexpr DEAL_II_HOST_DEVICE Number &
196 operator()(const unsigned int index);
197
203 template <typename OtherNumber>
204 constexpr Point<dim, Number> &
206
219
231
242
249 operator-() const;
250
267 template <typename OtherNumber>
268 constexpr DEAL_II_HOST_DEVICE Point<
269 dim,
270 typename ProductType<Number,
271 typename EnableIfScalar<OtherNumber>::type>::type>
272 operator*(const OtherNumber) const;
273
279 template <typename OtherNumber>
280 constexpr DEAL_II_HOST_DEVICE Point<
281 dim,
282 typename ProductType<Number,
283 typename EnableIfScalar<OtherNumber>::type>::type>
284 operator/(const OtherNumber) const;
285
291 constexpr DEAL_II_HOST_DEVICE Number
293
306 constexpr DEAL_II_HOST_DEVICE
308 square() const;
309
319
326 constexpr DEAL_II_HOST_DEVICE
329
339 template <class Archive>
340 void
341 serialize(Archive &ar, const unsigned int version);
342};
343
344/*--------------------------- Inline functions: Point -----------------------*/
345
346#ifndef DOXYGEN
347
348// At least clang-3.7 requires us to have a user-defined constructor
349// and we can't use 'Point<dim,Number>::Point () = default' here.
350template <int dim, typename Number>
352constexpr DEAL_II_HOST_DEVICE Point<dim, Number>::Point() // NOLINT
353{}
354
355
356
357template <int dim, typename Number>
359constexpr DEAL_II_HOST_DEVICE Point<dim, Number>::Point(
360 const Tensor<1, dim, Number> &t)
361 : Tensor<1, dim, Number>(t)
362{}
363
364
365
366template <int dim, typename Number>
368constexpr DEAL_II_HOST_DEVICE Point<dim, Number>::Point(const Number x)
369{
370 Assert(dim == 1,
372 "You can only initialize Point<1> objects using the constructor "
373 "that takes only one argument. Point<dim> objects with dim!=1 "
374 "require initialization with the constructor that takes 'dim' "
375 "arguments."));
376
377 // we can only get here if we pass the assertion. use the switch anyway so
378 // as to avoid compiler warnings about uninitialized elements or writing
379 // beyond the end of the 'values' array
380 switch (dim)
381 {
382 case 1:
383 this->values[0] = x;
384 break;
385
386 default:;
387 }
388}
389
390
391
392template <int dim, typename Number>
394constexpr DEAL_II_HOST_DEVICE Point<dim, Number>::Point(const Number x,
395 const Number y)
396{
397 Assert(dim == 2,
399 "You can only initialize Point<2> objects using the constructor "
400 "that takes two arguments. Point<dim> objects with dim!=2 "
401 "require initialization with the constructor that takes 'dim' "
402 "arguments."));
403
404 // we can only get here if we pass the assertion. use the indirection anyway
405 // so as to avoid compiler warnings about uninitialized elements or writing
406 // beyond the end of the 'values' array
407 constexpr unsigned int y_index = (dim < 2) ? 0 : 1;
408 this->values[0] = x;
409 this->values[y_index] = y;
410}
411
412
413
414template <int dim, typename Number>
416constexpr DEAL_II_HOST_DEVICE Point<dim, Number>::Point(const Number x,
417 const Number y,
418 const Number z)
419{
420 Assert(dim == 3,
422 "You can only initialize Point<3> objects using the constructor "
423 "that takes three arguments. Point<dim> objects with dim!=3 "
424 "require initialization with the constructor that takes 'dim' "
425 "arguments."));
426
427 // we can only get here if we pass the assertion. use the indirection anyway
428 // so as to avoid compiler warnings about uninitialized elements or writing
429 // beyond the end of the 'values' array
430 constexpr unsigned int y_index = (dim < 2) ? 0 : 1;
431 constexpr unsigned int z_index = (dim < 3) ? 0 : 2;
432 this->values[0] = x;
433 this->values[y_index] = y;
434 this->values[z_index] = z;
435}
436
437
438
439template <int dim, typename Number>
441template <std::size_t dummy_dim,
442 std::enable_if_t<(dim == dummy_dim) && (dummy_dim != 0), int>>
443constexpr Point<dim, Number>::Point(
444 const boost::geometry::model::
445 point<Number, dummy_dim, boost::geometry::cs::cartesian> &boost_pt)
446{
447 Assert(dim <= 3, ExcNotImplemented());
448 this->values[0] = boost::geometry::get<0>(boost_pt);
449 constexpr unsigned int y_index = (dim < 2) ? 0 : 1;
450 constexpr unsigned int z_index = (dim < 3) ? 0 : 2;
451
452 if (dim >= 2)
453 this->values[y_index] = boost::geometry::get<y_index>(boost_pt);
454
455 if (dim >= 3)
456 this->values[z_index] = boost::geometry::get<z_index>(boost_pt);
457}
458
459
460
461template <int dim, typename Number>
463constexpr DEAL_II_HOST_DEVICE
464 Point<dim, Number> Point<dim, Number>::unit_vector(unsigned int i)
465{
467 p[i] = 1.;
468 return p;
469}
470
471
472template <int dim, typename Number>
474constexpr DEAL_II_HOST_DEVICE Number Point<dim, Number>::operator()(
475 const unsigned int index) const
476{
477 AssertIndexRange(static_cast<int>(index), dim);
478 return this->values[index];
479}
480
481
482
483template <int dim, typename Number>
485constexpr DEAL_II_HOST_DEVICE Number &Point<dim, Number>::operator()(
486 const unsigned int index)
487{
488 AssertIndexRange(static_cast<int>(index), dim);
489 return this->values[index];
490}
491
492
493
494template <int dim, typename Number>
496template <typename OtherNumber>
497constexpr DEAL_II_ALWAYS_INLINE Point<dim, Number>
498 &Point<dim, Number>::operator=(const Tensor<1, dim, OtherNumber> &p)
499{
501 return *this;
502}
503
504
505
506template <int dim, typename Number>
508constexpr DEAL_II_HOST_DEVICE Point<dim, Number> Point<dim, Number>::operator+(
509 const Tensor<1, dim, Number> &p) const
510{
511 Point<dim, Number> tmp = *this;
512 tmp += p;
513 return tmp;
514}
515
516
517
518template <int dim, typename Number>
520constexpr DEAL_II_HOST_DEVICE
521 Tensor<1, dim, Number> Point<dim, Number>::operator-(
522 const Point<dim, Number> &p) const
523{
524 return (Tensor<1, dim, Number>(*this) -= p);
525}
526
527
528
529template <int dim, typename Number>
531constexpr DEAL_II_HOST_DEVICE Point<dim, Number> Point<dim, Number>::operator-(
532 const Tensor<1, dim, Number> &p) const
533{
534 Point<dim, Number> tmp = *this;
535 tmp -= p;
536 return tmp;
537}
538
539
540
541template <int dim, typename Number>
543constexpr DEAL_II_HOST_DEVICE Point<dim, Number> Point<dim, Number>::operator-()
544 const
545{
546 Point<dim, Number> result;
547 for (unsigned int i = 0; i < dim; ++i)
548 result.values[i] = -this->values[i];
549 return result;
550}
551
552
553
554template <int dim, typename Number>
556template <typename OtherNumber>
557constexpr DEAL_II_HOST_DEVICE Point<
558 dim,
559 typename ProductType<Number, typename EnableIfScalar<OtherNumber>::type>::
560 type> Point<dim, Number>::operator*(const OtherNumber factor) const
561{
563 for (unsigned int i = 0; i < dim; ++i)
564 tmp[i] = this->operator[](i) * factor;
565 return tmp;
566}
567
568
569
570template <int dim, typename Number>
572template <typename OtherNumber>
573constexpr DEAL_II_HOST_DEVICE Point<
574 dim,
575 typename ProductType<Number, typename EnableIfScalar<OtherNumber>::type>::
576 type> Point<dim, Number>::operator/(const OtherNumber factor) const
577{
578 const Tensor<1, dim, Number> &base_object = *this;
579 return Point<
580 dim,
581 typename ProductType<Number,
582 typename EnableIfScalar<OtherNumber>::type>::type>(
583 ::operator/(base_object, factor));
584}
585
586
587
588template <int dim, typename Number>
590constexpr DEAL_II_HOST_DEVICE Number Point<dim, Number>::operator*(
591 const Tensor<1, dim, Number> &p) const
592{
593 Number res = Number();
594 for (unsigned int i = 0; i < dim; ++i)
595 res += this->operator[](i) * p[i];
596 return res;
597}
598
599
600template <int dim, typename Number>
602constexpr DEAL_II_HOST_DEVICE typename numbers::NumberTraits<Number>::real_type
603 Point<dim, Number>::square() const
604{
605 return this->norm_square();
606}
607
608
609
610template <int dim, typename Number>
612inline DEAL_II_HOST_DEVICE typename numbers::NumberTraits<Number>::real_type
613 Point<dim, Number>::distance(const Point<dim, Number> &p) const
614{
615 return std::sqrt(distance_square(p));
616}
617
618
619
620template <int dim, typename Number>
622constexpr DEAL_II_HOST_DEVICE typename numbers::NumberTraits<Number>::real_type
623 Point<dim, Number>::distance_square(const Point<dim, Number> &p) const
624{
626 for (unsigned int i = 0; i < dim; ++i)
627 {
628 const Number diff = static_cast<Number>(this->values[i]) - p[i];
630 }
631
632 return sum;
633}
634
635
636
637template <int dim, typename Number>
639template <class Archive>
640inline void Point<dim, Number>::serialize(Archive &ar, const unsigned int)
641{
642 // forward to serialization
643 // function in the base class
644 ar &static_cast<Tensor<1, dim, Number> &>(*this);
645}
646
647#endif // DOXYGEN
648
649
650/*--------------------------- Global functions: Point -----------------------*/
651
652
660template <int dim, typename Number, typename OtherNumber>
661constexpr DEAL_II_HOST_DEVICE
662 Point<dim,
663 typename ProductType<Number,
664 typename EnableIfScalar<OtherNumber>::type>::type>
665 operator*(const OtherNumber factor, const Point<dim, Number> &p)
666{
667 return p * factor;
668}
669
670
671
679template <int dim, typename Number>
681inline std::ostream &
682operator<<(std::ostream &out, const Point<dim, Number> &p)
683{
684 for (unsigned int i = 0; i < dim - 1; ++i)
685 out << p[i] << ' ';
686 out << p[dim - 1];
687
688 return out;
689}
690
691
692
699template <int dim, typename Number>
701inline std::istream &
702operator>>(std::istream &in, Point<dim, Number> &p)
703{
704 for (unsigned int i = 0; i < dim; ++i)
705 in >> p[i];
706
707 return in;
708}
709
710
711#ifndef DOXYGEN
712
718template <typename Number>
719inline std::ostream &
720operator<<(std::ostream &out, const Point<1, Number> &p)
721{
722 out << p[0];
723
724 return out;
725}
726
727#endif // DOXYGEN
729
730#endif
std::ostream & operator<<(std::ostream &out, const Point< dim, Number > &p)
Definition point.h:682
Definition point.h:111
constexpr Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber) const
constexpr Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const OtherNumber) const
constexpr Tensor< 1, dim, Number > operator-(const Point< dim, Number > &) const
constexpr Point(const boost::geometry::model::point< Number, dummy_dim, boost::geometry::cs::cartesian > &boost_pt)
constexpr Point< dim, Number > & operator=(const Tensor< 1, dim, OtherNumber > &p)
constexpr numbers::NumberTraits< Number >::real_type distance_square(const Point< dim, Number > &p) const
constexpr Number & operator()(const unsigned int index)
constexpr Point(const Number x, const Number y)
constexpr Point< dim, Number > operator+(const Tensor< 1, dim, Number > &) const
constexpr Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber factor, const Point< dim, Number > &p)
Definition point.h:665
constexpr Point(const Number x, const Number y, const Number z)
static constexpr Point< dim, Number > unit_vector(const unsigned int i)
constexpr Point< dim, Number > operator-() const
constexpr Point(const Number x)
void serialize(Archive &ar, const unsigned int version)
constexpr Point(const Tensor< 1, dim, Number > &)
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
constexpr Point()
constexpr numbers::NumberTraits< Number >::real_type square() const
constexpr Number operator()(const unsigned int index) const
constexpr Point< dim, Number > operator-(const Tensor< 1, dim, Number > &) const
constexpr Number operator*(const Tensor< 1, dim, Number > &p) const
std::conditional_t< rank_==1, std::array< Number, dim >, std::array< Tensor< rank_ - 1, dim, Number >, dim > > values
Definition tensor.h:854
constexpr Tensor & operator=(const Tensor< rank_, dim, OtherNumber > &rhs)
#define DEAL_II_ALWAYS_INLINE
Definition config.h:109
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:175
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
T sum(const T &t, const MPI_Comm mpi_communicator)
STL namespace.
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
#define DEAL_II_HOST_DEVICE
Definition numbers.h:30
static constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE const T & value(const T &t)
Definition numbers.h:697
static constexpr real_type abs_square(const number &x)
Definition numbers.h:579