-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgeom.c
2868 lines (2568 loc) · 115 KB
/
geom.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* libctl: flexible Guile-based control files for scientific software
* Copyright (C) 1998-2020 Massachusetts Institute of Technology and Steven G. Johnson
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Steven G. Johnson can be contacted at stevenj@alum.mit.edu.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>
#ifndef LIBCTLGEOM
#include "ctl-io.h"
#else
#define material_type void *
static void material_type_copy(void **src, void **dest) { *dest = *src; }
#endif
#include "ctlgeom.h"
#ifdef CXX_CTL_IO
using namespace ctlio;
#define CTLIO ctlio::
#define GEOM geometric_object::
#define BLK block::
#define CYL cylinder::
#define MAT material_type::
#else
#define CTLIO
#define GEOM
#define BLK
#define CYL
#define MAT
#endif
#ifdef __cplusplus
#define MALLOC(type, num) (new type[num])
#define MALLOC1(type) (new type)
#define FREE(p) delete[](p)
#define FREE1(p) delete (p)
#else
#define MALLOC(type, num) ((type *)malloc(sizeof(type) * (num)))
#define MALLOC1(type) MALLOC(type, 1)
#define FREE(p) free(p)
#define FREE1(p) free(p)
#endif
#define K_PI 3.14159265358979323846
#define CHECK(cond, s) \
if (!(cond)) { \
fprintf(stderr, s "\n"); \
exit(EXIT_FAILURE); \
}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
// forward declarations of prism-related routines, at the bottom of this file
static boolean node_in_polygon(double qx, double qy, vector3 *nodes, int num_nodes);
static boolean point_in_prism(prism *prsm, vector3 pc);
static vector3 normal_to_prism(prism *prsm, vector3 pc);
static double intersect_line_segment_with_prism(prism *prsm, vector3 pc, vector3 dc, double a,
double b);
static double get_prism_volume(prism *prsm);
static void get_prism_bounding_box(prism *prsm, geom_box *box);
static void display_prism_info(int indentby, geometric_object *o);
static void init_prism(geometric_object *o);
/**************************************************************************/
/* Allows writing to Python's stdout when running from Meep's Python interface */
void (*ctl_printf_callback)(const char *s) = NULL;
void ctl_printf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
if (ctl_printf_callback) {
char *s;
CHECK(vasprintf(&s, fmt, ap) >= 0, "vasprintf failed");
ctl_printf_callback(s);
free(s);
}
else {
vprintf(fmt, ap);
fflush(stdout);
}
va_end(ap);
}
/* If v is a vector in the lattice basis, normalize v so that
its cartesian length is unity. */
static void lattice_normalize(vector3 *v) {
*v = vector3_scale(
1.0 / sqrt(vector3_dot(*v, matrix3x3_vector3_mult(geometry_lattice.metric, *v))), *v);
}
static vector3 lattice_to_cartesian(vector3 v) {
return matrix3x3_vector3_mult(geometry_lattice.basis, v);
}
static vector3 cartesian_to_lattice(vector3 v) {
return matrix3x3_vector3_mult(matrix3x3_inverse(geometry_lattice.basis), v);
}
/* geom_fix_object_ptr is called after an object's externally-configurable parameters
have been initialized, but before any actual geometry calculations are done;
it is an opportunity to (re)compute internal data fields (such as cached
rotation matrices) that depend on externally-configurable parameters.
One example: "Fix" the parameters of the given object to account for the
geometry_lattice basis, which may be non-orthogonal. In particular,
this means that the normalization of several unit vectors, such
as the cylinder or block axes, needs to be changed.
Unfortunately, we can't do this stuff at object-creation time
in Guile, because the geometry_lattice variable may not have
been assigned to its final value at that point. */
void geom_fix_object_ptr(geometric_object *o) {
switch (o->which_subclass) {
case GEOM CYLINDER:
lattice_normalize(&o->subclass.cylinder_data->axis);
if (o->subclass.cylinder_data->which_subclass == CYL WEDGE) {
vector3 a = o->subclass.cylinder_data->axis;
vector3 s = o->subclass.cylinder_data->subclass.wedge_data->wedge_start;
double p = vector3_dot(s, matrix3x3_vector3_mult(geometry_lattice.metric, a));
o->subclass.cylinder_data->subclass.wedge_data->e1 = vector3_minus(s, vector3_scale(p, a));
lattice_normalize(&o->subclass.cylinder_data->subclass.wedge_data->e1);
o->subclass.cylinder_data->subclass.wedge_data->e2 = cartesian_to_lattice(vector3_cross(
lattice_to_cartesian(o->subclass.cylinder_data->axis),
lattice_to_cartesian(o->subclass.cylinder_data->subclass.wedge_data->e1)));
}
break;
case GEOM BLOCK: {
matrix3x3 m;
lattice_normalize(&o->subclass.block_data->e1);
lattice_normalize(&o->subclass.block_data->e2);
lattice_normalize(&o->subclass.block_data->e3);
m.c0 = o->subclass.block_data->e1;
m.c1 = o->subclass.block_data->e2;
m.c2 = o->subclass.block_data->e3;
o->subclass.block_data->projection_matrix = matrix3x3_inverse(m);
break;
}
case GEOM PRISM: {
init_prism(o);
break;
}
case GEOM COMPOUND_GEOMETRIC_OBJECT: {
int i;
int n = o->subclass.compound_geometric_object_data->component_objects.num_items;
geometric_object *os = o->subclass.compound_geometric_object_data->component_objects.items;
for (i = 0; i < n; ++i) {
#if MATERIAL_TYPE_ABSTRACT
if (os[i].material.which_subclass == MAT MATERIAL_TYPE_SELF)
material_type_copy(&o->material, &os[i].material);
#endif
geom_fix_object_ptr(os + i);
}
break;
}
case GEOM GEOMETRIC_OBJECT_SELF:
case GEOM SPHERE: break; /* these objects are fine */
}
}
// deprecated API — doesn't work for prisms
void geom_fix_object(geometric_object o) { geom_fix_object_ptr(&o); }
/* fix all objects in the geometry list as described in
geom_fix_object, above */
void geom_fix_object_list(geometric_object_list geometry) {
int index;
for (index = 0; index < geometry.num_items; ++index)
geom_fix_object_ptr(geometry.items + index);
}
void geom_fix_objects0(geometric_object_list geometry) { geom_fix_object_list(geometry); }
void geom_fix_objects(void) { geom_fix_object_list(geometry); }
void geom_fix_lattice0(lattice *L) {
L->basis1 = unit_vector3(L->basis1);
L->basis2 = unit_vector3(L->basis2);
L->basis3 = unit_vector3(L->basis3);
L->b1 = vector3_scale(L->basis_size.x, L->basis1);
L->b2 = vector3_scale(L->basis_size.y, L->basis2);
L->b3 = vector3_scale(L->basis_size.z, L->basis3);
L->basis.c0 = L->b1;
L->basis.c1 = L->b2;
L->basis.c2 = L->b3;
L->metric = matrix3x3_mult(matrix3x3_transpose(L->basis), L->basis);
}
void geom_fix_lattice(void) { geom_fix_lattice0(&geometry_lattice); }
void geom_cartesian_lattice0(lattice *L) {
L->basis1.x = 1;
L->basis1.y = 0;
L->basis1.z = 0;
L->basis2.x = 0;
L->basis2.y = 1;
L->basis2.z = 0;
L->basis3.x = 0;
L->basis3.y = 0;
L->basis3.z = 1;
L->basis_size.x = L->basis_size.y = L->basis_size.z = 1;
geom_fix_lattice0(L);
}
void geom_cartesian_lattice(void) { geom_cartesian_lattice0(&geometry_lattice); }
void geom_initialize(void) {
/* initialize many of the input variables that are normally
initialized from Scheme, except for default_material and
geometry_lattice.size. */
geom_cartesian_lattice();
geometry_center.x = geometry_center.y = geometry_center.z = 0;
dimensions = 3;
ensure_periodicity = 1;
geometry.num_items = 0;
geometry.items = 0;
}
/**************************************************************************/
/* Return whether or not the point p (in the lattice basis) is inside
the object o.
Requires that the global input var geometry_lattice already be
initialized.
point_in_fixed_objectp additionally requires that geom_fix_object
has been called on o (if the lattice basis is non-orthogonal). */
boolean CTLIO point_in_objectp(vector3 p, geometric_object o) {
geom_fix_object_ptr(&o);
return point_in_fixed_objectp(p, o);
}
boolean point_in_fixed_objectp(vector3 p, geometric_object o) {
return point_in_fixed_pobjectp(p, &o);
}
/* as point_in_fixed_objectp, but sets o to the object in question (if true)
(which may be different from the input o if o is a compound object) */
boolean point_in_fixed_pobjectp(vector3 p, geometric_object *o) {
vector3 r = vector3_minus(p, o->center);
switch (o->which_subclass) {
case GEOM GEOMETRIC_OBJECT_SELF: return 0;
case GEOM SPHERE: {
number radius = o->subclass.sphere_data->radius;
return (radius > 0.0 && vector3_dot(r, matrix3x3_vector3_mult(geometry_lattice.metric, r)) <=
radius * radius);
}
case GEOM CYLINDER: {
vector3 rm = matrix3x3_vector3_mult(geometry_lattice.metric, r);
number proj = vector3_dot(o->subclass.cylinder_data->axis, rm);
number height = o->subclass.cylinder_data->height;
if (fabs(proj) <= 0.5 * height) {
number radius = o->subclass.cylinder_data->radius;
if (o->subclass.cylinder_data->which_subclass == CYL CONE)
radius += (proj / height + 0.5) *
(o->subclass.cylinder_data->subclass.cone_data->radius2 - radius);
else if (o->subclass.cylinder_data->which_subclass == CYL WEDGE) {
number x = vector3_dot(rm, o->subclass.cylinder_data->subclass.wedge_data->e1);
number y = vector3_dot(rm, o->subclass.cylinder_data->subclass.wedge_data->e2);
number theta = atan2(y, x);
number wedge_angle = o->subclass.cylinder_data->subclass.wedge_data->wedge_angle;
if (wedge_angle > 0) {
if (theta < 0) theta = theta + 2 * K_PI;
if (theta > wedge_angle) return 0;
}
else {
if (theta > 0) theta = theta - 2 * K_PI;
if (theta < wedge_angle) return 0;
}
}
return (radius != 0.0 && vector3_dot(r, rm) - proj * proj <= radius * radius);
}
else
return 0;
}
case GEOM BLOCK: {
vector3 proj = matrix3x3_vector3_mult(o->subclass.block_data->projection_matrix, r);
switch (o->subclass.block_data->which_subclass) {
case BLK BLOCK_SELF: {
vector3 size = o->subclass.block_data->size;
return (fabs(proj.x) <= 0.5 * size.x && fabs(proj.y) <= 0.5 * size.y &&
fabs(proj.z) <= 0.5 * size.z);
}
case BLK ELLIPSOID: {
vector3 isa = o->subclass.block_data->subclass.ellipsoid_data->inverse_semi_axes;
double a = proj.x * isa.x, b = proj.y * isa.y, c = proj.z * isa.z;
return (a * a + b * b + c * c <= 1.0);
}
}
break; // never get here but silence compiler warning
}
case GEOM PRISM: {
return point_in_prism(o->subclass.prism_data, p);
}
case GEOM COMPOUND_GEOMETRIC_OBJECT: {
int i;
int n = o->subclass.compound_geometric_object_data->component_objects.num_items;
geometric_object *os = o->subclass.compound_geometric_object_data->component_objects.items;
vector3 shiftby = o->center;
for (i = 0; i < n; ++i) {
*o = os[i];
o->center = vector3_plus(o->center, shiftby);
if (point_in_fixed_pobjectp(p, o)) return 1;
}
break;
}
}
return 0;
}
/**************************************************************************/
/* convert a point p inside o to a coordinate in [0,1]^3 that
is some "natural" coordinate for the object */
vector3 to_geom_object_coords(vector3 p, geometric_object o) {
const vector3 half = {0.5, 0.5, 0.5};
vector3 r = vector3_minus(p, o.center);
switch (o.which_subclass) {
default: {
vector3 po = {0, 0, 0};
return po;
}
case GEOM SPHERE: {
number radius = o.subclass.sphere_data->radius;
return vector3_plus(half, vector3_scale(0.5 / radius, r));
}
/* case GEOM CYLINDER:
NOT YET IMPLEMENTED */
case GEOM BLOCK: {
vector3 proj = matrix3x3_vector3_mult(o.subclass.block_data->projection_matrix, r);
vector3 size = o.subclass.block_data->size;
if (size.x != 0.0) proj.x /= size.x;
if (size.y != 0.0) proj.y /= size.y;
if (size.z != 0.0) proj.z /= size.z;
return vector3_plus(half, proj);
}
/* case GEOM PRISM:
NOT YET IMPLEMENTED */
}
}
/* inverse of to_geom_object_coords */
vector3 from_geom_object_coords(vector3 p, geometric_object o) {
const vector3 half = {0.5, 0.5, 0.5};
p = vector3_minus(p, half);
switch (o.which_subclass) {
default: return o.center;
case GEOM SPHERE: {
number radius = o.subclass.sphere_data->radius;
return vector3_plus(o.center, vector3_scale(radius / 0.5, p));
}
/* case GEOM CYLINDER:
NOT YET IMPLEMENTED */
case GEOM BLOCK: {
vector3 size = o.subclass.block_data->size;
return vector3_plus(
o.center,
vector3_plus(vector3_scale(size.x * p.x, o.subclass.block_data->e1),
vector3_plus(vector3_scale(size.y * p.y, o.subclass.block_data->e2),
vector3_scale(size.z * p.z, o.subclass.block_data->e3))));
}
/* case GEOM PRISM:
NOT YET IMPLEMENTED */
}
}
/**************************************************************************/
/* Return the normal vector from the given object to the given point,
in lattice coordinates, using the surface of the object that the
point is "closest" to for some definition of "closest" that is
reasonable (at least for points near to the object). The length and
sign of the normal vector are arbitrary. */
vector3 CTLIO normal_to_object(vector3 p, geometric_object o) {
geom_fix_object_ptr(&o);
return normal_to_fixed_object(p, o);
}
vector3 normal_to_fixed_object(vector3 p, geometric_object o) {
vector3 r = vector3_minus(p, o.center);
switch (o.which_subclass) {
case GEOM CYLINDER: {
vector3 rm = matrix3x3_vector3_mult(geometry_lattice.metric, r);
double proj = vector3_dot(o.subclass.cylinder_data->axis, rm),
height = o.subclass.cylinder_data->height, radius, prad;
if (fabs(proj) > height * 0.5) return o.subclass.cylinder_data->axis;
radius = o.subclass.cylinder_data->radius;
prad = sqrt(fabs(vector3_dot(r, rm) - proj * proj));
if (o.subclass.cylinder_data->which_subclass == CYL CONE)
radius += (proj / height + 0.5) *
(o.subclass.cylinder_data->subclass.cone_data->radius2 - radius);
if (fabs(fabs(proj) - height * 0.5) < fabs(prad - radius))
return o.subclass.cylinder_data->axis;
if (o.subclass.cylinder_data->which_subclass == CYL CONE)
return vector3_minus(
r, vector3_scale(
proj + prad * (o.subclass.cylinder_data->subclass.cone_data->radius2 - radius) /
height,
o.subclass.cylinder_data->axis));
else
return vector3_minus(r, vector3_scale(proj, o.subclass.cylinder_data->axis));
} // case GEOM CYLINDER
case GEOM BLOCK: {
vector3 proj = matrix3x3_vector3_mult(o.subclass.block_data->projection_matrix, r);
switch (o.subclass.block_data->which_subclass) {
case BLK BLOCK_SELF: {
vector3 size = o.subclass.block_data->size;
double d1 = fabs(fabs(proj.x) - 0.5 * size.x);
double d2 = fabs(fabs(proj.y) - 0.5 * size.y);
double d3 = fabs(fabs(proj.z) - 0.5 * size.z);
if (d1 < d2 && d1 < d3)
return matrix3x3_row1(o.subclass.block_data->projection_matrix);
else if (d2 < d3)
return matrix3x3_row2(o.subclass.block_data->projection_matrix);
else
return matrix3x3_row3(o.subclass.block_data->projection_matrix);
} // case BLK BLOCK_SELF
case BLK ELLIPSOID:
default: {
vector3 isa = o.subclass.block_data->subclass.ellipsoid_data->inverse_semi_axes;
proj.x *= isa.x * isa.x;
proj.y *= isa.y * isa.y;
proj.z *= isa.z * isa.z;
return matrix3x3_transpose_vector3_mult(o.subclass.block_data->projection_matrix, proj);
} // case BLK ELLIPSOID
} // switch (o.subclass.block_data->which_subclass)
} // case GEOM BLOCK
case GEOM PRISM: return normal_to_prism(o.subclass.prism_data, p);
default: return r;
} // switch (o.which_subclass)
return r; // never get here
}
/**************************************************************************/
/* Here is a useful macro to loop over different possible shifts of
the lattice vectors. body is executed for each possible shift,
where the shift is given by the value of shiftby (which should
be a vector3 variable). I would much rather make this a function,
but C's lack of lambda-like function construction or closures makes
this easier to do as a macro. (One could at least wish for
an easier way to make multi-line macros.) */
#define LOOP_PERIODIC(shiftby, body) \
{ \
switch (dimensions) { \
case 1: { \
int iii; \
shiftby.y = shiftby.z = 0; \
for (iii = -1; iii <= 1; ++iii) { \
shiftby.x = iii * geometry_lattice.size.x; \
body; \
} \
break; \
} \
case 2: { \
int iii, jjj; \
shiftby.z = 0; \
for (iii = -1; iii <= 1; ++iii) { \
shiftby.x = iii * geometry_lattice.size.x; \
for (jjj = -1; jjj <= 1; ++jjj) { \
shiftby.y = jjj * geometry_lattice.size.y; \
body; \
} \
} \
break; \
} \
case 3: { \
int iii, jjj, kkk; \
for (iii = -1; iii <= 1; ++iii) { \
shiftby.x = iii * geometry_lattice.size.x; \
for (jjj = -1; jjj <= 1; ++jjj) { \
shiftby.y = jjj * geometry_lattice.size.y; \
for (kkk = -1; kkk <= 1; ++kkk) { \
shiftby.z = kkk * geometry_lattice.size.z; \
body; \
if (geometry_lattice.size.z == 0) break; \
} \
if (geometry_lattice.size.y == 0) break; \
} \
if (geometry_lattice.size.x == 0) break; \
} \
break; \
} \
} \
}
/**************************************************************************/
/* Like point_in_objectp, but also checks the object shifted
by the lattice vectors: */
boolean CTLIO point_in_periodic_objectp(vector3 p, geometric_object o) {
geom_fix_object_ptr(&o);
return point_in_periodic_fixed_objectp(p, o);
}
boolean point_in_periodic_fixed_objectp(vector3 p, geometric_object o) {
vector3 shiftby;
LOOP_PERIODIC(shiftby, if (point_in_fixed_objectp(vector3_minus(p, shiftby), o)) return 1);
return 0;
}
boolean point_shift_in_periodic_fixed_pobjectp(vector3 p, geometric_object *o, vector3 *shiftby) {
geometric_object o0 = *o;
LOOP_PERIODIC((*shiftby), {
*o = o0;
if (point_in_fixed_pobjectp(vector3_minus(p, *shiftby), o)) return 1;
});
return 0;
}
/**************************************************************************/
/* Functions to return the object or material type corresponding to
the point p (in the lattice basis). Returns default_material if p
is not in any object.
Requires that the global input vars geometry_lattice, geometry,
dimensions, default_material and ensure_periodicity already be
initialized.
Also requires that geom_fix_objects() has been called!
material_of_point_inobject is a variant that also returns whether
or not the point was in any object. */
geometric_object object_of_point0(geometric_object_list geometry, vector3 p, vector3 *shiftby) {
geometric_object o;
int index;
shiftby->x = shiftby->y = shiftby->z = 0;
/* loop in reverse order so that later items are given precedence: */
for (index = geometry.num_items - 1; index >= 0; --index) {
o = geometry.items[index];
if ((ensure_periodicity && point_shift_in_periodic_fixed_pobjectp(p, &o, shiftby)) ||
point_in_fixed_pobjectp(p, &o))
return o;
}
o.which_subclass = GEOM GEOMETRIC_OBJECT_SELF; /* no object found */
return o;
}
geometric_object object_of_point(vector3 p, vector3 *shiftby) {
return object_of_point0(geometry, p, shiftby);
}
material_type material_of_point_inobject0(geometric_object_list geometry, vector3 p,
boolean *inobject) {
vector3 shiftby;
geometric_object o = object_of_point0(geometry, p, &shiftby);
*inobject = o.which_subclass != GEOM GEOMETRIC_OBJECT_SELF;
;
return (*inobject ? o.material : default_material);
}
material_type material_of_point_inobject(vector3 p, boolean *inobject) {
return material_of_point_inobject0(geometry, p, inobject);
}
material_type material_of_point0(geometric_object_list geometry, vector3 p) {
boolean inobject;
return material_of_point_inobject0(geometry, p, &inobject);
}
material_type material_of_point(vector3 p) { return material_of_point0(geometry, p); }
/**************************************************************************/
/* Given a geometric object o, display some information about it,
indented by indentby spaces. */
void CTLIO display_geometric_object_info(int indentby, geometric_object o) {
geom_fix_object_ptr(&o);
ctl_printf("%*s", indentby, "");
switch (o.which_subclass) {
case GEOM CYLINDER:
switch (o.subclass.cylinder_data->which_subclass) {
case CYL WEDGE: ctl_printf("wedge"); break;
case CYL CONE: ctl_printf("cone"); break;
case CYL CYLINDER_SELF: ctl_printf("cylinder"); break;
}
break;
case GEOM SPHERE: ctl_printf("sphere"); break;
case GEOM BLOCK:
switch (o.subclass.block_data->which_subclass) {
case BLK ELLIPSOID: ctl_printf("ellipsoid"); break;
case BLK BLOCK_SELF: ctl_printf("block"); break;
}
break;
case GEOM PRISM: ctl_printf("prism"); break;
case GEOM COMPOUND_GEOMETRIC_OBJECT: ctl_printf("compound object"); break;
default: ctl_printf("geometric object"); break;
}
ctl_printf(", center = (%g,%g,%g)\n", o.center.x, o.center.y, o.center.z);
switch (o.which_subclass) {
case GEOM CYLINDER:
ctl_printf("%*s radius %g, height %g, axis (%g, %g, %g)\n", indentby, "",
o.subclass.cylinder_data->radius, o.subclass.cylinder_data->height,
o.subclass.cylinder_data->axis.x, o.subclass.cylinder_data->axis.y,
o.subclass.cylinder_data->axis.z);
if (o.subclass.cylinder_data->which_subclass == CYL CONE)
ctl_printf("%*s radius2 %g\n", indentby, "",
o.subclass.cylinder_data->subclass.cone_data->radius2);
else if (o.subclass.cylinder_data->which_subclass == CYL WEDGE)
ctl_printf("%*s wedge-theta %g\n", indentby, "",
o.subclass.cylinder_data->subclass.wedge_data->wedge_angle);
break;
case GEOM SPHERE:
ctl_printf("%*s radius %g\n", indentby, "", o.subclass.sphere_data->radius);
break;
case GEOM BLOCK:
ctl_printf("%*s size (%g,%g,%g)\n", indentby, "", o.subclass.block_data->size.x,
o.subclass.block_data->size.y, o.subclass.block_data->size.z);
ctl_printf(
"%*s axes (%g,%g,%g), (%g,%g,%g), (%g,%g,%g)\n", indentby, "",
o.subclass.block_data->e1.x, o.subclass.block_data->e1.y, o.subclass.block_data->e1.z,
o.subclass.block_data->e2.x, o.subclass.block_data->e2.y, o.subclass.block_data->e2.z,
o.subclass.block_data->e3.x, o.subclass.block_data->e3.y, o.subclass.block_data->e3.z);
break;
case GEOM PRISM: display_prism_info(indentby, &o); break;
case GEOM COMPOUND_GEOMETRIC_OBJECT: {
int i;
int n = o.subclass.compound_geometric_object_data->component_objects.num_items;
geometric_object *os = o.subclass.compound_geometric_object_data->component_objects.items;
ctl_printf("%*s %d components:\n", indentby, "", n);
for (i = 0; i < n; ++i)
display_geometric_object_info(indentby + 5, os[i]);
break;
}
default: break;
}
}
/**************************************************************************/
/* Compute the intersections with o of a line along p+s*d, returning
the number of intersections (at most 2) and the two intersection "s"
values in s[0] and s[1]. (Note: o must not be a compound object.) */
int intersect_line_with_object(vector3 p, vector3 d, geometric_object o, double s[2]) {
p = vector3_minus(p, o.center);
s[0] = s[1] = 0;
switch (o.which_subclass) {
case GEOM SPHERE: {
number radius = o.subclass.sphere_data->radius;
vector3 dm = matrix3x3_vector3_mult(geometry_lattice.metric, d);
double a = vector3_dot(d, dm);
double b2 = -vector3_dot(dm, p);
double c =
vector3_dot(p, matrix3x3_vector3_mult(geometry_lattice.metric, p)) - radius * radius;
double discrim = b2 * b2 - a * c;
if (discrim < 0)
return 0;
else if (discrim == 0) {
s[0] = b2 / a;
return 1;
}
else {
discrim = sqrt(discrim);
s[0] = (b2 + discrim) / a;
s[1] = (b2 - discrim) / a;
return 2;
}
} // case GEOM SPHERE
case GEOM CYLINDER: {
vector3 dm = matrix3x3_vector3_mult(geometry_lattice.metric, d);
vector3 pm = matrix3x3_vector3_mult(geometry_lattice.metric, p);
number height = o.subclass.cylinder_data->height;
number radius = o.subclass.cylinder_data->radius;
number radius2 = o.subclass.cylinder_data->which_subclass == CYL CONE
? o.subclass.cylinder_data->subclass.cone_data->radius2
: radius;
double dproj = vector3_dot(o.subclass.cylinder_data->axis, dm);
double pproj = vector3_dot(o.subclass.cylinder_data->axis, pm);
double D = (radius2 - radius) / height;
double L = radius + (radius2 - radius) * 0.5 + pproj * D;
double a = vector3_dot(d, dm) - dproj * dproj * (1 + D * D);
double b2 = dproj * (pproj + D * L) - vector3_dot(p, dm);
double c = vector3_dot(p, pm) - pproj * pproj - L * L;
double discrim = b2 * b2 - a * c;
int ret;
if (a == 0) { /* linear equation */
if (b2 == 0) {
if (c == 0) { /* infinite intersections */
s[0] = ((height * 0.5) - pproj) / dproj;
s[1] = -((height * 0.5) + pproj) / dproj;
return 2;
}
else
ret = 0;
}
else {
s[0] = 0.5 * c / b2;
ret = 1;
}
}
else if (discrim < 0)
ret = 0;
else if (discrim == 0) {
s[0] = b2 / a;
ret = 1;
}
else {
discrim = sqrt(discrim);
s[0] = (b2 + discrim) / a;
s[1] = (b2 - discrim) / a;
ret = 2;
}
if (ret == 2 && fabs(pproj + s[1] * dproj) > height * 0.5) ret = 1;
if (ret >= 1 && fabs(pproj + s[0] * dproj) > height * 0.5) {
--ret;
s[0] = s[1];
}
if (ret == 2 || dproj == 0) return ret;
/* find intersections with endcaps */
s[ret] = (height * 0.5 - pproj) / dproj;
if (a * s[ret] * s[ret] - 2 * b2 * s[ret] + c <= 0) ++ret;
if (ret < 2) {
s[ret] = -(height * 0.5 + pproj) / dproj;
if (a * s[ret] * s[ret] - 2 * b2 * s[ret] + c <= 0) ++ret;
}
if (ret == 2 && s[0] == s[1]) ret = 1;
return ret;
} // case GEOM CYLINDER
case GEOM BLOCK: {
vector3 dproj = matrix3x3_vector3_mult(o.subclass.block_data->projection_matrix, d);
vector3 pproj = matrix3x3_vector3_mult(o.subclass.block_data->projection_matrix, p);
switch (o.subclass.block_data->which_subclass) {
case BLK BLOCK_SELF: {
vector3 size = o.subclass.block_data->size;
int ret = 0;
size.x *= 0.5;
size.y *= 0.5;
size.z *= 0.5;
if (dproj.x != 0) {
s[ret] = (size.x - pproj.x) / dproj.x;
if (fabs(pproj.y + s[ret] * dproj.y) <= size.y &&
fabs(pproj.z + s[ret] * dproj.z) <= size.z)
++ret;
s[ret] = (-size.x - pproj.x) / dproj.x;
if (fabs(pproj.y + s[ret] * dproj.y) <= size.y &&
fabs(pproj.z + s[ret] * dproj.z) <= size.z)
++ret;
if (ret == 2) return 2;
}
if (dproj.y != 0) {
s[ret] = (size.y - pproj.y) / dproj.y;
if (fabs(pproj.x + s[ret] * dproj.x) <= size.x &&
fabs(pproj.z + s[ret] * dproj.z) <= size.z)
++ret;
if (ret == 2) return 2;
s[ret] = (-size.y - pproj.y) / dproj.y;
if (fabs(pproj.x + s[ret] * dproj.x) <= size.x &&
fabs(pproj.z + s[ret] * dproj.z) <= size.z)
++ret;
if (ret == 2) return 2;
}
if (dproj.z != 0) {
s[ret] = (size.z - pproj.z) / dproj.z;
if (fabs(pproj.x + s[ret] * dproj.x) <= size.x &&
fabs(pproj.y + s[ret] * dproj.y) <= size.y)
++ret;
if (ret == 2) return 2;
s[ret] = (-size.z - pproj.z) / dproj.z;
if (fabs(pproj.x + s[ret] * dproj.x) <= size.x &&
fabs(pproj.y + s[ret] * dproj.y) <= size.y)
++ret;
}
return ret;
} // case BLK BLOCK_SELF:
case BLK ELLIPSOID:
default: {
vector3 isa = o.subclass.block_data->subclass.ellipsoid_data->inverse_semi_axes;
double a, b2, c, discrim;
dproj.x *= isa.x;
dproj.y *= isa.y;
dproj.z *= isa.z;
pproj.x *= isa.x;
pproj.y *= isa.y;
pproj.z *= isa.z;
a = vector3_dot(dproj, dproj);
b2 = -vector3_dot(dproj, pproj);
c = vector3_dot(pproj, pproj) - 1;
discrim = b2 * b2 - a * c;
if (discrim < 0)
return 0;
else if (discrim == 0) {
s[0] = b2 / a;
return 1;
}
else {
discrim = sqrt(discrim);
s[0] = (b2 + discrim) / a;
s[1] = (b2 - discrim) / a;
return 2;
}
} // case BLK BLOCK_SELF, default
} // switch (o.subclass.block_data->which_subclass)
} // case GEOM BLOCK
default: return 0;
}
}
/* Compute the intersections with o of a line along p+s*d in the interval s in [a,b], returning
the length of the s intersection in this interval. (Note: o must not be a compound object.) */
double intersect_line_segment_with_object(vector3 p, vector3 d, geometric_object o, double a,
double b) {
if (o.which_subclass == GEOM PRISM) {
return intersect_line_segment_with_prism(o.subclass.prism_data, p, d, a, b);
}
else {
double s[2];
if (2 == intersect_line_with_object(p, d, o, s)) {
double ds = (s[0] < s[1] ? MIN(s[1], b) - MAX(s[0], a) : MIN(s[0], b) - MAX(s[1], a));
return (ds > 0 ? ds : 0.0);
}
else
return 0.0;
}
}
/**************************************************************************/
/* Given a basis (matrix columns are the basis unit vectors) and the
size of the lattice (in basis vectors), returns a new "square"
basis. This corresponds to a region of the same volume, but made
rectangular, suitable for outputing to an HDF file.
Given a vector in the range (0..1, 0..1, 0..1), multiplying by
the square basis matrix will yield the coordinates of a point
in the rectangular volume, given in the lattice basis. */
matrix3x3 CTLIO square_basis(matrix3x3 basis, vector3 size) {
matrix3x3 square;
square.c0 = basis.c0;
square.c1 = vector3_minus(basis.c1, vector3_scale(vector3_dot(basis.c0, basis.c1), basis.c1));
square.c2 = vector3_minus(basis.c2, vector3_scale(vector3_dot(basis.c0, basis.c2), basis.c2));
square.c2 = vector3_minus(
square.c2, vector3_scale(vector3_dot(basis.c0, square.c2), unit_vector3(square.c2)));
square.c0 = vector3_scale(size.x, square.c0);
square.c1 = vector3_scale(size.y, square.c1);
square.c2 = vector3_scale(size.z, square.c2);
return matrix3x3_mult(matrix3x3_inverse(basis), square);
}
/**************************************************************************/
/* compute the 3d volume enclosed by a geometric object o. */
double geom_object_volume(GEOMETRIC_OBJECT o) {
switch (o.which_subclass) {
case GEOM SPHERE: {
number radius = o.subclass.sphere_data->radius;
return (1.333333333333333333 * K_PI) * radius * radius * radius;
}
case GEOM CYLINDER: {
number height = o.subclass.cylinder_data->height;
number radius = o.subclass.cylinder_data->radius;
number radius2 = o.subclass.cylinder_data->which_subclass == CYL CONE
? o.subclass.cylinder_data->subclass.cone_data->radius2
: radius;
double vol = height * (K_PI / 3) * (radius * radius + radius * radius2 + radius2 * radius2);
if (o.subclass.cylinder_data->which_subclass == CYL WEDGE)
return vol * fabs(o.subclass.cylinder_data->subclass.wedge_data->wedge_angle) / (2 * K_PI);
else
return vol;
}
case GEOM BLOCK: {
vector3 size = o.subclass.block_data->size;
double vol = size.x * size.y * size.z *
fabs(matrix3x3_determinant(geometry_lattice.basis) /
matrix3x3_determinant(o.subclass.block_data->projection_matrix));
return o.subclass.block_data->which_subclass == BLK BLOCK_SELF ? vol : vol * (K_PI / 6);
}
case GEOM PRISM: {
return get_prism_volume(o.subclass.prism_data);
}
default: return 0; /* unsupported object types? */
}
}
/**************************************************************************/
/**************************************************************************/
/* Fast geometry routines */
/* Using the above material_of_point routine is way too slow, especially
when there are lots of objects to test. Thus, we develop the following
replacement routines.
The basic idea here is twofold. (1) Compute bounding boxes for
each geometric object, for which inclusion tests can be computed
quickly. (2) Build a tree that recursively breaks down the unit cell
in half, allowing us to perform searches in logarithmic time. */
/**************************************************************************/
/* geom_box utilities: */
static void geom_box_union(geom_box *bu, const geom_box *b1, const geom_box *b2) {
bu->low.x = MIN(b1->low.x, b2->low.x);
bu->low.y = MIN(b1->low.y, b2->low.y);
bu->low.z = MIN(b1->low.z, b2->low.z);
bu->high.x = MAX(b1->high.x, b2->high.x);
bu->high.y = MAX(b1->high.y, b2->high.y);
bu->high.z = MAX(b1->high.z, b2->high.z);
}
static void geom_box_intersection(geom_box *bi, const geom_box *b1, const geom_box *b2) {
bi->low.x = MAX(b1->low.x, b2->low.x);
bi->low.y = MAX(b1->low.y, b2->low.y);
bi->low.z = MAX(b1->low.z, b2->low.z);
bi->high.x = MIN(b1->high.x, b2->high.x);
bi->high.y = MIN(b1->high.y, b2->high.y);
bi->high.z = MIN(b1->high.z, b2->high.z);
}
static void geom_box_add_pt(geom_box *b, vector3 p) {
b->low.x = MIN(b->low.x, p.x);
b->low.y = MIN(b->low.y, p.y);
b->low.z = MIN(b->low.z, p.z);
b->high.x = MAX(b->high.x, p.x);
b->high.y = MAX(b->high.y, p.y);
b->high.z = MAX(b->high.z, p.z);
}
#define BETWEEN(x, low, high) ((x) >= (low) && (x) <= (high))
static int geom_box_contains_point(const geom_box *b, vector3 p) {
return (BETWEEN(p.x, b->low.x, b->high.x) && BETWEEN(p.y, b->low.y, b->high.y) &&
BETWEEN(p.z, b->low.z, b->high.z));
}
/* return whether or not the given two boxes intersect */
static int geom_boxes_intersect(const geom_box *b1, const geom_box *b2) {
/* true if the x, y, and z ranges all intersect. */
return (
(BETWEEN(b1->low.x, b2->low.x, b2->high.x) || BETWEEN(b1->high.x, b2->low.x, b2->high.x) ||
BETWEEN(b2->low.x, b1->low.x, b1->high.x)) &&
(BETWEEN(b1->low.y, b2->low.y, b2->high.y) || BETWEEN(b1->high.y, b2->low.y, b2->high.y) ||
BETWEEN(b2->low.y, b1->low.y, b1->high.y)) &&
(BETWEEN(b1->low.z, b2->low.z, b2->high.z) || BETWEEN(b1->high.z, b2->low.z, b2->high.z) ||
BETWEEN(b2->low.z, b1->low.z, b1->high.z)));
}
static void geom_box_shift(geom_box *b, vector3 shiftby) {
b->low = vector3_plus(b->low, shiftby);
b->high = vector3_plus(b->high, shiftby);
}
/**************************************************************************/
/* Computing a bounding box for a geometric object: */
/* compute | (b x c) / (a * (b x c)) |, for use below */
static number compute_dot_cross(vector3 a, vector3 b, vector3 c) {
vector3 bxc = vector3_cross(b, c);
return fabs(vector3_norm(bxc) / vector3_dot(a, bxc));
}