Code refactoring for last commit

This commit is contained in:
YuSanka 2020-08-19 15:35:58 +02:00
parent 15285a68a0
commit 8af49d7d87
3 changed files with 167 additions and 255 deletions

View file

@ -17,8 +17,8 @@ class ConfigOptionsGroup;
using ConfigOptionsGroupShp = std::shared_ptr<ConfigOptionsGroup>;
struct BedShape {
struct BedShape
{
enum class Type {
Rectangular = 0,
Circular,
@ -32,83 +32,24 @@ struct BedShape {
Diameter
};
BedShape(const ConfigOptionPoints& points) {
auto polygon = Polygon::new_scale(points.values);
BedShape(const ConfigOptionPoints& points);
// is this a rectangle ?
if (points.size() == 4) {
auto lines = polygon.lines();
if (lines[0].parallel_to(lines[2]) && lines[1].parallel_to(lines[3])) {
// okay, it's a rectangle
// find origin
coordf_t x_min, x_max, y_min, y_max;
x_max = x_min = points.values[0](0);
y_max = y_min = points.values[0](1);
for (auto pt : points.values)
{
x_min = std::min(x_min, pt(0));
x_max = std::max(x_max, pt(0));
y_min = std::min(y_min, pt(1));
y_max = std::max(y_max, pt(1));
}
type = Type::Rectangular;
rectSize = Vec2d(x_max - x_min, y_max - y_min);
rectOrigin = Vec2d(-x_min, -y_min);
return;
}
}
// is this a circle ?
{
// Analyze the array of points.Do they reside on a circle ?
auto center = polygon.bounding_box().center();
std::vector<double> vertex_distances;
double avg_dist = 0;
for (auto pt : polygon.points)
{
double distance = (pt - center).cast<double>().norm();
vertex_distances.push_back(distance);
avg_dist += distance;
}
avg_dist /= vertex_distances.size();
bool defined_value = true;
for (auto el : vertex_distances)
{
if (abs(el - avg_dist) > 10 * SCALED_EPSILON)
defined_value = false;
break;
}
if (defined_value) {
// all vertices are equidistant to center
type = Type::Circular;
diameter = unscale<double>(avg_dist * 2);
return;
}
}
if (points.size() < 3) {
type = Type::Invalid;
return;
}
// This is a custom bed shape, use the polygon provided.
type = Type::Custom;
}
bool is_custom() { return m_type == Type::Custom; }
static void append_option_line(ConfigOptionsGroupShp optgroup, Parameter param);
static wxString get_name(Type type);
// convert Type to size_t
size_t get_type();
wxString get_full_name_with_params();
void apply_optgroup_values(ConfigOptionsGroupShp optgroup);
Type type = Type::Invalid;
Vec2d rectSize;
Vec2d rectOrigin;
double diameter;
private:
Type m_type {Type::Invalid};
Vec2d m_rectSize {200, 200};
Vec2d m_rectOrigin {0, 0};
double m_diameter {0};
};
class BedShapePanel : public wxPanel
@ -119,7 +60,6 @@ class BedShapePanel : public wxPanel
Bed_2D* m_canvas;
std::vector<Vec2d> m_shape;
std::vector<Vec2d> m_loaded_shape;
std::string m_custom_shape;
std::string m_custom_texture;
std::string m_custom_model;