Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

clparser.cpp

Go to the documentation of this file.
00001 /*
00002     toxic - A Global Illumination Renderer
00003     Copyright (C) 2003-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of toxic.
00007 
00008     toxic is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     toxic is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with toxic; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "clparser.h"   // include first
00024 #include "common/misc/stringutils.h"
00025 #include "driversettings.h"
00026 #include "error.h"
00027 
00028 #include <cassert>
00029 #include <cstdlib>  // std::exit(), EXIT_FAILURE, EXIT_SUCCESS
00030 #include <iostream>
00031 
00032 using namespace sheep;
00033 using namespace std;
00034 using namespace toxic;
00035 
00036 CLParser::CLParser(DriverSettings *driver_settings) :
00037     m_driver_settings(driver_settings)
00038 {
00039     assert(m_driver_settings);
00040 
00041     EnableFilenameExtraction(1, &CLParser::filename_handler);
00042 
00043     // -----------------------------------------------------------------------
00044     //  Primary output settings.
00045     // -----------------------------------------------------------------------
00046 
00047     RegisterOption("--output", "-o", &CLParser::opt_output);
00048     RegisterOption("--width", "-w", &CLParser::opt_width);
00049     RegisterOption("--height", "-h", &CLParser::opt_height);
00050 
00051     // -----------------------------------------------------------------------
00052     //  Photon maps settings.
00053     // -----------------------------------------------------------------------
00054 
00055     RegisterOption("--load-gpm", "", &CLParser::opt_load_gpm);
00056     RegisterOption("--save-gpm", "", &CLParser::opt_save_gpm);
00057     RegisterOption("--load-cpm", "", &CLParser::opt_load_cpm);
00058     RegisterOption("--save-cpm", "", &CLParser::opt_save_cpm);
00059 
00060     // -----------------------------------------------------------------------
00061     //  Miscellaneous settings.
00062     // -----------------------------------------------------------------------
00063 
00064     RegisterOption("--driver-settings", "-ds", &CLParser::opt_driver_settings);
00065     RegisterOption("--scene-settings", "-ss", &CLParser::opt_scene_settings);
00066     RegisterOption("--skip-render", "", &CLParser::opt_skip_render);
00067     RegisterOption("--help", "", &CLParser::opt_help);
00068 }
00069 
00070 void CLParser::Parse(int argc, char **argv) {
00071     m_progname = argv[0];
00072 
00073     if(argc < 2) {
00074         print_usage();
00075         exit(EXIT_FAILURE);
00076     }
00077 
00078     try {
00079         CommandLineParser<CLParser>::Parse(argc, argv);
00080         return;
00081     }
00082     catch(const UnknownOptionException &e) {
00083         cerr << Error << "\"" << e.m_option << "\" is not a valid option." << endl;
00084     }
00085     catch(const InvalidArgumentException &e) {
00086         cerr << Error << "\"" << e.m_arg << "\" is not a valid argument for option \""
00087             << e.m_option << "\"." << endl;
00088     }
00089     catch(const MissingArgumentException &e) {
00090         cerr << Error << "Missing argument for option \"" << e.m_option << "\"." << endl;
00091     }
00092     catch(const ParseException &) {
00093         cerr << Error << "An unknown error occured while parsing the command line." << endl;
00094     }
00095 
00096     cerr << "Please run toxic without argument to display a help message." << endl;
00097 
00098     exit(EXIT_FAILURE);
00099 }
00100 
00101 void CLParser::print_usage() const {
00102     cerr << "Usage: " << endl;
00103     cerr << "  " << m_progname << " <filename> [options]" << endl << endl;
00104     cerr << "Examples: " << endl;
00105     cerr << "  " << m_progname << " bunny.xml" << endl;
00106     cerr << "  " << m_progname << " bunny.xml --output bunny.png --width 720 --height 576" << endl;
00107     cerr << "  " << m_progname << " bunny.xml -o bunny.png -w 720 -h 576" << endl << endl;
00108     cerr << "Options: " << endl;
00109     cerr << "  Output settings" << endl;
00110     cerr << "    --output <filename> or -o <filename>: specify output file name." << endl;
00111     cerr << "        By default, output is written to ./out.png" << endl;
00112     cerr << "    --width <value> or -w <value>: specify output width" << endl;
00113     cerr << "    --height <value> or -h <value>: specify output height" << endl;
00114     cerr << "  Global Photon Map (GPM) settings" << endl;
00115     cerr << "    --load-gpm <filename>: read the GPM from disk" << endl;
00116     cerr << "    --save-gpm <filename>: write the GPM to disk" << endl;
00117     cerr << "  Caustics Photon Map (CPM) settings" << endl;
00118     cerr << "    --load-cpm <filename>: read the CPM from disk" << endl;
00119     cerr << "    --save-cpm <filename>: write the CPM to disk" << endl;
00120     cerr << "  Miscellaneous settings" << endl;
00121     cerr << "    --driver-settings <filename> or -ds <filename>: specify driver settings file name." << endl;
00122     cerr << "        By default, driver settings are read from {TOXICHOME}/settings/toxicsettings.xml" << endl;
00123     cerr << "    --scene-settings <filename> or -ss <filename>: specify scene settings file name." << endl;
00124     cerr << "        By default, settings for scene bunny.xml are read from ./bunny.settings.xml" << endl;
00125     cerr << "    --skip-render: skip the rendering part (do not produce an image)" << endl;
00126     cerr << "    --help: display this help message and exit" << endl;
00127 }
00128 
00129 void CLParser::filename_handler(const string &filename) {
00130     m_driver_settings->m_input_files.m_scene_file = filename;
00131     m_driver_settings->m_settings_files.m_scene_settings_file =
00132         StringUtils::RemoveExtension(filename) + ".settings.xml";
00133 }
00134 
00135 void CLParser::opt_output(const string &filename) {
00136     m_driver_settings->m_primary_output.m_file = filename;
00137 }
00138 
00139 void CLParser::opt_width(int width) {
00140     if(width < 1) {
00141         throw InvalidArgumentException(
00142             GetCurrentOptionName(),
00143             StringUtils::ToString(width)
00144         );
00145     }
00146 
00147     m_driver_settings->m_primary_output.m_width = width;
00148 }
00149 
00150 void CLParser::opt_height(int height) {
00151     if(height < 1) {
00152         throw InvalidArgumentException(
00153             GetCurrentOptionName(),
00154             StringUtils::ToString(height)
00155         );
00156     }
00157 
00158     m_driver_settings->m_primary_output.m_height = height;
00159 }
00160 
00161 void CLParser::opt_save_gpm(const string &filename) {
00162     m_driver_settings->m_global_photon_map.m_op =
00163         DriverSettings::BUILD_AND_SAVE_PM;
00164     m_driver_settings->m_global_photon_map.m_file = filename;
00165 }
00166 
00167 void CLParser::opt_load_gpm(const string &filename) {
00168     m_driver_settings->m_global_photon_map.m_op =
00169         DriverSettings::LOAD_PM;
00170     m_driver_settings->m_global_photon_map.m_file = filename;
00171 }
00172 
00173 void CLParser::opt_save_cpm(const string &filename) {
00174     m_driver_settings->m_caustics_photon_map.m_op =
00175         DriverSettings::BUILD_AND_SAVE_PM;
00176     m_driver_settings->m_caustics_photon_map.m_file = filename;
00177 }
00178 
00179 void CLParser::opt_load_cpm(const string &filename) {
00180     m_driver_settings->m_caustics_photon_map.m_op =
00181         DriverSettings::LOAD_PM;
00182     m_driver_settings->m_caustics_photon_map.m_file = filename;
00183 }
00184 
00185 void CLParser::opt_driver_settings(const string &filename) {
00186     m_driver_settings->m_settings_files.m_driver_settings_file = filename;
00187 }
00188 
00189 void CLParser::opt_scene_settings(const string &filename) {
00190     m_driver_settings->m_settings_files.m_scene_settings_file = filename;
00191 }
00192 
00193 void CLParser::opt_skip_render() {
00194     m_driver_settings->m_primary_output.m_enabled = false;
00195 }
00196 
00197 void CLParser::opt_help() {
00198     print_usage();
00199     exit(EXIT_SUCCESS);
00200 }

Generated on Tue May 11 01:31:49 2004 for toxic by doxygen 1.3.6