SignedDistFunctors.h 15.9 KB
Newer Older
1
2
3
4
5
6
7
/** \file SignedDistFunctors.h */

#ifndef SIGNED_DIST_FUNCTORS_H
#define SIGNED_DIST_FUNCTORS_H

#include "VectorOperations.h"
#include "GeometryTools.h"
8
#include "Views.h"
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

using namespace std;
using namespace AMDiS; 

/**
 * A collection of signed-dist function describing several gemoetric objects:
 *   - Circle(radius [, center])
 *   - PerturbedCircle(radius [, center])
 *   - TwoCircles(radius, center1, center2)
 *   - InverseCircle(radius [, center])
 *   - InverseCircles(radius, vector(centers))
 *   - Rectangle(width, height [, center])
 *   - Ellipse(a,b [, center])
 *   - Lemniskate(width, center)
 *   - Plane(shift)
 *   - PlaneRotation(shift, angle)
 *   - Propeller(radius, angle)
 *   - Torus(radius1, radius2)
27
 *   - Polygon(v0, v1, v2, v3), or Polygon(vec(v_i))
28
29
30
31
32
33
34
 **/

// signed distance functions for surfaces in polar coords
static double signedDist2D(const WorldVector<double> x, const WorldVector<double> midPoint, 
			     AbstractFunction<double, double> *radius, double eps)
{
  FUNCNAME("signedDist2D");
35
  using vector_operations::norm;
36
  
37
38
39
40
41
42
43
44
45
46
47
  WorldVector<double> x_trans;
  double norm_xy;
  double alpha;

  TEST_EXIT(x.getSize() == 2)("Dimension of world must be 2 to use signedDist2D-function. Use signedDist3D instead!");

  x_trans.fill(0.0);
  for (int k = 0; k < x.getSize(); k++) {
    x_trans[k] = x[k] - midPoint[k];
  }

48
  norm_xy = norm(x_trans, eps);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  
  if (x_trans[0] >= 0.0) {
    alpha = acos(x_trans[0] / norm_xy);
  } else {
    alpha = 2.0*M_PI - acos(x_trans[0] / norm_xy);
  }
  
  return (norm_xy - (*radius)(alpha));
};

static double signedDist3D(const WorldVector<double> x, const WorldVector<double> midPoint, 
			     BinaryAbstractFunction<double, double, double> *radius, double eps)
{
  FUNCNAME("signedDist3D");
63
  using vector_operations::norm;
64
  
65
66
67
68
69
70
71
72
73
74
75
  WorldVector<double> x_trans;
  double norm_xyz, norm_xy;
  double alpha, beta;
  
  TEST_EXIT(x.getSize() == 3)("Dimension of world must be 3 to use signedDist3D-function. Use signedDist2D instead!");
  
  x_trans.fill(0.0);
  for (int k = 0; k < x.getSize(); k++) {
    x_trans[k] = x[k] - midPoint[k];
  }

76
  norm_xyz = norm(x_trans, eps);
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  norm_xy = sqrt(sqr(x_trans[0]) + sqr(x_trans[1]) + sqr(eps));

  if(x_trans[1]>=0) {
    alpha = acos(x_trans[0]/norm_xy);
  } else {
    alpha = 2.0*M_PI - acos(x_trans[0]/norm_xy);
  }
  beta = 0.5*M_PI - atan(x_trans[2]/norm_xy);

  return (norm_xyz - (*radius)(alpha, beta)); 
};

// radius-functions
// ================

class CircleRadius : public AbstractFunction<double, double>
{
public:
  CircleRadius(double radius_) : radius(radius_) {};
  double operator()(const double& alpha) const { return radius; };
protected:
  double radius;
};

class BallRadius : public BinaryAbstractFunction<double, double, double>
{
public:
  BallRadius(double radius_) : radius(radius_) {};
  double operator()(const double &alpha, const double &beta) const { return radius; };
protected:
  double radius;
};

