-
Notifications
You must be signed in to change notification settings - Fork 217
/
CPRMPRDCMView.m
2778 lines (2232 loc) · 85.8 KB
/
CPRMPRDCMView.m
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
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "CPRController.h"
#import "CPRMPRDCMView.h"
#import "VRController.h"
#import "VRView.h"
#import "DCMCursor.h"
#import "ROI.h"
#import "Notifications.h"
#import "CPRController.h"
#import "CPRCurvedPath.h"
#import "CPRDisplayInfo.h"
#import "N3BezierPath.h"
#import "OSIEnvironment.h"
#import "OSIROI.h"
#import "OSIVolumeWindow.h"
#include <OpenGL/CGLMacro.h>
static float deg2rad = M_PI / 180.0;
extern unsigned int minimumStep;
#define CROSS(dest,v1,v2) \
dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
static BOOL arePlanesParallel( float *Pn1, float *Pn2)
{
float u[ 3];
CROSS(u, Pn1, Pn2);
float ax = (u[0] >= 0 ? u[0] : -u[0]);
float ay = (u[1] >= 0 ? u[1] : -u[1]);
float az = (u[2] >= 0 ? u[2] : -u[2]);
if ((ax+ay+az) < 0.001)
return YES;
return NO;
}
#define VIEW_COLOR_LABEL_SIZE 25
int splitPosition[3];
BOOL frameZoomed = NO;
static CGFloat CPRMPRDCMViewCurveMouseTrackingDistance = 20.0;
@interface CPRMPRDCMView ()
- (void)drawCurvedPathInGL;
- (void)drawOSIROIs;
- (OSIROIManager *)ROIManager;
- (void)drawCircleAtPoint:(NSPoint)point pointSize:(CGFloat)pointSize;
- (void)drawCircleAtPoint:(NSPoint)point;
- (void)sendWillEditCurvedPath;
- (void)sendDidUpdateCurvedPath;
- (void)sendDidEditCurvedPath;
- (void)sendDidEditAssistedCurvedPath;
- (void)sendWillEditDisplayInfo;
- (void)sendDidEditDisplayInfo;
@end
@implementation CPRMPRDCMView
@synthesize delegate;
@synthesize curvedPath;
@synthesize displayInfo;
@synthesize dontUseAutoLOD, pix, camera, angleMPR, vrView, viewExport, toIntervalExport, fromIntervalExport, rotateLines, moveCenter, displayCrossLines, LOD;
@synthesize CPRType = _CPRType;
- (BOOL)becomeFirstResponder
{
BOOL v = [super becomeFirstResponder];
[windowController updateToolbarItems];
return v;
}
- (void) setDisplayCrossLines: (BOOL) b
{
displayCrossLines = b;
[windowController updateToolbarItems];
}
- (BOOL)is2DTool:(short)tool;
{
switch( tool)
{
case tWL:
if( vrView.renderingMode == 1 || vrView.renderingMode == 3 || vrView.renderingMode == 2) return YES; // MIP
else return NO; // VR
break;
case tNext:
case tMesure:
case tROI:
case tOval:
case tOPolygon:
case tCPolygon:
case tAngle:
case tArrow:
case tText:
case tPencil:
case tPlain:
case t2DPoint:
case tRepulsor:
case tLayerROI:
case tROISelector:
case tCurvedROI:
return YES;
break;
}
return NO;
}
- (void) setDCMPixList:(NSMutableArray*)pixList filesList:(NSArray*)files roiList:(NSMutableArray*)rois firstImage:(short)firstImage type:(char)type reset:(BOOL)reset;
{
[super setPixels:pixList files:files rois:rois firstImage:firstImage level:type reset:reset];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(removeROI:)
name: OsirixRemoveROINotification
object: nil];
rotation = 0;
pix = [pixList lastObject];
currentTool = t3DRotate;
frameZoomed = NO;
displayCrossLines = YES;
windowController = [self windowController];
[windowController updateToolbarItems];
draggedToken = CPRCurvedPathControlTokenNone;
}
- (void) setVRView: (VRView*) v viewID:(int) i
{
viewID = i;
vrView = v;
[vrView prepareFullDepthCapture];
}
- (void) saveCamera
{
[camera release];
camera = [[vrView cameraWithThumbnail: NO] retain];
}
- (void) drawRect:(NSRect)rect
{
if( rect.size.width > 10)
{
[super drawRect: rect];
}
}
- (void) setFrame:(NSRect)frameRect
{
NSDisableScreenUpdates();
if( NSEqualRects( frameRect, [self frame]) == NO)
{
[NSObject cancelPreviousPerformRequestsWithTarget: windowController selector:@selector(updateViewsAccordingToFrame:) object: nil];
[windowController performSelector: @selector(updateViewsAccordingToFrame:) withObject: nil afterDelay: 0.1];
}
if( blendingView)
{
[blendingView setFrame: frameRect];
blendingView.drawingFrameRect = [self convertRectToBacking: frameRect]; // very important to have correct position values with PET-CT
}
[super setFrame: frameRect];
NSEnableScreenUpdates();
}
- (void)setCurvedPath:(CPRCurvedPath *)newCurvedPath
{
if (curvedPath != newCurvedPath) {
[curvedPath release];
curvedPath = [newCurvedPath copy];
[self setNeedsDisplay:YES];
}
}
- (void)setDisplayInfo:(CPRDisplayInfo *)newDisplayInfo
{
if (displayInfo != newDisplayInfo) {
[displayInfo release];
displayInfo = [newDisplayInfo copy];
[self setNeedsDisplay:YES];
}
}
-(void)setCPRType:(CPRMPRDCMViewCPRType)type
{
if (type != _CPRType) {
_CPRType = type;
[self setNeedsDisplay:YES];
}
}
- (void) checkForFrame
{
NSRect frame = [self convertRectToBacking: [self frame]];
NSPoint o = [self convertPoint: NSMakePoint(0, 0) toView:0L];
frame.origin = o;
if( NSEqualRects( frame, [vrView frame]) == NO)
{
[vrView setFrame: frame];
}
}
//
//- (BOOL) hasCameraMoved: (Camera*) currentCamera
//{
// if( fabs( currentCamera.position.x - camera.position.x) > 0.1) return YES;
// if( fabs( currentCamera.position.y - camera.position.y) > 0.1) return YES;
// if( fabs( currentCamera.position.z - camera.position.z) > 0.1) return YES;
//
// if( fabs( currentCamera.focalPoint.x - camera.focalPoint.x) > 0.1) return YES;
// if( fabs( currentCamera.focalPoint.y - camera.focalPoint.y) > 0.1) return YES;
// if( fabs( currentCamera.focalPoint.z - camera.focalPoint.z) > 0.1) return YES;
//
// if( fabs( currentCamera.viewUp.x - camera.viewUp.x) > 0.1) return YES;
// if( fabs( currentCamera.viewUp.y - camera.viewUp.y) > 0.1) return YES;
// if( fabs( currentCamera.viewUp.z - camera.viewUp.z) > 0.1) return YES;
//
// if( fabs( currentCamera.viewAngle - camera.viewAngle) > 3) return YES;
// if( fabs( currentCamera.eyeAngle - camera.eyeAngle) > 3) return YES;
//
// return NO;
//
//}
- (BOOL) hasCameraChanged: (Camera*) currentCamera
{
if( camera.forceUpdate)
{
camera.forceUpdate = NO;
return YES;
}
#define PRECISION 0.0001
if( fabs( currentCamera.position.x - camera.position.x) > PRECISION) return YES;
if( fabs( currentCamera.position.y - camera.position.y) > PRECISION) return YES;
if( fabs( currentCamera.position.z - camera.position.z) > PRECISION) return YES;
if( fabs( currentCamera.focalPoint.x - camera.focalPoint.x) > PRECISION) return YES;
if( fabs( currentCamera.focalPoint.y - camera.focalPoint.y) > PRECISION) return YES;
if( fabs( currentCamera.focalPoint.z - camera.focalPoint.z) > PRECISION) return YES;
if( fabs( currentCamera.viewUp.x - camera.viewUp.x) > PRECISION) return YES;
if( fabs( currentCamera.viewUp.y - camera.viewUp.y) > PRECISION) return YES;
if( fabs( currentCamera.viewUp.z - camera.viewUp.z) > PRECISION) return YES;
if( fabs( currentCamera.viewAngle - camera.viewAngle) > PRECISION) return YES;
if( fabs( currentCamera.eyeAngle - camera.eyeAngle) > PRECISION) return YES;
if( fabs( currentCamera.parallelScale - camera.parallelScale) > PRECISION) return YES;
if( currentCamera.clippingRangeNear != camera.clippingRangeNear) return YES;
if( currentCamera.clippingRangeFar != camera.clippingRangeFar) return YES;
// if( currentCamera.LOD < camera.LOD) return YES;
if( currentCamera.wl != camera.wl) return YES;
if( currentCamera.ww != camera.ww) return YES;
return NO;
}
- (void) restoreCamera
{
return [self restoreCameraAndCheckForFrame: YES];
}
- (void) restoreCameraAndCheckForFrame: (BOOL) v
{
if( v)
[self checkForFrame];
[vrView setCamera: camera];
}
- (void) dealloc
{
// [vrView restoreFullDepthCapture];
[curvedPath release];
[displayInfo release];
[camera release];
[super dealloc];
}
-(void) updateViewMPR
{
[self updateViewMPR: YES];
}
- (void) setLOD: (float) l
{
LOD = l;
}
- (void) reshape
{
// To display or hide the resulting plane on the CPR view
[self willChangeValueForKey:@"plane"];
[self didChangeValueForKey:@"plane"];
[super reshape];
}
- (void) updateViewMPR:(BOOL) computeCrossReferenceLines
{
if( [self frame].size.width <= 0)
return;
if( [self frame].size.height <= 0)
return;
long h, w;
float previousWW, previousWL;
BOOL isRGB;
BOOL previousOriginInPlane = NO;
[self getWLWW: &previousWL :&previousWW];
Camera *currentCamera = [vrView cameraWithThumbnail: NO];
minimumStep = 1;
if( [self hasCameraChanged: currentCamera] == YES)
{
// AutoLOD
if( dontUseAutoLOD == NO && lastRenderingWasMoveCenter == NO)
{
DCMPix *o = [windowController originalPix];
float minimumResolution = [o pixelSpacingX];
if( minimumResolution > [o pixelSpacingY])
minimumResolution = [o pixelSpacingY];
if( minimumResolution > [o sliceInterval])
minimumResolution = [o sliceInterval];
if( windowController.clippingRangeThickness <= 3)
minimumResolution *= 0.9;
else
minimumResolution *= 0.7;
if( minimumResolution > previousPixelSpacing && previousPixelSpacing != 0)
LOD *= ( minimumResolution / previousPixelSpacing);
if( previousResolution == 0)
previousResolution = [vrView getResolution];
float currentResolution = [vrView getResolution];
if( previousResolution < currentResolution)
LOD *= (previousResolution / currentResolution);
if( LOD < windowController.LOD)
LOD = windowController.LOD;
if( LOD > 4) LOD = 4;
if( windowController.lowLOD)
[vrView setLOD: LOD * vrView.lowResLODFactor];
else
[vrView setLOD: LOD];
}
else
[vrView setLOD: LOD];
if( [self frame].size.width > 0 && [self frame].size.height > 0)
{
if( windowController.maxMovieIndex > 1 && (windowController.clippingRangeMode == 1 || windowController.clippingRangeMode == 3 || windowController.clippingRangeMode == 2)) //To avoid the wrong pixel value bug...
[vrView prepareFullDepthCapture];
if( moveCenter)
{
lastRenderingWasMoveCenter = YES;
[vrView setLOD: 100]; // We dont need to really compute the image - we just want image origin for the other views.
}
else lastRenderingWasMoveCenter = NO;
[vrView render];
}
float *imagePtr = nil;
if( moveCenter)
{
imagePtr = [pix fImage];
w = [pix pwidth];
h = [pix pheight];
isRGB = [pix isRGB];
[vrView setLOD: LOD];
}
else
imagePtr = [vrView imageInFullDepthWidth: &w height: &h isRGB: &isRGB];
////
float orientation[ 9];
[vrView getOrientation: orientation];
float location[ 3] = {previousOrigin[ 0], previousOrigin[ 1], previousOrigin[ 2]}, orig[ 3] = {currentCamera.position.x, currentCamera.position.y, currentCamera.position.z}, locationTemp[ 3];
float distance = [DCMView pbase_Plane: location :orig :&(orientation[ 6]) :locationTemp];
if( distance < pix.sliceThickness / 2.)
previousOriginInPlane = YES;
else
previousOriginInPlane = NO;
[self saveCamera];
if( imagePtr)
{
BOOL cameraMoved = YES;
if( [curRoiList count] > 0)
{
if( previousOriginInPlane == NO || arePlanesParallel( orientation+6, previousOrientation+6) == NO)
cameraMoved = YES;
else
cameraMoved = NO;
if( cameraMoved == YES)
{
for( int i = (long)[curRoiList count] -1 ; i >= 0; i--)
{
ROI *r = [curRoiList objectAtIndex: i];
if( [r type] != t2DPoint)
[curRoiList removeObjectAtIndex: i];
}
}
}
if( [pix pwidth] == w && [pix pheight] == h && isRGB == [pix isRGB])
{
if( imagePtr != [pix fImage])
{
memcpy( [pix fImage], imagePtr, w*h*sizeof( float));
free( imagePtr);
}
}
else
{
[pix setRGB: isRGB];
[pix setfImage: imagePtr];
[pix freefImageWhenDone: YES];
[pix setPwidth: w];
[pix setPheight: h];
NSMutableArray *savedROIs = [[curRoiList copy] autorelease];
[self setIndex: 0];
[curRoiList addObjectsFromArray: savedROIs];
}
float porigin[ 3];
[vrView getOrigin: porigin windowCentered: YES sliceMiddle: YES];
[pix setOrigin: porigin];
float resolution = 0;
if( !moveCenter)
{
resolution = [vrView getResolution] * [vrView imageSampleDistance];
[pix setPixelSpacingX: resolution];
[pix setPixelSpacingY: resolution];
}
[self willChangeValueForKey:@"plane"];
[pix setOrientation: orientation];
[self didChangeValueForKey:@"plane"];
[pix setSliceThickness: [vrView getClippingRangeThicknessInMm]];
[self setWLWW: previousWL :previousWW];
if( !moveCenter)
{
[self setScaleValue: [vrView imageSampleDistance]];
float rotationPlane = 0;
if( cameraMoved == NO && [curRoiList count] > 0)
{
if( previousOrientation[ 0] != 0 || previousOrientation[ 1] != 0 || previousOrientation[ 2] != 0)
rotationPlane = -[CPRController angleBetweenVector: orientation andPlane: previousOrientation];
if( fabs( rotationPlane) < 0.01)
rotationPlane = 0;
}
NSPoint rotationCenter = NSMakePoint( [pix pwidth]/2., [pix pheight]/2.);
for( ROI* r in curRoiList)
{
if( rotationPlane)
{
[r setOriginAndSpacing: resolution : resolution : r.imageOrigin :NO];
[r rotate: rotationPlane :rotationCenter];
r.imageOrigin = [DCMPix originCorrectedAccordingToOrientation: pix];
r.pixelSpacingX = [pix pixelSpacingX];
r.pixelSpacingY = [pix pixelSpacingY];
}
else
[r setOriginAndSpacing: resolution : resolution :[DCMPix originCorrectedAccordingToOrientation: pix] :NO];
}
[pix orientation: previousOrientation];
previousOrigin[ 0] = currentCamera.position.x;
previousOrigin[ 1] = currentCamera.position.y;
previousOrigin[ 2] = currentCamera.position.z;
[self detect2DPointInThisSlice];
previousResolution = [vrView getResolution];
previousPixelSpacing = [pix pixelSpacingX];
}
}
if( blendingView)
{
[blendingView getWLWW: &previousWL :&previousWW];
[vrView renderBlendedVolume];
float *blendedImagePtr = nil;
DCMPix *bPix = [blendingView curDCM];
if( moveCenter)
{
blendedImagePtr = [bPix fImage];
w = [bPix pwidth];
h = [bPix pheight];
isRGB = [bPix isRGB];
}
else
blendedImagePtr = [vrView imageInFullDepthWidth: &w height: &h isRGB: &isRGB blendingView: YES];
if( [bPix pwidth] == w && [bPix pheight] == h && isRGB == [bPix isRGB])
{
if( blendedImagePtr != [bPix fImage])
{
memcpy( [bPix fImage], blendedImagePtr, w*h*sizeof( float));
free( blendedImagePtr);
}
}
else
{
[bPix setRGB: isRGB];
[bPix setfImage: blendedImagePtr];
[bPix setPwidth: w];
[bPix setPheight: h];
[blendingView setIndex: 0];
}
float porigin[ 3];
[vrView getOrigin: porigin windowCentered: YES sliceMiddle: YES blendedView: YES];
[bPix setOrigin: porigin];
if( !moveCenter)
{
float resolution = [vrView getResolution] * [vrView blendingImageSampleDistance];
[bPix setPixelSpacingX: resolution];
[bPix setPixelSpacingY: resolution];
}
float orientation[ 9];
[vrView getOrientation: orientation];
[bPix setOrientation: orientation];
[bPix setSliceThickness: [vrView getClippingRangeThicknessInMm]];
[blendingView setWLWW: previousWL :previousWW];
if( !moveCenter)
[blendingView setScaleValue: [vrView blendingImageSampleDistance]];
}
}
if( dontReenterCrossReferenceLines == NO)
{
dontReenterCrossReferenceLines = YES;
if( computeCrossReferenceLines)
[windowController computeCrossReferenceLines: self];
else
[windowController computeCrossReferenceLines: nil];
dontReenterCrossReferenceLines = NO;
}
[self setNeedsDisplay: YES];
}
- (void) colorForView:(int) v
{
CGLContextObj cgl_ctx = [[NSOpenGLContext currentContext] CGLContextObj];
if( cgl_ctx == nil)
return;
switch( v)
{
case 1:
//glColor4f (VIEW_1_RED, VIEW_1_GREEN, VIEW_1_BLUE, VIEW_1_ALPHA);
glColor4f ([windowController.colorAxis1 redComponent], [windowController.colorAxis1 greenComponent], [windowController.colorAxis1 blueComponent], [windowController.colorAxis1 alphaComponent]);
break;
case 2:
//glColor4f (VIEW_2_RED, VIEW_2_GREEN, VIEW_2_BLUE, VIEW_2_ALPHA);
glColor4f ([windowController.colorAxis2 redComponent], [windowController.colorAxis2 greenComponent], [windowController.colorAxis2 blueComponent], [windowController.colorAxis2 alphaComponent]);
break;
case 3:
//glColor4f (VIEW_3_RED, VIEW_3_GREEN, VIEW_3_BLUE, VIEW_3_ALPHA);
glColor4f ([windowController.colorAxis3 redComponent], [windowController.colorAxis3 greenComponent], [windowController.colorAxis3 blueComponent], [windowController.colorAxis3 alphaComponent]);
break;
}
}
- (void) drawLine: (float[2][3]) sft thickness: (float) thickness
{
CGLContextObj cgl_ctx = [[NSOpenGLContext currentContext] CGLContextObj];
if( cgl_ctx == nil)
return;
if( thickness > 2)
{
glLineWidth(2.0 * self.window.backingScaleFactor);
[self drawCrossLines: sft ctx: cgl_ctx withShift: 0];
glLineWidth(1.0 * self.window.backingScaleFactor);
[self drawCrossLines: sft ctx: cgl_ctx withShift: -thickness/2.];
[self drawCrossLines: sft ctx: cgl_ctx withShift: thickness/2.];
}
else
{
glLineWidth(2.0 * self.window.backingScaleFactor);
[self drawCrossLines: sft ctx: cgl_ctx withShift: 0];
}
}
- (void) drawExportLines: (float[2][3]) sft
{
// CGLContextObj cgl_ctx = [[NSOpenGLContext currentContext] CGLContextObj];
//
// glLineWidth(1.0 * self.window.backingScaleFactor);
//
// if( fromIntervalExport > 0)
// {
// for( int i = 1; i <= fromIntervalExport; i++)
// [self drawCrossLines: sft ctx: cgl_ctx withShift: -i * [windowController dcmInterval]];
// }
//
// if( !windowController.dcmBatchReverse)
// [self drawCrossLines: sft ctx: cgl_ctx withShift: -fromIntervalExport * [windowController dcmInterval] showPoint: YES];
//
// if( toIntervalExport > 0)
// {
// for( int i = 1; i <= toIntervalExport; i++)
// [self drawCrossLines: sft ctx: cgl_ctx withShift: i * [windowController dcmInterval]];
// }
//
// if( windowController.dcmBatchReverse)
// [self drawCrossLines: sft ctx: cgl_ctx withShift: toIntervalExport * [windowController dcmInterval] showPoint: YES];
}
- (void) drawRotationLines: (float[2][3]) sft
{
// CGLContextObj cgl_ctx = [[NSOpenGLContext currentContext] CGLContextObj];
//
// for( int i = 1; i < windowController.dcmNumberOfFrames; i++)
// {
// glRotatef( (float) (i * windowController.dcmRotation) / (float) windowController.dcmNumberOfFrames, 0, 0, 1);
// [self drawCrossLines: sft ctx: cgl_ctx perpendicular: NO withShift: 0 half: YES];
// glRotatef( -(float) (i * windowController.dcmRotation) / (float) windowController.dcmNumberOfFrames, 0, 0, 1);
// }
}
- (void) drawTextualData:(NSRect) size :(long) annotations
{
float copyScale = scaleValue;
scaleValue = 1;
[super drawTextualData: size :annotations];
scaleValue = copyScale;
}
- (void) subDrawRect: (NSRect) r
{
if( [stringID isEqualToString: @"export"])
return;
if( r.size.height < 10 || r.size.width < 10)
return;
rotation = 0;
CGLContextObj cgl_ctx = [[NSOpenGLContext currentContext] CGLContextObj];
if( cgl_ctx == nil)
return;
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable(GL_BLEND);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glPointSize( 12 * self.window.backingScaleFactor);
if( displayCrossLines && frameZoomed == NO)
{
// All pix have the same thickness
float thickness = [pix sliceThickness];
switch( viewID)
{
case 1:
glColor4f ([windowController.colorAxis2 redComponent], [windowController.colorAxis2 greenComponent], [windowController.colorAxis2 blueComponent], [windowController.colorAxis2 alphaComponent]);
if( crossLinesA[ 0][ 0] != HUGE_VALF)
{
[self drawLine: crossLinesA thickness: thickness];
}
glColor4f ([windowController.colorAxis3 redComponent], [windowController.colorAxis3 greenComponent], [windowController.colorAxis3 blueComponent], [windowController.colorAxis3 alphaComponent]);
if( crossLinesB[ 0][ 0] != HUGE_VALF)
{
[self drawLine: crossLinesB thickness: thickness];
}
break;
case 2:
glColor4f ([windowController.colorAxis1 redComponent], [windowController.colorAxis1 greenComponent], [windowController.colorAxis1 blueComponent], [windowController.colorAxis1 alphaComponent]);
if( crossLinesA[ 0][ 0] != HUGE_VALF)
{
[self drawLine: crossLinesA thickness: thickness];
}
glColor4f ([windowController.colorAxis3 redComponent], [windowController.colorAxis3 greenComponent], [windowController.colorAxis3 blueComponent], [windowController.colorAxis3 alphaComponent]);
if( crossLinesB[ 0][ 0] != HUGE_VALF)
{
[self drawLine: crossLinesB thickness: thickness];
}
break;
case 3:
glColor4f ([windowController.colorAxis1 redComponent], [windowController.colorAxis1 greenComponent], [windowController.colorAxis1 blueComponent], [windowController.colorAxis1 alphaComponent]);
if( crossLinesA[ 0][ 0] != HUGE_VALF)
{
[self drawLine: crossLinesA thickness: thickness];
}
glColor4f ([windowController.colorAxis2 redComponent], [windowController.colorAxis2 greenComponent], [windowController.colorAxis2 blueComponent], [windowController.colorAxis2 alphaComponent]);
if( crossLinesB[ 0][ 0] != HUGE_VALF)
{
[self drawLine: crossLinesB thickness: thickness];
}
break;
}
}
float heighthalf = [self convertSizeToBacking: self.frame.size].height/2;
float widthhalf = [self convertSizeToBacking: self.frame.size].width/2;
[self colorForView: viewID];
// Red Square
if( [[self window] firstResponder] == self && stringID == nil && frameZoomed == NO)
{
glLineWidth(8.0 * self.window.backingScaleFactor);
glBegin(GL_LINE_LOOP);
glVertex2f( -widthhalf, -heighthalf);
glVertex2f( -widthhalf, heighthalf);
glVertex2f( widthhalf, heighthalf);
glVertex2f( widthhalf, -heighthalf);
glEnd();
}
glLineWidth(2.0 * self.window.backingScaleFactor);
glBegin(GL_POLYGON);
glVertex2f(widthhalf-VIEW_COLOR_LABEL_SIZE, -heighthalf+VIEW_COLOR_LABEL_SIZE);
glVertex2f(widthhalf-VIEW_COLOR_LABEL_SIZE, -heighthalf);
glVertex2f(widthhalf, -heighthalf);
glVertex2f(widthhalf, -heighthalf+VIEW_COLOR_LABEL_SIZE);
glEnd();
glLineWidth(1.0 * self.window.backingScaleFactor);
if( displayCrossLines && frameZoomed == NO && windowController.displayMousePosition && !windowController.mprView1.rotateLines && !windowController.mprView2.rotateLines && !windowController.mprView3.rotateLines
&& !windowController.mprView1.moveCenter && !windowController.mprView2.moveCenter && !windowController.mprView3.moveCenter)
{
// Mouse Position
if( viewID == windowController.mouseViewID)
{
DCMPix *pixA, *pixB;
int viewIDA, viewIDB;
switch (viewID)
{
default:
case 1:
pixA = [windowController.mprView2 pix];
pixB = [windowController.mprView3 pix];
viewIDA = 2;
viewIDB = 3;
break;
case 2:
pixA = [windowController.mprView1 pix];
pixB = [windowController.mprView3 pix];
viewIDA = 1;
viewIDB = 3;
break;
case 3:
pixA = [windowController.mprView1 pix];
pixB = [windowController.mprView2 pix];
viewIDA = 1;
viewIDB = 2;
break;
}
[self colorForView:viewIDA];
Point3D *pt = windowController.mousePosition;
float sc[ 3], dc[ 3] = { pt.x, pt.y, pt.z}, location[ 3];
[pixA convertDICOMCoords: dc toSliceCoords: sc pixelCenter: YES];
sc[0] = sc[ 0] / pixA.pixelSpacingX;
sc[1] = sc[ 1] / pixA.pixelSpacingY;
[pixA convertPixX:sc[0] pixY:sc[1] toDICOMCoords:location pixelCenter:YES];
[pix convertDICOMCoords:location toSliceCoords:sc pixelCenter:YES];
glPointSize( 10 * self.window.backingScaleFactor);
glBegin( GL_POINTS);
sc[0] = sc[ 0] / curDCM.pixelSpacingX;
sc[1] = sc[ 1] / curDCM.pixelSpacingY;
sc[0] -= curDCM.pwidth * 0.5f;
sc[1] -= curDCM.pheight * 0.5f;
glVertex2f( scaleValue*sc[ 0], scaleValue*sc[ 1]);
glEnd();
[self colorForView:viewIDB];
pt = windowController.mousePosition;
dc[0] = pt.x; dc[1] = pt.y; dc[2] = pt.z;
[pixB convertDICOMCoords: dc toSliceCoords: sc pixelCenter: YES];
sc[0] = sc[ 0] / pixB.pixelSpacingX;
sc[1] = sc[ 1] / pixB.pixelSpacingY;
[pixB convertPixX:sc[0] pixY:sc[1] toDICOMCoords:location pixelCenter:YES];
[pix convertDICOMCoords:location toSliceCoords:sc pixelCenter:YES];
glPointSize( 10 * self.window.backingScaleFactor);
glBegin( GL_POINTS);
sc[0] = sc[ 0] / curDCM.pixelSpacingX;
sc[1] = sc[ 1] / curDCM.pixelSpacingY;
sc[0] -= curDCM.pwidth * 0.5f;
sc[1] -= curDCM.pheight * 0.5f;
glVertex2f( scaleValue*sc[ 0], scaleValue*sc[ 1]);
glEnd();
}
if( viewID != windowController.mouseViewID)
{
[self colorForView: viewID];
// [self colorForView: windowController.mouseViewID];
Point3D *pt = windowController.mousePosition;
float sc[ 3], dc[ 3] = { pt.x, pt.y, pt.z};
[pix convertDICOMCoords: dc toSliceCoords: sc pixelCenter: YES];
glPointSize( 10 * self.window.backingScaleFactor);
glBegin( GL_POINTS);
sc[0] = sc[ 0] / curDCM.pixelSpacingX;
sc[1] = sc[ 1] / curDCM.pixelSpacingY;
sc[0] -= curDCM.pwidth * 0.5f;
sc[1] -= curDCM.pheight * 0.5f;
glVertex2f( scaleValue*sc[ 0], scaleValue*sc[ 1]);
glEnd();
}
}
[self drawCurvedPathInGL];
[self drawOSIROIs];
if( windowController.displayMousePosition)
{
NSString *planeName;
for (planeName in [displayInfo planesWithMouseVectors]) {
if ([planeName isEqualToString:[self planeName]]) {
N3Vector cursorVector;
N3AffineTransform transform;
[self colorForView:viewID];
glEnable(GL_POINT_SMOOTH);
glPointSize(8 * self.window.backingScaleFactor);
transform = N3AffineTransformConcat(N3AffineTransformInvert([self pixToDicomTransform]), [self pixToSubDrawRectTransform]);
cursorVector = N3VectorApplyTransform([displayInfo mouseVectorForPlane:planeName], transform);
glBegin(GL_POINTS);
glVertex2f(cursorVector.x, cursorVector.y);
glEnd();
}
}
}
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POLYGON_SMOOTH);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_BLEND);
}
- (void) setCrossReferenceLines: (float[2][3]) a and: (float[2][3]) b
{
crossLinesA[ 0][ 0] = a[ 0][ 0];
crossLinesA[ 0][ 1] = a[ 0][ 1];
crossLinesA[ 0][ 2] = a[ 0][ 2];
crossLinesA[ 1][ 0] = a[ 1][ 0];
crossLinesA[ 1][ 1] = a[ 1][ 1];
crossLinesA[ 1][ 2] = a[ 1][ 2];
crossLinesB[ 0][ 0] = b[ 0][ 0];
crossLinesB[ 0][ 1] = b[ 0][ 1];
crossLinesB[ 0][ 2] = b[ 0][ 2];
crossLinesB[ 1][ 0] = b[ 1][ 0];
crossLinesB[ 1][ 1] = b[ 1][ 1];
crossLinesB[ 1][ 2] = b[ 1][ 2];
}
-(void) setCurrentTool:(short) i
{
if( i != tRepulsor)
[super setCurrentTool: i];
}
- (void) stopCurvedPathCreationMode
{
windowController.curvedPathCreationMode = NO;
draggedToken = CPRCurvedPathControlTokenNone;
if( curvedPath.nodes.count <= 2)
{
// Delete this curve
[self sendWillEditCurvedPath];
[curvedPath clearPath];
[self sendDidUpdateCurvedPath];
[self sendDidEditCurvedPath];
[self setNeedsDisplay:YES];
}
}
- (void) deleteCurrentCurvedPath
{
if (curvedPath.nodes.count > 0) {
if( NSRunInformationalAlertPanel( NSLocalizedString(@"Delete the Curve", nil),
NSLocalizedString(@"Are you sure you want to delete the entire curve?", nil),
NSLocalizedString(@"OK",nil),
NSLocalizedString(@"Cancel",nil),
nil) == NSAlertDefaultReturn)
{
[self sendWillEditCurvedPath];
[curvedPath clearPath];
[self sendDidUpdateCurvedPath];
[self sendDidEditCurvedPath];
[self setNeedsDisplay:YES];
}
}
}
- (void)keyDown:(NSEvent *)theEvent
{
if( [[theEvent characters] length] == 0) return;
unichar c = [[theEvent characters] characterAtIndex:0];
long tool = [self getTool: theEvent];
if(( c == NSCarriageReturnCharacter || c == NSEnterCharacter || c == NSNewlineCharacter) && tool == tCurvedROI)
{
if( windowController.curvedPathCreationMode)
[self stopCurvedPathCreationMode];
}
else if( c == ' ' || c == 27) // 27 : escape
{
if( c == 27 && tool == tCurvedROI)