forked from CGCookie/retopoflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolystrips.py
2584 lines (2105 loc) · 102 KB
/
polystrips.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 (C) 2014 CG Cookie
http://cgcookie.com
Created by Jonathan Denning, Jonathan Williamson, and Patrick Moore
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 3 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, see <http://www.gnu.org/licenses/>.
'''
####class definitions####
import bpy
import math
from math import sin, cos
import time
import copy
from mathutils import Vector, Quaternion
from mathutils.geometry import intersect_point_line, intersect_line_plane
from bpy_extras.view3d_utils import location_3d_to_region_2d, region_2d_to_vector_3d, region_2d_to_location_3d, region_2d_to_origin_3d
import bmesh
import blf, bgl
import itertools
from .lib import common_utilities
from .lib.common_utilities import iter_running_sum, dprint, get_object_length_scale, profiler, AddonLocator,frange
from .lib.common_utilities import zip_pairs
from . import polystrips_utilities
from .polystrips_utilities import cubic_bezier_blend_t, cubic_bezier_derivative, cubic_bezier_fit_points, cubic_bezier_split, sort_objects_by_angles, vector_angle_between
#Make the addon name and location accessible
AL = AddonLocator()
###############################################################################################################
# GVert
class GVert:
def __init__(self, obj, targ_obj, length_scale, position, radius, normal, tangent_x, tangent_y, from_mesh = False):
# store info
self.o_name = obj.name
self.targ_o_name = targ_obj.name
self.length_scale = length_scale
self.position = position
self.radius = radius
self.normal = normal
self.tangent_x = tangent_x
self.tangent_y = tangent_y
self.snap_pos = position
self.snap_norm = normal
self.snap_tanx = tangent_x
self.snap_tany = tangent_y
self.gedge0 = None
self.gedge1 = None
self.gedge2 = None
self.gedge3 = None
self.gedge_inner = None
self.zip_over_gedge = None # which gedge to zip over (which gets updated...)
self.zip_t = 0 # where do we attach?
self.zip_igv = 0
self.zip_snap_end = False # do we snap to endpoint of zip_over_gedge?
self.doing_update = False
self.visible = True
#data used when extending or emulating data
#already within a BMesh
self.from_mesh = from_mesh
self.from_mesh_ind = -1 #needs to be set explicitly
self.corner0_ind = -1
self.corner1_ind = -1
self.corner2_ind = -1
self.corner3_ind = -1
self.frozen = True if self.from_mesh else False
self.update()
def clone_detached(self):
'''
creates detached clone of gvert (without gedges)
'''
gv = GVert(bpy.data.objects[self.o_name], bpy.data.objects[self.targ_o_name], self.length_scale, Vector(self.position), self.radius, Vector(self.normal), Vector(self.tangent_x), Vector(self.tangent_y))
gv.snap_pos = Vector(self.snap_pos)
gv.snap_norm = Vector(self.snap_norm)
gv.snap_tanx = Vector(self.snap_tanx)
gv.snap_tany = Vector(self.snap_tany)
return gv
def has_0(self): return not (self.gedge0 is None)
def has_1(self): return not (self.gedge1 is None)
def has_2(self): return not (self.gedge2 is None)
def has_3(self): return not (self.gedge3 is None)
def is_inner(self): return not (self.gedge_inner is None)
def count_gedges(self): return len(self.get_gedges_notnone())
def is_unconnected(self): return not (self.has_0() or self.has_1() or self.has_2() or self.has_3())
def is_endpoint(self): return self.has_0() and not (self.has_1() or self.has_2() or self.has_3())
def is_endtoend(self): return self.has_0() and self.has_2() and not (self.has_1() or self.has_3())
def is_ljunction(self): return self.has_0() and self.has_1() and not (self.has_2() or self.has_3())
def is_tjunction(self): return self.has_0() and self.has_1() and self.has_3() and not self.has_2()
def is_cross(self): return self.has_0() and self.has_1() and self.has_2() and self.has_3()
def freeze(self): self.frozen = True
def thaw(self): self.frozen = False
def is_frozen(self): return self.frozen
def get_gedges(self): return [self.gedge0,self.gedge1,self.gedge2,self.gedge3]
def _set_gedges(self, ge0, ge1, ge2, ge3):
self.gedge0,self.gedge1,self.gedge2,self.gedge3 = ge0,ge1,ge2,ge3
def count_gedges(self):
return sum([self.has_0(),self.has_1(),self.has_2(),self.has_3()])
def get_gedges_notnone(self): return [ge for ge in self.get_gedges() if ge]
def get_inner_gverts(self): return [ge.get_inner_gvert_at(self) for ge in self.get_gedges_notnone()]
def get_zip_pair(self):
ge = self.zip_over_gedge
if not ge: return None
if ge.gvert0==self: return ge.gvert3
if ge.gvert3==self: return ge.gvert0
assert False
def disconnect_gedge(self, gedge):
pr = profiler.start()
if self.gedge_inner == gedge:
self.gedge_inner = None
else:
l_gedges = self.get_gedges_notnone()
assert gedge in l_gedges
l_gedges = [ge for ge in l_gedges if ge != gedge] #interesting way of removing
l = len(l_gedges)
l_gedges = [l_gedges[i] if i < l else None for i in range(4)]
self._set_gedges(*l_gedges)
self.update_gedges()
pr.done()
def connect_gedge_inner(self, gedge):
assert self.is_unconnected()
assert not self.gedge_inner
self.gedge_inner = gedge
def update_gedges(self):
if self.is_unconnected(): return
pr = profiler.start()
norm = self.snap_norm
l_gedges = self.get_gedges_notnone()
l_vecs = [ge.get_derivative_at(self).normalized() for ge in l_gedges]
if any(v.length == 0 for v in l_vecs): print (l_vecs)
#l_vecs = [v if v.length else Vector((1,0,0)) for v in l_vecs]
l_gedges = sort_objects_by_angles(norm, l_gedges, l_vecs)
l_vecs = [ge.get_derivative_at(self).normalized() for ge in l_gedges]
if any(v.length == 0 for v in l_vecs): print(l_vecs)
#l_vecs = [v if v.length else Vector((1,0,0)) for v in l_vecs]
l_angles = [vector_angle_between(v0,v1,norm) for v0,v1 in zip(l_vecs,l_vecs[1:]+[l_vecs[0]])]
connect_count = len(l_gedges)
if connect_count == 1:
self._set_gedges(l_gedges[0],None,None,None)
assert self.is_endpoint()
elif connect_count == 2:
d0 = abs(l_angles[0]-math.pi)
d1 = abs(l_angles[1]-math.pi)
if d0 < math.pi*0.2 and d1 < math.pi*0.2:
self._set_gedges(l_gedges[0],None,l_gedges[1],None)
assert self.is_endtoend()
else:
if l_angles[0] < l_angles[1]:
self._set_gedges(l_gedges[0],l_gedges[1],None,None)
else:
self._set_gedges(l_gedges[1],l_gedges[0],None,None)
assert self.is_ljunction()
elif connect_count == 3:
if l_angles[0] >= l_angles[1] and l_angles[0] >= l_angles[2]:
self._set_gedges(l_gedges[2],l_gedges[0],None,l_gedges[1])
elif l_angles[1] >= l_angles[0] and l_angles[1] >= l_angles[2]:
self._set_gedges(l_gedges[0],l_gedges[1],None,l_gedges[2])
else:
self._set_gedges(l_gedges[1],l_gedges[2],None,l_gedges[0])
assert self.is_tjunction()
elif connect_count == 4:
self._set_gedges(*l_gedges)
assert self.is_cross()
else:
assert False
self.update()
pr.done()
def connect_gedge(self, gedge):
pr = profiler.start()
if not self.gedge0: self.gedge0 = gedge
elif not self.gedge1: self.gedge1 = gedge
elif not self.gedge2: self.gedge2 = gedge
elif not self.gedge3: self.gedge3 = gedge
else: assert False
self.update_gedges()
pr.done()
def replace_gedge(self, gedge, ngedge):
if self.gedge0 == gedge: self.gedge0 = ngedge
elif self.gedge1 == gedge: self.gedge1 = ngedge
elif self.gedge2 == gedge: self.gedge2 = ngedge
elif self.gedge3 == gedge: self.gedge3 = ngedge
else: assert False
def snap_corners(self):
if self.frozen: return
pr = profiler.start()
mx = bpy.data.objects[self.o_name].matrix_world
mx3x3 = mx.to_3x3()
imx = mx.inverted()
if PolyStrips.settings.symmetry_plane == 'x':
self.corner0.x = max(0.0, self.corner0.x)
self.corner1.x = max(0.0, self.corner1.x)
self.corner2.x = max(0.0, self.corner2.x)
self.corner3.x = max(0.0, self.corner3.x)
self.corner0 = mx * bpy.data.objects[self.o_name].closest_point_on_mesh(imx*self.corner0)[0]
self.corner1 = mx * bpy.data.objects[self.o_name].closest_point_on_mesh(imx*self.corner1)[0]
self.corner2 = mx * bpy.data.objects[self.o_name].closest_point_on_mesh(imx*self.corner2)[0]
self.corner3 = mx * bpy.data.objects[self.o_name].closest_point_on_mesh(imx*self.corner3)[0]
if PolyStrips.settings.symmetry_plane == 'x':
self.corner0.x = max(0.0, self.corner0.x)
self.corner1.x = max(0.0, self.corner1.x)
self.corner2.x = max(0.0, self.corner2.x)
self.corner3.x = max(0.0, self.corner3.x)
self.position.x = max(0.0,self.position.x)
self.snap_pos.x = max(0.0,self.snap_pos.x)
pr.done()
def update(self, do_edges=True):
if self.doing_update: return
if self.zip_over_gedge and do_edges:
self.zip_over_gedge.update()
return
pr = profiler.start()
mx = bpy.data.objects[self.o_name].matrix_world
imx = mx.inverted()
mxnorm = imx.transposed().to_3x3()
mx3x3 = mx.to_3x3()
if not self.frozen:
l,n,i = bpy.data.objects[self.o_name].closest_point_on_mesh(imx*self.position)
self.snap_norm = (mxnorm * n).normalized()
self.snap_pos = mx * l
self.position = self.snap_pos
self.snap_tanx = self.tangent_x.normalized()
self.snap_tany = self.snap_norm.cross(self.snap_tanx).normalized()
# NOTE! DO NOT UPDATE NORMAL, TANGENT_X, AND TANGENT_Y
if do_edges:
self.doing_update = True
for gedge in [self.gedge0,self.gedge1,self.gedge2,self.gedge3]:
if gedge: gedge.update()
if self.gedge_inner: self.gedge_inner.update()
self.doing_update = False
self.snap_tanx = (Vector((0.2,0.1,0.5)) if not self.gedge0 else self.gedge0.get_derivative_at(self)).normalized()
self.snap_tany = self.snap_norm.cross(self.snap_tanx).normalized()
if self.frozen and self.gedge0:
vy = self.corner0 - self.corner1
vy.normalize()
vx = self.corner1 - self.corner2
vx.normalize()
test1 = vx.dot(self.snap_tanx)
test2 = vy.dot(self.snap_tany)
if test1 < -.7:
self.corner0, self.corner1, self.corner2, self.corner3 = self.corner2, self.corner3, self.corner0, self.corner1
self.corner0_ind, self.corner1_ind, self.corner2_ind, self.corner3_ind = self.corner2_ind, self.corner3_ind, self.corner0_ind, self.corner1_ind
if test1 > -.7 and test1 < .7:
if test2 > .7:
self.corner0, self.corner1, self.corner2, self.corner3 = self.corner3, self.corner0, self.corner1, self.corner2
self.corner0_ind, self.corner1_ind, self.corner2_ind, self.corner3_ind = self.corner3_ind, self.corner0_ind, self.corner1_ind, self.corner2_ind
else:
self.corner0, self.corner1, self.corner2, self.corner3 = self.corner1, self.corner2, self.corner3, self.corner0
self.corner0_ind, self.corner1_ind, self.corner2_ind, self.corner3_ind = self.corner1_ind, self.corner2_ind, self.corner3_ind, self.corner0_ind
if not self.zip_over_gedge:
# NOTE! DO NOT UPDATE NORMAL, TANGENT_X, AND TANGENT_Y
# ge2 #
# | #
# 2 --+-- 3 #
# | | #
# ge1--+ +Y +--ge3 #
# | X | #
# 1---+---0 #
# | #
# ge0 #
# TODO: make this go CCW :P
def get_corner(self,dmx,dmy, igv0,r0, igv1,r1):
if not igv0 and not igv1:
return self.snap_pos + self.snap_tanx*self.radius*dmx + self.snap_tany*self.radius*dmy
if igv0 and not igv1:
return igv0.position + igv0.tangent_y*r0
if igv1 and not igv0:
return igv1.position - igv1.tangent_y*r1
return (igv0.position+igv0.tangent_y*r0 + igv1.position-igv1.tangent_y*r1)/2
igv0 = None if not self.gedge0 else self.gedge0.get_igvert_at(self)
igv1 = None if not self.gedge1 else self.gedge1.get_igvert_at(self)
igv2 = None if not self.gedge2 else self.gedge2.get_igvert_at(self)
igv3 = None if not self.gedge3 else self.gedge3.get_igvert_at(self)
r0 = 0 if not igv0 else (igv0.radius*(1 if igv0.tangent_x.dot(self.snap_tanx)>0 else -1))
r1 = 0 if not igv1 else (igv1.radius*(1 if igv1.tangent_x.dot(self.snap_tany)<0 else -1))
r2 = 0 if not igv2 else (igv2.radius*(1 if igv2.tangent_x.dot(self.snap_tanx)<0 else -1))
r3 = 0 if not igv3 else (igv3.radius*(1 if igv3.tangent_x.dot(self.snap_tany)>0 else -1))
if not self.frozen:
self.corner0 = get_corner(self, 1, 1, igv0,r0, igv3,r3)
self.corner1 = get_corner(self, 1,-1, igv1,r1, igv0,r0)
self.corner2 = get_corner(self,-1,-1, igv2,r2, igv1,r1)
self.corner3 = get_corner(self,-1, 1, igv3,r3, igv2,r2)
self.snap_corners()
pr.done()
def update_corners_zip(self, p0, p1, p2, p3):
if self.zip_over_gedge == self.gedge0:
self.corner0 = p0
self.corner1 = p1
self.corner2 = p2
self.corner3 = p3
elif self.zip_over_gedge == self.gedge1:
self.corner1 = p0
self.corner2 = p1
self.corner3 = p2
self.corner0 = p3
elif self.zip_over_gedge == self.gedge2:
self.corner2 = p0
self.corner3 = p1
self.corner0 = p2
self.corner1 = p3
elif self.zip_over_gedge == self.gedge3:
self.corner3 = p0
self.corner0 = p1
self.corner1 = p2
self.corner2 = p3
else:
assert False
def update_visibility(self, r3d, update_gedges=False, hq = True):
if hq:
self.visible = False not in common_utilities.ray_cast_visible(self.get_corners(), bpy.data.objects[self.o_name], r3d)
else:
self.visible = common_utilities.ray_cast_visible([self.snap_pos], bpy.data.objects[self.o_name], r3d)[0]
if not update_gedges: return
for ge in self.get_gedges_notnone():
ge.update_visibility(r3d)
def is_visible(self): return self.visible
def get_corners(self):
return (self.corner0, self.corner1, self.corner2, self.corner3)
def get_corner_inds(self):
return (self.corner0_ind, self.corner1_ind, self.corner2_ind, self.corner3_ind)
def is_picked(self, pt):
if not self.visible: return False
c0 = self.corner0 - pt
c1 = self.corner1 - pt
c2 = self.corner2 - pt
c3 = self.corner3 - pt
n = self.snap_norm
d0 = c1.cross(c0).dot(n)
d1 = c2.cross(c1).dot(n)
d2 = c3.cross(c2).dot(n)
d3 = c0.cross(c3).dot(n)
return d0>0 and d1>0 and d2>0 and d3>0
def get_corners_of(self, gedge):
if gedge == self.gedge0: return (self.corner0, self.corner1)
if gedge == self.gedge1: return (self.corner1, self.corner2)
if gedge == self.gedge2: return (self.corner2, self.corner3)
if gedge == self.gedge3: return (self.corner3, self.corner0)
assert False, "GEdge is not connected"
def get_back_corners_of(self, gedge):
if gedge == self.gedge0: return (self.corner2, self.corner3)
if gedge == self.gedge1: return (self.corner3, self.corner0)
if gedge == self.gedge2: return (self.corner0, self.corner1)
if gedge == self.gedge3: return (self.corner1, self.corner2)
assert False, "GEdge is not connected"
def get_cornerinds_of(self, gedge):
if gedge == self.gedge0: return (0,1)
if gedge == self.gedge1: return (1,2)
if gedge == self.gedge2: return (2,3)
if gedge == self.gedge3: return (3,0)
assert False, "GEdge is not connected"
def get_back_cornerinds_of(self, gedge):
if gedge == self.gedge0: return (2,3)
if gedge == self.gedge1: return (3,0)
if gedge == self.gedge2: return (0,1)
if gedge == self.gedge3: return (1,2)
assert False, "GEdge is not connected"
def get_side_cornerinds_of(self, gedge, side):
'''
return cornerinds on side that go toward gedge
'''
if gedge == self.gedge0: return (3,0) if side>0 else (2,1)
if gedge == self.gedge1: return (0,1) if side>0 else (3,2)
if gedge == self.gedge2: return (1,2) if side>0 else (0,3)
if gedge == self.gedge3: return (2,3) if side>0 else (1,0)
assert False, "GEdge is not connected"
def toggle_corner(self):
if (self.is_endtoend() or self.is_ljunction()):
if self.is_ljunction():
self._set_gedges(self.gedge0,None,self.gedge1,None)
assert self.is_endtoend()
else:
self._set_gedges(self.gedge2,self.gedge0,None,None)
assert self.is_ljunction()
self.update()
elif self.is_tjunction():
self._set_gedges(self.gedge3,self.gedge0,None,self.gedge1)
assert self.is_tjunction()
self.update()
else:
print('Cannot toggle corner on GVert with %i connections' % self.count_gedges())
def smooth(self, v=0.15):
pr = profiler.start()
der0 = self.gedge0.get_derivative_at(self, ignore_igverts=True).normalized() if self.gedge0 else Vector()
der1 = self.gedge1.get_derivative_at(self, ignore_igverts=True).normalized() if self.gedge1 else Vector()
der2 = self.gedge2.get_derivative_at(self, ignore_igverts=True).normalized() if self.gedge2 else Vector()
der3 = self.gedge3.get_derivative_at(self, ignore_igverts=True).normalized() if self.gedge3 else Vector()
if self.is_endtoend():
angle = (math.pi - der0.angle(der2))*v
cross = der0.cross(der2).normalized()
quat0 = Quaternion(cross, -angle)
quat1 = Quaternion(cross, angle)
self.gedge0.rotate_gverts_at(self, quat0)
self.gedge2.rotate_gverts_at(self, quat1)
self.update()
if self.is_ljunction():
angle = (math.pi/2 - der0.angle(der1))*v
cross = der0.cross(der1).normalized()
quat0 = Quaternion(cross, -angle)
quat1 = Quaternion(cross, angle)
self.gedge0.rotate_gverts_at(self, quat0)
self.gedge1.rotate_gverts_at(self, quat1)
self.update()
if self.is_tjunction():
angle = (math.pi/2 - der3.angle(der0))*v
cross = der3.cross(der0).normalized()
self.gedge3.rotate_gverts_at(self, Quaternion(cross, -angle))
self.gedge0.rotate_gverts_at(self, Quaternion(cross, angle))
angle = (math.pi/2 - der0.angle(der1))*v
cross = der0.cross(der1).normalized()
self.gedge0.rotate_gverts_at(self, Quaternion(cross, -angle))
self.gedge1.rotate_gverts_at(self, Quaternion(cross, angle))
self.update()
if self.is_cross():
cross = self.snap_norm.normalized()
ang30 = (math.pi/2 - vector_angle_between(der3,der0,cross))*v
self.gedge3.rotate_gverts_at(self, Quaternion(cross, ang30))
self.gedge0.rotate_gverts_at(self, Quaternion(cross, -ang30))
ang01 = (math.pi/2 - vector_angle_between(der0,der1,cross))*v
self.gedge0.rotate_gverts_at(self, Quaternion(cross, ang01))
self.gedge1.rotate_gverts_at(self, Quaternion(cross, -ang01))
ang12 = (math.pi/2 - vector_angle_between(der1,der2,cross))*v
self.gedge1.rotate_gverts_at(self, Quaternion(cross, ang12))
self.gedge2.rotate_gverts_at(self, Quaternion(cross, -ang12))
ang23 = (math.pi/2 - vector_angle_between(der2,der3,cross))*v
self.gedge2.rotate_gverts_at(self, Quaternion(cross, ang23))
self.gedge3.rotate_gverts_at(self, Quaternion(cross, -ang23))
self.update()
pr.done()
def get_gedge_to_left(self, gedge):
if self.gedge0 == gedge: return self.gedge1
if self.gedge1 == gedge: return self.gedge2
if self.gedge2 == gedge: return self.gedge3
if self.gedge3 == gedge: return self.gedge0
def get_gedge_to_right(self, gedge):
if self.gedge0 == gedge: return self.gedge3
if self.gedge1 == gedge: return self.gedge0
if self.gedge2 == gedge: return self.gedge1
if self.gedge3 == gedge: return self.gedge2
###############################################################################################################
# GEdge between GVerts
class GEdge:
'''
Graph Edge (GEdge) stores end points and "way points" (cubic bezier)
'''
def __init__(self, obj, targ_obj, length_scale, gvert0, gvert1, gvert2, gvert3):
# store end gvertices
self.o_name = obj.name
self.targ_o_name = targ_obj.name
self.length_scale = length_scale
self.gvert0 = gvert0
self.gvert1 = gvert1
self.gvert2 = gvert2
self.gvert3 = gvert3
self.force_count = False
self.n_quads = None
self.changing_count = False
self.zip_to_gedge = None
self.zip_side = 1
self.zip_dir = 1
self.zip_attached = []
self.frozen = False
self.l_ts = []
self.gpatches = []
# create caching vars
self.cache_igverts = [] # cached interval gverts
# even-indexed igverts are poly "centers"
# odd-indexed igverts are poly "edges"
gvert0.connect_gedge(self)
gvert1.connect_gedge_inner(self)
gvert2.connect_gedge_inner(self)
gvert3.connect_gedge(self)
def get_count(self):
l = len(self.cache_igverts)
if l > 4:
n_quads = math.floor(l/2) + 1
else:
n_quads = 3
return n_quads
def set_count(self, c):
c = min(c,50)
if self.force_count and self.n_quads == c:
return
if self.changing_count:
# already changing! must be a bad loop
return
self.changing_count = True
self.force_count = True
self.n_quads = c
if self.gpatches:
for gpatch in self.gpatches:
gpatch.set_count(self)
self.changing_count = False
self.update()
def unset_count(self):
if self.fill_to0 or self.fill_to1:
print('Cannot unset force count when filling')
return
self.force_count = None
self.n_quads = None
self.update()
def has_endpoint(self, gv): return gv==self.gvert0 or gv==self.gvert3
def get_other_end(self, gv): return self.gvert0 if gv==self.gvert3 else self.gvert3
def is_zippered(self): return (self.zip_to_gedge != None)
def has_zippered(self): return len(self.zip_attached)!=0
def freeze(self):
self.frozen = True
self.gvert0.freeze()
self.gvert3.freeze()
def thaw(self):
self.frozen = False
for gp in self.gpatches:
gp.thaw()
def is_frozen(self): return self.frozen
def zip_to(self, gedge):
assert not self.zip_to_gedge
self.zip_to_gedge = gedge
gedge.zip_attached += [self]
t0,_ = gedge.get_closest_point(self.gvert0.position)
t3,_ = gedge.get_closest_point(self.gvert3.position)
pos = gedge.get_position_at_t(t0)
der = gedge.get_derivative_at_t(t0)
nor = gedge.gvert0.snap_norm
tny = nor.cross(der)
# which side are we on and which way are we going?
self.zip_side = 1 if tny.dot(self.gvert0.position-pos)>0 else -1
self.zip_dir = 1 if tny.dot(self.gvert0.snap_tany)>0 else -1
self.gvert0.zip_over_gedge = self
self.gvert0.zip_t = t0
self.gvert3.zip_over_gedge = self
self.gvert3.zip_t = t3
self.update()
def unzip(self):
assert self.zip_to_gedge
gedge = self.zip_to_gedge
self.zip_to_gedge = None
gedge.zip_attached = [ge for ge in gedge.zip_attached if ge != self]
self.gvert0.zip_over_gedge = None
self.gvert3.zip_over_gedge = None
self.update()
def is_gpatched(self):
return len(self.gpatches)
def attach_gpatch(self, gpatch):
if len(self.gpatches) >= 2:
print('Cannot attach more than two gpatches')
return
self.gpatches.append(gpatch)
def detach_gpatch(self, gpatch):
self.gpatches.remove(gpatch)
for gp in self.gpatches:
gp.update()
def rotate_gverts_at(self, gv, quat):
if gv == self.gvert0:
v = self.gvert1.position - self.gvert0.position
v = quat * v
self.gvert1.position = self.gvert0.position + v
self.gvert1.update()
elif gv == self.gvert3:
v = self.gvert2.position - self.gvert3.position
v = quat * v
self.gvert2.position = self.gvert3.position + v
self.gvert2.update()
else:
assert False
def disconnect(self):
if self.zip_to_gedge:
self.unzip()
for ge in self.zip_attached:
ge.unzip()
self.gvert0.disconnect_gedge(self)
self.gvert1.disconnect_gedge(self)
self.gvert2.disconnect_gedge(self)
self.gvert3.disconnect_gedge(self)
def update_visibility(self, rv3d):
lp = [gv.snap_pos for gv in self.cache_igverts]
lv = common_utilities.ray_cast_visible(lp, bpy.data.objects[self.o_name], rv3d)
for gv,v in zip(self.cache_igverts,lv): gv.visible = v
def gverts(self):
return [self.gvert0,self.gvert1,self.gvert2,self.gvert3]
def get_derivative_at(self, gv, ignore_igverts=False):
if not ignore_igverts and len(self.cache_igverts) < 3:
if self.gvert0 == gv:
return self.gvert3.position - self.gvert0.position
if self.gvert3 == gv:
return self.gvert0.position - self.gvert3.position
assert False, "gv is not an endpoint"
p0,p1,p2,p3 = self.get_positions()
if self.gvert0 == gv:
return cubic_bezier_derivative(p0,p1,p2,p3,0)
if self.gvert3 == gv:
return cubic_bezier_derivative(p3,p2,p1,p0,0)
assert False, "gv is not an endpoint"
def get_position_at_t(self, t):
p0,p1,p2,p3 = self.get_positions()
return cubic_bezier_blend_t(p0,p1,p2,p3,t)
def get_derivative_at_t(self, t):
p0,p1,p2,p3 = self.get_positions()
return cubic_bezier_derivative(p0,p1,p2,p3,t)
def get_inner_gvert_at(self, gv):
if self.gvert0 == gv: return self.gvert1
if self.gvert3 == gv: return self.gvert2
assert False, "gv is not an endpoint"
def get_outer_gvert_at(self, gv):
if self.gvert1 == gv: return self.gvert0
if self.gvert2 == gv: return self.gvert3
assert False, "gv is not an inner gvert"
def get_inner_gverts(self):
return [self.gvert1, self.gvert2]
def get_vector_from(self, gv):
is_0 = (self.gvert0==gv)
gv0 = self.gvert0 if is_0 else self.gvert3
gv1 = self.gvert2 if is_0 else self.gvert1
return gv1.position - gv0.position
def get_igvert_at(self, gv):
if self.gvert0 == gv:
if len(self.cache_igverts):
return self.cache_igverts[1]
return None #self.gvert0
if self.gvert3 == gv:
if len(self.cache_igverts):
return self.cache_igverts[-2]
return None #self.gvert3
assert False, "gv is not an endpoint"
def get_positions(self):
return (
self.gvert0.position,
self.gvert1.position,
self.gvert2.position,
self.gvert3.position
)
def get_normals(self):
return (
self.gvert0.normal,
self.gvert1.normal,
self.gvert2.normal,
self.gvert3.normal
)
def get_radii(self):
return (
self.gvert0.radius,
self.gvert1.radius,
self.gvert2.radius,
self.gvert3.radius
)
def get_length(self, precision = 64):
p0,p1,p2,p3 = self.get_positions()
mx = bpy.data.objects[self.o_name].matrix_world
imx = mx.inverted()
p3d = [cubic_bezier_blend_t(p0,p1,p2,p3,t/precision) for t in range(precision+1)]
p3d = [mx*bpy.data.objects[self.o_name].closest_point_on_mesh(imx * p)[0] for p in p3d]
return sum((p1-p0).length for p0,p1 in zip(p3d[:-1],p3d[1:]))
#return cubic_bezier_length(p0,p1,p2,p3)
def get_closest_point(self, pt):
p0,p1,p2,p3 = self.get_positions()
if len(self.cache_igverts) < 3:
return cubic_bezier_find_closest_t_approx(p0,p1,p2,p3,pt)
min_t,min_d = -1,-1
i,l = 0,len(self.cache_igverts)
for gv0,gv1 in zip(self.cache_igverts[:-1],self.cache_igverts[1:]):
p0,p1 = gv0.position,gv1.position
t,d = common_utilities.closest_t_and_distance_point_to_line_segment(pt, p0,p1)
if min_t < 0 or d < min_d: min_t,min_d = (i+t)/l,d
i += 1
return min_t,min_d
def get_igvert_from_t(self, t, next=False):
return int((float(len(self.cache_igverts)-1)*t + (1 if next else 0))/2)*2
def update_zip(self, debug=False):
'''
recomputes interval gverts along gedge---zipped version
extend off of igverts of self.zip_to_gedge
'''
zip_igverts = self.zip_to_gedge.cache_igverts
l = len(zip_igverts)
t0 = self.gvert0.zip_t
t3 = self.gvert3.zip_t
i0 = self.zip_to_gedge.get_igvert_from_t(t0)
i3 = self.zip_to_gedge.get_igvert_from_t(t3, next=True)
self.gvert0.zip_igv = i0
self.gvert3.zip_igv = i3
dprint('zippered indices: %i (%f) %i (%f) / %i' % (i0,t0,i3,t3,l))
if i0 == i3:
dprint('i0 == i3')
self.cache_igverts = []
else:
if i0 < i3:
ic = (i3-i0)+1
if i3>len(zip_igverts):
dprint('%i %i %i' % (i0,i3,ic))
loigv = [zip_igverts[i0+_i] for _i in range(ic)]
elif i3 < i0:
ic = (i0-i3)+1
if i0>len(zip_igverts):
dprint('%i %i %i' % (i3,i0,ic))
loigv = [zip_igverts[i3+_i] for _i in range(ic)]
loigv.reverse()
side = self.zip_side
zdir = self.zip_dir
r0,r3 = self.gvert0.radius,self.gvert3.radius
rm = (r3-r0)/float(ic+2)
l_radii = [r0+rm*(_i+1) for _i,oigv in enumerate(loigv)]
l_pos = [oigv.position+oigv.tangent_y*side*(oigv.radius+l_radii[_i]) for _i,oigv in enumerate(loigv)]
l_norms = [oigv.normal for _i,oigv in enumerate(loigv)]
l_tanx = [oigv.tangent_x*zdir for _i,oigv in enumerate(loigv)]
l_tany = [oigv.tangent_y*zdir for _i,oigv in enumerate(loigv)]
self.cache_igverts = [GVert(bpy.data.objects[self.o_name],
bpy.data.objects[self.targ_o_name],
self.length_scale,p,r,n,tx,ty) for p,r,n,tx,ty in zip(l_pos,l_radii,l_norms,l_tanx,l_tany)]
self.snap_igverts()
assert len(self.cache_igverts)>=2, 'not enough! %i (%f) %i (%f) %i' % (i0,t0,i3,t3,ic)
self.gvert0.position = self.cache_igverts[0].position
self.gvert1.position = (self.cache_igverts[0].position+self.cache_igverts[-1].position)/2
self.gvert2.position = (self.cache_igverts[0].position+self.cache_igverts[-1].position)/2
self.gvert3.position = self.cache_igverts[-1].position
def get_corners(ind, radius):
if ind == -1:
p0,p1 = self.zip_to_gedge.gvert0.get_back_corners_of(self.zip_to_gedge)
if side<0: p0,p1 = p0,p0+(p0-p1).normalized()*(radius*2)
else: p0,p1 = p1,p1+(p1-p0).normalized()*(radius*2)
return (p1,p0)
if ind == len(zip_igverts):
p0,p1 = self.zip_to_gedge.gvert3.get_back_corners_of(self.zip_to_gedge)
if side>0: p0,p1 = p0,p0+(p0-p1).normalized()*(radius*2)
else: p0,p1 = p1,p1+(p1-p0).normalized()*(radius*2)
return (p1,p0)
igv = zip_igverts[ind]
p0 = igv.position + igv.tangent_y*side*(igv.radius+radius*2)
p1 = igv.position + igv.tangent_y*side*(igv.radius)
return (p0,p1)
if i0 < i3:
p0,p1 = get_corners(i0+1,l_radii[1])
p3,p2 = get_corners(i0-1,r0)
if side < 0: p0,p1,p2,p3 = p1,p0,p3,p2
self.gvert0.update_corners_zip(p0,p1,p2,p3)
p0,p1 = get_corners(i3-1,l_radii[-2])
p3,p2 = get_corners(i3+1,r3)
if side > 0: p0,p1,p2,p3 = p1,p0,p3,p2
self.gvert3.update_corners_zip(p0,p1,p2,p3)
else:
p0,p1 = get_corners(i0-1,l_radii[1])
p3,p2 = get_corners(i0+1,r0)
if side > 0: p0,p1,p2,p3 = p1,p0,p3,p2
self.gvert0.update_corners_zip(p0,p1,p2,p3)
p0,p1 = get_corners(i3+1,l_radii[-2])
p3,p2 = get_corners(i3-1,r3)
if side < 0: p0,p1,p2,p3 = p1,p0,p3,p2
self.gvert3.update_corners_zip(p0,p1,p2,p3)
self.gvert0.update(do_edges=False)
self.gvert1.update(do_edges=False)
self.gvert2.update(do_edges=False)
self.gvert3.update(do_edges=False)
for ge in self.gvert0.get_gedges_notnone()+self.gvert3.get_gedges_notnone():
if ge != self: ge.update(debug=debug)
def update_nozip(self, debug=False):
p0,p1,p2,p3 = self.get_positions()
r0,r1,r2,r3 = self.get_radii()
n0,n1,n2,n3 = self.get_normals()
if False:
# attempting to smooth snapped igverts
mx = self.obj.matrix_world
mxnorm = mx.transposed().inverted().to_3x3()
mx3x3 = mx.to_3x3()
imx = mx.inverted()
p3d = [cubic_bezier_blend_t(p0,p1,p2,p3,t/16.0) for t in range(17)]
snap = [self.obj.closest_point_on_mesh(imx*p) for p in p3d]
snap_pos = [mx*pos for pos,norm,idx in snap]
bez = cubic_bezier_fit_points(snap_pos, min(r0,r3)/20, allow_split=False)
if bez:
_,_,p0,p1,p2,p3 = bez[0]
_,n1,_ = self.obj.closest_point_on_mesh(imx*p1)
_,n2,_ = self.obj.closest_point_on_mesh(imx*p2)
n1 = mxnorm*n1
n2 = mxnorm*n2
#get s_t_map
if self.n_quads:
step = 20* self.n_quads
else:
step = 100
s_t_map = polystrips_utilities.cubic_bezier_t_of_s_dynamic(p0, p1, p2, p3, initial_step = step )
#l = self.get_length() <-this is more accurate, but we need consistency
l = max(s_t_map)
if self.force_count and self.n_quads:
# force number of segments
# number of segments
c = 2 * (self.n_quads - 1)
# compute difference for smoothly interpolating radii perpendicular to GEdge
s = (r3-r0) / float(c+1)
L = c * r0 + s*(c+1)*c/2 #integer run sum
os = L - l
d_os = os/c
# compute interval lengths and ts
l_widths = [0] + [r0 + s*i - d_os for i in range(c)]
l_ts = [polystrips_utilities.closest_t_of_s(s_t_map, dist) for w,dist in iter_running_sum(l_widths)] #pure lenght distribution
else:
# find "optimal" count for subdividing spline based on radii of two endpoints
cmin,cmax = int(math.floor(l/max(r0,r3))),int(math.floor(l/min(r0,r3)))
c = 0
for ctest in range(max(4,cmin-2),cmax+2):
s = (r3-r0) / (ctest-1)
tot = r0*(ctest+1) + s*(ctest+1)*ctest/2
if tot > l:
break
if ctest % 2 == 1:
c = ctest
c = max(3,c)
# compute difference for smoothly interpolating radii
s = (r3-r0) / float(c-1)
# compute how much space is left over (to be added to each interval)
tot = r0*(c+1) + s*(c+1)*c/2
o = l - tot
oc = o / (c+1)