aboutsummaryrefslogtreecommitdiffstats
path: root/src/types/path.nim
blob: 8f21a8910ece6259e27c56c57accd34455a04d44 (plain) (blame)
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
import std/algorithm
import std/deques
import std/math

type Vector2D* = object
  x*: float64
  y*: float64

func `-`(v1, v2: Vector2D): Vector2D =
  return Vector2D(x: v1.x - v2.x, y: v1.y - v2.y)

func `+`(v1, v2: Vector2D): Vector2D =
  return Vector2D(x: v1.x + v2.x, y: v1.y + v2.y)

# scalar multiplication
func `*`(v: Vector2D, s: float64): Vector2D =
  return Vector2D(x: v.x * s, y: v.y * s)

func `/`(v: Vector2D, s: float64): Vector2D =
  return Vector2D(x: v.x / s, y: v.y / s)

# dot product
func `*`(v1, v2: Vector2D): float64 =
  return v1.x * v2.x + v1.y * v2.y

func norm(v: Vector2D): float64 =
  return sqrt(v.x * v.x + v.y * v.y)

# kind of a cross product?
func cross(v1, v2: Vector2D): float64 =
  return v1.x * v2.y - v1.y * v2.x

# https://en.wikipedia.org/wiki/Inner_product_space
func innerAngle(v1, v2: Vector2D): float64 =
  return arccos((v1 * v2) / (v1.norm() * v2.norm()))

func rotate(v: Vector2D, alpha: float64): Vector2D =
  let sa = sin(alpha)
  let ca = cos(alpha)
  return Vector2D(
    x: v.x * ca - v.y * sa,
    y: v.x * sa + v.y * ca
  )

func collinear(v1, v2, v3: Vector2D): bool =
  return almostEqual((v1.y - v2.y) * (v1.x - v3.x),
    (v1.y - v3.y) * (v1.x - v2.x))

type
  Line* = object
    p0*: Vector2D
    p1*: Vector2D

  LineSegment* = object
    line: Line
    miny*: float64
    maxy*: float64
    minyx*: float64
    islope*: float64

func minyx(line: Line): float64 =
  if line.p0.y < line.p1.y:
    return line.p0.x
  return line.p1.x

func miny(line: Line): float64 =
  return min(line.p0.y, line.p1.y)

func maxy(line: Line): float64 =
  return max(line.p0.y, line.p1.y)

# inverse slope
func islope(line: Line): float64 =
  let ydiff = (line.p0.y - line.p1.y)
  if ydiff == 0:
    return 0
  return (line.p0.x - line.p1.x) / ydiff

proc cmpLineSegmentY*(l1, l2: LineSegment): int =
  return cmp(l1.miny, l2.miny)

proc cmpLineSegmentX*(l1, l2: LineSegment): int =
  return cmp(l1.minyx, l2.minyx)

func p0*(ls: LineSegment): Vector2D {.inline.} = ls.line.p0
func p1*(ls: LineSegment): Vector2D {.inline.} = ls.line.p1

proc toLineSegment*(line: Line): LineSegment =
  LineSegment(
    line: line,
    miny: line.miny,
    maxy: line.maxy,
    minyx: line.minyx,
    islope: line.islope
  )

type Matrix* = object
  me*: seq[float64]
  w: int
  h: int

proc newMatrix*(me: seq[float64]; w, h: int): Matrix =
  return Matrix(
    me: me,
    w: w,
    h: h
  )

proc newIdentityMatrix*(n: int): Matrix =
  var me = newSeq[float64](n * n)
  for i in 0 ..< n:
    me[n * i + i] = 1
  return Matrix(
    me: me,
    w: n,
    h: n
  )

proc newMatrixUninitialized(w, h: int): Matrix =
  return Matrix(
    me: newSeqUninitialized[float64](w * h),
    w: w,
    h: h
  )

proc `*`*(a, b: Matrix): Matrix =
  assert a.w == b.h
  let h = a.h
  let w = b.w
  let n = a.w
  var c = newMatrixUninitialized(w, h)
  for x in 0 ..< w:
    for y in 0 ..< h:
      var val: float64 = 0
      for i in 0 ..< n:
        val += a.me[y * a.w + i] * b.me[i * b.w + x]
      c.me[y * c.w + x] = val
  return c

proc `*=`*(a: var Matrix; b: Matrix) =
  a = a * b

type
  Path* = ref object
    subpaths: seq[Subpath]
    needsNewSubpath: bool
    tempClosed: bool

  PathLines* = object
    lines*: seq[LineSegment]
    miny*: float64
    maxy*: float64

  PathSegmentType = enum
    pstStraight, pstQuadratic, pstBezier, pstArc, pstEllipse

  PathSegment = object
    case t: PathSegmentType
    of pstQuadratic:
      cp: Vector2D
    of pstBezier:
      cp0: Vector2D
      cp1: Vector2D
    of pstArc:
      oa: Vector2D
      r: float64
      ia: bool
    of pstEllipse:
      oe: Vector2D
      rx: float64
      ry: float64
    else: discard

  Subpath* = object
    points: seq[Vector2D]
    segments: seq[PathSegment]
    closed: bool

