Skip to content

Commit

Permalink
Merge branch 'main' into CURA-12081_scarf-seam
Browse files Browse the repository at this point in the history
  • Loading branch information
rburema committed Sep 24, 2024
2 parents 008ff2a + 5c8de19 commit 4ab56b5
Show file tree
Hide file tree
Showing 25 changed files with 478 additions and 139 deletions.
24 changes: 23 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ set(engine_SRCS # Except main.cpp.
src/communication/ArcusCommunication.cpp
src/communication/ArcusCommunicationPrivate.cpp
src/communication/CommandLine.cpp
src/communication/EmscriptenCommunication.cpp
src/communication/Listener.cpp

src/infill/ImageBasedDensityProvider.cpp
Expand Down Expand Up @@ -271,7 +272,28 @@ endif ()

if (CMAKE_CXX_PLATFORM_ID STREQUAL "emscripten")
message(STATUS "Building for Emscripten")
target_link_options(_CuraEngine PUBLIC -Wno-unused-command-line-argument -sINVOKE_RUN=0 -sEXPORT_NAME=CuraEngine -sEXPORTED_RUNTIME_METHODS=[callMain,FS] -sFORCE_FILESYSTEM=1 -sALLOW_MEMORY_GROWTH=1 -sEXPORT_ES6=1 -sMODULARIZE=1 -sSINGLE_FILE=1 -sENVIRONMENT=worker -sERROR_ON_UNDEFINED_SYMBOLS=0 -lembind --embind-emit-tsd CuraEngine.d.ts)
target_link_options(_CuraEngine
PUBLIC
"SHELL:-sINVOKE_RUN=0"
"SHELL:-sEXPORT_NAME=CuraEngine"
"SHELL:-sEXPORTED_RUNTIME_METHODS=[callMain,FS]"
"SHELL:-sFORCE_FILESYSTEM=1"
"SHELL:-sALLOW_MEMORY_GROWTH=1"
"SHELL:-sEXPORT_ES6=1"
"SHELL:-sMODULARIZE=1"
"SHELL:-sSINGLE_FILE=1"
"SHELL:-sENVIRONMENT=web"
"SHELL:-sERROR_ON_UNDEFINED_SYMBOLS=0"
"SHELL:-sWASM_BIGINT=1"
"SHELL:-sSTACK_SIZE=196608"
$<$<CONFIG:Debug>:SHELL:-sASSERTIONS=2>
$<$<CONFIG:Debug>:SHELL:-sSAFE_HEAP=1>
$<$<CONFIG:Debug>:SHELL:-sSTACK_OVERFLOW_CHECK=2>
$<$<CONFIG:Debug>:SHELL:-g3>
$<$<CONFIG:Debug>:SHELL:-gsource-map>
"SHELL:-lembind"
"SHELL:--embind-emit-tsd CuraEngine.d.ts"
)
endif ()

target_link_libraries(CuraEngine PRIVATE
Expand Down
5 changes: 3 additions & 2 deletions include/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cassert>
#include <cstddef>
#include <memory>
#include <string>

#include "utils/NoCopy.h"
Expand Down Expand Up @@ -38,14 +39,14 @@ class Application : NoCopy
* can assume that it is safe to access this without checking whether it is
* initialised.
*/
Communication* communication_ = nullptr;
std::shared_ptr<Communication> communication_;

/*
* \brief The slice that is currently ongoing.
*
* If no slice has started yet, this will be a nullptr.
*/
Slice* current_slice_ = nullptr;
std::shared_ptr<Slice> current_slice_;

/*!
* \brief ThreadPool with lifetime tied to Application
Expand Down
13 changes: 6 additions & 7 deletions include/communication/CommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ using container_setting_map = std::unordered_map<std::string, setting_map>;
class CommandLine : public Communication
{
public:
CommandLine() = default;

/*
* \brief Construct a new communicator that interprets the command line to
* start a slice.
Expand Down Expand Up @@ -141,18 +143,15 @@ class CommandLine : public Communication
*/
void sliceNext() override;

private:
#ifdef __EMSCRIPTEN__
std::string progressHandler;
#endif

std::vector<std::filesystem::path> search_directories_;

protected:
/*
* \brief The command line arguments that the application was called with.
*/
std::vector<std::string> arguments_;

private:
std::vector<std::filesystem::path> search_directories_;

/*
* The last progress update that we output to stdcerr.
*/
Expand Down
65 changes: 65 additions & 0 deletions include/communication/EmscriptenCommunication.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#ifndef EMSCRIPTENCOMMUNICATION_H
#define EMSCRIPTENCOMMUNICATION_H
#ifdef __EMSCRIPTEN__

#include "communication/CommandLine.h"

namespace cura
{

/**
* \class EmscriptenCommunication
* \brief A class for handling communication in an Emscripten environment.
*
* This class extends the CommandLine class and provides specific implementations
* for sending progress and handling slice information in an Emscripten environment.
*/
class EmscriptenCommunication : public CommandLine
{
private:
std::string progress_handler_; ///< Handler for progress messages.
std::string gcode_header_handler_; ///< Handler for getting the GCode handler.
std::string slice_info_handler_; ///< Handler for slice information messages.

/**
* \brief Creates a message containing slice information.
* \return A string containing the slice information message.
*/
[[nodiscard]] static std::string createSliceInfoMessage();

public:
/**
* \brief Constructor for EmscriptenCommunication.
* \param arguments A vector of strings containing the command line arguments.
*/
EmscriptenCommunication(const std::vector<std::string>& arguments);

/**
* \brief Sends the progress of the current operation.
* \param progress A double representing the progress percentage.
*/
void sendProgress(double progress) const override;

/**
* \brief Sends GcodeHeader
*/
void sendGCodePrefix(const std::string& prefix) const override;

/**
* \brief Initiates the slicing of the next item.
*/
void sliceNext() override;

bool isSequential() const override
{
return false;
}
};

} // namespace cura

#endif // __EMSCRIPTEN__
#endif // EMSCRIPTENCOMMUNICATION_H
78 changes: 69 additions & 9 deletions include/utils/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,97 @@
namespace cura
{

/**
* @brief Returns the square of a value.
*
* @tparam T A multipliable type (arithmetic types such as int, float, double, etc.)
* @param a The value to be squared.
* @return T The square of the input value.
*/
template<utils::multipliable T>
[[nodiscard]] T square(const T& a)
{
return a * a;
}

[[nodiscard]] inline int64_t round_divide_signed(const int64_t dividend, const int64_t divisor) //!< Return dividend divided by divisor rounded to the nearest integer
/**
* @brief Returns the quotient of the division of two signed integers, rounded to the nearest integer.
*
* @param dividend The numerator.
* @param divisor The denominator (must not be zero).
* @return int64_t The result of the division rounded to the nearest integer.
* @throws std::invalid_argument If the divisor is zero.
*/
[[nodiscard]] inline int64_t round_divide_signed(const int64_t dividend, const int64_t divisor)
{
if ((dividend < 0) ^ (divisor < 0)) // Either the numerator or the denominator is negative, so the result must be negative.
if ((dividend < 0) ^ (divisor < 0))
{
return (dividend - divisor / 2) / divisor; // Flip the .5 offset to do proper rounding in the negatives too.
return (dividend - divisor / 2) / divisor;
}
return (dividend + divisor / 2) / divisor;
}

[[nodiscard]] inline uint64_t ceil_divide_signed(const int64_t dividend, const int64_t divisor) //!< Return dividend divided by divisor rounded up towards positive infinity.
/**
* @brief Returns the quotient of the division of two signed integers, rounded up towards positive infinity.
*
* @param dividend The numerator.
* @param divisor The denominator (must not be zero).
* @return int64_t The result of the division rounded up.
* @throws std::invalid_argument If the divisor is zero.
*/
[[nodiscard]] inline int64_t ceil_divide_signed(const int64_t dividend, const int64_t divisor)
{
return static_cast<uint64_t>((dividend / divisor) + (dividend * divisor > 0 ? 1 : 0));
int64_t quotient = dividend / divisor;
int64_t remainder = dividend % divisor;

// Round up if there's a remainder and the signs of dividend and divisor are the same
if (remainder != 0 && ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0)))
{
quotient += 1;
}

return quotient;
}

[[nodiscard]] inline uint64_t floor_divide_signed(const int64_t dividend, const int64_t divisor) //!< Return dividend divided by divisor rounded down towards negative infinity.
/**
* @brief Returns the quotient of the division of two signed integers, rounded down towards negative infinity.
*
* @param dividend The numerator.
* @param divisor The denominator (must not be zero).
* @return int64_t The result of the division rounded down.
* @throws std::invalid_argument If the divisor is zero.
*/
[[nodiscard]] inline int64_t floor_divide_signed(const int64_t dividend, const int64_t divisor)
{
return static_cast<uint64_t>((dividend / divisor) + (dividend * divisor > 0 ? 0 : -1));
const int64_t quotient = dividend / divisor;
const int64_t remainder = dividend % divisor;
if (remainder != 0 && ((dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)))
{
return quotient - 1;
}
return quotient;
}

[[nodiscard]] inline uint64_t round_divide(const uint64_t dividend, const uint64_t divisor) //!< Return dividend divided by divisor rounded to the nearest integer
/**
* @brief Returns the quotient of the division of two unsigned integers, rounded to the nearest integer.
*
* @param dividend The numerator.
* @param divisor The denominator (must not be zero).
* @return uint64_t The result of the division rounded to the nearest integer.
*/
[[nodiscard]] inline uint64_t round_divide(const uint64_t dividend, const uint64_t divisor)
{
return (dividend + divisor / 2) / divisor;
}

[[nodiscard]] inline uint64_t round_up_divide(const uint64_t dividend, const uint64_t divisor) //!< Return dividend divided by divisor rounded to the nearest integer
/**
* @brief Returns the quotient of the division of two unsigned integers, rounded up towards positive infinity.
*
* @param dividend The numerator.
* @param divisor The denominator (must not be zero).
* @return uint64_t The result of the division rounded up.
*/
[[nodiscard]] inline uint64_t round_up_divide(const uint64_t dividend, const uint64_t divisor)
{
return (dividend + divisor - 1) / divisor;
}
Expand Down
27 changes: 27 additions & 0 deletions include/utils/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <ctype.h>
#include <sstream> // ostringstream

#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <spdlog/spdlog.h>

namespace cura
Expand All @@ -27,6 +30,30 @@ static inline int stringcasecompare(const char* a, const char* b)
return *a - *b;
}

