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# include <string>
42
44
45extern "C++" {
46
47namespace brlcad {
48class PullbackContext;
49
85}
86
94
95typedef struct pbc_data {
96 double tolerance;
97 double flatness;
100 /** Immutable surface parameterization facts cached for the numerical job.
101 * ON_NurbsSurface::IsClosed() constructs and compares boundary curves, so
102 * it must not be repeated for every projected sample. */
104 bool surface_closed[2] = {false, false};
110 /** Job-local closest-point cache. It never owns STEP or geometry data. */
112 /** Typed result from the bounded point-projection stage. */
114 /** A speculative caller needs only to establish that its exact candidate
115 * cannot be constructed. Once continuity-seeded and global projection
116 * have both rejected a sample, stop walking that direction instead of
117 * spending the caller's complete budget quantifying an already decisive
118 * failure. Ordinary and repair pullbacks retain exhaustive sampling. */
122 /** Rejected samples for which the closest-point solver found no finite
123 * candidate. These are solver failures, not evidence that the source
124 * curve lies outside its declared surface tolerance. */
126 /** Closed-curve seam transitions for which the local root finder did not
127 * locate an exact split. Callers may defer these to loop-level seam
128 * resolution, but must report the aggregate rather than printing inside
129 * this low-level numerical routine. */
132 /** Every stored UV was produced from a known source-curve parameter and
133 * its 3-D lift passed the active projection tolerance. Importers may
134 * consume this proof before any seam/topology routine mutates the samples;
135 * subsequent validation must use the represented curve locus again. */
137 bool tolerance_adjusted = false;
138 double declared_tolerance = 0.0;
139 /** Optional caller-authorized search bound used only when periodic seam
140 * handling collapses an otherwise valid pullback to one UV sample. The
141 * recovery still records the largest measured lift error separately, so
142 * callers can install a measured tolerance rather than this search bound.
143 * Generic callers retain strict behavior because this defaults to the
144 * ordinary pullback tolerance. */
147 /** Loop-level proof selected this edge boundary as the unique periodic
148 * cut to a surface pole. Endpoint closure must preserve this pair until
149 * the exact seam-edge and singular trims are emitted. */
153
154extern BREP_EXPORT int IsAtSingularity(const ON_Surface *surf, double u, double v, double tol = 1e-6);
155extern BREP_EXPORT int IsAtSingularity(const ON_Surface *surf, const ON_2dPoint &pt, double tol = 1e-6);
156extern BREP_EXPORT int IsAtSeam(const ON_Surface *surf,int dir,double u, double v,double tol = 0.0);
157extern BREP_EXPORT int IsAtSeam(const ON_Surface *surf,double u, double v,double tol = 0.0);
158extern BREP_EXPORT int IsAtSeam(const ON_Surface *surf,int dir,const ON_2dPoint &pt,double tol = 0.0);
159extern BREP_EXPORT int IsAtSeam(const ON_Surface *surf,const ON_2dPoint &pt,double tol = 0.0);
160extern BREP_EXPORT ON_2dPoint UnwrapUVPoint(const ON_Surface *surf,const ON_2dPoint &pt,double tol = 0.0);
161extern BREP_EXPORT double DistToNearestClosedSeam(const ON_Surface *surf,const ON_2dPoint &pt);
162extern BREP_EXPORT void SwapUVSeamPoint(const ON_Surface *surf,ON_2dPoint &p, int hint = 3);
163extern BREP_EXPORT void ForceToClosestSeam(const ON_Surface *surf,ON_2dPoint &pt,double tol= 0.0);
165extern 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);
168extern 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);
169
171 const ON_Curve *curve, double tolerance = 1.0e-6,
172 double flatness = 1.0e-3,
177 bool stop_after_first_rejection = false);
178
180
181extern BREP_EXPORT int
183
184namespace brlcad {
185
186 class PullbackWorkBudget;
188
189 /** Create a CPU-work budget shared by all threads assisting one geometry
190 * job. The budget follows the longest sequential worker path, rather than
191 * charging time while workers are descheduled or summing genuinely
192 * parallel work. A zero limit returns an unlimited handle. */
195 /** Return the calling thread's current job budget so nested helper threads
196 * can participate in the same limit. */
198
199 /** Install thread-local cancellation, CPU-work, and no-progress limits for
200 * the current geometry job. A zero work limit disables that timer. */
201 typedef bool (*PullbackCancellationCallback)(void *context);
207 /** Join an existing job budget from a nested helper thread. */
217 /** Record completion of one or more bounded solver operations. This
218 * heartbeat is independent of elapsed item deadlines and therefore keeps
219 * --no-item-budget runs cancellable when an algorithm stops advancing. */
221 /** Propagate a helper thread's stop reason to the parent geometry job. */
223 bool deadline_expired, bool stalled);
224 /** Milliseconds remaining on the calling thread's CPU-work budget.
225 * UINT64_MAX means no deadline is installed. This permits a bounded
226 * parent conversion job to propagate its original budget to helper
227 * threads without restarting the per-item limit. */
229
230 /**
231 * Mutable state used by one pullback job. Solver state and statistics are
232 * private; immutable span/bounding-box preparation may be shared explicitly
233 * by related jobs and remains bounded by their context lifetimes.
234 */
236 public:
239
242
244 const ON_3dPoint& point,
247 double& distance,
248 int quadrant = 0,
251
252 /** Refine a closest point from a known adjacent UV. This is the
253 * continuity fast path used between successive samples of one edge;
254 * the caller remains responsible for applying its lift tolerance. */
256 const ON_3dPoint& point,
257 const ON_2dPoint& seed,
260 double& distance,
261 double tolerance,
262 const bool *cached_closed = NULL,
264 double refinement_tolerance = 0.0);
265
267
268 /**
269 * Create an independent pullback job which shares only this context's
270 * lazily constructed, immutable surface-search cache. Closest-point
271 * state and statistics remain private to the returned context, so the
272 * jobs may run concurrently on the same read-only surface.
273 */
275
276 private:
277 struct Impl;
279 };
280
282 PullbackContext &context,
283 const ON_Surface *surf,
284 const ON_3dPoint& point,
287 double& distance,
288 int quadrant = 0,
291
292 /**
293 * approach:
294 *
295 * - get an estimate using the surface tree (if non-null, create
296 * one otherwise)
297 *
298 * - find a point (u, v) for which S(u, v) is closest to _point_
299 * _ __
300 * -- minimize the distance function: D(u, v) = sqrt(|S(u, v)-pt|^2)
301 * _ __
302 * -- simplify by minimizing f(u, v) = |S(u, v)-pt|^2
303 *
304 * -- minimum occurs when the gradient is zero, i.e.
305 * \f[ \nabla f(u, v) = |\vec{S}(u, v)-\vec{p}|^2 = 0 \f]
306 */
308 const ON_BrepFace& face,
309 const ON_3dPoint& point,
310 const SurfaceTree* tree = NULL,
311 double tolerance = BREP_FCP_ROOT_EPSILON);
312
313 /**
314 * Pull an arbitrary model-space *curve* onto the given *surface* as a
315 * curve within the surface's domain when, for each point c = C(t) on
316 * the curve and the closest point s = S(u, v) on the surface, we
317 * have: distance(c, s) <= *tolerance*.
318 *
319 * The resulting 2-dimensional curve will be approximated using the
320 * following process:
321 *
322 * 1. Adaptively sample the 3d curve in the domain of the surface
323 * (ensure tolerance constraint). Sampling terminates when the
324 * following flatness criterion is met:
325 *
326 * given two parameters on the curve t1 and t2 (which map to points p1
327 * and p2 on the curve) let m be a parameter randomly chosen near the
328 * middle of the interval [t1, t2] ____ then the curve between t1 and
329 * t2 is flat if distance(C(m), p1p2) < flatness
330 *
331 * 2. Join the validated samples on a continuous image of any periodic
332 * surface domains.
333 *
334 * 3. Construct a two-dimensional polyline and densely validate its lifted
335 * locus against the source curve.
336 */
338 const ON_Curve *curve,
339 double tolerance = BREP_FCP_ROOT_EPSILON,
340 double flatness = 1.0e-3,
341 std::string *failure_reason = NULL,
343
345 const ON_Curve *curve,
347 double tolerance = BREP_FCP_ROOT_EPSILON,
348 double flatness = 1.0e-3,
349 std::string *failure_reason = NULL,
351} /* end namespace brlcad */
352
353
354extern BREP_EXPORT bool
356 double *t,
357 const ON_NurbsCurve *nc,
358 const ON_3dPoint &p,
359 double maximum_distance = 0.0,
361 );
362
363
364bool
366 double *t,
367 const ON_BrepTrim *trim,
368 const ON_3dPoint &p,
369 double maximum_distance = 0.0,
371 );
372
373extern BREP_EXPORT bool
375 double *dist,
376 double *t,
377 const ON_NurbsCurve *nc,
378 const ON_Line &l,
379 double maximum_distance = 0.0,
380 const ON_Interval *subdomain = NULL
381 );
382
383} /* extern C++ */
384
386
387#endif
388
389/** @} */
390
391#endif /* BREP_PULLBACK_H */
392
393/*
394 * Local Variables:
395 * mode: C
396 * tab-width: 8
397 * indent-tabs-mode: t
398 * c-file-style: "stroustrup"
399 * End:
400 * ex: shiftwidth=4 tabstop=8
401 */
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:87
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:187
bool PullbackWorkStalled()
bool(* PullbackCancellationCallback)(void *context)
Definition pullback.h:201
PullbackWorkBudgetHandle CreatePullbackWorkBudget(uint64_t maximum_work_milliseconds)
void PropagatePullbackWorkStop(bool deadline_expired, bool stalled)
ON_Curve * pullback_curve(const ON_Surface *surface, const ON_Curve *curve, double tolerance=BREP_FCP_ROOT_EPSILON, double flatness=1.0e-3, std::string *failure_reason=NULL, PullbackFailureReason *failure=NULL)
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:70
double fallback_refinement_improvement_total
Definition pullback.h:82
uint64_t continuity_seed_successes
Definition pullback.h:58
uint64_t maximum_subdivision_nodes
Definition pullback.h:75
uint64_t continuity_seed_iterations
Definition pullback.h:61
uint64_t continuity_seed_finite_candidates
Definition pullback.h:60
uint64_t continuity_seed_searches
Definition pullback.h:57
uint64_t continuity_seed_line_searches
Definition pullback.h:62
uint64_t fallback_refinement_improvements
Definition pullback.h:71
uint64_t continuity_seed_failures
Definition pullback.h:59
uint64_t fallback_samples_evaluated
Definition pullback.h:69
uint64_t fallback_calls_with_finite_primary
Definition pullback.h:68
double fallback_primary_improvement_total
Definition pullback.h:80
uint64_t maximum_continuity_seed_iterations
Definition pullback.h:63
double fallback_primary_improvement_maximum
Definition pullback.h:81
double fallback_refinement_improvement_maximum
Definition pullback.h:83
uint64_t maximum_winning_seed_index
Definition pullback.h:73
uint64_t maximum_continuity_seed_line_searches
Definition pullback.h:64
uint64_t fallback_late_seed_improvements
Definition pullback.h:72
uint64_t primary_search_successes
Definition pullback.h:56
NMG topological face.
Definition topology.h:210
PullbackFailureReason failure_reason
Definition pullback.h:113
double maximum_recovery_distance
Definition pullback.h:146
size_t rejected_projection_samples
Definition pullback.h:121
double declared_tolerance
Definition pullback.h:138
bool periodic_pole_cut_before
Definition pullback.h:150
const ON_BrepEdge * edge
Definition pullback.h:108
bool periodic_pole_cut_after
Definition pullback.h:151
double maximum_recovery_tolerance
Definition pullback.h:145
double maximum_projection_distance
Definition pullback.h:131
bool surface_parameterization_cached
Definition pullback.h:103
std::list< ON_2dPointArray * > * segments
Definition pullback.h:107
bool surface_closed[2]
Definition pullback.h:104
std::shared_ptr< brlcad::PullbackContext > context
Definition pullback.h:111
const ON_Surface * surf
Definition pullback.h:99
bool order_reversed
Definition pullback.h:109
bool tolerance_adjusted
Definition pullback.h:137
ON_Interval surface_domain[2]
Definition pullback.h:105
size_t projection_samples
Definition pullback.h:120
bool stop_after_first_rejection
Definition pullback.h:119
double tolerance
Definition pullback.h:96
size_t failed_seam_crossing_searches
Definition pullback.h:130
const ON_Curve * curve
Definition pullback.h:98
size_t failed_projection_samples
Definition pullback.h:125
bool samples_source_validated
Definition pullback.h:136
double flatness
Definition pullback.h:97
brlcad::SurfaceTree * surftree
Definition pullback.h:106
Definition tree.h:166