Skip to content

Commit

Permalink
pointcloud to grid.array
Browse files Browse the repository at this point in the history
  • Loading branch information
railgnam committed Aug 5, 2024
1 parent df28af2 commit 30410a9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
9 changes: 8 additions & 1 deletion bdm_voxel_builder/grid/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
from compas.geometry import Box, Pointcloud, Transformation, transform_points_numpy

from bdm_voxel_builder import TEMP_DIR
from bdm_voxel_builder.helpers.numpy import convert_array_to_pts
from bdm_voxel_builder.helpers.numpy import convert_pointcloud_to_grid_array
from bdm_voxel_builder.helpers.savepaths import get_savepath
from bdm_voxel_builder.helpers.vdb import xform_to_compas, xform_to_vdb
from bdm_voxel_builder.helpers.compas import pointcloud_from_ply


class Grid:
Expand Down Expand Up @@ -163,3 +164,9 @@ def from_vdb(cls, grid: os.PathLike | vdb.GridBase, name: str = None):
array=arr,
xform=xform_to_compas(grid.transform),
)

def array_from_ply(self, path: os.PathLike):
pointcloud = pointcloud_from_ply(path)
grid_array = convert_pointcloud_to_grid_array(pointcloud, tolerance_mm = 5)
return grid_array

7 changes: 7 additions & 0 deletions bdm_voxel_builder/helpers/compas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy.typing as npt
from compas.data import json_dump
from compas.geometry import Pointcloud
import os

from bdm_voxel_builder import TEMP_DIR
from bdm_voxel_builder.helpers.numpy import sort_pts_by_values
Expand All @@ -24,3 +25,9 @@ def save_pointcloud(
dict_ = {"pointcloud": pointcloud, "values": values}

json_dump(dict_, get_savepath(TEMP_DIR, ".json", note=note))

def pointcloud_from_ply(
path: os.PathLike
):
pointcloud = Pointcloud.from_ply(path)
return pointcloud
27 changes: 27 additions & 0 deletions bdm_voxel_builder/helpers/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ def convert_array_to_pts(

return np.vstack(coordinates).transpose()

def convert_pointcloud_to_grid_array(pointcloud, unit_in_mm = 10):
pts = pointcloud.points
coordinate_array = np.asarray(pts) # array = [[x,y,z][x,y,z][x,y,z]]
index_array = np.floor_divide(coordinate_array, unit_in_mm)
index_array = np.int64(index_array)
# print(f'index array, floor divide:\n {index_array}')

maximums = np.amax(index_array, axis = 0)
minimums = np.amin(index_array, axis = 0)
# print(f'max:{maximums}, mins:{minimums}')

move_to_origin_vector = 0 - minimums
index_array += move_to_origin_vector
# print(f'index array, translated:\n {index_array}')

bounds = np.int64(maximums - minimums)
bounds += 1
# print(f'grid_size {bounds}')

grid_from_pointcloud = np.zeros(bounds)
for point in index_array:
x,y,z = point
# print(f'coord: {x,y,z}')
grid_from_pointcloud[x][y][z] = 1

# print(f'grid_from_pointcloud=\n{grid_from_pointcloud}')
return grid_from_pointcloud

def save_ndarray(arr: npt.NDArray, note: str = None):
np.save(get_savepath(TEMP_DIR, ".npy", note=note), arr)
Expand Down
23 changes: 15 additions & 8 deletions bdm_voxel_builder/test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import numpy as np

from bdm_voxel_builder.grid import Grid
from bdm_voxel_builder.helpers.numpy import convert_pointcloud_to_grid_array, convert_array_to_pts
from compas.geometry import Pointcloud
from bdm_voxel_builder.helpers.compas import pointcloud_from_ndarray
print("hello, lets test this!\n")
a = np.random.random(26) + 0.5
for i in [1, 4, 3, 22, 5]:
a[i] = 0

filled = np.nonzero(a)
print(f"filled i = {filled}")

v = np.int_(a)
print(f"trunced ints: {v}")
n = 5
pt_array = np.random.random(n * 3).reshape([n,3]) * 1200 - 100
# pt_array2 = np.transpose(pt_array)

pts = pt_array.tolist()
print(pt_array.shape)
pointcloud = Pointcloud(pts)
print(pointcloud)
array = convert_pointcloud_to_grid_array(pointcloud, unit_in_mm = 10)
print(array)

0 comments on commit 30410a9

Please sign in to comment.