-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1232 from wawuwo/more-wire-forms
Add two new wire forms
- Loading branch information
Showing
6 changed files
with
196 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include "wire_planner.h" | ||
|
||
namespace qucs_s { | ||
namespace wire { | ||
|
||
inline bool is_horizontal_or_vertical(const QPoint from, const QPoint to) { | ||
return from.x() == to.x() || from.y() == to.y(); | ||
} | ||
|
||
// TODO: migrate to C++20 and yonger and use std::midpoint instead | ||
int midpoint(int a, int b) { | ||
return a + (b - a) / 2; | ||
} | ||
|
||
|
||
std::vector<QPoint> straight(const QPoint from, const QPoint to) { | ||
return {from, to}; | ||
} | ||
|
||
std::vector<QPoint> two_step_xy(const QPoint from, const QPoint to) { | ||
if (is_horizontal_or_vertical(from, to)) { | ||
return {from, to}; | ||
}; | ||
|
||
/* | ||
From o---+ | ||
| | ||
+---o To | ||
*/ | ||
return {from, {to.x(), from.y()}, to}; | ||
} | ||
|
||
std::vector<QPoint> two_step_yx(const QPoint from, const QPoint to) { | ||
if (is_horizontal_or_vertical(from, to)) { | ||
return {from, to}; | ||
}; | ||
|
||
/* | ||
From o | ||
| | ||
+---o To | ||
*/ | ||
return {from, {from.x(), to.y()}, to}; | ||
} | ||
|
||
std::vector<QPoint> three_step_xy(const QPoint from, const QPoint to) { | ||
if (is_horizontal_or_vertical(from, to)) { | ||
return {from, to}; | ||
}; | ||
|
||
/* | ||
From o---+ | ||
| | ||
+---o To | ||
*/ | ||
int mid_x = midpoint(from.x(), to.x()); | ||
return {from, {mid_x, from.y()}, {mid_x, to.y()}, to}; | ||
} | ||
|
||
std::vector<QPoint> three_step_yx(const QPoint from, const QPoint to) { | ||
if (is_horizontal_or_vertical(from, to)) { | ||
return {from, to}; | ||
}; | ||
|
||
/* | ||
o From | ||
| | ||
+---+ | ||
| | ||
o To | ||
*/ | ||
int mid_y = midpoint(from.y(), to.y()); | ||
return {from, {from.x(), mid_y}, {to.x(), mid_y}, to}; | ||
} | ||
|
||
|
||
static const std::map<Planner::PlanType, Planner::RouterFunc> routers = { | ||
{Planner::PlanType::TwoStepXY, two_step_xy}, | ||
{Planner::PlanType::TwoStepYX, two_step_yx}, | ||
{Planner::PlanType::ThreeStepXY,three_step_xy}, | ||
{Planner::PlanType::ThreeStepYX, three_step_yx} | ||
}; | ||
|
||
|
||
Planner::Planner() : current{routers.begin()} {} | ||
|
||
std::vector<QPoint> Planner::plan(PlanType type, const QPoint from, const QPoint to) { | ||
return routers.at(type)(from, to); | ||
} | ||
|
||
std::vector<QPoint> Planner::plan(const QPoint from, const QPoint to) const { | ||
return current->second(from, to); | ||
} | ||
|
||
void Planner::next() { | ||
++current; | ||
|
||
if (current != routers.cend()) { | ||
return; | ||
} | ||
|
||
current = routers.cbegin(); | ||
} | ||
|
||
|
||
} // namespace wire | ||
} // namespace qucs_s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef WIRE_PLANNER_H | ||
#define WIRE_PLANNER_H | ||
|
||
#include <QPoint> | ||
#include <functional> | ||
#include <map> | ||
|
||
namespace qucs_s { | ||
namespace wire { | ||
|
||
class Planner { | ||
public: | ||
using RouterFunc = std::function<std::vector<QPoint>(QPoint, QPoint)>; | ||
enum class PlanType { TwoStepXY, TwoStepYX, ThreeStepXY, ThreeStepYX }; | ||
|
||
Planner(); | ||
static std::vector<QPoint> plan(PlanType type, const QPoint from, const QPoint to); | ||
std::vector<QPoint> plan(const QPoint from, const QPoint to) const; | ||
void next(); | ||
private: | ||
std::map<PlanType, RouterFunc>::const_iterator current; | ||
}; | ||
|
||
} // namespace wire | ||
} // namespace qucs_s | ||
|
||
#endif |