MCPcopy Create free account
hub / github.com/QWTforGithub/T2LDM / estimate_surface_normal

Function estimate_surface_normal

utils/render.py:135–224  ·  view source on GitHub ↗
(points, d=2, mode="closest")

Source from the content-addressed store, hash-verified

133
134
135def estimate_surface_normal(points, d=2, mode="closest"):
136 # estimate surface normal from coordinated point clouds
137 # re-implemented the following codes with pytorch:
138 # https://github.com/wkentaro/morefusion/blob/master/morefusion/geometry/estimate_pointcloud_normals.py
139 # https://github.com/jmccormac/pySceneNetRGBD/blob/master/calculate_surface_normals.py
140
141 assert points.dim() == 4, f"expected (B,3,H,W), but got {points.shape}"
142 B, C, H, W = points.shape
143 assert C == 3, f"expected C==3, but got {C}"
144 device = points.device
145
146 # points = F.pad(points, (0, 0, d, d), mode="constant", value=float("inf"))
147 points = F.pad(points, (0, 0, d, d), mode="replicate")
148 points = F.pad(points, (d, d, 0, 0), mode="circular")
149 points = points.permute(0, 2, 3, 1) # (B,H,W,3)
150
151 # 8 adjacent offsets
152 # -----------
153 # | 7 | 6 | 5 |
154 # -----------
155 # | 0 | | 4 |
156 # -----------
157 # | 1 | 2 | 3 |
158 # -----------
159 offsets = torch.tensor(
160 [
161 # (dh,dw)
162 (-d, 0), # 0
163 (-d, d), # 1
164 (0, d), # 2
165 (d, d), # 3
166 (d, 0), # 4
167 (d, -d), # 5
168 (0, -d), # 6
169 (-d, -d), # 7
170 ],
171 device=device,
172 )
173
174 # (B,H,W) indices
175 b = torch.arange(B, device=device)[:, None, None]
176 h = torch.arange(H, device=device)[None, :, None]
177 w = torch.arange(W, device=device)[None, None, :]
178 k = torch.arange(8, device=device)
179
180 # anchor points
181 b1 = b[:, None] # (B,1,1,1)
182 h1 = h[:, None] + d # (1,1,H,1)
183 w1 = w[:, None] + d # (1,1,1,W)
184 anchors = points[b1, h1, w1] # (B,H,W,3) -> (B,1,H,W,3)
185
186 # neighbor points
187 offset = offsets[k] # (8,2)
188 b2 = b1
189 h2 = h1 + offset[None, :, 0, None, None] # (1,8,H,1)
190 w2 = w1 + offset[None, :, 1, None, None] # (1,8,1,W)
191 points1 = points[b2, h2, w2] # (B,8,H,W,3)
192

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected