This repository has been archived by the owner on Sep 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDicomToEgsphantFilter.m
244 lines (202 loc) · 7.57 KB
/
DicomToEgsphantFilter.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
//
// DicomToEgsphantFilter.m
// DicomToEgsphant
//
// Copyright (c) 2007 Wang. All rights reserved.
//
#import "DicomToEgsphantFilter.h"
@implementation DicomToEgsphantFilter
// medium (CTmax-CTmin)/(max density - min density)
// ------ --------------------------------------
// AIR700ICRU (50-0)/(0.044-0.001)
// LUNG700ICRU (300-50)/(0.302-0.044)
// ICRUTISSUE700ICRU (1125-300)/(1.101-0.302)
// ICRPBONE700ICRU (3000-1125)/(2.088-1.101)
-(int) ctToMediumNumber:(float) ctval
{
float ctlb, ctub;
int i;
for (i=0; i<[[self mediums] count]; i++)
{
ctlb = [[[[[self mediums] objectAtIndex:i] properties] objectForKey:@"ctlb"] floatValue];
ctub = [[[[[self mediums] objectAtIndex:i] properties] objectForKey:@"ctub"] floatValue];
if ((ctval>=ctlb) && (ctval<ctub))
return i+1;
}
// out of boundary
return 0;
}
-(float) ctToMediumDensity:(float) ctval
{
int mediumNum;
float ctlb, ctub, denlb, denub;
mediumNum = [self ctToMediumNumber:ctval];
// since medium number start at 1, object index will be medium number - 1
ctlb = [[[[[self mediums] objectAtIndex:mediumNum-1] properties] objectForKey:@"ctlb"] floatValue];
ctub = [[[[[self mediums] objectAtIndex:mediumNum-1] properties] objectForKey:@"ctub"] floatValue];
denlb = [[[[[self mediums] objectAtIndex:mediumNum-1] properties] objectForKey:@"denlb"] floatValue];
denub = [[[[[self mediums] objectAtIndex:mediumNum-1] properties] objectForKey:@"denub"] floatValue];
return (ctval-ctlb)*(denub-denlb)/(ctub-ctlb)+denlb;
}
-(void) endSavePanel:(NSSavePanel*) sheet
returnCode:(int) retCode
contextInfo:(void*) contextInfo
{
[self saveConfigToDisk];
if (retCode != NSFileHandlingPanelOKButton) return;
// display a waiting window
id waitWindow = [viewerController startWaitWindow:@"Exporting..."];
// fetch pixel value from current series
NSMutableString *phantom = [NSMutableString stringWithCapacity: 100];
long i, j, k;
float *fImage;
NSArray *pixList = [viewerController pixList];
DCMPix *curPix;
curPix = [pixList objectAtIndex:0];
// number of medium in this phantom
[phantom appendFormat:@"%d\n", [[self mediums] count]];
// list of mediums
for (i=0; i<[[self mediums] count]; i++)
[phantom appendFormat:@"%@\n", [[[[self mediums] objectAtIndex:i] properties] objectForKey:@"medium"]];
// the ESTEPE value of the medium (dummy)
for (i=0; i<[[self mediums] count]; i++)
[phantom appendFormat:@"%f ", [[[[[self mediums] objectAtIndex:i] properties] objectForKey:@"estepe"] floatValue]];
[phantom appendFormat:@"\n"];
// th number of voxel in X, Y, Z dimensions
[phantom appendFormat:@" %ld %ld %d\n", [curPix pwidth], [curPix pheight], [pixList count]];
// voxel bondaries in X dimision (cm)
for (i=0; i<=[curPix pwidth]; i++)
[phantom appendFormat:@"%f ", ([curPix originX]+i*[curPix pixelSpacingX])/10];
[phantom appendFormat:@"\n"];
// voxel bondaries in Y dimision (cm)
for (i=0; i<=[curPix pheight]; i++)
[phantom appendFormat:@"%f ", ([curPix originY]+i*[curPix pixelSpacingY])/10];
[phantom appendFormat:@"\n"];
// voxel bondaries in Z dimision (cm)
for (i=0; i<=[pixList count]; i++)
[phantom appendFormat:@"%f ", ([curPix originZ]-0.5*[curPix sliceInterval]+i*[curPix sliceInterval])/10];
[phantom appendFormat:@"\n"];
// medium number in each voxel
for (i=0; i<[pixList count]; i++)
{
curPix = [pixList objectAtIndex :i];
fImage = [curPix fImage];
for (j=0; j<[curPix pwidth]; j++)
{
for (k=0; k<[curPix pheight]; k++)
{
if (![self ctToMediumNumber:*fImage])
{
NSRunInformationalAlertPanel( @"Alert", @"CT value out of boundary!\nCancel export to phantom.", nil, nil, nil );
[viewerController endWaitWindow: waitWindow];
return;
}
[phantom appendFormat:@"%d", [self ctToMediumNumber:*fImage]];
fImage++;
}
[phantom appendFormat:@"\n"];
}
[phantom appendFormat:@"\n\n"];
}
// densities in each voxel
for (i=0; i<[pixList count]; i++)
{
curPix = [pixList objectAtIndex :i];
fImage = [curPix fImage];
for (j=0; j<[curPix pwidth]; j++)
{
for (k=0; k<[curPix pheight]; k++)
{
[phantom appendFormat:@"%f ", [self ctToMediumDensity:*fImage]];
fImage++;
}
[phantom appendFormat:@"\n"];
}
[phantom appendFormat:@"\n\n"];
}
// write phantom data to file
[sheet setRequiredFileType:@"egsphant"];
[phantom writeToFile: [sheet filename] atomically: YES];
// close waiting window
[viewerController endWaitWindow: waitWindow];
}
-(id) init
{
if (self = [super init])
{
_rampSettings = [[NSMutableArray alloc] init];
}
return self;
}
-(void) dealloc
{
[_rampSettings release];
[super dealloc];
}
-(void) awakeFromNib
{
[self loadConfigFromDisk];
}
-(NSString *) pathForConfigFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *folder = @"~/Library/Application Support/OsiriX Data/";
folder = [folder stringByExpandingTildeInPath];
if ([fileManager fileExistsAtPath: folder] == NO)
{
[fileManager createDirectoryAtPath: folder attributes: nil];
}
NSString *fileName = @"plugin.DicomToEgsphant.config";
return [folder stringByAppendingPathComponent: fileName];
}
-(void) saveConfigToDisk
{
NSString *path = [self pathForConfigFile];
NSMutableDictionary *rootObject;
rootObject = [NSMutableDictionary dictionary];
[rootObject setValue: [self rampSettings] forKey:@"rampSettings"];
[NSKeyedArchiver archiveRootObject: rootObject toFile: path];
}
-(void) loadConfigFromDisk
{
NSString *path = [self pathForConfigFile];
NSDictionary *rootObject;
rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
[self setRampSettings: [rootObject valueForKey:@"rampSettings"]];
}
-(NSMutableArray*) rampSettings
{
return _rampSettings;
}
-(NSMutableArray*) mediums
{
return[[_rampSettings objectAtIndex:[selectedRamp indexOfSelectedItem]] mediums];
}
-(void) setRampSettings:(NSArray*) newRampSettings
{
if (_rampSettings != newRampSettings)
{
[_rampSettings autorelease];
_rampSettings = [[NSMutableArray alloc] initWithArray: newRampSettings];
}
}
-(long) filterImage:(NSString*) menuName
{
NSArray *topLevelObjects;
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSNib *nib = [[NSNib alloc] initWithNibNamed:@"RampSelector" bundle:thisBundle];
[nib instantiateNibWithOwner:self topLevelObjects:&topLevelObjects];
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setAccessoryView:accessoryView];
[panel beginSheetForDirectory:nil
file:nil
modalForWindow:[viewerController window]
modalDelegate:self
didEndSelector:@selector(endSavePanel:
returnCode:
contextInfo:)
contextInfo:nil];
[panel setAllowedFileTypes:[NSArray arrayWithObjects:@"egsphant", nil]];
return 0;
}
@end