Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Prepare enviroment
- name: Prepare environment
run: |
python -m pip install --upgrade pip
python -m pip install wheel
Expand Down
15 changes: 10 additions & 5 deletions docarray/document/mixins/mesh.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from typing import TYPE_CHECKING, Union

import numpy as np
Expand All @@ -7,7 +8,7 @@
import trimesh


class Mesh:
class MeshEnum(Enum):
FILE_EXTENSIONS = [
'glb',
'obj',
Expand All @@ -17,6 +18,10 @@ class Mesh:
FACES = 'faces'


class PointCloudEnum(Enum):
COLORS = 'point_cloud_colors'


class MeshDataMixin:
"""Provide helper functions for :class:`Document` to support 3D mesh data and point cloud."""

Expand Down Expand Up @@ -80,8 +85,8 @@ def load_uri_to_vertices_and_faces(self: 'T') -> 'T':
faces = mesh.faces.view(np.ndarray)

self.chunks = [
Document(name=Mesh.VERTICES, tensor=vertices),
Document(name=Mesh.FACES, tensor=faces),
Document(name=MeshEnum.VERTICES, tensor=vertices),
Document(name=MeshEnum.FACES, tensor=faces),
]

return self
Expand All @@ -96,9 +101,9 @@ def load_vertices_and_faces_to_point_cloud(self: 'T', samples: int) -> 'T':
faces = None

for chunk in self.chunks:
if chunk.tags['name'] == Mesh.VERTICES:
if chunk.tags['name'] == MeshEnum.VERTICES:
vertices = chunk.tensor
if chunk.tags['name'] == Mesh.FACES:
if chunk.tags['name'] == MeshEnum.FACES:
faces = chunk.tensor

if vertices is not None and faces is not None:
Expand Down
29 changes: 20 additions & 9 deletions docarray/document/mixins/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np

from docarray.document.mixins.mesh import Mesh
from docarray.document.mixins.mesh import MeshEnum, PointCloudEnum


class PlotMixin:
Expand Down Expand Up @@ -133,7 +133,7 @@ def _is_3d_vertices_and_faces(self):
"""
if self.chunks is not None:
name_tags = [c.tags['name'] for c in self.chunks]
if Mesh.VERTICES in name_tags and Mesh.FACES in name_tags:
if MeshEnum.VERTICES in name_tags and MeshEnum.FACES in name_tags:
return True
else:
return False
Expand Down Expand Up @@ -173,9 +173,11 @@ def display_vertices_and_faces(self):
import trimesh

vertices = [
c.tensor for c in self.chunks if c.tags['name'] == Mesh.VERTICES
c.tensor for c in self.chunks if c.tags['name'] == MeshEnum.VERTICES
][-1]
faces = [c.tensor for c in self.chunks if c.tags['name'] == Mesh.FACES][-1]
faces = [c.tensor for c in self.chunks if c.tags['name'] == MeshEnum.FACES][
-1
]
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
display(mesh.show())

Expand All @@ -185,15 +187,24 @@ def display_point_cloud_tensor(self) -> None:
from IPython.display import display
from hubble.utils.notebook import is_notebook

colors = np.tile(np.array([0, 0, 0]), (len(self.tensor), 1))
for chunk in self.chunks:
if (
'name' in chunk.tags.keys()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 'name' a key that is already established in other parts of the code / other features? If not I think I would prefer a more descriptive key here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used 'name' for the vertices and faces chunks, too. I think it makes sense to use the same for both of them, to keep it simple for the user. Do you have a suggestion for what you would prefer over 'name'?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no sounds good, let's keep it consistent

and chunk.tags['name'] == PointCloudEnum.COLORS
and chunk.tensor.shape[-1] in [3, 4]
):
colors = chunk.tensor

pc = trimesh.points.PointCloud(
vertices=self.tensor,
colors=colors,
)

if is_notebook():
pc = trimesh.points.PointCloud(
vertices=self.tensor,
colors=np.tile(np.array([0, 0, 0, 1]), (len(self.tensor), 1)),
)
s = trimesh.Scene(geometry=pc)
display(s.show())
else:
pc = trimesh.points.PointCloud(vertices=self.tensor)
display(pc.show())

def display_rgbd_tensor(self) -> None:
Expand Down
9 changes: 9 additions & 0 deletions docs/datatypes/mesh/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2576,6 +2576,15 @@ init();</script></body>
</html>" width="100%" height="500px" style="border:none;"></iframe


To display a colored point cloud, store the corresponding colors in the `.tensor` of a chunk `Document` with the name tag `'point_cloud_colors'`. The colors have to be of shape (n_samples, 3) or (n_samples, 4).

```python
Comment thread
anna-charlotte marked this conversation as resolved.
n_samples = 1000
colors = np.random.rand(n_samples, 3)
doc = Document(uri='mesh_man.glb').load_uri_to_point_cloud_tensor(samples=n_samples)
doc.chunks = [Document(tensor=colors, name='point_cloud_colors')]
```

## RGB-D image representation

The RGB-D image representation includes an RGB image of shape (w, h, 3) and a corresponding depth image (w, h). The depth image describes the distance between the image plane and the corresponding object for each pixel in the RGB image. Since the RGB and depth image are of identical width and height, they can be easily concatenated and stored in a tensor of shape (w, h, 4). Due to their fixed size, RGB-D images are suitable for 3D data representations for input to machine learning models.
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
'requests',
'matplotlib',
'Pillow',
'trimesh',
'trimesh[easy]',
Comment thread
anna-charlotte marked this conversation as resolved.
'ipython',
'scipy',
'av',
'fastapi',
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/document/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from docarray import Document
from docarray.document.generators import from_files
from docarray.document.mixins.mesh import Mesh
from docarray.document.mixins.mesh import MeshEnum

__windows__ = sys.platform == 'win32'

Expand Down Expand Up @@ -321,9 +321,9 @@ def test_load_uri_to_vertices_and_faces(uri):
doc.load_uri_to_vertices_and_faces()

assert len(doc.chunks) == 2
assert doc.chunks[0].tags['name'] == Mesh.VERTICES
assert doc.chunks[0].tags['name'] == MeshEnum.VERTICES
assert doc.chunks[0].tensor.shape[1] == 3
assert doc.chunks[1].tags['name'] == Mesh.FACES
assert doc.chunks[1].tags['name'] == MeshEnum.FACES
assert doc.chunks[1].tensor.shape[1] == 3


Expand Down