CEPS  24.01
Cardiac ElectroPhysiology Simulator
CepsVertex.cpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
37 
39 CepsVertex (const CepsReal &x, const CepsReal &y, const CepsReal &z) :
40  m_coor ({x, y, z})
41 {}
42 
44 CepsVertex (const CepsReal3D &coor) :
45  m_coor (coor)
46 {}
47 
48 CepsVertex::
49 operator CepsMathVertex () const
50 {
51  return CepsMathVertex (x (), y (), z ());
52 }
53 
54 // Coordinates
56 setX (const CepsReal &x)
57 {
58  m_coor[0] = x;
59  return;
60 }
61 
63 setY (const CepsReal &y)
64 {
65  m_coor[1] = y;
66  return;
67 }
68 
70 setZ (const CepsReal &z)
71 {
72  m_coor[2] = z;
73  return;
74 }
75 
77 setCoordinate (const CepsUInt& dim, const CepsReal &coor)
78 {
79  #ifdef CEPS_DEBUG_ENABLED
80  CEPS_ABORT_IF(dim>2, "Cannot set vertex coordinate in dimension " << dim);
81  #endif
82  m_coor[dim] = coor;
83  return;
84 }
85 
87 setCoordinates (const CepsReal &x, const CepsReal &y, const CepsReal &z)
88 {
89  m_coor = {x, y, z};
90  return;
91 }
92 
94 setCoordinates (const CepsReal3D &coors)
95 {
96  m_coor = coors;
97  return;
98 }
99 
101 setCoordinates (const CepsReal *coors, const CepsUInt &n)
102 {
103  for (CepsUInt dim = 0; dim < 3U; ++dim)
104  if (n > dim)
105  m_coor[dim] = coors[dim];
106 
107  return;
108 }
109 
111 scale (const CepsReal &scaleFactor)
112 {
113  #ifdef CEPS_DEBUG_ENABLED
114  CEPS_WARNS_IF (
115  std::fabs (scaleFactor) < FLOATING_POINT_EPSILON,
116  "Scaling vertex coordinates by very small factor : " << scaleFactor
117  );
118  #endif
119  m_coor[0] *= scaleFactor;
120  m_coor[1] *= scaleFactor;
121  m_coor[2] *= scaleFactor;
122  return;
123 }
124 
126 x () const
127 {
128  return m_coor[0];
129 }
130 
132 y () const
133 {
134  return m_coor[1];
135 }
136 
138 z () const
139 {
140  return m_coor[2];
141 }
142 
144 getCoordinate (const CepsSize &dim)
145 {
146  #ifdef CEPS_DEBUG_ENABLED
147  CEPS_ABORT_IF (dim>2, "Invalid coordinate index: " << dim);
148  #endif
149  return m_coor[dim];
150 }
151 
153 getCoordinate (const CepsSize &dim) const
154 {
155  #ifdef CEPS_DEBUG_ENABLED
156  CEPS_ABORT_IF (dim>2, "Invalid coordinate index: " << dim);
157  #endif
158  return m_coor[dim];
159 }
160 
162 operator[] (const CepsUInt& dim)
163 {
164  return getCoordinate(dim);
165 }
166 
168 operator[] (const CepsUInt& dim) const
169 {
170  return getCoordinate(dim);
171 }
172 
175 {
176  return m_coor;
177 }
178 
180 getCoordinates () const
181 {
182  return m_coor;
183 }
184 
186 getCoordinatesForEigen () const
187 {
188  return static_cast<const CepsMathVertex &> (*this); // call operator CepsMathVertices
189 }
190 
192 equals (const CepsVertex &vertex, const CepsReal &errorFactor)
193 {
194  return (
195  ceps::approxEquals (m_coor[0], vertex.x (), errorFactor) and
196  ceps::approxEquals (m_coor[1], vertex.y (), errorFactor) and
197  ceps::approxEquals (m_coor[2], vertex.z (), errorFactor)
198  );
199 }
200 
202 norm2 (const CepsSize &dim) const
203 {
204  const CepsReal &_x = x ();
205  const CepsReal &_y = y ();
206  const CepsReal &_z = z ();
207 
208  switch (dim)
209  {
210  case 1U: return abs (_x);
211  case 2U: return std::sqrt (_x * _x + _y * _y);
212  case 3U: return sqrt (_x * _x + _y * _y + _z * _z);
213  }
214  return 0.0;
215 }
216 
218 normp (const CepsInt &p, const CepsSize &dim) const
219 {
220  if (dim == 0)
221  return 0.;
222 
223  if (p < 0)
224  {
225  CepsReal val = std::abs (m_coor[0]);
226  for (CepsUInt i = 1; i < dim; ++i)
227  val = (std::abs (m_coor[i]) > val ? std::abs (m_coor[i]) : val);
228 
229  return val;
230  }
231 
232  CepsReal val = 0.0;
233  for (CepsUInt i = 0; i < dim; ++i) {
234  val += std::pow (std::abs(m_coor[i]), p);
235  }
236  return std::pow (val, 1.0 / p);
237 }
238 
240 dot (const CepsVertex &other, const CepsSize &dim) const
241 {
242  switch (dim)
243  {
244  case 1U: return x () * other.x ();
245  case 2U: return x () * other.x () + y () * other.y ();
246  case 3U: return x () * other.x () + y () * other.y () + z () * other.z ();
247  }
248  return 0.0;
249 }
250 
251 // Display vertex
252 
253 std::ostream &
254 operator<< (std::ostream &os, const CepsVertex &vertex)
255 {
256  os << "(" << std::setprecision (CEPS_IO_DECIMAL_PRECISION) << vertex.x () << ", " << vertex.y () << ", "
257  << vertex.z () << ")";
258  return os;
259 }
260 
261 CepsReal
262 dotProduct (const CepsVertex &a, const CepsVertex &b, const CepsSize &dim)
263 {
264  switch (dim)
265  {
266  case 1U: return a.x () * b.x ();
267  case 2U: return a.x () * b.x () + a.y () * b.y ();
268  case 3U: return a.x () * b.x () + a.y () * b.y () + a.z () * b.z ();
269  }
270  return 0.0;
271 }
#define CEPS_WARNS_IF(condition, message)
If condition is true, writes a warning in the debug log and in the terminal (stderr).
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
#define CEPS_IO_DECIMAL_PRECISION
Length of floats on std outputs and log files.
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
size_t CepsSize
Size unsigned.
Definition: CepsTypes.hpp:126
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
Eigen::Matrix< CepsScalar, 3, 1 > CepsMathVertex
Vertex, eigen format.
Definition: CepsTypes.hpp:135
CepsArray3< CepsReal > CepsReal3D
Three real scalars, used like this for compatibility in polynomials.
Definition: CepsTypes.hpp:178
int32_t CepsInt
Need 32 bit integer.
Definition: CepsTypes.hpp:106
#define FLOATING_POINT_EPSILON
Definition: CepsTypes.hpp:117
CepsReal dotProduct(const CepsVertex &a, const CepsVertex &b, const CepsSize &dim)
Scalar product. dim restricts which components are used (1:(x), 2:(x,y), 3:(x,y,z))
Definition: CepsVertex.cpp:262
std::ostream & operator<<(std::ostream &os, const CepsVertex &vertex)
Display stream operator.
Definition: CepsVertex.cpp:254
CepsVertex(const CepsReal &x=0.0, const CepsReal &y=0.0, const CepsReal &z=0.0)
Constructor with coordinates.
Definition: CepsVertex.cpp:39
CepsReal & operator[](const CepsUInt &dim)
Get coordinate of dimension 0 1 2, read & write.
Definition: CepsVertex.cpp:162
void setX(const CepsReal &x)
Set vertex x coordinate.
Definition: CepsVertex.cpp:56
CepsMathVertex getCoordinatesForEigen() const
Get three coordinates.
Definition: CepsVertex.cpp:186
const CepsReal & x() const
Vertex x coordinate.
Definition: CepsVertex.cpp:126
void setCoordinates(const CepsReal &x, const CepsReal &y=0., const CepsReal &z=0.)
Set the 3 coordinates at once.
Definition: CepsVertex.cpp:87
CepsReal & getCoordinate(const CepsSize &dim)
Get coordinate of dimension 0 1 2, read & write.
Definition: CepsVertex.cpp:144
CepsReal dot(const CepsVertex &other, const CepsSize &dim=3) const
Scalar product. dim restricts which components are used (1:(x), 2:(x,y), 3:(x,y,z))
Definition: CepsVertex.cpp:240
void setY(const CepsReal &y)
Set vertex y coordinate.
Definition: CepsVertex.cpp:63
void scale(const CepsReal &scaleFactor)
Scale all coordinates of this node.
Definition: CepsVertex.cpp:111
const CepsReal & y() const
Vertex y coordinate.
Definition: CepsVertex.cpp:132
void setCoordinate(const CepsUInt &dim, const CepsReal &coor)
Set the three coordinates.
Definition: CepsVertex.cpp:77
CepsReal norm2(const CepsSize &dim=3) const
Euclidian norm of coordinates.
Definition: CepsVertex.cpp:202
CepsReal normp(const CepsInt &p, const CepsSize &dim=3) const
Norm p (for p =-1, 1, 2, etc) with p=-1 gives the inf norm.
Definition: CepsVertex.cpp:218
void setZ(const CepsReal &z)
Set vertex z coordinate.
Definition: CepsVertex.cpp:70
const CepsReal & z() const
Vertex z coordinate.
Definition: CepsVertex.cpp:138
CepsReal3D m_coor
Definition: CepsVertex.hpp:201
CepsReal3D & getCoordinates()
Get three coordinates, read & write.
Definition: CepsVertex.cpp:174
CepsBool equals(const CepsVertex &vert, const CepsReal &errorFactor=1.0)
Check for coordinates equality.
Definition: CepsVertex.cpp:192
CepsBool approxEquals(CepsReal a, CepsReal b, CepsReal epsilon)
Approximate equality with epsilon tolerance.
Definition: Precision.hpp:67