-
Notifications
You must be signed in to change notification settings - Fork 50
/
archipack_floor_heating.py
2344 lines (2060 loc) · 74.8 KB
/
archipack_floor_heating.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: Jacob Morris - Stephen Leger (s-leger)
# ----------------------------------------------------------
import bpy
from bpy.types import Operator, PropertyGroup, Mesh, Curve, Panel
from bpy.props import (
FloatProperty, CollectionProperty, StringProperty,
BoolProperty, IntProperty, EnumProperty
)
from mathutils import Vector, Matrix
from mathutils.geometry import interpolate_bezier
from math import cos, sin, pi, tan, atan2
from .archipack_2d import Line, Arc
from .archipack_manipulator import Manipulable, archipack_manipulator
from .archipack_preset import ArchipackPreset, PresetMenuOperator
from .archipack_object import ArchipackCreateTool, ArchipackObject
from .archipack_cutter import (
CutAblePolygon, CutAbleGenerator,
ArchipackCutter,
ArchipackCutterPart
)
from .archipack_gl import GlText
from .archipack_polylines import Io, Qtree, Envelope
import logging
logger = logging.getLogger("archipack")
class SeekPoint():
"""
A location to find segments on tree
"""
def __init__(self, p):
self.p = p
self.envelope = Envelope(p, p)
def move(self, p):
self.p = p
self.envelope.initByPoints(p, p)
class SeekBox():
def __init__(self):
self.envelope = Envelope()
def init_right(self, turtle):
p0, v0 = turtle.front.p, turtle.right.v
p1 = p0 + v0
p2 = p0 + turtle.front.v
p3 = p2 + v0
self.envelope.initByPoints(p0, p1)
self.envelope.expandToInclude(p2)
self.envelope.expandToInclude(p3)
def init_left(self, turtle, length):
p0, v0 = turtle.front.p, turtle.left.v
p1 = p0 + v0
p2 = p0 + turtle.front.v * length
p3 = p2 + v0
self.envelope.initByPoints(p0, p1)
self.envelope.expandToInclude(p2)
self.envelope.expandToInclude(p3)
def does_intersect(self, Q_segs, seg, skip):
nb, found = Q_segs.intersects_ext(self, 0.001)
u_min = 2
v_min = 0
i_idx = -1
intersect = False
for idx in found:
it, pt, u, v = seg.intersect_ext(Q_segs._geoms[idx])
# u is limited by tree
if it and 1.25 >= u >= -0.25 and 1.0001 >= v >= -0.0001:
intersect = True
if u < u_min:
u_min = u
v_min = v
i_idx = idx
return intersect, u_min, v_min, i_idx
class Seg():
def __init__(self, p, v):
# c0 c1 are Points
self.p = p
self.v = v
self.envelope = Envelope(p, p + v)
self.idx = -1
def copy(self):
return Seg(self.p.copy(), self.v.copy())
def update_envelope(self):
self.envelope.initByPoints(self.p, self.p + self.v)
def t(self, other):
"""
return param t of intersection on this seg
"""
c = Vector((other.v.y, -other.v.x, 0))
d = self.v * c
if d == 0:
# parallel
return 0
dp = other.p - self.p
t = (c * dp) / d
return t
def intersect_ext(self, other):
"""
intersect, return param t on both lines
"""
c = Vector((other.v.y, -other.v.x, 0))
d = self.v * c
if d == 0:
return False, self.p, 0, 0
dp = other.p - self.p
c2 = Vector((self.v.y, -self.v.x, 0))
u = (c * dp) / d
v = (c2 * dp) / d
return True, self.p + self.v * u, u, v
def point_on_seg(self, pt):
dp = pt - self.p
dl = self.v.length
if dl == 0:
return 0
t = (self.v * dp) / (dl ** 2)
return t
def farest_point(self, other):
"""
find param t on current seg
and point on other seg
of worst point of other seg
return point on this segment
and farest point on other one
"""
p0 = other.p.copy()
p1 = p0 + other.v
t0 = self.point_on_seg(p0)
t1 = self.point_on_seg(p1)
if t1 > t0:
p0 = p1
t0 = t1
return t0, p0
def nearest_point(self, other):
"""
find param t on current seg
of closest point of other seg
"""
p0 = other.p.copy()
p1 = p0 + other.v
t0 = self.point_on_seg(p0)
t1 = self.point_on_seg(p1)
if t1 < t0:
p0 = p1
t0 = t1
return t0, p0
def init(self, p, v):
self.p = p
self.v = v
self.update_envelope()
def output(self, context, gf, coordsys, name="Line"):
p0 = self.p
p1 = p0 + self.v
if self.v.length > 0:
line = gf.createLineString((p0, p1))
Io.to_curve(context.scene, coordsys, line, name=name)
def distance_pt(self, pt):
dp = pt - self.p
dl = self.v.length
if dl == 0:
return 0, 0
d = (self.v.x * dp.y - self.v.y * dp.x) / dl
t = (self.v * dp) / (dl ** 2)
return d, t
def minimal_dist(self, other):
d0, t = self.distance_pt(other.p)
d1, t = self.distance_pt(other.p + other.v)
if d1 < d0:
d0 = d1
return d0
class Turtle():
def __init__(self, p, v, cw=True):
self.init_side(cw)
side = v.cross(self.zAxis)
self.p = p
self.front = Seg(p, v)
self.front2 = Seg(p, 2 * v)
self.front3 = Seg(p, 3 * v)
self.left = Seg(p, -side)
self.left2 = Seg(p, 2 * -side)
self.right = Seg(p, side)
def init_side(self, cw):
self.cw = cw
if cw:
self.up = 1
else:
self.up = -1
self.zAxis = Vector((0, 0, self.up))
def update_envelopes(self):
self.front.update_envelope()
self.front2.update_envelope()
self.front3.update_envelope()
self.left.update_envelope()
self.left2.update_envelope()
self.right.update_envelope()
def rotate(self, v):
"""
set absolute rotation
"""
side = v.cross(self.zAxis)
self.front.v = v
self.front2.v = 2 * v
self.front3.v = 3 * v
self.left.v = -side
self.left2.v = 2 * -side
self.right.v = side
self.update_envelopes()
def move(self, v):
self.p += v
self.update_envelopes()
def turn_right(self):
self.rotate(self.right.v)
def turn_left(self):
self.rotate(self.left.v)
def step_forward(self):
self.move(self.front.v)
def step_backward(self):
self.move(-self.front.v)
def reverse(self):
self.init_side(not self.cw)
self.rotate(self.front.v)
def scale(self, factor):
self.rotate(factor * self.front.v)
def relocate(self, p, v):
self.p += p - self.p
self.rotate(v)
class Tree(Qtree):
def __init__(self, coordsys):
Qtree.__init__(self, coordsys)
def newSegment(self, c0, c1):
p0 = Vector((c0.coord.x, c0.coord.y, 0))
p1 = Vector((c1.coord.x, c1.coord.y, 0))
new_seg = Seg(p0, p1 - p0)
self.insert(self.ngeoms, new_seg)
return new_seg
class AbstractAnalyser():
def __init__(self):
self._result = None
# segment to check for intersection
self.seg = None
# tree
self.tree = None
def reset(self):
self._result = None
def parse_kwargs(self, kwargs):
keys = self.__dict__.keys()
self.__dict__.update((key, value) for key, value in kwargs.items() if key in keys)
def compute(self, **kwargs):
if self._result is None:
self.parse_kwargs(kwargs)
self.analyse()
class AnalyseIntersection(AbstractAnalyser):
"""
Find closest intersection in the tree
for given seg
"""
def __init__(self):
AbstractAnalyser.__init__(self)
def _intersect(self):
_seg = self.seg
nb, found = self.tree.intersects(_seg)
t = 1e32
idx = -1
intersect = False
p0 = _seg.p0
for i in found:
f_seg = self.tree._geoms[i]
# prevent intesection with touching segment
if p0 != f_seg.p1:
it, pt, u, v = _seg.intersect_ext(f_seg)
# u and v are limited by tree
if it and u > 0 and 1.0001 >= v >= -0.0001:
if u < t:
intersect = True
t = u
idx = i
return intersect, t, idx
def analyse(self):
# max param t for intersection on seek seg
it, t, idx = self._intersect()
self._result = it, t, idx
def intersect(self, d):
"""
return intersecting segment
or None
"""
seg = None
it, t, idx = self.result
if it:
tmax = d / self.seg.length
if t <= tmax:
seg = self.tree._geoms[idx]
return seg, t
class AnalyseObstacle(AbstractAnalyser):
"""
Find closest cutter in the tree
for given seg
"""
def __init__(self):
AbstractAnalyser.__init__(self)
def _intersect(self):
_seg = self.seg
nb, found = self.tree.intersects(_seg)
t = 1e32
idx = -1
intersect = False
p0 = _seg.p0
for i in found:
f_seg = self.tree._geoms[i]
# prevent intesection with touching segment
if p0 != f_seg.p1:
it, pt, u, v = _seg.intersect_ext(f_seg)
# u and v are limited by tree
if it and u > 0 and 1.0001 >= v >= -0.0001:
if u < t:
intersect = True
t = u
idx = i
return intersect, t, idx
def analyse(self):
# init SeekBox around given segment
# find any cutter in that box
# compute closest distance to cutter
it, t, idx = self._intersect()
self._result = it, t, idx
def intersect(self, d):
"""
return intersecting segment
or None
"""
seg = None
it, t, idx = self.result
if it:
tmax = d / self.seg.length
if t <= tmax:
seg = self.tree._geoms[idx]
return seg, t
"""
a = AnalyseIntersection()
a.reset()
a.compute(seg=3, tree=1, d=2)
seg, t = a.intersect(dist)
"""
class AbstractRule():
def __init__(self, apply_immediately):
self.apply_immediately = apply_immediately
def add_test(self, test):
self.tests.append(test)
def apply(self, tree, turtle):
return
def run(self, tree, turtle):
res = False
for test in self.tests:
res = test(tree, turtle)
if res:
self.apply(tree, turtle)
break
return res
"""
Fast and reliabile way detect obstacles ?
Init a tree with cutters only !
Analysis:
- intersections in 3 directions
- obstacles
- stuck state
Rule:
determine direction and size for next segment
-exception: apply immediately
-generic: let other rules try to apply
"""
class PathFinder():
def __init__(self, tree, spacing, allow_backward, cw):
self.tree = tree
self.spacing = spacing
self.segs = tree._geoms
self.n_boundarys = tree.ngeoms
# wall segment idx
self.last = -1
# helpers to seek in tree
p = Vector((0, 0, 0))
v = Vector((1, 0, 0))
self.pt = SeekPoint(p)
self.seg = Seg(p, v)
self.left = Seg(p, v)
self.right = Seg(p, v)
self.turtle = Turtle(p, v * spacing, cw)
# output coords
self.coords = []
# iteration current and max
self.run = True
self.allow_backward = allow_backward
self.is_forward = True
self.iter = 0
self.max_iter = (tree.width * tree.height) / (spacing ** 2)
def insert(self, p0, p1):
seg = Seg(p0, p1 - p0)
idx = self.tree.ngeoms
seg.idx = idx
self.tree.insert(idx, seg)
return idx
def intersect(self, seg):
nb, found = self.tree.intersects_ext(seg, 0.005 * self.spacing)
t = 1e32
idx = -1
intersect = False
skip = self.tree.ngeoms - 1
for i in found:
if i != skip:
it, pt, u, v = seg.intersect_ext(self.segs[i])
# u is limited by tree
if it and 1.0001 >= u > 0 and 1.0001 >= v >= -0.0001:
intersect = True
if u < t:
t = u
idx = i
return intersect, t, idx
def normal(self, seg):
"""
return
n : normal with same orientation or turtle front
d : length of normal + turtle front normalized
"""
n = seg.v.normalized().cross(self.turtle.zAxis)
# direction must match turtle one
# when both are in same direction length > 1
v = self.turtle.front.v.normalized()
d = (n + v).length
if d < 1:
n = -n
d = (n + v).length
return self.spacing * n, d
def seg_at_pos(self, seg, p):
"""
segment either start or end at location
"""
v0 = seg.p - p
return v0.length < 0.0001 or (v0 + seg.v).length < 0.0001
def obstacle_seg(self, p):
"""
find segment with largest normal at location
"""
self.pt.move(p)
d_max = -1
idx = -1
n = self.turtle.front.v
nb, found = self.tree.intersects_ext(self.pt, 0.001)
for i in found:
seg = self.segs[i]
if self.seg_at_pos(seg, p):
ni, d = self.normal(seg)
if d > d_max:
d_max = d
idx = i
n = ni
return idx, n
def wall_seg(self, p):
"""
find segment != skip at location
"""
self.pt.move(p)
nb, found = self.tree.intersects_ext(self.pt, 0.001)
for i in found:
if i != self.last:
seg = self.segs[i]
if self.seg_at_pos(seg, p):
return i
return -1
def obstacle(self):
"""
find obstacles for self.left
return
t param on turtle front
n normal of hit segment
"""
it, t, idx = self.intersect(self.left)
if it:
# find u of closest point on intersected vector
seg = self.segs[idx]
# self.left.output(context, gf, coordsys, name="Left_{}".format(self.iter))
# seg.output(context, gf, coordsys, name="Hit_{}".format(self.iter))
# self.turtle.front.output(context, gf, coordsys, name="Turtle_{}_front".format(self.iter))
# self.turtle.left.output(context, gf, coordsys, name="Turtle_{}_left".format(self.iter))
t, p = self.turtle.front.nearest_point(seg)
next, n = self.obstacle_seg(p)
next_seg = self.segs[next]
# next_seg.output(context, gf, coordsys, name="next_seg_{}".format(self.iter))
if next < self.n_boundarys:
p -= 0.5 * n
else:
p -= n
self.seg.init(p, next_seg.v)
# self.seg.output(context, gf, coordsys, name="seek_seg_{}".format(self.iter))
# print(next, p, next_seg.v)
t = self.turtle.front.t(self.seg)
return True, t, n, idx
return False, 0, self.turtle.front.v, -1
def start(self, p, v):
self.turtle.relocate(p, v)
self.coords.append(p.copy())
def next(self):
if self.is_forward:
res = self.forward()
else:
if self.allow_backward:
res = self.backward()
else:
return False
if not res:
self.is_forward = not self.is_forward
logger.debug("%s forward:%s", self.iter, self.is_forward)
if self.iter > self.max_iter:
logger.debug("%s exit max iter", self.iter)
return False
return self.run
def forward(self):
self.iter += 1
if self.iter > self.max_iter:
logger.debug("%s exit max iter", self.iter)
return False
v = self.turtle.front.v
# follow wall
if self.last < 0:
it, t, idx = self.intersect(self.turtle.right)
logger.debug("%s hit right %s", self.iter, idx)
self.last = idx
#################
# regular segment
#################
# estimate front t_max on wall segment
# this is the size of seek area for
# front and left intersections
seg = self.segs[self.last]
t, p0 = self.turtle.front.farest_point(seg)
# store this one as last if nothing else hit
next = self.wall_seg(p0)
seg = self.segs[next]
# identify the side: when d > 0 we are left side
# multiply by turtle.up for reverse cases
self.seg.init(p0, self.turtle.front.v)
p1 = seg.p
if (p0 - p1).length < 0.0001:
p1 = seg.p + seg.v
d, t1 = self.seg.distance_pt(p1)
dir = 'RIGHT'
side = 1
if d * self.turtle.up > 0:
dir = 'LEFT'
side = -side
# logger.debug("%s side:%s", self.iter, side)
n, d = self.normal(seg)
# logger.debug("%s last:%s next:%s", self.iter, self.last, next)
# find intersection of turtle front vector
# and segment parallel to seg
# we know if the seg is
# right (wall) or left (empty) side
if next < self.n_boundarys:
p = seg.p + side * 0.5 * n
else:
p = seg.p + side * n
self.seg.init(p, seg.v)
# t param for wall segment (if nothing else is hit)
t = self.turtle.front.t(self.seg)
if t == 0:
print(p0)
return False
hit = False
#################
# obstacle on segment
#################
# segment parallel to seg
# check for obstacle
if not hit:
# p1 = self.turtle.p + 0.5 * self.turtle.left.v
p1 = self.turtle.p + self.turtle.left.v
self.left.init(p1, v)
# resize seek segment to max
# parallel to next -+ 0.5 spacing
self.seg.init(p + side * 0.5 * n, seg.v)
t_max = self.left.t(self.seg)
# logger.debug("%s t:%s t_max:%s", self.iter, t, t_max)
if t_max < 0:
t_max = 1 - t_max
self.left.init(p1, t_max * v)
it, o_t, o_n, idx = self.obstacle()
# left hit seg
if it and idx < self.n_boundarys and o_t < t:
dir = 'LEFT'
t, n = o_t, o_n
next = idx
logger.debug("%s hit left o %s t:%s", self.iter, idx, t)
hit = True
# check for pipe
# dosent always work with such (1.5) space as it does hit |_|
if not hit:
p1 = self.turtle.p + 1.5 * self.turtle.left.v
self.left.init(p1, v)
# resize seek segment to max
# parallel to next -+ spacing
self.seg.init(p + side * 1.5 * n, seg.v)
t_max = self.left.t(self.seg)
# logger.debug("%s t:%s t_max:%s", self.iter, t, t_max)
if t_max < 0:
t_max = 1 - t_max
self.left.init(p1, t_max * v)
it, o_t, o_n, idx = self.obstacle()
# left hit seg
if it and idx >= self.n_boundarys and o_t < t:
dir = 'LEFT'
t, n = o_t, o_n
next = idx
logger.debug("%s hit left p %s t:%s", self.iter, idx, t)
hit = True
if next < self.n_boundarys:
p = p0 - 0.5 * n
else:
p = p0 - n
###########################
# forward past segment
# as we might not go further
# when we do left an obstacle
############################
if not hit:
# find intersection with wall seg line
# resize seek segment to max + 2 to ensure there
# is enougth space to come back
self.seg.init(self.turtle.p, (t + 2.5) * v)
# front hit seg
it, t_f, idx = self.intersect(self.seg)
if it:
if idx != next:
# hit seg is not next one
next = idx
seg = self.segs[next]
n, d = self.normal(seg)
side = -1
if next < self.n_boundarys:
p = seg.p + side * 0.5 * n
else:
p = seg.p + side * n
self.seg.init(p, seg.v)
dir = 'LEFT'
t = self.turtle.front.t(self.seg)
logger.debug("%s hit front %s t:%s", self.iter, idx, t)
hit = True
###########################
# obstacle past segment
# (is there enougth space to realy go right?)
# When dir is right (wall side)
# check for obstacle on right side
# from t to t + 1 (1.5 for pipe ?)
# if hit check for t < 0.5 bound t < 1 pipe
# __
# | _h_|o |
# __| |__|
# ____t n
# |0.5_|
# s
# if hit, t is -1 / -0.5 from parallel to hit seg
# 2 cases on hit
# 1 obstacle seg is on same line as right one
# and obstacle is on right side of this line
# -> t becomes end obstacle
# 2 dir becomes left next become obstacle one
if not hit and dir == 'RIGHT':
# build check seg on right side
# start at intersection of parallel seg on turtle.right side
# and next seg + 0.5
# check for obstacle at 0.5 * right
# to know if there is enougth space left
self.seg.init(seg.p + 0.5 * n, seg.v)
self.right.init(self.turtle.p + 0.5 * self.turtle.right.v, v)
t0 = self.right.t(self.seg)
self.right.init(self.right.p + t0 * v, 2 * v)
it, o_t, idx = self.intersect(self.right)
if it and idx < self.n_boundarys:
# hit seg
o_t, p = self.turtle.front.nearest_point(self.segs[idx])
idx, o_n = self.obstacle_seg(p)
# seg with greatest normal
d = self.segs[idx].minimal_dist(seg)
if d < 1.5 * self.spacing:
dir = 'LEFT'
seg = self.segs[idx]
next = idx
n = o_n
self.seg.init(seg.p - 0.5 * n, seg.v)
t = self.turtle.front.t(self.seg)
logger.debug("%s right obstacle %s t:%s", self.iter, idx, t)
hit = True
if not hit and dir == 'RIGHT':
# check for pipe at 1 * right
# to know if there is enougth space left
self.seg.init(seg.p + 0.5 * n, seg.v)
self.right.init(self.turtle.p + self.turtle.right.v, v)
t0 = self.right.t(self.seg)
self.right.init(self.right.p + t0 * v, 2 * v)
it, o_t, idx = self.intersect(self.right)
if it and idx >= self.n_boundarys:
# hit seg
o_t, p = self.turtle.front.nearest_point(self.segs[idx])
idx, o_n = self.obstacle_seg(p)
# seg with greatest normal
d = self.segs[idx].minimal_dist(seg)
if d < 2 * self.spacing:
dir = 'LEFT'
seg = self.segs[idx]
next = idx
n = o_n
self.seg.init(seg.p - n, seg.v)
t = self.turtle.front.t(self.seg)
logger.debug("%s right pipe %s t:%s", self.iter, idx, t)
hit = True
self.last = next
logger.debug("%s t:%s", dir, t)
if t < 0.5:
# self.turtle.front.output(context, gf, coordsys, name="Turtle_{}".format(self.iter))
self.turtle.rotate(self.segs[-1].v.normalized() * self.spacing)
self.turtle.turn_right()
self.turtle.move(0.5 * self.turtle.front.v)
p0 = self.coords[-1]
p1 = self.turtle.p.copy()
self.coords.append(p1)
self.insert(p0, p1)
self.turtle.turn_right()
it, t, idx = self.intersect(self.turtle.left)
logger.debug("%s hit: last=%s", self.iter, idx)
self.last = idx
return False
else:
self.turtle.move(t * v)
# rotate turtle to normal
self.turtle.rotate(n)
p0 = self.coords[-1]
p1 = self.turtle.p.copy()
self.coords.append(p1)
self.insert(p0, p1)
if dir == 'LEFT':
self.turtle.turn_left()
elif dir == 'RIGHT':
self.turtle.turn_right()
# self.turtle.front.output(context, gf, coordsys, name="Turtle_{}".format(self.iter))
return True
def backward(self):
self.iter += 1
backward = True
v = self.turtle.front.v
#################
# wall found
# follow til obstacle
# or hit front
#################
# estimate front t_max on wall segment
# this is the size of seek area for
# front and left intersections
seg = self.segs[self.last]
t, p0 = self.turtle.front.farest_point(seg)
# store this one as last if nothing else hit
next = self.wall_seg(p0)
dir = 'RIGHT'
if next == -1:
# end condition: found first segment
self.run = False
n, d = self.normal(seg)
else:
seg = self.segs[next]
# identify the side: when d > 0 we are left side
# multiply by turtle.up for reverse cases
self.seg.init(p0, v)
p1 = seg.p.copy()
if (p0 - p1).length < 0.0001:
p1 += seg.v
d, t1 = self.seg.distance_pt(p1)
side = -1
if d * self.turtle.up > 0:
dir = 'LEFT'
side = -side
n, d = self.normal(seg)
# logger.debug("%s last:%s next:%s", self.iter, self.last, next)
# find intersection of turtle front vector
# and segment parallel to seg
# so we have to know if the seg is
# right (wall) or left (empty) side
if next < self.n_boundarys:
p = seg.p + side * 0.25 * n
else:
p = seg.p + side * 0.5 * n
self.seg.init(p, seg.v)
# t param for wall segment (if nothing else is hit)
t = self.turtle.front.t(self.seg)
logger.debug("%s side:%s, t:%s next:%s", self.iter, side, t, next)
# found first segment
# find any intersection along that segment
# this occurs when angle < 90
self.seg.init(self.turtle.p, v * t)
it, o_t, idx = self.intersect(self.seg)
if it:
next = idx
seg = self.segs[next]
n, d = self.normal(seg)
if next < self.n_boundarys:
p = seg.p - 0.25 * n
else:
p = seg.p - 0.5 * n
self.seg.init(p, seg.v)
t = self.turtle.front.t(self.seg)
logger.debug("%s hit along t:%s", self.iter, t)
dir = 'RIGHT'
else:
#################
# available
# directions
#################
# front must hit unless we do have space
p = self.turtle.p + v * t
self.seg.init(p, v * 1.5)
it, o_t, idx = self.intersect(self.seg)
if it or True:
logger.debug("%s hit front", self.iter)
# other dir must hit
# check in next wall direction
# if there is space
# if hit something, use closest point on that segment - spacing as t
if dir == 'LEFT':
seg = self.turtle.left
else:
seg = self.turtle.right
self.seg.init(p, 0.75 * seg.v)
it, o_t, idx = self.intersect(self.seg)
if it:
seg = self.segs[idx]
# self.left.output(context, gf, coordsys, name="Left_{}".format(self.iter))
# seg.output(context, gf, coordsys, name="Hit_{}".format(self.iter))
# self.turtle.front.output(context, gf, coordsys, name="Turtle_{}_front".format(self.iter))
# self.turtle.left.output(context, gf, coordsys, name="Turtle_{}_left".format(self.iter))
t, p = self.turtle.front.nearest_point(seg)
next, n = self.obstacle_seg(p)
next_seg = self.segs[next]
# next_seg.output(context, gf, coordsys, name="next_seg_{}".format(self.iter))
# seg dosent cross -> -n
# seg cross +n
self.seg.init(self.turtle.p, 2 * seg.v.length * -n)
o_t = next_seg.t(self.seg)
side = 1
if 0 < o_t < 1:
side = -side
if next < self.n_boundarys:
p -= side * 0.25 * n
else:
p -= side * 0.5 * n
self.seg.init(p, next_seg.v)
n = -n
# self.seg.output(context, gf, coordsys, name="seek_seg_{}".format(self.iter))
# print(next, p, next_seg.v)
t = self.turtle.front.t(self.seg)
logger.debug("%s hit side t:%s", self.iter, t)
if abs(t) < 0.5:
# self.turtle.front.output(context, gf, coordsys, name="Turtle_{}".format(self.iter))
self.run = False
return False
else:
# space in front, could we run forward ?
backward = False
self.last = next
self.turtle.move(t * v)
# rotate turtle to normal
self.turtle.rotate(n)
p0 = self.coords[-1]
p1 = self.turtle.p.copy()
self.coords.append(p1)
self.insert(p0, p1)
if dir == 'LEFT':
self.turtle.turn_left()
elif dir == 'RIGHT':
self.turtle.turn_right()
# self.turtle.front.output(context, gf, coordsys, name="Turtle_{}".format(self.iter))
return backward
class Floor():
def __init__(self):
# self.colour_inactive = (1, 1, 1, 1)
pass
def set_offset(self, offset, last=None):
"""
Offset line and compute intersection point
between segments
"""
self.line = self.make_offset(offset, last)
def straight_floor_heating(self, a0, length):
s = self.straight(length).rotate(a0)
return StraightFloor(s.p, s.v)
def curved_floor_heating(self, a0, da, radius):
n = self.normal(1).rotate(a0).scale(radius)
if da < 0:
n.v = -n.v