tests/testthat/test-mesh-sanity.R

context("mesh-sanity")

library(raster)
library(silicate)
library(dplyr)
v <- raster(diag(3))
#p <- raster::rasterToPolygons(v) %>% sf::st_as_sf() %>% group_by(layer) %>% summarize()
p <- structure(list(layer = c(0, 1), geometry = structure(list(structure(list(
  list(structure(c(0.333333333333, 0, 0, 0, 0.333333333333,
                   0.333333333333, 0.666666666667, 0.666666666667, 0.333333333333,
                   0, 0, 0.333333333333, 0.666666666667, 0.666666666667, 0.333333333333,
                   0.333333333333, 0, 0), .Dim = c(9L, 2L))), list(structure(c(1,
                                                                               1, 0.666666666667, 0.666666666667, 0.333333333333, 0.333333333333,
                                                                               0.666666666667, 1, 1, 0.666666666667, 0.333333333333, 0.333333333333,
                                                                               0.666666666667, 0.666666666667, 1, 1, 1, 0.666666666667), .Dim = c(9L,
                                                                                                                                                  2L)))), class = c("XY", "MULTIPOLYGON", "sfg")), structure(list(
                                                                                                                                                    list(structure(c(0, 0.333333333333, 0.333333333333, 0, 0,
                                                                                                                                                                     1, 1, 0.666666666667, 0.666666666667, 1), .Dim = c(5L, 2L
                                                                                                                                                                     ))), list(structure(c(0.666666666667, 1, 1, 0.666666666667,
                                                                                                                                                                                           0.666666666667, 0.333333333333, 0.333333333333, 0, 0, 0.333333333333
                                                                                                                                                                     ), .Dim = c(5L, 2L))), list(structure(c(0.333333333333, 0.666666666667,
                                                                                                                                                                                                             0.666666666667, 0.333333333333, 0.333333333333, 0.666666666667,
                                                                                                                                                                                                             0.666666666667, 0.333333333333, 0.333333333333, 0.666666666667
                                                                                                                                                                     ), .Dim = c(5L, 2L)))), class = c("XY", "MULTIPOLYGON", "sfg"
                                                                                                                                                                     ))), class = c("sfc_MULTIPOLYGON", "sfc"), precision = 0, bbox = structure(c(xmin = 0,
                                                                                                                                                                                                                                                  ymin = 0, xmax = 1, ymax = 1), class = "bbox"), crs = structure(list(
                                                                                                                                                                                                                                                    input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), row.names = c(NA,
                                                                                                                                                                                                                                                                                                                                               -2L), agr = structure(c(layer = NA_integer_), class = "factor", .Label = c("constant",
                                                                                                                                                                                                                                                                                                                                                                                                                          "aggregate", "identity")), class = c("sf", "tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                                                                                                                                          ), sf_column = "geometry")
#tp <- st_cast(sfdct::ct_triangulate(p), warn = FALSE)
tp <- structure(list(layer = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                               1, 1, 1, 1, 1, 1), geometry = structure(list(structure(list(structure(c(0,
                                                                                                       0.333333333333, 0, 0, 0, 0, 0.333333333333, 0), .Dim = c(4L,
                                                                                                                                                                2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(0,
                                                                                                                                                                                                                                      0.333333333333, 0.333333333333, 0, 0.333333333333, 0, 0.333333333333,
                                                                                                                                                                                                                                      0.333333333333), .Dim = c(4L, 2L))), class = c("XY", "POLYGON",
                                                                                                                                                                                                                                                                                     "sfg")), structure(list(structure(c(0, 0.333333333333, 0.333333333333,
                                                                                                                                                                                                                                                                                                                         0, 0.666666666667, 0.333333333333, 0.666666666667, 0.666666666667
                                                                                                                                                                                                                                                                                     ), .Dim = c(4L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
                                                                                                                                                                                                                                                                                       structure(c(0.333333333333, 0, 0, 0.333333333333, 0.333333333333,
                                                                                                                                                                                                                                                                                                   0.666666666667, 0.333333333333, 0.333333333333), .Dim = c(4L,
                                                                                                                                                                                                                                                                                                                                                             2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
                                                                                                                                                                                                                                                                                                                                                               structure(c(0.333333333333, 0.333333333333, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                           0.333333333333, 1, 0.666666666667, 0.666666666667, 1), .Dim = c(4L,
                                                                                                                                                                                                                                                                                                                                                                                                                                           2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
                                                                                                                                                                                                                                                                                                                                                                                                                                             structure(c(0.333333333333, 0.666666666667, 0.333333333333,
                                                                                                                                                                                                                                                                                                                                                                                                                                                         0.333333333333, 0, 0, 0.333333333333, 0), .Dim = c(4L, 2L
                                                                                                                                                                                                                                                                                                                                                                                                                                                         ))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             1, 1, 0.666666666667, 0.333333333333, 0.333333333333, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             0.333333333333), .Dim = c(4L, 2L))), class = c("XY", "POLYGON",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "sfg")), structure(list(structure(c(0.666666666667, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                0.333333333333, 0.666666666667, 0, 0.333333333333, 0.333333333333,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                0), .Dim = c(4L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  structure(c(0.666666666667, 0.666666666667, 0.333333333333,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              0.666666666667, 0.666666666667, 1, 1, 0.666666666667), .Dim = c(4L,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                structure(c(0.666666666667, 0.666666666667, 1, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1, 0.666666666667, 0.666666666667, 1), .Dim = c(4L, 2L))), class = c("XY",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 "POLYGON", "sfg")), structure(list(structure(c(1, 1, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1, 0.666666666667, 1, 1, 0.666666666667), .Dim = c(4L, 2L))), class = c("XY",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "POLYGON", "sfg")), structure(list(structure(c(0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       0.666666666667, 1, 0.666666666667, 0.666666666667, 0.333333333333,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       0.666666666667, 0.666666666667), .Dim = c(4L, 2L))), class = c("XY",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      "POLYGON", "sfg")), structure(list(structure(c(0, 0.333333333333,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     0.333333333333, 0, 1, 0.666666666667, 1, 1), .Dim = c(4L, 2L))), class = c("XY",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "POLYGON", "sfg")), structure(list(structure(c(0.333333333333,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               0, 0, 0.333333333333, 0.666666666667, 1, 0.666666666667, 0.666666666667
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ), .Dim = c(4L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  structure(c(0.333333333333, 0.666666666667, 0.333333333333,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              0.333333333333, 0.333333333333, 0.333333333333, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              0.333333333333), .Dim = c(4L, 2L))), class = c("XY", "POLYGON",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "sfg")), structure(list(structure(c(0.666666666667, 1, 1, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 0, 0, 0.333333333333, 0), .Dim = c(4L, 2L))), class = c("XY",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "POLYGON", "sfg")), structure(list(structure(c(1, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        0.666666666667, 1, 0.333333333333, 0.333333333333, 0, 0.333333333333
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ), .Dim = c(4L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           structure(c(0.666666666667, 0.666666666667, 0.333333333333,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       0.666666666667, 0.333333333333, 0.666666666667, 0.666666666667,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       0.333333333333), .Dim = c(4L, 2L))), class = c("XY", "POLYGON",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      "sfg"))), class = c("sfc_POLYGON", "sfc"), precision = 0, bbox = structure(c(xmin = 0,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ymin = 0, xmax = 1, ymax = 1), class = "bbox"), crs = structure(list(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), row.names = c(NA,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                -18L), class = c("sf", "tbl_df", "tbl", "data.frame"), sf_column = "geometry", agr = structure(c(layer = NA_integer_), .Label = c("constant",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  "aggregate", "identity"), class = "factor"))


nverts <- 16

test_that("vertex de-duplication is sane", {
  expect_equal(sc_coord(p) %>% distinct() %>% nrow(),
               nverts)
  expect_equal(sc_coord(tp) %>% distinct() %>% nrow(),
               nverts)
  #expect_equal(anglr(p)$v  %>% nrow(),
    #           nverts)
 # expect_equal(anglr(tp)$v  %>% nrow(),
   #            nverts)

})

ntriangles <- nrow(sc_path(tp))
test_that("triangle set is equivalent", {
  expect_equal(ntriangles, 18L)

  ## triangulating p here and below fails because of https://github.com/hypertidy/anglr/issues/54
  ## but it works for tp because those triangles already exist and the mesh comes out the same
 # anglr(p)$t %>% nrow() %>%  expect_equal(ntriangles)
  #anglr(tp)$t %>% nrow() %>%  expect_equal(ntriangles)

  ## we expect 18 because although the (constant) z value requires distinct features
  ## the number of triangles is the same, as one feature is in the gaps of the other
 # anglr(p, z = "layer")$t %>% nrow() %>%  expect_equal(ntriangles)
#  anglr(tp, z = "layer")$t %>% nrow() %>%  expect_equal(ntriangles)

})
r-gris/trimesh documentation built on Nov. 18, 2022, 2:46 a.m.