簡體   English   中英

我們可以識別繪制的 rgl 網格的“黑色”面嗎?

[英]Can we identify the "black" faces of a plotted rgl mesh?

我有這個 rgl 網格:

在此處輸入圖像描述

該網格來自等值面。 原始等值面沒有寬度,而此網格的寬度很小。

我有 function f定義等值面f=0 ,所以我可以計算每個頂點的梯度:

library(numDeriv)
normals <- apply(mesh$vb[-4L, ], 2L, function(v) {
  grad(f, v)
})

但是如果我使用這些法線,我會得到一個黑色的“邊”,因為正如我所說的那樣有一個寬度,所以實際上橙色“邊”和黑色“邊”都是同一邊:

在此處輸入圖像描述

理論上,如果我反轉頂點v法線使得f(v) < 0 ,那會很好,但由於不精確,如果我這樣做的話會有幾個黑臉:

ff <- apply(mesh$vb[-4L, ], 2L, f)
normals[, ff < 0] <- -normals[, ff < 0]
mesh$normals <- normals

在此處輸入圖像描述

所以我想知道:有沒有辦法識別用黑色繪制的面孔? 這樣我就可以反轉這些面孔的法線。

法線附加到網格中的頂點,並且記錄在it三角形或四邊形或ib定義相鄰的頂點。 所以你可以尋找一個頂點,它的法線與相鄰的頂點非常不同(大致相反)。

這是一個例子。 在 R 中,網格顯示與在 WebGL 中不同,因為 WebGL 代碼忽略了法線的方向。 我已將 R 顯示替換為下面的 output,但對您的原始問題更簡單的解決方案可能只是使用 WebGL 顯示而不是 R 顯示。

set.seed(1234)

library(rgl)

# Set up a smooth mesh

mesh <- icosahedron3d(col="red") |> subdivision3d(2) |> addNormals()
str(mesh)
#> List of 4
#>  $ material:List of 1
#>   ..$ color: chr "red"
#>  $ it      : num [1:3, 1:320] 1 43 44 13 45 43 14 44 45 44 ...
#>  $ vb      : num [1:4, 1:162] -1.42e-14 1.01e+03 1.63e+03 2.32e+03 -1.42e-14 ...
#>  $ normals : num [1:4, 1:162] 1.30e-16 5.26e-01 8.51e-01 1.00 -4.47e-18 ...
#>  - attr(*, "class")= chr "mesh3d"

# Randomly choose two vertices, and flip their normals:

i <- sample(seq_len(ncol(mesh$vb)), 2)
mesh$normals[1:3, i] <- -mesh$normals[1:3, i]

# Show it, but note that rglwidget uses different shading rules, so the
# dark spots won't show up:

open3d()
#> glX 
#>   1
shade3d(mesh)

截屏


# List all adjacent vertex numbers in a vector
adjacent <- as.numeric(rbind(mesh$it, mesh$it[1, ], NA))

# Extract the normals in Euclidean coordinates
normals <- asEuclidean2(mesh$normals)

# Compute the difference between adjacent normals
normaldiff <- (normals[, adjacent[-1]] 
               - normals[, adjacent[-length(adjacent)]])^2  |>
             colSums() |> 
             sqrt()

# Select the big ones that are not NA
bad <- !is.na(normaldiff) & normaldiff > 1

# Which vertices show up a lot?  
table(adjacent[c(bad, FALSE)])
#> 
#>  23  24  28  78  79  80  83  86  94  95  98 100 102 104 
#>   1   1   6   1   1   6   1   1   1   1   1   1   1   1

# Which vertices were originally chosen?
i
#> [1] 28 80

創建於 2023-06-03,使用reprex v2.0.2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM