1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include "config.h"
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <memory>
#include <tr1/memory>
#ifdef PSURFACE_STANDALONE
#include "TargetSurface.h"
#else
#include "hxsurface/Surface.h"
#endif
#include "AmiraMeshIO.h"
#include "PSurface.h"
#include "Hdf5IO.h"
#include "GmshIO.h"
#include "VtkIO.h"
#if defined HAVE_AMIRAMESH || !defined PSURFACE_STANDALONE
#include <amiramesh/AmiraMesh.h>
#endif
/** \brief List of the file types that we support */
enum FileTypes
{
AMIRA,
HDF5,
VTU,
GMSH
};
using namespace psurface;
int main(int argc, char **argv)
{
if (argc < 4) {
fprintf(stderr, "Usage: psurface_convert -i inputname -o outputname (-t type) \n");
fprintf(stderr, "Input file type could be amiramesh(*.am) , hdf5(*.h5) or gmsh(*.msh).\n");
fprintf(stderr, "Output file type could be amiramesh(*.am) , hdf5(*.h5) or vtu(*.vtu).\n");
fprintf(stderr, "type could be b(basegrid) or r(readable hdf5 file).\n -t b means that the output should only have base grid trianlge(This option is used when the output type is vtu type.\n -t r means that we get readable output hdf5 type data(This option is used when the output type is hdf5).\n");
exit(0);
}
//use get opt to deal with the argv
char *input, *output, *type;
bool basegrid = 0, basehdf5 = 1;
int opt=0;
int i=0;
const char* optstring=":i:o:t:";
const int num=3;
while((opt=getopt(argc,argv,optstring)) != -1)
{
switch(opt)
{
case 'i':
input = optarg;
break;
case 'o':
output = optarg;
break;
case 't':
type = optarg;
break;
case ':':
printf("the option needs a value\n");
break;
case '?':
printf("unknown option '%c'\n",optopt);
break;
}
}
for(i=0;optind<argc;i++,optind++)
{
if(i<num)
printf("argument:%s/n",argv[optind]);
else
printf("excess argument:%s/n",argv[optind]);
}
FileTypes inputType, outputType;
if(strstr(input,".am") != NULL || strstr(input,".par") != NULL )
inputType = AMIRA;
else if(strstr(input,".h5") != NULL)
inputType = HDF5;
else if(strstr(input,".vtu") != NULL)
inputType = VTU;
else if(strstr(input,".msh") != NULL)
inputType = GMSH;
else
printf(" could not tell the input type by file extension\n");
if(strstr(output,".am") != NULL)
outputType = AMIRA;
else if(strstr(output,".h5") != NULL)
{
outputType = HDF5;
if( type != NULL && *type == 'r') basehdf5 = 0;
}
else if(strstr(output,".vtu") != NULL)
{
outputType = VTU;
if( type != NULL && *type == 'b') basegrid = 1;
}
else if(strstr(output,".msh") != NULL)
outputType = GMSH;
else
printf(" could not tell the output type by file extension\n");
PSurface<2,float>* par;
switch(inputType)
{
case HDF5:
{
#if HAVE_HDF5
par = Hdf5IO<float,2>::read(input);
#else
std::cerr << "You have given an hdf5 input file, but psurface-convert" << std::endl;
std::cerr << "has been compiled without hdf5 support!" << std::endl;
exit(1);
#endif
}
break;
case GMSH:
{
par = GmshIO<float,2>::readGmsh(input);
}
break;
case AMIRA:
{
#if HAVE_AMIRAMESH
AmiraMesh* am = AmiraMesh::read(input);
AmiraMeshIO<float> amIO;
Surface* surf = new Surface;
par = new PSurface<2,float>;
if( !amIO.initFromAmiraMesh(par,am,input, surf))
{
printf("unable to initiate psurface from amira mesh file!\n");
return 0;
}
#else
std::cerr << "You have given an AmiraMesh input file, but psurface-convert" << std::endl;
std::cerr << "has been compiled without AmiraMesh support!" << std::endl;
exit(1);
#endif
}
break;
default:
{
// throw(std::runtime_error("unkown input type\n"));
return 0;
}
};
std::string str(output);
switch(outputType)
{
case HDF5:
{
#if HAVE_HDF5
std::string xdmffile(output);
xdmffile.erase (xdmffile.end() - 3, xdmffile.end());
xdmffile.append(".xdmf");
Hdf5IO<float,2>* pn = new Hdf5IO<float,2>(par);
pn->createHdfAndXdmf(xdmffile, output,basehdf5);
#else
std::cerr << "You have given an hdf5 output file, but psurface-convert" << std::endl;
std::cerr << "has been compiled without hdf5 support!" << std::endl;
exit(1);
#endif
}
break;
case VTU:
{
VTKIO<float,2> pn(par);
pn.createVTU(str.c_str(), basegrid ? "" : (str.substr(0, str.length()-std::string(".vtu").length())) + "-graph.vtu");
}
break;
case AMIRA:
{
#if HAVE_AMIRAMESH
PSURFACE_API AmiraMeshIO<float> amIO;
amIO.writeAmiraMesh(par, output);
#else
std::cerr << "You have given an AmiraMesh output file, but psurface-convert" << std::endl;
std::cerr << "has been compiled without AmiraMesh support!" << std::endl;
exit(1);
#endif
}
break;
default:
printf("unknown output type\n");
// throw(std::runtime_error("unkown output type\n"));
};
return 0;
}
|