Welcome to mirror list, hosted at ThFree Co, Russian Federation.

curve_utils.py « modules - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 529ddd0c0370b348bedee52657e8897abbe6ff8b (plain)
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# <pep8 compliant>

import bpy


def vis_curve_object():
    scene = bpy.data.scenes[0]  # weak!
    cu = bpy.data.curves.new(name="Line", type='CURVE')
    ob = bpy.data.objects.new(name="Test", object_data=cu)
    ob.layers = [True] * 20
    base = scene.objects.link(ob)
    return ob


def vis_curve_spline(p1, h1, p2, h2):
    ob = vis_curve_object()
    spline = ob.data.splines.new(type='BEZIER')
    spline.bezier_points.add(1)
    spline.bezier_points[0].co = p1.to_3d()
    spline.bezier_points[1].co = p2.to_3d()

    spline.bezier_points[0].handle_right = h1.to_3d()
    spline.bezier_points[1].handle_left = h2.to_3d()


def vis_circle_object(co, rad=1.0):
    import math
    scene = bpy.data.scenes[0]  # weak!
    ob = bpy.data.objects.new(name="Circle", object_data=None)
    ob.rotation_euler.x = math.pi / 2
    ob.location = co.to_3d()
    ob.empty_draw_size = rad
    ob.layers = [True] * 20
    base = scene.objects.link(ob)
    return ob


def visualize_line(p1, p2, p3=None, rad=None):
    pair = p1.to_3d(), p2.to_3d()

    ob = vis_curve_object()
    spline = ob.data.splines.new(type='POLY')
    spline.points.add(1)
    for co, v in zip((pair), spline.points):
        v.co.xyz = co

    if p3:
        spline = ob.data.splines.new(type='POLY')
        spline.points[0].co.xyz = p3.to_3d()
        if rad is not None:
            vis_circle_object(p3, rad)


def treat_points(points,
                 double_limit=0.0001,
                 ):

    # first remove doubles
    tot_len = 0.0
    if double_limit != 0.0:
        i = len(points) - 1
        while i > 0:
            length = (points[i] - points[i - 1]).length
            if length < double_limit:
                del points[i]
                if i >= len(points):
                    i -= 1
            else:
                tot_len += length
                i -= 1
    return tot_len


def solve_curvature(p1, p2, n1, n2, fac, fallback):
    """ Add a nice circular curvature on
    """
    from mathutils.geometry import (intersect_line_line,
                                    )

    p1_a = p1 + n1
    p2_a = p2 - n2

    isect = intersect_line_line(p1,
                                p1_a,
                                p2,
                                p2_a,
                                )

    if isect:
        corner = isect[0].lerp(isect[1], 0.5)
    else:
        corner = None

    if corner:
        p1_first_order = p1.lerp(corner, fac)
        p2_first_order = corner.lerp(p2, fac)
        co = p1_first_order.lerp(p2_first_order, fac)

        return co
    else:
        # cant interpolate. just return interpolated value
        return fallback.copy()  # p1.lerp(p2, fac)


