mirror of
https://github.com/yeicor-3d/yet-another-cad-viewer.git
synced 2025-12-19 22:24:17 +01:00
share hashcode implementation
This commit is contained in:
@@ -162,7 +162,7 @@ class Server:
|
|||||||
"""Publishes any single-file GLTF object to the server (GLB format recommended)."""
|
"""Publishes any single-file GLTF object to the server (GLB format recommended)."""
|
||||||
start = time.time()
|
start = time.time()
|
||||||
# Precompute the info and send it to the client as if it was a CAD object
|
# 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
|
# Also pre-populate the GLTF data for the object API
|
||||||
publish_to = BufferedPubSub[bytes]()
|
publish_to = BufferedPubSub[bytes]()
|
||||||
publish_to.publish_nowait(gltf)
|
publish_to.publish_nowait(gltf)
|
||||||
@@ -191,7 +191,7 @@ class Server:
|
|||||||
# Convert Z-up (OCCT convention) to Y-up (GLTF convention)
|
# Convert Z-up (OCCT convention) to Y-up (GLTF convention)
|
||||||
obj = Shape(obj).rotate(Axis.X, -90).wrapped
|
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:
|
async def _api_object(self, request: web.Request) -> web.Response:
|
||||||
"""Returns the object file with the matching name, building it if necessary."""
|
"""Returns the object file with the matching name, building it if necessary."""
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import io
|
import io
|
||||||
import re
|
import re
|
||||||
from typing import List, Dict, Tuple
|
from typing import List, Dict, Tuple, Union
|
||||||
|
|
||||||
from OCP.BRep import BRep_Tool
|
from OCP.BRep import BRep_Tool
|
||||||
from OCP.BRepAdaptor import BRepAdaptor_Curve
|
from OCP.BRepAdaptor import BRepAdaptor_Curve
|
||||||
@@ -123,18 +123,24 @@ def _tessellate_vertex(mgr: GLTFMgr, ocp_vertex: TopoDS_Vertex, faces: List[Topo
|
|||||||
mgr.add_vertex(_push_point((c.X, c.Y, c.Z), faces))
|
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"""
|
"""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
|
# 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
|
# This is best-effort and not guaranteed to be unique
|
||||||
map_of_shapes = TopTools_IndexedMapOfShape()
|
map_of_shapes = TopTools_IndexedMapOfShape()
|
||||||
TopExp.MapShapes_s(obj, map_of_shapes)
|
TopExp.MapShapes_s(obj, map_of_shapes)
|
||||||
hasher = hashlib.md5(usedforsecurity=False)
|
hasher = hashlib.md5(usedforsecurity=False)
|
||||||
for i in range(1, map_of_shapes.Extent() + 1):
|
for k, v in extras.items():
|
||||||
sub_shape = map_of_shapes.FindKey(i)
|
hasher.update(str(k).encode())
|
||||||
sub_data = io.BytesIO()
|
hasher.update(str(v).encode())
|
||||||
TopoDS_Shape.DumpJson(sub_shape, sub_data)
|
if isinstance(obj, bytes):
|
||||||
val = sub_data.getvalue()
|
hasher.update(obj)
|
||||||
val = re.sub(b'"this": "[^"]*"', b'', val) # Remove memory address
|
else:
|
||||||
hasher.update(val)
|
for i in range(1, map_of_shapes.Extent() + 1):
|
||||||
|
sub_shape = map_of_shapes.FindKey(i)
|
||||||
|
sub_data = io.BytesIO()
|
||||||
|
TopoDS_Shape.DumpJson(sub_shape, sub_data)
|
||||||
|
val = sub_data.getvalue()
|
||||||
|
val = re.sub(b'"this": "[^"]*"', b'', val) # Remove memory address
|
||||||
|
hasher.update(val)
|
||||||
return hasher.hexdigest()
|
return hasher.hexdigest()
|
||||||
|
|||||||
Reference in New Issue
Block a user