-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.h
More file actions
3486 lines (3022 loc) · 109 KB
/
Image.h
File metadata and controls
3486 lines (3022 loc) · 109 KB
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
//
// Image.h
// TestRayVolumeRendering
//
// Created by Casey on 2013/12/25.
// Copyright (c) 2013年 Casey. All rights reserved.
//
#ifndef _IMAGEBASED_H_
#define _IMAGEBASED_H_
#include <omp.h>
#include "RegularScalarData.h"
#include "myUtility.h"
#include "math.h"
#include <vector>
#include "time.h"
#include "stdlib.h"
#include "omp.h"
#include <algorithm>
using namespace std;
typedef struct _Segment{
int pxl;
int from;
int len;
float metric;//entropy or variance ...
float a;
float b;
}Segment;
bool segmentSort( Segment a, Segment b )
{
return (a.metric<b.metric);
}
bool segmentSortBigToSmall( Segment a, Segment b )
{
return (a.metric>b.metric);
}
bool segmentSortByFrom( Segment a, Segment b )
{
return (a.from<b.from);
}
class Image
{
public:
//u and v is the image dimension (make sure what you want)
Image( RegularScalarData* rsd, int u, int v )
{
m_rsd = rsd;
m_u = u;
m_v = v;
m_color = (unsigned char*) malloc( m_u*m_v*3*sizeof(unsigned char) );
m_color2 = (unsigned char*) malloc( m_u*m_v*3*sizeof(unsigned char) );
m_color3 = (unsigned char*) malloc( m_u*m_v*3*sizeof(unsigned char) );
memset(m_color, 0, m_u*m_v*4*sizeof(unsigned char) );
memset(m_color2, 0, m_u*m_v*4*sizeof(unsigned char) );
memset(m_color3, 0, m_u*m_v*4*sizeof(unsigned char) );
m_depth = (float*) malloc( m_u*m_v*sizeof(float) );
m_mrrV.resize(m_u*m_v);
m_mrrE.resize(m_u*m_v);
m_mrrL.resize(m_u*m_v);
}
//Image: input a preprocessing ray based file
//pathName: path and name of the file
//u,v : image dimension
//d depth
//bins: bin number
//slcSample: how many sample per slice of a ray
//rsd: is raw data
Image( char* pathName, int u, int v, int d, int bins, int slcSample, RegularScalarData* rsd=NULL )
{
if( rsd !=NULL ){
m_rsd = rsd;
}
m_u = u;
m_v = v;
m_d = d;
m_slcSample = slcSample;
m_color = (unsigned char*) malloc( m_u*m_v*4*sizeof(unsigned char) );
m_colorF = (float*) malloc( m_u*m_v*4*sizeof(float) );
m_color2 = (unsigned char*) malloc( m_u*m_v*4*sizeof(unsigned char) );
m_color2F = (float*) malloc( m_u*m_v*4*sizeof(float) );
m_color3 = (unsigned char*) malloc( m_u*m_v*4*sizeof(unsigned char) );
memset(m_color, 0, m_u*m_v*4*sizeof(unsigned char) );
memset(m_colorF, 0, m_u*m_v*4*sizeof(float) );
memset(m_color2, 0, m_u*m_v*4*sizeof(unsigned char) );
memset(m_color3, 0, m_u*m_v*4*sizeof(unsigned char) );
m_depth = (float*) malloc( m_u*m_v*sizeof(float) );
m_mrrV.resize(m_u*m_v);
m_mrrE.resize(m_u*m_v);
m_mrrL.resize(m_u*m_v);
m_bins = bins;
m_ddaX = (int*) malloc( sizeof(int)*m_d );
m_ddaY = (int*) malloc( sizeof(int)*m_d );
m_ddaCnt = 0;
//initialize entropy vector of every pixel
entropyQueue = new vector<Segment>*[m_u*m_v];
for( int i =0 ; i < m_u*m_v ; i++ )
entropyQueue[i] = new std::vector<Segment>;
FILE* fp = fopen( pathName, "rb");
float check=0;
fread(&check, sizeof(float), 1, fp);
Ray* ray = new Ray( bins, slcSample );
int i=0;
size_t result;
do{
if(check >=0 ){//same ray
fseek(fp, -1*sizeof(float), SEEK_CUR);
ray->loadFromFile(fp);
}else if( check == -1 ){//next ray
m_rays.push_back(ray);
ray = new Ray( bins, slcSample );
}
result = fread(&check, sizeof(float), 1, fp);
}while( check!=-2 && result==1 );
m_rays.push_back(ray);
fclose( fp );
}
//****************procude IDM from different axis*******************************************
//produceRayByZAsix:xy paralle to image plan
//usf: upsampling factor
//bins: number of bins
//uncomplted function
void produceRaysByZAxis( float usf, int bins, float error, float block )
{
float evMSError = 0;
float evCount = 0;
float nonZeroBins = 0;
Histogram histDns(bins);
Histogram histIdm(bins);
int allSegs = 0;
int cnt=0;
float*** data = alloc3DMatrix( m_rsd->getDim3()*usf, usf, usf);
for( int d2 = 0; d2< m_rsd->getDim2()-1; d2+=1 ){
for( int d1 = 0; d1< m_rsd->getDim1()-1; d1+=1 ){
//prepare upsampling ray
for( int k=0; k<(m_rsd->getDim3()-1)*usf; k++ ){//depth
for( int i=0; i<usf; i++ ){
for( int j=0; j<usf; j++ ){
data[k][i][j] = m_rsd->getUpsamplingVoxelValue(d1*usf+j, d2*usf+i, ((m_rsd->getDim3()-1)*((int)usf) -1) - k, usf);
}
}
}
Ray* r = new Ray( data, m_rsd->getDim3()*usf, usf, usf, bins );
m_rays.push_back(r);
float rayError = 0;
float rayCnt = 0;
int seg = r->produceMonoRepresentation( error, block, rayError, rayCnt );
allSegs += seg;
evCount += rayCnt;
evMSError += rayError;
r->releaseDnsIH();
printf("u:%d v:%d allSegments:%d percetageOfSegs: %f MSError: %10.9f\n", d2, d1, allSegs, allSegs/((d2*(m_rsd->getDim1()-1))+(d1+1))/((m_rsd->getDim3()-1)*usf), evMSError/evCount);
//get nonZeroBins
for( int i=0; i<r->getIdmIHSize(); i++ ){
r->getSingleIdmHistogram( &histIdm, i );
float nonZero = 0;
for( int j=0; j<bins; j++ ){
if( histIdm.getBin( j ) >=0.5 )nonZero++;
}
nonZeroBins += nonZero;
}
//end nonZeroBins
}
}
printf("MSError : %f/%f= %10.9f nonZeroBins: %f\n", evMSError, evCount, evMSError/evCount, nonZeroBins);
printf("Done!\n");
}
//produceRayByXAsix:yz paralle to image plan
//usf: upsampling factor
//bins: number of bins
//error: error tollerance: between 0-1
//block: evaluated block size
//best version right now
void produceRaysByXAxis( float usf, int bins, float error, float block )
{
float evMSError = 0;
float evCount = 0;
float nonZeroBins = 0;
Histogram histDns(bins);
Histogram histIdm(bins);
int allSegs = 0;
int cnt=0;
int uPixelSamples = ((m_rsd->getDim3()-1)*usf)/m_u;
int vPixelSamples = ((m_rsd->getDim2()-1)*usf)/m_v;
printf("UV-PixeSamples: %d %d %f %f %d, %d\n", vPixelSamples, uPixelSamples, m_rsd->getDim1()*usf, usf, m_v, m_rsd->getDim1());
float*** data = alloc3DMatrix( (int)(m_rsd->getDim1()*usf), vPixelSamples, uPixelSamples);
for( int v = 0; v< m_v; v+=1 ){
for( int u = 0; u< m_u; u+=1 ){
//prepare upsampling ray
for( int k=0; k<(m_rsd->getDim1()-1)*usf; k++ ){//depth
for( int i=0; i<vPixelSamples; i++ ){
for( int j=0; j<uPixelSamples; j++ ){
data[k][i][j] = m_rsd->getUpsamplingVoxelValue(k, v*vPixelSamples+i, u*uPixelSamples+j, usf);
}
}
}
Ray* r = new Ray( data, m_rsd->getDim1()*usf, vPixelSamples, uPixelSamples, bins );
m_rays.push_back(r);
float rayError = 0;
float rayCnt = 0;
int seg = r->produceMonoRepresentation( error, block, rayError, rayCnt );
allSegs += seg;
evCount += rayCnt;
evMSError += rayError;
r->releaseDnsIH();
printf("u:%d v:%d allSegments:%d percetageOfSegs: %f MSError: %10.9f\n", v, u, allSegs, allSegs/(float)((v*m_u+u)*((m_rsd->getDim1()-1)*usf)), evMSError/evCount);
//get nonZeroBins
for( int i=0; i<r->getIdmIHSize(); i++ ){
r->getSingleIdmHistogram( &histIdm, i );
float nonZero = 0;
for( int j=0; j<bins; j++ ){
if( histIdm.getBin( j ) >=0.5 )nonZero++;
}
nonZeroBins += nonZero;
}
//end nonZeroBins
}
}
printf("MSError : %f/%f= %10.9f nonZeroBins: %f\n", evMSError, evCount, evMSError/evCount, nonZeroBins);
printf("Done!\n");
}
//****************procude IDM from different axis -END*******************************************
//***********************DIRECT VOLUME REDNERING************************************
//colorRendering: render color from m_rays and store to m_color
//transfer function is from m_tf, so call makeTrasferfunction in advance is necesary
//step: a voxel block(slices)
void colorRenderingBySegmentMean( int step, float clrAdapt, float bgColor, float startDepth, float order, float lighting )
{
int rayCnt=0;
int cnt=0;
#pragma omp parallel for
for( int v = 0; v< m_v; v+=1 ){
Histogram hist(m_bins);
Histogram befHist(m_bins);
for( int u = 0; u< m_u; u+=1 ){
Ray* ray = m_rays[v*m_u+u];
float cr = 0, cg = 0, cb = 0, ca = 0;
for( int d = 0; d< m_d-step; d+=(step/8.0) ){
//tranditional implementation
ray->getHistByIdm(&hist, d, d+step-1);
float vxl = hist.getMean();
float r, g, b, a;
getRGBAbyTF(vxl, r, g, b, a, 1);
//if( vxl < 0.3 ){
// //if( hist.getBin( 6 ) == 0 ){
// if( vxl < 0.0468 || vxl > 0.054 ){
// a = 0;
// }else{
// getRGBAbyTF(vxl, r, g, b, a, 1);
// a = 1.0;
// }
//}else{
// a = 0.5;
//}
a*=clrAdapt;
for( int i=0; i< step/8.0; i++ ){
cr += (r*a) * (1.0 - ca);
cg += (g*a) * (1.0 - ca);
cb += (b*a) * (1.0 - ca);
ca += a * (1.0 - ca);
}
if( ca > 0.95 )break;
}
cr += (bgColor) * (1.0 - ca);
cg += (bgColor) * (1.0 - ca);
cb += (bgColor) * (1.0 - ca);
unsigned char optR = (unsigned char)(int)floor(cr * 255.0);
unsigned char optG = (unsigned char)(int)floor(cg * 255.0);
unsigned char optB = (unsigned char)(int)floor(cb * 255.0);
m_color[(v*m_u+u)*4+0] = optR;
m_color[(v*m_u+u)*4+1] = optG;
m_color[(v*m_u+u)*4+2] = optB;
m_color[(v*m_u+u)*4+3] = 255;//A
}
}
}
void colorRenderingByHistogramInfomation( )
{
float bgColor = m_sldBar8/(float)100.0;
float colorModulation = m_lineEdit0;
int step = m_lineEdit3;
#pragma omp parallel for
for( int v = 0; v< m_v; v+=1 ){
Histogram hist(m_bins);
Histogram befHist(m_bins);
Histogram rHist(m_bins);//use 16 too cluster visualization
Histogram gHist(m_bins);
Histogram bHist(m_bins);
for( int u = 0; u< m_u; u+=1 ){
Ray* ray = m_rays[v*m_u+u];
float cr = 0, cg = 0, cb = 0, ca = 0;
float ucr = 0, ucg = 0, ucb = 0, uca = 0; //uncertaiinty visualization color
for( int d = 0; d< m_d-step; d+=step ){
float frontSegTransparency, correctOpacity;
if( d>0 )
ray->getHistByIdm(&befHist, 0, d-1);
else
befHist.reset();
ray->getHistByIdm(&hist, d, d+step-1);
float rr, gg, bb;
calculateSegmentColorContribution( &befHist, &hist, colorModulation, rr, gg, bb, frontSegTransparency, correctOpacity );
cr += rr;
cg += gg;
cb += bb;
ca = ( 1 - frontSegTransparency ) + correctOpacity * frontSegTransparency; //not necessary
hist.reset();
ray->getHistByIdm( &hist, d, d + step );
//get weight histogram
float sumWeight = 0;
for( int i = 0; i < m_bins; i ++ ){
float r, g, b, a;
getRGBAbyTF( i / (float)m_bins, r, g, b, a, 1);
a *= colorModulation;
float w = hist.getBin( i ) * a;
sumWeight += w;
hist.setBin( i, w );
}
if( sumWeight< 0.001 ) continue;
hist.normalize();
//calculate weighted mean
float rmean = 0, gmean = 0, bmean = 0;
for( int i = 0; i < m_bins; i ++ ){
float r, g, b, a;
getRGBAbyTF( i / (float)m_bins, r, g, b, a, 1);
float w = hist.getBin( i );
rmean += r * w;
gmean += g * w;
bmean += b * w;
}
//calculate weighted variance
float rvariance = 0, gvariance = 0, bvariance = 0;
for( int i = 0; i < m_bins; i ++ ){
float r, g, b, a;
getRGBAbyTF( i / (float)m_bins, r, g, b, a, 1);
float w = hist.getBin( i );
rvariance += ( r - rmean ) * ( r - rmean ) * w;
gvariance += ( g - gmean ) * ( g - gmean ) * w;
bvariance += ( b - bmean ) * ( b - bmean ) * w;
}
rvariance = sqrt( rvariance / (float)m_bins );
gvariance = sqrt( gvariance / (float)m_bins );
bvariance = sqrt( bvariance / (float)m_bins );
//pick up max varaince
float variance = sqrt( ( ( rvariance * rvariance ) + ( gvariance * gvariance ) + ( bvariance * bvariance ) ) / 3.0 );
////uncertainty color
variance = ( variance )/ 0.05; //rescale to certain value, otherwise the varaince is too small
variance = pow( (double) variance, 1/1.0 );//also rescale
float r, g, b, a;
getRGBAbyTF( variance, r, g, b, a, 1);
ucr += r * correctOpacity * frontSegTransparency;
ucg += g * correctOpacity * frontSegTransparency;
ucb += b * correctOpacity * frontSegTransparency;
}
cr += (bgColor) * (1.0 - ca);
cg += (bgColor) * (1.0 - ca);
cb += (bgColor) * (1.0 - ca);
unsigned char optR = (unsigned char)(int)floor(cr * 255.0);
unsigned char optG = (unsigned char)(int)floor(cg * 255.0);
unsigned char optB = (unsigned char)(int)floor(cb * 255.0);
m_color[(v*m_u+u)*4+0] = optR;
m_color[(v*m_u+u)*4+1] = optG;
m_color[(v*m_u+u)*4+2] = optB;
m_color[(v*m_u+u)*4+3] = 255;//A
//uncertainty visualization
unsigned char optUR = (unsigned char)(int)floor(ucr * 255.0);
unsigned char optUG = (unsigned char)(int)floor(ucg * 255.0);
unsigned char optUB = (unsigned char)(int)floor(ucb * 255.0);
m_color2[(v*m_u+u)*4+0] = optUR;
m_color2[(v*m_u+u)*4+1] = optUG;
m_color2[(v*m_u+u)*4+2] = optUB;
m_color2[(v*m_u+u)*4+3] = 255;//A
}
}
}
void renderRenderingWorkload( )
{
//start to count time
clock_t start_time, end_time;
float total_time = 0;
start_time = clock(); /* mircosecond */
float colorModulation = m_lineEdit0;
int step = m_lineEdit3;
float transparantThreshold = 0.000001;
float earlyTerminationThreshold = 0.9;
int* renderFrom; //every ray render from
int* renderTo; //every ray render until
int* dispatchID; //render job of a ray belong to whick core
float sumWorkload = 0; //sum of ( renderFrom - renderTo ) of all rays
renderFrom = (int*) malloc( m_u * m_v * sizeof( int ) );
renderTo = (int*) malloc( m_u * m_v * sizeof( int ) );
dispatchID = (int*) malloc( m_u * m_v * sizeof( int ) );
#pragma omp parallel for
for( int v = 0; v< m_v; v+=1 ){
Histogram hist(m_bins);
for( int u = 0; u< m_u; u+=1 ){
int rayIdx = v*m_u+u;
Ray* ray = m_rays[ rayIdx ];
float cr = 0, cg = 0, cb = 0, ca = 0;
int startRenderPosition; //before this position, no visible data
int endRenderPosition; //rendering tail
int endRenderLeapingPosition; //after this position, no visible data
int earlyTerminationPosition;
//find startRenderPosition
ray->getHistByIdm( &hist, 0, m_d - 1 );
if( calculateSegmentOpacity( &hist, colorModulation ) < transparantThreshold ){
startRenderPosition = m_d - 1; //whole ray is transparancy
}else{
int min = 0;
int max = m_d - 1;
int mid = (int)((min+max)/2.0);
int lastOpaquePosition = m_d - 1;
bool flag = true; //true means it has opacity
//bineary search -like algorithm
//find the position, before it no visible data
do
{
ray->getHistByIdm( &hist, min, mid );
float opacity = calculateSegmentOpacity( &hist, colorModulation );
if( opacity < transparantThreshold ){//min to mid is totally transparancy
min = mid;
flag = false;
}else{
lastOpaquePosition = mid;
max = mid;
flag = true;
}
mid = ( int )( ( min + max ) / 2.0 );
if( max - min < step / 2.0 )break;
}while( lastOpaquePosition - min > step || flag == true );
startRenderPosition = min;
}
//find tail space leaping
ray->getHistByIdm( &hist, 0, m_d - 1 );
if( calculateSegmentOpacity( &hist, colorModulation ) < transparantThreshold ){
endRenderLeapingPosition = 0; //whole ray is transparancy
}else{
int min = 0;
int max = m_d - 1;
int mid = (int)((min+max)/2.0);
int lastOpaquePosition = m_d - 1;
bool flag = true; //true means it has opacity
//bineary search -like algorithm
//find the position, after it no visible data
do
{
ray->getHistByIdm( &hist, mid, max );
float opacity = calculateSegmentOpacity( &hist, colorModulation );
if( opacity < transparantThreshold ){//min to mid is totally transparancy
max = mid;
flag = false;
}else{
lastOpaquePosition = mid;
min = mid;
flag = true;
}
mid = ( int )( ( min + max ) / 2.0 );
if( max - min < step / 2.0 )break;
}while( max - lastOpaquePosition > step || flag == true );
endRenderLeapingPosition = max;
}
//find lastRenderPosition - early termination point
ray->getHistByIdm( &hist, 0, m_d - 1 );
if( calculateSegmentOpacity( &hist, colorModulation ) < earlyTerminationThreshold ){
earlyTerminationPosition = m_d - 1; //whole ray cannot reach 0.95 opacity
}else{
int min = 0;
int max = m_d - 1;
int mid = (int)((min+max)/2.0);
int lastNotTerminatePoint = 0;
bool flag = true; //true means it has opacity
//bineary search -like algorithm
//find the position, that is early termination point
do
{
ray->getHistByIdm( &hist, 0, mid );
float opacity = calculateSegmentOpacity( &hist, colorModulation );
if( opacity < earlyTerminationThreshold ){//this mid point cannot be terminated( opacity < earlyTerminationThreshold )
lastNotTerminatePoint = mid;
min = mid;
flag = false;
}else{
max = mid;
flag = true;
}
mid = ( int )( ( min + max ) / 2.0 );
if( max - min < step / 2.0 )break;
}while( max - lastNotTerminatePoint > step || flag == false );
earlyTerminationPosition = max;
}
//check earlyTerminationPosition and endRenderLeapoingPosition, which one is more close to the tail
if( earlyTerminationPosition < endRenderLeapingPosition )
endRenderPosition = earlyTerminationPosition;
else
endRenderPosition = endRenderLeapingPosition;
//store the render from and to information
if( endRenderPosition < startRenderPosition )
endRenderPosition = startRenderPosition;
renderFrom[ rayIdx ] = startRenderPosition;
renderTo[ rayIdx ] = endRenderPosition;
#pragma omp critical
{
sumWorkload += ( renderTo[ rayIdx ] - renderFrom[ rayIdx ] );
}
cr = ( renderTo[ rayIdx ] - renderFrom[ rayIdx ] ) / (float) m_d;
cg = ( renderTo[ rayIdx ] - renderFrom[ rayIdx ] ) / (float) m_d;
cb = ( renderTo[ rayIdx ] - renderFrom[ rayIdx ] ) / (float) m_d;
/*cr = ( renderTo[ rayIdx ] ) / (float) m_d;
cg = ( renderTo[ rayIdx ] ) / (float) m_d;
cb = ( renderTo[ rayIdx ] ) / (float) m_d;*/
unsigned char optR = (unsigned char)(int)floor(cr * 255.0);
unsigned char optG = (unsigned char)(int)floor(cg * 255.0);
unsigned char optB = (unsigned char)(int)floor(cb * 255.0);
m_color[(v*m_u+u)*4+0] = optR;
m_color[(v*m_u+u)*4+1] = optG;
m_color[(v*m_u+u)*4+2] = optB;
m_color[(v*m_u+u)*4+3] = 255;//A
}//u loop
}//v loop
//dispatch the job and store the dispatch result in dispatchID
//sumWorkload = m_u * m_v; //for load imbalance
float maxThreads = omp_get_max_threads();
float avgWorkload = sumWorkload / maxThreads;
int currentID = 0;
float workload = 0;
for( int v = 0; v< m_v; v+=1 ){
for( int u = 0; u< m_u; u+=1 ){
int rayIdx = v * m_u + u;
dispatchID[ rayIdx ] = currentID;
workload += ( renderTo[ rayIdx ] - renderFrom[ rayIdx ] ); //for load balance
//workload ++; //for load imbalane
if( workload >= avgWorkload ){
workload = 0;
currentID ++;
}
}
}
#pragma omp parallel for
for( int p = 0; p < (int) maxThreads ; p ++ ){
int threadID = omp_get_thread_num();
clock_t start_time, end_time;
float total_time = 0;
start_time = clock(); /* mircosecond */
int step = 8;
for( int v = 0; v< m_v; v+=1 ){
Histogram hist(m_bins);
Histogram befHist(m_bins);
for( int u = 0; u< m_u; u+=1 ){
int rayIdx = v * m_u + u;
if( threadID != dispatchID[ rayIdx ] )continue;
Ray* ray = m_rays[ rayIdx ];
float cr = 0, cg = 0, cb = 0, ca = 0;
for( int d = renderFrom[ rayIdx ]; d< renderTo[ rayIdx ] - step; d += step ){
//for( int d = 0; d < m_d - step; d += step ){
float frontSegTransparency, correctOpacity;
if( d>0 )
ray->getHistByIdm(&befHist, 0, d-1);
else
befHist.reset();
ray->getHistByIdm(&hist, d, d+step-1);
float rr, gg, bb;
calculateSegmentColorContribution( &befHist, &hist, colorModulation, rr, gg, bb, frontSegTransparency, correctOpacity );
cr += rr;
cg += gg;
cb += bb;
ca = ( 1 - frontSegTransparency ) + correctOpacity * frontSegTransparency; //not necessary
}
//for dispatch visualization
/*float r, g, b, a;
getRGBAbyTF( p / 8.0 , r, g, b, a, 1);
cr = r;
cg = g;
cb = b;*/
unsigned char optR = (unsigned char)(int)floor(cr * 255.0);
unsigned char optG = (unsigned char)(int)floor(cg * 255.0);
unsigned char optB = (unsigned char)(int)floor(cb * 255.0);
m_color[(v*m_u+u)*4+0] = optR;
m_color[(v*m_u+u)*4+1] = optG;
m_color[(v*m_u+u)*4+2] = optB;
m_color[(v*m_u+u)*4+3] = 255;//A
}//u loop
}//v loop
end_time = clock();
char fn[500];
sprintf( fn, "time%d.txt", p );
FILE* fp = fopen( fn, "w" );
total_time = (float)(end_time - start_time)/CLOCKS_PER_SEC;
fprintf(fp, "Time : %f sec \n", total_time);
fclose( fp );
}//p loop
free( dispatchID );
free( renderTo );
free( renderFrom );
end_time = clock();
FILE* fp = fopen( "tmp.txt", "w" );
total_time = (float)(end_time - start_time)/CLOCKS_PER_SEC;
fprintf(fp, "Time : %f sec \n", total_time);
fclose( fp );
}
void calculateSegmentColorContribution( Histogram* histFront, Histogram* hist, float colorModulation,
float &rr, float &rg, float &rb, float &frontSegTransparency, float& correctOpacity )
{
//calculate the transparency of segment before current target segment
frontSegTransparency = 1.0;
frontSegTransparency = 1.0 - calculateSegmentOpacity( histFront, colorModulation );
//calculate correct opacity and sum of opacity of target segment
correctOpacity = 1;
float sumOpacity = 0;
correctOpacity = calculateSegmentOpacity( hist, colorModulation );
//calculate color of target segment, but this is not correct
float tmpR = 0, tmpG = 0, tmpB = 0;
for( int i=0; i< m_bins; i++ ){
float r, g, b, a;
getRGBAbyTF( (i/(float)m_bins) , r, g, b, a, 1);
a *= colorModulation;
float freq = hist->getBin(i);
tmpR += r * a * freq;
tmpG += g * a * freq;
tmpB += b * a * freq;
sumOpacity += a * freq;
}
//calculate correct color and add into pixel color
float opacityReachCamera = 0.0;
if( sumOpacity > 0.0 )
opacityReachCamera = frontSegTransparency * ( correctOpacity / sumOpacity );
rr = tmpR * opacityReachCamera;
rg = tmpG * opacityReachCamera;
rb = tmpB * opacityReachCamera;
//the following is for mean color rendering, remove this part will back to histogram rendering directly
Histogram h( hist );
h.normalize();
float mean = h.getMean();
float r, g, b, a;
getRGBAbyTF( mean , r, g, b, a, 1);
rr = r * frontSegTransparency * correctOpacity;
rg = g * frontSegTransparency * correctOpacity;
rb = b * frontSegTransparency * correctOpacity;
}
bool entropyBasedRefineRendering()
{
//initialize the UI inputing parameters
float bgColor = m_sldBar8/(100.0);
float colorModulation = m_lineEdit0;
int step = m_lineEdit3;
bool continueFlag = false;
if( entropyQueue[0]->size() == 0 ){
#pragma omp parallel for
for( int v = 0; v< m_v; v+=1 ){
Histogram hist(m_bins);
Histogram befHist(m_bins);
for( int u = 0; u< m_u; u+=1 ){
int rayIdx = v*m_u+u;
Ray* ray = m_rays[rayIdx];
float cr = 0, cg = 0, cb = 0, ca = 0; //color of a pixel
float sumOfEntropy = 0;
for( int d = 0; d< m_d-step; d+=step ){
float frontSegTransparency, correctOpacity;
if( d>0 )
ray->getHistByIdm(&befHist, 0, d-1);
else
befHist.reset();
ray->getHistByIdm(&hist, d, d+step-1);
float rr, gg, bb;
calculateSegmentColorContribution( &befHist, &hist, colorModulation, rr, gg, bb, frontSegTransparency, correctOpacity );
cr += rr;
cg += gg;
cb += bb;
ca = ( 1 - frontSegTransparency ) + correctOpacity * frontSegTransparency; //not necessary
//calculate entropy and put in entropyQueue
hist.normalize();
float entropy = hist.getEntropy();
Segment seg;
seg.from = d;
seg.len = step;
seg.metric = frontSegTransparency * correctOpacity * entropy;
seg.a = frontSegTransparency; seg.b = correctOpacity;
std::vector<Segment>::iterator position;
position = std::lower_bound (entropyQueue[rayIdx]->begin(), entropyQueue[rayIdx]->end(), seg, segmentSort );
entropyQueue[rayIdx]->insert( position, seg );
sumOfEntropy += seg.metric;
}
cr += (bgColor) * (1.0 - ca);
cg += (bgColor) * (1.0 - ca);
cb += (bgColor) * (1.0 - ca);
unsigned char optR = (unsigned char)(int)floor(cr * 255.0);
unsigned char optG = (unsigned char)(int)floor(cg * 255.0);
unsigned char optB = (unsigned char)(int)floor(cb * 255.0);
m_color[(v*m_u+u)*4+0] = optR;
m_color[(v*m_u+u)*4+1] = optG;
m_color[(v*m_u+u)*4+2] = optB;
m_color[(v*m_u+u)*4+3] = 255;//A
m_colorF[(v*m_u+u)*4+0] = cr;
m_colorF[(v*m_u+u)*4+1] = cg;
m_colorF[(v*m_u+u)*4+2] = cb;
//output entropy image
float r, g, b, a;
getRGBAbyTF( sumOfEntropy/7.0 , r, g, b, a, 1); //7.0 is the maximum entropy if the bin number is 128
m_color2[(v*m_u+u)*4+0] = (unsigned char)(int)floor(r * 255.0);
m_color2[(v*m_u+u)*4+1] = (unsigned char)(int)floor(g * 255.0);
m_color2[(v*m_u+u)*4+2] = (unsigned char)(int)floor(b * 255.0);
m_color2F[(v*m_u+u)*4+0] = sumOfEntropy;
}//u
}//v
continueFlag = true;
}else{
//not first run
#pragma omp parallel for
for( int v = 0; v< m_v; v+=1 ){
Histogram hist(m_bins);
Histogram frontHist(m_bins);
for( int u = 0; u< m_u; u+=1 ){
int rayIdx = v*m_u+u;
Ray* ray = m_rays[rayIdx];
vector<Segment>::iterator it = ( entropyQueue[rayIdx]->end() - 1 );
int from = it->from;
int len = it->len;
int nextFrom = from + (int)( len / 2.0 ) ;
entropyQueue[rayIdx]->pop_back();
if( len >= 2 ){
float sumOfEntropy = m_color2F[(v*m_u+u)*4+0];
//old segment information
float oldR, oldG, oldB;
float oldFrontSegTransparency, oldCorrectOpacity;
if( from > 0 )
ray->getHistByIdm( &frontHist, 0, from - 1 );
else
frontHist.reset();
ray->getHistByIdm( &hist, from, from + len - 1 );
calculateSegmentColorContribution( &frontHist, &hist, colorModulation, oldR, oldG, oldB, oldFrontSegTransparency, oldCorrectOpacity );
hist.normalize();
float entropy = hist.getEntropy();
sumOfEntropy -= ( oldFrontSegTransparency * oldCorrectOpacity * entropy );
//first segment
float r1, g1, b1;
float frontSegTransparency1, correctOpacity1;
if( from > 0 )
ray->getHistByIdm( &frontHist, 0, from - 1 );
else
frontHist.reset();
ray->getHistByIdm( &hist, from, nextFrom - 1 );
calculateSegmentColorContribution( &frontHist, &hist, colorModulation, r1, g1, b1, frontSegTransparency1, correctOpacity1 );
hist.normalize();
float entropy1 = hist.getEntropy();
Segment seg1;
seg1.from = from;
seg1.len = nextFrom - from;
seg1.metric = frontSegTransparency1 * correctOpacity1 * entropy1;
seg1.a = frontSegTransparency1; seg1.b = correctOpacity1;
std::vector<Segment>::iterator position1;
position1 = std::lower_bound (entropyQueue[rayIdx]->begin(), entropyQueue[rayIdx]->end(), seg1, segmentSort );
entropyQueue[rayIdx]->insert( position1, seg1 );
sumOfEntropy += seg1.metric;
//second segment
float r2, g2, b2;
float frontSegTransparency2, correctOpacity2;
if( nextFrom > 0 )
ray->getHistByIdm( &frontHist, 0, nextFrom - 1 );
else
frontHist.reset();
ray->getHistByIdm( &hist, nextFrom, from + len - 1 );
calculateSegmentColorContribution( &frontHist, &hist, colorModulation, r2, g2, b2, frontSegTransparency2, correctOpacity2 );
hist.normalize();
float entropy2 = hist.getEntropy();
Segment seg2;
seg2.from = nextFrom;
seg2.len = from + len - nextFrom;
seg2.metric = frontSegTransparency2 * correctOpacity2 * entropy2;
seg2.a = frontSegTransparency2; seg2.b = correctOpacity2;
std::vector<Segment>::iterator position2;
position2 = std::lower_bound (entropyQueue[rayIdx]->begin(), entropyQueue[rayIdx]->end(), seg2, segmentSort );
entropyQueue[rayIdx]->insert( position2, seg2 );
sumOfEntropy += seg2.metric;
//update color
float changeR = ( - oldR + r1 + r2 );
float changeG = ( - oldG + g1 + g2 );
float changeB = ( - oldB + b1 + b2 );
m_colorF[(v*m_u+u)*4+0] = m_colorF[(v*m_u+u)*4+0] + changeR;
m_colorF[(v*m_u+u)*4+1] = m_colorF[(v*m_u+u)*4+1] + changeG;
m_colorF[(v*m_u+u)*4+2] = m_colorF[(v*m_u+u)*4+2] + changeB;
unsigned char optR = (unsigned char)(int)floor(m_colorF[(v*m_u+u)*4+0] * 255.0);
unsigned char optG = (unsigned char)(int)floor(m_colorF[(v*m_u+u)*4+1] * 255.0);
unsigned char optB = (unsigned char)(int)floor(m_colorF[(v*m_u+u)*4+2] * 255.0);
m_color[(v*m_u+u)*4+0] = optR;
m_color[(v*m_u+u)*4+1] = optG;
m_color[(v*m_u+u)*4+2] = optB;
m_color[(v*m_u+u)*4+3] = 255;//A
//stop or not
if( changeR > 0.075 || changeG > 0.075 || changeB > 0.075 ){
#pragma omp critical
{
continueFlag = true;
}
}
//output entropy image
float r, g, b, a;
getRGBAbyTF( sumOfEntropy / 7.0 , r, g, b, a, 1); //7.0 is the maximum entropy if the bin number is 128
m_color2[(v*m_u+u)*4+0] = (unsigned char)(int)floor(r * 255.0);
m_color2[(v*m_u+u)*4+1] = (unsigned char)(int)floor(g * 255.0);
m_color2[(v*m_u+u)*4+2] = (unsigned char)(int)floor(b * 255.0);
}
}
}
}
return continueFlag;
}
void emphasizeIsovalueInBoxRegion( Histogram *reHist, int mouseXDown, int mouseYDown, int mouseXNow, int mouseYNow, int binSelect,
int rangeS, int rangeT, int rangeRatio, float enhanceS, float enhanceT, float bgColor )
{
printf("start to run by boundary search\n");
//not real transfrer function analysis
int mtrlBoundaryCnt = 9;
float mtrlBoundary[9] = {0.125*0.0, 0.125*1.0, 0.125*2.0,
0.125*3.0, 0.125*4.0, 0.125*5.0,
0.125*6.0, 0.125*7.0, 0.125*8.0};
int rayCnt=0;
int cnt=0;
Histogram hist(m_bins);
//clock_t start_time, end_time;
//float total_time = 0;
//start_time = clock();
float* sumHist = (float*)malloc( sizeof(float)*m_bins );
memset( sumHist, 0, sizeof(float)*m_bins );
#pragma omp parallel
{
float* privateHist = (float*) malloc( sizeof(float)*m_bins );
memset( privateHist, 0, sizeof(float)*m_bins );
#pragma omp for
for( int v = 0; v< m_v; v+=1 ){
Histogram hist(m_bins);
for( int u = 0; u< m_u; u+=1 ){
Ray* r = m_rays[v*m_u+u];
int loc = -1;
float cr = 0, cg = 0, cb = 0, ca = 0;
if( mouseXDown>=u || u>=mouseXNow || mouseYDown>=v || v>=mouseYNow ){
//out of selection region, do nothging
}else{//in the select region, render and collect histgraom
int curIdx, thick;
//****************************************************
loc = -1;
int lastLoc= -1;
int step=8;
for( int d = 0; d< m_d-step; d+=step ){
r->getHistByIdm(&hist, d, d+step);
if( hist.getBin(binSelect) != 0 ){
loc = d;
break;
}
}
for( int d = m_d-20; d>=0 ; d-=step ){
r->getHistByIdm(&hist, d, d+step);
if( hist.getBin(binSelect) != 0 ){
lastLoc = d;
break;
}
}
//****************************************************
if( loc != -1 ){
//add histogram to private hist
if( loc < lastLoc ){
r->getHistByIdm(&hist, loc, lastLoc);
for( int i=0; i<m_bins; i++ ){
privateHist[i] += hist.getBin(i);
}
for( int d = loc; d< lastLoc; d+=8 ){
r->getHistByIdm(&hist, d, d+8-1);
float vxl = hist.getMean();
float r, g, b, a;
getRGBAbyTF(vxl, r, g, b, a, 1);
a*=0.1;
cr += (r*a) * (1.0 - ca);
cg += (g*a) * (1.0 - ca);
cb += (b*a) * (1.0 - ca);
ca += a * (1.0 - ca);
if( ca > 0.95 )break;
}
}
}else{//in box , but no isovalue we want
cr = 1;
cg = 0;
cb = 0;
}