diff --git a/src/libslic3r/TextureDisplacement.cpp b/src/libslic3r/TextureDisplacement.cpp index 26e8726a72..074eb69db6 100644 --- a/src/libslic3r/TextureDisplacement.cpp +++ b/src/libslic3r/TextureDisplacement.cpp @@ -2724,7 +2724,8 @@ indexed_triangle_set build_texture_displacement_v2(const indexed_triangle_set bool flip_normals, BakeStageRecorder *debug, TextureBakeStats *stats, - const std::vector *model_uv) + const std::vector *model_uv, + float resting_drop) { HeightFieldSampler combined = make_combined_displacement_sampler(mesh, layers, facets_data, model_uv); if (!combined) @@ -2871,6 +2872,9 @@ indexed_triangle_set build_texture_displacement_v2(const indexed_triangle_set bounds.min = bounds.min.cwiseMin(v); bounds.max = bounds.max.cwiseMax(v); } + // The bottom the pipeline clamps and snaps to is the plane the whole object rests on, not this + // part's own: a part held off the plate by another keeps the relief on its underside. + bounds.min.z() -= resting_drop; const auto sample = [&combined](const Vec3f &pos, const Vec3f &, const Vec3f &blend_normal) { // The blend normal chooses the projection; the move itself is along the smooth normal, which @@ -3030,7 +3034,8 @@ static indexed_triangle_set build_texture_displacement_in_place( bool flip_normals, BakeStageRecorder *debug, TextureBakeStats *stats, - const std::vector *model_uv) + const std::vector *model_uv, + float resting_drop) { // The classic path moves the vertices the mesh already has, so there is no budget to report on. (void) stats; @@ -3067,7 +3072,7 @@ static indexed_triangle_set build_texture_displacement_in_place( if (options.pipeline_v2) return build_texture_displacement_v2(mesh, layers, facets_data, options, progress, color, flip_normals, - debug, stats, model_uv); + debug, stats, model_uv, resting_drop); // Layers are combined in slot order, like stacked layers in an image editor: each one folds its // own displacement into the running total via its blend mode (see TextureBlendMode). @@ -3399,10 +3404,12 @@ static indexed_triangle_set build_texture_displacement_in_place( if (!any_displacement) return mesh; - // The model's own resting plane, taken before anything moves - see the clamp after smoothing. + // The plane the model rests on, taken before anything moves - see the clamp after smoothing. That + // is this mesh's own bottom, lowered to the object's when another part of it sits lower. float resting_z = std::numeric_limits::max(); for (const Vec3f &v : mesh.vertices) resting_z = std::min(resting_z, v.z()); + resting_z -= resting_drop; size_t moved_count = 0; for (size_t vi = 0; vi < mesh.vertices.size(); ++vi) @@ -3494,17 +3501,20 @@ indexed_triangle_set build_texture_displacement(const indexed_triangle_set const Transform3d &volume_to_world, BakeStageRecorder *debug, TextureBakeStats *stats, - const std::vector *model_uv) + const std::vector *model_uv, + const Vec3d *texture_anchor, + float resting_drop) { // An untransformed volume on an untransformed instance is by far the common case, and the round // trip costs two matrix multiplies per vertex on a mesh that can carry millions of them - so take // the identity out of the way rather than paying for it. // The texture is projected in the bake frame - world orientation and scale, anchored at the - // volume's origin (see texture_displacement_bake_frame()). - const Transform3d frame = texture_displacement_bake_frame(volume_to_world); + // texture's anchor (see texture_displacement_bake_frame()). + const Transform3d frame = texture_anchor != nullptr ? texture_displacement_bake_frame(volume_to_world, *texture_anchor) : + texture_displacement_bake_frame(volume_to_world); if (frame.matrix().isApprox(Transform3d::Identity().matrix())) return build_texture_displacement_in_place(base_mesh, layers, facets_data, options, progress, color, - false, debug, stats, model_uv); + false, debug, stats, model_uv, resting_drop); const Transform3d to_local = frame.inverse(); // A mirroring placement leaves the positions correct but every winding-derived normal pointing @@ -3520,8 +3530,8 @@ indexed_triangle_set build_texture_displacement(const indexed_triangle_set const size_t debug_mark = (debug != nullptr) ? debug->mark() : 0; // The UV channel rides along untouched: a uv is a coordinate in the texture, not on the model, so // the frame the mesh is measured in does not reach it. - indexed_triangle_set out = build_texture_displacement_in_place(world, layers, facets_data, options, - progress, color, mirrored, debug, stats, model_uv); + indexed_triangle_set out = build_texture_displacement_in_place(world, layers, facets_data, options, progress, + color, mirrored, debug, stats, model_uv, resting_drop); // Everything the bake recorded is in world millimetres, like `out` itself. The debug view draws in // the volume's local frame, so the stages are brought back the same way the result is. if (debug != nullptr) @@ -3568,13 +3578,18 @@ void texture_displacement_patch_frame(const indexed_triangle_set &patch, const s (an.y() <= an.x() && an.y() <= an.z()) ? Vec3f::UnitY() : Vec3f::UnitZ(); } +Transform3d texture_displacement_bake_frame(const Transform3d &volume_to_world, const Vec3d &anchor) +{ + // World orientation and scale, but the origin moved onto the model: the texture then rides with + // the model when it is moved about the plate (and a single, untiled stamp starts on the model + // rather than at the plate's corner), while a tile is still `tiling_scale` printed millimetres + // whatever the instance's scale. + return Eigen::Translation3d(-anchor) * volume_to_world; +} + Transform3d texture_displacement_bake_frame(const Transform3d &volume_to_world) { - // World orientation and scale, but the origin moved to where the volume's own origin sits: the - // texture then rides with the model when it is moved about the plate (and a single, untiled stamp - // starts on the model rather than at the plate's corner), while a tile is still `tiling_scale` - // printed millimetres whatever the instance's scale. - return Eigen::Translation3d(-volume_to_world.translation()) * volume_to_world; + return texture_displacement_bake_frame(volume_to_world, volume_to_world.translation()); } Transform3d texture_displacement_volume_to_world(const ModelVolume &volume) diff --git a/src/libslic3r/TextureDisplacement.hpp b/src/libslic3r/TextureDisplacement.hpp index 919dfad654..89450270eb 100644 --- a/src/libslic3r/TextureDisplacement.hpp +++ b/src/libslic3r/TextureDisplacement.hpp @@ -1040,14 +1040,26 @@ indexed_triangle_set build_texture_displacement(const indexed_triangle_set // When given, receives what the bake spent. TextureBakeStats *stats = nullptr, // The model's own UV channel; see above. - const std::vector *model_uv = nullptr); + const std::vector *model_uv = nullptr, + // The world point the texture is anchored at, null for + // the volume's own origin. Every part of a multibody + // object passes the same point, so the pattern runs on + // across them. See texture_displacement_bake_frame(). + const Vec3d *texture_anchor = nullptr, + // How far the object's lowest point lies below this + // volume's own, in mm. Relief is kept above the plane + // the whole object rests on, so a part held off the + // plate by another keeps the relief on its underside. + float resting_drop = 0.f); // `volume`'s mesh coordinates -> world millimetres: its first instance's transform times its own. // The mesh is shared by every instance, so a multi-instance object can only be baked for one of // them; the first is what the gizmo edits against. Identity when the volume has no object yet. Transform3d texture_displacement_volume_to_world(const ModelVolume &volume); -// The frame the bake and the previews project the texture in: `volume_to_world` with its translation -// removed, i.e. world orientation and scale about the volume's own origin. See build_texture_displacement(). +// The frame the bake and the previews project the texture in: world orientation and scale, with the +// world point `anchor` as its origin. See build_texture_displacement(). +Transform3d texture_displacement_bake_frame(const Transform3d &volume_to_world, const Vec3d &anchor); +// Anchored at the volume's own origin, which is where a single-part object's texture sits. Transform3d texture_displacement_bake_frame(const Transform3d &volume_to_world); // Area-weighted vertex normals of `its` - the directions the bake both projects and displaces along. diff --git a/src/slic3r/GUI/Gizmos/GLGizmoTextureDisplacement.cpp b/src/slic3r/GUI/Gizmos/GLGizmoTextureDisplacement.cpp index e269dbca53..adc9982a80 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoTextureDisplacement.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoTextureDisplacement.cpp @@ -7,6 +7,7 @@ #include "ColorSpaceConvert.hpp" #include "libslic3r/AABBTreeIndirect.hpp" +#include "libslic3r/BoundingBox.hpp" #include "libslic3r/Color.hpp" #include "libslic3r/FilamentMixer.hpp" #include "libslic3r/PresetBundle.hpp" @@ -26,6 +27,7 @@ #include "slic3r/GUI/GuiColor.hpp" #include "slic3r/GUI/ImGuiWrapper.hpp" #include "slic3r/GUI/MainFrame.hpp" // wxGetApp().mainframe, as the projector window's parent +#include "slic3r/GUI/MeshUtils.hpp" #include "slic3r/GUI/MsgDialog.hpp" #include "slic3r/GUI/OpenGLManager.hpp" #include "slic3r/GUI/Plater.hpp" @@ -2483,7 +2485,10 @@ void GLGizmoTextureDisplacement::queue_preview_job() // the overlays and the wireframe are keyed to - from the rest. std::vector part_ids; part_ids.reserve(parts.size()); - for (const ModelVolume *part : parts) { + const Vec3d anchor = texture_anchor(); + const std::vector drops = resting_drops(parts); + for (size_t i = 0; i < parts.size(); ++i) { + const ModelVolume *part = parts[i]; TextureDisplacementPreviewVolume entry; entry.base_mesh = part->mesh().its; entry.layers = layers_for_part(*part); @@ -2493,10 +2498,12 @@ void GLGizmoTextureDisplacement::queue_preview_job() if (entry.options.pipeline_v2) entry.options.v2_max_triangles_k = 0; entry.volume_to_world = texture_displacement_volume_to_world(*part); + entry.texture_anchor = anchor; + entry.resting_drop = drops[i]; if (part->has_texture_uv()) entry.model_uv = part->texture_uv; - for (int i = 0; i < int(TEXTURE_DISPLACEMENT_MAX_LAYERS); ++i) - entry.facets_data[size_t(i)] = part->texture_displacement_facet(i).get_data(); + for (int slot = 0; slot < int(TEXTURE_DISPLACEMENT_MAX_LAYERS); ++slot) + entry.facets_data[size_t(slot)] = part->texture_displacement_facet(slot).get_data(); input.volumes.push_back(std::move(entry)); part_ids.push_back(part->id()); } @@ -3839,6 +3846,36 @@ std::vector GLGizmoTextureDisplacement::layers_for_par return out; } +Vec3d GLGizmoTextureDisplacement::texture_anchor() const +{ + const ModelVolume *owner = texture_volume(); + return owner != nullptr ? Vec3d(texture_displacement_volume_to_world(*owner).translation()) : Vec3d::Zero(); +} + +std::vector GLGizmoTextureDisplacement::resting_drops(const std::vector &parts) +{ + const auto bottom = [](const ModelVolume &part) { + return part.mesh().transformed_bounding_box(texture_displacement_volume_to_world(part)).min.z(); + }; + // Each part's bottom is measured the same way as the object's, so the lowest part's drop is exactly + // 0 and a single-part object bakes against its own bottom as it always has. + double object_bottom = std::numeric_limits::max(); + for (const ModelVolume *part : texture_parts()) + object_bottom = std::min(object_bottom, bottom(*part)); + std::vector out; + out.reserve(parts.size()); + for (const ModelVolume *part : parts) + out.push_back(float(bottom(*part) - object_bottom)); + return out; +} + +Transform3d GLGizmoTextureDisplacement::prepare_texture_frame(const ModelVolume &part) +{ + if (texture_parts().size() < 2) + return Transform3d::Identity(); + return texture_displacement_bake_frame(texture_displacement_volume_to_world(part), texture_anchor()); +} + void GLGizmoTextureDisplacement::update_model_object() { store_panel_state(); @@ -5119,7 +5156,10 @@ void GLGizmoTextureDisplacement::remove_texture_layer(int slot) [slot](const TextureDisplacementLayer &l) { return l.slot == slot; }), layers.end()); if (slot >= 0 && slot < int(TEXTURE_DISPLACEMENT_MAX_LAYERS)) { - mv->texture_displacement_facet(slot).reset(); + // Every model part: paint left in the slot on another part would still bake, and the next layer + // added reuses the slot. + for (ModelVolume *part : texture_parts()) + part->texture_displacement_facet(slot).reset(); m_thumbnails[size_t(slot)].reset(); m_thumbnail_source[size_t(slot)].reset(); m_layer_expanded[size_t(slot)] = false; @@ -5210,25 +5250,19 @@ void GLGizmoTextureDisplacement::move_texture_layer(int slot, int to_index) int GLGizmoTextureDisplacement::select_visible_faces(const std::array *uv_clip) { - ModelVolume *mv = texture_volume(); - ModelObject *mo = m_c->selection_info()->model_object(); - const int idx = texture_volume_raycaster_index(); - if (mv == nullptr || mo == nullptr || idx < 0 || idx >= int(m_triangle_selectors.size())) + const ModelVolume *owner = texture_volume(); + ModelObject *mo = m_c->selection_info()->model_object(); + // Every model part, at its selector's and its raycaster's index (see texture_parts()): what is in + // view gets painted whichever part it belongs to, and one part can hide another. + const std::vector parts = texture_parts(); + const std::vector raycasters = m_c->raycaster()->raycasters(); + if (owner == nullptr || mo == nullptr || parts.size() > m_triangle_selectors.size() || parts.size() > raycasters.size()) return 0; - const indexed_triangle_set &its = mv->mesh().its; - if (its.indices.empty()) - return 0; - - const Selection &selection = m_parent.get_selection(); - const Geometry::Transformation trafo(mo->instances[selection.get_instance_idx()]->get_transformation().get_matrix() * - mv->get_matrix()); - const Transform3d &to_world = trafo.get_matrix(); - const Camera &camera = wxGetApp().plater()->get_camera(); - - // Normals transform by the inverse transpose, not by the matrix itself - with a non-uniform - // scale the two differ, and using the wrong one flips the facing test on the scaled axes. - const Matrix3d normal_matrix = to_world.matrix().block<3, 3>(0, 0).inverse().transpose(); + const Selection &selection = m_parent.get_selection(); + const Transform3d instance = mo->instances[selection.get_instance_idx()]->get_transformation().get_matrix(); + const Transform3d owner_to_world = instance * owner->get_matrix(); + const Camera &camera = wxGetApp().plater()->get_camera(); // Pass 1 (cheap): drop back-facing triangles. Under perspective the view direction varies across // the model, so it is taken per triangle from the eye to the centroid; under an orthographic @@ -5238,69 +5272,109 @@ int GLGizmoTextureDisplacement::select_visible_faces(const std::array const bool ortho = camera.get_type() == Camera::EType::Ortho; const Vec3d fwd = camera.get_dir_forward(); - std::vector centroids; // world coords, what get_unobscured_idxs() expects - std::vector front_facing; // parallel: which facet each centroid came from - centroids.reserve(its.indices.size() / 2); - front_facing.reserve(its.indices.size() / 2); + std::vector trafos; + std::vector> centroids(parts.size()); // world coords, what get_unobscured_idxs() expects + std::vector> front_facing(parts.size()); // parallel: which facet each centroid came from + trafos.reserve(parts.size()); + for (size_t p = 0; p < parts.size(); ++p) { + const ModelVolume &part = *parts[p]; + const indexed_triangle_set &its = part.mesh().its; + trafos.emplace_back(instance * part.get_matrix()); + const Transform3d &to_world = trafos.back().get_matrix(); + // The projection frame maps the owner's local coordinates (see apply_projection_frame()), so + // another part's centroids are brought into them before the clip test. + const Transform3d to_owner = (&part == owner) ? Transform3d::Identity() : Transform3d(owner_to_world.inverse() * to_world); - for (size_t f = 0; f < its.indices.size(); ++f) { - const stl_triangle_vertex_indices &tri = its.indices[f]; - const Vec3d a = its.vertices[tri[0]].cast(); - const Vec3d b = its.vertices[tri[1]].cast(); - const Vec3d c = its.vertices[tri[2]].cast(); - const Vec3d n_world = normal_matrix * (b - a).cross(c - a); - if (n_world.squaredNorm() < 1e-20) - continue; // degenerate triangle: no meaningful normal, so no meaningful facing test - const Vec3d centroid_local = (a + b + c) / 3.0; - const Vec3d centroid_world = to_world * centroid_local; - const Vec3d view_dir = ortho ? fwd : Vec3d(centroid_world - eye); - if (n_world.dot(view_dir) >= 0.0) - continue; // facing away from the camera + // Normals transform by the inverse transpose, not by the matrix itself - with a non-uniform + // scale the two differ, and using the wrong one flips the facing test on the scaled axes. + const Matrix3d normal_matrix = to_world.matrix().block<3, 3>(0, 0).inverse().transpose(); - // Clip to the projection frame before the raycast, not after: outside the frame the texture - // samples to nothing anyway, so those facets would only be painted to no effect - and this - // is also what keeps the ray queries proportional to the framed area instead of the model. - if (uv_clip != nullptr) { - Vec2f uv; - if (!project_uv_projective(*uv_clip, centroid_local.cast(), uv)) - continue; // behind the projector - if (uv.x() < 0.f || uv.x() > 1.f || uv.y() < 0.f || uv.y() > 1.f) - continue; + centroids[p].reserve(its.indices.size() / 2); + front_facing[p].reserve(its.indices.size() / 2); + for (size_t f = 0; f < its.indices.size(); ++f) { + const stl_triangle_vertex_indices &tri = its.indices[f]; + const Vec3d a = its.vertices[tri[0]].cast(); + const Vec3d b = its.vertices[tri[1]].cast(); + const Vec3d c = its.vertices[tri[2]].cast(); + const Vec3d n_world = normal_matrix * (b - a).cross(c - a); + if (n_world.squaredNorm() < 1e-20) + continue; // degenerate triangle: no meaningful normal, so no meaningful facing test + const Vec3d centroid_local = (a + b + c) / 3.0; + const Vec3d centroid_world = to_world * centroid_local; + const Vec3d view_dir = ortho ? fwd : Vec3d(centroid_world - eye); + if (n_world.dot(view_dir) >= 0.0) + continue; // facing away from the camera + + // Clip to the projection frame before the raycast, not after: outside the frame the texture + // samples to nothing anyway, so those facets would only be painted to no effect - and this + // is also what keeps the ray queries proportional to the framed area instead of the model. + if (uv_clip != nullptr) { + Vec2f uv; + if (!project_uv_projective(*uv_clip, (to_owner * centroid_local).cast(), uv)) + continue; // behind the projector + if (uv.x() < 0.f || uv.x() > 1.f || uv.y() < 0.f || uv.y() > 1.f) + continue; + } + centroids[p].emplace_back(centroid_world.cast()); + front_facing[p].push_back(unsigned(f)); } - centroids.emplace_back(centroid_world.cast()); - front_facing.push_back(unsigned(f)); } - if (centroids.empty()) - return 0; // Pass 2 (the expensive one): a real ray query per surviving centroid, so geometry in front of a // front-facing triangle correctly hides it - the far inner wall of a cup is front-facing but not - // visible. This is why the whole thing is click-driven rather than live. - std::vector unobscured; + // visible. This is why the whole thing is click-driven rather than live. Each raycaster only sees + // its own part's mesh, so a centroid has to be clear of every one of them; its own part goes first, + // since that is what hides most of them. + size_t visible = 0; { wxBusyCursor wait; - unobscured = m_c->raycaster()->raycasters()[size_t(idx)]->get_unobscured_idxs( - trafo, camera, centroids, m_c->object_clipper()->get_clipping_plane()); + const ClippingPlane *clipping_plane = m_c->object_clipper()->get_clipping_plane(); + for (size_t p = 0; p < parts.size(); ++p) { + std::vector &points = centroids[p]; + std::vector &facets = front_facing[p]; + const auto keep_unobscured_by = [&](size_t q) { + const std::vector clear = raycasters[q]->get_unobscured_idxs(trafos[q], camera, points, clipping_plane); + std::vector kept_points; + std::vector kept_facets; + kept_points.reserve(clear.size()); + kept_facets.reserve(clear.size()); + for (unsigned i : clear) { + kept_points.push_back(points[i]); + kept_facets.push_back(facets[i]); + } + points = std::move(kept_points); + facets = std::move(kept_facets); + }; + if (!points.empty()) + keep_unobscured_by(p); + for (size_t q = 0; q < parts.size() && !points.empty(); ++q) + if (q != p) + keep_unobscured_by(q); + visible += facets.size(); + } } - if (unobscured.empty()) + if (visible == 0) return 0; Plater::TakeSnapshot snapshot(wxGetApp().plater(), _u8L("Select visible faces for texture displacement"), UndoRedo::SnapshotType::GizmoAction); - // Replaces the layer's paint rather than adding to it: the checkbox means "project onto what I - // can see", so a second capture from a new angle should not leave the previous angle painted. + // Replaces the layer's paint rather than adding to it, on every part including those nothing was + // visible on: the checkbox means "project onto what I can see", so a second capture from a new + // angle should not leave the previous angle painted. // TriangleSelectorGUI, not the TriangleSelector base: request_update_render_data() is declared on // the GUI subclass, so binding to the base here would drop it. - TriangleSelectorGUI &selector = *m_triangle_selectors[size_t(idx)]; - selector.reset(); - for (unsigned i : unobscured) - selector.set_facet(int(front_facing[i]), EnforcerBlockerType::ENFORCER); - selector.request_update_render_data(true); + for (size_t p = 0; p < parts.size(); ++p) { + TriangleSelectorGUI &selector = *m_triangle_selectors[p]; + selector.reset(); + for (unsigned f : front_facing[p]) + selector.set_facet(int(f), EnforcerBlockerType::ENFORCER); + selector.request_update_render_data(true); + } update_model_object(); m_parent.set_as_dirty(); - return int(unobscured.size()); + return int(visible); } void GLGizmoTextureDisplacement::select_whole_model() @@ -5996,13 +6070,35 @@ ColorQuantizeFn GLGizmoTextureDisplacement::make_palette_quantizer(const std::ve TextureDisplacementPrepareResult GLGizmoTextureDisplacement::prepare_mesh( const indexed_triangle_set &base, const TextureDisplacementFacetsData &masks, const std::vector &layers, const std::vector &model_uv, - const TextureDisplacementPrepareParams ¶ms, + const Transform3d &texture_frame, const TextureDisplacementPrepareParams ¶ms, const TextureColorSettings &color_settings, const DisplacementProgressFn &progress, BakeStageRecorder *debug) { TextureDisplacementPrepareResult out; const auto report = [&progress](int pct) { return !progress || progress(pct); }; + // The samplers are built over a copy of the current mesh moved into `texture_frame`, and queried + // with points and normals mapped across, so the stages below go on measuring the mesh itself. + const bool framed = !texture_frame.matrix().isApprox(Transform3d::Identity().matrix()); + const Matrix3d frame_normal = texture_frame.linear().inverse().transpose(); + indexed_triangle_set framed_mesh; + const auto mesh_to_sample = [&](const indexed_triangle_set &its) -> const indexed_triangle_set & { + if (!framed) + return its; + framed_mesh = its; + for (Vec3f &v : framed_mesh.vertices) + v = (texture_frame * v.cast()).cast(); + return framed_mesh; + }; + const auto sample_in_frame = [&](auto sampler) -> decltype(sampler) { + if (!framed || !sampler) + return sampler; + return [sampler, frame = texture_frame, frame_normal](const Vec3f &pos, const Vec3f &normal) { + return sampler((frame * pos.cast()).cast(), + (frame_normal * normal.cast()).normalized().cast()); + }; + }; + // Stage capture for the debug view. Timed from the end of the previous stage, so the capture - // a copy plus an edge scan - never lands inside the measurement it reports. auto stage_clock = std::chrono::steady_clock::now(); @@ -6078,17 +6174,20 @@ TextureDisplacementPrepareResult GLGizmoTextureDisplacement::prepare_mesh( // length baseline alone. const std::vector uv_stage = uv_now(); const std::vector *uv_ptr = uv_stage.empty() ? nullptr : &uv_stage; + const indexed_triangle_set &sampled = mesh_to_sample(mesh.its); HeightFieldSampler sampler; if (params.subdiv_feature) - sampler = make_combined_displacement_sampler(mesh.its, layers, current, uv_ptr); + sampler = sample_in_frame(make_combined_displacement_sampler(sampled, layers, current, uv_ptr)); // Colour boundaries need triangles of their own - the chord test cannot see them, since // the height field is perfectly smooth across a change of filament. ColorFieldSampler color; // A flat-colour layer is matched against single filaments only, as the bake does, so its // boundaries are refined where the bake will actually change filament. if (params.subdiv_color_edge_mm > 0.f && !color_settings.empty()) - color = make_combined_color_sampler(mesh.its, layers, current, make_palette_quantizer(color_settings.palette), - make_palette_quantizer(color_settings.palette_pure), uv_ptr); + color = sample_in_frame(make_combined_color_sampler(sampled, layers, current, + make_palette_quantizer(color_settings.palette), + make_palette_quantizer(color_settings.palette_pure), + uv_ptr)); // The refinement follows perceived colour: a mix is one colour here, however the slicer // interleaves its filaments layer by layer. // "Min edge" is a feature-mode control (it is the floor the curvature test refines down @@ -6149,8 +6248,8 @@ TextureDisplacementPrepareResult GLGizmoTextureDisplacement::prepare_mesh( cut_region[i] = (region[size_t(source[i])] & REFINE_PAINTED) ? 1 : 0; const std::vector uv_refined = uv_now(); const HeightFieldSampler refined_sampler = - make_combined_displacement_sampler(mesh.its, layers, current, - uv_refined.empty() ? nullptr : &uv_refined); + sample_in_frame(make_combined_displacement_sampler(mesh_to_sample(mesh.its), layers, current, + uv_refined.empty() ? nullptr : &uv_refined)); std::vector cut_source; size_t cuts = 0; indexed_triangle_set cut = cut_mesh_at_steps(mesh.its, cut_region, refined_sampler, @@ -6519,11 +6618,13 @@ void GLGizmoTextureDisplacement::bake(bool own_snapshot) return; } + const Vec3d anchor = texture_anchor(); + const std::vector drops = resting_drops(parts); std::vector volumes; volumes.reserve(parts.size()); - for (const ModelVolume *part : parts) - volumes.push_back(texture_displacement_bake_volume(*part, layers_for_part(*part), - owner->texture_displacement_options)); + for (size_t i = 0; i < parts.size(); ++i) + volumes.push_back(texture_displacement_bake_volume(*parts[i], layers_for_part(*parts[i]), + owner->texture_displacement_options, anchor, drops[i])); m_bake_in_progress = true; queue_texture_displacement_bake(std::move(volumes), color_settings_for(*owner), [this]() { @@ -6672,7 +6773,8 @@ void GLGizmoTextureDisplacement::run_stage_debug() m_debug_in_progress = true; queue_texture_displacement_debug( - *mv, color_settings_for(*mv), params, run_prepare, m_debug_check_topology, + *mv, color_settings_for(*mv), params, prepare_texture_frame(*mv), resting_drops({mv}).front(), run_prepare, + m_debug_check_topology, [this](std::vector stages) { m_debug_in_progress = false; if (!stages.empty()) { @@ -6849,6 +6951,7 @@ void GLGizmoTextureDisplacement::queue_prepare(const TextureDisplacementPrepareP entry.layers = layers_for_part(*part); if (part->has_texture_uv()) entry.model_uv = part->texture_uv; + entry.texture_frame = prepare_texture_frame(*part); input.volumes.push_back(std::move(entry)); } if (params.subdiv_color_edge_mm > 0.f) @@ -8712,7 +8815,9 @@ void GLGizmoTextureDisplacement::on_render_input_window(float x, float y, float ImGui::SameLine(x0 + panel_w - button_w * 2 - style.ItemSpacing.x); - const bool can_bake = !busy && mv != nullptr && any_part_painted(); + // Read before Close below, which releases the selection any_part_painted() finds the parts through. + const bool painted = mv != nullptr && any_part_painted(); + const bool can_bake = !busy && painted; const std::string bake_label = m_prepare_in_progress ? _u8L("Preparing...") : m_bake_in_progress ? _u8L("Baking...") : into_u8(m_desc.at("bake")); @@ -8732,7 +8837,7 @@ void GLGizmoTextureDisplacement::on_render_input_window(float x, float y, float m_imgui->pop_bold_font(); GLGizmoUtils::pop_orca_button_style(); if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) - panel_tooltip(mv != nullptr && !any_part_painted() ? + panel_tooltip(mv != nullptr && !painted ? (m_seam_edit_mode ? _u8L("Nothing is painted yet. The UV editor's seam tool is on, so " "strokes on the model mark seams instead of painting - turn " "it off in the pane to paint.") : @@ -8769,7 +8874,7 @@ void GLGizmoTextureDisplacement::on_render_input_window(float x, float y, float "Bake moves existing vertices - the model stays at %1% triangles", base_n)), base_count) : Slic3r::format(into_u8(_L_PLURAL("Bake result up to ~%1% k triangle", "Bake result up to ~%1% k triangles", result_n)), base_k + m_subdivide_budget_k); - if (any_part_painted()) { + if (painted) { std::vector skipped; for (const TextureDisplacementLayer &l : mv->texture_displacement_layers) if (!slot_painted(l.slot)) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoTextureDisplacement.hpp b/src/slic3r/GUI/Gizmos/GLGizmoTextureDisplacement.hpp index 64bc992bac..873d90ff69 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoTextureDisplacement.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoTextureDisplacement.hpp @@ -70,6 +70,14 @@ public: // to the samplers so a ModelUV layer drives // the refinement through its own layout. const std::vector &model_uv, + // `base`'s coordinates -> the frame the bake + // samples the texture in, so the refinement + // follows the texture the bake will apply. + // Only the sampling moves into it: the mesh, + // and every length in `params`, stay in + // `base`'s own coordinates. Identity samples + // there as well. + const Transform3d &texture_frame, const TextureDisplacementPrepareParams ¶ms, const TextureColorSettings &color_settings, const DisplacementProgressFn &progress, @@ -312,9 +320,21 @@ private: // seam edges, the island placements, their join groups and the per-vertex uv overrides. Carried onto // another part they would address triangles and charts that have nothing to do with it, so they are // dropped and that part unwraps on its own. Everything else - the image, depth, tiling, projection - // method, blend mode, colour - applies unchanged, which is what lets one stack texture a whole - // multibody object. + // method, blend mode, colour - applies unchanged, since every part is baked in the one texture frame + // anchored at texture_anchor(); that is what lets one stack texture a whole multibody object. std::vector layers_for_part(const ModelVolume &part) const; + // The world point the object's texture is anchored at: texture_volume()'s origin. Every part is + // baked and previewed in the frame anchored there (see texture_displacement_bake_frame()), so a + // tiled pattern runs on across the parts and a view projection lands where it was aimed on each. + Vec3d texture_anchor() const; + // For each of `parts`, at the same index: how far the object's lowest point lies below that part's + // own, in mm - what its bake lowers its resting plane by, so every part is clamped against the one + // plane the object rests on. 0 for the lowest part, and so for a single-part object. + std::vector resting_drops(const std::vector &parts); + // Where prepare_mesh() samples `part`'s texture: the frame its bake samples in when the object has + // several model parts, so they are all refined for one continuous texture. A single-part object + // keeps the identity, the frame it has always been refined in. + Transform3d prepare_texture_frame(const ModelVolume &part); void add_texture_layer(); void remove_texture_layer(int slot); @@ -409,14 +429,14 @@ private: // volume's slot among them, or -1 if it has none (no selection, or the lists disagree). int texture_volume_raycaster_index() const; - // Paints exactly the facets currently visible from the camera onto the active layer, replacing - // whatever that layer had painted. "Visible" is two tests: the facet faces the camera, and its - // centroid is not hidden behind other geometry (a real raycast, so a concave part's far inner - // wall is correctly excluded). When `uv_clip` is given (the projection frame's matrix), facets - // whose centroid falls outside the frame's uv unit square are skipped first - which both clips - // the selection to the frame and spares the raycast for everything outside it. Costs one ray - // query per surviving facet, so it is a one-shot action, never a per-frame one. Returns the - // number of facets selected. + // Paints exactly the facets currently visible from the camera onto the active layer, on every model + // part, replacing whatever that layer had painted on each. "Visible" is two tests: the facet faces + // the camera, and its centroid is not hidden behind other geometry of any part (a real raycast, so + // a concave part's far inner wall is correctly excluded). When `uv_clip` is given (the projection + // frame's matrix), facets whose centroid falls outside the frame's uv unit square are skipped first + // - which both clips the selection to the frame and spares the raycast for everything outside it. + // Costs one ray query per surviving facet and part, so it is a one-shot action, never a per-frame + // one. Returns the number of facets selected. int select_visible_faces(const std::array *uv_clip = nullptr); // When set, "Capture current view" also re-selects the visible faces, so the viewpoint the diff --git a/src/slic3r/GUI/Jobs/TextureDisplacementBakeJob.cpp b/src/slic3r/GUI/Jobs/TextureDisplacementBakeJob.cpp index 33106bd056..ef3a49b8f7 100644 --- a/src/slic3r/GUI/Jobs/TextureDisplacementBakeJob.cpp +++ b/src/slic3r/GUI/Jobs/TextureDisplacementBakeJob.cpp @@ -6,6 +6,7 @@ #include #include #include "libslic3r/TextureDisplacement.hpp" +#include "libslic3r/Point.hpp" #include #include #include @@ -88,7 +89,7 @@ void TextureDisplacementBakeJob::process(Ctl &ctl) return true; }, color, volume.volume_to_world, nullptr, &m_results[i].stats, - volume.model_uv.empty() ? nullptr : &volume.model_uv)); + volume.model_uv.empty() ? nullptr : &volume.model_uv, &volume.texture_anchor, volume.resting_drop)); if (ctl.was_canceled()) break; } @@ -336,7 +337,9 @@ void TextureDisplacementBakeJob::finalize(bool canceled, std::exception_ptr &ept TextureDisplacementBakeVolume texture_displacement_bake_volume(const ModelVolume &volume, const std::vector &layers, - const TextureDisplacementOptions &options) + const TextureDisplacementOptions &options, + const Vec3d &texture_anchor, + float resting_drop) { TextureDisplacementBakeVolume out; out.volume_id = volume.id(); @@ -344,6 +347,8 @@ TextureDisplacementBakeVolume texture_displacement_bake_volume(const ModelVolume out.layers = layers; out.options = options; out.volume_to_world = texture_displacement_volume_to_world(volume); + out.texture_anchor = texture_anchor; + out.resting_drop = resting_drop; if (volume.has_texture_uv()) out.model_uv = volume.texture_uv; for (int i = 0; i < int(TEXTURE_DISPLACEMENT_MAX_LAYERS); ++i) diff --git a/src/slic3r/GUI/Jobs/TextureDisplacementBakeJob.hpp b/src/slic3r/GUI/Jobs/TextureDisplacementBakeJob.hpp index 3e5210072c..dc611910bf 100644 --- a/src/slic3r/GUI/Jobs/TextureDisplacementBakeJob.hpp +++ b/src/slic3r/GUI/Jobs/TextureDisplacementBakeJob.hpp @@ -34,6 +34,10 @@ struct TextureDisplacementBakeVolume // The part's own UV channel (ModelVolume::texture_uv), empty when it has none: what a ModelUV // layer maps through, and what the commit puts back on the baked mesh. std::vector model_uv; + // Where the part sits in its object: the world point the object's texture is anchored at, and how + // far the object's lowest point lies below the part's own. See build_texture_displacement(). + Vec3d texture_anchor = Vec3d::Zero(); + float resting_drop = 0.f; }; struct TextureDisplacementBakeInput @@ -93,12 +97,15 @@ void queue_texture_displacement_bake(std::vector const TextureColorSettings &color, std::function on_finished, bool take_snapshot = true); -// `volume`'s share of a bake, read off the live Model. `layers` and `options` are passed in rather than -// taken from `volume` because they belong to the object, not to the part: every part of a multibody -// object is displaced by the same stack (see GLGizmoTextureDisplacement::layers_for_part()). +// `volume`'s share of a bake, read off the live Model. `layers`, `options`, `texture_anchor` and +// `resting_drop` are passed in rather than taken from `volume` because they belong to the object, not +// to the part: every part of a multibody object is displaced by the same stack, in the same texture +// frame and against the same plate (see GLGizmoTextureDisplacement::layers_for_part()). TextureDisplacementBakeVolume texture_displacement_bake_volume(const ModelVolume &volume, const std::vector &layers, - const TextureDisplacementOptions &options); + const TextureDisplacementOptions &options, + const Vec3d &texture_anchor, + float resting_drop); } // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/Jobs/TextureDisplacementDebugJob.cpp b/src/slic3r/GUI/Jobs/TextureDisplacementDebugJob.cpp index eb4eec036f..0ef72a2f03 100644 --- a/src/slic3r/GUI/Jobs/TextureDisplacementDebugJob.cpp +++ b/src/slic3r/GUI/Jobs/TextureDisplacementDebugJob.cpp @@ -60,8 +60,8 @@ void TextureDisplacementDebugJob::process(Ctl &ctl) // experimental pipeline refines as part of the bake and has nothing to prepare. if (m_input.run_prepare && !m_input.options.pipeline_v2) { const TextureDisplacementPrepareResult prepared = - GLGizmoTextureDisplacement::prepare_mesh(mesh, masks, m_input.layers, uv, m_input.prepare_params, - m_input.color, + GLGizmoTextureDisplacement::prepare_mesh(mesh, masks, m_input.layers, uv, m_input.prepare_texture_frame, + m_input.prepare_params, m_input.color, // Preparation is roughly half the run; the bake // takes the progress bar from there. [&report](int pct) { return report(1 + pct / 2); }, @@ -86,7 +86,8 @@ void TextureDisplacementDebugJob::process(Ctl &ctl) build_texture_displacement(mesh, m_input.layers, masks, m_input.options, [&report](int pct) { return report(50 + pct / 2); }, /* color */ nullptr, m_input.volume_to_world, &recorder, /* stats */ nullptr, - uv.empty() ? nullptr : &uv); + uv.empty() ? nullptr : &uv, /* texture_anchor: the volume's own origin */ nullptr, + m_input.resting_drop); m_stages = recorder.take(); ctl.update_status(100, status); // always finish at 100: this is what closes the notification @@ -106,6 +107,7 @@ void TextureDisplacementDebugJob::finalize(bool canceled, std::exception_ptr &ep void queue_texture_displacement_debug(const ModelVolume &volume, const TextureColorSettings &color, const TextureDisplacementPrepareParams &prepare_params, + const Transform3d &prepare_texture_frame, float resting_drop, bool run_prepare, bool check_topology, std::function)> on_finished) { @@ -119,6 +121,8 @@ void queue_texture_displacement_debug(const ModelVolume &volume, const TextureCo input.check_topology = check_topology; input.color = color; input.volume_to_world = texture_displacement_volume_to_world(volume); + input.prepare_texture_frame = prepare_texture_frame; + input.resting_drop = resting_drop; if (volume.has_texture_uv()) input.model_uv = volume.texture_uv; for (int i = 0; i < int(TEXTURE_DISPLACEMENT_MAX_LAYERS); ++i) diff --git a/src/slic3r/GUI/Jobs/TextureDisplacementDebugJob.hpp b/src/slic3r/GUI/Jobs/TextureDisplacementDebugJob.hpp index 981a17773f..251f2cbccf 100644 --- a/src/slic3r/GUI/Jobs/TextureDisplacementDebugJob.hpp +++ b/src/slic3r/GUI/Jobs/TextureDisplacementDebugJob.hpp @@ -44,6 +44,11 @@ struct TextureDisplacementDebugInput // Mesh coordinates -> world millimetres, as the bake gets it. The captured stages are brought // back into mesh coordinates before they are handed over, so the gizmo can draw them directly. Transform3d volume_to_world = Transform3d::Identity(); + // Where the preparation samples the texture, and how far below the volume's own bottom its object + // rests - both as the bake and its preparation get them (see TextureDisplacementPrepareVolume and + // TextureDisplacementBakeVolume). + Transform3d prepare_texture_frame = Transform3d::Identity(); + float resting_drop = 0.f; // Only the quantizer is used, and only by the refinement's colour criterion - the debug run // produces no colour of its own. TextureColorSettings color; @@ -79,6 +84,7 @@ private: // caller can clear its own in-progress state. Must be called from the main thread. void queue_texture_displacement_debug(const ModelVolume &volume, const TextureColorSettings &color, const TextureDisplacementPrepareParams &prepare_params, + const Transform3d &prepare_texture_frame, float resting_drop, bool run_prepare, bool check_topology, std::function)> on_finished); diff --git a/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.cpp b/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.cpp index 0741519ef3..5c19e6b450 100644 --- a/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.cpp +++ b/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.cpp @@ -52,7 +52,8 @@ void TextureDisplacementPrepareJob::process(Ctl &ctl) for (size_t i = 0; i < count; ++i) { const TextureDisplacementPrepareVolume &volume = m_input.volumes[i]; m_results[i] = GLGizmoTextureDisplacement::prepare_mesh(volume.base_mesh, volume.masks, volume.layers, - volume.model_uv, m_input.params, m_input.color, + volume.model_uv, volume.texture_frame, + m_input.params, m_input.color, [&ctl, &status, &last_reported, i, count](int percent) { if (ctl.was_canceled()) return false; diff --git a/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.hpp b/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.hpp index 04bf3985a4..1565e54f83 100644 --- a/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.hpp +++ b/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.hpp @@ -49,6 +49,10 @@ struct TextureDisplacementPrepareVolume // resamples it onto the mesh it produces, and the commit puts it back - like the masks, it is // indexed by triangle and so cannot survive the stages on its own. std::vector model_uv; + // Where prepare_mesh() samples the texture: the bake frame shared by every part of a multibody + // object, so each part is refined for the texture the bake will put on it. Identity, the part's own + // coordinates, for a single-part object. + Transform3d texture_frame = Transform3d::Identity(); }; struct TextureDisplacementPrepareInput diff --git a/src/slic3r/GUI/Jobs/TextureDisplacementPreviewJob.cpp b/src/slic3r/GUI/Jobs/TextureDisplacementPreviewJob.cpp index 6c6018217e..ee17d11c8e 100644 --- a/src/slic3r/GUI/Jobs/TextureDisplacementPreviewJob.cpp +++ b/src/slic3r/GUI/Jobs/TextureDisplacementPreviewJob.cpp @@ -58,7 +58,8 @@ void TextureDisplacementPreviewJob::process(Ctl &ctl) m_results[i].mesh = build_texture_displacement(volume.base_mesh, volume.layers, volume.facets_data, volume.options, still_wanted, color, volume.volume_to_world, nullptr, nullptr, - volume.model_uv.empty() ? nullptr : &volume.model_uv); + volume.model_uv.empty() ? nullptr : &volume.model_uv, + &volume.texture_anchor, volume.resting_drop); if (!still_wanted(0)) { m_results.clear(); return; diff --git a/src/slic3r/GUI/Jobs/TextureDisplacementPreviewJob.hpp b/src/slic3r/GUI/Jobs/TextureDisplacementPreviewJob.hpp index 94ccdb06c1..c8ef0fb168 100644 --- a/src/slic3r/GUI/Jobs/TextureDisplacementPreviewJob.hpp +++ b/src/slic3r/GUI/Jobs/TextureDisplacementPreviewJob.hpp @@ -29,6 +29,9 @@ struct TextureDisplacementPreviewVolume // The part's own UV channel (ModelVolume::texture_uv), so a ModelUV layer previews through the // same layout it will bake through. Empty when the part has none. std::vector model_uv; + // Where the part sits in its object, as TextureDisplacementBakeVolume has it. + Vec3d texture_anchor = Vec3d::Zero(); + float resting_drop = 0.f; }; struct TextureDisplacementPreviewInput diff --git a/tests/libslic3r/test_texture_displacement.cpp b/tests/libslic3r/test_texture_displacement.cpp index 4c3e74d705..d9b3ad3d90 100644 --- a/tests/libslic3r/test_texture_displacement.cpp +++ b/tests/libslic3r/test_texture_displacement.cpp @@ -2424,6 +2424,89 @@ TEST_CASE("TextureDisplacement: moving the model about the plate does not move t CHECK(max_diff < 1e-3f); } +TEST_CASE("Parts anchored at one point carry one continuous texture", "[TextureDisplacement]") +{ + // The same surface, once as a part whose origin is the anchor and once as a part whose own origin + // sits `offset` away from it - the way the bodies of a multibody object are each centred on their + // own origin. Anchored at the same point they must bake the same relief; anchored at their own + // origins, the second is the tiled pattern shifted by `offset`. + const indexed_triangle_set cube = subdivide_mesh_uniform(its_make_cube(10.f, 10.f, 10.f), 1.f, 6); + // Not a multiple of the 4 mm tile, and exact in binary like the grid itself: the checkerboard's steps + // fall right on grid vertices, so a vertex rounded across a step would bake the other side of it. + const Vec3d offset(1.25, 2.5, 0.75); + indexed_triangle_set shifted = cube; + for (Vec3f &v : shifted.vertices) + v -= offset.cast(); + const Transform3d shifted_to_world = Transform3d(Eigen::Translation3d(offset)); + + TextureDisplacementLayer layer; + layer.slot = 0; + layer.image_data = make_checkerboard_png(16, 16); + layer.tiling_scale = 4.f; + layer.depth_mm = 0.5f; + TextureDisplacementFacetsData facets; + facets[0] = paint_whole_mesh(cube); + + const indexed_triangle_set reference = build_texture_displacement(cube, { layer }, facets, classic_options()); + const Vec3d anchor = Vec3d::Zero(); + const indexed_triangle_set anchored = build_texture_displacement(shifted, { layer }, facets, classic_options(), {}, + nullptr, shifted_to_world, nullptr, nullptr, + nullptr, &anchor); + const indexed_triangle_set own_origin = build_texture_displacement(shifted, { layer }, facets, classic_options(), {}, + nullptr, shifted_to_world); + REQUIRE(anchored.vertices.size() == reference.vertices.size()); + REQUIRE(own_origin.vertices.size() == reference.vertices.size()); + + float anchored_diff = 0.f, own_origin_diff = 0.f; + for (size_t i = 0; i < reference.vertices.size(); ++i) { + anchored_diff = std::max(anchored_diff, (anchored.vertices[i] + offset.cast() - reference.vertices[i]).norm()); + own_origin_diff = std::max(own_origin_diff, (own_origin.vertices[i] + offset.cast() - reference.vertices[i]).norm()); + } + CHECK(anchored_diff < 1e-3f); + CHECK(own_origin_diff > 0.1f); +} + +TEST_CASE("A part held above the plate keeps the relief on its underside", "[TextureDisplacement]") +{ + // A fully painted cube displaces its bottom face straight down by the full depth. On its own, that + // relief is clamped to the cube's bottom (see "relief is never driven below the model's resting + // plane"); as a part of an object resting `drop` below it, it is clamped only at the object's plane. + const bool v2 = GENERATE(false, true); + const indexed_triangle_set cube = subdivide_mesh_uniform(its_make_cube(10.f, 10.f, 10.f), 2.f, 6); + + TextureDisplacementFacetsData facets{}; + facets[0] = paint_whole_mesh(cube); + TextureDisplacementLayer layer; + layer.slot = 0; + layer.depth_mm = 2.0f; + layer.tiling_scale = 5.0f; + layer.image_data = make_flat_gray_png(255); // full depth everywhere + + TextureDisplacementOptions options = classic_options(); + options.pipeline_v2 = v2; + options.v2_refine_mm = 2.f; + options.v2_max_triangles_k = 0; + + const auto lowest_after = [&](float drop) { + const indexed_triangle_set out = build_texture_displacement(cube, { layer }, facets, options, {}, nullptr, + Transform3d::Identity(), nullptr, nullptr, nullptr, + nullptr, drop); + REQUIRE_FALSE(out.vertices.empty()); + float lowest = std::numeric_limits::max(); + for (const Vec3f &v : out.vertices) + lowest = std::min(lowest, v.z()); + return lowest; + }; + + CAPTURE(v2); + // The cube's bottom is z = 0. With the object resting 5 mm lower, the 2 mm relief fits above it... + CHECK_THAT(lowest_after(5.f), WithinAbs(-2.0f, 1e-3f)); + // ...with the object resting 1 mm lower, it is clamped at that plane... + CHECK_THAT(lowest_after(1.f), WithinAbs(-1.0f, 1e-3f)); + // ...and as the lowest part, at its own bottom. + CHECK_THAT(lowest_after(0.f), WithinAbs(0.0f, 1e-3f)); +} + // Longest edge over the shortest altitude: 1.15 for an equilateral triangle, 2 for a right isosceles // one, unbounded for a needle. static float triangle_aspect(const Vec3f &a, const Vec3f &b, const Vec3f &c)