Skip to content

Commit 49130dd

Browse files
committed
update docs and add test
1 parent af152ef commit 49130dd

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

docs/userguide/basics.visualisation.rst

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,6 @@ For example, meshes can have different colors for the vertices, the edges, and t
6464
And the colors of vertices, edges, and faces can be specified individually, per element.
6565
See the section about mesh visualisation for more information.
6666

67-
Object Transformation
68-
=====================
69-
70-
All scene objects have a transformation matrix that can be used to transform the object in the visualisation,
71-
independently of the geometry of the underlying data object.
72-
The default transformation matrix is the identity matrix, which means that the visualised geometry is the same as the geometry represented by the data.
73-
74-
>>> sceneobj = scene.add(box)
75-
>>> sceneobj.transformation
76-
Transformation([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]])
77-
78-
The transformation matrix can be set using a COMPAS Transformation object, or a 4x4 nested list of floats.
79-
80-
>>> from compas.geometry import Translation
81-
>>> sceneobj.transformation = Translation.from_vector([1.0, 2.0, 3.0])
82-
>>> sceneobj.transformation
83-
Transformation([[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 3.0], [0.0, 0.0, 0.0, 1.0]])
84-
85-
.. note::
86-
87-
For more information about working with transformations in COMPAS, see :doc:`basics.geometry.transformations`.
88-
8967
Scene Hierarchy
9068
===============
9169

@@ -115,6 +93,34 @@ To use a different scene object as the parent, the parent attribute of the scene
11593
>>> boxobj.parent
11694
PointObject
11795

96+
Object Frame And Transformation
97+
=====================
98+
99+
Every scene objects can have a reference "frame" that represents its local coordinate system relative to the frame of its hierarchical parent.
100+
In addition, an object can also have a local "transformation" which orientates this object from its frame.
101+
The final transformation of an object relative to the world coordinate system is the aggregated multiplication of all its hierarchical ancesters' frames,
102+
together with its own local frame and transformation. This prorperty can be accessed through the read-only attribute "worldtransformation".
103+
104+
>>> from compas.geometry import Translation
105+
>>> from compas.geometry import Box
106+
>>> from compas.geometry import Frame
107+
>>> sceneobj1 = scene.add(Box())
108+
>>> sceneobj1.frame = Frame(point = [1.0, 0.0, 0.0], xaxis=[1.0, 0.0, 0.0],yaxis=[0.0, 1.0, 0.0])
109+
>>> sceneobj1.transformation = Translation.from_vector([10.0, 0.0, 0.0])
110+
>>> sceneobj1.worldtransformation
111+
Transformation([[1.0, 0.0, 0.0, 11.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]], check=False)
112+
>>> sceneobj1.worldtransformation == sceneobj1.frame.to_transfromation() * sceneobj1.transformation
113+
True
114+
115+
The worldtransformation of a nestd "sceneobj2" will be calculated as: Frame of "sceneobj1" * Frame of "sceneobj2" * Transformation of "sceneobj2"
116+
>>> sceneobj2 = scene.add(Box(), parent=sceneobj1)
117+
>>> sceneobj2.frame = Frame([1.0, 1.0, 0.0], xaxis=[1.0, 0.0, 0.0],yaxis=[0.0, 1.0, 0.0])
118+
>>> sceneobj2.transformation = Translation.from_vector([10.0, 10.0, 0.0])
119+
>>> sceneobj.worldtransformation
120+
Transformation([[1.0, 0.0, 0.0, 12.0], [0.0, 1.0, 0.0, 11.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]], check=False)
121+
>>> sceneobj2.worldtransformation == sceneobj1.frame.to_transfromation() * sceneobj2.frame.to_transfrom() * sceneobj2.transformation
122+
True
123+
118124

119125
Scene Context
120126
=============

tests/compas/scene/test_scene.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import pytest # noqa: F401
55
from compas.scene import context
66
from compas.scene import register
7+
from compas.scene import Scene
78
from compas.scene import SceneObject
89
from compas.scene import SceneObjectNotRegisteredError
910
from compas.data import Data
11+
from compas.geometry import Box
12+
from compas.geometry import Frame
13+
from compas.geometry import Translation
1014

1115
@pytest.fixture(autouse=True)
1216
def reset_sceneobjects_registration():
@@ -75,3 +79,23 @@ def test_sceneobject_auto_context_discovery_no_context(mocker):
7579
with pytest.raises(SceneObjectNotRegisteredError):
7680
item = FakeSubItem()
7781
_ = SceneObject(item)
82+
83+
def test_sceneobject_transform():
84+
scene = Scene()
85+
sceneobj1 = scene.add(Box())
86+
sceneobj1.frame = Frame([1.0, 0.0, 0.0], xaxis=[1.0, 0.0, 0.0], yaxis=[0.0, 1.0, 0.0])
87+
sceneobj1.transformation = Translation.from_vector([10.0, 0.0, 0.0])
88+
assert sceneobj1.worldtransformation == sceneobj1.frame.to_transformation() * sceneobj1.transformation
89+
90+
sceneobj2 = scene.add(Box(), parent=sceneobj1)
91+
sceneobj2.frame = Frame([1.0, 1.0, 0.0], xaxis=[1.0, 0.0, 0.0], yaxis=[0.0, 1.0, 0.0])
92+
sceneobj2.transformation = Translation.from_vector([10.0, 10.0, 0.0])
93+
assert sceneobj2.worldtransformation == sceneobj1.frame.to_transformation() * sceneobj2.frame.to_transformation() * sceneobj2.transformation
94+
95+
sceneobj3 = scene.add(Box(), parent=sceneobj2)
96+
sceneobj3.frame = Frame([1.0, 1.0, 1.0], xaxis=[0.0, 1.0, 0.0], yaxis=[1.0, 0.0, 0.0])
97+
sceneobj3.transformation = Translation.from_vector([10.0, 10.0, 10.0])
98+
assert (
99+
sceneobj3.worldtransformation
100+
== sceneobj1.frame.to_transformation() * sceneobj2.frame.to_transformation() * sceneobj3.frame.to_transformation() * sceneobj3.transformation
101+
)

0 commit comments

Comments
 (0)