Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wawanbreton committed Apr 12, 2024
1 parent a25fada commit fdb724f
Show file tree
Hide file tree
Showing 23 changed files with 392 additions and 205 deletions.
36 changes: 19 additions & 17 deletions benchmark/infill_benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <benchmark/benchmark.h>

#include "geometry/open_lines_set.h"
#include "infill.h"

namespace cura
Expand Down Expand Up @@ -58,8 +59,8 @@ class InfillTest : public benchmark::Fixture
ff_holes.back().emplace_back(MM2INT(60), MM2INT(40));
ff_holes.back().emplace_back(MM2INT(90), MM2INT(25));

outline_polygons.add(square_shape);
outline_polygons.add(ff_holes);
outline_polygons.push_back(square_shape);
outline_polygons.push_back(ff_holes);

settings.add("fill_outline_gaps", "false");
settings.add("meshfix_maximum_deviation", "0.1");
Expand Down Expand Up @@ -93,29 +94,30 @@ class InfillTest : public benchmark::Fixture

BENCHMARK_DEFINE_F(InfillTest, Infill_generate_connect)(benchmark::State& st)
{
Infill infill(pattern,
zig_zagify,
connect_polygons,
outline_polygons,
INFILL_LINE_WIDTH,
line_distance,
INFILL_OVERLAP,
INFILL_MULTIPLIER,
FILL_ANGLE,
Z,
SHIFT,
MAX_RESOLUTION,
MAX_DEVIATION); // There are some optional parameters, but these will do for now (future improvement?).
Infill infill(
pattern,
zig_zagify,
connect_polygons,
outline_polygons,
INFILL_LINE_WIDTH,
line_distance,
INFILL_OVERLAP,
INFILL_MULTIPLIER,
FILL_ANGLE,
Z,
SHIFT,
MAX_RESOLUTION,
MAX_DEVIATION); // There are some optional parameters, but these will do for now (future improvement?).

for (auto _ : st)
{
std::vector<VariableWidthLines> result_paths;
Shape result_polygons;
Shape result_lines;
OpenLinesSet result_lines;
infill.generate(result_paths, result_polygons, result_lines, settings, 0, SectionType::INFILL, nullptr, nullptr);
}
}

BENCHMARK_REGISTER_F(InfillTest, Infill_generate_connect)->ArgsProduct({{true, false}, {400, 800, 1200}})->Unit(benchmark::kMillisecond);
BENCHMARK_REGISTER_F(InfillTest, Infill_generate_connect)->ArgsProduct({ { true, false }, { 400, 800, 1200 } })->Unit(benchmark::kMillisecond);
} // namespace cura
#endif // CURAENGINE_INFILL_BENCHMARK_H
25 changes: 12 additions & 13 deletions benchmark/wall_benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
#ifndef CURAENGINE_WALL_BENCHMARK_H
#define CURAENGINE_WALL_BENCHMARK_H

#include <benchmark/benchmark.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

#include <benchmark/benchmark.h>
#include <range/v3/view/join.hpp>
#include <spdlog/spdlog.h>

#include "InsetOrderOptimizer.h"
#include "WallsComputation.h"
#include "geometry/polygon.h"
#include "settings/Settings.h"
#include "sliceDataStorage.h"
#include "geometry/polygon.h"

#include <filesystem>
#include <fstream>
#include <iostream>
#include <spdlog/spdlog.h>

namespace cura
{
Expand Down Expand Up @@ -85,7 +84,7 @@ class WallTestFixture : public benchmark::Fixture
layer.parts.emplace_back();

SliceLayerPart& part = layer.parts.back();
part.outline.add(ff_holes);
part.outline.push_back(ff_holes);
}

void TearDown(const ::benchmark::State& state)
Expand Down Expand Up @@ -145,7 +144,7 @@ class HolesWallTestFixture : public benchmark::Fixture
layer.parts.emplace_back();

SliceLayerPart& part = layer.parts.back();
part.outline.add(shape.paths.front());
part.outline.push_back(shape.front());
part.print_outline = shape;
}