proc newPath*(): Path =
  return Path(
    needsNewSubpath: true
  )

proc addSubpathAt(path: Path; p: Vector2D) =
  path.subpaths.add(Subpath(points: @[p]))

proc addSegment(path: Path; segment: PathSegment; p: Vector2D) =
  path.subpaths[^1].segments.add(segment)
  path.subpaths[^1].points.add(p)

proc addStraightSegment(path: Path; p: Vector2D) =
  let segment = PathSegment(t: pstStraight)
  path.addSegment(segment, p)

proc addQuadraticSegment(path: Path; cp, p: Vector2D) =
  let segment = PathSegment(
    t: pstQuadratic,
    cp: cp
  )
  path.addSegment(segment, p)

proc addBezierSegment(path: Path; cp0, cp1, p: Vector2D) =
  let segment = PathSegment(
    t: pstBezier,
    cp0: cp0,
    cp1: cp1
  )
  path.addSegment(segment, p)

# Goes from start tangent point to end tangent point
proc addArcSegment(path: Path; o, etan: Vector2D; r: float64; ia: bool) =
  let segment = PathSegment(
    t: pstArc,
    oa: o,
    r: r,
    ia: ia
  )
  path.addSegment(segment, etan)

proc addEllipseSegment(path: Path; o, etan: Vector2D; rx, ry: float64) =
  #TODO simplify to bezier?
  let segment = PathSegment(
    t: pstEllipse,
    oe: o,
    rx: rx,
    ry: ry
  )
  path.addSegment(segment, etan)

# https://hcklbrrfnn.files.wordpress.com/2012/08/bez.pdf
func flatEnough(a, b, c: Vector2D): bool =
  let ux = 3 * c.x - 2 * a.x - b.x
  let uy = 3 * c.y - 2 * a.y - b.y
  let vx = 3 * c.x - 2 * b.x - b.x
  let vy = 3 * c.y - 2 * b.y - b.y
  return max(ux * ux, vx * vx) + max(uy * uy, vy * vy) <= 0.02

func flatEnough(a, b, c0, c1: Vector2D): bool =
  let ux = 3 * c0.x - 2 * a.x - b.x
  let uy = 3 * c0.y - 2 * a.y - b.y
  let vx = 3 * c1.x - a.x - 2 * b.x
  let vy = 3 * c1.y - a.y - 2 * b.y
  return max(ux * ux, vx * vx) + max(uy * uy, vy * vy) <= 0.02

iterator items*(pl: PathLines): LineSegment {.inline.} =
  for line in pl.lines:
    yield line

func `[]`*(pl: PathLines; i: int): LineSegment = pl.lines[i]
func `[]`*(pl: PathLines; i: BackwardsIndex): LineSegment = pl.lines[i]
func len*(pl: PathLines): int = pl.lines.len

iterator quadraticLines(a, b, c: Vector2D): Line {.inline.} =
  var points: Deque[tuple[a, b, c: Vector2D]]
  let tup = (a, b, c)
  points.addFirst(tup)
  while points.len > 2:
    let (a, b, c) = points.popFirst()
    if flatEnough(a, b, c):
      yield Line(p0: a, p1: b)
    else:
      let mid1 = (c + a) / 2
      let mid2 = (c + b) / 2
      let s = (mid1 + mid2) / 2
      points.addFirst((a, s, mid1))
      points.addFirst((s, b, mid2))

iterator bezierLines(p0, p1, c0, c1: Vector2D): Line {.inline.} =
  var points: Deque[tuple[p0, p1, c0, c1: Vector2D]]
  let tup = (p0, p1, c0, c1)
  points.addLast(tup)
  while points.len > 0:
    let (p0, p1, c0, c1) = points.popFirst()
    if flatEnough(p0, p1, c0, c1):
      yield Line(p0: p0, p1: p1)
    else:
      let mida1 = (p0 + c0) / 2
      let mida2 = (c0 + c1) / 2
      let mida3 = (c1 + p1) / 2
      let midb1 = (mida1 + mida2) / 2
      let midb2 = (mida2 + mida3) / 2
      let midc = (midb1 + midb2) / 2
      points.addLast((p0, midc, mida1, midb1))
      points.addLast((midc, p1, midb2, mida3))

