Measuring: refactoring

This commit is contained in:
Lukas Matena 2022-08-18 13:42:26 +02:00
parent 2f5032ab02
commit 72d7cdd5f8
3 changed files with 180 additions and 145 deletions

View File

@ -34,24 +34,21 @@ public:
struct PlaneData { struct PlaneData {
std::vector<int> facets; std::vector<int> facets;
std::vector<std::vector<Vec3d>> borders; // FIXME: should be in fact local in update_planes() std::vector<std::vector<Vec3d>> borders; // FIXME: should be in fact local in update_planes()
std::vector<std::unique_ptr<SurfaceFeature>> surface_features; std::vector<SurfaceFeature> surface_features;
Vec3d normal; Vec3d normal;
float area; float area;
}; };
const std::vector<const SurfaceFeature*>& get_features() const; std::vector<SurfaceFeature> get_all_features() const;
const SurfaceFeature* get_feature(size_t face_idx, const Vec3d& point) const; std::optional<SurfaceFeature> get_feature(size_t face_idx, const Vec3d& point) const;
const std::vector<std::vector<int>> get_planes_triangle_indices() const; std::vector<std::vector<int>> get_planes_triangle_indices() const;
private: private:
void update_planes(); void update_planes();
void extract_features(); void extract_features();
void save_features();
std::vector<PlaneData> m_planes; std::vector<PlaneData> m_planes;
std::vector<size_t> m_face_to_plane; std::vector<size_t> m_face_to_plane;
std::vector<const SurfaceFeature*> m_features;
const indexed_triangle_set& m_its; const indexed_triangle_set& m_its;
}; };
@ -65,7 +62,6 @@ MeasuringImpl::MeasuringImpl(const indexed_triangle_set& its)
{ {
update_planes(); update_planes();
extract_features(); extract_features();
save_features();
} }
@ -233,7 +229,7 @@ void MeasuringImpl::extract_features()
bool circle = false; bool circle = false;
std::vector<std::unique_ptr<SurfaceFeature>> circles; std::vector<SurfaceFeature> circles;
std::vector<std::pair<size_t, size_t>> circles_idxs; std::vector<std::pair<size_t, size_t>> circles_idxs;
for (int i=1; i<angles.size(); ++i) { for (int i=1; i<angles.size(); ++i) {
if (Slic3r::is_approx(lengths[i], lengths[i-1]) if (Slic3r::is_approx(lengths[i], lengths[i-1])
@ -249,8 +245,7 @@ void MeasuringImpl::extract_features()
// Add the circle and remember indices into borders. // Add the circle and remember indices into borders.
const auto& [center, radius] = get_center_and_radius(border, start_idx, i, trafo); const auto& [center, radius] = get_center_and_radius(border, start_idx, i, trafo);
circles_idxs.emplace_back(start_idx, i); circles_idxs.emplace_back(start_idx, i);
circles.emplace_back(std::unique_ptr<SurfaceFeature>( circles.emplace_back(SurfaceFeature(SurfaceFeatureType::Circle, center, plane.normal, std::optional<Vec3d>(), radius));
new Circle(center, radius, plane.normal)));
circle = false; circle = false;
} }
} }
@ -266,10 +261,10 @@ void MeasuringImpl::extract_features()
double angle = angles[circles_idxs[i].first + 1]; double angle = angles[circles_idxs[i].first + 1];
if (angle > polygon_lower_threshold) { if (angle > polygon_lower_threshold) {
if (angle < polygon_upper_threshold) { if (angle < polygon_upper_threshold) {
const Vec3d center = static_cast<const Circle*>(circles[i].get())->get_center(); const Vec3d center = std::get<0>(circles[i].get_circle());
for (int j=circles_idxs[i].first + 1; j<=circles_idxs[i].second; ++j) for (int j=circles_idxs[i].first + 1; j<=circles_idxs[i].second; ++j)
plane.surface_features.emplace_back(std::unique_ptr<SurfaceFeature>( plane.surface_features.emplace_back(SurfaceFeature(SurfaceFeatureType::Edge,
new Edge(border[j-1], border[j], center))); border[j-1], border[j], std::make_optional(center), 0.));
} else { } else {
// This will be handled just like a regular edge. // This will be handled just like a regular edge.
circles_idxs.erase(circles_idxs.begin() + i); circles_idxs.erase(circles_idxs.begin() + i);
@ -288,8 +283,8 @@ void MeasuringImpl::extract_features()
for (int i=1; i<int(border.size()); ++i) { for (int i=1; i<int(border.size()); ++i) {
if (cidx < circles_idxs.size() && i > circles_idxs[cidx].first) if (cidx < circles_idxs.size() && i > circles_idxs[cidx].first)
i = circles_idxs[cidx++].second; i = circles_idxs[cidx++].second;
else plane.surface_features.emplace_back(std::unique_ptr<SurfaceFeature>( else plane.surface_features.emplace_back(SurfaceFeature(
new Edge(border[i-1], border[i]))); SurfaceFeatureType::Edge, border[i-1], border[i], std::optional<Vec3d>(), 0.));
} }
// FIXME Throw away / do not create edges which are parts of circles or // FIXME Throw away / do not create edges which are parts of circles or
@ -298,14 +293,16 @@ void MeasuringImpl::extract_features()
// FIXME Check and merge first and last circle if needed. // FIXME Check and merge first and last circle if needed.
// Now move the circles into the feature list. // Now move the circles into the feature list.
assert(std::all_of(circles.begin(), circles.end(), [](const std::unique_ptr<SurfaceFeature>& f) { return f->get_type() == SurfaceFeatureType::Circle; })); assert(std::all_of(circles.begin(), circles.end(), [](const SurfaceFeature& f) {
return f.get_type() == SurfaceFeatureType::Circle;
}));
plane.surface_features.insert(plane.surface_features.end(), std::make_move_iterator(circles.begin()), plane.surface_features.insert(plane.surface_features.end(), std::make_move_iterator(circles.begin()),
std::make_move_iterator(circles.end())); std::make_move_iterator(circles.end()));
} }
// The last surface feature is the plane itself. // The last surface feature is the plane itself.
plane.surface_features.emplace_back(std::unique_ptr<SurfaceFeature>( plane.surface_features.emplace_back(SurfaceFeature(SurfaceFeatureType::Plane,
new Plane(i))); Vec3d::Zero(), Vec3d::Zero(), std::optional<Vec3d>(), i + 0.0001));
plane.borders.clear(); plane.borders.clear();
plane.borders.shrink_to_fit(); plane.borders.shrink_to_fit();
@ -314,56 +311,58 @@ void MeasuringImpl::extract_features()
void MeasuringImpl::save_features() std::vector<SurfaceFeature> MeasuringImpl::get_all_features() const
{ {
m_features.clear(); std::vector<SurfaceFeature> features;
for (PlaneData& plane : m_planes)
//PlaneData& plane = m_planes[0]; //PlaneData& plane = m_planes[0];
{ for (const PlaneData& plane : m_planes)
for (const std::unique_ptr<SurfaceFeature>& feature : plane.surface_features) { for (const SurfaceFeature& feature : plane.surface_features)
m_features.emplace_back(feature.get()); features.emplace_back(feature);
} return features;
}
} }
const SurfaceFeature* MeasuringImpl::get_feature(size_t face_idx, const Vec3d& point) const
std::optional<SurfaceFeature> MeasuringImpl::get_feature(size_t face_idx, const Vec3d& point) const
{ {
if (face_idx >= m_face_to_plane.size()) if (face_idx >= m_face_to_plane.size())
return nullptr; return std::optional<SurfaceFeature>();
const PlaneData& plane = m_planes[m_face_to_plane[face_idx]]; const PlaneData& plane = m_planes[m_face_to_plane[face_idx]];
const SurfaceFeature* closest_feature = nullptr; const SurfaceFeature* closest_feature = nullptr;
double min_dist = std::numeric_limits<double>::max(); double min_dist = std::numeric_limits<double>::max();
for (const std::unique_ptr<SurfaceFeature>& feature : plane.surface_features) { MeasurementResult res;
double dist = Measuring::get_distance(feature.get(), &point); SurfaceFeature point_sf(point);
if (dist < 0.5 && dist < min_dist) {
min_dist = std::min(dist, min_dist); for (const SurfaceFeature& feature : plane.surface_features) {
closest_feature = feature.get(); res = get_measurement(feature, point_sf);
if (res.distance_strict) { // TODO: this should become an assert after all combinations are implemented.
double dist = *res.distance_strict;
if (dist < 0.5 && dist < min_dist) {
min_dist = std::min(dist, min_dist);
closest_feature = &feature;
}
} }
} }
if (closest_feature) if (closest_feature)
return closest_feature; return std::make_optional(*closest_feature);
// Nothing detected, return the plane as a whole. // Nothing detected, return the plane as a whole.
assert(plane.surface_features.back().get()->get_type() == SurfaceFeatureType::Plane); assert(plane.surface_features.back().get_type() == SurfaceFeatureType::Plane);
return plane.surface_features.back().get(); return std::make_optional(plane.surface_features.back());
} }
const std::vector<const SurfaceFeature*>& MeasuringImpl::get_features() const
{
return m_features;
}
std::vector<std::vector<int>> MeasuringImpl::get_planes_triangle_indices() const
const std::vector<std::vector<int>> MeasuringImpl::get_planes_triangle_indices() const
{ {
std::vector<std::vector<int>> out; std::vector<std::vector<int>> out;
for (const PlaneData& plane : m_planes) for (const PlaneData& plane : m_planes)
@ -390,45 +389,81 @@ Measuring::Measuring(const indexed_triangle_set& its)
Measuring::~Measuring() {} Measuring::~Measuring() {}
const std::vector<const SurfaceFeature*>& Measuring::get_features() const std::vector<SurfaceFeature> Measuring::get_all_features() const
{ {
return priv->get_features(); return priv->get_all_features();
} }
const SurfaceFeature* Measuring::get_feature(size_t face_idx, const Vec3d& point) const std::optional<SurfaceFeature> Measuring::get_feature(size_t face_idx, const Vec3d& point) const
{ {
return priv->get_feature(face_idx, point); return priv->get_feature(face_idx, point);
} }
const std::vector<std::vector<int>> Measuring::get_planes_triangle_indices() const std::vector<std::vector<int>> Measuring::get_planes_triangle_indices() const
{ {
return priv->get_planes_triangle_indices(); return priv->get_planes_triangle_indices();
} }
double Measuring::get_distance(const SurfaceFeature* feature, const Vec3d* pt)
MeasurementResult get_measurement(const SurfaceFeature& a, const SurfaceFeature& b)
{ {
if (feature->get_type() == SurfaceFeatureType::Edge) { assert(a.get_type() != SurfaceFeatureType::Undef && b.get_type() != SurfaceFeatureType::Undef);
const Edge* edge = static_cast<const Edge*>(feature);
const auto& [s,e] = edge->get_edge(); const bool swap = int(a.get_type()) > int(b.get_type());
Eigen::ParametrizedLine<double, 3> line(s, (e-s).normalized()); const SurfaceFeature& f1 = swap ? b : a;
return line.distance(*pt); const SurfaceFeature& f2 = swap ? a : b;
}
else if (feature->get_type() == SurfaceFeatureType::Circle) { MeasurementResult result;
const Circle* circle = static_cast<const Circle*>(feature); if (f1.get_type() == SurfaceFeatureType::Point) {
// Find a plane containing normal, center and the point. if (f2.get_type() == SurfaceFeatureType::Point) {
const Vec3d& c = circle->get_center(); Vec3d diff = (f2.get_point() - f1.get_point());
const Vec3d& n = circle->get_normal(); result.distance_strict = diff.norm();
Eigen::Hyperplane<double, 3> circle_plane(n, c); result.distance_xyz = diff;
Vec3d proj = circle_plane.projection(*pt); } else if (f2.get_type() == SurfaceFeatureType::Edge) {
return std::sqrt( std::pow((proj - c).norm() - circle->get_radius(), 2.) + (*pt - proj).squaredNorm()); const auto& [s,e] = f2.get_edge();
Eigen::ParametrizedLine<double, 3> line(s, (e-s).normalized());
result.distance_strict = std::make_optional(line.distance(f1.get_point())); // TODO: this is really infinite dist
} else if (f2.get_type() == SurfaceFeatureType::Circle) {
// Find a plane containing normal, center and the point.
const auto& [c, radius, n] = f2.get_circle();
Eigen::Hyperplane<double, 3> circle_plane(n, c);
Vec3d proj = circle_plane.projection(f1.get_point());
result.distance_strict = std::make_optional(std::sqrt(
std::pow((proj - c).norm() - radius, 2.) + (f1.get_point() - proj).squaredNorm()));
} else if (f2.get_type() == SurfaceFeatureType::Plane) {
}
} else if (f1.get_type() == SurfaceFeatureType::Edge) {
if (f2.get_type() == SurfaceFeatureType::Edge) {
} else if (f2.get_type() == SurfaceFeatureType::Circle) {
} else if (f2.get_type() == SurfaceFeatureType::Plane) {
}
} else if (f1.get_type() == SurfaceFeatureType::Circle) {
if (f2.get_type() == SurfaceFeatureType::Circle) {
} else if (f2.get_type() == SurfaceFeatureType::Plane) {
}
} else if (f1.get_type() == SurfaceFeatureType::Plane) {
assert(f2.get_type() == SurfaceFeatureType::Plane);
} }
return std::numeric_limits<double>::max();
return result;
} }
@ -436,5 +471,8 @@ double Measuring::get_distance(const SurfaceFeature* feature, const Vec3d* pt)
} // namespace Measure } // namespace Measure
} // namespace Slic3r } // namespace Slic3r

View File

@ -1,6 +1,7 @@
#ifndef Slic3r_Measure_hpp_ #ifndef Slic3r_Measure_hpp_
#define Slic3r_Measure_hpp_ #define Slic3r_Measure_hpp_
#include <optional>
#include <memory> #include <memory>
#include "Point.hpp" #include "Point.hpp"
@ -15,55 +16,50 @@ namespace Measure {
enum class SurfaceFeatureType { enum class SurfaceFeatureType {
Edge = 1 << 0, Undef,
Circle = 1 << 1, Point,
Plane = 1 << 2 Edge,
}; Circle,
Plane
};
class SurfaceFeature { class SurfaceFeature {
public:
virtual SurfaceFeatureType get_type() const = 0;
};
class Edge : public SurfaceFeature {
public: public:
Edge(const Vec3d& start, const Vec3d& end) : m_start{start}, m_end{end} {} SurfaceFeature(SurfaceFeatureType type, const Vec3d& pt1, const Vec3d& pt2, std::optional<Vec3d> pt3, double value)
Edge(const Vec3d& start, const Vec3d& end, const Vec3d& pin) : m_start{start}, m_end{end}, : m_type{type}, m_pt1{pt1}, m_pt2{pt2}, m_pt3{pt3}, m_value{value} {}
m_pin{std::unique_ptr<Vec3d>(new Vec3d(pin))} {}
SurfaceFeatureType get_type() const override { return SurfaceFeatureType::Edge; }
std::pair<Vec3d, Vec3d> get_edge() const { return std::make_pair(m_start, m_end); }
const Vec3d* get_point_of_interest() const { return m_pin.get(); }
private:
Vec3d m_start;
Vec3d m_end;
std::unique_ptr<Vec3d> m_pin;
};
class Circle : public SurfaceFeature { explicit SurfaceFeature(const Vec3d& pt)
public: : m_type{SurfaceFeatureType::Point}, m_pt1{pt} {}
Circle(const Vec3d& center, double radius, const Vec3d& normal)
: m_center{center}, m_radius{radius}, m_normal{normal} {}
SurfaceFeatureType get_type() const override { return SurfaceFeatureType::Circle; }
Vec3d get_center() const { return m_center; }
double get_radius() const { return m_radius; }
Vec3d get_normal() const { return m_normal; }
private:
Vec3d m_center;
double m_radius;
Vec3d m_normal;
};
class Plane : public SurfaceFeature {
public: // Get type of this feature.
Plane(int idx) : m_idx(idx) {} SurfaceFeatureType get_type() const { return m_type; }
SurfaceFeatureType get_type() const override { return SurfaceFeatureType::Plane; }
int get_plane_idx() const { return m_idx; } // index into vector provided by Measuring::get_plane_triangle_indices // For points, return the point.
Vec3d get_point() const { assert(m_type == SurfaceFeatureType::Point); return m_pt1; }
// For edges, return start and end.
std::pair<Vec3d, Vec3d> get_edge() const { assert(m_type == SurfaceFeatureType::Edge); return std::make_pair(m_pt1, m_pt2); }
// For circles, return center, radius and normal.
std::tuple<Vec3d, double, Vec3d> get_circle() const { assert(m_type == SurfaceFeatureType::Circle); return std::make_tuple(m_pt1, m_value, m_pt2); }
// For planes, return index into vector provided by Measuring::get_plane_triangle_indices.
int get_plane_idx() const { return int(m_value); }
// For anything, return an extra point that should also be considered a part of this.
std::optional<Vec3d> get_extra_point() const { assert(m_type != SurfaceFeatureType::Undef); return m_pt3; }
private: private:
int m_idx; SurfaceFeatureType m_type = SurfaceFeatureType::Undef;
Vec3d m_pt1;
Vec3d m_pt2;
std::optional<Vec3d> m_pt3;
double m_value;
}; };
class MeasuringImpl; class MeasuringImpl;
@ -75,34 +71,36 @@ public:
~Measuring(); ~Measuring();
// Return a reference to a list of all features identified on the its. // Return a reference to a list of all features identified on the its.
[[deprecated]]const std::vector<const SurfaceFeature*>& get_features() const; // Use only for debugging. Expensive, do not call often.
[[deprecated]] std::vector<SurfaceFeature> get_all_features() const;
// Given a face_idx where the mouse cursor points, return a feature that // Given a face_idx where the mouse cursor points, return a feature that
// should be highlighted or nullptr. // should be highlighted (if any).
const SurfaceFeature* get_feature(size_t face_idx, const Vec3d& point) const; std::optional<SurfaceFeature> get_feature(size_t face_idx, const Vec3d& point) const;
// Returns a list of triangle indices for each identified plane. Each // Returns a list of triangle indices for each identified plane. Each
// Plane object contains an index into this vector. // Plane object contains an index into this vector. Expensive, do not
const std::vector<std::vector<int>> get_planes_triangle_indices() const; // call too often.
std::vector<std::vector<int>> get_planes_triangle_indices() const;
// Returns distance between two SurfaceFeatures.
static double get_distance(const SurfaceFeature* a, const SurfaceFeature* b);
// Returns distance between a SurfaceFeature and a point.
static double get_distance(const SurfaceFeature* a, const Vec3d* pt);
// Returns true if measuring angles between features makes sense.
// If so, result contains the angle in radians.
static bool get_angle(const SurfaceFeature* a, const SurfaceFeature* b, double& result);
private: private:
std::unique_ptr<MeasuringImpl> priv; std::unique_ptr<MeasuringImpl> priv;
}; };
struct MeasurementResult {
std::optional<double> angle;
std::optional<double> distance_infinite;
std::optional<double> distance_strict;
std::optional<Vec3d> distance_xyz;
};
// Returns distance/angle between two SurfaceFeatures.
static MeasurementResult get_measurement(const SurfaceFeature& a, const SurfaceFeature& b);
} // namespace Measure } // namespace Measure
} // namespace Slic3r } // namespace Slic3r

