-
Notifications
You must be signed in to change notification settings - Fork 31
/
volumes.cpp
1383 lines (1101 loc) · 43.5 KB
/
volumes.cpp
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
#include <iostream>
#include <map>
#include <cfloat>
#include "mcnp2cad.hpp"
#include "MCNPInput.hpp"
#include "volumes.hpp"
#include "geometry.hpp"
#include "options.hpp"
#include "GQ_Characterize.hpp"
static Vector3d origin(0,0,0);
iBase_EntityHandle makeWorldSphere( iGeom_Instance& igm, double world_size ){
iBase_EntityHandle world_sphere;
int igm_result;
// Note: I tried using createBrick instead of createSphere to bound the universe with a box
// instead of a sphere. This worked but led to a substantial increase in run times and
// memory usage, so should be avoided.
iGeom_createSphere( igm, world_size, &world_sphere, &igm_result);
CHECK_IGEOM( igm_result, "making world sphere" );
return world_sphere;
}
/**
* A convenience function for SurfaceVolumes to call at the end of getHandle functions.
* Return the negative or positive sense of the given body, as appropriate. If
* bound_within_world is true, a negative-sense body will be intersected with the world sphere
* (a step necessary for cylinders and other infinite bodies)
*/
static iBase_EntityHandle embedWithinWorld( bool positive, iGeom_Instance& igm, double world_size,
iBase_EntityHandle body, bool bound_with_world )
{
iBase_EntityHandle final_body;
int igm_result;
if( !positive && !bound_with_world ){
final_body = body;
}
else{
iBase_EntityHandle world_sphere = makeWorldSphere( igm, world_size );
if( positive ){
iGeom_subtractEnts( igm, world_sphere, body, &final_body, &igm_result);
CHECK_IGEOM( igm_result, "making positive body" );
}
else{ // !positive && bound_with_world
iGeom_intersectEnts( igm, world_sphere, body, &final_body, &igm_result);
CHECK_IGEOM( igm_result, "making negative body" );
}
}
return final_body;
}
iBase_EntityHandle applyTransform( const Transform& t, iGeom_Instance& igm, iBase_EntityHandle& e ) {
int igm_result;
if( t.hasRot() ){
const Vector3d& axis = t.getAxis();
iGeom_rotateEnt( igm, e, t.getTheta(), axis.v[0], axis.v[1], axis.v[2], &igm_result );
CHECK_IGEOM( igm_result, "applying rotation" );
}
if( t.hasInversion() ){
//TODO: ask about the right way to do this. Rotate seems to work, but I don't really know why...
//iGeom_rotateEnt( e, 180, 0, 0, 0, &igm_result );
//if( !t.hasRot() ){
iGeom_reflectEnt( igm, e, 0, 0, 0, 0, 0, 1, &igm_result );
iGeom_reflectEnt( igm, e, 0, 0, 0, 0, 1, 0, &igm_result );
iGeom_reflectEnt( igm, e, 0, 0, 0, 1, 0, 0, &igm_result );
//}
//else{
// const Vector3d& axis = t.getAxis();
// iGeom_reflectEnt( igm, e, axis.v[0], axis.v[1], axis.v[2], &igm_result );
//}
//CHECK_IGEOM( igm_result, "inverting for transformation" );
}
const Vector3d& translation = t.getTranslation();
iGeom_moveEnt( igm, e, translation.v[0], translation.v[1], translation.v[2], &igm_result);
CHECK_IGEOM( igm_result, "applying translation" );
return e;
}
iBase_EntityHandle applyReverseTransform( const Transform& tx, iGeom_Instance& igm, iBase_EntityHandle& e ) {
int igm_result;
Transform rev_t = tx.reverse();
const Vector3d& translation = rev_t.getTranslation();
iGeom_moveEnt( igm, e, translation.v[0], translation.v[1], translation.v[2], &igm_result);
CHECK_IGEOM( igm_result, "applying reverse translation" );
if( rev_t.hasInversion() ){
iGeom_rotateEnt( igm, e, 180, 0, 0, 0, &igm_result );
CHECK_IGEOM( igm_result, "inverting for reverse transformation" );
}
if( rev_t.hasRot() ){
const Vector3d& axis = rev_t.getAxis();
iGeom_rotateEnt( igm, e, rev_t.getTheta(), axis.v[0], axis.v[1], axis.v[2], &igm_result );
CHECK_IGEOM( igm_result, "applying rotation" );
}
return e;
}
iBase_EntityHandle SurfaceVolume::define( bool positive, iGeom_Instance& igm, double world_size ){
iBase_EntityHandle handle = this->getHandle( positive, igm, world_size );
if( transform ){
handle = applyTransform( *transform, igm, handle );
}
return handle;
}
class PlaneSurface : public SurfaceVolume {
protected:
Vector3d normal;
double offset;
public:
PlaneSurface( const Vector3d& normal_p, const Vector3d& pos, bool end ) :
SurfaceVolume(), normal(normal_p)
{
//This is used to find D in the planar equation Ax + By + Cz - D = 0 when
//given two vectors.
//This comes from the form A(x-a) + B(y-b) + C(z-c) = 0, so D = Aa + Bb + Cc.
//normal is the vector to which the plane will be perpendicular, pos is to
//help determine the position. If end is true, the plane the end of normal
//in relation to the end of pos1 will intersect, otherwise the plane and the
//end of pos will intersect.
double D;
if( end ){
D = normal.v[0]*( pos.v[0] + normal.v[0] ) + normal.v[1]*( pos.v[1] + normal.v[1] ) + normal.v[2]*( pos.v[2] + normal.v[2] );
}
else{
D = normal.v[0]*pos.v[0] + normal.v[1]*pos.v[1] + normal.v[2]*pos.v[2];
}
offset = D/normal.length() ;
}
PlaneSurface( const Vector3d& normal_p, double offset_p ) :
SurfaceVolume(), normal(normal_p), offset(offset_p)
{}
virtual double getFarthestExtentFromOrigin() const{
// this is a funny situation, since planes are technically infinte...
// in order to have a sane answer, we just return the offset from the origin.
// (multiplied by root 3, which was done in the old converter, why?)
return sqrt(3.0) * std::fabs(offset);
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size){
int igm_result;
iBase_EntityHandle world_sphere = makeWorldSphere(igm, world_size);
iBase_EntityHandle hemisphere;
// note the reversal of sense in this call; mcnp and igeom define it differently.
iGeom_sectionEnt( igm, world_sphere,
normal.v[0], normal.v[1], normal.v[2], offset, !positive, &hemisphere, &igm_result);
CHECK_IGEOM( igm_result, "Sectioning world for a plane" );
return hemisphere;
}
};
typedef enum { X=0, Y=1, Z=2 } axis_t;
class GeneralQuadraticSurface : public SurfaceVolume, public GQ_Characterize {
protected:
// principle axes extents of the GQ
double extents[3];
public:
GeneralQuadraticSurface(double A, double B, double C, double D, double E, double F, double G, double H, double J, double K):
SurfaceVolume(),GQ_Characterize(A,B,C,D,E,F,G,H,J,K) {
transform_ = Transform(rotation_mat, translation);
}
virtual double getFarthestExtentFromOrigin() const{ return 0; }
protected:
Transform transform_;
iBase_EntityHandle ellipsoid(iGeom_Instance &igm) {
int igm_result;
iBase_EntityHandle gq_handle;
double radius = 1;
iGeom_createSphere( igm, radius, &gq_handle, &igm_result);
CHECK_IGEOM( igm_result, "making sphere" );
iGeom_scaleEnt( igm, gq_handle, 0, 0, 0, sqrt(-K_/A_),sqrt(-K_/B_),sqrt(-K_/C_), &igm_result);
CHECK_IGEOM( igm_result, "scaling sphere to ellipsoid" );
return gq_handle;
}
iBase_EntityHandle elliptic_cyl(iGeom_Instance &igm, double world_size) {
int igm_result;
double r1,r2;
int axis;
//figure out which direction is zero
if (A_ == 0) {
axis = 0;
r1 = sqrt(fabs(K_/C_));
r2 = sqrt(fabs(K_/B_));
}
else if (B_ == 0) {
axis = 1;
r1 = sqrt(fabs(K_/A_));
r2 = sqrt(fabs(K_/C_));
}
else if (C_ == 0) {
axis = 2;
r1 = sqrt(fabs(K_/A_));
r2 = sqrt(fabs(K_/B_));
}
iBase_EntityHandle cyl;
iGeom_createCylinder(igm,2*world_size,r1,r2,&cyl,&igm_result);
CHECK_IGEOM(igm_result, "Creating elliptic cylinder for GQ.");
if (1 == axis) {
iGeom_rotateEnt(igm,cyl,90,1,0,0,&igm_result);
}
else if (0 == axis) {
iGeom_rotateEnt(igm,cyl,90,0,1,0,&igm_result);
}
else if (2 == axis) {
igm_result = iBase_SUCCESS;
}
CHECK_IGEOM(igm_result, "Rotating canonical elliptic cylinder into place.");
return cyl;
}
iBase_EntityHandle elliptic_cone(iGeom_Instance& igm, double world_size) {
if( 0 == A_ || 0 == B_ || 0 == C_ ){
if( OPT_DEBUG ){
record << "Error in GeneralQuadraticSurface::elliptic_cone(double world_size) in volumes.cpp" << std::endl;
record << "0 == A_ || 0 == B_ || 0 == C_" << std::endl;
}
throw std::runtime_error("Error in definition of elliptic cone.");
}
iBase_EntityHandle gq_handle;
int igm_result=0;
double minor_radius,major_radius,rot_angle;
int rot_axis;
//establish orientation
if (A_ < 0) {
minor_radius = 2*world_size*sqrt(-A_/C_);
major_radius = 2*world_size*sqrt(-A_/B_);
rot_angle = -90;
rot_axis = 1;
}
else if (B_ < 0) {
minor_radius = 2*world_size*sqrt(-B_/A_);
major_radius = 2*world_size*sqrt(-B_/C_);
rot_angle = 90;
rot_axis = 0;
}
else if (C_ < 0) {
minor_radius = 2*world_size*sqrt(-C_/A_);
major_radius = 2*world_size*sqrt(-C_/B_);
rot_angle = 180;
rot_axis = 0;
}
//create cone
iBase_EntityHandle pos_cone;
iGeom_createCone( igm, 2*world_size, major_radius, minor_radius, 0, &pos_cone, &igm_result);
CHECK_IGEOM(igm_result, "Creating positive cone for GQ.");
//now move the cone s.t. the point is on the origin
iGeom_moveEnt( igm, pos_cone, 0, 0, -world_size, &igm_result );
CHECK_IGEOM(igm_result, "Moving positive cone for GQ.");
double rot_vec[3] = {0,0,0};
rot_vec[rot_axis] = 1;
//rotate to proper axis
iGeom_rotateEnt( igm, pos_cone, rot_angle, rot_vec[0], rot_vec[1], rot_vec[2], &igm_result );
CHECK_IGEOM(igm_result, "Rotating positive cone for GQ.");
//create a copy
iBase_EntityHandle neg_cone;
iGeom_copyEnt( igm, pos_cone, &neg_cone, &igm_result );
CHECK_IGEOM(igm_result, "Copying positive cone for GQ.");
iGeom_rotateEnt( igm, neg_cone, 180, rot_vec[0], rot_vec[1], rot_vec[2], &igm_result );
CHECK_IGEOM(igm_result, "Rotating negative cone for GQ.");
iBase_EntityHandle cones[2] = {pos_cone, neg_cone};
iGeom_uniteEnts( igm, cones, 2, &gq_handle, &igm_result );
CHECK_IGEOM(igm_result, "Uniting positive and negative cones for GQ.");
return gq_handle;
}
virtual iBase_EntityHandle getHandle(bool positive, iGeom_Instance& igm, double world_size) {
iBase_EntityHandle gq;
switch(type){
case ELLIPSOID:
gq = ellipsoid(igm);
break;
case ELLIPTIC_CONE:
gq = elliptic_cone( igm, world_size);
break;
case ELLIPTIC_CYL:
gq = elliptic_cyl( igm, world_size);
break;
default:
record << "GQ type is currently unsupported" << std::endl;
}
//re-orient gq into original position
Transform rotation_transform(rotation_mat, Vector3d(0,0,0));
applyReverseTransform(rotation_transform, igm, gq);
Transform translation_transform(translation);
applyTransform(translation_transform, igm, gq);
iBase_EntityHandle final_gq = embedWithinWorld(-positive, igm, world_size, gq, true);
return final_gq;
}
};
class CylinderSurface : public SurfaceVolume {
protected:
axis_t axis;
double radius;
Vector3d center;
bool onaxis;
public:
CylinderSurface( axis_t axis_p, double radius_p ):
SurfaceVolume(), axis(axis_p), radius(radius_p), center(origin), onaxis(true)
{}
CylinderSurface( axis_t axis_p, double radius_p, double trans1, double trans2 ):
SurfaceVolume(), axis(axis_p), radius(radius_p), center(origin), onaxis(false)
{
switch(axis){
case X: center.v[Y] += trans1; center.v[Z] += trans2; break;
case Y: center.v[X] += trans1; center.v[Z] += trans2; break;
case Z: center.v[X] += trans1; center.v[Y] += trans2; break;
}
}
virtual double getFarthestExtentFromOrigin( ) const{
return radius + center.length();
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle cylinder;
iGeom_createCylinder( igm, 2.0 * world_size, radius, 0, &cylinder, &igm_result);
CHECK_IGEOM( igm_result, "making cylinder" );
if( axis == X ){
iGeom_rotateEnt( igm, cylinder, 90, 0, 1, 0, &igm_result );
CHECK_IGEOM( igm_result, "rotating cylinder (X)" );
}
else if( axis == Y ){
iGeom_rotateEnt( igm, cylinder, 90, 1, 0, 0, &igm_result );
CHECK_IGEOM( igm_result, "rotating cylinder (Y)" );
}
if( onaxis == false ){
iGeom_moveEnt( igm, cylinder, center.v[0], center.v[1], center.v[2], &igm_result);
CHECK_IGEOM( igm_result, "moving cylinder" );
}
iBase_EntityHandle final_cylinder = embedWithinWorld( positive, igm, world_size, cylinder, true );
return final_cylinder;
};
};
#ifdef HAVE_IGEOM_CONE
class ConeSurface : public SurfaceVolume {
protected:
enum nappe {LEFT=-1, BOTH=0, RIGHT=1};
static enum nappe make_nappe( double param ){
int wrapper = static_cast<int>(param);
enum nappe n = static_cast<enum nappe>(wrapper);
//enum nappe n = static_cast<enum nappe>(param);
if( -1 <= n && n <= 1 ){
return n;
}
else{
std::cerr << "WARNING: Bad cylinder +/-1 argument: " << param << std::endl;
std::cerr << " will pretend it was really 0" << std::endl;
record << "WARNING: Bad cylinder +/-1 argument: " << param << std::endl;
record << " will pretend it was really 0" << std::endl;
return BOTH;
}
}
axis_t axis;
double theta; /// the cone's opening angle
Vector3d center; /// the cone's apex
bool onaxis;
enum nappe nappe;
public:
ConeSurface( axis_t axis_p, double tsquared_p, double point_p, double nappe_p ):
SurfaceVolume(), axis(axis_p), theta( atan(sqrt(tsquared_p)) ), center(origin), onaxis(true), nappe(make_nappe(nappe_p))
{
center.v[axis] = point_p;
}
ConeSurface( axis_t axis_p, double tsquared_p, Vector3d center_p, double nappe_p ):
SurfaceVolume(), axis(axis_p), theta( atan(sqrt(tsquared_p)) ), center(center_p), onaxis(false), nappe(make_nappe(nappe_p))
{}
virtual double getFarthestExtentFromOrigin( ) const{
return sqrt(3) + ( center.length() );
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm,double world_size ){
double height = (center.length() + world_size);
// based on the textual descriptions in the manual, I think the following expression should be
// height * tan ( theta / 2 ) -- unless "opening angle" refers to only half the apex angle
// of the cylinder. But this implementation seems to be more correct in examples I can check against.
double base_radius = height * tan( theta );
int igm_result;
iBase_EntityHandle right_nappe = 0;
iBase_EntityHandle left_nappe = 0;
iBase_EntityHandle cone;
if( nappe != LEFT){
iGeom_createCone( igm, height, base_radius, 0, 0, &right_nappe, &igm_result);
CHECK_IGEOM( igm_result, "making cone (right nappe)" );
iGeom_rotateEnt( igm, right_nappe, 180, 1, 0, 0, &igm_result);
CHECK_IGEOM( igm_result, "Rotating cone (right nappe)");
iGeom_moveEnt( igm, right_nappe, 0, 0, height/2.0, &igm_result );
CHECK_IGEOM( igm_result, "Moving cone (right nappe)");
cone = right_nappe;
}
if( nappe != RIGHT ){
iGeom_createCone( igm, height, base_radius, 0, 0, &left_nappe, &igm_result );
CHECK_IGEOM( igm_result, "making cone (left nappe)" );
iGeom_moveEnt( igm, left_nappe, 0, 0, -height/2.0, &igm_result );
CHECK_IGEOM( igm_result, "Moving cone (left nappe)" );
cone = left_nappe;
}
if( right_nappe && left_nappe ){
iBase_EntityHandle nappes[2] = {right_nappe, left_nappe};
iGeom_uniteEnts( igm, nappes, 2, &cone, &igm_result );
CHECK_IGEOM( igm_result, "Unioning cone nappes" );
}
if( axis == X ){
iGeom_rotateEnt( igm, cone, 90, 0, 1, 0, &igm_result );
CHECK_IGEOM( igm_result, "rotating cone (X)" );
}
else if( axis == Y ){
iGeom_rotateEnt( igm, cone, -90, 1, 0, 0, &igm_result );
CHECK_IGEOM( igm_result, "rotating cone (Y)" );
}
iGeom_moveEnt( igm, cone, center.v[0], center.v[1], center.v[2], &igm_result);
CHECK_IGEOM( igm_result, "moving cone to its apex" );
iBase_EntityHandle final_cone = embedWithinWorld( positive, igm, world_size, cone, true );
return final_cone;
}
};
#endif /* HAVE_IGEOM_CONE */
class TorusSurface : public SurfaceVolume {
protected:
Vector3d center;
axis_t axis;
double radius;
double ellipse_axis_rad;
double ellipse_perp_rad;
public:
TorusSurface( axis_t axis_p, const Vector3d& center_p, double A, double B, double C ) :
center(center_p), axis( axis_p ), radius(A), ellipse_axis_rad( B ), ellipse_perp_rad( C )
{}
virtual double getFarthestExtentFromOrigin ( ) const {
return center.length() + radius + std::max( ellipse_axis_rad, ellipse_perp_rad );
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle torus;
iGeom_createTorus( igm, radius, ellipse_perp_rad, &torus, &igm_result );
CHECK_IGEOM( igm_result, "Creating initial torus");
if( ellipse_axis_rad != ellipse_perp_rad ){
double scalef = fabs(ellipse_axis_rad) / fabs(ellipse_perp_rad);
iGeom_scaleEnt( igm, torus, 0, 0, 0, 1.0, 1.0, scalef, &igm_result );
CHECK_IGEOM( igm_result, "Scaling torus" );
}
if( axis == X ){
iGeom_rotateEnt( igm, torus, 90, 0, 1, 0, &igm_result );
CHECK_IGEOM( igm_result, "rotating torus (X)" );
}
else if( axis == Y ){
iGeom_rotateEnt( igm, torus, -90, 1, 0, 0, &igm_result );
CHECK_IGEOM( igm_result, "rotating torus (Y)" );
}
iGeom_moveEnt( igm, torus, center.v[0], center.v[1], center.v[2], &igm_result);
CHECK_IGEOM( igm_result, "moving torus to its center point" );
iBase_EntityHandle final_torus = embedWithinWorld( positive, igm, world_size, torus, false );
return final_torus;
}
};
class SphereSurface : public SurfaceVolume {
protected:
Vector3d center;
double radius;
public:
SphereSurface( const Vector3d& center_p, double radius_p ) :
SurfaceVolume(), center(center_p), radius(radius_p)
{}
virtual ~SphereSurface(){}
virtual double getFarthestExtentFromOrigin ( ) const {
return (center.length() + radius);
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle sphere;
iGeom_createSphere( igm, radius, &sphere, &igm_result);
CHECK_IGEOM( igm_result, "making sphere" );
iGeom_moveEnt( igm, sphere, center.v[0], center.v[1], center.v[2], &igm_result );
CHECK_IGEOM( igm_result, "moving sphere" );
iBase_EntityHandle final_sphere = embedWithinWorld( positive, igm, world_size, sphere, false );
return final_sphere;
}
};
class EllipsoidSurface : public SurfaceVolume {
protected:
Vector3d center;
Vector3d axes;
public:
EllipsoidSurface( const Vector3d& center_p, const Vector3d& axes_p ) :
SurfaceVolume(), center(center_p), axes(axes_p)
{}
virtual ~EllipsoidSurface(){}
virtual double getFarthestExtentFromOrigin ( ) const {
return (center.length() + axes.length());
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle sphere;
double radius = 1;
iGeom_createSphere( igm, radius, &sphere, &igm_result);
CHECK_IGEOM( igm_result, "making sphere" );
iGeom_scaleEnt( igm, sphere, 0, 0, 0, sqrt(1/axes.v[0]), sqrt(1/axes.v[1]), sqrt(1/axes.v[2]), &igm_result);
CHECK_IGEOM( igm_result, "scaling sphere to ellipsoid" );
iGeom_moveEnt( igm, sphere, center.v[0], center.v[1], center.v[2], &igm_result );
CHECK_IGEOM( igm_result, "moving sphere" );
iBase_EntityHandle final_sphere = embedWithinWorld( positive, igm, world_size, sphere, false );
return final_sphere;
}
};
static Transform axesImage( const Vector3d& v1, const Vector3d& v2, const Vector3d &v3, const Vector3d& translation = Vector3d() )
{
Vector3d x(1, 0, 0), y(0, 1, 0), z(0, 0, 1);
Vector3d a1 = v1.normalize(), a2 = v2.normalize(), a3 = v3.normalize();
if( OPT_DEBUG ) record << "Axes image: " << a1 << " : " << a2 << " : " << a3 << std::endl;
double rot_matrix[9] =
{ a1.dot(x), a2.dot(x), a3.dot(x),
a1.dot(y), a2.dot(y), a3.dot(y),
a1.dot(z), a2.dot(z), a3.dot(z) };
return Transform( rot_matrix, translation );
}
static Transform imageZAxisTo( const Vector3d& v, const Vector3d& translation = Vector3d() ){
// approach: find two vectors perpendicular to v, then use axesImage.
Vector3d a = v.normalize();
Vector3d x(1, 0, 0);
Vector3d b = x.cross( v.normalize() );
if( b.length() < DBL_EPSILON ){
// v is indistinguishable from the x axis
b = Vector3d(0, -1, 0);
if( OPT_DEBUG ) record << "imageZAxisTo: Changing v " << std::endl;
}
Vector3d c = b.cross( v.normalize() );
return axesImage( c, b, a, translation );
}
class BoxVolume : public SurfaceVolume {
protected:
Vector3d dimensions;
Transform transform;
public:
BoxVolume( const Vector3d& corner, const Vector3d& v1, const Vector3d& v2, const Vector3d& v3 ) :
dimensions( v1.length(), v2.length(), v3.length() ), transform( axesImage(v1,v2,v3,corner) )
{}
virtual double getFarthestExtentFromOrigin ( ) const {
return transform.getTranslation().length() + dimensions.length();
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle box;
iGeom_createBrick( igm, dimensions.v[0], dimensions.v[1], dimensions.v[2], &box, &igm_result );
CHECK_IGEOM( igm_result, "making box" );
Vector3d halfdim = dimensions.scale( 1.0 / 2.0 );
iGeom_moveEnt( igm, box, halfdim.v[0], halfdim.v[1], halfdim.v[2], &igm_result );
CHECK_IGEOM( igm_result, "moving box (halfdim)" );
box = applyTransform( transform, igm, box );
iBase_EntityHandle final_box = embedWithinWorld( positive, igm, world_size, box, false );
return final_box;
}
};
class RppVolume : public SurfaceVolume {
protected:
Vector3d dimensions;
Vector3d center_offset;
public:
RppVolume( const Vector3d& lower, const Vector3d& upper )
{
for( int i = 0; i < 3; ++i ){
dimensions.v[i] = upper.v[i] - lower.v[i];
center_offset.v[i] = (upper.v[i]+lower.v[i]) / 2.0;
}
}
virtual double getFarthestExtentFromOrigin ( ) const {
return dimensions.length() + center_offset.length();
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle rpp;
iGeom_createBrick( igm, dimensions.v[0], dimensions.v[1], dimensions.v[2], &rpp, &igm_result );
CHECK_IGEOM( igm_result, "making rpp" );
iGeom_moveEnt( igm, rpp, center_offset.v[0], center_offset.v[1], center_offset.v[2], &igm_result );
CHECK_IGEOM( igm_result, "moving rpp" );
iBase_EntityHandle final_rpp = embedWithinWorld( positive, igm, world_size, rpp, false );
return final_rpp;
}
};
class RecVolume : public SurfaceVolume {
protected:
Vector3d base_center;
Transform transform;
double length, radius1, radius2;
bool facet;
public:
RecVolume( const Vector3d& center_p, const Vector3d& axis, const Vector3d& v1, const Vector3d& v2, bool facet_p ) :
base_center( center_p ), transform( axesImage( v1, v2, axis, center_p ) ),
length( axis.length() ), radius1( v1.length() ), radius2( v2.length() ), facet( facet_p)
{}
RecVolume( const Vector3d& center_p, const Vector3d& axis, const Vector3d& v1, double length2, bool facet_p ) :
base_center( center_p ), transform( axesImage( v1, v1.cross(axis), axis, center_p ) ),
length( axis.length() ), radius1( v1.length() ), radius2( length2 ), facet( facet_p )
{}
virtual double getFarthestExtentFromOrigin ( ) const {
return base_center.length() + length + std::max( radius1, radius2 );
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle rec;
if( facet ){
iGeom_createCylinder( igm, 2.0 * world_size + length / 2.0, radius1, radius2, &rec, &igm_result );
}
else{
iGeom_createCylinder( igm, length, radius1, radius2, &rec, &igm_result );
}
CHECK_IGEOM( igm_result, "creating rec" );
double movement_factor = length / 2.0;
iGeom_moveEnt( igm, rec, 0, 0, movement_factor, &igm_result );
CHECK_IGEOM( igm_result, "moving rec" );
rec = applyTransform( transform, igm, rec );
iBase_EntityHandle final_rec = embedWithinWorld( positive, igm, world_size, rec, false );
return final_rec;
}
};
class RccVolume : public SurfaceVolume {
protected:
Vector3d base_center;
Transform transform;
double length, radius;
bool facet;
public:
RccVolume( const Vector3d& center_p, const Vector3d& axis, double radius_p, bool facet_p ) :
base_center( center_p ), transform( imageZAxisTo( axis, center_p ) ), length( axis.length() ), radius(radius_p), facet( facet_p )
{}
virtual double getFarthestExtentFromOrigin ( ) const {
return base_center.length() + length + radius;
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle rcc;
if( facet ){
iGeom_createCylinder( igm, 2.0 * world_size, radius, 0, &rcc, &igm_result );
}
else{
iGeom_createCylinder( igm, length, radius, 0, &rcc, &igm_result );
}
CHECK_IGEOM( igm_result, "creating rcc" );
double movement_factor = length / 2.0;
iGeom_moveEnt( igm, rcc, 0, 0, movement_factor, &igm_result );
CHECK_IGEOM( igm_result, "moving rcc" );
rcc = applyTransform( transform, igm, rcc );
iBase_EntityHandle final_rcc = embedWithinWorld( positive, igm, world_size, rcc, false );
return final_rcc;
}
};
#ifdef HAVE_IGEOM_CONE
class TrcVolume : public SurfaceVolume {
protected:
Vector3d base_center;
Transform transform;
double length, radius1, radius2;
public:
TrcVolume( const Vector3d& center_p, const Vector3d& axis, double radius1_p, double radius2_p ) :
base_center( center_p ), transform( imageZAxisTo( axis, center_p ) ), length( axis.length() ),
radius1(radius1_p), radius2(radius2_p)
{}
virtual double getFarthestExtentFromOrigin ( ) const {
return base_center.length() + length + std::max(radius1,radius2);
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle trc;
iGeom_createCone( igm, length, radius1, 0, radius2, &trc, &igm_result );
CHECK_IGEOM( igm_result, "creating trc" );
iGeom_moveEnt( igm, trc, 0, 0, length / 2.0, &igm_result );
CHECK_IGEOM( igm_result, "moving trc" );
trc = applyTransform( transform, igm, trc );
iBase_EntityHandle final_trc = embedWithinWorld( positive, igm, world_size, trc, false );
return final_trc;
}
};
#endif
class HexVolume : public SurfaceVolume {
protected:
Vector3d base_center;
Vector3d heightV, RV, SV, TV;
//Transform transform;
public:
HexVolume( const Vector3d& center_p, const Vector3d& h_p, const Vector3d& r_p,
const Vector3d& s_p, const Vector3d& t_p ) :
base_center(center_p), heightV(h_p), RV(r_p), SV(s_p), TV(t_p)
{}
HexVolume( const Vector3d& center_p, const Vector3d& h_p, const Vector3d& r_p ) :
base_center(center_p), heightV(h_p), RV(r_p), SV( r_p.rotate_about(h_p,60.0) ), TV( r_p.rotate_about(h_p,120.0) )
{
if( OPT_DEBUG ){
record << "Inferred vectors for 9-args HEX/RHP:" << RV << SV << TV << std::endl;
}
}
virtual double getFarthestExtentFromOrigin ( ) const {
double hex_max = std::max( RV.length(), std::max( SV.length(), TV.length() ) );
return base_center.length() + heightV.length() + hex_max;
}
protected:
virtual iBase_EntityHandle getHandle( bool positive, iGeom_Instance& igm, double world_size ){
int igm_result;
iBase_EntityHandle hex;
hex = makeWorldSphere( igm, world_size );
Vector3d b = - heightV.normalize();
iGeom_sectionEnt( igm, hex, b.v[0], b.v[1], b.v[2], 0, true, &hex, &igm_result );
CHECK_IGEOM( igm_result, "Sectioning world for a hex (1)" );
b = -b;
iGeom_sectionEnt( igm, hex, b.v[0], b.v[1], b.v[2], heightV.length(), true, &hex, &igm_result );
CHECK_IGEOM( igm_result, "Sectioning world for a hex (2)" );
const Vector3d* vec[3] = {&RV, &SV, &TV};
for( int i = 0; i < 3; ++i ){
Vector3d v = *(vec[i]);
double length = v.length();
v = v.normalize();
iGeom_sectionEnt( igm, hex, v.v[0], v.v[1], v.v[2], length, true, &hex, &igm_result );
CHECK_IGEOM( igm_result, "Sectioning world for a hex (3)" );
v = -v;
iGeom_sectionEnt( igm, hex, v.v[0], v.v[1], v.v[2], length, true, &hex, &igm_result );
CHECK_IGEOM( igm_result, "Sectioning world for a hex (4)" );
}
iGeom_moveEnt( igm, hex, base_center.v[0], base_center.v[1], base_center.v[2], &igm_result );
CHECK_IGEOM( igm_result, "Moving hex" );
iBase_EntityHandle final_hex = embedWithinWorld( positive, igm, world_size, hex, false );
return final_hex;
}
};
class VolumeCache{
protected:
std::map<const SurfaceCard*,SurfaceVolume*> mapping;
public:
VolumeCache(){}
bool contains( const SurfaceCard* c ) const {
return ( mapping.find(c) != mapping.end() );
}
SurfaceVolume* get( const SurfaceCard* c ) {
if( !contains( c ) ){
if( OPT_DEBUG ){
record << "Error in VolumeCache::get( const SurfaceCard* c) in volumes.cpp" << std::endl;
record << "!contains( c )" << std::endl;
}
throw std::runtime_error("Surface card not found in surface volume.");
}
return (*(mapping.find(c))).second;
}
void insert( const SurfaceCard* c, SurfaceVolume* s ){
mapping[c] = s;
}
};
bool sqIsEllipsoid(const std::vector< double >& args) {
bool isEllipsoid = true;
if ( args.at(3)*args.at(3) + args.at(4)*args.at(4) + args.at(5)*args.at(5) != 0)
isEllipsoid = false;
if ( args.at(0)*args.at(1)*args.at(2)*args.at(6) >= 0 )
isEllipsoid = false;
return isEllipsoid;