BRL-CAD
util.h
Go to the documentation of this file.
1/* U T I L . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2022 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/** @file brep/util.h */
22/** @addtogroup brep_util
23 *
24 * @brief
25 * These are utility routines for libbrep, used throughout the library.
26 *
27 */
28
29#ifndef BREP_UTIL_H
30#define BREP_UTIL_H
31
32#include "common.h"
33
34#include "bnetwork.h" /* Needed for ntohl and htonl */
35
36#ifdef __cplusplus
37// @cond SKIP_C++_INCLUDE
38extern "C++" {
39# include <cstring>
40}
41// @endcond
42#endif
43
44
45#include "bu/cv.h"
46#include "bu/endian.h"
47#include "bu/log.h"
48#include "bu/malloc.h"
49#include "bu/parse.h"
50
51#include "brep/defines.h"
52
53__BEGIN_DECLS
54
55#ifdef __cplusplus
56extern "C++" {
57
58extern BREP_EXPORT void ON_BoundingBox_Plot(FILE *pf, ON_BoundingBox &bb);
59extern BREP_EXPORT ON_3dPoint ON_LinePlaneIntersect(ON_Line &line, ON_Plane &plane);
60extern BREP_EXPORT void ON_Plane_Plot(FILE *pf, ON_Plane &plane);
61extern BREP_EXPORT void ON_MinMaxInit(ON_3dPoint *min, ON_3dPoint *max);
62extern BREP_EXPORT bool ON_NearZero(double x, double tolerance = ON_ZERO_TOLERANCE);
63
64extern BREP_EXPORT ON_BOOL32 face_GetBoundingBox(const ON_BrepFace &face,ON_BoundingBox& bbox,ON_BOOL32 bGrowBox);
65extern BREP_EXPORT ON_BOOL32 surface_GetBoundingBox(const ON_Surface *surf,const ON_Interval &u_interval,const ON_Interval &v_interval,ON_BoundingBox& bbox,ON_BOOL32 bGrowBox);
66extern BREP_EXPORT ON_BOOL32 surface_EvNormal(const ON_Surface *surf,double s,double t,ON_3dPoint& point,ON_3dVector& normal,int side=0,int* hint=0);
67
68extern BREP_EXPORT ON_Curve *interpolateCurve(ON_2dPointArray &samples);
69extern BREP_EXPORT ON_NurbsCurve *
70interpolateLocalCubicCurve(const ON_3dPointArray &Q);
71
72extern BREP_EXPORT int
73ON_Curve_PolyLine_Approx(ON_Polyline *polyline, const ON_Curve *curve, double tol);
74
75
76/* Experimental function to generate Tikz plotting information
77 * from B-Rep objects. This may or may not be something we
78 * expose as a feature long term - probably should be a more
79 * generic API that supports multiple formats... */
80extern BREP_EXPORT int
81ON_BrepTikz(ON_String &s, const ON_Brep *brep, const char *color, const char *prefix);
82
83/**
84 * Get the curve segment between param a and param b
85 *
86 * @param in [in] the curve to split
87 * @param a [in] either a or b can be the larger one
88 * @param b [in] either a or b can be the larger one
89 *
90 * @return the result curve segment. NULL for error.
91 */
92extern BREP_EXPORT ON_Curve *
93sub_curve(const ON_Curve *in, double a, double b);
94
95/**
96 * Get the sub-surface whose u in [a,b] or v in [a, b]
97 *
98 * @param in [in] the surface to split
99 * @param dir [in] 0: u-split, 1: v-split
100 * @param a [in] either a or b can be the larger one
101 * @param b [in] either a or b can be the larger one
102 *
103 * @return the result sub-surface. NULL for error.
104 */
105extern BREP_EXPORT ON_Surface *
106sub_surface(const ON_Surface *in, int dir, double a, double b);
107
108
109BREP_EXPORT void set_key(struct bu_vls *key, int k, int *karray);
110
111
113{
114public:
116 m_capacity(1024 * 1024),
117 m_external()
118 {
119 BU_EXTERNAL_INIT(&m_external);
120
121 m_external.ext_buf = static_cast<uint8_t *>(bu_malloc(m_capacity, "m_external"));
122 }
123
124
126 {
127 bu_free_external(&m_external);
128 }
129
130
132 {
133 const bu_external result = m_external;
134 m_capacity = 1024 * 1024;
135 BU_EXTERNAL_INIT(&m_external);
136 m_external.ext_buf = static_cast<uint8_t *>(bu_malloc(m_capacity, "m_external"));
137 return result;
138 }
139
140
141 void write_uint8(uint8_t value)
142 {
143 uint8_t * const dest = extend(sizeof(value));
144 *dest = value;
145 }
146
147
148 void write_uint32(uint32_t value)
149 {
150 uint8_t * const dest = extend(SIZEOF_NETWORK_LONG);
151 const uint32_t out_value = htonl(value);
152 std::memcpy(dest, &out_value, SIZEOF_NETWORK_LONG);
153 }
154
155
156 void write_int32(int32_t value)
157 {
158 uint8_t * const dest = extend(SIZEOF_NETWORK_LONG);
159 long out_value = value;
160
161 if (UNLIKELY(1 != bu_cv_htonsl(dest, SIZEOF_NETWORK_LONG, &out_value, 1)))
162 bu_bomb("bu_cv_htonsl() failed");
163 }
164
165
166 void write_double(double value)
167 {
168 uint8_t * const dest = extend(SIZEOF_NETWORK_DOUBLE);
169 bu_cv_htond(dest, reinterpret_cast<const unsigned char *>(&value), 1);
170 }
171
172
173 void write(const ON_3dPoint &value)
174 {
175 write_double(value.x);
176 write_double(value.y);
177 write_double(value.z);
178 }
179
180
181 void write(const ON_BoundingBox &value)
182 {
183 write(value.m_min);
184 write(value.m_max);
185 }
186
187
188 void write(const ON_Interval &value)
189 {
190 write_double(value.m_t[0]);
191 write_double(value.m_t[1]);
192 }
193
194
195private:
196 Serializer(const Serializer &source);
197 Serializer &operator=(const Serializer &source);
198
199
200 uint8_t *extend(std::size_t size)
201 {
202 const std::size_t position = m_external.ext_nbytes;
203 m_external.ext_nbytes += size;
204
205 if (m_external.ext_nbytes > m_capacity) {
206 m_capacity = m_external.ext_nbytes * 2;
207 m_external.ext_buf = static_cast<uint8_t *>(bu_realloc(m_external.ext_buf, m_capacity, "m_external"));
208 }
209
210 return &m_external.ext_buf[position];
211 }
212
213
214 std::size_t m_capacity;
215 bu_external m_external;
216};
217
218
220{
221public:
222Deserializer(const bu_external &external) :
223 m_position(0),
224 m_external(external)
225 {
226 BU_CK_EXTERNAL(&m_external);
227 }
228
229
231 {
232 if (m_position != m_external.ext_nbytes)
233 bu_bomb("did not deserialize entire stream");
234 }
235
236
237 uint8_t read_uint8()
238 {
239 return *get(1);
240 }
241
242
243 uint32_t read_uint32()
244 {
245 uint32_t result;
246 std::memcpy(&result, get(SIZEOF_NETWORK_LONG), SIZEOF_NETWORK_LONG);
247 return ntohl(result);
248 }
249
250
251 int32_t read_int32()
252 {
253 long result;
254
255 if (UNLIKELY(1 != bu_cv_ntohsl(&result, sizeof(result), const_cast<void *>(reinterpret_cast<const void *>(get(SIZEOF_NETWORK_LONG))), 1)))
256 bu_bomb("bu_cv_ntohsl() failed");
257
258 return result;
259 }
260
261
262 double read_double()
263 {
264 double result;
265 bu_cv_ntohd(reinterpret_cast<unsigned char *>(&result), get(SIZEOF_NETWORK_DOUBLE), 1);
266 return result;
267 }
268
269
270 void read(ON_3dPoint &value)
271 {
272 value.x = read_double();
273 value.y = read_double();
274 value.z = read_double();
275 }
276
277
278 void read(ON_3dVector &value)
279 {
280 value.x = read_double();
281 value.y = read_double();
282 value.z = read_double();
283 }
284
285
286 void read(ON_BoundingBox &value)
287 {
288 read(value.m_min);
289 read(value.m_max);
290 }
291
292
293 void read(ON_Interval &value)
294 {
295 value.m_t[0] = read_double();
296 value.m_t[1] = read_double();
297 }
298
299
300private:
301 Deserializer(const Deserializer &source);
302 Deserializer &operator=(const Deserializer &source);
303
304
305 const uint8_t *get(std::size_t size)
306 {
307 const std::size_t result_position = m_position;
308 m_position += size;
309
310 if (UNLIKELY(m_position > m_external.ext_nbytes))
311 bu_bomb("invalid position");
312
313 return &m_external.ext_buf[result_position];
314 }
315
316
317 std::size_t m_position;
318 const bu_external &m_external;
319};
320
321
322template <typename T>
324{
325public:
326 static void *operator new(std::size_t size)
327 {
328 if (UNLIKELY(size != sizeof(T)))
329 throw std::bad_alloc();
330
331 return bu_heap_get(size);
332 }
333
334
335 static void operator delete(void *pointer)
336 {
337 bu_heap_put(pointer, sizeof(T));
338 }
339};
340
341// Moved from librt - placed here until a better location is found
342BREP_EXPORT int
344 ON_Brep *brep,
345 int surface_index,
346 int i,
347 int j,
348 fastf_t dx,
349 fastf_t dy,
350 fastf_t dz);
351
352} /* extern C++ */
353
354__END_DECLS
355
356#endif /* __cplusplus */
357
358/** @} */
359
360#endif /* BREP_UTIL_H */
361
362/*
363 * Local Variables:
364 * mode: C
365 * tab-width: 8
366 * indent-tabs-mode: t
367 * c-file-style: "stroustrup"
368 * End:
369 * ex: shiftwidth=4 tabstop=8
370 */
ON_3dPoint ON_LinePlaneIntersect(ON_Line &line, ON_Plane &plane)
int brep_translate_scv(ON_Brep *brep, int surface_index, int i, int j, fastf_t dx, fastf_t dy, fastf_t dz)
void set_key(struct bu_vls *key, int k, int *karray)
ON_Curve * interpolateCurve(ON_2dPointArray &samples)
ON_BOOL32 face_GetBoundingBox(const ON_BrepFace &face, ON_BoundingBox &bbox, ON_BOOL32 bGrowBox)
void ON_MinMaxInit(ON_3dPoint *min, ON_3dPoint *max)
void ON_BoundingBox_Plot(FILE *pf, ON_BoundingBox &bb)
bool ON_NearZero(double x, double tolerance=ON_ZERO_TOLERANCE)
void ON_Plane_Plot(FILE *pf, ON_Plane &plane)
int ON_BrepTikz(ON_String &s, const ON_Brep *brep, const char *color, const char *prefix)
ON_Surface * sub_surface(const ON_Surface *in, int dir, double a, double b)
ON_Curve * sub_curve(const ON_Curve *in, double a, double b)
int ON_Curve_PolyLine_Approx(ON_Polyline *polyline, const ON_Curve *curve, double tol)
ON_NurbsCurve * interpolateLocalCubicCurve(const ON_3dPointArray &Q)
ON_BOOL32 surface_EvNormal(const ON_Surface *surf, double s, double t, ON_3dPoint &point, ON_3dVector &normal, int side=0, int *hint=0)
ON_BOOL32 surface_GetBoundingBox(const ON_Surface *surf, const ON_Interval &u_interval, const ON_Interval &v_interval, ON_BoundingBox &bbox, ON_BOOL32 bGrowBox)
void read(ON_Interval &value)
Definition: util.h:293
int32_t read_int32()
Definition: util.h:251
void read(ON_3dPoint &value)
Definition: util.h:270
double read_double()
Definition: util.h:262
uint8_t read_uint8()
Definition: util.h:237
void read(ON_BoundingBox &value)
Definition: util.h:286
Deserializer(const bu_external &external)
Definition: util.h:222
~Deserializer()
Definition: util.h:230
uint32_t read_uint32()
Definition: util.h:243
void read(ON_3dVector &value)
Definition: util.h:278
void write(const ON_BoundingBox &value)
Definition: util.h:181
void write(const ON_Interval &value)
Definition: util.h:188
void write_int32(int32_t value)
Definition: util.h:156
void write_uint8(uint8_t value)
Definition: util.h:141
~Serializer()
Definition: util.h:125
void write_uint32(uint32_t value)
Definition: util.h:148
bu_external take()
Definition: util.h:131
void write(const ON_3dPoint &value)
Definition: util.h:173
void write_double(double value)
Definition: util.h:166
Serializer()
Definition: util.h:115
Header file for the BRL-CAD common definitions.
void bu_bomb(const char *str) _BU_ATTR_ANALYZE_NORETURN _BU_ATTR_NORETURN
uint32_t ntohl(uint32_t)
uint32_t htonl(uint32_t)
#define SIZEOF_NETWORK_DOUBLE
Definition: cv.h:100
#define SIZEOF_NETWORK_LONG
Definition: cv.h:98
void bu_cv_htond(unsigned char *out, const unsigned char *in, size_t count)
void bu_cv_ntohd(unsigned char *out, const unsigned char *in, size_t count)
size_t bu_cv_ntohsl(signed long int *, size_t, void *, size_t)
size_t bu_cv_htonsl(void *, size_t, long *, size_t)
void * bu_realloc(void *ptr, size_t siz, const char *str)
void * bu_malloc(size_t siz, const char *str)
void * bu_heap_get(size_t sz)
void bu_heap_put(void *ptr, size_t sz)
#define BU_EXTERNAL_INIT(_p)
Definition: parse.h:239
void bu_free_external(struct bu_external *ep)
#define BU_CK_EXTERNAL(_p)
Definition: parse.h:226
void float float int int int int float * size
Definition: tig.h:132
void int char int int double * min
Definition: tig.h:182
void float * x
Definition: tig.h:72
void int char int int double double * dx
Definition: tig.h:183
#define UNLIKELY(expression)
Definition: common.h:388
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:330
size_t ext_nbytes
Definition: parse.h:212
uint8_t * ext_buf
Definition: parse.h:218
Definition: vls.h:53
NMG topological face.
Definition: topology.h:210