forked from gradientspace/geometry3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplicit3d.cs
679 lines (535 loc) · 16.4 KB
/
Implicit3d.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
253
254
255
256
257
258
259
260
261
262
263
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
378
379
380
381
382
383
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
460
461
462
463
464
465
466
467
468
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
576
577
578
579
580
581
582
583
584
585
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
using System;
using System.Collections.Generic;
namespace g3
{
/// <summary>
/// Minimalist implicit function interface
/// </summary>
public interface ImplicitFunction3d
{
double Value(ref Vector3d pt);
}
/// <summary>
/// Bounded implicit function has a bounding box within which
/// the "interesting" part of the function is contained
/// (eg the surface)
/// </summary>
public interface BoundedImplicitFunction3d : ImplicitFunction3d
{
AxisAlignedBox3d Bounds();
}
/// <summary>
/// Implicit sphere, where zero isocontour is at Radius
/// </summary>
public class ImplicitSphere3d : BoundedImplicitFunction3d
{
public Vector3d Origin;
public double Radius;
public double Value(ref Vector3d pt)
{
return pt.Distance(ref Origin) - Radius;
}
public AxisAlignedBox3d Bounds()
{
return new AxisAlignedBox3d(Origin, Radius);
}
}
/// <summary>
/// Implicit half-space. "Inside" is opposite of Normal direction.
/// </summary>
public class ImplicitHalfSpace3d : BoundedImplicitFunction3d
{
public Vector3d Origin;
public Vector3d Normal;
public double Value(ref Vector3d pt)
{
return (pt - Origin).Dot(Normal);
}
public AxisAlignedBox3d Bounds()
{
return new AxisAlignedBox3d(Origin, MathUtil.Epsilon);
}
}
/// <summary>
/// Implicit axis-aligned box
/// </summary>
public class ImplicitAxisAlignedBox3d : BoundedImplicitFunction3d
{
public AxisAlignedBox3d AABox;
public double Value(ref Vector3d pt)
{
return AABox.SignedDistance(pt);
}
public AxisAlignedBox3d Bounds()
{
return AABox;
}
}
/// <summary>
/// Implicit oriented box
/// </summary>
public class ImplicitBox3d : BoundedImplicitFunction3d
{
Box3d box;
AxisAlignedBox3d local_aabb;
AxisAlignedBox3d bounds_aabb;
public Box3d Box {
get { return box; }
set {
box = value;
local_aabb = new AxisAlignedBox3d(
-Box.Extent.x, -Box.Extent.y, -Box.Extent.z,
Box.Extent.x, Box.Extent.y, Box.Extent.z);
bounds_aabb = box.ToAABB();
}
}
public double Value(ref Vector3d pt)
{
double dx = (pt - Box.Center).Dot(Box.AxisX);
double dy = (pt - Box.Center).Dot(Box.AxisY);
double dz = (pt - Box.Center).Dot(Box.AxisZ);
return local_aabb.SignedDistance(new Vector3d(dx, dy, dz));
}
public AxisAlignedBox3d Bounds()
{
return bounds_aabb;
}
}
/// <summary>
/// Implicit tube around line segment
/// </summary>
public class ImplicitLine3d : BoundedImplicitFunction3d
{
public Segment3d Segment;
public double Radius;
public double Value(ref Vector3d pt)
{
double d = Math.Sqrt(Segment.DistanceSquared(pt));
return d - Radius;
}
public AxisAlignedBox3d Bounds()
{
Vector3d o = Radius * Vector3d.One, p0 = Segment.P0, p1 = Segment.P1;
AxisAlignedBox3d box = new AxisAlignedBox3d(p0 - o, p0 + o);
box.Contain(p1 - o);
box.Contain(p1 + o);
return box;
}
}
/// <summary>
/// Offset the zero-isocontour of an implicit function.
/// Assumes that negative is inside, if not, reverse offset.
/// </summary>
public class ImplicitOffset3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public double Offset;
public double Value(ref Vector3d pt)
{
return A.Value(ref pt) - Offset;
}
public AxisAlignedBox3d Bounds()
{
AxisAlignedBox3d box = A.Bounds();
box.Expand(Offset);
return box;
}
}
/// <summary>
/// remaps values so that values within given interval are negative,
/// and values outside this interval are positive. So, for a distance
/// field, this converts single isocontour into two nested isocontours
/// with zeros at interval a and b, with 'inside' in interval
/// </summary>
public class ImplicitShell3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public Interval1d Inside;
public double Value(ref Vector3d pt)
{
double f = A.Value(ref pt);
if (f < Inside.a)
f = Inside.a - f;
else if (f > Inside.b)
f = f - Inside.b;
else f = -Math.Min(Math.Abs(f - Inside.a), Math.Abs(f - Inside.b));
return f;
}
public AxisAlignedBox3d Bounds()
{
AxisAlignedBox3d box = A.Bounds();
box.Expand(Math.Max(0, Inside.b));
return box;
}
}
/// <summary>
/// Boolean Union of two implicit functions, A OR B.
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitUnion3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
public double Value(ref Vector3d pt)
{
return Math.Min(A.Value(ref pt), B.Value(ref pt));
}
public AxisAlignedBox3d Bounds()
{
var box = A.Bounds();
box.Contain(B.Bounds());
return box;
}
}
/// <summary>
/// Boolean Intersection of two implicit functions, A AND B
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitIntersection3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
public double Value(ref Vector3d pt)
{
return Math.Max(A.Value(ref pt), B.Value(ref pt));
}
public AxisAlignedBox3d Bounds()
{
// [TODO] intersect boxes
var box = A.Bounds();
box.Contain(B.Bounds());
return box;
}
}
/// <summary>
/// Boolean Difference/Subtraction of two implicit functions A-B = A AND (NOT B)
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitDifference3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
public double Value(ref Vector3d pt)
{
return Math.Max(A.Value(ref pt), -B.Value(ref pt));
}
public AxisAlignedBox3d Bounds()
{
// [TODO] can actually subtract B.Bounds() here...
return A.Bounds();
}
}
/// <summary>
/// Boolean Union of N implicit functions, A OR B.
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitNaryUnion3d : BoundedImplicitFunction3d
{
public List<BoundedImplicitFunction3d> Children;
public double Value(ref Vector3d pt)
{
double f = Children[0].Value(ref pt);
int N = Children.Count;
for (int k = 1; k < N; ++k)
f = Math.Min(f, Children[k].Value(ref pt));
return f;
}
public AxisAlignedBox3d Bounds()
{
var box = Children[0].Bounds();
int N = Children.Count;
for (int k = 1; k < N; ++k)
box.Contain(Children[k].Bounds());
return box;
}
}
/// <summary>
/// Boolean Intersection of N implicit functions, A AND B.
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitNaryIntersection3d : BoundedImplicitFunction3d
{
public List<BoundedImplicitFunction3d> Children;
public double Value(ref Vector3d pt)
{
double f = Children[0].Value(ref pt);
int N = Children.Count;
for (int k = 1; k < N; ++k)
f = Math.Max(f, Children[k].Value(ref pt));
return f;
}
public AxisAlignedBox3d Bounds()
{
var box = Children[0].Bounds();
int N = Children.Count;
for (int k = 1; k < N; ++k) {
box = box.Intersect(Children[k].Bounds());
}
return box;
}
}
/// <summary>
/// Boolean Difference of N implicit functions, A - Union(B1..BN)
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitNaryDifference3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public List<BoundedImplicitFunction3d> BSet;
public double Value(ref Vector3d pt)
{
double fA = A.Value(ref pt);
int N = BSet.Count;
if (N == 0)
return fA;
double fB = BSet[0].Value(ref pt);
for (int k = 1; k < N; ++k)
fB = Math.Min(fB, BSet[k].Value(ref pt));
return Math.Max(fA, -fB);
}
public AxisAlignedBox3d Bounds()
{
// [TODO] could actually subtract other bounds here...
return A.Bounds();
}
}
/// <summary>
/// Continuous R-Function Boolean Union of two implicit functions, A OR B.
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitSmoothUnion3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
const double mul = 1.0 / 1.5;
public double Value(ref Vector3d pt) {
double fA = A.Value(ref pt);
double fB = B.Value(ref pt);
return mul * (fA + fB - Math.Sqrt(fA*fA + fB*fB - fA*fB));
}
public AxisAlignedBox3d Bounds() {
var box = A.Bounds();
box.Contain(B.Bounds());
return box;
}
}
/// <summary>
/// Continuous R-Function Boolean Intersection of two implicit functions, A-B = A AND (NOT B)
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitSmoothIntersection3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
const double mul = 1.0 / 1.5;
public double Value(ref Vector3d pt) {
double fA = A.Value(ref pt);
double fB = B.Value(ref pt);
return mul * (fA + fB + Math.Sqrt(fA*fA + fB*fB - fA*fB));
}
public AxisAlignedBox3d Bounds() {
var box = A.Bounds();
box.Contain(B.Bounds());
return box;
}
}
/// <summary>
/// Continuous R-Function Boolean Difference of two implicit functions, A AND B
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class ImplicitSmoothDifference3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
const double mul = 1.0 / 1.5;
public double Value(ref Vector3d pt) {
double fA = A.Value(ref pt);
double fB = -B.Value(ref pt);
return mul * (fA + fB + Math.Sqrt(fA*fA + fB*fB - fA*fB));
}
public AxisAlignedBox3d Bounds() {
var box = A.Bounds();
box.Contain(B.Bounds());
return box;
}
}
/// <summary>
/// Blend of two implicit surfaces. Assumes surface is at zero iscontour.
/// Uses Pasko blend from http://www.hyperfun.org/F-rep.pdf
/// </summary>
public class ImplicitBlend3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
/// <summary>Weight on implicit A</summary>
public double WeightA {
get { return weightA; }
set { weightA = MathUtil.Clamp(value, 0.00001, 100000); }
}
double weightA = 0.01;
/// <summary>Weight on implicit B</summary>
public double WeightB {
get { return weightB; }
set { weightB = MathUtil.Clamp(value, 0.00001, 100000); }
}
double weightB = 0.01;
/// <summary>Blending power</summary>
public double Blend {
get { return blend; }
set { blend = MathUtil.Clamp(value, 0.0, 100000); }
}
double blend = 2.0;
public double ExpandBounds = 0.25;
public double Value(ref Vector3d pt)
{
double fA = A.Value(ref pt);
double fB = B.Value(ref pt);
double sqr_sum = fA*fA + fB*fB;
if (sqr_sum > 1e12)
return Math.Min(fA, fB);
double wa = fA/weightA, wb = fB/weightB;
double b = blend / (1.0 + wa*wa + wb*wb);
//double a = 0.5;
//return (1.0/(1.0+a)) * (fA + fB - Math.Sqrt(fA*fA + fB*fB - 2*a*fA*fB)) - b;
return 0.666666 * (fA + fB - Math.Sqrt(sqr_sum - fA*fB)) - b;
}
public AxisAlignedBox3d Bounds()
{
var box = A.Bounds();
box.Contain(B.Bounds());
box.Expand(ExpandBounds * box.MaxDim );
return box;
}
}
/*
* Skeletal implicit ops
*/
/// <summary>
/// This class converts the interval [-falloff,falloff] to [0,1],
/// Then applies Wyvill falloff function (1-t^2)^3.
/// The result is a skeletal-primitive-like shape with
/// the distance=0 isocontour lying just before midway in
/// the range (at the .ZeroIsocontour constant)
/// </summary>
public class DistanceFieldToSkeletalField : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d DistanceField;
public double FalloffDistance;
public const double ZeroIsocontour = 0.421875;
public AxisAlignedBox3d Bounds()
{
AxisAlignedBox3d bounds = DistanceField.Bounds();
bounds.Expand(FalloffDistance);
return bounds;
}
public double Value(ref Vector3d pt)
{
double d = DistanceField.Value(ref pt);
if (d > FalloffDistance)
return 0;
else if (d < -FalloffDistance)
return 1.0;
double a = (d + FalloffDistance) / (2 * FalloffDistance);
double t = 1 - (a * a);
return t * t * t;
}
}
/// <summary>
/// sum-blend
/// </summary>
public class SkeletalBlend3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
public double Value(ref Vector3d pt)
{
return A.Value(ref pt) + B.Value(ref pt);
}
public AxisAlignedBox3d Bounds()
{
AxisAlignedBox3d box = A.Bounds();
box.Contain(B.Bounds());
box.Expand(0.25 * box.MaxDim);
return box;
}
}
/// <summary>
/// Ricci blend
/// </summary>
public class SkeletalRicciBlend3d : BoundedImplicitFunction3d
{
public BoundedImplicitFunction3d A;
public BoundedImplicitFunction3d B;
public double BlendPower = 2.0;
public double Value(ref Vector3d pt)
{
double a = A.Value(ref pt);
double b = B.Value(ref pt);
if ( BlendPower == 1.0 ) {
return a + b;
} else if (BlendPower == 2.0) {
return Math.Sqrt(a*a + b*b);
} else {
return Math.Pow( Math.Pow(a,BlendPower) + Math.Pow(b,BlendPower), 1.0/BlendPower);
}
}
public AxisAlignedBox3d Bounds()
{
AxisAlignedBox3d box = A.Bounds();
box.Contain(B.Bounds());
box.Expand(0.25 * box.MaxDim);
return box;
}
}
/// <summary>
/// Boolean Union of N implicit functions, A OR B.
/// Assumption is that both have surface at zero isocontour and
/// negative is inside.
/// </summary>
public class SkeletalRicciNaryBlend3d : BoundedImplicitFunction3d
{
public List<BoundedImplicitFunction3d> Children;
public double BlendPower = 2.0;
public double FieldShift = 0;
public double Value(ref Vector3d pt)
{
int N = Children.Count;
double f = 0;
if (BlendPower == 1.0) {
for (int k = 0; k < N; ++k)
f += Children[k].Value(ref pt);
} else if (BlendPower == 2.0) {
for (int k = 0; k < N; ++k) {
double v = Children[k].Value(ref pt);
f += v * v;
}
f = Math.Sqrt(f);
} else {
for (int k = 0; k < N; ++k) {
double v = Children[k].Value(ref pt);
f += Math.Pow(v, BlendPower);
}
f = Math.Pow(f, 1.0 / BlendPower);
}
return f + FieldShift;
}
public AxisAlignedBox3d Bounds()
{
var box = Children[0].Bounds();
int N = Children.Count;
for (int k = 1; k < N; ++k)
box.Contain(Children[k].Bounds());
box.Expand(0.25 * box.MaxDim);
return box;
}
}
}