GCC Code Coverage Report


Directory: ./
File: src/gvtools.cpp
Date: 2024-12-20 15:53:58
Exec Total Coverage
Lines: 0 50 0.0%
Branches: 0 198 0.0%

Line Branch Exec Source
1 // Copyright (c) 2019, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of gepetto-viewer.
5 // gepetto-viewer is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // gepetto-viewer is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // gepetto-viewer. If not, see <http://www.gnu.org/licenses/>.
16
17 #include <osg/ArgumentParser>
18 #include <osg/LOD>
19 #include <osgDB/ReadFile>
20 #include <osgDB/WriteFile>
21 #include <osgUtil/Simplifier>
22
23 int main(int argc, char** argv) {
24 osg::ArgumentParser arguments(&argc, argv);
25 osg::ApplicationUsage* au(arguments.getApplicationUsage());
26 au->setCommandLineUsage(arguments.getApplicationName() + " [options]");
27
28 au->addCommandLineOption("-i <file> or --input <file>", "Read file");
29 au->addCommandLineOption("-s or --simplify",
30 "Add to LOD node a simplified version of the input. "
31 "Value must be ratio,mindist,maxdist");
32 au->addCommandLineOption("-o <file> or --output <file>",
33 "Write current LOD node to file.");
34
35 osg::ApplicationUsage::Type help(arguments.readHelpType());
36
37 std::string inputFilename, outputFilename;
38 std::vector<float> ratios, minDists, maxDists;
39
40 bool usage = (help != osg::ApplicationUsage::NO_HELP);
41 if (!arguments.read("-i", inputFilename) &&
42 !arguments.read("--input", inputFilename)) {
43 usage = true;
44 }
45 if (!arguments.read("-o", outputFilename) &&
46 !arguments.read("--output", outputFilename)) {
47 usage = true;
48 }
49 std::string str;
50 float ratio, minDist, maxDist;
51 while (arguments.read("-s", str) || arguments.read("--simplify", str)) {
52 if (sscanf(str.c_str(), "%f,%f,%f", &ratio, &minDist, &maxDist) != 3) {
53 std::cerr << "Simplifier argument format incorrect." << std::endl;
54 return 1;
55 }
56 ratios.push_back(ratio);
57 minDists.push_back(minDist);
58 maxDists.push_back(maxDist);
59 }
60 if (ratios.size() == 0) usage = true;
61
62 arguments.reportRemainingOptionsAsUnrecognized(osg::ArgumentParser::BENIGN);
63 if (arguments.errors(osg::ArgumentParser::CRITICAL)) {
64 arguments.writeErrorMessages(std::cout);
65 } else if (arguments.errors(osg::ArgumentParser::BENIGN)) {
66 arguments.writeErrorMessages(std::cout);
67 }
68
69 if (usage) {
70 au->write(std::cout, help, 80, true);
71 return 1;
72 }
73
74 osg::ref_ptr<osg::Node> input = osgDB::readNodeFile(inputFilename);
75 osg::ref_ptr<osg::LOD> lod = new osg::LOD;
76 osgUtil::Simplifier simplifier;
77
78 for (std::size_t i = 0; i < ratios.size(); ++i) {
79 std::cout << "Simplifying with " << ratios[i] << ", " << minDists[i] << ", "
80 << maxDists[i] << std::endl;
81
82 ratio = ratios[i];
83 osg::ref_ptr<osg::Node> simplified;
84 if (ratio == 1) {
85 simplified = input;
86 } else {
87 simplifier.setSampleRatio(ratio);
88 simplified =
89 dynamic_cast<osg::Node*>(input->clone(osg::CopyOp::DEEP_COPY_ALL));
90 simplified->accept(simplifier);
91 }
92 lod->addChild(simplified, minDists[i], maxDists[i]);
93 }
94
95 osgDB::writeNodeFile(*lod, outputFilename);
96 return 0;
97 }
98