Expand All @@ -168,7 +167,7 @@ BENCHMARK_DEFINE_F(WallTestFixture, InsetOrderOptimizer_getRegionOrder)(benchmar
{
walls_computation.generateWalls(&layer, SectionType::WALL);
std::vector<ExtrusionLine> all_paths;
for (auto& line : layer.parts.back().wall_toolpaths | ranges::views::join )
for (auto& line : layer.parts.back().wall_toolpaths | ranges::views::join)
{
all_paths.emplace_back(line);
}
Expand All @@ -184,7 +183,7 @@ BENCHMARK_DEFINE_F(WallTestFixture, InsetOrderOptimizer_getInsetOrder)(benchmark
{
walls_computation.generateWalls(&layer, SectionType::WALL);
std::vector<ExtrusionLine> all_paths;
for (auto& line : layer.parts.back().wall_toolpaths | ranges::views::join )
for (auto& line : layer.parts.back().wall_toolpaths | ranges::views::join)
{
all_paths.emplace_back(line);
}
Expand All @@ -210,7 +209,7 @@ BENCHMARK_DEFINE_F(HolesWallTestFixture, InsetOrderOptimizer_getRegionOrder)(ben
{
walls_computation.generateWalls(&layer, SectionType::WALL);
std::vector<ExtrusionLine> all_paths;
for (auto& line : layer.parts.back().wall_toolpaths | ranges::views::join )
for (auto& line : layer.parts.back().wall_toolpaths | ranges::views::join)
{
all_paths.emplace_back(line);
}
Expand All @@ -226,7 +225,7 @@ BENCHMARK_DEFINE_F(HolesWallTestFixture, InsetOrderOptimizer_getInsetOrder)(benc
{
walls_computation.generateWalls(&layer, SectionType::WALL);
std::vector<ExtrusionLine> all_paths;
for (auto& line : layer.parts.back().wall_toolpaths | ranges::views::join )
for (auto& line : layer.parts.back().wall_toolpaths | ranges::views::join)
{
all_paths.emplace_back(line);
}
Expand Down
29 changes: 29 additions & 0 deletions include/geometry/lines_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <vector>

#include <range/v3/view/drop.hpp>

#include "geometry/point2ll.h"

namespace cura
Expand All @@ -25,6 +27,10 @@ class LinesSet
private:
std::vector<LineType> lines_;

public:
// Requires for some std calls as a container
typedef LineType value_type;

public:
LinesSet() = default;

Expand Down Expand Up @@ -226,6 +232,29 @@ class LinesSet
void addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType PolyTyp) const;

void addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType jointType, ClipperLib::EndType endType) const;

/*!
* \brief Display operator, useful for debugging/testing
*/
template<class CharT, class TraitsT>
friend std::basic_ostream<CharT, TraitsT>& operator<<(std::basic_ostream<CharT, TraitsT>& os, const LinesSet& lines)
{
os << "LinesSet[";

for (const LineType& line : lines | ranges::views::drop(1))
{
os << line;
os << ", ";
}

if (lines.size() > 1)
{
os << lines.back();
}

os << "]";
return os;
}
};

} // namespace cura
Expand Down
13 changes: 13 additions & 0 deletions include/geometry/points_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class PointsSet
private:
ClipperLib::Path points_;

public:
// Requires for some std calls as a container
typedef Point2LL value_type;

public:
PointsSet() = default;

Expand Down Expand Up @@ -195,6 +199,15 @@ class PointsSet
*/
void applyMatrix(const PointMatrix& matrix);
void applyMatrix(const Point3Matrix& matrix);

/*!
* \brief Display operator, useful for debugging/testing
*/
template<class CharT, class TraitsT>
friend std::basic_ostream<CharT, TraitsT>& operator<<(std::basic_ostream<CharT, TraitsT>& os, const PointsSet& polygon)
{
return os << "PointsSet(" << polygon.getPoints() << ")";
}
};

} // namespace cura
Expand Down
15 changes: 15 additions & 0 deletions include/geometry/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ class Shape : public LinesSet<Polygon>
*/
void simplify(ClipperLib::PolyFillType fill_type = ClipperLib::pftEvenOdd);

#ifdef BUILD_TESTS
/*!
* @brief Import the polygon from a WKT string
* @param wkt The WKT string to read from
* @return Polygons The polygons read from the stream
*/
[[maybe_unused]] static Shape fromWkt(const std::string& wkt);

/*!
* @brief Export the polygon to a WKT string
* @param stream The stream to write to
*/
[[maybe_unused]] void writeWkt(std::ostream& stream) const;
#endif

private:
/*!
* recursive part of \ref Polygons::removeEmptyHoles and \ref Polygons::getEmptyHoles
Expand Down
2 changes: 1 addition & 1 deletion include/utils/polygonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class PolygonUtils
* the polygon.
* \return Always returns 0.
*/
static unsigned int moveInside(const Polygon& polygon, Point2LL& from, int distance = 0, int64_t max_dist2 = std::numeric_limits<int64_t>::max());
static unsigned int moveInside(const ClosedPolyline& polygon, Point2LL& from, int distance = 0, int64_t max_dist2 = std::numeric_limits<int64_t>::max());

/*!
* Moves the point \p from onto the nearest polygon or leaves the point as-is, when the comb boundary is not within the root of \p max_dist2 distance.
Expand Down
66 changes: 66 additions & 0 deletions src/geometry/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
#include <numeric>
#include <unordered_set>

#ifdef BUILD_TESTS
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/read.hpp>
#include <fmt/format.h>
#include <range/v3/to_container.hpp>
#include <range/v3/view/c_str.hpp>
#include <range/v3/view/concat.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/take.hpp>
#include <range/v3/view/transform.hpp>
#endif

#include <range/v3/view/filter.hpp>
#include <range/v3/view/sliding.hpp>

Expand Down Expand Up @@ -916,6 +929,59 @@ void Shape::applyMatrix(const Point3Matrix& matrix)
}
}

#ifdef BUILD_TESTS
[[maybe_unused]] Shape Shape::fromWkt(const std::string& wkt)
{
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon_type;

polygon_type poly;
boost::geometry::read_wkt(wkt, poly);

Shape ret;

Polygon outer;
for (const auto& point : poly.outer())
{
outer.emplace_back(point.x(), point.y());
}
ret.push_back(outer);

for (const auto& hole : poly.inners())
{
Polygon inner;
for (const auto& point : hole)
{
inner.emplace_back(point.x(), point.y());
}
ret.push_back(inner);
}

return ret;
}

[[maybe_unused]] void Shape::writeWkt(std::ostream& stream) const
{
stream << "POLYGON (";
const auto paths_str = getLines()
| ranges::views::transform(
[](const Polygon& path)
{
const auto line_string = ranges::views::concat(path, path | ranges::views::take(1))
| ranges::views::transform(
[](const Point2LL& point)
{
return fmt::format("{} {}", point.X, point.Y);
})
| ranges::views::join(ranges::views::c_str(", ")) | ranges::to<std::string>();
return "(" + line_string + ")";
})
| ranges::views::join(ranges::views::c_str(", ")) | ranges::to<std::string>();
stream << paths_str;
stream << ")";
}
#endif

template LinesSet<OpenPolyline> Shape::intersection(const LinesSet<OpenPolyline>& polylines, bool restitch, const coord_t max_stitch_distance) const;
template LinesSet<OpenPolyline> Shape::intersection(const LinesSet<ClosedPolyline>& polylines, bool restitch, const coord_t max_stitch_distance) const;
template LinesSet<OpenPolyline> Shape::intersection(const LinesSet<Polygon>& polylines, bool restitch, const coord_t max_stitch_distance) const;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/polygonUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ size_t PolygonUtils::moveInside(const Shape& polygons, Point2LL& from, int dista
}

// Version that works on single PolygonRef.
unsigned int PolygonUtils::moveInside(const Polygon& polygon, Point2LL& from, int distance, int64_t maxDist2)
unsigned int PolygonUtils::moveInside(const ClosedPolyline &polygon, Point2LL& from, int distance, int64_t maxDist2)
{
// TODO: This is copied from the moveInside of Polygons.
/*
Expand Down
12 changes: 6 additions & 6 deletions stress_benchmark/stress_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
#include <spdlog/spdlog.h>

#include "WallsComputation.h"
#include "geometry/polygon.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "settings/Settings.h"
#include "sliceDataStorage.h"
#include "geometry/polygon.h"


constexpr std::string_view USAGE = R"(Stress Benchmark.
Expand Down Expand Up @@ -80,18 +80,18 @@ struct Resource
cura::Polygon outer;
for (const auto& point : boost_polygon.outer())
{
outer.add(cura::Point2LL(point.x(), point.y()));
outer.push_back(cura::Point2LL(point.x(), point.y()));
}
polygon.add(outer);
polygon.push_back(outer);

for (const auto& hole : boost_polygon.inners())
{
cura::Polygon inner;
for (const auto& point : hole)
{
inner.add(cura::Point2LL(point.x(), point.y()));
inner.push_back(cura::Point2LL(point.x(), point.y()));
}
polygon.add(inner);
polygon.push_back(inner);
}

polygons.push_back(polygon);
Expand Down Expand Up @@ -149,7 +149,7 @@ void handleChildProcess(const auto& shapes, const auto& settings)
{
layer.parts.emplace_back();
cura::SliceLayerPart& part = layer.parts.back();
part.outline.add(shape);
part.outline.push_back(shape);
}
cura::LayerIndex layer_idx(100);
cura::WallsComputation walls_computation(settings, layer_idx);
Expand Down
Loading

0 comments on commit fdb724f

Please sign in to comment.