class PerturbedCircleRadius : public AbstractFunction<double, double>
{
public:

  PerturbedCircleRadius(const double radius_, const double strength_, const int period_) : 
    radius(radius_), strength(strength_), rotation(0.0), period(period_) {};
  PerturbedCircleRadius(const double radius_, const double strength_, const int period_, const double rotation_) : 
    radius(radius_), strength(strength_), rotation(rotation_), period(period_) {};
  double operator()(const double &alpha) const
  {
    double result = 1.0 + strength * cos(period * alpha + rotation);
    result *= radius;
    return result;
  };

protected:

  double radius;
  double strength;
  double rotation;
  int period;
};

class PerturbedBallRadius : public BinaryAbstractFunction<double, double, double>
{
public:

  PerturbedBallRadius(const double radius_, const double strength_, const int period_) : 
    radius(radius_), strength(strength_), period(period_) {};
  double operator()(const double &alpha, const double &beta) const
  {
    double result = 1.0 + strength * (cos(period*alpha) + cos(period*beta) * cos(period*alpha));
    result *= radius;
    return result;
  };

protected:

  double radius;
  double strength;
  int period;
};

// lemniscate of Bernoulli
class LemniskateRadius : public AbstractFunction<double, double>
{
public:

  LemniskateRadius(const double breite_) : breite(breite_) {};
  double operator()(const double &alpha) const
  {
    double result = 2*cos(2*alpha);
    result = breite*result;
    return result;
  };

protected:

  double breite;
};

// lemniscate of Bernoulli in 3D
class Lemniskate3D : public BinaryAbstractFunction<double, double, double>
{
public:

  Lemniskate3D(const double breite_) : breite(breite_) {};
  double operator()(const double &alpha, const double &beta) const
  {
    double result = 2*breite*cos(2*alpha)*sin(beta);
    return result;
  };

protected:

  double breite;
};

class EllipseRadius : public AbstractFunction<double, double>
{
public:

  EllipseRadius(const double a_, const double b_) : 
    a(a_), b(b_) {};
  double operator()(const double &alpha) const
  {
    double x=b*cos(alpha), y=a*sin(alpha);
    return a*b / sqrt(x*x+y*y);
  };

protected:

  double a,b;
};

class EllipsoidRadius : public BinaryAbstractFunction<double, double, double>
{
public:

  EllipsoidRadius(const double a_, const double b_, const double c_) : 
    a(a_), b(b_), c(c_) {};
  double operator()(const double &alpha, const double &beta) const
  {
    double sa = sin(alpha), sb = sin(beta);
    double ca = cos(alpha), cb = cos(beta);
    double x = a*sa*cb, y = b*sa*sb, z = c*ca;
    
    return sqrt(x*x+y*y+z*z);
  };

protected:

  double a,b,c;
};

// =============================================================

class PlaneRotation : public AbstractFunction<double, WorldVector<double> > 
{
public:

  PlaneRotation(double shift_, double theta_, double factor_=1.0) :
    shift(shift_), theta(theta_), factor(factor_) {}
  double operator()(const WorldVector<double>& x) const
  {
    double x_temp;
    x_temp= cos(theta)*x[0] - sin(theta)*x[1];
//     x_temp[1]= sin(theta)*x[0] + cos(theta)*x[1]; 
    return factor*(shift-x_temp);
  };

private:

  double shift;
  int comp;
  double theta;
  double factor;
};

class Plane : public AbstractFunction<double, WorldVector<double> > 
{
public:

Praetorius, Simon's avatar
Praetorius, Simon committed
253
  Plane(double shift_, double factor_=1.0, int component_=1) :
254
    shift(shift_), component(component_), factor(factor_) {}
255
256
  double operator()(const WorldVector<double>& x) const
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
257
    return factor*(shift-x[component]);
258
259
260
261
262
  };

private:

  double shift;