# https://stackoverflow.com/a/44829356
func arcControlPoints(p1, p4, o: Vector2D): tuple[c0, c1: Vector2D] =
  let a = p1 - o
  let b = p4 - o
  let q1 = a.x * a.x + a.y * a.y
  let q2 = q1 + a.x * b.x + a.y * b.y
  let k2 = (4 / 3) * (sqrt(2 * q1 * q2) - q2) / a.cross(b)
  let c0 = o + a + Vector2D(x: -k2 * a.y, y:  k2 * a.x)
  let c1 = o + b + Vector2D(x:  k2 * b.y, y: -k2 * b.x)
  return (c0, c1)

iterator arcLines(p0, p1, o: Vector2D; r: float64; i: bool): Line {.inline.} =
  var p0 = p0
  let pp0 = p0 - o
  let pp1 = p1 - o
  var theta = pp0.innerAngle(pp1)
  if not i:
    theta = PI * 2 - theta
  while theta > 0:
    let step = if theta > PI / 2: PI / 2 else: theta
    let p1 = (p0 - o).rotate(step) + o
    let (c0, c1) = arcControlPoints(p0, p1, o)
    for line in bezierLines(p0, p1, c0, c1):
      yield line
    p0 = p1
    theta -= step

proc addLines(lines: var seq[Line]; subpath: Subpath; i: int) =
  let p0 = subpath.points[i]
  let p1 = subpath.points[i + 1]
  case subpath.segments[i].t
  of pstStraight:
    if p0 != p1:
      lines.add(Line(p0: p0, p1: p1))
  of pstQuadratic:
    let c = subpath.segments[i].cp
    for line in quadraticLines(p0, p1, c):
      if line.p0 != line.p1:
        lines.add(line)
  of pstBezier:
    let c0 = subpath.segments[i].cp0
    let c1 = subpath.segments[i].cp1
    for line in bezierLines(p0, p1, c0, c1):
      if line.p0 != line.p1:
        lines.add(line)
  of pstArc:
    let o = subpath.segments[i].oa
    let r = subpath.segments[i].r
    let i = subpath.segments[i].ia
    for line in arcLines(p0, p1, o, r, i):
      if line.p0 != line.p1:
        lines.add(line)
  of pstEllipse:
    discard #TODO

proc getLines*(path: Path): seq[Line] =
  var lines: seq[Line] = @[]
  for subpath in path.subpaths:
    assert subpath.points.len == subpath.segments.len + 1
    for i in 0 ..< subpath.segments.len:
      lines.addLines(subpath, i)
  return lines

proc getLineSegments*(path: Path): PathLines =
  if path.subpaths.len == 0:
    return PathLines()
  var miny = Inf
  var maxy = -Inf
  let lines = path.getLines()
  var segments: seq[LineSegment] = @[]
  for line in lines:
    let ls = line.toLineSegment()
    miny = min(miny, ls.miny)
    maxy = max(maxy, ls.maxy)
    segments.add(ls)
  segments.sort(cmpLineSegmentY)
  return PathLines(
    miny: miny,
    maxy: maxy,
    lines: segments
  )

proc moveTo(path: Path; v: Vector2D) =
  path.addSubpathAt(v)
  path.needsNewSubpath = false #TODO TODO TODO ???? why here

proc beginPath*(path: Path) =
  path.subpaths.setLen(0)

proc moveTo*(path: Path; x, y: float64) =
  for v in [x, y]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  path.moveTo(Vector2D(x: x, y: y))

proc ensureSubpath(path: Path; x, y: float64) =
  if path.needsNewSubpath:
    path.moveTo(x, y)
    path.needsNewSubpath = false

proc closePath*(path: Path) =
  let lsp = path.subpaths[^1]
  if path.subpaths.len > 0 and (lsp.points.len > 0 or lsp.closed):
    path.subpaths[^1].closed = true
    path.addSubpathAt(path.subpaths[^1].points[0])

#TODO this is a hack, and breaks as soon as any draw command is issued
# between tempClosePath and tempOpenPath
proc tempClosePath*(path: Path) =
  if path.subpaths.len > 0 and not path.subpaths[^1].closed:
    path.subpaths[^1].closed = true
    let lsp = path.subpaths[^1]
    path.addSubpathAt(lsp.points[^1])
    path.addStraightSegment(lsp.points[0])
    path.tempClosed = true

proc tempOpenPath*(path: Path) =
  if path.tempClosed:
    path.subpaths.setLen(path.subpaths.len - 1)
    path.subpaths[^1].closed = false
    path.tempClosed = false

proc lineTo*(path: Path; x, y: float64) =
  for v in [x, y]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  if path.subpaths.len == 0:
    path.ensureSubpath(x, y)
  else:
    path.addStraightSegment(Vector2D(x: x, y: y))

proc quadraticCurveTo*(path: Path; cpx, cpy, x, y: float64) =
  for v in [cpx, cpy, x, y]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  path.ensureSubpath(cpx, cpy)
  let cp = Vector2D(x: cpx, y: cpy)
  let p = Vector2D(x: x, y: y)
  path.addQuadraticSegment(cp, p)

