Giter VIP home page Giter VIP logo

polygonsoup's Introduction

PolygonSoup

Make a consistent mesh from a polygon soup.

R-CMD-check

A polygon soup is a set of unorganized polygons. For example, define a tetrahedron as follows:

vertices <- rbind(
  c(-1, -1, -1),
  c( 1,  1, -1),
  c( 1, -1,  1),
  c(-1,  1,  1)
)
faces <- rbind(
  c(1, 2, 3),
  c(3, 4, 2),
  c(4, 2, 1),
  c(4, 3, 1)
)

Now plot it by painting the front of its faces in blue and the back of its faces in red; then both colors appear:

library(rgl)
tmesh <- tmesh3d(
  vertices    = t(vertices),
  indices     = t(faces),
  homogeneous = FALSE
)
shade3d(tmesh, color = "blue", back = "cull")
shade3d(tmesh, color = "red", front = "cull")

To get a mesh with coherently oriented faces, use the Mesh function:

library(PolygonSoup)
mesh  <- Mesh(vertices, faces, normals = FALSE)
tmesh <- toRGL(mesh)
shade3d(tmesh, color = "blue", back = "cull")

The PolygonSoup package can deal with polygon soups whose polygons have any number of sides. For example, consider a pentagrammic prism:

The vertices and the faces of a pentagrammic prism are given in the package, in a list named PentagrammicPrism. Two faces, at the center of the two pentagramms, are pentagonal. Therefore one cannot directly plot the prism with rgl. The Mesh function allows to triangulate the faces:

mesh <- Mesh(
  mesh = pentagrammicPrism,
  triangulate = TRUE, normals = FALSE
)

Now we can plot the pentagrammic prism. We can also plot its edges with the help of the plotEdges function:

tmesh <- toRGL(mesh)
shade3d(tmesh, color = "maroon")
plotEdges(
  mesh[["vertices"]], mesh[["edges"]], color = "darkred",
  tubesRadius = 0.02, spheresRadius = 0.02
)

The triangulation introduces additional edges, and you probably don't want to see them. In mesh[["edges0"]], one has the edges before the triangulation:

shade3d(tmesh, color = "maroon")
plotEdges(
  mesh[["vertices"]], mesh[["edges0"]], color = "darkred",
  tubesRadius = 0.02, spheresRadius = 0.02
)

One can also plot only the exterior edges, that is to say the edges which are not between two coplanar faces:

shade3d(tmesh, color = "maroon")
plotEdges(
  mesh[["vertices"]], mesh[["exteriorEdges"]], color = "darkred",
  tubesRadius = 0.02, spheresRadius = 0.02
)

License

This package is provided under the GPL-3 license but it uses the C++ library CGAL. If you wish to use CGAL for commercial purposes, you must obtain a license from the GeometryFactory.

polygonsoup's People

Contributors

stla avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

missilent

polygonsoup's Issues

wrong vertex index count in writing .obj files

Dear developers,

when using the function writeMeshFile() to write a .obj file (Wavefront file), the vertex indexing start from zero, while it should start from 1 (https://en.wikipedia.org/wiki/Wavefront_.obj_file).

This wrong indexing prevent the file to be imported correctly in 3D visualization/modeling tools such as Blender.

example code:

rm(list=ls())
library(PolygonSoup)

my.mesh <- list()
my.mesh$vertices <-rbind(c(1,1,0),c(1,-1,0),c(-1,-1,0),c(-1,1,0))
my.mesh$faces <- matrix(c(1,2,3,4), ncol=4)
writeMeshFile(mesh = my.mesh, filename = 'my_mesh.obj')

resulting in output file (my_mesh.obj):

# file written from a CGAL tool in Wavefront obj format
# 4 vertices
# 0 halfedges
# 1 facets


# 4 vertices
# ------------------------------------------

v 1 1 0
v 1 -1 0
v -1 -1 0
v -1 1 0

# 1 facets
# ------------------------------------------

f  0 1 2 3

# End of Wavefront obj format #

that cannot be used, since facets are defined with vertex index starting from zero (f 0 1 2 3 while it should be f 1 2 3 4).

Linking to libatomic is required on 32-bit platforms

/opt/local/bin/g++-mp-12 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/opt/local/Library/Frameworks/R.framework/Resources/lib -Wl,-headerpad_max_install_names -Wl,-rpath,/opt/local/lib/libgcc -L/opt/local/lib -lMacportsLegacySupport -arch ppc -o PolygonSoup.so RcppExports.o ReadWrite.o mesh.o unexported1.o unexported2.o -L/opt/local/lib -lgmp -lmpfr -lgmp -F/opt/local/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /opt/local/var/macports/build/_opt_PPCRosettaPorts_R_R-PolygonSoup/R-PolygonSoup/work/destroot/opt/local/Library/Frameworks/R.framework/Versions/4.2/Resources/library/00LOCK-PolygonSoup/00new/PolygonSoup/libs
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘PolygonSoup’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/opt/local/var/macports/build/_opt_PPCRosettaPorts_R_R-PolygonSoup/R-PolygonSoup/work/destroot/opt/local/Library/Frameworks/R.framework/Versions/4.2/Resources/library/00LOCK-PolygonSoup/00new/PolygonSoup/libs/PolygonSoup.so':
  dlopen(/opt/local/var/macports/build/_opt_PPCRosettaPorts_R_R-PolygonSoup/R-PolygonSoup/work/destroot/opt/local/Library/Frameworks/R.framework/Versions/4.2/Resources/library/00LOCK-PolygonSoup/00new/PolygonSoup/libs/PolygonSoup.so, 6): Symbol not found: ___atomic_load_8
  Referenced from: /opt/local/var/macports/build/_opt_PPCRosettaPorts_R_R-PolygonSoup/R-PolygonSoup/work/destroot/opt/local/Library/Frameworks/R.framework/Versions/4.2/Resources/library/00LOCK-PolygonSoup/00new/PolygonSoup/libs/PolygonSoup.so
  Expected in: dynamic lookup

Error: loading failed
Execution halted
ERROR: loading failed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.