View File

@ -158,24 +158,25 @@ void GLGizmoMeasure::on_render()
std::vector<const Measure::SurfaceFeature*> features = {m_measuring->get_feature(facet_idx, pos.cast<double>())}; std::vector<Measure::SurfaceFeature> features;
if (m_show_all) { if (m_show_all) {
features = m_measuring->get_features(); features = m_measuring->get_all_features(); // EXPENSIVE - debugging only.
features.erase(std::remove_if(features.begin(), features.end(), features.erase(std::remove_if(features.begin(), features.end(),
[](const Measure::SurfaceFeature* f) { [](const Measure::SurfaceFeature& f) {
return f->get_type() == Measure::SurfaceFeatureType::Plane; return f.get_type() == Measure::SurfaceFeatureType::Plane;
}), features.end()); }), features.end());
} else {
std::optional<Measure::SurfaceFeature> feat = m_measuring->get_feature(facet_idx, pos.cast<double>());
if (feat)
features.emplace_back(*feat);
} }
for (const Measure::SurfaceFeature* feature : features) {
if (! feature)
continue;
if (feature->get_type() == Measure::SurfaceFeatureType::Circle) {
const auto* circle = static_cast<const Measure::Circle*>(feature);
const Vec3d& c = circle->get_center(); for (const Measure::SurfaceFeature& feature : features) {
const Vec3d& n = circle->get_normal();
if (feature.get_type() == Measure::SurfaceFeatureType::Circle) {
const auto& [c, radius, n] = feature.get_circle();
Transform3d view_feature_matrix = view_model_matrix * Transform3d(Eigen::Translation3d(c)); Transform3d view_feature_matrix = view_model_matrix * Transform3d(Eigen::Translation3d(c));
view_feature_matrix.scale(0.5); view_feature_matrix.scale(0.5);
shader->set_uniform("view_model_matrix", view_feature_matrix); shader->set_uniform("view_model_matrix", view_feature_matrix);
@ -186,7 +187,7 @@ void GLGizmoMeasure::on_render()
Vec3d rad = n.cross(Vec3d::UnitX()); Vec3d rad = n.cross(Vec3d::UnitX());
if (rad.squaredNorm() < 0.1) if (rad.squaredNorm() < 0.1)
rad = n.cross(Vec3d::UnitY()); rad = n.cross(Vec3d::UnitY());
rad *= circle->get_radius() * rad.norm(); rad *= radius * rad.norm();
const int N = 20; const int N = 20;
for (int i=0; i<N; ++i) { for (int i=0; i<N; ++i) {
rad = Eigen::AngleAxisd(6.28/N, n) * rad; rad = Eigen::AngleAxisd(6.28/N, n) * rad;
@ -197,9 +198,8 @@ void GLGizmoMeasure::on_render()
m_vbo_sphere.render(); m_vbo_sphere.render();
} }
} }
else if (feature->get_type() == Measure::SurfaceFeatureType::Edge) { else if (feature.get_type() == Measure::SurfaceFeatureType::Edge) {
const auto* edge = static_cast<const Measure::Edge*>(feature); const auto& [start, end] = feature.get_edge();
auto& [start, end] = edge->get_edge();
Transform3d view_feature_matrix = view_model_matrix * Transform3d(Eigen::Translation3d(start)); Transform3d view_feature_matrix = view_model_matrix * Transform3d(Eigen::Translation3d(start));
auto q = Eigen::Quaternion<double>::FromTwoVectors(Vec3d::UnitZ(), end - start); auto q = Eigen::Quaternion<double>::FromTwoVectors(Vec3d::UnitZ(), end - start);
view_feature_matrix *= q; view_feature_matrix *= q;
@ -207,8 +207,8 @@ void GLGizmoMeasure::on_render()
shader->set_uniform("view_model_matrix", view_feature_matrix); shader->set_uniform("view_model_matrix", view_feature_matrix);
m_vbo_cylinder.set_color(ColorRGBA(0.8f, 0.2f, 0.2f, 1.f)); m_vbo_cylinder.set_color(ColorRGBA(0.8f, 0.2f, 0.2f, 1.f));
m_vbo_cylinder.render(); m_vbo_cylinder.render();
if (edge->get_point_of_interest()) { if (feature.get_extra_point()) {
Vec3d pin = *edge->get_point_of_interest(); Vec3d pin = *feature.get_extra_point();
view_feature_matrix = view_model_matrix * Transform3d(Eigen::Translation3d(pin)); view_feature_matrix = view_model_matrix * Transform3d(Eigen::Translation3d(pin));
view_feature_matrix.scale(0.5); view_feature_matrix.scale(0.5);
shader->set_uniform("view_model_matrix", view_feature_matrix); shader->set_uniform("view_model_matrix", view_feature_matrix);
@ -216,10 +216,9 @@ void GLGizmoMeasure::on_render()
m_vbo_sphere.render(); m_vbo_sphere.render();
} }
} }
else if (feature->get_type() == Measure::SurfaceFeatureType::Plane) { else if (feature.get_type() == Measure::SurfaceFeatureType::Plane) {
const auto* plane = static_cast<const Measure::Plane*>(feature); assert(feature.get_plane_idx() < m_plane_models.size());
assert(plane->get_plane_idx() < m_plane_models.size()); m_plane_models[feature.get_plane_idx()]->render();
m_plane_models[plane->get_plane_idx()]->render();
} }
} }
shader->set_uniform("view_model_matrix", view_model_matrix); shader->set_uniform("view_model_matrix", view_model_matrix);