The module in this example manipulates both the data and the connections components of an input Field (the modules in Chapter 5. "Working with Data" and Chapter 6. "Working with Positions", did not require a connections component). Modules that perform interpolation need information about the interpolation elements (connections). For example, the Isosurface module uses different interpolation methods according to the type of connection.
The AverageCell module computes, for each cell, the average of the data values of that cell and all its neighbors.
Note: | This module works only on data that is cell-centered (i.e., connection-dependent) and has connections of type "quads." |
The AverageCell module takes one input: input, of type field, which has no default value. The module has one output: output, of type field.
(1) Start the Module Builder with the command:
dx -builder
The Module Builder dialog box appears. Note that the dialog box carries no information, since no module has been specified. (For a simple example of creating a module with the Module Builder, see 3.3 , "Using the Module Builder: A Quick Walk Through").
(2) Select Open from the File pull-down menu. An Open a Module Builder file... dialog box appears.
(3) Read in /usr/local/dx/samples/program_guide/averagecell.mb as follows:
(4) Save the .mb file to a writable directory (use Save As... in the File pull-down menu).
(5) Select Create All from the Build pull-down menu of the dialog box. This option creates three files for the module: averagecell.c, averagecell.mdf, and averagecell.make,
(6) Implement the AverageCell function.
Use an editor to add the following lines after "User's code goes here" in the averagecell.c file:
int *itemcounts = NULL, i, neighbor; /* make scratch space to hold the number of items added for each element */ itemcounts = DXAllocate(input_knt*sizeof(int)); if (!itemcounts) goto error; /* * first initialize the output data component to zero, and itemcounts to * zero. */ for (i=0; i<input_knt; i++) { output_data[i] = 0; itemcounts[i]=0; }
/* for each data value, add that value to the appropriate items in the * output data array. Also increment itemcounts for those cells. */ for (i=0; i<input_knt; i++) { /* first do itself */ output_data[i]+=input_data[i]; itemcounts[i]++; /* now do neighbors in fastest-varying dimension */ neighbor = i-1; if (neighbor >= 0 && ((i % (c_counts[1]-1)) != 0)) { output_data[neighbor]+=input_data[i]; itemcounts[neighbor]++; }
neighbor = i+1; if (neighbor < input_knt &&(((i+1)%(c_counts[1]-1)) != 0)) { output_data[neighbor]+=input_data[i]; itemcounts[neighbor]++; } /* now do neighbors in the slowest-varying dimension */ neighbor = i - (c_counts[1]-1); if (neighbor >= 0) { output_data[neighbor]+=input_data[i]; itemcounts[neighbor]++; } neighbor = i + (c_counts[1]-1); if (neighbor < input_knt) { output_data[neighbor]+=input_data[i]; itemcounts[neighbor]++; } }
/* now divide by the number of items added for that cell */ for (i=0; i< input_knt; i++) output_data[i] = output_data[i]/itemcounts[i]; DXFree((Pointer)itemcounts); return OK; error: DXFree((Pointer)itemcounts); return ERROR; }
The file /usr/local/dx/samples/program_guide/averagecell.c contains a completed version of this program.
(7) To create a version of Data Explorer that includes the AverageCell module, enter the command:
make -f averagecell.make dxexec
(You have now created an executable that contains the AverageCell module.)
(8) To invoke this version, enter:
dx -mdf ./averagecell.mdf -exec ./dxexec
This command starts Data Explorer (the averagecell.mdf file tells the graphical user interface about AverageCell and its inputs and outputs). The executable dxexec invoked here is the one created in Step 6.
(9) With this version of Data Explorer you can now run any visual program that uses the AverageCell module. One such program is /usr/local/dx/samples/program_guide/averagecell.net