Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created December 23, 2025 04:37
Show Gist options
  • Select an option

  • Save mdsumner/757f2bf6bba47b3f01a9fae9af1f29aa to your computer and use it in GitHub Desktop.

Select an option

Save mdsumner/757f2bf6bba47b3f01a9fae9af1f29aa to your computer and use it in GitHub Desktop.
# test_minimal_mesh.R
# Run this locally where wk/vctrs are available

library(wk)
library(vctrs)

# Source our wkpool code
source("R/wkpool.R")
#> Warning in file(filename, "r", encoding = encoding): cannot open file
#> 'R/wkpool.R': No such file or directory
#> Error in file(filename, "r", encoding = encoding): cannot open the connection
source("R/establish_topology.R")
#> Warning in file(filename, "r", encoding = encoding): cannot open file
#> 'R/establish_topology.R': No such file or directory
#> Error in file(filename, "r", encoding = encoding): cannot open the connection
source("R/merge_coincident.R")
#> Warning in file(filename, "r", encoding = encoding): cannot open file
#> 'R/merge_coincident.R': No such file or directory
#> Error in file(filename, "r", encoding = encoding): cannot open the connection

# The minimal_mesh from silicate - two adjacent multipolygons
x <- wk::as_wkb(c(
  "MULTIPOLYGON (((0 0, 0 1, 0.75 1, 1 0.8, 0.5 0.7, 0.8 0.6, 0.6899999999999999 0, 0 0), (0.2 0.2, 0.5 0.2, 0.5 0.4, 0.3 0.6, 0.2 0.4, 0.2 0.2)))",
  "MULTIPOLYGON (((0.6899999999999999 0, 0.8 0.6, 1.1 0.63, 1.23 0.3, 0.6899999999999999 0)))"
))

cat("=== Input geometry ===\n")
#> === Input geometry ===
print(x)
#> <wk_wkb[2]>
#> [1] <MULTIPOLYGON (((0 0, 0 1, 0.75 1, 1 0.8, 0.5 0.7, 0.8 0.6...>  
#> [2] <MULTIPOLYGON (((0.69 0, 0.8 0.6, 1.1 0.63, 1.23 0.3, 0.69 0)))>

cat("\n=== Raw coordinates from wk ===\n")
#> 
#> === Raw coordinates from wk ===
coords <- wk_coords(x)
print(coords)
#>    feature_id part_id ring_id    x    y
#> 1           1       2       1 0.00 0.00
#> 2           1       2       1 0.00 1.00
#> 3           1       2       1 0.75 1.00
#> 4           1       2       1 1.00 0.80
#> 5           1       2       1 0.50 0.70
#> 6           1       2       1 0.80 0.60
#> 7           1       2       1 0.69 0.00
#> 8           1       2       1 0.00 0.00
#> 9           1       2       2 0.20 0.20
#> 10          1       2       2 0.50 0.20
#> 11          1       2       2 0.50 0.40
#> 12          1       2       2 0.30 0.60
#> 13          1       2       2 0.20 0.40
#> 14          1       2       2 0.20 0.20
#> 15          2       4       3 0.69 0.00
#> 16          2       4       3 0.80 0.60
#> 17          2       4       3 1.10 0.63
#> 18          2       4       3 1.23 0.30
#> 19          2       4       3 0.69 0.00

cat("\n=== Establish topology (raw, no merge) ===\n")
#> 
#> === Establish topology (raw, no merge) ===
pool <- establish_topology(x)
#> Error in establish_topology(x): could not find function "establish_topology"
print(pool)
#> Error: object 'pool' not found

cat("\nVertices:\n")
#> 
#> Vertices:
print(pool_vertices(pool))
#> Error in pool_vertices(pool): could not find function "pool_vertices"

cat("\nSegments:\n")
#> 
#> Segments:
print(pool_segments(pool))
#> Error in pool_segments(pool): could not find function "pool_segments"

cat("\n=== Topology report (before merge) ===\n")
#> 
#> === Topology report (before merge) ===
report <- topology_report(pool)
#> Error in topology_report(pool): could not find function "topology_report"
print(report)
#> Error: object 'report' not found

cat("\n=== Merge coincident vertices (exact) ===\n")
#> 
#> === Merge coincident vertices (exact) ===
merged <- merge_coincident(pool, tolerance = 0)
#> Error in merge_coincident(pool, tolerance = 0): could not find function "merge_coincident"
print(merged)
#> Error: object 'merged' not found

cat("\nVertices after merge:\n")
#> 
#> Vertices after merge:
print(pool_vertices(merged))
#> Error in pool_vertices(merged): could not find function "pool_vertices"

cat("\nSegments after merge:\n")
#> 
#> Segments after merge:
print(pool_segments(merged))
#> Error in pool_segments(merged): could not find function "pool_segments"

cat("\n=== Shared edges ===\n")
#> 
#> === Shared edges ===
shared <- find_shared_edges(merged)
#> Error in find_shared_edges(merged): could not find function "find_shared_edges"
print(shared)
#> Error: object 'shared' not found

cat("\n=== Test subsetting ===\n")
#> 
#> === Test subsetting ===
sub <- pool[1:5]
#> Error: object 'pool' not found
print(sub)
#> function (pattern, replacement, x, ignore.case = FALSE, perl = FALSE, 
#>     fixed = FALSE, useBytes = FALSE) 
#> {
#>     if (is.factor(x) && length(levels(x)) < length(x)) {
#>         sub(pattern, replacement, levels(x), ignore.case, perl, 
#>             fixed, useBytes)[x]
#>     }
#>     else {
#>         if (!is.character(x)) 
#>             x <- as.character(x)
#>         .Internal(sub(as.character(pattern), as.character(replacement), 
#>             x, ignore.case, perl, fixed, useBytes))
#>     }
#> }
#> <bytecode: 0x55ae04968c08>
#> <environment: namespace:base>
cat("Pool vertices preserved:", nrow(pool_vertices(sub)), "\n")
#> Error in pool_vertices(sub): could not find function "pool_vertices"

cat("\n=== Test combining ===\n")
#> 
#> === Test combining ===
combined <- vec_c(pool, pool)
#> Error: object 'pool' not found
cat("Combined segments:", length(combined), "\n")
#> Error: object 'combined' not found
cat("Combined vertices:", nrow(pool_vertices(combined)), "\n")
#> Error in pool_vertices(combined): could not find function "pool_vertices"


# Visual check: plot the segments
cat("\n=== Plotting (if interactive) ===\n")
#> 
#> === Plotting (if interactive) ===
if (interactive()) {
  vertices <- pool_vertices(merged)
  segments <- pool_segments(merged)

  plot(vertices$x, vertices$y, pch = 19, cex = 0.5,
       xlab = "x", ylab = "y", main = "minimal_mesh as wkpool")

  # Draw segments
  for (i in seq_len(nrow(segments))) {
    v0 <- vertices[vertices$.vx == segments$.vx0[i], ]
    v1 <- vertices[vertices$.vx == segments$.vx1[i], ]
    lines(c(v0$x, v1$x), c(v0$y, v1$y), col = "steelblue")
  }

  # Highlight shared edge vertices
  text(vertices$x, vertices$y, vertices$.vx, cex = 0.7, pos = 3)
}

Created on 2025-12-23 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment