BRL-CAD
tol.h
Go to the documentation of this file.
1/* T O L . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 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/*----------------------------------------------------------------------*/
22/** @addtogroup bn_tol
23 *
24 * @brief Support for uniform tolerances
25 *
26 * A handy way of passing around the tolerance information needed to
27 * perform approximate floating-point calculations on geometry.
28 *
29 * dist & dist_sq establish the distance tolerance.
30 *
31 * If two points are closer together than dist, then they are to be
32 * considered the same point.
33 *
34 * For example:
35 @code
36 point_t a, b;
37 vect_t diff;
38 VSUB2(diff, a, b);
39 if (MAGNITUDE(diff) < tol->dist) a & b are the same.
40 or, more efficiently:
41 if (MAQSQ(diff) < tol->dist_sq)
42 @endcode
43 * perp & para establish the angular tolerance.
44 *
45 * If two rays emanate from the same point, and their dot product is
46 * nearly one, then the two rays are the same, while if their dot
47 * product is nearly zero, then they are perpendicular.
48 *
49 * For example:
50 @code
51 vect_t a, b;
52 if (fabs(VDOT(a, b)) >= tol->para) a & b are parallel
53 if (fabs(VDOT(a, b)) <= tol->perp) a & b are perpendicular
54 @endcode
55 *
56 *@note
57 * tol->dist_sq = tol->dist * tol->dist;
58 *@n tol->para = 1 - tol->perp;
59 */
60/** @{ */
61/** @file bn/tol.h */
62
63#ifndef BN_TOL_H
64#define BN_TOL_H
65
66#include "common.h"
67#include "bn/defines.h"
68#include "bu/magic.h"
69
70__BEGIN_DECLS
71
72struct bn_tol {
73 uint32_t magic;
74 double dist; /**< @brief >= 0 */
75 double dist_sq; /**< @brief dist * dist */
76 double perp; /**< @brief nearly 0 */
77 double para; /**< @brief nearly 1 */
78};
79
80/**
81 * asserts the validity of a bn_tol struct.
82 */
83#define BN_CK_TOL(_p) BU_CKMAG(_p, BN_TOL_MAGIC, "bn_tol")
84
85/**
86 * initializes a bn_tol struct to zero without allocating any memory.
87 */
88#define BN_TOL_INIT(_p) { \
89 (_p)->magic = BN_TOL_MAGIC; \
90 (_p)->dist = 0.0; \
91 (_p)->dist_sq = 0.0; \
92 (_p)->perp = 0.0; \
93 (_p)->para = 1.0; \
94 }
95
96/**
97 * macro suitable for declaration statement zero-initialization of a
98 * bn_tol struct.
99 */
100#define BN_TOL_INIT_ZERO { BN_TOL_MAGIC, 0.0, 0.0, 0.0, 1.0 }
101
102/**
103 * commonly used default initialization of a bn_tol struct.
104 */
105#define BN_TOL_INIT_TOL {BN_TOL_MAGIC, BN_TOL_DIST, BN_TOL_DIST * BN_TOL_DIST, 1.0e-6, 1.0 - 1.0e-6 }
106#define BN_TOL_INIT_SET_TOL(_p) { \
107 (_p)->magic = BN_TOL_MAGIC; \
108 (_p)->dist = BN_TOL_DIST; \
109 (_p)->dist_sq = BN_TOL_DIST * BN_TOL_DIST; \
110 (_p)->perp = 1.0e-6; \
111 (_p)->para = 1.0 - 1.0e-6; \
112 }
113
114/**
115 * returns truthfully whether a bn_tol struct has been initialized.
116 */
117#define BN_TOL_IS_INITIALIZED(_p) (((struct bn_tol *)(_p) != (struct bn_tol *)0) && LIKELY((_p)->magic == BN_TOL_MAGIC))
118
119/**
120 * replaces the hard coded tolerance value
121 */
122#define BN_TOL_DIST 0.0005
123
124/**
125 * returns truthfully whether a given dot-product of two unspecified
126 * vectors are within a specified parallel tolerance.
127 */
128#define BN_VECT_ARE_PARALLEL(_dot, _tol) \
129 (((_dot) <= -SMALL_FASTF) ? (NEAR_EQUAL((_dot), -1.0, (_tol)->perp)) : (NEAR_EQUAL((_dot), 1.0, (_tol)->perp)))
130
131/**
132 * returns truthfully whether a given dot-product of two unspecified
133 * vectors are within a specified perpendicularity tolerance.
134 */
135#define BN_VECT_ARE_PERP(_dot, _tol) \
136 (((_dot) < 0) ? ((-(_dot))<=(_tol)->perp) : ((_dot) <= (_tol)->perp))
137
138__END_DECLS
139
140#endif /* BN_TOL_H */
141/** @} */
142/*
143 * Local Variables:
144 * mode: C
145 * tab-width: 8
146 * indent-tabs-mode: t
147 * c-file-style: "stroustrup"
148 * End:
149 * ex: shiftwidth=4 tabstop=8
150 */
Header file for the BRL-CAD common definitions.
Global registry of recognized magic numbers.
Definition: tol.h:72
double perp
nearly 0
Definition: tol.h:76
double para
nearly 1
Definition: tol.h:77
uint32_t magic
Definition: tol.h:73
double dist_sq
dist * dist
Definition: tol.h:75
double dist
>= 0
Definition: tol.h:74