Recti 1.2.4
Loading...
Searching...
No Matches
global_router.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <algorithm>
14#include <cmath>
15#include <deque>
16#include <limits>
17#include <memory>
18#include <optional>
19#include <recti/generic.hpp> // for enlarge_with
20#include <recti/interval.hpp>
21#include <recti/point.hpp>
22#include <string>
23#include <unordered_map>
24#include <vector>
25
26namespace recti {
27
31 enum class NodeType {
32 Steiner,
33 Terminal,
34 Source
35 };
36
42 std::string to_string(const NodeType routing_node_type);
43
52 template <typename IntPoint> class RoutingNode {
53 public:
54 std::string id;
56 IntPoint pt;
57 std::vector<RoutingNode<IntPoint>*>
60 = nullptr;
61 double capacitance = 0.0;
62 double delay = 0.0;
63 int path_length = 0;
64
71 RoutingNode(std::string node_id, NodeType node_type, IntPoint node_position)
72 : id(std::move(node_id)), node_type(node_type), pt(node_position) {}
73
78 void add_child(RoutingNode<IntPoint>* child_node) {
79 child_node->parent = this;
80 children.emplace_back(child_node);
81 }
82
88 auto iter = std::find(children.begin(), children.end(), child_node);
89 if (iter != children.end()) {
90 children.erase(iter);
91 child_node->parent = nullptr;
92 }
93 }
94
99 auto get_position() const -> const IntPoint& { return pt; }
100
106 auto manhattan_distance(const RoutingNode<IntPoint>* other_node) const -> int {
107 return pt.min_dist_with(other_node->pt);
108 }
109 };
110
165 template <typename IntPoint> class GlobalRoutingTree {
166 public:
167 using Keepout = decltype(std::declval<IntPoint>().enlarge_with(1));
168
169 private:
172 std::deque<RoutingNode<IntPoint>> _arena;
173 int next_steiner_id = 1;
174 int next_terminal_id = 1;
175
176 auto _find_nearest_node(const IntPoint& point, std::optional<std::string> exclude_id
177 = std::nullopt) -> RoutingNode<IntPoint>*;
178
179 auto _find_nearest_insertion_with_constraints(const IntPoint& pt,
180 int allowed_wirelength
181 = std::numeric_limits<int>::max(),
182 std::optional<std::vector<Keepout>> keepouts
183 = std::nullopt)
184 -> std::pair<RoutingNode<IntPoint>*, RoutingNode<IntPoint>*>;
185
186 auto _insert_terminal_impl(const IntPoint& point,
187 int allowed_wirelength = std::numeric_limits<int>::max(),
188 std::optional<std::vector<Keepout>> keepouts = std::nullopt)
189 -> void;
190
191 public:
192 std::unordered_map<std::string, RoutingNode<IntPoint>*>
196
201 GlobalRoutingTree(IntPoint source_position) {
202 _arena.emplace_back("source", NodeType::Source, source_position);
203 nodes["source"] = &this->_arena.back();
204 }
205
210 auto get_source() const -> const RoutingNode<IntPoint>* { return &this->_arena[0]; }
211
216 auto get_source() -> RoutingNode<IntPoint>* { return &this->_arena[0]; }
217
226 auto insert_steiner_node(const IntPoint& point, std::optional<std::string> parent_id
227 = std::nullopt) -> std::string;
228
237 auto insert_terminal_node(const IntPoint& point, std::optional<std::string> parent_id
238 = std::nullopt) -> std::string;
239
250 auto insert_node_on_branch(NodeType new_node_type, const IntPoint& point,
251 std::string branch_start_id, std::string branch_end_id)
252 -> std::string;
253
259 auto insert_terminal_with_steiner(const IntPoint& point,
260 std::optional<std::vector<Keepout>> keepouts
261 = std::nullopt) -> void {
262 _insert_terminal_impl(point, std::numeric_limits<int>::max(), keepouts);
263 }
264
271 auto insert_terminal_with_constraints(const IntPoint& point, int allowed_wirelength,
272 std::optional<std::vector<Keepout>> keepouts
273 = std::nullopt) -> void {
274 _insert_terminal_impl(point, allowed_wirelength, keepouts);
275 }
276
281 auto calculate_total_wirelength() const -> int;
282
287 auto calculate_worst_wirelength() const -> int;
288
296 auto get_tree_structure(const RoutingNode<IntPoint>* current_node = nullptr,
297 int level = 0) const -> std::string;
298
306 auto find_path_to_source(const std::string& node_id) const
307 -> std::vector<const RoutingNode<IntPoint>*>;
308
313 auto get_all_terminals() const -> std::vector<const RoutingNode<IntPoint>*>;
314
319 auto get_all_steiner_nodes() const -> std::vector<const RoutingNode<IntPoint>*>;
320
327
331 void visualize_tree() const;
332 };
333
342 template <typename IntPoint> class GlobalRouter {
343 IntPoint source_position;
344 std::vector<IntPoint> terminal_positions;
347 = 0;
348 std::optional<std::vector<typename GlobalRoutingTree<IntPoint>::Keepout>>
349 keepouts;
350
351 public:
352 using Keepout = decltype(std::declval<IntPoint>().enlarge_with(1));
353
360 GlobalRouter(const IntPoint& source_pos, std::vector<IntPoint> terminal_positions,
361 std::optional<std::vector<Keepout>> keepout_regions = std::nullopt);
362
367 for (const auto& terminal : this->terminal_positions) {
368 this->tree.insert_terminal_node(terminal);
369 }
370 }
371
376 this->tree.worst_wirelength = this->worst_wirelength; // Store the allowed wirelength
377 // in the tree for reference
378 for (const auto& terminal : this->terminal_positions) {
379 this->tree.insert_terminal_with_steiner(terminal, this->keepouts);
380 }
381 }
382
388 void route_with_constraints(double multiplier = 1.0) {
389 int allowed_wirelength
390 = static_cast<int>(std::round(this->worst_wirelength * multiplier));
391 this->tree.worst_wirelength = this->worst_wirelength; // Store the allowed wirelength
392 // in the tree for reference
393 for (const auto& terminal : this->terminal_positions) {
394 this->tree.insert_terminal_with_constraints(terminal, allowed_wirelength,
395 this->keepouts);
396 }
397 }
398
403 auto get_tree() const -> const GlobalRoutingTree<IntPoint>& { return this->tree; }
404 };
405
406 template <typename IntPoint> extern std::string visualize_routing_tree_svg(
407 const GlobalRoutingTree<IntPoint>& tree,
408 std::optional<std::vector<typename GlobalRoutingTree<IntPoint>::Keepout>> keepouts
409 = std::nullopt,
410 const int width = 800, const int height = 600, const int margin = 50);
411
412 template <typename IntPoint> extern void save_routing_tree_svg(
413 const GlobalRoutingTree<IntPoint>& tree,
414 std::optional<std::vector<typename GlobalRoutingTree<IntPoint>::Keepout>> keepouts
415 = std::nullopt,
416 const std::string& filename = "routing_tree.svg", const int width = 800,
417 const int height = 600);
418
419 template <typename IntPoint> extern std::string visualize_routing_tree3d_svg(
420 const GlobalRoutingTree<IntPoint>& tree,
421 std::optional<std::vector<typename GlobalRoutingTree<IntPoint>::Keepout>> keepouts
422 = std::nullopt,
423 const int scale_z = 100, const int width = 800, const int height = 600,
424 const int margin = 50);
425
426 template <typename IntPoint> extern void save_routing_tree3d_svg(
427 const GlobalRoutingTree<IntPoint>& tree,
428 std::optional<std::vector<typename GlobalRoutingTree<IntPoint>::Keepout>> keepouts
429 = std::nullopt,
430 const int scale_z = 100, const std::string& filename = "routing_tree3d.svg",
431 const int width = 800, const int height = 600);
432
433} // namespace recti
Manages the global routing process, constructing a GlobalRoutingTree.
Definition global_router.hpp:342
decltype(std::declval< IntPoint >().enlarge_with(1)) Keepout
Definition global_router.hpp:352
auto get_tree() const -> const GlobalRoutingTree< IntPoint > &
Gets a const reference to the constructed GlobalRoutingTree<IntPoint>.
Definition global_router.hpp:403
void route_with_steiners()
Routes terminals, potentially inserting Steiner nodes to optimize connections.
Definition global_router.hpp:375
GlobalRouter(const IntPoint &source_pos, std::vector< IntPoint > terminal_positions, std::optional< std::vector< Keepout > > keepout_regions=std::nullopt)
Constructs a new GlobalRouter.
void route_simple()
Routes terminals simply by connecting each to the nearest existing node.
Definition global_router.hpp:366
void route_with_constraints(double multiplier=1.0)
Routes terminals with wirelength constraints.
Definition global_router.hpp:388
Represents the entire global routing tree.
Definition global_router.hpp:165
auto get_all_steiner_nodes() const -> std::vector< const RoutingNode< IntPoint > * >
Retrieves all Steiner nodes in the tree.
auto calculate_worst_wirelength() const -> int
Calculates the worst wirelength of the routing tree.
auto calculate_total_wirelength() const -> int
Calculates the total wirelength of the routing tree.
int worst_wirelength
Definition global_router.hpp:194
auto find_path_to_source(const std::string &node_id) const -> std::vector< const RoutingNode< IntPoint > * >
Finds the path from a given node to the source node.
auto get_tree_structure(const RoutingNode< IntPoint > *current_node=nullptr, int level=0) const -> std::string
Generates a string representation of the tree structure for visualization or debugging.
void visualize_tree() const
Visualizes the routing tree (implementation in .cpp file).
auto insert_node_on_branch(NodeType new_node_type, const IntPoint &point, std::string branch_start_id, std::string branch_end_id) -> std::string
Inserts a new node (Steiner or Terminal) onto an existing branch of the tree.
auto insert_steiner_node(const IntPoint &point, std::optional< std::string > parent_id=std::nullopt) -> std::string
Inserts a new Steiner node into the routing tree.
auto get_source() const -> const RoutingNode< IntPoint > *
Gets a const pointer to the source node of the tree.
Definition global_router.hpp:210
auto get_all_terminals() const -> std::vector< const RoutingNode< IntPoint > * >
Retrieves all terminal nodes in the tree.
GlobalRoutingTree(IntPoint source_position)
Constructs a new GlobalRoutingTree with a specified source position.
Definition global_router.hpp:201
auto insert_terminal_node(const IntPoint &point, std::optional< std::string > parent_id=std::nullopt) -> std::string
Inserts a new terminal node into the routing tree.
std::unordered_map< std::string, RoutingNode< IntPoint > * > nodes
Map from node ID to RoutingNode<IntPoint> pointer.
Definition global_router.hpp:193
auto get_source() -> RoutingNode< IntPoint > *
Gets a non-const pointer to the source node of the tree.
Definition global_router.hpp:216
decltype(std::declval< IntPoint >().enlarge_with(1)) Keepout
Definition global_router.hpp:167
void optimize_steiner_points()
Optimizes the routing tree by removing redundant Steiner points. A Steiner point is considered redund...
auto insert_terminal_with_steiner(const IntPoint &point, std::optional< std::vector< Keepout > > keepouts=std::nullopt) -> void
Inserts a new terminal node with possible Steiner point.
Definition global_router.hpp:259
auto insert_terminal_with_constraints(const IntPoint &point, int allowed_wirelength, std::optional< std::vector< Keepout > > keepouts=std::nullopt) -> void
Inserts a new terminal node with constraints.
Definition global_router.hpp:271
Represents a node in the global routing tree.
Definition global_router.hpp:52
double delay
Delay associated with the node (for future use).
Definition global_router.hpp:62
NodeType node_type
The type of the node.
Definition global_router.hpp:55
auto get_position() const -> const IntPoint &
Gets the position of the routing node.
Definition global_router.hpp:99
auto manhattan_distance(const RoutingNode< IntPoint > *other_node) const -> int
Calculates the Manhattan distance between this node and another node.
Definition global_router.hpp:106
RoutingNode(std::string node_id, NodeType node_type, IntPoint node_position)
Constructs a new RoutingNode<IntPoint>.
Definition global_router.hpp:71
void remove_child(RoutingNode< IntPoint > *child_node)
Removes a child node from this node.
Definition global_router.hpp:87
std::string id
Unique identifier for the node.
Definition global_router.hpp:54
RoutingNode< IntPoint > * parent
Pointer to the parent node. Null for the source node.
Definition global_router.hpp:60
IntPoint pt
The 2D integer coordinates of the node.
Definition global_router.hpp:56
void add_child(RoutingNode< IntPoint > *child_node)
Adds a child node to this node.
Definition global_router.hpp:78
double capacitance
Capacitance associated with the node (for future use).
Definition global_router.hpp:61
int path_length
Path length from the source to this node.
Definition global_router.hpp:63
std::vector< RoutingNode< IntPoint > * > children
Pointers to child nodes in the routing tree.
Definition global_router.hpp:58
Generic free functions for geometric operations (overlap, intersection, hull, distance).
Interval (range) template class with arithmetic and set operations.
Definition svg_utils.hpp:12
std::string visualize_routing_tree_svg(const GlobalRoutingTree< IntPoint > &tree, std::optional< std::vector< typename GlobalRoutingTree< IntPoint >::Keepout > > keepouts=std::nullopt, const int width=800, const int height=600, const int margin=50)
void save_routing_tree_svg(const GlobalRoutingTree< IntPoint > &tree, std::optional< std::vector< typename GlobalRoutingTree< IntPoint >::Keepout > > keepouts=std::nullopt, const std::string &filename="routing_tree.svg", const int width=800, const int height=600)
void save_routing_tree3d_svg(const GlobalRoutingTree< IntPoint > &tree, std::optional< std::vector< typename GlobalRoutingTree< IntPoint >::Keepout > > keepouts=std::nullopt, const int scale_z=100, const std::string &filename="routing_tree3d.svg", const int width=800, const int height=600)
NodeType
Defines the type of a routing node.
Definition global_router.hpp:31
@ Terminal
A terminal point, representing a pin or a component connection.
@ Steiner
A Steiner point, an intermediate point added to optimize routing.
@ Source
The source point of the routing tree.
std::string visualize_routing_tree3d_svg(const GlobalRoutingTree< IntPoint > &tree, std::optional< std::vector< typename GlobalRoutingTree< IntPoint >::Keepout > > keepouts=std::nullopt, const int scale_z=100, const int width=800, const int height=600, const int margin=50)
std::string to_string(const NodeType routing_node_type)
Converts a NodeType enum value to its string representation.
2D Point template class supporting intervals, arithmetic, and geometric queries.