PCB Environment 2

Loading...
Searching...
No Matches
NavPoint.hpp
1#ifndef GYM_PCB_NAVPOINT_H
2#define GYM_PCB_NAVPOINT_H
3
4#include "Geometry.hpp"
5#include "GridDirection.hpp"
6#include "UniformGrid25.hpp"
7
9
12
13 class NavGrid;
14
20#ifndef GYM_PCB_NAVPOINT_NUM_COST_TERMS
21#define GYM_PCB_NAVPOINT_NUM_COST_TERMS 2
22#endif
23
25#define NAV_POINT_FLAG_BLOCKED_TEMPORARY (1 << 0) // blockages other than pins or tracks
26#define NAV_POINT_FLAG_BLOCKED_PERMANENT (1 << 8) // holes etc.
27#define NAV_POINT_FLAG_NO_VIAS (1 << 1) // blockages other than pins or tracks affecting vias only
28#define NAV_POINT_FLAG_INSIDE_PIN (1 << 2) // cells covered by a pin
29#define NAV_POINT_FLAG_INSIDE_COMPONENT (1 << 3) // cells inside a component footprint
30#define NAV_POINT_FLAG_PIN_TRACK_CLEARANCE (1 << 4) // keepout area for tracks stemming from pins
31#define NAV_POINT_FLAG_PIN_VIA_CLEARANCE (1 << 5) // keepout area for vias stemming from pins
32#define NAV_POINT_FLAG_ROUTE_TRACK_CLEARANCE (1 << 6) // keepout area for tracks stemming from other tracks
33#define NAV_POINT_FLAG_ROUTE_VIA_CLEARANCE (1 << 7) // keepout area for vias stemming from other tracks
34#define NAV_POINT_FLAG_SOURCE (1 << 9) // for use by A*
35#define NAV_POINT_FLAG_TARGET (1 << 10) // for use by A*
36#define NAV_POINT_FLAG_ROUTE_GUARD (1 << 11) // to restrict routing area
37
39#define NAV_POINT_FLAGS_PIN_CLEARANCE 0x030 // PIN_TRACK|VIA_CLEARANCE
40#define NAV_POINT_FLAGS_ROUTE_CLEARANCE 0x0c0 // ROUTE_TRACK|VIA_CEARANCE
41#define NAV_POINT_FLAGS_TRACK_CLEARANCE 0x050 // PIN|ROUTE_TRACK_CLEARANCE
42#define NAV_POINT_FLAGS_VIA_CLEARANCE 0x0a0 // PIN|ROUTE_VIA_CLEARANCE
43#define NAV_POINT_FLAGS_CLEARANCE 0x0f0 // *_CLEARANCE
44#define NAV_POINT_FLAGS_TRACKS_BLOCKED 0x951 // BLOCKED|TRACK_CLEARANCE|GUARD
45#define NAV_POINT_FLAGS_VIAS_BLOCKED 0x9f3 // BLOCKED|NO_VIAS|CLEARANCE|GUARD
46#define NAV_POINT_FLAGS_VIAS_BLOCKED_P 0x9f2 // BLOCKED_PERMANENT|NO_VIAS|CLEARANCE|GUARD
47#define NAV_POINT_FLAGS_ENDPOINT 0x600 // SOURCE|TARGET
48#define NAV_POINT_FLAGS_ALL 0xfff
49
52 class AStarVisitStatus
53{
54 public:
55 void reset() { mSeq = 0; }
56 void setOpen(uint16_t seq) { mSeq = seq; }
57 void setDone(uint16_t seq) { assert(isOpen(seq)); mSeq = seq | 0x8000; }
58 bool isSeen(uint16_t seq) const { return (mSeq & 0x7fff) == seq; }
59 bool isOpen(uint16_t seq) const { return mSeq == seq; }
60 bool isDone(uint16_t seq) const { return mSeq == (seq | 0x8000); }
61 private:
62 uint16_t mSeq{0};
63};
64
70 struct NavKeepoutCounts
71{
72 void setPin(int8_t n) { _RouteTracks = 0; _RouteVias = 0; _PinTracks = n; _PinVias = n; Sum = 2 * n; }
73 void setPin(int8_t n, int8_t m) { _RouteTracks = 0; _RouteVias = 0; _PinTracks = n; _PinVias = m; Sum = n + m; }
74 void setPinTracks(int8_t n) { _RouteTracks = 0; _RouteVias = 0; _PinTracks = n; _PinVias = 0; Sum = n; }
75 void setPinVias(int8_t n) { _RouteTracks = 0; _RouteVias = 0; _PinTracks = 0; _PinVias = n; Sum = n; }
76 void setRoute(int8_t n) { _RouteTracks = n; _RouteVias = n; _PinTracks = 0; _PinVias = 0; Sum = 2 * n; }
77 void setRoute(int8_t n, int8_t m) { _RouteTracks = n; _RouteVias = m; _PinTracks = 0; _PinVias = 0; Sum = n + m; }
78 void setRouteTracks(int8_t n) { _RouteTracks = n; _RouteVias = 0; _PinTracks = 0; _PinVias = 0; Sum = n; }
79 void setRouteVias(int8_t n) { _RouteTracks = 0; _RouteVias = n; _PinTracks = 0; _PinVias = 0; Sum = n; }
80 void reset() { _RouteTracks = 0; _RouteVias = 0; _PinTracks = 0; _PinVias = 0; Sum = 0; }
81 bool isZero() const { return _RouteTracks == 0 && _RouteVias == 0 && _PinTracks == 0 && _PinVias == 0 && RRRTrack == 0; }
82 int8_t _RouteTracks{0};
83 int8_t _RouteVias{0};
84 int8_t _PinTracks{0};
85 int8_t _PinVias{0};
86 int8_t Sum{0};
87 int8_t RRRTrack{0};
88};
89
92 struct NavRasterizeParams
93{
94 Real Expansion{0.0};
95 uint16_t IgnoreMask{0};
96 uint16_t FlagsAnd{0xffff};
97 uint16_t FlagsOr{0};
98 mutable uint16_t WriteSeq;
99 bool AutoExpand{false};
100 bool AutoIncrementWriteSeq{true};
101 NavKeepoutCounts KOCount;
102 int8_t TrackRasterCount{0};
103
104 bool isNoop() const;
105};
106 inline bool NavRasterizeParams::isNoop() const
107{
108 return (FlagsAnd == 0xffff) && (FlagsOr == 0x0) && KOCount.isZero();
109}
110
115 class NavPoint
116{
117 public:
118 void setRefPoint(int x, int y, int z) { mRefX = x; mRefY = y; mLayer = z; assert(x >= 0 && x <= 32767); assert(y >= 0 && y <= 32767); assert(z >= 0 && z <= 65535); }
119
120 Point_2 getRefPoint(const UniformGrid25 *nav) const { return nav->MidPoint(mRefX, mRefY); }
121 Point_25 getRefPoint25(const UniformGrid25 *nav) const { return Point_25(getRefPoint(nav), mLayer); }
122 uint8_t getLayer() const { return mLayer; }
123
124 bool hasEdge(GridDirection d) const { return mEdgeMask & (1 << d.n()); }
125 NavPoint *getEdge(NavGrid&, GridDirection d) const;
126 void setEdge(GridDirection d, bool mask);
127
128 Segment_25 getSegmentTo(const NavPoint &v, const UniformGrid25 *nav) const { return Segment_25(getRefPoint(nav), v.getRefPoint(nav), mLayer); }
129
130 const NavKeepoutCounts& getKOCounts() const { return mKOCount; }
131 NavKeepoutCounts& getKOCounts() { return mKOCount; }
132 void resetKO() { mKOCount.reset(); clearFlags(NAV_POINT_FLAGS_CLEARANCE); }
133 bool canRoute() const { return !(mFlags & NAV_POINT_FLAGS_TRACKS_BLOCKED); }
134 bool canPlaceVia() const { return !(mFlags & NAV_POINT_FLAGS_VIAS_BLOCKED); }
135 bool canPlaceViaEver() const { return !(mFlags & NAV_POINT_FLAGS_VIAS_BLOCKED_P); }
136 bool canAddVia(const NavPoint &to) const;
137 bool canAddViaEver(const NavPoint &to) const;
138 bool insidePin() const { return (mFlags & NAV_POINT_FLAG_INSIDE_PIN); }
139 bool hasFlags(uint16_t mask) const { return !!(mFlags & mask); }
140 uint16_t getFlags() const { return mFlags; }
141 void setFlags(uint16_t mask) { mFlags |= mask; }
142 void clearFlags(uint16_t mask) { mFlags &= ~mask; }
143 void saveFlags() { mFlagsSaved = mFlags; }
144 void restoreFlags() { mFlags = mFlagsSaved; }
145
146 void write(const NavRasterizeParams&);
147 void copyFrom(const NavPoint&);
148
149 float getCost(uint term) const { return mCosts[term]; }
150 void setCost(uint term, float cost) { mCosts[term] = cost; }
151 float getScore() const { return mScore; }
152 void setScore(float score) { mScore = score; }
153
154 NavPoint *getBackEdge(NavGrid &nav) const { return getEdge(nav, mBackDir); }
155 GridDirection getBackDirection() const { return mBackDir; }
156 void setBackDirection(GridDirection d) { mBackDir = d; }
157
158 AStarVisitStatus& getVisits() { return mVisits; }
159 const AStarVisitStatus& getVisits() const { return mVisits; }
160
161 uint16_t getWriteSeq() const { return mWriteSeq; }
162 void setWriteSeq(uint16_t v) { mWriteSeq = v; }
163
164 std::string str(const NavGrid *) const;
165 private:
166 int16_t mRefX;
167 int16_t mRefY;
168 float mCosts[GYM_PCB_NAVPOINT_NUM_COST_TERMS]{1.0f}; // 16 + 8
169 float mScore; // 24 + 4
170 uint16_t mFlags{0}; // 28 + 2
171 uint16_t mFlagsSaved{0}; // 30 + 2
172 uint16_t mEdgeMask{0}; // 32 + 2
173 uint8_t mLayer; // 34 + 1
174 GridDirection mBackDir; // 35 + 1
175 AStarVisitStatus mVisits; // 36 + 2
176 NavKeepoutCounts mKOCount; // 38 + 6
177 uint16_t mWriteSeq{0}; // 48 + 2
178 // Align for sizeof(double) // 50 ~ 56
179};
180
181 inline bool NavPoint::canAddVia(const NavPoint &to) const
182{
183 return canPlaceVia() && to.canPlaceVia() && !((mFlags ^ to.mFlags) & NAV_POINT_FLAG_INSIDE_PIN);
184}
185 inline bool NavPoint::canAddViaEver(const NavPoint &to) const
186{
187 return canPlaceViaEver() && to.canPlaceViaEver() && !((mFlags ^ to.mFlags) & NAV_POINT_FLAG_INSIDE_PIN);
188}
189
190 inline void NavPoint::setEdge(GridDirection d, bool mask)
191{
192 if (mask)
193 mEdgeMask |= 1 << d.n();
194 else mEdgeMask &= ~(1 << d.n());
195}
196
197 inline void NavPoint::copyFrom(const NavPoint &nav)
198{
199 assert(mRefX == nav.mRefX && mRefY == nav.mRefY && mLayer == nav.mLayer);
200 for (uint i = 0; i < GYM_PCB_NAVPOINT_NUM_COST_TERMS; ++i)
201 mCosts[i] = nav.mCosts[i];
202 mFlags = nav.mFlags;
203 mKOCount = nav.mKOCount;
204}
205
206 inline void NavPoint::write(const NavRasterizeParams &data)
207{
208 if ((mWriteSeq == data.WriteSeq) || (mFlags & data.IgnoreMask))
209 return;
210 mWriteSeq = data.WriteSeq;
211 mFlags &= ~NAV_POINT_FLAGS_CLEARANCE;
212 if (mKOCount._RouteTracks += data.KOCount._RouteTracks) mFlags |= NAV_POINT_FLAG_ROUTE_TRACK_CLEARANCE;
213 if (mKOCount._RouteVias += data.KOCount._RouteVias) mFlags |= NAV_POINT_FLAG_ROUTE_VIA_CLEARANCE;
214 if (mKOCount._PinTracks += data.KOCount._PinTracks) mFlags |= NAV_POINT_FLAG_PIN_TRACK_CLEARANCE;
215 if (mKOCount._PinVias += data.KOCount._PinVias) mFlags |= NAV_POINT_FLAG_PIN_VIA_CLEARANCE;
216 mFlags = (mFlags & data.FlagsAnd) | data.FlagsOr;
217}
218
219#endif // GYM_PCB_NAVPOINT_H
AStarVisitStatus
Definition NavPoint.hpp:53
GridDirection
Definition GridDirection.hpp:6
NavGrid
The grid-representation of the board.
Definition NavGrid.hpp:61
NavPoint
Definition NavPoint.hpp:116
Point_25
Definition Geometry.hpp:131
Segment_25
Definition Geometry.hpp:155
UniformGrid25
Definition UniformGrid25.hpp:9
NavKeepoutCounts
Definition NavPoint.hpp:71
NavRasterizeParams
Definition NavPoint.hpp:93
NavRasterizeParams::FlagsAnd
uint16_t FlagsAnd
Skip all operations for points with hasFlags(IgnoreMask).
Definition NavPoint.hpp:96
NavRasterizeParams::IgnoreMask
uint16_t IgnoreMask
Expand shapes outward by this amount (ignored with AutoExpand).
Definition NavPoint.hpp:95
NavRasterizeParams::FlagsOr
uint16_t FlagsOr
Clear flags not set here (may remove CLEARANCE).
Definition NavPoint.hpp:97
NavRasterizeParams::TrackRasterCount
int8_t TrackRasterCount
Added to NavPoint's counts to determine CLEARANCE flags.
Definition NavPoint.hpp:102
NavRasterizeParams::AutoIncrementWriteSeq
bool AutoIncrementWriteSeq
Whether to rasterize the track & via clearance areas (assumes KOCount.Tracks != 0).
Definition NavPoint.hpp:100
NavRasterizeParams::AutoExpand
bool AutoExpand
Used to prevent writing overlapping points of track segments/joints twice.
Definition NavPoint.hpp:99
NavRasterizeParams::WriteSeq
uint16_t WriteSeq
Set these flags (after FlagsAnd).
Definition NavPoint.hpp:98