BRL-CAD
bbnode.h
Go to the documentation of this file.
1/* B B N O D E . 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/** @addtogroup brep_bbnode
22 *
23 * @brief
24 * Bounding Box Hierarchy Node.
25 *
26 */
27#ifndef BREP_BBNODE_H
28#define BREP_BBNODE_H
29
30#include "common.h"
31
32#include "brep/defines.h"
33#include "brep/curvetree.h"
34#include "brep/ray.h"
35#include "brep/util.h"
36
37/** @{ */
38/** @file brep/bbnode.h */
39
40#ifdef __cplusplus
41extern "C++" {
42namespace brlcad {
43 class BBNode;
44}
45}
46#endif
47
48#ifndef __cplusplus
49typedef struct _bounding_volume_placeholder {
50 int dummy;
52#else
53__BEGIN_DECLS
55__END_DECLS
56#endif
57
58#ifdef __cplusplus
59
60__BEGIN_DECLS
61
62extern "C++" {
63namespace brlcad {
64
65 /**
66 * Bounding Box Hierarchy Node
67 */
68 class BREP_EXPORT BBNode : public PooledObject<BBNode> {
69 public:
70 explicit BBNode(const ON_BoundingBox &node, const CurveTree *ct = NULL);
71 BBNode(const CurveTree *ct,
72 const ON_BoundingBox &node,
73 const ON_Interval &u,
74 const ON_Interval &v,
75 bool checkTrim,
76 bool trimmed);
78
79 BBNode(Deserializer &deserieralizer, const CurveTree &ctree);
80 void serialize(Serializer &serializer) const;
81
82 const ON_BrepFace &get_face() const;
83
84 /** Test if this node is a leaf node in the hierarchy */
85 bool isLeaf() const;
86
87 /** Return all leaves below this node that are leaf nodes */
88 void getLeaves(std::list<const BBNode *> &out_leaves) const;
89
90 /** Functions to add and remove child nodes from this node. */
91 void addChild(BBNode *child);
92
93 /** Report the depth of this node in the hierarchy */
94 int depth() const;
95
96 /** Get 2 points defining a bounding box
97 *
98 * @verbatim
99 * _ max _
100 * _ - + - _
101 * * _ + _ *
102 * | - _ + _ - |
103 * | *+ |
104 * | |+ |
105 * | _ |+ _ |
106 * | _ - | - _ |
107 * * _ | _ *
108 * - _ | _ -
109 * min
110 * @endverbatim
111 */
112 void GetBBox(float *min, float *max) const;
113 void GetBBox(double *min, double *max) const;
114
115 /** Test whether a ray intersects the 3D bounding volume of the
116 * node - if so, and node is not a leaf node, query children. If
117 * leaf node, and intersects, add to list.
118 */
119 bool intersectsHierarchy(const ON_Ray &ray, std::list<const BBNode *> &results) const;
120
121 ON_2dPoint getClosestPointEstimate(const ON_3dPoint &pt) const;
122 ON_2dPoint getClosestPointEstimate(const ON_3dPoint &pt, ON_Interval &u, ON_Interval &v) const;
123 int getLeavesBoundingPoint(const ON_3dPoint &pt, std::list<const BBNode *> &out) const;
124 bool isTrimmed(const ON_2dPoint &uv, const BRNode **closest, double &closesttrim, double within_distance_tol) const;
125
126 void BuildBBox();
127 bool prepTrims();
128
129 const std::vector<BBNode *> &get_children() const;
130
131 /** Bounding Box */
132 ON_BoundingBox m_node;
133
134 /** Surface Information */
135 ON_Interval m_u;
136 ON_Interval m_v;
137
138 /** Trimming Flags */
141
142 /** Point used for closeness testing - usually based on evaluation
143 * of the curve/surface at the center of the parametric domain
144 */
145 ON_3dPoint m_estimate;
146
147 /* Normal at the m_estimate point */
148 ON_3dVector m_normal;
149
150 /** Curve Tree associated with the parent Surface Tree */
151 const CurveTree * const m_ctree;
152
153
154 private:
155 BBNode(const BBNode &source);
156 BBNode &operator=(const BBNode &source);
157
158 void removeChild(BBNode *child);
159 bool intersectedBy(const ON_Ray &ray, double *tnear = NULL, double *tfar = NULL) const;
160
161 /** Report if a given uv point is within the uv boundaries defined
162 * by a node.
163 */
164 bool containsUV(const ON_2dPoint &uv) const;
165
166 void getTrimsAbove(const ON_2dPoint &uv, std::list<const BRNode *> &out_leaves) const;
167 const BBNode *closer(const ON_3dPoint &pt, const BBNode *left, const BBNode *right) const;
168
169 struct Stl : public PooledObject<Stl> {
170 Stl() : m_children(), m_trims_above() {}
171
172 std::vector<BBNode *> m_children;
173 std::list<const BRNode *> m_trims_above;
174 } * const m_stl;
175 };
176
177 inline void
179 {
180 if (LIKELY(child != NULL)) {
181 m_stl->m_children.push_back(child);
182 }
183 }
184
185 inline void
186 BBNode::removeChild(BBNode *child)
187 {
188 std::vector<BBNode *>::iterator i;
189 for (i = m_stl->m_children.begin(); i != m_stl->m_children.end();) {
190 if (*i == child) {
191 delete *i;
192 i = m_stl->m_children.erase(i);
193 } else {
194 ++i;
195 }
196 }
197 }
198
199
200 inline const ON_BrepFace &
202 {
203 return *m_ctree->m_face;
204 }
205
206 inline const std::vector<BBNode *> &
208 {
209 return m_stl->m_children;
210 }
211
212 inline bool
214 {
215 return m_stl->m_children.empty();
216 }
217
218 inline void
219 BBNode::GetBBox(float *min, float *max) const
220 {
221 min[0] = (float)m_node.m_min.x;
222 min[1] = (float)m_node.m_min.y;
223 min[2] = (float)m_node.m_min.z;
224 max[0] = (float)m_node.m_max.x;
225 max[1] = (float)m_node.m_max.y;
226 max[2] = (float)m_node.m_max.z;
227 }
228
229 inline void
230 BBNode::GetBBox(double *min, double *max) const
231 {
232 min[0] = m_node.m_min.x;
233 min[1] = m_node.m_min.y;
234 min[2] = m_node.m_min.z;
235 max[0] = m_node.m_max.x;
236 max[1] = m_node.m_max.y;
237 max[2] = m_node.m_max.z;
238 }
239
240
241} /* namespace brlcad */
242} /* extern C++ */
243
244__END_DECLS
245
246#endif
247
248/** @} */
249
250#endif /* BREP_BBNODE_H */
251
252/*
253 * Local Variables:
254 * mode: C
255 * tab-width: 8
256 * indent-tabs-mode: t
257 * c-file-style: "stroustrup"
258 * End:
259 * ex: shiftwidth=4 tabstop=8
260 */
Definition: ray.h:58
ON_3dVector m_normal
Definition: bbnode.h:148
bool m_trimmed
Definition: bbnode.h:140
void serialize(Serializer &serializer) const
bool isLeaf() const
Definition: bbnode.h:213
BBNode(Deserializer &deserieralizer, const CurveTree &ctree)
ON_Interval m_u
Definition: bbnode.h:135
bool isTrimmed(const ON_2dPoint &uv, const BRNode **closest, double &closesttrim, double within_distance_tol) const
const std::vector< BBNode * > & get_children() const
Definition: bbnode.h:207
ON_2dPoint getClosestPointEstimate(const ON_3dPoint &pt, ON_Interval &u, ON_Interval &v) const
const CurveTree *const m_ctree
Definition: bbnode.h:151
ON_BoundingBox m_node
Definition: bbnode.h:132
ON_3dPoint m_estimate
Definition: bbnode.h:145
int depth() const
bool m_checkTrim
Definition: bbnode.h:139
bool intersectsHierarchy(const ON_Ray &ray, std::list< const BBNode * > &results) const
void addChild(BBNode *child)
Definition: bbnode.h:178
ON_Interval m_v
Definition: bbnode.h:136
BBNode(const CurveTree *ct, const ON_BoundingBox &node, const ON_Interval &u, const ON_Interval &v, bool checkTrim, bool trimmed)
void GetBBox(float *min, float *max) const
Definition: bbnode.h:219
void getLeaves(std::list< const BBNode * > &out_leaves) const
BBNode(const ON_BoundingBox &node, const CurveTree *ct=NULL)
ON_2dPoint getClosestPointEstimate(const ON_3dPoint &pt) const
const ON_BrepFace & get_face() const
Definition: bbnode.h:201
int getLeavesBoundingPoint(const ON_3dPoint &pt, std::list< const BBNode * > &out) const
Header file for the BRL-CAD common definitions.
brlcad::BBNode BrepBoundingVolume
Definition: bbnode.h:54
void int char int int double * min
Definition: tig.h:182
#define LIKELY(expression)
Definition: common.h:358
Definition: bbnode.h:42