Skip to content

Commit 10e185c

Browse files
authored
Heightfield to mesh conversion (#890)
1 parent d0b2b81 commit 10e185c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+5506
-652
lines changed

apps/CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
add_subdirectory(yscntrace)
2-
add_subdirectory(yscnproc)
3-
add_subdirectory(yimgproc)
4-
add_subdirectory(ymshproc)
1+
add_subdirectory(yscenetrace)
2+
add_subdirectory(ysceneproc)
3+
add_subdirectory(yimageproc)
4+
add_subdirectory(yshapeproc)
5+
add_subdirectory(yheightfieldproc)
56

67
if(YOCTO_OPENGL)
7-
add_subdirectory(yscnview)
8-
add_subdirectory(yimshproc)
9-
add_subdirectory(yscnitrace)
10-
add_subdirectory(yscnitraces)
11-
add_subdirectory(yimgview)
12-
add_subdirectory(yimgviews)
8+
add_subdirectory(ysceneview)
9+
add_subdirectory(ysceneitrace)
10+
add_subdirectory(ysceneitraces)
11+
add_subdirectory(yimageview)
12+
add_subdirectory(yimageviews)
1313
endif(YOCTO_OPENGL)

apps/yheightfieldproc/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_executable(yheightfieldproc yheightfieldproc.cpp)
2+
3+
set_target_properties(yheightfieldproc PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED YES)
4+
target_include_directories(yheightfieldproc PRIVATE ${CMAKE_SOURCE_DIR}/libs)
5+
target_link_libraries(yheightfieldproc yocto)
6+
File renamed without changes.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//
2+
// LICENSE:
3+
//
4+
// Copyright (c) 2016 -- 2020 Fabio Pellacini
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met:
8+
//
9+
// 1. Redistributions of source code must retain the above copyright notice,
10+
// this list of conditions and the following disclaimer.
11+
//
12+
// 2. Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
// POSSIBILITY OF SUCH DAMAGE.
27+
//
28+
29+
#include <yocto/yocto_commonio.h>
30+
#include <yocto/yocto_image.h>
31+
#include <yocto/yocto_math.h>
32+
#include <yocto/yocto_shape.h>
33+
using namespace yocto::math;
34+
namespace shp = yocto::shape;
35+
namespace img = yocto::image;
36+
namespace cli = yocto::commonio;
37+
38+
#include "ext/filesystem.hpp"
39+
namespace sfs = ghc::filesystem;
40+
41+
using std::string;
42+
using std::vector;
43+
using namespace std::string_literals;
44+
45+
int main(int argc, const char* argv[]) {
46+
// command line parameters
47+
auto smooth = false;
48+
auto scale = vec3f{1};
49+
auto uscale = 1.0f;
50+
auto height = 1.0f;
51+
auto rotate = zero3f;
52+
auto translate = zero3f;
53+
auto info = false;
54+
auto output = "out.ply"s;
55+
auto filename = "heightfield.png"s;
56+
57+
// parse command line
58+
auto cli = cli::make_cli(
59+
"yheightfieldproc", "Makes a mesh from a heightfield");
60+
add_option(cli, "--height,-h", height, "Height scale");
61+
add_option(cli, "--smooth", smooth, "Compute smooth normals");
62+
add_option(cli, "--rotatey,-ry", rotate.y, "Rotate around y axis");
63+
add_option(cli, "--rotatex,-rx", rotate.x, "Rotate around x axis");
64+
add_option(cli, "--rotatez,-rz", rotate.z, "Rotate around z axis");
65+
add_option(cli, "--translatey,-ty", translate.y, "Translate along y axis");
66+
add_option(cli, "--translatex,-tx", translate.x, "Translate along x axis");
67+
add_option(cli, "--translatez,-tz", translate.z, "Translate along z axis");
68+
add_option(cli, "--scale,-s", uscale, "Scale along xyz axes");
69+
add_option(cli, "--scaley,-sy", scale.y, "Scale along y axis");
70+
add_option(cli, "--scalex,-sx", scale.x, "Scale along x axis");
71+
add_option(cli, "--scalez,-sz", scale.z, "Scale along z axis");
72+
add_option(cli, "--info,-i", info, "print mesh info");
73+
add_option(cli, "--output,-o", output, "output mesh");
74+
add_option(cli, "mesh", filename, "input heightfield", true);
75+
parse_cli(cli, argc, argv);
76+
77+
// mesh data
78+
auto positions = std::vector<vec3f>{};
79+
auto normals = std::vector<vec3f>{};
80+
auto texcoords = std::vector<vec2f>{};
81+
auto quads = std::vector<vec4i>{};
82+
83+
// image data
84+
auto heightfield = img::image<float>{};
85+
86+
// load mesh
87+
auto ioerror = ""s;
88+
cli::print_progress("load image", 0, 1);
89+
if (img::is_hdr_filename(filename)) {
90+
if (!img::load_image(filename, heightfield, ioerror))
91+
cli::print_fatal(ioerror);
92+
} else {
93+
auto heightfield16 = img::image<ushort>{};
94+
if (!img::load_image(filename, heightfield16, ioerror))
95+
cli::print_fatal(ioerror);
96+
heightfield = img::ushort_to_float(heightfield16);
97+
}
98+
cli::print_progress("load shape", 1, 1);
99+
100+
// adjust height
101+
if (height != 1) {
102+
for (auto& pixel : heightfield) pixel *= height;
103+
}
104+
105+
// create heightfield
106+
shp::make_heightfield(quads, positions, normals, texcoords,
107+
heightfield.size(), heightfield.data_vector());
108+
if (!smooth) normals.clear();
109+
110+
// print info
111+
if (info) {
112+
cli::print_info("shape stats ------------");
113+
auto stats = shp::shape_stats(
114+
{}, {}, {}, quads, {}, {}, {}, positions, normals, texcoords, {}, {});
115+
for (auto& stat : stats) cli::print_info(stat);
116+
}
117+
118+
// transform
119+
if (uscale != 1) scale *= uscale;
120+
if (translate != zero3f || rotate != zero3f || scale != vec3f{1}) {
121+
cli::print_progress("transform shape", 0, 1);
122+
auto xform = translation_frame(translate) * scaling_frame(scale) *
123+
rotation_frame({1, 0, 0}, radians(rotate.x)) *
124+
rotation_frame({0, 0, 1}, radians(rotate.z)) *
125+
rotation_frame({0, 1, 0}, radians(rotate.y));
126+
for (auto& p : positions) p = transform_point(xform, p);
127+
for (auto& n : normals)
128+
n = transform_normal(xform, n, max(scale) != min(scale));
129+
cli::print_progress("transform shape", 1, 1);
130+
}
131+
132+
// save mesh
133+
cli::print_progress("save shape", 0, 1);
134+
if (!shp::save_shape(output, {}, {}, {}, quads, positions, normals, texcoords,
135+
{}, {}, ioerror))
136+
cli::print_fatal(ioerror);
137+
cli::print_progress("save shape", 1, 1);
138+
139+
// done
140+
return 0;
141+
}

apps/yimageproc/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable(yimageproc yimageproc.cpp)
2+
3+
set_target_properties(yimageproc PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED YES)
4+
target_include_directories(yimageproc PRIVATE ${CMAKE_SOURCE_DIR}/libs)
5+
target_link_libraries(yimageproc yocto)
File renamed without changes.

apps/yimgproc/yimgproc.cpp renamed to apps/yimageproc/yimageproc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ bool make_image_preset(
225225
make_fbmmap(img, size);
226226
img = srgb_to_rgb(img);
227227
} else if (type == "test-checker-opacity") {
228-
make_checker(img, size, 1, {1,1,1,1}, {0,0,0,0});
228+
make_checker(img, size, 1, {1, 1, 1, 1}, {0, 0, 0, 0});
229229
} else if (type == "test-grid-opacity") {
230-
make_grid(img, size, 1, {1,1,1,1}, {0,0,0,0});
230+
make_grid(img, size, 1, {1, 1, 1, 1}, {0, 0, 0, 0});
231231
} else {
232232
error = "unknown preset";
233233
img = {};

apps/yimageview/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable(yimageview yimageview.cpp)
2+
3+
set_target_properties(yimageview PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED YES)
4+
target_include_directories(yimageview PUBLIC ${CMAKE_SOURCE_DIR}/libs)
5+
target_link_libraries(yimageview yocto yocto_gui)
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)