Skip to content

Commit

Permalink
revert entity caching
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Sep 16, 2021
1 parent b377b09 commit d016aed
Showing 1 changed file with 2 additions and 51 deletions.
53 changes: 2 additions & 51 deletions trimesh/path/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
Basic geometric primitives which only store references to
vertex indices rather than vertices themselves.
"""
from functools import wraps
import numpy as np

from copy import deepcopy
Expand All @@ -16,45 +15,6 @@
from ..util import ABC


def cached(function):
"""
A decorator for class methods to cache computed values.
Parameters
------------
function : method
This is used as a decorator:
```
@cache_decorator
def foo(self, things):
return 'happy days'
```
"""

# use wraps to preserve docstring
@wraps(function)
def get_cached(self, vertices, **kwargs):
"""
Only execute the function if its value isn't stored
in cache already.
"""
# get a cache key by XOR'ing relevant fields.
key = (vertices.fast_hash() ^
hash(self) ^
hash(function.__name__))
stored = self._cache.get(key)
if stored is not None:
return stored
value = function(self, vertices=vertices, **kwargs)
self._cache[key] = value
return value

# all cached values are also properties
# so they can be accessed like value attributes
# rather than functions
return get_cached


class Entity(ABC):

def __init__(self,
Expand Down Expand Up @@ -256,7 +216,6 @@ def bounds(self, vertices):
vertices[self.points].max(axis=0)])
return bounds

@cached
def length(self, vertices):
"""
Return the total length of the entity.
Expand Down Expand Up @@ -558,7 +517,6 @@ class Line(Entity):
A line or poly-line entity
"""

@cached
def discrete(self, vertices, scale=1.0):
"""
Discretize into a world- space path.
Expand Down Expand Up @@ -661,7 +619,6 @@ def _bytes(self):
order = int(self.points[0] > self.points[-1]) * 2 - 1
return b'Arc' + bytes(self.closed) + self.points[::order].tobytes()

@cached
def length(self, vertices):
"""
Return the arc length of the 3-point arc.
Expand All @@ -688,7 +645,6 @@ def length(self, vertices):
vertices, return_normal=False, return_angle=True)
return fit['span'] * fit['radius'] * 2

@cached
def discrete(self, vertices, scale=1.0):
"""
Discretize the arc entity into line sections.
Expand All @@ -711,7 +667,6 @@ def discrete(self, vertices, scale=1.0):
close=self.closed,
scale=scale))

@cached
def center(self, vertices, **kwargs):
"""
Return the center information about the arc entity.
Expand All @@ -728,7 +683,6 @@ def center(self, vertices, **kwargs):
"""
return arc_center(vertices[self.points], **kwargs)

@cached
def bounds(self, vertices):
"""
Return the AABB of the arc entity.
Expand Down Expand Up @@ -778,7 +732,6 @@ class Bezier(Curve):
An open or closed Bezier curve
"""

@cached
def discrete(self, vertices, scale=1.0, count=None):
"""
Discretize the Bezier curve.
Expand All @@ -797,11 +750,10 @@ def discrete(self, vertices, scale=1.0, count=None):
discrete : (m, 2) or (m, 3) float
Curve as line segments
"""
discrete = discretize_bezier(
return self._orient(discretize_bezier(
vertices[self.points],
count=count,
scale=scale)
return self._orient(discrete)
scale=scale))


class BSpline(Curve):
Expand All @@ -826,7 +778,6 @@ def __init__(self,
self.kwargs = kwargs
self.color = color

@cached
def discrete(self, vertices, count=None, scale=1.0):
"""
Discretize the B-Spline curve.
Expand Down

0 comments on commit d016aed

Please sign in to comment.