From 5a98f9f5e6bb82f67e4b155bff3bd68fb215037b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Nicolas <6371790+pynicolas@users.noreply.github.com> Date: Tue, 1 Jul 2025 21:25:26 +0200 Subject: [PATCH] Refactoring: move "norm" to Geometry.kt and add test --- .../org/mydomain/myscan/DocumentDetection.kt | 6 ----- .../main/java/org/mydomain/myscan/Geometry.kt | 13 ++++++++- .../java/org/mydomain/myscan/GeometryTest.kt | 27 +++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 app/src/test/java/org/mydomain/myscan/GeometryTest.kt diff --git a/app/src/main/java/org/mydomain/myscan/DocumentDetection.kt b/app/src/main/java/org/mydomain/myscan/DocumentDetection.kt index 0adab1f..7bae2ac 100644 --- a/app/src/main/java/org/mydomain/myscan/DocumentDetection.kt +++ b/app/src/main/java/org/mydomain/myscan/DocumentDetection.kt @@ -25,7 +25,6 @@ import org.opencv.core.Size import org.opencv.imgproc.Imgproc import kotlin.math.abs import kotlin.math.max -import kotlin.math.sqrt fun detectDocumentQuad(mask: Bitmap, minQuadAreaRatio: Double = 0.02): Quad? { val mat = Mat() @@ -143,8 +142,3 @@ fun Point.toCv(): org.opencv.core.Point { return org.opencv.core.Point(x.toDouble(), y.toDouble()) } -private fun norm(p1: Point, p2: Point): Double { - val dx = (p2.x - p1.x) - val dy = (p2.y - p1.y) - return sqrt(dx.toDouble() * dx + dy * dy) -} diff --git a/app/src/main/java/org/mydomain/myscan/Geometry.kt b/app/src/main/java/org/mydomain/myscan/Geometry.kt index e23358a..d3ffce0 100644 --- a/app/src/main/java/org/mydomain/myscan/Geometry.kt +++ b/app/src/main/java/org/mydomain/myscan/Geometry.kt @@ -15,10 +15,21 @@ package org.mydomain.myscan import kotlin.math.atan2 +import kotlin.math.sqrt data class Point(val x: Int, val y: Int) -data class Line(val from: Point, val to: Point) +data class Line(val from: Point, val to: Point) { + fun norm(): Double { + return norm(from, to) + } +} + +fun norm(p1: Point, p2: Point): Double { + val dx = (p2.x - p1.x) + val dy = (p2.y - p1.y) + return sqrt(dx.toDouble() * dx + dy * dy) +} data class Quad( val topLeft: Point, diff --git a/app/src/test/java/org/mydomain/myscan/GeometryTest.kt b/app/src/test/java/org/mydomain/myscan/GeometryTest.kt new file mode 100644 index 0000000..47b55be --- /dev/null +++ b/app/src/test/java/org/mydomain/myscan/GeometryTest.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2025 Pierre-Yves Nicolas + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ +package org.mydomain.myscan + +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class GeometryTest { + + @Test + fun line() { + assertThat(Line(Point(0, 0), Point(10, 0)).norm()).isEqualTo(10.0) + assertThat(Line(Point(1, 2), Point(4, 6)).norm()).isEqualTo(5.0) + } +}