Nek5000
SEM for Incompressible NS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
obbox.h
Go to the documentation of this file.
1 #ifndef OBBOX_H
2 #define OBBOX_H
3 
4 #if !defined(TYPES_H) || !defined(NAME_H)
5 #warning "obbox.h" requires "types.h" and "name.h"
6 #endif
7 
8 #define obbox_calc_2 PREFIXED_NAME(obbox_calc_2)
9 #define obbox_calc_3 PREFIXED_NAME(obbox_calc_3)
10 
11 /*--------------------------------------------------------------------------
12  Oriented and axis-aligned bounding box computation for spectral elements
13 
14  Usage:
15 
16  double x[n][nt][ns][nr], y[n][nt][ns][nr], z[n][nt][ns][nr];
17  obbox_3 ob[n];
18 
19  unsigned mr=4*nr, ms=4*ns, mt=4*nt;
20  double tol = 1e-6;
21  obbox_3_calc(ob, x,y,z, nr,ns,nt,n, mr,ms,mt, tol);
22 
23  The parameters mr,ms,mt specify number of points to use in computing
24  bounds (see lob_bnd.h). It is expected that mr>nr, etc. For reasonable
25  quality, a factor of at least 2 is recommended.
26 
27  tol is a relative amount by which to expand the bounding box.
28  This would accommodate, e.g., rounding errors.
29 
30  The axis aligned bounds for a given element are
31  ob[i].x.min <= x <= ob[i].x.max
32  ob[i].y.min <= y <= ob[i].y.max
33  ob[i].z.min <= z <= ob[i].z.max
34 
35  The oriented bounding box is given by
36  (-1,-1,-1)^T <= ob[i].A * (x - ob[i].c0) <= (1,1,1)
37 
38  where the matrix is row-major format,
39  dx = x - c0[0], dy = y - c0[1], dz = z - c0[2]
40  -1 <= r[0] = A[0]*dx + A[1]*dy + A[2]*dz <= 1
41  -1 <= r[1] = A[3]*dx + A[4]*dy + A[5]*dz <= 1
42  -1 <= r[2] = A[6]*dx + A[7]*dy + A[8]*dz <= 1
43 
44  Also, ob[i].A * (x - ob[i].c0) should be a reasonable seed for Newton's.
45 
46  --------------------------------------------------------------------------*/
47 
48 #ifndef LOB_BND_H
49 struct dbl_range { double min, max; };
50 #endif
51 
52 struct obbox_2 { double c0[2], A[4];
53  struct dbl_range x[2]; };
54 
55 struct obbox_3 { double c0[3], A[9];
56  struct dbl_range x[3]; };
57 
58 void obbox_calc_2(struct obbox_2 *out,
59  const double *const elx[2],
60  const unsigned n[2], uint nel,
61  const unsigned m[2], const double tol);
62 
63 void obbox_calc_3(struct obbox_3 *out,
64  const double *const elx[3],
65  const unsigned n[3], uint nel,
66  const unsigned m[3], const double tol);
67 
68 /* positive when possibly inside */
69 static double obbox_axis_test_2(const struct obbox_2 *const b,
70  const double x[2])
71 {
72  const double bx = (x[0]-b->x[0].min)*(b->x[0].max-x[0]);
73  return bx<0 ? bx : (x[1]-b->x[1].min)*(b->x[1].max-x[1]);
74 }
75 
76 /* positive when possibly inside */
77 static double obbox_test_2(const struct obbox_2 *const b, const double x[2])
78 {
79  const double bxy = obbox_axis_test_2(b,x);
80  if(bxy<0) return bxy; else {
81  const double dx = x[0]-b->c0[0], dy = x[1]-b->c0[1];
82  const double r = b->A[0]*dx + b->A[1]*dy,
83  s = b->A[2]*dx + b->A[3]*dy;
84  const double br = (r+1)*(1-r);
85  return br<0 ? br : (s+1)*(1-s);
86  }
87 }
88 
89 /* positive when possibly inside */
90 static double obbox_axis_test_3(const struct obbox_3 *const b,
91  const double x[3])
92 {
93  const double bx = (x[0]-b->x[0].min)*(b->x[0].max-x[0]);
94  const double by = (x[1]-b->x[1].min)*(b->x[1].max-x[1]);
95  return bx<0 ? bx : (by<0 ? by : (x[2]-b->x[2].min)*(b->x[2].max-x[2]));
96 }
97 
98 /* positive when possibly inside */
99 static double obbox_test_3(const struct obbox_3 *const b, const double x[3])
100 {
101  const double bxyz = obbox_axis_test_3(b,x);
102  if(bxyz<0) return bxyz; else {
103  const double dx = x[0]-b->c0[0], dy = x[1]-b->c0[1], dz = x[2]-b->c0[2];
104  const double r = b->A[0]*dx + b->A[1]*dy + b->A[2]*dz,
105  s = b->A[3]*dx + b->A[4]*dy + b->A[5]*dz,
106  t = b->A[6]*dx + b->A[7]*dy + b->A[8]*dz;
107  const double br = (r+1)*(1-r), bs = (s+1)*(1-s);
108  return br<0 ? br : (bs<0 ? bs : (t+1)*(1-t));
109  }
110 }
111 
112 #endif
113 
double A[9]
Definition: obbox.c:19
#define uint
Definition: types.h:70
n
Definition: xxt_test.m:73
double max
Definition: lob_bnd.c:21
#define x
static double obbox_test_3(const struct obbox_3 *const b, const double x[3])
Definition: obbox.h:99
Definition: obbox.c:19
struct dbl_range x[3]
Definition: obbox.c:20
struct dbl_range x[2]
Definition: obbox.c:17
static double elx[NR *NS]
double A[4]
Definition: obbox.c:16
#define obbox_calc_3
Definition: obbox.h:9
static double obbox_axis_test_2(const struct obbox_2 *const b, const double x[2])
Definition: obbox.h:69
#define obbox_calc_2
Definition: obbox.h:8
double c0[3]
Definition: obbox.c:19
double min
Definition: lob_bnd.c:21
Definition: obbox.c:16
static double obbox_axis_test_3(const struct obbox_3 *const b, const double x[3])
Definition: obbox.h:90
ulong out[N]
Definition: sort_test2.c:20
double c0[2]
Definition: obbox.c:16
static double obbox_test_2(const struct obbox_2 *const b, const double x[2])
Definition: obbox.h:77