Reference documentation for deal.II version 9.6.0
Searching...
No Matches
geometric_utilities.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
4// Copyright (C) 2016 - 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
18
19#include <array>
20#include <cmath>
21#include <limits>
22
23
25
26
27namespace GeometricUtilities
28{
29 namespace Coordinates
30 {
32 double,
33 << "The radius <" << arg1 << "> can not be negative.");
34
36 double,
37 << "The azimuth angle <" << arg1 << "> is not in [0,2Pi).");
38
40 double,
41 << "The polar angle <" << arg1 << "> is not in [0,Pi].");
42
43
44 template <int dim>
45 std::array<double, dim>
46 to_spherical(const Point<dim> &position)
47 {
48 std::array<double, dim> scoord{};
49 Assert(dim > 1, ExcNotImplemented());
50
52 if constexpr (dim > 1)
53 {
54 scoord[0] = position.norm();
55 // azimuth angle \theta:
56 scoord[1] = std::atan2(position[1], position[0]);
57 // correct to [0,2*pi)
58 if (scoord[1] < 0.0)
59 scoord[1] += 2.0 * numbers::PI;
60 }
61
62 // polar angle \phi:
63 if constexpr (dim == 3)
64 {
65 // acos returns the angle in the range [0,\pi]
66 if (scoord[0] > std::numeric_limits<double>::min())
67 scoord[2] = std::acos(position[2] / scoord[0]);
68 else
69 scoord[2] = 0.0;
70 }
71 return scoord;
72 }
73
74 template <std::size_t dim>
76 from_spherical(const std::array<double, dim> &scoord)
77 {
78 Point<dim> ccoord;
79
81
82 Assert(scoord[1] >= 0. && scoord[1] < 2. * numbers::PI,
83 SphericalAzimuth(scoord[1]));
84
85 switch (dim)
86 {
87 case 2:
88 {
89 ccoord[0] = scoord[0] * std::cos(scoord[1]);
90 ccoord[1] = scoord[0] * std::sin(scoord[1]);
91 break;
92 }
93 case 3:
94 {
95 Assert(scoord[2] >= 0. && scoord[2] <= numbers::PI,
96 SphericalPolar(scoord[2]));
97
98 ccoord[0] = scoord[0] * std::sin(scoord[2]) * std::cos(scoord[1]);
99 ccoord[1] = scoord[0] * std::sin(scoord[2]) * std::sin(scoord[1]);
100 ccoord[2] = scoord[0] * std::cos(scoord[2]);
101 break;
102 }
103 default:
105 break;
106 }
107
108 return ccoord;
109 }
110
111 // explicit instantiations
112#include "geometric_utilities.inst"
113
114 } // namespace Coordinates
115} // namespace GeometricUtilities
116
Definition point.h:111
numbers::NumberTraits< Number >::real_type norm() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
static ::ExceptionBase & SphericalAzimuth(double arg1)