Please note that even after deleting the previous commits, they may still be present in web crawls or in other repositories that have already cloned the previous version of your project. It's not possible to completely erase all traces of the previous commits from the internet.
Here, create_octree() takes a point cloud as input and returns a list of octants that form an octree with the specified depth. Octant is a class that represents an octant and its children, and split() is a method that splits an octant into 8 children. query_octree() takes a list of octants and a set of query points as input, and returns the distance from each query point to the closest octant. gradient_query_octree() computes the gradient of the output of query_octree() with respect to
import torch
import torch.nn.functional as F
class OctreeSDF(torch.nn.Module):
def __init__(self, depth=6, size=1.0):
super(OctreeSDF, self).__init__()
self.depth = depth
self.size = size
self.root = OctreeNode(size)
self.register_buffer('grid_points', self.generate_grid_points())
def generate_grid_points(self):
n = 2**self.depth
coords = torch.linspace(-self.size, self.size, n)
points = torch.zeros((n**3, 3))
idx = 0
for x in coords:
for y in coords:
for z in coords:
points[idx] = torch.tensor([x, y, z])
idx += 1
return points
def forward(self, x):
# Query SDF values at input points
sdf_values = self.query_sdf(x)
# Compute occupancy from SDF values
occupancy = torch.sign(sdf_values)
return occupancy, sdf_values
def query_sdf(self, x):
# Normalize query points to [-1, 1] range
x_norm = x / self.size
# Find octree nodes containing query points
nodes = self.find_octree_nodes(x_norm)
# Compute SDF values for each query point
sdf_values = torch.zeros(x.shape[0])
for i in range(x.shape[0]):
sdf_values[i] = self.compute_sdf(nodes[i], x_norm[i])
return sdf_values
def find_octree_nodes(self, x):
# Find the leaf node for each query point
nodes = []
for i in range(x.shape[0]):
node = self.root
for j in range(self.depth):
child_idx = self.get_child_index(node, x[i])
if child_idx is None or node.children[child_idx] is None:
break
node = node.children[child_idx]
nodes.append(node)
return nodes
def get_child_index(self, node, x):
# Compute child index for a given node and query point
idx = ((x > node.center) * 1).int()
if torch.all(idx == 0) or torch.all(idx == 1):
return None
else:
return idx[0] * 4 + idx[1] * 2 + idx[2]
def compute_sdf(self, node, x):
# Compute SDF value for a given node and query point
if node.children is None:
# Leaf node, compute SDF value
return torch.norm(x - node.center, dim=0) - node.size / 2
else:
# Interior node, recurse on child node
child_idx = self.get_child_index(node, x)
if child_idx is None or node.children[child_idx] is None:
return 0
else:
return self.compute_sdf(node.children[child_idx], x)
def get_gradients(self, x):
# Compute gradients of SDF values with respect to input points
x.requires_grad_()
sdf_values = self.query_sdf(x)
gradients = torch.autograd.grad(sdf_values, x, torch.ones_like(sdf_values), create_graph=True)[0]
return gradients
class OctreeNode:
def __init__(self, size):
self.center = torch.zeros(3)
self.size