|
| 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 | +} |
0 commit comments