-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMIVVesselPlotView.mm
525 lines (447 loc) · 14.1 KB
/
CMIVVesselPlotView.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
//
// CMIVVesselPlotView.m
// CMIV_CTA_TOOLS
//
// Created by chuwa on 12/4/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "CMIVVesselPlotView.h"
@implementation CMIVVesselPlotView
@synthesize curPtX;
#pragma mark - 1. init and delloc functions
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
backgroundColor = [NSColor blackColor];
pointsColor = [NSColor blackColor];
textLabelColor = [NSColor whiteColor];
curveColor = [NSColor whiteColor];
axesColor = [NSColor whiteColor];
selectBoxColor = [NSColor blueColor];
referenceLineColor = [NSColor yellowColor];
[curveColor retain];
pointDiameter = 8;
lineWidth = 1.5;
xUnit=1.0;
yUnit=1.0;
xLeftLimit=0;
xRightLimit=500;
yTopLimit=100;
yBottomLimit=0;
handleSize=20;
leftSpace=22;
bottomSpace=22;
xScaleFactor=(frame.size.width-leftSpace)/xRightLimit;
yScaleFactor=(frame.size.height-bottomSpace)/yTopLimit;
viewFrame=frame;
currentCurve=nil;
}
return self;
}
- (void)dealloc
{
if(globalTransform)
[globalTransform release];
if(currentCurve)
[currentCurve release];
currentCurve=nil;
if(curveColor)
[curveColor release];
[super dealloc];
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
#pragma mark - 2. drawing functions
- (void)drawRect:(NSRect)rect
{
[self drawAxesInRect:rect];
[self drawCurveInRect:rect];
[self drawSelectedBoxInRect:rect];
[self drawStenosisBoxInRect:rect];
}
- (void)drawAxesInRect:(NSRect)rect
{
// background
[backgroundColor set];
NSRectFill(rect);
// y axis
[axesColor set];
NSAffineTransform *transform = [self transform:rect];
NSBezierPath *line = [NSBezierPath bezierPath];
[line setLineWidth:lineWidth];
NSPoint p1, p2;
p1 = NSMakePoint(xLeftLimit, yBottomLimit-handleSize*2);
p2 = NSMakePoint(xLeftLimit, yTopLimit);
p1 = [transform transformPoint:p1];
p2 = [transform transformPoint:p2];
[line moveToPoint:p1];
[line lineToPoint:p2];
// Handle of y axis
NSRect hanlerect;
hanlerect.origin.x=p1.x-handleSize/2;
hanlerect.origin.y=p1.y+handleSize/2;
hanlerect.size.width=handleSize;
hanlerect.size.height=handleSize;
// x axis
p1 = NSMakePoint(xLeftLimit-handleSize*2, yBottomLimit);
p2 = NSMakePoint(xRightLimit, yBottomLimit);
p1 = [transform transformPoint:p1];
p2 = [transform transformPoint:p2];
[line moveToPoint:p1];
[line lineToPoint:p2];
// Handle of x axis
hanlerect.origin.x=p1.x+handleSize/2;
hanlerect.origin.y=p1.y-handleSize/2;
hanlerect.size.width=handleSize;
hanlerect.size.height=handleSize;
//path = [NSBezierPath bezierPathWithOvalInRect:hanlerect];
//[path setLineWidth:lineWidth];
//[path stroke];
if(mousePtX>0)
{
p1 = NSMakePoint(mousePtX, 0);
p2 = NSMakePoint(mousePtX, yTopLimit);
p1 = [transform transformPoint:p1];
p2 = [transform transformPoint:p2];
[line moveToPoint:p1];
[line lineToPoint:p2];
}
[line stroke];
}
- (void)drawCurveInRect:(NSRect)rect
{
if (!currentCurve||[currentCurve count]==0)
return;
NSAffineTransform *transform = [self transform:rect];
NSBezierPath *line = [NSBezierPath bezierPath];
NSPoint pt;
pt = NSMakePoint(0,[[currentCurve objectAtIndex:0] floatValue]);
[line moveToPoint:pt];
unsigned j;
for (j=1; j<[currentCurve count]; j++)
{
pt = NSMakePoint(j*xUnit,[[currentCurve objectAtIndex:j] floatValue]);
[line lineToPoint:pt];
}
line = [transform transformBezierPath:line];
[curveColor set];
[line setLineWidth:lineWidth];
[line stroke];
}
- (void)drawSelectedBoxInRect:(NSRect)rect
{
// Draw box borders
if (!currentCurve||[currentCurve count]==0||(endPtX==0 && startPtX==0))
return;
NSRect selectRect;
selectRect.origin.x=startPtX;
selectRect.origin.y=0;
selectRect.size.width=endPtX-startPtX;
selectRect.size.height=yTopLimit;
NSBezierPath *line = [NSBezierPath bezierPathWithRect:selectRect];
NSAffineTransform *transform = [self transform:rect];
line = [transform transformBezierPath:line];
[selectBoxColor set];
[line setLineWidth:lineWidth];
[line stroke];
line = [NSBezierPath bezierPath];
[line setLineWidth:lineWidth];
NSPoint p1, p2;
p1 = NSMakePoint(curPtX, 0);
p2 = NSMakePoint(curPtX, yTopLimit);
p1 = [transform transformPoint:p1];
p2 = [transform transformPoint:p2];
[line moveToPoint:p1];
[line lineToPoint:p2];
[line stroke];
line = [NSBezierPath bezierPath];
// Draw reference lines
[referenceLineColor set];
p1 = NSMakePoint(startPtX, 0);
unsigned int xindex;
float y;
xindex=startPtX/xUnit;
if(xindex+1>=[currentCurve count])
y=[[currentCurve lastObject] floatValue];
else
{
y=[[currentCurve objectAtIndex:xindex] floatValue]+([[currentCurve objectAtIndex:xindex+1] floatValue]-[[currentCurve objectAtIndex:xindex] floatValue])*(startPtX-(float)xindex*xUnit)/xUnit;
}
p2 = NSMakePoint(startPtX, y);
p1 = [transform transformPoint:p1];
p2 = [transform transformPoint:p2];
[line moveToPoint:p1];
[line lineToPoint:p2];
referenceY=y;
p1 = NSMakePoint(endPtX, 0);
xindex=endPtX/xUnit;
if(xindex+1>=[currentCurve count])
y=[[currentCurve lastObject] floatValue];
else
{
y=[[currentCurve objectAtIndex:xindex] floatValue]+([[currentCurve objectAtIndex:xindex+1] floatValue]-[[currentCurve objectAtIndex:xindex] floatValue])*(endPtX-(float)xindex*xUnit)/xUnit;
}
p2 = NSMakePoint(endPtX, y);
p1 = [transform transformPoint:p1];
p2 = [transform transformPoint:p2];
[line moveToPoint:p1];
[line lineToPoint:p2];
if(endPtX!=curPtX)
referenceY=(referenceY+y)/2;
p1 = NSMakePoint(curPtX, 0);
xindex=curPtX/xUnit;
if(xindex+1>=[currentCurve count])
y=[[currentCurve lastObject] floatValue];
else
{
y=[[currentCurve objectAtIndex:xindex] floatValue]+([[currentCurve objectAtIndex:xindex+1] floatValue]-[[currentCurve objectAtIndex:xindex] floatValue])*(curPtX-(float)xindex*xUnit)/xUnit;
}
p2 = NSMakePoint(curPtX, y);
p1 = [transform transformPoint:p1];
p2 = [transform transformPoint:p2];
[line moveToPoint:p2];
[line lineToPoint:p1];
NSPoint pt;
pt.y=p1.y-handleSize;
pt.x=p1.x-handleSize/2;
[line lineToPoint:pt];
pt.y=p1.y-handleSize;
pt.x=p1.x+handleSize/2;
[line lineToPoint:pt];
[line lineToPoint:p1];
curXHandleRect.origin.x=p1.x-handleSize/2;
curXHandleRect.origin.y=pt.y;
curXHandleRect.size.width=handleSize;
curXHandleRect.size.height=handleSize;
curPtY=y;
[line stroke];
}
- (void)drawStenosisBoxInRect:(NSRect)rect
{
if (!currentCurve||[currentCurve count]==0||(endPtX==0 && startPtX==0))
return;
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithCapacity:3];
[attrsDictionary setObject:textLabelColor forKey:NSForegroundColorAttributeName];
NSAttributedString *label = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f/%.2f=%d%%", curPtY, referenceY, (int)(100.0*curPtY/referenceY)] attributes:attrsDictionary] autorelease];
NSPoint pt1 = curXHandleRect.origin;
NSPoint labelPosition = NSMakePoint(pt1.x + 2*handleSize, pt1.y);
NSRect labelBounds = [label boundingRectWithSize:rect.size options:NSStringDrawingUsesDeviceMetrics];
labelBounds.size.height += 1.0;
labelBounds.size.width += 4.0;
if (labelPosition.x+labelBounds.size.width >= rect.size.width)
{
labelPosition.x = pt1.x - handleSize - labelBounds.size.width;
}
//NSBezierPath *labelRect = [NSBezierPath bezierPathWithRect:NSMakeRect(labelPosition.x-2.0,labelPosition.y,labelBounds.size.width,labelBounds.size.height)];
//[[[NSColor blackColor] colorWithAlphaComponent:0.5] set];
//[labelRect fill];
[label drawAtPoint:labelPosition];
}
#pragma mark - 3. central parameters
- (NSAffineTransform *)transform:(NSRect) rect
{
if (!globalTransform || rect.size.width!=viewFrame.size.width ||rect.size.height!=viewFrame.size.height)
{
if (globalTransform)
[globalTransform release];
if (xRightLimit>0 && yTopLimit>0)
{
xScaleFactor=(rect.size.width-leftSpace)/xRightLimit;
yScaleFactor=(rect.size.height-bottomSpace)/yTopLimit;
if(xScaleFactor<1/xUnit)
xScaleFactor=1/xUnit;
}
globalTransform = [NSAffineTransform transform];
[globalTransform scaleXBy:xScaleFactor yBy:yScaleFactor];
NSAffineTransform* transform2 = [NSAffineTransform transform];
[transform2 translateXBy:leftSpace yBy:bottomSpace];
[globalTransform appendTransform:transform2];
[globalTransform retain];
viewFrame=rect;
if (invertedTransfer)
[invertedTransfer release];
invertedTransfer = [[NSAffineTransform alloc] initWithTransform:globalTransform];
[invertedTransfer invert];
}
return globalTransform;
}
-(void) updateAllControllsWithCurve:(NSString*)name
{
}
#pragma mark - 4. mouse operations
- (void)mouseDown:(NSEvent *)theEvent
{
if (currentCurve && [currentCurve count]>0)
{
[[self window] makeFirstResponder: self];
NSPoint mousePositionInWindow = [theEvent locationInWindow];
NSPoint mousePositionInView = [self convertPoint:mousePositionInWindow fromView:nil];
if (mousePositionInView.x>0 &&
mousePositionInView.y>0 &&
mousePositionInView.x<viewFrame.size.width &&
mousePositionInView.y<viewFrame.size.height)
{
NSPoint ptInPlot;
ptInPlot = [invertedTransfer transformPoint:mousePositionInView];
mouseStartDraggingFlag=0;
if (ptInPlot.x>0 && ptInPlot.y>0 && ptInPlot.x<xRightLimit && ptInPlot.y<yTopLimit)
{
mouseStartDraggingFlag=1;
startPtX=endPtX= curPtX=ptInPlot.x;
[viewControllor syncWithPlot];
}
else if (NSPointInRect(mousePositionInView, curXHandleRect))
{
mouseStartDraggingFlag=2;
startDragPoint=ptInPlot;
startDragPoint.y=curPtX;
}
}
[self setNeedsDisplay:YES];
}
[super mouseDown:theEvent];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
if(currentCurve && [currentCurve count]>0)
{
NSPoint mousePositionInWindow = [theEvent locationInWindow];
NSPoint mousePositionInView = [self convertPoint:mousePositionInWindow fromView:nil];
if(mouseStartDraggingFlag)
{
NSPoint ptInPlot;
ptInPlot = [invertedTransfer transformPoint:mousePositionInView];
if(ptInPlot.x>0 && ptInPlot.x<xRightLimit && ptInPlot.y<yTopLimit)
{
if( mouseStartDraggingFlag==1)
endPtX=curPtX=ptInPlot.x;
else if( mouseStartDraggingFlag==2)
{
curPtX=startDragPoint.y+ptInPlot.x-startDragPoint.x;
}
[viewControllor syncWithPlot];
}
}
[self setNeedsDisplay:YES];
}
[super mouseDragged:theEvent];
}
- (void)mouseMoved:(NSEvent *)theEvent
{
if(currentCurve && [currentCurve count]>0)
{
NSPoint mousePositionInWindow = [theEvent locationInWindow];
NSPoint mousePositionInView = [self convertPoint:mousePositionInWindow fromView:nil];
NSPoint ptInPlot;
ptInPlot = [invertedTransfer transformPoint:mousePositionInView];
if(ptInPlot.x>0 && ptInPlot.y>0 && ptInPlot.x<xRightLimit && ptInPlot.y<yTopLimit)
{
curseLabelFlag=1;
unsigned int xindex=ptInPlot.x/xUnit;
float y;
if(xindex+1>=[currentCurve count])
y=[[currentCurve lastObject] floatValue];
else
{
y=[[currentCurve objectAtIndex:xindex] floatValue]+([[currentCurve objectAtIndex:xindex+1] floatValue]-[[currentCurve objectAtIndex:xindex] floatValue])*(ptInPlot.x-(float)xindex*xUnit)/xUnit;
}
[self setCursorLabelWithText:[NSString stringWithFormat:@"x: %.1fmm, y:%.2f", ptInPlot.x, y]];
mousePtX=ptInPlot.x;
[self setNeedsDisplay:YES];
}
else
{
if(curseLabelFlag==1)
{
curseLabelFlag=0;
[self setCursorLabelWithText:@""];
mousePtX=0;
[self setNeedsDisplay:YES];
}
}
}
[super mouseMoved:theEvent];
}
- (void)setCursorLabelWithText:(NSString*)text
{
if([text isEqualToString:@""])
{
[[NSCursor arrowCursor] set];
return;
}
NSPoint hotSpot = [[NSCursor arrowCursor] hotSpot];
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithCapacity:3];
[attrsDictionary setObject:textLabelColor forKey:NSForegroundColorAttributeName];
NSAttributedString *label = [[[NSAttributedString alloc] initWithString:text attributes:attrsDictionary] autorelease];
// NSRect labelBounds = [label boundingRectWithSize:[self bounds].size options:NSStringDrawingUsesDeviceMetrics];
NSRect labelBounds = [label boundingRectWithSize:viewFrame.size options:NSStringDrawingUsesDeviceMetrics];
NSSize imageSize = [[[NSCursor arrowCursor] image] size];
float arrowWidth = imageSize.width;
imageSize.width += labelBounds.size.width;
NSImage *cursorImage = [[NSImage alloc] initWithSize: imageSize];
NSPoint labelPosition = NSMakePoint(arrowWidth-6, .0);
// draw
[cursorImage lockFocus];
[[[NSCursor arrowCursor] image] drawAtPoint: NSMakePoint( 0, 0) fromRect: NSZeroRect operation: NSCompositeCopy fraction: 1.0];
[[[NSColor blackColor] colorWithAlphaComponent:0.5] set];
//NSRectFill(NSMakeRect(labelPosition.x-2, labelPosition.y+1, labelBounds.size.width+4, labelBounds.size.height+4));
NSRectFill(NSMakeRect(labelPosition.x-2, labelPosition.y+1, labelBounds.size.width+4, 13)); // nicer if the height stays the same when moving the mouse
[label drawAtPoint:labelPosition];
[cursorImage unlockFocus];
NSCursor *cursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:hotSpot];
[cursor set];
[cursorImage release];
[cursor release];
}
#pragma mark - 4. outlet for controllor
- (void)setViewControllor:(id)controllor
{
viewControllor=controllor;
}
- (void)setACurve:(NSString*)name
:(NSArray*)curve
:(NSColor*)color
:(float)xu
:(float)yu
{
if (currentCurve)
[currentCurve release];
currentCurve=curve;
[currentCurve retain];
[curveColor release];
curveColor = color;
[curveColor retain];
xUnit=xu;
yUnit=yu;
xRightLimit=[currentCurve count]*xUnit*1.05;
unsigned i;
yTopLimit=[[currentCurve objectAtIndex:0] floatValue];
for (i=0;i<[currentCurve count];i++)
{
if (yTopLimit<[[currentCurve objectAtIndex:i] floatValue])
yTopLimit=[[currentCurve objectAtIndex:i] floatValue];
}
yTopLimit=yTopLimit*1.2;
[globalTransform release];
globalTransform=nil;
[self setNeedsDisplay:YES];
}
- (void)removeCurCurve
{
if(currentCurve)
[currentCurve release];
currentCurve=nil;
curveColor = [NSColor whiteColor];
xUnit=1.0;
yUnit=1.0;
xScaleFactor=1.0;
yScaleFactor=1.0;
[self setNeedsDisplay:YES];
}
@end