Simulation speed made 3X

case where no polygon in layer is also addressed

CURA-11289
This commit is contained in:
saumya.jain 2023-12-22 10:37:51 +01:00
parent 13540c3ed2
commit 411b40d78c

View file

@ -57,7 +57,7 @@ class SimulationView(CuraView):
LAYER_VIEW_TYPE_LINE_TYPE = 1 LAYER_VIEW_TYPE_LINE_TYPE = 1
LAYER_VIEW_TYPE_FEEDRATE = 2 LAYER_VIEW_TYPE_FEEDRATE = 2
LAYER_VIEW_TYPE_THICKNESS = 3 LAYER_VIEW_TYPE_THICKNESS = 3
SIMULATION_FACTOR = 5 SIMULATION_FACTOR = 3
_no_layers_warning_preference = "view/no_layers_warning" _no_layers_warning_preference = "view/no_layers_warning"
@ -192,33 +192,30 @@ class SimulationView(CuraView):
return self._current_path_num return self._current_path_num
def setTime(self, time: float) -> None: def setTime(self, time: float) -> None:
self._current_time = time
left_i = 0
right_i = self._max_paths - 1
cumulative_line_duration = self.cumulativeLineDuration() cumulative_line_duration = self.cumulativeLineDuration()
total_duration = cumulative_line_duration[-1] if len(cumulative_line_duration) > 0:
self._current_time = time
left_i = 0
right_i = self._max_paths - 1
total_duration = cumulative_line_duration[-1]
# make an educated guess about where to start
i = int(right_i * max(0.0, min(1.0, self._current_time / total_duration)))
# binary search for the correct path
while left_i < right_i:
if cumulative_line_duration[i] <= self._current_time:
left_i = i + 1
else:
right_i = i
i = int((left_i + right_i) / 2)
# make an educated guess about where to start left_value = cumulative_line_duration[i - 1] if i > 0 else 0.0
i = int(right_i * max(0.0, min(1.0, self._current_time / total_duration))) right_value = cumulative_line_duration[i]
# binary search for the correct path assert (left_value <= self._current_time <= right_value)
while left_i < right_i:
if cumulative_line_duration[i] <= self._current_time:
left_i = i + 1
else:
right_i = i
i = int((left_i + right_i) / 2)
left_value = cumulative_line_duration[i - 1] if i > 0 else 0.0 fractional_value = (self._current_time - left_value) / (right_value - left_value)
right_value = cumulative_line_duration[i]
assert (left_value <= self._current_time <= right_value) self.setPath(i + fractional_value)
fractional_value = (self._current_time - left_value) / (right_value - left_value)
self.setPath(i + fractional_value)
def advanceTime(self, time_increase: float) -> bool: def advanceTime(self, time_increase: float) -> bool:
""" """
@ -227,7 +224,9 @@ class SimulationView(CuraView):
:param time_increase: The amount of time to advance (in seconds). :param time_increase: The amount of time to advance (in seconds).
:return: True if the time was advanced, False if the end of the simulation was reached. :return: True if the time was advanced, False if the end of the simulation was reached.
""" """
total_duration = self.cumulativeLineDuration()[-1] total_duration = 0.0
if len(self.cumulativeLineDuration()) > 0:
total_duration = self.cumulativeLineDuration()[-1]
if self._current_time + time_increase > total_duration: if self._current_time + time_increase > total_duration:
# If we have reached the end of the simulation, go to the next layer. # If we have reached the end of the simulation, go to the next layer.
@ -250,10 +249,12 @@ class SimulationView(CuraView):
self._cumulative_line_duration = {} self._cumulative_line_duration = {}
self._cumulative_line_duration[self.getCurrentLayer()] = [] self._cumulative_line_duration[self.getCurrentLayer()] = []
total_duration = 0.0 total_duration = 0.0
for polyline in self.getLayerData().polygons: polylines = self.getLayerData()
for line_duration in list((polyline.lineLengths / polyline.lineFeedrates)[0]): if polylines is not None:
total_duration += line_duration / SimulationView.SIMULATION_FACTOR for polyline in polylines.polygons:
self._cumulative_line_duration[self.getCurrentLayer()].append(total_duration) for line_duration in list((polyline.lineLengths / polyline.lineFeedrates)[0]):
total_duration += line_duration / SimulationView.SIMULATION_FACTOR
self._cumulative_line_duration[self.getCurrentLayer()].append(total_duration)
return self._cumulative_line_duration[self.getCurrentLayer()] return self._cumulative_line_duration[self.getCurrentLayer()]