OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::MinCostMaxFlow Struct Reference
Collaboration diagram for Slic3r::MinCostMaxFlow:

Public Member Functions

std::vector< int > solve ()
void add_edge (int from, int to, int capacity, int cost)
bool spfa (int source, int sink)
int get_distance (int idx_in_left, int idx_in_right)

Public Attributes

std::vector< std::vector< float > > matrix
std::vector< int > l_nodes
std::vector< int > r_nodes
std::vector< Edge > edges
std::vector< std::vector< int > > adj
int total_nodes { -1 }
int source_id { -1 }
int sink_id { -1 }

Detailed Description

Member Function Documentation

◆ add_edge()

void Slic3r::MinCostMaxFlow::add_edge ( int from,
int to,
int capacity,
int cost )
153 {
154 adj[from].emplace_back(edges.size());
155 edges.emplace_back(from, to, capacity, cost);
156 //also add reverse edge ,set capacity to zero,cost to negative
157 adj[to].emplace_back(edges.size());
158 edges.emplace_back(to, from, 0, -cost);
159 }
std::vector< Edge > edges
Definition ToolOrderUtils.cpp:126
std::vector< std::vector< int > > adj
Definition ToolOrderUtils.cpp:127

References adj, and edges.

◆ get_distance()

int Slic3r::MinCostMaxFlow::get_distance ( int idx_in_left,
int idx_in_right )
208 {
209 if (l_nodes[idx_in_left] == -1) {
210 return 0;
211 }
212
213 float val = matrix[l_nodes[idx_in_left]][r_nodes[idx_in_right]];
214 return std::min(static_cast<int>(val), MaxFlowGraph::MCMF_MAX_EDGE_COST);
215 }
constexpr int MCMF_MAX_EDGE_COST
Definition ToolOrderUtils.hpp:23
std::vector< int > r_nodes
Definition ToolOrderUtils.cpp:125
std::vector< std::vector< float > > matrix
Definition ToolOrderUtils.cpp:123
std::vector< int > l_nodes
Definition ToolOrderUtils.cpp:124

References l_nodes, matrix, Slic3r::MaxFlowGraph::MCMF_MAX_EDGE_COST, and r_nodes.

◆ solve()

std::vector< int > Slic3r::MinCostMaxFlow::solve ( )
135 {
136 while (spfa(source_id, sink_id));
137
138 std::vector<int>matching(l_nodes.size(), MaxFlowGraph::INVALID_ID);
139 // to get the match info, just traverse the left nodes and
140 // check the edges with flow > 0 and linked to right nodes
141 for (int u = 0; u < l_nodes.size(); ++u) {
142 for (int eid : adj[u]) {
143 Edge& e = edges[eid];
144 if (e.flow > 0 && e.to >= l_nodes.size() && e.to < l_nodes.size() + r_nodes.size())
145 matching[e.from] = r_nodes[e.to - l_nodes.size()];
146 }
147 }
148
149 return matching;
150 }
const int INVALID_ID
Definition ToolOrderUtils.hpp:21
@ Edge
Definition Measure.hpp:26
bool spfa(int source, int sink)
Definition ToolOrderUtils.cpp:161
int sink_id
Definition ToolOrderUtils.cpp:131
int source_id
Definition ToolOrderUtils.cpp:130

References adj, edges, Slic3r::Edge::flow, Slic3r::Edge::from, Slic3r::MaxFlowGraph::INVALID_ID, l_nodes, r_nodes, sink_id, source_id, spfa(), and Slic3r::Edge::to.

Here is the call graph for this function:

◆ spfa()

bool Slic3r::MinCostMaxFlow::spfa ( int source,
int sink )
162 {
163 std::vector<int>dist(total_nodes, MaxFlowGraph::INF);
164 std::vector<bool>in_queue(total_nodes, false);
165 std::vector<int>flow(total_nodes, MaxFlowGraph::INF);
166 std::vector<int>prev(total_nodes, 0);
167
168 std::queue<int>q;
169 q.push(source);
170 in_queue[source] = true;
171 dist[source] = 0;
172
173 while (!q.empty()) {
174 int now_at = q.front();
175 q.pop();
176 in_queue[now_at] = false;
177
178 for (auto eid : adj[now_at]) //traverse all linked edges
179 {
180 Edge& e = edges[eid];
181 if (e.flow<e.capacity && dist[e.to]>dist[now_at] + e.cost) {
182 dist[e.to] = dist[now_at] + e.cost;
183 prev[e.to] = eid;
184 flow[e.to] = std::min(flow[now_at], e.capacity - e.flow);
185 if (!in_queue[e.to]) {
186 q.push(e.to);
187 in_queue[e.to] = true;
188 }
189 }
190 }
191 }
192
193 if (dist[sink] == MaxFlowGraph::INF)
194 return false;
195
196 int now_at = sink;
197 while (now_at != source) {
198 int prev_edge = prev[now_at];
199 edges[prev_edge].flow += flow[sink];
200 edges[prev_edge ^ 1].flow -= flow[sink];
201 now_at = edges[prev_edge].from;
202 }
203
204 return true;
205 }
T dist(const boost::polygon::point_data< T > &p1, const boost::polygon::point_data< T > &p2)
Definition Geometry.cpp:298
const int INF
Definition ToolOrderUtils.hpp:20
int total_nodes
Definition ToolOrderUtils.cpp:129

References adj, Slic3r::Edge::capacity, Slic3r::Edge::cost, Slic3r::Geometry::dist(), edges, Slic3r::Edge::flow, Slic3r::MaxFlowGraph::INF, Slic3r::Edge::to, and total_nodes.

Referenced by solve().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ adj

std::vector<std::vector<int> > Slic3r::MinCostMaxFlow::adj

Referenced by add_edge(), solve(), and spfa().

◆ edges

std::vector<Edge> Slic3r::MinCostMaxFlow::edges

Referenced by add_edge(), solve(), and spfa().

◆ l_nodes

std::vector<int> Slic3r::MinCostMaxFlow::l_nodes

Referenced by get_distance(), and solve().

◆ matrix

std::vector<std::vector<float> > Slic3r::MinCostMaxFlow::matrix

Referenced by get_distance().

◆ r_nodes

std::vector<int> Slic3r::MinCostMaxFlow::r_nodes

Referenced by get_distance(), and solve().

◆ sink_id

int Slic3r::MinCostMaxFlow::sink_id { -1 }
131{ -1 };

Referenced by solve().

◆ source_id

int Slic3r::MinCostMaxFlow::source_id { -1 }
130{ -1 };

Referenced by solve().

◆ total_nodes

int Slic3r::MinCostMaxFlow::total_nodes { -1 }
129{ -1 };

Referenced by spfa().


The documentation for this struct was generated from the following file: