Spaces:
Runtime error
Runtime error
Upload selfcontact.diff
Browse files- patches/selfcontact.diff +159 -0
patches/selfcontact.diff
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
+++ venv/lib/python3.10/site-packages/selfcontact/body_segmentation.py
|
2 |
+
@@ -14,6 +14,8 @@
|
3 |
+
#
|
4 |
+
# Contact: [email protected]
|
5 |
+
|
6 |
+
+from pathlib import Path
|
7 |
+
+
|
8 |
+
import torch
|
9 |
+
import trimesh
|
10 |
+
import torch.nn as nn
|
11 |
+
@@ -22,6 +24,17 @@
|
12 |
+
|
13 |
+
from .utils.mesh import winding_numbers
|
14 |
+
|
15 |
+
+
|
16 |
+
+def load_pkl(path):
|
17 |
+
+ with open(path, "rb") as fin:
|
18 |
+
+ return pickle.load(fin)
|
19 |
+
+
|
20 |
+
+
|
21 |
+
+def save_pkl(obj, path):
|
22 |
+
+ with open(path, "wb") as fout:
|
23 |
+
+ pickle.dump(obj, fout)
|
24 |
+
+
|
25 |
+
+
|
26 |
+
class BodySegment(nn.Module):
|
27 |
+
def __init__(self,
|
28 |
+
name,
|
29 |
+
@@ -63,9 +76,17 @@
|
30 |
+
self.register_buffer('segment_faces', segment_faces)
|
31 |
+
|
32 |
+
# create vector to select vertices form faces
|
33 |
+
- tri_vidx = []
|
34 |
+
- for ii in range(faces.max().item()+1):
|
35 |
+
- tri_vidx += [torch.nonzero(faces==ii)[0].tolist()]
|
36 |
+
+ segments_folder = Path(segments_folder)
|
37 |
+
+ tri_vidx_path = segments_folder / "tri_vidx.pkl"
|
38 |
+
+ if not tri_vidx_path.is_file():
|
39 |
+
+ tri_vidx = []
|
40 |
+
+ for ii in range(faces.max().item()+1):
|
41 |
+
+ tri_vidx += [torch.nonzero(faces==ii)[0].tolist()]
|
42 |
+
+
|
43 |
+
+ save_pkl(tri_vidx, tri_vidx_path)
|
44 |
+
+ else:
|
45 |
+
+ tri_vidx = load_pkl(tri_vidx_path)
|
46 |
+
+
|
47 |
+
self.register_buffer('tri_vidx', torch.tensor(tri_vidx))
|
48 |
+
|
49 |
+
def create_band_faces(self):
|
50 |
+
@@ -149,7 +170,7 @@
|
51 |
+
self.segmentation = {}
|
52 |
+
for idx, name in enumerate(names):
|
53 |
+
self.segmentation[name] = BodySegment(name, faces, segments_folder,
|
54 |
+
- model_type).to('cuda')
|
55 |
+
+ model_type).to(device)
|
56 |
+
|
57 |
+
def batch_has_self_isec_verts(self, vertices):
|
58 |
+
"""
|
59 |
+
+++ venv/lib/python3.10/site-packages/selfcontact/selfcontact.py
|
60 |
+
@@ -41,6 +41,7 @@
|
61 |
+
test_segments=True,
|
62 |
+
compute_hd=False,
|
63 |
+
buffer_geodists=False,
|
64 |
+
+ device="cuda",
|
65 |
+
):
|
66 |
+
super().__init__()
|
67 |
+
|
68 |
+
@@ -95,7 +96,7 @@
|
69 |
+
if self.test_segments:
|
70 |
+
sxseg = pickle.load(open(segments_bounds_path, 'rb'))
|
71 |
+
self.segments = BatchBodySegment(
|
72 |
+
- [x for x in sxseg.keys()], faces, segments_folder, self.model_type
|
73 |
+
+ [x for x in sxseg.keys()], faces, segments_folder, self.model_type, device=device,
|
74 |
+
)
|
75 |
+
|
76 |
+
# load regressor to get high density mesh
|
77 |
+
@@ -106,7 +107,7 @@
|
78 |
+
torch.tensor(hd_operator['values']),
|
79 |
+
torch.Size(hd_operator['size']))
|
80 |
+
self.register_buffer('hd_operator',
|
81 |
+
- torch.tensor(hd_operator).float())
|
82 |
+
+ hd_operator.clone().detach().float())
|
83 |
+
|
84 |
+
with open(point_vert_corres_path, 'rb') as f:
|
85 |
+
hd_geovec = pickle.load(f)['faces_vert_is_sampled_from']
|
86 |
+
@@ -135,9 +136,13 @@
|
87 |
+
# split because of memory into two chunks
|
88 |
+
exterior = torch.zeros((bs, nv), device=vertices.device,
|
89 |
+
dtype=torch.bool)
|
90 |
+
- exterior[:, :5000] = winding_numbers(vertices[:,:5000,:],
|
91 |
+
+ exterior[:, :3000] = winding_numbers(vertices[:,:3000,:],
|
92 |
+
triangles).le(0.99)
|
93 |
+
- exterior[:, 5000:] = winding_numbers(vertices[:,5000:,:],
|
94 |
+
+ exterior[:, 3000:6000] = winding_numbers(vertices[:,3000:6000,:],
|
95 |
+
+ triangles).le(0.99)
|
96 |
+
+ exterior[:, 6000:9000] = winding_numbers(vertices[:,6000:9000,:],
|
97 |
+
+ triangles).le(0.99)
|
98 |
+
+ exterior[:, 9000:] = winding_numbers(vertices[:,9000:,:],
|
99 |
+
triangles).le(0.99)
|
100 |
+
|
101 |
+
# check if intersections happen within segments
|
102 |
+
@@ -173,9 +178,13 @@
|
103 |
+
# split because of memory into two chunks
|
104 |
+
exterior = torch.zeros((bs, np), device=points.device,
|
105 |
+
dtype=torch.bool)
|
106 |
+
- exterior[:, :6000] = winding_numbers(points[:,:6000,:],
|
107 |
+
+ exterior[:, :3000] = winding_numbers(points[:,:3000,:],
|
108 |
+
+ triangles).le(0.99)
|
109 |
+
+ exterior[:, 3000:6000] = winding_numbers(points[:,3000:6000,:],
|
110 |
+
triangles).le(0.99)
|
111 |
+
- exterior[:, 6000:] = winding_numbers(points[:,6000:,:],
|
112 |
+
+ exterior[:, 6000:9000] = winding_numbers(points[:,6000:9000,:],
|
113 |
+
+ triangles).le(0.99)
|
114 |
+
+ exterior[:, 9000:] = winding_numbers(points[:,9000:,:],
|
115 |
+
triangles).le(0.99)
|
116 |
+
|
117 |
+
return exterior
|
118 |
+
@@ -371,6 +380,23 @@
|
119 |
+
|
120 |
+
return hd_v2v_mins, hd_exteriors, hd_points, hd_faces_in_contacts
|
121 |
+
|
122 |
+
+ def verts_in_contact(self, vertices, return_idx=False):
|
123 |
+
+
|
124 |
+
+ # get pairwise distances of vertices
|
125 |
+
+ v2v = self.get_pairwise_dists(vertices, vertices, squared=True)
|
126 |
+
+
|
127 |
+
+ # mask v2v with eucledean and geodesic dsitance
|
128 |
+
+ euclmask = v2v < self.euclthres**2
|
129 |
+
+ mask = euclmask * self.geomask
|
130 |
+
+
|
131 |
+
+ # find closes vertex in contact
|
132 |
+
+ in_contact = mask.sum(1) > 0
|
133 |
+
+
|
134 |
+
+ if return_idx:
|
135 |
+
+ in_contact = torch.where(in_contact)
|
136 |
+
+
|
137 |
+
+ return in_contact
|
138 |
+
+
|
139 |
+
|
140 |
+
|
141 |
+
class SelfContactSmall(nn.Module):
|
142 |
+
+++ venv/lib/python3.10/site-packages/selfcontact/utils/mesh.py
|
143 |
+
@@ -82,7 +82,7 @@
|
144 |
+
if valid_vals > 0:
|
145 |
+
loss = (mask * dists).sum() / valid_vals
|
146 |
+
else:
|
147 |
+
- loss = torch.Tensor([0]).cuda()
|
148 |
+
+ loss = mask.new_tensor([0])
|
149 |
+
return loss
|
150 |
+
|
151 |
+
def batch_index_select(inp, dim, index):
|
152 |
+
@@ -103,6 +103,7 @@
|
153 |
+
xx = torch.bmm(x, x.transpose(2, 1))
|
154 |
+
yy = torch.bmm(y, y.transpose(2, 1))
|
155 |
+
zz = torch.bmm(x, y.transpose(2, 1))
|
156 |
+
+ use_cuda = x.device.type == "cuda"
|
157 |
+
if use_cuda:
|
158 |
+
dtype = torch.cuda.LongTensor
|
159 |
+
else:
|