-
Notifications
You must be signed in to change notification settings - Fork 6
/
curves.py
1754 lines (1564 loc) · 70.1 KB
/
curves.py
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2022 Google LLC
# Copyright 2022 Raph Levien
# Copyright 2023 Jens Zamanian
# DISCLAIMER:
# A big part of this file is based on the ideas found in the blogpost:
# https://raphlinus.github.io/curves/2022/09/09/parallel-beziers.html
# and parts of the code is adapted from the in the interactive
# demo on that page (source can be found at: raphlinus/raphlinus.github.io).
# However, all the code have been rewritten in Python and almost
# all of it is modified so any bugs/errors are my fault (JZ).
# Hopefully it is correct to consider this a derived work and
# hence retain the Apache 2.0 licence for this file.
# END DISCLAIMER
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Classes and utility functions for Bezier curves."""
# Bezier: Contains all data and operations needed to define and work with a Bezier curve.
# Spline: Contains a list of Bezier curves.
# 1. How should we init this class? Either we init by passing all the points and the class creates and stores the Bezier instances, or we can init by passing pre-fabricated Bezier instances.
# 2. Can we programme for both options? Either with wargs or kwargs.
# Curves
# Again we need to think about how to init these.
##### CONSTANTS #####
INTERSECTION_THRESHOLD = 1e-6 # Threshold for when to stop subdividing when finding intersections.
TUPLE_FILTER_THRESHOLD = .2e-1 # Threshold for when two intersections are assumed to be the same.
OFFSET_TOLERANCE = 1e-4
##### END: CONSTANTS #####
import mathutils
import math
import bpy
import itertools
import numpy as np
from typing import Optional
from . import solvers
from .gauss_legendre import GAUSS_LEGENDRE_COEFFS_32
##### UTILITY FUNCTIONS #####
def add_line(a: mathutils.Vector, b: mathutils.Vector):
"""Add a line between a and b in Blender."""
me = bpy.data.meshes.new('Line')
verts = [a, b]
edges = [(0,1)]
faces = []
me.from_pydata(verts, edges, faces)
ob = bpy.data.objects.new('Line', me)
bpy.data.collections['Collection'].objects.link(ob)
def add_square(p: mathutils.Vector, r = 0.1):
"""Adds a square to Blender at position p with side r."""
me = bpy.data.meshes.new('Square')
x = mathutils.Vector((1,0,0))
y = mathutils.Vector((0,1,0))
verts = [p + r * (x + y) / 2, p + r * (x - y) / 2, p - r * (x + y) / 2, p - r * (x - y) / 2]
edges = [(0,1), (1,2), (2,3), (3,0), (0,2), (1,3)]
faces = []
me.from_pydata(verts, edges, faces)
ob = bpy.data.objects.new(me.name, me)
bpy.data.collections['Collection'].objects.link(ob)
def filter_duplicates(tuples, threshold = TUPLE_FILTER_THRESHOLD):
"""Filter out tuples that differ less than threshold."""
result = []
for tup in tuples:
if not any(tuple_is_close(tup, other, threshold) for other in result):
result.append(tup)
# excluded = [(0, 0), (1, 0), (0, 1), (1, 1)]
# final = []
# for tup in result:
# if not any(tuple_is_close(tup, other, threshold) for other in excluded):
# final.append(tup)
return result
def tuple_is_close(a: tuple[float, float], b: tuple[float, float], threshold = TUPLE_FILTER_THRESHOLD):
"""Checks if two tuples a, and b, differ less then threshold.
(a, b) is close to (a', b') if (a - a') < threshold and abs(b - b') < threshold."""
comparisons = all(math.isclose(*c, abs_tol = threshold) for c in zip(a,b))
return comparisons
##### END: UTILITY FUNCTIONS #####
class CurveObject():
"""Base class for all curves."""
__slots__ = ("name",
"_location",
"_scale",
"_rotation")
# Methods:
# - boundary box
# - intersections
# - self intersections
# - create blender curve object
def __init__(self,
name = "Curve Object",
location = mathutils.Vector((0.0 ,0.0 ,0.0)),
scale = mathutils.Vector((1.0, 1.0, 1.0)),
rotation = mathutils.Euler((0.0, 0.0, 0.0),'XYZ')
):
self.name = name
self.location = location
self.scale = scale
self.rotation = rotation
@property # Use property for location and rotation so that subclasses can do that do.
def location(self):
return self._location
@location.setter
def location(self, location: mathutils.Vector):
self._location = location
@property
def scale(self):
return self._scale
@scale.setter
def scale(self, scale: mathutils.Vector):
self._scale = scale
@property
def rotation(self):
return self._rotation
@rotation.setter
def rotation(self, rotation: mathutils.Euler):
self._rotation = rotation
class QuadraticBezier():
"""Class to handle some functions of a quadratic Bezier curve.
Used mainly for handling derivatives, etc, of a cubic Bezier."""
__slots__ = ("points")
def __init__(self, p0: mathutils.Vector, p1: mathutils.Vector, p2: mathutils.Vector):
self.points = [p0, p1, p2]
def __call__(self, t: float):
p = self.points
return p[0] * (1 - t)**2 + 2 * p[1] * (1 - t)*t + p[2] * t**2
def eval_derivative(self, t: float):
"""Evaluates the derivative of the curve at parameter t."""
p = self.points
return -2 * p[0] * (1 - t) - 2 * p[1] * t + 2 * p[1] * (1 - t) + 2 * p[2] * t
class Bezier(CurveObject):
"""
Bezier curve of 3rd order. p0, p1, p2, p3 are mathutils.Vector
t0 and t1 are the parameter time at the start and the end of the
original curve for instances created as splits of larger curves.
"""
__slots__ = ("points",
"t0", "t1",
"start_handle_left",
"end_handle_right",
"is_closed" # In Blender, a single Bezier curve can be toggled closed (it is really handled as a spline).
)
def __init__(self,
p0: mathutils.Vector,
p1: mathutils.Vector,
p2: mathutils.Vector,
p3: mathutils.Vector,
start_handle_left: Optional[mathutils.Vector] = None,
end_handle_right: Optional[mathutils.Vector] = None,
t0 = 0.0,
t1 = 1.0,
is_closed = False,
name = "Bezier",
location: mathutils.Vector = mathutils.Vector((0.0, 0.0, 0.0)),
scale = mathutils.Vector((1.0, 1.0, 1.0)),
rotation = mathutils.Euler((0.0, 0.0, 0.0), 'XYZ'),
) -> None:
"""
Initializes the cubic Bezier and sets its points and degree.
The points should be mathutils.Vectors of some fixed dimension.
The number of points should be 3 or higher.
"""
super().__init__(name, location, scale, rotation)
self.points = [p0, p1, p2, p3]
# The dangling handles of a Bezier curve in Blender are not really part of a mathematical curve.
# Instead they belong to the previous or next Bezier in case of a poly-bezier curve.
# Since Blender uses them, it is better to keep them.
self.start_handle_left = start_handle_left
self.end_handle_right = end_handle_right
self.is_closed = is_closed # In Blender, single Bezier curves can be toggled closed.
# t0 and t1 give the parameter values of the parent curve in case this is created from a split.
# Needed for keeping track of intersections.
# TODO: Might not need this with the new algorithm (but perhaps to find intersections).
self.t0 = t0
self.t1 = t1
# self.handle_linear()
@classmethod
def from_Blender(cls, name: str):
"""Alternative constructor to read and import a Bezier curve from Blender.
This assumes that the named object is only a simple bezier curve,
if the Blender object is a spline, only the first part of the curve will
be imported. Use Spline.from_Blender() instead in those cases."""
cu = bpy.data.collections['Collection'].objects[name]
spline = cu.data.splines[0]
is_closed = spline.use_cyclic_u
bezier_points = spline.bezier_points
start_handle_left = bezier_points[0].handle_left
p0: mathutils.Vector = bezier_points[0].co
p1: mathutils.Vector = bezier_points[0].handle_right
p2: mathutils.Vector = bezier_points[1].handle_left
p3: mathutils.Vector = bezier_points[1].co
end_handle_right: mathutils.Vector = bezier_points[1].handle_right
loc: mathutils.Vector = cu.location
sca: mathutils.Vector = cu.scale
rot: mathutils.Euler = cu.rotation_euler
return cls(p0, p1, p2, p3,
name = name,
location = loc,
scale = sca,
rotation = rot,
start_handle_left = start_handle_left,
end_handle_right = end_handle_right,
is_closed = is_closed
)
def handle_linear(self):
"""Handles the cases where either or both of the control handles
coincides with the start or endpoints."""
# TODO: Remove this. Handles that conicide with start/endpoints are
# only a problem for offsetting.
# Handle it there.
# Not used.
p0 = self.points[0]
p1 = self.points[1]
p2 = self.points[2]
p3 = self.points[3]
if p1 == p0:
print("Left linear")
n = 2
p1_new = self(1/2**n) - p0
while p1_new.length > 0 and n < 100:
n += 1
p1_new = self(1/2**n) - p0
self.points[1] = self(1/2**(n-1))
print(n)
if p2 == p3:
print("Right linear")
n = 2
p2_new = self(1 - 1/2**n)
while p2_new.length> 0 and n < 100:
n += 1
p2_new = p3 - self(1 - 1/2**n)
print(n)
self.points[2] = self(1 - 1/2**(n-1))
def __repr__(self):
"""Prints the name of the together with all the points. """
p = self.points
string = '<' + self.name + '\n'
string += "p0: " + str(p[0]) + '\n'
string += "p1: " + str(p[1]) + '\n'
string += "p2: " + str(p[2]) + '\n'
string += "p3: " + str(p[3]) + '\n'
string += "start_handle_left: " + str(self.start_handle_left) + '\n'
string += "end_handle_right: " + str(self.end_handle_right) + '>'
return string
def __call__(self, t: float, world_space: bool = False) -> mathutils.Vector:
""" Returns the value at parameter t.
If world_space = False, the position is calculated relative
to the origin of the Bezier."""
p: list[mathutils.Vector] = self.points
pos = p[0] * (1 - t)**3 + 3 * p[1] * (1 - t)**2 * t + 3 * p[2] * (1 - t) * t**2 + p[3] * t**3
if world_space:
if self.rotation.z != 0.0:
rot = self.rotation.to_matrix() # type: ignore
pos: mathutils.Vector = rot @ pos + self.location #type: ignore
return pos
else:
return pos + self.location
else:
return pos
def reverse(self):
"""Reverses the direction of the curve."""
self.points = list(reversed(self.points))
self.start_handle_left, self.end_handle_right = self.end_handle_right, self.start_handle_left
def set_point(self, point: mathutils.Vector, i: int):
"""Sets the point with index i of the Bezier."""
# TODO: Remove this.
self.points[i] = point
def translate_origin(self, vector: mathutils.Vector):
"""Translates the origin of the Bezier to the position given by
vector without changing the world position of the curve.
"""
dist = self.location - vector
self.points = [p + dist for p in self.points]
self.start_handle_left = self.start_handle_left + dist
self.end_handle_right = self.end_handle_right + dist
self.location = vector
def eval_derivative(self, t: float):
p = self.points
return 3 * (p[1] - p[0]) * (1-t)**2 + 6 * (p[2] - p[1]) * (1-t) * t + 3 * (p[3] - p[2]) * t**2
def eval_second_derivative(self, t: float):
"""Evaluate the second derivative at parameter t."""
# TODO: Only used in curvature, which is currently not used for anything.
p = self.points
return 6 * (p[0] - 2 * p[1] + p[2]) * (1-t) + 6 * (p[1] - 2 * p[2] + p[3]) * t
def derivative(self):
"""Returns the derivative (quadratic Bezier) curve of self."""
p = self.points
return QuadraticBezier(3 * (p[1] - p[0]), 3 * (p[2] - p[1]), 3 * (p[3] - p[2]))
def tangent(self, t: float):
"""Calculate the unit tangent at parameter t."""
# TODO: This is just a convenience function and can be removed.
derivative = self.eval_derivative(t)
if derivative.length > 0.0:
return derivative / derivative.length #mathutils.vector.length
else:
return derivative # mathutils.Vector((0,0,0))
def normal(self, t: float):
"""Return the tangent rotated 90 degrees anti-clockwise."""
# The real normal always points in the direction the curve is turning
# so we should probably call this something else.
der = self.eval_derivative(t)
unit_der = der / der.length
return mathutils.Vector((-unit_der.y, unit_der.x, 0.0))
def curvature(self, t):
"""Returns the curvature at parameter t."""
# TODO: Can probably be removed.
d = self.eval_derivative(t)
sd = self.eval_second_derivative(t)
denom = math.pow(d.length, 3 / 2)
return (d[0] * sd[1] - sd[0] * d[1]) / denom
def area(self) -> float:
"""Returns the (signed) area of the curve."""
p = self.points
# Precalculated in terms of the points using Green's theorem.
# TODO: This os not really used for anything.
# The area we need is the area of the sampled curve which is calculated
# by GAUSS_LEGENDRE_COEFFS_32. REMOVE?
x0 = p[0].x
y0 = p[0].y
x1 = p[1].x
y1 = p[1].y
x2 = p[2].x
y2 = p[2].y
x3 = p[3].x
y3 = p[3].y
area = 3 / 20 * (x0 * (-2 * y1 - y2 + 3 * y3) + x1 * (2 * y0 - y2 - y3) + x2 * (y0 + y1 - 2 * y3) + x3 * (-3 * y0 + y1 + 2 * y2))
# area = 3 / 20 * (
# p[0][0] * ( - 2 * p[1][1] - p[2][1] + 3 * p[3][1] )
# + p[1][0] * ( 2 * p[0][1] - p[2][1] - p[3][1] )
# + p[2][0] * ( p[0][1] + p[1][1] - 2 * p[3][1] )
# + p[3][0] * ( - 3 * p[0][1] + p[1][1] + 2 * p[2][1] )
# )
return area
def x_moment(self):
"""Calculate the x_moment of a curve that starts at the origin
and ends on the x-axis."""
# TODO: This is only relevant in this version for the offset.
# However, it is not used. Remove.
p = self.points
x1 = p[1][0]
y1 = p[1][1]
x2 = p[2][0]
y2 = p[2][1]
x3 = p[3][0]
moment = -9*x1**2*y2/280 + 9*x1*x2*y1/280 - 9*x1*x2*y2/280 + 3*x1*x3*y1/140 + 9*x2**2*y1/280 + 3*x2*x3*y1/56 + 3*x2*x3*y2/56 + x3**2*y1/28 + x3**2*y2/8
return moment
def eval_offset(self, t: float, d: float):
"""Calculates the vector from self(t) to the offset at parameter t."""
# Could be calculated via the normal as (t) + d * N(t),
# but the normal always points in the direction the curve
# is turning.
# By using a rotation we instead always get a rotation to the left
# side of the curve and we can control which direction the offset is.
n = None
dp = self.eval_derivative(t)
if dp.length > 0.0:
s = d / dp.length
else:
if t == 0.0:
p1 = self(0.0001)
dp = p1 - self.points[0]
s = d / dp.length
elif t == 1.0:
p2 = self(.9999)
dp = self.points[3] - p2
s = d / dp.length
else:
dp = mathutils.Vector((0, 0, 0))
s = 0
return mathutils.Vector((-s * dp[1], s * dp[0], 0))
def aligned(self):
"""Returns the points of the corresponding aligned curve.
Aligned means: start point in origin, end point on x-axis.
"""
# TODO: If this is needed, rewrite the code so that usage of mathutils is minimized.
# mathutils is not exact enough.
# Otherwise, remove.
m = mathutils.Matrix.Translation(-self.points[0])
end = m @ self.points[3]
if end[0] != 0.0:
angle = -math.atan2(end[1],end[0])
else:
angle = 0.0
k = mathutils.Matrix.Rotation(angle, 4, 'Z') @ m
aligned_points = []
for p in self.points:
aligned_points.append(k @ p)
return aligned_points
def bounding_box(self, world_space = False):
"""Calculate the bounding box of the curve.
Returns dict('min_x', 'max_x', 'min_y', 'max_y')"""
# TODO: Make it possible to calculate the tight bounding box if this
# is deemed useful.
extrema = self.extrema()
x_values = [self(t, world_space = world_space)[0] for t in extrema]
y_values = [self(t, world_space = world_space)[1] for t in extrema]
min_x = min(x_values)
max_x = max(x_values)
min_y = min(y_values)
max_y = max(y_values)
area = (max_x - min_x) * (max_y - min_y)
return {'min_x': min_x, 'max_x': max_x, 'min_y': min_y, 'max_y': max_y, 'area': area}
def extrema(self):
"""
Returns the parameter values for the minimum and maximum of the curve
in the x and y coordinate.
"""
# TODO: Clean up using e.g. itertools.
# TODO: This must take the rotation into account when that is added.
p0, p1, p2, p3 = self.points
a = 3 * (-p0 + 3 * p1 - 3 * p2 + p3)
b = 6 * (p0 - 2*p1 + p2)
c = 3*(p1 - p0)
# Solve for all points where x'(t) = 0 and y'(t) = 0.
tx_roots = solvers.solve_quadratic(a.x, b.x, c.x)
ty_roots = solvers.solve_quadratic(a.y, b.y, c.y)
roots = [0.0, 1.0] # Extrema can also occur at the endpoints.
roots.extend([t for t in tx_roots if isinstance(t, float) and 0.0 <= t <= 1.0])
roots.extend([t for t in ty_roots if isinstance(t, float) and 0.0 <= t <= 1.0])
return roots
def split(self, t0: float, t1: Optional[float] = None):
# TODO: Remove this
"""Splits the Bezier curve at the parameter(s) t0 (and t1).
In case just one parameter value is given, a list of two curves
is returned.
Else, a single curve, corresponding to the curve between
t0 and t1 is returned.
Based on: https://github.com/Pomax/BezierInfo-2
The code for this function is almost a straight translation
of the JavaScript code in the ref above int Python.
"""
loc = self.location
sca = self.scale
rot = self.rotation
if t0 == 0.0 and t1 is not None:
return self.split(t1)[0]
elif t1 == 1.0:
return self.split(t0)[1]
else:
p = self.points
new1 = p[0] * (1 - t0) + p[1] * t0
new2 = p[1] * (1 - t0) + p[2] * t0
new3 = p[2] * (1 - t0) + p[3] * t0
new4 = new1 * (1 - t0) + new2 * t0
new5 = new2 * (1 - t0) + new3 * t0
new6 = new4 * (1 - t0) + new5 * t0
# result[0].t0 = self.map_split_to_whole(0)
# result[0].t1 = self.map_split_to_whole(t0)
# result[1].t0 = self.map_split_to_whole(t0)
# result[1].t1 = self.map_split_to_whole(1)
t00 = self.map_split_to_whole(0)
t10 = self.map_split_to_whole(t0)
t01 = self.map_split_to_whole(t0)
t11 = self.map_split_to_whole(1)
result = [Bezier(p[0], new1, new4, new6, t0 = t00, t1 = t10, location = loc, scale = sca, rotation = rot),
Bezier(new6, new5, new3, p[3], t0 = t01, t1 = t11, location = loc, scale = sca, rotation = rot)]
# The new split curves should keep track for the original
# parameter values at the end points (t0 and t1).
if t1 is None:
return result
else:
# Calculate which parameter of the split curve (result[1])
# which corresponds to the point t1 on the whole curve.
# Then split again at that point.
t1p = self.map_whole_to_split(t1, t0, 1)
return result[1].split(t1p)[0]
def whole_parameter_to_split(self, t: float):
ds = self.t0
de = self.t1
return (t - ds) / (de - ds)
def subsegment(self, t0: float, t1: float):
"""Splits out the subsegment between t0 and t1.
If the curves have been previously split, we can insert the parameter
of the original whole curve where we want to split the curve."""
p0 = self(t0)
p3 = self(t1)
factor = (t1 - t0) / 3
d1 = self.eval_derivative(t0)
p1 = p0 + factor * d1
d2 = self.eval_derivative(t1)
p2 = p3 - factor * d2
t0new = self.map_split_to_whole(t0)
t1new = self.map_split_to_whole(t1)
# TODO: The below name is perhaps not so good.
name = self.name + '(' + str(t0new) + ',' + str(t1new) + ')'
loc = self.location
sca = self.scale
rot = self.rotation
return Bezier(p0, p1, p2, p3,
t0 = t0new, t1 = t1new,
name = name,
location = loc,
scale = sca,
rotation = rot)
def split2(self, *parameters: float):
"""Split the curve at parameters. Returns a list of the split segments."""
# TODO: Rename this to split as soon as the other one is removed.
ts = sorted([t for t in parameters])
if ts[0] != 0.0:
ts.insert(0, 0.0)
if ts[-1] != 1.0:
ts.append(1.0)
sub_curves: list[Bezier] = []
for i in range(1, len(ts)):
sub_curves.append(self.subsegment(ts[i-1], ts[i]))
return sub_curves
@staticmethod
def map_whole_to_split(t, ds, de):
"""Returns the parameter in the splitted curve
corresponding to the parameter t of the whole (unsplitted) curve.
t1 is the parameter value which we want to map and
the split curve runs from parameter ds to de of the whole curve.
Ref: http://web.mit.edu/hyperbook/Patrikalakis-Maekawa-Cho/node13.html
"""
return (t - ds) / (de - ds)
def map_split_to_whole(self, t: float):
"""Returns the parameter value of the whole curve,
corresponding to the parameter t in the splitted curve.
"""
return self.t0 + (self.t1 - self.t0) * t
def _create_Blender_curve(self):
"""Creates a new curve object in Blender."""
# TODO: Catch the name of the object created by ob.name.
# and store this for later reference?
cu = bpy.data.curves.new(self.name, 'CURVE')
ob = bpy.data.objects.new(self.name, cu)
ob.location = self.location
ob.scale = self.scale
ob.rotation_euler = self.rotation
bpy.data.collections['Collection'].objects.link(ob)
ob.data.resolution_u = 64
cu.splines.new('BEZIER') # Add a first spline to Blender.
return cu
def add_to_Blender(self, blender_curve_object = None, stringed = False):
"""Adds the Bezier curve to Blender as splines.
blender_curve_object: an existing curve in Blender.
stringed: is a bool which is true in case the Bezier curve is part of a Spline.
Both the parameters are used to make it possible to reuse this in the case where
the Bezier curve is part of a Spline.
"""
# Stringed = True means that the Bezier as part of a Spline.
# The end and beginning start_handle_left and end_handle_right
# should not be set, since these are set by the previous and the
# next Bezier curves in the spline.
# where the end point of one curve coincides with the start point
# of the next.
p = self.points
cu = blender_curve_object or self._create_Blender_curve()
spline = cu.splines[-1]
spline.use_cyclic_u = self.is_closed
bezier_points = spline.bezier_points
bezier_points[-1].handle_right = p[1]
bezier_points.add(1)
bezier_points[-1].handle_left = p[2]
bezier_points[-1].co = p[3]
if not stringed:
# If not part of spline set also the first point and the dangling handles.
bezier_points[-2].co = p[0]
bezier_points[-2].handle_left = self.start_handle_left or p[0]
bezier_points[-1].handle_right = self.end_handle_right or p[3]
def overlaps(self, bezier):
"""Check if the bounding box of self and Bezier overlaps."""
# 0 1 2 3
# min_x, max_x, min_y, max_y
bb1 = self.bounding_box(world_space = True)
bb2 = bezier.bounding_box(world_space = True)
if bb1['min_x'] >= bb2['max_x'] or bb2['min_x'] >= bb1['max_x'] or bb1['min_y'] >= bb2['max_y'] or bb2['min_y'] >= bb1['max_y']:
return False
else:
return True
def transform(self, angle = 0.0, translation = mathutils.Vector((0,0,0)), world_space = False):
"""Rotates the curve an angle around the z axis and then translates it.
If world_space, then the curve object is transformed instead of the local coordinates."""
# TODO: Implement the world_space option. Use the superclass and put the rotation there.
if world_space:
print("NOT YET IMPLEMENTED: Will do local tranform instead.")
m = mathutils.Matrix.Rotation(angle, 3, 'Z')
p = self.points
q0: mathutils.Vector = m @ p[0] + translation # type: ignore
q1: mathutils.Vector = m @ p[1] + translation # type: ignore
q2: mathutils.Vector = m @ p[2] + translation # type: ignore
q3: mathutils.Vector = m @ p[3] + translation # type: ignore
self.points = [q0, q1, q2, q3] # type: ignore
def intersect_ray(self, sp: mathutils.Vector, d: mathutils.Vector):
"""Find intersection of the cubic with a ray from point sp in the direction d.
Return the parameter value t where the intersection occurs (if any)."""
points = self.points
p0 = points[0]
p1 = points[1]
p2 = points[2]
p3 = points[3]
c0 = d.x * (p0.y - sp.y) + d.y * (sp.x - p0.x)
c1 = 3 * d.x * (p1.y - p0.y) + 3 * d.y * (p0.x - p1.x)
c2 = 3 * d.x * (p0.y - 2 * p1.y + p2.y) - 3 * d.y * (p0.x - 2 * p1.x + p2.x)
c3 = d.x * (-p0.y + 3 * p1.y - 3 * p2.y + p3.y) + d.y * (p0.x - 3 * p1.x + 3 * p2.x - p3.x)
qs = solvers.solve_cubic(c3, c2, c1, c0)
sols: list[float] = [s for s in qs if isinstance(s, float) and 0.0 < s < 1.0]
return sols
def find_offset_cusps(self, d: float):
"""Find the parameters values where the curvature is equal to the inverse of the distance d."""
results = []
n = 200
q = self.derivative()
ya = 0.0
last_t = 0.0
t0 = 0.0
for i in range(0,n+1):
t = i / n
ds = q(t).length
# Curvature
k = (q.eval_derivative(t).x * q(t).y - q.eval_derivative(t).y * q(t).x) / ds**3
yb = k * d + 1
if i != 0:
# Sign has changed
if ya * yb < 0:
tx = (yb * last_t - ya * t) / (yb - ya)
iv = {'t0': t0, 't1': tx, 'sign': math.copysign(1, ya)}
results.append(iv)
t0 = tx
ya = yb
last_t = t
last_iv = {'t0': t0, 't1': 1.0, 'sign': math.copysign(1, ya)}
results.append(last_iv)
return results
def offset(self, d: float, double_side: bool = False):
cusps = self.find_offset_cusps(d)
loffsets: list[Bezier] = []
roffsets: list[Bezier] = []
for cusp in cusps:
curve = self.subsegment(cusp['t0'], cusp['t1'])
off = OffsetBezier(curve, d, cusp['sign'])
offset = off.find_cubic_approximation()
if offset is not None:
for bez in offset:
bez.location = self.location
bez.scale = self.scale
bez.rotation = self.rotation
loffsets.extend(offset)
if double_side:
cusps = self.find_offset_cusps(-d)
for cusp in cusps:
curve = self.subsegment(cusp['t0'], cusp['t1'])
off = OffsetBezier(curve, -d, cusp['sign'])
offset = off.find_cubic_approximation()
if offset is not None:
for bez in offset:
# TODO: In case we just create the splines directly,
# location, etc, can be set in the resulting spline
# only.
bez.location = self.location
bez.scale = self.scale
bez.rotation = self.rotation
roffsets.extend(offset)
return [loffsets, roffsets]
def stroke(self, d: float):
# d = self.d
offsets = self.offset(d, double_side = True)
left_offset = Spline(*offsets[0], location = self.location, rotation = self.rotation)
right_offset = Spline(*offsets[1], location = self.location, rotation = self.rotation)
left_offset.name = 'Left'
right_offset.name = 'Right'
left_offset.add_to_Blender()
right_offset.add_to_Blender()
def find_self_intersection(self):
"""Finds the self intersection of the curve (if any).
Returns three parameter values.
Ref: https://comp.graphics.algorithms.narkive.com/tqLNEZqM/cubic-bezier-self-intersections
"""
p0 = self.points[0]
p1 = self.points[1]
p2 = self.points[2]
p3 = self.points[3]
H1 = -3 * p0 + 3 * p1
H2 = 3 * p0 - 6 * p1 + 3 * p2
H3 = - p0 + 3 * p1 - 3 * p2 + p3
if H3 == mathutils.Vector((0,0,0)):
return None
A = H2.x / H3.x
B = H1.x / H3.x
P = H2.y / H3.y
Q = H1.y / H3.y
if A == P or Q == B:
return None
k = (Q - B) / (A - P)
r0 = (- k**3 - A * k**2 - B * k ) / 2
r1 = (3 * k**2 + 2 * k * A + 2 * B) / 2
r2 = - 3 * k / 2;
sols = solvers.solve_cubic(1.0, r2, r1, r0)
if sols:
solutions: list[float] = []
for s in sols:
if (s >= 0.0 and s <= 1.0):
solutions.append(s)
solutions.sort()
if len(solutions) == 3:
# The middle solution is a rouge solution.
# The first and last are the two parameter values where it meets itself.
# Only need the first.
return [solutions[0], solutions[2]]
return None
def find_intersections(self, bez):
"""Find the intersection between self and other bez.
Returns a list of parameter value pairs (t, t') so that self(t) = bez(t').
"""
# TODO: Not in use. use curve_intersections instead.
treshold = 1e-12
bb1 = self.bounding_box(world_space = True)
bb2 = bez.bounding_box(world_space = True)
if bb1['min_x'] >= bb2['max_x'] or bb2['min_x'] >= bb1['max_x'] or bb1['min_y'] >= bb2['max_y'] or bb2['min_y'] >= bb1['max_y']:
return None
else:
if bb1['area'] * bb2['area'] < treshold:
if (self.t0 < INTERSECTION_THRESHOLD or self.t1 > 1.0 - INTERSECTION_THRESHOLD) and (bez.t0 < INTERSECTION_THRESHOLD or bez.t1 > 1.0 - INTERSECTION_THRESHOLD):
return None
return [(self.t0, self.t1, bez.t0, bez.t1)]
else:
bb1s = self.split(0.5)
bb2s = bez.split(0.5)
inters = []
a = bb1s[0].find_intersections(bb2s[0])
b = bb1s[0].find_intersections(bb2s[1])
c = bb1s[1].find_intersections(bb2s[0])
d = bb1s[1].find_intersections(bb2s[1])
if a:
inters.extend(a)
if b:
inters.extend(b)
if c:
inters.extend(c)
if d:
inters.extend(d)
if inters:
return inters
else:
return None
def curve_intersections(self, c2: 'Bezier', threshold = INTERSECTION_THRESHOLD):
"""Recursive method used for finding the intersection between two Bezier curves, c1, and c2.
"""
# TODO: Better
# Since the class Bezier is not defined at this point,
# we use 'Bezier' for the type annotation to make the typing system work properly.
# TODO: Check for endpoint matching.
# For now, we simply remove tuples where both values are sufficiently close to 0 or 1.
# Probably we can save some time by checking this earlier.
# if self.points[3] == c2.points[0]:
# print("HOPSAN")
# print(self.points[3], c2.points[0])
# bez0 = self.split2(.9999)[0]
# c2 = c2.split2(.001)[1]
# else:
# bez0 = self
threshold2 = threshold * threshold
bez0 = self
results: list[tuple[float, float]] = []
# if bez0.t1 - bez0.t0 < threshold and c2.t1 - c2.t0 < threshold:
if bez0.bounding_box()['area'] < threshold2 and c2.bounding_box()['area'] < threshold2:
# return [((bez0.t0 + bez0.t1)/2 , (c2.t0 + c2.t1)/2)]
if (bez0.t0 < TUPLE_FILTER_THRESHOLD or bez0.t1 > 1.0 - TUPLE_FILTER_THRESHOLD) and (c2.t0 < TUPLE_FILTER_THRESHOLD or c2.t1 > 1.0 - TUPLE_FILTER_THRESHOLD):
return []
else:
return [(bez0.t0, bez0.t1, c2.t0, c2.t1)]
cc1 = bez0.split2(0.5)
cc2 = c2.split2(0.5)
pairs = itertools.product(cc1, cc2)
pairs = list(filter(lambda x: x[0].overlaps(x[1]), pairs))
if len(pairs) == 0:
return results
for pair in pairs:
results += pair[0].curve_intersections(pair[1], threshold)
results = filter_duplicates(results)
return results
### Start: Are these used?
def fat_line(self):
"""Used for Bezier clipping:
T.W. Sederberg, T. Nishita, Curve intersection using Bezier clipping
https://doi.org/10.1016/0010-4485(90)90039-F
"""
p0 = self.points[0]
p1 = self.points[1]
p2 = self.points[2]
p3 = self.points[3]
t = p3 - p0
# TODO: Check for zero curve?
n = mathutils.Vector((-t.y, t.x, 0.0))
n = n / n.length
c = - p0.dot(n)
d1 = p1.dot(n) + c
d2 = p2.dot(n) + c
if d1 * d2 > 0:
k = 3 / 4
else:
k = 4 / 9
dmin = k * min(0, d1, d2)
dmax = k * max(0, d1, d2)
return dmin, dmax, n, c
def fat_line_distance(self, bez: 'Bezier'):
"""Calculate the distance from the control points of self
to the fat line of bez.
"""
dmin, dmax, n, c = bez.fat_line()
d0 = self.points[0].dot(n) + c
d1 = self.points[1].dot(n) + c
d2 = self.points[2].dot(n) + c
d3 = self.points[3].dot(n) + c
return d0, d1, d2, d3, dmin, dmax
def intersect_fat_line(self, bez: 'Bezier'):
d0, d1, d2, d3, dmin, dmax = self.fat_line_distance(bez)
D0 = mathutils.Vector((0.0, d0, 0.0))
D1 = mathutils.Vector((1 / 3, d1, 0.0))
D2 = mathutils.Vector((2 / 3, d2, 0.0))
D3 = mathutils.Vector((1.0, d3, 0.0))
L0 = mathutils.Vector((0.0 ,dmin,0.0))
L1 = mathutils.Vector((1.0 ,dmin,0.0))
L2 = mathutils.Vector((0.0 ,dmax,0.0))
L3 = mathutils.Vector((1.0 ,dmax,0.0))
a = mathutils.geometry.intersect_line_line_2d(D0, D1, L0, L1)
b = mathutils.geometry.intersect_line_line_2d(D0, D1, L2, L3)
c = mathutils.geometry.intersect_line_line_2d(D0, D3, L0, L1)
d = mathutils.geometry.intersect_line_line_2d(D0, D3, L2, L3)
e = mathutils.geometry.intersect_line_line_2d(D1, D2, L0, L1)
f = mathutils.geometry.intersect_line_line_2d(D1, D2, L2, L3)
g = mathutils.geometry.intersect_line_line_2d(D2, D3, L0, L1)
h = mathutils.geometry.intersect_line_line_2d(D2, D3, L2, L3)
L = [a, b, c, d, e, f, g, h]
L = [x[0] for x in L if x is not None]
tmin = min(L)
tmax = max(L)
return tmin, tmax
def find_intersections2(self, bez: 'Bezier'):
"""Hej"""
# TODO: Abandon this version of bez-bez intersection?
# Instead we can check for bad cases, e.g. when the curve
# is close to itself and remove these.
ta1, tb1 = self.intersect_fat_line(bez)
a = self.subsegment(ta1, tb1)
ta2, tb2 = bez.intersect_fat_line(a)
b = bez.subsegment(ta2, tb2)
ta3, tb3 = a.intersect_fat_line(b)
a1 = a.subsegment(ta3, tb3)
ta4, tb4 = b.intersect_fat_line(a1)
b1 = b.subsegment(ta4, tb4)
print(ta4, tb4, b1.t0, b1.t1)
ta5, tb5 = a1.intersect_fat_line(b1)
print(ta5, tb5)
# End: Are these used?
class Spline(CurveObject):
"""A list of Bezier curves corresponds to a single spline object.
For each Bezier, the end point coincide with the starting point of
the next curve."""
# TODO: Handle offsets when the handles at a point are not aligned!
# TODO: Handle endcaps.
# TODO: Handle intersections between two splines.
# TODO: Handle massaging of the offset curve so that all intersections are
# combined.
__slots__ = ('beziers', 'is_closed', 'strokewidth')
def __init__(self,
*beziers: Bezier,
is_closed = False,
strokewidth = 0.01,
name = "Spline",
location = mathutils.Vector((0.0 ,0.0, 0.0)),
scale = mathutils.Vector((1.0, 1.0, 1.0)),
rotation = mathutils.Euler((0,0,0), 'XYZ'),
):
self.beziers = list(beziers)
# Ensure that the end point and handles of one point, coincides with the corresponding for the next point.
prev_bez = None
for bez in self.beziers:
if not prev_bez:
prev_bez = bez
continue
bez.points[0] = prev_bez.points[3]
bez.start_handle_left = prev_bez.points[2]
prev_bez = bez
self.is_closed = is_closed
self.strokewidth = strokewidth
# Reset all the Beziers that comes from a split curve.
for bezier in self.beziers:
bezier.t0 = 0.0
bezier.t1 = 1.0
super().__init__(name, location, scale, rotation)
@classmethod
def from_Blender(cls, name: str):
"""Alternative constructor where the Spline is imported from Blender."""