X
35 Rate this article:
No rating

INTERNAL: 3-dimensional convex hull example

Zachary Norman
Topic:
This tech tip provides an example that constructs a convex hull about a set of points in 3-dimensions.Discussion:
[Edit this field in the IDL-based Tech Tip Editor, v62]Solution:
pro add_axes, oModel

; Tick length
    tLen = 0.05

    oAxisX = obj_new('IDLgrAxis',0, TICKLEN = tLen) ; x-axis
    oAxisY = obj_new('IDLgrAxis',1, TICKLEN = tLen) ; y-axis
    oAxisZ = obj_new('IDLgrAxis',2, TICKLEN = tLen) ; z-axis

; Add to the model
    oModel -> Add, [oAxisX,oAxisY,oAxisZ]

end


pro test_3d_convex_hull

; Create some random data
    n = 100
    data = randomu(seed, 3, 100)

; Display the data
    odata = obj_new('IDLgrPolyline', data, $
        SYMBOL = obj_new('IDLgrSymbol', 3, SIZE = [0.1,0.1,0.1]), $
        LINESTYLE = 6)
    oDataModel = obj_new('IDLgrModel')
    oDataModel -> Add, odata
    add_axes, oDataModel
    xobjview, oDataModel, TITLE = 'Data', $
        XSIZE = 500, YSIZE = 500, $
        XOFF = 0, YOFF = 0

; Get the points defining the convex hull.
    qhull, data, convexHull

; Create a connectivity array from the convex hull
    polys = lonarr(4, (size(convexHull, /DIM))[1])
    polys[0,*] = 3
    polys[1:3,*] = convexHull
    polys = reform(polys, n_elements(polys), /OVERWRITE)

; Create a polygon from the convex hull.
    oPoly = obj_new('IDLgrpolygon', data, $
        POLYGONS = polys, $
        COLOR = [0,255,0])

; Display the convex hull
    oHullModel = obj_new('IDLgrModel')
    oHullModel -> Add, oPoly
    add_axes, oHullModel
    xobjview, oHullModel, TITLE = 'Convex Hull', $
        XSIZE = 500, YSIZE = 500, $
        XOFF = 510, YOFF = 0, /BLOCK

    oData -> GetProperty, SYMBOL = oSym
    obj_destroy, [oDataModel,oHullModel,oSym]

end