PCB Environment 2
Loading...
Searching...
No Matches
NavGrid.hpp
1#ifndef GYM_PCB_NAVGRID_H
2#define GYM_PCB_NAVGRID_H
3
4#include "NavPoint.hpp"
5#include "Rasterizer.hpp"
6//#include "Util/HugePageAllocator.hpp"
7
17
19
20class PCBoard;
21class Component;
22class Pin;
23class Path;
24class Connection;
25
29constexpr const bool CanSafelyEraseOverlappingSegments = false;
30
32struct NavSpacings
33{
34 Real Clearance;
35 Real TrackWidthHalf;
36 Real ViaRadius;
37 NavSpacings() { }
38 NavSpacings(const Connection&);
39 bool initialized() const { return !std::isnan(Clearance); }
40 bool operator==(const NavSpacings &that) const { return Clearance == that.Clearance && TrackWidthHalf == that.TrackWidthHalf && ViaRadius == that.ViaRadius; }
42 Real getExpansionForTracks(Real clearance) const;
43 Real getExpansionForVias(Real clarance) const;
44};
45
47{
48 float MaskedLayer;
49 float Via;
50 float Violation;
51 float WrongDirection;
52 void reset();
53 void setViolationCostInf() { Violation = std::numeric_limits<float>::infinity(); }
54 void setXYBiasNone() { WrongDirection = 1.0f; }
55 bool valid() const { return MaskedLayer >= 0.0f && Via >= 0.0f && Violation >= 0.0f && WrongDirection >= 0.0f; }
56 void setPy(PyObject *);
57};
58
60class NavGrid : public UniformGrid25
61{
62public:
63 NavGrid(PCBoard&);
64 const PCBoard& getPCB() const { return mPCB; }
65 PCBoard& getPCB() { return mPCB; }
66
67 void build();
68 void copyFrom(const NavGrid&);
69
70 const std::vector<NavPoint>& getPoints() const { return mPoints; }
71 std::vector<NavPoint>& getPoints() { return mPoints; }
72 uint getNumPoints() const { return mPoints.size(); }
73
74 const NavPoint& getPoint(uint i) const { return mPoints[i]; }
75 const NavPoint& getPoint(uint x, uint y, uint z) const;
76 const NavPoint *getPoint(const Point_2&, uint z) const;
77 const NavPoint *getPoint(const Point_25 &v) const { return getPoint(v.xy(), v.z()); }
78 NavPoint& getPoint(uint i) { return mPoints[i]; }
79 NavPoint& getPoint(uint x, uint y, uint z);
80 NavPoint& getPoint(const IPoint_3 &v) { return getPoint(v.x, v.y, v.z); }
81 NavPoint *getPoint(const Point_2&, uint z);
82 NavPoint *getPoint(const Point_25 &v) { return getPoint(v.xy(), v.z()); }
83 int getDirectionStride(GridDirection d) const { assert(d.n() <= 9); return mDirectionStride[d.n()]; }
84 uint32_t getAddressOffset(const NavPoint *nav) const;
85 NavPoint *getPointFromAddressOffset(uint32_t);
86
87 const NavSpacings& getSpacings() const { return mSpacings; }
88 bool setSpacings(const NavSpacings&);
89 void initSpacingsForAnyRoutedTrack();
90
91 void resetKeepouts();
92 void resetKeepoutsRRR();
93 void rasterize(const Component&, const NavRasterizeParams&);
94 void rasterize(const Pin&, const NavRasterizeParams&);
95 void rasterizeCompound(const Pin&, const NavRasterizeParams&);
96 void rasterize(const Connection&, const NavRasterizeParams&);
97 void rasterizeLayoutAreaBorder(const NavRasterizeParams&);
98 void rasterize(const Track&, const NavRasterizeParams&, uint rasterizeMask);
99 void rasterize(const Path&, const NavRasterizeParams&);
100
101 void getCosts(std::vector<float>&, uint index);
102 void setCosts(uint index, float, const IBox_3&);
103 void setCosts(uint index, const float *, const IBox_3&);
104 void setCosts(uint index, const float *);
105 AStarCosts& getAStarCosts() { return mAStarCosts; }
106 bool findPathAStar(Connection&, const AStarCosts *);
107 Real sumViolationArea(const Connection&);
108
109 uint16_t nextRasterSeq();
110 uint16_t nextSearchSeq();
111 uint16_t getSearchSeq() const { return mSearchSeq; }
112
113 std::string str(const IBox_3 * = 0) const;
114 PyObject *getPy(const IBox_3&) const;
115 PyObject *getPathCoordinatesNumpy(const Track&) const;
116
117private:
118 PCBoard &mPCB;
119 std::vector<NavPoint> mPoints;
120 NavSpacings mSpacings;
121 // We use these to look up the addresses of neighbours in the grid because NavPoint doesn't have edge pointers (to save space).
122 int mDirectionStride[10];
123 AStarCosts mAStarCosts;
124
125private:
126 void initDirectionStrides();
127 void initEdges(NavPoint&, const IPoint_3&);
128 void rasterizeFootprints();
129 void rasterizeClearanceAreas();
130 void rasterize(const AShape *, uint Z0, uint Z1, const NavRasterizeParams&);
131
132 uint16_t mSearchSeq{0};
133 uint16_t mRasterSeq{0};
134 void resetSearchSeq();
135 void resetRasterSeq();
136};
137
138inline NavPoint& NavGrid::getPoint(uint x, uint y, uint z)
139{
140 return mPoints.at(LinearIndex(z,y,x));
141}
142inline const NavPoint& NavGrid::getPoint(uint x, uint y, uint z) const
143{
144 return mPoints.at(LinearIndex(z,y,x));
145}
146inline NavPoint *NavGrid::getPoint(const Point_2 &v, uint z)
147{
148 const uint x = XIndex(v.x(), 0.0);
149 const uint y = YIndex(v.y(), 0.0);
150 return inside(x,y,z) ? &getPoint(x, y, z) : 0;
151}
152inline const NavPoint *NavGrid::getPoint(const Point_2 &v, uint z) const
153{
154 const uint x = XIndex(v.x(), 0.0);
155 const uint y = YIndex(v.y(), 0.0);
156 return inside(x,y,z) ? &getPoint(x, y, z) : 0;
157}
158
159inline uint16_t NavGrid::nextSearchSeq()
160{
161 if (mSearchSeq == 0x7fff) // 0x8000 is used to indicate that a point is on A-star's open list
162 resetSearchSeq();
163 return ++mSearchSeq;
164}
165inline uint16_t NavGrid::nextRasterSeq()
166{
167 if (mRasterSeq == 0xffff)
168 resetRasterSeq();
169 return ++mRasterSeq;
170}
171
172inline Real NavSpacings::getExpansionForTracks(Real clearance) const
173{
174 return std::max(Clearance, clearance) + TrackWidthHalf;
175}
176inline Real NavSpacings::getExpansionForVias(Real clearance) const
177{
178 return std::max(Clearance, clearance) + ViaRadius;
179}
180
181inline NavPoint *NavPoint::getEdge(NavGrid &nav, GridDirection d) const
182{
183 char *_this = const_cast<char *>(reinterpret_cast<const char *>(this));
184 if (!hasEdge(d))
185 return 0;
186 return reinterpret_cast<NavPoint *>(_this + nav.getDirectionStride(d));
187}
188
189inline uint32_t NavGrid::getAddressOffset(const NavPoint *nav) const
190{
191 const char *base = reinterpret_cast<const char *>(&mPoints[0]);
192 const char *addr = reinterpret_cast<const char *>(nav);
193 return addr - base;
194}
195inline NavPoint *NavGrid::getPointFromAddressOffset(uint32_t offset)
196{
197 char *base = reinterpret_cast<char *>(&mPoints[0]);
198 return reinterpret_cast<NavPoint *>(base + offset);
199}
200
201#endif // GYM_PCB_NAVGRID_H
Definition AShape.hpp:21
Definition Component.hpp:18
Definition Connection.hpp:17
Definition GridDirection.hpp:6
Definition IPoint3.hpp:36
The grid-representation of the board.
Definition NavGrid.hpp:61
Definition NavPoint.hpp:116
Definition PCBoard.hpp:36
Definition Path.hpp:13
Definition Pin.hpp:18
Definition Geometry.hpp:131
Definition Track.hpp:21
Definition NavGrid.hpp:47
Definition IPoint3.hpp:60
Definition NavPoint.hpp:93
This struct stores the spacing requirements the NavGrid is/should be prepared for.
Definition NavGrid.hpp:33
Real getExpansionForTracks(Real clearance) const
@clearance is the clearance required by the obstacle, this.Clearance is that required by the track.
Definition NavGrid.hpp:172