summaryrefslogtreecommitdiff
path: root/hyp2mat/lib/copper.cc
blob: 3b5addd212da4583c5c51543caa4bd76f582f4fa (plain)
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
/*
 * hyp2mat - convert hyperlynx files to matlab scripts
 * Copyright 2012 Koen De Vleeschauwer.
 *
 * This file is part of hyp2mat.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "hyperlynx.h"
#include "crop.h"
#include <iostream>
#include <algorithm>

using namespace Hyp2Mat;

/*
 * These routines calculate the copper layers.
 * The routines operate in one of two modes: raw or normal.
 *
 * In raw mode, polygons are simply copied from input to output. No processing is done. Raw mode is mainly useful for debugging purposes.
 *
 * In normal mode, all copper on a single layer is calculated as a set of non-overlapping polygons.
 * Two kinds of copper polygons exist: normal and "PLANE". 
 *
 * Normal copper polygons are simply added to the copper of the plane.
 * Copper polygons of type "PLANE" have a clearance from other nets. This clearance is the "plane separation".
 * Plane separation can be specified for the board, for a layer, for a net or for a polygon.
 *
 * Copper polygons of type "PLANE" are calculated as follows:
 *  - all polygons of all other nets on the same layer are computed, and explanded by the plane separation. This produces the mask.
 *  - The mask is subtracted from the "PLANE" type polygon.
 *
 * If mask polygon and plane polygon have a different value for plane separation the largest value is used.
 *
 * Note there is no plane separation between polygons of the same net.
 */

/*
 * copy layer stackup
 */

void HyperLynx::CopyStackUp(Hyp2Mat::PCB& pcb)
{
  /* iterate over all layers */
  for (HypFile::LayerList::iterator l = hyp_file.stackup.begin(); l != hyp_file.stackup.end(); ++l) {

    /* check if we're interested in this layer. if no layers are specified, copy all layers */
    bool layer_wanted = layers.empty() || (std::find(layers.begin(), layers.end(), l->layer_name) != layers.end());

    /* copy this layer if needed */
    if (layer_wanted) CopyLayer(pcb, *l);
    }

  return;
}

/*
 * copy layer
 */

void HyperLynx::CopyLayer(Hyp2Mat::PCB& pcb, HypFile::Layer& hyp_layer)
{

  Hyp2Mat::Layer layer;
  layer.layer_name = hyp_layer.layer_name;
  layer.material_name = hyp_layer.material_name;
  switch (hyp_layer.layer_type) {
    case HypFile::LAYER_SIGNAL :     layer.layer_type = Hyp2Mat::LAYER_SIGNAL;
      break;
    case HypFile::LAYER_DIELECTRIC : layer.layer_type = Hyp2Mat::LAYER_DIELECTRIC;
      break;
    case HypFile::LAYER_PLANE :      layer.layer_type = Hyp2Mat::LAYER_PLANE;
      break;
    default:                         layer.layer_type = Hyp2Mat::LAYER_SIGNAL;
      break;
    }
  layer.z0 = hyp_layer.z0;
  layer.z1 = hyp_layer.z1;
  layer.thickness = hyp_layer.thickness;
  layer.epsilon_r = hyp_layer.epsilon_r;
  layer.loss_tangent = hyp_layer.loss_tangent;
  layer.bulk_resistivity = hyp_layer.bulk_resistivity;
  layer.resistivity_temp_coeff = hyp_layer.temperature_coefficient;

  /* if the layer is metallic, copy the metal */
  if (layer.layer_type != Hyp2Mat::LAYER_DIELECTRIC) {

    /* Use default plane separation if layer plane separation is not set. layer plane separation is -1.0 if not set */
    layer_plane_separation = hyp_file.board.plane_separation;

    /* board-level override from SetClearance method / --clearance command-line parameter */
    if (HyperLynx::clearance >= 0) layer_plane_separation = HyperLynx::clearance;

    if (hyp_layer.plane_separation >= 0) layer_plane_separation = hyp_layer.plane_separation;

    /* copy layer copper */
    Hyp2Mat::FloatPolygons raw_polygons;
    Hyp2Mat::Polygon layer_copper = CopyCopper(layer, raw_polygons);
   
    if (raw)
      layer.metal = raw_polygons;
    else 
      layer.metal = layer_copper.Result(); /* calculate layer copper */
    }

  /* add to stackup */
  pcb.stackup.push_back(layer);

  return;
}

/*
 * copy copper
 */

