[R] Visualization of a Convex Hull in R (Possibly with RGL)

Michael Sumner mdsumner at gmail.com
Tue Jul 26 12:48:22 CEST 2016


On Tue, 26 Jul 2016 at 20:29 Lorenzo Isella <lorenzo.isella at gmail.com>
wrote:

> Dear All,
> I am not an expert about the calculation and visualization of convex
> hulls, but I am trying to do something relatively simple.
> Please consider the snippet at the end of the email.
> The array pts  represents the position of (the centres of) a set of
> spheres in 3D (whose radius is 0.5).
> I found the geometry package which provides bindings to the qhull
> (http://www.qhull.org/) library and allows me to calculate the convex
> hull of pts and some interesting properties, like the area and the
> volume of the convex hull.
> That is all fine, but I would like to represent the results in 3D.
> I would like to see a set of spheres with the given position in pts
> and the radius R=0.5 and the convex hull going based on the
> coordinates of pts.
> Does anybody know how to achieve that?
> Many thanks
>
> Lorenzo
>
>
>
> ####################################################################
>
>
> library(geometry)
>
>
>
> pts <- read.table("test-agg.dat")
>
> ## names(pts) <- c("x", "y", "z")
>
>
> pts <- structure(list(x = c(0.145138112090815, 0.880429452229448,
> -1.66682740805663,
> -0.955356943224252, 1.09396798448412, -2.63620653885452,
> 0.190270563436841,
> 2.77638842095489, -0.0914977922901252, -0.484087722062158,
> 0.674992784754569,
> 1.02571108118502, 2.78789154912298, -0.146882388586427,
> -1.71507089246001,
> -1.87886026272455, 6.61540228778138, 7.46818822627362,
> 4.80777628045963,
> 6.35487269602107, 4.70026626314108, 3.44366671298125,
> 2.92460648354883,
> 5.07053866161192, 7.2164343710271, 6.93292242981006, 7.84238243682145,
> 7.4321237311656, 9.59692827237335, 8.3454086671306, 6.574251622581,
> 6.93340304175759), y = c(-0.189309482569001, 1.67057595730283,
> -0.995542605143325, 2.31599069771684, 1.79876139786291,
> 0.165886909346058,
> 2.70740725397259, 1.70535678189514, -0.819087329147786,
> 0.967935667739331,
> -2.53860079551206, -1.60932884056828, -1.42050583991613,
> -2.36883245674979,
> 0.191848867151458, -1.58255618338079, 1.98324724741419,
> 3.0095048680572,
> 1.97159273200079, 2.26459561200295, 0.4461804374566, 1.87939977307282,
> 1.98510457359889, -0.103495422088799, -1.32050185858493,
> 0.584580736435273,
> -2.97562362406436, 0.106204178143333, -2.0843758994948,
> -1.1492729389287,
> -2.13797391772643, -4.45916649729404), z = c(-1.44428170702261,
> -1.45742688370091, -1.70267889327056, -1.93637881287001,
> -3.4452409532781,
> -3.02436538816822, 0.114790290814684, -2.10208878117278,
> 2.07425243689128,
> 1.26652602551291, 1.39914827137784, -0.345006925422662,
> 0.596828021941431,
> 3.351773622867, 2.68408561840144, 3.97006405709929, 0.82767367646934,
> -0.662142231346811, 1.68344957582882, 2.92819854377685,
> 0.386683699222387,
> -0.220305098209874, 2.37510769001993, -1.51041233970289,
> -0.707073219742548,
> -1.24585080403725, -1.63914669343685, 0.683153891726357,
> -1.26623658129696,
> 1.95073173465968, -2.94804638502708, -0.635785458903106)), .Names =
> c("x",
> "y", "z"), class = "data.frame", row.names = c(NA, -32L))
>
> res<-convhulln(pts, options = "FA")
>
>
In short

library(rgl)
bg3d("grey")
spheres3d(pts, radius = 0.5, col = "white")
## triangle functions generally expect triplets of points, one after another
## and hull/triangulation functions generally return arrays of indexes
## so transpose index of hull is the right order to draw triangles
triangles3d(pts[t(res$hull), ], col = "firebrick", alpha = 0.3)

(But, I wonder if you mean to find the convex hull based on the outer shell
of those spheres? )

Cheers, Mike.



> print("So far so good")
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
-- 
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

	[[alternative HTML version deleted]]



More information about the R-help mailing list