Praetorius, Simon's avatar
Praetorius, Simon committed
263
  int component;
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  double factor;
};

class One : public AbstractFunction<double, WorldVector<double> > 
{
public:
  double operator()(const WorldVector<double>& x) const
  {
    return 1.0;
  };
};

class Rectangle : public AbstractFunction<double, WorldVector<double> > 
{
public:

  Rectangle(double width_, double height_) : width(width_), height(height_) {shift.set(0.0);only2D();}
  Rectangle(double width_, double height_, WorldVector<double> shift_) : width(width_), height(height_),shift(shift_) {only2D();}
  double operator()(const WorldVector<double>& x0) const
  {
    WorldVector<double> x=x0-shift;
    double result=0.0;
    if(abs(x[0])<width/2.0 && abs(x[1])<height/2.0) {
      result = -std::min(abs(abs(x[0])-width/2.0),abs(abs(x[1])-height/2.0));
    } else if(abs(x[0])<width/2.0) {
      result = abs(abs(x[1])-height/2.0);
    } else if(abs(x[1])<height/2.0) {
      result = abs(abs(x[0])-width/2.0);
    } else {
      WorldVector<double> corner;
      corner[0] = abs(x[0])-width/2.0;
      corner[1] = abs(x[1])-height/2.0;
      result = norm(corner);
    }
    return result;
  };
  void only2D() { TEST_EXIT(shift.getSize()==2)("Rectangle is defined only for 2 dimension!\n"); }

private:

  double width,height;
  WorldVector<double> shift;
};

// simple circle: phi_0(x,y) := sqrt(x^2+y^2)-r
class Circle : public AbstractFunction<double, WorldVector<double> > 
{
public:

  Circle(double radius_, WorldVector<double> midPoint_, double factor_ = 1.0) :
    radius(radius_), midPoint(midPoint_), factor(factor_) {}
  Circle(double radius_, double factor_ = 1.0) :
    radius(radius_), factor(factor_) { midPoint.set(0.0); }
  double operator()(const WorldVector<double>& x) const
  {
    double result = 0.0;

    for(int k=0; k<x.getSize(); k++) {
      result += sqr(x[k]-midPoint[k]);
    }
    result = sqrt(result)-radius;
    return factor * result;
  };

private:

  double radius;
  WorldVector<double> midPoint;
  double factor;
};

class InverseCircle : public AbstractFunction<double, WorldVector<double> > 
{
public:

  InverseCircle(double radius_, WorldVector<double> midPoint_) :
    radius(radius_), midPoint(midPoint_) {}
  InverseCircle(double radius_) :
    radius(radius_) { midPoint.set(0.0); }
  double operator()(const WorldVector<double>& x) const
  {
    double result = 0.0;

    for(int k=0; k<x.getSize(); k++) {
      result += sqr(x[k]-midPoint[k]);
    }
    result = sqrt(result)-radius;
    return -result;
  };

private:

  double radius;
  WorldVector<double> midPoint;
};


class InverseCircles : public AbstractFunction<double, WorldVector<double> >
{
public:
  InverseCircles(double radius_, std::vector<WorldVector<double> > pos_) :
	  radius(radius_), pos(pos_) {}
  InverseCircles(double radius_) :
	  radius(radius_)
  {
    WorldVector<double> center; center.set(0.0);
    pos.push_back(center);
  }

  double operator()(const WorldVector<double>& x) const
  {
	  std::vector<double> result(pos.size(),0.0);

	  for(int k=0; k<x.getSize(); k++) {
378
	    for (size_t j=0; j<pos.size(); j++) {
379
380
381
382
	      result[j] += sqr(x[k]-pos[j][k]);
	    }
	  }
	  double minResult = 1.e10;
383
	  for (size_t j=0; j<pos.size(); j++) {
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
	    minResult = std::min(minResult, sqrt(result[j])-radius);
	  }
	  return -minResult;
  }
private:
  double radius;
  std::vector<WorldVector<double> > pos;
};


