-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathablTransistorMapper.mjs
195 lines (167 loc) · 6.29 KB
/
ablTransistorMapper.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
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
import { Coordinate } from "./coordinate.mjs";
import { Pin } from "./pin.mjs";
import { Transistor } from "./transistor.mjs";
/**
* Class for mapping and geometrical transforming of ABL transistors to TikZ. It contains position information both for
* ABL and TikZ.
*
* @property {Transistor} tikzTransistor - the transistor used as stencil
* @property {Pin[]} pins - top, bottom and tap pin (ABL)
* @property {0 | 1 | 2} anchorNr - the selected anchor; 0=top, 1=bottom, 2=tap (ABL)
*/
class ABLTransistorMapper {
tikzTransistor;
pins;
anchorNr;
/**
* Generate the "converter" for an existing TikZ transistor stencil.
*
* @param {Transistor} tikzTransistor - the transistor used as stencil
* @param {Pin[]} pins - top, bottom and tap coordinate
* @param {0 | 1 | 2} anchorNr - the selected anchor; 0=top, 1=bottom, 2=tap
*/
constructor(tikzTransistor, pins, anchorNr) {
this.tikzTransistor = tikzTransistor;
this.pins = pins;
this.anchorNr = anchorNr;
}
/**
* Calculates the coordinate where the top-bottom line crosses the orthogonal tap line. This can but does not need
* to be the center point.
*
* @returns {Coordinate}
*/
get lineCrossingCoord() {
return this.pins[2].coord.orthogonalProjection(this.pins[0].coord, this.pins[1].coord);
}
/**
* Mirror coords/pins around the anchor.
*
* @param {boolean} mirrorX - true to mirror
* @param {boolean} mirrorY - true to mirror
*/
mirror(mirrorX, mirrorY) {
if (!(mirrorX || mirrorY)) return;
// Update mirror flags (xor)
this.mirrorX = this.mirrorX != mirrorX;
this.mirrorY = this.mirrorY != mirrorY;
const mirrorMid = this.pins[this.anchorNr].coord;
for (let i = 0; i < this.pins.length; i++) {
if (i !== this.anchorCoord) {
const mirrorCoord = this.pins[i].coord;
mirrorCoord.subtract(mirrorMid);
if (mirrorX) mirrorCoord.y = -mirrorCoord.y;
if (mirrorY) mirrorCoord.x = -mirrorCoord.x;
mirrorCoord.add(mirrorMid);
}
}
}
/**
* Rotate every coord around the anchor. The rotation is counter clockwise, like the default mathematical rotation.
*
* @param {number} angle
*/
rotate(angle) {
if (!Number.isFinite(angle) || angle === 0) return;
for (let i = 0; i < this.pins.length; i++) {
if (i !== this.anchorNr) this.pins[i].coord.rotate(angle);
}
}
/**
* Move all pins/coordinates using a vector.
*
* @param {Coordinate} vector - vector to add to all coordinates
*/
translate(vector) {
this.pins.forEach((pin) => pin.coord.add(vector));
}
/**
* Clones this instance. This is neither a shallow nor a deep clone. Only the pins are deeply cloned.
*
* @returns {ABLTransistorMapper} - the semi deep clone
*/
clone() {
return new ABLTransistorMapper(
this.tikzTransistor,
this.pins.map((pin) => pin.deepClone()),
this.anchorNr
);
}
/**
* Generate a TikZ component using `this` as an stencil. The parameters are informations extracted from ABL.
*
* @param {string} libraryName - the ABL library name, e.g. "ads_rflib"
* @param {string} cellName - the librarys component name, e.g. "R"
* @param {string} instanceName - the instance/component name, e.g. "R1"
* @param {Map<string,string>} attributes - map of all (XML) attributes
* @param {Pin[]} pins - the pins with their name and number (position not yet set)
* @param {{x: number, y: number, angle: number, xScale: number, yScale: number, mirrorX: boolean, mirrorY: boolean, scaling: number}} placement - general component placement information
* @param {Wire[]} wires - list of all wires
* @param {Map<string,Net>} nets - list of all nets
* @param {Coordinate[]} coords - list of all coordinates
*
* @returns {Transistor} the new Transistor
*/
useAsStencil(libraryName, cellName, instanceName, attributes, pins, placement, wires, nets, coords) {
// normalize angle --> -180 < angle <= 180
while (placement.angle <= -180) placement.angle += 360;
while (placement.angle > 180) placement.angle -= 360;
// get coordinate
let instanceCoord = new Coordinate(placement.x * placement.scaling, placement.y * placement.scaling);
const scaling = {
x: placement.xScale * placement.scaling,
y: placement.yScale * placement.scaling,
};
const ablTransistorClone = this.clone();
ablTransistorClone.pins.forEach((pin) => pin.coord.scale(scaling.x, scaling.y));
ablTransistorClone.rotate(placement.angle);
ablTransistorClone.mirror(placement.mirrorX, placement.mirrorY);
ablTransistorClone.translate(instanceCoord);
// scale
const transistor = this.tikzTransistor.deepClone();
transistor.rotate(placement.angle);
transistor.mirror(placement.mirrorX, placement.mirrorY);
transistor.translate(ablTransistorClone.lineCrossingCoord.clone().subtract(transistor.lineCrossingCoord));
if (global.DEBUG) {
// "Midpoint"
console.error("\\draw[Gold3] " + transistor.lineCrossingCoord.serializeName() + " circle [radius=1.2pt];");
// Initial pins of TikZ transistor (SearchHints)
transistor.pins.forEach((pin) =>
console.error("\\draw[Cornsilk4] " + pin.coord.serializeName() + " circle [radius=1.2pt];")
);
}
if (transistor.anchorNr != null && transistor.anchorCoord) {
const oldCoord = coords.find((existingCoord) => transistor.anchorCoord.equals(existingCoord));
if (oldCoord) transistor.anchorCoord = oldCoord;
else coords.push(transistor.anchorCoord);
}
// set pins (+positions)
pins.forEach((pin) => pin.findPosition(ablTransistorClone.pins, null, null, wires, nets, coords, false));
// reorder
pins = ablTransistorClone.pins.map((ablPin) =>
pins.find((pin) =>
pin.name && ablPin.name ? pin.name == ablPin.name : pin.instTermNumber === ablPin.instTermNumber
)
);
// Pins of ADS transistor
if (global.DEBUG)
pins.forEach((pin) =>
console.error("\\draw[SpringGreen4] " + pin.coord.serializeName() + " circle [radius=1.2pt];")
);
for (let i = 0; i < transistor.pins.length && i < pins.length; i++) {
pins[i].coord.x = transistor.pins[i].coord.x;
pins[i].coord.y = transistor.pins[i].coord.y;
}
// Pins of final TikZ Transistor
if (global.DEBUG)
pins.forEach((pin) =>
console.error(
"\\node at " + pin.coord.serializeName() + " {\\color{Turquoise3}\\pgfuseplotmark{Mercedes star}};"
)
);
transistor.pins = pins;
transistor.instanceName = instanceName;
return transistor;
}
}
export { ABLTransistorMapper };