36 #include <unordered_map>
46 constexpr
double REFERENCE_EDGE_LENGTH = 25.0;
47 constexpr
double NODE_RADIUS_RATIO = 1.0 / REFERENCE_EDGE_LENGTH;
48 constexpr
double EDGE_WIDTH_RATIO = 0.5 / REFERENCE_EDGE_LENGTH;
49 constexpr
double PICTURE_MARGIN_RATIO = 20.0 / REFERENCE_EDGE_LENGTH;
50 constexpr
double EDGE_PICTURE_PIXELS = 2.0 * REFERENCE_EDGE_LENGTH;
53 constexpr
double EXTENT_RATIO = 0.1;
63 Bounds bounds_of(
const Layout &layout )
65 const Node &first = layout.nodes.front();
66 Bounds bounds = { first.x, first.x, first.y, first.y };
68 for(
const Node &node : layout.nodes )
70 bounds.min_x = std::min( bounds.min_x, node.x );
71 bounds.max_x = std::max( bounds.max_x, node.x );
72 bounds.min_y = std::min( bounds.min_y, node.y );
73 bounds.max_y = std::max( bounds.max_y, node.y );
79 double typical_edge_length(
const Layout &layout )
81 std::vector<double> lengths;
82 lengths.reserve( layout.edges.size() );
84 for(
const Edge &edge : layout.edges )
86 const Node &from = layout.nodes[edge.from];
87 const Node &
to = layout.nodes[edge.to];
89 if(
const double length = std::hypot(
to.x - from.x,
to.y - from.y ); length > 0.0 )
91 lengths.push_back( length );
95 if( !lengths.empty() )
97 const auto middle = lengths.begin() + lengths.size() / 2;
98 std::nth_element( lengths.begin(), middle, lengths.end() );
102 const Bounds bounds = bounds_of( layout );
103 const double extent = std::max( bounds.max_x - bounds.min_x, bounds.max_y - bounds.min_y );
105 return extent > 0.0 ? EXTENT_RATIO * extent : 1.0;
108 bool is_valid(
const std::optional<double> &value,
const bool allow_zero )
110 return !value.has_value() || ( std::isfinite( *value ) && ( allow_zero ? *value >= 0.0 : *value > 0.0 ) );
115 const std::optional<std::string> &x_key,
116 const std::optional<std::string> &y_key )
118 if( x_key.has_value() != y_key.has_value() )
120 return ERR(
"give the parameter names of both coordinates or of none" );
123 std::vector<const Gate *> gates = tree.
get_gates();
126 return ERR(
"the clock tree contains no gates" );
131 gates.begin(), gates.end(), [](
const Gate *a,
const Gate *b ) { return a->get_id() < b->get_id(); } );
134 std::unordered_map<const Gate *, std::size_t> node_of_gate;
136 for(
const Gate *gate : gates )
139 std::optional<double>
x;
140 std::optional<double>
y;
141 if( x_key.has_value() )
146 else if( gate->has_location() )
148 x = gate->get_location_x();
149 y = gate->get_location_y();
152 if( !
x.has_value() || !
y.has_value() )
158 node_of_gate[gate] = layout.
nodes.size();
159 layout.
nodes.push_back( { gate->get_id(), *
x, *
y } );
162 if( layout.
nodes.empty() )
164 const std::string expected = x_key.has_value() ?
"the gate parameters '" + *x_key +
"' and '" + *y_key +
"'"
165 : std::string(
"a location in the layout" );
166 return ERR(
"none of the " + std::to_string( gates.size() )
167 +
" gates of the clock tree has coordinates, expected " + expected );
171 for(
const Gate *gate : gates )
173 const auto successors = tree.
get_neighbors( gate, IGRAPH_OUT );
174 if( successors.is_error() )
176 return ERR(
"could not read the successors of gate with ID " + std::to_string( gate->get_id() ) +
": "
177 + successors.get_error().get() );
180 for(
const auto &[ptr,
type] : successors.get() )
187 const auto from = node_of_gate.find( gate );
188 const auto to = node_of_gate.find(
static_cast<const Gate *
>( ptr ) );
190 if( from == node_of_gate.end() ||
to == node_of_gate.end() )
196 layout.
edges.push_back( { from->second,
to->second } );
200 return OK( std::move( layout ) );
205 if( layout.
nodes.empty() )
207 return ERR(
"the layout has no gates to size the drawing after" );
211 return ERR(
"node_radius must be a positive number" );
213 if( !is_valid( overrides.
edge_width,
false ) )
215 return ERR(
"edge_width must be a positive number" );
219 return ERR(
"picture_margin must not be negative" );
223 return ERR(
"pixels_per_unit must be a positive number" );
226 const double length = typical_edge_length( layout );
std::vector< const Gate * > get_gates() const
Result< std::vector< std::pair< const void *, PtrType > > > get_neighbors(const void *ptr, igraph_neimode_t direction) const
std::optional< double > read_parameter_coordinate(const Gate *gate, const std::string &name)
Result< Layout > build_layout(const cte::ClockTree &tree, const std::optional< std::string > &x_key=std::nullopt, const std::optional< std::string > &y_key=std::nullopt)
Result< Style > resolve_style(const Layout &layout, const StyleOverrides &overrides)
std::vector< u32 > gates_without_coordinates
std::size_t omitted_edges
std::vector< Node > nodes
std::vector< Edge > edges
bool pixels_per_unit_is_fixed
std::optional< double > edge_width
std::optional< double > pixels_per_unit
std::optional< double > node_radius
std::optional< double > picture_margin