-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMIVVRcontroller.mm
1886 lines (1589 loc) · 55 KB
/
CMIVVRcontroller.mm
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
/*=========================================================================
Author: Chunliang Wang ([email protected])
Program: CMIV CTA image processing Plugin for OsiriX
This file is part of CMIV CTA image processing Plugin for OsiriX.
Copyright (c) 2007,
Center for Medical Image Science and Visualization (CMIV),
Linkšping University, Sweden, http://www.cmiv.liu.se/
CMIV CTA image processing Plugin for OsiriX is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.
CMIV CTA image processing Plugin for OsiriX is distributed in
the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
=========================================================================*/
#define id Id
#include <vtkCamera.h>
#include <vtkPiecewiseFunction.h>
#undef id
#import "CMIVVRcontroller.h"
#import "QuicktimeExport.h"
#if 1 // @@@
#import <AVFoundation/AVFoundation.h>
#else
#include "VRMakeObject.h"
#import <QTKit/QTKit.h>
#endif
#import "DICOMExport.h"
#import <MieleAPI/BrowserController.h>
#import "url2.h" // for OUR_DATA_LOCATION
#import "VRView.h"
static void needAdjustClipPlane(vtkObject*,unsigned long c, void* ptr, void*)
{
CMIVVRcontroller* controller = (CMIVVRcontroller*) ptr;
[controller resetClipPlane];
}
@implementation CMIVVRcontroller
-(NSImage*) imageForFrame:(NSNumber*) cur maxFrame:(NSNumber*) max
{
NSImage* curimage = nil;
int curangle = [cur intValue] / maxMovieIndex;
int cursegment = [cur intValue] % maxMovieIndex;
#if 1 // @@@ QT todo
CMTimeValue timeValue = 60*(cursegment*220+curangle);
CMTime frameDuration = CMTimeMake( timeValue, 60); // 60 TBC
#else
QTMovie *mMovie = [imagesFor4DQTVR objectAtIndex:0]; // "As of macOS 10.15 this method always returns nil.
long long timeValue = 60*(cursegment*220+curangle);
long timeScale = 600;
QTTime curTime = QTMakeTime(timeValue, timeScale);
curimage=[mMovie frameImageAtTime:curTime];
#endif
return curimage;
}
-(NSImage*) imageForVR:(NSNumber*) cur maxFrame:(NSNumber*) max
{
if ([max intValue]==220)
{
int iteminrow=20,itemincolumn=10;
if ( [cur intValue] == 0)
{
[vrViewer Vertical: 45];
[vrViewer Vertical: 45];
verticalAngleForVR=90;
}
else
{
if ( verticalAngleForVR==-90 )
{
aCamera->Roll(360/iteminrow);
}
else if( verticalAngleForVR==90)
{
aCamera->Roll(-360/iteminrow);
}
else
{
[vrViewer Vertical: -verticalAngleForVR];
}
if ([cur intValue]%iteminrow==0)
{
if ( verticalAngleForVR==-90 || verticalAngleForVR==90)
{
[vrViewer Vertical: -45];
[vrViewer Vertical: -45];
aCamera->Azimuth(-360/iteminrow);
}
verticalAngleForVR-=180/itemincolumn;
if ( verticalAngleForVR==-90 )
{
aCamera->Azimuth(360/iteminrow);
[vrViewer Vertical: -45];
[vrViewer Vertical: -45];
}
}
if (verticalAngleForVR!=-90 && verticalAngleForVR!=90)
{
aCamera->Azimuth(360/iteminrow);
aCamera->Elevation(verticalAngleForVR);
}
}
}
NSImage* tempImage = [vrViewer nsimageQuicktime];
if ([max intValue]==220)
{
int iteminrow=20; //itemincolumn=10;
if ( [cur intValue] == 219)
{
aCamera->Roll(360/iteminrow);
[vrViewer Vertical: 45];
[vrViewer Vertical: 45];
verticalAngleForVR=0;
}
}
return tempImage;
}
- (int) prepareImageFor4DQTVR
{
NSString *fileName = [[self hostAppDocumentPath2] stringByAppendingPathComponent:@"/TEMP/CMIV4DQTVR.mov"];
#if 1 // @@@ QT todo
NSError *error = nil;
AVAssetWriter *writer = [[AVAssetWriter alloc] initWithURL: [NSURL fileURLWithPath: fileName] fileType: AVFileTypeQuickTimeMovie error:&error];
// [writer addInput:writerInput];
// [writer startWriting];
// [writer startSessionAtSourceTime:nextPresentationTimeStamp];
// [writerInput markAsFinished];
// [writer finishWritingWithCompletionHandler:^{ }];
// [writerInput release];
// [writer release];
#else
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys: @"jpeg", QTAddImageCodecType, [NSNumber numberWithInt: codecHighQuality], QTAddImageCodecQuality, nil]; // qdrw, tiff, jpeg
[[QTMovie movie] writeToFile: fileName withAttributes: 0L];
QTMovie *mMovie = [QTMovie movieWithFile:fileName error:nil];
[mMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
#endif
long long timeValue = 60;
long timeScale = 600;
#if 0 // @@@ QT
QTTime curTime = QTMakeTime(timeValue, timeScale);
#endif
NSImage* tempImage = nil;
for (int i=0; i<maxMovieIndex; i++)
{
[segmentList selectRow:i byExtendingSelection:NO];
[self selectASegment:segmentList];
for (int j=0; j<220; j++)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
tempImage=[self imageForVR:[NSNumber numberWithInt:j] maxFrame:[NSNumber numberWithInt:220]];
#if 0 // @@@ QT
[mMovie addImage:tempImage forDuration:curTime withAttributes: myDict];
#endif
[pool release];
}
}
#if 0 // @@@ QT
[imagesFor4DQTVR addObject:mMovie];
#endif
return [imagesFor4DQTVR count];
}
- (IBAction)captureImage:(id)sender
{
if (!isSegmentVR)
{
float o[ 9];
DCMPix *firstObject=[[originalViewController pixList] objectAtIndex: 0];
DICOMExport *dcmSequence = [[DICOMExport alloc] init];
[dcmSequence setSeriesNumber:6700 + [[NSCalendarDate date] minuteOfHour] + [[NSCalendarDate date] secondOfMinute]];
[dcmSequence setSeriesDescription:@"4D VR"];
[dcmSequence setSourceFile: [firstObject sourceFile]];
[vrViewer renderImageWithBestQuality: YES waitDialog: NO];
for (int i=0; i<maxMovieIndex; i++)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float* newVolumeData=[originalViewController volumePtr:i];
//[vrViewer movieBlendingChangeSource:i];
[vrViewer movieChangeSource: newVolumeData];
[self setBlendVolumeCLUT];
volumeOfVRView = (vtkVolume *)[vrViewer volume];
volumeMapper = (vtkVolumeMapper *) volumeOfVRView->GetMapper() ;
if ([cutPlaneSwitch state] == NSControlStateValueOn)
volumeMapper->AddClippingPlane(clipPlane1);
[clutViewer setCurves:[mutiplePhaseOpacityCurves objectAtIndex:i ]];
[clutViewer setPointColors:[mutiplePhaseColorCurves objectAtIndex:i ]];
[clutViewer setClutChanged];
[clutViewer updateView];
// [clutViewer setCLUTtoVRViewWithoutRedraw];
// [vrViewer display];
[vrViewer renderImageWithBestQuality: YES waitDialog: NO display: YES];
long width, height;
long spp, bpp;
//int spp, bpp;
long err;
unsigned char *dataPtr = nullptr;
dataPtr = [vrViewer getRawPixels:&width :&height :&spp :&bpp :YES :NO];
[vrViewer endRenderImageWithBestQuality];
if (dataPtr)
{
[vrViewer getOrientation: o];
[dcmSequence setOrientation: o];
#if 1
[dcmSequence setPixelData:dataPtr
samplesPerPixel:(int)spp
bitsPerSample:(int)bpp
width:width
height:height];
#else // original
[dcmSequence setPixelData: dataPtr
samplePerPixel: spp
bitsPerPixel: bpp
width: width
height: height];
#endif
NSString *f = [dcmSequence writeDCMFile: 0L];
if (f)
[BrowserController addFiles: [NSArray arrayWithObject: f]
toContext: [[BrowserController currentBrowser] managedObjectContext]
toDatabase: [BrowserController currentBrowser]
onlyDICOM: YES
notifyAddedFiles: YES
parseExistingObject: YES
dbFolder: [[BrowserController currentBrowser] documentsDirectory]
generatedByOsiriX: YES];
free( dataPtr);
}
[pool release];
}
[vrViewer endRenderImageWithBestQuality];
[dcmSequence release];
}
else
{
[vrViewer exportDCMCurrentImage];
}
}
- (IBAction)openQTVRExportDlg:(id)sender
{
[NSApp beginSheet: qtvrsettingwin
modalForWindow: [self window]
modalDelegate: self
didEndSelector: nil
contextInfo: nil];
}
- (IBAction)creatQTVRFromFile:(id)sender
{
int result;
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[oPanel setAllowsMultipleSelection:NO];
[oPanel setCanChooseDirectories:NO];
result = [oPanel runModalForDirectory:0L file:nil types:nil];
if (result == NSModalResponseOK)
{
NSString* path;
path=[[oPanel filenames] objectAtIndex: 0];
#if 0 // @@@ QT
QTMovie *mMovie = [QTMovie movieWithFile:path error:nil];
[mMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
#endif
long long timeValue = 60;
long timeScale = 600;
#if 0 // @@@ QT
QTTime curTime = QTMakeTime(timeValue, timeScale);
#endif
imagesFor4DQTVR=[[NSMutableArray alloc] initWithCapacity: 0];
#if 0 // @@@ QT
[imagesFor4DQTVR addObject:mMovie];
#endif
do
{
QuicktimeExport *mov = [[QuicktimeExport alloc] initWithSelector: self : @selector(imageForFrame: maxFrame:) :220*maxMovieIndex];
path=[mov createMovieQTKit:YES :NO :[[[originalViewController fileList] objectAtIndex:0] valueForKeyPath:@"series.study.name"]];
[mov release];
} while(path);
[imagesFor4DQTVR removeAllObjects];
[imagesFor4DQTVR release];
}
}
- (IBAction)closeQTVRExportDlg:(id)sender
{
int tag =[sender tag];
[qtvrsettingwin orderOut:sender];
[NSApp endSheet:qtvrsettingwin returnCode:tag];
}
- (IBAction)exportQTVR:(id)sender
{
//FSRef fsref;
//FSSpec spec, newspec;
// [vrViewer renderImageWithBestQuality: YES waitDialog: NO];
if (!isSegmentVR)
{
// [vrViewer renderImageWithBestQuality: YES waitDialog: NO];
[self closeQTVRExportDlg:sender];
imagesFor4DQTVR=[[NSMutableArray alloc] initWithCapacity: 0];
[self prepareImageFor4DQTVR];
NSString* path = nil;
do
{
QuicktimeExport *mov = [[QuicktimeExport alloc] initWithSelector: self : @selector(imageForFrame: maxFrame:) :220*maxMovieIndex];
path = [mov createMovieQTKit:YES :NO :[[[originalViewController fileList] objectAtIndex:0] valueForKeyPath:@"series.study.name"]];
[mov release];
} while(path);
[imagesFor4DQTVR removeAllObjects];
[imagesFor4DQTVR release];
NSString *fileName = [[self hostAppDocumentPath2] stringByAppendingPathComponent:@"/TEMP/CMIV4DQTVR.mov"] ;
[[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
}
else
{
verticalAngleForVR = 0;
QuicktimeExport *mov = [[QuicktimeExport alloc] initWithSelector: self : @selector(imageForVR: maxFrame:) :220];
NSString* path = [mov createMovieQTKit:YES :NO :[[[originalViewController fileList] objectAtIndex:0] valueForKeyPath:@"series.study.name"]];
[mov release];
}
// [vrViewer endRenderImageWithBestQuality];
}
- (IBAction)endPanel:(id)sender
{
if (clipCallBack)
[vrViewer renderWindow]->RemoveObserver(clipCallBack);
if (clipPlaneWidget)
clipPlaneWidget->Delete();
if (clipCallBack)
clipCallBack->Delete();
if (clipPlane1)
clipPlane1->Delete();
clipCallBack=nil;
clipPlaneWidget=nil;
clipPlane1=nil;
[segmentList setDataSource:nil];
[[self window] performClose:sender];
}
- (void)windowWillClose:(NSNotification *)notification
{
if( [notification object] == [self window])
{
if(isSegmentVR)
{
if(volumePropteryOfVRView)
volumeOfVRView->SetProperty( volumePropteryOfVRView);
[propertyDictList removeAllObjects];
if(colorMapFromFile)
free(colorMapFromFile);
}
unsigned int i,j;
for(i=0;i<[mutiplePhaseOpacityCurves count];i++) // every segment or volume
{
for(j=0;j<[[mutiplePhaseOpacityCurves objectAtIndex:i] count];j++) // every curve for each segment or volume
{
[[[mutiplePhaseOpacityCurves objectAtIndex:i] objectAtIndex:j] removeAllObjects];
[[[mutiplePhaseColorCurves objectAtIndex:i] objectAtIndex:j] removeAllObjects];
}
[[mutiplePhaseOpacityCurves objectAtIndex:i] removeAllObjects];
[[mutiplePhaseColorCurves objectAtIndex:i] removeAllObjects];
}
[mutiplePhaseOpacityCurves removeAllObjects];
[mutiplePhaseColorCurves removeAllObjects];
[mutiplePhaseOpacityCurves release];
[mutiplePhaseColorCurves release];
[[NSNotificationCenter defaultCenter] removeObserver: self];
if( inROIArray)
[inROIArray removeAllObjects];
[[self window] setDelegate:nil];
[originalViewController release];
[originalViewVolumeData release];
[originalViewPixList release];
[self autorelease];
}
}
-(void) dealloc
{
if (clipCallBack)
[vrViewer renderWindow]->RemoveObserver(clipCallBack);
if (clipPlaneWidget)
clipPlaneWidget->Delete();
if (clipCallBack)
clipCallBack->Delete();
if (clipPlane1)
clipPlane1->Delete();
[super dealloc];
[vrViewer prepareForRelease];
}
- (IBAction)setColorProtocol:(id)sender
{
unsigned int row = [segmentList selectedRow];
[self applyCLUTToPropertyList:row];
[self applyCLUT];
}
- (IBAction)setBackgroundColor:(id)sender
{
[vrViewer changeColorWith:[colorControl color]];
}
- (IBAction)setOpacity:(id)sender
{
unsigned int row = [segmentList selectedRow];
float opacity = [opacitySlider floatValue];
if (row>=0 && row<[propertyDictList count])
[[propertyDictList objectAtIndex: row] setObject: [NSNumber numberWithFloat:opacity] forKey:@"Opacity"];
[self applyOpacity];
[segmentList reloadData];
}
- (IBAction)setWLWW:(id)sender
{
float ww,wl;
ww = [wwSlider floatValue];
wl = [wlSlider floatValue];
unsigned int i;
if ([wlwwForAll state] == NSControlStateValueOn)
{
for(i=0;i<[propertyDictList count];i++)
{
[[propertyDictList objectAtIndex:i] setObject: [NSNumber numberWithFloat:ww] forKey:@"WW"];
[[propertyDictList objectAtIndex:i] setObject: [NSNumber numberWithFloat:wl] forKey:@"WL"];
}
}
else
{
i = [segmentList selectedRow];
if(i>=0&&i<[propertyDictList count])
{
[[propertyDictList objectAtIndex:i] setObject: [NSNumber numberWithFloat:ww] forKey:@"WW"];
[[propertyDictList objectAtIndex:i] setObject: [NSNumber numberWithFloat:wl] forKey:@"WL"];
}
}
[self applyCLUT];
[self applyOpacity];
}
- (id) showVRPanel:(ViewerController *) vc
:(CMIV_CTA_TOOLS*) owner
{
// Initialize the window
self = [super initWithWindowNibName:@"VR_Panel"];
[[self window] setDelegate:self];
// Prepare VTK volume for 4D or 3.5D data
int err=0;
originalViewController=vc;
parent = owner;
blendingController=[vc blendingController];
originalViewVolumeData=[vc volumeData];
originalViewPixList=[vc pixList];
[originalViewController retain];
[originalViewVolumeData retain];
[originalViewPixList retain];
maxMovieIndex=[vc maxMovieIndex];
if (maxMovieIndex>1)
isSegmentVR=0;
else
isSegmentVR=1;
if (isSegmentVR)
err= [self initVRViewForSegmentalVR];
else
{
err= [self initVRViewForDynamicVR];
// show the window
screenrect=[[[originalViewController window] screen] visibleFrame];
[[self window]setFrame:screenrect display:NO animate:NO];
[super showWindow:parent];
}
if (err)
{
[self endPanel:self];
return nil;
}
[[self window] makeKeyAndOrderFront:parent];
[[self window] display];
[[self window] setMovableByWindowBackground:NO];
//
volumeOfVRView = (vtkVolume * )[vrViewer volume];
fixedPointVolumeMapper=(vtkVolumeMapper *) volumeOfVRView->GetMapper() ;
volumeMapper=fixedPointVolumeMapper;
volumeImageData=(vtkImageData *)volumeMapper->GetInput();
realVolumedata=(unsigned short*)volumeImageData->GetScalarPointer();
// Prepare clipPlane
clipPlane1=vtkPlane::New();
//volumeMapper->AddClippingPlane(clipPlane1);
double* tempcenter;
tempcenter=volumeOfVRView->GetCenter();
clipPlane1->SetOrigin(tempcenter);
clipPlane1->SetNormal(1,0,0);
clipCallBack = vtkCallbackCommand::New();
clipCallBack->SetCallback( needAdjustClipPlane);
clipCallBack->SetClientData( self);
// Need VTK graphics library but cause crash for 2D cross section measurement.
[vrViewer renderWindow]->AddObserver(vtkCommand::StartEvent, clipCallBack);
//clipPlaneWidget=vtkPlaneWidget::New();
// Prepare the 16-bit CLUT
[self initCLUTView];
if(isSegmentVR)
{
[clutViewer setVRController:self];
[self initTaggedColorList];
}
return self;
}
- (void)initTaggedColorList
{
unsigned int i;
RGBColor color;
ROI * tempROI;
for (i=0;i<[inROIArray count];i++)
{
tempROI = [inROIArray objectAtIndex: i];
color= [tempROI rgbcolor];
NSMutableArray *someColors = [[mutiplePhaseColorCurves objectAtIndex:i+1] objectAtIndex:0];
[someColors replaceObjectAtIndex:1 withObject:[NSColor colorWithDeviceRed:color.red/65536.0 green:color.green/65536.0 blue:color.blue/65536.0 alpha:1.0]];
[someColors replaceObjectAtIndex:2 withObject:[NSColor colorWithDeviceRed:color.red/65536.0 green:color.green/65536.0 blue:color.blue/65536.0 alpha:1.0]];
}
}
- (int)loadMaskFromTempFolder
{
int step=0;//[parent loadCrashBackup];
if(step!=101)
{
return 0;
}
int size = sizeof(unsigned char ) * imageWidth * imageHeight * imageAmount;
NSMutableDictionary* dic=[parent dataOfWizard];
NSNumber* maskmapsize=[dic objectForKey:@"MaskMapSize"];
if(size!=[maskmapsize intValue])
{
NSLog(@"maskmapsize doesn't match");
return 0;
}
colorMapFromFile=(unsigned char*)malloc(size);
if(!colorMapFromFile)
{
NSLog(@"Not enough memory for mask map");
return 0;
}
NSArray* seednamearray=[dic objectForKey:@"SeedNameArray"];
NSArray* seedscolorR=[dic objectForKey:@"SeedsColorR"];
NSArray* seedscolorG=[dic objectForKey:@"SeedsColorG"];
NSArray* seedscolorB=[dic objectForKey:@"SeedsColorB"];
NSString* maskmapfile=[dic objectForKey:@"MaskMapPath"];
FILE* tempFile = fopen([maskmapfile cStringUsingEncoding:NSASCIIStringEncoding],"r");
fread(colorMapFromFile,sizeof(char),[maskmapsize intValue],tempFile);
fclose(tempFile);
[inROIArray removeAllObjects];
unsigned int i;
for (i=0;i<[seednamearray count];i++)
{
ROI *newROI;
newROI=[[ROI alloc] initWithType: tOval :1.0 :1.0 : NSMakePoint( 0, 0)];
[newROI setName:[seednamearray objectAtIndex:i]];
RGBColor c;
c.red =[[seedscolorR objectAtIndex:i] intValue];
c.green =[[seedscolorG objectAtIndex:i] intValue];
c.blue = [[seedscolorB objectAtIndex:i] intValue];
[newROI setColor:c];
[inROIArray addObject:newROI];
[newROI release];
}
[parent cleanDataOfWizard];
return 1;
}
- (void) initCLUTView
{
[clutViewer setVolumePointer:originalVolumeData width:imageWidth height:imageHeight numberOfSlices:imageAmount];
[clutViewer setHUmin:minInSeries HUmax:maxInSeries];
[clutViewer computeHistogram];
[clutViewer addCurveIfNeeded];
[clutViewer updateView];
[clutViewer setCLUTtoVRView:NO];
mutiplePhaseOpacityCurves=[[NSMutableArray alloc] initWithCapacity: 0];
mutiplePhaseColorCurves=[[NSMutableArray alloc] initWithCapacity: 0];
isShowingVolumeArray=[[NSMutableArray alloc] initWithCapacity: 0];
int i;
if(isSegmentVR)
{
for(i=0;i<(signed)[propertyDictList count];i++)
{
NSMutableArray* opacitycurves=[NSMutableArray arrayWithCapacity:0];
[mutiplePhaseOpacityCurves addObject:opacitycurves];
NSMutableArray* colorcurvers=[NSMutableArray arrayWithCapacity:0];
[mutiplePhaseColorCurves addObject:colorcurvers];
[clutViewer setCurves:opacitycurves];
[clutViewer setPointColors:colorcurvers];
[clutViewer newCurve:self];
[isShowingVolumeArray addObject:[NSNumber numberWithInt:1]];
}
}
else
{
for(i=0;i<maxMovieIndex;i++)
{
NSMutableArray* opacitycurves=[NSMutableArray arrayWithCapacity:0];
[mutiplePhaseOpacityCurves addObject:opacitycurves];
NSMutableArray* colorcurvers=[NSMutableArray arrayWithCapacity:0];
[mutiplePhaseColorCurves addObject:colorcurvers];
[clutViewer setCurves:opacitycurves];
[clutViewer setPointColors:colorcurvers];
[clutViewer newCurve:self];
}
}
[clutViewer setCurves:[mutiplePhaseOpacityCurves objectAtIndex:0 ]];
[clutViewer setPointColors:[mutiplePhaseColorCurves objectAtIndex:0 ]];
// [clutOpacityView newCurve:self];
//if(![view advancedCLUT])[[[clutPopup menu] itemAtIndex:0] setTitle:NSLocalizedString(@"16-bit CLUT", nil)];
//if(![view advancedCLUT])[self setCurCLUTMenu:NSLocalizedString(@"16-bit CLUT", nil)];
//[OpacityPopup setEnabled:NO];
}
- (int) initVRViewForSegmentalVR
{
int err=0;
DCMPix* curPix = [[originalViewController pixList] objectAtIndex: [[originalViewController imageView] curImage]];;
NSMutableArray *pixList = [originalViewController pixList];
maxInSeries = [curPix maxValueOfSeries];
minInSeries = [curPix minValueOfSeries];
imageWidth = [curPix pwidth];
imageHeight = [curPix pheight];
imageAmount = [pixList count];
// Initialize proptery list
propertyDictList = [[NSMutableArray alloc] initWithCapacity: 0];
inROIArray = [[NSMutableArray alloc] initWithCapacity: 0];
NSMutableArray *curRoiList = [originalViewController roiList];
ROI * tempROI;
unsigned int i,j,k;
if(![self loadMaskFromTempFolder])
{
int thereIsSameName ;
for(i=0;i<[curRoiList count];i++)
for(j=0;j<[[curRoiList objectAtIndex:i] count];j++)
{
tempROI = [[curRoiList objectAtIndex: i] objectAtIndex:j];
thereIsSameName=0;
if([tempROI type]==tPlain)
{
for(k=0;k<[inROIArray count];k++)
{
if ([[tempROI name] isEqualToString:[[inROIArray objectAtIndex: k] name]]==YES)
thereIsSameName=1;
}
if(!thereIsSameName)
{
[inROIArray addObject:tempROI];
}
}
}
}
NSMutableDictionary *tempProperyDict;
float segmentWW,segmentWL;
// segmentWW=[curPix ww];
// segmentWL=[curPix wl];
segmentWW = 600;
segmentWL = 200;
segmentWW = segmentWW*2047/(maxInSeries-minInSeries);
segmentWL = (segmentWL-minInSeries)*2047/(maxInSeries-minInSeries);
[wlSlider setFloatValue: segmentWL];
[wwSlider setFloatValue: segmentWW];
RGBColor color;
NSMutableArray *rArray,*gArray,*bArray,*xArray;
rArray = [[NSMutableArray alloc] initWithCapacity: 0];
gArray = [[NSMutableArray alloc] initWithCapacity: 0];
bArray = [[NSMutableArray alloc] initWithCapacity: 0];
xArray = [[NSMutableArray alloc] initWithCapacity: 0];
[rArray addObject: [NSNumber numberWithFloat:0.0]];
[gArray addObject: [NSNumber numberWithFloat:0.0]];
[bArray addObject: [NSNumber numberWithFloat:0.0]];
[xArray addObject: [NSNumber numberWithFloat:0.0]];
[rArray addObject: [NSNumber numberWithFloat:1.0]];
[gArray addObject: [NSNumber numberWithFloat:0.0]];
[bArray addObject: [NSNumber numberWithFloat:0.0]];
[xArray addObject: [NSNumber numberWithFloat:1024.0]];
[rArray addObject: [NSNumber numberWithFloat:1.0]];
[gArray addObject: [NSNumber numberWithFloat:1.0]];
[bArray addObject: [NSNumber numberWithFloat:0.0]];
[xArray addObject: [NSNumber numberWithFloat:1500.0]];
[rArray addObject: [NSNumber numberWithFloat:1.0]];
[gArray addObject: [NSNumber numberWithFloat:1.0]];
[bArray addObject: [NSNumber numberWithFloat:1.0]];
[xArray addObject: [NSNumber numberWithFloat:2047.0]];
tempProperyDict = [NSMutableDictionary dictionary];
[tempProperyDict setObject: @"other part" forKey:@"Name"];
[tempProperyDict setObject: [NSNumber numberWithFloat:0.0] forKey:@"RangeFrom"];
[tempProperyDict setObject: rArray forKey:@"RedTable"];
[tempProperyDict setObject: gArray forKey:@"GreenTable"];
[tempProperyDict setObject: bArray forKey:@"BlueTable"];
[tempProperyDict setObject: xArray forKey:@"xTableForColorPoints"];
[tempProperyDict setObject: [NSNumber numberWithFloat:0.001] forKey:@"Opacity"];
[tempProperyDict setObject: [NSNumber numberWithFloat:segmentWW] forKey:@"WW"];
[tempProperyDict setObject: [NSNumber numberWithFloat:segmentWL] forKey:@"WL"];
[propertyDictList addObject: tempProperyDict];
for(i=0;i<[inROIArray count];i++)
{
tempROI = [inROIArray objectAtIndex: i];
rArray = [[NSMutableArray alloc] initWithCapacity: 0];
gArray = [[NSMutableArray alloc] initWithCapacity: 0];
bArray = [[NSMutableArray alloc] initWithCapacity: 0];
xArray = [[NSMutableArray alloc] initWithCapacity: 0];
[rArray addObject: [NSNumber numberWithFloat:0.0]];
[gArray addObject: [NSNumber numberWithFloat:0.0]];
[bArray addObject: [NSNumber numberWithFloat:0.0]];
[xArray addObject: [NSNumber numberWithFloat:0.0]];
color= [tempROI rgbcolor];
[rArray addObject: [NSNumber numberWithFloat:color.red/65536.0]];
[gArray addObject: [NSNumber numberWithFloat:color.green /65536.0]];
[bArray addObject: [NSNumber numberWithFloat:color.blue/65536.0]];
[xArray addObject: [NSNumber numberWithFloat:1024.0]];
[rArray addObject: [NSNumber numberWithFloat:1.0]];
[gArray addObject: [NSNumber numberWithFloat:1.0]];
[bArray addObject: [NSNumber numberWithFloat:1.0]];
[xArray addObject: [NSNumber numberWithFloat:2047.0]];
tempProperyDict = [NSMutableDictionary dictionary];
[tempProperyDict setObject: [tempROI name] forKey:@"Name"];
[tempProperyDict setObject: [NSNumber numberWithFloat:(i+1)*2048.0] forKey:@"RangeFrom"];
[tempProperyDict setObject: rArray forKey:@"RedTable"];
[tempProperyDict setObject: gArray forKey:@"GreenTable"];
[tempProperyDict setObject: bArray forKey:@"BlueTable"];
[tempProperyDict setObject: xArray forKey:@"xTableForColorPoints"];
[tempProperyDict setObject: [NSNumber numberWithFloat:1.0] forKey:@"Opacity"];
[tempProperyDict setObject: [NSNumber numberWithFloat:segmentWW] forKey:@"WW"];
[tempProperyDict setObject: [NSNumber numberWithFloat:segmentWL] forKey:@"WL"];
[propertyDictList addObject: tempProperyDict];
}
curProperyDict = [propertyDictList objectAtIndex: 0];
wholeVolumeWW = [propertyDictList count]*2048;
wholeVolumeWL = wholeVolumeWW/2;
if ([curPix SUVConverted])
{
NSAlert *alert = [[NSAlert new] autorelease];
alert.alertStyle = NSAlertStyleCritical;
alert.messageText = NSLocalizedString(@"SUVConverted",nil);
alert.informativeText = NSLocalizedString(@"SUVConverted is true, can not apply segment volume rendering.",nil);
[alert runModal];
return 0;
}
if ([curPix isRGB])
{
NSAlert *alert = [[NSAlert new] autorelease];
alert.alertStyle = NSAlertStyleCritical;
alert.messageText = NSLocalizedString(@"no RGB Support",nil);
alert.informativeText = NSLocalizedString(@"This plugin doesn't surpport RGB images, please convert this series into BW images first", nil);
[alert runModal];
return 0;
}
// Initialize VR view
originalVolumeData=[originalViewController volumePtr:0];
err = [vrViewer setPixSource:pixList :originalVolumeData];
//clutViewPoints=[colorViewer getPoints];
//clutViewColors=[colorViewer getColors];
NSString* path=[parent hostAppDocumentPath];
NSString *str = [path stringByAppendingString:@"/CMIVCTACache/VRT.sav"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: str];
// if(dict)
// [self applyAdvancedCLUT:dict];
if (err != 0)
{
NSAlert *alert = [[NSAlert new] autorelease];
alert.alertStyle = NSAlertStyleCritical;
alert.messageText = NSLocalizedString(MALLOC_ERROR_MESSAGE2,nil);
alert.informativeText = NSLocalizedString(@"Not enough memory (RAM) to use the 3D engine.",nil);
[alert runModal];
return err;
}
[vrViewer set3DStateDictionary:dict];
// Get the control of color and opacity
renderOfVRView = [vrViewer renderer];
aCamera = renderOfVRView->GetActiveCamera();
//volumeCollectionOfVRView = renderOfVRView->GetVolumes();
volumeOfVRView = (vtkVolume * )[vrViewer volume]; //volumeOfVRView = (vtkVolume * )volumeCollectionOfVRView->GetItemAsObject (0);
volumePropteryOfVRView = volumeOfVRView->GetProperty();
myVolumeProperty = vtkVolumeProperty::New();
myColorTransferFunction = vtkColorTransferFunction::New();
#if 1 // @@@
myOpacityTransferFunction = vtkPiecewiseFunction::New();
#endif
myVolumeProperty->SetColor( myColorTransferFunction );
myVolumeProperty->SetScalarOpacity(myOpacityTransferFunction);
myVolumeProperty->ShadeOn();
myVolumeProperty->SetAmbient(0.15);
myVolumeProperty->SetDiffuse(0.9);
myVolumeProperty->SetSpecular(0.3);
myVolumeProperty->SetSpecularPower(15);
myVolumeProperty->SetShade( 1);
// if( [[NSApp currentEvent] modifierFlags] & NSEventModifierFlagOption)
// myVolumeProperty->SetInterpolationTypeToNearest();
// else
// myVolumeProperty->SetInterpolationTypeToLinear();// can not use linear interpolation because the CT value jump at edge
volumeOfVRView->SetProperty( myVolumeProperty);
// myGradientTransferFunction = vtkPiecewiseFunction::New();
// myGradientTransferFunction->AddPoint(3,0.0);
// myGradientTransferFunction->AddPoint(4,1.0);
//// myGradientTransferFunction->AddPoint(2000,1.0);
// myGradientTransferFunction->AddPoint(2001,0.0);
// volumePropteryOfVRView->SetGradientOpacity( myGradientTransferFunction );
// volumePropteryOfVRView->ShadeOff ();
// volumePropteryOfVRView->SetInterpolationTypeToLinear();// can not use linear interpolation because the CT value jump at edge
// Create new datevolume and new pixlist
float tempfloat;
fixedPointVolumeMapper=(vtkVolumeMapper *) volumeOfVRView->GetMapper() ;
volumeMapper=fixedPointVolumeMapper;
volumeImageData=(vtkImageData *)volumeMapper->GetInput();
realVolumedata=(unsigned short*)volumeImageData->GetScalarPointer();
unsigned int size=(unsigned int)(imageWidth*imageHeight*imageAmount);
for(i=0;i<size;i++)
{
if(*(originalVolumeData+i)>10000)
tempfloat=0;
tempfloat=( (*(originalVolumeData+i))-minInSeries)*2047/(maxInSeries-minInSeries);
if( tempfloat<=0)
tempfloat= 1.0;
else if(tempfloat>2047.0)
tempfloat = 2047.0;
*(realVolumedata+i)=(unsigned short)tempfloat;
}
if(colorMapFromFile)
{
for(i=0;i<size;i++)
{
*(realVolumedata+i)+=(*(colorMapFromFile+i))*2048;
}
}
else
{
unsigned short segOffset;
// Set segment value
int x,y,x1,x2,y1,y2,textureWidth;
unsigned char * texture;
for(i=0;i<[curRoiList count];i++)
for(j=0;j<[[curRoiList objectAtIndex:i] count];j++)
{
tempROI = [[curRoiList objectAtIndex: i] objectAtIndex:j];
if([tempROI type]==tPlain)
for(k=0;k<[inROIArray count];k++)
if ([[tempROI name] isEqualToString:[[inROIArray objectAtIndex: k] name]]==YES)
{
x1 = [tempROI textureUpLeftCornerX];
y1 = [tempROI textureUpLeftCornerY];
x2 = [tempROI textureDownRightCornerX];
y2 = [tempROI textureDownRightCornerY];