-
Notifications
You must be signed in to change notification settings - Fork 91
/
wings_sel_cmd.erl
1600 lines (1453 loc) · 51 KB
/
wings_sel_cmd.erl
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
%%
%% wings_sel_cmd.erl --
%%
%% This module implements the commands in the selection menu.
%%
%% Copyright (c) 2001-2011 Bjorn Gustavsson
%%
%% See the file "license.terms" for information on usage and redistribution
%% of this file, and for a DISCLAIMER OF ALL WARRANTIES.
%%
%% $Id$
%%
-module(wings_sel_cmd).
-export([menu/0,update_menu/1,command/2]).
%% Utilities.
-export([init/0,select_all/1]).
-include("wings.hrl").
-import(lists, [map/2,foldl/3,reverse/1,member/2,keymember/3,keyfind/3,
usort/1,any/2]).
init() ->
wings_pref:set_default(saved_selections_cycle_by_mode,false).
update_menu(#st{selmode=Mode}) ->
FaceMode = Mode =:= face,
[wings_menu:update_menu_enabled(select, Cmd, FaceMode)
|| {_, Cmd, _} <- faces_menu()].
menu() ->
Help = ?__(99," (from selection or all visible objects (if no selection))"),
RHelp = random_help(),
Objects = ?__(106," Objects"),
[{?__(1,"Deselect"),deselect,?__(2,"Clear the selection")},
separator,
{?__(3,"More"),more,more_help()},
{?__(4,"Less"),less,less_help()},
{?__(5,"Similar"),similar,similar_help()} | faces_menu()] ++
[separator,
{?__(6,"Edge Loop"),
{edge_loop,
[{?__(7,"Edge Loop"),
edge_loop,?__(8,"Expand edge selection to loop; ")++
?__(9,"convert face selection to selected border edges")++
?__(98,"; convert consecutive vertices to edges")},
{?__(10,"Edge Loop to Region"),edge_loop_to_region,
?__(11,"Select all faces on one side of an edge loop")},
{?__(12,"Edge Ring"),
edge_ring,?__(13,"Expand edge selection to ring")},
{?__(100,"Every Nth Ring"),{nth_edge_ring,
[{?__(101,"Second"),2},
{?__(102,"Third"),3},
{?__(103,"Nth..."),true}]}},
{?__(109,"Every Nth Loop"),{nth_edge_loop,
[{?__(101,"Second"),2},
{?__(102,"Third"),3},
{?__(103,"Nth..."),true}]}},
separator,
{?__(14,"Previous Edge Loop"),
prev_edge_loop,?__(15,"Select the previous edge loop")},
{?__(16,"Next Edge Loop"),
next_edge_loop,?__(17,"Select the next edge loop")},
separator,
{?__(18,"Grow Edge Loop"),edge_link_incr,
?__(19,"Grow edge selection by one edge in loop directions")},
{?__(20,"Shrink Edge Loop"),edge_link_decr,
?__(21,"Shrink edge selection by one in loop direction")},
{?__(22,"Grow Edge Ring"),edge_ring_incr,
?__(23,"Grow edge selection by one edge in ring direction")},
{?__(24,"Shrink Edge Ring"),edge_ring_decr,
?__(25,"Shrink edge selection by one edge in ring directions")},
separator,
{?__(96,"To Complete Loops"),complete_loops,
?__(97,"Switches to Edge mode and completes loop selection")}]}},
separator,
{?__(26,"Adjacent"),
{adjacent,[{?__(27,"Vertices"),vertex},
{?__(28,"Edges"),edge},
{?__(29,"Faces"),face},
{?__(30,"Objects"),body}]}},
{?__(31,"By"),
{by,[{?__(32,"Hard Edges"),
hard_edges,?__(33,"Select all hard edges")++Help},
{?__(34,"Isolated Vertices"),
isolated_vertices,?__(35,"Select all isolated vertices")++Help},
{?__(85,"Non-planar Faces..."),
nonplanar_faces,?__(86,"Select all non-planar faces")++Help},
{?__(36,"Vertices With"),
{vertices_with,
[{?__(37,"2 Edges"),2,Help},
{?__(38,"3 Edges"),3,Help},
{?__(39,"4 Edges"),4,Help},
{?__(40,"5 Edges"),5,Help},
{?__(401,"6 or More"),6,Help},
{?__(402,"Specify..."),true,Help}]}},
{?__(41,"Faces With"),
{faces_with,
[{?__(42,"2 Edges"),2,Help},
{?__(43,"3 Edges"),3,Help},
{?__(44,"4 Edges"),4,Help},
{?__(45,"5 or More"),5,Help},
{?__(402,"Specify..."),true,Help}]}},
{?__(nq0,"Non Quadrangle Faces"),
{non_quad,
[{?__(nq1,"All Non Quadrangle Faces"),all,Help},
{?__(nq2,"Odd Non Quadrangle Faces"),odd,Help},
{?__(nq3,"Even Non Quadrangle Faces"),even,Help}]}},
{?__(46,"Random"),
{random,[{"10%",10, RHelp},
{"20%",20, RHelp},
{"30%",30, RHelp},
{"40%",40, RHelp},
{"50%",50, RHelp},
{"60%",60, RHelp},
{"70%",70, RHelp},
{"80%",80, RHelp},
{"90%",90, RHelp},
{"__%",true, RHelp}
]}},
{?__(56,"Short Edges..."),
short_edges,?__(57,"Select (too) short edges")++Help},
{?__(87,"Sharp Edges..."),
sharp_edges,?__(88,"Select sharp edges")++Help},
{?__(95,"Vertex Path"),
{vertex_path,
[{?__(89,"Fewest Edges Path"),
fewest_edges_path,?__(90,"Select the path with the fewest edges between two vertices")},
{?__(91,"Shortest Path (Dijkstra)"),
dijkstra_shortest_path,?__(92,"Select the shortest path between two vertices (Dijkstra)")},
{?__(93,"Shortest Path (A-Star)"),
astar_shortest_path,?__(94,"Select the shortest path between two vertices (A-Star)")}]}},
{?__(58,"Material Edges"),material_edges,
?__(59,"Select all edges between different materials")++Help},
{?__(60,"UV-Mapped Faces"),uv_mapped_faces,
?__(61,"Select all edges that have UV coordinates")++Help},
{?__(62,"Id..."),id,?__(63,"Select by numeric id")},
{?__(107,"Name..."),
by_name,?__(108,"Select objects by name. *'s may be used as wildcards")}]}},
{?__(64,"Lights"),lights,?__(65,"Select all lights")},
separator,
{?__(661, "All"), all,?__(66,"Select all elements")},
separator,
{?__(67,"Inverse"),inverse,?__(68,"Invert the selection")},
separator,
{?__(69,"Hide Selected")++Objects,
hide_selected,
?__(70,"Hide all (partly or wholly) selected objects")},
{?__(71,"Hide Unselected")++Objects,
hide_unselected,?__(72,"Hide objects that have no selection")},
{?__(110,"Lock Selected")++Objects,
lock_selected,
?__(111,"Lock all (partly or wholly) selected objects")},
{?__(73,"Lock Unselected")++Objects,
lock_unselected,?__(74,"Lock objects that have no selection")},
separator,
{?__(104,"Unhide All Objects"),
show_all,?__(76,"Show all objects that have been hidden")},
{?__(105,"Unlock All Objects"),
unlock_all,?__(78,"Unlock all locked objects")},
separator | groups_menu()].
random_help() ->
?__(1,"Select random elements from current selection, or all visible objects (no selection)").
faces_menu() ->
[{?__(1,"Similar Normals..."), oriented_faces,
?__(11,"Select faces with normals similar to those of the already selected faces")},
{?__(2,"Similar Area"), similar_area,
?__(21,"Select faces with areas similar to that of the already selected face")},
{?__(3,"Similar Material..."),similar_material,
?__(31,"Select faces with a similar material to those already selected")}].
groups_menu() ->
[{?__(22,"Selection Groups"),
{ssels,
[{?__(79,"Store Selection"),store_selection,
?__(80,"Store the selection into the selection group named \"StoredSelection\"")},
{?__(81,"Recall Selection"),recall_selection,
?__(82,"Recall the selection from the selection group named \"StoredSelection\"")},
separator,
{?__(83,"New Group..."),new_group,?__(84,"Create a new selection group")},
separator,
{?__(24,"Next Group"),next_group},
{?__(25,"Previous Group"),prev_group},
{?__(26,"Cycle In Selection Mode"),saved_selections_cycle_by_mode,
?__(27,"Cycle Prev/Next only within active selection mode"),
wings_menu_util:crossmark(saved_selections_cycle_by_mode)}]}}].
more_help() ->
?__(1,"Select all elements adjacent to the selected elements").
less_help() ->
?__(1,"Deselect all elements adjacent to the unselected elements").
similar_help() ->
?__(1,"Select elements similar to the already selected elements").
command({edge_loop,edge_loop}, #st{selmode=vertex}=St) ->
{save_state,vs_to_edge_loop(St)};
command({edge_loop,edge_loop}, #st{selmode=face}=St) ->
{save_state,face_region_to_edge_loop(St)};
command({edge_loop,edge_loop}, St) ->
{save_state,wings_edge_loop:stoppable_sel_loop(St)};
command({edge_loop,edge_link_decr}, St) ->
{save_state,wings_edge_loop:select_link_decr(St)};
command({edge_loop,edge_link_incr}, St) ->
{save_state,wings_edge_loop:select_link_incr(St)};
command({edge_loop,{nth_edge_ring,N}}, St) ->
select_nth_ring(N, St);
command({edge_loop,{nth_edge_loop,N}}, St) ->
select_nth_loop(N, St);
command({edge_loop,edge_ring}, St) ->
{save_state,wings_edge:select_edge_ring(St)};
command({edge_loop,edge_ring_incr}, St) ->
{save_state,wings_edge:select_edge_ring_incr(St)};
command({edge_loop,edge_ring_decr}, St) ->
{save_state,wings_edge:select_edge_ring_decr(St)};
command({edge_loop,next_edge_loop}, St) ->
{save_state,wings_edge_loop:select_next(St)};
command({edge_loop,prev_edge_loop}, St) ->
{save_state,wings_edge_loop:select_prev(St)};
command({edge_loop,edge_loop_to_region}, St) ->
{save_state,wings_edge:select_region(St)};
command({edge_loop,complete_loops}, St) ->
{save_state,complete_loops(St)};
command(deselect, St) ->
{save_state,deselect(St)};
command(more, St) ->
{save_state,wings_sel_conv:more(St)};
command(less, St) ->
{save_state,wings_sel_conv:less(St)};
command(all, St) ->
{save_state,select_all(St)};
command(lights, St) ->
{save_state,select_lights(St)};
command({by,Command}, St) ->
by_command(Command, St);
command(similar, St) ->
{save_state,similar(St)};
command(oriented_faces, St) ->
oriented_faces(true, St);
command({oriented_faces,Ask}, St) ->
oriented_faces(Ask, St);
command(similar_area, St) ->
similar_area(true, St);
command({similar_area,Ask}, St) ->
similar_area(Ask, St);
command(similar_material, St) ->
similar_material(true, St);
command({similar_material,Ask}, St) ->
similar_material(Ask, St);
command(inverse, St) ->
{save_state,inverse(St)};
command(hide_selected, St) ->
{save_state,hide_selected(St)};
command(hide_unselected, St) ->
{save_state,hide_unselected(St)};
command(lock_selected, St) ->
{save_state,lock_selected(St)};
command(lock_unselected, St) ->
{save_state,lock_unselected(St)};
command(show_all, St0) ->
All = wings_obj:fold(fun(#{id:=I}, A) -> [I|A] end, [], St0),
St = wings_obj:unhide(All, St0),
{save_state,St};
command(unlock_all, St0) ->
All = wings_obj:fold(fun(#{id:=I}, A) -> [I|A] end, [], St0),
St = wings_obj:unlock(All, St0),
{save_state,St};
command({adjacent,Type}, St) ->
set_select_mode(Type, St);
command({ssels, store_selection}, #st{ssels=Ssels0,selmode=Mode,sel=Sel}=St) ->
Key = {Mode,"StoredSelection"},
Ssels = gb_trees:enter(Key, Sel, Ssels0),
{save_state,St#st{ssels=Ssels}};
command({ssels, recall_selection}, #st{selmode=Mode,ssels=Ssels}=St0) ->
Key = {Mode, "StoredSelection"},
case gb_trees:is_defined(Key, Ssels) of
false -> St0;
true ->
St = select_group(Key, St0),
{save_state,St}
end;
command({ssels,saved_selections_cycle_by_mode}, _St) ->
Pref = wings_pref:get_value(saved_selections_cycle_by_mode),
wings_pref:set_value(saved_selections_cycle_by_mode, not Pref),
keep;
command({ssels,next_group}, St) ->
{save_state,cycle_group(next_group, St)};
command({ssels,prev_group}, St) ->
{save_state,cycle_group(prev_group, St)};
command({ssels,{select_group,Id}}, St) ->
{save_state,select_group(Id, St)};
command({ssels,{union_group,Id}}, St) ->
{save_state,union_group(Id, St)};
command({ssels,{subtract_group,Id}}, St) ->
{save_state,subtract_group(Id, St)};
command({ssels,{intersect_group,Id}}, St) ->
{save_state,intersect_group(Id, St)};
command({ssels,{add_to_group,Id}}, St) ->
{save_state,add_to_group(Id, St)};
command({ssels,{replace_group,Id}}, St) ->
{save_state,replace_group(Id, St)};
command({ssels,{subtract_from_group,Id}}, St) ->
{save_state,subtract_from_group(Id, St)};
command({new_group_name, Name}, St) ->
{save_state,new_group_name(Name, St)};
command({ssels, new_group}, St) ->
new_group(St);
command({ssels,{delete_group,invalid}}, St) ->
{save_state,delete_invalid_groups(St)};
command({ssels,{delete_group,all}}, St) ->
{save_state,St#st{ssels=gb_trees:empty()}};
command({ssels,{delete_group,Id}}, #st{ssels=Ssels}=St) ->
{save_state,St#st{ssels=gb_trees:delete(Id, Ssels)}};
command(Type, St) ->
set_select_mode(Type, St).
by_command(hard_edges, St) ->
hard_edges(St);
by_command(isolated_vertices, St) ->
{save_state,select_isolated(St)};
by_command(nonplanar_faces, St) ->
nonplanar_faces(true, St);
by_command({nonplanar_faces,Ask}, St) ->
nonplanar_faces(Ask, St);
by_command({vertices_with,N}, St) ->
vertices_with(N, St);
by_command({non_quad,Type}, St) ->
faces_with({non_quad,Type}, St);
by_command({faces_with,N}, St) ->
faces_with({faces_with,N}, St);
by_command(material_edges, St) ->
material_edges(St);
by_command({random, Percent}, St) ->
random(Percent, St);
by_command(short_edges, St) ->
short_edges(true, St);
by_command({short_edges,Ask}, St) ->
short_edges(Ask, St);
by_command(sharp_edges, St) ->
sharp_edges(true, St);
by_command({sharp_edges,Ask}, St) ->
sharp_edges(Ask, St);
by_command({vertex_path,fewest_edges_path}, St) ->
shortest_path(fewest_edges, St);
by_command({vertex_path,dijkstra_shortest_path}, St) ->
shortest_path(dijkstra, St);
by_command({vertex_path,astar_shortest_path}, St) ->
shortest_path(astar, St);
by_command(uv_mapped_faces, St) ->
uv_mapped_faces(St);
by_command(id, St) ->
by_id(St);
by_command({id,Sel}, St) ->
{save_state,sel_by_id(Sel, St)};
by_command(by_name, St) ->
by_name(St);
by_command({by_name_with, Name}, St) ->
{save_state,by_name_with(Name, St)}.
face_region_to_edge_loop(St) ->
wings_sel:update_sel(
fun(Fs, We) ->
Es0 = wings_face:outer_edges(Fs, We),
Es = subtract_mirror_edges(Es0, We),
gb_sets:from_list(Es)
end, edge, St).
subtract_mirror_edges(Es, #we{mirror=none}) -> Es;
subtract_mirror_edges(Es, #we{mirror=Face}=We) ->
Es -- wings_face:to_edges([Face], We).
vs_to_edge_loop(St) ->
wings_sel:update_sel(
fun(Vs, We) ->
Es0 = vs_to_edges(Vs, We, []),
Es = subtract_mirror_edges(Es0, We),
gb_sets:from_list(Es)
end, edge, St).
vs_to_edges(Vs0, We, Es0) ->
case gb_sets:is_empty(Vs0) of
true -> lists:usort(Es0);
false ->
{Va,Vs} = gb_sets:take_smallest(Vs0),
Es = wings_vertex:fold(
fun(Edge, _, EdgeRec, Es1) ->
Vb = wings_vertex:other(Va, EdgeRec),
case gb_sets:is_element(Vb, Vs) of
true -> [Edge|Es1];
_ -> Es1
end
end, Es0, Va, We),
vs_to_edges(Vs, We, Es)
end.
%%%
%%% Selection commands.
%%%
set_select_mode(Type, St) ->
{save_state,wings_sel_conv:mode(Type, St)}.
select_all(#st{selmode=body}=St) ->
wings_sel:new_sel(fun(Sel, _) -> Sel end, body, St);
select_all(#st{selmode=Mode,sel=[]}=St) ->
wings_sel:new_sel(fun(Sel, _) -> Sel end, Mode, St);
select_all(#st{selmode=Mode}=St) ->
wings_sel:update_sel(
fun(_Sel, We) ->
wings_sel:get_all_items(Mode, We)
end, Mode, St).
%%%
%%% Select Inverse.
%%%
inverse(#st{selmode=body}=St) ->
Inverse = wings_sel:unselected_ids(St),
wings_sel:make(fun(_, #we{id=Id}) ->
member(Id, Inverse)
end, body, St);
inverse(#st{selmode=Mode}=St) ->
wings_sel:update_sel(
fun(Items, We) ->
Diff = wings_sel:inverse_items(Mode, Items, We),
case gb_sets:is_empty(Diff) of
true -> Items; %Can't inverse.
false -> Diff
end
end, St).
%%%
%%% Deselect
%%%
deselect(St) ->
case wings_pref:get_value(conditional_deselect) of
true -> wings_sel:conditional_reset(St);
false -> wings_sel:reset(St)
end.
%%%
%%% Hide Selected
%%% Hide Unselected
%%% Lock Unselected
%%%
hide_selected(St) ->
Selected = wings_sel:selected_ids(St),
wings_obj:hide(Selected, St).
hide_unselected(St) ->
Unselected = wings_sel:unselected_ids(St),
wings_obj:hide(Unselected, St).
lock_selected(St) ->
Selected = wings_sel:selected_ids(St),
wings_obj:lock(Selected, St).
lock_unselected(St) ->
Unselected = wings_sel:unselected_ids(St),
wings_obj:lock(Unselected, St).
%%%
%%% Selection Groups
%%%
union_group(Key, #st{sel=Sel0}=St) ->
Ssel = coerce_ssel(Key, St),
Sel = union(Sel0, Ssel),
wings_sel:valid_sel(St#st{sel=Sel}).
union(Sa, Sb) ->
combine_sel(fun(Ss) -> gb_sets:union(Ss) end, Sa, Sb).
subtract_group(Key, #st{sel=Sel0}=St) ->
Ssel = coerce_ssel(Key, St),
Sel = subtract(Sel0, Ssel),
St#st{sel=Sel}.
subtract([{Id1,_}=E1|Es1], [{Id2,_}|_]=Set2) when Id1 < Id2 ->
[E1|subtract(Es1, Set2)];
subtract([{Id1,_}|_]=Set1, [{Id2,_}|Es2]) when Id1 > Id2 ->
subtract(Set1, Es2);
subtract([{Id,E1}|Es1], [{Id,E2}|Es2]) -> %E1 == E2
E = gb_sets:subtract(E1, E2),
case gb_sets:is_empty(E) of
true -> subtract(Es1, Es2);
false -> [{Id,E}|subtract(Es1, Es2)]
end;
subtract([], _Es2) -> [];
subtract(Es1, []) -> Es1.
intersect_group(Key, #st{sel=Sel0}=St) ->
Ssel = coerce_ssel(Key, St),
Sel = intersection(Sel0, Ssel),
St#st{sel=Sel}.
intersection(Sa, Sb) ->
Empty = gb_sets:empty(),
combine_sel(fun([_]) -> Empty;
(Ss) -> gb_sets:intersection(Ss)
end, Sa, Sb).
combine_sel(Combine, Sa, Sb) ->
combine_sel(Combine, lists:merge(Sa, Sb)).
combine_sel(Combine, [{Id,Sa},{Id,Sb}|T]) ->
S = Combine([Sa,Sb]),
case gb_sets:is_empty(S) of
true -> combine_sel(Combine, T);
false -> [{Id,S}|combine_sel(Combine, T)]
end;
combine_sel(Combine, [{Id,S0}|T]) ->
S = Combine([S0]),
case gb_sets:is_empty(S) of
true -> combine_sel(Combine, T);
false -> [{Id,S}|combine_sel(Combine, T)]
end;
combine_sel(_Combine, []) -> [].
coerce_ssel({Mode,_}=Key, #st{ssels=Ssels}=St) ->
case gb_trees:is_defined(Key,Ssels) of
true ->
Ssel = gb_trees:get(Key, Ssels),
coerce_ssel(Mode, Ssel, St);
false ->
[]
end.
coerce_ssel(Mode, Ssel, #st{selmode=Mode}) -> Ssel;
coerce_ssel(Smode, Ssel0, #st{selmode=Mode}=St) ->
StTemp = St#st{selmode=Smode,sel=wings_sel:valid_sel(Ssel0, Smode, St)},
#st{sel=Ssel} = wings_sel_conv:mode(Mode, StTemp),
Ssel.
select_group({Mode,_}=Key, #st{ssels=Ssels}=St) ->
Ssel = gb_trees:get(Key, Ssels),
ValidSel = wings_sel:valid_sel(Ssel, Mode, St),
St#st{selmode=Mode,sel=ValidSel}.
%%%% Delete Groups that return an empty selection. Invalid ssels can result from
%%%% creating or deleting geometry.
delete_invalid_groups(#st{ssels=Ssels}=St0) ->
case gb_trees:is_empty(Ssels) of
true ->
St0;
false ->
Keys = gb_trees:keys(Ssels),
lists:foldl(fun(Key,#st{ssels=Ss,selmode=Mode}=St) ->
Ssel = gb_trees:get(Key,Ss),
ValidSel = wings_sel:valid_sel(Ssel, Mode, St),
case ValidSel of
[] -> St#st{ssels=gb_trees:delete(Key,Ss)};
_ -> St
end
end,St0,Keys)
end.
add_to_group({Mode,_}=Key, #st{ssels=Ssels}=St) ->
Ssel0 = gb_trees:get(Key, Ssels),
Ssel1 = wings_sel:valid_sel(Ssel0, Mode, St),
#st{sel=Sel} = possibly_convert(Mode, St),
Ssel = union(Ssel1, Sel),
save_group(Key, Ssel, St).
replace_group({_,Name}=Key, #st{ssels=Ssels0, selmode=Mode, sel=Sel}=St) ->
case Key of
{Mode,Name} -> % same mode - no need to check for name duplication
Ssels = gb_trees:update(Key, Sel, Ssels0);
_ ->
NewKey =
case gb_trees:is_defined({Mode,Name}, Ssels0) of
true ->
Names = [Name0 || {Mode0,Name0} <- gb_trees:keys(Ssels0), Mode0=:=Mode],
{Mode, wings_util:unique_name(Name, Names)};
false ->
{Mode, Name}
end,
Ssels = gb_trees:insert(NewKey, Sel, gb_trees:delete(Key, Ssels0))
end,
St#st{ssels=Ssels}.
subtract_from_group({Mode,_}=Key, #st{ssels=Ssels}=St) ->
Ssel0 = gb_trees:get(Key, Ssels),
Ssel1 = wings_sel:valid_sel(Ssel0, Mode, St),
#st{sel=Sel} = possibly_convert(Mode, St),
Ssel = subtract(Ssel1, Sel),
save_group(Key, Ssel, St).
possibly_convert(Mode, #st{selmode=Mode}=St) -> St;
possibly_convert(Mode, St) -> wings_sel_conv:mode(Mode, St).
save_group(Key, Sel, #st{ssels=Ssels0}=St) ->
Ssels = gb_trees:update(Key, Sel, Ssels0),
St#st{ssels=Ssels}.
new_group(_) ->
wings_dialog:ask(?__(1,"Create New Group"),
[{?__(2,"Group Name"), "",[{width,22}]}],
fun([String]) -> {select,{new_group_name,String}} end).
new_group_name(Name, #st{ssels=Ssels0,selmode=Mode,sel=Sel}=St) ->
Key = {Mode,Name},
case gb_trees:is_defined(Key, Ssels0) of
false -> ok;
true ->
%% Careful: don't use io_lib:format/2 here. The group name
%% may contain Unicode characters.
GroupMode = group_mode_string(Mode),
Exists = ?__(exists,"already exists."),
Msg0 = [GroupMode," \"",Name,"\" ",Exists],
Msg = lists:flatten(Msg0),
wings_u:error_msg(Msg)
end,
Ssels = gb_trees:insert(Key, Sel, Ssels0),
St#st{ssels=Ssels}.
group_mode_string(vertex) ->
?__(vertex, "Vertex selection group");
group_mode_string(edge) ->
?__(edge, "Edge selection group");
group_mode_string(face) ->
?__(face, "Face selection group");
group_mode_string(body) ->
?__(body, "Body selection group").
%%%% Cycle Through Save Selections
cycle_group(Dir, #st{selmode=SelMode,ssels=Ssels,sh=Sh}=St) ->
case gb_trees:is_empty(Ssels) of
true -> St;
false ->
Keys0 = gb_trees:keys(Ssels),
Keys1 = case wings_pref:get_value(saved_selections_cycle_by_mode) of
true when Sh -> Keys0;
true -> [Key || {Mode,_}=Key <- Keys0, Mode =:= SelMode];
false -> Keys0
end,
Keys = case Dir of
next_group -> Keys1;
prev_group -> lists:reverse(Keys1)
end,
cycle_ss_keys(Keys,St)
end.
cycle_ss_keys([],St) -> St;
cycle_ss_keys(Keys,St) ->
case search_ssel_keys(Keys,St,[]) of
{none,[]} -> [Key|_] = Keys;
{none,Acc} ->
Key = lists:last(Acc);
[] ->
[Key|_] = Keys;
Other ->
[Key|_] = Other
end,
case select_group(Key,St) of
#st{sel=[]} -> cycle_ss_keys(lists:delete(Key,Keys),St);
NewSt -> NewSt
end.
search_ssel_keys([{Mode,_}=PKey|Keys],#st{selmode=Mode,sel=Sel,ssels=Ssels}=St,Acc) ->
PSel0 = gb_trees:get(PKey,Ssels),
PSel = wings_sel:valid_sel(PSel0, Mode, St),
case PSel =:= Sel of
true ->
Keys;
false ->
search_ssel_keys(Keys,St,[PKey|Acc])
end;
search_ssel_keys([_|Keys],St,Acc) ->
search_ssel_keys(Keys,St,Acc);
search_ssel_keys([],_St,Acc) ->
{none,Acc}.
%%%
%%% Select Similar.
%%%
similar(#st{selmode=vertex}=St) ->
do_similar(fun make_vertex_template/2, St);
similar(#st{selmode=edge}=St) ->
do_similar(fun make_edge_template/2, St);
similar(#st{selmode=face}=St) ->
do_similar(fun make_face_template/2, St);
similar(#st{selmode=body}=St) ->
MF = fun(_, #we{vp=Vtab,es=Etab,fs=Ftab}) ->
{wings_util:array_entries(Vtab),
wings_util:array_entries(Etab),
gb_trees:size(Ftab)}
end,
RF = fun(T, A) -> [T|A] end,
Templates0 = wings_sel:dfold(MF, RF, [], St),
Templates = usort(Templates0),
Zero = gb_sets:singleton(0),
wings_sel:make(fun(_, We) ->
Template = MF(Zero, We),
member(Template, Templates)
end, body, St).
do_similar(MakeTemplate, #st{selmode=Mode,sel=Sel0}=St) when Sel0 =/= [] ->
MF = fun(Sel, We) ->
Ts = gb_sets:fold(
fun(I, A) ->
[MakeTemplate(I, We)|A]
end, [], Sel),
usort(Ts)
end,
RF = fun lists:umerge/2,
Templates0 = wings_sel:dfold(MF, RF, [], St),
Templates = consolidate_templates(Templates0),
wings_sel:make(
fun(Item, We) ->
Template = MakeTemplate(Item, We),
match_templates(Template, Templates)
end, Mode, St);
do_similar(_, St) -> St.
consolidate_templates([H|T]) ->
consolidate_templates_1(H, T).
consolidate_templates_1(Templ0, [Templ|T]) ->
case match_template(Templ0, Templ) of
true ->
consolidate_templates_1(Templ0, T);
false->
[Templ0|consolidate_templates_1(Templ, T)]
end;
consolidate_templates_1(Templ, []) -> [Templ].
match_templates(T, Templates) ->
any(fun(Template) ->
match_template(T, Template)
end, Templates).
match_template({Len,Ad,As}, {Len,Bd,Bs}) ->
compare(Ad, Bd) andalso compare(As, Bs);
match_template(_, _) -> false.
make_face_template(Face, We) ->
VsPos = wings_face:vertex_positions(Face, We),
{DotSum,SqSum} = face_dots_and_sqlens(VsPos),
{length(VsPos),DotSum,SqSum}.
face_dots_and_sqlens([Va,Vb|_]=Vpos) ->
D = e3d_vec:sub(Va, Vb),
face_dots_and_sqlens_2(D, Vpos, Vpos, 0, 0).
face_dots_and_sqlens_2(D1, [_|[Vb,Vc|_]=Vs], More, Dot0, Sq0) ->
D2 = e3d_vec:sub(Vb, Vc),
Dot = Dot0 + e3d_vec:dot(D1, D2),
Sq = Sq0 + e3d_vec:dot(D1, D1),
face_dots_and_sqlens_2(D2, Vs, More, Dot, Sq);
face_dots_and_sqlens_2(D1, Vs, [Va,Vb|_], Dot, Sq) ->
face_dots_and_sqlens_2(D1, Vs++[Va,Vb], [], Dot, Sq);
face_dots_and_sqlens_2(_D1, _Other, _More, Dot, Sq) -> {Dot,Sq}.
make_edge_template(Edge, #we{vp=Vtab,es=Etab}=We) ->
#edge{vs=Va,ve=Vb,ltpr=LP,ltsu=LS,rtpr=RP,rtsu=RS} =
array:get(Edge, Etab),
VaPos = array:get(Va, Vtab),
VbPos = array:get(Vb, Vtab),
Vec = e3d_vec:sub(VaPos, VbPos),
DotSum = edge_dot(LP, Vb, VbPos, Vec, We) +
edge_dot(RS, Vb, VbPos, Vec, We) +
edge_dot(LS, Va, VaPos, Vec, We) +
edge_dot(RP, Va, VaPos, Vec, We),
{0,DotSum,e3d_vec:dot(Vec, Vec)}.
edge_dot(Edge, V, Pos, Vec, #we{es=Etab}=We) ->
Rec = array:get(Edge, Etab),
OtherPos = wings_vertex:other_pos(V, Rec, We),
ThisVec = e3d_vec:sub(Pos, OtherPos),
abs(e3d_vec:dot(ThisVec, Vec)).
make_vertex_template(V, #we{vp=Vtab}=We) ->
Center = array:get(V, Vtab),
Vecs = wings_vertex:fold(
fun(_, _, Rec, Acc0) ->
Pos = wings_vertex:other_pos(V, Rec, Vtab),
Vec = e3d_vec:sub(Pos, Center),
[Vec|Acc0]
end, [], V, We),
{DotSum,SqSum} = vertex_dots_and_sqlens(Vecs, Vecs, 0, 0),
{length(Vecs),DotSum,SqSum}.
vertex_dots_and_sqlens([VecA|[VecB|_]=T], More, Dot0, Sq0) ->
Dot = Dot0 + abs(e3d_vec:dot(VecA, VecB)),
Sq = Sq0 + e3d_vec:dot(VecA, VecA),
vertex_dots_and_sqlens(T, More, Dot, Sq);
vertex_dots_and_sqlens(Vecs, [VecB|_], Dot, Sq) ->
vertex_dots_and_sqlens(Vecs++[VecB], [], Dot, Sq);
vertex_dots_and_sqlens(_Other, _More, Dot, Sq) -> {Dot,Sq}.
-define(TOLERANCE, 1.0E-5).
compare(A, B) ->
%% Comparison with a relative tolerance for large
%% values and an absolute tolerance for small values
%% as described by Christer Ericson in
%% http://realtimecollisiondetection.net/blog/?p=89
%% and in his book "Real-Time Collision Detection".
%%
abs(A-B) =< ?TOLERANCE*max(1.0, max(abs(A), abs(B))).
%%
%% Select Random.
%%
random(true, St) ->
Qs0 = [{hframe,[{slider,{text,float(wings_pref:get_value(random_select, 25.0)),
[{range,{0.0,100.0}}]}}]}],
Qs = [{vframe,Qs0}],
Title = ?__(1,"Select Random"),
Cmd = {select,by,random},
wings_dialog:dialog_preview(Cmd, true, Title, Qs, St);
random([Percent], St) ->
random(Percent, St);
random(Percent, #st{selmode=Mode}=St0) ->
P = Percent / 100,
wings_pref:set_value(random_select, Percent),
St = intersect_sel_items(fun(_, _) -> rand:uniform() < P end, Mode, St0),
{save_state,St}.
%%
%% Select short edges.
%%
short_edges(Ask, St) when is_atom(Ask) ->
Qs0 = [{label,?__(1,"Length tolerance")},
{text,1.0E-3,[{range,{1.0E-5,10.0}}]}],
Qs = [{hframe,Qs0}],
Title = ?__(2,"Select Short Edges"),
Cmd = {select,by,short_edges},
wings_dialog:dialog_preview(Cmd, Ask, Title, Qs, St);
short_edges([Tolerance], St0) ->
St = intersect_sel_items(fun(Edge, We) ->
is_short_edge(Tolerance, Edge, We)
end, edge, St0),
{save_state,St}.
is_short_edge(Tolerance, Edge, #we{es=Etab,vp=Vtab}) ->
#edge{vs=Va,ve=Vb} = array:get(Edge, Etab),
VaPos = array:get(Va, Vtab),
VbPos = array:get(Vb, Vtab),
e3d_vec:dist(VaPos, VbPos) < Tolerance.
%%
%% Select all edges between materials.
%%
material_edges(St) ->
intersect_sel_items(
fun(Edge, #we{es=Etab}=We) ->
#edge{lf=Lf,rf=Rf} = array:get(Edge, Etab),
wings_facemat:face(Lf, We) =/= wings_facemat:face(Rf, We)
end, edge, St).
%%
%% Select all faces that have (proper) UV coordinates.
%%
uv_mapped_faces(St0) ->
St1 = possibly_convert(face, St0),
St = intersect_sel(
fun(We) ->
UVF = wings_we:uv_mapped_faces(We),
gb_sets:from_ordset(UVF)
end, face, St1),
{save_state,St}.
%%
%% Select by numerical item id.
%%
by_id(#st{selmode=body}=St) ->
ask([{"Object Id",1}],
fun([Id]) ->
{"",[{Id,gb_sets:singleton(0)}]}
end, St);
by_id(#st{selmode=vertex}=St) ->
item_by_id("Vertex Id", St);
by_id(#st{selmode=edge}=St) ->
item_by_id("Edge Id", St);
by_id(#st{selmode=face}=St) ->
item_by_id("Face Id", St).
item_by_id(Prompt, #st{sel=[_]}=St) ->
MF = fun(_, #we{id=Id}) -> Id end,
RF = fun(I, []) -> I end,
Id = wings_sel:dfold(MF, RF, [], St),
ask([{Prompt,0}],
fun([Item]) ->
{Prompt,[{Id,gb_sets:singleton(Item)}]}
end, St);
item_by_id(Prompt, St) ->
FF = fun(#{id:=Id}, A) -> [Id|A] end,
case wings_obj:fold(FF, [], St) of
[] ->
wings_u:error_msg(?__(1,"Nothing to select."));
[Id] ->
ask([{Prompt,0}],
fun([Item]) ->
{Prompt,[{Id,gb_sets:singleton(Item)}]}
end, St);
[Id0|_] ->
ask([{?__(2,"Object Id"),Id0},
{Prompt,0}],
fun([Id,Item]) ->
{Prompt,[{Id,gb_sets:singleton(Item)}]}
end, St)
end.
by_name(St) ->
case wings_obj:num_objects(St) of
0 ->
wings_u:error_msg(?__(1,"Nothing to select."));
_ ->
wings_dialog:ask(?__(2,"Select by name"),
[{?__(3,"Name"), ""}],
fun([String]) ->
{select,{by,{by_name_with,String}}}
end)
end.
by_name_with(Filter, #st{selmode=Mode}=St0) ->
FF = fun(#{id:=Id,name:=Name}, A) ->
case wings_util:is_name_masked(Name, Filter) of
true -> [Id|A];
false -> A
end
end,
Ids = gb_sets:from_list(wings_obj:fold(FF, [], St0)),
SF = fun(_, #we{id=Id}) -> gb_sets:is_member(Id, Ids) end,
St = wings_sel:make(SF, body, St0),
wings_sel_conv:mode(Mode, St).
valid_sel(Prompt, Sel, #st{selmode=Mode}=St) ->
case wings_sel:valid_sel(Sel, Mode, St) of
[] ->
[{Id,Item0}] = Sel,
[Item] = gb_sets:to_list(Item0),
FF = fun(#{id:=ObjId}, A) -> (Id =:= ObjId) or A end,
case wings_obj:fold(FF, false, St) of
false ->
wings_u:error_msg(?__(1,"The Object Id ")++
integer_to_list(Id)++
?__(2," is invalid."));
true ->
wings_u:error_msg(?__(3,"The ")++Prompt++" "++
integer_to_list(Item)++
?__(4," is invalid."))
end;
Sel -> Sel
end.
ask(Qs, Fun, St) ->
wings_dialog:ask(?__(1,"Select By Id"), {preview,Qs},
fun
({dialog_preview,Res}) ->
try
Sel = Fun(Res),
{preview,St,sel_by_id(Sel, St)}
catch throw:_ -> {preview,St,St}
end;
(cancel) -> St;
(Res) ->
Sel = Fun(Res),
{commit,St,sel_by_id(Sel, St)}
end).
sel_by_id({Prompt,Sel}, St) ->
wings_sel:set(valid_sel(Prompt, Sel, St), St).
%%%
%%% Select lights.
%%%
select_lights(#st{selmode=Mode}=St0) ->
SF = fun(_, We) when ?IS_LIGHT(We) ->
gb_sets:singleton(0);
(_, _) ->
gb_sets:empty()
end,
St = wings_sel:new_sel(SF, body, St0),
wings_sel_conv:mode(Mode, St).
%%%
%%% Select isolated vertices.
%%%
select_isolated(St) ->
intersect_sel(
fun(We) ->
gb_sets:from_list(wings_vertex:isolated(We))
end, vertex, St).