def points_to_bezier(points_orig,
                     double_limit=0.0001,
                     kink_tolerance=0.25,
                     bezier_tolerance=0.05,  # error distance, scale dependant
                     subdiv=8,
                     angle_span=0.95,  # 1.0 tries to evaluate splines of 180d
                     ):

    import math
    from mathutils import Vector

    class Point(object):
        __slots__ = ("co",
                     "angle",
                     "no",
                     "is_joint",
                     "next",
                     "prev",
                     )

        def __init__(self, co):
            self.co = co
            self.is_joint = False

        def calc_angle(self):
            if self.prev is None or self.next is None:
                self.angle = 0.0
            else:
                va = self.co - self.prev.co
                vb = self.next.co - self.co
                self.angle = va.angle(vb, 0.0)

        def angle_diff(self):
            """ use for detecting joints, detect difference in angle from
                surrounding points.
            """
            if self.prev is None or self.next is None:
                return 0.0
            else:
                if (self.angle > self.prev.angle and
                            self.angle > self.next.angle):
                    return abs(self.angle - self.prev.angle) / math.pi
                else:
                    return 0.0

        def calc_normal(self):
            v1 = v2 = None
            if self.prev and not self.prev.is_joint:
                v1 = (self.co - self.prev.co).normalized()
            if self.next and not self.next.is_joint:
                v2 = (self.next.co - self.co).normalized()

            if v1 and v2:
                self.no = (v1 + v2).normalized()
            elif v1:
                self.no = v1
            elif v2:
                self.no = v2
            else:
                print("Warning, assigning dummy normal")
                self.no = Vector((0.0, 1, 0.0))

    class Spline(object):
        __slots__ = ("points",
                     "handle_left",
                     "handle_right",
                     "next",
                     "prev",
                     )

        def __init__(self, points):
            self.points = points

        def link_points(self):

            if hasattr(self.points[0], "prev"):
                raise Exception("already linked")

            p_prev = None
            for p in self.points:
                p.prev = p_prev
                p_prev = p

            p_prev = None
            for p in reversed(self.points):
                p.next = p_prev
                p_prev = p

        def split(self, i, is_joint=False):
            prev = self.prev
            next = self.next

            if is_joint:
                self.points[i].is_joint = True

            # share a point
            spline_a = Spline(self.points[:i + 1])
            spline_b = Spline(self.points[i:])

            # invalidate self, dont reuse!
            self.points = None

            spline_a.next = spline_b
            spline_b.prev = spline_a

            spline_a.prev = prev
            spline_b.next = next
            if prev:
                prev.next = spline_a
            if next:
                next.prev = spline_b

            return spline_a, spline_b

        def calc_angle(self):
            for p in self.points:
                p.calc_angle()

        def calc_normal(self):
            for p in self.points:
                p.calc_normal()

        def calc_all(self):
            self.link_points()
            self.calc_angle()
            self.calc_normal()

        #~ def total_angle(self):
            #~ return abs(sum((p.angle for p in self.points)))

        def redistribute(self, segment_length, smooth=False):
            if len(self.points) == 1:
                return

            from mathutils.geometry import intersect_line_sphere

            p_line = p = self.points[0]
            points = [(p.co.copy(), p.co.copy())]
            p = p.next

            def point_add(co, p=None):
                co = co.copy()
                co_smooth = co.copy()

                if smooth:
                    if p is None:
                        pass  # works ok but no smoothing
                    elif (p.prev.no - p.no).length < 0.001:
                        pass  # normals are too similar, paralelle
                    elif (p.angle > 0.0) != (p.prev.angle > 0.0):
                        pass
                    else:
                        # visualize_line(p.co, p.co + p.no)

                        # this assumes co is on the line
                        fac = ((p.prev.co - co).length /
                               (p.prev.co - p.co).length)

                        assert(fac >= 0.0 and fac <= 1.0)

                        co_smooth = solve_curvature(p.prev.co,
                                                    p.co,
                                                    p.prev.no,
                                                    p.no,
                                                    fac,
                                                    co,
                                                    )

                points.append((co, co_smooth))

            def point_step(p):
                if p.is_joint or p.next is None:
                    point_add(p.co)
                    return None
                else:
                    return p.next

            print("START")
            while p:
                # we want the first pont past the segment size

                #if p.is_joint:
                #    vis_circle_object(p.co)

                length = (points[-1][0] - p.co).length

                if abs(length - segment_length) < 0.00001:
                    # close enough to be considered on the circle bounds
                    point_add(p.co)
                    p_line = p
                    p = point_step(p)
                elif length < segment_length:
                    p = point_step(p)
                else:
                    # the point is further then the segment width
                    p_start = points[-1][0] if p.prev is p_line else p.prev.co

                    if (p_start - points[-1][0]).length > segment_length:
                        raise Exception("eek2")
                    if (p.co - points[-1][0]).length < segment_length:
                        raise Exception("eek3")

                    # print(p_start, p.co, points[-1][0], segment_length)
                    i1, i2 = intersect_line_sphere(p_start,
                                                   p.co,
                                                   points[-1][0],
                                                   segment_length,
                                                   )
                    # print()
                    # print(i1, i2)
                    # assert(i1 is not None)
                    if i1 is not None:
                        point_add(i1, p)
                        p_line = p.prev
                    elif i2:
                        raise Exception("err")

                    elif i1 is None and i2 is None:
                        visualize_line(p_start,
                                       p.co,
                                       points[-1][0],
                                       segment_length,
                                       )

                        # XXX FIXME
                        # raise Exception("BAD!s")
                        point_add(p.co)
                        p_line = p
                        p = point_step(p)

            joint = self.points[0].is_joint, self.points[-1].is_joint

            self.points = [Point(p[1]) for p in points]

            self.points[0].is_joint, self.points[-1].is_joint = joint

            self.calc_all()
            # raise Exception("END")

        def intersect_line(self, l1, l2, reverse=False):
            """ Spectial kind of intersection, works in 3d on the plane
                defimed by the points normal and the line.
            """

            from mathutils.geometry import (intersect_point_line,
                                            )

            if reverse:
                p_first = self.points[-1]
                no = -self.points[-1].no
                point_iter = reversed(self.points[:-1])
            else:
                p_first = self.points[0]
                no = self.points[0].no
                point_iter = self.points[1:]

            # calculate the line right angles to the line
            bi_no = (no - no.project(l2 - l1)).normalized()

            bi_l1 = p_first.co
            bi_l2 = p_first.co + bi_no

            for p_apex in point_iter:
                ix, fac = intersect_point_line(p_apex.co, bi_l1, bi_l2)

                if fac < 0.0001:

                    if reverse:
                        p_apex_other = p_apex.next
                    else:
                        p_apex_other = p_apex.prev

                    # find the exact point on the line between the apex and
                    # the middle
                    p_test_1 = intersect_point_line(p_apex.co,
                                                    l1,
                                                    l2)[0]
                    p_test_2 = intersect_point_line(p_apex_other.co,
                                                    l1,
                                                    l2)[0]

                    w1 = (p_test_1 - p_apex.co).length
                    w2 = (p_test_2 - p_apex_other.co).length

                    #assert(w1 + w2 != 0)
                    try:
                        fac = w1 / (w1 + w2)
                    except ZeroDivisionError:
                        fac = 0.5
                    assert(fac >= 0.0 and fac <= 1.0)

                    p_apex_co = p_apex.co.lerp(p_apex_other.co, fac)
                    p_apex_no = p_apex.no.lerp(p_apex_other.no, fac)
                    p_apex_no.normalize()

                    # visualize_line(p_mid.to_3d(), corner.to_3d())
                    # visualize_line(p_apex.co.to_3d(), p_apex_co.to_3d())

                    return p_apex_co, p_apex_no, p_apex

            # intersection not found
            return None, None, None


        @staticmethod
        def bez_solve(p0, p1, p2, p3, u, v):
            ui = 1.0 - u
            vi = 1.0 - v
            u_p3 = u * u * u
            v_p3 = v * v * v
            ui_p3 = ui * ui * ui
            vi_p3 = vi * vi * vi

            a = 3.0 * ui * ui * u
            b = 3.0 * ui * u * u
            c = 3.0 * vi * vi * v
            d = 3.0 * vi * v * v
            det = a * d - b * c

            if det == 0.0:
                assert(0)
                return 0

            q1 = p1 - (ui_p3 * p0 + u_p3 * p3)
            q2 = p2 - (vi_p3 * p0 + v_p3 * p3)

            return ((d * q1 - b * q2) / det,
                    (-c * q1 + a * q2) / det
                    )

        def bezier_solve__math1(self):
            """ Calculate bezier handles,
                assume the splines have been broken up.

                http://polymathprogrammer.com/
            """

            def get(f, min=0.0, max=1.0):
                f = (f * (max - min) + min)
                return self.points[int((len(self.points) - 1) * f)].co
            
            
            p1 = get(0.0)
            p2 = get(1.0)
            i1 = get(1/3)
            i2 = get(2/3)

            pos = __class__.bez_solve(p1, i1, i2, p2, 1.0 / 3.0, 2.0 / 3.0)
            self.handle_left = self.points[0].co + (pos[0] - self.points[0].co)
            self.handle_right = self.points[-1].co + (pos[1] - self.points[-1].co)
        
        def bezier_solve__math2(self):

            def get(f, min=0.0, max=1.0):
                f = (f * (max - min) + min)
                return self.points[int((len(self.points) - 1) * f)].co

            p1 = get(0.0, min=0.0, max=0.5)
            p2 = get(1.0, min=0.0, max=0.5)
            i1 = get(1/3, min=0.0, max=0.5)
            i2 = get(2/3, min=0.0, max=0.5)
            
            pos_a = __class__.bez_solve(p1, i1, i2, p2, 1.0 / 3.0, 2.0 / 3.0)
            
            p1 = get(0.0, min=0.5, max=1.0)
            p2 = get(1.0, min=0.5, max=1.0)
            i1 = get(1/3, min=0.5, max=1.0)
            i2 = get(2/3, min=0.5, max=1.0)
            
            pos_b = __class__.bez_solve(p1, i1, i2, p2, 1.0 / 3.0, 2.0 / 3.0)

            self.handle_left = self.points[0].co + (pos_a[0] - self.points[0].co) * 2
            self.handle_right = self.points[-1].co + (pos_b[1] - self.points[-1].co) * 2

        def bezier_solve__inkscape(self):
                        
            # static void
            # estimate_bi(Point bezier[4], unsigned const ei,
            #             Point const data[], double const u[], unsigned const len)
            def estimate_bi(bezier, ei, data, u):

                def B0(u): return ( ( 1.0 - u )  *  ( 1.0 - u )  *  ( 1.0 - u ) )
                def B1(u): return ( 3 * u  *  ( 1.0 - u )  *  ( 1.0 - u ) )
                def B2(u): return ( 3 * u * u  *  ( 1.0 - u ) )
                def B3(u): return ( u * u * u )

                # assert( not (1 <= ei and ei <= 2))
                oi = 3 - ei
                num = [0.0, 0.0, 0.0]
                den = 0.0
                
                for i in range(len(data)):
                    ui = u[i];
                    b = [
                        B0(ui),
                        B1(ui),
                        B2(ui),
                        B3(ui)
                    ]

                    for d in range(3):
                        num[d] += (b[ei] * (b[0]  * bezier[0][d] +
                                           b[oi] * bezier[oi][d] +
                                           b[3]  * bezier[3][d] +
                                           - data[i][d]))

                    den -= b[ei] * b[ei]

                if den:
                    for d in range(3):
                        bezier[ei][d] = num[d] / den
                else:
                    bezier[ei] = (oi * bezier[0] + ei * bezier[3]) / 3.0
            bezier = [
                self.points[0].co,
                self.points[0].co.lerp(self.points[-1].co, 1/3),
                self.points[0].co.lerp(self.points[-1].co, 2/3),
                self.points[-1].co,
            ]
            data = [p.co for p in self.points]
            u = [i / len(self.points) for i in range(len(self.points))]
            estimate_bi(bezier, 1, data, u)
            estimate_bi(bezier, 2, data, u)
            estimate_bi(bezier, 1, data, u)
            estimate_bi(bezier, 2, data, u)
            estimate_bi(bezier, 1, data, u)
            estimate_bi(bezier, 2, data, u)
            estimate_bi(bezier, 1, data, u)
            estimate_bi(bezier, 2, data, u)
            
            self.handle_left = bezier[1]
            self.handle_right = bezier[2]

        def bezier_solve_ideasman42(self):
            from mathutils.geometry import (intersect_point_line,
                                            intersect_line_line,
                                            )

            # get a line
            p1 = self.points[0]
            p2 = self.points[-1]

            # ------
            # take 2
            p_vec = (p2.co - p1.co).normalized()

            # vector between line and point directions
            l1_no = (p1.no + p_vec).normalized()
            l2_no = ((-p2.no) - p_vec).normalized()

            l1_co = p1.co + l1_no
            l2_co = p2.co + l2_no

            # visualize_line(p1.co, l1_co)
            # visualize_line(p2.co, l2_co)

            line_ix_p1_co, line_ix_p1_no, line_ix_p1 = \
                    self.intersect_line(p1.co,
                                        l1_co,
                                        )
            line_ix_p2_co, line_ix_p2_no, line_ix_p2 = \
                    self.intersect_line(p2.co,
                                        l2_co,
                                        reverse=True,
                                        )
            if line_ix_p1_co is None:
                line_ix_p1_co, line_ix_p1_no, line_ix_p1 = \
                        p1.next.co, p1.next.no, p1.next
            if line_ix_p2_co is None:
                line_ix_p2_co, line_ix_p2_no, line_ix_p2 = \
                        p2.prev.co, p2.prev.no, p2.prev

            # vis_circle_object(line_ix_p1_co)
            # vis_circle_object(line_ix_p2_co)

            l1_max = 0.0
            p1_apex_co = None
            p = self.points[1]
            while p and (not p.is_joint) and p != line_ix_p1:
                ix = intersect_point_line(p.co, p1.co, l1_co)[0]
                length = (ix - p.co).length
                if length > l1_max:
                    l1_max = length
                    p1_apex_co = p.co
                p = p.next

            l2_max = 0.0
            p2_apex_co = None
            p = self.points[-2]
            while p and (not p.is_joint) and p != line_ix_p2:
                ix = intersect_point_line(p.co, p2.co, l2_co)[0]
                length = (ix - p.co).length
                if length > l2_max:
                    l2_max = length
                    p2_apex_co = p.co
                p = p.prev

            if p1_apex_co is None:
                p1_apex_co = p1.next.co
            if p2_apex_co is None:
                p2_apex_co = p2.prev.co

            l1_tan = (p1.no - p1.no.project(l1_no)).normalized()
            l2_tan = -(p2.no - p2.no.project(l2_no)).normalized()

            # values are good!
            visualize_line(p1.co, p1.co + l1_tan)
            visualize_line(p2.co, p2.co + l2_tan)

            visualize_line(p1.co, p1.co + l1_no)
            visualize_line(p2.co, p2.co + l2_no)

            # calculate bias based on the position of the other point allong
            # the tangent.

            # first need to reflect the second normal for angle comparison
            # first fist need the reflection normal
            
            # angle between - 0 - 1
            from math import pi
            no_ref = p_vec.cross(p2.no).cross(p_vec).normalized()
            l2_no_ref = p2.no.reflect(no_ref).normalized()
            no_angle = p1.no.angle(l2_no_ref) / pi
            del no_ref

            # This could be tweaked but seems to work well

            # fac_fac = 1.0

            print("angle", "%.6f" % no_angle)

            fac_1 = intersect_point_line(p2_apex_co,
                                         p1.co,
                                         p1.co + l1_tan * (p1.co - p1_apex_co).length,
                                         )[1] * (1.0 + no_angle)
            fac_2 = intersect_point_line(p1_apex_co,
                                         p2.co,
                                         p2.co + l2_tan * (p2.co - p2_apex_co).length,
                                         )[1] * (1.0 + no_angle)

            h1_fac = abs(fac_1)
            h2_fac = abs(fac_2)

            h1 = p1.co + (p1.no * h1_fac)
            h2 = p2.co - (p2.no * h2_fac)

            self.handle_left = h1
            self.handle_right = h2

            '''
            visualize_line(p1.co, p1_apex_co)
            visualize_line(p1_apex_co, p2_apex_co)
            visualize_line(p2.co, p2_apex_co)
            visualize_line(p1.co, p2.co)
            '''

        def bezier_solve(self):
            return self.bezier_solve__inkscape()

        def bezier_error(self, error_max=-1.0, test_count=8):
            from mathutils.geometry import interpolate_bezier

            test_points = interpolate_bezier(self.points[0].co,
                                             self.handle_left,
                                             self.handle_right,
                                             self.points[-1].co,
                                             test_count,
                                             )

            from mathutils.geometry import intersect_point_line

            error = 0.0

            # this is a rough method measuring the error but should be ok
            # TODO. dont test against every single point.
            for co in test_points:
                # initial values
                co_best = self.points[0].co

                length_best = (co - co_best).length
                for p in self.points[1:]:
                    # dist to point
                    length = (co - p.co).length
                    if length < length_best:
                        length_best = length
                        co_best = p.co

                    p_ix, fac = intersect_point_line(co, p.co, p.prev.co)
                    p_ix = p_ix
                    if fac >= 0.0 and fac <= 1.0:
                        length = (co - p_ix).length
                        if length < length_best:
                            length_best = length
                            co_best = p_ix

                error += length_best / test_count

                if error_max != -1.0 and error > error_max:
                    return True

            if error_max != -1.0:
                return False
            else:
                return error

    class Curve(object):
        __slots__ = ("splines",
                     )

        def __init__(self, splines):
            self.splines = splines

        def link_splines(self):
            s_prev = None
            for s in self.splines:
                s.prev = s_prev
                s_perv = s

            s_prev = None
            for s in reversed(self.splines):
                s.next = s_prev
                s_perv = s

        def calc_data(self):
            for s in self.splines:
                s.calc_all()

            self.link_splines()

        def split_func_map_point(self, func, is_joint=False):
            """ func takes a point and returns true on split

                return True if any splits are made.
            """
            s_index = 0
            s = self.splines[s_index]
            while s:
                assert(self.splines[s_index] == s)

                for i, p in enumerate(s.points):

                    if i == 0 or i >= len(s.points) - 1:
                        continue

                    if func(p):
                        split_pair = s.split(i, is_joint=is_joint)
                        # keep list in sync
                        self.splines[s_index:s_index + 1] = split_pair

                        # advance on main while loop
                        s = split_pair[0]
                        assert(self.splines[s_index] == s)
                        break

                s = s.next
                s_index += 1

        def split_func_spline(self, func, is_joint=False, recursive=False):
            """ func takes a spline and returns the point index on split or -1

                return True if any splits are made.
            """
            s_index = 0
            s = self.splines[s_index]
            while s:
                assert(self.splines[s_index] == s)

                i = func(s)

                if i != -1:
                    split_pair = s.split(i, is_joint=is_joint)
                    # keep list in sync
                    self.splines[s_index:s_index + 1] = split_pair

                    # advance on main while loop
                    s = split_pair[0]
                    assert(self.splines[s_index] == s)

                    if recursive:
                        continue

                s = s.next
                s_index += 1

        def validate(self):
            s_prev = None
            iii = 0
            for s in self.splines:
                assert(s.prev == s_prev)
                if s_prev:
                    assert(s_prev.next == s)
                s_prev = s
                iii += 1

        def redistribute(self, segment_length, smooth=False):
            for s in self.splines:
                s.redistribute(segment_length, smooth)

        def to_blend_data(self):
            """ Points to blender data, debugging only
            """
            scene = bpy.data.scenes[0]  # weak!
            for base in scene.object_bases:
                base.select = False
            cu = bpy.data.curves.new(name="Test", type='CURVE')
            for s in self.splines:
                spline = cu.splines.new(type='POLY')
                spline.points.add(len(s.points) - 1)
                for p, v in zip(s.points, spline.points):
                    v.co.xyz = p.co

            ob = bpy.data.objects.new(name="Test", object_data=cu)
            ob.layers = [True] * 20
            base = scene.objects.link(ob)
            scene.objects.active = ob
            base.select = True
            # base.layers = [True] * 20
            print(ob, "Done")

        def to_blend_curve(self, cu=None, cu_matrix=None):
            """ return new bezier spline datablock or add to an existing
            """
            if not cu:
                cu = bpy.data.curves.new(name="Curve", type='CURVE')

            spline = cu.splines.new(type='BEZIER')
            spline.bezier_points.add(len(self.splines))

            s_prev = None
            for i, bp in enumerate(spline.bezier_points):
                if i < len(self.splines):
                    s = self.splines[i]
                else:
                    s = None

                if s_prev and s:
                    pt = s.points[0]
                    hl = s_prev.handle_right
                    hr = s.handle_left
                elif s:
                    pt = s.points[0]
                    hr = s.handle_left
                    hl = (pt.co + (pt.co - hr))
                elif s_prev:
                    pt = s_prev.points[-1]
                    hl = s_prev.handle_right
                    hr = (pt.co + (pt.co - hl))
                else:
                    assert(0)

                bp.co.xyz = pt.co
                bp.handle_left.xyz = hl
                bp.handle_right.xyz = hr

                handle_type = 'FREE'

                if pt.is_joint == False or (s_prev and s) == False:

                    # XXX, this should not happen, but since it can
                    # at least dont allow allignment to break the curve output
                    if (pt.co - hl).angle(hr - pt.co, 0.0) < 0.1:

                        handle_type = 'ALIGNED'

                bp.handle_left_type = bp.handle_right_type = handle_type
                s_prev = s

            scene = bpy.data.scenes[0]  # weak!
            ob = bpy.data.objects.new(name="Test", object_data=cu)
            ob.layers = [True] * 20
            base = scene.objects.link(ob)
            scene.objects.active = ob
            base.select = True

            return cu

    points = list(points_orig)

    # remove doubles
    tot_length = treat_points(points)

    # calculate segment spacing
    segment_length = (tot_length / len(points)) / subdiv

    curve = Curve([Spline([Point(p) for p in points])])

    curve.calc_data()

    if kink_tolerance != 0.0:
        pass

    curve.split_func_map_point(lambda p: p.angle_diff() > kink_tolerance,
                               is_joint=True,
                               )

    # return
    # curve.validate()

    # higher quality but not really needed
    '''
    curve.redistribute(segment_length / 4.0, smooth=True)
    curve.redistribute(segment_length, smooth=False)
    '''
    curve.redistribute(segment_length, smooth=True)

    # debug only!
    # to test how good the bezier spline fitting is without corrections

    '''
    for s in curve.splines:
        s.bezier_solve()
    '''
    
    '''
    def angle_point(s):
        a = 0.0
        a_best = len(s.points) // 2
        i = 1
        for p in s.points[2:-2]:
            if p.angle > a:
                a = p.angle
                a_best = i
            i += 1
        return a_best
    '''

    # or recursively subdivide...
    curve.split_func_spline(lambda s:
                                len(s.points) // 2  # angle_point(s)
                                if ((s.bezier_solve(),
                                    s.bezier_error(bezier_tolerance))[1]
                                    and (len(s.points)))
                                else -1,
                            recursive=True,
                            )

    
    error = 0.0
    for s in curve.splines:
        error += s.bezier_error()
    print("%d :: %.6f" % (len(curve.splines), error))

    # VISUALIZE
    # curve.to_blend_data()
    curve.to_blend_curve()


if __name__ == "__main__":
    if 0:
        bpy.ops.wm.open_mainfile(filepath="/root/curve_test3.blend")

        for c in "Curve Curve.001 Curve.002 Curve.003 Curve.004 Curve.005".split():
            print("---", c)
            ob = bpy.data.objects[c]
            points = [p.co.xyz for s in ob.data.splines for p in s.points]

            print("points_to_bezier 1")
            points_to_bezier(points)
            print("points_to_bezier 2")
    else:
        bpy.ops.wm.open_mainfile(filepath="/root/curve_test2.blend")

        ob = bpy.data.objects['Curve']
        points = [p.co.xyz for s in ob.data.splines for p in s.points]

        print("points_to_bezier 1")
        points_to_bezier(points)
        print("points_to_bezier 2")

    bpy.ops.wm.save_as_mainfile(filepath="/root/curve_test_edit.blend",
                                copy=True)
    print("done!")