Cleaver Tetrahedral Meshing  2.2.1
Cleaving algorithm for high quality tetrahedral meshing
All Classes Pages
main.cpp
1 
2 
3 /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
4 /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
5 //
6 // Cleaver - A MultiMaterial Conforming Tetrahedral Meshing Library
7 // - Command Line Program
8 //
9 // Primary Author: Jonathan Bronson (bronson@sci.utah.edu)
10 //
11 /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
12 /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
13 //
14 //-------------------------------------------------------------------
15 //
16 // Copyright (C) 2014, Jonathan Bronson
17 // Scientific Computing & Imaging Institute
18 // University of Utah
19 //
20 // Permission is hereby granted, free of charge, to any person
21 // obtaining a copy of this software and associated documentation
22 // files ( the "Software" ), to deal in the Software without
23 // restriction, including without limitation the rights to use,
24 // copy, modify, merge, publish, distribute, sublicense, and/or
25 // sell copies of the Software, and to permit persons to whom the
26 // Software is furnished to do so, subject to the following
27 // conditions:
28 //
29 // The above copyright notice and this permission notice shall
30 // be included in all copies or substantial portions of the
31 // Software.
32 //
33 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
34 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
35 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
37 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
39 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
40 // USE OR OTHER DEALINGS IN THE SOFTWARE.
41 //-------------------------------------------------------------------
42 //
43 /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
44 #include <Cleaver/Cleaver.h>
45 #include <Cleaver/InverseField.h>
46 #include <Cleaver/SizingFieldCreator.h>
47 #include <NRRDTools.h>
48 
49 #include <boost/program_options.hpp>
50 
51 // STL Includes
52 #include <exception>
53 #include <iostream>
54 #include <vector>
55 #include <sstream>
56 #include <fstream>
57 #include <string>
58 #include <ctime>
59 
60 const std::string kDefaultOutputName = "sizingfield";
61 
62 const double kDefaultScale = 2.0;
63 const double kDefaultLipschitz = 0.2;
64 const double kDefaultMultiplier = 1.0;
65 const int kDefaultPadding = 0;
66 
67 const std::string kDefaultScaleString = "2.0";
68 const std::string kDefaultLipschitzString = "0.2";
69 const std::string kDefaultMultiplierString = "1.0";
70 
71 namespace po = boost::program_options;
72 
73 // Entry Point
74 int main(int argc, char* argv[])
75 {
76  bool verbose = false;
77  std::vector<std::string> material_fields;
78  std::string output_path = kDefaultOutputName;
79  double scale = kDefaultScale;
80  double lipschitz = kDefaultLipschitz;
81  double multiplier = kDefaultMultiplier;
82  int padding = kDefaultPadding;
83 
84  //-------------------------------
85  // Parse Command Line Params
86  //-------------------------------
87  try{
88  po::options_description description("Command line flags");
89  description.add_options()
90  ("help,h", "display help message")
91  ("verbose,v", "enable verbose output")
92  ("version", "display version information")
93  ("material_fields", po::value<std::vector<std::string> >()->multitoken(), "material field paths")
94  ("grading", po::value<double>(&lipschitz)->default_value(kDefaultLipschitz, kDefaultLipschitzString), "sizing field grading")
95  ("multiplier", po::value<double>(&multiplier)->default_value(kDefaultMultiplier), "sizing field multiplier")
96  ("scale", po::value<double>(&scale)->default_value(kDefaultScale), "sizing field scale")
97  ("output", po::value<std::string>()->default_value(kDefaultOutputName, "sizingfield"), "output path")
98  ("padding", po::value<int>()->default_value(kDefaultPadding), "padding")
99  ;
100 
101  boost::program_options::variables_map variables_map;
102  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), variables_map);
103  boost::program_options::notify(variables_map);
104 
105  // print version info
106  if (variables_map.count("version")) {
107  std::cout << cleaver::Version << std::endl;
108  return 0;
109  }
110  // print help
111  else if (variables_map.count("help") || (argc ==1)) {
112  std::cout << description << std::endl;
113  return 0;
114  }
115 
116  // enable verbose mode
117  if (variables_map.count("verbose")) {
118  verbose = true;
119  }
120 
121  // parse the material field input file names
122  if (variables_map.count("material_fields")) {
123  material_fields = variables_map["material_fields"].as<std::vector<std::string> >();
124  int file_count = material_fields.size();
125  }
126  else{
127  std::cout << "Error: At least one material field file must be specified." << std::endl;
128  return 0;
129  }
130 
131  // set output path
132  if (variables_map.count("output")) {
133  output_path = variables_map["output"].as<std::string>();
134  }
135  }
136  catch (std::exception& e) {
137  std::cerr << "Error: " << e.what() << std::endl;
138  return 0;
139  }
140  catch(...) {
141  std::cerr << "Unhandled exception caught. Terminating." << std::endl;
142  return 0;
143  }
144 
145  //-----------------------------------
146  // Load Data & Construct Volume
147  //-----------------------------------
148  std::cout << " Loading input fields:" << std::endl;
149  for (size_t i=0; i < material_fields.size(); i++) {
150  std::cout << " - " << material_fields[i] << std::endl;
151  }
152 
153  std::vector<cleaver::AbstractScalarField*> fields = NRRDTools::loadNRRDFiles(material_fields);
154  if(fields.empty()){
155  std::cerr << "Failed to load image data. Terminating." << std::endl;
156  return 0;
157  }
158  else if(fields.size() == 1) {
159  fields.push_back(new cleaver::InverseScalarField(fields[0]));
160  }
161 
162  cleaver::Volume *volume = new cleaver::Volume(fields);
163 
164  //------------------------------------------------------------
165  // Construct Sizing Field
166  //------------------------------------------------------------
167  cleaver::FloatField *sizingField =
168  cleaver::SizingFieldCreator::createSizingFieldFromVolume(
169  volume,
170  (float)(1.0/lipschitz),
171  (float)scale,
172  (float)multiplier,
173  (int)padding,
174  false);
175 
176  //------------------------------------------------------------
177  // Write Field to File
178  //------------------------------------------------------------
179  std::cout << " Writing sizing field to file: " << output_path << ".nrrd" << std::endl; // todo(jrb) strip path
180  NRRDTools::saveNRRDFile(sizingField, output_path);
181 
182 
183  // done
184  std::cout << " Done." << std::endl;
185  return 0;
186 }
187