Hyp2Mat::Polygon HyperLynx::CopyCopper(Hyp2Mat::Layer layer, Hyp2Mat::FloatPolygons& raw_polygons)
{

  /* vectors of different polygon types. element [i] belongs to net hyp_file.net[i].net_name */
  std::vector<Hyp2Mat::Polygon> net_pour;      /* POLYGON T=POUR  and ordinary line and arc segments */
  std::vector<Hyp2Mat::Polygon> net_plane;     /* POLYGON T=PLANE */
  std::vector<Hyp2Mat::Polygon> net_copper;    /* POLYGON T=COPPER */
  std::vector<Hyp2Mat::Polygon> net_pads;      /* pads */
  std::vector<Hyp2Mat::Polygon> net_antipads;  /* anti-pads */
  std::vector<bool> net_wanted;                /* true if net needs to be included in the output */

  HypFile::NetList::size_type net_size =  hyp_file.net.size();
  net_pour.resize(net_size);
  net_plane.resize(net_size);
  net_copper.resize(net_size);
  net_pads.resize(net_size);
  net_antipads.resize(net_size);
  net_wanted.resize(net_size);

  /* iterate over all nets twice.
     The first time we calculate all the copper except "plane" polygons. 
     The second time we calculate plane polygon pours. Plane polygons have a clearance from copper of other nets */

  /* Determine which nets we need to include */
  for (int i = 0; i < hyp_file.net.size(); ++i) {
    /* check if we're interested in this net. if no nets are specified, copy all nets */
    net_wanted[i] = nets.empty() || (std::find(nets.begin(), nets.end(), hyp_file.net[i].net_name) != nets.end());
    }

  /* calculate all copper. */
  for (int i = 0; i < hyp_file.net.size(); ++i) {

    /* skip unwanted nets */
    if (!net_wanted[i]) continue;

    /* copy anti-metal of this net */
    net_pour[i] = CopyNet(layer, hyp_file.net[i], POLYGON_TYPE_POUR, raw_polygons); 
    net_copper[i] = CopyNet(layer, hyp_file.net[i], POLYGON_TYPE_COPPER, raw_polygons); 
    net_plane[i] = CopyNet(layer, hyp_file.net[i], POLYGON_TYPE_PLANE, raw_polygons); 
    net_pads[i] = CopyNet(layer, hyp_file.net[i], POLYGON_TYPE_PAD, raw_polygons); 
    net_antipads[i] = CopyNet(layer, hyp_file.net[i], POLYGON_TYPE_ANTIPAD, raw_polygons); 
    }

  /*
   * sum all copper on this layer 
   */ 

  Hyp2Mat::Polygon layer_copper;

  /* add plane and pour polygons */
  for (int i = 0; i < hyp_file.net.size(); ++i) {

    /* skip unwanted nets */
    if (!net_wanted[i]) continue;

    layer_copper.Union(net_pour[i]);
    layer_copper.Union(net_plane[i]);
    }

  /* subtract antipads from pour and plane polygons. */
  for (int i = 0; i < hyp_file.net.size(); ++i) {

    /* skip unwanted nets */
    if (!net_wanted[i]) continue;

    layer_copper.Difference(net_antipads[i]);
    }

  /* add copper and pad polygons */
  for (int i = 0; i < hyp_file.net.size(); ++i) {

    /* skip unwanted nets */
    if (!net_wanted[i]) continue;

    layer_copper.Union(net_copper[i]);
    layer_copper.Union(net_pads[i]);
    }

  /*
   * Crop to board size 
   */

  if (!board.IsEmpty()) {
    layer_copper.Intersection(board);
    layer_copper = CropPolygon(layer_copper, bounds);
    }

  return layer_copper;
}

/*
 * copy net
 * copy all polygons of a net.
 * if anti is true, copy anti-pads
 * if plane is true, copy plane polygons (and subtract other_nets)
 */

Hyp2Mat::Polygon HyperLynx::CopyNet(Hyp2Mat::Layer layer, HypFile::Net& hyp_net, polygon_type_enum poly_type, Hyp2Mat::FloatPolygons& raw_polygons)
{
  Hyp2Mat::Polygon net_copper;

  /* calculate net-specific plane separation */
  net_plane_separation = layer_plane_separation;
  if (hyp_net.plane_separation >= 0) net_plane_separation = hyp_net.plane_separation;

  /* iterate over all Hyperlynx polygon id's */
  for (HypFile::PolygonMap::iterator j =  hyp_net.metal.begin(); j != hyp_net.metal.end(); ++j) {

    if (j->second.empty()) continue;

    /* polygon layer */
    std::string poly_layer_name = j->second.begin()->layer_name;
  
    /* this polygon is on our layer if the polygon's layer name is the current layer's name.*/
    if (poly_layer_name != layer.layer_name) continue;
 
    /* Check polygon is the correct type (pour, plane, copper, pad or antipad) */
    if (j->second.begin()->polygon_type != poly_type) continue;

    /* Join all Hyperlynx polygons/polyvoids with the same id in a single Hyp2Mat polygon */
    Hyp2Mat::Polygon poly_copper = CopyPolygon(j->second, raw_polygons);
    net_copper.Union(poly_copper);
    }

  return net_copper;
}

