NEW: upgrade Measure class

SurfaceFeature is global,Measuring is local
Jira: STUDIO-6166

Change-Id: I62dc58c51f0ac5d9c587e2debe3d774f02a3158a
This commit is contained in:
zhou.xu 2024-02-27 19:25:33 +08:00 committed by Lane.Wei
parent 98faf48787
commit ac1459e338
2 changed files with 107 additions and 21 deletions

View File

@ -73,10 +73,12 @@ public:
bool features_extracted = false; bool features_extracted = false;
}; };
std::optional<SurfaceFeature> get_feature(size_t face_idx, const Vec3d& point); std::optional<SurfaceFeature> get_feature(size_t face_idx, const Vec3d& point, const Transform3d &world_tran);
int get_num_of_planes() const; int get_num_of_planes() const;
const std::vector<int>& get_plane_triangle_indices(int idx) const; const std::vector<int>& get_plane_triangle_indices(int idx) const;
std::vector<int>* get_plane_tri_indices(int idx);
const std::vector<SurfaceFeature>& get_plane_features(unsigned int plane_id); const std::vector<SurfaceFeature>& get_plane_features(unsigned int plane_id);
std::vector<SurfaceFeature>* get_plane_features_pointer(unsigned int plane_id);
const indexed_triangle_set& get_its() const; const indexed_triangle_set& get_its() const;
private: private:
@ -504,7 +506,7 @@ void MeasuringImpl::extract_features(int plane_idx)
plane.features_extracted = true; plane.features_extracted = true;
} }
std::optional<SurfaceFeature> MeasuringImpl::get_feature(size_t face_idx, const Vec3d& point) std::optional<SurfaceFeature> MeasuringImpl::get_feature(size_t face_idx, const Vec3d& point, const Transform3d &world_tran)
{ {
if (face_idx >= m_face_to_plane.size()) if (face_idx >= m_face_to_plane.size())
return std::optional<SurfaceFeature>(); return std::optional<SurfaceFeature>();
@ -544,18 +546,33 @@ std::optional<SurfaceFeature> MeasuringImpl::get_feature(size_t face_idx, const
const auto& [sp, ep] = f.get_edge(); const auto& [sp, ep] = f.get_edge();
double len_sq = (ep-sp).squaredNorm(); double len_sq = (ep-sp).squaredNorm();
double limit_sq = std::max(0.025*0.025, std::min(0.5*0.5, 0.1 * 0.1 * len_sq)); double limit_sq = std::max(0.025*0.025, std::min(0.5*0.5, 0.1 * 0.1 * len_sq));
if ((point - sp).squaredNorm() < limit_sq) {
if ((point-sp).squaredNorm() < limit_sq) SurfaceFeature local_f(sp);
return std::make_optional(SurfaceFeature(sp)); local_f.origin_surface_feature = std::make_shared<SurfaceFeature>(local_f);
if ((point-ep).squaredNorm() < limit_sq) local_f.translate(world_tran);
return std::make_optional(SurfaceFeature(ep)); return std::make_optional(local_f);
}
if ((point - ep).squaredNorm() < limit_sq) {
SurfaceFeature local_f(ep);
local_f.origin_surface_feature = std::make_shared<SurfaceFeature>(local_f);
local_f.translate(world_tran);
return std::make_optional(local_f);
}
} }
return std::make_optional(f); SurfaceFeature f_tran(f);
f_tran.origin_surface_feature = std::make_shared<SurfaceFeature>(f);
f_tran.translate(world_tran);
return std::make_optional(f_tran);
} }
// Nothing detected, return the plane as a whole. // Nothing detected, return the plane as a whole.
assert(plane.surface_features.back().get_type() == SurfaceFeatureType::Plane); assert(plane.surface_features.back().get_type() == SurfaceFeatureType::Plane);
return std::make_optional(plane.surface_features.back()); auto cur_plane = const_cast<PlaneData*>(&plane);
SurfaceFeature f_tran(cur_plane->surface_features.back());
f_tran.origin_surface_feature = std::make_shared<SurfaceFeature>(cur_plane->surface_features.back());
f_tran.translate(world_tran);
return std::make_optional(f_tran);
} }
@ -575,6 +592,12 @@ const std::vector<int>& MeasuringImpl::get_plane_triangle_indices(int idx) const
return m_planes[idx].facets; return m_planes[idx].facets;
} }
std::vector<int>* MeasuringImpl::get_plane_tri_indices(int idx)
{
assert(idx >= 0 && idx < int(m_planes.size()));
return &m_planes[idx].facets;
}
const std::vector<SurfaceFeature>& MeasuringImpl::get_plane_features(unsigned int plane_id) const std::vector<SurfaceFeature>& MeasuringImpl::get_plane_features(unsigned int plane_id)
{ {
assert(plane_id < m_planes.size()); assert(plane_id < m_planes.size());
@ -583,6 +606,13 @@ const std::vector<SurfaceFeature>& MeasuringImpl::get_plane_features(unsigned in
return m_planes[plane_id].surface_features; return m_planes[plane_id].surface_features;
} }
std::vector<SurfaceFeature>* MeasuringImpl::get_plane_features_pointer(unsigned int plane_id) {
assert(plane_id < m_planes.size());
if (!m_planes[plane_id].features_extracted)
extract_features(plane_id);
return &m_planes[plane_id].surface_features;
}
const indexed_triangle_set& MeasuringImpl::get_its() const const indexed_triangle_set& MeasuringImpl::get_its() const
{ {
return this->m_its; return this->m_its;
@ -596,9 +626,8 @@ Measuring::~Measuring() {}
std::optional<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 Transform3d &world_tran) const {
{ return priv->get_feature(face_idx, point,world_tran);
return priv->get_feature(face_idx, point);
} }
@ -778,7 +807,7 @@ static AngleAndEdges angle_plane_plane(const std::tuple<int, Vec3d, Vec3d>& p1,
return ret; return ret;
} }
MeasurementResult get_measurement(const SurfaceFeature& a, const SurfaceFeature& b, const Measuring* measuring) MeasurementResult get_measurement(const SurfaceFeature& a, const SurfaceFeature& b)
{ {
assert(a.get_type() != SurfaceFeatureType::Undef && b.get_type() != SurfaceFeatureType::Undef); assert(a.get_type() != SurfaceFeatureType::Undef && b.get_type() != SurfaceFeatureType::Undef);
@ -920,9 +949,9 @@ MeasurementResult get_measurement(const SurfaceFeature& a, const SurfaceFeature&
result.distance_infinite = std::make_optional(DistAndPoints{ it->dist, it->from, it->to }); result.distance_infinite = std::make_optional(DistAndPoints{ it->dist, it->from, it->to });
} }
else { else {
const std::vector<SurfaceFeature>& plane_features = measuring->get_plane_features(idx); std::vector<SurfaceFeature>* plane_features = f2.world_plane_features;
std::vector<DistAndPoints> distances; std::vector<DistAndPoints> distances;
for (const SurfaceFeature& sf : plane_features) { for (const SurfaceFeature& sf : *plane_features) {
if (sf.get_type() == SurfaceFeatureType::Edge) { if (sf.get_type() == SurfaceFeatureType::Edge) {
const auto m = get_measurement(sf, f1); const auto m = get_measurement(sf, f1);
if (!m.distance_infinite.has_value()) { if (!m.distance_infinite.has_value()) {
@ -1174,9 +1203,9 @@ MeasurementResult get_measurement(const SurfaceFeature& a, const SurfaceFeature&
const bool coplanar = are_parallel(normal1, normal2) && Eigen::Hyperplane<double, 3>(normal1, center).absDistance(origin2) < EPSILON; const bool coplanar = are_parallel(normal1, normal2) && Eigen::Hyperplane<double, 3>(normal1, center).absDistance(origin2) < EPSILON;
if (!coplanar) { if (!coplanar) {
const std::vector<SurfaceFeature>& plane_features = measuring->get_plane_features(idx2); std::vector<SurfaceFeature>* plane_features = f2.world_plane_features;
std::vector<DistAndPoints> distances; std::vector<DistAndPoints> distances;
for (const SurfaceFeature& sf : plane_features) { for (const SurfaceFeature& sf : *plane_features) {
if (sf.get_type() == SurfaceFeatureType::Edge) { if (sf.get_type() == SurfaceFeatureType::Edge) {
const auto m = get_measurement(sf, f1); const auto m = get_measurement(sf, f1);
if (!m.distance_infinite.has_value()) { if (!m.distance_infinite.has_value()) {
@ -1243,5 +1272,34 @@ void SurfaceFeature::translate(const Vec3d& displacement) {
} }
} }
}} // namespace Slic3r void SurfaceFeature::translate(const Transform3d &tran)
{
switch (get_type()) {
case Measure::SurfaceFeatureType::Point: {
m_pt1 = tran * m_pt1;
break;
}
case Measure::SurfaceFeatureType::Edge: {
m_pt1 = tran * m_pt1;
m_pt2 = tran * m_pt2;
if (m_pt3.has_value()) { // extra_point()
m_pt3 = tran * *m_pt3;
}
break;
}
case Measure::SurfaceFeatureType::Plane: {
// m_pt1 is normal;
m_pt2 = tran * m_pt2;
break;
}
case Measure::SurfaceFeatureType::Circle: {
m_pt1 = tran * m_pt1;
// m_pt2 is normal;
break;
}
default: break;
}
}
}//namespace Measure
} // namespace Slic3r

View File

@ -30,10 +30,27 @@ public:
SurfaceFeature(SurfaceFeatureType type, const Vec3d& pt1, const Vec3d& pt2, std::optional<Vec3d> pt3 = std::nullopt, double value = 0.0) SurfaceFeature(SurfaceFeatureType type, const Vec3d& pt1, const Vec3d& pt2, std::optional<Vec3d> pt3 = std::nullopt, double value = 0.0)
: m_type(type), m_pt1(pt1), m_pt2(pt2), m_pt3(pt3), m_value(value) {} : m_type(type), m_pt1(pt1), m_pt2(pt2), m_pt3(pt3), m_value(value) {}
explicit SurfaceFeature(const Vec3d& pt) SurfaceFeature(const Vec3d& pt)
: m_type{SurfaceFeatureType::Point}, m_pt1{pt} {} : m_type{SurfaceFeatureType::Point}, m_pt1{pt} {}
SurfaceFeature(const SurfaceFeature& sf){
this->clone(sf);
}
void clone(const SurfaceFeature &sf)
{
m_type = sf.get_type();
m_pt1 = sf.get_pt1();
m_pt2 = sf.get_pt2();
m_pt3 = sf.get_pt3();
m_value = sf.get_value();
mesh = sf.mesh;
plane_indices = sf.plane_indices;
world_tran = sf.world_tran;
world_plane_features = sf.world_plane_features;
}
void translate(const Vec3d& displacement); void translate(const Vec3d& displacement);
void translate(const Transform3d& tran);
// Get type of this feature. // Get type of this feature.
SurfaceFeatureType get_type() const { return m_type; } SurfaceFeatureType get_type() const { return m_type; }
@ -74,6 +91,17 @@ public:
return !operator == (other); return !operator == (other);
} }
indexed_triangle_set* mesh{nullptr};
std::vector<int>* plane_indices{nullptr};
Transform3d world_tran;
std::vector<SurfaceFeature>* world_plane_features{nullptr};
std::shared_ptr<SurfaceFeature> origin_surface_feature{nullptr};
Vec3d get_pt1() const{ return m_pt1; }
Vec3d get_pt2() const { return m_pt2; }
const std::optional<Vec3d>& get_pt3() const { return m_pt3; }
double get_value() const { return m_value; }
private: private:
SurfaceFeatureType m_type{ SurfaceFeatureType::Undef }; SurfaceFeatureType m_type{ SurfaceFeatureType::Undef };
Vec3d m_pt1{ Vec3d::Zero() }; Vec3d m_pt1{ Vec3d::Zero() };
@ -96,7 +124,7 @@ public:
// 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 (if any). // should be highlighted (if any).
std::optional<SurfaceFeature> get_feature(size_t face_idx, const Vec3d& point) const; std::optional<SurfaceFeature> get_feature(size_t face_idx, const Vec3d& point, const Transform3d & world_tran) const;
// Return total number of planes. // Return total number of planes.
int get_num_of_planes() const; int get_num_of_planes() const;
@ -151,7 +179,7 @@ struct MeasurementResult {
}; };
// Returns distance/angle between two SurfaceFeatures. // Returns distance/angle between two SurfaceFeatures.
MeasurementResult get_measurement(const SurfaceFeature& a, const SurfaceFeature& b, const Measuring* measuring = nullptr); MeasurementResult get_measurement(const SurfaceFeature& a, const SurfaceFeature& b);
inline Vec3d edge_direction(const Vec3d& from, const Vec3d& to) { return (to - from).normalized(); } inline Vec3d edge_direction(const Vec3d& from, const Vec3d& to) { return (to - from).normalized(); }
inline Vec3d edge_direction(const std::pair<Vec3d, Vec3d>& e) { return edge_direction(e.first, e.second); } inline Vec3d edge_direction(const std::pair<Vec3d, Vec3d>& e) { return edge_direction(e.first, e.second); }