Something like this?
EDIT: (untested of course)...
EDIT: (untested of course)...
Code:
command({vertex,{manifoldlab,connect_surface_norm}},#st{sel=[{WeID,Set}]}=St0) ->
We = gb_trees:get(WeID,St0#st.shapes),
case gb_sets:to_list(Set) of
[Vs,Ve] ->
connect_surface(Vs, Ve, We, St0);
_ ->
wings_util:error("only two verts can be selected..") %% FIXME
end.
connect_surface(VS0, VE0, #wx{id=WeID}=We, St0) ->
{Mid, A_B_C_D, Plane1, Plane2} = calc_planes(VS0, VE0, We),
ETree = collect_edges(A_B_C_D, We),
Es = gb_sets:from_list(gb_trees:keys(ETree)),
FaceSet2 = collect_faces(Mid, Plane1, Plane2, Es, We, St0),
{We2,Vs} = make_cuts(ETree, We, limit_edges(VS0, VE0, FaceSet2, Es, We)),
St4 = wings_shape:replace(WeID,We2,St0#st{selmode=vertex,sel=[{WeID,Vs}]}),
{save_state,#st{}=St5} = wings_vertex_cmd:command(connect,St4),
#we{} = We5 = gb_trees:get(WeID,St5#st.shapes),
MyBetweens = fun(Ei,#edge{vs=_VS,ve=_VE}, Acc) ->
Two = gb_sets:from_list([_VS,_VE]),
_I = gb_sets:intersection(Two,Vs),
case gb_sets:size(_I) /= 2 of
true -> Acc;
false -> gb_sets:add(Ei,Acc)
end
end,
EsConn = array:sparse_foldl(MyBetweens, gb_sets:empty(), We5#we.es),
%%{_,StCleaned} = wings_body:command({cleanup, [{short_edges,true},1.0E-5,{isolated_vs,true}]}, St7),
St5#st{selmode=edge,sel=[{WeID,EsConn}]}.
calc_planes(VS0, VE0, We) ->
Pt1a = wings_vertex:pos(VS0,We),
Pt2a = wings_vertex:pos(VE0,We),
V1 = e3d_vec:sub(Pt2a,Pt1a),
Mid0 = e3d_vec:average([Pt1a,Pt2a]),
Norm0 = e3d_vec:average([wings_vertex:normal(VS0,We), wings_vertex:normal(VE0,We)]),
Cross = e3d_vec:norm(e3d_vec:cross(V1,Norm0)),
A_B_C_D = e3d_vec:plane(Mid0,Cross), %% slicer plane
Plane1 = e3d_vec:plane(Pt1a,e3d_vec:norm(V1)),
Plane2 = e3d_vec:plane(Pt2a,e3d_vec:norm(V1)),
{Mid0, A_B_C_D, Plane1, Plane2}.
collect_edges(Plane, #we{es=Etab}=We) ->
MyAcc = fun(Ei, _Val, Acc) ->
#edge{vs=VS,ve=VE} = array:get(Ei,Etab),
Pt1 = wings_vertex:pos(VS,We),
Pt2 = wings_vertex:pos(VE,We),
S1 = e3d_vec:plane_side(Pt1, Plane),
S2 = e3d_vec:plane_side(Pt2, Plane),
if (S1 /= S2) ->
D1 = abs(e3d_vec:plane_dist(Pt1, Plane)),
D2 = abs(e3d_vec:plane_dist(Pt2, Plane)),
Dir = e3d_vec:norm(e3d_vec:sub(Pt2,Pt1)),
Percent = D1/(D1+D2),
PtX = e3d_vec:add(Pt1, e3d_vec:mul(Dir, Percent*wings_edge:length(Ei,We))),
gb_trees:enter(Ei,PtX,Acc);
true ->
Acc
end
end,
array:sparse_foldl(MyAcc, gb_trees:empty(), Etab).
collect_faces(Mid, Plane1, Plane2, Es, #we{id=Id} = We, St0) ->
St1 = St0#st{selmode=edge,sel=[{Id,Es}]},
#st{sel=[{Id,FaceSet}]} = wings_sel_conv:mode(face,St1),
MyFs = fun(Fi, Acc) ->
Center = wings_face:center(Fi, We),
S1a = e3d_vec:plane_side(Mid, Plane1),
S2a = e3d_vec:plane_side(Center, Plane1),
S1b = e3d_vec:plane_side(Mid, Plane2),
S2b = e3d_vec:plane_side(Center, Plane2),
if (S1a ==S2a) andalso (S1b ==S2b) ->
gb_sets:add(Fi,Acc);
true -> Acc end
end,
gb_sets:fold(MyFs, gb_sets:empty(), FaceSet).
limit_edges(VS0, VE0, FaceSet2, Es, #{} = We) ->
%% if the initial verts are on in the face region ... remove edges.
FaceRgns = wings_sel:strict_face_regions(FaceSet2, We),
EsIa = wings_edge:from_faces(FaceSet2, We),
EsIb = gb_sets:intersection(EsIa,Es),
ConnectsOnly = fun(Region, Acc) ->
%% if the initial verts are on in the face region ... remove edges.
_Vs = wings_face:to_vertices(Region,We),
_Es = wings_edge:from_faces(Region,We),
I = gb_sets:intersection(gb_sets:from_list(_Vs),
gb_sets:from_list([VS0,VE0])),
case gb_sets:is_empty(I) of
true -> gb_sets:subtract(Acc,_Es);
false -> Acc
end
end,
lists:foldl(ConnectsOnly, EsIb, FaceRgns).
make_cuts(ETree, We, EsI) ->
MyCuts = fun(Ei, {#we{}=WeAcc,AccVs}) ->
Pos = gb_trees:get(Ei,ETree),
{We1,Idx} = wings_edge:screaming_cut(Ei, Pos, WeAcc),
{We1,gb_sets:add(Idx,AccVs)}
end,
gb_sets:fold(MyCuts, {We,gb_sets:empty()}, EsI).