class TwoCircles : public AbstractFunction<double, WorldVector<double> > 
{
public:

  TwoCircles(double radius_, WorldVector<double> pos1_, WorldVector<double> pos2_) :
    radius(radius_), pos1(pos1_), pos2(pos2_) {}
  double operator()(const WorldVector<double>& x) const
  {
    double result1=0.0, result2=0.0;

    for(int k=0; k<x.getSize(); k++) {
      result1 += sqr(x[k]-pos1[k]);
      result2 += sqr(x[k]-pos2[k]);
    }
    result1 = sqrt(result1)-radius;
    result2 = sqrt(result2)-radius;
    return -std::min(result1,result2);
  };

private:

  double radius;
  WorldVector<double> pos1,pos2;
};

// perturbed circle
class PertCircle : public AbstractFunction<double, WorldVector<double> > 
{
public:

  PertCircle(double radius_, double strength_, int period_) : 
    radius(radius_), strength(strength_), period(period_) { midPoint.set(0.0); }
  PertCircle(double radius_, double strength_, int period_, WorldVector<double> midPoint_) : 
    radius(radius_), strength(strength_), period(period_), midPoint(midPoint_) {}
  double operator()(const WorldVector<double>& x) const
  {
    double result=0.0;
    if(x.getSize()==2) {
      PerturbedCircleRadius perturbedCircle(radius, strength, period);
      result = signedDist2D(x, midPoint, &perturbedCircle, 1.e-6);
    } else {
      PerturbedBallRadius perturbedBall(radius, strength, period);
      result = signedDist3D(x, midPoint, &perturbedBall, 1.e-6);
    }
    return result;
  };

private:

  double radius;
  double strength;
  int period;
  WorldVector<double> midPoint;
};

class Propeller : public AbstractFunction<double, WorldVector<double> > 
{
public:

  Propeller(double radius_, double rotation_) : 
    radius(radius_), rotation(rotation_) { midPoint.set(0.0); }
  Propeller(WorldVector<double> midPoint_, double radius_, double rotation_) : 
    radius(radius_), rotation(rotation_),midPoint(midPoint_) {}
  double operator()(const WorldVector<double>& x) const
  {
    double result=0.0;
Praetorius, Simon's avatar
Praetorius, Simon committed
460
    PerturbedCircleRadius perturbedCircle(radius, 0.7, 3, rotation);
461
462
    result = signedDist2D(x, midPoint, &perturbedCircle, 1.e-6);
    return -result;
Praetorius, Simon's avatar
Praetorius, Simon committed
463
464
465
466
467
468
  }
  
  void setRotation(double rotation_)
  {
    rotation = rotation_;
  }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575

private:

  double radius;
  double rotation;
  WorldVector<double> midPoint;
};

class Lemniskate : public AbstractFunction<double, WorldVector<double> > 
{
public:

  Lemniskate(double breite_, WorldVector<double> midPoint_) : 
    breite(breite_), midPoint(midPoint_) {}
  double operator()(const WorldVector<double>& x) const
  {
    double result=0.0;
    if(x.getSize()==2) {
      LemniskateRadius lemniskate(breite);
      result = signedDist2D(x, midPoint, &lemniskate, 1.e-6);
    } else {
      Lemniskate3D lemniskate(breite);
      result = signedDist3D(x, midPoint, &lemniskate, 1.e-6);
    }
    return result;
  };

private:

  double breite;
  WorldVector<double> midPoint;
};

class Ellipse : public AbstractFunction<double, WorldVector<double> > 
{
public:

