File: geomtst.c

package info (click to toggle)
libctl 3.0.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 2,084 kB
  • ctags: 976
  • sloc: sh: 8,567; ansic: 5,584; lisp: 2,219; makefile: 102
file content (205 lines) | stat: -rw-r--r-- 5,426 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#include <ctlgeom.h>

/************************************************************************/

/* return a random number in [0,1]: */
static double mydrand(void)
{
     double d = rand();
     return (d / (double) RAND_MAX);
}

/* return a uniform random number in [a,b] */
static double myurand(double a, double b)
{
     return ((b - a) * mydrand() + a);
}

#define K_PI 3.141592653589793238462643383279502884197

/* return a random unit vector, uniformly distributed over a sphere */
vector3 random_unit_vector3(void)
{
     double z, t, r;
     vector3 v;

     z = 2*mydrand() - 1;
     t = 2*K_PI*mydrand();
     r = sqrt(1 - z*z);
     v.x = r * cos(t);
     v.y = r * sin(t);
     v.z = z;
     return v;
}

double find_edge(geometric_object o, vector3 dir, double max, double tol)
{
     double min = 0;
     if (!(point_in_fixed_objectp(vector3_scale(min, dir), o) &&
	   !point_in_fixed_objectp(vector3_scale(max, dir), o))) {
	  fprintf(stderr, "object out of bounds in find_edge");
	  exit(1);
     }
     do {
	  double d = (min + max) / 2;
          if (point_in_fixed_objectp(vector3_scale(d, dir), o))
	       min = d;
	  else
	       max = d;
     } while (max - min > tol);
     return (min + max) / 2;
}

static vector3 make_vector3(double x, double y, double z)
{
     vector3 v;
     v.x = x; v.y = y; v.z = z;
     return v;
}

/* return a random geometric object, centered at the origin, with
   diameter roughly 1 */
geometric_object random_object(void)
{
     material_type m = { 0 };
     vector3 c = { 0, 0, 0 };
     geometric_object o;
     switch (rand() % 5) {
	 case 0:
	      o = make_sphere(m, c, myurand(0.5,1.5));
	      break;
	 case 1:
	      o = make_cylinder(m, c, myurand(0.5,1.5), myurand(0.5,1.5),
				random_unit_vector3());
	      break;
	 case 2:
	      o = make_cone(m, c, myurand(0.5,1.5), myurand(0.5,1.5),
			    random_unit_vector3(), myurand(0.5,1.5));
	      break;
	 case 3:
	      o = make_block(m, c, 
#if 1
			     random_unit_vector3(),
			     random_unit_vector3(),
			     random_unit_vector3(),
#else
			     make_vector3(1,0,0),
			     make_vector3(0,1,0),
			     make_vector3(0,0,1),
#endif
			     make_vector3(myurand(0.5,1.5),
					  myurand(0.5,1.5),
					  myurand(0.5,1.5)));
	      break;
	 case 4:
	      o = make_ellipsoid(m, c, 
				 random_unit_vector3(),
				 random_unit_vector3(),
				 random_unit_vector3(),
				 make_vector3(myurand(0.5,1.5),
					      myurand(0.5,1.5),
					      myurand(0.5,1.5)));
	      break;
     }
     return o;
}

/************************************************************************/

static double simple_overlap(geom_box b, geometric_object o, double tol)
{
     double d1,d2,d3, x1,x2,x3, olap0 = 0;
     double itol = 1.0 / ((int) (1/tol + 0.5));

     d1 = (b.high.x - b.low.x) * itol;
     d2 = (b.high.y - b.low.y) * itol;
     d3 = (b.high.z - b.low.z) * itol;
     for (x1 = b.low.x + d1*0.5; x1 <= b.high.x; x1 += d1)
      for (x2 = b.low.y + d2*0.5; x2 <= b.high.y; x2 += d2)
       for (x3 = b.low.z + d3*0.5; x3 <= b.high.z; x3 += d3) {
	    vector3 v;
	    v.x = x1; v.y = x2; v.z = x3;
	    olap0 += d1*d2*d3 * point_in_fixed_objectp(v, o);
       }
     olap0 /= (b.high.x-b.low.x) * (b.high.y-b.low.y) * (b.high.z-b.low.z);
     return olap0;
}

static void test_overlap(double tol)
{
     geometric_object o = random_object();
     vector3 dir = random_unit_vector3();
     geom_box b;
     double d, olap, olap0;

#if 1
     geometry_lattice.basis1 = random_unit_vector3();
     geometry_lattice.basis2 = random_unit_vector3();
     geometry_lattice.basis3 = random_unit_vector3();
     geom_fix_lattice();
     geom_fix_object(o);
#endif

     b.low = make_vector3(myurand(-1,0), myurand(-1,0), myurand(-1,0));
     b.high = make_vector3(myurand(0,1), myurand(0,1), myurand(0,1));

     d = find_edge(o, dir, 10, tol);
     b.low = vector3_plus(b.low, vector3_scale(d, dir));
     b.high = vector3_plus(b.high, vector3_scale(d, dir));

     olap = box_overlap_with_object(b, o, tol, 100/tol);
     olap0 = simple_overlap(b, o, tol);

     if (fabs(olap0 - olap) > 2 * tol * fabs(olap)) {
	  fprintf(stderr, "Large error %e in overlap (%g vs. %g) for:\n"
		  "  lattice = (%g,%g,%g), (%g,%g,%g), (%g,%g,%g)\n"
		  "  box = (%g,%g,%g) - (%g,%g,%g)\n",
		  fabs(olap0 - olap) / fabs(olap),
		  olap, olap0,
		  geometry_lattice.basis1.x,
		  geometry_lattice.basis1.y,
		  geometry_lattice.basis1.z,
		  geometry_lattice.basis2.x,
		  geometry_lattice.basis2.y,
		  geometry_lattice.basis2.z,
		  geometry_lattice.basis3.x,
		  geometry_lattice.basis3.y,
		  geometry_lattice.basis3.z,
		  b.low.x, b.low.y, b.low.z,
		  b.high.x, b.high.y, b.high.z);
	  display_geometric_object_info(2, o);
#if 1
	  while (1) {
	       tol /= sqrt(2.0);
	       fprintf(stderr, "olap = %g, olap0 = %g (with tol = %e)\n",
		       box_overlap_with_object(b, o, tol, 100/tol),
		       simple_overlap(b, o, tol), tol);
	  }
#endif
	  exit(1);
     }
     else
	  printf("Got overlap %g vs. %g with tol = %e\n", olap,olap0,tol);
     geometric_object_destroy(o);
}

/************************************************************************/

int main(void)
{
     const int ntest = 100;
     const double tol = 1e-2;
     int i;

     srand(time(NULL));

     for (i = 0; i < ntest; ++i) test_overlap(tol);
     
     return 0;
}