proc bezierCurveTo*(path: Path; cp0x, cp0y, cp1x, cp1y, x, y: float64) =
  for v in [cp0x, cp0y, cp1x, cp1y, x, y]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  path.ensureSubpath(cp0x, cp0y)
  let cp0 = Vector2D(x: cp0x, y: cp0y)
  let cp1 = Vector2D(x: cp1x, y: cp1y)
  let p = Vector2D(x: x, y: y)
  path.addBezierSegment(cp0, cp1, p)

proc arcTo*(path: Path; x1, y1, x2, y2, radius: float64) =
  for v in [x1, y1, x2, y2, radius]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  path.ensureSubpath(x1, y1)
  #TODO this should be transformed by the inverse of the transformation matrix
  let v0 = path.subpaths[^1].points[^1]
  let v1 = Vector2D(x: x1, y: y1)
  let v2 = Vector2D(x: x2, y: y2)
  if v0.x == x1 and v0.y == y1 or x1 == x2 and y1 == y2 or radius == 0:
    path.addStraightSegment(v1)
  elif collinear(v0, v1, v2):
    path.addStraightSegment(v1)
  else:
    let pv0 = v0 - v1
    let pv2 = v2 - v1
    let tv0 = v1 + pv0 * radius * 2 / pv0.norm()
    let tv2 = v1 + pv2 * radius * 2 / pv2.norm()
    let q = -(pv0.x * tv0.x + pv0.y * tv0.y)
    let p = -(pv2.x * tv2.x + pv2.y * tv2.y)
    let cr = pv0.cross(pv2)
    let origin = Vector2D(
      x: (pv0.y * p - pv2.y * q) / cr,
      y: (pv2.x * q - pv0.x * p) / cr
    )
    path.addStraightSegment(tv0)
    path.addArcSegment(origin, tv2, radius, true) #TODO always inner?

# Ref. https://math.stackexchange.com/a/22067
# (Originally found it in SerenityOS.)
func resolveEllipsePoint(o: Vector2D; angle, radiusX, radiusY,
    rotation: float64): Vector2D =
  let tanrel = tan(angle)
  let tan2 = tanrel * tanrel
  let ab = radiusX * radiusY
  let a2 = radiusX * radiusX
  let b2 = radiusY * radiusY
  let sq = sqrt(b2 + a2 * tan2)
  let sn = if cos(angle) >= 0: 1f64 else: -1f64
  let relx = ab / sq * sn
  let rely = ab * tanrel / sq * sn
  return Vector2D(x: relx, y: rely).rotate(rotation) + o

proc arc*(path: Path; x, y, radius, startAngle, endAngle: float64;
    counterclockwise: bool) =
  for v in [x, y, radius, startAngle, endAngle]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  let o = Vector2D(x: x, y: y)
  var s = resolveEllipsePoint(o, startAngle, radius, radius, 0)
  var e = resolveEllipsePoint(o, endAngle, radius, radius, 0)
  if counterclockwise:
    swap(s, e)
  if path.subpaths.len > 0:
    path.addStraightSegment(s)
  else:
    path.moveTo(s)
  path.addArcSegment(o, e, radius, abs(startAngle - endAngle) < PI)

proc ellipse*(path: Path; x, y, radiusX, radiusY, rotation, startAngle,
    endAngle: float64; counterclockwise: bool) =
  for v in [x, y, radiusX, radiusY, rotation, startAngle, endAngle]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  let o = Vector2D(x: x, y: y)
  var s = resolveEllipsePoint(o, startAngle, radiusX, radiusY, rotation)
  var e = resolveEllipsePoint(o, endAngle, radiusX, radiusY, rotation)
  if counterclockwise:
    swap(s, e)
  if path.subpaths.len > 0:
    path.addStraightSegment(s)
  else:
    path.moveTo(s)
  path.addEllipseSegment(o, e, radiusX, radiusY)

proc rect*(path: Path; x, y, w, h: float64) =
  for v in [x, y, w, h]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  path.addSubpathAt(Vector2D(x: x, y: y))
  path.addStraightSegment(Vector2D(x: x + w, y: y))
  path.addStraightSegment(Vector2D(x: x + w, y: y + h))
  path.addStraightSegment(Vector2D(x: x, y: y + h))
  path.addStraightSegment(Vector2D(x: x, y: y))
  path.addSubpathAt(Vector2D(x: x, y: y))

proc roundRect*(path: Path; x, y, w, h, radii: float64) =
  for v in [x, y, w, h]:
    if classify(v) in {fcInf, fcNegInf, fcNan}:
      return
  #TODO implement
  path.rect(x, y, w, h) # :P