-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
150 lines (113 loc) · 4.72 KB
/
Program.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
using GShark.Geometry;
namespace example;
// example-0022
// Nurb surface join on two tubes
//
// - use 'space' application key to toggle joint visiblity
class Program
{
static void Main(string[] args)
{
InitAvalonia();
GLPointFigure? profilePtsFig = null;
var w = GLWindow.Create(
// customize ControlFigureVisible to not show green profile dots on "RENDER" titled views
onGLControlCreated: (avaloniaGLControl) =>
{
avaloniaGLControl.GLControl.ControlFigureVisible = (glControl, glFigure) =>
{
if (glControl.Title.StartsWith("RENDER"))
return glFigure != profilePtsFig;
return true;
};
},
onFocusedControlChanged: (split, AvaloniaGLControl, isInitial) =>
{
if (isInitial)
split.LoadViewLayout();
}
);
GLTriangleFigure? joinFig = null;
w.GLModel.BuildModel = (glCtl, isInitial) =>
{
if (!isInitial) return;
var glModel = glCtl.GLModel;
const int JOINT_DIV = 8;
const int TUBE_DIV = JOINT_DIV;
const int RAIL_DIVS = 4;
glModel.Clear();
glModel.AddFigure(MakeWCSFigure());
var tube1 = new Cone(
baseCS: (WCS * Matrix4x4.CreateRotationX((float)(PI / 2)) * Matrix4x4.CreateRotationZ(-(float)(PI / 4)))
.Move((float)(-3 * Cos(45d.ToRad())), (float)(-3 * Sin(45d.ToRad())), 0),
baseRadius: 2, topRadius: 2, height: 6,
bottomCap: false, topCap: false).Figure(JOINT_DIV);
// mirror tube1 respect the YZ plane
var tube2 = tube1.Mirror(YZCS)!;
glModel.AddFigure(tube1);
glModel.AddFigure(tube2);
// grab tube vertexes near join as profile to sweep
var _profilePts = tube1
.BuildVertexPosDict(tol: 1e-5f)
.Select(w => w.Value.First())
.Where(r => r.Position.Length() < 5).Select(w => new
{
vtx = w,
sig = w.PositionSignature(1e-5f),
pos = w.Position
}).ToList();
var profilePts = _profilePts.Select(w => w.pos).ToList();
var profilePtsMean = profilePts.Mean();
var profileCenter = profilePtsMean;
var profilePlane = MakeCS(profilePtsMean,
profilePts[JOINT_DIV / 4] - profilePtsMean,
profilePts[0] - profilePtsMean,
makeOrthonormalization: true);
Debug.WriteLine($"profilePlane START: {profilePlane.BaseZ()}");
profilePts.Add(profilePts[0]);
profilePtsFig = new GLPointFigure(profilePts).SetColor(Color.Green);
glModel.AddFigure(profilePtsFig);
var railPts = new List<Vector3>();
{
var N = RAIL_DIVS;
var alpha = 0f;
var alphaStep = (float)(PI / 2 / N);
var rotCenter = new Vector3(0, (float)(-3 * Sqrt(2)), 0);
for (int i = 0; i < N + 1; ++i)
{
railPts.Add(Vector3.Transform(profileCenter, Matrix4x4.CreateRotationZ(-alpha, rotCenter)));
alpha += alphaStep;
}
}
for (int i = 0; i < railPts.Count; ++i)
{
var railPt = railPts[i];
System.Console.WriteLine($"rail [{i}]: {railPt}");
}
System.Console.WriteLine($"profile CENTER : {profileCenter}");
for (int i = 0; i < profilePts.Count; ++i)
{
var profilePt = profilePts[i];
System.Console.WriteLine($"profile [{i}]: {profilePt}");
}
var sweepNurb = NurbsSurface.FromSweep(
new NurbsCurve(railPts.ToPoint3().ToList(), 1),
new NurbsCurve(profilePts.ToPoint3().ToList(), 1),
startTangent: new GShark.Geometry.Vector3(0.7071068, 0.70710677, 0),
endTangent: new GShark.Geometry.Vector3(0.7071068, -0.70710677, 0));
var railFig = new GLPointFigure(railPts).SetColor(Color.Yellow);
glModel.AddFigure(railFig);
joinFig = sweepNurb.NurbToGL(Color.Red, N: TUBE_DIV).ToFigure();
glModel.AddFigure(joinFig);
};
w.KeyDown += (sender, e) =>
{
if (e.Key == Key.Space)
{
if (joinFig is not null) joinFig.Visible = !joinFig.Visible;
w.Invalidate();
}
};
w.ShowSync();
}
}