share hashcode implementation

This commit is contained in:
Yeicor
2024-02-24 11:00:34 +01:00
parent 69b1da711e
commit 872d8e5e03
2 changed files with 17 additions and 11 deletions

View File

@@ -162,7 +162,7 @@ class Server:
"""Publishes any single-file GLTF object to the server (GLB format recommended)."""
start = time.time()
# Precompute the info and send it to the client as if it was a CAD object
precomputed_info = self._show_common(name, hashlib.md5(gltf).hexdigest(), start)
precomputed_info = self._show_common(name, _hashcode(gltf, **kwargs), start)
# Also pre-populate the GLTF data for the object API
publish_to = BufferedPubSub[bytes]()
publish_to.publish_nowait(gltf)
@@ -191,7 +191,7 @@ class Server:
# Convert Z-up (OCCT convention) to Y-up (GLTF convention)
obj = Shape(obj).rotate(Axis.X, -90).wrapped
self._show_common(name, _hashcode(obj), start, obj)
self._show_common(name, _hashcode(obj, **kwargs), start, obj)
async def _api_object(self, request: web.Request) -> web.Response:
"""Returns the object file with the matching name, building it if necessary."""

View File

@@ -1,7 +1,7 @@
import hashlib
import io
import re
from typing import List, Dict, Tuple
from typing import List, Dict, Tuple, Union
from OCP.BRep import BRep_Tool
from OCP.BRepAdaptor import BRepAdaptor_Curve
@@ -123,13 +123,19 @@ def _tessellate_vertex(mgr: GLTFMgr, ocp_vertex: TopoDS_Vertex, faces: List[Topo
mgr.add_vertex(_push_point((c.X, c.Y, c.Z), faces))
def _hashcode(obj: TopoDS_Shape) -> str:
def _hashcode(obj: Union[bytes, TopoDS_Shape], **extras) -> str:
"""Utility to compute the hash code of a shape recursively without the need to tessellate it"""
# NOTE: obj.HashCode(MAX_HASH_CODE) is not stable across different runs of the same program
# This is best-effort and not guaranteed to be unique
map_of_shapes = TopTools_IndexedMapOfShape()
TopExp.MapShapes_s(obj, map_of_shapes)
hasher = hashlib.md5(usedforsecurity=False)
for k, v in extras.items():
hasher.update(str(k).encode())
hasher.update(str(v).encode())
if isinstance(obj, bytes):
hasher.update(obj)
else:
for i in range(1, map_of_shapes.Extent() + 1):
sub_shape = map_of_shapes.FindKey(i)
sub_data = io.BytesIO()