-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMIVChopperController.mm
886 lines (756 loc) · 27.5 KB
/
CMIVChopperController.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
/*=========================================================================
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 <vtkImageData.h>
#undef id
#import "CMIVChopperController.h"
#import "url2.h" // for MALLOC_ERROR_MESSAGE2
@implementation CMIVChopperController
- (IBAction)changeReformView:(id)sender
{
[reformViewSlider setMaxValue: imageHeight];
[reformViewSlider setIntValue: imageHeight/2];
if ([reformViewState selectedSegment])
[curReformROI setROIRect:sagittalROIRect];
else
[curReformROI setROIRect:coronalROIRect];
[self setReformViewIndex:reformViewSlider];
}
- (IBAction)setImageFromTo:(id)sender
{
iImageFrom = [imageFrom intValue];
iImageTo = [imageTo intValue];
coronalROIRect.origin.y = (iImageFrom-1) / ratioYtoThick ;
coronalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick;
sagittalROIRect.origin.y = (iImageFrom-1)/ ratioYtoThick ;
sagittalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick ;
[self updateImageFromToSliders];
[self updateALLROIs];
}
- (IBAction)setImageFromToSlider:(id)sender
{
iImageFrom = [imageFromSlider intValue];
iImageTo = [imageToSlider intValue];
coronalROIRect.origin.y = (iImageFrom-1) / ratioYtoThick ;
coronalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick;
sagittalROIRect.origin.y = (iImageFrom-1)/ ratioYtoThick ;
sagittalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick ;
[self updateAllTextField];
[self updateALLROIs];
}
- (IBAction)setCurrentToImageFromTo:(id)sender
{
if ([sender tag])
iImageTo = imageAmount - [ originalView curImage ];
else
iImageFrom = imageAmount - [ originalView curImage ];
coronalROIRect.origin.y = (iImageFrom-1) / ratioYtoThick ;
coronalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick;
sagittalROIRect.origin.y = (iImageFrom-1)/ ratioYtoThick ;
sagittalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick ;
[self updateALLROIs];
[self updateAllTextField];
[self updateImageFromToSliders];
}
- (IBAction)setROIRect:(id)sender
{
roiRect.origin.x = [leftTopX intValue];
roiRect.origin.y = [leftTopY intValue];
roiRect.size.width = [rightBottomX intValue] - [leftTopX intValue];
roiRect.size.height = [rightBottomY intValue] - [leftTopY intValue];
coronalROIRect.origin.x = roiRect.origin.x;
coronalROIRect.size.width = roiRect.size.width ;
sagittalROIRect.origin.x = roiRect.origin.y / ratioXtoY ;
sagittalROIRect.size.width = roiRect.size.height / ratioXtoY;
[self updateALLROIs];
}
- (void)windowWillClose:(NSNotification *)notification
{
if (reader)
{
reader->Delete() ;
sliceTransform->Delete();
rotate->Delete();
}
for (unsigned int i = 0; i < [[originalViewController pixList] count]; i++)
{
[[roiListAxial objectAtIndex:i] removeAllObjects];
}
[originalView setDrawing: NO];
[reformView setDrawing: NO];
[roiListAxial removeAllObjects];
[roiListAxial release];
[curAxialROI release];
[[roiListReform objectAtIndex: 0] removeAllObjects];
[roiListReform removeAllObjects];
[roiListReform release];
[curReformROI release];
[reformPixList removeAllObjects];
[reformPixList release];
[reformView setTranlateSlider:nil];
[[NSNotificationCenter defaultCenter] removeObserver: self];
[[self window] setDelegate:nil];
[originalViewController release];
[originalViewVolumeData release];
[originalViewPixList release];
[self autorelease];
}
- (int)reduceTheVolume:(NSArray*)bordersArray:(ViewerController *) vc
{
float *srcImage, *dstImage;
int x,y,z;
long size;
NSArray *pixList = [vc pixList];
curPix = [pixList objectAtIndex: 0];
float vectors[9];
[curPix orientation:vectors];
imageWidth=[curPix pwidth];
int tempint;
int x1=[[bordersArray objectAtIndex:0] intValue];
int x2=[[bordersArray objectAtIndex:1] intValue];
int y1=[[bordersArray objectAtIndex:2] intValue];
int y2=[[bordersArray objectAtIndex:3] intValue];
int z1=[[bordersArray objectAtIndex:4] intValue];
int z2=[[bordersArray objectAtIndex:5] intValue];
if(x1>x2)
{
tempint = x1;
x1=x2;
x2=tempint;
}
if(y1>y2)
{
tempint = y1;
y1=y2;
y2=tempint;
}
if(z1>z2)
{
tempint = z1;
z1=z2;
z2=tempint;
}
if(z1<0)
z1=0;
if(z2>=[pixList count])
z2=[pixList count]-1;
size = sizeof(float) * (x2-x1+1) * (y2-y1+1) * (z2-z1+1);
id waitWindow = [vc startWaitWindow:@"producing new volume"];
outputVolumeData = (float*) malloc(size);
if ( !outputVolumeData)
{
NSRunAlertPanel(NSLocalizedString(MALLOC_ERROR_MESSAGE2, nil),
NSLocalizedString(MALLOC_ERROR_MESSAGE2, nil),
NSLocalizedString(@"OK", nil), nil, nil);
[vc endWaitWindow: waitWindow];
return 1;
}
for( z = z1; z <= z2; z++)
{
curPix = [pixList objectAtIndex: z];
srcImage = [curPix fImage];
dstImage = outputVolumeData + (x2-x1+1) * (y2-y1+1) * (z-z1);
for(y = y1;y <= y2; y++)
for(x = x1; x <= x2; x++)
*( dstImage + (x2-x1+1)*(y-y1) + x-x1) = *( srcImage + imageWidth*y + x);
}
NSMutableArray *newPixList = [NSMutableArray arrayWithCapacity: 0];
NSMutableArray *newDcmList = [NSMutableArray arrayWithCapacity: 0];
NSData *newData = [NSData dataWithBytesNoCopy:outputVolumeData length: size freeWhenDone:YES];
float origin[3];
for( z = z1 ; z <= z2; z ++)
{
curPix = [pixList objectAtIndex: z];
DCMPix *copyPix = [curPix copy];
[newPixList addObject: copyPix];
[copyPix release];
[newDcmList addObject: [[vc fileList] objectAtIndex: z]];
[[newPixList lastObject] setPwidth: x2-x1+1];
[[newPixList lastObject] setPheight: y2-y1+1];
origin[0]=[curPix originX]+x1*[curPix pixelSpacingX]*vectors[0]+y1*[curPix pixelSpacingY]*vectors[3];
origin[1]=[curPix originY]+x1*[curPix pixelSpacingX]*vectors[1]+y1*[curPix pixelSpacingY]*vectors[4];
origin[2]=[curPix originZ]+x1*[curPix pixelSpacingX]*vectors[2]+y1*[curPix pixelSpacingY]*vectors[5];
[copyPix setOrigin:origin];
[[newPixList lastObject] setfImage: (float*) (outputVolumeData + (x2-x1+1)* (y2-y1+1)* (z-z1))];
[[newPixList lastObject] setTot: (z2-z1+1)];
[[newPixList lastObject] setFrameNo: 0];
[[newPixList lastObject] setID: (z-z1)];
}
// Replace A series
[vc replaceSeriesWith:newPixList :newDcmList :newData];
[vc endWaitWindow: waitWindow];
return 0;
}
- (IBAction)endPanel:(id)sender
{
if( [sender tag]&&!isSelectAll) // User clicks OK Button
{
int x1 = [leftTopX intValue];
int x2 = [rightBottomX intValue];
int y1 = [leftTopY intValue];
int y2 = [rightBottomY intValue];
int z1 = imageAmount - [imageFrom intValue];
int z2 = imageAmount - [imageTo intValue];
int tempint;
if(x1>x2)
{
tempint = x1;
x1=x2;
x2=tempint;
}
if(y1>y2)
{
tempint = y1;
y1=y2;
y2=tempint;
}
if(z1>z2)
{
tempint = z1;
z1=z2;
z2=tempint;
}
if(x1<0)
x1=0;
if(x2>=imageWidth)
x2=imageWidth-1;
if(y1<0)
y1=0;
if(y2>=imageHeight)
y2=imageHeight-1;
if(z1<0)
z1=0;
if(z2>=imageAmount)
z2=imageAmount-1;
NSMutableArray* subvolumedimensionarray=[NSMutableArray arrayWithCapacity:0];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:x1]];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:x2]];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:y1]];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:y2]];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:z1]];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:z2]];
curPix=[[originalViewController pixList] objectAtIndex:0];
[subvolumedimensionarray addObject:[NSNumber numberWithFloat:[curPix originX]]];
[subvolumedimensionarray addObject:[NSNumber numberWithFloat:[curPix originY]]];
[subvolumedimensionarray addObject:[NSNumber numberWithFloat:[curPix originZ]]];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:imageWidth]];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:imageHeight]];
[subvolumedimensionarray addObject:[NSNumber numberWithInt:imageAmount]];
float origin[3];
float vectors[9];
[curPix orientation:vectors];
curPix=[[originalViewController pixList] objectAtIndex: z1];
origin[0]=[curPix originX]+x1*[curPix pixelSpacingX]*vectors[0]+y1*[curPix pixelSpacingY]*vectors[3];
origin[1]=[curPix originY]+x1*[curPix pixelSpacingX]*vectors[1]+y1*[curPix pixelSpacingY]*vectors[4];
origin[2]=[curPix originZ]+x1*[curPix pixelSpacingX]*vectors[2]+y1*[curPix pixelSpacingY]*vectors[5];
[subvolumedimensionarray addObject:[NSNumber numberWithFloat:origin[0]]];
[subvolumedimensionarray addObject:[NSNumber numberWithFloat:origin[1]]];
[subvolumedimensionarray addObject:[NSNumber numberWithFloat:origin[2]]];
[parent cleanDataOfWizard];
NSMutableDictionary* dic=[parent dataOfWizard];
[dic setObject:subvolumedimensionarray forKey:@"SubvolumesDimension"];
[parent saveCurrentStep];
if (![self reduceTheVolume:subvolumedimensionarray:originalViewController]&&[sender tag])
{
[originalViewController checkEverythingLoaded];
[[originalViewController window] setTitle:@"VOI"];
}
}
if ([sender tag]==2)
[parent gotoStepNo:2];
else
[parent cleanSharedData];
[[self window] performClose:sender];
}
- (id)showPanelAsWizard:(ViewerController *) vc
:(CMIV_CTA_TOOLS*) owner
{
isInWizardMode=YES;
return [self showChopperPanel: vc
: owner];
}
- (id) showChopperPanel:(ViewerController *) vc
:(CMIV_CTA_TOOLS*) owner
{
NSLog(@"%s %d", __FUNCTION__, __LINE__);
// Initialize the window
self = [super initWithWindowNibName:@"Chopper_Panel"];
[[self window] setDelegate:self];
int err=0;
originalViewController=vc;
parent = owner;
originalViewVolumeData=[vc volumeData];
originalViewPixList=[vc pixList];
[originalViewController retain];
[originalViewVolumeData retain];
[originalViewPixList retain];
isSelectAll=NO;
curPix = [[originalViewController pixList] objectAtIndex: [[originalViewController imageView] curImage]];;
NSMutableArray *pixList = [originalViewController pixList];
NSArray *fileList =[originalViewController fileList ];
if ([curPix isRGB])
{
NSRunAlertPanel(NSLocalizedString(@"no RGB Support", nil),
NSLocalizedString(@"This plugin doesn't surpport RGB images, please convert this series into BW images first", nil),
NSLocalizedString(@"OK", nil), nil, nil);
return 0;
}
imageWidth = [curPix pwidth];
imageHeight = [curPix pheight];
imageAmount = [pixList count];
roiRect.origin.x=imageWidth/8;
roiRect.origin.y=imageHeight/8;
roiRect.size.width=imageWidth*3/4;
roiRect.size.height=imageHeight*3/4;
iImageFrom = 1;
iImageTo = imageAmount;
[imageFromSlider setMaxValue: imageAmount];
[imageToSlider setMaxValue: imageAmount];
[originalViewSlider setMaxValue: imageAmount-1];
[originalViewSlider setIntValue: imageAmount/2];
[reformViewSlider setMaxValue: imageHeight];
[reformViewSlider setIntValue: imageHeight/2];
[reformViewState setSelectedSegment:0];
RGBColor color;
color.red = 0;
color.blue = 0;
color.green =65535;
NSString *roiName = @"ROI";
curAxialROI = [[ROI alloc] initWithType: tROI :[curPix pixelSpacingX] :[curPix pixelSpacingY] : NSMakePoint( [curPix originX], [curPix originY])];
[curAxialROI setName:roiName];
[curAxialROI setROIRect:roiRect];
[curAxialROI setColor:color];
roiListAxial = [[NSMutableArray alloc] initWithCapacity: 0];
for (unsigned int i = 0; i < [pixList count]; i++)
{
[roiListAxial addObject:[NSMutableArray arrayWithCapacity:0]];
[[roiListAxial objectAtIndex:i] addObject:curAxialROI];
}
[originalView setDCM:pixList
:fileList
:roiListAxial
:0
:'i'
:YES];
[originalView setStringID: roiName];
[originalView setIndexWithReset: [pixList count]/2 :YES];
[originalView setOrigin: NSMakePoint(0,0)];
[originalView setCurrentTool:tROI];
#if 0 // @@@ zoom level
[reformView setScaleValue:2.3];
#endif
[originalView scaleToFit];
[curAxialROI setROIMode:ROI_selected];
err = [self initReformView];
[curReformROI setROIMode:ROI_selected];
if (!err)
{
[self updateImageFromToSliders];
[self updateAllTextField];
[reformView setTranlateSlider:reformViewSlider];
[reformView setHorizontalSlider:nil];
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(defaultToolModified:) name:@"defaultToolModified" object:nil];
[nc addObserver:self selector:@selector(roiChanged:) name:@"roiChange" object:nil];
[nc addObserver: self selector: @selector(changeWLWW:) name: @"changeWLWW" object: nil];
// show the window
screenrect = [[[originalViewController window] screen] visibleFrame];
[[self window]setFrame:screenrect display:NO animate:NO];
#if 1 // original
[super showWindow:parent];
#else // try 20231004
dispatch_async(dispatch_get_main_queue(), ^(){
[super showWindow:parent];
});
#endif
[[self window] makeKeyAndOrderFront:parent];
[[self window] display];
if (!isInWizardMode)
{
[nextStep setHidden:YES];
[wizardTips setHidden:YES];
}
}
return self;
}
- (void) updateAllTextField
{
[leftTopX setIntValue: (int)(roiRect.origin.x)];
[leftTopY setIntValue: (int)(roiRect.origin.y)];
[rightBottomX setIntValue: (int)(roiRect.origin.x+roiRect.size.width)];
[rightBottomY setIntValue: (int)(roiRect.origin.y+roiRect.size.height)];
[imageFrom setIntValue:iImageFrom];
[imageTo setIntValue:iImageTo];
isSelectAll=NO;
}
- (void) updateImageFromToSliders
{
[imageFromSlider setIntValue: iImageFrom];
[imageToSlider setIntValue: iImageTo];
}
-(void) defaultToolModified: (NSNotification*) note
{
id sender = [note object];
ToolMode tag;
if ( sender)
{
if ([sender isKindOfClass:[NSMatrix class]])
{
NSButtonCell *theCell = [sender selectedCell];
tag = (ToolMode)[theCell tag];
}
else
{
tag = (ToolMode)[sender tag];
}
}
else
tag = (ToolMode)[[[note userInfo] valueForKey:@"toolIndex"] intValue];
if ( tag >= tWL )
{
if ( tag > tNext)
tag = tROI;
[originalView setCurrentTool: tag];
[reformView setCurrentTool: tag];
}
}
-(void) roiChanged: (NSNotification*) note
{
id sender = [note object];
if( sender)
{
if ([sender isKindOfClass:[ROI class]])
{
if([curAxialROI isEqual:sender]==YES)
{
int lefttopx,lefttopy,rightbottomx,rightbottomy,pointamount;
NSMutableArray *ptsTemp = [curAxialROI points];
pointamount = [ptsTemp count];
lefttopx = rightbottomx = (int)([[ptsTemp objectAtIndex: 0] point].x);
lefttopy = rightbottomy = (int)([[ptsTemp objectAtIndex: 0] point].y);
int k,tempxx,tempyy;
for( k = 1; k < pointamount; k++)
{
tempxx = (int)([[ptsTemp objectAtIndex: k] point].x);
tempyy = (int)([[ptsTemp objectAtIndex: k] point].y);
if(tempxx < lefttopx)
lefttopx = tempxx;
if(tempxx > rightbottomx )
rightbottomx = tempxx;
if( tempyy < lefttopy )
lefttopy = tempyy;
if( tempyy > rightbottomy )
rightbottomy = tempyy;
}
if(lefttopx<0)
lefttopx=0;
if(lefttopy<0)
lefttopy=0;
if(rightbottomx>=imageWidth)
rightbottomx=imageWidth-1;
if(rightbottomy>=imageHeight)
rightbottomy=imageHeight-1;
roiRect.origin.x = lefttopx;
roiRect.origin.y = lefttopy;
roiRect.size.width = rightbottomx - lefttopx;
roiRect.size.height = rightbottomy - lefttopy;
coronalROIRect.origin.x = roiRect.origin.x;
coronalROIRect.size.width = roiRect.size.width ;
sagittalROIRect.origin.x = roiRect.origin.y / ratioXtoY ;
sagittalROIRect.size.width = roiRect.size.height / ratioXtoY;
if([reformViewState selectedSegment])
[curReformROI setROIRect:sagittalROIRect];
else
[curReformROI setROIRect:coronalROIRect];
[reformView setIndex: 0];
[self updateAllTextField];
}
else if ([curReformROI isEqual:sender]==YES)
{
int lefttopx,lefttopy,rightbottomx,rightbottomy,pointamount;
NSMutableArray *ptsTemp = [curReformROI points];
pointamount = [ptsTemp count];
lefttopx = rightbottomx = (int)([[ptsTemp objectAtIndex: 0] point].x);
lefttopy = rightbottomy = (int)([[ptsTemp objectAtIndex: 0] point].y);
int k,tempxx,tempyy;
for( k = 1; k < pointamount; k++)
{
tempxx = (int)([[ptsTemp objectAtIndex: k] point].x);
tempyy = (int)([[ptsTemp objectAtIndex: k] point].y);
if(tempxx < lefttopx)
lefttopx = tempxx;
if(tempxx > rightbottomx )
rightbottomx = tempxx;
if( tempyy < lefttopy )
lefttopy = tempyy;
if( tempyy > rightbottomy )
rightbottomy = tempyy;
}
if([reformViewState selectedSegment])
{
if(lefttopx<0)
lefttopx=0;
if(rightbottomx>=imageHeight)
rightbottomx=imageHeight-1;
sagittalROIRect.origin.x = lefttopx;
sagittalROIRect.origin.y = lefttopy;
sagittalROIRect.size.width = rightbottomx - lefttopx;
sagittalROIRect.size.height = rightbottomy - lefttopy;
iImageFrom = (int)(sagittalROIRect.origin.y * ratioYtoThick+1);
iImageTo = (int)(sagittalROIRect.size.height * ratioYtoThick + sagittalROIRect.origin.y * ratioYtoThick+1);
roiRect.origin.y = sagittalROIRect.origin.x *ratioXtoY;
roiRect.size.height = sagittalROIRect.size.width * ratioXtoY;
coronalROIRect.origin.y = (iImageFrom-1) / ratioYtoThick ;
coronalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick;
}
else
{
if(lefttopx<0)
lefttopx=0;
if(rightbottomx>=imageWidth)
rightbottomx=imageWidth-1;
coronalROIRect.origin.x = lefttopx;
coronalROIRect.origin.y = lefttopy;
coronalROIRect.size.width = rightbottomx - lefttopx;
coronalROIRect.size.height = rightbottomy - lefttopy;
iImageFrom = (int)(coronalROIRect.origin.y * ratioYtoThick+1);
iImageTo = (int)(coronalROIRect.size.height * ratioYtoThick + coronalROIRect.origin.y * ratioYtoThick+1);
roiRect.origin.x = coronalROIRect.origin.x ;
roiRect.size.width = coronalROIRect.size.width ;
sagittalROIRect.origin.y = (iImageFrom-1)/ ratioYtoThick ;
sagittalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick ;
}
if(iImageFrom<1)
iImageFrom = 1 ;
if(iImageTo > imageAmount)
iImageTo = imageAmount;
[curAxialROI setROIRect:roiRect];
[originalView setIndex: [originalView curImage]];
[self updateImageFromToSliders];
[self updateAllTextField];
}
}
}
}
- (int) initReformView
{
NSLog(@"%s %d", __FUNCTION__, __LINE__);
NSArray *pixList = [originalViewController pixList];
long size = sizeof(float) * imageWidth * imageHeight * imageAmount;
outputVolumeData=[originalViewController volumePtr:0];
curPix = [pixList objectAtIndex: 0];
float vectors[9];
[curPix orientation:vectors];
vtkOriginalX = ([curPix originX] ) * vectors[0] + ([curPix originY]) * vectors[1] + ([curPix originZ] )*vectors[2];
vtkOriginalY = ([curPix originX] ) * vectors[3] + ([curPix originY]) * vectors[4] + ([curPix originZ] )*vectors[5];
vtkOriginalZ = ([curPix originX] ) * vectors[6] + ([curPix originY]) * vectors[7] + ([curPix originZ] )*vectors[8];
sliceThickness = [curPix sliceInterval];
if ( sliceThickness == 0)
{
NSLog(@"Slice interval = slice thickness!");
sliceThickness = [curPix sliceThickness];
}
ratioXtoThick = [curPix pixelSpacingX]/sliceThickness;
ratioYtoThick = [curPix pixelSpacingY]/sliceThickness;
ratioXtoY = [curPix pixelSpacingX]/[curPix pixelSpacingY];
reader = vtkImageImport::New();
reader->SetWholeExtent(0, imageWidth-1,
0, imageHeight-1,
0, imageAmount-1);
reader->SetDataSpacing( [curPix pixelSpacingX], [curPix pixelSpacingY], sliceThickness);
reader->SetDataOrigin( vtkOriginalX,vtkOriginalY,vtkOriginalZ );
//reader->SetDataOrigin( [firstObject originX],[firstObject originY],[firstObject originZ]);
reader->SetDataExtentToWholeExtent();
reader->SetImportVoidPointer(outputVolumeData);
reader->SetDataScalarTypeToFloat();
sliceTransform = vtkTransform::New();
sliceTransform->RotateX( -90);
sliceTransform->Translate( 0, 0, vtkOriginalY + [curPix pixelSpacingY]*imageWidth/2 );
// FINAL IMAGE RESLICE
rotate = vtkImageReslice::New();
rotate->SetAutoCropOutput( true);
rotate->SetInformationInput( reader->GetOutput());
rotate->SetInputConnection( reader->GetOutputPort());
rotate->SetOptimization( true);
rotate->SetResliceTransform( sliceTransform);
rotate->SetResliceAxesOrigin( 0, 0, 0);
// rotate->SetTransformInputSampling( false);
rotate->SetInterpolationModeToNearestNeighbor(); //SetInterpolationModeToLinear(); //SetInterpolationModeToCubic(); //SetInterpolationModeToCubic();
rotate->SetOutputDimensionality( 2);
// rotate->SetOutputOrigin( 0,0,0);
rotate->SetBackgroundLevel( -1024);
int imExtent[ 6];
double space[ 3], origin[ 3];
vtkImageData *tempIm = rotate->GetOutput();
tempIm->GetExtent( imExtent);
tempIm->GetSpacing( space);
tempIm->GetOrigin( origin);
float *im = (float*) tempIm->GetScalarPointer();
DCMPix* mypix = [[DCMPix alloc] initwithdata:(float*) im
:32
:imExtent[ 1]-imExtent[ 0]+1
:imExtent[ 3]-imExtent[ 2]+1
:space[0]
:space[1]
:origin[0]
:origin[1]
:origin[2]];
[mypix copySUVfrom: curPix];
reformPixList = [[NSMutableArray alloc] initWithCapacity:0];
[reformPixList addObject: mypix];
reformFileList = [originalViewController fileList ];
// Initialize ROI
coronalROIRect.origin.x = roiRect.origin.x;
coronalROIRect.origin.y = (iImageFrom-1) / ratioYtoThick ;
coronalROIRect.size.width = roiRect.size.width ;
coronalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick;
sagittalROIRect.origin.x = roiRect.origin.y / ratioXtoY ;
sagittalROIRect.origin.y = (iImageFrom-1)/ ratioYtoThick ;
sagittalROIRect.size.width = roiRect.size.height / ratioXtoY;
sagittalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick ;
RGBColor color;
color.red = 0;
color.blue = 0;
color.green =65535;
NSString *roiName = @"reformROI";
curReformROI = [[ROI alloc] initWithType: tROI :[mypix pixelSpacingX] :[mypix pixelSpacingY] : NSMakePoint( [mypix originX], [mypix originY])];
[curReformROI setName:roiName];
[curReformROI setROIRect:coronalROIRect];
[curReformROI setColor:color];
roiListReform = [[NSMutableArray alloc] initWithCapacity: 0];
[roiListReform addObject:[NSMutableArray arrayWithCapacity:0]];
[[roiListReform objectAtIndex:0] addObject:curReformROI];
[reformView setDCM:reformPixList :reformFileList :roiListReform :0 :'i' :YES];
[reformView setStringID:roiName];
[reformView setIndexWithReset: 0 :YES];
[reformView setOrigin: NSMakePoint(0,0)];
[reformView setCurrentTool:tROI];
#if 0 // @@@ FIXME: zoom level
[reformView setScaleValue:2.3];
#endif
[reformView scaleToFit];
float iww = [originalView curWW];
float iwl = [originalView curWL];
[reformView setWLWW:iwl :iww];
[mypix release];
return 0;
}
- (void) updateALLROIs
{
[curAxialROI setROIRect:roiRect];
int i = [originalView curImage];
[originalView setIndex: i];
if ([reformViewState selectedSegment])
[curReformROI setROIRect:sagittalROIRect];
else
[curReformROI setROIRect:coronalROIRect];
[reformView setIndex: 0];
isSelectAll=NO;
}
- (IBAction)setReformViewIndex:(id)sender
{
NSLog(@"%s %d, segment:%ld", __FUNCTION__, __LINE__, (long)[reformViewState selectedSegment]);
int i = [sender intValue];
sliceTransform->Identity();
if ([reformViewState selectedSegment])
{
sliceTransform->RotateY( -90);
sliceTransform->RotateZ( 90);
sliceTransform->Translate( 0, 0 ,-( vtkOriginalX + [curPix pixelSpacingX]*i));
}
else
{
sliceTransform->RotateX( -90);
sliceTransform->Translate( 0, 0 , vtkOriginalY + [curPix pixelSpacingY]*i);
}
rotate->SetResliceAxesOrigin( 0, 0, 0);
int imExtent[ 6];
double space[ 3], origin[ 3];
vtkImageData *tempIm = rotate->GetOutput();
tempIm->GetExtent( imExtent);
tempIm->GetSpacing( space);
tempIm->GetOrigin( origin);
float *im = (float*) tempIm->GetScalarPointer();
DCMPix* mypix = [[DCMPix alloc] initwithdata:(float*) im
:32
:imExtent[ 1]-imExtent[ 0]+1
:imExtent[ 3]-imExtent[ 2]+1
:space[0]
:space[1]
:origin[0]
:origin[1]
:origin[2]];
[mypix copySUVfrom: curPix];
//finalPixList = [[NSMutableArray alloc] initWithCapacity:0];
[reformPixList removeAllObjects];
[reformPixList addObject: mypix];
[mypix release];
[reformView setIndex: 0];
}
- (void) changeWLWW: (NSNotification*) note
{
id sender = [note object] ;
if ([sender isKindOfClass:[DCMPix class]])
{
DCMPix *otherPix = sender;
float iww = [otherPix ww];
float iwl = [otherPix wl];
if ( [reformPixList containsObject: otherPix])
{
if ( iww != [originalView curWW] || iwl != [originalView curWL])
[originalView setWLWW:iwl :iww];
}
else
{
if ( iww != [reformView curWW] || iwl != [reformView curWL])
[reformView setWLWW:iwl :iww];
}
}
}
- (IBAction)selectAll:(id)sender
{
roiRect.origin.x = 0;
roiRect.origin.y = 0;
roiRect.size.width = imageWidth;
roiRect.size.height = imageHeight;
coronalROIRect.origin.x = roiRect.origin.x;
coronalROIRect.size.width = roiRect.size.width ;
sagittalROIRect.origin.x = roiRect.origin.y / ratioXtoY ;
sagittalROIRect.size.width = roiRect.size.height / ratioXtoY;
iImageFrom = 1;
iImageTo = imageAmount;
coronalROIRect.origin.y = (iImageFrom-1) / ratioYtoThick ;
coronalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick;
sagittalROIRect.origin.y = (iImageFrom-1)/ ratioYtoThick ;
sagittalROIRect.size.height = (iImageTo-iImageFrom-1)/ ratioYtoThick ;
[curAxialROI setROIRect:roiRect];
[originalView setIndex: [originalView curImage ]];
if([reformViewState selectedSegment])
[curReformROI setROIRect:sagittalROIRect];
else
[curReformROI setROIRect:coronalROIRect];
[reformView setIndex: 0];
[self updateImageFromToSliders];
[self updateAllTextField];
isSelectAll=YES;
}
@end