|
| 1 | +#include "contour.h" |
| 2 | + |
| 3 | +#include <set> |
| 4 | + |
| 5 | +inline constexpr int poss[16][2] = { {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, |
| 6 | + {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1} }; |
| 7 | + |
| 8 | +namespace bc { |
| 9 | + |
| 10 | + |
| 11 | +class MapContour |
| 12 | +{ |
| 13 | + OutputContour& contur; |
| 14 | + ushort accum = 1; |
| 15 | + |
| 16 | + // Map |
| 17 | + std::set<unsigned int> points; |
| 18 | + |
| 19 | + // Runtime |
| 20 | + enum StartPos : char { LeftMid = 0, LeftTop = 1, TopMid = 2, RigthTop = 3, RigthMid = 4, RigthBottom = 5, BottomMid = 6, LeftBottom = 7 }; |
| 21 | + |
| 22 | +public: |
| 23 | + MapContour(OutputContour& contur) : contur(contur) |
| 24 | + { } |
| 25 | + |
| 26 | + void set4Directions() |
| 27 | + { |
| 28 | + accum = 2; |
| 29 | + } |
| 30 | + |
| 31 | + void set8Directions() |
| 32 | + { |
| 33 | + accum = 1; |
| 34 | + } |
| 35 | + |
| 36 | + auto make_point(point p) const |
| 37 | + { |
| 38 | + return py::make_tuple(xm.convert(p.x), ym.convert(p.y)); |
| 39 | + } |
| 40 | + |
| 41 | + Mutator xm, ym; |
| 42 | + void run(int startX, int startY, Mutator xm, Mutator ym, const bool aproxim = false) |
| 43 | + { |
| 44 | + if (points.empty()) |
| 45 | + return; |
| 46 | + |
| 47 | + this->xm = xm; |
| 48 | + this->ym = ym; |
| 49 | + |
| 50 | + StartPos dirct = RigthMid; |
| 51 | + const bc::point startPoint(startX, startY); |
| 52 | + bc::point cur(startPoint); |
| 53 | + |
| 54 | + while (true) |
| 55 | + { |
| 56 | + ushort start = (dirct + 6) % 8; // Safe minus 2 |
| 57 | + const ushort end = start + 7; |
| 58 | + // Check |
| 59 | + // 1 2 3 |
| 60 | + // 0 X 4 |
| 61 | + // 6 5 |
| 62 | + |
| 63 | + for (; start < end; start += accum) |
| 64 | + { |
| 65 | + const int* off = poss[start]; |
| 66 | + const bc::point newPoint(cur.x + off[0], cur.y + off[1]); |
| 67 | + |
| 68 | + if (this->exists(newPoint)) |
| 69 | + { |
| 70 | + // Update dir |
| 71 | + const StartPos newDirection = (StartPos)(start % 8); |
| 72 | + |
| 73 | + // In case of approximation |
| 74 | + // put only if direction has changed |
| 75 | + if (!aproxim || dirct != newDirection) |
| 76 | + { |
| 77 | + contur.append(make_point(cur)); |
| 78 | + } |
| 79 | + |
| 80 | + dirct = newDirection; |
| 81 | + cur = newPoint; |
| 82 | + if (cur == startPoint) |
| 83 | + { |
| 84 | + auto firstAddedPoint = contur[0].cast<py::tuple>(); |
| 85 | + if (firstAddedPoint[0].cast<double>() != startPoint.x || firstAddedPoint[1].cast<double>() != startPoint.y) |
| 86 | + { |
| 87 | + contur.append(make_point(startPoint)); |
| 88 | + } |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // -------- |
| 97 | + // not found assert |
| 98 | + assert(start < end); |
| 99 | + if (start >= end) |
| 100 | + return; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + void set(const bc::barvalue& p) { points.insert(p.getIndex()); } |
| 105 | + void set(int x, int y) { points.insert(bc::barvalue::getStatInd(x, y)); } |
| 106 | + |
| 107 | +private: |
| 108 | + |
| 109 | + bool exists(bc::point p) |
| 110 | + { |
| 111 | + return points.contains(bc::barvalue::getStatInd(p.x, p.y)); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | + |
| 116 | +OutputContour findContour(const bc::barline& line, Mutator mx, Mutator my, bool aproximate) |
| 117 | +{ |
| 118 | + OutputContour contour; |
| 119 | + |
| 120 | + int stX = 99999999; |
| 121 | + int stY = 0; |
| 122 | + |
| 123 | + MapContour dictPoints(contour); |
| 124 | + dictPoints.set8Directions(); |
| 125 | + |
| 126 | + const auto& points = line.matr; |
| 127 | + for (auto& p : points) |
| 128 | + { |
| 129 | + dictPoints.set(p); |
| 130 | + dictPoints.set(p.x, p.y + 1); |
| 131 | + dictPoints.set(p.x + 1, p.y); |
| 132 | + dictPoints.set(p.x + 1, p.y + 1); |
| 133 | + const int x = p.getX(); |
| 134 | + const int y = p.getY(); |
| 135 | + if (x < stX) |
| 136 | + { |
| 137 | + stX = x; |
| 138 | + stY = y; |
| 139 | + } |
| 140 | + } |
| 141 | + dictPoints.run(stX, stY, mx, my, aproximate); |
| 142 | + |
| 143 | + return contour; |
| 144 | +} |
| 145 | + |
| 146 | +template<class T> |
| 147 | +py::dict convertLasPointsToDictInner(const bn::array& x, const bn::array& y, const bn::array& z) |
| 148 | +{ |
| 149 | + py::dict output; |
| 150 | + for (size_t i = 0, total = x.shape()[0]; i < total; i++) |
| 151 | + { |
| 152 | + T xv = *reinterpret_cast<const T*>(x.data(i)); |
| 153 | + T yv = *reinterpret_cast<const T*>(y.data(i)); |
| 154 | + T zv = *reinterpret_cast<const T*>(z.data(i)); |
| 155 | + output[py::make_tuple(xv,yv)] = zv; |
| 156 | + } |
| 157 | + |
| 158 | + return output; |
| 159 | +} |
| 160 | +py::dict convertLasPointsToDict(const bn::array& x, const bn::array& y, const bn::array& z) |
| 161 | +{ |
| 162 | + auto mtype = z.dtype(); |
| 163 | + if (mtype.is(pybind11::dtype::of<float>())) |
| 164 | + return convertLasPointsToDictInner<float>(x, y, z); |
| 165 | + else if (mtype.is(pybind11::dtype::of<double>())) |
| 166 | + return convertLasPointsToDictInner<double>(x, y, z); |
| 167 | + else if (mtype.is(pybind11::dtype::of<int8_t>())) |
| 168 | + return convertLasPointsToDictInner<int8_t>(x, y, z); |
| 169 | + else if (mtype.is(pybind11::dtype::of<int32_t>())) |
| 170 | + return convertLasPointsToDictInner<int32_t>(x, y, z); |
| 171 | + else if (mtype.is(pybind11::dtype::of<int64_t>())) |
| 172 | + return convertLasPointsToDictInner<int64_t>(x, y, z); |
| 173 | + else |
| 174 | + throw pybind11::type_error("Unsupported data type"); |
| 175 | +} |
| 176 | + |
| 177 | +} |
0 commit comments