OGS
TINInterface.cpp
Go to the documentation of this file.
1
9#include "TINInterface.h"
10
11#include <fstream>
12#include <limits>
13
14#include "BaseLib/FileTools.h"
15#include "BaseLib/Logging.h"
16#include "BaseLib/StringTools.h"
18#include "GeoLib/Surface.h"
19#include "GeoLib/Triangle.h"
21
22namespace GeoLib
23{
24namespace IO
25{
26GeoLib::Surface* TINInterface::readTIN(std::string const& fname,
27 GeoLib::PointVec& pnt_vec,
28 std::vector<std::string>* errors)
29{
30 // open file
31 std::ifstream in(fname.c_str());
32 if (!in)
33 {
34 WARN("readTIN(): could not open stream from {:s}.", fname);
35 if (errors)
36 {
37 errors->push_back("readTINFile error opening stream from " + fname);
38 }
39 return nullptr;
40 }
41
42 auto* sfc = new GeoLib::Surface(pnt_vec.getVector());
43 std::size_t id;
47 std::string line;
48 while (std::getline(in, line).good())
49 {
50 // allow empty lines
51 if (line.empty())
52 {
53 continue;
54 }
55
56 // parse line
57 std::stringstream input(line);
58 // read id
59 if (!(input >> id))
60 {
61 in.close();
62 delete sfc;
63 return nullptr;
64 }
65 // read first point
66 if (!(input >> p0[0] >> p0[1] >> p0[2]))
67 {
68 ERR("Could not read coords of 1st point of triangle {:d}.", id);
69 if (errors)
70 {
71 errors->push_back(
72 std::string("readTIN error: ") +
73 std::string(
74 "Could not read coords of 1st point in triangle ") +
75 std::to_string(id));
76 }
77 in.close();
78 delete sfc;
79 return nullptr;
80 }
81 // read second point
82 if (!(input >> p1[0] >> p1[1] >> p1[2]))
83 {
84 ERR("Could not read coords of 2nd point of triangle {:d}.", id);
85 if (errors)
86 {
87 errors->push_back(
88 std::string("readTIN error: ") +
89 std::string(
90 "Could not read coords of 2nd point in triangle ") +
91 std::to_string(id));
92 }
93 in.close();
94 delete sfc;
95 return nullptr;
96 }
97 // read third point
98 if (!(input >> p2[0] >> p2[1] >> p2[2]))
99 {
100 ERR("Could not read coords of 3rd point of triangle {:d}.", id);
101 if (errors)
102 {
103 errors->push_back(
104 std::string("readTIN error: ") +
105 std::string(
106 "Could not read coords of 3rd point in triangle ") +
107 std::to_string(id));
108 }
109 in.close();
110 delete sfc;
111 return nullptr;
112 }
113
114 // check area of triangle
115 double const d_eps(std::numeric_limits<double>::epsilon());
116 if (MathLib::calcTriangleArea(p0, p1, p2) < d_eps)
117 {
118 ERR("readTIN: Triangle {:d} has zero area.", id);
119 if (errors)
120 {
121 errors->push_back(std::string("readTIN: Triangle ") +
122 std::to_string(id) +
123 std::string(" has zero area."));
124 }
125 delete sfc;
126 return nullptr;
127 }
128
129 // determine size pnt_vec to insert the correct ids
130 std::size_t const s(pnt_vec.getVector().size());
131
132 std::size_t const pnt_pos_0(
133 pnt_vec.push_back(new GeoLib::Point(p0, s)));
134 std::size_t const pnt_pos_1(
135 pnt_vec.push_back(new GeoLib::Point(p1, s + 1)));
136 std::size_t const pnt_pos_2(
137 pnt_vec.push_back(new GeoLib::Point(p2, s + 2)));
138 // create new Triangle
139 if (pnt_pos_0 != std::numeric_limits<std::size_t>::max() &&
140 pnt_pos_1 != std::numeric_limits<std::size_t>::max() &&
141 pnt_pos_2 != std::numeric_limits<std::size_t>::max())
142 {
143 sfc->addTriangle(pnt_pos_0, pnt_pos_1, pnt_pos_2);
144 }
145 }
146
147 if (sfc->getNumberOfTriangles() == 0)
148 {
149 WARN("readTIN(): No triangle found.", fname);
150 if (errors)
151 {
152 errors->push_back("readTIN error because of no triangle found");
153 }
154 delete sfc;
155 return nullptr;
156 }
157
158 return sfc;
159}
160
162 std::string const& file_name)
163{
164 std::ofstream os(file_name.c_str());
165 if (!os)
166 {
167 WARN("writeSurfaceAsTIN(): could not open stream to {:s}.", file_name);
168 return;
169 }
170 os.precision(std::numeric_limits<double>::digits10);
171 const std::size_t n_tris(surface.getNumberOfTriangles());
172 for (std::size_t l(0); l < n_tris; l++)
173 {
174 GeoLib::Triangle const& tri(*(surface[l]));
175 os << l << " " << *(tri.getPoint(0)) << " " << *(tri.getPoint(1)) << " "
176 << *(tri.getPoint(2)) << "\n";
177 }
178 os.close();
179}
180} // end namespace IO
181} // end namespace GeoLib
Definition of analytical geometry functions.
Filename manipulation routines.
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:44
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:39
Definition of string helper functions.
static GeoLib::Surface * readTIN(std::string const &fname, GeoLib::PointVec &pnt_vec, std::vector< std::string > *errors=nullptr)
static void writeSurfaceAsTIN(GeoLib::Surface const &surface, std::string const &file_name)
This class manages pointers to Points in a std::vector along with a name. It also handles the deletio...
Definition: PointVec.h:38
std::size_t push_back(Point *pnt)
Definition: PointVec.cpp:133
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
Definition: Surface.h:34
std::size_t getNumberOfTriangles() const
Definition: Surface.cpp:82
std::vector< T * > const & getVector() const
Definition: TemplateVec.h:109
Class Triangle consists of a reference to a point vector and a vector that stores the indices in the ...
Definition: Triangle.h:26
const Point * getPoint(std::size_t i) const
const access operator to access the i-th triangle Point
Definition: Triangle.h:49
double calcTriangleArea(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &c)
static const double s