forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROI.m
7346 lines (5932 loc) · 257 KB
/
ROI.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 "AppController.h"
#import "StringTexture.h"
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#include <OpenGL/glu.h>
#import "ROI.h"
#import "DCMView.h"
#import "DCMPix.h"
#import "ITKSegmentation3D.h"
#import "Notifications.h"
#import "N2Debug.h"
#import "ITKBrushROIFilter.h"
#import "DCMUSRegion.h" // mapping ultrason
#define CIRCLERESOLUTION 200
#define ROIVERSION 11
static float deg2rad = M_PI / 180.0f;
static float fontHeight = 0;
static NSString *defaultName;
static int gUID = 0;
extern long BresLine(int Ax, int Ay, int Bx, int By,long **xBuffer, long **yBuffer);
static float ROIRegionOpacity, ROITextThickness, ROIThickness, ROIOpacity, ROIColorR, ROIColorG, ROIColorB, ROITextColorR, ROITextColorG, ROITextColorB;
static float ROIRegionThickness, ROIRegionColorR, ROIRegionColorG, ROIRegionColorB, ROIArrowThickness;
static BOOL ROITEXTIFSELECTED, ROITEXTNAMEONLY, ROITextIfMouseIsOver, ROIDrawPlainEdge;
static BOOL ROIDefaultsLoaded = NO;
static BOOL splineForROI = NO;
static BOOL displayCobbAngle = NO;
int spline( NSPoint *Pt, int tot, NSPoint **newPt, long **correspondingSegmentPt, double scale)
{
NSPoint p1, p2;
long long i, j;
double xi, yi;
long long nb;
double *px, *py;
int ok;
double *a, b, *c, *cx, *cy, *d, *g, *h;
double bet, *gam;
double aax, bbx, ccx, ddx, aay, bby, ccy, ddy; // coef of spline
if( scale > 5) scale = 5;
// function spline S(x) = a x3 + bx2 + cx + d
// with S continue, S1 continue, S2 continue.
// smoothing of a closed polygon given by a list of points (x,y)
// we compute a spline for x and a spline for y
// where x and y are function of d where t is the distance between points
// compute tridiag matrix
// | b1 c1 0 ... | | u1 | | r1 |
// | a2 b2 c2 0 ... | | u2 | | r2 |
// | 0 a3 b3 c3 0 ... | * | ... | = | ... |
// | ... | | ... | | ... |
// | an-1 bn-1 cn-1 | | ... | | ... |
// | 0 an bn | | un | | rn |
// bi = 4
// resolution algorithm is taken from the book : Numerical recipes in C
// initialization of different vectors
// element number 0 is not used (except h[0])
nb = tot + 2;
a = malloc(nb*sizeof(double));
c = malloc(nb*sizeof(double));
cx = malloc(nb*sizeof(double));
cy = malloc(nb*sizeof(double));
d = malloc(nb*sizeof(double));
g = malloc(nb*sizeof(double));
gam = malloc(nb*sizeof(double));
h = malloc(nb*sizeof(double));
px = malloc(nb*sizeof(double));
py = malloc(nb*sizeof(double));
BOOL failed = NO;
if( !a) failed = YES;
if( !c) failed = YES;
if( !cx) failed = YES;
if( !cy) failed = YES;
if( !d) failed = YES;
if( !g) failed = YES;
if( !gam) failed = YES;
if( !h) failed = YES;
if( !px) failed = YES;
if( !py) failed = YES;
if( failed)
{
if( a) free(a);
if( c) free(c);
if( cx) free(cx);
if( cy) free(cy);
if( d) free(d);
if( g) free(g);
if( gam) free(gam);
if( h) free(h);
if( px) free(px);
if( py) free(py);
return 0;
}
//initialisation
for (i=0; i<nb; i++)
h[i] = a[i] = cx[i] = d[i] = c[i] = cy[i] = g[i] = gam[i] = 0.0;
// as a spline starts and ends with a line one adds two points
// in order to have continuity in starting point
for (i=0; i<tot; i++)
{
px[i+1] = Pt[i].x;// * fZoom / 100;
py[i+1] = Pt[i].y;// * fZoom / 100;
}
px[0] = px[nb-3]; px[nb-1] = px[2];
py[0] = py[nb-3]; py[nb-1] = py[2];
// check all points are separate, if not do not smooth
// this happens when the zoom factor is too small
// so in this case the smooth is not useful
ok=TRUE;
if(nb<3) ok=FALSE;
for (i=1; i<nb; i++)
if (px[i] == px[i-1] && py[i] == py[i-1]) {ok = FALSE; break;}
if (ok == FALSE)
failed = YES;
if( failed)
{
if( !a) free(a);
if( !c) free(c);
if( !cx) free(cx);
if( !cy) free(cy);
if( !d) free(d);
if( !g) free(g);
if( !gam) free(gam);
if( !h) free(h);
if( !px) free(px);
if( !py) free(py);
return 0;
}
// define hi (distance between points) h0 distance between 0 and 1.
// di distance of point i from start point
for (i = 0; i<nb-1; i++)
{
xi = px[i+1] - px[i];
yi = py[i+1] - py[i];
h[i] = (double) sqrt(xi*xi + yi*yi) * scale;
d[i+1] = d[i] + h[i];
}
// define ai and ci
for (i=2; i<nb-1; i++) a[i] = 2.0 * h[i-1] / (h[i] + h[i-1]);
for (i=1; i<nb-2; i++) c[i] = 2.0 * h[i] / (h[i] + h[i-1]);
// define gi in function of x
// gi+1 = 6 * Y[hi, hi+1, hi+2],
// Y[hi, hi+1, hi+2] = [(yi - yi+1)/(di - di+1) - (yi+1 - yi+2)/(di+1 - di+2)]
// / (di - di+2)
for (i=1; i<nb-1; i++)
g[i] = 6.0 * ( ((px[i-1] - px[i]) / (d[i-1] - d[i])) - ((px[i] - px[i+1]) / (d[i] - d[i+1])) ) / (d[i-1]-d[i+1]);
// compute cx vector
b=4; bet=4;
cx[1] = g[1]/b;
for (j=2; j<nb-1; j++)
{
gam[j] = c[j-1] / bet;
bet = b - a[j] * gam[j];
cx[j] = (g[j] - a[j] * cx[j-1]) / bet;
}
for (j=(nb-2); j>=1; j--) cx[j] -= gam[j+1] * cx[j+1];
// define gi in function of y
// gi+1 = 6 * Y[hi, hi+1, hi+2],
// Y[hi, hi+1, hi+2] = [(yi - yi+1)/(hi - hi+1) - (yi+1 - yi+2)/(hi+1 - hi+2)]
// / (hi - hi+2)
for (i=1; i<nb-1; i++)
g[i] = 6.0 * ( ((py[i-1] - py[i]) / (d[i-1] - d[i])) - ((py[i] - py[i+1]) / (d[i] - d[i+1])) ) / (d[i-1]-d[i+1]);
// compute cy vector
b = 4.0; bet = 4.0;
cy[1] = g[1] / b;
for (j=2; j<nb-1; j++)
{
gam[j] = c[j-1] / bet;
bet = b - a[j] * gam[j];
cy[j] = (g[j] - a[j] * cy[j-1]) / bet;
}
for (j=(nb-2); j>=1; j--) cy[j] -= gam[j+1] * cy[j+1];
// OK we have the cx and cy vectors, from that we can compute the
// coeff of the polynoms for x and y and for each interval
// S(x) (xi, xi+1) = ai + bi (x-xi) + ci (x-xi)2 + di (x-xi)3
// di = (ci+1 - ci) / 3 hi
// ai = yi
// bi = ((ai+1 - ai) / hi) - (hi/3) (ci+1 + 2 ci)
int totNewPt = 0;
for (i=1; i<nb-2; i++)
{
totNewPt++;
for (j = 1; j <= h[i]; j++) totNewPt++;
}
*newPt = calloc(totNewPt, sizeof(NSPoint));
if( newPt == nil)
{
if( !a) free(a);
if( !c) free(c);
if( !cx) free(cx);
if( !cy) free(cy);
if( !d) free(d);
if( !g) free(g);
if( !gam) free(gam);
if( !h) free(h);
if( !px) free(px);
if( !py) free(py);
return 0;
}
if( correspondingSegmentPt)
{
*correspondingSegmentPt = calloc(totNewPt, sizeof(long));
if( *correspondingSegmentPt == nil)
{
free( newPt);
if( !a) free(a);
if( !c) free(c);
if( !cx) free(cx);
if( !cy) free(cy);
if( !d) free(d);
if( !g) free(g);
if( !gam) free(gam);
if( !h) free(h);
if( !px) free(px);
if( !py) free(py);
return 0;
}
}
int tt = 0;
// for each interval
for (i=1; i<nb-2; i++)
{
// compute coef for x polynom
ccx = cx[i];
aax = px[i];
ddx = (cx[i+1] - cx[i]) / (3.0 * h[i]);
bbx = ((px[i+1] - px[i]) / h[i]) - (h[i] / 3.0) * (cx[i+1] + 2.0 * cx[i]);
// compute coef for y polynom
ccy = cy[i];
aay = py[i];
ddy = (cy[i+1] - cy[i]) / (3.0 * h[i]);
bby = ((py[i+1] - py[i]) / h[i]) - (h[i] / 3.0) * (cy[i+1] + 2.0 * cy[i]);
// compute points in this interval and display
p1.x = aax;
p1.y = aay;
(*newPt)[tt]=p1;
if( correspondingSegmentPt)
(*correspondingSegmentPt)[tt]=i-1;
tt++;
for (j = 1; j <= h[i]; j++)
{
p2.x = (aax + bbx * (double)j + ccx * (double)(j * j) + ddx * (double)(j * j * j));
p2.y = (aay + bby * (double)j + ccy * (double)(j * j) + ddy * (double)(j * j * j));
(*newPt)[tt]=p2;
if( correspondingSegmentPt)
(*correspondingSegmentPt)[tt]=i-1;
tt++;
}//endfor points in 1 interval
}//endfor each interval
// delete dynamic structures
free(a);
free(c);
free(cx);
free(cy);
free(d);
free(g);
free(gam);
free(h);
free(px);
free(py);
return tt;
}
@implementation ROI
@synthesize min = rmin, max = rmax, mean = rmean;
@synthesize textureWidth, textureHeight, textureBuffer, locked, selectable, isAliased, originalIndexForAlias, imageOrigin, pixelSpacingX, pixelSpacingY;
@synthesize textureDownRightCornerX,textureDownRightCornerY, textureUpLeftCornerX, textureUpLeftCornerY;
@synthesize opacity, hidden;
@synthesize name, comments, type, ROImode = mode, thickness;
@synthesize zPositions;
@synthesize clickInTextBox;
@synthesize rect;
@synthesize pix, displayCMOrPixels;
@synthesize curView;
@synthesize mousePosMeasure;
@synthesize rgbcolor = color;
@synthesize parentROI;
@synthesize displayCalciumScoring = _displayCalciumScoring, calciumThreshold = _calciumThreshold;
@synthesize sliceThickness = _sliceThickness;
@synthesize layerReferenceFilePath;
@synthesize layerImage;
@synthesize layerPixelSpacingX, layerPixelSpacingY;
@synthesize textualBoxLine1, textualBoxLine2, textualBoxLine3, textualBoxLine4, textualBoxLine5, textualBoxLine6;
@synthesize groupID, mouseOverROI;
@synthesize isLayerOpacityConstant, canColorizeLayer, displayTextualData, clickPoint;
+(void) setFontHeight: (float) f
{
fontHeight = f;
}
+(void) saveDefaultSettings
{
if( ROIDefaultsLoaded)
{
[[NSUserDefaults standardUserDefaults] setFloat: ROIRegionOpacity forKey: @"ROIRegionOpacity"];
[[NSUserDefaults standardUserDefaults] setFloat: ROITextThickness forKey: @"ROITextThickness"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIThickness forKey: @"ROIThickness"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIOpacity forKey: @"ROIOpacity"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIColorR forKey: @"ROIColorR"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIColorG forKey: @"ROIColorG"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIColorB forKey: @"ROIColorB"];
[[NSUserDefaults standardUserDefaults] setFloat: ROITextColorR forKey: @"ROITextColorR"];
[[NSUserDefaults standardUserDefaults] setFloat: ROITextColorG forKey: @"ROITextColorG"];
[[NSUserDefaults standardUserDefaults] setFloat: ROITextColorB forKey: @"ROITextColorB"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIRegionColorR forKey: @"ROIRegionColorR"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIRegionColorG forKey: @"ROIRegionColorG"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIRegionColorB forKey: @"ROIRegionColorB"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIRegionThickness forKey: @"ROIRegionThickness"];
[[NSUserDefaults standardUserDefaults] setFloat: ROIArrowThickness forKey: @"ROIArrowThickness"];
}
}
+(void) loadDefaultSettings
{
ROIRegionOpacity = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIRegionOpacity"];
if( ROIRegionOpacity < 0.3) ROIRegionOpacity = 0.3;
ROITextThickness = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROITextThickness"];
ROIThickness = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIThickness"];
if( ROIThickness < 0.3) ROIThickness = 0.3;
ROIOpacity = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIOpacity"];
if( ROIOpacity < 0.3) ROIOpacity = 0.3;
ROIColorR = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIColorR"];
ROIColorG = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIColorG"];
ROIColorB = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIColorB"];
ROITextColorR = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROITextColorR"];
ROITextColorG = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROITextColorG"];
ROITextColorB = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROITextColorB"];
ROIRegionColorR = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIRegionColorR"];
ROIRegionColorG = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIRegionColorG"];
ROIRegionColorB = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIRegionColorB"];
ROIRegionThickness = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIRegionThickness"];
ROIArrowThickness = [[NSUserDefaults standardUserDefaults] floatForKey: @"ROIArrowThickness"];
ROITEXTIFSELECTED = [[NSUserDefaults standardUserDefaults] boolForKey: @"ROITEXTIFSELECTED"];
ROITextIfMouseIsOver = [NSUserDefaults.standardUserDefaults boolForKey: @"ROITextIfMouseIsOver"];
ROITEXTNAMEONLY = [[NSUserDefaults standardUserDefaults] boolForKey: @"ROITEXTNAMEONLY"];
splineForROI = [[NSUserDefaults standardUserDefaults] boolForKey: @"splineForROI"];
displayCobbAngle = [[NSUserDefaults standardUserDefaults] boolForKey: @"displayCobbAngle"];
ROIDrawPlainEdge = [[NSUserDefaults standardUserDefaults] boolForKey: @"ROIDrawPlainEdge"];
ROIDefaultsLoaded = YES;
}
+ (BOOL) splineForROI
{
return splineForROI;
}
-(void)setIsSpline:(BOOL)isSpline {
_isSpline = isSpline;
_hasIsSpline = YES;
}
-(BOOL)isSpline {
return _hasIsSpline? _isSpline : [ROI splineForROI];
}
+(void) setDefaultName:(NSString*) n
{
[defaultName release];
if ( n == nil ) {
defaultName = nil;
return;
}
defaultName = [[[NSString alloc] initWithString: n] retain];
}
+(NSString*) defaultName {
return defaultName;
}
+ (NSPoint) pointBetweenPoint:(NSPoint) a and:(NSPoint) b ratio: (float) r
{
return NSMakePoint(a.x*(1.0-r)+b.x*r, a.y*(1.0-r)+b.y*r);
}
+ (float) lengthBetween:(NSPoint) mesureA and :(NSPoint) mesureB
{
return sqrtf((mesureA.x-mesureB.x)*(mesureA.x-mesureB.x)+(mesureA.y-mesureB.y)*(mesureA.y-mesureB.y));
}
+ (NSPoint) segmentDistToPoint: (NSPoint) segA :(NSPoint) segB :(NSPoint) p
{
NSPoint p2 = NSMakePoint(segB.x - segA.x, segB.y - segA.y);
float f = p2.x*p2.x + p2.y*p2.y;
float u = ((p.x - segA.x) * p2.x + (p.y - segA.y) * p2.y) / f;
// if (u > 1)
// u = 1;
// else if (u < 0)
// u = 0;
float x = segA.x + u * p2.x;
float y = segA.y + u * p2.y;
// float dx = x - p.x;
// float dy = y - p.y;
//
// float length = sqrtf(dx*dx + dy*dy);
return NSMakePoint( x, y);
}
+(NSPoint) positionAtDistance: (float) distance inPolygon:(NSArray*) points
{
int i = 0;
float position = 0, ratio;
NSPoint p;
if( [points count] == 0)
return NSMakePoint( 0, 0);
if( distance == 0) return [[points objectAtIndex:0] point];
while( position < distance && i < (long)[points count] -1)
{
position += [ROI lengthBetween:[[points objectAtIndex:i] point] and:[[points objectAtIndex:i+1] point]];
i++;
}
if( position < distance)
{
position += [ROI lengthBetween:[[points objectAtIndex:i] point] and:[[points objectAtIndex:0] point]];
i++;
}
if( i == [points count])
{
ratio = (position - distance) / [ROI lengthBetween: [[points objectAtIndex:i-1] point] and:[[points objectAtIndex: 0] point]];
p = [ROI pointBetweenPoint:[[points objectAtIndex:i-1] point] and:[[points objectAtIndex:0] point] ratio: 1.0 - ratio];
}
else
{
ratio = (position - distance) / [ROI lengthBetween: [[points objectAtIndex:i-1] point] and:[[points objectAtIndex:i] point]];
p = [ROI pointBetweenPoint:[[points objectAtIndex:i-1] point] and:[[points objectAtIndex:i] point] ratio: 1.0 - ratio];
}
return p;
}
+(NSMutableArray*) resamplePoints: (NSArray*) points number:(int) no
{
float length = 0.0f;
int i;
for( i = 0; i < (long)[points count]-1; i++ )
{
length += [ROI lengthBetween:[[points objectAtIndex:i] point] and:[[points objectAtIndex:i+1] point]];
}
length += [ROI lengthBetween:[[points objectAtIndex:i] point] and:[[points objectAtIndex:0] point]];
NSMutableArray* newPts = [NSMutableArray array];
for( int i = 0; i < no; i++) {
float s = (i * length) / no;
NSPoint p = [ROI positionAtDistance: s inPolygon: points];
[newPts addObject: [MyPoint point: p]];
}
float minx = [[newPts objectAtIndex: 0] x];
float miny = [[newPts objectAtIndex: 0] y];
int minyIndex = 0, minxIndex = 0;
//find min x - reorder the points
for( int i = 0 ; i < [newPts count] ; i++) {
if( minx > [[newPts objectAtIndex: i] x])
{
minx = [[newPts objectAtIndex: i] x];
minxIndex = i;
}
if( miny > [[newPts objectAtIndex: i] y])
{
miny = [[newPts objectAtIndex: i] y];
minyIndex = i;
}
}
BOOL reverse = NO;
int distance = 0;
distance = minxIndex - minyIndex;
if( fabs( distance) > [newPts count]/2)
{
if( distance >= 0) reverse = YES;
else reverse = NO;
}
else
{
if( distance >= 0) reverse = NO;
else reverse = YES;
}
NSMutableArray* orderedPts = [NSMutableArray array];
if( reverse == NO )
{
for( int i = 0 ; i < [newPts count] ; i++) {
[orderedPts addObject: [newPts objectAtIndex: minxIndex]];
minxIndex++;
if( minxIndex == [newPts count]) minxIndex = 0;
}
}
else
{
for( int i = 0 ; i < [newPts count] ; i++) {
[orderedPts addObject: [newPts objectAtIndex: minxIndex]];
minxIndex--;
if( minxIndex < 0) minxIndex = (long)[newPts count] -1;
}
}
return orderedPts;
}
- (BOOL) isValidForVolume
{
if( type == tCPolygon || type == tOPolygon || type == tPlain || type == tPencil || type == tOval)
return YES;
else
return NO;
}
-(void) setDefaultName:(NSString*) n { [ROI setDefaultName: n]; }
-(NSString*) defaultName { return defaultName; }
// --- tPlain functions
-(void)displayTexture
{
printf( "-*- DISPLAY ROI TEXTURE -*-\n" );
for ( int j=0; j<textureHeight; j++ ) {
for( int i=0; i<textureWidth; i++ )
printf( "%d ",textureBuffer[i+j*textureWidth] );
printf("\n");
}
}
- (void) setOpacity:(float) a
{
[self setOpacity: a globally: YES];
}
- (void) setOpacity:(float)newOpacity globally: (BOOL) g
{
opacity = newOpacity;
if( type == tPlain)
{
if( g)
ROIRegionOpacity = opacity;
}
else if(type == tLayerROI)
{
while( [ctxArray count]) [self deleteTexture: [ctxArray lastObject]];
}
else if( g)
ROIOpacity = opacity;
}
- (DCMPix*)pix
{
if( pix)
{
return pix;
}
else
{
NSLog( @"----- warning pix == [curView curDCM]");
return pix = [curView.curDCM retain];
}
}
- (id) initWithCoder:(NSCoder*) coder
{
long fileVersion;
if( self = [super init])
{
uniqueID = [[NSNumber numberWithInt: gUID++] retain];
groupID = 0.0;
PointUnderMouse = -1;
selectedModifyPoint = -1;
fileVersion = [coder versionForClassName: @"ROI"];
parentROI = nil;
points = [coder decodeObject];
rect = NSRectFromString( [coder decodeObject]);
type = [[coder decodeObject] floatValue];
needQuartz = [[coder decodeObject] floatValue];
thickness = [[coder decodeObject] floatValue];
fill = [[coder decodeObject] floatValue];
opacity = [[coder decodeObject] floatValue];
color.red = [[coder decodeObject] floatValue];
color.green = [[coder decodeObject] floatValue];
color.blue = [[coder decodeObject] floatValue];
name = [coder decodeObject];
comments = [coder decodeObject];
pixelSpacingX = [[coder decodeObject] floatValue];
imageOrigin = NSPointFromString( [coder decodeObject]);
if( fileVersion >= 2)
{
pixelSpacingY = [[coder decodeObject] floatValue];
}
else pixelSpacingY = pixelSpacingX;
if (type == tPlain)
{
textureWidth = [[coder decodeObject] intValue];
[[coder decodeObject] intValue]; // Keep it for backward compatibility & compatibility with encoder
textureHeight = [[coder decodeObject] intValue];
[[coder decodeObject] intValue]; // Keep it for backward compatibility & compatibility with encoder
textureUpLeftCornerX = [[coder decodeObject] intValue];
textureUpLeftCornerY = [[coder decodeObject] intValue];
textureDownRightCornerX = [[coder decodeObject] intValue];
textureDownRightCornerY = [[coder decodeObject] intValue];
textureBuffer=(unsigned char*)malloc(textureWidth*textureHeight*sizeof(unsigned char));
@try
{
unsigned char* pointerBuff=(unsigned char*)[[coder decodeObject] bytes];
for( int j=0; j<textureHeight; j++ )
{
for( int i=0; i<textureWidth; i++ )
textureBuffer[i+j*textureWidth]=pointerBuff[i+j*textureWidth];
}
}
@catch (NSException * e)
{
}
}
if( fileVersion >= 3)
{
zPositions = [coder decodeObject];
}
else zPositions = [[NSMutableArray array] retain];
if( fileVersion >= 4)
{
offsetTextBox_x = [[coder decodeObject] floatValue];
offsetTextBox_y = [[coder decodeObject] floatValue];
}
else
{
offsetTextBox_x = 0;
offsetTextBox_y = 0;
}
if (fileVersion >= 5) {
_calciumThreshold = [[coder decodeObject] intValue];
_displayCalciumScoring = [[coder decodeObject] boolValue];
}
if (fileVersion >= 6)
{
groupID = [[coder decodeObject] doubleValue];
if (type==tLayerROI)
{
layerImageJPEG = [coder decodeObject];
[layerImageJPEG retain];
// layerImageWhenSelectedJPEG = [coder decodeObject];
// [layerImageWhenSelectedJPEG retain];
layerImage = [[NSImage alloc] initWithData: layerImageJPEG];
// layerImageWhenSelected = [[NSImage alloc] initWithData: layerImageWhenSelectedJPEG];
while( [ctxArray count]) [self deleteTexture: [ctxArray lastObject]];
//needsLoadTexture2 = YES;
}
textualBoxLine1 = [coder decodeObject];
textualBoxLine2 = [coder decodeObject];
textualBoxLine3 = [coder decodeObject];
textualBoxLine4 = [coder decodeObject];
textualBoxLine5 = [coder decodeObject];
[textualBoxLine1 retain];
[textualBoxLine2 retain];
[textualBoxLine3 retain];
[textualBoxLine4 retain];
[textualBoxLine5 retain];
}
if (fileVersion >= 7)
{
isLayerOpacityConstant = [[coder decodeObject] boolValue];
canColorizeLayer = [[coder decodeObject] boolValue];
layerColor = [coder decodeObject];
if(layerColor)[layerColor retain];
displayTextualData = [[coder decodeObject] boolValue];
}
else displayTextualData = YES;
if (fileVersion >= 8)
{
canResizeLayer = [[coder decodeObject] boolValue];
}
if( fileVersion >= 9)
{
selectable = [[coder decodeObject] boolValue];
locked = [[coder decodeObject] boolValue];
}
else
{
selectable = YES;
locked = NO;
}
if( fileVersion >= 10)
{
isAliased = [[coder decodeObject] boolValue];
}
else
{
isAliased = NO;
}
if( fileVersion >= 11)
{
_isSpline = [[coder decodeObject] boolValue];
_hasIsSpline = [[coder decodeObject] boolValue];
}
else
{
_isSpline = NO;
_hasIsSpline = NO;
}
[points retain];
[name retain];
[comments retain];
[zPositions retain];
mode = ROI_sleep;
previousPoint.x = previousPoint.y = -1000;
stringTex = nil;
rmean = rmax = rmin = rdev = rtotal = -1;
Brmean = Brmax = Brmin = Brdev = Brtotal = -1;
mousePosMeasure = -1;
ctxArray = [[NSMutableArray arrayWithCapacity: 10] retain];
textArray = [[NSMutableArray arrayWithCapacity: 10] retain];
// init fonts for use with strings
NSFont * font =[NSFont fontWithName:@"Helvetica" size: 12.0 + thickness*2];
stanStringAttrib = [[NSMutableDictionary dictionary] retain];
[stanStringAttrib setObject:font forKey:NSFontAttributeName];
[stanStringAttrib setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
[self reduceTextureIfPossible];
}
[[NSNotificationCenter defaultCenter] postNotificationName: OsirixROIChangeNotification object:self userInfo: nil];
return self;
}
- (id) copyWithZone:(NSZone *)zone
{
ROI *c = [[[self class] allocWithZone: zone] init];
if( c == nil) return nil;
c->uniqueID = [[NSNumber numberWithInt: gUID++] retain];
c->groupID = 0.0;
c->PointUnderMouse = -1;
c->selectedModifyPoint = -1;
c->parentROI = nil;
NSMutableArray *a = [[NSMutableArray array] retain];
for( MyPoint *p in points)
[a addObject: [[p copy] autorelease]];
c->points = a;
c->rect = rect;
c->type = type;
c->needQuartz = needQuartz;
c->thickness = thickness;
c->fill = fill;
c->opacity = opacity;
c->color = color;
c->name = [name copy];
c->comments = [comments copy];
c->pixelSpacingX = pixelSpacingX;
c->imageOrigin = imageOrigin;
c->pixelSpacingY = pixelSpacingY;
if (c->type == tPlain)
{
c->textureWidth = textureWidth;
c->textureHeight = textureHeight;
c->textureUpLeftCornerX = textureUpLeftCornerX;
c->textureUpLeftCornerY = textureUpLeftCornerY;
c->textureDownRightCornerX = textureDownRightCornerX;
c->textureDownRightCornerY = textureDownRightCornerY;
c->textureBuffer = (unsigned char*) malloc( textureWidth*textureHeight*sizeof(unsigned char));
if( c->textureBuffer == nil)
{
[c autorelease];
return nil;
}
if( c->textureBuffer && textureBuffer)
memcpy( c->textureBuffer, textureBuffer, textureWidth*textureHeight*sizeof(unsigned char));
}
NSMutableArray *z = [[NSMutableArray array] retain];
for( NSNumber *p in zPositions)
[z addObject: [[p copy] autorelease]];
c->zPositions = z;
c->offsetTextBox_x = offsetTextBox_x;
c->offsetTextBox_y = offsetTextBox_y;
c->_calciumThreshold = _calciumThreshold;
c->_displayCalciumScoring = _displayCalciumScoring;
c->groupID = groupID;
if (c->type == tLayerROI)
{
c->layerImageJPEG = [layerImageJPEG copy];
c->layerImage = [[NSImage alloc] initWithData: c->layerImageJPEG];
if( c->layerImage == nil)
return nil;
while( [ctxArray count]) [self deleteTexture: [ctxArray lastObject]];
}
c->textualBoxLine1 = [textualBoxLine1 copy];
c->textualBoxLine2 = [textualBoxLine2 copy];
c->textualBoxLine3 = [textualBoxLine3 copy];
c->textualBoxLine4 = [textualBoxLine4 copy];
c->textualBoxLine5 = [textualBoxLine5 copy];
c->isLayerOpacityConstant = isLayerOpacityConstant;
c->canColorizeLayer = canColorizeLayer;
c->layerColor = [layerColor copy];
c->displayTextualData = displayTextualData;
c->canResizeLayer = canResizeLayer;
c->selectable = selectable;
c->locked = locked;
c->isAliased = isAliased;
c->mode = ROI_sleep;
c->previousPoint.x = c->previousPoint.y = -1000;
c->stringTex = nil;
c->rmean = c->rmax = c->rmin = c->rdev = c->rtotal = -1;
c->Brmean = c->Brmax = c->Brmin = c->Brdev = c->Brtotal = -1;
c->mousePosMeasure = -1;
c->ctxArray = [[NSMutableArray arrayWithCapacity: 10] retain];
c->textArray = [[NSMutableArray arrayWithCapacity: 10] retain];
// init fonts for use with strings
NSFont * font =[NSFont fontWithName:@"Helvetica" size: 12.0 + c->thickness*2];
c->stanStringAttrib = [[NSMutableDictionary dictionary] retain];
[c->stanStringAttrib setObject:font forKey:NSFontAttributeName];
[c->stanStringAttrib setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
[c reduceTextureIfPossible];
c->_hasIsSpline = _hasIsSpline;
c->_isSpline = _isSpline;
return c;
}
- (void) encodeWithCoder:(NSCoder*) coder
{
[ROI setVersion:ROIVERSION];
[coder encodeObject:points];
[coder encodeObject:NSStringFromRect(rect)];
[coder encodeObject:[NSNumber numberWithFloat:type]];
[coder encodeObject:[NSNumber numberWithFloat:needQuartz]];
[coder encodeObject:[NSNumber numberWithFloat:thickness]];
[coder encodeObject:[NSNumber numberWithFloat:fill]];
[coder encodeObject:[NSNumber numberWithFloat:opacity]];
[coder encodeObject:[NSNumber numberWithFloat:color.red]];
[coder encodeObject:[NSNumber numberWithFloat:color.green]];
[coder encodeObject:[NSNumber numberWithFloat:color.blue]];
[coder encodeObject:name];
[coder encodeObject:comments];
[coder encodeObject:[NSNumber numberWithFloat:pixelSpacingX]];
[coder encodeObject:NSStringFromPoint(imageOrigin)];
[coder encodeObject:[NSNumber numberWithFloat:pixelSpacingY]];
if (type==tPlain)
{
[coder encodeObject:[NSNumber numberWithInt:textureWidth]];
[coder encodeObject:[NSNumber numberWithInt:0]];
[coder encodeObject:[NSNumber numberWithInt:textureHeight]];
[coder encodeObject:[NSNumber numberWithInt:0]];
[coder encodeObject:[NSNumber numberWithInt:textureUpLeftCornerX]];
[coder encodeObject:[NSNumber numberWithInt:textureUpLeftCornerY]];
[coder encodeObject:[NSNumber numberWithInt:textureDownRightCornerX]];
[coder encodeObject:[NSNumber numberWithInt:textureDownRightCornerY]];
[coder encodeObject:[NSData dataWithBytes:textureBuffer length:(textureWidth*textureHeight)]];
}
[coder encodeObject:zPositions];
[coder encodeObject:[NSNumber numberWithFloat:offsetTextBox_x]];
[coder encodeObject:[NSNumber numberWithFloat:offsetTextBox_y]];
[coder encodeObject:[NSNumber numberWithInt:_calciumThreshold]];
[coder encodeObject:[NSNumber numberWithBool:_displayCalciumScoring]];
// ROIVERSION = 6
[coder encodeObject:[NSNumber numberWithDouble:groupID]];
if (type==tLayerROI)
{
if( layerImageJPEG == nil)
{
// NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData: [layerImage TIFFRepresentation]];
// NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.3] forKey:NSImageCompressionFactor];
//
// layerImageJPEG = [[imageRep representationUsingType:NSJPEG2000FileType properties:imageProps] retain]; //NSJPEGFileType
[self generateEncodedLayerImage];
}
// if( layerImageWhenSelectedJPEG == nil)
// {
// NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData: [layerImage TIFFRepresentation]];
// NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.3] forKey:NSImageCompressionFactor];