Example a_backup/resample_texture: Resample texture.

This example shows how to use cmgui to resample a texture volume using a coordinate space transformation defined by a cubic hermite element.

Screenshot of example a_backup/resample_texture


The comfile run by this example is as follows:

############################################################################
#
# This CMGUI command file reads two images from a volume scan of porcine
# cardiac tissue into a receptacle element and then projects the texture
# onto 2D cutting planes. 
#
# To run:
#   cmgui -console -no_display resample_texture.com
#
# Written by Mark Trew, 7 April 2006
############################################################################

{
############################################################################
# Read in nodes and elements describing the mapping from the volume scan
# to the experimental context (MRI fitting). Do not display this
# information, however, can be turned on by typing:
#   gfx set visibility VolumeMRIFit on
# at the command line or by turning on the radio button in the scene 
# editor.
############################################################################

#This element has its own coordinate field "receptacle_coordinates" so that
#when constructing the texture the two different fields we are interested in,
#"coordinates" in the sample plane and "receptacle_coordinates" are not
#sharing the same cache.  This speeds things up an order of magnitude.
gfx read node $example/Receptacle.exnode
gfx read elements $example/Receptacle.exelem
gfx modify g_element Receptacle general clear;

############################################################################
# Read in the image stack. Images are stacked from -z upwards
############################################################################
$t0 = time();
($u0,$s0,$uc0,$sc0)=times();
print(" ... Reading in images ...\n");
gfx create texture volume_scan width 1 height 1 depth 1 clamp_wrap linear_filter image $example/imageXXXX.tif number_pattern "XXXX" number_series 0002 0001 -1 modulate;
$t1 = time();
($u1,$s1,$uc1,$sc1)=times();
$wall_time = $t1-$t0;
$cpu_time = ($u1+$s1)-($u0+$s0);
print( "Image reading time: wall: $wall_time, cpu: $cpu_time\n");

############################################################################
# Set up the texture material and the texture coordinates.
############################################################################
$t0 = time();
($u0,$s0,$uc0,$sc0)=times();
print(" ... Setting up texture material and coordinates ...\n");
# use linear interpolation
gfx modify texture volume_scan linear
# create material with texture on it
gfx create material volume_scan ambient 1 1 1 diffuse 1 1 1 emission 1 1 1 alpha 1.0 texture volume_scan

# define the tex_coords texture coordinates field which vary from 0 to 1 along the axes
gfx read nodes $example/TextureCoordinates.exnode
gfx read elements $example/TextureCoordinates.exelem
gfx modify g_element TextureCoordinates general clear;

############################################################################
# Read in the slice to evaluate the texture onto. Note that slices have a 
# node and element number offset of 10000.
############################################################################
#Read in an initial slice so that the coordinate field is defined.
gfx read node $example/SamplePlane_1.exnode
gfx read elem $example/Slice.exelem

# create a texture coordinate field which can be evaluated using only 
# coordinates.  We need to map from the coordinates in the plane to the 
# receptacle_coordinates to find the corresponding texture_coordinate, tex_coords.
# This effectively means the texture is embedded in the coordinate space of the
# receptacle element.
# Only search the 3D element, not the faces and lines of the Receptacle.
gfx define field LOOKUP_TEXTURE_COORDS compose texture_coordinates coordinates group Receptacle find_element_xi receptacle_coordinates calculate_values_field tex_coords element_dimension 3;

# create a sample texture field which we can use to evaluate the texture
# values for each plane
gfx define field SLICE sample_texture field volume_scan coordinates LOOKUP_TEXTURE_COORDS;

# create an rgb spectrum
gfx cre spectrum rgb clear;
gfx modify spectrum rgb linear range 0 1 extend_above extend_below red colour_range 0 1 ambient diffuse component 1
gfx modify spectrum rgb linear range 0 1 extend_above extend_below green colour_range 0 1 ambient diffuse component 2
gfx modify spectrum rgb linear range 0 1 extend_above extend_below blue colour_range 0 1 ambient diffuse component 3

# create one texture called slice which will be used each time to evaluate
# the SLICE field
gfx create texture slice_image width 1 height 1 depth 0 clamp_wrap decal;
gfx modify texture slice_image linear;
gfx create material slice texture slice_image;

# link up the plane to the material plane
gfx modify g_element slice general clear element_discretization "20*20*20";
gfx modify g_element slice lines coordinate coordinates select_on material default selected_material default_selected;
gfx modify g_element slice surfaces coordinate coordinates select_on material slice texture_coordinates xi selected_material default_selected render_shaded;

$t1 = time();
($u1,$s1,$uc1,$sc1)=times();
$wall_time = $t1-$t0;
$cpu_time = ($u1+$s1)-($u0+$s0);
print( "Set up time: wall: $wall_time, cpu: $cpu_time\n");

############################################################################
# Loop through slices.
############################################################################
for($s=1;$s<=2;++$s)
  {
  gfx read node $example/SamplePlane_${s}.exnode;

  $t0 = time();
  ($u0,$s0,$uc0,$sc0)=times();
  print(" ... Evaluating texture field for slice $s ...\n");
  # evaluate the texture field SLICE over the surface of the plane
  # pixels not contained in the element block will become transparent gray 50% which
  # is also the default "fail_material".
  # We want to specify the texture as we do not want to use the values from the source
  gfx modify texture slice_image specify_width 512 specify_height 512 specify_depth 1 specify_format rgba evaluate element_group slice element_dimension 2 field SLICE spectrum rgb texture_coordinates xi fail_material transparent_gray50;
  $t1 = time();
  ($u1,$s1,$uc1,$sc1)=times();
  $wall_time = $t1-$t0;
  $cpu_time = ($u1+$s1)-($u0+$s0);
  print( "Texture evaluate time: wall: $wall_time, cpu: $cpu_time\n");

  print(" ... Writing texture ...\n");
  gfx write texture slice_image file SamplePlaneStructure_${s}.png;
  }

}

