-
Notifications
You must be signed in to change notification settings - Fork 185
/
dbHierarchyBuilder.h
383 lines (320 loc) · 14.7 KB
/
dbHierarchyBuilder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
KLayout Layout Viewer
Copyright (C) 2006-2020 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef HDR_dbHierarchyBuilder
#define HDR_dbHierarchyBuilder
#include "dbCommon.h"
#include "dbRecursiveShapeIterator.h"
#include "dbLayout.h"
#include <map>
#include <vector>
#include <set>
namespace db
{
/**
* @brief A helper function comparing two recursive shape iterators for compatibility with respect to hierarchy building
*
* This function will return -1, 0 or 1 depending on whether the two iterators
* can be used with the same builder (0) or whether they are less (-1) or greater (1).
*/
int DB_PUBLIC compare_iterators_with_respect_to_target_hierarchy (const db::RecursiveShapeIterator &iter1, const db::RecursiveShapeIterator &iter2);
/**
* @brief A class to receive shapes from the hierarchy builder
*
* This class can be reimplemented to implement clipping and/or
* simplification.
*/
class DB_PUBLIC HierarchyBuilderShapeReceiver
{
public:
HierarchyBuilderShapeReceiver () { }
virtual ~HierarchyBuilderShapeReceiver () { }
virtual void push (const db::Shape &shape, const db::ICplxTrans &trans, const db::Box ®ion, const db::RecursiveShapeReceiver::box_tree_type *complex_region, db::Shapes *target) = 0;
virtual void push (const db::Box &shape, const db::ICplxTrans &trans, const db::Box ®ion, const db::RecursiveShapeReceiver::box_tree_type *complex_region, db::Shapes *target) = 0;
virtual void push (const db::Polygon &shape, const db::ICplxTrans &trans, const db::Box ®ion, const db::RecursiveShapeReceiver::box_tree_type *complex_region, db::Shapes *target) = 0;
};
/**
* @brief A shape receiver that simply pushes into the target
*/
class DB_PUBLIC HierarchyBuilderShapeInserter
: public HierarchyBuilderShapeReceiver
{
public:
HierarchyBuilderShapeInserter () { }
virtual void push (const db::Shape &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target)
{
tl::ident_map<db::Layout::properties_id_type> pm;
target->insert (shape, trans, pm);
}
virtual void push (const db::Box &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target)
{
if (trans.is_ortho ()) {
target->insert (shape.transformed (trans));
} else {
db::Polygon poly (shape);
target->insert (poly.transformed (trans));
}
}
virtual void push (const db::Polygon &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target)
{
if (trans.is_unity ()) {
target->insert (shape);
} else {
target->insert (shape.transformed (trans));
}
}
};
/**
* @brief A clipping shape receiver that forwards to another one
*/
class DB_PUBLIC ClippingHierarchyBuilderShapeReceiver
: public HierarchyBuilderShapeReceiver
{
public:
ClippingHierarchyBuilderShapeReceiver (HierarchyBuilderShapeReceiver *pipe = 0);
virtual void push (const db::Shape &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Box &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Polygon &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
private:
void insert_clipped (const db::Box &box, const db::ICplxTrans &trans, const db::Box ®ion, const db::RecursiveShapeReceiver::box_tree_type *complex_region, db::Shapes *target);
void insert_clipped (const db::Polygon &poly, const db::ICplxTrans &trans, const db::Box ®ion, const db::RecursiveShapeReceiver::box_tree_type *complex_region, db::Shapes *target);
static bool is_inside (const db::Box &box, const db::Box ®ion, const db::RecursiveShapeReceiver::box_tree_type *complex_region);
static bool is_outside (const db::Box &box, const db::Box ®ion, const db::RecursiveShapeReceiver::box_tree_type *complex_region);
HierarchyBuilderShapeReceiver *mp_pipe;
};
/**
* @brief A polygon reducing shape receiver that forwards to another one
*/
class DB_PUBLIC ReducingHierarchyBuilderShapeReceiver
: public HierarchyBuilderShapeReceiver
{
public:
ReducingHierarchyBuilderShapeReceiver (HierarchyBuilderShapeReceiver *pipe = 0, double area_ratio = 3.0, size_t max_vertex_count = 16);
virtual void push (const db::Shape &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Box &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Polygon &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
private:
void reduce (const db::Polygon &poly, const db::ICplxTrans &trans, const db::Box ®ion, const db::RecursiveShapeReceiver::box_tree_type *complex_region, db::Shapes *target);
HierarchyBuilderShapeReceiver *mp_pipe;
double m_area_ratio;
size_t m_max_vertex_count;
};
/**
* @brief A polygon reference generating shape receiver that feeds a shapes array after turning the shapes into PolygonRefs
*/
class DB_PUBLIC PolygonReferenceHierarchyBuilderShapeReceiver
: public HierarchyBuilderShapeReceiver
{
public:
PolygonReferenceHierarchyBuilderShapeReceiver (db::Layout *layout, int text_enlargement = -1, const tl::Variant &text_prop_name = tl::Variant ());
virtual void push (const db::Shape &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Box &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Polygon &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
private:
db::Layout *mp_layout;
int m_text_enlargement;
bool m_make_text_prop;
db::property_names_id_type m_text_prop_id;
};
/**
* @brief An edge-generating shape receiver that feeds a shapes array after turning the shapes into edges
*/
class DB_PUBLIC EdgeBuildingHierarchyBuilderShapeReceiver
: public HierarchyBuilderShapeReceiver
{
public:
EdgeBuildingHierarchyBuilderShapeReceiver (bool as_edges);
virtual void push (const db::Shape &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Box &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Polygon &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
private:
bool m_as_edges;
};
/**
* @brief An edge pair-generating shape receiver that feeds a shapes array after turning the shapes into edges
*/
class DB_PUBLIC EdgePairBuildingHierarchyBuilderShapeReceiver
: public HierarchyBuilderShapeReceiver
{
public:
EdgePairBuildingHierarchyBuilderShapeReceiver ();
virtual void push (const db::Shape &shape, const db::ICplxTrans &trans, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *target);
virtual void push (const db::Box &, const db::ICplxTrans &, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *) { }
virtual void push (const db::Polygon &, const db::ICplxTrans &, const db::Box &, const db::RecursiveShapeReceiver::box_tree_type *, db::Shapes *) { }
};
/**
* @brief A class building a hierarchy from a recursive shape iterator in push mode
*
* This class is a RecursiveShapeReceiver which acts on the hierarchy events and
* uses them to rebuild a hierarchy in the target layout. In can be used multiple
* times and will reuse the hierarchy as far as possible.
*
* The hierarchy builder can form clip variants for cells and clip the shapes
* according to the selected region.
*
* NOTE: the hierarchy build should not be used in multiple passes with regions
* as the hierarchy is sampled in the first pass and the hierarchy builder will
* rely on precisely the same hierarchy arrangement. This is not given with
* region selections.
*/
class DB_PUBLIC HierarchyBuilder
: public db::RecursiveShapeReceiver
{
public:
struct CellMapKey
{
CellMapKey ()
: original_cell (0), inactive (false)
{ }
CellMapKey (db::cell_index_type _original_cell, bool _inactive, const std::set<db::Box> &_clip_region)
: original_cell (_original_cell), inactive (_inactive), clip_region (_clip_region)
{ }
bool operator== (const CellMapKey &other) const
{
return original_cell == other.original_cell && inactive == other.inactive && clip_region == other.clip_region;
}
bool operator< (const CellMapKey &other) const
{
if (original_cell != other.original_cell) { return original_cell < other.original_cell; }
if (inactive != other.inactive) { return inactive < other.inactive; }
if (clip_region != other.clip_region) { return clip_region < other.clip_region; }
return false;
}
db::cell_index_type original_cell;
bool inactive;
std::set<db::Box> clip_region;
};
typedef std::map<CellMapKey, db::cell_index_type> cell_map_type;
typedef std::map<db::cell_index_type, std::vector<db::cell_index_type> > original_target_to_variants_map_type;
typedef std::map<db::cell_index_type, db::cell_index_type> variant_to_original_target_map_type;
HierarchyBuilder (db::Layout *target, unsigned int target_layer, const db::ICplxTrans &trans = db::ICplxTrans (), HierarchyBuilderShapeReceiver *pipe = 0);
HierarchyBuilder (db::Layout *target, const db::ICplxTrans &trans = db::ICplxTrans (), HierarchyBuilderShapeReceiver *pipe = 0);
virtual ~HierarchyBuilder ();
/**
* @brief Installs a custom shape receiver
* The hierarchy builder will *NOT* take ownership of this object.
*/
void set_shape_receiver (HierarchyBuilderShapeReceiver *pipe);
virtual void begin (const RecursiveShapeIterator *iter);
virtual void end (const RecursiveShapeIterator *iter);
virtual void enter_cell (const RecursiveShapeIterator *iter, const db::Cell *cell, const db::Box ®ion, const box_tree_type *complex_region);
virtual void leave_cell (const RecursiveShapeIterator *iter, const db::Cell *cell);
virtual new_inst_mode new_inst (const RecursiveShapeIterator *iter, const db::CellInstArray &inst, const db::Box ®ion, const box_tree_type *complex_region, bool all);
virtual bool new_inst_member (const RecursiveShapeIterator *iter, const db::CellInstArray &inst, const db::ICplxTrans &trans, const db::Box ®ion, const box_tree_type *complex_region, bool all);
virtual void shape (const RecursiveShapeIterator *iter, const db::Shape &shape, const db::ICplxTrans &trans, const db::Box ®ion, const box_tree_type *complex_region);
/**
* @brief Sets the target layer - shapes will be put there
*/
void set_target_layer (unsigned int target_layer)
{
m_target_layer = target_layer;
}
/**
* @brief Reset the builder - performs a new initial pass
*/
void reset ();
/**
* @brief Gets the initial cell the builder produced in the target layout
*/
db::Cell *initial_cell ()
{
return mp_initial_cell;
}
/**
* @brief Gets the target layout
*/
db::Layout *target ()
{
return mp_target.get ();
}
/**
* @brief Gets the recursive shape iterator the data was taken from
*/
const db::RecursiveShapeIterator &source () const
{
return m_source;
}
/**
* @brief Gets the iterator for the cell map
*/
cell_map_type::const_iterator begin_cell_map () const
{
return m_cell_map.begin ();
}
/**
* @brief Gets the iterator for the cell map (end)
*/
cell_map_type::const_iterator end_cell_map () const
{
return m_cell_map.end ();
}
/**
* @brief Unmaps an original cell/clip box version from the original-to-working copy cell map
*
* An unmapped cell is never again considered.
*/
void unmap (const cell_map_type::key_type &k)
{
m_cell_map.erase (k);
}
/**
* @brief Maps an original cell/clip box version to a original-to-working copy cell
*/
void map (const cell_map_type::key_type &k, db::cell_index_type ci)
{
m_cell_map [k] = ci;
}
/**
* @brief Marks a cell as a variant of another
*
* The first cell is either the original, non-variant target cell or itself a variant.
* The second cell will be registered as a variant of the first one.
*/
void register_variant (db::cell_index_type non_var, db::cell_index_type var);
/**
* @brief Unregisters a cell as a variant
*/
void unregister_variant (db::cell_index_type var);
/**
* @brief Gets a value indicating whether the given cell is a variant cell
*/
bool is_variant (db::cell_index_type ci) const
{
return m_variants_to_original_target_map.find (ci) != m_variants_to_original_target_map.end ();
}
/**
* @brief Gets the original target for a variant cell
*/
db::cell_index_type original_target_for_variant (db::cell_index_type ci) const;
private:
db::cell_index_type make_cell_variant (const HierarchyBuilder::CellMapKey &key, const std::string &cell_name);
tl::weak_ptr<db::Layout> mp_target;
HierarchyBuilderShapeReceiver *mp_pipe;
bool m_initial_pass;
db::RecursiveShapeIterator m_source;
cell_map_type m_cell_map;
original_target_to_variants_map_type m_original_targets_to_variants_map;
variant_to_original_target_map_type m_variants_to_original_target_map;
std::set<cell_map_type::key_type> m_cells_seen;
std::set<db::cell_index_type> m_cells_to_be_filled;
cell_map_type::const_iterator m_cm_entry;
bool m_cm_new_entry;
unsigned int m_target_layer;
std::vector<std::pair<bool, std::vector<db::Cell *> > > m_cell_stack;
db::Cell *mp_initial_cell;
db::ICplxTrans m_trans;
};
}
#endif