Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix collection #138

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed `NurbsSurfaceObject` to use tessellation function of `OCCBrep`, show boundary curves instead of control curves.
* Renamed all lazy setup functions to `lazy_init`.
* Fixed camera initialization issue.
* Fixed and brought back `CollectionObject`.

### Removed

Expand Down
24 changes: 24 additions & 0 deletions scripts/collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from compas.geometry import Box
from compas.geometry import Line
from compas.geometry import Frame
from compas_viewer.viewer import Viewer
from compas_viewer.scene import Collection
from random import random

viewer = Viewer()

boxes = []
for i in range(20):
for j in range(20):
box = Box(0.5, 0.5, 0.5, Frame([i, j, 0], [1, 0, 0], [0, 1, 0]))
boxes.append(box)

viewer.scene.add(Collection(boxes), name="Boxes")

lines = []
for i in range(1000):
lines.append(Line([random() * 20, random() * 20, random() * 20], [random() * 20, random() * 20, random() * 20]))

viewer.scene.add(Collection(lines), name="Lines")

viewer.show()
6 changes: 1 addition & 5 deletions src/compas_viewer/components/renderer/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from compas_viewer.base import Base
from compas_viewer.configurations import RendererConfig
from compas_viewer.scene import TagObject
from compas_viewer.scene.collectionobject import CollectionObject
from compas_viewer.scene.vectorobject import VectorObject

from .camera import Camera
Expand Down Expand Up @@ -576,10 +575,7 @@ def sort(obj):
mesh_objs.append(obj)

for obj in objs:
if isinstance(obj, CollectionObject):
[sort(item) for item in obj.objects]
else:
sort(obj)
sort(obj)

return tag_objs, vector_objs, mesh_objs

Expand Down
5 changes: 5 additions & 0 deletions src/compas_viewer/scene/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
from .geometryobject import GeometryObject
from .groupobject import Group
from .groupobject import GroupObject
from .collectionobject import Collection
from .collectionobject import CollectionObject


@plugin(category="drawing-utils", requires=["compas_viewer"])
Expand Down Expand Up @@ -84,6 +86,7 @@ def register_scene_objects():
register(Cone, ConeObject, context="Viewer")
register(Capsule, CapsuleObject, context="Viewer")
register(list, GroupObject, context="Viewer")
register(Collection, CollectionObject, context="Viewer")

try:
from compas_occ.brep import OCCBrep
Expand Down Expand Up @@ -138,4 +141,6 @@ def register_scene_objects():
"GeometryObject",
"Group",
"GroupObject",
"Collection",
"CollectionObject",
]
64 changes: 21 additions & 43 deletions src/compas_viewer/scene/collectionobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
class Collection(Data):
"""Viewer scene object for displaying a collection of COMPAS geometries."""

def __init__(self, items: list[Union[Geometry, Mesh]], **kwargs):
def __init__(self, items: list[Union[Geometry, Mesh]] = None, **kwargs):
super().__init__(**kwargs)
self.items = items

@property
def __data__(self):
return self.items
return {"items": self.items}


class CollectionObject(ViewerSceneObject, GeometryObject):
"""Viewer scene object for displaying a collection of COMPAS geometries."""

def __init__(self, items: list[Union[Geometry, Mesh]], **kwargs):
self.collection = Collection(items)
def __init__(self, collection: Collection, **kwargs):
self.collection = collection
super().__init__(geometry=self.collection, **kwargs)
self.objects = [ViewerSceneObject(item=item, **kwargs) for item in self.collection.items]
self.objects = [ViewerSceneObject(item, **kwargs) for item in self.collection.items]

def _read_points_data(self) -> ShaderDataType:
positions = []
Expand All @@ -43,6 +43,7 @@ def _read_points_data(self) -> ShaderDataType:
colors += c
elements += (array(e) + count).tolist()
count += len(p)

return positions, colors, elements

def _read_lines_data(self) -> ShaderDataType:
Expand All @@ -56,56 +57,33 @@ def _read_lines_data(self) -> ShaderDataType:
colors += c
elements += (array(e) + count).tolist()
count += len(p)

return positions, colors, elements

def _read_frontfaces_data(self) -> ShaderDataType:
positions = []
colors = []
opacities = []
elements = []
count = 0
for obj in self.objects:
if obj.use_rgba:
p, c, o, e = obj._read_frontfaces_data() or ([], [], [])
positions += p
colors += c
opacities += o
elements += (np.array(e) + count).tolist()
count += len(p)
else:
p, c, e = obj._read_frontfaces_data() or ([], [], [])
positions += p
colors += c
elements += (np.array(e) + count).tolist()
count += len(p)

if opacities:
return positions, colors, opacities, elements
else:
return positions, colors, elements
p, c, e = obj._read_frontfaces_data() or ([], [], [])
positions += p
colors += c
elements += (np.array(e) + count).tolist()
count += len(p)

return positions, colors, elements

def _read_backfaces_data(self) -> ShaderDataType:
positions = []
colors = []
opacities = []
elements = []
count = 0
for obj in self.objects:
if obj.use_rgba:
p, c, o, e = obj._read_backfaces_data() or ([], [], [])
positions += p
colors += c
opacities += o
elements += (np.array(e) + count).tolist()
count += len(p)
else:
p, c, e = obj._read_backfaces_data() or ([], [], [])
positions += p
colors += c
elements += (np.array(e) + count).tolist()
count += len(p)

if opacities:
return positions, colors, opacities, elements
else:
return positions, colors, elements
p, c, e = obj._read_backfaces_data() or ([], [], [])
positions += p
colors += c
elements += (np.array(e) + count).tolist()
count += len(p)

return positions, colors, elements
1 change: 0 additions & 1 deletion src/compas_viewer/scene/sceneobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ def __init__(
self._matrix_buffer: Optional[list[list[float]]] = None
self._bounding_box: Optional[list[float]] = None
self._bounding_box_center: Optional[Point] = None
self._is_collection = False

# Primitive
self._points_data: Optional[ShaderDataType] = None
Expand Down
Loading