|
|
|
|
|
|
|
|
| cat("=== Debugging Hexagonal Separation ===\n\n")
|
|
|
|
|
| source("R/logging.R")
|
| source("R/config_utils.R")
|
| source("R/hexagonal_puzzle.R")
|
| source("R/hexagonal_topology.R")
|
| source("R/hexagonal_neighbors.R")
|
| source("R/hexagonal_bezier_generation.R")
|
| source("R/hexagonal_edge_generation_fixed.R")
|
| source("R/unified_piece_generation.R")
|
| source("R/piece_positioning.R")
|
|
|
|
|
| rings <- 3
|
| seed <- 42
|
| diameter <- 200
|
|
|
| cat("Generating hexagonal puzzle with", rings, "rings\n\n")
|
|
|
|
|
| pieces_result <- generate_pieces_internal(
|
| type = "hexagonal",
|
| seed = seed,
|
| grid = c(rings),
|
| size = c(diameter),
|
| tabsize = 27,
|
| jitter = 5,
|
| do_warp = FALSE,
|
| do_trunc = FALSE
|
| )
|
|
|
| cat("Generated", length(pieces_result$pieces), "pieces\n\n")
|
|
|
|
|
| cat("=== Compact Centers ===\n")
|
| for (i in 1:min(7, length(pieces_result$pieces))) {
|
| p <- pieces_result$pieces[[i]]
|
| cat(sprintf("Piece %s: center (%.2f, %.2f)\n", p$id, p$center[1], p$center[2]))
|
| }
|
|
|
|
|
| offset <- 20
|
| positioned <- apply_piece_positioning(pieces_result, offset = offset)
|
|
|
| cat("\n=== Separated Centers (offset =", offset, "mm) ===\n")
|
| for (i in 1:min(7, length(positioned$pieces))) {
|
| p <- positioned$pieces[[i]]
|
| cat(sprintf("Piece %s: center (%.2f, %.2f)\n", p$id, p$center[1], p$center[2]))
|
| }
|
|
|
|
|
| cat("\n=== Checking for Duplicate/Shared Starting Points ===\n")
|
|
|
|
|
| extract_move_points <- function(path) {
|
|
|
| matches <- regmatches(path, gregexpr("M [0-9.-]+ [0-9.-]+", path, perl = TRUE))[[1]]
|
| points <- list()
|
| for (m in matches) {
|
| parts <- as.numeric(unlist(strsplit(gsub("M ", "", m), " ")))
|
| if (length(parts) == 2) {
|
| points[[length(points) + 1]] <- parts
|
| }
|
| }
|
| return(points)
|
| }
|
|
|
| all_start_points <- list()
|
| for (i in 1:length(positioned$pieces)) {
|
| p <- positioned$pieces[[i]]
|
| points <- extract_move_points(p$path)
|
| for (pt in points) {
|
| key <- sprintf("%.1f,%.1f", pt[1], pt[2])
|
| if (is.null(all_start_points[[key]])) {
|
| all_start_points[[key]] <- c()
|
| }
|
| all_start_points[[key]] <- c(all_start_points[[key]], p$id)
|
| }
|
| }
|
|
|
|
|
| cat("\nStarting points shared by multiple pieces:\n")
|
| shared_count <- 0
|
| for (key in names(all_start_points)) {
|
| if (length(all_start_points[[key]]) > 1) {
|
| cat(sprintf(" Point %s: %s\n", key, paste(all_start_points[[key]], collapse = ", ")))
|
| shared_count <- shared_count + 1
|
| }
|
| }
|
| if (shared_count == 0) {
|
| cat(" None found - each piece starts at a unique point\n")
|
| }
|
|
|
|
|
| cat("\n=== Center Piece Path Analysis ===\n")
|
| center_piece <- positioned$pieces[[1]]
|
| cat("Piece ID:", center_piece$id, "\n")
|
| cat("Center:", sprintf("(%.2f, %.2f)\n", center_piece$center[1], center_piece$center[2]))
|
| cat("Path (first 500 chars):\n")
|
| cat(substr(center_piece$path, 1, 500), "\n...\n")
|
|
|
|
|
| extract_all_coords <- function(path) {
|
|
|
| coords_str <- gsub("[MLCAZ]", " ", path)
|
| numbers <- as.numeric(unlist(strsplit(trimws(coords_str), "\\s+")))
|
| numbers <- numbers[!is.na(numbers)]
|
|
|
|
|
| if (length(numbers) %% 2 != 0) {
|
| numbers <- numbers[-length(numbers)]
|
| }
|
|
|
| x_coords <- numbers[seq(1, length(numbers), 2)]
|
| y_coords <- numbers[seq(2, length(numbers), 2)]
|
|
|
| return(list(x = x_coords, y = y_coords))
|
| }
|
|
|
| center_coords <- extract_all_coords(center_piece$path)
|
| cat("\nCenter piece coordinate range:\n")
|
| cat(sprintf(" X: [%.2f, %.2f]\n", min(center_coords$x), max(center_coords$x)))
|
| cat(sprintf(" Y: [%.2f, %.2f]\n", min(center_coords$y), max(center_coords$y)))
|
|
|
|
|
| near_origin <- sum(abs(center_coords$x) < 1 & abs(center_coords$y) < 1)
|
| cat(sprintf(" Coords near origin (|x|<1 && |y|<1): %d\n", near_origin))
|
|
|
|
|
| cat("\n=== Outer Piece (last one) Path Analysis ===\n")
|
| outer_piece <- positioned$pieces[[length(positioned$pieces)]]
|
| cat("Piece ID:", outer_piece$id, "\n")
|
| cat("Center:", sprintf("(%.2f, %.2f)\n", outer_piece$center[1], outer_piece$center[2]))
|
|
|
| outer_coords <- extract_all_coords(outer_piece$path)
|
| cat("\nOuter piece coordinate range:\n")
|
| cat(sprintf(" X: [%.2f, %.2f]\n", min(outer_coords$x), max(outer_coords$x)))
|
| cat(sprintf(" Y: [%.2f, %.2f]\n", min(outer_coords$y), max(outer_coords$y)))
|
|
|
|
|
| dist_from_center <- sqrt(outer_coords$x^2 + outer_coords$y^2)
|
| cat(sprintf(" Min distance from origin: %.2f\n", min(dist_from_center)))
|
| cat(sprintf(" Max distance from origin: %.2f\n", max(dist_from_center)))
|
|
|
|
|
| if (min(dist_from_center) < 10) {
|
| cat(" WARNING: Some coordinates are very close to origin!\n")
|
| idx <- which(dist_from_center < 10)
|
| cat(" Suspicious coords:\n")
|
| for (i in head(idx, 5)) {
|
| cat(sprintf(" (%.2f, %.2f) dist=%.2f\n", outer_coords$x[i], outer_coords$y[i], dist_from_center[i]))
|
| }
|
| }
|
|
|
|
|
| cat("\n=== Checking for Lines to Origin ===\n")
|
| for (i in 1:length(positioned$pieces)) {
|
| p <- positioned$pieces[[i]]
|
|
|
| if (grepl("L 0\\.00 0\\.00|L -?0\\.[0-9]+ -?0\\.[0-9]+", p$path)) {
|
| cat(sprintf("WARNING: Piece %s has L command near origin!\n", p$id))
|
| }
|
|
|
| coords <- extract_all_coords(p$path)
|
| near_zero_x <- which(abs(coords$x) < 2)
|
| near_zero_y <- which(abs(coords$y) < 2)
|
| near_zero <- intersect(near_zero_x, near_zero_y)
|
| if (length(near_zero) > 0 && i > 1) {
|
| cat(sprintf("Piece %s has coords near origin: ", p$id))
|
| for (idx in near_zero) {
|
| cat(sprintf("(%.2f, %.2f) ", coords$x[idx], coords$y[idx]))
|
| }
|
| cat("\n")
|
| }
|
| }
|
|
|
|
|
| cat("\n=== Middle Ring Piece (piece 8) Path Analysis ===\n")
|
| mid_piece <- positioned$pieces[[8]]
|
| cat("Piece ID:", mid_piece$id, "\n")
|
| cat("Center:", sprintf("(%.2f, %.2f)\n", mid_piece$center[1], mid_piece$center[2]))
|
| cat("Full path:\n")
|
| cat(mid_piece$path, "\n")
|
|
|
| cat("\n=== Debug Complete ===\n")
|
|
|