Mercurial > repos > greg > icqsol_add_texture
changeset 0:4afbbfafc3c2 draft
Uploaded
author | greg |
---|---|
date | Wed, 09 Dec 2015 11:07:55 -0500 |
parents | |
children | 0a94307d301c |
files | icqsol_add_texture.py icqsol_add_texture.xml icqsol_macros.xml icqsol_utils.py test-data/sandstone.jpg test-data/shaft.vtkascii tool_dependencies.xml |
diffstat | 7 files changed, 413 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icqsol_add_texture.py Wed Dec 09 11:07:55 2015 -0500 @@ -0,0 +1,50 @@ +#!/usr/bin/env python +import argparse +import shutil + +import icqsol_utils +from icqsol.shapes.icqShapeManager import ShapeManager + +# Parse Command Line. +parser = argparse.ArgumentParser() +parser.add_argument('--input', dest='input', help='Shape dataset selected from history') +parser.add_argument('--input_file_format_and_type', dest='input_file_format_and_type', help='Input file format and type') +parser.add_argument('--input_dataset_type', dest='input_dataset_type', help='Input dataset_type') +parser.add_argument('--input_texture', dest='input_texture', help='Image dataset selected from history') +parser.add_argument('--input_texture_file_format', dest='input_texture_file_format', help='Input texture file format') +parser.add_argument('--max_edge_length', dest='max_edge_length', type=float, default=float('inf'), help='Maximum edge length') +parser.add_argument('--output', dest='output', help='Output dataset') +parser.add_argument('--output_vtk_type', dest='output_vtk_type', help='Output VTK type') + +args = parser.parse_args() + +input_format, input_file_type = icqsol_utils.get_format_and_type(args.input_file_format_and_type) +tmp_dir = icqsol_utils.get_temp_dir() + +# Instantiate a ShapeManager for loading the input. +if input_format == icqsol_utils.VTK: + shape_mgr = ShapeManager(file_format=input_format, vtk_dataset_type=args.input_dataset_type) +else: + shape_mgr = ShapeManager(file_format=input_format) + +# Get the vtk polydata from the input dataset. +vtk_poly_data = shape_mgr.loadAsVtkPolyData(args.input) +# The ShapeManager.addTextureToVtkPolyData function expects +# a specific file extension at the end of the file path. +tmp_texture_file_path = icqsol_utils.get_input_file_path(tmp_dir, args.input_texture, args.input_texture_file_format) + +# Apply the texture to the shape's surface. +vtk_poly_data = shape_mgr.addTextureToVtkPolyData(vtk_poly_data, + texture_file=tmp_texture_file_path, + max_edge_length=args.max_edge_length) + +# Define the output file format and type (the output_format can only be 'vtk'). +output_format, output_file_type = icqsol_utils.get_format_and_type(args.output_vtk_type) +tmp_output_path = icqsol_utils.get_temporary_file_path(tmp_dir, output_format) + +# Make sure the ShapeManager's writer is vtk. +shape_mgr.setWriter(file_format=icqsol_utils.VTK, vtk_dataset_type=icqsol_utils.POLYDATA) + +# Save the output. +shape_mgr.saveVtkPolyData(vtk_poly_data=vtk_poly_data, file_name=tmp_output_path, file_type=output_file_type) +shutil.move(tmp_output_path, args.output)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icqsol_add_texture.xml Wed Dec 09 11:07:55 2015 -0500 @@ -0,0 +1,64 @@ +<?xml version='1.0' encoding='UTF-8'?> +<tool id="icqsol_add_texture" name="Add texture image" version="@WRAPPER_VERSION@.0"> + <description>to a shape surface</description> + <macros> + <import>icqsol_macros.xml</import> + </macros> + <expand macro="requirements" /> + <command> +<![CDATA[ + python $__tool_directory__/icqsol_add_texture.py + --input "$input" + --input_file_format_and_type $input.ext + --input_dataset_type $input.metadata.dataset_type + --input_texture "$input_texture" + --input_texture_file_format $input_texture.ext + #if $max_edge_length > 0: + --max_edge_length $max_edge_length + #end if + --output "$output" + --output_vtk_type $output_vtk_type +]]> + </command> + <inputs> + <param name="input" type="data" format="vtkascii,vtkbinary" label="Shape" help="Format can be vtkascii or vtkbinary." /> + <param name="input_texture" type="data" format="jpg,png" label="Texture image" help="Format can be jpg or png." /> + <param name="max_edge_length" type="float" value="0.0" optional="True" label="Maximum edge length" help="Optionally refine shape by restricting edges to this length (zero value results in no refinement)."> + <validator type="in_range" min="0" exclude_min="true" /> + </param> + <expand macro="output_vtk_type_params" /> + </inputs> + <outputs> + <data name="output" format="vtkascii"> + <actions> + <action type="format"> + <option type="from_param" name="output_vtk_type" /> + </action> + </actions> + </data> + </outputs> + <tests> + <test> + <param name="input" value="shaft.vtkascii" ftype="vtkascii" /> + <param name="input_file_format_and_type" value="vtkascii" /> + <param name="input_dataset_type" value="POLYDATA" /> + <param name="input_texture" value="sandstone.jpg" ftype="jpg" /> + <param name="max_edge_length" value="0.1" /> + <output name="output" file="shaft_with_texture.vtkascii" ftype="vtkascii" /> + <param name="output_vtk_type" value="vtkascii" /> + </test> + </tests> + <help> +**What it does** + +Adds a texture by applying an image to the surface of a shape. The shape is projected onto a bounding box +and the color is selected by flattening the box to fit the image. Refinement can be applied for a smoother +result. + +* **Shape** - Shape for applying a surface texture. +* **Maximum edge length** - The shape's edges are smoothed by adding points to its edges that are longer than the defined maximum length. +* **Texture image** - Image to be applied to the shape's surface to create the texture. + + </help> + <expand macro="citations" /> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icqsol_macros.xml Wed Dec 09 11:07:55 2015 -0500 @@ -0,0 +1,116 @@ +<?xml version='1.0' encoding='UTF-8'?> +<macros> + <token name="@WRAPPER_VERSION@">1.0</token> + <xml name="requirements"> + <requirements> + <requirement type="package" version="1.0">icqsol</requirement> + </requirements> + </xml> + <xml name="stdio"> + <stdio> + <exit_code range="1:"/> + <exit_code range=":-1"/> + <regex match="Error:"/> + <regex match="Exception:"/> + </stdio> + </xml> + <!-- TODO: place this in colormaps_conf.xml --> + <xml name="color_map_param"> + <param name="color_map" type="select" label="Color map"> + <option value="hot" selected="True">Hot</option> + <option value="cold">Cold</option> + <option value="blackbody">Blackbody</option> + <option value="gnu">Gnu</option> + </param> + </xml> + <xml name="output_vtk_type_params"> + <param name="output_vtk_type" type="select" label="Output file type"> + <option value="vtkascii" selected="True">Vtk-ascii</option> + <option value="vtkbinary">Vtk-binary</option> + </param> + </xml> + <token name="@origin_cmd_args@"> + --origin_x $create_process_cond.shape_cond.origin_x + --origin_y $create_process_cond.shape_cond.origin_y + --origin_z $create_process_cond.shape_cond.origin_z + </token> + <xml name="origin_params"> + <param name="origin_x" type="float" value="0.0" label="X coordinate of origin" help="Floating point number"/> + <param name="origin_y" type="float" value="0.0" label="Y coordinate of origin" help="Floating point number"/> + <param name="origin_z" type="float" value="0.0" label="Z coordinate of origin" help="Floating point number"/> + </xml> + <token name="@lengths_cmd_args@"> + --length_x $create_process_cond.shape_cond.length_x + --length_y $create_process_cond.shape_cond.length_y + --length_z $create_process_cond.shape_cond.length_z + </token> + <xml name="lengths_params"> + <!-- At least one of these lengths must be greater than zero, but we have no validator for this. --> + <param name="length_x" type="float" value="1.0" label="Length in the X direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="false" /> + </param> + <param name="length_y" type="float" value="0.0" label="Length in the Y direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="false" /> + </param> + <param name="length_z" type="float" value="0.0" label="Length in the Z direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="false" /> + </param> + </xml> + <xml name="lengths_exclude_min_params"> + <param name="length_x" type="float" value="1.0" label="Length in the X direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="true" /> + </param> + <param name="length_y" type="float" value="1.0" label="Length in the Y direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="true" /> + </param> + <param name="length_z" type="float" value="1.0" label="Length in the Z direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="true" /> + </param> + </xml> + <token name="@radius_cmd_args@"> + --radius $create_process_cond.shape_cond.radius + </token> + <xml name="radius_params"> + <param name="radius" type="float" value="1.0" label="Radius" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="True" /> + </param> + </xml> + <token name="@n_theta_cmd_args@"> + --n_theta $create_process_cond.shape_cond.n_theta + </token> + <xml name="n_theta_params"> + <param name="n_theta" type="integer" value="16" label="Number of slices" help="Controls the tessellation along the longitude direction"> + <validator type="in_range" min="0" exclude_min="False" /> + </param> + </xml> + <token name="@n_phi_cmd_args@"> + --n_phi $create_process_cond.shape_cond.n_phi + </token> + <xml name="n_phi_params"> + <param name="n_phi" type="integer" value="8" label="Number of stacks" help="Controls the tessellation along the latitude direction"> + <validator type="in_range" min="0" exclude_min="False" /> + </param> + </xml> + <xml name="citations"> + <citations> + <citation type="bibtex"> + @unpublished{None, + author = {None}, + title = {None}, + year = {None}, + eprint = {None}, + url = {https://github.com/gregvonkuster/galaxy-csg} + }</citation> + <citation type="bibtex"> + @misc(Schroeder-Martin-Lorensen2006, + author = "Will Schroeder and + Ken Martin and + Bill Lorensen", + year = "2006", + title = "The Visualization Toolkit (4th ed.)", + publisher = "Kitware", + url = "https://en.wikipedia.org/wiki/Special:BookSources/978-1-930934-19-1") + </citation> + </citations> + </xml> +</macros>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icqsol_utils.py Wed Dec 09 11:07:55 2015 -0500 @@ -0,0 +1,74 @@ +import os +import sys +import tempfile + +PLY = 'ply' +POLYDATA = 'POLYDATA' +VTK = 'vtk' + + +def asbool(val): + return str(val).lower() in ['yes', 'true'] + + +def get_format_and_type(galaxy_ext): + # Define the output file format and type. + format = None + datatype = None + if galaxy_ext in ['vtkascii', 'vtkbinary']: + format = VTK + elif galaxy_ext in ['plyascii', 'plybinary']: + format = PLY + if galaxy_ext in ['vtkascii', 'plyascii']: + datatype = 'ascii' + elif galaxy_ext in ['vtkbinary', 'plybinary']: + datatype = 'binary' + return format, datatype + + +def get_input_file_path(tmp_dir, input_file, format): + """ + iCqSol uses file extensions (e.g., .ply, .vtk) when reading and + writing files, so the Galaxy dataset naming convention of + setting all file extensions as .dat must be handled. + """ + file_path = get_temporary_file_path(tmp_dir, format) + # Remove the file so we can create a symlink. + os.remove(file_path) + os.symlink(input_file, file_path) + return file_path + + +def get_temp_dir(prefix='tmp-vtk-', dir=None): + """ + Return a temporary directory. + """ + return tempfile.mkdtemp(prefix=prefix, dir=dir) + + +def get_tempfilename(dir=None, suffix=None): + """ + Return a temporary file name. + """ + if suffix is None: + s = None + elif suffix.startswith('.'): + s = suffix + else: + s = '.%s' % suffix + fd, name = tempfile.mkstemp(suffix=s, dir=dir) + os.close(fd) + return name + + +def get_temporary_file_path(tmp_dir, file_extension): + """ + Return the path to a temporary file with a valid VTK format + file extension. + """ + return get_tempfilename(tmp_dir, file_extension) + + +def stop_err(msg): + sys.stderr.write("%s\n" % msg) + sys.exit()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/shaft.vtkascii Wed Dec 09 11:07:55 2015 -0500 @@ -0,0 +1,103 @@ +# vtk DataFile Version 3.0 +vtk output +ASCII +DATASET POLYDATA +POINTS 66 float +0 0 0 0 0 -0.1 0 -0.0382683 -0.092388 +1 0 -0.1 1 -0.0382683 -0.092388 1 0 0 +0 -0.0707107 -0.0707107 1 -0.0707107 -0.0707107 0 -0.092388 -0.0382683 +1 -0.092388 -0.0382683 0 -0.1 -6.12323e-18 1 -0.1 -6.12323e-18 +0 -0.092388 0.0382683 1 -0.092388 0.0382683 0 -0.0707107 0.0707107 +1 -0.0707107 0.0707107 0 -0.0382683 0.092388 1 -0.0382683 0.092388 +0 -1.22465e-17 0.1 1 -1.22465e-17 0.1 0 0.0382683 0.092388 +1 0.0382683 0.092388 0 0.0707107 0.0707107 1 0.0707107 0.0707107 +0 0.092388 0.0382683 1 0.092388 0.0382683 0 0.1 1.83697e-17 +1 0.1 1.83697e-17 0 0.092388 -0.0382683 1 0.092388 -0.0382683 +0 0.0707107 -0.0707107 1 0.0707107 -0.0707107 0 0.0382683 -0.092388 +1 0.0382683 -0.092388 0 0 -0.1 0 -0.0382683 -0.092388 +1 0 -0.1 1 -0.0382683 -0.092388 0 -0.0707107 -0.0707107 +1 -0.0707107 -0.0707107 0 -0.092388 -0.0382683 1 -0.092388 -0.0382683 +0 -0.1 -6.12323e-18 1 -0.1 -6.12323e-18 0 -0.092388 0.0382683 +1 -0.092388 0.0382683 0 -0.0707107 0.0707107 1 -0.0707107 0.0707107 +0 -0.0382683 0.092388 1 -0.0382683 0.092388 0 -1.22465e-17 0.1 +1 -1.22465e-17 0.1 0 0.0382683 0.092388 1 0.0382683 0.092388 +0 0.0707107 0.0707107 1 0.0707107 0.0707107 0 0.092388 0.0382683 +1 0.092388 0.0382683 0 0.1 1.83697e-17 1 0.1 1.83697e-17 +0 0.092388 -0.0382683 1 0.092388 -0.0382683 0 0.0707107 -0.0707107 +1 0.0707107 -0.0707107 0 0.0382683 -0.092388 1 0.0382683 -0.092388 + +POLYGONS 48 208 +3 0 1 2 +4 35 34 3 4 +3 5 37 36 +3 0 2 6 +4 38 35 4 7 +3 5 39 37 +3 0 6 8 +4 40 38 7 9 +3 5 41 39 +3 0 8 10 +4 42 40 9 11 +3 5 43 41 +3 0 10 12 +4 44 42 11 13 +3 5 45 43 +3 0 12 14 +4 46 44 13 15 +3 5 47 45 +3 0 14 16 +4 48 46 15 17 +3 5 49 47 +3 0 16 18 +4 50 48 17 19 +3 5 51 49 +3 0 18 20 +4 52 50 19 21 +3 5 53 51 +3 0 20 22 +4 54 52 21 23 +3 5 55 53 +3 0 22 24 +4 56 54 23 25 +3 5 57 55 +3 0 24 26 +4 58 56 25 27 +3 5 59 57 +3 0 26 28 +4 60 58 27 29 +3 5 61 59 +3 0 28 30 +4 62 60 29 31 +3 5 63 61 +3 0 30 32 +4 64 62 31 33 +3 5 65 63 +3 0 32 1 +4 34 64 33 3 +3 5 36 65 + +POINT_DATA 66 +NORMALS Normals float +-1 0 0 -1 0 0 -1 0 0 +0 0 -1 0 -0.382683 -0.92388 1 0 0 +-1 0 0 0 -0.707107 -0.707107 -1 0 0 +0 -0.92388 -0.382683 -1 0 0 0 -1 0 +-1 0 0 0 -0.92388 0.382683 -1 0 0 +0 -0.707107 0.707107 -1 0 0 0 -0.382683 0.92388 +-1 0 0 0 0 1 -1 0 0 +0 0.382683 0.92388 -1 0 0 0 0.707107 0.707107 +-1 0 0 0 0.92388 0.382683 -1 0 0 +0 1 0 -1 0 0 0 0.92388 -0.382683 +-1 0 0 0 0.707107 -0.707107 -1 0 0 +0 0.382683 -0.92388 0 0 -1 0 -0.382683 -0.92388 +1 0 0 1 0 0 0 -0.707107 -0.707107 +1 0 0 0 -0.92388 -0.382683 1 0 0 +0 -1 0 1 0 0 0 -0.92388 0.382683 +1 0 0 0 -0.707107 0.707107 1 0 0 +0 -0.382683 0.92388 1 0 0 0 0 1 +1 0 0 0 0.382683 0.92388 1 0 0 +0 0.707107 0.707107 1 0 0 0 0.92388 0.382683 +1 0 0 0 1 0 1 0 0 +0 0.92388 -0.382683 1 0 0 0 0.707107 -0.707107 +1 0 0 0 0.382683 -0.92388 1 0 0 +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_dependencies.xml Wed Dec 09 11:07:55 2015 -0500 @@ -0,0 +1,6 @@ +<?xml version="1.0"?> +<tool_dependency> + <package name="icqsol" version="1.0"> + <repository changeset_revision="789f4e7df861" name="package_icqsol_1_0" owner="greg" toolshed="https://testtoolshed.g2.bx.psu.edu" /> + </package> +</tool_dependency>