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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::{cell::RefCell, rc::Rc};
use crate::{
common::{BitMatrix, Result},
qrcode::encoder::ByteMatrix,
Exceptions, Point,
};
use super::{BitMatrixCursorTrait, Direction, RegressionLineTrait, StepResult, Value};
#[derive(Clone)]
pub struct EdgeTracer<'a> {
pub(crate) img: &'a BitMatrix,
pub(crate) p: Point, // current position
d: Point, // current direction
// pub history: Option<&'a mut ByteMatrix>, // = nullptr;
pub history: Option<Rc<RefCell<ByteMatrix>>>,
pub state: i32,
// const BitMatrix* img;
// POINT p; // current position
// POINT d; // current direction
}
// impl<'a> Clone for EdgeTracer<'_> {
// fn clone(&self) -> Self {
// if let Some(history) = self.history {
// Self { img: self.img, p: self.p.clone(), d: self.d.clone(), history: Some(history), state: self.state.clone() }
// }else {
// Self { img: self.img, p: self.p.clone(), d: self.d.clone(), history: None, state: self.state.clone() }
// }
// }
// }
impl BitMatrixCursorTrait for EdgeTracer<'_> {
fn testAt(&self, p: Point) -> Value {
if self.img.isIn(p, 0) {
Value::from(self.img.get_point(p))
} else {
Value::Invalid
}
}
fn isIn(&self, p: Point) -> bool {
self.img.isIn(p, 0)
}
fn isInSelf(&self) -> bool {
self.isIn(self.p)
}
fn isBlack(&self) -> bool {
self.blackAt(self.p)
}
fn isWhite(&self) -> bool {
self.whiteAt(self.p)
}
fn front(&self) -> &Point {
&self.d
}
fn back(&self) -> Point {
Point {
x: -self.d.x,
y: -self.d.y,
}
}
fn left(&self) -> Point {
Point {
x: self.d.y,
y: -self.d.x,
}
}
fn right(&self) -> Point {
Point {
x: -self.d.y,
y: self.d.x,
}
}
fn turnBack(&mut self) {
self.d = self.back()
}
fn turnLeft(&mut self) {
self.d = self.left()
}
fn turnRight(&mut self) {
self.d = self.right()
}
fn turn(&mut self, dir: Direction) {
self.d = self.direction(dir)
}
fn edgeAt_point(&self, d: Point) -> Value {
let v = self.testAt(self.p);
if self.testAt(self.p + d) != v {
v
} else {
Value::Invalid
}
}
fn setDirection(&mut self, dir: Point) {
self.d = dir.bresenhamDirection();
}
fn step(&mut self, s: Option<f32>) -> bool {
let s = if let Some(s) = s { s } else { 1.0 };
self.p += self.d * s;
self.isIn(self.p)
}
fn movedBy<T: BitMatrixCursorTrait>(self, d: Point) -> Self {
let mut res = self;
res.p += d;
res
}
fn turnedBack(&self) -> Self {
let mut res = self.clone();
res.d = res.back();
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 {
let mut nth = nth.unwrap_or(1); //if let Some(nth) = nth { nth } else { 1 };
let range = range.unwrap_or(0); //if let Some(r) = range { r } else { 0 };
let backup = backup.unwrap_or(false); //if let Some(b) = backup { b } else { false };
// TODO: provide an alternative and faster out-of-bounds check than isIn() inside testAt()
let mut steps = 0;
let mut lv = self.testAt(self.p);
while nth > 0 && (range <= 0 || steps < range) && lv.isValid() {
steps += 1;
let v = self.testAt(self.p + steps * self.d);
if lv != v {
lv = v;
nth -= 1;
}
}
if backup {
steps -= 1;
}
self.p += self.d * steps;
steps * i32::from(nth == 0)
}
fn p(&self) -> Point {
self.p
}
fn d(&self) -> Point {
self.d
}
fn img(&self) -> &BitMatrix {
self.img
}
}
impl<'a> EdgeTracer<'_> {
pub fn new(image: &'a BitMatrix, p: Point, d: Point) -> EdgeTracer<'a> {
// : img(&image), p(p) { setDirection(d); }
EdgeTracer {
img: image,
p,
d: Point::bresenhamDirection(d), //d,
history: None,
state: 0,
}
}
fn traceStep(
&mut self,
dEdge: Point,
maxStepSize: i32,
goodDirection: bool,
) -> Result<StepResult> {
let dEdge = Point::mainDirection(dEdge);
for breadth in 1..=(if maxStepSize == 1 {
2
} else if goodDirection {
1
} else {
3
}) {
// for (int breadth = 1; breadth <= (maxStepSize == 1 ? 2 : (goodDirection ? 1 : 3)); ++breadth)
for step in 1..=maxStepSize {
// for (int step = 1; step <= maxStepSize; ++step)
for i in 0..=(2 * (step / 4 + 1) * breadth) {
// for (int i = 0; i <= 2*(step/4+1) * breadth; ++i) {
let mut pEdge = self.p
+ step * self.d
+ (if i & 1 > 0 { (i + 1) / 2 } else { -i / 2 }) * dEdge;
// dbg!(pEdge);
if !self.blackAt(pEdge + dEdge) {
continue;
}
// found black pixel -> go 'outward' until we hit the b/w border
for _j in 0..(std::cmp::max(maxStepSize, 3)) {
// for (int j = 0; j < std::max(maxStepSize, 3) && isIn(pEdge); ++j) {
if self.whiteAt(pEdge) {
// if we are not making any progress, we still have another endless loop bug
if self.p == pEdge.centered() {
return Err(Exceptions::ILLEGAL_STATE);
}
self.p = pEdge.centered();
// if (self.history && maxStepSize == 1) {
if let Some(history) = &self.history {
if maxStepSize == 1 {
if history.borrow().get(self.p.x as u32, self.p.y as u32)
== self.state as u8
{
return Ok(StepResult::ClosedEnd);
}
history.borrow_mut().set(
self.p.x as u32,
self.p.y as u32,
self.state as u8,
);
}
}
return Ok(StepResult::Found);
}
pEdge -= dEdge;
if self.blackAt(pEdge - self.d) {
pEdge -= self.d;
}
// dbg!(pEdge);
if !self.isIn(pEdge) {
break;
}
}
// no valid b/w border found within reasonable range
return Ok(StepResult::ClosedEnd);
}
}
}
Ok(StepResult::OpenEnd)
}
pub fn updateDirectionFromOrigin(&mut self, origin: Point) -> bool {
let old_d = self.d;
self.setDirection(self.p - origin);
// if the new direction is pointing "backward", i.e. angle(new, old) > 90 deg -> break
if Point::dot(self.d, old_d) < 0.0 {
return false;
}
// make sure d stays in the same quadrant to prevent an infinite loop
if (self.d.x).abs() == (self.d.y).abs() {
self.d = Point::mainDirection(old_d) + 0.99 * (self.d - Point::mainDirection(old_d));
} else if Point::mainDirection(self.d) != Point::mainDirection(old_d) {
self.d = Point::mainDirection(old_d) + 0.99 * Point::mainDirection(self.d);
}
true
}
pub fn traceLine<T: RegressionLineTrait>(
&mut self,
dEdge: Point,
line: &mut T,
) -> Result<bool> {
line.setDirectionInward(dEdge);
loop {
// log(self.p);
line.add(self.p)?;
if line.points().len() % 50 == 10 {
if !line.evaluate_max_distance(None, None) {
return Ok(false);
}
if !self.updateDirectionFromOrigin(
self.p - line.project(self.p)
+ **line
.points()
.first()
.as_ref()
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?,
) {
return Ok(false);
}
}
let stepResult = self.traceStep(dEdge, 1, line.isValid())?;
if stepResult != StepResult::Found {
return Ok(stepResult == StepResult::OpenEnd && line.points().len() > 1);
}
} // while (true);
}
pub fn traceGaps<T: RegressionLineTrait>(
&mut self,
dEdge: Point,
line: &mut T,
maxStepSize: i32,
finishLine: &mut T,
) -> Result<bool> {
let mut maxStepSize = maxStepSize;
line.setDirectionInward(dEdge);
let mut gaps = 0;
loop {
// detect an endless loop (lack of progress). if encountered, please report.
if !(line.points().is_empty()
|| &&self.p
!= line
.points()
.last()
.as_ref()
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?)
{
return Err(Exceptions::ILLEGAL_STATE);
}
if !line.points().is_empty()
&& &&self.p
== line
.points()
.last()
.as_ref()
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
{
return Ok(false);
}
// log(p);
// if we drifted too far outside of the code, break
if line.isValid()
&& line.signedDistance(self.p) < -5.0
&& (!line.evaluate_max_distance(None, None) || line.signedDistance(self.p) < -5.0)
{
return Ok(false);
}
// if we are drifting towards the inside of the code, pull the current position back out onto the line
if line.isValid() && line.signedDistance(self.p) > 3.0 {
// The current direction d and the line we are tracing are supposed to be roughly parallel.
// In case the 'go outward' step in traceStep lead us astray, we might end up with a line
// that is almost perpendicular to d. Then the back-projection below can result in an
// endless loop. Break if the angle between d and line is greater than 45 deg.
if (Point::dot(Point::normalized(self.d), line.normal())).abs() > 0.7
// thresh is approx. sin(45 deg)
{
return Ok(false);
}
// re-evaluate line with all the points up to here before projecting
if !line.evaluate_max_distance(Some(1.5), None) {
return Ok(false);
}
let mut np = line.project(self.p);
// make sure we are making progress even when back-projecting:
// consider a 90deg corner, rotated 45deg. we step away perpendicular from the line and get
// back projected where we left off the line.
// The 'while' instead of 'if' was introduced to fix the issue with #245. It turns out that
// np can actually be behind the projection of the last line point and we need 2 steps in d
// to prevent a dead lock. see #245.png
while Point::distance(
np,
line.project(
line.points()
.last()
.copied()
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?,
),
) < 1.0
{
np += self.d;
}
self.p = Point::centered(np);
} else {
let stepLengthInMainDir = if line.points().is_empty() {
0.0
} else {
Point::dot(
Point::mainDirection(self.d),
self.p
- line
.points()
.last()
.copied()
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?,
)
};
line.add(self.p)?;
if stepLengthInMainDir > 1.0 {
gaps += 1;
if gaps >= 2 || line.points().len() > 5 {
if !line.evaluate_max_distance(Some(1.5), None) {
return Ok(false);
}
if !self.updateDirectionFromOrigin(
self.p - line.project(self.p)
+ line
.points()
.first()
.copied()
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?,
) {
return Ok(false);
}
// check if the first half of the top-line trace is complete.
// the minimum code size is 10x10 -> every code has at least 4 gaps
//TODO: maybe switch to termination condition based on bottom line length to get a better
// finishLine for the right line trace
if !finishLine.isValid() && gaps == 4 {
// undo the last insert, it will be inserted again after the restart
line.pop_back();
// gaps -= 1;
return Ok(true);
}
}
} else if gaps == 0 && line.points().len() >= (2 * maxStepSize) as usize {
return Ok(false);
} // no point in following a line that has no gaps
}
if finishLine.isValid() {
maxStepSize =
std::cmp::min(maxStepSize, (finishLine.signedDistance(self.p)) as i32);
}
let stepResult = self.traceStep(dEdge, maxStepSize, line.isValid())?;
if stepResult != StepResult::Found
// we are successful iff we found an open end across a valid finishLine
{
return Ok(stepResult == StepResult::OpenEnd
&& finishLine.isValid()
&& (finishLine.signedDistance(self.p)) as i32 <= maxStepSize + 1);
}
} //while (true);
}
pub fn traceCorner(&mut self, dir: &mut Point, corner: &mut Point) -> Result<bool> {
self.step(None);
// log(p);
*corner = self.p;
std::mem::swap(&mut self.d, dir);
self.traceStep(-1.0 * (*dir), 2, false)?;
// #ifdef PRINT_DEBUG
// printf("turn: %.0f x %.0f -> %.2f, %.2f\n", p.x, p.y, d.x, d.y);
// #endif
Ok(self.isIn(*corner) && self.isIn(self.p))
}
}