  Ellipse(double a_, double b_) :
    a(a_), b(b_), c(0.0) { midPoint.set(0.0); }
  Ellipse(double a_, double b_, WorldVector<double> midPoint_) :
    a(a_), b(b_), c(0.0), midPoint(midPoint_) {}
  Ellipse(double a_, double b_, double c_) :
    a(a_), b(b_), c(c_) { midPoint.set(0.0); }
  Ellipse(double a_, double b_, double c_, WorldVector<double> midPoint_) :
    a(a_), b(b_), c(c_), midPoint(midPoint_) {}
  double operator()(const WorldVector<double>& x) const
  {
    double result = sqr(x[0]-midPoint[0])/sqr(a);
    result+= sqr(x[1]-midPoint[1])/sqr(b);
    if(x.getSize()>2) {
      result+= sqr(x[2]-midPoint[2])/sqr(c);
    }
    result = 1.0-sqrt(result);
    return result;
  };

private:

  double a,b,c;
  WorldVector<double> midPoint;
};

class Torus : public AbstractFunction<double, WorldVector<double> > 
{
public:

  Torus(double r1_, double r2_) :
    r1(r1_), r2(r2_) { midPoint.set(0.0); only3D(); }
  Torus(double r1_, double r2_, WorldVector<double> midPoint_) :
    r1(r1_), r2(r2_), midPoint(midPoint_) { only3D(); }
  void only3D() { TEST_EXIT(midPoint.getSize()==3)("Torus is defined in 3D only!\n"); }
  double operator()(const WorldVector<double>& x) const
  {
    double result=0.0;
    result = sqr(x[0]-midPoint[0])+sqr(x[1]-midPoint[1]);
    result = sqr(sqrt(result)-r1)+ sqr(x[2]-midPoint[2]);
    result = sqrt(result)-r2;
    
    return result;
  };

private:

  double r1,r2;
  WorldVector<double> midPoint;
};


class Polygon : public AbstractFunction<double, WorldVector<double> >
{
public:

  Polygon(WorldVector<double> x0_, WorldVector<double> x1_, WorldVector<double> x2_, WorldVector<double> x3_, double factor_=1.0) : factor(factor_)
  {
    vertices.push_back(x0_);
    vertices.push_back(x1_);
    vertices.push_back(x2_);
    vertices.push_back(x3_);
    vertices.push_back(x0_);
  }

  Polygon(std::vector<WorldVector<double> > xi_, double factor_=1.0) : vertices(xi_), factor(factor_) { }

  double operator()(const WorldVector<double>& x) const
  {
    double result = 1.e15;
    for (size_t i = 0; i < vertices.size()-1; i++)
576
577
      result = std::min(result, meshconv2::distance_point_line_2d(x.begin(), vertices[i].begin(), vertices[i+1].begin()));
    return factor * result * (meshconv2::point_in_polygon(x.begin(), vertices) ? -1.0 : 1.0);
578
579
580
581
582
583
584
  }

  void refine(int np)
  {
    std::vector<WorldVector<double> > newVertices;

    for (size_t i = 0; i < vertices.size()-1; i++) {
585
      for (int j = 0; j < np-1; j++) {
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
	double lambda = static_cast<double>(j)/static_cast<double>(np - 1.0);
	WorldVector<double> p = lambda*vertices[i+1] + (1.0-lambda)*vertices[i];
	newVertices.push_back(p);
      }
    }
    swap(vertices, newVertices);
  }

  void move(const DOFVector<WorldVector<double> >* velocity)
  {
    for (size_t i = 0; i < vertices.size()-1; i++) {
      WorldVector<double> shift = evalAtPoint(*velocity, vertices[i]);
      vertices[i] += shift;
    }
    vertices[vertices.size()-1] = vertices[0];
  }

  void move(AbstractFunction<WorldVector<double>, WorldVector<double> >* velocity)
  {
    for (size_t i = 0; i < vertices.size()-1; i++) {
      WorldVector<double> shift = (*velocity)(vertices[i]);
      vertices[i] += shift;
    }
    vertices[vertices.size()-1] = vertices[0];
  }

private:
  std::vector<WorldVector<double> > vertices;
  double factor;
};

#endif // SIGNED_DIST_FUNCTORS_H