forked from richardjgowers/zeoplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray.cc
1428 lines (1224 loc) · 46 KB
/
ray.cc
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 "network.h"
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <voro++.hh>
#include "ray.h"
#include "zeo_consts.h"
#include "channel.h"
#include "area_and_volume.h"
#include "network.h"
#include "networkaccessibility.h"
using namespace std;
using namespace voro;
/** Using |Point_0 - center| - radius^2 = 0 and Point_0 = t(Ray_dir) + Ray_pnt. Solve
for t which is the distance. **/
void Sphere::hitSphere(ray r,hitdata& hitsphere)
{
Point newCenter = center-r.base; //map the rand_ray to the origin and move system
double temp=(r.vector*newCenter)*(r.vector*newCenter)-(newCenter*newCenter)+radius*radius;
if (temp>0) //check inside of sqrt is not negative
{
if ((r.vector * newCenter) - sqrt(temp) > 0) //If it hits the sphere twice pick the closest
{
hitsphere.hit=true;
hitsphere.dist=(r.vector*newCenter) - sqrt(temp);
hitsphere.hitpoint=r.base+r.vector.scale(hitsphere.dist);
}
else if ((r.vector * newCenter) + sqrt(temp) > 0) //Ray was inside sphere (this is possible in some cases)
{
hitsphere.hit=true;
hitsphere.dist=(r.vector*newCenter) + sqrt(temp);
hitsphere.hitpoint=r.base+r.vector.scale(hitsphere.dist);
}
}
return;
}
/** Using normal dot (Point_Plane - Point_0| = 0 and Point_Plane = t(Ray_dir) + Ray_pnt. Solve for t which is the distance to plane. **/
void Plane::hitPlane(ray r,hitdata& hitplane)
{
if (normal*r.vector >= 0){ //ray is either parallel or infront of the plane
return;
}
double dist=((normal*point)-(normal*r.base))/(normal*r.vector);
if (dist < 0){ //There is no reason that the distance should be negative
cerr << "Error: Distance = " << dist << " This means ray got outside of unitcell" << endl;
cerr << "Point: " << r.base << " Vector: " << r.vector << endl;
abort();
}
hitplane.hit=true;
hitplane.dist=dist;
hitplane.hitpoint=r.base + r.vector.scale(dist);
return;
}
/** Returns the shortest distance from point to plane **/
double Plane::distToPlane(Point pnt)
{
return fabs(normal *(pnt - point));
}
/** Returns a class hitdata which has important information of the closest object hit. Function calls hitSphere for each sphere in the list **/
hitdata findClosestSphere(vector<Sphere>& s,ray& r)
{
hitdata hitsphere;
hitdata hitobject;
assert(hitobject.object == NULL);
for (unsigned int i=0;i<s.size();i++)
{
s[i].hitSphere(r,hitsphere);
if (hitsphere.hit == true)
{
if(hitobject.hit == false || hitobject.dist > hitsphere.dist) //Conditions for this to be closest sphere found so far
{
hitobject.hit = true;
hitobject.dist = hitsphere.dist;
hitobject.hitpoint = hitsphere.hitpoint;
hitobject.id = i;
hitobject.object = &(s[i]);
}
hitsphere.hit = false;
}
}
return hitobject;
}
/** Returns a class hitdata which has important information of the closest object hit. Function calls hitPlane for each plane in the list **/
hitdata findClosestPlane(vector<Plane>& p,ray& r)
{
hitdata hitplane;
hitdata hitobject;
for (unsigned int i=0;i<p.size();i++)
{
p[i].hitPlane(r,hitplane);
if (hitplane.hit == true)
{
if(hitobject.hit == false || hitobject.dist > hitplane.dist) //Conditions for this to be closest sphere found so far
{
hitobject.hit = true;
hitobject.dist = hitplane.dist;
hitobject.hitpoint = hitplane.hitpoint;
hitobject.id = i;
hitobject.object = &(p[i]);
}
hitplane.hit = false;
}
}
return hitobject;
}
/** Calculates the normals to the faces of the unit cell order is v_a x v_b : v_b x v_c : v_a x v_c as unit vectors**/
void calcPlanesToCell(ATOM_NETWORK *cell,vector<Plane> &p)
{
XYZ norm1 = cell->v_a.cross(cell->v_b).unit();
XYZ norm2 = cell->v_c.cross(cell->v_a).unit();
XYZ norm3 = cell->v_b.cross(cell->v_c).unit();
Plane face;
//Each face with abc point 0,0,0
face.point = cell->abc_to_xyz(Point(0,0,0));
face.normal = Point(norm1.x,norm1.y,norm1.z);
p.push_back(face);
face.normal = Point(norm2.x,norm2.y,norm2.z);
p.push_back(face);
face.normal = Point(norm3.x,norm3.y,norm3.z);
p.push_back(face);
//Each face with point abc point 1,1,1 (Notice that normals are reversed this is so that a vector can easily be determined if
//it hit within the unit cell
face.point = cell->abc_to_xyz(Point(1,1,1));
face.normal = Point(-norm1.x,-norm1.y,-norm1.z);
p.push_back(face);
face.normal = Point(-norm2.x,-norm2.y,-norm2.z);
p.push_back(face);
face.normal = Point(-norm3.x,-norm3.y,-norm3.z);
p.push_back(face);
}
/** Since all the opperations in Zeo++ are done within one unit cell it is important to duplicate the atoms that would overlap with the cell walls on the other side **/
void duplicateSpheresOnFace(ATOM_NETWORK *cell,vector<Sphere>& spheres,vector<Plane>& faces)
{
Sphere temp_sphere;
bool touch_face[6];
vector<double> permutation_a;
vector<double> permutation_b;
vector<double> permutation_c;
unsigned int size = spheres.size(); //because I will be adding spheres to list (there is a smarter way to do this...)
for (unsigned int i=0; i<size; i++)
{
//Assemble touch_face[] for which faces it touches
for (unsigned int j=0; j<faces.size(); j++)
{
if (faces[j].distToPlane(spheres[i].center) < (spheres[i].radius))
{
touch_face[j] = true;
}
else
{
touch_face[j] = false;
}
}
//Add allowable permutations on the unitcell
permutation_a.clear(); permutation_a.push_back(0.0);
permutation_b.clear(); permutation_b.push_back(0.0);
permutation_c.clear(); permutation_c.push_back(0.0);
if (touch_face[0] == true)
{
permutation_c.push_back(1.0);
}
if (touch_face[1] == true)
{
permutation_b.push_back(1.0);
}
if (touch_face[2] == true)
{
permutation_a.push_back(1.0);
}
if (touch_face[3] == true)
{
permutation_c.push_back(-1.0);
}
if (touch_face[4] == true)
{
permutation_b.push_back(-1.0);
}
if (touch_face[5] == true)
{
permutation_a.push_back(-1.0);
}
//Iterate through all the permutations on the sphere
temp_sphere = spheres[i];
for (unsigned int a=0; a<permutation_a.size(); a++)
{
for (unsigned int b=0; b<permutation_b.size(); b++)
{
for (unsigned int c=0; c<permutation_c.size(); c++)
{
if (permutation_a[a] != 0 || permutation_b[b] != 0 || permutation_c[c] != 0)
{
temp_sphere.center = spheres[i].center;
temp_sphere.center = temp_sphere.center + cell->abc_to_xyz(Point(permutation_a[a],permutation_b[b],permutation_c[c]));
spheres.push_back(temp_sphere);
}
}
}
}
}
}
/** This function converts nodes to spheres it allows for a more flexible implementation of ray_tracing thus making higher abstractions easier we are only concerned with the accessible nodes thus only these will be copied **/
void convertNodeToSphere(VORONOI_NETWORK& vornet,vector<Sphere>& nodes, vector<bool>& accessInfo)
{
Sphere temp_sphere;
VOR_NODE temp_vornode;
nodes.clear();
#if DEBUG
cout << "Adding: " << vornet.nodes.size() << " nodes (if all accessible)." << endl;
#endif
// for each node check if it intersects the unitcell
for (unsigned int i=0; i<vornet.nodes.size(); i++)
{
if (accessInfo[i] ==true)
{
temp_vornode = vornet.nodes[i];
temp_sphere.center = Point(temp_vornode.x,temp_vornode.y,temp_vornode.z);
temp_sphere.radius = temp_vornode.rad_stat_sphere;
nodes.push_back(temp_sphere);
}
}
}
/** This function converts atoms to spheres it allows for a more flexible implementation of ray_tracing thus making higher abstractions easier **/
void convertAtomToSphere(ATOM_NETWORK *cell,vector<Sphere>& atoms)
{
Sphere temp_sphere;
ATOM temp_atom;
atoms.clear();
#if DEBUG
cout << "Adding: " << cell->atoms.size() << " atoms." << endl;
#endif
// for each node check if it intersects the unitcell
for (unsigned int i=0; i<cell->atoms.size(); i++)
{
temp_atom = cell->atoms[i];
temp_sphere.center = Point(temp_atom.x,temp_atom.y,temp_atom.z);
temp_sphere.radius = temp_atom.radius;
atoms.push_back(temp_sphere);
}
}
/** This function finds a nodes radius that encompasses the point raypoint. It will not count the previous node that the raypoint was in by using the reference id**/
bool findSphereOfPoint(Point p,vector<Sphere>& spheres,int& id)
{
for (unsigned int i=0; i<spheres.size(); i++){
if ((calcEuclideanDistance(p,spheres[i].center) < spheres[i].radius) && (int)i != id){
id = i;
return true;
}
}
return false;
}
/**Generates a random vector direction and unitizes it. Since we want a random ray orientation in cartesian coordinates, sphereical coordinates are used to create a point **/
Point genRandomVec()
{
// Randomly sample point on a sphere of radius 1
double theta = (rand()*1.0/RAND_MAX)*2*PI;
double cosphi = 1.0 - (rand()*1.0/RAND_MAX)*2.0;
double phi = acos(cosphi);
// Convert spherical coordinates to xyz coordinates
double x = sin(phi)*cos(theta); //Don't need abs(sin(phi)) becase phi is from 0 to PI
double y = sin(phi)*sin(theta);
double z = cosphi;
Point temp(x,y,z);
if (temp.magnitude() == 0){
temp = genRandomVec();
}
return temp.unit();
}
/** This function generates a random point within the unit cell with values 0-1 for a,b, and c **/
Point genRandomPoint()
{
// Randomly sample ray across the unit cell
double aPoint = (rand()*1.0)/RAND_MAX;
double bPoint = (rand()*1.0)/RAND_MAX;
double cPoint = (rand()*1.0)/RAND_MAX;
return Point(aPoint,bPoint,cPoint);
}
/** This function will travel within a sphere until it hits a point that is no longer within spheres. Returns hitdata on the point it hits**/
void rayTraceInsideSphere(ATOM_NETWORK *cell,vector<Sphere>& spheres,ray r, hitdata& hitobject)
{
#if DEBUG
cout << "Current Ray Length: " << hitobject.dist << endl;
#endif
if (hitobject.dist > MAXRAYDIST){
return;
}
if (findSphereOfPoint(r.base,spheres,hitobject.id) == false) //This function also updates the id of the new sphere in hitsphere
{
return; //No sphere found that included point.
}
hitobject.object = &(spheres[hitobject.id]); //set the object*
//If inside sphere this it should hit the sphere
hitdata hitsphere;
spheres[hitobject.id].hitSphere(r,hitsphere);
if (hitsphere.hit == false){
//It is possible that point is an epsilon outside lets check
if (fabs(calcEuclideanDistance(r.base,spheres[hitobject.id].center) - spheres[hitobject.id].radius) < threshold)
{
//It was round-off error so lets continue
Sphere epsilon_sphere = spheres[hitobject.id];
if (((r.base - spheres[hitobject.id].center) * r.vector) > 0)
{ //vector is headed away from sphere
epsilon_sphere.radius += threshold;
}
else
{ // vector is headed into the sphere
epsilon_sphere.radius += -threshold;
}
epsilon_sphere.hitSphere(r,hitsphere);
}
if (hitsphere.hit == false) //Epsilon Case has been covered so thid should not happen
{
cerr << "Error: Ray did not hit a sphere. findSphereOfPoint said that one was within the radius. And it failed to be within an epsilon. Output in vmd style for easy visulization" << endl;
cout << "draw sphere {" << spheres[hitobject.id].center << "} radius " << spheres[hitobject.id].radius << " resolution 10\n";
Point p = r.base+r.vector.scale(10);
cout << "draw line {" << r.base << "} {" << p << "}\n";
abort();
}
}
hitobject.hit = hitsphere.hit;
hitobject.dist += hitsphere.dist; //Updates the distance traveled in spheres
r.base = cell->shiftXYZInUC(hitsphere.hitpoint); //Move back to unit cell and set values
hitobject.hitpoint = hitsphere.hitpoint;
rayTraceInsideSphere(cell,spheres,r,hitobject);
return;
}
/** This is a recursive function that will keep on extending a ray unitl it hits an atom or exceeds the maximum distance allowed (MAXRAYDIST)**/
void rayTraceToSphere(ATOM_NETWORK *cell,vector<Sphere>& spheres,ray r, vector<Plane>& faces,hitdata& hitobject)
{
#if DEBUG
cout << "Current Ray Length: " << hitobject.dist << endl;
#endif
if (hitobject.dist > MAXRAYDIST){
return;
}
//Find the closest atom that it will hit
hitdata hitsphere = findClosestSphere(spheres,r);
//If I hit an Sphere great!
if (hitsphere.hit == true)
{
if (hitsphere.id == hitobject.id)
{
//cerr << "Just hit same atom this is not allowable" << endl;
}
hitobject.hit = true;
hitobject.hitpoint = hitsphere.hitpoint;
hitobject.dist += hitsphere.dist;
hitobject.id = hitsphere.id;
hitobject.object = hitsphere.object;
return;
}
//If I didn't hit a Sphere then I must hit a unitcell face
hitsphere = findClosestPlane(faces,r);
assert(hitsphere.hit==true); //Thus it hit a plane
hitobject.hit = true;
hitobject.hitpoint = hitsphere.hitpoint;
hitobject.dist += hitsphere.dist;
hitobject.id = -1;
hitobject.object = NULL;
//I am moving the point just slightly outside of the unit cell to be shifted back in
r.base = cell->shiftXYZInUC(hitobject.hitpoint+r.vector.scale(threshold));
//this is a recursive call to get the total length of the ray
rayTraceToSphere(cell,spheres,r,faces,hitobject);
return;
}
/** Returns data from shooting a bunch of rays from accesible regions in the network until they hit an atom. Since the cell is periodic a max ray length can be set so ray does not shoot forever*/
void calcRaysInAV(ATOM_NETWORK *hiaccatmnet, ATOM_NETWORK *orgatmnet, bool highAccuracy, double r_probe_chan,double r_probe,int numSamples, ostream &output, bool visualize,string option){
ATOM_NETWORK *cell;
if(highAccuracy) cell=orgatmnet; else cell = hiaccatmnet; // this ensures that the loops below loop over original atoms
// and not "high-acuracy" clusters, which are used only for
// voronoi decomposition
// Create an object that handles analysis of accessibility of sampled points
AccessibilityClass accessAnalysis;
if(highAccuracy) accessAnalysis.setupAndFindChannels(hiaccatmnet, orgatmnet, highAccuracy, r_probe_chan, r_probe);
else accessAnalysis.setupAndFindChannels(hiaccatmnet, hiaccatmnet, highAccuracy, r_probe_chan, r_probe);
srand(randSeed); //randSeed can be set in network.h
//Initialize Ray Trace World
vector<Sphere> atoms;
convertAtomToSphere(cell,atoms);
vector<Sphere> nodes;
convertNodeToSphere(accessAnalysis.vornet,nodes,accessAnalysis.accessInfo);
vector<Plane> faces;
calcPlanesToCell(cell,faces);
//Duplicate the nodes and atoms that intersect with the plane faces
duplicateSpheresOnFace(cell,atoms,faces);
duplicateSpheresOnFace(cell,nodes,faces);
//Lists of Accessible, Inaccessible, and Resampled Rays
vector<ray> axsray; //List of successful rays that were shot
vector<ray> inaxsray;
vector<ray> resampledray; // List of resampled rays
#if DEBUG
//List Atoms, Nodes, and Planes
cout << "List of Atoms: " << endl;
for (unsigned int i=0; i<atoms.size(); i++)
{
cout << "Center: " << atoms[i].center << " Radius: " << atoms[i].radius << endl;;
}
cout << "List of Nodes: " << endl;
for (unsigned int i=0; i<nodes.size(); i++)
{
cout << "Center: " << nodes[i].center << " Radius: " << nodes[i].radius << endl;;
}
cout << "List of Cell Wall Faces: " << endl;
for (unsigned int i=0; i<faces.size(); i++)
{
cout << "Point: " << faces[i].point << " Normal: " << faces[i].normal << endl;;
}
#endif
//Begin Implementaitons of Ray Tracing Algorithm
cout << "Begin Ray Tracing Analysis: " << endl;
if (option.compare("atom") == 0)
{
cout << "Atom Implementation Chosen: " << endl;
cout << "Number of Samples: " << numSamples << endl;
ray rand_ray;
ray opposite_rand_ray;
hitdata hitobject;
for (int i=0; i<numSamples; i++)
{
rand_ray.base = cell->abc_to_xyz(genRandomPoint());
rand_ray.vector = genRandomVec();
bool point_accessible = accessAnalysis.isVPointAccessible(rand_ray.base);
if (point_accessible == false)
{
rand_ray.vector = Point(0,0,0);
inaxsray.push_back(rand_ray);
}
else //point is accessible
{
opposite_rand_ray.base = rand_ray.base;
opposite_rand_ray.vector = rand_ray.vector.scale(-1);
rayTraceToSphere(cell,atoms,rand_ray,faces,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
//Test negative of ray
rayTraceToSphere(cell,atoms,opposite_rand_ray,faces,hitobject);
opposite_rand_ray.vector = opposite_rand_ray.vector.scale(hitobject.dist);
rand_ray.base = rand_ray.base + opposite_rand_ray.vector;
rand_ray.vector = rand_ray.vector - opposite_rand_ray.vector;
axsray.push_back(rand_ray);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
}
}
}
if (option.compare("node") == 0)
{
cout << "Node Implementation Chosen: " << endl;
cout << "Number of Samples: " << numSamples << endl;
int ndx = 0;
ray rand_ray;
ray opposite_rand_ray;
hitdata hitobject;
for (int i=0; i<numSamples; i++)
{
do {
ndx++;
if (ndx >= accessAnalysis.accessInfo.size())
{
ndx = 0;
}
} while(accessAnalysis.accessInfo[ndx] != true);
rand_ray.base = nodes[ndx].center;
rand_ray.vector = genRandomVec();
opposite_rand_ray.base = rand_ray.base;
opposite_rand_ray.vector = rand_ray.vector.scale(-1);
rayTraceToSphere(cell,atoms,rand_ray,faces,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
//Test negative of ray
rayTraceToSphere(cell,atoms,opposite_rand_ray,faces,hitobject);
opposite_rand_ray.vector = opposite_rand_ray.vector.scale(hitobject.dist);
rand_ray.base = rand_ray.base + opposite_rand_ray.vector;
rand_ray.vector = rand_ray.vector - opposite_rand_ray.vector;
axsray.push_back(rand_ray);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
}
}
//Does not require to be found out if point is accessible
if (option.compare("sphere") == 0)
{
cout << "Sphere Implementation Chosen: " << endl;
cout << "Number of Samples: " << numSamples << endl;
ray rand_ray;
ray opposite_rand_ray;
hitdata hitobject;
for (int i=0; i<numSamples; i++)
{
rand_ray.base = cell->abc_to_xyz(genRandomPoint());
rand_ray.vector = genRandomVec();
opposite_rand_ray.base = rand_ray.base;
opposite_rand_ray.vector = rand_ray.vector.scale(-1);
rayTraceInsideSphere(cell,nodes,rand_ray,hitobject);
if (hitobject.hit == false)
{
rand_ray.vector = Point(0,0,0);
inaxsray.push_back(rand_ray);
}
else
{ //It was traveling within a sphere
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.id = -1;
hitobject.object = NULL;
hitobject.hitpoint = Point(0,0,0);
rayTraceInsideSphere(cell,nodes,opposite_rand_ray,hitobject);
opposite_rand_ray.vector = opposite_rand_ray.vector.scale(hitobject.dist);
rand_ray.base = rand_ray.base + opposite_rand_ray.vector;
rand_ray.vector = rand_ray.vector - opposite_rand_ray.vector;
axsray.push_back(rand_ray);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
}
}
}
//Does not require to be found out if point is accessible
if (option.compare("andrew_sphere") == 0)
{
cout << "Andrew's Sphere Implementation Chosen: " << endl;
cout << "Number of Samples: " << numSamples << endl;
ray rand_ray;
ray opposite_rand_ray;
ray init_ray;
ray opposite_init_ray;
double distance = 0.0;
hitdata hitobject;
bool inside_node = true;
for (int i=0; i<numSamples; i++)
{
distance = 0.0;
rand_ray.base = cell->abc_to_xyz(genRandomPoint());
rand_ray.vector = genRandomVec();
//Ray in the opposite direction
opposite_rand_ray.base = rand_ray.base;
opposite_rand_ray.vector = rand_ray.vector.scale(-1);
//For this implementation I need a reference to the original ray
init_ray = rand_ray;
opposite_init_ray = opposite_rand_ray;
if (findSphereOfPoint(rand_ray.base,nodes,hitobject.id) == false)
{
inside_node = false; //Point is not within a node
}
while(distance < MAXRAYDIST) //Iterate Ray untill it reaches max distance
{
if (inside_node == false)
{
rayTraceToSphere(cell,nodes,rand_ray,faces,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
rand_ray.base = init_ray.base + init_ray.vector.scale(distance);
inaxsray.push_back(rand_ray);
//Update Total Info
distance += hitobject.dist;
rand_ray.vector = init_ray.vector;
rand_ray.base = cell->shiftXYZInUC(hitobject.hitpoint+rand_ray.vector.scale(threshold));
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.hitpoint = Point(0,0,0);
hitobject.id = -1;
hitobject.object = NULL;
inside_node = true; //Now inside node
}
else
{ //Shoot Through Nodes
rayTraceInsideSphere(cell,nodes,rand_ray,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
rand_ray.base = init_ray.base + init_ray.vector.scale(distance);
axsray.push_back(rand_ray);
//Update Total Info
distance += hitobject.dist;
rand_ray.vector = init_ray.vector;
rand_ray.base = cell->shiftXYZInUC(hitobject.hitpoint+rand_ray.vector.scale(threshold));
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.hitpoint = Point(0,0,0);
//Notice that I am not resetting hitobject.id and setting object
hitobject.object = &(nodes[hitobject.id]);
inside_node = false; //Now outside of nodes
}
}
}
cout << "Ray Andrew Sphere Implementation Completed:" << endl;
}
//Does not require to be found out if point is accessible
if (option.compare("andrew_atom") == 0)
{
cout << "Andrew's Atom Implementation Chosen: " << endl;
cout << "Number of Samples: " << numSamples << endl;
ray rand_ray;
ray opposite_rand_ray;
ray init_ray;
ray opposite_init_ray;
double distance = 0.0;
hitdata hitobject;
bool inside_atom = false;
bool inside_accessible_region;
for (int i=0; i<numSamples; i++)
{
distance = 0.0;
rand_ray.base = cell->abc_to_xyz(genRandomPoint());
rand_ray.vector = genRandomVec();
//Ray in the opposite direction
opposite_rand_ray.base = rand_ray.base;
opposite_rand_ray.vector = rand_ray.vector.scale(-1);
//For this implementation I need a reference to the original ray
init_ray = rand_ray;
opposite_init_ray = opposite_rand_ray;
if ((findSphereOfPoint(rand_ray.base,atoms,hitobject.id) == true))
{
inside_atom = true; //Point is within an atom
}
else //Point is not within an atom
{
inside_atom = false;
}
inside_accessible_region = accessAnalysis.isVPointAccessible(rand_ray.base);
while(distance < MAXRAYDIST) //Iterate Ray untill it reaches max distance
{
if ((inside_atom == false) && (inside_accessible_region == true))
{
rayTraceToSphere(cell,atoms,rand_ray,faces,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
//rand_ray.base = init_ray.base + init_ray.vector.scale(distance);
axsray.push_back(rand_ray);
//Update Total Info
distance += hitobject.dist;
rand_ray.vector = init_ray.vector;
rand_ray.base = cell->shiftXYZInUC(hitobject.hitpoint+rand_ray.vector.scale(threshold));
//Reset all hitobject data
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.hitpoint = Point(0,0,0);
hitobject.id = -1;
hitobject.object = NULL;
inside_atom = true; //Now inside atom
}
else if ((inside_atom == false) && (inside_accessible_region == false))
{ //not within an atom but in inaccessible region
rayTraceToSphere(cell,atoms,rand_ray,faces,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
//rand_ray.base = init_ray.base + init_ray.vector.scale(distance);
inaxsray.push_back(rand_ray);
//Update Total Info
distance += hitobject.dist;
rand_ray.vector = init_ray.vector;
rand_ray.base = cell->shiftXYZInUC(hitobject.hitpoint+init_ray.vector.scale(threshold));
//Reset all hitobject data
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.hitpoint = Point(0,0,0);
hitobject.object = NULL;
hitobject.id = -1;
inside_atom = true; //Now inside atom
}
else // Ray is inside of atoms
{
rayTraceInsideSphere(cell,atoms,rand_ray,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
//rand_ray.base = init_ray.base + init_ray.vector.scale(distance);
//This is where I would push data on traveling through atoms
//but it is not here yet
//Update Total Info
distance += hitobject.dist;
rand_ray.vector = init_ray.vector;
rand_ray.base = cell->shiftXYZInUC(hitobject.hitpoint+init_ray.vector.scale(threshold));
//Reset all hitobject data besides the last object id hit
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.hitpoint = Point(0,0,0);
//Notice that I am not resetting hitobject.id and setting object
hitobject.object = &(nodes[hitobject.id]);
//So at this point I know that I am outside an atom however am
//I in an accesible region or not?
inside_atom = false; //Now outside of atoms and in accessible region
inside_accessible_region = accessAnalysis.isVPointAccessible(rand_ray.base);
}
}
}
cout << "Ray Andrew Atom Implementation Completed:" << endl;
}
//Post Process Information
if(visualize)
{
reportRays(output, axsray, inaxsray, true);
//reportAtoms(output,atoms);
//reportNodes(output,nodes);
}
else //no visulization then output a histogram
{
//This parameter can be changed in network.h
reportHistogram(output,BINSIZE,MAXBINS,axsray);
}
reportRayInfo(axsray); // A more detailed output of ray info from program
//FREE MEMORY
accessAnalysis.deconstruct();
}
/* backup of calcRayinAV
// Returns data from shooting a bunch of rays from accesible regions in the network until they hit an atom. Since the cell is periodic a max ray length can be set so ray does not shoot forever
void calcRaysInAV(ATOM_NETWORK *cell, ATOM_NETWORK *orgcell, bool highAccuracy, double r_probe_chan,double r_probe,int numSamples, ostream &output, bool visualize,string option){
// Create a temporary copy of the atomic network in which each atom's radius has been increased by the probe radius
ATOM_NETWORK newAtomNet;
cell->copy(&newAtomNet);
for(int i = 0; i < newAtomNet.numAtoms; i++){ newAtomNet.atoms[i].radius += r_probe; }
// Calculate and store the Voronoi network for this new atomic network
VORONOI_NETWORK vornet;
vector<BASIC_VCELL> vorcells;
vector<VOR_CELL> advCells;
container_periodic_poly *new_rad_con = (container_periodic_poly *)performVoronoiDecomp(true, &newAtomNet, &vornet, advCells, false, vorcells);
vector<CHANNEL> channels = vector<CHANNEL>();
vector<bool> accessInfo = vector<bool> ();
CHANNEL::findChannels(&vornet, max(0.0, r_probe_chan - r_probe), &accessInfo, &channels);
srand(randSeed); //randSeed can be set in network.h
//Initialize Ray Trace World
vector<Sphere> atoms;
convertAtomToSphere(&newAtomNet,atoms);
vector<Sphere> nodes;
convertNodeToSphere(vornet,nodes,accessInfo);
vector<Plane> faces;
calcPlanesToCell(cell,faces);
//Duplicate the nodes and atoms that intersect with the plane faces
duplicateSpheresOnFace(cell,atoms,faces);
duplicateSpheresOnFace(cell,nodes,faces);
//Lists of Accessible, Inaccessible, and Resampled Rays
vector<ray> axsray; //List of successful rays that were shot
vector<ray> inaxsray;
vector<ray> resampledray; // List of resampled rays
#if DEBUG
//List Atoms, Nodes, and Planes
cout << "List of Atoms: " << endl;
for (unsigned int i=0; i<atoms.size(); i++)
{
cout << "Center: " << atoms[i].center << " Radius: " << atoms[i].radius << endl;;
}
cout << "List of Nodes: " << endl;
for (unsigned int i=0; i<nodes.size(); i++)
{
cout << "Center: " << nodes[i].center << " Radius: " << nodes[i].radius << endl;;
}
cout << "List of Cell Wall Faces: " << endl;
for (unsigned int i=0; i<faces.size(); i++)
{
cout << "Point: " << faces[i].point << " Normal: " << faces[i].normal << endl;;
}
#endif
//Begin Implementaitons of Ray Tracing Algorithm
cout << "Begin Ray Tracing Analysis: " << endl;
if (option.compare("atom") == 0)
{
cout << "Atom Implementation Chosen: " << endl;
cout << "Number of Samples: " << numSamples << endl;
ray rand_ray;
ray opposite_rand_ray;
hitdata hitobject;
for (int i=0; i<numSamples; i++)
{
rand_ray.base = cell->abc_to_xyz(genRandomPoint());
rand_ray.vector = genRandomVec();
bool point_accessible = accessiblePoint(rand_ray.base,new_rad_con,r_probe,cell,vorcells,accessInfo);
if (point_accessible == false)
{
rand_ray.vector = Point(0,0,0);
inaxsray.push_back(rand_ray);
}
else //point is accessible
{
opposite_rand_ray.base = rand_ray.base;
opposite_rand_ray.vector = rand_ray.vector.scale(-1);
rayTraceToSphere(cell,atoms,rand_ray,faces,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
//Test negative of ray
rayTraceToSphere(cell,atoms,opposite_rand_ray,faces,hitobject);
opposite_rand_ray.vector = opposite_rand_ray.vector.scale(hitobject.dist);
rand_ray.base = rand_ray.base + opposite_rand_ray.vector;
rand_ray.vector = rand_ray.vector - opposite_rand_ray.vector;
axsray.push_back(rand_ray);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
}
}
}
if (option.compare("node") == 0)
{
cout << "Node Implementation Chosen: " << endl;
cout << "Number of Samples: " << numSamples << endl;
int ndx = 0;
ray rand_ray;
ray opposite_rand_ray;
hitdata hitobject;
for (int i=0; i<numSamples; i++)
{
do {
ndx++;
if (ndx >= accessInfo.size())
{
ndx = 0;
}
} while(accessInfo[ndx] != true);
rand_ray.base = nodes[ndx].center;
rand_ray.vector = genRandomVec();
opposite_rand_ray.base = rand_ray.base;
opposite_rand_ray.vector = rand_ray.vector.scale(-1);
rayTraceToSphere(cell,atoms,rand_ray,faces,hitobject);
rand_ray.vector = rand_ray.vector.scale(hitobject.dist);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
//Test negative of ray
rayTraceToSphere(cell,atoms,opposite_rand_ray,faces,hitobject);
opposite_rand_ray.vector = opposite_rand_ray.vector.scale(hitobject.dist);
rand_ray.base = rand_ray.base + opposite_rand_ray.vector;
rand_ray.vector = rand_ray.vector - opposite_rand_ray.vector;
axsray.push_back(rand_ray);
hitobject.dist = 0.0;
hitobject.hit = false;
hitobject.object = NULL;
hitobject.id = -1;
hitobject.hitpoint = Point(0,0,0);
}
}