Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Systematic use of coord_t conversions, enabling to change unit size #1710

Closed
wants to merge 9 commits into from
29 changes: 13 additions & 16 deletions include/BeadingStrategy/BeadingStrategyFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ namespace cura
class BeadingStrategyFactory
{
public:
static BeadingStrategyPtr makeStrategy
(
const coord_t preferred_bead_width_outer = MM2INT(0.5),
const coord_t preferred_bead_width_inner = MM2INT(0.5),
const coord_t preferred_transition_length = MM2INT(0.4),
const float transitioning_angle = M_PI / 4.0,
const bool print_thin_walls = false,
const coord_t min_bead_width = 0,
const coord_t min_feature_size = 0,
const Ratio wall_split_middle_threshold = 0.5_r,
const Ratio wall_add_middle_threshold = 0.5_r,
const coord_t max_bead_count = 0,
const coord_t outer_wall_offset = 0,
const int inward_distributed_center_wall_count = 2,
const Ratio minimum_variable_line_ratio = 0.5
);
static BeadingStrategyPtr makeStrategy(const coord_t preferred_bead_width_outer = 0.5_mm,
const coord_t preferred_bead_width_inner = 0.5_mm,
const coord_t preferred_transition_length = 0.4_mm,
const float transitioning_angle = M_PI / 4.0,
const bool print_thin_walls = false,
const coord_t min_bead_width = 0,
const coord_t min_feature_size = 0,
const Ratio wall_split_middle_threshold = 0.5_r,
const Ratio wall_add_middle_threshold = 0.5_r,
const coord_t max_bead_count = 0,
const coord_t outer_wall_offset = 0,
const int inward_distributed_center_wall_count = 2,
const Ratio minimum_variable_line_ratio = 0.5);
};

} // namespace cura
Expand Down
2 changes: 1 addition & 1 deletion include/PathOrderMonotonic.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class PathOrderMonotonic : public PathOrder<PathType>
return a_projection < b_projection;
});
//Create a bucket grid to be able to find adjacent lines quickly.
SparsePointGridInclusive<Path*> line_bucket_grid(MM2INT(2)); //Grid size of 2mm.
SparsePointGridInclusive<Path*> line_bucket_grid(2_mm); // Grid size of 2mm.
for(Path* polyline : polylines)
{
if(polyline->converted->empty())
Expand Down
2 changes: 1 addition & 1 deletion include/PathOrderOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ class PathOrderOptimizer

if(combing_grid == nullptr)
{
constexpr coord_t grid_size = 2000; //2mm grid cells. Smaller will use more memory, but reduce chance of unnecessary collision checks.
constexpr coord_t grid_size = 2_mm; // 2mm grid cells. Smaller will use more memory, but reduce chance of unnecessary collision checks.
combing_grid = PolygonUtils::createLocToLineGrid(*combing_boundary, grid_size);
}

Expand Down
4 changes: 2 additions & 2 deletions include/SkeletalTrapezoidation.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class SkeletalTrapezoidation
coord_t transition_filter_dist; //!< Filter transition mids (i.e. anchors) closer together than this
coord_t allowed_filter_deviation; //!< The allowed line width deviation induced by filtering
coord_t beading_propagation_transition_dist; //!< When there are different beadings propagated from below and from above, use this transitioning distance
static constexpr coord_t central_filter_dist = 20; //!< Filter areas marked as 'central' smaller than this
static constexpr coord_t snap_dist = 20; //!< Generic arithmatic inaccuracy. Only used to determine whether a transition really needs to insert an extra edge.
static constexpr coord_t central_filter_dist = 2 * INT_EPSILON; //!< Filter areas marked as 'central' smaller than this
static constexpr coord_t snap_dist = 2 * INT_EPSILON; //!< Generic arithmatic inaccuracy. Only used to determine whether a transition really needs to insert an extra edge.

/*!
* The strategy to use to fill a certain shape with lines.
Expand Down
2 changes: 1 addition & 1 deletion include/infill/LightningTreeNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace cura
{

constexpr coord_t locator_cell_size = 4000;
constexpr coord_t locator_cell_size = 4_mm;

class LightningTreeNode;

Expand Down
5 changes: 3 additions & 2 deletions include/pathPlanning/Comb.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ class Comb
const coord_t offset_from_inside_to_outside; //!< The sum of the offsets for the inside and outside boundary Comb::offset_from_outlines and Comb::offset_from_outlines_outside
const coord_t max_crossing_dist2; //!< The maximal distance by which to cross the in_between area between inside and outside
static const coord_t max_moveOutside_distance2 = std::numeric_limits<coord_t>::max(); //!< Any point which is not inside should be considered outside.
static constexpr coord_t offset_dist_to_get_from_on_the_polygon_to_outside = 40; //!< in order to prevent on-boundary vs crossing boundary confusions (precision thing)
static constexpr coord_t offset_extra_start_end = 100; //!< Distance to move start point and end point toward eachother to extra avoid collision with the boundaries.
static constexpr coord_t offset_dist_to_get_from_on_the_polygon_to_outside = 4 * INT_EPSILON; //!< in order to prevent on-boundary vs crossing boundary confusions (precision thing)
static constexpr coord_t offset_extra_start_end = 100_mu; //!< Distance to move start point and end point toward eachother to extra avoid collision with the boundaries.
// FIXME: ^ Magic constant (how is it derived? should it scale with units?)

Polygons boundary_inside_minimum; //!< The boundary within which to comb. (Will be reordered by the partsView_inside_minimum)
Polygons boundary_inside_optimal; //!< The boundary within which to comb. (Will be reordered by the partsView_inside_optimal)
Expand Down
2 changes: 1 addition & 1 deletion include/settings/AdaptiveLayerHeights.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AdaptiveLayer
{
public:
/*!
* Height of the layer in microns.
* Height of the layer in INT_PER_MM units.
*/
coord_t layer_height;

Expand Down
2 changes: 1 addition & 1 deletion include/slicer.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class SlicerLayer

/*!
* Connecting polygons that are not closed yet, as models are not always perfect manifold we need to join some stuff up to get proper polygons.
* First link up polygon ends that are within 2 microns.
* First link up polygon ends that are within 2 INT_PER_MM units.
*
* Clears all open polylines which are used up in the process
*
Expand Down
100 changes: 92 additions & 8 deletions include/utils/Coord_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,100 @@ namespace cura

using coord_t = ClipperLib::cInt;

static inline coord_t operator "" _mu(unsigned long long i) { return i; };
template<typename T>
constexpr T ipow(T x, unsigned power)
{
return power != 0 ? x * ipow(x, power - 1) : T(1);
}

static constexpr unsigned INT10POW_PER_MM = 3;
static constexpr coord_t INT_PER_MM = ipow(10, INT10POW_PER_MM);
static constexpr coord_t INT_PER_MM2 = INT_PER_MM * INT_PER_MM;
static constexpr coord_t INT_PER_MM3 = INT_PER_MM2 * INT_PER_MM;

// Minimum distance for snapping, tolerance for arcs, etc.
static constexpr coord_t INT_EPSILON = 10;
// The lenght of unit vectors for normal() and rotation computation, such that the rounding errors are negligible.
static constexpr coord_t INT_PRECISION_COMP = 10000;

#define INT2MM(n) (static_cast<double>(n) / 1000.0)
#define INT2MM2(n) (static_cast<double>(n) / 1000000.0)
#define MM2INT(n) (static_cast<coord_t>((n) * 1000 + 0.5 * (((n) > 0) - ((n) < 0))))
#define MM2_2INT(n) (static_cast<coord_t>((n) * 1000000 + 0.5 * (((n) > 0) - ((n) < 0))))
#define MM3_2INT(n) (static_cast<coord_t>((n) * 1000000000 + 0.5 * (((n) > 0) - ((n) < 0))))
constexpr double coord_to_mm(coord_t n)
{
return static_cast<double>(n) / INT_PER_MM;
}
constexpr double coord_to_mm2(coord_t n)
{
return static_cast<double>(n) / INT_PER_MM2;
}
constexpr double coord_to_mm3(coord_t n)
{
return static_cast<double>(n) / INT_PER_MM3;
}
constexpr coord_t mm_to_coord(double n)
{
return static_cast<coord_t>(n * INT_PER_MM + (n >= 0. ? 0.5 : -0.5));
}
constexpr coord_t mm2_to_coord(double n)
{
return static_cast<coord_t>(n * INT_PER_MM2 + (n >= 0. ? 0.5 : -0.5));
}
constexpr coord_t mm3_to_coord(double n)
{
return static_cast<coord_t>(n * INT_PER_MM3 + (n >= 0. ? 0.5 : -0.5));
}
constexpr coord_t mu2_to_coord(double n)
{
return static_cast<coord_t>(n * (INT_PER_MM2 / 1000000.) + (n >= 0. ? 0.5 : -0.5));
}

#define INT2MICRON(n) ((n) / 1)
#define MICRON2INT(n) ((n) * 1)
constexpr coord_t operator"" _mm(long double n)
{
return mm_to_coord(n);
}
constexpr coord_t operator"" _mm(unsigned long long int n)
{
return mm_to_coord(n);
}
constexpr coord_t operator"" _mm2(long double n)
{
return mm2_to_coord(n);
}
constexpr coord_t operator"" _mm2(unsigned long long int n)
{
return mm2_to_coord(n);
}
constexpr coord_t operator"" _mm3(long double n)
{
return mm3_to_coord(n);
}
constexpr coord_t operator"" _mm3(unsigned long long int n)
{
return mm3_to_coord(n);
}

constexpr coord_t operator"" _mu(long double n)
{
return mm_to_coord(n * 1e-3);
}
constexpr coord_t operator"" _mu(unsigned long long int n)
{
return mm_to_coord(n * 1e-3);
}
constexpr coord_t operator"" _mu2(long double n)
{
return mm2_to_coord(n * 1e-6);
}
constexpr coord_t operator"" _mu2(unsigned long long int n)
{
return mm2_to_coord(n * 1e-6);
}
constexpr coord_t operator"" _mu3(long double n)
{
return mm3_to_coord(n * 1e-9);
}
constexpr coord_t operator"" _mu3(unsigned long long int n)
{
return mm3_to_coord(n * 1e-9);
}

} // namespace cura

Expand Down
6 changes: 3 additions & 3 deletions include/utils/IntPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define UTILS_INT_POINT_H

/**
The integer point classes are used as soon as possible and represent microns in 2D or 3D space.
The integer point classes are used as soon as possible and represent in 2D or 3D space with INT_PER_MM units.
Integer points are used to avoid floating point rounding errors, and because ClipperLib uses them.
*/
#define INLINE static inline
Expand Down Expand Up @@ -107,8 +107,8 @@ INLINE coord_t vSize(const Point& p0)

INLINE double vSizeMM(const Point& p0)
{
double fx = INT2MM(p0.X);
double fy = INT2MM(p0.Y);
double fx = coord_to_mm(p0.X);
double fy = coord_to_mm(p0.Y);
return sqrt(fx*fx+fy*fy);
}

Expand Down
6 changes: 3 additions & 3 deletions include/utils/Point3.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ class Point3

double vSizeMM() const
{
double fx = INT2MM(x);
double fy = INT2MM(y);
double fz = INT2MM(z);
double fx = coord_to_mm(x);
double fy = coord_to_mm(y);
double fz = coord_to_mm(z);
return sqrt(fx*fx+fy*fy+fz*fz);
}

Expand Down
2 changes: 1 addition & 1 deletion include/utils/PolylineStitcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PolylineStitcher
* \param snap_distance Points closer than this distance are considered to
* be the same point.
*/
static void stitch(const Paths& lines, Paths& result_lines, Paths& result_polygons, coord_t max_stitch_distance = MM2INT(0.1), coord_t snap_distance = 10)
static void stitch(const Paths& lines, Paths& result_lines, Paths& result_polygons, coord_t max_stitch_distance = 0.1_mm, coord_t snap_distance = INT_EPSILON)
{
if (lines.empty())
{
Expand Down
3 changes: 1 addition & 2 deletions include/utils/SVG.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ class SVG : NoCopy
* \param stroke_width The width of the grid lines.
* \param font_size The size of the font to write the coordinates with.
*/
void writeCoordinateGrid(const coord_t grid_size = MM2INT(1), const Color color = Color::BLACK, const float stroke_width = 0.1, const float font_size = 10) const;

void writeCoordinateGrid(const coord_t grid_size = 1_mm, const Color color = Color::BLACK, const float stroke_width = 0.1, const float font_size = 10) const;
};

template<typename... Args>
Expand Down
2 changes: 1 addition & 1 deletion include/utils/Simplify.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Simplify
* If a vertex causes deviation of less than this, it should always be
* removed.
*/
constexpr static coord_t min_resolution = 5; //5 units, regardless of how big those are, to allow for rounding errors.
constexpr static coord_t min_resolution = INT_EPSILON / 2; // 5 units, regardless of how big those are, to allow for rounding errors.

/*!
* The main simplification algorithm starts here.
Expand Down
6 changes: 4 additions & 2 deletions include/utils/floatpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class FPoint3
float x,y,z;
FPoint3() {}
FPoint3(float _x, float _y, float _z): x(_x), y(_y), z(_z) {}
FPoint3(const Point3& p): x(p.x*.001), y(p.y*.001), z(p.z*.001) {}
FPoint3(const Point3& p) : x(coord_to_mm(p.x)), y(coord_to_mm(p.y)), z(coord_to_mm(p.z))
{
}

FPoint3 operator+(const FPoint3& p) const { return FPoint3(x+p.x, y+p.y, z+p.z); }
FPoint3 operator-(const FPoint3& p) const { return FPoint3(x-p.x, y-p.y, z-p.z); }
Expand Down Expand Up @@ -83,7 +85,7 @@ class FPoint3

Point3 toPoint3()
{
return Point3(MM2INT(x), MM2INT(y), MM2INT(z));
return Point3(mm_to_coord(x), mm_to_coord(y), mm_to_coord(z));
}
};

Expand Down
25 changes: 16 additions & 9 deletions include/utils/polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class ListPolyIt;
typedef std::list<Point> ListPolygon; //!< A polygon represented by a linked list instead of a vector
typedef std::vector<ListPolygon> ListPolygons; //!< Polygons represented by a vector of linked lists instead of a vector of vectors

const static int clipper_init = (0);
// How far a miter can be offseted before being truncated, relative to the offset size (http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Classes/ClipperOffset/Properties/MiterLimit.htm)
static constexpr double clipper_miter_limit = 1.2;
static constexpr int clipper_init = 0;
#define NO_INDEX (std::numeric_limits<unsigned int>::max())

class ConstPolygonPointer;
Expand Down Expand Up @@ -162,7 +164,7 @@ class ConstPolygonRef
return ClipperLib::Orientation(*path);
}

Polygons offset(int distance, ClipperLib::JoinType joinType = ClipperLib::jtMiter, double miter_limit = 1.2) const;
Polygons offset(int distance, ClipperLib::JoinType joinType = ClipperLib::jtMiter, double miter_limit = clipper_miter_limit) const;

coord_t polygonLength() const
{
Expand Down Expand Up @@ -394,6 +396,11 @@ class ConstPolygonRef
* \param[in,out] backward_is_too_far Whether trying another step backward is blocked by the shortcut length condition. Updated for the next iteration.
*/
static void smooth_outward_step(const Point p1, const int64_t shortcut_length2, ListPolyIt& p0_it, ListPolyIt& p2_it, bool& forward_is_blocked, bool& backward_is_blocked, bool& forward_is_too_far, bool& backward_is_too_far);


/// Allowed deviation for round joints while offsetting (this reduces the number of segment
/// @see http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Classes/ClipperOffset/Properties/ArcTolerance.htm
static double calcArcTolerance(int offset);
};


Expand Down Expand Up @@ -551,19 +558,20 @@ class PolygonRef : public ConstPolygonRef
* \param smallest_line_segment_squared maximal squared length of removed line segments
* \param allowed_error_distance_squared The square of the distance of the middle point to the line segment of the consecutive and previous point for which the middle point is removed
*/
void simplify(const coord_t smallest_line_segment_squared = MM2INT(0.01) * MM2INT(0.01), const coord_t allowed_error_distance_squared = 25);
void simplify(const coord_t smallest_line_segment_squared = 100_mu2, const coord_t allowed_error_distance_squared = 25_mu2);

/*!
* See simplify(.)
*/
void simplifyPolyline(const coord_t smallest_line_segment_squared = 100, const coord_t allowed_error_distance_squared = 25);
void simplifyPolyline(const coord_t smallest_line_segment_squared = 100_mu2, const coord_t allowed_error_distance_squared = 25_mu2);

protected:
/*!
* Private implementation for both simplify and simplifyPolygons.
*
* Made private to avoid accidental use of the wrong function.
*/
void _simplify(const coord_t smallest_line_segment_squared = 100, const coord_t allowed_error_distance_squared = 25, bool processing_polylines = false);
void _simplify(const coord_t smallest_line_segment_squared = 100_mu2, const coord_t allowed_error_distance_squared = 25_mu2, bool processing_polylines = false);

public:
void pop_back()
Expand Down Expand Up @@ -1003,16 +1011,15 @@ class Polygons
return ret;
}

Polygons offset(int distance, ClipperLib::JoinType joinType = ClipperLib::jtMiter, double miter_limit = 1.2) const;
Polygons offset(int distance, ClipperLib::JoinType joinType = ClipperLib::jtMiter, double miter_limit = clipper_miter_limit) const;

Polygons offsetPolyLine(int distance, ClipperLib::JoinType joinType = ClipperLib::jtMiter) const
{
Polygons ret;
double miterLimit = 1.2;
ClipperLib::EndType end_type = (joinType == ClipperLib::jtMiter)? ClipperLib::etOpenSquare : ClipperLib::etOpenRound;
ClipperLib::ClipperOffset clipper(miterLimit, 10.0);
ClipperLib::ClipperOffset clipper(clipper_miter_limit, ConstPolygonRef::calcArcTolerance(distance));
clipper.AddPaths(paths, joinType, end_type);
clipper.MiterLimit = miterLimit;
clipper.MiterLimit = clipper_miter_limit;
clipper.Execute(ret.paths, distance);
return ret;
}
Expand Down
Loading