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-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/** @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 bool face_GetBoundingBox(const ON_BrepFace &face,ON_BoundingBox& bbox,bool bGrowBox);
65extern BREP_EXPORT bool surface_GetBoundingBox(const ON_Surface *surf,const ON_Interval &u_interval,const ON_Interval &v_interval,ON_BoundingBox& bbox,bool bGrowBox);
66extern BREP_EXPORT bool 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
75extern BREP_EXPORT ON_Curve*
77 const ON_Surface *s,
78 const ON_Curve& curve_2d,
79 const ON_Interval* curve_2d_subdomain
80 );
81
82/* Report the object type as a string */
83extern BREP_EXPORT const char *
84ON_ObjectTypeToString(ON::object_type t);
85
86/* Experimental function to generate Tikz plotting information
87 * from B-Rep objects. This may or may not be something we
88 * expose as a feature long term - probably should be a more
89 * generic API that supports multiple formats... */
90extern BREP_EXPORT int
91ON_BrepTikz(ON_String &s, const ON_Brep *brep, const char *color, const char *prefix);
92
93/**
94 * Get the curve segment between param a and param b
95 *
96 * @param in [in] the curve to split
97 * @param a [in] either a or b can be the larger one
98 * @param b [in] either a or b can be the larger one
99 *
100 * @return the result curve segment. NULL for error.
101 */
102extern BREP_EXPORT ON_Curve *
103sub_curve(const ON_Curve *in, double a, double b);
104
105/**
106 * Get the sub-surface whose u in [a,b] or v in [a, b]
107 *
108 * @param in [in] the surface to split
109 * @param dir [in] 0: u-split, 1: v-split
110 * @param a [in] either a or b can be the larger one
111 * @param b [in] either a or b can be the larger one
112 *
113 * @return the result sub-surface. NULL for error.
114 */
115extern BREP_EXPORT ON_Surface *
116sub_surface(const ON_Surface *in, int dir, double a, double b);
117
118
119BREP_EXPORT void set_key(struct bu_vls *key, int k, int *karray);
120
121
123{
124public:
126 m_capacity(1024 * 1024),
127 m_external()
128 {
129 BU_EXTERNAL_INIT(&m_external);
130
131 m_external.ext_buf = static_cast<uint8_t *>(bu_malloc(m_capacity, "m_external"));
132 }
133
134
136 {
137 bu_free_external(&m_external);
138 }
139
140
142 {
143 const bu_external result = m_external;
144 m_capacity = 1024 * 1024;
145 BU_EXTERNAL_INIT(&m_external);
146 m_external.ext_buf = static_cast<uint8_t *>(bu_malloc(m_capacity, "m_external"));
147 return result;
148 }
149
150
151 void write_uint8(uint8_t value)
152 {
153 uint8_t * const dest = extend(sizeof(value));
154 *dest = value;
155 }
156
157
158 void write_uint32(uint32_t value)
159 {
160 uint8_t * const dest = extend(SIZEOF_NETWORK_LONG);
161 const uint32_t out_value = htonl(value);
162 std::memcpy(dest, &out_value, SIZEOF_NETWORK_LONG);
163 }
164
165
166 void write_int32(int32_t value)
167 {
168 uint8_t * const dest = extend(SIZEOF_NETWORK_LONG);
169 long out_value = value;
170
171 if (UNLIKELY(1 != bu_cv_htonsl(dest, SIZEOF_NETWORK_LONG, &out_value, 1)))
172 bu_bomb("bu_cv_htonsl() failed");
173 }
174
175
176 void write_double(double value)
177 {
178 uint8_t * const dest = extend(SIZEOF_NETWORK_DOUBLE);
179 bu_cv_htond(dest, reinterpret_cast<const unsigned char *>(&value), 1);
180 }
181
182
183 void write(const ON_3dPoint &value)
184 {
185 write_double(value.x);
186 write_double(value.y);
187 write_double(value.z);
188 }
189
190
191 void write(const ON_BoundingBox &value)
192 {
193 write(value.m_min);
194 write(value.m_max);
195 }
196
197
198 void write(const ON_Interval &value)
199 {
200 write_double(value.m_t[0]);
201 write_double(value.m_t[1]);
202 }
203
204
205private:
206 Serializer(const Serializer &source);
207 Serializer &operator=(const Serializer &source);
208
209
210 uint8_t *extend(std::size_t size)
211 {
212 const std::size_t position = m_external.ext_nbytes;
213 m_external.ext_nbytes += size;
214
215 if (m_external.ext_nbytes > m_capacity) {
216 m_capacity = m_external.ext_nbytes * 2;
217 m_external.ext_buf = static_cast<uint8_t *>(bu_realloc(m_external.ext_buf, m_capacity, "m_external"));
218 }
219
220 return &m_external.ext_buf[position];
221 }
222
223
224 std::size_t m_capacity;
225 bu_external m_external;
226};
227
228
230{
231public:
232Deserializer(const bu_external &external) :
233 m_position(0),
234 m_external(external)
235 {
236 BU_CK_EXTERNAL(&m_external);
237 }
238
239
241 {
242 if (m_position != m_external.ext_nbytes)
243 bu_bomb("did not deserialize entire stream");
244 }
245
246
247 uint8_t read_uint8()
248 {
249 return *get(1);
250 }
251
252
253 uint32_t read_uint32()
254 {
255 uint32_t result;
256 std::memcpy(&result, get(SIZEOF_NETWORK_LONG), SIZEOF_NETWORK_LONG);
257 return ntohl(result);
258 }
259
260
261 int32_t read_int32()
262 {
263 long result;
264
265 if (UNLIKELY(1 != bu_cv_ntohsl(&result, sizeof(result), const_cast<void *>(reinterpret_cast<const void *>(get(SIZEOF_NETWORK_LONG))), 1)))
266 bu_bomb("bu_cv_ntohsl() failed");
267
268 return result;
269 }
270
271
272 double read_double()
273 {
274 double result;
275 bu_cv_ntohd(reinterpret_cast<unsigned char *>(&result), get(SIZEOF_NETWORK_DOUBLE), 1);
276 return result;
277 }
278
279
280 void read(ON_3dPoint &value)
281 {
282 value.x = read_double();
283 value.y = read_double();
284 value.z = read_double();
285 }
286
287
288 void read(ON_3dVector &value)
289 {
290 value.x = read_double();
291 value.y = read_double();
292 value.z = read_double();
293 }
294
295
296 void read(ON_BoundingBox &value)
297 {
298 read(value.m_min);
299 read(value.m_max);
300 }
301
302
303 void read(ON_Interval &value)
304 {
305 value.m_t[0] = read_double();
306 value.m_t[1] = read_double();
307 }
308
309
310private:
311 Deserializer(const Deserializer &source);
312 Deserializer &operator=(const Deserializer &source);
313
314
315 const uint8_t *get(std::size_t size)
316 {
317 const std::size_t result_position = m_position;
318 m_position += size;
319
320 if (UNLIKELY(m_position > m_external.ext_nbytes))
321 bu_bomb("invalid position");
322
323 return &m_external.ext_buf[result_position];
324 }
325
326
327 std::size_t m_position;
328 const bu_external &m_external;
329};
330
331
332template <typename T>
334{
335public:
336 static void *operator new(std::size_t size)
337 {
338 if (UNLIKELY(size != sizeof(T)))
339 throw std::bad_alloc();
340
341 return bu_heap_get(size);
342 }
343
344
345 static void operator delete(void *pointer)
346 {
347 bu_heap_put(pointer, sizeof(T));
348 }
349};
350
351// Moved from librt - placed here until a better location is found
352BREP_EXPORT int
354 ON_Brep *brep,
355 int surface_index,
356 int i,
357 int j,
358 fastf_t dx,
359 fastf_t dy,
360 fastf_t dz);
361
362} /* extern C++ */
363
364__END_DECLS
365
366#endif /* __cplusplus */
367
368/** @} */
369
370#endif /* BREP_UTIL_H */
371
372/*
373 * Local Variables:
374 * mode: C
375 * tab-width: 8
376 * indent-tabs-mode: t
377 * c-file-style: "stroustrup"
378 * End:
379 * ex: shiftwidth=4 tabstop=8
380 */
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)
bool surface_EvNormal(const ON_Surface *surf, double s, double t, ON_3dPoint &point, ON_3dVector &normal, int side=0, int *hint=0)
ON_Curve * interpolateCurve(ON_2dPointArray &samples)
void ON_MinMaxInit(ON_3dPoint *min, ON_3dPoint *max)
void ON_BoundingBox_Plot(FILE *pf, ON_BoundingBox &bb)
const char * ON_ObjectTypeToString(ON::object_type t)
bool face_GetBoundingBox(const ON_BrepFace &face, ON_BoundingBox &bbox, bool bGrowBox)
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)
bool surface_GetBoundingBox(const ON_Surface *surf, const ON_Interval &u_interval, const ON_Interval &v_interval, ON_BoundingBox &bbox, bool bGrowBox)
ON_Curve * ON_Surface_Pushup(const ON_Surface *s, const ON_Curve &curve_2d, const ON_Interval *curve_2d_subdomain)
int ON_Curve_PolyLine_Approx(ON_Polyline *polyline, const ON_Curve *curve, double tol)
ON_NurbsCurve * interpolateLocalCubicCurve(const ON_3dPointArray &Q)
void read(ON_Interval &value)
Definition: util.h:303
int32_t read_int32()
Definition: util.h:261
void read(ON_3dPoint &value)
Definition: util.h:280
double read_double()
Definition: util.h:272
uint8_t read_uint8()
Definition: util.h:247
void read(ON_BoundingBox &value)
Definition: util.h:296
Deserializer(const bu_external &external)
Definition: util.h:232
~Deserializer()
Definition: util.h:240
uint32_t read_uint32()
Definition: util.h:253
void read(ON_3dVector &value)
Definition: util.h:288
void write(const ON_BoundingBox &value)
Definition: util.h:191
void write(const ON_Interval &value)
Definition: util.h:198
void write_int32(int32_t value)
Definition: util.h:166
void write_uint8(uint8_t value)
Definition: util.h:151
~Serializer()
Definition: util.h:135
void write_uint32(uint32_t value)
Definition: util.h:158
bu_external take()
Definition: util.h:141
void write(const ON_3dPoint &value)
Definition: util.h:183
void write_double(double value)
Definition: util.h:176
Serializer()
Definition: util.h:125
Header file for the BRL-CAD common definitions.
NORETURN void bu_bomb(const char *str)
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:379
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