Skip to content

Commit

Permalink
Refactor Progress class and improve type safety
Browse files Browse the repository at this point in the history
This refactor cleans up the Progress class and enhances type safety. It
replaces raw C-style arrays with std::array, which guarantees type
safety and prevents out-of-bound access. The type of several variables
and methods has also been changed from int and float to size_t and
double to increase precision.
  • Loading branch information
jellespijker committed Nov 8, 2023
1 parent 138d6cd commit eeb93cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
26 changes: 19 additions & 7 deletions include/progress/Progress.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright (c) 2018 Ultimaker B.V.
// CuraEngine is released under the terms of the AGPLv3 or higher.
// Copyright (c) 2023 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#ifndef PROGRESS_H
#define PROGRESS_H

#include <array>
#include <string>
#include <string_view>
#include <optional>

#include "utils/gettime.h"

Expand All @@ -13,7 +16,7 @@ namespace cura

struct LayerIndex;

#define N_PROGRESS_STAGES 7
static constexpr size_t N_PROGRESS_STAGES = 7;

/*!
* Class for handling the progress bar and the progress logging.
Expand All @@ -39,9 +42,18 @@ class Progress
};

private:
static double times[N_PROGRESS_STAGES]; //!< Time estimates per stage
static std::string names[N_PROGRESS_STAGES]; //!< name of each stage
static double accumulated_times[N_PROGRESS_STAGES]; //!< Time past before each stage
static constexpr std::array<double, N_PROGRESS_STAGES> times{
0.0, // START = 0,
5.269, // SLICING = 1,
1.533, // PARTS = 2,
71.811, // INSET_SKIN = 3
51.009, // SUPPORT = 4,
154.62, // EXPORT = 5,
0.1 // FINISH = 6
};

static constexpr std::array<std::string_view, N_PROGRESS_STAGES> names{ "start", "slice", "layerparts", "inset+skin", "support", "export", "process" };
static std::array<double, N_PROGRESS_STAGES> accumulated_times; //!< Time past before each stage
static double total_timing; //!< An estimate of the total time
static std::optional<LayerIndex> first_skipped_layer; //!< The index of the layer for which we skipped time reporting
/*!
Expand All @@ -51,7 +63,7 @@ class Progress
* \param stage_process How far we currently are in the \p stage
* \return An estimate of the overall progress.
*/
static float calcOverallProgress(Stage stage, float stage_progress);
static double calcOverallProgress(Stage stage, double stage_progress);

public:
static void init(); //!< Initialize some values needed in a fast computation of the progress
Expand Down
40 changes: 14 additions & 26 deletions src/progress/Progress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,50 @@

namespace cura
{

double Progress::times[] = {
0.0, // START = 0,
5.269, // SLICING = 1,
1.533, // PARTS = 2,
71.811, // INSET_SKIN = 3
51.009, // SUPPORT = 4,
154.62, // EXPORT = 5,
0.1 // FINISH = 6
};
std::string Progress::names[] = { "start", "slice", "layerparts", "inset+skin", "support", "export", "process" };

double Progress::accumulated_times[N_PROGRESS_STAGES] = { -1 };
std::array<double, N_PROGRESS_STAGES> Progress::accumulated_times = { -1 };
double Progress::total_timing = -1;
std::optional<LayerIndex> Progress::first_skipped_layer{};

float Progress::calcOverallProgress(Stage stage, float stage_progress)
double Progress::calcOverallProgress(Stage stage, double stage_progress)
{
assert(stage_progress <= 1.0);
assert(stage_progress >= 0.0);
return (accumulated_times[(int)stage] + stage_progress * times[(int)stage]) / total_timing;
return (accumulated_times.at(static_cast<size_t>(stage)) + stage_progress * times.at(static_cast<size_t>(stage))) / total_timing;
}

void Progress::init()
{
double accumulated_time = 0;
for (int stage = 0; stage < N_PROGRESS_STAGES; stage++)
for (size_t stage = 0; stage < N_PROGRESS_STAGES; stage++)
{
accumulated_times[(int)stage] = accumulated_time;
accumulated_time += times[(int)stage];
accumulated_times.at(static_cast<size_t>(stage)) = accumulated_time;
accumulated_time += times.at(static_cast<size_t>(stage));
}
total_timing = accumulated_time;
}

void Progress::messageProgress(Progress::Stage stage, int progress_in_stage, int progress_in_stage_max)
{
float percentage = calcOverallProgress(stage, float(progress_in_stage) / float(progress_in_stage_max));
Application::getInstance().communication->sendProgress(percentage);
double percentage = calcOverallProgress(stage, static_cast<double>(progress_in_stage / static_cast<double>(progress_in_stage_max)));
Application::getInstance().communication->sendProgress(static_cast<float>(percentage));
}

void Progress::messageProgressStage(Progress::Stage stage, TimeKeeper* time_keeper)
{
if (time_keeper)
if (time_keeper != nullptr)
{
if ((int)stage > 0)
if (static_cast<int>(stage) > 0)
{
spdlog::info("Progress: {} accomplished in {:03.3f}s", names[(int)stage - 1], time_keeper->restart());
spdlog::info("Progress: {} accomplished in {:03.3f}s", names.at(static_cast<size_t>(stage) - 1), time_keeper->restart());
}
else
{
time_keeper->restart();
}

if ((int)stage < (int)Stage::FINISH)
if (static_cast<int>(stage) < static_cast<int>(Stage::FINISH))
{
spdlog::info("Starting {}...", names[(int)stage]);
spdlog::info("Starting {}...", names.at(static_cast<size_t>(stage)));
}
}
}
Expand Down Expand Up @@ -108,7 +96,7 @@ void Progress::messageProgressLayer(LayerIndex layer_nr, size_t total_layers, do
{
padding = iterator_max_size->stage.size();

for (auto [index, time] : stages | ranges::views::enumerate)
for (const auto& [index, time] : stages | ranges::views::enumerate)
{
spdlog::info("{}── {}:{} {:03.3f}s", index < stages.size() - 1 ? "" : "", time.stage, std::string(padding - time.stage.size(), ' '), time.duration);
}
Expand Down

0 comments on commit eeb93cf

Please sign in to comment.