File size: 6,666 Bytes
e232e39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env Rscript
# Debug hexagonal separation positioning
# Check for duplicate vertices or lines connecting to inner circles

cat("=== Debugging Hexagonal Separation ===\n\n")

# Source dependencies
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")

# Generate a simple 3-ring hexagonal puzzle
rings <- 3
seed <- 42
diameter <- 200

cat("Generating hexagonal puzzle with", rings, "rings\n\n")

# Generate pieces internally (compact positions)
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")

# Check piece centers at compact position
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]))
}

# Apply separation
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]))
}

# Check for duplicate coordinates in paths
cat("\n=== Checking for Duplicate/Shared Starting Points ===\n")

# Extract all M (move) commands from paths
extract_move_points <- function(path) {
  # Find all "M x y" patterns
  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)
  }
}

# Find any shared starting points
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")
}

# Check piece 1 (center) path in detail
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 coordinate pairs from the path
extract_all_coords <- function(path) {
  # Remove commands and split by space
  coords_str <- gsub("[MLCAZ]", " ", path)
  numbers <- as.numeric(unlist(strsplit(trimws(coords_str), "\\s+")))
  numbers <- numbers[!is.na(numbers)]

  # Pair up as x,y
  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)))

# Check if any coordinates are at (0,0) or very close to origin
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))

# Check an outer piece
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)))

# Check if any coordinates are near the center
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 there are small distances, that might indicate wrong coordinates
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]))
  }
}

# Check for L 0 0 or L commands to origin
cat("\n=== Checking for Lines to Origin ===\n")
for (i in 1:length(positioned$pieces)) {
  p <- positioned$pieces[[i]]
  # Check for L commands near origin
  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))
  }
  # Check for any coordinate pair (0, 0) or near it
  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) {  # Skip center piece
    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")
  }
}

# Look at a middle ring piece in detail
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")