ReUseX  0.0.5
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
sam3_geometry.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Povl Filip Sonne-Frederiksen
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4//
5// Backend-agnostic helpers for SAM3 geometry (box) prompts: the box encoding
6// the geometry encoder expects and the per-prompt token layout the decoder
7// consumes. Standard library only, so they are unit-testable without a GPU.
8
9#pragma once
10
11#include "reusex/vision/sam3_prompt.hpp"
12
13#include <array>
14#include <cstddef>
15#include <cstdint>
16#include <functional>
17#include <optional>
18#include <string>
19#include <string_view>
20#include <utility>
21#include <vector>
22
23namespace reusex::vision {
24
28std::array<float, 4> sam3_box_cxcywh(const std::array<float, 4> &xyxy,
29 int image_w, int image_h);
30
32std::int64_t sam3_box_label(std::string_view polarity) noexcept;
33
37int sam3_geometry_slot_len(int max_boxes) noexcept;
38
45
46std::vector<std::uint8_t> sam3_geometry_slot_mask(int n_boxes, int slot_len);
47
57std::optional<bool>
58sam3_mask_true_is_valid(const std::vector<std::uint8_t> &mask, int real_tokens);
59
62constexpr bool sam3_mask_value(bool valid, bool true_is_valid) noexcept {
63 return valid == true_is_valid;
64}
65
68int sam3_geometry_box_count(std::size_t requested, int capacity) noexcept;
69
76int sam3_geometry_capacity(int model_cap, int encoder_min_boxes,
77 int encoder_max_boxes, int text_len,
78 int decoder_max_len) noexcept;
79
94 const std::array<float, 4> &det_xyxy, const std::vector<SegmentBox> &boxes,
95 const std::vector<std::array<float, 2>> &points = {},
96 const std::function<bool(float, float)> &covers = {});
97
105const std::vector<float> &sam3_point_exemplar_fracs();
106
109std::array<float, 4> sam3_point_exemplar_box(const std::array<float, 2> &point,
110 int image_w, int image_h,
111 float frac);
112
117std::vector<std::size_t>
118sam3_best_detection_per_point(const std::vector<float> &scores,
119 const std::vector<std::vector<bool>> &covers);
120
127std::vector<std::size_t>
128sam3_point_prompt_selection(const std::vector<float> &scores,
129 const std::vector<std::vector<bool>> &covers,
130 const std::vector<bool> &box_selected);
131
138 public:
139 static constexpr int kFailuresBeforeDisable = 3;
140
142 bool record_failure() noexcept {
143 return ++consecutive_ >= kFailuresBeforeDisable;
144 }
145 void record_success() noexcept { consecutive_ = 0; }
146 [[nodiscard]] int consecutive() const noexcept { return consecutive_; }
147
148 private:
149 int consecutive_ = 0;
150};
151
152} // namespace reusex::vision
When a request's geometry path fails at run time (encode_boxes / decode with a geometry slot),...
static constexpr int kFailuresBeforeDisable
bool record_failure() noexcept
Record one failed request; true when geometry should now be disabled.
std::array< float, 4 > sam3_box_cxcywh(const std::array< float, 4 > &xyxy, int image_w, int image_h)
Pixel box [x1, y1, x2, y2] (corners in any order) -> the normalised [cx, cy, w, h] the SAM3 geometry ...
std::vector< std::size_t > sam3_best_detection_per_point(const std::vector< float > &scores, const std::vector< std::vector< bool > > &covers)
Which detections a click selects: for each point, the single highest- scoring detection that covers i...
int sam3_geometry_slot_len(int max_boxes) noexcept
Geometry tokens one prompt slot holds when the largest prompt in a decoder batch has max_boxes boxes:...
std::int64_t sam3_box_label(std::string_view polarity) noexcept
SAM3 box label: 1 for a positive ("pos") box, 0 for a negative one.
std::vector< std::uint8_t > sam3_geometry_slot_mask(int n_boxes, int slot_len)
Validity of one prompt's geometry slot of slot_len tokens holding n_boxes encoded boxes: 1 == a token...
const std::vector< float > & sam3_point_exemplar_fracs()
Exemplar box half-sizes, as fractions of the image's shorter side, a click point is tried at.
int sam3_geometry_capacity(int model_cap, int encoder_min_boxes, int encoder_max_boxes, int text_len, int decoder_max_len) noexcept
Most boxes per prompt the engines can take, or 0 when geometry prompting is unusable.
std::array< float, 4 > sam3_point_exemplar_box(const std::array< float, 2 > &point, int image_w, int image_h, float frac)
The exemplar box [x1, y1, x2, y2] for a click point: half-size frac of the image's shorter side,...
bool sam3_detection_selected(const std::array< float, 4 > &det_xyxy, const std::vector< SegmentBox > &boxes, const std::vector< std::array< float, 2 > > &points={}, const std::function< bool(float, float)> &covers={})
Does a detection with pixel box det_xyxy belong to a box-prompted selection?
int sam3_geometry_box_count(std::size_t requested, int capacity) noexcept
Boxes actually fed for a prompt with requested boxes when the model accepts at most capacity (0 = geo...
constexpr bool sam3_mask_value(bool valid, bool true_is_valid) noexcept
The bool an engine mask holds for a token that is valid, under the polarity sam3_mask_true_is_valid()...
std::vector< std::size_t > sam3_point_prompt_selection(const std::vector< float > &scores, const std::vector< std::vector< bool > > &covers, const std::vector< bool > &box_selected)
The detections a prompt with click points keeps: the UNION of.
std::optional< bool > sam3_mask_true_is_valid(const std::vector< std::uint8_t > &mask, int real_tokens)
Which way an export's bool token masks point, read off the text encoder's own output for an input who...