Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/gprim/geomobjects.hpp
3206 views
1
#ifndef FILE_OBJECTS
2
#define FILE_OBJECTS
3
4
/* *************************************************************************/
5
/* File: geomobjects.hpp */
6
/* Author: Joachim Schoeberl */
7
/* Date: 20. Jul. 02 */
8
/* *************************************************************************/
9
10
11
12
template <int D> class Vec;
13
template <int D> class Point;
14
15
16
template <int D>
17
class Point
18
{
19
20
protected:
21
double x[D];
22
23
public:
24
Point () { ; }
25
Point (double ax) { x[0] = ax; }
26
Point (double ax, double ay) { x[0] = ax; x[1] = ay; }
27
Point (double ax, double ay, double az)
28
{ x[0] = ax; x[1] = ay; x[2] = az; }
29
Point (double ax, double ay, double az, double au)
30
{ x[0] = ax; x[1] = ay; x[2] = az; x[3] = au;}
31
32
Point (const Point<D> & p2)
33
{ for (int i = 0; i < D; i++) x[i] = p2.x[i]; }
34
35
explicit Point (const Vec<D> & v)
36
{ for (int i = 0; i < D; i++) x[i] = v(i); }
37
38
39
Point & operator= (const Point<D> & p2)
40
{
41
for (int i = 0; i < D; i++) x[i] = p2.x[i];
42
return *this;
43
}
44
45
Point & operator= (double val)
46
{
47
for (int i = 0; i < D; i++) x[i] = val;
48
return *this;
49
}
50
51
double & operator() (int i) { return x[i]; }
52
const double & operator() (int i) const { return x[i]; }
53
54
operator const double* () const { return x; }
55
};
56
57
58
59
60
61
template <int D>
62
class Vec
63
{
64
65
protected:
66
double x[D];
67
68
public:
69
Vec () { ; } // for (int i = 0; i < D; i++) x[i] = 0; }
70
Vec (double ax) { for (int i = 0; i < D; i++) x[i] = ax; }
71
Vec (double ax, double ay) { x[0] = ax; x[1] = ay; }
72
Vec (double ax, double ay, double az)
73
{ x[0] = ax; x[1] = ay; x[2] = az; }
74
Vec (double ax, double ay, double az, double au)
75
{ x[0] = ax; x[1] = ay; x[2] = az; x[3] = au; }
76
77
Vec (const Vec<D> & p2)
78
{ for (int i = 0; i < D; i++) x[i] = p2.x[i]; }
79
80
explicit Vec (const Point<D> & p)
81
{ for (int i = 0; i < D; i++) x[i] = p(i); }
82
83
Vec (const Vec<D> & p1, const Vec<D> & p2)
84
{ for(int i=0; i<D; i++) x[i] = p2(i)-p1(1); }
85
86
87
88
Vec & operator= (const Vec<D> & p2)
89
{
90
for (int i = 0; i < D; i++) x[i] = p2.x[i];
91
return *this;
92
}
93
94
Vec & operator= (double s)
95
{
96
for (int i = 0; i < D; i++) x[i] = s;
97
return *this;
98
}
99
100
double & operator() (int i) { return x[i]; }
101
const double & operator() (int i) const { return x[i]; }
102
103
operator const double* () const { return x; }
104
105
double Length () const
106
{
107
double l = 0;
108
for (int i = 0; i < D; i++)
109
l += x[i] * x[i];
110
return sqrt (l);
111
}
112
113
double Length2 () const
114
{
115
double l = 0;
116
for (int i = 0; i < D; i++)
117
l += x[i] * x[i];
118
return l;
119
}
120
121
const Vec<D> & Normalize ()
122
{
123
double l = Length();
124
if (l != 0)
125
for (int i = 0; i < D; i++)
126
x[i] /= l;
127
return *this;
128
}
129
130
Vec<D> GetNormal () const;
131
};
132
133
134
135
136
137
template <int H, int W=H>
138
class Mat
139
{
140
141
protected:
142
double x[H*W];
143
144
public:
145
Mat () { ; }
146
Mat (const Mat & b)
147
{ for (int i = 0; i < H*W; i++) x[i] = b.x[i]; }
148
149
Mat & operator= (double s)
150
{
151
for (int i = 0; i < H*W; i++) x[i] = s;
152
return *this;
153
}
154
155
Mat & operator= (const Mat & b)
156
{
157
for (int i = 0; i < H*W; i++) x[i] = b.x[i];
158
return *this;
159
}
160
161
double & operator() (int i, int j) { return x[i*W+j]; }
162
const double & operator() (int i, int j) const { return x[i*W+j]; }
163
double & operator() (int i) { return x[i]; }
164
const double & operator() (int i) const { return x[i]; }
165
166
Vec<H> Col (int i) const
167
{
168
Vec<H> hv;
169
for (int j = 0; j < H; j++)
170
hv(j) = x[j*W+i];
171
return hv;
172
}
173
174
Vec<W> Row (int i) const
175
{
176
Vec<W> hv;
177
for (int j = 0; j < W; j++)
178
hv(j) = x[i*W+j];
179
return hv;
180
}
181
182
void Solve (const Vec<H> & rhs, Vec<W> & sol) const
183
{
184
Mat<W,H> inv;
185
CalcInverse (*this, inv);
186
sol = inv * rhs;
187
}
188
};
189
190
191
192
193
template <int D>
194
class Box
195
{
196
protected:
197
Point<D> pmin, pmax;
198
public:
199
Box () { ; }
200
Box ( const Point<D> & p1, const Point<D> & p2)
201
{
202
for (int i = 0; i < D; i++)
203
{
204
pmin(i) = min2(p1(i), p2(i));
205
pmax(i) = max2(p1(i), p2(i));
206
}
207
}
208
209
enum EB_TYPE { EMPTY_BOX = 1 };
210
Box ( EB_TYPE et )
211
{
212
pmin = Point<3> (1e99, 1e99, 1e99);
213
pmax = Point<3> (-1e99, -1e99, -1e99);
214
}
215
216
const Point<D> & PMin () const { return pmin; }
217
const Point<D> & PMax () const { return pmax; }
218
219
void Set (const Point<D> & p)
220
{ pmin = pmax = p; }
221
222
void Add (const Point<D> & p)
223
{
224
for (int i = 0; i < D; i++)
225
{
226
if (p(i) < pmin(i)) pmin(i) = p(i);
227
else if (p(i) > pmax(i)) pmax(i) = p(i);
228
}
229
}
230
231
Point<D> Center () const
232
{
233
Point<D> c;
234
for (int i = 0; i < D; i++)
235
c(i) = 0.5 * (pmin(i)+pmax(i));
236
return c;
237
}
238
double Diam () const { return Abs (pmax-pmin); }
239
240
Point<D> GetPointNr (int nr) const
241
{
242
Point<D> p;
243
for (int i = 0; i < D; i++)
244
{
245
p(i) = (nr & 1) ? pmax(i) : pmin(i);
246
nr >>= 1;
247
}
248
return p;
249
}
250
251
252
bool Intersect (const Box<D> & box2) const
253
{
254
for (int i = 0; i < D; i++)
255
if (pmin(i) > box2.pmax(i) ||
256
pmax(i) < box2.pmin(i)) return 0;
257
return 1;
258
}
259
260
261
bool IsIn (const Point<D> & p) const
262
{
263
for (int i = 0; i < D; i++)
264
if (p(i) < pmin(i) || p(i) > pmax(i)) return 0;
265
return 1;
266
}
267
268
269
void Increase (double dist)
270
{
271
for (int i = 0; i < D; i++)
272
{
273
pmin(i) -= dist;
274
pmax(i) += dist;
275
}
276
}
277
};
278
279
280
281
282
template <int D>
283
class BoxSphere : public Box<D>
284
{
285
protected:
286
///
287
Point<D> c;
288
///
289
double diam;
290
///
291
double inner;
292
public:
293
///
294
BoxSphere () { };
295
///
296
BoxSphere (const Box<D> & box)
297
: Box<D> (box)
298
{
299
CalcDiamCenter();
300
};
301
302
///
303
BoxSphere ( Point<D> apmin, Point<D> apmax )
304
: Box<D> (apmin, apmax)
305
{
306
CalcDiamCenter();
307
}
308
309
///
310
const Point<D> & Center () const { return c; }
311
///
312
double Diam () const { return diam; }
313
///
314
double Inner () const { return inner; }
315
316
317
///
318
void GetSubBox (int nr, BoxSphere & sbox) const
319
{
320
for (int i = 0; i < D; i++)
321
{
322
if (nr & 1)
323
{
324
sbox.pmin(i) = c(i);
325
sbox.pmax(i) = this->pmax(i);
326
}
327
else
328
{
329
sbox.pmin(i) = this->pmin(i);
330
sbox.pmax(i) = c(i);
331
}
332
sbox.c(i) = 0.5 * (sbox.pmin(i) + sbox.pmax(i));
333
nr >>= 1;
334
}
335
sbox.diam = 0.5 * diam;
336
sbox.inner = 0.5 * inner;
337
}
338
339
340
///
341
void CalcDiamCenter ()
342
{
343
c = Box<D>::Center ();
344
diam = Dist (this->pmin, this->pmax);
345
346
inner = this->pmax(0) - this->pmin(0);
347
for (int i = 1; i < D; i++)
348
if (this->pmax(i) - this->pmin(i) < inner)
349
inner = this->pmax(i) - this->pmin(i);
350
}
351
352
};
353
354
355
356
357
358
359
#endif
360
361