00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "pfmBba.h"
00016 #include "config_pfm.h"
00017 #include "pfmFile.h"
00018 #include "pystub.h"
00019
00020
00021
00022
00023
00024
00025 PfmBba::
00026 PfmBba() {
00027 set_program_description
00028 ("pfm-bba generates a .bba file from a .pfm file that lists the "
00029 "planar bounding volume of the pfm's internal data.");
00030
00031 add_option
00032 ("z", "", 0,
00033 "Treats (0,0,0) in the pfm file as a special don't-touch value.",
00034 &PfmBba::dispatch_none, &_got_zero_special);
00035
00036 add_option
00037 ("r", "index", 0,
00038 "Selects a reorder index.",
00039 &PfmBba::dispatch_int, NULL, &_reorder_index);
00040
00041 add_option
00042 ("o", "filename", 50,
00043 "Specify the filename to which the resulting bba file will be written.",
00044 &PfmBba::dispatch_filename, &_got_output_filename, &_output_filename);
00045
00046 _reorder_index = 0;
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 void PfmBba::
00056 run() {
00057 Filenames::const_iterator fi;
00058 for (fi = _input_filenames.begin(); fi != _input_filenames.end(); ++fi) {
00059 PfmFile file;
00060 if (!file.read(*fi)) {
00061 nout << "Cannot read " << *fi << "\n";
00062 exit(1);
00063 }
00064 if (!process_pfm(*fi, file)) {
00065 exit(1);
00066 }
00067 }
00068 }
00069
00070
00071
00072
00073
00074
00075 bool PfmBba::
00076 process_pfm(const Filename &input_filename, PfmFile &file) {
00077 file.set_zero_special(_got_zero_special);
00078
00079 Filename bba_filename;
00080 if (_got_output_filename) {
00081 bba_filename = _output_filename;
00082 } else {
00083 bba_filename = input_filename;
00084 bba_filename.set_extension("bba");
00085 }
00086
00087 if (!bba_filename.empty()) {
00088 bba_filename.set_text();
00089 PT(BoundingHexahedron) bounds = file.compute_planar_bounds(pfm_bba_dist[0], pfm_bba_dist[1]);
00090 nassertr(bounds != (BoundingHexahedron *)NULL, false);
00091
00092 pofstream out;
00093 if (!bba_filename.open_write(out)) {
00094 pfm_cat.error()
00095 << "Unable to open " << bba_filename << "\n";
00096 return false;
00097 }
00098
00099
00100 static const int num_reorder_points = 4;
00101 static const int reorder_points[num_reorder_points][8] = {
00102 { 0, 1, 2, 3, 4, 5, 6, 7 },
00103 { 7, 5, 1, 3, 6, 4, 0, 2 },
00104 { 4, 6, 2, 0, 5, 7, 3, 1 },
00105 { 7, 5, 1, 3, 2, 0, 4, 6 },
00106 };
00107 int ri = max(_reorder_index, 0);
00108 ri = min(ri, num_reorder_points - 1);
00109
00110 for (int i = 0; i < bounds->get_num_points(); ++i) {
00111 LPoint3 p = bounds->get_point(reorder_points[ri][i]);
00112 out << p[0] << "," << p[1] << "," << p[2] << "\n";
00113 }
00114 }
00115
00116 return true;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 bool PfmBba::
00128 handle_args(ProgramBase::Args &args) {
00129 if (args.empty()) {
00130 nout << "You must specify the pfm file(s) to read on the command line.\n";
00131 return false;
00132 }
00133
00134 if (args.size() > 1 && _got_output_filename) {
00135 nout << "Cannot use -o when multiple pfm files are specified.\n";
00136 return false;
00137 }
00138
00139 Args::const_iterator ai;
00140 for (ai = args.begin(); ai != args.end(); ++ai) {
00141 _input_filenames.push_back(Filename::from_os_specific(*ai));
00142 }
00143
00144 return true;
00145 }
00146
00147
00148 int main(int argc, char *argv[]) {
00149
00150 pystub();
00151
00152 PfmBba prog;
00153 prog.parse_command_line(argc, argv);
00154 prog.run();
00155 return 0;
00156 }