-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimdraw.go
626 lines (537 loc) · 16.8 KB
/
imdraw.go
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
//go:build !tinygo
// +build !tinygo
package gfx
import (
"image/color"
"math"
)
// IMDraw is an immediate-mode-like shape drawer and BasicTarget. IMDraw supports TrianglesPosition,
// TrianglesColor, TrianglesPicture and PictureColor.
//
// IMDraw, other than a regular BasicTarget, is used to draw shapes. To draw shapes, you first need
// to Push some points to IMDraw:
//
// imd := gfx.NewIMDraw(pic) // use nil pic if you only want to draw primitive shapes
// imd.Push(gfx.V(100, 100))
// imd.Push(gfx.V(500, 100))
//
// Once you have Pushed some points, you can use them to draw a shape, such as a line:
//
// imd.Line(20) // draws a 20 units thick line
//
// Set exported fields to change properties of Pushed points:
//
// imd.Color = gfx.RGB(1, 0, 0)
// imd.Push(gfx.V(200, 200))
// imd.Circle(400, 0)
//
// Here is the list of all available point properties (need to be set before Pushing a point):
// - Color - applies to all
// - Picture - coordinates, only applies to filled polygons
// - Intensity - picture intensity, only applies to filled polygons
// - Precision - curve drawing precision, only applies to circles and ellipses
// - EndShape - shape of the end of a line, only applies to lines and outlines
//
// And here's the list of all shapes that can be drawn (all, except for line, can be filled or
// outlined):
// - Line
// - Polygon
// - Circle
// - Circle arc
// - Ellipse
// - Ellipse arc
type IMDraw struct {
Color color.Color
Picture Vec
Intensity float64
Precision int
EndShape EndShape
points []point
pool [][]point
matrix Matrix
tri *TrianglesData
batch *Batch
}
var _ BasicTarget = (*IMDraw)(nil)
type point struct {
pos Vec
col color.NRGBA
pic Vec
in float64
precision int
endshape EndShape
}
// EndShape specifies the shape of an end of a line or a curve.
type EndShape int
const (
// NoEndShape leaves a line point with no special end shape.
NoEndShape EndShape = iota
// SharpEndShape is a sharp triangular end shape.
SharpEndShape
// RoundEndShape is a circular end shape.
RoundEndShape
)
// NewIMDraw creates a new empty IMDraw. An optional Picture can be used to draw with a Picture.
//
// If you just want to draw primitive shapes, pass nil as the Picture.
func NewIMDraw(pic Picture) *IMDraw {
tri := &TrianglesData{}
im := &IMDraw{
tri: tri,
batch: NewBatch(tri, pic),
}
im.SetMatrix(IM)
im.Reset()
return im
}
// Clear removes all drawn shapes from the IM. This does not remove Pushed points.
func (imd *IMDraw) Clear() {
imd.tri.SetLen(0)
imd.batch.Dirty()
}
// Reset restores all point properties to defaults and removes all Pushed points.
//
// This does not affect matrix set by SetMatrix.
func (imd *IMDraw) Reset() {
imd.points = imd.points[:0]
imd.Color = ColorOpaque
imd.Picture = ZV
imd.Intensity = 0
imd.Precision = 64
imd.EndShape = NoEndShape
}
// Draw draws all currently drawn shapes inside the IM onto another Target.
//
// Note, that IMDraw's matrix have no effect here.
func (imd *IMDraw) Draw(t Target) {
imd.batch.Draw(t)
}
// Push adds some points to the IM queue. All Pushed points will have the same properties except for
// the position.
func (imd *IMDraw) Push(pts ...Vec) {
opts := point{
col: imd.Color.(color.NRGBA),
pic: imd.Picture,
in: imd.Intensity,
precision: imd.Precision,
endshape: imd.EndShape,
}
for _, pt := range pts {
imd.pushPt(pt, opts)
}
}
func (imd *IMDraw) pushPt(pos Vec, pt point) {
pt.pos = pos
imd.points = append(imd.points, pt)
}
// SetMatrix sets a Matrix that all further points will be transformed by.
func (imd *IMDraw) SetMatrix(m Matrix) {
imd.matrix = m
imd.batch.SetMatrix(imd.matrix)
}
// MakeTriangles returns a specialized copy of the provided Triangles that draws onto this IMDraw.
func (imd *IMDraw) MakeTriangles(t Triangles) TargetTriangles {
return imd.batch.MakeTriangles(t)
}
// MakePicture returns a specialized copy of the provided Picture that draws onto this IMDraw.
func (imd *IMDraw) MakePicture(p Picture) TargetPicture {
return imd.batch.MakePicture(p)
}
// Line draws a polyline of the specified thickness between the Pushed points.
func (imd *IMDraw) Line(thickness float64) {
imd.polyline(thickness, false)
}
// Rectangle draws a rectangle between each two subsequent Pushed points. Drawing a rectangle
// between two points means drawing a rectangle with sides parallel to the axes of the coordinate
// system, where the two points specify it's two opposite corners.
//
// If the thickness is 0, rectangles will be filled, otherwise will be outlined with the given
// thickness.
func (imd *IMDraw) Rectangle(thickness float64) {
if thickness == 0 {
imd.fillRectangle()
} else {
imd.outlineRectangle(thickness)
}
}
// Polygon draws a polygon from the Pushed points. If the thickness is 0, the convex polygon will be
// filled. Otherwise, an outline of the specified thickness will be drawn. The outline does not have
// to be convex.
//
// Note, that the filled polygon does not have to be strictly convex. The way it's drawn is that a
// triangle is drawn between each two adjacent points and the first Pushed point. You can use this
// property to draw certain kinds of concave polygons.
func (imd *IMDraw) Polygon(thickness float64) {
if thickness == 0 {
imd.fillPolygon()
} else {
imd.polyline(thickness, true)
}
}
// Circle draws a circle of the specified radius around each Pushed point. If the thickness is 0,
// the circle will be filled, otherwise a circle outline of the specified thickness will be drawn.
func (imd *IMDraw) Circle(radius, thickness float64) {
if thickness == 0 {
imd.fillEllipseArc(V(radius, radius), 0, 2*math.Pi)
} else {
imd.outlineEllipseArc(V(radius, radius), 0, 2*math.Pi, thickness, false)
}
}
// CircleArc draws a circle arc of the specified radius around each Pushed point. If the thickness
// is 0, the arc will be filled, otherwise will be outlined. The arc starts at the low angle and
// continues to the high angle. If low<high, the arc will be drawn counterclockwise. Otherwise it
// will be clockwise. The angles are not normalized by any means.
//
// imd.CircleArc(40, 0, 8*math.Pi, 0)
//
// This line will fill the whole circle 4 times.
func (imd *IMDraw) CircleArc(radius, low, high, thickness float64) {
if thickness == 0 {
imd.fillEllipseArc(V(radius, radius), low, high)
} else {
imd.outlineEllipseArc(V(radius, radius), low, high, thickness, true)
}
}
// Ellipse draws an ellipse of the specified radius in each axis around each Pushed points. If the
// thickness is 0, the ellipse will be filled, otherwise an ellipse outline of the specified
// thickness will be drawn.
func (imd *IMDraw) Ellipse(radius Vec, thickness float64) {
if thickness == 0 {
imd.fillEllipseArc(radius, 0, 2*math.Pi)
} else {
imd.outlineEllipseArc(radius, 0, 2*math.Pi, thickness, false)
}
}
// EllipseArc draws an ellipse arc of the specified radius in each axis around each Pushed point. If
// the thickness is 0, the arc will be filled, otherwise will be outlined. The arc starts at the low
// angle and continues to the high angle. If low<high, the arc will be drawn counterclockwise.
// Otherwise it will be clockwise. The angles are not normalized by any means.
//
// imd.EllipseArc(gfx.V(100, 50), 0, 8*math.Pi, 0)
//
// This line will fill the whole ellipse 4 times.
func (imd *IMDraw) EllipseArc(radius Vec, low, high, thickness float64) {
if thickness == 0 {
imd.fillEllipseArc(radius, low, high)
} else {
imd.outlineEllipseArc(radius, low, high, thickness, true)
}
}
func (imd *IMDraw) getAndClearPoints() []point {
points := imd.points
// use one of the existing pools so we don't reallocate as often
if len(imd.pool) > 0 {
pos := len(imd.pool) - 1
imd.points = imd.pool[pos][:0]
imd.pool = imd.pool[:pos]
} else {
imd.points = nil
}
return points
}
func (imd *IMDraw) restorePoints(points []point) {
imd.pool = append(imd.pool, imd.points)
imd.points = points[:0]
}
func (imd *IMDraw) applyMatrix(off int) {
for i := range (*imd.tri)[off:] {
(*imd.tri)[off+i].Position = imd.matrix.Project((*imd.tri)[off+i].Position)
}
}
func (imd *IMDraw) fillRectangle() {
points := imd.getAndClearPoints()
if len(points) < 2 {
imd.restorePoints(points)
return
}
off := imd.tri.Len()
imd.tri.SetLen(imd.tri.Len() + 6*(len(points)-1))
for i, j := 0, off; i+1 < len(points); i, j = i+1, j+6 {
a, b := points[i], points[i+1]
c := point{
pos: V(a.pos.X, b.pos.Y),
//col: a.col.Add(b.col).Mul(Alpha(0.5)),
pic: V(a.pic.X, b.pic.Y),
in: (a.in + b.in) / 2,
}
d := point{
pos: V(b.pos.X, a.pos.Y),
//col: a.col.Add(b.col).Mul(Alpha(0.5)),
pic: V(b.pic.X, a.pic.Y),
in: (a.in + b.in) / 2,
}
for k, p := range [...]point{a, b, c, a, b, d} {
(*imd.tri)[j+k].Position = p.pos
(*imd.tri)[j+k].Color = p.col
(*imd.tri)[j+k].Picture = p.pic
(*imd.tri)[j+k].Intensity = p.in
}
}
imd.applyMatrix(off)
imd.batch.Dirty()
imd.restorePoints(points)
}
func (imd *IMDraw) outlineRectangle(thickness float64) {
points := imd.getAndClearPoints()
if len(points) < 2 {
imd.restorePoints(points)
return
}
for i := 0; i+1 < len(points); i++ {
a, b := points[i], points[i+1]
mid := a
mid.col = mixNRGBA(a.col, b.col)
mid.in = (a.in + b.in) / 2
imd.pushPt(a.pos, a)
imd.pushPt(V(a.pos.X, b.pos.Y), mid)
imd.pushPt(b.pos, b)
imd.pushPt(V(b.pos.X, a.pos.Y), mid)
imd.polyline(thickness, true)
}
imd.restorePoints(points)
}
func (imd *IMDraw) fillPolygon() {
points := imd.getAndClearPoints()
if len(points) < 3 {
imd.restorePoints(points)
return
}
off := imd.tri.Len()
imd.tri.SetLen(imd.tri.Len() + 3*(len(points)-2))
for i, j := 1, off; i+1 < len(points); i, j = i+1, j+3 {
for k, p := range [...]int{0, i, i + 1} {
tri := &(*imd.tri)[j+k]
tri.Position = points[p].pos
tri.Color = points[p].col
tri.Picture = points[p].pic
tri.Intensity = points[p].in
}
}
imd.applyMatrix(off)
imd.batch.Dirty()
imd.restorePoints(points)
}
func (imd *IMDraw) fillEllipseArc(radius Vec, low, high float64) {
points := imd.getAndClearPoints()
for _, pt := range points {
num := math.Ceil(math.Abs(high-low) / (2 * math.Pi) * float64(pt.precision))
delta := (high - low) / num
off := imd.tri.Len()
imd.tri.SetLen(imd.tri.Len() + 3*int(num))
for i := range (*imd.tri)[off:] {
(*imd.tri)[off+i].Color = pt.col
(*imd.tri)[off+i].Picture = ZV
(*imd.tri)[off+i].Intensity = 0
}
for i, j := 0.0, off; i < num; i, j = i+1, j+3 {
angle := low + i*delta
sin, cos := math.Sincos(angle)
a := pt.pos.Add(V(
radius.X*cos,
radius.Y*sin,
))
angle = low + (i+1)*delta
sin, cos = math.Sincos(angle)
b := pt.pos.Add(V(
radius.X*cos,
radius.Y*sin,
))
(*imd.tri)[j+0].Position = pt.pos
(*imd.tri)[j+1].Position = a
(*imd.tri)[j+2].Position = b
}
imd.applyMatrix(off)
imd.batch.Dirty()
}
imd.restorePoints(points)
}
func (imd *IMDraw) outlineEllipseArc(radius Vec, low, high, thickness float64, doEndShape bool) {
points := imd.getAndClearPoints()
for _, pt := range points {
num := math.Ceil(math.Abs(high-low) / (2 * math.Pi) * float64(pt.precision))
delta := (high - low) / num
off := imd.tri.Len()
imd.tri.SetLen(imd.tri.Len() + 6*int(num))
for i := range (*imd.tri)[off:] {
(*imd.tri)[off+i].Color = pt.col
(*imd.tri)[off+i].Picture = ZV
(*imd.tri)[off+i].Intensity = 0
}
for i, j := 0.0, off; i < num; i, j = i+1, j+6 {
angle := low + i*delta
sin, cos := math.Sincos(angle)
normalSin, normalCos := V(sin, cos).ScaledXY(radius).Unit().XY()
a := pt.pos.Add(V(
radius.X*cos-thickness/2*normalCos,
radius.Y*sin-thickness/2*normalSin,
))
b := pt.pos.Add(V(
radius.X*cos+thickness/2*normalCos,
radius.Y*sin+thickness/2*normalSin,
))
angle = low + (i+1)*delta
sin, cos = math.Sincos(angle)
normalSin, normalCos = V(sin, cos).ScaledXY(radius).Unit().XY()
c := pt.pos.Add(V(
radius.X*cos-thickness/2*normalCos,
radius.Y*sin-thickness/2*normalSin,
))
d := pt.pos.Add(V(
radius.X*cos+thickness/2*normalCos,
radius.Y*sin+thickness/2*normalSin,
))
(*imd.tri)[j+0].Position = a
(*imd.tri)[j+1].Position = b
(*imd.tri)[j+2].Position = c
(*imd.tri)[j+3].Position = c
(*imd.tri)[j+4].Position = b
(*imd.tri)[j+5].Position = d
}
imd.applyMatrix(off)
imd.batch.Dirty()
if doEndShape {
lowSin, lowCos := math.Sincos(low)
lowCenter := pt.pos.Add(V(
radius.X*lowCos,
radius.Y*lowSin,
))
normalLowSin, normalLowCos := V(lowSin, lowCos).ScaledXY(radius).Unit().XY()
normalLow := V(normalLowCos, normalLowSin).Angle()
highSin, highCos := math.Sincos(high)
highCenter := pt.pos.Add(V(
radius.X*highCos,
radius.Y*highSin,
))
normalHighSin, normalHighCos := V(highSin, highCos).ScaledXY(radius).Unit().XY()
normalHigh := V(normalHighCos, normalHighSin).Angle()
orientation := 1.0
if low > high {
orientation = -1.0
}
switch pt.endshape {
case NoEndShape:
// nothing
case SharpEndShape:
thick := V(thickness/2, 0).Rotated(normalLow)
imd.pushPt(lowCenter.Add(thick), pt)
imd.pushPt(lowCenter.Sub(thick), pt)
imd.pushPt(lowCenter.Sub(thick.Normal().Scaled(orientation)), pt)
imd.fillPolygon()
thick = V(thickness/2, 0).Rotated(normalHigh)
imd.pushPt(highCenter.Add(thick), pt)
imd.pushPt(highCenter.Sub(thick), pt)
imd.pushPt(highCenter.Add(thick.Normal().Scaled(orientation)), pt)
imd.fillPolygon()
case RoundEndShape:
imd.pushPt(lowCenter, pt)
imd.fillEllipseArc(V(thickness/2, thickness/2), normalLow, normalLow-math.Pi*orientation)
imd.pushPt(highCenter, pt)
imd.fillEllipseArc(V(thickness/2, thickness/2), normalHigh, normalHigh+math.Pi*orientation)
}
}
}
imd.restorePoints(points)
}
func (imd *IMDraw) polyline(thickness float64, closed bool) {
points := imd.getAndClearPoints()
if len(points) == 0 {
imd.restorePoints(points)
return
}
if len(points) == 1 {
// one point special case
points = append(points, points[0])
}
// first point
j := 0
ijNormal := points[0].pos.To(points[1].pos).Normal().Unit().Scaled(thickness / 2)
if !closed {
switch points[j].endshape {
case NoEndShape:
// nothing
case SharpEndShape:
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
imd.pushPt(points[j].pos.Add(ijNormal.Normal()), points[j])
imd.fillPolygon()
case RoundEndShape:
imd.pushPt(points[j].pos, points[j])
imd.fillEllipseArc(V(thickness/2, thickness/2), ijNormal.Angle(), ijNormal.Angle()+math.Pi)
}
}
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
// middle points
for i := 0; i < len(points); i++ {
j, k := i+1, i+2
closing := false
if j >= len(points) {
j %= len(points)
closing = true
}
if k >= len(points) {
if !closed {
break
}
k %= len(points)
}
jkNormal := points[j].pos.To(points[k].pos).Normal().Unit().Scaled(thickness / 2)
orientation := 1.0
if ijNormal.Cross(jkNormal) > 0 {
orientation = -1.0
}
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.fillPolygon()
switch points[j].endshape {
case NoEndShape:
// nothing
case SharpEndShape:
imd.pushPt(points[j].pos, points[j])
imd.pushPt(points[j].pos.Add(ijNormal.Scaled(orientation)), points[j])
imd.pushPt(points[j].pos.Add(jkNormal.Scaled(orientation)), points[j])
imd.fillPolygon()
case RoundEndShape:
imd.pushPt(points[j].pos, points[j])
imd.fillEllipseArc(V(thickness/2, thickness/2), ijNormal.Angle(), ijNormal.Angle()-math.Pi)
imd.pushPt(points[j].pos, points[j])
imd.fillEllipseArc(V(thickness/2, thickness/2), jkNormal.Angle(), jkNormal.Angle()+math.Pi)
}
if !closing {
imd.pushPt(points[j].pos.Add(jkNormal), points[j])
imd.pushPt(points[j].pos.Sub(jkNormal), points[j])
}
// "next" normal becomes previous normal
ijNormal = jkNormal
}
// last point
i := len(points) - 2
j = len(points) - 1
ijNormal = points[i].pos.To(points[j].pos).Normal().Unit().Scaled(thickness / 2)
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.fillPolygon()
if !closed {
switch points[j].endshape {
case NoEndShape:
// nothing
case SharpEndShape:
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
imd.pushPt(points[j].pos.Add(ijNormal.Normal().Scaled(-1)), points[j])
imd.fillPolygon()
case RoundEndShape:
imd.pushPt(points[j].pos, points[j])
imd.fillEllipseArc(V(thickness/2, thickness/2), ijNormal.Angle(), ijNormal.Angle()-math.Pi)
}
}
imd.restorePoints(points)
}
func mixNRGBA(c1, c2 color.NRGBA) color.NRGBA {
return color.NRGBA{
c1.R/2 + c2.R/2,
c1.G/2 + c2.G/2,
c1.B/2 + c2.B/2,
c1.A/2 + c2.A/2,
}
}