From dddf3f345c5db218e051696bec8150d2c808f348 Mon Sep 17 00:00:00 2001 From: vovastelmashchuk Date: Sat, 30 Mar 2024 15:49:47 +0200 Subject: [PATCH] Fixed verification and caches --- .../nestapp/nest/nfp/NfpCacheRepository.kt | 23 ++++++++++++++----- .../nestapp/nest/util/PlacementWorker.java | 4 +++- .../main/java/com/nestapp/nest_api/NestApi.kt | 12 ++++------ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/backend/src/main/java/com/nestapp/nest/nfp/NfpCacheRepository.kt b/backend/src/main/java/com/nestapp/nest/nfp/NfpCacheRepository.kt index 36f93d1..674f757 100644 --- a/backend/src/main/java/com/nestapp/nest/nfp/NfpCacheRepository.kt +++ b/backend/src/main/java/com/nestapp/nest/nfp/NfpCacheRepository.kt @@ -64,7 +64,7 @@ class NfpCacheRepository( } - fun prepareCacheForKeys(keys: List) { + fun prepareCacheForKeys(keys: List): Boolean { val keysInDatabaseFormat = keys.toSet().map { json.encodeToString(it) }.distinct() @@ -82,17 +82,26 @@ class NfpCacheRepository( listOf(key.aBid, key.bBid) }.distinct() - transaction { + return transaction { val nestPaths = getNestPathsByBids(allNestPathIdsNeedForNfpGeneration) - keyWithoutNfpCache.map { key -> - NfpCacheDatabase.new(id = json.encodeToString(key)) { - this.nfp = generateNfp(key, nestPaths).joinToString("|") { path -> + keyWithoutNfpCache.forEach { key -> + + val generatedNfp = try { + generateNfp(key, nestPaths).joinToString("|") { path -> path.segments.joinToString(";") { segment -> "${segment.x},${segment.y}" } } + } catch (e: CannotCreateNfpException) { + logger.error("Cannot generate NFP for key: $key") + return@transaction false + } + + NfpCacheDatabase.new(id = json.encodeToString(key)) { + this.nfp = generatedNfp } } + return@transaction true } } @@ -108,8 +117,10 @@ class NfpCacheRepository( key = key, ) - return NfpUtil.nfpGenerator(nfpPair) ?: throw IllegalArgumentException("Cannot generate NFP for key: $key") + return NfpUtil.nfpGenerator(nfpPair) ?: throw CannotCreateNfpException(key) } + + class CannotCreateNfpException(key: NfpKey) : Exception("Cannot generate NFP for key: $key") } object NestPathTable : IdTable(name = "nest_paths") { diff --git a/backend/src/main/java/com/nestapp/nest/util/PlacementWorker.java b/backend/src/main/java/com/nestapp/nest/util/PlacementWorker.java index c2833a3..8260a88 100644 --- a/backend/src/main/java/com/nestapp/nest/util/PlacementWorker.java +++ b/backend/src/main/java/com/nestapp/nest/util/PlacementWorker.java @@ -64,7 +64,9 @@ public List placePaths(NestPath binPolygon, List paths) keysToCache.add(key); } - nfpCache.prepareCacheForKeys(keysToCache); + if (!nfpCache.prepareCacheForKeys(keysToCache)) { + continue; + } List binNfp = nfpCacheReader.get(binKey); diff --git a/backend/src/main/java/com/nestapp/nest_api/NestApi.kt b/backend/src/main/java/com/nestapp/nest_api/NestApi.kt index 4b847e3..bc35935 100644 --- a/backend/src/main/java/com/nestapp/nest_api/NestApi.kt +++ b/backend/src/main/java/com/nestapp/nest_api/NestApi.kt @@ -79,19 +79,17 @@ class NestApi( private fun checkIfCanBePlaced(plate: Rectangle2D.Double, nestPath: NestPath, rotationCount: Int): Boolean { if (rotationCount == 0) { val bound = GeometryUtil.getPolygonBounds(nestPath) - if (plate.width < bound.width || plate.height < bound.height) { - return false - } + return !(plate.width < bound.width || plate.height < bound.height) } else { for (j in 0 until rotationCount) { val rotatedBound = rotatePolygon(nestPath, (360 / rotationCount) * j) - if (plate.width < rotatedBound.width || plate.height < rotatedBound.height) { - return false + if (plate.width > rotatedBound.width && plate.height > rotatedBound.height) { + return true } } } - return true + return false } private fun rotatePolygon(polygon: NestPath, angle: Int): Bound { @@ -107,7 +105,7 @@ class NestApi( val y1 = x * sin(Fangle) + y * cos(Fangle) rotated.add(x1, y1) } - return GeometryUtil.getPolygonBounds(polygon) + return GeometryUtil.getPolygonBounds(rotated) } private fun createBinNestPath(rect: Rectangle2D.Double, boundSpacing: Double): NestPath {