-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwo_d_lines.py
1685 lines (1562 loc) · 67.4 KB
/
two_d_lines.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
"""three_d_surfaces.py
PZero© Andrea Bistacchi"""
from copy import deepcopy
from PySide6.QtGui import QAction
from geopandas import GeoDataFrame as geodataframe
from numpy import stack as np_stack
from numpy import arange as np_arange
from numpy import array as np_array
from numpy import column_stack as np_column_stack
from numpy import flipud as np_flipud
from numpy import round as np_round
from numpy import shape as np_shape
from numpy import zeros as np_zeros
from numpy import concatenate as np_concatenate
from numpy.linalg import norm as np_norm
# from shapely import affinity
from shapely.affinity import scale as shp_scale
from shapely.affinity import rotate as shp_rotate
from shapely.geometry import Point as shp_point
from shapely.geometry import LineString as shp_linestring
# from shapely.geometry import MultiLineString as shp_multilinestring
from shapely.ops import snap as shp_snap
from shapely.ops import split as shp_split
from .helpers.helper_dialogs import multiple_input_dialog, input_one_value_dialog, message_dialog
from .helpers.helper_widgets import Editor, Tracer
from .helpers.helper_functions import freeze_gui
from .entities_factory import PolyLine, XsPolyLine
from .windows_factory import ViewMap, ViewXsection
def draw_line(self):
def end_digitize(event, input_dict):
# Signal called to end the digitization of a trace. It returns a new polydata
self.plotter.untrack_click_position()
traced_pld = (
tracer.GetContourRepresentation().GetContourRepresentationAsPolyData()
)
if traced_pld.GetNumberOfPoints() > 0:
input_dict["vtk_obj"].ShallowCopy(traced_pld)
self.parent.geol_coll.add_entity_from_dict(input_dict)
tracer.EnabledOff()
self.enable_actions()
self.disable_actions()
"""Create deepcopy of the geological entity dictionary."""
line_dict = deepcopy(self.parent.geol_coll.entity_dict)
"""One dictionary is set as input for a general widget of multiple-value-input"""
line_dict_in = {
"name": ["PolyLine name: ", "new_pline"],
"role": [
"Role: ",
self.parent.geol_coll.valid_roles,
],
"feature": [
"Feature: ",
self.parent.geol_coll.legend_df["feature"].tolist(),
],
"scenario": [
"Scenario: ",
list(set(self.parent.geol_coll.legend_df["scenario"].tolist())),
],
}
line_dict_updt = multiple_input_dialog(
title="Digitize new PolyLine", input_dict=line_dict_in
)
"""Check if the output of the widget is empty or not. If the Cancel button was clicked, the tool quits"""
if line_dict_updt is None:
self.enable_actions()
return
"""Getting the values that have been typed by the user through the widget"""
for key in line_dict_updt:
line_dict[key] = line_dict_updt[key]
if isinstance(self, ViewMap):
line_dict["topology"] = "PolyLine"
line_dict["x_section"] = None
line_dict["vtk_obj"] = PolyLine()
elif isinstance(self, ViewXsection):
line_dict["topology"] = "XsPolyLine"
line_dict["x_section"] = self.this_x_section_uid
line_dict["vtk_obj"] = XsPolyLine(
x_section_uid=self.this_x_section_uid, parent=self.parent
)
tracer = Tracer(self)
tracer.EnabledOn()
self.plotter.track_click_position(
side="right", callback=lambda event: end_digitize(event, line_dict)
)
def edit_line(self):
def end_edit(event, uid):
self.plotter.untrack_click_position(side="right")
traced_pld = (
editor.GetContourRepresentation().GetContourRepresentationAsPolyData()
)
if isinstance(self, ViewMap):
vtk_obj = PolyLine()
elif isinstance(self, ViewXsection):
vtk_obj = XsPolyLine(
x_section_uid=self.this_x_section_uid, parent=self.parent
)
vtk_obj.ShallowCopy(traced_pld)
self.parent.geol_coll.replace_vtk(uid=uid, vtk_object=vtk_obj)
editor.EnabledOff()
self.clear_selection()
self.enable_actions()
if not self.selected_uids:
print(" -- No input data selected -- ")
return
self.disable_actions()
sel_uid = self.selected_uids[0]
actor = self.plotter.renderer.actors[sel_uid]
data = actor.mapper.dataset
# self.tracer.SetInputData(data)
editor = Editor(self)
editor.EnabledOn()
editor.initialize(data, "edit")
self.plotter.track_click_position(
side="right", callback=lambda event: end_edit(event, sel_uid)
)
# self.plotter.track_mouse_position()
# self.plotter.track_click_position(side='left', callback=left_click, viewport=True)
def sort_line_nodes(self):
"""Sort line nodes."""
print("Sort line nodes according to cell order.")
# """Terminate running event loops"""
# self.stop_event_loops()
"""Check if a line is selected"""
if not self.selected_uids:
print(" -- No input data selected -- ")
return
# """Freeze QT interface"""
# for action in self.findChildren(QAction):
# if isinstance(action.parentWidget(), NavigationToolbar) is False:
# action.setDisabled(True)
"""If more than one line is selected, keep the first."""
for current_uid in self.selected_uids:
"""For some reason in the following the [:] is needed."""
self.parent.geol_coll.get_uid_vtk_obj(
current_uid
).sort_nodes() # this could be probably done per-part__________________________
"""Deselect input line."""
self.parent.geol_coll.signals.geom_modified.emit(
[current_uid]
) # emit uid as list to force redraw()
# """Un-Freeze QT interface"""
# for action in self.findChildren(QAction):
# action.setEnabled(True)
self.clear_selection()
def move_line(self, vector):
"""Move the whole line by rigid-body translation.
Here transformation to UV is not necessary since the translation vector is already in world space
"""
print("Move Line. Move the whole line by rigid-body translation.")
if vector.length == 0:
print("Zero-length vector")
self.enable_actions()
return
for current_uid in self.selected_uids:
if (
self.parent.geol_coll.get_uid_topology(current_uid) != "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(current_uid) != "XsPolyLine"
):
print(" -- Selected data is not a line -- ")
return
"""Editing loop."""
"""For some reason in the following the [:] is needed."""
x = (
self.parent.geol_coll.get_uid_vtk_obj(current_uid).points_X[:]
+ vector.deltas[0]
)
y = (
self.parent.geol_coll.get_uid_vtk_obj(current_uid).points_Y[:]
+ vector.deltas[1]
)
z = (
self.parent.geol_coll.get_uid_vtk_obj(current_uid).points_Z[:]
+ vector.deltas[2]
)
points = np_stack((x, y, z), axis=1)
self.parent.geol_coll.get_uid_vtk_obj(current_uid).points = points
left_right(current_uid)
"""Deselect input line."""
self.parent.geol_coll.signals.geom_modified.emit(
[current_uid]
) # emit uid as list to force redraw()
"""Un-Freeze QT interface"""
self.clear_selection()
self.enable_actions()
@freeze_gui
def rotate_line(self):
"""Rotate lines by rigid-body rotation using Shapely."""
self.parent.TextTerminal.appendPlainText(
"Rotate Line. Rotate the whole line by rigid-body rotation. Please insert angle of anticlockwise rotation."
)
# Check if at least a line is selected.
if not self.selected_uids:
self.parent.TextTerminal.appendPlainText(" -- No input data selected -- ")
return
# Input rotation angle. None exits the function.
angle = input_one_value_dialog(
parent=self,
title="Rotate Line",
label="Insert rotation angle in degrees, anticlockwise",
default_value=10,
)
if angle is None:
self.parent.TextTerminal.appendPlainText(" -- Angle is None -- ")
return
for current_uid in self.selected_uids:
if (
self.parent.geol_coll.get_uid_topology(current_uid) != "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(current_uid) != "XsPolyLine"
):
self.parent.TextTerminal.appendPlainText(" -- Selected data is not a line -- ")
return
if isinstance(self, ViewMap):
inU = self.parent.geol_coll.get_uid_vtk_obj(current_uid).points_X
inV = self.parent.geol_coll.get_uid_vtk_obj(current_uid).points_Y
elif isinstance(self, ViewXsection):
inU, inV = self.parent.geol_coll.get_uid_vtk_obj(current_uid).world2plane()
# Stack coordinates in two-columns matrix and convert into Shapely object.
inUV = np_column_stack((inU, inV))
shp_line_in = shp_linestring(inUV)
# Use Shapely to rotate
shp_line_out = shp_rotate(shp_line_in, angle, origin="centroid", use_radians=False)
# Un-stack output coordinates and write them to the empty dictionary.
outUV = np_array(shp_line_out.coords)
outU = outUV[:, 0]
outV = outUV[:, 1]
if isinstance(self, ViewMap):
outX = outU
outY = outV
outZ = self.parent.geol_coll.get_uid_vtk_obj(current_uid).points_Z
elif isinstance(self, ViewXsection):
outX, outY, outZ = self.parent.xsect_coll.plane2world(
self.this_x_section_uid, outU, outV
)
outXYZ = np_column_stack((outX, outY, outZ))
self.parent.geol_coll.get_uid_vtk_obj(current_uid).points = outXYZ
left_right(current_uid)
# emit uid as list to force redraw()
self.parent.geol_coll.signals.geom_modified.emit([current_uid])
"""Deselect input line."""
self.clear_selection()
def extend_line(self):
def end_edit(event, uid):
self.plotter.untrack_click_position(side="right")
self.plotter.untrack_click_position(side="left")
self.plotter.clear_events_for_key("k")
traced_pld = (
extender.GetContourRepresentation().GetContourRepresentationAsPolyData()
)
if isinstance(self, ViewMap):
vtk_obj = PolyLine()
elif isinstance(self, ViewXsection):
vtk_obj = XsPolyLine(
x_section_uid=self.this_x_section_uid, parent=self.parent
)
vtk_obj.ShallowCopy(traced_pld)
self.parent.geol_coll.replace_vtk(uid=uid, vtk_object=vtk_obj)
extender.EnabledOff()
self.clear_selection()
self.enable_actions()
"""Extend selected line."""
print("Extend Line. Press k to change end of line to extend.")
"""Terminate running event loops"""
# self.stop_event_loops()
"""Check if a line is selected"""
if not self.selected_uids:
print(" -- No input data selected -- ")
return
if (
self.parent.geol_coll.get_uid_topology(self.selected_uids[0])
!= "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(self.selected_uids[0])
!= "XsPolyLine"
):
print(" -- Selected data is not a line -- ")
return
"""Freeze QT interface"""
self.disable_actions()
"""If more than one line is selected, keep the first"""
sel_uid = self.selected_uids[0]
current_line = (
self.actors_df.loc[self.actors_df["uid"] == sel_uid, "actor"]
.values[0]
.GetMapper()
.GetInput()
)
extender = Editor(self)
extender.EnabledOn()
extender.initialize(current_line, "extend")
self.plotter.track_click_position(
side="right", callback=lambda event: end_edit(event, sel_uid)
)
def split_line_line(self):
"""Split line (paper) with another line (scissors). First, select the paper-line then the scissors-line"""
print(
"Split line with line. Line to be split has been selected, please select an intersecting line."
)
"""Terminate running event loops"""
"""Check if a line is selected"""
if not self.selected_uids:
print(" -- No input data selected -- ")
return
elif len(self.selected_uids) <= 1:
print(" -- Not enough input data selected. Select at least 2 objects -- ")
return
"""Freeze QT interface"""
self.disable_actions()
current_uid_scissors = self.selected_uids[-1]
if (
self.parent.geol_coll.get_uid_topology(current_uid_scissors)
!= "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(current_uid_scissors)
!= "XsPolyLine"
):
print(" -- Selected scissor is not a line -- ")
return
if isinstance(self, ViewMap):
inU = self.parent.geol_coll.get_uid_vtk_obj(current_uid_scissors).points_X
inV = self.parent.geol_coll.get_uid_vtk_obj(current_uid_scissors).points_Y
elif isinstance(self, ViewXsection):
inU, inV = self.parent.geol_coll.get_uid_vtk_obj(
current_uid_scissors
).world2plane()
inUV_scissors = np_column_stack((inU, inV))
shp_line_in_scissors = shp_linestring(inUV_scissors)
for current_uid_paper in self.selected_uids[:-1]:
if (
self.parent.geol_coll.get_uid_topology(current_uid_paper)
!= "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(current_uid_paper)
!= "XsPolyLine"
):
print(" -- Selected paper is not a line -- ")
return
if isinstance(self, ViewMap):
inU = self.parent.geol_coll.get_uid_vtk_obj(current_uid_paper).points_X
inV = self.parent.geol_coll.get_uid_vtk_obj(current_uid_paper).points_Y
elif isinstance(self, ViewXsection):
inU, inV = self.parent.geol_coll.get_uid_vtk_obj(
current_uid_paper
).world2plane()
inUV_paper = np_column_stack((inU, inV))
"""Create deepcopies of the selected entities. Split U- and V-coordinates."""
# inU_paper = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(current_uid_paper).points[:, 0])
# inV_paper = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(current_uid_paper).points[:, 1])
# inZ_paper = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(current_uid_paper).points[:, 2])
# inU_scissors = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(current_uid_scissors).points[:, 0])
# inV_scissors = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(current_uid_scissors).points[:, 1])
# inZ_scissors = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(current_uid_scissors).points[:, 2])
"""Stack coordinates in two-columns matrix"""
# inUV_paper = np_column_stack((inU_paper, inV_paper,inZ_paper))
"""Run the Shapely function."""
shp_line_in_paper = shp_linestring(inUV_paper)
"""Check if the two lineal geometries have shared path with dimension 1 (= they share a line-type object)"""
if shp_line_in_paper.crosses(shp_line_in_scissors):
"""Run the split shapely function."""
split_lines = shp_split(shp_line_in_paper, shp_line_in_scissors) # lines must include all line parts not affected by splitting and two parts for the split line__________
else: # handles the case when the shp_linestring share a linear path and, for the moment, exists the tool
"""Un-Freeze QT interface"""
self.clear_selection()
self.enable_actions()
return
replace = 1 # replace = 1 for the first line to operate replace_vtk
uids = [current_uid_scissors]
for line in split_lines.geoms:
"""Create empty dictionary for the output lines."""
new_line = deepcopy(self.parent.geol_coll.entity_dict)
new_line["name"] = (
self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == current_uid_paper, "name"
].values[0]
+ "_split"
)
new_line["topology"] = self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == current_uid_paper, "topology"
].values[0]
new_line["role"] = self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == current_uid_paper, "role"
].values[0]
new_line["feature"] = self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == current_uid_paper,
"feature",
].values[0]
new_line["scenario"] = self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == current_uid_paper, "scenario"
].values[0]
outU = np_array(line.coords)[:, 0]
outV = np_array(line.coords)[:, 1]
if isinstance(self, ViewMap):
new_line["x_section"] = None
new_line["vtk_obj"] = PolyLine()
outX = outU
outY = outV
outZ = np_zeros(np_shape(outX))
elif isinstance(self, ViewXsection):
new_line["x_section"] = self.this_x_section_uid
new_line["vtk_obj"] = XsPolyLine(
self.this_x_section_uid, parent=self.parent
)
outX, outY, outZ = self.parent.xsect_coll.plane2world(
self.this_x_section_uid, outU, outV
)
"""Create new vtk objects"""
outXYZ = np_column_stack((outX, outY, outZ))
new_line["vtk_obj"].points = outXYZ
new_line["vtk_obj"].auto_cells()
if new_line["vtk_obj"].points_number > 0:
"""Replace VTK object"""
if replace == 1:
self.parent.geol_coll.replace_vtk(uid=current_uid_paper, vtk_object=new_line["vtk_obj"])
self.parent.geol_coll.signals.geom_modified.emit(
[current_uid_paper]
) # emit uid as list to force redraw()
replace = 0
uids.append(current_uid_paper)
else:
"""Create entity from the dictionary"""
uid = self.parent.geol_coll.add_entity_from_dict(new_line)
uids.append(uid)
del new_line["vtk_obj"]
else:
print("Empty object")
"""Deselect input line and force redraw"""
# self.parent.geol_coll.signals.geom_modified.emit(uids) # emit uid as list to force redraw()
self.clear_selection()
"""Un-Freeze QT interface"""
self.enable_actions()
def split_line_existing_point(self):
# Here transformation to UV is not necessary since we select a point in world space
def end_select(event, uid):
point_pos = selector.active_pos
self.plotter.untrack_click_position(side="right")
"""Create empty dictionary for the output line"""
new_line_1 = deepcopy(self.parent.geol_coll.entity_dict)
new_line_2 = deepcopy(self.parent.geol_coll.entity_dict)
new_line_2["name"] = (
self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == uid, "name"
].values[0]
+ "_split"
)
new_line_2["topology"] = self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == uid, "topology"
].values[0]
new_line_2["role"] = self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == uid, "role"
].values[0]
new_line_2["feature"] = self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == uid, "feature"
].values[0]
new_line_2["scenario"] = self.parent.geol_coll.df.loc[
self.parent.geol_coll.df["uid"] == uid, "scenario"
].values[0]
if isinstance(self, ViewMap):
inU_line = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(uid).points[:, 0])
inV_line = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(uid).points[:, 1])
elif isinstance(self, ViewXsection):
inU_line = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(uid).points_W)
inV_line = deepcopy(self.parent.geol_coll.get_uid_vtk_obj(uid).points_Z)
new_line_2["x_section"] = self.this_x_section_uid
"""Stack coordinates in two-columns matrix"""
inUV_line = np_column_stack((inU_line, inV_line))
"""Run the Shapely function."""
shp_line_in = shp_linestring(
deepcopy(self.parent.geol_coll.get_uid_vtk_obj(uid).points)
)
# x_vertex_unit = deepcopy(current_line_U_true[vertex_ind])
# y_vertex_unit = deepcopy(current_line_V_true[vertex_ind])
shp_point_in = shp_point(point_pos[0], point_pos[1], point_pos[2])
"""Splitting shapely function."""
split_lines = shp_split(shp_line_in, shp_point_in)
line1_out = shp_linestring(split_lines.geoms[0])
line2_out = shp_linestring(split_lines.geoms[1])
"""Convert shapely lines to UV objects"""
outUV_1 = deepcopy(np_array(line1_out.coords))
outUV_2 = deepcopy(np_array(line2_out.coords))
"""Un-stack output coordinates and write them to the empty dictionary."""
outU_1 = outUV_1[:, 0]
outV_1 = outUV_1[:, 1]
outU_2 = outUV_2[:, 0]
outV_2 = outUV_2[:, 1]
if isinstance(self, ViewMap):
outX_1 = outU_1
outY_1 = outV_1
outZ_1 = np_zeros(np_shape(outX_1))
outX_2 = outU_2
outY_2 = outV_2
outZ_2 = np_zeros(np_shape(outX_2))
elif isinstance(self, ViewXsection):
outX_1, outY_1 = self.parent.xsect_coll.get_XY_from_W(
section_uid=self.this_x_section_uid, W=outU_1
)
outZ_1 = outV_1
outX_2, outY_2 = self.parent.xsect_coll.get_XY_from_W(
section_uid=self.this_x_section_uid, W=outU_2
)
outZ_2 = outV_2
new_points_1 = np_column_stack((outX_1, outY_1, outZ_1))
new_points_2 = np_column_stack((outX_2, outY_2, outZ_2))
if isinstance(self, ViewMap):
new_line_1["vtk_obj"] = PolyLine()
new_line_2["vtk_obj"] = PolyLine()
elif isinstance(self, ViewXsection):
new_line_1["vtk_obj"] = XsPolyLine(
self.this_x_section_uid, parent=self.parent
)
new_line_2["vtk_obj"] = XsPolyLine(
self.this_x_section_uid, parent=self.parent
)
new_line_1["vtk_obj"].points = deepcopy(np_array(line1_out.coords))
new_line_1["vtk_obj"].auto_cells()
new_line_2["vtk_obj"].points = deepcopy(np_array(line2_out.coords))
new_line_2[
"vtk_obj"
].auto_cells() # lines must include all line parts not affected by splitting and two parts for the split line__________
"""Replace VTK object"""
if new_line_1["vtk_obj"].points_number > 0:
self.parent.geol_coll.replace_vtk(uid=uid, vtk_object=new_line_1["vtk_obj"])
del new_line_1
else:
print("Empty object")
"""Create entity from the dictionary"""
if new_line_2["vtk_obj"].points_number > 0:
self.parent.geol_coll.add_entity_from_dict(new_line_2)
del new_line_2
else:
print("Empty object")
"""Deselect input line."""
self.clear_selection()
selector.EnabledOff()
"""Un-Freeze QT interface"""
self.enable_actions()
"""Split line at selected existing point (vertex)"""
print(
"Split line at existing point. Line to be split has been selected, "
"please select an existing point for splitting."
)
"""Check if a line is selected"""
if not self.selected_uids:
print(" -- No input data selected -- ")
return
if (
self.parent.geol_coll.get_uid_topology(self.selected_uids[0])
!= "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(self.selected_uids[0])
!= "XsPolyLine"
):
print(" -- Selected data is not a line -- ")
return
"""Freeze QT interface"""
self.disable_actions
"""If more than one line is selected, keep the first"""
sel_uid = self.selected_uids[0]
current_line = self.actors_df.loc[self.actors_df["uid"] == sel_uid, "actor"].values[
0
]
line = current_line.mapper.dataset
selector = Editor(self)
selector.EnabledOn()
selector.initialize(line, "select")
self.plotter.track_click_position(
side="right", callback=lambda event: end_select(event, sel_uid)
)
def split_line_vector(self, vector):
...
# check merge, snap, and see if a bridge nodes method is needed____________________
def merge_lines(self):
"""Merge two (contiguous or non-contiguous) lines.
Metadata will be taken from the first selected line."""
# Freeze QT interface
self.disable_actions()
# Check if at least 2 lines are selected.
if not self.selected_uids:
print(" -- No input data selected -- ")
self.enable_actions()
return
# Create local copy of selected_uids
in_uids = self.selected_uids
if len(in_uids) <= 1:
print(" -- Not enough input data selected. Select at least 2 objects -- ")
self.enable_actions()
return
# Check if all input entities are PolyLine or XsPolyLine
print(in_uids)
for uid in in_uids:
if self.parent.geol_coll.get_uid_topology(uid) == "PolyLine":
continue
elif self.parent.geol_coll.get_uid_topology(uid) == "XsPolyLine":
continue
else:
print(" -- Selection must include lines only -- ")
self.enable_actions()
return
# For XsPolyLine, check that they all belong to the same cross-section.
this_xsection = None
for uid in in_uids:
if self.parent.geol_coll.get_uid_topology(uid) == "XsPolyLine":
if this_xsection is None:
this_xsection = self.parent.geol_coll.get_uid_x_section(uid)
elif this_xsection is not None:
if self.parent.geol_coll.get_uid_x_section(uid) != this_xsection:
print(" -- Selection must include lines belonging to the same cross-section only -- ")
self.enable_actions()
return
# Create empty dictionary for the output line.
new_line = deepcopy(self.parent.geol_coll.entity_dict)
# Populate metadata from first selected line.
new_line["name"] = self.parent.geol_coll.get_uid_name(in_uids[0])
new_line["topology"] = self.parent.geol_coll.get_uid_topology(in_uids[0])
new_line["role"] = self.parent.geol_coll.get_uid_role(in_uids[0])
new_line["feature"] = self.parent.geol_coll.get_uid_feature(in_uids[0])
new_line["scenario"] = self.parent.geol_coll.get_uid_scenario(in_uids[0])
new_line["x_section"] = self.parent.geol_coll.get_uid_x_section(in_uids[0])
# Mering properties not yet implemented.
new_line["properties_names"] = []
new_line["properties_components"] = []
# Create empty PolyLine() or XsPolyLine().
if self.parent.geol_coll.get_uid_topology(in_uids[0]) == "XsPolyLine":
new_line["vtk_obj"] = XsPolyLine()
else:
new_line["vtk_obj"] = PolyLine()
# Add points to new merged line.
points_0 = self.parent.geol_coll.get_uid_vtk_obj(in_uids[0]).points.copy()
for uid in in_uids[1::]:
points_1 = self.parent.geol_coll.get_uid_vtk_obj(uid).points.copy()
first2first = points_0[1,:] - points_1[1,:]
first2first_norm = np_norm(first2first)
first2last = points_0[1, :] - points_1[-1, :]
first2last_norm = np_norm(first2last)
last2first = points_0[-1, :] - points_1[1, :]
last2first_norm = np_norm(last2first)
last2last = points_0[-1, :] - points_1[-1, :]
last2last_norm = np_norm(last2last)
scores = np_array([first2first_norm, first2last_norm, last2first_norm, last2last_norm])
# Smaller norm first2first_norm -> join first node of points_0 to first point of points_1 -> need to revert points_0
if scores.argmin() == 0:
points_0 = np_flipud(points_0)
# Smaller norm first2last_norm -> join first node of points_0 to last point of points_1 -> need to revert both
if scores.argmin() == 1:
points_0 = np_flipud(points_0)
points_1 = np_flipud(points_1)
# Smaller norm last2first_norm -> join last node of points_0 to first point of points_1 -> need to revert none
if scores.argmin() == 2:
pass
# Smaller norm last2last_norm -> join last node of points_0 to last point of points_1 -> need to revert points_1
if scores.argmin() == 3:
points_1 = np_flipud(points_1)
points_0 = np_concatenate((points_0, points_1), axis=0)
new_line["vtk_obj"].points = points_0
# Automatically create all line cells.
new_line["vtk_obj"].auto_cells()
# Deselect input lines.
self.clear_selection()
# Remove input lines.
for uid in in_uids:
self.parent.geol_coll.remove_entity(uid)
self.parent.geol_coll.add_entity_from_dict(new_line)
"""Un-Freeze QT interface"""
self.enable_actions()
def snap_line(self):
"""Snaps vertices of the selected line (the snapping-line) to the nearest vertex of the chosen line (goal-line),
depending on the Tolerance parameter."""
print(
"Snap line to line. Line to be snapped has been selected, please select second line."
)
"""Terminate running event loops"""
"""Check if a line is selected"""
if not self.selected_uids:
print(" -- No input data selected -- ")
return
elif len(self.selected_uids) <= 1:
print(" -- Not enough input data selected. Select at least 2 objects -- ")
return
"""Freeze QT interface"""
self.disable_actions()
current_uid_goal = self.selected_uids[-1]
if (
self.parent.geol_coll.get_uid_topology(current_uid_goal) != "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(current_uid_goal) != "XsPolyLine"
):
print(" -- Selected goal is not a line -- ")
return
tolerance = input_one_value_dialog(
parent=self,
title="Snap tolerance",
label="Insert snap tolerance",
default_value=10,
)
for current_uid_snap in self.selected_uids[:-1]:
print(current_uid_snap)
if (
self.parent.geol_coll.get_uid_topology(current_uid_snap)
!= "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(current_uid_snap)
!= "XsPolyLine"
):
print(" -- Selected snap is not a line -- ")
return
"""Create empty dictionary for the output line."""
new_line_snap = deepcopy(self.parent.geol_coll.entity_dict)
new_line_goal = deepcopy(self.parent.geol_coll.entity_dict)
"""Editing loop. Get coordinates of the line to be modified (snap-line)."""
if isinstance(self, ViewMap):
new_line_snap["vtk_obj"] = PolyLine()
new_line_snap["x_section"] = None
new_line_goal["vtk_obj"] = PolyLine()
new_line_goal["x_section"] = None
inU_snap = deepcopy(
self.parent.geol_coll.get_uid_vtk_obj(current_uid_snap).points_X
)
inV_snap = deepcopy(
self.parent.geol_coll.get_uid_vtk_obj(current_uid_snap).points_Y
)
inU_goal = deepcopy(
self.parent.geol_coll.get_uid_vtk_obj(current_uid_goal).points_X
)
inV_goal = deepcopy(
self.parent.geol_coll.get_uid_vtk_obj(current_uid_goal).points_Y
)
elif isinstance(self, ViewXsection):
new_line_snap["vtk_obj"] = XsPolyLine(
self.this_x_section_uid, parent=self.parent
)
new_line_snap["x_section"] = self.this_x_section_uid
new_line_goal["vtk_obj"] = XsPolyLine(
self.this_x_section_uid, parent=self.parent
)
new_line_goal["x_section"] = self.this_x_section_uid
inU_snap, inV_snap = self.parent.geol_coll.get_uid_vtk_obj(
current_uid_snap
).world2plane()
inU_goal, inV_goal = self.parent.geol_coll.get_uid_vtk_obj(
current_uid_goal
).world2plane()
"""Stack coordinates in two-columns matrix"""
inUV_snap = np_column_stack((inU_snap, inV_snap))
inUV_goal = np_column_stack((inU_goal, inV_goal))
"""Run the Shapely function."""
shp_line_in_snap = shp_linestring(inUV_snap)
shp_line_in_goal = shp_linestring(inUV_goal)
shp_line_in_goal, extended = int_node(shp_line_in_goal, shp_line_in_snap)
# plt.plot(np_array(shp_line_in_goal.coords)[:, 0], np_array(shp_line_in_goal.coords)[:, 1], 'r-o')
# plt.plot(np_array(extended.coords)[:, 0], np_array(extended.coords)[:, 1], 'b-o')
# plt.show()
"""In the snapping tool, the last input value is called Tolerance. Can be modified, do some checks.
Little tolerance risks of not snapping distant lines, while too big tolerance snaps to the wrong vertex and
not to the nearest one"""
if shp_line_in_snap.is_simple and shp_line_in_goal.is_simple:
shp_line_out_snap = shp_snap(shp_line_in_snap, shp_line_in_goal, tolerance)
else:
print("Polyline is not simple, it self-intersects")
"""Un-Freeze QT interface"""
self.enable_actions()
return
shp_line_out_diff = shp_line_out_snap.difference(
shp_line_in_goal
) # eliminate the shared path that Snap may create
outUV_snap = deepcopy(np_array(shp_line_out_diff.coords))
outUV_goal = deepcopy(np_array(shp_line_in_goal.coords))
"""Un-stack output coordinates and write them to the empty dictionary."""
if outUV_snap.ndim < 2:
print("Invalid shape")
continue
outU_snap = outUV_snap[:, 0]
outV_snap = outUV_snap[:, 1]
outU_goal = outUV_goal[:, 0]
outV_goal = outUV_goal[:, 1]
"""Convert local coordinates to XYZ ones."""
if isinstance(self, ViewMap):
outX_snap = outU_snap
outY_snap = outV_snap
outZ_snap = np_zeros(np_shape(outX_snap))
outX_goal = outU_goal
outY_goal = outV_goal
outZ_goal = np_zeros(np_shape(outX_goal))
elif isinstance(self, ViewXsection):
outX_snap, outY_snap, outZ_snap = self.parent.xsect_coll.plane2world(
self.this_x_section_uid, outU_snap, outV_snap
)
outX_goal, outY_goal, outZ_goal = self.parent.xsect_coll.plane2world(
self.this_x_section_uid, outU_goal, outV_goal
)
# outZ = outV
"""Create new vtk objects"""
new_points_snap = np_column_stack((outX_snap, outY_snap, outZ_snap))
new_points_goal = np_column_stack((outX_goal, outY_goal, outZ_goal))
new_line_snap["vtk_obj"].points = new_points_snap
new_line_snap["vtk_obj"].auto_cells()
new_line_goal["vtk_obj"].points = new_points_goal
new_line_goal["vtk_obj"].auto_cells()
"""Replace VTK object"""
if new_line_snap["vtk_obj"].points_number > 0:
self.parent.geol_coll.replace_vtk(uid=current_uid_snap,vtk_object=new_line_snap["vtk_obj"])
self.parent.geol_coll.replace_vtk(uid=current_uid_goal,vtk_object=new_line_goal["vtk_obj"])
del new_line_snap
del new_line_goal
else:
print("Empty object")
"""Un-Freeze QT interface"""
self.clear_selection()
self.enable_actions()
def resample_line_distance(
self,
): # this must be done per-part_______________________________________________________
"""Resample selected line with constant spacing. Distance of spacing is required"""
print("Resample line. Define constant spacing for resampling.")
"""Terminate running event loops"""
"""Check if a line is selected"""
if not self.selected_uids:
print(" -- No input data selected -- ")
return
"""Freeze QT interface"""
self.disable_actions()
"""Ask for distance for evenly spacing resampling"""
distance_delta = input_one_value_dialog(
parent=self,
title="Spacing distance for Line Resampling",
label="Insert spacing distance",
default_value="Distance",
)
for current_uid in self.selected_uids:
if (
self.parent.geol_coll.get_uid_topology(current_uid) != "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(current_uid) != "XsPolyLine"
):
print(" -- Selected data is not a line -- ")
return
if distance_delta is None or isinstance(distance_delta, str):
"""Un-Freeze QT interface"""
for action in self.findChildren(QAction):
action.setEnabled(True)
return
else:
distance_delta = int(distance_delta)
if distance_delta <= 0:
distance_delta = 20
"""Create empty dictionary for the output line"""
new_line = deepcopy(self.parent.geol_coll.entity_dict)
if isinstance(self, ViewMap):
new_line["topology"] = "PolyLine"
new_line["x_section"] = None
inU = deepcopy(
self.parent.geol_coll.get_uid_vtk_obj(current_uid).points[:, 0]
)
inV = deepcopy(
self.parent.geol_coll.get_uid_vtk_obj(current_uid).points[:, 1]
)
elif isinstance(self, ViewXsection):
new_line["topology"] = "XsPolyLine"
new_line["x_section"] = self.this_x_section_uid
inU, inV = self.parent.geol_coll.get_uid_vtk_obj(current_uid).world2plane()
"""Stack coordinates in two-columns matrix"""
inUV = np_column_stack((inU, inV))
"""Run the Shapely function."""
shp_line_in = shp_linestring(inUV)
if distance_delta >= shp_line_in.length:
while distance_delta >= shp_line_in.length:
distance_delta = distance_delta / 2
distances = np_arange(0, shp_line_in.length, distance_delta)
points = [[np_array(shp_line_in.interpolate(distance).coords) for distance in distances], np_array(shp_line_in.coords[-1])]
print("points: ", points)
shp_line_out = shp_linestring(points)
outUV = deepcopy(np_array(shp_line_out.coords))
"""Un-stack output coordinates and write them to the empty dictionary."""
outU = outUV[:, 0]
outV = outUV[:, 1]
if isinstance(self, ViewMap):
# if isinstance(self, (ViewMap, ViewMap)):
outX = outU
outY = outV
outZ = np_zeros(np_shape(outX))
new_line["vtk_obj"] = PolyLine()
# elif isinstance(self, (ViewXsection, ViewXsection)):
elif isinstance(self, ViewXsection):
outX, outY, outZ = self.parent.xsect_coll.plane2world(
self.this_x_section_uid, outU, outV
)
new_line["vtk_obj"] = XsPolyLine(
self.this_x_section_uid, parent=self.parent
)
outXYZ = np_column_stack((outX, outY, outZ))
new_line["vtk_obj"].points = outXYZ
new_line["vtk_obj"].auto_cells()
"""Replace VTK object"""
if new_line["vtk_obj"].points_number > 0:
self.parent.geol_coll.replace_vtk(uid=current_uid, vtk_object=new_line["vtk_obj"])
del new_line
else:
print("Empty object")
"""Deselect input line."""
self.parent.geol_coll.signals.geom_modified.emit(
[current_uid]
) # emit uid as list to force redraw()
"""Un-Freeze QT interface"""
self.clear_selection()
self.enable_actions()
def resample_line_number_points(
self,
): # this must be done per-part___________________________________________________
"""Resample selected line with constant spacing. Number of points to divide the line in is required"""
print("Resample line. Define number of vertices to create on the line.")
"""Terminate running event loops"""
"""Check if a line is selected"""
if not self.selected_uids:
print(" -- No input data selected -- ")
return
"""Freeze QT interface"""
self.disable_actions()
"""Ask for the number of points for evenly spacing resampling"""
number_of_points = input_one_value_dialog(
parent=self,
title="Number of points for Line Resampling",
label="Insert number of points",
default_value="Number",
)
for current_uid in self.selected_uids:
if (
self.parent.geol_coll.get_uid_topology(current_uid) != "PolyLine"
) and (
self.parent.geol_coll.get_uid_topology(current_uid) != "XsPolyLine"
):
print(" -- Selected data is not a line -- ")
return
if number_of_points is None or isinstance(number_of_points, str):
"""Un-Freeze QT interface"""
self.enable_actions()
return
else: