GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/gvtools.cpp Lines: 0 53 0.0 %
Date: 2020-05-14 11:23:33 Branches: 0 202 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
{
25
  osg::ArgumentParser arguments(&argc, argv);
26
  osg::ApplicationUsage* au (arguments.getApplicationUsage());
27
  au->setCommandLineUsage(arguments.getApplicationName()+" [options]");
28
29
  au->addCommandLineOption("-i <file> or --input <file>", "Read file");
30
  au->addCommandLineOption("-s or --simplify", "Add to LOD node a simplified version of the input. Value must be ratio,mindist,maxdist");
31
  au->addCommandLineOption("-o <file> or --output <file>", "Write current LOD node to file.");
32
33
  osg::ApplicationUsage::Type help (arguments.readHelpType());
34
35
  std::string inputFilename, outputFilename;
36
  std::vector<float> ratios, minDists, maxDists;
37
38
  bool usage = (help != osg::ApplicationUsage::NO_HELP);
39
  if (!arguments.read("-i", inputFilename)
40
      && !arguments.read("--input", inputFilename)) {
41
    usage = true;
42
  }
43
  if (!arguments.read("-o", outputFilename)
44
      && !arguments.read("--output", outputFilename)) {
45
    usage = true;
46
  }
47
  std::string str;
48
  float ratio, minDist, maxDist;
49
  while (arguments.read("-s", str)
50
      || arguments.read("--simplify", str)) {
51
    if( sscanf( str.c_str(), "%f,%f,%f",
52
          &ratio, &minDist, &maxDist ) != 3 )
53
    {
54
      std::cerr << "Simplifier argument format incorrect." << std::endl;
55
      return 1;
56
    }
57
    ratios  .push_back(ratio  );
58
    minDists.push_back(minDist);
59
    maxDists.push_back(maxDist);
60
  }
61
  if (ratios.size() == 0) usage = true;
62
63
  arguments.reportRemainingOptionsAsUnrecognized(osg::ArgumentParser::BENIGN);
64
  if (arguments.errors(osg::ArgumentParser::CRITICAL)) {
65
    arguments.writeErrorMessages(std::cout);
66
  } else if (arguments.errors(osg::ArgumentParser::BENIGN)) {
67
    arguments.writeErrorMessages(std::cout);
68
  }
69
70
  if (usage)  {
71
    au->write(std::cout, help, 80, true);
72
    return 1;
73
  }
74
75
  osg::ref_ptr<osg::Node> input = osgDB::readNodeFile(inputFilename);
76
  osg::ref_ptr<osg::LOD> lod = new osg::LOD;
77
  osgUtil::Simplifier simplifier;
78
79
  for (std::size_t i = 0; i < ratios.size(); ++i) {
80
    std::cout << "Simplifying with " << ratios[i]
81
      << ", " << minDists[i]
82
      << ", " << maxDists[i] << std::endl;
83
84
    ratio = ratios[i];
85
    osg::ref_ptr<osg::Node> simplified;
86
    if (ratio == 1) {
87
      simplified = input;
88
    } else {
89
      simplifier.setSampleRatio(ratio);
90
      simplified = dynamic_cast<osg::Node*>(input->clone(osg::CopyOp::DEEP_COPY_ALL));
91
      simplified->accept (simplifier);
92
    }
93
    lod->addChild (simplified, minDists[i], maxDists[i]);
94
  }
95
96
  osgDB::writeNodeFile (*lod, outputFilename);
97
  return 0;
98
}