-
Notifications
You must be signed in to change notification settings - Fork 217
/
CPRStretchedView.m
2509 lines (2074 loc) · 93.2 KB
/
CPRStretchedView.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
//
// CPRStretchedView.m
// OsiriX
//
// Created by Joël Spaltenstein on 6/4/11.
// Copyright 2011 OsiriX Team. All rights reserved.
//
#import "CPRStretchedView.h"
#import "CPRGeneratorRequest.h"
#import "CPRVolumeData.h"
#import "DCMPix.h"
#import "CPRCurvedPath.h"
#import "CPRDisplayInfo.h"
#import "N3BezierPath.h"
#import "CPRMPRDCMView.h"
#import "N3Geometry.h"
#import "N3BezierCoreAdditions.h"
#import "CPRController.h"
#import "ROI.h"
#import "Notifications.h"
#import "StringTexture.h"
#import "NSColor+N2.h"
#import <objc/runtime.h>
static float deg2rad = M_PI / 180.0f;
#define _extraWidthFactor 1.2
extern BOOL frameZoomed;
extern int splitPosition[ 3];
@interface _CPRStretchedViewPlaneRun : NSObject
{
NSRange _range;
NSMutableArray *_distances;
}
@property (nonatomic, readwrite, assign) NSRange range;
@property (nonatomic, readwrite, retain) NSMutableArray *distances;
@end
@interface N3BezierPath (CPRStretchedViewPlaneRunAdditions)
- (id)initWithCPRStretchedViewPlaneRun:(_CPRStretchedViewPlaneRun *)planeRun heightPixelsPerMm:(CGFloat)pixelsPerMm;
@end
@implementation _CPRStretchedViewPlaneRun
@synthesize range = _range;
@synthesize distances = _distances;
- (id)init
{
if ( (self = [super init]) ) {
_distances = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[_distances release];
_distances = nil;
[super dealloc];
}
@end
@interface CPRStretchedView ()
@property (nonatomic, readwrite, retain) CPRVolumeData *curvedVolumeData; // the volume data that was generated
@property (nonatomic, readwrite, retain) CPRStretchedGeneratorRequest *lastRequest;
@property (nonatomic, readwrite, assign) BOOL drawAllNodes;
@property (nonatomic, readwrite, retain) N3BezierPath *centerlinePath;
+ (NSInteger)_fusionModeForCPRViewClippingRangeMode:(CPRViewClippingRangeMode)clippingRangeMode;
- (void)_setNeedsNewRequest;
- (void)_sendNewRequestIfNeeded;
- (void)_sendNewRequest;
- (void)_sendWillEditCurvedPath;
- (void)_sendDidUpdateCurvedPath;
- (void)_sendDidEditCurvedPath;
- (void)_sendWillEditDisplayInfo;
- (void)_sendDidEditDisplayInfo;
- (void)_updateGeneratedHeight;
- (N3BezierPath *)_projectedBezierPathFromStretchedGeneratorRequest:(CPRStretchedGeneratorRequest *)generatorRequest;
- (void)_drawVerticalLines:(NSArray *)verticalLines;
- (void)_drawVerticalLines:(NSArray *)verticalLines length:(CGFloat)length;
- (void)_updateMousePlanePointsForViewPoint:(NSPoint)point; // this will modify _mousePlanePointsInPix and _displayInfo
- (CGFloat)_distanceToPoint:(NSPoint)point onVerticalLines:(NSArray *)verticalLines pixVector:(N3VectorPointer)closestPixVectorPtr volumeVector:(N3VectorPointer)volumeVectorPtr;
- (CGFloat)_distanceToPoint:(NSPoint)point onPlaneRuns:(NSArray *)planeRuns pixVector:(N3VectorPointer)closestPixVectorPtr volumeVector:(N3VectorPointer)volumeVectorPtr;
- (void)_drawPlaneRuns:(NSArray*)planeRuns;
- (NSArray *)_runsForPlane:(N3Plane)plane verticalLineIndexes:(NSArray **)verticalLinesHandle;
- (void)_buildVerticalLinesAndPlaneRunsForPlaneFullName:(NSString *)planeFullName;
- (void)_clearAllPlanes;
- (void)_planeSetter:(N3Plane)plane;
- (N3Plane)_planeGetter;
- (void)_slabThicknessSetter:(CGFloat)thickness;
- (CGFloat)_slabThicknessGetter;
- (void)_planeColorSetter:(NSColor *)color;
- (NSColor *)_planeColorGetter;
- (void)_buildTransverseVerticalLinesAndPlaneRuns;
- (void)_clearTransversePlanes;
- (N3Vector)_centerlinePixVectorForRelativePosition:(CGFloat)relativePosition;
- (CGFloat)_relativePositionForPixPoint:(NSPoint)pixPoint;
- (CGFloat)_relativePositionForIndex:(NSInteger)index;
- (N3Vector)_vectorForPixPoint:(NSPoint)pixPoint;
- (_CPRStretchedViewPlaneRun *)_limitedRunForRelativePosition:(CGFloat)relativePosition verticalLineIndex:(NSUInteger *)verticalLinePointer lengthFromCenterline:(CGFloat)length;
// calls for dealing with intersections with planes
- (void)_pushBezierPath:(CGFloat)distance;
- (void)_osirixUpdateVolumeDataNotification:(NSNotification *)notification;
@end
@implementation CPRStretchedView
@synthesize delegate = _delegate;
@synthesize volumeData = _volumeData;
@synthesize curvedPath = _curvedPath;
@synthesize displayInfo = _displayInfo;
@synthesize curvedVolumeData = _curvedVolumeData;
@synthesize clippingRangeMode = _clippingRangeMode;
@synthesize lastRequest = _lastRequest;
@synthesize drawAllNodes = _drawAllNodes;
@dynamic orangePlane;
@dynamic purplePlane;
@dynamic bluePlane;
@dynamic orangeSlabThickness;
@dynamic purpleSlabThickness;
@dynamic blueSlabThickness;
@dynamic orangePlaneColor;
@dynamic purplePlaneColor;
@dynamic bluePlaneColor;
@synthesize displayTransverseLines = _displayTransverseLines;
@synthesize displayCrossLines = _displayCrossLines;
@synthesize centerlinePath = _centerlinePath;
+ (BOOL)resolveInstanceMethod:(SEL)selector
{
NSString *methodName;
IMP imp;
const char* typeEncoding;
SEL proxySelector;
methodName = NSStringFromSelector(selector);
proxySelector = NULL;
if ([methodName hasPrefix:@"get"] == NO && [methodName hasPrefix:@"set"] == NO) {
if ([methodName hasSuffix:@"Plane"]) {
proxySelector = @selector(_planeGetter);
} else if ([methodName hasSuffix:@"SlabThickness"]) {
proxySelector = @selector(_slabThicknessGetter);
} else if ([methodName hasSuffix:@"PlaneColor"]) {
proxySelector = @selector(_planeColorGetter);
}
} else if ([methodName hasPrefix:@"set"]) {
if ([methodName hasSuffix:@"Plane:"]) {
proxySelector = @selector(_planeSetter:);
} else if ([methodName hasSuffix:@"SlabThickness:"]) {
proxySelector = @selector(_slabThicknessSetter:);
} else if ([methodName hasSuffix:@"PlaneColor:"]) {
proxySelector = @selector(_planeColorSetter:);
}
}
if (proxySelector) {
imp = class_getMethodImplementation([self class], proxySelector);
typeEncoding = method_getTypeEncoding(class_getInstanceMethod([self class], proxySelector));
return class_addMethod([self class], selector, imp, typeEncoding);
}
return [super resolveInstanceMethod:selector];
}
- (void)setDisplayCrossLines:(BOOL)displayCrossLines
{
if (displayCrossLines != _displayCrossLines) {
_displayCrossLines = displayCrossLines;
if (_displayCrossLines == NO) {
[self _clearAllPlanes];
}
[self setNeedsDisplay:YES];
[[self windowController] updateToolbarItems];
}
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
_planes = [[NSMutableDictionary alloc] init];
_slabThicknesses = [[NSMutableDictionary alloc] init];
_verticalLines = [[NSMutableDictionary alloc] init];
_planeRuns = [[NSMutableDictionary alloc] init];
_planeColors = [[NSMutableDictionary alloc] init];
_mousePlanePointsInPix = [[NSMutableDictionary alloc] init];
_transverseVerticalLines = [[NSMutableDictionary alloc] init];
_transversePlaneRuns = [[NSMutableDictionary alloc] init];
_displayCrossLines = NO;
_displayTransverseLines = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_osirixUpdateVolumeDataNotification:) name:OsirixUpdateVolumeDataNotification object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
_generator.delegate = nil;
[_generator release];
_generator = nil;
[_volumeData release];
_volumeData = nil;
[_curvedVolumeData release];
_curvedVolumeData = nil;
[_curvedPath release];
_curvedPath = nil;
[_displayInfo release];
_displayInfo = nil;
[_lastRequest release];
_lastRequest = nil;
[_centerlinePath release];
_centerlinePath = nil;
[_planes release];
_planes = nil;
[_slabThicknesses release];
_slabThicknesses = nil;
[_verticalLines release];
_verticalLines = nil;
[_planeRuns release];
_planeRuns = nil;
[_planeColors release];
_planeColors = nil;
[_transverseVerticalLines release];
_transverseVerticalLines = nil;
[_transversePlaneRuns release];
_transversePlaneRuns = nil;
[self _clearAllPlanes];
[_mousePlanePointsInPix release];
_mousePlanePointsInPix = nil;
[stanStringAttrib release];
[stringTexA release];
[stringTexB release];
[stringTexC release];
[super dealloc];
}
- (id)valueForKey:(NSString *)key
{
NSString *planeFullName; // full plane name may include Top or Bottom before the plane name
if ([key hasSuffix:@"VerticalLines"]) {
planeFullName = [key substringToIndex:[key length] - 13];
if ([_verticalLines valueForKey:planeFullName] == nil) {
[self _buildVerticalLinesAndPlaneRunsForPlaneFullName:planeFullName];
}
return [_verticalLines objectForKey:planeFullName];
} else if ([key hasSuffix:@"PlaneRuns"]) {
planeFullName = [key substringToIndex:[key length] - 9];
if ([_planeRuns valueForKey:planeFullName] == nil) {
[self _buildVerticalLinesAndPlaneRunsForPlaneFullName:planeFullName];
}
return [_planeRuns valueForKey:planeFullName];
} else {
return [super valueForKey:key];
}
}
- (void)mouseDraggedWindowLevel:(NSEvent *)event
{
[super mouseDraggedWindowLevel: event];
[[self windowController] propagateWLWW: self];
}
- (void)setDrawAllNodes:(BOOL)drawAllNodes
{
if (drawAllNodes != _drawAllNodes) {
_drawAllNodes = drawAllNodes;
[self setNeedsDisplay:YES];
}
}
- (void)setVolumeData:(CPRVolumeData *)volumeData
{
if (volumeData != _volumeData) {
_generator.delegate = nil;
[_generator release];
[_volumeData release];
_volumeData = [volumeData retain];
_generator = [[CPRGenerator alloc] initWithVolumeData:_volumeData];
_generator.delegate = self;
[self _setNeedsNewRequest];
}
}
- (void)setCurvedPath:(CPRCurvedPath *)curvedPath
{
if (curvedPath != _curvedPath) {
[_curvedPath release];
_curvedPath = [curvedPath copy];
[self _clearTransversePlanes];
[self _setNeedsNewRequest];
[self setNeedsDisplay:YES];
}
}
- (void)setDisplayInfo:(CPRDisplayInfo *)dispalyInfo
{
assert(dispalyInfo); // doesn't really need to be the case, but for debugging
if (dispalyInfo != _displayInfo) {
[_displayInfo release];
_displayInfo = [dispalyInfo copy];
[self setNeedsDisplay:YES];
}
}
- (void)setClippingRangeMode:(CPRViewClippingRangeMode)mode
{
if (mode != _clippingRangeMode) {
_clippingRangeMode = mode;
if (curDCM) {
[self setFusion:[[self class] _fusionModeForCPRViewClippingRangeMode:_clippingRangeMode] :self.curvedVolumeData.pixelsDeep];
}
[self _setNeedsNewRequest];
}
}
- (void)setFrame:(NSRect)frameRect
{
BOOL needsUpdate;
needsUpdate = NO;
if( NSEqualRects( frameRect, [self frame]) == NO) {
needsUpdate = YES;
}
[super setFrame: frameRect];
if (needsUpdate) {
[self _setNeedsNewRequest];
}
}
- (CGFloat)generatedHeight
{
return _generatedHeight;
}
- (void) drawTextualData:(NSRect) size :(long) annotations
{
if(_displayTransverseLines)
{
float length = [_curvedPath.bezierPath length];
NSMutableArray *topLeft = [curDCM.annotationsDictionary objectForKey: @"TopLeft"];
length *= 0.1; // We want cm
[topLeft addObject: [NSArray arrayWithObject: [NSString stringWithFormat: NSLocalizedString( @"A-B : %2.2f cm", nil), length*fabs( _curvedPath.transverseSectionPosition - _curvedPath.leftTransverseSectionPosition)]]];
[topLeft addObject: [NSArray arrayWithObject: [NSString stringWithFormat: NSLocalizedString( @"B-C : %2.2f cm", nil), length*fabs( _curvedPath.transverseSectionPosition - _curvedPath.rightTransverseSectionPosition)]]];
[topLeft addObject: [NSArray arrayWithObject: [NSString stringWithFormat: NSLocalizedString( @"A-C : %2.2f cm", nil), length*fabs( _curvedPath.leftTransverseSectionPosition - _curvedPath.rightTransverseSectionPosition)]]];
[super drawTextualData: size :annotations];
[topLeft removeLastObject];
[topLeft removeLastObject];
[topLeft removeLastObject];
}
else [super drawTextualData: size :annotations];
}
- (void)drawRect:(NSRect)rect
{
if( rect.size.width > 10)
{
_processingRequest = YES;
[self _sendNewRequestIfNeeded];
_processingRequest = NO;
// [self _adjustROIs];
[super drawRect: rect];
}
}
- (void)setNeedsDisplay:(BOOL)flag
{
if (_processingRequest == NO) {
[super setNeedsDisplay:flag];
}
}
- (NSPoint) positionWithoutRotation: (NSPoint) tPt
{
NSRect unrotatedRect = NSMakeRect( tPt.x/scaleValue, tPt.y/scaleValue, 1, 1);
NSRect centeredRect = unrotatedRect;
float ratio = 1;
if( self.pixelSpacingX != 0 && self.pixelSpacingY != 0)
ratio = self.pixelSpacingX / self.pixelSpacingY;
centeredRect.origin.y -= [self origin].y*ratio/scaleValue;
centeredRect.origin.x -= - [self origin].x/scaleValue;
unrotatedRect.origin.x = centeredRect.origin.x*cos( -self.rotation*deg2rad) + centeredRect.origin.y*sin( -self.rotation*deg2rad)/ratio;
unrotatedRect.origin.y = -centeredRect.origin.x*sin( -self.rotation*deg2rad) + centeredRect.origin.y*cos( -self.rotation*deg2rad)/ratio;
unrotatedRect.origin.y *= ratio;
unrotatedRect.origin.y += [self origin].y*ratio/scaleValue;
unrotatedRect.origin.x += - [self origin].x/scaleValue;
tPt = NSMakePoint( unrotatedRect.origin.x, unrotatedRect.origin.y);
tPt.x = (tPt.x)*scaleValue - unrotatedRect.size.width/2;
tPt.y = (tPt.y)/ratio*scaleValue - unrotatedRect.size.height/2/ratio;
return tPt;
}
- (void)subDrawRect:(NSRect)rect
{
double pixToSubdrawRectOpenGLTransform[16];
NSInteger i;
N3Vector endpoint;
N3BezierPath *centerline;
NSString *planeName;
NSColor *planeColor;
N3AffineTransform pixToSubDrawRectTransform;
N3Vector cursorVector;
CGFloat relativePosition;
CGLContextObj cgl_ctx = [[NSOpenGLContext currentContext] CGLContextObj];
if( cgl_ctx == nil)
return;
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if ([curDCM pixelSpacingX] == 0) {
return;
}
centerline = [self centerlinePath];
pixToSubDrawRectTransform = [self pixToSubDrawRectTransform];
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
N3AffineTransformGetOpenGLMatrixd([self pixToSubDrawRectTransform], pixToSubdrawRectOpenGLTransform);
glMultMatrixd(pixToSubdrawRectOpenGLTransform);
// draw the centerline.
glColor3f(0, 1, 0);
glLineWidth(1.0 * self.window.backingScaleFactor);
glBegin(GL_LINE_STRIP);
for (i = 0; i < [centerline elementCount]; i++) {
[centerline elementAtIndex:i control1:NULL control2:NULL endpoint:&endpoint];
glVertex2d(endpoint.x, endpoint.y);
}
glEnd();
glColor4d(0.0, 1.0, 0.0, 0.8);
if ( [[self windowController] displayMousePosition] == YES && _displayInfo.mouseCursorHidden == NO)
{
cursorVector = [self _centerlinePixVectorForRelativePosition:_displayInfo.mouseCursorPosition];
glEnable(GL_POINT_SMOOTH);
glPointSize(8 * self.window.backingScaleFactor);
glBegin(GL_POINTS);
glVertex2f(cursorVector.x, cursorVector.y);
glEnd();
glDisable(GL_POINT_SMOOTH);
}
glPopMatrix();
if (_displayCrossLines) {
for (planeName in _planes) {
planeColor = [self valueForKey:[planeName stringByAppendingString:@"PlaneColor"]];
glLineWidth(2.0 * self.window.backingScaleFactor);
// draw planes
glColor4f ([planeColor redComponent], [planeColor greenComponent], [planeColor blueComponent], [planeColor alphaComponent]);
[self _drawPlaneRuns:[self valueForKey:[planeName stringByAppendingString:@"PlaneRuns"]]];
[self _drawVerticalLines:[self valueForKey:[planeName stringByAppendingString:@"VerticalLines"]]];
glLineWidth(1.0 * self.window.backingScaleFactor);
[self _drawPlaneRuns:[self valueForKey:[planeName stringByAppendingString:@"TopPlaneRuns"]]];
[self _drawPlaneRuns:[self valueForKey:[planeName stringByAppendingString:@"BottomPlaneRuns"]]];
[self _drawVerticalLines:[self valueForKey:[planeName stringByAppendingString:@"TopVerticalLines"]]];
[self _drawVerticalLines:[self valueForKey:[planeName stringByAppendingString:@"BottomVerticalLines"]]];
}
}
float exportTransverseSliceInterval = 0;
if( [[self windowController] exportSequenceType] == CPRSeriesExportSequenceType && [[self windowController] exportSeriesType] == CPRTransverseViewsExportSeriesType)
exportTransverseSliceInterval = [[self windowController] exportTransverseSliceInterval];
if( exportTransverseSliceInterval > 0)
{
glColor4d(1.0, 1.0, 0.0, 1.0);
N3MutableBezierPath *flattenedPath = [[_curvedPath.bezierPath mutableCopy] autorelease];
[flattenedPath subdivide:N3BezierDefaultSubdivideSegmentLength];
[flattenedPath flatten:N3BezierDefaultFlatness];
float curveLength = [flattenedPath length];
int noOfFrames = ( curveLength / exportTransverseSliceInterval);
noOfFrames++;
float startingDistance = curveLength - (noOfFrames-1) * exportTransverseSliceInterval;
startingDistance /= 2;
// we need to find the tangents to the curve at
N3VectorArray vectors;
N3VectorArray tangents;
vectors = malloc(noOfFrames * sizeof(N3Vector));
tangents = malloc(noOfFrames * sizeof(N3Vector));
noOfFrames = N3BezierCoreGetVectorInfo([_curvedPath.bezierPath N3BezierCore], exportTransverseSliceInterval, startingDistance, N3VectorZero, vectors, tangents, NULL, noOfFrames);
CPRTransverseView *t = [[self windowController] middleTransverseView];
CGFloat transverseWidth = (float)t.curDCM.pwidth/t.pixelsPerMm;
transverseWidth /= self.pixelSpacingY;
for( int i = 0; i < noOfFrames; i++)
{
_CPRStretchedViewPlaneRun *transverseRun;
NSUInteger transverseIndex;
relativePosition = (startingDistance + (exportTransverseSliceInterval * (CGFloat)i)) / curveLength;
transverseRun = [self _limitedRunForRelativePosition:relativePosition verticalLineIndex:&transverseIndex lengthFromCenterline: transverseWidth];
glLineWidth(2.0 * self.window.backingScaleFactor);
if (transverseRun) {
[self _drawPlaneRuns:[NSArray arrayWithObject:transverseRun]];
} else {
[self _drawVerticalLines:[NSArray arrayWithObject:[NSNumber numberWithUnsignedInteger:transverseIndex]] length: transverseWidth];
}
}
}
else if(_displayTransverseLines)
{
NSString *name;
if ([_transverseVerticalLines count] == 0) {
[self _buildTransverseVerticalLinesAndPlaneRuns];
}
glColor4d(1.0, 1.0, 0.0, 1.0);
for (name in _transverseVerticalLines) {
NSArray *transverseVerticalLine = [_transverseVerticalLines objectForKey:name];
if ([name isEqualToString:@"center"]) {
glLineWidth(2.0 * self.window.backingScaleFactor);
} else {
glLineWidth(1.0 * self.window.backingScaleFactor);
}
[self _drawVerticalLines:transverseVerticalLine length:curDCM.pheight/3.0];
}
for (name in _transversePlaneRuns) {
NSArray *transversePlaneRun = [_transversePlaneRuns objectForKey:name];
if ([name isEqualToString:@"center"]) {
glLineWidth(2.0 * self.window.backingScaleFactor);
} else {
glLineWidth(1.0 * self.window.backingScaleFactor);
}
[self _drawPlaneRuns:transversePlaneRun];
}
N3Vector transverseIntersectionA = [self _centerlinePixVectorForRelativePosition:[_curvedPath leftTransverseSectionPosition]];
N3Vector transverseIntersectionB = [self _centerlinePixVectorForRelativePosition:[_curvedPath transverseSectionPosition]];
N3Vector transverseIntersectionC = [self _centerlinePixVectorForRelativePosition:[_curvedPath rightTransverseSectionPosition]];
transverseIntersectionA = N3VectorApplyTransform(transverseIntersectionA, pixToSubDrawRectTransform);
transverseIntersectionB = N3VectorApplyTransform(transverseIntersectionB, pixToSubDrawRectTransform);
transverseIntersectionC = N3VectorApplyTransform(transverseIntersectionC, pixToSubDrawRectTransform);
// --- Text
if( stanStringAttrib == nil)
{
stanStringAttrib = [[NSMutableDictionary dictionary] retain];
[stanStringAttrib setObject:[NSFont fontWithName:@"Helvetica" size: 14.0] forKey:NSFontAttributeName];
[stanStringAttrib setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
}
if( stringTexA == nil)
{
stringTexA = [[StringTexture alloc] initWithString: @"A"
withAttributes:stanStringAttrib
withTextColor:[NSColor colorWithDeviceRed: 1 green: 1 blue: 0 alpha:1.0f]
withBoxColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:0.0f]
withBorderColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:0.0f]];
[stringTexA setAntiAliasing: YES];
}
if( stringTexB == nil)
{
stringTexB = [[StringTexture alloc] initWithString: @"B"
withAttributes:stanStringAttrib
withTextColor:[NSColor colorWithDeviceRed: 1 green: 1 blue: 0 alpha:1.0f]
withBoxColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:0.0f]
withBorderColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:0.0f]];
[stringTexB setAntiAliasing: YES];
}
if( stringTexC == nil)
{
stringTexC = [[StringTexture alloc] initWithString: @"C"
withAttributes:stanStringAttrib
withTextColor:[NSColor colorWithDeviceRed: 1 green: 1 blue: 0 alpha:1.0f]
withBoxColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:0.0f]
withBorderColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:0.0f]];
[stringTexC setAntiAliasing: YES];
}
glEnable (GL_TEXTURE_RECTANGLE_EXT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
{
glPushMatrix();
float ratio = 1;
if( self.pixelSpacingX != 0 && self.pixelSpacingY != 0)
ratio = self.pixelSpacingX / self.pixelSpacingY;
glLoadIdentity (); // reset model view matrix to identity (eliminates rotation basically)
glScalef (2.0f /([self xFlipped] ? -([self drawingFrameRect].size.width) : [self drawingFrameRect].size.width), -2.0f / ([self yFlipped] ? -([self drawingFrameRect].size.height) : [self drawingFrameRect].size.height), 1.0f); // scale to port per pixel scale
glTranslatef( [self origin].x, -[self origin].y, 0.0f);
[stringTexA setFlippedX: [self xFlipped] Y:[self yFlipped]];
[stringTexB setFlippedX: [self xFlipped] Y:[self yFlipped]];
[stringTexC setFlippedX: [self xFlipped] Y:[self yFlipped]];
NSPoint tPt;
tPt = [self positionWithoutRotation: NSMakePoint( transverseIntersectionA.x, transverseIntersectionA.y)];
glColor4f (0, 0, 0, 1); [stringTexA drawAtPoint:NSMakePoint(tPt.x+1, tPt.y+1) ratio: 1];
glColor4f (1, 1, 0, 1); [stringTexA drawAtPoint:NSMakePoint(tPt.x, tPt.y) ratio: 1];
tPt = [self positionWithoutRotation: NSMakePoint( transverseIntersectionB.x, transverseIntersectionB.y)];
glColor4f (0, 0, 0, 1); [stringTexB drawAtPoint:NSMakePoint(tPt.x+1, tPt.y+1) ratio: 1];
glColor4f (1, 1, 0, 1); [stringTexB drawAtPoint:NSMakePoint(tPt.x, tPt.y) ratio: 1];
tPt = [self positionWithoutRotation: NSMakePoint( transverseIntersectionC.x, transverseIntersectionC.y)];
glColor4f (0, 0, 0, 1); [stringTexC drawAtPoint:NSMakePoint(tPt.x+1, tPt.y+1) ratio: 1];
glColor4f (1, 1, 0, 1); [stringTexC drawAtPoint:NSMakePoint(tPt.x, tPt.y) ratio: 1];
glPopMatrix();
}
glDisable (GL_TEXTURE_RECTANGLE_EXT);
}
if( [[self windowController] displayMousePosition] == YES)
{
// draw the point on the plane lines
for (planeName in _mousePlanePointsInPix)
{
planeColor = [self valueForKey:[NSString stringWithFormat:@"%@PlaneColor", planeName]];
glColor4f ([planeColor redComponent], [planeColor greenComponent], [planeColor blueComponent], [planeColor alphaComponent]);
glEnable(GL_POINT_SMOOTH);
glPointSize(8 * self.window.backingScaleFactor);
cursorVector = N3VectorApplyTransform([[_mousePlanePointsInPix objectForKey:planeName] N3VectorValue], pixToSubDrawRectTransform);
glBegin(GL_POINTS);
glVertex2f(cursorVector.x, cursorVector.y);
glEnd();
}
// if (_displayInfo.mouseTransverseSection != CPRTransverseViewNoneSectionType) {
// switch (_displayInfo.mouseTransverseSection) {
// case CPRTransverseViewLeftSectionType:
// relativePosition = _curvedPath.leftTransverseSectionPosition;
// break;
// case CPRTransverseViewCenterSectionType:
// relativePosition = _curvedPath.transverseSectionPosition;
// break;
// case CPRTransverseViewRightSectionType:
// relativePosition = _curvedPath.rightTransverseSectionPosition;
// break;
// default:
// relativePosition = 0;
// break;
// }
//
// cursorVector = N3VectorMake((CGFloat)curDCM.pwidth*relativePosition, ((CGFloat)curDCM.pheight/2.0)+(_displayInfo.mouseTransverseSectionDistance*pixelsPerMm), 0);
// cursorVector = N3VectorApplyTransform(cursorVector, pixToSubDrawRectTransform);
//
// glColor4d(1.0, 1.0, 0.0, 1.0);
// glEnable(GL_POINT_SMOOTH);
// glPointSize(8 * self.window.backingScaleFactor);
// glBegin(GL_POINTS);
// glVertex2f(cursorVector.x, cursorVector.y);
// glEnd();
// }
}
if (_drawAllNodes)
{
for (i = 0; i < [_curvedPath.nodes count]; i++)
{
relativePosition = [_curvedPath relativePositionForNodeAtIndex:i];
cursorVector = [self _centerlinePixVectorForRelativePosition:relativePosition];
// cursorVector = N3VectorMake(curDCM.pwidth * relativePosition, (CGFloat)curDCM.pheight/2.0, 0);
cursorVector = N3VectorApplyTransform(cursorVector, pixToSubDrawRectTransform);
if (_displayInfo.hoverNodeHidden == NO && _displayInfo.hoverNodeIndex == i)
{
glColor4d(1.0, 0.5, 0.0, 1.0);
} else {
glColor4d(1.0, 0.0, 0.0, 1.0);
}
glEnable(GL_POINT_SMOOTH);
glPointSize(8 * self.window.backingScaleFactor);
glBegin(GL_POINTS);
glVertex2f(cursorVector.x, cursorVector.y);
glEnd();
}
}
// Red Square
if( [[self window] firstResponder] == self && stringID == nil)
{
glLoadIdentity (); // reset model view matrix to identity (eliminates rotation basically)
glScalef (2.0f /(xFlipped ? -(drawingFrameRect.size.width) : drawingFrameRect.size.width), -2.0f / (yFlipped ? -(drawingFrameRect.size.height) : drawingFrameRect.size.height), 1.0f); // scale to port per pixel scale
glColor4d(1.0, 0, 0.0, 1.0);
float heighthalf = drawingFrameRect.size.height/2;
float widthhalf = drawingFrameRect.size.width/2;
glLineWidth(8.0 * self.window.backingScaleFactor);
glBegin(GL_LINE_LOOP);
glVertex2f( -widthhalf, -heighthalf);
glVertex2f( -widthhalf, heighthalf);
glVertex2f( widthhalf, heighthalf);
glVertex2f( widthhalf, -heighthalf);
glEnd();
}
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POLYGON_SMOOTH);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_BLEND);
}
- (void) updatePresentationStateFromSeriesOnlyImageLevel: (BOOL) onlyImage
{
}
- (void)generator:(CPRGenerator *)generator didGenerateVolume:(CPRVolumeData *)volume request:(CPRGeneratorRequest *)request
{
if( [self windowController] == nil)
return;
NSUInteger i;
NSMutableArray *pixArray;
DCMPix *newPix;
CPRVolumeDataInlineBuffer inlineBuffer;
[self _updateGeneratedHeight];
NSPoint previousOrigin = [self origin];
float previousScale = [self scaleValue];
float previousRotation = [self rotation];
int previousHeight = [curDCM pheight], previousWidth = [curDCM pwidth];
NSData *previousROIs = [NSArchiver archivedDataWithRootObject: [self curRoiList]];
[[self.curvedVolumeData retain] autorelease]; // make sure this is around long enough so that it doesn't disapear under the old DCMPix
self.curvedVolumeData = volume;
pixArray = [[NSMutableArray alloc] init];
// blow away local caches of overlay lines
self.centerlinePath = nil;
_midHeightPoint = N3VectorZero;
_projectionNormal = N3VectorZero;
for (i = 0; i < self.curvedVolumeData.pixelsDeep; i++)
{
if ([self.curvedVolumeData aquireInlineBuffer:&inlineBuffer]) {
newPix = [[DCMPix alloc] initWithData:(float *)CPRVolumeDataFloatBytes(&inlineBuffer) + (i*self.curvedVolumeData.pixelsWide*self.curvedVolumeData.pixelsHigh) :32
:self.curvedVolumeData.pixelsWide :self.curvedVolumeData.pixelsHigh :self.curvedVolumeData.pixelSpacingX :self.curvedVolumeData.pixelSpacingY
:0.0 :0.0 :0.0 :NO];
} else {
assert(0);
newPix = [[DCMPix alloc] init];
}
[self.curvedVolumeData releaseInlineBuffer:&inlineBuffer];
[newPix setImageObjectID: [[[self windowController] originalPix] imageObjectID]];
[newPix setSourceFile: [[[self windowController] originalPix] sourceFile]];
[newPix setAnnotationsDictionary: [[[self windowController] originalPix] annotationsDictionary]];
[pixArray addObject:newPix];
[newPix release];
}
if( [pixArray count])
{
[self _clearAllPlanes];
[self _clearTransversePlanes];
self.centerlinePath = [self _projectedBezierPathFromStretchedGeneratorRequest:(CPRStretchedGeneratorRequest*)request];
_midHeightPoint = [(CPRStretchedGeneratorRequest*)request midHeightPoint];
_projectionNormal = [(CPRStretchedGeneratorRequest*)request projectionNormal];
for( i = 0; i < [pixArray count]; i++)
[[pixArray objectAtIndex: i] setArrayPix:pixArray :i];
[self setPixels:pixArray files:NULL rois:NULL firstImage:0 level:'i' reset:YES];
[self setScaleValueCentered: 0.8 * self.window.backingScaleFactor];
//[self setWLWW:wl :ww];
[[self windowController] propagateWLWW: [[self windowController] mprView1]];
[self setFusion:[[self class] _fusionModeForCPRViewClippingRangeMode:_clippingRangeMode] :self.curvedVolumeData.pixelsDeep];
if( previousWidth == [curDCM pwidth] && previousHeight == [curDCM pheight])
{
[self setOrigin:previousOrigin];
[self setScaleValue: previousScale];
[self setRotation: previousRotation];
}
NSArray *roiArray = [NSUnarchiver unarchiveObjectWithData: previousROIs];
for( ROI *r in roiArray)
{
r.pix = curDCM;
[r setOriginAndSpacing :curDCM.pixelSpacingX : curDCM.pixelSpacingY :NSMakePoint( curDCM.originX, curDCM.originY) :NO :NO];
[r setRoiView :self];
}
[[self curRoiList] addObjectsFromArray: roiArray];
[self setNeedsDisplay:YES];
}
[pixArray release];
}
- (void)generator:(CPRGenerator *)generator didAbandonRequest:(CPRGeneratorRequest *)request
{
}
- (void)waitUntilPixUpdate
{
[self _sendNewRequestIfNeeded];
[_generator runUntilAllRequestsAreFinished];
}
- (void)mouseEntered:(NSEvent *)theEvent
{
[self _sendWillEditDisplayInfo];
_displayInfo.mouseCursorHidden = NO;
[self _sendDidEditDisplayInfo];
[super mouseEntered:theEvent];
}
- (void)mouseExited:(NSEvent *)theEvent
{
[self _sendWillEditDisplayInfo];
_displayInfo.mouseCursorHidden = YES;
[_displayInfo clearAllMouseVectors];
_displayInfo.mouseTransverseSection = CPRTransverseViewNoneSectionType;
_displayInfo.mouseTransverseSectionDistance = 0;
[self _sendDidEditDisplayInfo];
[_mousePlanePointsInPix removeAllObjects];
self.drawAllNodes = NO;
[self setNeedsDisplay:YES];
[super mouseExited:theEvent];
}
- (void)mouseMoved:(NSEvent *)theEvent
{
NSView* view = [[[theEvent window] contentView] hitTest:[theEvent locationInWindow]];
if( view == self)
{
NSPoint viewPoint;
N3Vector pixVector;
NSInteger i;
BOOL overNode;
NSInteger hoverNodeIndex;
CGFloat relativePosition;
N3Vector vector;
CGFloat distanceFromCenterline;
viewPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
if( NSPointInRect( viewPoint, [self bounds]) == NO)
return;
pixVector = N3VectorApplyTransform(N3VectorMakeFromNSPoint(viewPoint), [self viewToPixTransform]);
if (NSPointInRect(viewPoint, self.bounds) && curDCM.pwidth > 0) {
[self _sendWillEditDisplayInfo];
_displayInfo.mouseCursorPosition = [self _relativePositionForPixPoint:NSPointFromN3Vector(pixVector)];
// _displayInfo.mouseCursorPosition = MIN(MAX(pixVector.x/(CGFloat)curDCM.pwidth, 0.0), 1.0);
[self setNeedsDisplay:YES];
[self _updateMousePlanePointsForViewPoint:viewPoint]; // this will modify _mousePlanePointsInPix and _displayInfo
// test to see if the mouse is near a trasverse line
// _displayInfo.mouseTransverseSection = CPRTransverseViewNoneSectionType;
// _displayInfo.mouseTransverseSectionDistance = 0.0;
// if (_displayTransverseLines) {
// distance = ABS(pixVector.x - _curvedPath.leftTransverseSectionPosition*(CGFloat)curDCM.pwidth);
// minDistance = distance;
// if (distance < 20.0) {
// _displayInfo.mouseTransverseSection = CPRTransverseViewLeftSectionType;
// _displayInfo.mouseTransverseSectionDistance = (pixVector.y - ((CGFloat)curDCM.pheight/2.0))*([_curvedPath.bezierPath length]/(CGFloat)curDCM.pwidth);
// }
// distance = ABS(pixVector.x - _curvedPath.rightTransverseSectionPosition*(CGFloat)curDCM.pwidth);
// if (distance < 20.0 && distance < minDistance) {
// _displayInfo.mouseTransverseSection = CPRTransverseViewRightSectionType;
// _displayInfo.mouseTransverseSectionDistance = (pixVector.y - ((CGFloat)curDCM.pheight/2.0))*([_curvedPath.bezierPath length]/(CGFloat)curDCM.pwidth);
// minDistance = distance;
// }
// distance = ABS(pixVector.x - _curvedPath.transverseSectionPosition*(CGFloat)curDCM.pwidth);
// if (distance < 20.0 && distance < minDistance) {
// _displayInfo.mouseTransverseSection = CPRTransverseViewCenterSectionType;
// _displayInfo.mouseTransverseSectionDistance = (pixVector.y - ((CGFloat)curDCM.pheight/2.0))*([_curvedPath.bezierPath length]/(CGFloat)curDCM.pwidth);
// }
// }
// line = N3LineMake(N3VectorMake(0, (CGFloat)curDCM.pheight / 2.0, 0), N3VectorMake(1, 0, 0));
// line = N3LineApplyTransform(line, N3AffineTransformInvert([self viewToPixTransform]));
//
[_centerlinePath relativePositionClosestToLine:N3LineMake(pixVector, N3VectorMake(0, 0, 1)) closestVector:&vector];
distanceFromCenterline = N3VectorDistanceToLine(vector, N3LineMake(pixVector, N3VectorMake(0, 0, 1)));
if (distanceFromCenterline < 20.0) {
self.drawAllNodes = YES;
} else {
self.drawAllNodes = NO;
}
overNode = NO;
hoverNodeIndex = 0;
if (self.drawAllNodes) {
for (i = 0; i < [_curvedPath.nodes count]; i++) {
relativePosition = [_curvedPath relativePositionForNodeAtIndex:i];
if (N3VectorDistance(pixVector, [self _centerlinePixVectorForRelativePosition:relativePosition]) < 10) {
overNode = YES;
hoverNodeIndex = i;
break;
}
}
if (overNode) {
if (_displayInfo.hoverNodeHidden == YES || _displayInfo.hoverNodeIndex != hoverNodeIndex) {
_displayInfo.hoverNodeHidden = NO;
_displayInfo.hoverNodeIndex = hoverNodeIndex;