Something went wrong. Try again.
Builds CAD models from 3d scan data
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172// fit.hpp — primitive fitting from a local mesh patch.#pragma once#include <vector>
#include "geom.hpp"#include "mesh.hpp"
namespace fit {
struct PlaneFit { bool ok = false; geom::Vec3 origin; // centroid of the patch geom::Vec3 normal; // unit, oriented toward the picked side geom::Vec3 u, v; // orthonormal in-plane basis double rms = 0; // fit residual (mm) int npts = 0;};
struct CylinderFit { bool ok = false; geom::Vec3 axisPoint; // a point on the axis (near patch centroid) geom::Vec3 axisDir; // unit double radius = 0; double diameter = 0; // axis extent of the supporting points (projection onto axisDir, relative to axisPoint) double tMin = 0, tMax = 0; // depth = tMax - tMin geom::Vec3 p1, p2; // axis endpoints spanning the supporting points double rms = 0; // radial residual (mm) int npts = 0; bool radiusFixed = false; // true if caller supplied a known diameter};
// ---- point-set fitters (used by the discovery engine on arbitrary clusters) ----
// Fit a plane to a point set. `nrm` (parallel to `pts`, may be empty) orients the// normal; otherwise `viewDir` is used.PlaneFit fitPlanePoints(const std::vector<geom::Vec3>& pts, const std::vector<geom::Vec3>& nrm, const geom::Vec3& viewDir);
// Fit a cylinder to a point set with parallel normals. `knownDiameter > 0` fixes// the radius; `axisHint` (non-zero) seeds the axis and the wall-point filter.CylinderFit fitCylinderPoints(std::vector<geom::Vec3> pts, std::vector<geom::Vec3> nrm, double knownDiameter, const geom::Vec3& axisHint);
// ---- mesh radius-query fitters (used by interactive picking) ----
// Fit a plane to mesh vertices within `radius` of `seed`.// `viewDir` (optional, may be zero) orients the normal toward the viewer/pick side.PlaneFit fitPlane(const mesh::Mesh& m, const geom::Vec3& seed, double radius, const geom::Vec3& viewDir);
// Fit a plane to mesh vertices within `radius` of `seed`, restricted to a slab of// half-thickness `slabHalf` around the plane through `seed` normal to `axis`. Use// instead of fitPlane when the spherical query would swallow the far side of a// plate (e.g. refitting a mount face at a through hole). Normal keeps `axis` sense.// `supportPts`/`supportNrm` (optional) receive the points the fit used, so callers// can pool patches from several fits into one joint plane (coplanar snapping).PlaneFit fitPlaneSlab(const mesh::Mesh& m, const geom::Vec3& seed, double radius, const geom::Vec3& axis, double slabHalf, std::vector<geom::Vec3>* supportPts = nullptr, std::vector<geom::Vec3>* supportNrm = nullptr);
// Which side of the seat plane holds the scanned material? Counts mesh vertices in// a cylinder around `axis` through `seat` (radial distance <= radius, axial offset// within ±height, excluding a thin band at the plane itself). Returns +1 if the// material lies along +axis, -1 if along -axis, 0 if ambiguous or no data.int materialSide(const mesh::Mesh& m, const geom::Vec3& seat, const geom::Vec3& axis, double radius, double height);
// Fit a cylinder to mesh vertices within `radius` of `seed`.// If knownDiameter > 0, the radius is held fixed at that value and only the// axis/center are optimized (more robust; use for caliper/thread specs).// `axisHint` (e.g. the datum-plane normal) is used, when non-zero, to seed the// axis and reject flat-face points — far more robust on off-centre picks.CylinderFit fitCylinder(const mesh::Mesh& m, const geom::Vec3& seed, double radius, double knownDiameter, const geom::Vec3& axisHint);
// ---- seat-plane fitters (two-step hole picking) ----
struct SeatFit : PlaneFit { int ncloud = 0; // auxiliary point-cloud points merged into the final fit};
// Fit the surface a hole pierces from a screen-drag box: `region` is 4 half-space// planes [nx,ny,nz,d] (inside ⇔ ≤ 0 on all). Keeps the camera-facing (`viewDir`// points toward the viewer) dominant surface in the column, then trim-refits,// merging `cloud` points that lie near the plane.SeatFit fitPlaneRegion(const mesh::Mesh& m, const std::vector<geom::Vec3>& cloud, const std::vector<std::array<double, 4>>& region, const geom::Vec3& viewDir);
// Fit the seat plane from 3 user-picked points on the hole rim, refined against// mesh + cloud points near the rim plane (the face annulus around the opening).SeatFit fitPlaneRim(const mesh::Mesh& m, const std::vector<geom::Vec3>& cloud, const geom::Vec3& a, const geom::Vec3& b, const geom::Vec3& c, const geom::Vec3& viewDir);
// Re-solve a seat with its normal locked to `n` (a reference surface the seat// must be parallel to): only the offset is fitted, from the seat's own points.// `seed`/`searchR` select the seat's support points again.SeatFit refitSeatParallel(const mesh::Mesh& m, const std::vector<geom::Vec3>& cloud, const SeatFit& seat, const geom::Vec3& n);
// Fit the seat from 1–3 clicked surface points: seed a plane at the first pick,// grow the connected planar patch across the mesh (adjacency must be built),// trim-refit, and merge cloud points lying on the patch. Extra picks join the// seed set — the user's "refine with more points" path. `patchIds` (optional)// receives the grown patch's vertex indices for UI highlighting.SeatFit fitSeatFromPoints(const mesh::Mesh& m, const std::vector<geom::Vec3>& cloud, const std::vector<geom::Vec3>& picks, const geom::Vec3& viewDir, std::vector<int>* patchIds);
// Fit the seat from a PAINTED selection: `centers` are brush-stroke sample points// (world) and `radius` the brush radius — every mesh vertex within `radius` of any// sample is selected. The plane is fitted from exactly that selection, with// normal-consistency + residual-band passes rejecting brushed-over walls, bore// edges, and outliers, then cloud points on the painted region join the refinement.// No patch growing: the paint IS the selection, so strokes on several disconnected// (coplanar) surfaces define one plane through all of them. `patchIds` (optional)// receives the accepted vertex ids for UI highlighting.SeatFit fitSeatFromPaint(const mesh::Mesh& m, const std::vector<geom::Vec3>& cloud, const std::vector<geom::Vec3>& centers, double radius, const geom::Vec3& viewDir, std::vector<int>* patchIds);
// Fit a cylinder with the axis HARD-LOCKED to `axis` (the seat normal): points// project into the seat plane and only the circle is fitted, so the result can// never pivot off-axis. Cloud points within the radial band join the refinement.// `seatOrigin` (pass nullptr if unknown) is a point on the seat plane: the initial// circle is then seeded from a thin slice just below the seat, where only the bore// wall exists — immune to noisy face normals on dense scans.CylinderFit fitCylinderLocked(const mesh::Mesh& m, const std::vector<geom::Vec3>& cloud, const geom::Vec3& seed, double radius, double knownDiameter, const geom::Vec3& axis, const geom::Vec3* seatOrigin);
// ---- hole detection from the seat plane's data gap ----
struct HoleDetect { bool ok = false; geom::Vec3 center; // on the (locally refined) seat plane geom::Vec3 normal; // locally refined face normal — use as the hole axis double diameter = 0; double coverage = 0; // fraction of angular bins whose gap edge lies on the circle double rms = 0; // radial residual of the gap-edge points int npts = 0; // plane-band points examined (mesh + cloud) double depth = 0; // axial extent of wall evidence below the seat (0 = none seen)};
// Find a hole near `hint` WITHOUT needing bore-wall data: the opening is a gap in// the surface points lying on the seat plane. Per angular bin around the center,// the innermost plane point marks the gap edge; a circle fitted to those edges is// the hole. `knownDiameter > 0` fixes the radius (caliper/thread) and solves only// the center. `minD`/`maxD` (> 0) bound the accepted diameter — a gap outside the// window, or one that isn't round, is rejected rather than snapped.// Works when the scanner never resolved the bore interior.HoleDetect detectHoleInSeat(const mesh::Mesh& m, const std::vector<geom::Vec3>& cloud, const geom::Vec3& seatOrigin, const geom::Vec3& normal, const geom::Vec3& hint, double searchR, double knownDiameter, double minD, double maxD);
// Scan the seat surface (grown from `seedPick`) for circular data gaps in the// diameter window: occupancy-grid the patch, take interior empty regions, and// confirm each with the angular gap-edge circle fit. Returns accepted holes.std::vector<HoleDetect> detectHolesInSeat(const mesh::Mesh& m, const std::vector<geom::Vec3>& cloud, const geom::Vec3& seatOrigin, const geom::Vec3& normal, const geom::Vec3& seedPick, double minD, double maxD);
} // namespace fit