4
%% This module contains most edge command and edge utility functions.
7
-module(wings_edge_cmd).
11
-include("wings.hrl").
14
%%% The Loop Cut command.
18
{Sel,St} = wings_sel:fold(fun loop_cut/3, {[],St0}, St0),
19
wings_sel:set(body, Sel, St).
21
loop_cut(Edges, #we{name=Name,id=Id,fs=Ftab}=We0, {Sel,St0}) ->
22
AdjFaces = wings_face:from_edges(Edges, We0),
23
case loop_cut_partition(AdjFaces, Edges, We0, []) of
25
io:format("Edge loop doesn't divide ~p into two parts.", [Name]);
27
%% We arbitrarily decide that the largest part of the object
28
%% will be left unselected and will keep the name of the object.
30
Parts1 = [{gb_trees:size(P),P} || P <- Parts0],
31
Parts2 = lists:reverse(lists:sort(Parts1)),
32
[_|Parts] = [gb_sets:to_list(P) || {_,P} <- Parts2],
34
%% Also, this first part will also contain any sub-object
35
%% that was not reachable from any of the edges. Therefore,
36
%% we calculate the first part as the complement of the union
37
%% of all other parts.
39
FirstComplement = ordsets:union(Parts),
40
First = ordsets:subtract(gb_trees:keys(Ftab), FirstComplement),
42
We = wings_dissolve:complement(First, We0),
44
St = St0#st{shapes=gb_trees:update(Id, We, Shs)},
45
loop_cut_make_copies(Parts, We0, Sel, St)
48
loop_cut_make_copies([P|Parts], We0, Sel0, #st{onext=Id}=St0) ->
49
Sel = [{Id,gb_sets:singleton(0)}|Sel0],
50
We = wings_dissolve:complement(P, We0),
51
St = wings_shape:insert(We, cut, St0),
52
loop_cut_make_copies(Parts, We0, Sel, St);
53
loop_cut_make_copies([], _, Sel, St) -> {Sel,St}.
55
loop_cut_partition(Faces0, Edges, We, Acc) ->
56
case gb_sets:is_empty(Faces0) of
59
{AFace,Faces1} = gb_sets:take_smallest(Faces0),
60
Reachable = collect_faces(AFace, Edges, We),
61
Faces = gb_sets:difference(Faces1, Reachable),
62
loop_cut_partition(Faces, Edges, We, [Reachable|Acc])
65
collect_faces(Face, Edges, We) ->
66
collect_faces(gb_sets:singleton(Face), We, Edges, gb_sets:empty()).
68
collect_faces(Work0, We, Edges, Acc0) ->
69
case gb_sets:is_empty(Work0) of
72
{Face,Work1} = gb_sets:take_smallest(Work0),
73
Acc = gb_sets:insert(Face, Acc0),
74
Work = collect_maybe_add(Work1, Face, Edges, We, Acc),
75
collect_faces(Work, We, Edges, Acc)
78
collect_maybe_add(Work, Face, Edges, We, Res) ->
80
fun(_, Edge, Rec, A) ->
81
case gb_sets:is_member(Edge, Edges) of
84
Of = wings_face:other(Face, Rec),
85
case gb_sets:is_member(Of, Res) of
87
false -> gb_sets:add(Of, A)