// Convert string to base64 string.
// This function is useful to forward string through javascript even if they contain any special strings
//
[[maybe_unused]] static std::string convertTobase64(const std::string& input)
{
using namespace boost::archive::iterators;
// prepare the stream to hold the encoded data
std::stringstream output;

// encode data
typedef base64_from_binary<transform_width<std::string::const_iterator, 6, 8>> base64_enc;
std::copy(base64_enc(input.begin()), base64_enc(input.end()), ostream_iterator<char>(output));

// Retrieve the encoded string
std::string output_encoded = output.str();

// ensure padding if needed
size_t num = (3 - input.length() % 3) % 3;
for (size_t i = 0; i < num; i++)
{
output_encoded.push_back('=');
}
return output_encoded;
}
/*!
* Efficient conversion of micron integer type to millimeter string.
*
Expand Down
12 changes: 8 additions & 4 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>

#include "Slice.h"
#include "communication/ArcusCommunication.h" //To connect via Arcus to the front-end.
#include "communication/CommandLine.h" //To use the command line to slice stuff.
#include "communication/EmscriptenCommunication.h" // To use Emscripten to slice stuff.
#include "progress/Progress.h"
#include "utils/ThreadPool.h"
#include "utils/string.h" //For stringcasecompare.
Expand All @@ -45,7 +47,6 @@ Application::Application()

Application::~Application()
{
delete communication_;
delete thread_pool_;
}

Expand Down Expand Up @@ -100,7 +101,7 @@ void Application::connect()
}
}

ArcusCommunication* arcus_communication = new ArcusCommunication();
auto arcus_communication = std::make_shared<ArcusCommunication>();
arcus_communication->connect(ip, port);
communication_ = arcus_communication;
}
Expand Down Expand Up @@ -214,8 +215,11 @@ void Application::slice()
{
arguments.emplace_back(argv_[argument_index]);
}

communication_ = new CommandLine(arguments);
#ifdef __EMSCRIPTEN__
communication_ = std::make_shared<EmscriptenCommunication>(arguments);
#else
communication_ = std::make_shared<CommandLine>(arguments);
#endif
}

void Application::run(const size_t argc, char** argv)
Expand Down
2 changes: 1 addition & 1 deletion src/ConicalOverhang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void ConicalOverhang::apply(Slicer* slicer, const Mesh& mesh)
const coord_t layer_thickness = mesh.settings_.get<coord_t>("layer_height");
coord_t max_dist_from_lower_layer = std::llround(tan_angle * static_cast<double>(layer_thickness)); // max dist which can be bridged

for (LayerIndex layer_nr = slicer->layers.size() - 2; static_cast<int>(layer_nr) >= 0; layer_nr--)
for (LayerIndex layer_nr = LayerIndex(slicer->layers.size()) - 2; layer_nr >= 0; layer_nr--)
{
SlicerLayer& layer = slicer->layers[static_cast<size_t>(layer_nr)];
SlicerLayer& layer_above = slicer->layers[static_cast<size_t>(layer_nr) + 1ul];
Expand Down
Loading

0 comments on commit 4ab56b5

Please sign in to comment.