File: main.cpp

package info (click to toggle)
sfcgal 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,348 kB
  • sloc: cpp: 44,959; sh: 198; makefile: 33; ansic: 24; python: 23
file content (107 lines) | stat: -rw-r--r-- 2,566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2012-2013, IGN France.
// Copyright (c) 2012-2024, Oslandia.
// Copyright (c) 2024-2025, SFCGAL team.
// SPDX-License-Identifier: LGPL-2.0-or-later

#include <fstream>
#include <iostream>
#include <sstream>

#include <boost/lexical_cast.hpp>

#include "SFCGAL/Exception.h"
#include "SFCGAL/GeometryCollection.h"
#include "SFCGAL/detail/generator/building.h"
#include "SFCGAL/io/wkt.h"

// optional osg support for outputs
#include "SFCGAL/io/osg.h"

using namespace SFCGAL;

class InputData {
public:
  InputData(const std::string &wkt_ = "", double wallHeight_ = 5.0,
            double roofSlope_ = 1.0)
      : wkt(wkt_), roofSlope(roofSlope_), wallHeight(wallHeight_)
  {
  }

public:
  std::string wkt;
  double      roofSlope;
  double      wallHeight;
};

/**
 * @brief read input file
 */
std::vector<InputData>
readInputFile(const std::string &filename)
{
  std::vector<InputData> result;

  std::ifstream ifs(filename.c_str());
  if (!ifs.good()) {
    BOOST_THROW_EXCEPTION(Exception("can't open file"));
    return result;
  }

  std::string line;
  while (std::getline(ifs, line)) {
    std::istringstream iss(line);

    std::string wkt;
    std::string wallHeight = "10.0";
    std::string roofSlope  = "1.0";

    std::getline(iss, wkt, '|') && std::getline(iss, wallHeight, '|') &&
        std::getline(iss, roofSlope, '|');
    if (!wkt.empty()) {
      result.push_back(InputData(wkt, boost::lexical_cast<double>(wallHeight),
                                 boost::lexical_cast<double>(roofSlope)));
    }
  }
  ifs.close();
  return result;
}

void
processFile(const std::string &filename)
{
  std::vector<InputData> data = readInputFile(filename);

  std::unique_ptr<GeometryCollection> result(new GeometryCollection());

  std::cout << "#" << data.size() << std::endl;
  for (size_t i = 0; i < data.size(); i++) {
    std::unique_ptr<Geometry> g(io::readWkt(data[i].wkt));
    std::unique_ptr<Geometry> building(
        generator::building(*g, data[i].wallHeight, data[i].roofSlope));
    result->addGeometry(building.release());
  }

  io::osgWriteFile(*result, "building.osg");
}

int
main(int argc, char *argv[])
{
  if (argc < 2) {
    std::cerr << "usage : SFCGAL-building <input-file>" << std::endl;
    std::cerr << "file format : " << std::endl;
    std::cerr << "<wkt(polygon|multipolygon)|wallHeight>|<roofSlope>"
              << std::endl;
    return 0;
  }

  try {
    processFile(argv[1]);
    return 0;
  } catch (Exception &e) {
    std::cerr << e.diagnostic() << std::endl;
    return 1;
  }

  return 0;
}