30{
31
32 std::ifstream in(fname.c_str());
33 if (!in)
34 {
35 WARN(
"readTIN(): could not open stream from {:s}.", fname);
36 if (errors)
37 {
38 errors->push_back("readTINFile error opening stream from " + fname);
39 }
40 return nullptr;
41 }
42
44 std::size_t id;
48 std::string line;
49 while (std::getline(in, line).good())
50 {
51
52 if (line.empty())
53 {
54 continue;
55 }
56
57
58 std::stringstream input(line);
59
60 if (!(input >> id))
61 {
62 in.close();
63 delete sfc;
64 return nullptr;
65 }
66
67 if (!(input >> p0[0] >> p0[1] >> p0[2]))
68 {
69 ERR(
"Could not read coords of 1st point of triangle {:d}.",
id);
70 if (errors)
71 {
72 errors->push_back(
73 std::string("readTIN error: ") +
74 std::string(
75 "Could not read coords of 1st point in triangle ") +
76 std::to_string(id));
77 }
78 in.close();
79 delete sfc;
80 return nullptr;
81 }
82
83 if (!(input >> p1[0] >> p1[1] >> p1[2]))
84 {
85 ERR(
"Could not read coords of 2nd point of triangle {:d}.",
id);
86 if (errors)
87 {
88 errors->push_back(
89 std::string("readTIN error: ") +
90 std::string(
91 "Could not read coords of 2nd point in triangle ") +
92 std::to_string(id));
93 }
94 in.close();
95 delete sfc;
96 return nullptr;
97 }
98
99 if (!(input >> p2[0] >> p2[1] >> p2[2]))
100 {
101 ERR(
"Could not read coords of 3rd point of triangle {:d}.",
id);
102 if (errors)
103 {
104 errors->push_back(
105 std::string("readTIN error: ") +
106 std::string(
107 "Could not read coords of 3rd point in triangle ") +
108 std::to_string(id));
109 }
110 in.close();
111 delete sfc;
112 return nullptr;
113 }
114
115
116 double const d_eps(std::numeric_limits<double>::epsilon());
118 {
119 ERR(
"readTIN: Triangle {:d} has zero area.",
id);
120 if (errors)
121 {
122 errors->push_back(std::string("readTIN: Triangle ") +
123 std::to_string(id) +
124 std::string(" has zero area."));
125 }
126 delete sfc;
127 return nullptr;
128 }
129
130
131 std::size_t
const s(pnt_vec.
getVector().size());
132
133 std::size_t const pnt_pos_0(
135 std::size_t const pnt_pos_1(
137 std::size_t const pnt_pos_2(
139
140 if (pnt_pos_0 != std::numeric_limits<std::size_t>::max() &&
141 pnt_pos_1 != std::numeric_limits<std::size_t>::max() &&
142 pnt_pos_2 != std::numeric_limits<std::size_t>::max())
143 {
144 sfc->addTriangle(pnt_pos_0, pnt_pos_1, pnt_pos_2);
145 }
146 }
147
148 if (sfc->getNumberOfTriangles() == 0)
149 {
150 WARN(
"readTIN(): No triangle found.", fname);
151 if (errors)
152 {
153 errors->push_back("readTIN error because of no triangle found");
154 }
155 delete sfc;
156 return nullptr;
157 }
158
159 return sfc;
160}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
std::size_t push_back(Point *pnt)
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
std::vector< T * > const & getVector() const
double calcTriangleArea(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &c)