Skip to content

Commit 948d4ed

Browse files
committed
monitor a curves ctrlpts and weights for changes
1 parent f4e6347 commit 948d4ed

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

Diff for: geomdl/NURBS.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ def __deepcopy__(self, memo):
9191
result.init_cache()
9292
return result
9393

94-
def init_cache(self):
95-
self._cache['ctrlpts'] = self._init_array()
96-
self._cache['weights'] = self._init_array()
94+
def init_cache(self, ctrlpts=[], weights=[]):
95+
self._cache['ctrlpts'] = self._array_type(iter(ctrlpts))
96+
self._cache['weights'] = self._array_type(iter(weights))
97+
self._cache['ctrlpts'].register_callback(lambda: setattr(self, '_control_points_valid', False))
98+
self._cache['weights'].register_callback(lambda: setattr(self, '_control_points_valid', False))
9799

98100
@property
99101
def ctrlptsw(self):
@@ -107,6 +109,9 @@ def ctrlptsw(self):
107109
:getter: Gets the weighted control points
108110
:setter: Sets the weighted control points
109111
"""
112+
if not self._control_points_valid:
113+
ctrlptsw = compatibility.combine_ctrlpts_weights(self.ctrlpts, self.weights)
114+
self.set_ctrlpts(ctrlptsw)
110115
return self._control_points
111116

112117
@ctrlptsw.setter
@@ -127,22 +132,17 @@ def ctrlpts(self):
127132
# Populate the cache, if necessary
128133
if not self._cache['ctrlpts']:
129134
c, w = compatibility.separate_ctrlpts_weights(self._control_points)
130-
self._cache['ctrlpts'] = self._array_type(iter(c))
131-
self._cache['weights'] = self._array_type(iter(w))
135+
self.init_cache(c, w)
132136
return self._cache['ctrlpts']
133137

134138
@ctrlpts.setter
135139
def ctrlpts(self, value):
136140
# Check if we can retrieve the existing weights. If not, generate a weights vector of 1.0s.
137141
if not self.weights:
138-
weights = [1.0 for _ in range(len(value))]
139-
else:
140-
weights = self.weights
142+
self.weights[:] = [1.0 for _ in range(len(value))]
141143

142144
# Generate weighted control points using the new control points
143-
ctrlptsw = compatibility.combine_ctrlpts_weights(value, weights)
144-
145-
# Set new weighted control points
145+
ctrlptsw = compatibility.combine_ctrlpts_weights(value, self.weights)
146146
self.set_ctrlpts(ctrlptsw)
147147

148148
@property
@@ -159,8 +159,7 @@ def weights(self):
159159
# Populate the cache, if necessary
160160
if not self._cache['weights']:
161161
c, w = compatibility.separate_ctrlpts_weights(self._control_points)
162-
self._cache['ctrlpts'] = self._array_type(iter(c))
163-
self._cache['weights'] = self._array_type(iter(w))
162+
self.init_cache(c, w)
164163
return self._cache['weights']
165164

166165
@weights.setter
@@ -174,6 +173,12 @@ def weights(self, value):
174173
# Set new weighted control points
175174
self.set_ctrlpts(ctrlptsw)
176175

176+
def _check_variables(self):
177+
super(Curve, self)._check_variables()
178+
if not self._control_points_valid:
179+
ctrlptsw = compatibility.combine_ctrlpts_weights(self.ctrlpts, self.weights)
180+
self.set_ctrlpts(ctrlptsw)
181+
177182
def reset(self, **kwargs):
178183
""" Resets control points and/or evaluated points.
179184
@@ -189,11 +194,9 @@ def reset(self, **kwargs):
189194
# Call parent function
190195
super(Curve, self).reset(ctrlpts=reset_ctrlpts, evalpts=reset_evalpts)
191196

197+
# Delete the caches
192198
if reset_ctrlpts:
193-
# Delete the caches
194-
self._cache['ctrlpts'] = self._init_array()
195-
# TODO: why is this reset differently from the initialisation?
196-
self._cache['weights'][:] = self._init_array()
199+
self.init_cache()
197200

198201

199202
@export

Diff for: geomdl/abstract.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def __init__(self, **kwargs):
135135
self._knot_vector = [self._init_array() for _ in range(self._pdim)] # knot vector
136136
self._control_points = self._init_array() # control points
137137
self._control_points_size = [0 for _ in range(self._pdim)] # control points length
138+
self._control_points_valid = False
138139
self._delta = [self._dinit for _ in range(self._pdim)] # evaluation delta
139140
self._bounding_box = self._init_array() # bounding box
140141
self._evaluator = None # evaluator instance
@@ -466,7 +467,7 @@ def validate_and_clean(pts_in, check_for, dimension, pts_out, **kws):
466467
raise ValueError("Number of arguments after ctrlpts must be " + str(self._pdim))
467468

468469
# Keyword arguments
469-
array_init = kwargs.get('array_init', [[] for _ in range(len(ctrlpts))])
470+
array_init = kwargs.get('array_init', self._array_type([] for _ in range(len(ctrlpts))))
470471
array_check_for = kwargs.get('array_check_for', (list, tuple))
471472
callback_func = kwargs.get('callback', validate_and_clean)
472473
self._dimension = kwargs.get('dimension', len(ctrlpts[0]))
@@ -480,6 +481,7 @@ def validate_and_clean(pts_in, check_for, dimension, pts_out, **kws):
480481
# Set control points and sizes
481482
self._control_points = callback_func(ctrlpts, array_check_for, self._dimension, array_init, **kwargs)
482483
self._control_points_size = [int(arg) for arg in args]
484+
self._control_points_valid = True
483485

484486
@abc.abstractmethod
485487
def render(self, **kwargs):
@@ -891,6 +893,7 @@ def reset(self, **kwargs):
891893
if reset_ctrlpts:
892894
self._control_points = self._init_array()
893895
self._bounding_box = self._init_array()
896+
self._control_points_valid = False
894897

895898
if reset_evalpts:
896899
self._eval_points = self._init_array()

0 commit comments

Comments
 (0)