-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.cc
2077 lines (1992 loc) · 58.3 KB
/
container.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 "container.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "box.h"
#include "controls.h"
#include "meshalyzer.h"
#include "object.h"
#include "space.h"
using std::cerr;
using std::cout;
using std::endl;
Container::Container (void)
:eg(NULL),o(),files(),num_files(0),area(),aspect_ratio(),edge_length(),
edge_angle(),adjacent_face(),good_integrity(true),num_orph(0),num_mis(0),num_deg(0),num_bor(0),
num_flip(0),num_nonman_e(0),num_nonman_v(0),num_obj(0),num_vert(0),
num_face(0),num_edge(0),num_vol(0),num_sep(0),num_bou(0),num_indistin(0),
num_dupl_v(0),num_dupl_f(0),num_clo_cc(0),num_clo_nn(0)
{
// cumulative Stats
setbb[0]=setbb[1]=setbb[2]=1E30;
setbb[3]=setbb[4]=setbb[5]=-1E30;
num_man[0]=num_man[1]=num_man[2]=0;
num_cons[0]=num_cons[1]=num_cons[2]=0;
num_out[0]=num_out[1]=num_out[2]=0;
pairs[0][0] = 0;
pairs[0][1] = 1;
pairs[1][0] = 1;
pairs[1][1] = 2;
pairs[2][0] = 2;
pairs[2][1] = 0;
}
int Container::countIntFace (void)
{
int a=0;
for (o_iterator i=o.begin();i!=o.end();i++)
{
a+=(*i)->getNumIntFace();
}
return a;
}
void Container::countOutward (int val[3])
{
// consistent outward
// val[0] true true
// val[1] true false
// val[2] false true or false
// val[0]=0; val[1]=0; val[2]=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// if ((*i)->consistent==true && (*i)->outward==true) {val[0]++;}
// else if ((*i)->consistent==true && (*i)->outward==false){val[1]++;}
// else {val[2]++;}
// }
val[0]=num_out[0];
val[1]=num_out[1];
val[2]=num_out[2];
}
void Container::countConsistent (int val[3])
{
// manifold consistent
// val[0] true true
// val[1] true false
// val[2] false true or false
// val[0]=0; val[1]=0; val[2]=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// if ((*i)->manifold==true && (*i)->consistent==true) {val[0]++;}
// else if ((*i)->manifold==true && (*i)->consistent==false){val[1]++;}
// else {val[2]++;}
// }
val[0]=num_cons[0];
val[1]=num_cons[1];
val[2]=num_cons[2];
}
void Container::countManifold (int val[3])
{
// int cc=0,nn=0; // cc=manifold, nn=not manifold
// for (o_iterator i=o.begin();i!=o.end();i++){
// if ((*i)->manifold==true){cc++;}
// else {nn++;}
// }
// return std::make_pair(cc,nn);
// close manifold
// val[0] true true 'manifold'
// val[1] true false 'nonmanifold'
// val[2] false true or false 'undefined'
val[0]=num_man[0];
val[1]=num_man[1];
val[2]=num_man[2];
}
std::pair<int,int> Container::countClosed (void)
{
// int cc=0,nn=0; // cc=closed, nn=not closed
// for (o_iterator i=o.begin();i!=o.end();i++){
// if ((*i)->closed==true){cc++;}
// else {nn++;}
// }
// return std::make_pair(cc,nn);
return std::make_pair(num_clo_cc,num_clo_nn);
}
int Container::countDuplV (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->dupl_v_index.size();
// }
// return a;
return num_dupl_v;
}
int Container::countDuplF (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->dupl_f_index.size();
// }
// return a;
return num_dupl_f;
}
int Container::countIndistin (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->indistin_v.size();
// }
// return a;
return num_indistin;
}
int Container::countBoundaries (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->num_bou;
// }
// return a;
return num_bou;
}
int Container::countComponents (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->num_sep;
// }
// return a;
return num_sep;
}
double Container::countArea (void)
{
double a=0.0;
for (o_iterator i=o.begin();i!=o.end();i++)
{
a+=(*i)->getAreaSum();
}
return a;
}
double Container::countVol (void)
{
// double a=0.0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->vol;
// }
// return a;
return num_vol;
}
int Container::countEdge (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->e.size();
// }
// return a;
return num_edge;
}
int Container::countFace (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->f.size();
// }
// return a;
return num_face;
}
int Container::countVertex (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->v.size();
// }
// return a;
return num_vert;
}
int Container::countObject (void)
{
// return o.size();
return num_obj;
}
int Container::countNonmanV (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->nonman_v.size();
// }
// return a;
return num_nonman_v;
}
int Container::countNonmanE (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->nonman_e.size();
// }
// return a;
return num_nonman_e;
}
int Container::countFlipped (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->flipped.size();
// }
// return a;
return num_flip;
}
int Container::countBorder (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->border.size();
// }
// return a;
return num_bor;
}
int Container::countDegen (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->degen.size();
// }
// return a;
return num_deg;
}
int Container::countMissing (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->missing_v.size();
// }
// return a;
return num_mis;
}
int Container::countOrphan (void)
{
// int a=0;
// for (o_iterator i=o.begin();i!=o.end();i++){
// a+=(*i)->orphan.size();
// }
// return a;
return num_orph;
}
Container::~Container (void)
{
o_iterator i;
for (i=o.begin();i!=o.end();i++){ delete *i; }
}
void Container::clear (void)
{
o.clear();
}
int Container::checkEdgeEdgeIntersection (Face *cf,Face *of,bool share_edge_flag)
{
Controls & cs(Controls::instance());
// cpvc = current_polygon_vertex_coordinates
// opvc = other_polygon_vertex_coordinates
// cv = current_vertex
// ov = other_vertex
Vertex *cv[2],*ov[2];
// for each current face edge
for (int i=0;i<3;i++)
{
// for each other face edge
for (int j=0;j<3;j++)
{
cv[0] = cf->ptr_vertex(pairs[i][0]);
cv[1] = cf->ptr_vertex(pairs[i][1]);
ov[0] = of->ptr_vertex(pairs[j][0]);
ov[1] = of->ptr_vertex(pairs[j][0]);
// if the edges do not share a vertex
if (cv[0]!=ov[0]&&cv[0]!=ov[1]&&cv[1]!=ov[0]&&cv[1]!=ov[1])
{
// and the edges are not parallel
bool parallel_flag = false;
double x[3],y[3];
for (int k=0;k<3;k++)
{
x[k] = cv[1]->getpN(k)-cv[0]->getpN(k);
y[k] = ov[1]->getpN(k)-ov[0]->getpN(k);
}
double term1 = x[0]*y[0]+ x[1]*y[1]+ x[2]*y[2];
if ( !distinguishable(term1*term1,
(x[0]*x[0]+ x[1]*x[1]+ x[2]*x[2])*
(y[0]*y[0]+ y[1]*y[1]+ y[2]*y[2])) )
{
parallel_flag = true;
}
if (!parallel_flag)
{
// compute scalars
double qDen = (cv[1]->getpN(0)-cv[0]->getpN(0))*(ov[1]->getpN(1)-ov[0]->getpN(1))
-(cv[1]->getpN(1)-cv[0]->getpN(1))*(ov[1]->getpN(0)-ov[0]->getpN(0));
double qNum = (cv[1]->getpN(0)-cv[0]->getpN(0))*(cv[0]->getpN(1)-ov[0]->getpN(1))
-(cv[1]->getpN(1)-cv[0]->getpN(1))*(cv[0]->getpN(0)-ov[0]->getpN(0));
double q = qNum/qDen;
double uNum = ov[0]->getpN(0)-cv[0]->getpN(0)+q*(ov[1]->getpN(0)-ov[0]->getpN(0));
double uDen = cv[1]->getpN(0)-cv[0]->getpN(0);
if (fabs(qDen)>cs.get_double_epsilon() && fabs(uDen)>cs.get_double_epsilon())
{
double u = uNum/uDen;
if (
((u > cs.get_double_epsilon() && u < (1.0-cs.get_double_epsilon())) &&
(q > cs.get_double_epsilon() && q < (1.0-cs.get_double_epsilon())) ) ||
( share_edge_flag && ((u > cs.get_double_epsilon() && u < (1.0-cs.get_double_epsilon())) ||
(q > cs.get_double_epsilon() && q < (1.0-cs.get_double_epsilon()))) )
)
{
return(1);
}
}
}
}
}
}
return(0);
}
int Container::checkFaceEdgeIntersection (Face *cf,Face *of)
{
// NOTE THAT INTERSECTION CHECK IS ASSYMETRIC
// I.E. CHECK IS IF CURRENT FACE EDGE INTERSECTS OTHER FACE ONLY.
// NO CHECKING IS DONE IF CURRENT FACE IS INTERSECTED BY OTHER FACE EDGE.
// THIS IS OK SINCE THE ROLES OF CURRENT AND OTHER FACE GET REVERSED LATER
// IN THE CODE, SO ALL NECESSARY CHECKS ARE EVENTUALLY EXECUTED.
//
// Actually, I just added code to make the test symmetric.
double lp[2][3],*cpvc[3];
bool line_flag=false, poly_flag=false, poly_edge_flag;
//cpvc = current_polygon_vertex_coordinates
// get face vertex coordinates
cf->getVertexCoordinates(cpvc);
// for each current polygon edge
for (int i=0;i<3;i++)
{
if (!line_flag || !poly_flag)
{
lp[0][0] = cpvc[pairs[i][0]][0];
lp[0][1] = cpvc[pairs[i][0]][1];
lp[0][2] = cpvc[pairs[i][0]][2];
lp[1][0] = cpvc[pairs[i][1]][0];
lp[1][1] = cpvc[pairs[i][1]][1];
lp[1][2] = cpvc[pairs[i][1]][2];
checkLineFaceIntersection(of,lp,line_flag,poly_flag,poly_edge_flag);
}
}
if (line_flag && (poly_flag || poly_edge_flag)) {return(1);}
// get face vertex coordinates
of->getVertexCoordinates(cpvc);
// for each current polygon edge
for (int i=0;i<3;i++)
{
if (!line_flag || !poly_flag)
{
lp[0][0] = cpvc[pairs[i][0]][0];
lp[0][1] = cpvc[pairs[i][0]][1];
lp[0][2] = cpvc[pairs[i][0]][2];
lp[1][0] = cpvc[pairs[i][1]][0];
lp[1][1] = cpvc[pairs[i][1]][1];
lp[1][2] = cpvc[pairs[i][1]][2];
checkLineFaceIntersection(cf,lp,line_flag,poly_flag,poly_edge_flag);
}
}
// do polygons intersect?
if (line_flag && (poly_flag || poly_edge_flag)) {return(1);}
else {return(0);}
}
void Container::printBatch (Controls &cs)
{
int num_intf = countIntFace();
cout << "\n\n" << "/* ********************** BATCH RESULTS ********************** */\n";
// intersecting faces
if (num_intf==0)
{
cout << "# intersecting faces: none\n";
}
else
{
cout << "# intersecting faces: " << num_intf << endl;
// if -p option, print offending
if (cs.get_print_detailed_info()==1)
{
int j=1;
// for each object
for (o_iterator m=o.begin();m!=o.end();m++)
{
// for each intersected face
for (ff_iterator i=(*m)->getFirstIntFace();i!=(*m)->getOnePastLastIntFace();i++)
{
cout << "# intersecting faces: intersected face " << j++ << endl;
// print intersected face
(*i).first->print(cout);
// print intersecting faces
for (f_iterator k=(*(*i).second).begin();k!=(*(*i).second).end();k++)
{
(*k)->print(cout);
cout << endl;
}
}
}
}
}
}
void Container::createEdges (void)
{
o_iterator i;
// for each object, create edges
for (i=o.begin();i!=o.end();i++)
{
(*i)->createEdges();
}
}
void Container::findVertexAdjacencies (void)
{
o_iterator i;
// for each object, find vertex adjacencies
for (i=o.begin();i!=o.end();i++)
{
(*i)->findVertexAdjacencies();
}
}
void Container::scanDir (const char *filename)
{
struct dirent *pent; // pointer to dirent structure
DIR *pdir = opendir(filename); // pointer to a directory data structure
if (!pdir) {printf("Error. Could not open %s.\n",filename);exit(1);}
else { cerr << "\nFolder found " << filename << endl << endl;}
while ((pent=readdir(pdir)))
{
// copy char array to string
std::string str = pent->d_name;
// if file of typ *.mesh
std::string::size_type found = str.find(".mesh",0);
// if found
if (found != std::string::npos)
{
// save filename
files.push_back(str);
// update index
num_files++;
// print file found to screen
// cout << "file found: " << str << "\n"; cout.flush();
}
}
closedir(pdir);
sort(files.begin(),files.end());
}
Object* Container::processFile (std::string filename)
{
// create new Object
Object *obj = new Object(filename);
// scan file
scanFile(obj,filename);
// check object contents
if (obj->noVertices()==true || obj->noFaces()==true)
{
delete obj;
cout << "\n Container::processFile: "
<< "no valid mesh object found in "
<< filename << ". Skipping file.\n";
return NULL;
}
else
{
// save Object* in container
o.push_back(obj);
// save first vertex* in object
if (obj->noVertices()==false)
{
eg=obj->getFrontVertex();
}
else
{
cout << "\nContainer::processFile: Error. "
<< "Object " << obj->getName()
<< " contains no vertices.\n";
exit(1);
}
// return
return obj;
}
}
void Container::update (Object *oo)
{
// update Container
num_obj++;
num_vert+=oo->getNumVertices();
// DEBUG
// cout << "\nContainer::update: "
// << "oo->f.size()=" << oo->f.size() << endl;
// DEBUG
num_face+=oo->getNumFaces();
num_edge+=oo->getNumEdges();
num_sep+=oo->getNumSep();
// if (oo->manifold==true){num_man_cc++;}
// else {num_man_nn++;}
bool closed = oo->isClosed();
bool consistent = oo->isConsistent();
bool outward = oo->getOutward();
bool manifold = oo->isManifold();
if (manifold==true){num_man[0]++;}
else if (manifold==false && closed==true) {num_man[1]++;}
else {num_man[2]++;}
num_bou+=oo->getNumBoundaries();
num_indistin+=oo->getNumIndistinVertices();
if (manifold==true && consistent==true) {num_cons[0]++;}
else if (manifold==true && consistent==false){num_cons[1]++;}
else {num_cons[2]++;}
num_vol+=oo->getVolume();
if (closed==true){num_clo_cc++;}
else {num_clo_nn++;}
//num_bor+=oo->border.size();
num_bor+=oo->getNumBorderEdges();
num_nonman_v+=oo->getNumNonmanVertices();
num_nonman_e+=oo->getNumNonmanEdges();
num_flip+=oo->getNumFlippedEdges();
if (consistent==true && outward==true) {num_out[0]++;}
else if (consistent==true && outward==false){num_out[1]++;}
else {num_out[2]++;}
num_orph+=oo->getNumOrphanVertices();
num_mis+=oo->getNumMissingVertices();
num_deg+=oo->getNumDegenerateFaces();
num_dupl_v+=oo->getNumDuplVertexIndices();
num_dupl_f+=oo->getNumDuplFaceIndices();
}
void Container::scanFile (Object *obj,std::string filename)
{
char line[2048];
// open file
FILE *F = fopen(filename.c_str(),"r");
if (!F)
{
cout <<"Couldn't open input file "
<< filename << endl;
exit(1);
}
else
{
cerr << "\n\n" << "/* ********************** "
<< "OBJECT ********************** */\n";
// print object name
cerr << "name: " << obj->getName() << endl;
cerr.flush();
// cout << "file found: " << filename << "\n"; cout.flush();
}
// for every line in file
for (char *str=fgets(line,2048,F) ; str!=NULL ; str=fgets(line,2048,F))
{
// skip leading whitespace
while (strchr(" \t,",*str)!=NULL) { str++;}
// if first character is V for Vertex, add new linked list class instance
if (strchr("V",*str)!=NULL)
{
Vertex *v=new Vertex(str,obj);
// obj->v.push_back(v);
obj->addVertex(v);
//obj->vp.insert(std::make_pair(v->getIndex(),v));
obj->addVertexIndexPair(v->getIndex(),v);
//obj->found.insert(std::make_pair(v->getIndex(),false));
obj->addIndexBoolPair(v->getIndex(),false);
}
// if first character is F for Face, add new linked list class instance
else if (strchr("F",*str)!=NULL)
{
Face *f=new Face(str,obj);
obj->addFace(f);
}
}
fclose(F);
}
void Container::writeDistances (void)
{
char file[Controls::instance().get_filename_size()];
// create output filename
sprintf(file,"closest_point_distances.dat");
// open output file
std::ofstream newfile (file,std::ios::out);
if (newfile.is_open())
{
newfile.precision(4);
// for each object
for (o_iterator i=o.begin();i!=o.end();i++)
{
// for each vertex in object
for (v_iterator j=(*i)->getFirstVertex();j!=(*i)->getOnePastLastVertex();j++)
{
// if vertex has a closest face
if ((*j)->ptr_closest_face()!=NULL)
{
// compute separation vector
double s[3];
for (int k=0;k<3;k++){ s[k]=(*j)->getpC(k)-(*j)->getpN(k); }
// print separation distance
if ((*i)->vertexIsNice(*j)){ newfile << sqrt(dot(s,s)) << endl;}
else {newfile << -sqrt(dot(s,s)) << endl;}
}
}
}
newfile.close();
}
}
void Container::boundWorld (Space &s)
{
o_iterator i;
double xmin,xmax,ymin,ymax,zmin,zmax,range[6];
//initialize mins and maxes
// xmin = o[0]->v[0]->getpN(0);
// xmax = o[0]->v[0]->getpN(0);
// ymin = o[0]->v[0]->getpN(1);
// ymax = o[0]->v[0]->getpN(1);
// zmin = o[0]->v[0]->getpN(2);
// zmax = o[0]->v[0]->getpN(2);
xmin = eg->getpN(0);
xmax = eg->getpN(0);
ymin = eg->getpN(1);
ymax = eg->getpN(1);
zmin = eg->getpN(2);
zmax = eg->getpN(2);
////////// loop through all objects //////////
// for each object
for (i=o.begin();i!=o.end();i++)
{
// get range of object vertices
// bounding box: [xmin,ymin,zmin][xmax,ymax,zmax]\n";
(*i)->boundObject(range);
if (range[0]<xmin) {xmin = range[0];}
if (range[1]<ymin) {ymin = range[1];}
if (range[2]<zmin) {zmin = range[2];}
if (range[3]>xmax) {xmax = range[3];}
if (range[4]>ymax) {ymax = range[4];}
if (range[5]>zmax) {zmax = range[5];}
if (xmin <setbb[0]) {setbb[0]=xmin;}
if (ymin <setbb[1]) {setbb[1]=ymin;}
if (zmin <setbb[2]) {setbb[2]=zmin;}
if (xmax >setbb[3]) {setbb[3]=xmax;}
if (ymax >setbb[4]) {setbb[4]=ymax;}
if (zmax >setbb[5]) {setbb[5]=zmax;}
}
if (xmin<0) {s.setWorld(0,xmin*1.01);} else {s.setWorld(0,xmin*0.99);}
if (xmax<0) {s.setWorld(1,xmax*0.99);} else {s.setWorld(1,xmax*1.01);}
if (ymin<0) {s.setWorld(2,ymin*1.01);} else {s.setWorld(2,ymin*0.99);}
if (ymax<0) {s.setWorld(3,ymax*0.99);} else {s.setWorld(3,ymax*1.01);}
if (zmin<0) {s.setWorld(4,zmin*1.01);} else {s.setWorld(4,zmin*0.99);}
if (zmax<0) {s.setWorld(5,zmax*0.99);} else {s.setWorld(5,zmax*1.01);}
}
void Container::getExtraRay (Vertex *v,double lp[2][3],int index)
{
Controls & cs(Controls::instance());
// get normal info
double n[3];
v->f[index]->getNormal(n);
// compute centroid of first adjacent face
double cx = (v->f[index]->ptr_vertex(0)->getpN(0)+
v->f[index]->ptr_vertex(1)->getpN(0)+
v->f[index]->ptr_vertex(2)->getpN(0))/3.0;
double cy = (v->f[index]->ptr_vertex(0)->getpN(1)+
v->f[index]->ptr_vertex(1)->getpN(1)+
v->f[index]->ptr_vertex(2)->getpN(1))/3.0;
double cz = (v->f[index]->ptr_vertex(0)->getpN(2)+
v->f[index]->ptr_vertex(1)->getpN(2)+
v->f[index]->ptr_vertex(2)->getpN(2))/3.0;
double L=sqrt( dot(n,n) );
lp[0][0] = cx;
lp[1][0] = lp[0][0]+n[0]/L*cs.get_ray_epsilon();
lp[0][1] = cy;
lp[1][1] = lp[0][1]+n[1]/L*cs.get_ray_epsilon();
lp[0][2] = cz;
lp[1][2] = lp[0][2]+n[2]/L*cs.get_ray_epsilon();
}
int Container::findExtraPoint (Space &s,Vertex *v,double p[3],int index)
{
double lp[2][3];
int num_odd_objects;
vec_i face_flags;
vec_o tmp;
f_iterator jj;
vec_f crossed_faces,unique_faces;
std::pair<f_iterator,vec_f::iterator> pp;
std::pair<o_iterator,vec_o::iterator> ppp;
getExtraRay(v,lp,index); // returns ray
// look for intersected faces along ray, i.e. between face centroid and RAY ORIGIN
collectNiceFaces(s,lp,unique_faces);
findIntersectedFaces(lp,unique_faces,crossed_faces,face_flags);
sort(crossed_faces.begin(),crossed_faces.end());
// look for adjacent face in crossed_faces
pp=equal_range(crossed_faces.begin(),crossed_faces.end(),v->f[index]);
// if found, then remove
if (pp.first!=pp.second){crossed_faces.erase(pp.first);}
findOddMeshes(crossed_faces,face_flags,num_odd_objects,tmp);
p[0]=lp[1][0];
p[1]=lp[1][1];
p[2]=lp[1][2];
if (!tmp.empty())
{
ppp=equal_range(tmp.begin(),tmp.end(),v->getObject());
if (ppp.first!=ppp.second)
{
return 0;
}
}
return 1;
}
void Container::findCrossed1 (Space &s,Vertex *v,double lp[2][3],vec_o &c)
{
// find and return crossed objects between pN and extracellular point
int num_odd_objects;
vec_i face_flags;
f_iterator i;
vec_f crossed_faces,unique_faces;
std::pair<f_iterator,vec_f::iterator> pp;
collectNiceFaces(s,lp,unique_faces);
findIntersectedFaces(lp,unique_faces,crossed_faces,face_flags);
// remove current vertex adjacent faces from crossed_faces
// for each adjacent face
sort(crossed_faces.begin(),crossed_faces.end());
for (i=v->f.begin();i!=v->f.end();i++)
{
pp=equal_range(crossed_faces.begin(),crossed_faces.end(),*i);
// if adjacent face is found in crossed_faces, then remove from crossed_faces
if (pp.first!=pp.second){crossed_faces.erase(pp.first);}
}
findOddMeshes(crossed_faces,face_flags,num_odd_objects,c);
}
void Container::findCrossed2 (Space &s,double lp[2][3],vec_o &c)
{
// find and return crossed objects between pN and extracellular point
int num_odd_objects;
vec_i face_flags;
vec_f crossed_faces,unique_faces;
collectNiceFaces(s,lp,unique_faces);
findIntersectedFaces(lp,unique_faces,crossed_faces,face_flags);
findOddMeshes(crossed_faces,face_flags,num_odd_objects,c);
}
void Container::collectCrossed (Space &s,Vertex *v,vec_o &cb)
{
vec_o ca;
std::pair<o_iterator,vec_o::iterator> pp;
double lp[2][3],p[3];
// find point, p, outside of current object
unsigned int index = 0;
while (!findExtraPoint(s,v,p,index))
{
index++;
if (index>(v->f.size()-1))
{
cout << "\n\nEvery adjacent face failed!\n";
cout << v->getObject()->getName() << "->" << v->getIndex()
<< " current vertex [" << v->getpN(0) << " "
<< v->getpN(1) << " "
<< v->getpN(2) << "]"
<< endl;
exit(1);
}
}
// grab intersected objects between pN and RAY ORIGIN and return as ca
lp[0][0]=v->getpN(0);
lp[0][1]=v->getpN(1);
lp[0][2]=v->getpN(2);
lp[1][0]=p[0];
lp[1][1]=p[1];
lp[1][2]=p[2];
findCrossed1(s,v,lp,ca);
// grab intersected objects along RAY as cb
lp[0][0]=p[0];
lp[0][1]=p[1];
lp[0][2]=p[2];
lp[1][0]=p[0];
lp[1][1]=p[1];
lp[1][2]=p[2];
findClosestAxis(s,v,lp); // returns ray
lp[0][0]=p[0];
lp[0][1]=p[1];
lp[0][2]=p[2];
lp[1][1]=p[1];
lp[1][2]=p[2];
if (v->getIndex()==531 && !strcmp(v->getObject()->getName().c_str(),"a001_FILTERED_SMOOTH_SMOOTH"))
{
}
findCrossed2(s,lp,cb);
// remove all meshes in ca from cb
// since the object is not odd relative to pN
sort(cb.begin(),cb.end());
for (o_iterator ii=ca.begin();ii!=ca.end();ii++)
{
pp=equal_range(cb.begin(),cb.end(),*ii);
// if object in ca is found in cb, then remove from cb
if (pp.first!=pp.second)
{
cb.erase(pp.first);
}
// else add it to cb
else
{
cb.push_back(*ii);
}
}
}
bool Container::updateNiceness (Vertex *v,vec_o &cb)
{
std::pair<o_iterator,vec_o::iterator> pp;
int old_nice = v->getVertexNiceness();
// if vertex niceness changes then set flag=true
bool flag = false;
// if cb is not empty, then vertex is not nice
if (!cb.empty())
{
v->setVertexNiceness(1);
// if vertex was nice
if (!old_nice)
{
flag = true;
pp=equal_range(cb.begin(),cb.end(),v->getObject());
// if vertex is inside self object
if (pp.first!=pp.second)
{
v->setVertexNiceness(2);
cout << endl << endl
<< v->getObject()->getName() << "->" << v->getIndex()
<< " vertex inside self [" << v->getpN(0) << " "
<< v->getpN(1) << " "
<< v->getpN(2)
<< "], cb.size() " << cb.size()
<< endl;
for (f_iterator jj=v->nf.begin();jj!=v->nf.end();jj++)
{
cout << v->getObject()->getName() << "->" << (*jj)->getIndex()
<< " adjacent face\n";
(*jj)->print(cout);
cout << endl;
}
}
}
}
else
{
// else cb is empty, then vertex is nice
// if vertex was nonnice, but not to self
if (old_nice==1)
{
flag=true;
}
// if vertex was at least nonnice to self
else if (old_nice==2)
{
flag=true;
}
// update niceness
v->setVertexNiceness(0);
}
return flag;
}
bool Container::checkNiceness (Space &s,Vertex *v)
{
vec_o cb;
// collect objects inside which vertex lies
collectCrossed(s,v,cb);
// update niceness of vertex based on cb
return updateNiceness(v,cb);
}
void Container::findNice (Space &s)
{
cout << "Iteration 0: ";
cout << "find nice vertices................";
cout.flush();
o_iterator i;
v_iterator j;
// for each object in container
for (i=o.begin();i!=o.end();i++)
{
// for each vertex in object
for (j=(*i)->getFirstVertex();j!=(*i)->getOnePastLastVertex();j++)
{
checkNiceness(s,*j);
}
}
cout << "complete.\n";
cout.flush();
}
void Container::findClosestAxis (Space &s,Vertex *v,double lp[2][3])
{
// n=normal,r=ray
// get normal info
// identify nearest boundary
double dis[6] =
{ fabs(v->getpN(0)-s.getWorld(0)),
fabs(v->getpN(0)-s.getWorld(1)),
fabs(v->getpN(1)-s.getWorld(2)),
fabs(v->getpN(1)-s.getWorld(3)),
fabs(v->getpN(2)-s.getWorld(4)),
fabs(v->getpN(2)-s.getWorld(5))
};
int i=0;
double min = dis[i];
for (int j=1;j<6;j++)
{
if (dis[j]<min){i=j;min=dis[j];}
}
// configure ray
if (i==0)
{
lp[1][0] = lp[0][0]-2*(s.getWorld(1)-s.getWorld(0)); // end x
lp[1][1] = lp[0][1]; // end y
lp[1][2] = lp[0][2]; // end z
}
else if (i==1)
{
lp[1][0] = lp[0][0]+2*(s.getWorld(1)-s.getWorld(0)); // end x
lp[1][1] = lp[0][1]; // end y
lp[1][2] = lp[0][2]; // end z
}
else if (i==2)
{
lp[1][0] = lp[0][0]; // end x
lp[1][1] = lp[0][1]-2*(s.getWorld(3)-s.getWorld(2)); // end y
lp[1][2] = lp[0][2]; // end z
}
else if (i==3)
{
lp[1][0] = lp[0][0]; // end x
lp[1][1] = lp[0][1]+2*(s.getWorld(3)-s.getWorld(2)); // end y
lp[1][2] = lp[0][2]; // end z
}
else if (i==4)
{
lp[1][0] = lp[0][0]; // end x
lp[1][1] = lp[0][1]; // end y
lp[1][2] = lp[0][2]-2*(s.getWorld(5)-s.getWorld(4)); // end z
}
else if (i==5)
{
lp[1][0] = lp[0][0]; // end x
lp[1][1] = lp[0][1]; // end y
lp[1][2] = lp[0][2]+2*(s.getWorld(5)-s.getWorld(4)); // end z