Skip to content

Commit 462cba7

Browse files
committed
Merge branch 'main' into conversions-api
2 parents 6c37fc2 + 62d3ec4 commit 462cba7

File tree

9 files changed

+431
-23
lines changed

9 files changed

+431
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3838
* Added `compas_rhino.conversions.docobjects.meshobject_to_compas`.
3939
* Added `compas_rhino.conversions.docobjects.pointobject_to_compas`.
4040
* Added `compas_rhino.conversions.docobjects.surfaceobject_to_compas`.
41+
* Added `compas.datastructures.HashTree` and `compas.datastructures.HashNode`.
4142

4243
### Changed
4344

@@ -62,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6263
* Changed `compas_rhino.objects.get_line_coordinates` to deprecated (removed in v2.3).
6364
* Changed `compas_rhino.objects.get_polyline_coordinates` to deprecated (removed in v2.3).
6465
* Changed `compas_rhino.objects.get_polygon_coordinates` to deprecated (removed in v2.3).
66+
* Fixed a bug in `worldtransformation` of `compas.scene.SceneObject` to include the object's own frame.
6567

6668
### Removed
6769

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
********************************************************************************
2+
Hash Tree
3+
********************************************************************************
4+
5+
Hash tree (or Merkle tree) is a tree data structure in which every leaf node is labelled with the hash of a data block and every non-leaf node is labelled with the cryptographic hash of the labels of its child nodes.
6+
Hash trees are useful because they allow efficient and secure verification of the contents of large data structures. It is widly used in modern distributed version control systems like Git as well as peer-to-peer systems like Blockchain.
7+
COMPAS provides a simple implementation of a hash tree that can be used for detecting and locating changes in a complex data structure. In context of AEC, this feature can also be useful for many real-world applications,
8+
such as detecting changes in a complicated Building Information Model, tracking minor deformation in structural assessments, or even detecting robot joint movements in a digital fabracation process, and many more.
9+
10+
Hash Tree From Dict
11+
===================
12+
13+
A COMPAS hash tree can be created from any raw python dictionary using the `HashTree.from_dict` method.
14+
15+
>>> from compas.datastructures import HashTree
16+
>>> data = {'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}}
17+
>>> tree = HashTree.from_dict(data)
18+
19+
The structure of the hash tree and crypo hash on each node can be visualised using the `print` function.
20+
21+
>>> print(tree)
22+
<Tree with 6 nodes>
23+
└── ROOT @ b2e1c
24+
├── .a:1 @ 4d9a8
25+
├── .b:2 @ 82b86
26+
└── .c @ 664a3
27+
├── .d:3 @ 76d82
28+
└── .e:4 @ ebe84
29+
30+
Once the original data is modified, a new hash tree can be created from the modified data and the changes can be detected by comparing the two hash trees.
31+
32+
>>> data['c']['d'] = 5
33+
>>> del data["b"]
34+
>>> data["f"] = True
35+
>>> new_tree = HashTree.from_dict(data)
36+
>>> print(new_tree)
37+
<Tree with 6 nodes>
38+
└── ROOT @ a8c1b
39+
├── .a:1 @ 4d9a8
40+
├── .c @ e1701
41+
│ ├── .d:5 @ 98b1e
42+
│ └── .e:4 @ ebe84
43+
└── .f:True @ 753e5
44+
45+
>>> new_tree.diff(tree)
46+
{'added': [{'path': '.f', 'value': True}], 'removed': [{'path': '.b', 'value': 2}], 'modified': [{'path': '.c.d', 'old': 3, 'new': 5}]}
47+
48+
Hash Tree From COMPAS Data
49+
==========================
50+
51+
A COMPAS hash tree can also be created from any classes that inherit from the base `Data` class in COMPAS, such as `Mesh`, `Graph`, `Shape`, `Geometry`, etc.
52+
This is done by hashing the serilised data of the object.
53+
54+
>>> from compas.datastructures import Mesh
55+
>>> mesh = Mesh.from_polyhedron(6)
56+
>>> tree = HashTree.from_object(mesh)
57+
>>> print(tree)
58+
<Tree with 58 nodes>
59+
└── ROOT @ 44cc1
60+
├── .attributes @ 3370c
61+
├── .default_vertex_attributes @ 84700
62+
│ ├── .x:0.0 @ 5bc2d
63+
│ ├── .y:0.0 @ 1704b
64+
│ └── .z:0.0 @ 6199e
65+
├── .default_edge_attributes @ 5e834
66+
├── .default_face_attributes @ 5a8d9
67+
├── .vertex @ ff6d0
68+
│ ├── .0 @ 84ec1
69+
│ │ ├── .x:-1.1547005383792517 @ 874f4
70+
│ │ ├── .y:-1.1547005383792517 @ d2b16
71+
│ │ └── .z:-1.1547005383792517 @ bd9f0
72+
│ ├── .1 @ 316d3
73+
...
74+
75+
>>> mesh.vertex_attribute(0, "x", 1.0)
76+
>>> mesh.delete_face(3)
77+
>>> new_tree = HashTree.from_object(mesh)
78+
>>> new_tree.diff(tree)
79+
{'added': [], 'removed': [{'path': '.face.3', 'value': [4, 2, 3, 5]}, {'path': '.facedata.3', 'value': None}], 'modified': [{'path': '.vertex.0.x', 'old': -1.1547005383792517, 'new': 1.0}]}
80+

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
=============

docs/userguide/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ User Guide
3232
advanced.tolerance
3333
advanced.serialisation
3434
advanced.rpc
35+
advanced.hashtree
3536

3637

3738
.. toctree::

src/compas/datastructures/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from .assembly.part import Feature, GeometricFeature, ParametricFeature, Part
5656
from .cell_network.cell_network import CellNetwork
5757
from .tree.tree import Tree, TreeNode
58+
from .tree.hashtree import HashTree, HashNode
5859

5960
Network = Graph
6061

@@ -72,4 +73,6 @@
7273
"ParametricFeature",
7374
"Tree",
7475
"TreeNode",
76+
"HashTree",
77+
"HashNode",
7578
]

0 commit comments

Comments
 (0)