Files
animation/characters/work/lena/06c_gusset_membrane.py
T

120 lines
3.5 KiB
Python
Raw Normal View History

# Stage 6c: bi-harmonic membrane heal of the briefs-gusset residue between the legs.
#
# History: v1 donor snap (06_crotch.py) fixed the pubic front but its 2 cm SNAP_MAX left the
# gusset panel torn (the male crotch sits ~3 cm below the female bridge). Melting the panel
# (06b) smoothed the interior but not the torn silhouette; widening the snap window (v2)
# shredded the inner-thigh walls. This does what worked on the chest bow: excise the residue
# box and solve a rim-anchored bi-harmonic membrane to convergence — no donor reach limits,
# no orientation risk, C1-continuous with the surrounding skin by construction.
#
# blender --background --python 06c_gusset_membrane.py -- <06_crotched.blend> <out.blend>
import bpy, sys, time
from collections import deque
import numpy as np
argv = sys.argv[sys.argv.index("--") + 1:]
BLEND, OUT = argv[0], argv[1]
t0 = time.time()
# residue box: the gusset bridge underside + torn seam + gathered inner-thigh cloth edges.
# (bridge underside starts at z~0.41; tear at ~0.44; gathers reach ~0.46 and |x|~0.05)
BOX_X = 0.055
BOX_Z0, BOX_Z1 = 0.385, 0.462
def log(m):
print(f"[gusset {time.time()-t0:6.1f}s] {m}", flush=True)
bpy.ops.wm.open_mainfile(filepath=BLEND)
ob = max([o for o in bpy.data.objects if o.type == 'MESH'],
key=lambda o: len(o.data.vertices))
me = ob.data
n_v = len(me.vertices)
co = np.empty(n_v * 3)
me.vertices.foreach_get("co", co)
co = co.reshape(-1, 3)
n_e = len(me.edges)
ev0 = np.empty(n_e * 2, dtype=np.int32)
me.edges.foreach_get("vertices", ev0)
ev0 = ev0.reshape(-1, 2)
def grow_edges(mask, rings, ev):
m = mask.copy()
for _ in range(rings):
hit = m[ev[:, 0]] | m[ev[:, 1]]
m2 = m.copy()
m2[ev[:, 0]] |= hit
m2[ev[:, 1]] |= hit
m = m2
return m
box = (np.abs(co[:, 0]) < BOX_X) & (co[:, 2] > BOX_Z0) & (co[:, 2] < BOX_Z1)
free_m = grow_edges(box, 2, ev0)
collar = grow_edges(free_m, 2, ev0) & ~free_m
S = np.nonzero(free_m | collar)[0]
in_S = np.zeros(n_v, dtype=bool)
in_S[S] = True
glb = np.full(n_v, -1, dtype=np.int64)
glb[S] = np.arange(len(S))
se = ev0[in_S[ev0].all(axis=1)]
a_ = glb[se[:, 0]]
b_ = glb[se[:, 1]]
deg = np.zeros(len(S))
np.add.at(deg, a_, 1.0)
np.add.at(deg, b_, 1.0)
free = free_m[S]
log(f"box {box.sum()} verts -> free {free.sum()}, collar {(~free).sum()}")
def Ls(X):
out = deg[:, None] * X
np.add.at(out, a_, -X[b_])
np.add.at(out, b_, -X[a_])
return out
def A_op(U):
X = np.zeros((len(S), 3))
X[free] = U
return Ls(Ls(X))[free]
Xc = np.zeros((len(S), 3))
Xc[~free] = co[S[~free]]
rhs = -Ls(Ls(Xc))[free]
U = co[S[free]].copy()
r = rhs - A_op(U)
p = r.copy()
rs = (r * r).sum()
rs0 = rs
for it in range(120000):
Ap = A_op(p)
al = rs / max((p * Ap).sum(), 1e-30)
U += al * p
r -= al * Ap
rs2 = (r * r).sum()
if rs2 < 1e-18 or rs2 < rs0 * 1e-14:
break
p = r + (rs2 / rs) * p
rs = rs2
co_new = co.copy()
co_new[S[free]] = U
delta = np.linalg.norm(co_new - co, axis=1)
log(f"membrane: {free.sum()} verts healed (CG {it} iters, "
f"rel residual {rs2/max(rs0,1e-30):.2e}), max move {delta.max():.4f}")
me.vertices.foreach_set("co", co_new.reshape(-1))
me.update()
if me.has_custom_normals:
vn = np.empty(n_v * 3, dtype=np.float32)
me.vertices.foreach_get("normal", vn)
me.normals_split_custom_set_from_vertices(vn.reshape(-1, 3))
bpy.context.preferences.filepaths.save_version = 0 # no .blend1 autosave
bpy.ops.wm.save_as_mainfile(filepath=OUT)
log(f"WROTE {OUT}")
print("GUSSET_DONE")