PCB Environment 2

Loading...
Searching...
No Matches
UniformGrid25.hpp
1#ifndef GYM_PCB_UNIFORMGRID25_H
2#define GYM_PCB_UNIFORMGRID25_H
3
4#include "Geometry.hpp"
5#include "Math/IPoint3.hpp"
6#include "UserSettings.hpp"
7
8 class UniformGrid25
9{
10 public:
11 const Real EdgeLen, EdgeLen05, EdgeLenRcp; // Compiler should realize that these are 1/0.5 in NavGrid!
12 public:
13 UniformGrid25(Real edgeLen) : EdgeLen(edgeLen), EdgeLen05(edgeLen * 0.5), EdgeLenRcp(1.0 / EdgeLen) { }
14
15 IVector_3 getSize() const { return IVector_3(mSize[0], mSize[1], mSize[2]); }
16 uint getSize(uint d) const { assert(d < 3); return mSize[d]; }
17 const Bbox_2& getBbox() const { return mBbox; }
18
19 Real XCoord(int x) const { return x * EdgeLen + mBbox.xmin(); }
20 Real YCoord(int y) const { return y * EdgeLen + mBbox.ymin(); }
21 Point_2 Coords(const IPoint_2 &v) const { return Coords(v.x, v.y); }
22 Point_2 Coords(int x, int y) const { return Point_2(XCoord(x), YCoord(y)); }
23 Point_25 Coords(const IPoint_3 &v) const { return Coords(v.x, v.y, v.z); }
24 Point_25 Coords(int x, int y, uint z) const { assert(z < mSize[2]); return Point_25(XCoord(x), YCoord(y), z); }
25
26 Vector_2 MidPointOffset() const { return Vector_2(EdgeLen05, EdgeLen05); }
27 Real MidPointX(int x) const { return XCoord(x) + MidPointOffset().x(); }
28 Real MidPointY(int y) const { return YCoord(y) + MidPointOffset().y(); }
29 Point_2 MidPoint(const IPoint_2 &v) const { return MidPoint(v.x, v.y); }
30 Point_2 MidPoint(int x, int y) const { return Coords(x,y) + MidPointOffset(); }
31 Point_25 MidPoint(const IPoint_3 &v) const { return Coords(v) + MidPointOffset(); }
32
33 IVector_2 HIndices(const Point_2 &v) const { return IVector_2(XIndex(v.x()), YIndex(v.y())); }
34 int XIndex(Real x, Real ex = 0.0) const;
35 int YIndex(Real y, Real ey = 0.0) const;
36 uint XIndexBounded(Real x, Real ex = 0.0) const;
37 uint YIndexBounded(Real y, Real ey = 0.0) const;
38 uint LinearIndex(uint z, uint y, uint x) const { return z * mStrideZ + y * mStrideY + x; }
39 uint LinearIndex(const Point_25 &v) const { return LinearIndex(XIndexBounded(v.x()), YIndexBounded(v.y()), 0); }
40 IBox_3 getBox(const Bbox_2&, uint z0 = 0, uint z1 = std::numeric_limits<uint>::max()) const;
41
42 Real XfIndex(Real x, Real ex = 0.0) const;
43 Real YfIndex(Real y, Real ey = 0.0) const;
44
45 uint getNumPoints2D() const { return mSize[0] * mSize[1]; }
46 uint getNumPoints3D() const { return mNumPoints3D; }
47
48 bool inside(const IPoint_3 &v) const { return inside(v.x, v.y, v.z); }
49 bool inside(uint x, uint y, uint z) const;
50
51 Point_25 snapToMidPoint(const Point_25&) const;
52
53 protected:
54 Bbox_2 mBbox;
55 uint mSize[3];
56 uint mStrideZ;
57 uint mStrideY;
58 uint mNumPoints3D;
59
60 void calcNumPoints3D();
61};
62
63 inline Real UniformGrid25::XfIndex(Real x, Real ex) const
64{
65 return (x + ex - mBbox.xmin()) * EdgeLenRcp;
66}
67 inline Real UniformGrid25::YfIndex(Real y, Real ey) const
68{
69 return (y + ey - mBbox.ymin()) * EdgeLenRcp;
70}
71
72 inline int UniformGrid25::XIndex(Real x, Real ex) const
73{
74 return int(XfIndex(x, ex));
75}
76 inline int UniformGrid25::YIndex(Real y, Real ey) const
77{
78 return int(YfIndex(y, ey));
79}
80 inline uint UniformGrid25::XIndexBounded(Real x, Real ex) const
81{
82 return std::max(0, std::min(XIndex(x, ex), int(mSize[0] - 1)));
83}
84 inline uint UniformGrid25::YIndexBounded(Real y, Real ey) const
85{
86 return std::max(0, std::min(YIndex(y, ey), int(mSize[1] - 1)));
87}
88 inline bool UniformGrid25::inside(uint x, uint y, uint z) const
89{
90 return (x < mSize[0]) && (y < mSize[1]) && (z < mSize[2]);
91}
92
93 inline Point_25 UniformGrid25::snapToMidPoint(const Point_25 &v) const
94{
95 return Point_25(std::floor(v.x() - mBbox.xmin()) + EdgeLen05 + mBbox.xmin(),
96 std::floor(v.y() - mBbox.ymin()) + EdgeLen05 + mBbox.ymin(),
97 v.z());
98}
99
100 inline void UniformGrid25::calcNumPoints3D()
101{
102 uint64_t N = (uint64_t(mSize[0]) * mSize[1]) * mSize[2];
103 if (N > UserSettings::get().MaxGridCells || N > std::numeric_limits<uint>::max())
104 throw std::runtime_error("grid is too large, reduce the resolution");
105 mNumPoints3D = N;
106}
107
108 inline IBox_3 UniformGrid25::getBox(const Bbox_2 &bbox, uint z0, uint z1) const
109{
110 return IBox_3(IPoint_3(XIndexBounded(bbox.xmin()), YIndexBounded(bbox.ymin()), std::min(z0, mSize[2] - 1)),
111 IPoint_3(XIndexBounded(bbox.xmax()), YIndexBounded(bbox.ymax()), std::min(z1, mSize[2] - 1)));
112}
113
114#endif // GYM_PCB_UNIFORMGRID25_H
IPoint_2
Definition IPoint2.hpp:37
IPoint_3
Definition IPoint3.hpp:36
IVector_2
Definition IPoint2.hpp:10
IVector_3
Definition IPoint3.hpp:8
Point_25
Definition Geometry.hpp:131
IBox_3
Definition IPoint3.hpp:60