From df3e2dc3e28cc2d89737c4e332a18220c83244b3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Nicolas <6371790+pynicolas@users.noreply.github.com> Date: Mon, 6 Oct 2025 14:52:18 +0200 Subject: [PATCH] Geometry: use Double instead of Int to scale precisely --- .../main/java/org/fairscan/app/DocumentDetection.kt | 4 ++-- app/src/main/java/org/fairscan/app/Geometry.kt | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/fairscan/app/DocumentDetection.kt b/app/src/main/java/org/fairscan/app/DocumentDetection.kt index 8ab82ad..c475d36 100644 --- a/app/src/main/java/org/fairscan/app/DocumentDetection.kt +++ b/app/src/main/java/org/fairscan/app/DocumentDetection.kt @@ -67,7 +67,7 @@ fun detectDocumentQuad(mask: Bitmap, minQuadAreaRatio: Double = 0.02): Quad? { return null } - val vertices = biggest?.toList()?.map { Point(it.x.toInt(), it.y.toInt()) } + val vertices = biggest?.toList()?.map { Point(it.x, it.y) } return if (vertices?.size == 4) createQuad(vertices) else null } @@ -162,6 +162,6 @@ private fun toBitmap(mat: Mat): Bitmap { } fun Point.toCv(): org.opencv.core.Point { - return org.opencv.core.Point(x.toDouble(), y.toDouble()) + return org.opencv.core.Point(x, y) } diff --git a/app/src/main/java/org/fairscan/app/Geometry.kt b/app/src/main/java/org/fairscan/app/Geometry.kt index ca9ba35..76b3aa0 100644 --- a/app/src/main/java/org/fairscan/app/Geometry.kt +++ b/app/src/main/java/org/fairscan/app/Geometry.kt @@ -15,9 +15,10 @@ package org.fairscan.app import kotlin.math.atan2 -import kotlin.math.sqrt -data class Point(val x: Int, val y: Int) +data class Point(val x: Double, val y: Double) { + constructor(x: Int, y: Int) : this (x.toDouble(), y.toDouble()) +} data class Line(val from: Point, val to: Point) { fun norm(): Double { @@ -28,7 +29,7 @@ data class Line(val from: Point, val to: Point) { 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) + return kotlin.math.hypot(dx, dy) } data class Quad( @@ -72,8 +73,8 @@ fun createQuad(vertices: List): Quad { val cy = vertices.map { it.y }.average() // Sort by angle from centroid (clockwise) - val sorted = vertices.sortedWith(compareBy { - atan2((it.y - cy).toDouble(), (it.x - cx).toDouble()) + val sorted = vertices.sortedWith(compareBy { + atan2(it.y - cy, it.x - cx) }) return Quad(sorted[0], sorted[1], sorted[2], sorted[3]) @@ -91,5 +92,5 @@ fun Quad.scaledTo(fromWidth: Int, fromHeight: Int, toWidth: Int, toHeight: Int): } fun Point.scaled(scaleX: Float, scaleY: Float): Point { - return Point((x * scaleX).toInt(), (y * scaleY).toInt()) + return Point((x * scaleX), (y * scaleY)) }