-
Notifications
You must be signed in to change notification settings - Fork 3
/
XNSurfaceData.m
279 lines (213 loc) · 5.99 KB
/
XNSurfaceData.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
//
// XNSurfaceData.m
// XNMaths
//
// Created by Нат Гаджибалаев on 01.12.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#pragma mark -
#pragma mark Imports
//
// Class header
#import "XNSurfaceData.h"
#import "XNFloatRange.h"
#import "XNFunctionOf2D.h"
#import "plplot.h"
#pragma mark -
#pragma mark XNSurfaceData class private category
@interface XNSurfaceData (Private)
- (void) updateRanges;
- (void) validateAccessToI: (NSUInteger) i J: (NSUInteger) j;
@end
#pragma mark -
#pragma mark XNSurfaceData class implementation
@implementation XNSurfaceData
@synthesize xData, yData;
@synthesize zData;
@synthesize xRange, yRange, zRange;
@synthesize xPointsCount, yPointsCount;
@synthesize isDirty;
#pragma mark -
#pragma mark Class init methods
//
// Build a surface with functiomn in rect with quality
+ (XNSurfaceData *) surfaceWithFunction: (XNFunctionOf2D*) aFunction
xRange: (XNFloatRange*) aXRange
yRange: (XNFloatRange*) aYRange
withQuality: (NSUInteger) lineQuality
{
return [[[XNSurfaceData alloc] initWithFunction:aFunction xRange:aXRange yRange:aYRange withQuality:lineQuality] autorelease];
}
//
// Create an empty surface with N points
+ (XNSurfaceData *) surfaceWithCapacityX: (NSInteger) newXCapacity Y: (NSInteger) newYCapacity
{
return [[[XNSurfaceData alloc] initWithCapacityX: newXCapacity Y: newYCapacity] autorelease];
}
#pragma mark -
#pragma mark Instance init methods
//
// Build a surface with function in rect with quality.
- (XNSurfaceData *) initWithFunction: (XNFunctionOf2D*) aFunction
xRange: (XNFloatRange*) aXRange
yRange: (XNFloatRange*) aYRange
withQuality: (NSUInteger) lineQuality
{
self = [super init];
// set new line quality;
quality = lineQuality;
xRange = [aXRange retain];
yRange = [aYRange retain];
xPointsCount = quality * (NSInteger)[xRange length];
yPointsCount = quality * (NSInteger)[yRange length];
// init data containers
xData = calloc(xPointsCount, sizeof(CGFloat));
yData = calloc(yPointsCount, sizeof(CGFloat));
plAlloc2dGrid(&zData, xPointsCount, yPointsCount);
// set up default y range.
zRange = [XNFloatRange rangeWithMin:[aFunction valueWithX:[xRange min] y:[yRange min]] max:[aFunction valueWithX:[xRange max] y:[yRange max]]];
// calculate data and set min/max values
for(NSInteger i = 0; i < xPointsCount; i++){
xData[i] = xRange.min + ([xRange length] / xPointsCount)*i;
}
for(NSInteger i = 0; i < yPointsCount; i++){
yData[i] = yRange.min + ([yRange length] / yPointsCount)*i;
}
for(NSInteger i = 0; i < xPointsCount; i++){
for(NSUInteger j = 0; j < yPointsCount; j++){
zData[i][j] = [aFunction valueWithX: xData[i] y: yData[j]];
if( zData[i][j] < zRange.min ){
zRange.min = zData[i][j];
}
if( zData[i][j] > zRange.max ){
zRange.max = zData[i][j];
}
}
}
return self;
}
//
// Inits an empty surface with N points
- (XNSurfaceData *) initWithCapacityX: (NSInteger) newXCapacity Y: (NSInteger) newYCapacity
{
self = [super init];
//
// Set zero counts
xPointsCount = newXCapacity;
yPointsCount = newYCapacity;
//
// Allocate memory
xData = calloc(xPointsCount, sizeof(CGFloat));
yData = calloc(yPointsCount, sizeof(CGFloat));
plAlloc2dGrid(&zData, xPointsCount, yPointsCount);
//
// Zero memory
for( NSUInteger i = 0; i < xPointsCount; i++ ){
xData[i] = 0.;
}
for( NSUInteger i = 0; i < yPointsCount; i++ ){
yData[i] = 0.;
}
for( NSUInteger i = 0; i < xPointsCount; i++ ){
for(NSUInteger j = 0; j < yPointsCount; j++ ){
zData[i][j] = 0.;
}
}
[self cleanupRanges];
return self;
}
#pragma mark -
#pragma mark Instance logic
- (void) set3DPoint: (XN3DPoint) point atI: (NSUInteger) i J: (NSUInteger) j dirty: (BOOL) dirty
{
[self validateAccessToI:i J:j];
//
// Set the point
xData[i] = point.x;
yData[j] = point.y;
zData[i][j] = point.z;
if( !dirty){
[self cleanupRanges];
} else {
isDirty = YES;
}
}
- (void) setArguments2DPoint: (XN2DPoint) point atI: (NSUInteger) i J: (NSUInteger) j dirty: (BOOL) dirty
{
[self validateAccessToI:i J:j];
//
// Set the point
xData[i] = point.x;
yData[j] = point.y;
if( !dirty){
[self cleanupRanges];
} else {
isDirty = YES;
}
}
- (void) setValue: (CGFloat) value atI: (NSUInteger) i J: (NSUInteger) j dirty: (BOOL) dirty
{
[self validateAccessToI:i J:j];
zData[i][j] = value;
if( !dirty){
[self cleanupRanges];
} else {
isDirty = YES;
}
}
- (CGFloat) valueAtI: (NSUInteger) i J: (NSUInteger) j
{
[self validateAccessToI:i J:j];
return zData[i][j];
}
- (XN3DPoint) pointAtI: (NSUInteger) i J: (NSUInteger) j
{
[self validateAccessToI:i J:j];
return XNMake3DPoint(xData[i], yData[j], zData[i][j]);
}
#pragma mark -
#pragma mark Private category methods
- (void) cleanupRanges
{
//
// Release old ranges
[xRange release];
[yRange release];
[zRange release];
//
// Create new ranges
xRange = [[XNFloatRange rangeWithCArray: xData withCapacity: xPointsCount] retain];
yRange = [[XNFloatRange rangeWithCArray: yData withCapacity: yPointsCount] retain];
//
// Update Z range
CGFloat zMin, zMax;
plMinMax2dGrid(zData, xPointsCount, yPointsCount, &zMax, &zMin);
zRange = [[XNFloatRange rangeWithMin: zMin max: zMax] retain];
isDirty = NO;
}
- (void) validateAccessToI: (NSUInteger) i J: (NSUInteger) j
{
if( i >= xPointsCount){
[NSException raise: @"Accessing surface point out of the grid."
format: @"Trying to access %d point in X scale, but X scale contains only %d positions", i, xPointsCount];
}
if( j >= yPointsCount){
[NSException raise: @"Accessing surface point out of the grid."
format: @"Trying to access %d point in Y scale, but Y scale contains only %d positions", j, yPointsCount];
}
}
#pragma mark -
#pragma mark Private runtime service methods
//
// Deallocate instance hook
- (void) dealloc
{
plFree2dGrid(zData, xPointsCount, yPointsCount);
free(xData);
free(yData);
[xRange release];
[yRange release];
[zRange release];
[super dealloc];
}
@end