-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpin.mjs
130 lines (120 loc) · 3.98 KB
/
pin.mjs
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
import { Coordinate } from "./coordinate.mjs";
import { Net } from "./net.mjs";
/**
* Class representing a component pin.
*
* @property {Coordinate} coord - the coresponding coordinate
* @property {string} name - name of the pin, e.g. "gate"
* @property {number} instTermNumber - number for matching with ADS pins
* @property {Net} net - the correspondind net/potential
*/
class Pin {
coord;
name;
instTermNumber;
net;
/**
* Generates a pin.
*
* @param {Coordinate} coord - the coresponding coordinate
* @param {string} name - name of the pin, e.g. "gate"
* @param {number} instTermNumber - number for matching with ADS pins
* @param {Net} net - the correspondind net/potential
*/
constructor(coord, name, instTermNumber, net) {
this.coord = coord;
this.name = name;
this.instTermNumber = instTermNumber || 0;
this.net = net;
}
/**
* Clones the instance.
*
* <em>This is not a deep clone. Neither coord nor net is cloned.</em>
*
* @returns {Pin} the new instance
*/
clone() {
return new Pin(this.coord, this.name, this.instTermNumber, this.net);
}
/**
* Clones the insstance with the param coord.
*
* <em>The param net will not be cloned.</em>
*
* @returns {Pin} the cloned pin
*/
deepClone() {
const clone = this.clone();
clone.coord = clone.coord.clone();
return clone;
}
/**
* Search the position of this pin using hints from existing wires, coordinates and the main coordinate of the
* component.
*
* Creates a new coordinate if no existing one is found in a certin distance.
*
* @param {{x: number, y: number, angle: number, xScale: number, yScale: number, mirrorX: boolean, mirrorY: boolean, scaling: number}} placement - general component placement information
* @param {Pin[]} [pins=[]] - list of all pins
* @param {Coordinate} instanceCoord - the component position
* @param {Wire[]} wires - list of all wires
* @param {Net[]} _nets - list of all nets
* @param {Coordinate[]} coords - list of all coordinates
* @param {boolean} [relativeCoords=true] - set to false if the pin should not be scaled, mirrored, etc.
* @returns {Pin} the new pin
*/
findPosition(pins = [], placement, instanceCoord, wires, _nets, coords, relativeCoords = true) {
// set pin placement hints
if (pins){
let pin = pins.find((stencilPin) => stencilPin.instTermNumber == this.instTermNumber);
let pinCoord = pin ? pin.coord : null;
this.coord = pinCoord ? pinCoord.clone() : null;
}
if (relativeCoords) {
if (this.coord) {
// found --> scale
this.coord.scale(placement.xScale * placement.scaling, placement.yScale * placement.scaling);
this.coord.rotate(placement.angle);
if (placement.mirrorX) this.coord.mirrorX();
if (placement.mirrorY) this.coord.mirrorY();
this.coord.add(instanceCoord); // relative -> absolute position
} else {
this.coord = instanceCoord; // no search hint found -> use component position
}
}
// search real position
/** @typedef {{distance: number, coord: (Coordinate|null)}} distanceCoordStruct */
/** @type {distanceCoordStruct[]} */
const possibleCoords =
(this.net &&
wires
.filter((wire) => wire.net == this.net)
.map((wire) =>
wire.coords.reduce(
(prevVal, coord) => {
const distance = this.coord.getDistance(coord);
if (prevVal.distance < distance) return prevVal;
else return { distance: distance, coord: coord };
},
{ distance: 1 /* max search distance */, coord: null }
)
)) ||
[];
let coordStruct = possibleCoords.sort((a, b) => a.distance - b.distance)[0];
if (coordStruct && coordStruct.coord) {
this.coord = coordStruct.coord;
// DEBUG:
// console.log("found");
} else {
// no existing wire coord found -> create new one
// DEBUG:
// console.log("not found");
const existingCoord = coords.find((existingCoord) => this.coord.equals(existingCoord));
if (existingCoord) this.coord = existingCoord;
else coords.push(this.coord);
}
return this;
}
}
export { Pin };