/*
 * copy polygon
 * polygon is copied twice; once to the list of "raw" polygons for --raw output; 
 * once added to this layers' copper for normal output.
 */

Hyp2Mat::Polygon HyperLynx::CopyPolygon(HypFile::PolygonList metal, Hyp2Mat::FloatPolygons& raw_polygons)
{
  Hyp2Mat::Polygon poly;

  if (metal.empty()) return poly; /* ought never to happen */

  /* Join all Hyperlynx polygons/polyvoids with the same id in a single Hyp2Mat polygon */
  for (HypFile::PolygonList::iterator k = metal.begin(); k != metal.end(); ++k) {

    /* copy vertices */
    Hyp2Mat::FloatPolygon edge;
    for (HypFile::PointList::iterator v = k->vertex.begin(); v != k->vertex.end(); ++v)
      edge.push_back(Hyp2Mat::FloatPoint(v->x, v->y));

    /* fix orientation */
    if (IsClockwise(edge) != k->positive) Reverse(edge);

    /* add to raw polygons */
    Hyp2Mat::FloatPoly raw_poly;
    raw_poly.poly = edge;
    raw_poly.is_hole = !k->positive;
    raw_poly.nesting_level = 0;
    raw_polygons.push_back(raw_poly);

    /* add to cooked polygons */
    if (k->positive) poly.AddEdge(edge);
    else poly.AddHole(edge);
    }

  /* take line width of polygon into account */
  double width = metal.begin()->width;
  poly.Simplify();
  poly.Offset(width/2);

  if (metal.front().polygon_type == POLYGON_TYPE_PLANE) {
    /* calculate polygon-specific plane separation */
    polygon_plane_separation = net_plane_separation;
    if (metal.front().plane_separation >= 0) polygon_plane_separation = metal.front().plane_separation;

    /* calculate mask needed to get clearance with other nets */
    Hyp2Mat::Polygon mask;
    mask = PlaneSeparation(metal.front().layer_name, metal.front().net_name);

    /* subtract mask from polygon */
    poly.Difference(mask);
    }

  return poly;
}

/*
 * Creates the mask by which to trim a plane polygon, taking into account plane separation.
 *
 * Algorithm: sum all polygons on layer 'layer_name' belonging to nets other than net 'net_name', 
 * expanded by the biggest polygon_plane separation.
 */

Hyp2Mat::Polygon HyperLynx::PlaneSeparation(std::string layer_name, std::string net_name)
{
  Hyp2Mat::Polygon mask;

  if (polygon_plane_separation < 0) return mask; // XXX correct?

  /* iterate over all nets */
  for (HypFile::NetList::iterator i = hyp_file.net.begin(); i != hyp_file.net.end(); ++i) {

    /* all nets except net 'net_name' */
    if (i->net_name == net_name) continue;

    /* wanted nets only */
    bool net_wanted = nets.empty() || (std::find(nets.begin(), nets.end(), net_name) != nets.end());
    if (!net_wanted) continue;

    /* iterate over all Hyperlynx polygon id's */
    for (HypFile::PolygonMap::iterator j =  i->metal.begin(); j != i->metal.end(); ++j) {
      Hyp2Mat::Polygon poly;

      if (j->second.empty()) continue; /* ought never to happen */

      /* polygons on layer 'layer_name' only */
      if (j->second.begin()->layer_name != layer_name) continue;

      /* iterate over all edges */
      for (HypFile::PolygonList::iterator k = j->second.begin(); k != j->second.end(); ++k) {

        /* iterate over all vertices */
        Hyp2Mat::FloatPolygon edge;
        for (HypFile::PointList::iterator v = k->vertex.begin(); v != k->vertex.end(); ++v)
          edge.push_back(Hyp2Mat::FloatPoint(v->x, v->y));

        /* add edge to polygon */
        if (k->positive) poly.AddEdge(edge);
        else poly.AddHole(edge);
        }

      double width = j->second.begin()->width;

      /* calculate plane separation of the masking polygon */
      double mask_plane_separation;
      mask_plane_separation = layer_plane_separation; /* layer */
      if (i->plane_separation >= 0) mask_plane_separation = i->plane_separation; /* net */
      if (j->second.begin()->plane_separation >= 0) mask_plane_separation = j->second.begin()->plane_separation; /* polygon */

      /* compare plane separation of polygon and masking polygon; choose biggest plane separation. */
      double biggest_plane_separation;
      if (polygon_plane_separation > mask_plane_separation) biggest_plane_separation = polygon_plane_separation;
      else biggest_plane_separation = mask_plane_separation;

      /* grow masking polygon by plane separation */
      poly.Offset(width/2 + biggest_plane_separation);

      /* add to mask */
      mask.Union(poly);
      
      }

    }

  return mask;  
}

/* not truncated */