/
DLPViewCanvas.cs
282 lines (207 loc) · 7.11 KB
/
DLPViewCanvas.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
using System;
using System.Collections.Generic;
using Gtk;
using GLib;
using SkiaSharp;
using g3;
using gs;
namespace DLPViewer
{
class DLPViewCanvas : DrawingArea
{
public bool ShowOpenEndpoints = true;
public float Zoom = 0.95f;
// this is a pixel-space translate
public Vector2f Translate = Vector2f.Zero;
public DLPViewCanvas()
{
ExposeEvent += OnExpose;
ButtonPressEvent += OnButtonPressEvent;
ButtonReleaseEvent += OnButtonReleaseEvent;
MotionNotifyEvent += OnMotionNotifyEvent;
ScrollEvent += OnScrollEvent;
Events = Gdk.EventMask.ExposureMask | Gdk.EventMask.LeaveNotifyMask |
Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask | Gdk.EventMask.PointerMotionMask |
Gdk.EventMask.ScrollMask;
SetSlices(new PlanarSliceStack());
}
PlanarSliceStack Stack;
int currentLayer = 0;
public void SetSlices(PlanarSliceStack slices) {
Stack = slices;
CurrentLayer = 0;
SliceImages = new List<SKBitmap>();
}
public int CurrentLayer {
get { return currentLayer; }
set {
currentLayer = MathUtil.Clamp(value, 0, Stack.Slices.Count - 1);
QueueDraw();
}
}
List<SKBitmap> SliceImages = new List<SKBitmap>();
float CurrentDPIMM = 1.0f;
// compute bitmap for each slice (maybe do on demand?)
void compute_slice_images() {
// construct bounding box and add buffer region
AxisAlignedBox3d bounds = Stack.Bounds;
double bufferMM = 1;
bounds.Expand(bufferMM);
Vector3d center = bounds.Center;
// will create a bitmap with this dots-per-inch
double dpi = 300;
double dpmm = dpi / Units.Convert(Units.Linear.Inches, Units.Linear.Millimeters);
CurrentDPIMM = (float)dpmm;
// pixel dimensions of image
int width = (int)(bounds.Width * dpmm);
int height = (int)(bounds.Height * dpmm);
// backgroun and object colors
SKColor bgColor = SkiaUtil.Color(0, 0, 0, 255);
SKColor objColor = SkiaUtil.Color(255, 255, 255, 255);
// function that maps from input coordinates to pixel space
Func<Vector2d, SKPoint> mapToSkiaF = (origPt) => {
origPt.y = -origPt.y; // [RMS] !!! flip Y here? if we don't do this the
// polylines we draw below don't match up. But maybe
// its the polylines that are wrong??
origPt *= dpmm;
origPt.x += width / 2; origPt.y += height / 2;
return new SKPoint((float)origPt.x, (float)origPt.y);
};
foreach (PlanarSlice slice in Stack.Slices) {
var bitmap = new SKBitmap(width, height, SkiaUtil.ColorType(), SKAlphaType.Premul);
IntPtr len;
using (var skSurface = SKSurface.Create(bitmap.Info.Width, bitmap.Info.Height, SkiaUtil.ColorType(), SKAlphaType.Premul, bitmap.GetPixels(out len), bitmap.Info.RowBytes)) {
var canvas = skSurface.Canvas;
canvas.Clear(bgColor);
using (var paint = new SKPaint()) {
paint.IsAntialias = false;
paint.Style = SKPaintStyle.Fill;
paint.Color = objColor;
foreach (GeneralPolygon2d poly in slice.Solids) {
SKPath path = SkiaUtil.ToSKPath(poly, mapToSkiaF);
canvas.DrawPath(path, paint);
}
}
}
SliceImages.Add(bitmap);
}
}
SKBitmap get_slice_image(int i) {
if (SliceImages.Count == 0)
compute_slice_images();
return SliceImages[i];
}
void OnExpose(object sender, ExposeEventArgs args)
{
DrawingArea area = (DrawingArea) sender;
Cairo.Context cr = Gdk.CairoHelper.Create(area.GdkWindow);
int width = area.Allocation.Width;
int height = area.Allocation.Height;
AxisAlignedBox3d bounds3 = Stack.Bounds;
AxisAlignedBox2d bounds = (bounds3 == AxisAlignedBox3d.Empty) ?
new AxisAlignedBox2d(0, 0, 500, 500) :
new AxisAlignedBox2d(bounds3.Min.x, bounds3.Min.y, bounds3.Max.x, bounds3.Max.y );
double sx = (double)width / bounds.Width;
double sy = (double)height / bounds.Height;
float scale = (float)Math.Min(sx, sy);
// we apply this translate after scaling to pixel coords
Vector2f pixC = Zoom * scale * (Vector2f)bounds.Center;
Vector2f translate = new Vector2f(width/2, height/2) - pixC;
using (var bitmap = new SKBitmap(width, height, SkiaUtil.ColorType(), SKAlphaType.Premul))
{
IntPtr len;
using (var skSurface = SKSurface.Create(bitmap.Info.Width, bitmap.Info.Height, SkiaUtil.ColorType(), SKAlphaType.Premul, bitmap.GetPixels(out len), bitmap.Info.RowBytes))
{
var canvas = skSurface.Canvas;
canvas.Clear(SkiaUtil.Color(240, 240, 240, 255));
Func<Vector2d, Vector2f> xformF = (pOrig) => {
Vector2f pNew = (Vector2f)pOrig;
pNew -= (Vector2f)bounds.Center;
pNew = Zoom * scale * pNew;
pNew += (Vector2f)pixC;
pNew += translate + Zoom*Translate;
pNew.y = canvas.LocalClipBounds.Height - pNew.y;
return pNew;
};
Func<Vector2d, SKPoint> mapToSkiaF = (pOrig) => {
Vector2f p = xformF(pOrig);
return new SKPoint(p.x, p.y);
};
using (var paint = new SKPaint())
{
SKBitmap sliceImg = get_slice_image(currentLayer);
float w = sliceImg.Width / CurrentDPIMM, h = sliceImg.Height / CurrentDPIMM;
w *= Zoom * scale;
h *= Zoom * scale;
SKPoint sliceCenter = mapToSkiaF(Vector2d.Zero);
SKRect drawRect = new SKRect(
sliceCenter.X - w / 2, sliceCenter.Y - h / 2,
sliceCenter.X + w / 2, sliceCenter.Y + h / 2);
canvas.DrawBitmap(sliceImg, drawRect, paint);
paint.IsAntialias = true;
paint.Style = SKPaintStyle.Stroke;
PlanarSlice slice = Stack.Slices[currentLayer];
paint.Color = SkiaUtil.Color(255, 0, 0, 255); ;
paint.StrokeWidth = 2;
foreach ( GeneralPolygon2d poly in slice.Solids ) {
SKPath path = SkiaUtil.ToSKPath(poly, mapToSkiaF);
canvas.DrawPath(path, paint);
}
}
Cairo.Surface surface = new Cairo.ImageSurface(
bitmap.GetPixels(out len),
Cairo.Format.Argb32,
bitmap.Width, bitmap.Height,
bitmap.Width * 4);
surface.MarkDirty();
cr.SetSourceSurface(surface, 0, 0);
cr.Paint();
}
}
//return true;
}
// zoom
private void OnScrollEvent(object o, ScrollEventArgs args)
{
if (args.Event.Direction == Gdk.ScrollDirection.Up)
Zoom *= 1.05f;
else
Zoom = Math.Max(0.25f, Zoom * (1.0f / 1.05f));
QueueDraw();
}
// pan support
bool left_down = false;
Vector2f start_pos = Vector2f.Zero;
Vector2f pan_start;
private void OnButtonPressEvent(object o, ButtonPressEventArgs args)
{
if (args.Event.Button == 1) {
left_down = true;
start_pos = new Vector2f((float)args.Event.X, (float)args.Event.Y);
pan_start = Translate;
}
}
private void OnButtonReleaseEvent(object o, ButtonReleaseEventArgs args)
{
if (left_down)
left_down = false;
}
private void OnMotionNotifyEvent(object o, MotionNotifyEventArgs args)
{
if (left_down) {
Vector2f cur_pos = new Vector2f((float)args.Event.X, (float)args.Event.Y);
Vector2f delta = cur_pos - start_pos;
delta.y = -delta.y;
delta *= 1.0f; // speed
Translate = pan_start + delta / Zoom;
QueueDraw();
}
}
void Reset()
{
Zoom = 1.0f;
Translate = Vector2f.Zero;
QueueDraw();
}
}
}