-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhmap2obj.cpp
400 lines (330 loc) · 10.9 KB
/
hmap2obj.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* @file hmap2obj.cpp
* @brief Convert heightmap/displacement file to Wavefront's OBJ.
* @internal
*
* Copyright(c) 2017 by [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @endinternal
*
* @details
* Add detailed description of file
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <limits>
#include <array>
#include <algorithm>
#include <numeric>
#include <exception>
#include <utility>
/**
* Application of converting Wavefront's OBJ file into binary 2d heightfield file.
*
* This class can parse command line arguments, validate them, and report a help message if needed.
*/
class hmap2obj
{
public:
/// 32-bit unsigned 2d vector
typedef std::array<std::uint32_t, 2> uvec2;
/// Single float based 3d vector
typedef std::array<float, 3> vec3;
/// Double precision float based 3d vector (prioritize bin->obj->bin iterations)
typedef std::array<double, 3> dvec3;
/// Describes the application parameters
struct param_type
{
std::string hmap; ///< Input *.* heightmap binary file to read from
std::string obj; ///< Output *.obj file to write to
uvec2 hmap_size; ///< How big is the integer grid of the input heighmap file
dvec3 obj_blo; ///< The lowest corner of the obj bounding box
dvec3 obj_bhi; ///< The highest corner of the obj bounding box
bool absolute; ///< Whether height values shall occupy the whole input grid range
};
//
static param_type parse_cli (std::vector<std::string> const& args);
//
static std::string validate_params (param_type const& params);
/// Just inits the app parameters.
hmap2obj (param_type const& p) : params (p) {};
/// Empty dtor
~ hmap2obj () {};
//
void read_hmap ();
/// Peek how small is the minimum elevation in the read heightmap
auto hmap_min () const {
return vmin;
}
/// Peek how big is the maximum elevation in the read heightmap
auto hmap_max () const {
return vmax;
}
//
void make_xyz ();
//
void dump_obj ();
private:
param_type params; ///< The input to the app
std::vector<dvec3> xyz; ///< The point cloud data coming from the obj file
std::vector<dvec3::value_type> grid;///< The imported height values in XY order
uvec2::value_type vmin, vmax; ///< The min/max elevation data of the imported heightmap
};
//--------------------------------------------------------------------------------------------------
/**
* Create application parameters out of the C++ main() arguments
*
* The arguments are expected to be in any order, but certain priority.
*
* @param args as reported by main() (e.g. the first one is the exe name path)
*/
hmap2obj::param_type hmap2obj::parse_cli (std::vector<std::string> const& args)
{
using namespace std;
param_type p;
p.absolute = false;
p.hmap_size.fill (0);
p.obj_blo.fill (numeric_limits<decltype(p.obj_blo)::value_type>::quiet_NaN ());
p.obj_bhi.fill (numeric_limits<decltype(p.obj_bhi)::value_type>::quiet_NaN ());
for (auto& arg: args)
{
if (p.hmap.empty ())
{
p.hmap = arg;
continue;
}
if (p.obj.empty ())
{
p.obj = arg;
continue;
}
if (arg == "--absolute")
{
p.absolute = true;
continue;
}
try
{
bool succ = false;
int n = stoi (arg, nullptr, 0);
if (n > 0)
for (auto& d: p.hmap_size) if (d <= 0)
{
d = static_cast<unsigned> (n);
succ = true;
break;
}
if (succ) continue;
}
catch (exception&)
{}
try
{
bool succ = false;
auto x = stod (arg);
for (size_t i = 0, n = p.obj_blo.size (); i < n; ++i)
if (isnan (p.obj_blo[i]))
{
p.obj_blo[i] = x;
succ = true;
break;
}
if (succ) continue;
for (size_t i = 0, n = p.obj_bhi.size (); i < n; ++i)
if (isnan (p.obj_bhi[i]))
{
p.obj_bhi[i] = x;
break;
}
}
catch (exception&)
{}
}
return p;
}
//--------------------------------------------------------------------------------------------------
/**
* Validation of the paramaters (the object does not assumes such).
*
* @param p to check
* @return an human-readable error message if any issue was found
*/
std::string hmap2obj::validate_params (param_type const& p)
{
using namespace std;
if ([&p] () -> bool {
ifstream f;
f.open (p.hmap);
return !f.is_open ();
} ())
return "An input heightmap file was not opened!";
if ([&p] () -> bool {
ofstream f;
f.open (p.obj, ios_base::app);
return !f.is_open ();
} ())
return "An output Wavefront *.obj file was not opened!";
for (auto n: p.hmap_size)
if (n <= 0)
return "The heightmap size parameter is invalid!";
for (auto n: p.obj_blo)
if (!isnormal (n) && n != 0)
return "The obj lowest corner parameter is invalid!";
for (auto n: p.obj_bhi)
if (!isnormal (n) && n != 0)
return "The obj highest corner parameter is invalid!";
for (size_t i = 0, n = p.obj_blo.size (); i < n; ++i)
{
if (p.obj_blo[i] >= p.obj_bhi[i])
return "Obj lowest corner value is greater!";
}
return "";
}
//--------------------------------------------------------------------------------------------------
/**
* Parse and extract up the elevation data from the heightmap file.
*/
void hmap2obj::read_hmap ()
{
using namespace std;
ifstream hmap (params.hmap, ios_base::binary);
grid.clear ();
grid.resize (params.hmap_size[0] * params.hmap_size[1], 0);
vmin = numeric_limits<decltype(vmin)>::max ();
vmax = numeric_limits<decltype(vmax)>::min ();
uint16_t p;
for (size_t i = 0, n = grid.size ();
i < n && hmap.read (reinterpret_cast<char*> (&p), sizeof p); ++i, p = 0)
{
grid[i] = p;
vmin = min<decltype(vmin)> (vmin, p);
vmax = max<decltype(vmin)> (vmax, p);
};
}
//--------------------------------------------------------------------------------------------------
/**
* Convert the elevation data to XYZ point cloud.
*
* For each point we first obtain its percentage location and then remap it to the obj dimension.
*/
void hmap2obj::make_xyz ()
{
using namespace std;
xyz.clear ();
xyz.resize (grid.size (), { 0.f, 0.f, 0.f });
double grid_min = params.absolute ? 0 : vmin;
double grid_max = params.absolute ? 0xFFFF : vmax;
for (size_t i = 0, n = grid.size (); i < n; ++i)
{
dvec3 pt;
pt[0] = double (i % params.hmap_size[0]) / (params.hmap_size[0] - 1);
pt[2] = double (i / params.hmap_size[1]) / (params.hmap_size[1] - 1);
pt[1] = (grid[i] - grid_min) / (grid_max - grid_min);
for (size_t j = params.obj_blo.size (); j--; )
pt[j] = params.obj_blo[j] + pt[j] * (params.obj_bhi[j] - params.obj_blo[j]);
xyz[i] = pt;
}
}
//--------------------------------------------------------------------------------------------------
/**
* Dump the XYZ cloud onto a Wavefront file object.
*/
void hmap2obj::dump_obj ()
{
using namespace std;
ofstream file (params.obj);
file.precision (numeric_limits<double>::digits10);
file.setf (ios::fixed, ios::floatfield);
for (auto v: xyz)
{
file << "v " << v[0] << ' ' << v[1] << ' ' << v[2] << '\n';
}
for (size_t i = 1, n = xyz.size () - params.hmap_size[0]; i <= n; ++i)
{
if (i % params.hmap_size[0])
{
auto v1 = i + 1;
auto v2 = i;
auto v3 = i + params.hmap_size[0];
file << "f " << v1 << ' ' << v2 << ' ' << v3 << '\n';
v2 = v3;
v3 = v2 + 1;
file << "f " << v1 << ' ' << v2 << ' ' << v3 << '\n';
}
}
}
//--------------------------------------------------------------------------------------------------
/**
* The venerable C++ main() function.
*/
int main (int argc, const char* argv[])
{
using namespace std;
const char* info =
"hmap2obj - A binary heightmap convertor to Wavefront *.obj file\n"
"\n"
"hmap2obj HMAP OBJ SIZE_X SIZE_Y OBJ_LOW_CORNER OBJ_HIGH_CORNER [--absolute]\n"
"HMAP - is the output binary heightmap file\n"
"OBJ - is the input obj file\n"
"SIZE_XY - the two integer dimensions of the heightmap which to put into the obj\n"
"OBJ_CORNER - the low/high 3d floating corners of the obj to hold the heightmap\n"
"absolute - disables the automatic stretch of input min/max height values\n"
"\n"
"Example:\n"
"hmap2obj terrain.r16 terrain.obj 4096 4096 -0.5 0 -0.5 0.5 0.1 0.5\n"
;
try
{
// CLI
vector<string> args (argv + 1, argv + argc);
for (auto& str: args)
if (str == "--help")
{
cout << info << endl;
return 0;
}
auto p = hmap2obj::parse_cli (args);
auto err = hmap2obj::validate_params (p);
if (err.size ())
{
cerr << err << endl;
return 1;
}
hmap2obj tool (move (p));
// Parse heightmap
cout << "Read heightmap file..." << endl;
tool.read_hmap ();
cout << "Min height: " << tool.hmap_min () << '\n'
<< "Max height: " << tool.hmap_max () << '\n';
// Create XYZ cloud
cout << "Create point cloud..." << endl;
tool.make_xyz ();
// Dump obj
cout << "Dump object file..." << endl;
tool.dump_obj ();
cout << "Done." << endl;
}
catch (exception& ex)
{
cerr << ex.what () << endl;
return 1;
}
return 0;
}
//--------------------------------------------------------------------------------------------------