forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPRHorizontalFillOperation.m
214 lines (172 loc) · 6.46 KB
/
CPRHorizontalFillOperation.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
/*=========================================================================
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 "CPRHorizontalFillOperation.h"
#import "CPRVolumeData.h"
#import "N3Geometry.h"
@interface CPRHorizontalFillOperation ()
- (void)_nearestNeighborFill;
- (void)_linearInterpolatingFill;
- (void)_cubicInterpolatingFill;
- (void)_unknownInterpolatingFill;
@end
@implementation CPRHorizontalFillOperation
@synthesize volumeData = _volumeData;
@synthesize width = _width;
@synthesize height = _height;
@synthesize floatBytes = _floatBytes;
@synthesize vectors = _vectors;
@synthesize normals = _normals;
@synthesize interpolationMode = _interpolationMode;
- (id)initWithVolumeData:(CPRVolumeData *)volumeData interpolationMode:(CPRInterpolationMode)interpolationMode floatBytes:(float *)floatBytes width:(NSUInteger)width height:(NSUInteger)height vectors:(N3VectorArray)vectors normals:(N3VectorArray)normals
{
if ( (self = [super init])) {
_volumeData = [volumeData retain];
_floatBytes = floatBytes;
_width = width;
_height = height;
_vectors = malloc(width * sizeof(N3Vector));
memcpy(_vectors, vectors, width * sizeof(N3Vector));
_normals = malloc(width * sizeof(N3Vector));
memcpy(_normals, normals, width * sizeof(N3Vector));
_interpolationMode = interpolationMode;
}
return self;
}
- (void)dealloc
{
[_volumeData release];
_volumeData = nil;
free(_vectors);
_vectors = NULL;
free(_normals);
_normals = NULL;
[super dealloc];
}
- (void)main
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
double threadPriority;
@try {
if ([self isCancelled]) {
return;
}
threadPriority = [NSThread threadPriority];
[NSThread setThreadPriority:threadPriority * .5];
if (_interpolationMode == CPRInterpolationModeLinear) {
[self _linearInterpolatingFill];
} else if (_interpolationMode == CPRInterpolationModeNearestNeighbor) {
[self _nearestNeighborFill];
} else if (_interpolationMode == CPRInterpolationModeCubic) {
[self _cubicInterpolatingFill];
} else {
[self _unknownInterpolatingFill];
}
[NSThread setThreadPriority:threadPriority];
}
@catch (...) {
}
@finally {
[pool release];
}
}
- (void)_linearInterpolatingFill
{
NSUInteger x;
NSUInteger y;
N3AffineTransform vectorTransform;
N3VectorArray volumeVectors;
N3VectorArray volumeNormals;
CPRVolumeDataInlineBuffer inlineBuffer;
volumeVectors = malloc(_width * sizeof(N3Vector));
memcpy(volumeVectors, _vectors, _width * sizeof(N3Vector));
N3VectorApplyTransformToVectors(_volumeData.volumeTransform, volumeVectors, _width);
volumeNormals = malloc(_width * sizeof(N3Vector));
memcpy(volumeNormals, _normals, _width * sizeof(N3Vector));
vectorTransform = _volumeData.volumeTransform;
vectorTransform.m41 = vectorTransform.m42 = vectorTransform.m43 = 0.0;
N3VectorApplyTransformToVectors(vectorTransform, volumeNormals, _width);
[_volumeData aquireInlineBuffer:&inlineBuffer];
for (y = 0; y < _height; y++) {
if ([self isCancelled]) {
break;
}
for (x = 0; x < _width; x++) {
_floatBytes[y*_width + x] = CPRVolumeDataLinearInterpolatedFloatAtVolumeVector(&inlineBuffer, volumeVectors[x]);
}
N3VectorAddVectors(volumeVectors, volumeNormals, _width);
}
free(volumeVectors);
free(volumeNormals);
}
- (void)_nearestNeighborFill
{
NSUInteger x;
NSUInteger y;
N3AffineTransform vectorTransform;
N3VectorArray volumeVectors;
N3VectorArray volumeNormals;
CPRVolumeDataInlineBuffer inlineBuffer;
volumeVectors = malloc(_width * sizeof(N3Vector));
memcpy(volumeVectors, _vectors, _width * sizeof(N3Vector));
N3VectorApplyTransformToVectors(_volumeData.volumeTransform, volumeVectors, _width);
volumeNormals = malloc(_width * sizeof(N3Vector));
memcpy(volumeNormals, _normals, _width * sizeof(N3Vector));
vectorTransform = _volumeData.volumeTransform;
vectorTransform.m41 = vectorTransform.m42 = vectorTransform.m43 = 0.0;
N3VectorApplyTransformToVectors(vectorTransform, volumeNormals, _width);
[_volumeData aquireInlineBuffer:&inlineBuffer];
for (y = 0; y < _height; y++) {
if ([self isCancelled]) {
break;
}
for (x = 0; x < _width; x++) {
_floatBytes[y*_width + x] = CPRVolumeDataNearestNeighborInterpolatedFloatAtVolumeVector(&inlineBuffer, volumeVectors[x]);
}
N3VectorAddVectors(volumeVectors, volumeNormals, _width);
}
free(volumeVectors);
free(volumeNormals);
}
- (void)_cubicInterpolatingFill
{
NSUInteger x;
NSUInteger y;
N3AffineTransform vectorTransform;
N3VectorArray volumeVectors;
N3VectorArray volumeNormals;
CPRVolumeDataInlineBuffer inlineBuffer;
volumeVectors = malloc(_width * sizeof(N3Vector));
memcpy(volumeVectors, _vectors, _width * sizeof(N3Vector));
N3VectorApplyTransformToVectors(_volumeData.volumeTransform, volumeVectors, _width);
volumeNormals = malloc(_width * sizeof(N3Vector));
memcpy(volumeNormals, _normals, _width * sizeof(N3Vector));
vectorTransform = _volumeData.volumeTransform;
vectorTransform.m41 = vectorTransform.m42 = vectorTransform.m43 = 0.0;
N3VectorApplyTransformToVectors(vectorTransform, volumeNormals, _width);
[_volumeData aquireInlineBuffer:&inlineBuffer];
for (y = 0; y < _height; y++) {
if ([self isCancelled]) {
break;
}
for (x = 0; x < _width; x++) {
_floatBytes[y*_width + x] = CPRVolumeDataCubicInterpolatedFloatAtVolumeVector(&inlineBuffer, volumeVectors[x]);
}
N3VectorAddVectors(volumeVectors, volumeNormals, _width);
}
free(volumeVectors);
free(volumeNormals);
}
- (void)_unknownInterpolatingFill
{
NSLog(@"unknown interpolation mode");
memset(_floatBytes, 0, _height * _width);
}
@end