Skip to content

Commit

Permalink
Fixed verification and caches
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Mar 30, 2024
1 parent cb2c04a commit dddf3f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
23 changes: 17 additions & 6 deletions backend/src/main/java/com/nestapp/nest/nfp/NfpCacheRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NfpCacheRepository(

}

fun prepareCacheForKeys(keys: List<NfpKey>) {
fun prepareCacheForKeys(keys: List<NfpKey>): Boolean {
val keysInDatabaseFormat = keys.toSet().map {
json.encodeToString(it)
}.distinct()
Expand All @@ -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
}
}

Expand All @@ -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<String>(name = "nest_paths") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public List<PathPlacement> placePaths(NestPath binPolygon, List<NestPath> paths)
keysToCache.add(key);
}

nfpCache.prepareCacheForKeys(keysToCache);
if (!nfpCache.prepareCacheForKeys(keysToCache)) {
continue;
}

List<NestPath> binNfp = nfpCacheReader.get(binKey);

Expand Down
12 changes: 5 additions & 7 deletions backend/src/main/java/com/nestapp/nest_api/NestApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit dddf3f3

Please sign in to comment.