gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Numeric.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef NUMERIC_H
7 #define NUMERIC_H
8 
9 #include <cmath>
10 #include <vector>
11 
12 #include "SPoint2.h"
13 #include "SPoint3.h"
14 #include "SVector3.h"
15 
16 template <class scalar> class fullVector;
17 
18 template <class T> inline double myhypot(const T &a, const T &b)
19 {
20  return std::sqrt(a * a + b * b);
21 }
22 
23 template <class T> inline double hypotenuse(T const &a, T const &b, T const &c)
24 {
25  return std::sqrt(a * a + b * b + c * c);
26 }
27 
28 template <typename T> inline int gmsh_sign(T const &value)
29 {
30  return (T(0) < value) - (value < T(0));
31 }
32 
33 struct mean_plane {
34  double plan[3][3];
35  double a, b, c, d;
36  double x, y, z;
37 };
38 
39 inline double pow_int(const double &a, const int &n)
40 {
41  if(n < 0) return pow_int(1 / a, -n);
42 
43  switch(n) {
44  case 0: return 1.0;
45  case 1: return a;
46  case 2: return a * a;
47  case 3: return a * a * a;
48  case 4: {
49  const double a2 = a * a;
50  return a2 * a2;
51  }
52  case 5: {
53  const double a2 = a * a;
54  return a2 * a2 * a;
55  }
56  case 6: {
57  const double a3 = a * a * a;
58  return a3 * a3;
59  }
60  case 7: {
61  const double a3 = a * a * a;
62  return a3 * a3 * a;
63  }
64  case 8: {
65  const double a2 = a * a;
66  const double a4 = a2 * a2;
67  return a4 * a4;
68  }
69  case 9: {
70  const double a3 = a * a * a;
71  return a3 * a3 * a3;
72  }
73  case 10: {
74  const double a2 = a * a;
75  const double a4 = a2 * a2;
76  return a4 * a4 * a2;
77  }
78  default: return pow_int(a, n - 10) * pow_int(a, 10);
79  }
80 }
81 
82 inline double pow_int(const double &a, const double &d)
83 {
84  // Round double !
85  int n = static_cast<int>(d + .5);
86  return pow_int(a, n);
87 }
88 
89 double myatan2(double a, double b);
90 double myasin(double a);
91 double myacos(double a);
92 
93 inline double crossProd(double a[3], double b[3], int i)
94 {
95  int i1 = (i + 1) % 3;
96  int i2 = (i + 2) % 3;
97  return a[i1] * b[i2] - a[i2] * b[i1];
98 }
99 
100 inline double scalProd(double a[3], double b[3])
101 {
102  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
103 }
104 
105 inline void prodve(double a[3], double b[3], double c[3])
106 {
107  c[2] = a[0] * b[1] - a[1] * b[0];
108  c[1] = -a[0] * b[2] + a[2] * b[0];
109  c[0] = a[1] * b[2] - a[2] * b[1];
110 }
111 
112 inline double prosca(double const a[3], double const b[3])
113 {
114  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
115 }
116 
117 void matvec(double mat[3][3], double vec[3], double res[3]);
118 void matmat(double mat1[3][3], double mat2[3][3], double res[3][3]);
119 inline double norm3(double a[3])
120 {
121  return sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
122 }
123 inline double norme(double a[3])
124 {
125  const double mod = norm3(a);
126  if(mod != 0.0) {
127  const double one_over_mod = 1. / mod;
128  a[0] *= one_over_mod;
129  a[1] *= one_over_mod;
130  a[2] *= one_over_mod;
131  }
132  return mod;
133 }
134 double norm2(double a[3][3]);
135 
136 void normal3points(double x0, double y0, double z0, double x1, double y1,
137  double z1, double x2, double y2, double z2, double n[3]);
138 void normal2points(double x0, double y0, double z0, double x1, double y1,
139  double z1, double n[3]);
140 int sys2x2(double mat[2][2], double b[2], double res[2]);
141 int sys3x3(double mat[3][3], double b[3], double res[3], double *det);
142 int sys3x3_with_tol(double mat[3][3], double b[3], double res[3], double *det);
143 double det2x2(double mat[2][2]);
144 double det2x3(double mat[2][3]);
145 double det3x3(double mat[3][3]);
146 double trace3x3(double mat[3][3]);
147 double trace2(double mat[3][3]);
148 double inv3x3(double mat[3][3], double inv[3][3]);
149 double inv2x2(double mat[2][2], double inv[2][2]);
150 double angle_02pi(double A3);
151 double angle_plan(double v[3], double p1[3], double p2[3], double n[3]);
152 double triangle_area(double p0[3], double p1[3], double p2[3]);
153 double triangle_area2d(double p0[2], double p1[2], double p2[2]);
154 void circumCenterXY(double *p1, double *p2, double *p3, double *res);
155 void circumCenterXYZ(double *p1, double *p2, double *p3, double *res,
156  double *uv = nullptr);
157 // operate a transformation on the 4 points of a Quad in 3D, to have an
158 // equivalent Quad in 2D
159 void planarQuad_xyz2xy(double *x, double *y, double *z, double *xn, double *yn);
160 // compute the radius of the circle that is tangent to the 3 edges defined by 4
161 // points edge_1=(x0,y0)->(x1,y1); edge_2=(x1,y1)->(x2,y2);
162 // edge_3=(x2,y2)->(x3,y3)
163 double computeInnerRadiusForQuad(double *x, double *y, int i);
164 char float2char(float f);
165 float char2float(char c);
166 void eigenvalue2x2(double mat[2][2], double v[2]);
167 void eigenvalue(double mat[3][3], double re[3]);
168 void FindCubicRoots(const double coeff[4], double re[3], double im[3]);
169 void eigsort(double d[3]);
170 void gradSimplex(double *x, double *y, double *z, double *v, double *grad);
171 double ComputeVonMises(double *val);
172 double ComputeScalarRep(int numComp, double *val, int tensorRep = 0);
173 void invert_singular_matrix3x3(double MM[3][3], double II[3][3]);
174 bool newton_fd(bool (*func)(fullVector<double> &, fullVector<double> &, void *),
175  fullVector<double> &x, void *data, double relax = 1.,
176  double tolx = 1.e-6);
177 
178 void signedDistancePointTriangle(const SPoint3 &p1, const SPoint3 &p2,
179  const SPoint3 &p3, const SPoint3 &p, double &d,
180  SPoint3 &closePt);
181 
182 void signedDistancesPointsTriangle(std::vector<double> &distances,
183  std::vector<SPoint3> &closePts,
184  const std::vector<SPoint3> &pts,
185  const SPoint3 &p1, const SPoint3 &p2,
186  const SPoint3 &p3);
187 
188 void signedDistancePointLine(const SPoint3 &p1, const SPoint3 &p2,
189  const SPoint3 &p, double &distance,
190  SPoint3 &closePt);
191 void signedDistancesPointsLine(std::vector<double> &distances,
192  std::vector<SPoint3> &closePts,
193  const std::vector<SPoint3> &pts,
194  const SPoint3 &p1, const SPoint3 &p2);
195 
196 void changeReferential(const int direction, const SPoint3 &p,
197  const SPoint3 &closePt, const SPoint3 &p1,
198  const SPoint3 &p2, double *xp, double *yp,
199  double *otherp, double *x, double *y, double *other);
200 int computeDistanceRatio(const double &y, const double &yp, const double &x,
201  const double &xp, double *distance, const double &r1,
202  const double &r2);
203 
204 void signedDistancesPointsEllipsePoint(std::vector<double> &distances,
205  std::vector<double> &distancesE,
206  std::vector<int> &isInYarn,
207  std::vector<SPoint3> &closePts,
208  const std::vector<SPoint3> &pts,
209  const SPoint3 &p1, const SPoint3 &p2,
210  const double radius);
211 
213  std::vector<double> &distances, std::vector<double> &distancesE,
214  std::vector<int> &isInYarn, std::vector<SPoint3> &closePts,
215  const std::vector<SPoint3> &pts, const SPoint3 &p1, const SPoint3 &p2,
216  const double maxA, const double minA, const double maxB, const double minB,
217  const int typeLevelSet);
218 
219 int intersection_segments(const SPoint3 &p1, const SPoint3 &p2,
220  const SPoint3 &q1, const SPoint3 &q2, double x[2]);
221 int intersection_segments(const SPoint2 &p1, const SPoint2 &p2,
222  const SPoint2 &q1, const SPoint2 &q2, double x[2]);
223 
224 // tools for projection onto plane
225 void fillMeanPlane(double res[4], double t1[3], double t2[3],
226  mean_plane &meanPlane);
227 void computeMeanPlaneSimple(const std::vector<SPoint3> &points,
228  mean_plane &meanPlane);
229 void projectPointToPlane(const SPoint3 &pt, SPoint3 &ptProj,
230  const mean_plane &meanPlane);
231 void projectPointsToPlane(const std::vector<SPoint3> &pts,
232  std::vector<SPoint3> &ptsProj,
233  const mean_plane &meanPlane);
234 void transformPointsIntoOrthoBasis(const std::vector<SPoint3> &ptsProj,
235  std::vector<SPoint3> &pointsUV,
236  const SPoint3 &ptCG,
237  const mean_plane &meanPlane);
238 
239 bool catenary(double x0, double x1, double y0, double y1, double ys, int N,
240  double *yp);
241 
242 #endif
det2x2
double det2x2(double mat[2][2])
Definition: Numeric.cpp:199
triangle_area
double triangle_area(double p0[3], double p1[3], double p2[3])
Definition: Numeric.cpp:293
computeDistanceRatio
int computeDistanceRatio(const double &y, const double &yp, const double &x, const double &xp, double *distance, const double &r1, const double &r2)
Definition: Numeric.cpp:997
sys3x3_with_tol
int sys3x3_with_tol(double mat[3][3], double b[3], double res[3], double *det)
Definition: Numeric.cpp:177
mean_plane::d
double d
Definition: Numeric.h:35
gmsh_sign
int gmsh_sign(T const &value)
Definition: Numeric.h:28
catenary
bool catenary(double x0, double x1, double y0, double y1, double ys, int N, double *yp)
Definition: Numeric.cpp:1554
mean_plane::c
double c
Definition: Numeric.h:35
trace2
double trace2(double mat[3][3])
Definition: Numeric.cpp:135
distance
double distance(MVertex *v1, MVertex *v2)
Definition: MVertex.h:245
mean_plane::x
double x
Definition: Numeric.h:36
fullVector
Definition: MElement.h:26
SPoint2
Definition: SPoint2.h:12
c
static double c(int i, int j, fullMatrix< double > &CA, const std::vector< SPoint3 > &P, const std::vector< SPoint3 > &Q)
Definition: discreteFrechetDistance.cpp:15
a4
#define a4
Definition: GaussQuadratureTet.cpp:11
circumCenterXYZ
void circumCenterXYZ(double *p1, double *p2, double *p3, double *res, double *uv=nullptr)
Definition: Numeric.cpp:342
signedDistancesPointsEllipsePoint
void signedDistancesPointsEllipsePoint(std::vector< double > &distances, std::vector< double > &distancesE, std::vector< int > &isInYarn, std::vector< SPoint3 > &closePts, const std::vector< SPoint3 > &pts, const SPoint3 &p1, const SPoint3 &p2, const double radius)
Definition: Numeric.cpp:1105
SPoint3
Definition: SPoint3.h:14
LegendrePolynomials::f
void f(int n, double u, double *val)
Definition: orthogonalBasis.cpp:77
scalProd
double scalProd(double a[3], double b[3])
Definition: Numeric.h:100
SVector3.h
signedDistancesPointsLine
void signedDistancesPointsLine(std::vector< double > &distances, std::vector< SPoint3 > &closePts, const std::vector< SPoint3 > &pts, const SPoint3 &p1, const SPoint3 &p2)
Definition: Numeric.cpp:923
mean_plane::b
double b
Definition: Numeric.h:35
gradSimplex
void gradSimplex(double *x, double *y, double *z, double *v, double *grad)
Definition: Numeric.cpp:475
myasin
double myasin(double a)
Definition: Numeric.cpp:18
mean_plane
Definition: Numeric.h:33
eigenvalue
void eigenvalue(double mat[3][3], double re[3])
Definition: Numeric.cpp:550
normal3points
void normal3points(double x0, double y0, double z0, double x1, double y1, double z1, double x2, double y2, double z2, double n[3])
Definition: Numeric.cpp:76
norm3
double norm3(double a[3])
Definition: Numeric.h:119
fillMeanPlane
void fillMeanPlane(double res[4], double t1[3], double t2[3], mean_plane &meanPlane)
Definition: Numeric.cpp:1412
angle_02pi
double angle_02pi(double A3)
Definition: Numeric.cpp:255
projectPointToPlane
void projectPointToPlane(const SPoint3 &pt, SPoint3 &ptProj, const mean_plane &meanPlane)
Definition: Numeric.cpp:1497
signedDistancePointTriangle
void signedDistancePointTriangle(const SPoint3 &p1, const SPoint3 &p2, const SPoint3 &p3, const SPoint3 &p, double &d, SPoint3 &closePt)
Definition: Numeric.cpp:758
newton_fd
bool newton_fd(bool(*func)(fullVector< double > &, fullVector< double > &, void *), fullVector< double > &x, void *data, double relax=1., double tolx=1.e-6)
Definition: Numeric.cpp:685
angle_plan
double angle_plan(double v[3], double p1[3], double p2[3], double n[3])
Definition: Numeric.cpp:267
pow_int
double pow_int(const double &a, const int &n)
Definition: Numeric.h:39
eigenvalue2x2
void eigenvalue2x2(double mat[2][2], double v[2])
Definition: Numeric.cpp:538
normal2points
void normal2points(double x0, double y0, double z0, double x1, double y1, double z1, double n[3])
Definition: Numeric.cpp:85
signedDistancesPointsEllipseLine
void signedDistancesPointsEllipseLine(std::vector< double > &distances, std::vector< double > &distancesE, std::vector< int > &isInYarn, std::vector< SPoint3 > &closePts, const std::vector< SPoint3 > &pts, const SPoint3 &p1, const SPoint3 &p2, const double maxA, const double minA, const double maxB, const double minB, const int typeLevelSet)
Definition: Numeric.cpp:1139
crossProd
double crossProd(double a[3], double b[3], int i)
Definition: Numeric.h:93
float2char
char float2char(float f)
Definition: Numeric.cpp:452
mean_plane::a
double a
Definition: Numeric.h:35
mean_plane::z
double z
Definition: Numeric.h:36
projectPointsToPlane
void projectPointsToPlane(const std::vector< SPoint3 > &pts, std::vector< SPoint3 > &ptsProj, const mean_plane &meanPlane)
Definition: Numeric.cpp:1514
prosca
double prosca(double const a[3], double const b[3])
Definition: Numeric.h:112
triangle_area2d
double triangle_area2d(double p0[2], double p1[2], double p2[2])
Definition: Numeric.cpp:309
signedDistancesPointsTriangle
void signedDistancesPointsTriangle(std::vector< double > &distances, std::vector< SPoint3 > &closePts, const std::vector< SPoint3 > &pts, const SPoint3 &p1, const SPoint3 &p2, const SPoint3 &p3)
Definition: Numeric.cpp:822
circumCenterXY
void circumCenterXY(double *p1, double *p2, double *p3, double *res)
Definition: Numeric.cpp:317
trace3x3
double trace3x3(double mat[3][3])
Definition: Numeric.cpp:133
prodve
void prodve(double a[3], double b[3], double c[3])
Definition: Numeric.h:105
a2
const double a2
Definition: GaussQuadratureHex.cpp:12
myacos
double myacos(double a)
Definition: Numeric.cpp:28
inv3x3
double inv3x3(double mat[3][3], double inv[3][3])
Definition: Numeric.cpp:232
transformPointsIntoOrthoBasis
void transformPointsIntoOrthoBasis(const std::vector< SPoint3 > &ptsProj, std::vector< SPoint3 > &pointsUV, const SPoint3 &ptCG, const mean_plane &meanPlane)
Definition: Numeric.cpp:1524
changeReferential
void changeReferential(const int direction, const SPoint3 &p, const SPoint3 &closePt, const SPoint3 &p1, const SPoint3 &p2, double *xp, double *yp, double *otherp, double *x, double *y, double *other)
Definition: Numeric.cpp:942
matmat
void matmat(double mat1[3][3], double mat2[3][3], double res[3][3])
Definition: Numeric.cpp:54
invert_singular_matrix3x3
void invert_singular_matrix3x3(double MM[3][3], double II[3][3])
Definition: Numeric.cpp:650
mean_plane::y
double y
Definition: Numeric.h:36
norm2
double norm2(double a[3][3])
Definition: Numeric.cpp:38
char2float
float char2float(char c)
Definition: Numeric.cpp:464
eigsort
void eigsort(double d[3])
Definition: Numeric.cpp:634
intersection_segments
int intersection_segments(const SPoint3 &p1, const SPoint3 &p2, const SPoint3 &q1, const SPoint3 &q2, double x[2])
3D VERSION
Definition: Numeric.cpp:1358
sys3x3
int sys3x3(double mat[3][3], double b[3], double res[3], double *det)
Definition: Numeric.cpp:147
myhypot
double myhypot(const T &a, const T &b)
Definition: Numeric.h:18
mean_plane::plan
double plan[3][3]
Definition: Numeric.h:34
ComputeVonMises
double ComputeVonMises(double *val)
Definition: Numeric.cpp:496
signedDistancePointLine
void signedDistancePointLine(const SPoint3 &p1, const SPoint3 &p2, const SPoint3 &p, double &distance, SPoint3 &closePt)
Definition: Numeric.cpp:908
z
const double z
Definition: GaussQuadratureQuad.cpp:56
computeInnerRadiusForQuad
double computeInnerRadiusForQuad(double *x, double *y, int i)
Definition: Numeric.cpp:407
det3x3
double det3x3(double mat[3][3])
Definition: Numeric.cpp:126
hypotenuse
double hypotenuse(T const &a, T const &b, T const &c)
Definition: Numeric.h:23
direction
static void direction(Vertex *v1, Vertex *v2, double d[3])
Definition: Geo.cpp:218
det2x3
double det2x3(double mat[2][3])
Definition: Numeric.cpp:204
myatan2
double myatan2(double a, double b)
Definition: Numeric.cpp:12
computeMeanPlaneSimple
void computeMeanPlaneSimple(const std::vector< SPoint3 > &points, mean_plane &meanPlane)
Definition: Numeric.cpp:1438
inv2x2
double inv2x2(double mat[2][2], double inv[2][2])
Definition: Numeric.cpp:214
norme
double norme(double a[3])
Definition: Numeric.h:123
ComputeScalarRep
double ComputeScalarRep(int numComp, double *val, int tensorRep=0)
Definition: Numeric.cpp:506
sys2x2
int sys2x2(double mat[2][2], double b[2], double res[2])
Definition: Numeric.cpp:101
SPoint3.h
matvec
void matvec(double mat[3][3], double vec[3], double res[3])
Definition: Numeric.cpp:47
planarQuad_xyz2xy
void planarQuad_xyz2xy(double *x, double *y, double *z, double *xn, double *yn)
Definition: Numeric.cpp:376
SPoint2.h
FindCubicRoots
void FindCubicRoots(const double coeff[4], double re[3], double im[3])
Definition: Numeric.cpp:574