X
499 Rate this article:
No rating

INTERNAL/REVIEW: Decimating a Mesh Object in IDL Object Graphics

Help Article Update 3

Anonym

[Needs to be reviewed for Compliance and IP issues (i.e. .pro file included)]

This example decimates a DEM (digital elevation model) mesh. Decimation reduces either the number of vertices or the number of connections within a mesh while trying to maintain the overall shape of the mesh. Very large meshes usually contain redundant or useless information, which may slow down any interactive displays of the mesh. Decimation helps to reduce the size of these large meshes.

The DEM in this example comes from the elevbin.dat file found in the /examples/data directory. The DEM is converted into a mesh with the MESH_OBJ procedure. The results of this routine are placed in a polygon object, which is added to a model. The models are displayed in the XOBJVIEW utility. The XOBJVEW utility allows you to click-and-drag the polygon object to rotate and translate it. See XOBJVIEW in the IDL Help for more information on this utility.

The first display contains a wire outline of the DEM as a mesh. When you quit out of the first XOBJVIEW display, the second XOBJVIEW display will appear showing a filled mesh. The colors correspond to the change in elevation. The third display is the result of decimating the mesh down to 20 percent of the original number of vertices. The final display is the resulting mesh filled with the elevation colors.

Code example:

Click here to download DecimatingAMesh.pro.

PRO decimatingAMesh
 
    ; Determine path to data file.
    elevbinFile = FILEPATH('elevbin.dat', $
        SUBDIRECTORY = ['examples', 'data'])
 
    ; Initialize data parameters.
    elevbinSize = [64, 64]
    elevbinData = BYTARR(elevbinSize[0], elevbinSize[1])
 
    ; Open file, read in data, and close file.
    OPENR, unit, elevbinFile, /GET_LUN
    READU, unit, elevbinData
    FREE_LUN, unit
 
    ; Convert data into a mesh, which is defined by
    ; vertice locations and their connectivity.
    MESH_OBJ, 1, vertices, connectivity, elevbinData
 
    ; Initialize a model for display.
    oModel = OBJ_NEW('IDLgrModel')
 
    ; Form a polygon from the mesh.
    oPolygon = OBJ_NEW('IDLgrPolygon', vertices, $
        POLYGONS = connectivity, SHADING = 1.5, $
        COLOR = [0, 255, 0], STYLE = 1)
 
    ; Add polygon to model.
    oModel -> Add, oPolygon
 
    ; Rotate model for better initial perspective.
    oModel -> Rotate, [-1, 0, 1], 22.5
 
    ; Display model in the XOBJVIEW utility.
    XOBJVIEW, oModel, /BLOCK, SCALE = 1.5, $
        TITLE = 'Original Mesh from Elevation Data'
 
    ; Derive a color table for the filled polygon.
    oPalette = OBJ_NEW('IDLgrPalette')
    oPalette -> LOADCT, 29
 
    ; Fill in the polygon mesh with the colors of the table
    ; (the colors correspond to the z-values of the polygon).
    oPolygon -> SetProperty, STYLE = 2, $
        VERT_COLORS = BYTSCL(vertices[2, *]), $
        PALETTE = oPalette
 
    ; Display model in the XOBJVIEW utility.
    XOBJVIEW, oModel, /BLOCK, SCALE = 1.5, $
        TITLE = 'Filled Original Mesh'
 
    ; Decimate the mesh down to 20 percent of the original
    ; number of vertices.
    numberVertices = MESH_DECIMATE(vertices, connectivity, $
        decimatedConnectivity, VERTICES = decimatedVertices, $
        PERCENT_VERTICES = 20)
 
    ; Update the polygon with the resulting decimated mesh.
    oPolygon -> SetProperty, DATA = decimatedVertices, $
        POLYGONS = decimatedConnectivity, STYLE = 1, $
        VERT_COLORS = 0, COLOR = [0, 255, 0]
 
    ; Display updated model in the XOBJVIEW utility.
    XOBJVIEW, oModel, /BLOCK, SCALE = 1.5, $
        TITLE = 'Decimation Results (by 80%)'
 
    ; Fill in the updated polygon mesh with the colors of
    ; the table (the colors correspond to the z-values of
    ; the updated polygon).
    oPolygon -> SetProperty, STYLE = 2, $
        VERT_COLORS = BYTSCL(decimatedVertices[2, *]), $
        PALETTE = oPalette
 
    ; Display model in the XOBJVIEW utility.
    XOBJVIEW, oModel, /BLOCK, SCALE = 1.5, $
        TITLE = 'Filled Decimation Results'
 
    ; Cleanup all the objects by destroying the model.
    OBJ_DESTROY, [oModel, oPalette]

END