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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::{
common::{
detector::WhiteRectangleDetector,
reedsolomon::{self, ReedSolomonDecoder},
BitMatrix, DefaultGridSampler, GridSampler, Quadrilateral, Result,
},
exceptions::Exceptions,
point_f, Point,
};
use super::aztec_detector_result::AztecDetectorRXingResult;
const EXPECTED_CORNER_BITS: [u32; 4] = [
0xee0, // 07340 XXX .XX X.. ...
0x1dc, // 00734 ... XXX .XX X..
0x83b, // 04073 X.. ... XXX .XX
0x707, // 03407 .XX X.. ... XXX
];
/**
* Encapsulates logic that can detect an Aztec Code in an image, even if the Aztec Code
* is rotated or skewed, or partially obscured.
*
* @author David Olivier
* @author Frank Yellin
*/
pub struct Detector<'a> {
image: &'a BitMatrix,
compact: bool,
nb_layers: u32,
nb_data_blocks: u32,
nb_center_layers: u32,
shift: u32,
}
impl<'a> Detector<'_> {
pub fn new(image: &'a BitMatrix) -> Detector<'a> {
Detector {
image,
compact: false,
nb_layers: 0,
nb_data_blocks: 0,
nb_center_layers: 0,
shift: 0,
}
}
pub fn detect_false(&mut self) -> Result<AztecDetectorRXingResult> {
self.detect(false)
}
/**
* Detects an Aztec Code in an image.
*
* @param isMirror if true, image is a mirror-image of original
* @return {@link AztecDetectorRXingResult} encapsulating results of detecting an Aztec Code
* @throws NotFoundException if no Aztec Code can be found
*/
pub fn detect(&mut self, is_mirror: bool) -> Result<AztecDetectorRXingResult> {
// dbg!(self.image.to_string());
// 1. Get the center of the aztec matrix
let p_center = self.get_matrix_center();
// 2. Get the center points of the four diagonal points just outside the bull's eye
// [topRight, bottomRight, bottomLeft, topLeft]
let mut bulls_eye_corners = self.get_bulls_eye_corners(p_center)?;
if is_mirror {
bulls_eye_corners.swap(0, 2);
}
// 3. Get the size of the matrix and other parameters from the bull's eye
self.extractParameters(&bulls_eye_corners)?;
let src_quad = Quadrilateral::new(
bulls_eye_corners[self.shift as usize % 4],
bulls_eye_corners[(self.shift as usize + 1) % 4],
bulls_eye_corners[(self.shift as usize + 2) % 4],
bulls_eye_corners[(self.shift as usize + 3) % 4],
);
// 4. Sample the grid
let bits = self.sample_grid(self.image, src_quad)?;
// 5. Get the corners of the matrix.
let corners = self.get_matrix_corner_points(&bulls_eye_corners);
Ok(AztecDetectorRXingResult::new(
bits,
corners,
self.compact,
self.nb_data_blocks,
self.nb_layers,
))
}
/**
* Extracts the number of data layers and data blocks from the layer around the bull's eye.
*
* @param bullsEyeCorners the array of bull's eye corners
* @throws NotFoundException in case of too many errors or invalid parameters
*/
fn extractParameters(&mut self, bulls_eye_corners: &[Point]) -> Result<()> {
if !self.is_valid(bulls_eye_corners[0])
|| !self.is_valid(bulls_eye_corners[1])
|| !self.is_valid(bulls_eye_corners[2])
|| !self.is_valid(bulls_eye_corners[3])
{
return Err(Exceptions::not_found_with("no valid points"));
}
let length = 2 * self.nb_center_layers;
// Get the bits around the bull's eye
let sides = [
self.sample_line(bulls_eye_corners[0], bulls_eye_corners[1], length), // Right side
self.sample_line(bulls_eye_corners[1], bulls_eye_corners[2], length), // Bottom
self.sample_line(bulls_eye_corners[2], bulls_eye_corners[3], length), // Left side
self.sample_line(bulls_eye_corners[3], bulls_eye_corners[0], length), // Top
];
// bullsEyeCorners[shift] is the corner of the bulls'eye that has three
// orientation marks.
// sides[shift] is the row/column that goes from the corner with three
// orientation marks to the corner with two.
self.shift = Self::get_rotation(&sides, length)?;
// Flatten the parameter bits into a single 28- or 40-bit long
let mut parameter_data: u64 = 0;
for i in 0..4 {
// for (int i = 0; i < 4; i++) {
let side = sides[(self.shift + i) as usize % 4];
if self.compact {
// Each side of the form ..XXXXXXX. where Xs are parameter data
parameter_data <<= 7;
parameter_data += (side as u64 >> 1) & 0x7F;
} else {
// Each side of the form ..XXXXX.XXXXX. where Xs are parameter data
parameter_data <<= 10;
parameter_data += ((side as u64 >> 2) & (0x1f << 5)) + ((side as u64 >> 1) & 0x1F);
}
}
// Corrects parameter data using RS. Returns just the data portion
// without the error correction.
let corrected_data = Self::get_corrected_parameter_data(parameter_data, self.compact)?;
if self.compact {
// 8 bits: 2 bits layers and 6 bits data blocks
self.nb_layers = (corrected_data >> 6) + 1;
self.nb_data_blocks = (corrected_data & 0x3F) + 1;
} else {
// 16 bits: 5 bits layers and 11 bits data blocks
self.nb_layers = (corrected_data >> 11) + 1;
self.nb_data_blocks = (corrected_data & 0x7FF) + 1;
}
Ok(())
}
fn get_rotation(sides: &[u32], length: u32) -> Result<u32> {
// In a normal pattern, we expect to See
// ** .* D A
// * *
//
// . *
// .. .. C B
//
// Grab the 3 bits from each of the sides the form the locator pattern and concatenate
// into a 12-bit integer. Start with the bit at A
let mut corner_bits = 0;
for side in sides {
// for (int side : sides) {
// XX......X where X's are orientation marks
let t = ((side >> (length - 2)) << 1) + (side & 1);
corner_bits = (corner_bits << 3) + t;
}
// Mov the bottom bit to the top, so that the three bits of the locator pattern at A are
// together. cornerBits is now:
// 3 orientation bits at A || 3 orientation bits at B || ... || 3 orientation bits at D
corner_bits = ((corner_bits & 1) << 11) + (corner_bits >> 1);
// The result shift indicates which element of BullsEyeCorners[] goes into the top-left
// corner. Since the four rotation values have a Hamming distance of 8, we
// can easily tolerate two errors.
for shift in 0..4 {
// for (int shift = 0; shift < 4; shift++) {
if (corner_bits ^ EXPECTED_CORNER_BITS[shift as usize]).count_ones() <= 2 {
// if (Integer.bitCount(cornerBits ^ EXPECTED_CORNER_BITS[shift]) <= 2) {
return Ok(shift);
}
}
Err(Exceptions::not_found_with("rotation failure"))
}
/**
* Corrects the parameter bits using Reed-Solomon algorithm.
*
* @param parameterData parameter bits
* @param compact true if this is a compact Aztec code
* @throws NotFoundException if the array contains too many errors
*/
fn get_corrected_parameter_data(parameterData: u64, compact: bool) -> Result<u32> {
let mut parameter_data = parameterData;
let num_codewords: i32;
let num_data_codewords: i32;
if compact {
num_codewords = 7;
num_data_codewords = 2;
} else {
num_codewords = 10;
num_data_codewords = 4;
}
let num_eccodewords = num_codewords - num_data_codewords;
let mut parameterWords = vec![0; num_codewords as usize];
for i in (0..num_codewords).rev() {
// for (int i = numCodewords - 1; i >= 0; --i) {
parameterWords[i as usize] = (parameter_data & 0xF) as i32;
parameter_data >>= 4;
}
//try {
let field =
reedsolomon::get_predefined_genericgf(reedsolomon::PredefinedGenericGF::AztecParam);
let rs_decoder = ReedSolomonDecoder::new(field);
rs_decoder.decode(&mut parameterWords, num_eccodewords)?;
//} catch (ReedSolomonException ignored) {
//throw NotFoundException.getNotFoundInstance();
//}
// Toss the error correction. Just return the data as an integer
let mut result: u32 = 0;
for i in 0..num_data_codewords {
// for (int i = 0; i < numDataCodewords; i++) {
result = (result << 4) + parameterWords[i as usize] as u32;
}
Ok(result)
}
/**
* Finds the corners of a bull-eye centered on the passed point.
* This returns the centers of the diagonal points just outside the bull's eye
* Returns [topRight, bottomRight, bottomLeft, topLeft]
*
* @param pCenter Center point
* @return The corners of the bull-eye
* @throws NotFoundException If no valid bull-eye can be found
*/
fn get_bulls_eye_corners(&mut self, pCenter: Point) -> Result<[Point; 4]> {
let mut pina = pCenter;
let mut pinb = pCenter;
let mut pinc = pCenter;
let mut pind = pCenter;
let mut color = true;
self.nb_center_layers = 1;
while self.nb_center_layers < 9 {
// for nbCenterLayers in 1..9 {
// for (nbCenterLayers = 1; nbCenterLayers < 9; nbCenterLayers++) {
let pouta = self.get_first_different(pina, color, 1, -1);
let poutb = self.get_first_different(pinb, color, 1, 1);
let poutc = self.get_first_different(pinc, color, -1, 1);
let poutd = self.get_first_different(pind, color, -1, -1);
//d a
//
//c b
if self.nb_center_layers > 2 {
let q: f32 = Self::distance_points(poutd, pouta) * self.nb_center_layers as f32
/ (Self::distance_points(pind, pina) * (self.nb_center_layers + 2) as f32);
// let q: f32 = Self::distance(
// &poutd.to_rxing_result_point(),
// &pouta.to_rxing_result_point(),
// ) * nbCenterLayers as f32
// / (Self::distance(
// &pind.to_rxing_result_point(),
// &pina.to_rxing_result_point(),
// ) * (nbCenterLayers + 2) as f32);
if !(0.75..=1.25).contains(&q)
|| !self.is_white_or_black_rectangle(&pouta, &poutb, &poutc, &poutd)
{
break;
}
}
pina = pouta;
pinb = poutb;
pinc = poutc;
pind = poutd;
color = !color;
self.nb_center_layers += 1;
}
if self.nb_center_layers != 5 && self.nb_center_layers != 7 {
return Err(Exceptions::NOT_FOUND);
}
self.compact = self.nb_center_layers == 5;
// Expand the square by .5 pixel in each direction so that we're on the border
// between the white square and the black square
let pinax = point_f(pina.x + 0.5, pina.y - 0.5);
let pinbx = point_f(pinb.x + 0.5, pinb.y + 0.5);
let pincx = point_f(pinc.x - 0.5, pinc.y + 0.5);
let pindx = point_f(pind.x - 0.5, pind.y - 0.5);
// Expand the square so that its corners are the centers of the points
// just outside the bull's eye.
Ok(Self::expand_square(
&[pinax, pinbx, pincx, pindx],
2 * self.nb_center_layers - 3,
2 * self.nb_center_layers,
))
}
/**
* Finds a candidate center point of an Aztec code from an image
*
* @return the center point
*/
fn get_matrix_center(&self) -> Point {
let mut point_a = Point::default();
let mut point_b = Point::default();
let mut point_c = Point::default();
let mut point_d = Point::default();
let mut fnd = false;
//Get a white rectangle that can be the border of the matrix in center bull's eye or
if let Ok(wrd) = WhiteRectangleDetector::new_from_image(self.image) {
if let Ok(cornerPoints) = wrd.detect() {
point_a = cornerPoints[0];
point_b = cornerPoints[1];
point_c = cornerPoints[2];
point_d = cornerPoints[3];
fnd = true;
}
}
// This exception can be in case the initial rectangle is white
// In that case, surely in the bull's eye, we try to expand the rectangle.
if !fnd {
let cx: i32 = (self.image.getWidth() / 2) as i32;
let cy: i32 = (self.image.getHeight() / 2) as i32;
point_a = self.get_first_different(Point::from((cx + 7, cy - 7)), false, 1, -1);
point_b = self.get_first_different(Point::from((cx + 7, cy + 7)), false, 1, 1);
point_c = self.get_first_different(Point::from((cx - 7, cy + 7)), false, -1, 1);
point_d = self.get_first_different(Point::from((cx - 7, cy - 7)), false, -1, -1);
}
// try {
// let cornerPoints = WhiteRectangleDetector::new(image).detect();
// pointA = cornerPoints[0];
// pointB = cornerPoints[1];
// pointC = cornerPoints[2];
// pointD = cornerPoints[3];
// } catch (NotFoundException e) {
// // This exception can be in case the initial rectangle is white
// // In that case, surely in the bull's eye, we try to expand the rectangle.
// int cx = image.getWidth() / 2;
// int cy = image.getHeight() / 2;
// pointA = getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toPoint();
// pointB = getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toPoint();
// pointC = getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toPoint();
// pointD = getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toPoint();
// }
//Compute the center of the rectangle
let mut cx = ((point_a.x + point_d.x + point_b.x + point_c.x) / 4.0).round() as i32;
let mut cy = ((point_a.y + point_d.y + point_b.y + point_c.y) / 4.0).round() as i32;
// Redetermine the white rectangle starting from previously computed center.
// This will ensure that we end up with a white rectangle in center bull's eye
// in order to compute a more accurate center.
let mut fnd = false;
if let Ok(wrd) = WhiteRectangleDetector::new(self.image, 15, cx, cy) {
if let Ok(cornerPoints) = wrd.detect() {
point_a = cornerPoints[0];
point_b = cornerPoints[1];
point_c = cornerPoints[2];
point_d = cornerPoints[3];
fnd = true;
}
}
// This exception can be in case the initial rectangle is white
// In that case we try to expand the rectangle.
if !fnd {
point_a = self.get_first_different(Point::from((cx + 7, cy - 7)), false, 1, -1);
point_b = self.get_first_different(Point::from((cx + 7, cy + 7)), false, 1, 1);
point_c = self.get_first_different(Point::from((cx - 7, cy + 7)), false, -1, 1);
point_d = self.get_first_different(Point::from((cx - 7, cy - 7)), false, -1, -1);
}
// try {
// Point[] cornerPoints = new WhiteRectangleDetector(image, 15, cx, cy).detect();
// pointA = cornerPoints[0];
// pointB = cornerPoints[1];
// pointC = cornerPoints[2];
// pointD = cornerPoints[3];
// } catch (NotFoundException e) {
// // This exception can be in case the initial rectangle is white
// // In that case we try to expand the rectangle.
// pointA = getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toPoint();
// pointB = getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toPoint();
// pointC = getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toPoint();
// pointD = getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toPoint();
// }
// Recompute the center of the rectangle
cx = ((point_a.x + point_d.x + point_b.x + point_c.x) / 4.0).round() as i32;
cy = ((point_a.y + point_d.y + point_b.y + point_c.y) / 4.0).round() as i32;
Point::from((cx, cy))
}
/**
* Gets the Aztec code corners from the bull's eye corners and the parameters.
*
* @param bullsEyeCorners the array of bull's eye corners
* @return the array of aztec code corners
*/
fn get_matrix_corner_points(&self, bulls_eye_corners: &[Point]) -> [Point; 4] {
Self::expand_square(
bulls_eye_corners,
2 * self.nb_center_layers,
self.get_dimension(),
)
}
/**
* Creates a BitMatrix by sampling the provided image.
* topLeft, topRight, bottomRight, and bottomLeft are the centers of the squares on the
* diagonal just outside the bull's eye.
*/
fn sample_grid(&self, image: &BitMatrix, quad: Quadrilateral) -> Result<BitMatrix> {
let sampler = DefaultGridSampler;
let dimension = self.get_dimension();
let low = dimension as f32 / 2.0 - self.nb_center_layers as f32;
let high = dimension as f32 / 2.0 + self.nb_center_layers as f32;
let dst = Quadrilateral::new(
point_f(low, low),
point_f(high, low),
point_f(high, high),
point_f(low, high),
);
let (res, _) = sampler.sample_grid_detailed(image, dimension, dimension, dst, quad)?;
Ok(res)
}
/**
* Samples a line.
*
* @param p1 start point (inclusive)
* @param p2 end point (exclusive)
* @param size number of bits
* @return the array of bits as an int (first bit is high-order bit of result)
*/
fn sample_line(&self, p1: Point, p2: Point, size: u32) -> u32 {
let mut result = 0;
let d = Self::distance(p1, p2);
let module_size = d / size as f32;
let px = p1.x;
let py = p1.y;
let dx = module_size * (p2.x - p1.x) / d;
let dy = module_size * (p2.y - p1.y) / d;
for i in 0..size {
// for (int i = 0; i < size; i++) {
if self.image.get(
(px + i as f32 * dx).round() as u32,
(py + i as f32 * dy).round() as u32,
) {
result |= 1 << (size - i - 1);
}
}
result
}
/**
* @return true if the border of the rectangle passed in parameter is compound of white points only
* or black points only
*/
fn is_white_or_black_rectangle(&self, p1: &Point, p2: &Point, p3: &Point, p4: &Point) -> bool {
let corr = 3.0;
let p1 = Point::new(
0_f32.max(p1.x - corr),
(self.image.getHeight() as f32 - 1.0).min(p1.y + corr),
);
// let p1 = point(Math.max(0, p1.getX() - corr), Math.min(image.getHeight() - 1, p1.getY() + corr));
let p2 = Point::new(0_f32.max(p2.x - corr), 0_f32.max(p2.y - corr));
// let p2 = point(Math.max(0, p2.getX() - corr), Math.max(0, p2.getY() - corr));
let p3 = Point::new(
(self.image.getWidth() as f32 - 1.0).min(p3.x + corr),
0_f32.max((self.image.getHeight() as f32 - 1.0).min(p3.y - corr)),
);
// let p3 = point(Math.min(image.getWidth() - 1, p3.getX() + corr),
// Math.max(0, Math.min(image.getHeight() - 1, p3.getY() - corr)));
let p4 = Point::new(
(self.image.getWidth() as f32 - 1.0).min(p4.x + corr),
(self.image.getHeight() as f32 - 1.0).min(p4.y + corr),
);
// let p4 = point(Math.min(image.getWidth() - 1, p4.getX() + corr),
// Math.min(image.getHeight() - 1, p4.getY() + corr));
let c_init = self.get_color(p4, p1);
if c_init == 0 {
return false;
}
let c = self.get_color(p1, p2);
if c != c_init {
return false;
}
let c = self.get_color(p2, p3);
if c != c_init {
return false;
}
let c = self.get_color(p3, p4);
c == c_init
}
/**
* Gets the color of a segment
*
* @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
*/
fn get_color(&self, p1: Point, p2: Point) -> i32 {
let d = Self::distance_points(p1, p2);
if d == 0.0 {
return 0;
}
let dx = (p2.x - p1.x) / d;
let dy = (p2.y - p1.y) / d;
let mut error = 0;
let mut px = p1.x;
let mut py = p1.y;
let color_model = self.image.get(p1.x as u32, p1.y as u32);
let i_max = d.floor() as u32; //(int) Math.floor(d);
for _i in 0..i_max {
// for (int i = 0; i < iMax; i++) {
if self.image.get(px.round() as u32, py.round() as u32) != color_model {
error += 1;
}
px += dx;
py += dy;
}
let err_ratio = error as f32 / d;
if err_ratio > 0.1 && err_ratio < 0.9 {
return 0;
}
if (err_ratio <= 0.1) == color_model {
1
} else {
-1
}
}
/**
* Gets the coordinate of the first point with a different color in the given direction
*/
fn get_first_different(&self, init: Point, color: bool, dx: i32, dy: i32) -> Point {
let mut point = init + Point::from((dx, dy));
while self.is_valid_points(point) && self.image.get(point.x as u32, point.y as u32) == color
{
point += Point::from((dx, dy));
}
point -= Point::from((dx, dy));
while self.is_valid_points(point) && self.image.get(point.x as u32, point.y as u32) == color
{
point.x += dx as f32;
}
point.x -= dx as f32;
while self.is_valid_points(point) && self.image.get(point.x as u32, point.y as u32) == color
{
point.y += dy as f32;
}
point.y -= dy as f32;
point
}
/**
* Expand the square represented by the corner points by pushing out equally in all directions
*
* @param cornerPoints the corners of the square, which has the bull's eye at its center
* @param oldSide the original length of the side of the square in the target bit matrix
* @param newSide the new length of the size of the square in the target bit matrix
* @return the corners of the expanded square
*/
fn expand_square(corner_points: &[Point], old_side: u32, new_side: u32) -> [Point; 4] {
let ratio = new_side as f32 / (2.0 * old_side as f32);
let d = corner_points[0] - corner_points[2];
let middle = corner_points[0].middle(corner_points[2]);
let result0 = middle + ratio * d;
let result2 = middle - ratio * d;
let d = corner_points[1] - corner_points[3];
let middle = corner_points[1].middle(corner_points[3]);
let result1 = middle + ratio * d;
let result3 = middle - ratio * d;
[result0, result1, result2, result3]
}
fn is_valid_points(&self, p: Point) -> bool {
p.x >= 0.0
&& p.x < self.image.getWidth() as f32
&& p.y >= 0.0
&& p.y < self.image.getHeight() as f32
}
fn is_valid(&self, point: Point) -> bool {
self.is_valid_points(point.round())
}
fn distance_points(a: Point, b: Point) -> f32 {
a.distance(b)
}
fn distance(a: Point, b: Point) -> f32 {
a.distance(b)
}
fn get_dimension(&self) -> u32 {
if self.compact {
4 * self.nb_layers + 11
} else {
4 * self.nb_layers + 2 * ((2 * self.nb_layers + 6) / 15) + 15
}
}
}