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
use crate::Point;

use super::{util::opposite, Direction, Value};

/**
 * @brief The BitMatrixCursor represents a current position inside an image and current direction it can advance towards.
 *
 * The current position and direction is a PointT<T>. So depending on the type it can be used to traverse the image
 * in a Bresenham style (PointF) or in a discrete way (step only horizontal/vertical/diagonal (PointI)).
 */
pub trait BitMatrixCursor {
    // const BitMatrix* img;

    // POINT p; // current position
    // POINT d; // current direction

    // BitMatrixCursor(const BitMatrix& image, POINT p, POINT d) : img(&image), p(p) { setDirection(d); }

    fn testAt(&self, p: Point) -> Value; //const
                                         // {
                                         // 	return img->isIn(p) ? Value{img->get(p)} : Value{};
                                         // }

    fn blackAt(&self, pos: Point) -> bool {
        self.testAt(pos).isBlack()
    }
    fn whiteAt(&self, pos: Point) -> bool {
        self.testAt(pos).isWhite()
    }

    fn isIn(&self, p: Point) -> bool; // { return img->isIn(p); }
    fn isInSelf(&self) -> bool; // { return self.isIn(p); }
    fn isBlack(&self) -> bool; // { return blackAt(p); }
    fn isWhite(&self) -> bool; // { return whiteAt(p); }

    fn front(&self) -> &Point; //{ return d; }
    fn back(&self) -> Point; // { return {-d.x, -d.y}; }
    fn left(&self) -> Point; //{ return {d.y, -d.x}; }
    fn right(&self) -> Point; //{ return {-d.y, d.x}; }
    fn direction(&self, dir: Direction) -> Point {
        self.right() * Into::<i32>::into(dir)
    }

    fn turnBack(&mut self); // noexcept { d = back(); }
    fn turnLeft(&mut self); //noexcept { d = left(); }
    fn turnRight(&mut self); //noexcept { d = right(); }
    fn turn(&mut self, dir: Direction); //noexcept { d = direction(dir); }

    fn edgeAt_point(&self, d: Point) -> Value;
    // {
    // 	Value v = testAt(p);
    // 	return testAt(p + d) != v ? v : Value();
    // }

    fn edgeAtFront(&self) -> Value {
        return self.edgeAt_point(*self.front());
    }
    fn edgeAtBack(&self) -> Value {
        self.edgeAt_point(self.back())
    }
    fn edgeAtLeft(&self) -> Value {
        self.edgeAt_point(self.left())
    }
    fn edgeAtRight(&self) -> Value {
        self.edgeAt_point(self.right())
    }
    fn edgeAt_direction(&self, dir: Direction) -> Value {
        self.edgeAt_point(self.direction(dir))
    }

    fn setDirection(&mut self, dir: Point); // { d = bresenhamDirection(dir); }
                                            // fn setDirection(&self, dir: Point);// { d = dir; }

    fn step(&mut self, s: Option<f32>) -> bool; // DEF to 1
                                                // {
                                                // 	p += s * d;
                                                // 	return isIn(p);
                                                // }

    fn movedBy<T: BitMatrixCursor>(self, d: Point) -> Self;
    // {
    // 	auto res = *this;
    // 	res.p += d;
    // 	return res;
    // }

    /**
     * @brief stepToEdge advances cursor to one step behind the next (or n-th) edge.
     * @param nth number of edges to pass
     * @param range max number of steps to take
     * @param backup whether or not to backup one step so we land in front of the edge
     * @return number of steps taken or 0 if moved outside of range/image
     */
    fn stepToEdge(&mut self, nth: Option<i32>, range: Option<i32>, backup: Option<bool>) -> i32;
    // fn stepToEdge(&self, int nth = 1, int range = 0, bool backup = false) -> i32
    // {
    // 	// TODO: provide an alternative and faster out-of-bounds check than isIn() inside testAt()
    // 	int steps = 0;
    // 	auto lv = testAt(p);

    // 	while (nth && (!range || steps < range) && lv.isValid()) {
    // 		++steps;
    // 		auto v = testAt(p + steps * d);
    // 		if (lv != v) {
    // 			lv = v;
    // 			--nth;
    // 		}
    // 	}
    // 	if (backup)
    // 		--steps;
    // 	p += steps * d;
    // 	return steps * (nth == 0);
    // }

    fn stepAlongEdge(&mut self, dir: Direction, skipCorner: Option<bool>) -> bool
// fn stepAlongEdge(&self,  dir:Direction, skipCorner:Option<bool> = false) -> bool
    {
        let skipCorner = if let Some(sc) = skipCorner { sc } else { false };

        if !self.edgeAt_direction(dir).isValid() {
            self.turn(dir);
        } else if self.edgeAtFront().isValid() {
            self.turn(opposite(dir));
            if self.edgeAtFront().isValid() {
                self.turn(opposite(dir));
                if self.edgeAtFront().isValid() {
                    return false;
                }
            }
        }

        let mut ret = self.step(None);

        if ret && skipCorner && !self.edgeAt_direction(dir).isValid() {
            self.turn(dir);
            ret = self.step(None);
        }

        ret
    }

    fn countEdges(&mut self, range: i32) -> i32 {
        let mut res = 0;
        let mut range = range;

        let mut steps;

        while {
            steps = if range == 0 {
                0
            } else {
                self.stepToEdge(Some(1), Some(range), None)
            };
            steps > 0
        } {
            range -= steps;
            res += 1;
        }

        res
    }

    // template<typename ARRAY>
    // ARRAY readPattern(int range = 0)
    // {
    // 	ARRAY res;
    // 	for (auto& i : res)
    // 		i = stepToEdge(1, range);
    // 	return res;
    // }

    // template<typename ARRAY>
    // ARRAY readPatternFromBlack(int maxWhitePrefix, int range = 0)
    // {
    // 	if (maxWhitePrefix && isWhite() && !stepToEdge(1, maxWhitePrefix))
    // 		return {};
    // 	return readPattern<ARRAY>(range);
    // }
}