forked from jkmnt/matmill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch.cs
119 lines (93 loc) · 3.24 KB
/
branch.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
using System;
using System.Collections;
using System.Collections.Generic;
using CamBam.CAD;
using CamBam.Geom;
using Geom;
namespace Matmill
{
class Branch : Medial_branch
{
public delegate int Branch_visitor(Point2F pt);
private readonly Curve2d _curve = new Curve2d();
private readonly Branch Parent = null;
private readonly List<Branch> _children = new List<Branch>();
//--- Medial_branch interface
public double Deep_distance { get { return calc_deep_distance(); } }
public double Shallow_distance { get { return _curve.Length; } }
public Point2F Start { get { return _curve.Start; } }
public Point2F End { get { return _curve.End; } }
public void Add_point(Point2F pt)
{
_curve.Add(pt);
}
public Medial_branch Spawn_child()
{
return new Branch(this);
}
public void Postprocess()
{
// sort children so the shortest branch comes first in df traverse
this._children.Sort((a, b) => a.Deep_distance.CompareTo(b.Deep_distance));
}
public void Attach_to_parent()
{
if (this.Parent == null)
throw new Exception("attempt to attach orphaned branch");
this.Parent._children.Add(this);
}
//--- own interface
public bool Is_leaf { get { return _children.Count == 0; } }
public IEnumerable Children
{
get { return _children; }
}
public List<Branch> Df_traverse() //
{
List<Branch> result = new List<Branch>();
result.Add(this);
foreach (Branch b in _children)
result.AddRange(b.Df_traverse());
return result;
}
public double calc_deep_distance()
{
double dist = _curve.Length;
foreach (Branch b in _children)
dist += b.Deep_distance;
return dist;
}
public void Bisect(Branch_visitor visitor, ref double t, double stop_distance)
{
if (t < 0.0 || t > 1.0)
throw new Exception("branch bisector was called with a wrong range");
double left = t;
double right = 1.0;
double mid;
while (true)
{
mid = (left + right) / 2;
Point2F pt = _curve.Get_parametric_pt(mid);
int result = visitor(pt);
if (result == 0)
break;
if (result < 0)
right = mid;
else if (result > 0)
left = mid;
Point2F other = _curve.Get_parametric_pt(left == mid ? right : left);
if (pt.DistanceTo(other) < stop_distance) // range has shrinked, stop search
break;
}
t = mid;
}
public Polyline To_polyline()
{
return _curve.To_polyline();
}
public Branch(Branch parent)
{
Parent = parent;
}
}
}