-
Notifications
You must be signed in to change notification settings - Fork 49
/
archipack_roof.py
5594 lines (4906 loc) · 193 KB
/
archipack_roof.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
# -*- coding:utf-8 -*-
# ##### 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>
# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------
# noinspection PyUnresolvedReferences
import bpy
import time
# noinspection PyUnresolvedReferences
from bpy.types import Operator, PropertyGroup, Mesh, Panel
from bpy.props import (
FloatProperty, BoolProperty, IntProperty,
StringProperty, EnumProperty,
CollectionProperty
)
from .bmesh_utils import BmeshEdit as bmed
from random import randint
import bmesh
from mathutils import Vector, Matrix
from math import sin, cos, pi, atan2, sqrt, tan
from .archipack_manipulator import Manipulable, archipack_manipulator
from .archipack_2d import Line, Arc
from .archipack_preset import ArchipackPreset, PresetMenuOperator
from .archipack_object import ArchipackCreateTool, ArchipackObject
from .archipack_cutter import (
CutAblePolygon, CutAbleGenerator,
ArchipackCutter,
ArchipackCutterPart
)
from .archipack_polylines import Io, ShapelyOps
from .archipack_dimension import DimensionProvider
class Roof():
def __init__(self):
self.angle_0 = 0
self.v0_idx = 0
self.v1_idx = 0
self.constraint_type = None
self.slope_left = 1
self.slope_right = 1
self.width_left = 1
self.width_right = 1
self.auto_left = 'AUTO'
self.auto_right = 'AUTO'
self.type = 'SIDE'
# force hip or valley
self.enforce_part = 'AUTO'
self.triangular_end = False
# seg is part of hole
self.is_hole = False
def copy_params(self, s):
s.angle_0 = self.angle_0
s.v0_idx = self.v0_idx
s.v1_idx = self.v1_idx
s.constraint_type = self.constraint_type
s.slope_left = self.slope_left
s.slope_right = self.slope_right
s.width_left = self.width_left
s.width_right = self.width_right
s.auto_left = self.auto_left
s.auto_right = self.auto_right
s.type = self.type
s.enforce_part = self.enforce_part
s.triangular_end = self.triangular_end
# segment is part of hole / slice
s.is_hole = self.is_hole
@property
def copy(self):
s = StraightRoof(self.p.copy(), self.v.copy())
self.copy_params(s)
return s
def straight(self, length, t=1):
s = self.copy
s.p = self.lerp(t)
s.v = self.v.normalized() * length
return s
def set_offset(self, offset, last=None):
"""
Offset line and compute intersection point
between segments
"""
self.line = self.make_offset(offset, last)
def offset(self, offset):
o = self.copy
o.p += offset * self.cross_z.normalized()
return o
@property
def oposite(self):
o = self.copy
o.p += o.v
o.v = -o.v
return o
@property
def t_diff(self):
return self.t_end - self.t_start
def straight_roof(self, a0, length):
s = self.straight(length).rotate(a0)
r = StraightRoof(s.p, s.v)
r.angle_0 = a0
return r
def curved_roof(self, a0, da, radius):
n = self.normal(1).rotate(a0).scale(radius)
if da < 0:
n.v = -n.v
c = n.p - n.v
r = CurvedRoof(c, radius, n.angle, da)
r.angle_0 = a0
return r
class StraightRoof(Roof, Line):
def __str__(self):
return "p0:{} p1:{}".format(self.p0, self.p1)
def __init__(self, p, v):
Line.__init__(self, p, v)
Roof.__init__(self)
class CurvedRoof(Roof, Arc):
def __str__(self):
return "t_start:{} t_end:{} dist:{}".format(self.t_start, self.t_end, self.dist)
def __init__(self, c, radius, a0, da):
Arc.__init__(self, c, radius, a0, da)
Roof.__init__(self)
class RoofSegment():
"""
Roof part with 2 polygons
and "axis" StraightRoof segment
"""
def __init__(self, seg, left, right):
self.seg = seg
self.left = left
self.right = right
self.a0 = 0
self.reversed = False
class RoofAxisNode():
"""
Connection between parts
for radial analysis
"""
def __init__(self):
# axis segments
self.segs = []
self.root = None
self.center = 0
# store count of horizontal segs
self.n_horizontal = 0
# store count of slopes segs
self.n_slope = 0
@property
def count(self):
return len(self.segs)
@property
def last(self):
"""
last segments in this node
"""
return self.segs[-1]
def left(self, index):
if index + 1 >= self.count:
return self.segs[0]
return self.segs[index + 1]
def right(self, index):
return self.segs[index - 1]
def add(self, a0, reversed, seg, left, right):
if seg.constraint_type == 'HORIZONTAL':
self.n_horizontal += 1
elif seg.constraint_type == 'SLOPE':
self.n_slope += 1
s = RoofSegment(seg, left, right)
s.a0 = a0
s.reversed = reversed
if reversed:
self.root = s
self.segs.append(s)
def update_center(self):
for i, s in enumerate(self.segs):
if s is self.root:
self.center = i
return
# sort tree segments by angle
def partition(self, array, begin, end):
pivot = begin
for i in range(begin + 1, end + 1):
if array[i].a0 < array[begin].a0:
pivot += 1
array[i], array[pivot] = array[pivot], array[i]
array[pivot], array[begin] = array[begin], array[pivot]
return pivot
def sort(self):
def _quicksort(array, begin, end):
if begin >= end:
return
pivot = self.partition(array, begin, end)
_quicksort(array, begin, pivot - 1)
_quicksort(array, pivot + 1, end)
end = len(self.segs) - 1
_quicksort(self.segs, 0, end)
# index of root in segs array
self.update_center()
class RoofPolygon(CutAblePolygon):
"""
ccw roof pitch boundary
closed by explicit segment
handle triangular shape with zero axis length
mov <_________________
| /\
| | rot
| | left last <> next
\/_____axis_______>|
node <_____axis________ next
| /\
| | rot
| | right last <> next
mov \/________________>|
side angle
"""
def __init__(self, axis, side, fake_axis=None):
"""
Create a default rectangle
axis from node to next
slope float -z for 1 in side direction
side in ['LEFT', 'RIGHT'] in axis direction
NOTE:
when axis length is null (eg: triangular shape)
use "fake_axis" with a 1 length to handle
distance from segment
"""
if side == 'LEFT':
# slope
self.slope = axis.slope_left
# width
self.width = axis.width_left
# constraint width
self.auto_mode = axis.auto_left
else:
# slope
self.slope = axis.slope_right
# width
self.width = axis.width_right
# constraint width
self.auto_mode = axis.auto_right
self.side = side
# backward deps
self.backward = False
# pointers to neighboors along axis
self.last = None
self.next = None
self.other_side = None
# axis segment
if side == 'RIGHT':
self.axis = axis.oposite
else:
self.axis = axis
self.fake_axis = None
# _axis is either a fake one or real one
# to prevent further check
if fake_axis is None:
self._axis = self.axis
self.fake_axis = self.axis
self.next_cross = axis
self.last_cross = axis
else:
if side == 'RIGHT':
self.fake_axis = fake_axis.oposite
else:
self.fake_axis = fake_axis
self._axis = self.fake_axis
# unit vector perpendicular to axis
# looking at outside part
v = self.fake_axis.sized_normal(0, -1)
self.cross = v
self.next_cross = v
self.last_cross = v
self.convex = True
# segments from axis end in ccw order
# closed by explicit segment
self.segs = []
# holes
self.holes = []
# Triangular ends
self.node_tri = False
self.next_tri = False
self.is_tri = False
# sizes
self.tmin = 0
self.tmax = 1
self.dt = 1
self.ysize = 0
self.xsize = 0
self.vx = Vector()
self.vy = Vector()
self.vz = Vector()
def move_node(self, p):
"""
Move slope point in node side
"""
if self.side == 'LEFT':
self.segs[-1].p0 = p
self.segs[2].p1 = p
else:
self.segs[2].p0 = p
self.segs[1].p1 = p
def move_next(self, p):
"""
Move slope point in next side
"""
if self.side == 'LEFT':
self.segs[2].p0 = p
self.segs[1].p1 = p
else:
self.segs[-1].p0 = p
self.segs[2].p1 = p
def node_link(self, da):
angle_90 = round(pi / 2, 4)
if self.side == 'LEFT':
idx = -1
else:
idx = 1
da = abs(round(da, 4))
type = "LINK"
if da < angle_90:
type += "_VALLEY"
elif da > angle_90:
type += "_HIP"
self.segs[idx].type = type
def next_link(self, da):
angle_90 = round(pi / 2, 4)
if self.side == 'LEFT':
idx = 1
else:
idx = -1
da = abs(round(da, 4))
type = "LINK"
if da < angle_90:
type += "_VALLEY"
elif da > angle_90:
type += "_HIP"
self.segs[idx].type = type
def bind(self, last, ccw=False):
"""
always in axis real direction
"""
# backward dependancy relative to axis
if last.backward:
self.backward = self.side == last.side
if self.side == last.side:
last.next_cross = self.cross
else:
last.last_cross = self.cross
self.last_cross = last.cross
# axis of last / next segments
if self.backward:
self.next = last
last.last = self
else:
self.last = last
last.next = self
# width auto
if self.auto_mode == 'AUTO':
self.width = last.width
self.slope = last.slope
elif self.auto_mode == 'WIDTH' and self.width != 0:
self.slope = last.slope * last.width / self.width
elif self.auto_mode == 'SLOPE' and self.slope != 0:
self.width = last.width * last.slope / self.slope
self.make_segments()
last.make_segments()
res, p, t = self.segs[2].intersect(last.segs[2])
if res:
# dont move anything when no intersection found
# aka when delta angle == 0
self.move_node(p)
if self.side != last.side:
last.move_node(p)
else:
last.move_next(p)
# Free mode
# move border
# and find intersections
# with sides
if self.auto_mode == 'ALL':
s0 = self._axis.offset(-self.width)
res, p0, t = self.segs[1].intersect(s0)
if res:
self.segs[2].p0 = p0
self.segs[1].p1 = p0
res, p1, t = self.segs[-1].intersect(s0)
if res:
self.segs[2].p1 = p1
self.segs[-1].p0 = p1
# /\
# | angle
# |____>
#
# v1 node -> next
if self.side == 'LEFT':
v1 = self._axis.v
else:
v1 = -self._axis.v
if last.side == self.side:
# contigous, v0 node <- next
# half angle between segments
if self.side == 'LEFT':
v0 = -last._axis.v
else:
v0 = last._axis.v
da = v0.angle_signed(v1)
if ccw:
if da < 0:
da = 2 * pi + da
elif da > 0:
da = da - 2 * pi
last.next_link(0.5 * da)
else:
# alternate v0 node -> next
# half angle between segments
if last.side == 'LEFT':
v0 = last._axis.v
else:
v0 = -last._axis.v
da = v0.angle_signed(v1)
# angle always ccw
if ccw:
if da < 0:
da = 2 * pi + da
elif da > 0:
da = da - 2 * pi
last.node_link(0.5 * da)
self.node_link(-0.5 * da)
def next_seg(self, index):
idx = self.get_index(index + 1)
return self.segs[idx]
def last_seg(self, index):
return self.segs[index - 1]
def make_segments(self):
if len(self.segs) < 1:
s0 = self._axis
w = self.width
s1 = s0.straight(w, 1).rotate(pi / 2)
s1.type = 'SIDE'
s3 = s0.straight(w, 0).rotate(pi / 2).oposite
s3.type = 'SIDE'
s2 = StraightRoof(s1.p1, s3.p0 - s1.p1)
s2.type = 'BOTTOM'
self.segs = [s0, s1, s2, s3]
def move_side(self, pt):
"""
offset side to point
"""
s2 = self.segs[2]
d0, t = self.distance(s2.p0)
d1, t = self.distance(pt)
# adjust width and slope according
self.width = d1
self.slope = self.slope * d0 / d1
self.segs[2] = s2.offset(d1 - d0)
def propagate_backward(self, pt):
"""
Propagate slope, keep 2d angle of slope
Move first point and border
keep border parallel
adjust slope
and next shape
"""
# distance of p
# offset side to point
self.move_side(pt)
# move verts on node side
self.move_next(pt)
if self.side == 'LEFT':
# move verts on next side
res, p, t = self.segs[-1].intersect(self.segs[2])
else:
# move verts on next side
res, p, t = self.segs[1].intersect(self.segs[2])
if res:
self.move_node(p)
if self.next is not None and self.next.auto_mode in {'AUTO'}:
self.next.propagate_backward(p)
def propagate_forward(self, pt):
"""
Propagate slope, keep 2d angle of slope
Move first point and border
keep border parallel
adjust slope
and next shape
"""
# offset side to point
self.move_side(pt)
# move verts on node side
self.move_node(pt)
if self.side == 'LEFT':
# move verts on next side
res, p, t = self.segs[1].intersect(self.segs[2])
else:
# move verts on next side
res, p, t = self.segs[-1].intersect(self.segs[2])
if res:
self.move_next(p)
if self.next is not None and self.next.auto_mode in {'AUTO'}:
self.next.propagate_forward(p)
def rotate_next_slope(self, a0):
"""
Rotate next slope part
"""
if self.side == 'LEFT':
s0 = self.segs[1].rotate(a0)
s1 = self.segs[2]
res, p, t = s1.intersect(s0)
else:
s0 = self.segs[2]
s1 = self.segs[-1]
res, p, t = s1.oposite.rotate(-a0).intersect(s0)
if res:
s1.p0 = p
s0.p1 = p
if self.next is not None:
if self.next.auto_mode == 'ALL':
return
if self.next.backward:
self.next.propagate_backward(p)
else:
self.next.propagate_forward(p)
def rotate_node_slope(self, a0):
"""
Rotate node slope part
"""
if self.side == 'LEFT':
s0 = self.segs[2]
s1 = self.segs[-1]
res, p, t = s1.oposite.rotate(-a0).intersect(s0)
else:
s0 = self.segs[1].rotate(a0)
s1 = self.segs[2]
res, p, t = s1.intersect(s0)
if res:
s1.p0 = p
s0.p1 = p
if self.next is not None:
if self.next.auto_mode == 'ALL':
return
if self.next.backward:
self.next.propagate_backward(p)
else:
self.next.propagate_forward(p)
def distance(self, pt):
"""
distance from axis
always use fake_axis here to
allow axis being cut and
still work
"""
res, d, t = self.fake_axis.point_sur_segment(pt)
return d, t
def altitude(self, pt):
d, t = self.distance(pt)
return -d * self.slope
def uv(self, pt):
d, t = self.distance(pt)
return ((t - self.tmin) * self.xsize, d)
def intersect(self, seg):
"""
compute intersections of a segment with boundaries
segment must start on axis
return segments inside
"""
it = []
for s in self.segs:
res, p, t, u = seg.intersect_ext(s)
if res:
it.append((t, p))
return it
def merge(self, other):
raise NotImplementedError
def draw(self, context, z, verts, edges):
f = len(verts)
#
# 0_______1
# |_______|
# 3 2
verts.extend([(s.p0.x, s.p0.y, z + self.altitude(s.p0)) for s in self.segs])
n_segs = len(self.segs) - 1
edges.extend([[f + i, f + i + 1] for i in range(n_segs)])
edges.append([f + n_segs, f])
"""
f = len(verts)
verts.extend([(s.p1.x, s.p1.y, z + self.altitude(s.p1)) for s in self.segs])
n_segs = len(self.segs) - 1
edges.extend([[f + i, f + i + 1] for i in range(n_segs)])
edges.append([f + n_segs, f])
"""
# holes
for hole in self.holes:
f = len(verts)
#
# 0_______1
# |_______|
# 3 2
verts.extend([(s.p0.x, s.p0.y, z + self.altitude(s.p0)) for s in hole.segs])
n_segs = len(hole.segs) - 1
edges.extend([[f + i, f + i + 1] for i in range(n_segs)])
edges.append([f + n_segs, f])
# axis
"""
f = len(verts)
verts.extend([self.axis.p0.to_3d(), self.axis.p1.to_3d()])
edges.append([f, f + 1])
# cross
f = len(verts)
verts.extend([self.axis.lerp(0.5).to_3d(), (self.axis.lerp(0.5) + self.cross.v).to_3d()])
edges.append([f, f + 1])
"""
# relationships arrows
if self.next or self.last:
w = 0.2
s0 = self._axis.offset(-0.5 * self.ysize)
p0 = s0.lerp(0.4).to_3d()
p0.z = z
p1 = s0.lerp(0.6).to_3d()
p1.z = z
if self.side == 'RIGHT':
p0, p1 = p1, p0
if self.backward:
p0, p1 = p1, p0
s1 = s0.sized_normal(0.5, w)
s2 = s0.sized_normal(0.5, -w)
f = len(verts)
p2 = s1.p1.to_3d()
p2.z = z
p3 = s2.p1.to_3d()
p3.z = z
verts.extend([p1, p0, p2, p3])
edges.extend([[f + 1, f], [f + 2, f], [f + 3, f]])
def as_string(self):
"""
Print strips relationships
"""
if self.backward:
dir = "/\\"
print("%s next" % (dir))
else:
dir = "\\/"
print("%s node" % (dir))
print("%s %s" % (dir, self.side))
if self.backward:
print("%s node" % (dir))
else:
print("%s next" % (dir))
if self.next:
print("_________")
self.next.as_string()
else:
print("#########")
def limits(self):
dist = []
param_t = []
for s in self.segs:
res, d, t = self.fake_axis.point_sur_segment(s.p0)
param_t.append(t)
dist.append(d)
if len(param_t) > 0:
self.tmin = min(param_t)
self.tmax = max(param_t)
else:
self.tmin = 0
self.tmax = 1
self.dt = self.tmax - self.tmin
if len(dist) > 0:
self.ysize = max(dist)
else:
self.ysize = 0
self.xsize = self.fake_axis.length * self.dt
# vectors components of part matrix
# where x is is axis direction
# y down
# z up
vx = -self.fake_axis.v.normalized().to_3d()
vy = Vector((-vx.y, vx.x, self.slope)).normalized()
self.vx = vx
self.vy = vy
self.vz = vx.cross(vy)
"""
import bpy
import bmesh
def print_list(name, lst, cols):
size = len(lst)
rows = 1 + int(size / cols)
print("%s" % "{} = [\n {}\n ]\n".format(name,
",\n ".join(
[", ".join([str(lst[r * cols + i]) for i in range(cols) if r * cols + i < size])
for r in range(rows)
])
))
def dump_mesh(m, cols, rounding):
verts = [(round(v.co.x, rounding), round(v.co.y, rounding), round(v.co.z, rounding)) for v in m.vertices]
faces = [tuple(p.vertices) for p in m.polygons]
bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(m)
edges = [tuple(i.index for i in edge.verts) for edge in bm.edges]
uvs = []
layer = bm.loops.layers.uv.verify()
for i, face in enumerate(bm.faces):
uv = []
for j, loop in enumerate(face.loops):
co = loop[layer].uv
uv.append((round(co.x, rounding), round(co.y, rounding)))
uvs.append(uv)
matids = [p.material_index for p in m.polygons]
print_list("verts", verts, cols)
print_list("faces", faces, cols)
print_list("matids", matids, cols)
print_list("uvs", uvs, cols)
def dump_curve(m, cols, rounding):
verts = [(round(v.co.x, rounding), round(v.co.y, rounding), round(v.co.z, rounding)) for v in m.points]
print_list("verts", verts, cols)
cols = 3
rounding = 3
m = C.object.data
dump_mesh(m, cols, rounding)
for c in m.splines:
dump_curve(c, cols, rounding)
"""
class RoofGenerator(CutAbleGenerator):
def __init__(self, d, origin=Vector((0, 0, 0))):
self.d = d
self.parts = d.parts
self.segs = []
self.nodes = []
self.pans = []
self.length = 0
self.origin = origin.to_2d()
self.z = origin.z
self.width_right = d.width_right
self.width_left = d.width_left
self.slope_left = d.slope_left
self.slope_right = d.slope_right
self.user_defined_tile = None
self.user_defined_uvs = None
self.user_defined_mat = None
self.is_t_child = d.t_parent != ""
def add_part(self, part):
if len(self.segs) < 1 or part.bound_idx < 1:
s = None
else:
s = self.segs[part.bound_idx - 1]
a0 = part.a0
if part.constraint_type == 'SLOPE' and a0 == 0:
a0 = 90
# start a new roof
if s is None:
v = part.length * Vector((cos(a0), sin(a0)))
s = StraightRoof(self.origin, v)
else:
s = s.straight_roof(a0, part.length)
# parent segment (root) index is v0_idx - 1
s.v0_idx = min(len(self.segs), part.bound_idx)
s.constraint_type = part.constraint_type
if part.constraint_type == 'SLOPE':
s.enforce_part = part.enforce_part
else:
s.enforce_part = 'AUTO'
s.angle_0 = a0
s.take_precedence = part.take_precedence
s.auto_right = part.auto_right
s.auto_left = part.auto_left
s.width_left = part.width_left
s.width_right = part.width_right
s.slope_left = part.slope_left
s.slope_right = part.slope_right
s.type = 'AXIS'
s.triangular_end = part.triangular_end
self.segs.append(s)
def locate_manipulators(self):
"""
"""
for i, f in enumerate(self.segs):
manipulators = self.parts[i].manipulators
p0 = f.p0.to_3d()
p0.z = self.z
p1 = f.p1.to_3d()
p1.z = self.z
# angle from last to current segment
if i > 0:
manipulators[0].type_key = 'ANGLE'
v0 = self.segs[f.v0_idx - 1].straight(-1, 1).v.to_3d()
v1 = f.straight(1, 0).v.to_3d()
manipulators[0].set_pts([p0, v0, v1])
# segment length
manipulators[1].type_key = 'SIZE'
manipulators[1].prop1_name = "length"
manipulators[1].set_pts([p0, p1, (1.0, 0, 0)])
# dumb segment id
manipulators[2].set_pts([p0, p1, (1, 0, 0)])
p0 = f.lerp(0.5).to_3d()
p0.z = self.z
# size left
p1 = f.sized_normal(0.5, -self.parts[i].width_left).p1.to_3d()
p1.z = self.z
manipulators[3].set_pts([p0, p1, (1, 0, 0)])
# size right
p1 = f.sized_normal(0.5, self.parts[i].width_right).p1.to_3d()
p1.z = self.z
manipulators[4].set_pts([p0, p1, (-1, 0, 0)])
# slope left
n0 = f.sized_normal(0.5, -1)
p0 = n0.p1.to_3d()
p0.z = self.z
p1 = p0.copy()
p1.z = self.z - self.parts[i].slope_left
manipulators[5].set_pts([p0, p1, (-1, 0, 0)], normal=n0.v.to_3d())
# slope right
n0 = f.sized_normal(0.5, 1)
p0 = n0.p1.to_3d()
p0.z = self.z
p1 = p0.copy()
p1.z = self.z - self.parts[i].slope_right
manipulators[6].set_pts([p0, p1, (1, 0, 0)], normal=n0.v.to_3d())
def seg_partition(self, array, begin, end):
"""
sort tree segments by angle
"""
pivot = begin
for i in range(begin + 1, end + 1):
if array[i].a0 < array[begin].a0:
pivot += 1
array[i], array[pivot] = array[pivot], array[i]
array[pivot], array[begin] = array[begin], array[pivot]
return pivot
def sort_seg(self, array, begin=0, end=None):
# print("sort_child")
if end is None:
end = len(array) - 1
def _quicksort(array, begin, end):
if begin >= end:
return
pivot = self.seg_partition(array, begin, end)
_quicksort(array, begin, pivot - 1)
_quicksort(array, pivot + 1, end)
return _quicksort(array, begin, end)
def make_roof(self, context):
"""
Init data structure for possibly multi branched nodes
nodes : radial relationships
pans : quad strip linear relationships
"""
pans = []
# node are connected segments