Skip to content

Commit

Permalink
tool drag polygon edge: properly handle arc edges
Browse files Browse the repository at this point in the history
  • Loading branch information
carrotIndustries committed Nov 18, 2021
1 parent eb8ef54 commit d2158ab
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/core/tools/tool_drag_polygon_edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void ToolDragPolygonEdge::update_tip()
ToolDragPolygonEdge::PolyInfo::PolyInfo(const Polygon &poly, int edge)
{
auto get_pos = [&poly](int x) { return poly.get_vertex(x).position; };
arc_center_orig = poly.get_vertex(edge).arc_center;
pos_from_orig = get_pos(edge);
pos_from2 = get_pos(edge - 1);
pos_to_orig = get_pos(edge + 1);
Expand Down Expand Up @@ -61,6 +62,9 @@ ToolResponse ToolDragPolygonEdge::update(const ToolArgs &args)
const auto pos = poly_info->get_pos(args.coords - pos_orig);
poly->get_vertex(edge).position = pos.from;
poly->get_vertex(edge + 1).position = pos.to;
if (poly->get_vertex(edge).type == Polygon::Vertex::Type::ARC) {
poly->get_vertex(edge).arc_center = poly_info->arc_center_orig + pos.arc_center;
}
}
else if (args.type == ToolEventType::ACTION) {
switch (args.action) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/tools/tool_drag_polygon_edge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class ToolDragPolygonEdge : public virtual ToolBase, public ToolHelperPlane {
class PolyInfo : public KeepSlopeInfo {
public:
PolyInfo(const class Polygon &poly, int edge);

Coordi arc_center_orig;
};
std::optional<PolyInfo> poly_info;

Expand Down
5 changes: 4 additions & 1 deletion src/util/keep_slope_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ KeepSlopeInfo::Position KeepSlopeInfo::get_pos(const Coordd &shift) const
const Coordd vtr = pos_to_orig - pos_from_orig;
const Coordd vtrn = Coordd(vtr.y, -vtr.x).normalize();

const auto cos_alpha = vto.normalize().dot(vfrom.normalize());
const auto n = 2. / (1 + cos_alpha); // 1/cos(α/2)²

// shift projected onto vector perpendicular to track
const Coordd vshift2 = vtrn * vtrn.dot(shift);

Expand All @@ -18,6 +21,6 @@ KeepSlopeInfo::Position KeepSlopeInfo::get_pos(const Coordd &shift) const
shift_to = {0, 0};
}

return {pos_from_orig + shift_from.to_coordi(), pos_to_orig + shift_to.to_coordi()};
return {pos_from_orig + shift_from.to_coordi(), pos_to_orig + shift_to.to_coordi(), (vshift2 * n).to_coordi()};
}
} // namespace horizon
1 change: 1 addition & 0 deletions src/util/keep_slope_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class KeepSlopeInfo {
struct Position {
Coordi from;
Coordi to;
Coordi arc_center;
};
Position get_pos(const Coordd &shift) const;

Expand Down

0 comments on commit d2158ab

Please sign in to comment.