BRL-CAD
Loading...
Searching...
No Matches
pullback.h
Go to the documentation of this file.
1/* P U L L B A C K . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2026 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21/** @addtogroup brep_pullback
22 *
23 * @brief
24 * point pullback.
25 *
26 */
27
28#ifndef BREP_PULLBACK_H
29#define BREP_PULLBACK_H
30
31#include "common.h"
32#include "brep/defines.h"
33#include "brep/surfacetree.h"
34
35/** @{ */
36/** @file brep/pullback.h */
37
38#ifdef __cplusplus
39
40# include <memory>
41
43
44extern "C++" {
45
46namespace brlcad {
47class PullbackContext;
48
84}
85
92
93typedef struct pbc_data {
94 double tolerance;
95 double flatness;
98 /** Immutable surface parameterization facts cached for the numerical job.
99 * ON_NurbsSurface::IsClosed() constructs and compares boundary curves, so
100 * it must not be repeated for every projected sample. */
102 bool surface_closed[2] = {false, false};
108 /** Job-local closest-point cache. It never owns STEP or geometry data. */
110 /** Typed result from the bounded point-projection stage. */
112 /** A speculative caller needs only to establish that its exact candidate
113 * cannot be constructed. Once continuity-seeded and global projection
114 * have both rejected a sample, stop walking that direction instead of
115 * spending the caller's complete budget quantifying an already decisive
116 * failure. Ordinary and repair pullbacks retain exhaustive sampling. */
120 /** Rejected samples for which the closest-point solver found no finite
121 * candidate. These are solver failures, not evidence that the source
122 * curve lies outside its declared surface tolerance. */
124 /** Closed-curve seam transitions for which the local root finder did not
125 * locate an exact split. Callers may defer these to loop-level seam
126 * resolution, but must report the aggregate rather than printing inside
127 * this low-level numerical routine. */
130 /** Every stored UV was produced from a known source-curve parameter and
131 * its 3-D lift passed the active projection tolerance. Importers may
132 * consume this proof before any seam/topology routine mutates the samples;
133 * subsequent validation must use the represented curve locus again. */
135 bool tolerance_adjusted = false;
136 double declared_tolerance = 0.0;
137 /** Optional caller-authorized search bound used only when periodic seam
138 * handling collapses an otherwise valid pullback to one UV sample. The
139 * recovery still records the largest measured lift error separately, so
140 * callers can install a measured tolerance rather than this search bound.
141 * Generic callers retain strict behavior because this defaults to the
142 * ordinary pullback tolerance. */
145 /** Loop-level proof selected this edge boundary as the unique periodic
146 * cut to a surface pole. Endpoint closure must preserve this pair until
147 * the exact seam-edge and singular trims are emitted. */
151
152extern BREP_EXPORT int IsAtSingularity(const ON_Surface *surf, double u, double v, double tol = 1e-6);
153extern BREP_EXPORT int IsAtSingularity(const ON_Surface *surf, const ON_2dPoint &pt, double tol = 1e-6);
154extern BREP_EXPORT int IsAtSeam(const ON_Surface *surf,int dir,double u, double v,double tol = 0.0);
155extern BREP_EXPORT int IsAtSeam(const ON_Surface *surf,double u, double v,double tol = 0.0);
156extern BREP_EXPORT int IsAtSeam(const ON_Surface *surf,int dir,const ON_2dPoint &pt,double tol = 0.0);
157extern BREP_EXPORT int IsAtSeam(const ON_Surface *surf,const ON_2dPoint &pt,double tol = 0.0);
158extern BREP_EXPORT ON_2dPoint UnwrapUVPoint(const ON_Surface *surf,const ON_2dPoint &pt,double tol = 0.0);
159extern BREP_EXPORT double DistToNearestClosedSeam(const ON_Surface *surf,const ON_2dPoint &pt);
160extern BREP_EXPORT void SwapUVSeamPoint(const ON_Surface *surf,ON_2dPoint &p, int hint = 3);
161extern BREP_EXPORT void ForceToClosestSeam(const ON_Surface *surf,ON_2dPoint &pt,double tol= 0.0);
163extern BREP_EXPORT bool FindTrimSeamCrossing(const ON_BrepTrim &trim,double t0,double t1,double &seam_t,ON_2dPoint &from,ON_2dPoint &to,double tol = 0.0);
166extern BREP_EXPORT bool ConsecutivePointsCrossClosedSeam(const ON_Surface *surf,const ON_2dPoint &pt,const ON_2dPoint &prev_pt, int &udir, int &vdir,double tol = 1e-6);
167
169 const ON_Curve *curve, double tolerance = 1.0e-6,
170 double flatness = 1.0e-3,
175 bool stop_after_first_rejection = false);
176
178
179extern BREP_EXPORT int
181
182namespace brlcad {
183
184 class PullbackWorkBudget;
186
187 /** Create a CPU-work budget shared by all threads assisting one geometry
188 * job. The budget follows the longest sequential worker path, rather than
189 * charging time while workers are descheduled or summing genuinely
190 * parallel work. A zero limit returns an unlimited handle. */
193 /** Return the calling thread's current job budget so nested helper threads
194 * can participate in the same limit. */
196
197 /** Install thread-local cancellation, CPU-work, and no-progress limits for
198 * the current geometry job. A zero work limit disables that timer. */
199 typedef bool (*PullbackCancellationCallback)(void *context);
205 /** Join an existing job budget from a nested helper thread. */
215 /** Record completion of one or more bounded solver operations. This
216 * heartbeat is independent of elapsed item deadlines and therefore keeps
217 * --no-item-budget runs cancellable when an algorithm stops advancing. */
219 /** Propagate a helper thread's stop reason to the parent geometry job. */
221 bool deadline_expired, bool stalled);
222 /** Milliseconds remaining on the calling thread's CPU-work budget.
223 * UINT64_MAX means no deadline is installed. This permits a bounded
224 * parent conversion job to propagate its original budget to helper
225 * threads without restarting the per-item limit. */
227
228 /**
229 * Mutable state used by one pullback job. Solver state and statistics are
230 * private; immutable span/bounding-box preparation may be shared explicitly
231 * by related jobs and remains bounded by their context lifetimes.
232 */
234 public:
237
240
242 const ON_3dPoint& point,
245 double& distance,
246 int quadrant = 0,
249
250 /** Refine a closest point from a known adjacent UV. This is the
251 * continuity fast path used between successive samples of one edge;
252 * the caller remains responsible for applying its lift tolerance. */
254 const ON_3dPoint& point,
255 const ON_2dPoint& seed,
258 double& distance,
259 double tolerance,
260 const bool *cached_closed = NULL,
262 double refinement_tolerance = 0.0);
263
265
266 /**
267 * Create an independent pullback job which shares only this context's
268 * lazily constructed, immutable surface-search cache. Closest-point
269 * state and statistics remain private to the returned context, so the
270 * jobs may run concurrently on the same read-only surface.
271 */
273
274 private:
275 struct Impl;
277 };
278
280 PullbackContext &context,
281 const ON_Surface *surf,
282 const ON_3dPoint& point,
285 double& distance,
286 int quadrant = 0,
289
290 /**
291 * approach:
292 *
293 * - get an estimate using the surface tree (if non-null, create
294 * one otherwise)
295 *
296 * - find a point (u, v) for which S(u, v) is closest to _point_
297 * _ __
298 * -- minimize the distance function: D(u, v) = sqrt(|S(u, v)-pt|^2)
299 * _ __
300 * -- simplify by minimizing f(u, v) = |S(u, v)-pt|^2
301 *
302 * -- minimum occurs when the gradient is zero, i.e.
303 * \f[ \nabla f(u, v) = |\vec{S}(u, v)-\vec{p}|^2 = 0 \f]
304 */
306 const ON_BrepFace& face,
307 const ON_3dPoint& point,
308 const SurfaceTree* tree = NULL,
309 double tolerance = BREP_FCP_ROOT_EPSILON);
310
311 /**
312 * Pull an arbitrary model-space *curve* onto the given *surface* as a
313 * curve within the surface's domain when, for each point c = C(t) on
314 * the curve and the closest point s = S(u, v) on the surface, we
315 * have: distance(c, s) <= *tolerance*.
316 *
317 * The resulting 2-dimensional curve will be approximated using the
318 * following process:
319 *
320 * 1. Adaptively sample the 3d curve in the domain of the surface
321 * (ensure tolerance constraint). Sampling terminates when the
322 * following flatness criterion is met:
323 *
324 * given two parameters on the curve t1 and t2 (which map to points p1
325 * and p2 on the curve) let m be a parameter randomly chosen near the
326 * middle of the interval [t1, t2] ____ then the curve between t1 and
327 * t2 is flat if distance(C(m), p1p2) < flatness
328 *
329 * 2. Use the sampled points to perform a global interpolation using
330 * universal knot generation to build a B-Spline curve.
331 *
332 * 3. If the curve is a line or an arc (determined with openNURBS
333 * routines), return the appropriate ON_Curve subclass (otherwise,
334 * return an ON_NurbsCurve).
335 */
337 const ON_Curve *curve,
339 double tolerance = BREP_FCP_ROOT_EPSILON,
340 double flatness = 1.0e-3);
341} /* end namespace brlcad */
342
343
344extern BREP_EXPORT bool
346 double *t,
347 const ON_NurbsCurve *nc,
348 const ON_3dPoint &p,
349 double maximum_distance = 0.0,
351 );
352
353
354bool
356 double *t,
357 const ON_BrepTrim *trim,
358 const ON_3dPoint &p,
359 double maximum_distance = 0.0,
361 );
362
363extern BREP_EXPORT bool
365 double *dist,
366 double *t,
367 const ON_NurbsCurve *nc,
368 const ON_Line &l,
369 double maximum_distance = 0.0,
370 const ON_Interval *subdomain = NULL
371 );
372
373} /* extern C++ */
374
376
377#endif
378
379/** @} */
380
381#endif /* BREP_PULLBACK_H */
382
383/*
384 * Local Variables:
385 * mode: C
386 * tab-width: 8
387 * indent-tabs-mode: t
388 * c-file-style: "stroustrup"
389 * End:
390 * ex: shiftwidth=4 tabstop=8
391 */
bool SurfaceClosestPointFromSeed(const ON_Surface *surf, const ON_3dPoint &point, const ON_2dPoint &seed, ON_2dPoint &surface_point, ON_3dPoint &lifted_point, double &distance, double tolerance, const bool *cached_closed=NULL, const ON_Interval *cached_domains=NULL, double refinement_tolerance=0.0)
PullbackContext(const PullbackContext &)=delete
std::shared_ptr< PullbackContext > ForkWithSharedSurfaceCache() const
PullbackContext & operator=(const PullbackContext &)=delete
bool SurfaceClosestPoint(const ON_Surface *surf, const ON_3dPoint &point, ON_2dPoint &surface_point, ON_3dPoint &lifted_point, double &distance, int quadrant=0, double same_point_tol=BREP_SAME_POINT_TOLERANCE, double within_distance_tol=BREP_EDGE_MISS_TOLERANCE)
PullbackStatistics Statistics() const
Definition dvec.h:74
dvec(double s)
Header file for the BRL-CAD common definitions.
#define BREP_SAME_POINT_TOLERANCE
Definition defines.h:180
#define BREP_EDGE_MISS_TOLERANCE
Definition defines.h:178
#define BREP_FCP_ROOT_EPSILON
Definition defines.h:156
double DistToNearestClosedSeam(const ON_Surface *surf, const ON_2dPoint &pt)
void SwapUVSeamPoint(const ON_Surface *surf, ON_2dPoint &p, int hint=3)
bool ConsecutivePointsCrossClosedSeam(const ON_Surface *surf, const ON_2dPoint &pt, const ON_2dPoint &prev_pt, int &udir, int &vdir, double tol=1e-6)
bool ON_NurbsCurve_GetClosestPoint(double *t, const ON_NurbsCurve *nc, const ON_3dPoint &p, double maximum_distance=0.0, const ON_Interval *sub_domain=NULL)
bool Find3DCurveSeamCrossing(PBCData &data, double t0, double t1, double offset, double &seam_t, ON_2dPoint &from, ON_2dPoint &to, double tol=0.0, double same_point_tol=BREP_SAME_POINT_TOLERANCE, double within_distance_tol=BREP_EDGE_MISS_TOLERANCE)
int check_pullback_singularity_bridge(const ON_Surface *surf, const ON_2dPoint &p1, const ON_2dPoint &p2)
ON_2dPoint UnwrapUVPoint(const ON_Surface *surf, const ON_2dPoint &pt, double tol=0.0)
PullbackFailureReason
Definition pullback.h:86
int IsAtSingularity(const ON_Surface *surf, double u, double v, double tol=1e-6)
int IsAtSeam(const ON_Surface *surf, int dir, double u, double v, double tol=0.0)
bool FindTrimSeamCrossing(const ON_BrepTrim &trim, double t0, double t1, double &seam_t, ON_2dPoint &from, ON_2dPoint &to, double tol=0.0)
PBCData * pullback_samples(const ON_Surface *surf, const ON_Curve *curve, double tolerance=1.0e-6, double flatness=1.0e-3, double same_point_tol=BREP_SAME_POINT_TOLERANCE, double within_distance_tol=BREP_EDGE_MISS_TOLERANCE, const std::shared_ptr< brlcad::PullbackContext > &context=std::shared_ptr< brlcad::PullbackContext >(), bool stop_after_first_rejection=false)
bool trim_GetClosestPoint3dFirstOrder(const ON_BrepTrim &trim, const ON_3dPoint &p, ON_2dPoint &p2d, double &t, double &distance, const ON_Interval *interval, double same_point_tol=BREP_SAME_POINT_TOLERANCE, double within_distance_tol=BREP_EDGE_MISS_TOLERANCE)
bool ON_NurbsCurve_ClosestPointToLineSegment(double *dist, double *t, const ON_NurbsCurve *nc, const ON_Line &l, double maximum_distance=0.0, const ON_Interval *subdomain=NULL)
struct pbc_data PBCData
bool check_pullback_data(std::list< PBCData * > &pbcs)
bool surface_GetClosestPoint3dFirstOrder(const ON_Surface *surf, const ON_3dPoint &p, ON_2dPoint &p2d, ON_3dPoint &p3d, double &current_distance, int quadrant=0, double same_point_tol=BREP_SAME_POINT_TOLERANCE, double within_distance_tol=BREP_EDGE_MISS_TOLERANCE)
void ForceToClosestSeam(const ON_Surface *surf, ON_2dPoint &pt, double tol=0.0)
bool ON_TrimCurve_GetClosestPoint(double *t, const ON_BrepTrim *trim, const ON_3dPoint &p, double maximum_distance=0.0, const ON_Interval *sub_domain=NULL)
void PullbackWorkProgress(uint64_t operations=1)
std::shared_ptr< PullbackWorkBudget > PullbackWorkBudgetHandle
Definition pullback.h:185
bool PullbackWorkStalled()
bool(* PullbackCancellationCallback)(void *context)
Definition pullback.h:199
PullbackWorkBudgetHandle CreatePullbackWorkBudget(uint64_t maximum_work_milliseconds)
void PropagatePullbackWorkStop(bool deadline_expired, bool stalled)
ON_Curve * pullback_curve(ON_BrepFace *face, const ON_Curve *curve, SurfaceTree *tree=NULL, double tolerance=BREP_FCP_ROOT_EPSILON, double flatness=1.0e-3)
void ClearPullbackWorkLimit()
bool surface_GetClosestPoint3dFirstOrder(PullbackContext &context, const ON_Surface *surf, const ON_3dPoint &point, ON_2dPoint &surface_point, ON_3dPoint &lifted_point, double &distance, int quadrant=0, double same_point_tol=BREP_SAME_POINT_TOLERANCE, double within_distance_tol=BREP_EDGE_MISS_TOLERANCE)
PullbackWorkBudgetHandle CurrentPullbackWorkBudget()
bool PullbackWorkDeadlineExpired()
void SetPullbackWorkLimit(PullbackCancellationCallback cancellation_callback, void *cancellation_context, uint64_t maximum_work_milliseconds, uint64_t maximum_stall_milliseconds=0)
bool PullbackWorkCancelled()
uint64_t PullbackWorkRemainingMilliseconds()
bool get_closest_point(ON_2dPoint &outpt, const ON_BrepFace &face, const ON_3dPoint &point, const SurfaceTree *tree=NULL, double tolerance=BREP_FCP_ROOT_EPSILON)
uint64_t fallback_seed_refinements
Definition pullback.h:69
double fallback_refinement_improvement_total
Definition pullback.h:81
uint64_t continuity_seed_successes
Definition pullback.h:57
uint64_t maximum_subdivision_nodes
Definition pullback.h:74
uint64_t continuity_seed_iterations
Definition pullback.h:60
uint64_t continuity_seed_finite_candidates
Definition pullback.h:59
uint64_t continuity_seed_searches
Definition pullback.h:56
uint64_t continuity_seed_line_searches
Definition pullback.h:61
uint64_t fallback_refinement_improvements
Definition pullback.h:70
uint64_t continuity_seed_failures
Definition pullback.h:58
uint64_t fallback_samples_evaluated
Definition pullback.h:68
uint64_t fallback_calls_with_finite_primary
Definition pullback.h:67
double fallback_primary_improvement_total
Definition pullback.h:79
uint64_t maximum_continuity_seed_iterations
Definition pullback.h:62
double fallback_primary_improvement_maximum
Definition pullback.h:80
double fallback_refinement_improvement_maximum
Definition pullback.h:82
uint64_t maximum_winning_seed_index
Definition pullback.h:72
uint64_t maximum_continuity_seed_line_searches
Definition pullback.h:63
uint64_t fallback_late_seed_improvements
Definition pullback.h:71
uint64_t primary_search_successes
Definition pullback.h:55
NMG topological face.
Definition topology.h:210
PullbackFailureReason failure_reason
Definition pullback.h:111
double maximum_recovery_distance
Definition pullback.h:144
size_t rejected_projection_samples
Definition pullback.h:119
double declared_tolerance
Definition pullback.h:136
bool periodic_pole_cut_before
Definition pullback.h:148
const ON_BrepEdge * edge
Definition pullback.h:106
bool periodic_pole_cut_after
Definition pullback.h:149
double maximum_recovery_tolerance
Definition pullback.h:143
double maximum_projection_distance
Definition pullback.h:129
bool surface_parameterization_cached
Definition pullback.h:101
std::list< ON_2dPointArray * > * segments
Definition pullback.h:105
bool surface_closed[2]
Definition pullback.h:102
std::shared_ptr< brlcad::PullbackContext > context
Definition pullback.h:109
const ON_Surface * surf
Definition pullback.h:97
bool order_reversed
Definition pullback.h:107
bool tolerance_adjusted
Definition pullback.h:135
ON_Interval surface_domain[2]
Definition pullback.h:103
size_t projection_samples
Definition pullback.h:118
bool stop_after_first_rejection
Definition pullback.h:117
double tolerance
Definition pullback.h:94
size_t failed_seam_crossing_searches
Definition pullback.h:128
const ON_Curve * curve
Definition pullback.h:96
size_t failed_projection_samples
Definition pullback.h:123
bool samples_source_validated
Definition pullback.h:134
double flatness
Definition pullback.h:95
brlcad::SurfaceTree * surftree
Definition pullback.h:104
Definition tree.h:166