23 float metric = FLT_MAX;
25 bool isStart()
const {
return !back.valid(); }
30using VertPathInfoMap = HashMap<VertId, VertPathInfo>;
33template<
class MetricToPenalty>
42 void reset(
const EdgeMetric & metric );
77 bool done()
const {
return nextSteps_.empty(); }
80 float doneDistance()
const {
return nextSteps_.empty() ? FLT_MAX : nextSteps_.top().penalty; }
101 VertPathInfoMap vertPathInfoMap_;
107 float penalty = FLT_MAX;
110 friend bool operator <(
const CandidateVert & a,
const CandidateVert & b )
112 return a.penalty > b.penalty;
115 std::priority_queue<CandidateVert> nextSteps_;
124struct TrivialMetricToPenalty
126 float operator()(
float metric, VertId )
const {
return metric; }
129using EdgePathsBuilder = EdgePathsBuilderT<TrivialMetricToPenalty>;
131template<
class MetricToPenalty>
133 : topology_( topology )
138template<
class MetricToPenalty>
140 : topology_( topology )
144template<
class MetricToPenalty>
148 vertPathInfoMap_.clear();
149 while ( !nextSteps_.empty() )
153template<
class MetricToPenalty>
156 auto & vi = vertPathInfoMap_[startVert];
157 if ( vi.metric > startMetric )
159 vi = {
EdgeId{}, startMetric };
160 nextSteps_.push( CandidateVert{ startVert, metricToPenalty_( startMetric, startVert ) } );
166template<
class MetricToPenalty>
169 auto it = vertPathInfoMap_.find( v );
170 return ( it != vertPathInfoMap_.end() ) ? &it->second :
nullptr;
173template<
class MetricToPenalty>
177 trackPathBack( v, &res );
181template<
class MetricToPenalty>
186 auto it = vertPathInfoMap_.find( v );
187 if ( it == vertPathInfoMap_.end() )
192 auto & vi = it->second;
196 res->push_back( vi.back );
197 v = topology_.dest( vi.back );
202template<
class MetricToPenalty>
205 if ( !( c.metric < FLT_MAX ) )
207 const auto vert = topology_.org( c.back );
208 auto & vi = vertPathInfoMap_[vert];
209 if ( vi.metric > c.metric )
212 nextSteps_.push( CandidateVert{ vert, metricToPenalty_( c.metric, vert ) } );
218template<
class MetricToPenalty>
221 bool aNextStepAdded =
false;
225 return aNextStepAdded;
227 const float orgMetric = rv.
metric;
233 c.metric = orgMetric + metric_( e );
234 aNextStepAdded = addNextStep_( c ) || aNextStepAdded;
236 return aNextStepAdded;
239template<
class MetricToPenalty>
242 while ( !nextSteps_.empty() )
244 const auto c = nextSteps_.top();
246 auto & vi = vertPathInfoMap_[c.v];
247 if ( metricToPenalty_( vi.metric, c.v ) < c.penalty )
252 assert( metricToPenalty_( vi.metric, c.v ) == c.penalty );
253 return { .v = c.v, .backward = vi.back, .penalty = c.penalty, .metric = vi.metric };
258template<
class MetricToPenalty>
261 auto res = reachNext();
262 addOrgRingSteps( res );
272 float operator()(
float metric,
VertId v )
const
274 return metric + ( (*points)[v] - target ).
length();
280class EdgePathsAStarBuilder:
public EdgePathsBuilderT<MetricToAStarPenalty>
290 EdgePathsAStarBuilder(
const Mesh & mesh,
const MeshTriPoint & target,
const MeshTriPoint & start ) :
295 const auto startPt = mesh.triPoint( start );
296 mesh.topology.forEachVertex( start, [&]( VertId v )
298 addStart( v, ( mesh.points[v] - startPt ).length() );
#define MR_NO_UNIQUE_ADDRESS
Definition MRMacros.h:42
length
Definition MRObjectDimensionsEnum.h:14
unsafe EdgePathsAStarBuilder(MR._ByValue_EdgePathsAStarBuilder _other)
unsafe bool addStart(MR.VertId startVert, float startMetric)
the class is responsible for finding smallest metric edge paths on a mesh
Definition MREdgePathsBuilder.h:35
ReachedVert growOneEdge()
the same as reachNext() + addOrgRingSteps()
Definition MREdgePathsBuilder.h:259
bool done() const
returns true if further edge forest growth is impossible
Definition MREdgePathsBuilder.h:77
bool addStart(VertId startVert, float startMetric)
Definition MREdgePathsBuilder.h:154
ReachedVert reachNext()
Definition MREdgePathsBuilder.h:240
EdgePathsBuilderT(const MeshTopology &topology)
Definition MREdgePathsBuilder.h:139
float doneDistance() const
returns path length till the next candidate vertex or maximum float value if all vertices have been r...
Definition MREdgePathsBuilder.h:80
bool addOrgRingSteps(const ReachedVert &rv)
Definition MREdgePathsBuilder.h:219
void reset(const EdgeMetric &metric)
clears everything without freeing memory, and sets new metric
Definition MREdgePathsBuilder.h:145
MR_NO_UNIQUE_ADDRESS MetricToPenalty metricToPenalty_
Definition MREdgePathsBuilder.h:96
VertId trackPathBack(VertId backpathStart, EdgePath *res=nullptr) const
Definition MREdgePathsBuilder.h:182
EdgePathsBuilderT(const MeshTopology &topology, const EdgeMetric &metric)
Definition MREdgePathsBuilder.h:132
EdgePath getPathBack(VertId backpathStart) const
returns the path in the forest from given vertex to one of start vertices
Definition MREdgePathsBuilder.h:174
const VertPathInfoMap & vertPathInfoMap() const
gives read access to the map from vertex to path to it
Definition MREdgePathsBuilder.h:83
const VertPathInfo * getVertInfo(VertId v) const
returns one element from the map (or nullptr if the element is missing)
Definition MREdgePathsBuilder.h:167
Definition MRMesh/MRMeshTopology.h:19
Definition MREdgePathsBuilder.h:269
Definition MREdgePathsBuilder.h:19
MRMESH_API EdgeMetric edgeLengthMetric(const Mesh &mesh)
Definition MRCameraOrientationPlugin.h:8
IteratorRange< OrgRingIterator > orgRing(const MeshTopology &topology, EdgeId edge)
Definition MRRingIterator.h:70
information about just reached vertex (with final metric value)
Definition MREdgePathsBuilder.h:50
float penalty
Definition MREdgePathsBuilder.h:58
EdgeId backward
edge from this vertex to its predecessor in the forest (if this vertex is not start)
Definition MREdgePathsBuilder.h:54
float metric
summed metric to reach this vertex
Definition MREdgePathsBuilder.h:61
VertId v
Definition MREdgePathsBuilder.h:51