zarquon42b / Rvcg

R-package providing mesh manipulation routines from VCGLIB
Other
25 stars 10 forks source link

Question: Find vertices adjacent to a given vertex? #20

Closed dfsp-spirit closed 3 years ago

dfsp-spirit commented 3 years ago

Is there any fast way to find all vertices incident on a given vertex, or even for all vertices (that would be an adjacency list representation of the mesh?

I only found Rvcg::vcgVFadj which gives the adjacent faces of a vertex. I want the vertices.

If there is nothing, I would give it a try myself and do a PR.

zarquon42b commented 3 years ago

How about this:

GetOneRingNeighbourhood <- function(x,self=FALSE) {
    VFadj <- vcgVFadj(x)
    VVadj <- lapply(b,function(x) x <- unique(humface$it[,x]))
    if (!self) ## remove vertex from its own neighbourhood
        VVadj <- lapply(1:length(b),function(x) VVadj[[x]] <- VVadj[[x]][-which(VVadj[[x]]==x)])
    return(VVadj)
}
data(humface)
nh <- GetOneRingNeighbourhood(humface)
wire3d(humface,specular=1)
shade3d(humface,col="white")

spheres3d(Morpho::vert2points(humface)[nh[[2000]],],radius=1,col="blue")
spheres3d(Morpho::vert2points(humface)[2000,],col="red",radius=1.5)

OneRing

If you think the function is useful, I can implement it in Rvcg. If you want to do this in C++, you can have a look at https://github.com/zarquon42b/Rvcg/blob/master/src/ROneRing.cpp where I was playing around with visiting a OneRing neighbourhood it computes the number of adjacent vertices and the area of the one-ring neighbourhood. The corresponding R-function is a (private) function Rvcg:::vcgOneRingArea. You can find details about iterating over a neighbourhood here: http://vcg.isti.cnr.it/vcglib/adjacency.html

dfsp-spirit commented 3 years ago

I also tried that, but it gets slow for larger meshes. For humface its okay, but for my brain meshes with 160k vertices per hemisphere it already hurts, especially when filtering out the query vertices from the neighborhoods.

I think I have come up with something quite fast based on vcg::face::VVExtendedStarVF. See the RVVadj function at the bottom of this file. I will test some more, separate the function into its own branch and do a PR if that's fine with you.

zarquon42b commented 3 years ago

Your are right. It becomes slow quickly. But the RVVadj code looks fine to me. Thanks for the contribution.

dfsp-spirit commented 3 years ago

I've opened a PR for the C++ function, so I am closing this.