-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectSegment.h
executable file
·2073 lines (1843 loc) · 73.8 KB
/
ObjectSegment.h
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 <limits.h>
//#include <vtkDataSet.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataArray.h>
#include <vtkContourFilter.h>
#include <vtkPointData.h>
#include <vtkFloatArray.h>
#include <vtkIdList.h>
#include <vtkType.h>
#include <assert.h>
#include <string>
#include "input.h"
#include "nodeinfo.h"
#include "cellinfo.h"
#include "objectinfo.h"
#include "objSegmentUtil.h"
#include "classifier.h"
#define PI 3.14159265
using namespace std;
extern string getlabel(std::string listfile);
extern string precision_time(int const time,int const precision);
extern void SetCellNodes(unsigned long pos[8],stCellHex *cell);
extern int ProcessCells(InpObject<stCellHex> *inPtr);
extern void ResetArray(unsigned long nnodes, char *mergeArray);
/*
extern string getlabel(std::string listfile);
extern string precision_time(int const time,int const precision);
extern void SetCellNodes(long pos[8],stCellHex *cell);
extern int ProcessCells(InpObject<stCellHex> *inPtr);
int Get_Conn(InpObject<stCellHex> *inPtr, int *node_conn_array);
void ResetArray(int nnodes, char *mergeArray);
void GetPoint(stCellHex *cell, long& point, int ptNumber);
void SETARRAY(stCellHex *cell,char *mergeArray);
void SetArray(int index, char *mergeArray);
void HANDLE_CELL_IND(InpObject<stCellHex> *inPtr, stCellHex *cell, long* vertex_ind,long* speedingtable,char *mergeArray);
void SetOutField(vtkUnstructuredGrid *out_ds, int nnodes, int ncells, int nspace,int cellpoints, float* coords, float* nodedata, int *connects);
string getlabel(std::string listfile);
int OutputAttribute(stObject *obj, char *outFile);
int OutputTrak( stObject *obj, char *fileBaseName,int timeFrame);
*/
//extern int MergeCellList(InpObject<stCellHex> *inptr, stObject*objPtr, long objNum, stCellHex *cellList, char *mergeArray);
#define HANDLE_(a) { if ( ((nodeList[a].flag & F_THR) == F_THR ) && \
((nodeList[a].flag & F_USED) != F_USED) ) { \
cellPtr=GetIncidentCells( nodeList, a); \
AddCellListToObj(objPtr, cellPtr,mergeArray); \
nodeList[a].flag |= F_USED; vol++; }}
#define HANDLE_CELL_INDEX(a, b) { n_interval=(int)(b/INTERVAL); \
int t2, t1=n_interval*INTERVAL; \
for(t2=0, i=t1;i<b;i++) if(mergeArray[i]) t2++; \
cout<<"b=["<<b<<"] and t2 =["<<t2<<"]"<<endl; \
if(n_interval) vertex_ind[a]=speedingtable[n_interval-1]+t2; \
else vertex_ind[a]=t2; \
}
//extern void SetOutField(vtkUnstructuredGrid *out_ds, int nnodes, int ncells, int nspace,int cellpoints, float* coords, float* nodedata, int *connects);
string getlabel(std::string listfile)
{
ifstream fp;
fp.open(listfile.c_str());
if (!fp.is_open())
{
cout<<"getlabel: cannot open listfile\n";
string tmp="";
return tmp;
}
string label;
fp>>label;
string file;
fp>>file;
fp.close();
int s;
for (s=file.size()-1;s>-1;--s) {
if (file[s]>'9'||file[s]<'0')
break;
}
label+=file.substr(0,s+1);
return label;
}
//
//
//
//template<class T>
//int PopulateInputData(InpObject<T> *inPtr, unsigned long *node_conn_array, float *coord_array, float *node_data)
//{
// //#cout << "Now in PopulateInputData \n";
// register unsigned long i=0, j=0;
// stNodePos *nodePtr, *curNode;
// stNodeData *nodeDataPtr, *curDataPtr;
// float max_node_data0=-99999.0, min_node_data0=99999.0;
// float max_node_data1=-99999.0, min_node_data1=99999.0;
// float max_node_data2=-99999.0, min_node_data2=99999.0;
// int aaaa = Get_Conn(inPtr, node_conn_array);
// //get coords
// nodePtr= CreateNodeArray(inPtr->numNodes);
// //assert(nodePtr);
// cout<<"inPtr->numNodes: "<<inPtr->numNodes<<endl;
// //#cout << "post creating node list\n";
// for (j=0; j<inPtr->numNodes; j++)
// {
// i=3*j;
// curNode = GetCurNode( nodePtr, j);
// curNode->x=coord_array[i];
// curNode->y=coord_array[i+1];
// curNode->z=coord_array[i+2];
// curNode->list=NULL;
// curNode->flag=-1;
// }
// //get node_data
// nodeDataPtr = CreateDataArray( inPtr->numNodes);
// //assert(nodeDataPtr);
// //#cout << "post creating data list\n";
//
// for (j=0; j<inPtr->numNodes; j++)
// {
// i=j*inPtr->nncomp;
// curDataPtr = GetCurNodeData( nodeDataPtr, j);
// curDataPtr->val0=node_data[i];
// curDataPtr->val1=node_data[i+1];
// curDataPtr->val2=node_data[i+2];
// if(max_node_data0<node_data[i])
// max_node_data0=node_data[i];
// if(min_node_data0>node_data[i])
// min_node_data0=node_data[i];
// if(max_node_data1<node_data[i+1])
// max_node_data1=node_data[i+1];
// if(min_node_data1>node_data[i+1])
// min_node_data1=node_data[i+1];
// if(max_node_data2<node_data[i+2])
// max_node_data2=node_data[i+2];
// if(min_node_data2>node_data[i+2])
// min_node_data2=node_data[i+2];
// }
//
//
// /*
// for (j=0;j<inPtr->numNodes;j++)
// {
// curDataPtr = GetCurNodeData( nodeDataPtr, j);
// curDataPtr->val0=node_data[j];
// if(max_node_data<node_data[j])
// max_node_data=node_data[j];
// if(min_node_data>node_data[j])
// min_node_data=node_data[j];
// }
// */
// inPtr->pnode = nodePtr;
// inPtr->pnodeData = nodeDataPtr;
// inPtr->minData = min_node_data0;
// inPtr->maxData = max_node_data0;
// inPtr->minData1 = min_node_data1;
// inPtr->maxData1 = max_node_data1;
// inPtr->minData2 = min_node_data2;
// inPtr->maxData2 = max_node_data2;
// cout<<"Inside the populateInputdata: inPtr->thresh_deltaz: "<<inPtr->thresh_deltaz<<endl;
//
// return METHOD_SUCCESS;
//}
//
int Get_Conn(InpObject<stCellHex> *inPtr, unsigned long *node_conn_array)
{
//#cout << "In Get_Conn\n";
register long i = 0, j = 0;
stCellHex *cellArrPtr, *curCell;
cellArrPtr=CreateCellArray<stCellHex>(inPtr->numCell);
// assert(cellArrPtr);
if(cellArrPtr!=NULL)
cout << "post cell array creation\n";
else
cout << "array null";
for(j=0;j<inPtr->numCell;j++)
{
i=8*j;
curCell = GetCurCell( cellArrPtr, j);
curCell->point0=node_conn_array[i];
curCell->point1=node_conn_array[i+1];
curCell->point2=node_conn_array[i+2];
curCell->point3=node_conn_array[i+3];
curCell->point4=node_conn_array[i+4];
curCell->point5=node_conn_array[i+5];
curCell->point6=node_conn_array[i+6];
curCell->point7=node_conn_array[i+7];
curCell->objNum=-1; // the object No. to which this tetra belongs.
curCell->flag = -1;
}
inPtr->pcell= cellArrPtr;
return METHOD_SUCCESS;
}
//marking all nodes according to whether their node data is less than threshold or not.
template <class T>
int ProcessThresholds(InpObject<T> *inPtr, double threshold_value, int thrValType)
{
cout << "inside processthreshold \n";
cout<<"inPtr->thrVal:["<<inPtr->thrVal<<"] and inPtr->nncomp:["<<inPtr->nncomp<<"] "<<endl;
register unsigned long j=0;
stNodePos *nodeList, *curNodePtr;
stNodeData *dataList, *curDataPtr;
static unsigned long threshCnt = 0;
bool comparisonValue = 0;
//int numUnused = 0; // counting the number of nodes whose node data is less than threshold
threshCnt = 0; // counting the number of nodes whose node data is greater than threshold
// cout<<"Inside ProcessThresholds!!: inPtr->nncomp"<<inPtr->nncomp<<endl;
if(thrValType==THRESH_PERCENT)
{
inPtr->threshPercent_FromTop = threshold_value;
inPtr->threshPercent_FromBottom = 0.0;
}
else
cout << "cannot process threshold type other than PERCENTAGE\n";
nodeList = inPtr->pnode;
dataList= inPtr->pnodeData;
for( j=0;j<inPtr->numNodes;j++ )
{
curDataPtr = GetCurNodeData(dataList, j );
curNodePtr = GetCurNode(nodeList, j);
if(inPtr->thrType==THRESH_PERCENT)
{
if (inPtr->nncomp == 1)
{
comparisonValue = (curDataPtr->val0 >= inPtr->thrVal);
}
if (inPtr->nncomp == 3)
{
comparisonValue = ((curDataPtr->val0 >= inPtr->thrVal) && (curDataPtr->val1 >= inPtr->thrVal1));
}
if (comparisonValue)
{
curNodePtr->flag = F_THR;
threshCnt++;
//numUnused++;
//curNodePtr->flag = F_UNUSED;
}
else
{
//curNodePtr->flag = F_THR;
//threshCnt++;
curNodePtr->flag = F_UNUSED;
}
}
else
cout << "cannot process threshold type other than PERCENTAGE\n";
}
cout<<"leaving processthreshold"<<endl;
return METHOD_SUCCESS;
}
template<class T>
int SegmentObjects(InpObject<T> *inPtr, int minObjSize,char *mergeArray)
{
//register int i =0;//, l=0;
stCellIndex *indexPtr = NULL, *nextPtr, *index1Ptr, *cellPtr, *swapPtr;
stObject *objPtr;
stNodeData *dataList;
stNodePos *nodeList;
T *cellList, *cell;
unsigned long pos[8],vol;
dataList = inPtr->pnodeData;
nodeList = inPtr->pnode;
cellList = inPtr->pcell;
indexPtr=NULL;
//switch (inPtr->thrType)
//{
//case 0: // upwards thresholding
while(1)
{
//free indexPtr
while(indexPtr)
{
nextPtr=indexPtr->next;
delete indexPtr;
indexPtr=nextPtr;
}
// find list of remaining unmarked nodes with greatest val
indexPtr=GetMaxDataValue(inPtr);
if (indexPtr == 0) break; // stop if list is empty
// go through all nodes with val=maxVal in list
while (indexPtr)
{
if((nodeList[indexPtr->index].flag & F_USED) == F_USED)
{
// if max node marked as used - advance to next one
// used means this max node has been handled
swapPtr = indexPtr->next;
delete indexPtr; // free temp mem held by node on max list
indexPtr = swapPtr;
continue;
}
ResetArray(inPtr->numCell,mergeArray);
// before grow a new object, reset MergeArray.
nodeList[indexPtr->index].flag |= F_USED; // mark node used
objPtr = (stObject*)CreateObject();
cellPtr = GetIncidentCells(nodeList, indexPtr->index );
// get cells incident with this point.
AddCellListToObj(objPtr, cellPtr,mergeArray);
index1Ptr=objPtr->cellPtr;
vol=0; // number of nodes(not cells) in the object
while (index1Ptr != 0 )
{
cell = GetCurCell(cellList, index1Ptr->index);
SetCellNodes(pos,cell);
HANDLE_(pos[0]);
HANDLE_(pos[1]);
HANDLE_(pos[2]);
HANDLE_(pos[3]);
HANDLE_(pos[4]);
HANDLE_(pos[5]);
HANDLE_(pos[6]);
HANDLE_(pos[7]);
index1Ptr = index1Ptr->next;
}
AddObjToObjList( inPtr, objPtr, vol, minObjSize);
index1Ptr=objPtr->cellPtr;
while (index1Ptr)
{
cell = GetCurCell(cellList, index1Ptr->index);
if (cell->objNum != INVALID_OBJ)
break;
// problem: how about the new object is touching two or more objects???
index1Ptr=index1Ptr->next;
}
if(index1Ptr == 0 )
MarkCellList(cellList, objPtr);
else
{
MergeCellList(inPtr, objPtr, cell->objNum, cellList,mergeArray);
objPtr->cellPtr = 0x00;
inPtr->pobject->objNum--;
}
swapPtr = indexPtr->next;
delete indexPtr; // free temp mem held by node on max list
indexPtr = swapPtr;
} // while (indexPtr != 0 )
}
// break;
//}
return METHOD_SUCCESS;
}
template <class T>
stCellIndex* GetMaxDataValue(InpObject<T> *inObj)
{
unsigned long cnt;
float val, maxVal;
stCellIndex *tmpPtr, *maxretPtr;
stNodeData *dataList=inObj->pnodeData;
stNodePos *nodeList=inObj->pnode;
maxretPtr=tmpPtr=0;
cnt=inObj->numNodes;
maxVal = LONG_MIN;
register unsigned long i = 0;
for (i=0;i<cnt;i++)
{
if ( nodeList[i].flag == F_UNUSED ) // means this cell is not in the threshold
continue;
if ( nodeList[i].flag & F_USED ) // this cell has been browsed
continue;
val = dataList[i].val0;
maxVal = ( val > maxVal ) ? val : maxVal;
}
// SEDAT NOTES to SEDAT: try to find the min point (minVal) above as well.
// And then instead of using the condition (maxVal == val ) below,
// try to use (maxVal-val)/(maxVal - minVal)<0.01
// also check and warn if (maxVal == minVal)
/* make a list of all points with the exteme value */
for ( i=0;i<cnt;i++ )
{
val = dataList[i].val0;
if ( maxVal == val )
{
tmpPtr = new stCellIndex;
//assert(tmpPtr);
tmpPtr->index=i;
tmpPtr->next = maxretPtr;
maxretPtr = tmpPtr;
}
}
return maxretPtr;
};
void ResetArray(unsigned long nnodes, char *mergeArray)
{
for(register unsigned long i=0;i<nnodes;i++)
mergeArray[i] = 0;
}
template<class T>
int SetObjMax( InpObject<T> *inPtr,int cellpoints )
{
stCellIndex *cellPtr;
stObject *objPtr;
stNodeData *dataList;
stNodePos *nodeList;
float val;
int ptNumber;
unsigned long point;
T *cell, *cellList;
int nspace = 3;
float min, max, mass, massSqr; // setObjMax
max = min = -100000.0;
objPtr = inPtr->pobject;
nodeList = inPtr->pnode;
cellList = inPtr->pcell;
dataList = inPtr->pnodeData;
register unsigned long i = 0;
for(i=0;i<inPtr->numNodes;i++)
nodeList[i].flag &= (~F_USED);
while( objPtr )
{
mass=0.0;
massSqr=0.0;
cellPtr = objPtr->cellPtr;
objPtr->objMax = -100000.00;
objPtr->objMin = 100000.00;
objPtr->LowerLeft_Corner_x = 99999999999.99;
objPtr->LowerLeft_Corner_y = 99999999999.99;
objPtr->LowerLeft_Corner_z = 99999999999.99;
objPtr->UpperRight_Corner_x = -999999999.99;
objPtr->UpperRight_Corner_y = -999999999.99;
objPtr->UpperRight_Corner_z = -999999999.99;
while( cellPtr )
{
cell=GetCurCell(cellList, cellPtr->index);
for(ptNumber=0; ptNumber<cellpoints; ptNumber++)
{
GetPoint(cell,point,ptNumber);
// if point not visited already in object mark it and
// add its value to the object mass
val=dataList[point].val0;
if ((((nodeList[point].flag) & F_USED)!=F_USED) &&
(((nodeList[point].flag) & F_THR)==F_THR))
{
nodeList[point].flag|=F_USED;
mass += val;
massSqr += val*val;
// Find the box corners here for each objects
for(int j = 0; j <nspace;j++)
{
if ( nodeList[point].x < objPtr->LowerLeft_Corner_x )
objPtr->LowerLeft_Corner_x = nodeList[point].x;
else if ( nodeList[point].x > objPtr->UpperRight_Corner_x )
objPtr->UpperRight_Corner_x = nodeList[point].x;
if ( nodeList[point].y < objPtr->LowerLeft_Corner_y )
objPtr->LowerLeft_Corner_y = nodeList[point].y;
else if ( nodeList[point].y > objPtr->UpperRight_Corner_y )
objPtr->UpperRight_Corner_y = nodeList[point].y;
if ( nodeList[point].z < objPtr->LowerLeft_Corner_z )
objPtr->LowerLeft_Corner_z = nodeList[point].z;
else if ( nodeList[point].z > objPtr->UpperRight_Corner_z )
objPtr->UpperRight_Corner_z = nodeList[point].z;
}
//
if(val > objPtr->objMax ) // if value exceeds max update max
{
objPtr->objMax=val;
objPtr->maxX=nodeList[point].x;
objPtr->maxY=nodeList[point].y;
objPtr->maxZ=nodeList[point].z;
objPtr->maxNode=point;
}
if(val < objPtr->objMin ) // if value exceeds min update min
{
objPtr->objMin=val;
objPtr->minX=nodeList[point].x;
objPtr->minY=nodeList[point].y;
objPtr->minZ=nodeList[point].z;
objPtr->minNode=point;
}
}
}
cellPtr = cellPtr->next;
}
objPtr->mass = mass;
objPtr->massSqr = massSqr;
objPtr = objPtr->next;
}
return METHOD_SUCCESS;
}
void GetPoint(stCellHex *cell, unsigned long& point, int ptNumber)
{
switch (ptNumber)
{
case 0: point=cell->point0; break;
case 1: point=cell->point1; break;
case 2: point=cell->point2; break;
case 3: point=cell->point3; break;
case 4: point=cell->point4; break;
case 5: point=cell->point5; break;
case 6: point=cell->point6; break;
case 7: point=cell->point7; break;
default: break;
}
}
void SETARRAY(stCellHex *cell,char *mergeArray)
{
SetArray(cell->point0,mergeArray);
SetArray(cell->point1,mergeArray);
SetArray(cell->point2,mergeArray);
SetArray(cell->point3,mergeArray);
SetArray(cell->point4,mergeArray);
SetArray(cell->point5,mergeArray);
SetArray(cell->point6,mergeArray);
SetArray(cell->point7,mergeArray);
//sedat cout<<"cell->point7 :"<<cell->point7<<endl;
}
void SetArray(unsigned long index, char *mergeArray)
{
mergeArray[index] = 1;
}
void HANDLE_CELL_IND(InpObject<stCellHex> *inPtr, stCellHex *cell, unsigned long* vertex_ind,unsigned long* speedingtable,char *mergeArray)
{
int n_interval=0, i = 0;
HANDLE_CELL_INDEX(0,cell->point0);
HANDLE_CELL_INDEX(1,cell->point1);
HANDLE_CELL_INDEX(2,cell->point2);
HANDLE_CELL_INDEX(3,cell->point3);
HANDLE_CELL_INDEX(4,cell->point4);
HANDLE_CELL_INDEX(5,cell->point5);
HANDLE_CELL_INDEX(6,cell->point6);
HANDLE_CELL_INDEX(7,cell->point7);
}
/************** Find object Centroid,volume & tetra num.************/
template <class T>
int FindCentAndVol( InpObject<T> *inPtr,int cellpoints)
{ /* this function can not be called prior to mass calc func. */
stCellIndex *cellPtr;
stObject *objPtr;
stNodeData *dataList;
stNodePos *nodeList;
float val=0.0, Cx, Cy, Cz;
int ptNumber=0, cellNum=0;
unsigned long pointCount=0, point=0; // pointCount is used to set volume
T *cell, *cellList;
objPtr = inPtr->pobject;
nodeList = inPtr->pnode;
register unsigned long i = 0;
for (i=0;i<inPtr->numNodes;i++) // reset all point's Used flag
nodeList[i].flag &= (~F_USED);
nodeList = inPtr->pnode;
cellList = inPtr->pcell;
dataList = inPtr->pnodeData;
while ( objPtr != NULL )
{
pointCount = 0;
Cx=Cy=Cz=0.0;
cellPtr = objPtr->cellPtr;
cellNum=0; // reset number of tetrahedra in object
while(cellPtr)
{
cellNum++;
cell=GetCurCell(cellList, cellPtr->index);
for(ptNumber=0; ptNumber<cellpoints; ptNumber++)
{
GetPoint(cell,point,ptNumber);
// if point not visited already in object mark it and count it for the object volume
val=dataList[point].val0;
if ((((nodeList[point].flag) & F_USED)!=F_USED) &&
(((nodeList[point].flag) & F_THR)==F_THR))
{
nodeList[point].flag|=F_USED;
pointCount++;
Cx += (val*nodeList[point].x)/((float)(objPtr->mass));
Cy += (val*nodeList[point].y)/((float)(objPtr->mass));
Cz += (val*nodeList[point].z)/((float)(objPtr->mass));
}
}
cellPtr = cellPtr->next;
}
objPtr->numCell = cellNum;
objPtr->volume = pointCount;
objPtr->centroidX = Cx;
objPtr->centroidY = Cy;
objPtr->centroidZ = Cz;
objPtr = objPtr->next;
}
return 1;
}
// SetOutField(out_ds,cursor,objPtr->numCell,nspace,cellpoints,coords,nodevals,connects);
void SetOutField(vtkUnstructuredGrid *out_ds, unsigned long nnodes, unsigned long ncells, int nspace,int cellpoints, float* coords, double* nodedata, unsigned long *connects)
{
//watch<float>(coords, 20, _COORDS_);
//#cout << "In SetOutField \n";
vtkFloatArray *pcoords = vtkFloatArray::New();
pcoords->SetNumberOfComponents(nspace);
pcoords->SetNumberOfTuples(nnodes);
unsigned long n_connect = 0;
unsigned long n_coords = 0;
float *temp_coord = new float[3];
//float temp_coord[3] = {0.0};
//cout<<" ---------Inside setoutfield.. nnodes["<<nnodes<<"] ncells=["<< ncells <<"]------------------ "<<endl;
for(unsigned long i = 0; i < /*ncell*cellpoints*/ nnodes; i ++)
{
for(int ii = 0 ; ii < nspace;ii++)
{
//cout<<" Inside setoutfield.. coords["<<n_coords<<"]= "<< coords[n_coords] <<endl;
temp_coord[ii] = coords[n_coords++];
}
//cout<<"settuple3: i=["<<i<<"] temp_coord[0]=["<<temp_coord[0] <<"] temp_coord[1]=["<<temp_coord[1]<<"] temp_coord[2]=["<<temp_coord[2]<<"]"<<endl;
pcoords->SetTuple3(i,(double)temp_coord[0],(double)temp_coord[1],(double)temp_coord[2]);
// pcoords->SetTuple(i,temp_coord);
}
out_ds->Allocate(ncells*cellpoints);// this step is important
vtkPoints *points = vtkPoints::New();
//points->SetNumberOfPoints(nnodes);
points->SetData(pcoords);
// ### just to test the SetPoint function++
// ### just to test the SetPoint function --
//#cout << "post writing coords to vtk\n";
// out_ds->GetPointData()->SetNumberOfComponents(1);
// vtkPointData *vtk_pt_data = vtkPointData::New();
//vtk_pt_data->GetScalars()->SetNumberOfTuples(nnodes);
vtkFloatArray *pdata = vtkFloatArray::New();
pdata->SetNumberOfTuples((vtkIdType)nnodes);
//cout << " Num Tuples in data : " << vtk_pt_data->GetScalars()->GetNumberOfTuples() << endl;
//vtkDataArray *vtk_node_data = vtk_pt_data->GetScalars();
double *temp_data = new double;
for(unsigned long i = 0; i < nnodes; i++)
{
//cout<<"inside the for loop. nodedata["<<i<<"]="<<nodedata[i]<<endl;
*temp_data = nodedata[i];
pdata->SetTuple1(i,(double)*temp_data);
}
//#cout << "post writing node_data to vtk\n";
// int *temp_connect = new int[cellpoints];
vtkIdType *temp_connect = new vtkIdType[cellpoints]; //int n_connect = 0;
//cout<<"---------Before InsertNextCell --------- "<<endl;
for(unsigned long i = 0; i < ncells;i++)
{
for(int j = 0; j < cellpoints;j++)
{
temp_connect[j] = connects[n_connect++];
//cout<<"temp_connect["<<j<<"]="<<temp_connect[j]<<endl;
}
out_ds->InsertNextCell(VTK_VOXEL, (vtkIdType) cellpoints,temp_connect);
}
// cout<<"---------AFter InsertNextCell --------- "<<endl;
out_ds->SetPoints(points);
points->Delete();
pcoords->Delete();
out_ds->GetPointData()->SetScalars(pdata);
pdata->Delete();
//cout << "End of SetOutField\n";
}
template<class T>
int ExtractSurf(vtkDataSet *in_ds,InpObject<T> *inPtr,vtkDataSet **outDS, char* mypolyfile,int nspace,int cellpoints)
{
// cout<<"------ Inside ExtractSurf ------- [ 1 ] ------- "<<endl;
T *cell,*cellList;
stCellIndex *cellPtr;
stNodePos *nodeList;
stObject *objPtr;
stNodeData *dataList;
objPtr = inPtr->pobject;
cellList = inPtr->pcell;
nodeList = inPtr->pnode;
dataList = inPtr->pnodeData;
register unsigned long i =0,j=0;
char *mergeArray;
for (i=0;i<inPtr->numNodes;i++)
nodeList[i].flag &= (~F_USED);
//
// Now I use mergeArray to process the coordinates
// of the object.
mergeArray=new char[inPtr->numNodes];
assert(mergeArray);
//#cout << "Post creating merge\n";
bool firstobj=true;
long *min_node_list, *max_node_list;
long maxVol=0,minVol=9999999,minVolobj,maxVolobj,minAreaobj,maxAreaobj;
double maxAr=0.0,minAr=9999999.99;
ofstream afile;
/*cout<<"\nmypolyfile has value"<<mypolyfile;
std::string temp_file(mypolyfile);
int dpos=temp_file.find('.');
temp_file=temp_file.substr(0,dpos+1);
cout<<"\ntemp_file has value"<<temp_file;
temp_file.append("addl");*/
// cout<<" objPtr value[ "<<objPtr<<" ] "<<endl;
while (objPtr)
{
// build a field acceptable by UTILisosurf() in avs/dv_util.h
cellPtr = objPtr->cellPtr;
ResetArray(inPtr->numNodes,mergeArray);
while(cellPtr) //for each object.. set all the cell nodes =1 in mergeArray array.
{
cell=GetCurCell(cellList,cellPtr->index);
SETARRAY(cell,mergeArray);
cellPtr=cellPtr->next;
}
unsigned long *speedingtable;
speedingtable=new unsigned long[(unsigned long)(inPtr->numNodes/INTERVAL)];
assert(speedingtable);
//#cout << "Post creating speeding table\n";
unsigned long cursor=0;
for(i=0;i<inPtr->numNodes;i++)
{
if(mergeArray[i])
cursor++;
if(i>0 && (i+1)%INTERVAL==0)
speedingtable[(unsigned long)((i+1)/INTERVAL)-1]=cursor;
}
float *coords=0;
double *nodevals=0;
coords=new float[cursor*nspace];
assert(coords);
nodevals=new double[cursor];
assert(nodevals);
for(i=0, j=0;i<inPtr->numNodes;i++) // set coords and node values
if(mergeArray[i])
{
coords[j++]=nodeList[i].x;
coords[j++]=nodeList[i].y;
coords[j++]=nodeList[i].z;
nodevals[(unsigned long)(j/nspace)-1]=dataList[i].val0;
}
//cout << "------ cursor value = ["<< cursor<<"\n";
unsigned long *connects=0;
//#cout << "objPtr->numCell " << objPtr->numCell <<endl;
//#cout << "cellpoints " << cellpoints <<endl;
connects=new unsigned long[objPtr->numCell*cellpoints];
assert(connects);
cellPtr=objPtr->cellPtr;
j=0;
unsigned long *vertex_ind;
vertex_ind=new unsigned long[cellpoints];
assert(vertex_ind);
while(cellPtr)
{ //set connects
cell=GetCurCell(cellList,cellPtr->index);
HANDLE_CELL_IND(inPtr,cell,vertex_ind, speedingtable,mergeArray);
for(i=0;i<cellpoints;i++)
{
connects[j++]=vertex_ind[i];
}
cellPtr=cellPtr->next;
}
delete [] vertex_ind;
vertex_ind = NULL;
//#cout << "Post filling the connects\n";
vtkUnstructuredGrid *out_ds = vtkUnstructuredGrid::New();
SetOutField(out_ds,cursor,objPtr->numCell,nspace,cellpoints,coords,nodevals,connects);
//#cout << "GetNumberOfCells() : " << out_ds->GetNumberOfCells() << endl;
// now call the isosurface module
// cout<<"------ Inside ExtractSurf ------- [ 2 ] ------- "<<endl;
vtkContourFilter *isosurface = vtkContourFilter::New();
isosurface->SetInput(in_ds);
isosurface->SetNumberOfContours(1);
//SEDAT look at here, thresholds.....
isosurface->SetValue(0,inPtr->thrVal); // from Sedat: some web on internet says that this is double type sometimes it says this is float. which one? :). Currently it is double.
// cout<<" isosurface->SetValue(0,inPtr->thrVal); : "<<inPtr->thrVal<<endl;
// cout<<"------ Inside ExtractSurf ------- [ 3] ------- "<<endl;
//#cout << "isosurface->GetValue() " << isosurface->GetValue(0) << endl;
//*outDS = (vtkDataSet *)isosurface->GetOutput();
vtkPolyData *outpoly = isosurface->GetOutput();
// cout<<"------ Inside ExtractSurf ------- [ 3.5 ] ------- "<<endl;
//cout<<"test 1"<<endl;
outpoly->Update();
// cout<<"------ Inside ExtractSurf ------- [ 4 ] ------- "<<endl;
// cout<<"outpoly->GetNumberOfPoints():["<<outpoly->GetNumberOfPoints()<<"]"<<endl;
//cout<<"test 2"<<endl;
outpoly->Register(NULL);
// cout<<"test 3"<<endl;
*outDS = outpoly;
// cout<<"test 4"<<endl;
out_ds->Delete();
out_ds = NULL;
isosurface->Delete();
isosurface= NULL;
double p = 0;
//#cout << "the poly data is : \n";
ofstream pfile;
if(firstobj)
pfile.open(mypolyfile,ofstream::out | ofstream::trunc);
else
pfile.open(mypolyfile,ofstream::out | ofstream::app);
if(!pfile.is_open())
{
cout<<"ExtractSurf:cannot open "<<mypolyfile<<endl;
return 0;
}
// cout<<"------ Inside ExtractSurf ------- [ 5 ] ------- "<<endl;
//#cout << "Num cells post contour is : " << outpoly->GetNumberOfCells()<< endl;
vtkIdList *listconnect = vtkIdList::New();
//#cout << "Num points is : " << outpoly->GetNumberOfPoints()<< endl;
//#cout << "writing to poly file\n";
//#cout << "mypolyfie is : " << mypolyfile << endl;
pfile<<objPtr->color.r<<" "<<objPtr->color.g<<" "<<objPtr->color.b<<endl;
pfile<<outpoly->GetNumberOfPoints()<<endl;
//Sedat cout<<" outpoly->GetNumberOfPoints() : "<<outpoly->GetNumberOfPoints()<<endl;
pfile.setf(ios::fixed,ios::floatfield);
pfile<<setprecision(6);
double *temp_coord_array = new double[3];
for(i=0;i<outpoly->GetNumberOfPoints();i++)
{
temp_coord_array = outpoly->GetPoint(i);
for(int j = 0; j <nspace;j++)
{
pfile<<temp_coord_array[j] << " ";
}
pfile<<endl;
}
// cout<<"------ Inside ExtractSurf ------- [ 6] ------- "<<endl;
pfile<<outpoly->GetNumberOfCells()<<endl;
for(i=0;i<outpoly->GetNumberOfCells();i++)
{
outpoly->GetCellPoints(i,listconnect);
pfile<<"3 ";
for(j = 0; j < listconnect->GetNumberOfIds();j++)
{
unsigned long temp_id = (unsigned long)listconnect->GetId((const vtkIdType) j) ;
pfile<< temp_id << " ";
}
pfile << endl;
}
pfile<<0<<endl<<endl;
//#cout << "finished writing poly file\n";
pfile.close();
listconnect->Delete();
listconnect = NULL;
delete [] coords;
coords = NULL;
delete [] connects;
connects = NULL;
delete [] nodevals;
nodevals = NULL;
delete [] speedingtable;
speedingtable = NULL;
objPtr = objPtr->next;
firstobj=false;
//return 1;
}
// cout<<"------ Inside ExtractSurf ------- [ 7 ] ------- "<<endl;
delete [] mergeArray;
//#cout <<"End of Extract Surface\n";
return METHOD_SUCCESS;
}
template <class T>
int FindMoment( InpObject<T> *inPtr,int cellpoints )
{ // this function can not be called prior to Centroid calc func.
stCellIndex *cellPtr;
stObject *objPtr;
stNodeData *dataList;
stNodePos *nodeList;
float val =0.0, Ixx=0.0, Iyy=0.0, Izz=0.0, Iyz=0.0, Ixy=0.0, Izx=0.0;
float mx = 0.0, my = 0.0, mz = 0.0;
unsigned long /*rc =0, */ptNumber=0, objNum=0;
register unsigned long i =0, point =0;
T *cell, *cellList;
objPtr = inPtr->pobject;
nodeList = inPtr->pnode;
for (i=0;i<inPtr->numNodes;i++) // reset all point's Used flag
nodeList[i].flag &= (~F_USED);
nodeList = inPtr->pnode;
cellList = inPtr->pcell;
dataList = inPtr->pnodeData;
while( objPtr )
{
Ixx = Iyy = Izz = Ixy = Iyz = Izx = 0.0;
cellPtr = objPtr->cellPtr;
while(cellPtr)
{
cell = GetCurCell(cellList, cellPtr->index);
for(ptNumber=0; ptNumber<cellpoints; ptNumber++)
{
GetPoint(cell,point,ptNumber);
// if point not visited already in object mark it and
// sum its weight into object moments
val=dataList[point].val0;
if ((((nodeList[point].flag) & F_USED)!=F_USED) &&
(((nodeList[point].flag) & F_THR)==F_THR))
{
nodeList[point].flag|=F_USED;