Files used by this example are:

Name                       Modified     Size

resample_texture.com 20-Apr-2012 6.2k COPYRIGHT 19-Apr-2012 504 Receptacle.exelem 20-Apr-2012 9.8k Receptacle.exnode 20-Apr-2012 10k SamplePlane_1.exnode 20-Apr-2012 373 SamplePlane_2.exnode 20-Apr-2012 367 Slice.exelem 20-Apr-2012 1.5k TextureCoordinates.exelem 20-Apr-2012 3.0k TextureCoordinates.exnode 20-Apr-2012 711 image0001.tif 20-Apr-2012 786k image0002.tif 20-Apr-2012 786k

Download the entire example:

Name                                       Modified     Size

examples_a_backup_resample_texture.tar.gz 12-Aug-2014 3.8M

Testing status by version:

StatusTestedReal time (s)
i686-linux
cmgui-wxSuccessSun Mar 6 00:23:41 20167
cmgui-wx-debugSuccessSun Mar 6 00:30:51 201616
cmgui-wx-debug-memorycheckSuccessSun Mar 6 00:39:22 201625
cmgui-wx-debug-valgrindSuccessSun Mar 6 02:46:23 2016467
x86_64-linux
cmgui-wxSuccessThu Jan 7 00:01:55 20165
cmgui-wx-debugSuccessThu Jan 7 00:03:33 20169
cmgui-wx-debug-memorycheckSuccessThu Jan 7 00:04:31 201614
cmgui-wx-debug-valgrindFailureSun Mar 6 00:15:03 2016338
last breakSun Mar 6 00:09:00 2016338
last successWed Jun 3 01:38:00 2015342
cmgui-wx-gcc-cad-debug-valgrindSuccessThu Jan 7 00:14:48 2016282

Testing status by file:


Html last generated: Sun Mar 6 05:51:00 2016

Input last modified: Fri Apr 20 16:01:12 2012


CMISS Help / Examples / a_backup / resample_texture