Skip to content

Commit

Permalink
A couple of small performance tweaks to model collection
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhogg committed Dec 31, 2024
1 parent e801633 commit f5b8230
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
37 changes: 20 additions & 17 deletions src/flitter/render/window/canvas3d.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,26 @@ cdef Model get_model(Node node, bint top):
model = Model._vector(vertices, faces)
if model is not None and node.get_bool('repair', False):
model = model.repair()
elif not top and node.kind is 'transform':
transform_matrix = update_transform_matrix(node, IdentityTransform)
model = Model._boolean('union', [get_model(child, False)._transform(transform_matrix) for child in node._children], 0, 0, 0)
elif node.kind is 'trim' or node.kind is 'slice':
normal = node.get_fvec('normal', 3, null_)
origin = node.get_fvec('origin', 3, Zero3)
if len(node._children) == 1:
model = get_model(node._children[0], False)
else:
model = Model._boolean('union', [get_model(child, False) for child in node._children], 0, 0, 0)
if model is not None and normal.as_bool():
model = model._trim(origin, normal,
node.get_float('smooth', 0),
node.get_float('fillet', 0),
node.get_float('chamfer', 0))
elif node.kind in ('union', 'intersect', 'difference'):
model = Model._boolean(node.kind, [get_model(child, False) for child in node._children],
node.get_float('smooth', 0),
node.get_float('fillet', 0),
node.get_float('chamfer', 0))
elif node.kind is 'sdf':
maximum = node.get_fvec('maximum', 3, node.get_fvec('max', 3, One3))
minimum = node.get_fvec('minimum', 3, node.get_fvec('min', 3, maximum.neg()))
Expand All @@ -483,23 +503,6 @@ cdef Model get_model(Node node, bint top):
model = Model._sdf(None, model, minimum, maximum, resolution)
elif node.kind is 'mix':
model = Model._mix([get_model(child, False) for child in node._children], node.get_fvec('weights', 0, true_))
elif not top and node.kind is 'transform':
transform_matrix = update_transform_matrix(node, IdentityTransform)
model = Model._boolean('union', [get_model(child, False)._transform(transform_matrix) for child in node._children], 0, 0, 0)
elif node.kind in ('union', 'intersect', 'difference'):
model = Model._boolean(node.kind, [get_model(child, False) for child in node._children],
node.get_float('smooth', 0),
node.get_float('fillet', 0),
node.get_float('chamfer', 0))
elif node.kind is 'trim' or node.kind is 'slice':
normal = node.get_fvec('normal', 3, null_)
origin = node.get_fvec('origin', 3, Zero3)
model = Model._boolean('union', [get_model(child, False) for child in node._children], 0, 0, 0)
if model is not None and normal.as_bool():
model = model._trim(origin, normal,
node.get_float('smooth', 0),
node.get_float('fillet', 0),
node.get_float('chamfer', 0))
elif (cls := get_plugin('flitter.render.window.models', node.kind)) is not None:
model = cls.from_node(node)
if model is not None:
Expand Down
6 changes: 3 additions & 3 deletions src/flitter/render/window/models.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ cdef class Transform(UnaryOperation):

@staticmethod
cdef Model _get(Model original, Matrix44 transform_matrix):
cdef str name = f'{original.name}@{hex(transform_matrix.hash(False))[2:]}'
cdef str name = f'{original.name}@{transform_matrix.hash(False):x}'
cdef Transform model = <Transform>ModelCache.get(name, None)
if model is None:
model = Transform.__new__(Transform)
Expand Down Expand Up @@ -646,7 +646,7 @@ cdef class Trim(UnaryOperation):
cdef Trim _get(Model original, Vector origin, Vector normal, double smooth, double fillet, double chamfer):
if origin.numbers == NULL or origin.length != 3 or normal.numbers == NULL or normal.length != 3:
return None
cdef str name = f'trim({original.name}, {hex(origin.hash(False) ^ normal.hash(False))[2:]}'
cdef str name = f'trim({original.name}, {origin.hash(False) ^ normal.hash(False):x}'
if smooth:
name += f', smooth={smooth}'
elif fillet:
Expand Down Expand Up @@ -956,7 +956,7 @@ cdef class Box(PrimitiveModel):
@staticmethod
cdef Box _get(str uv_map):
uv_map = uv_map if uv_map in Box.VertexUV else 'standard'
cdef str name = '!box' if uv_map == 'standard' else f'!box({uv_map})'
cdef str name = '!box' if uv_map is 'standard' else f'!box-{uv_map}'
cdef Box model = <Box>ModelCache.get(name, None)
if model is None:
model = Box.__new__(Box)
Expand Down

0 comments on commit f5b8230

Please sign in to comment.