-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotData.m
217 lines (180 loc) · 5.8 KB
/
PlotData.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
//
// DataPoints.m
// CocoaNXYPlot
//
// Created by Roberto Abraham on Sun Aug 18 2002.
// Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//
#import "PlotData.h"
@implementation PlotData
+(void) initialize
{
if (self==[PlotData class]){
[self setVersion:2];
}
}
-(id)init
{
self = [super init];
if (self) {
[self setNPoints: 0];
[self setShowMe:YES];
}
return self;
}
-(void)loadDataPoints:(int)npts withXValues:(float *)xpts andYValues:(float *)ypts
{
int i;
RGAPoint *buffer;
RGAPoint temp;
float xmin,xmax,ymin,ymax;
NSMutableData *tempData;
xmin=xpts[0]; xmax=xpts[0]; ymin=ypts[0]; ymax=ypts[0];
buffer = (RGAPoint *) malloc(npts*sizeof(RGAPoint));
for(i=0;i<npts;i++){
temp.p = NSMakePoint(*(xpts + i),*(ypts + i));
temp.x = *(xpts + i);
temp.y = *(ypts + i);
temp.xErrorLeft = 0.0;
temp.xErrorRight = 0.0;
temp.yErrorLeft = 0.0;
temp.yErrorRight = 0.0;
temp.symbolColor = [NSColor blackColor];
temp.symbolIndex = 1;
temp.symbolAngle = 0.0;
temp.symbolSize = 1.0;
*(buffer + i) = temp;
if(temp.x>xmax){xmax=temp.x;}
if(temp.x<xmin){xmin=temp.x;}
if(temp.y>ymax){ymax=temp.y;}
if(temp.y<ymin){ymin=temp.y;}
}
tempData = [[[NSMutableData alloc] init] autorelease]; // latest RGA hack - Dec 03
[tempData appendBytes:buffer length:npts*sizeof(RGAPoint)];
[self setData:tempData];
[self setNPoints:npts];
[self setXMin:xmin];
[self setXMax:xmax];
[self setYMin:ymin];
[self setYMax:ymax];
free(buffer);
}
-(void)loadWave:(Wave *)w withRedshift:(float)z;
{
int i;
RGAPoint *buffer;
RGAPoint temp;
float xmin,xmax,ymin,ymax;
float xval, yval;
NSMutableData *tempData;
xmin=(1.0+z)*[w xAtIndex:[w p0]];
xmax=(1.0+z)*[w xAtIndex:([w p0] + [w n])];
ymin=[w yAtIndex:[w p0]];
ymax=[w yAtIndex:([w p0] + [w n])];
buffer = (RGAPoint *) malloc([w n]*sizeof(RGAPoint));
for(i=(int)[w p0];i<(int)([w p0] + [w n]);i++){ //waves return longs not ints!
xval = (1+z) * (float)[w xAtIndex:i]; //waves return double precision not floats!
yval = (float)[w yAtIndex:i];
temp.p = NSMakePoint(xval,yval);
temp.x = xval;
temp.y = yval;
temp.xErrorLeft = 0.0;
temp.xErrorRight = 0.0;
temp.yErrorLeft = 0.0;
temp.yErrorRight = 0.0;
temp.symbolColor = [NSColor blackColor];
temp.symbolIndex = 1;
temp.symbolAngle = 0.0;
temp.symbolSize = 1.0;
*(buffer + i) = temp;
if(temp.x>xmax){xmax=temp.x;}
if(temp.x<xmin){xmin=temp.x;}
if(temp.y>ymax){ymax=temp.y;}
if(temp.y<ymin){ymin=temp.y;}
}
tempData = [[[NSMutableData alloc] init] autorelease]; // latest RGA hack - Dec 03
[tempData appendBytes:buffer length:(int)[w n]*sizeof(RGAPoint)];
[self setData:tempData];
[self setNPoints:(int)[w n]];
[self setXMin:xmin];
[self setXMax:xmax];
[self setYMin:ymin];
[self setYMax:ymax];
free(buffer);
}
-(NSPoint)pointNearestTo:(NSPoint)pt
{
int i;
int indexOfMinimumPoint = 0;
double currentDistanceSquared = 0.0;
double minimumDistanceSquared = 1.e100;
NSPoint currentPoint;
for(i=0;i<[self nPoints];i++){
currentPoint = [self getNSPoint:i];
currentDistanceSquared = pow(currentPoint.x - pt.x,2.0) + pow(currentPoint.y - pt.y,2.0);
if (currentDistanceSquared < minimumDistanceSquared){
minimumDistanceSquared = currentDistanceSquared;
indexOfMinimumPoint = i;
}
}
return [self getNSPoint:indexOfMinimumPoint];
}
//Note zero offset. Using this for accessing many points is probably less efficient than
//glorping through the data all in one go as there is some extra overhead for each
//accessor call. Till I can test it with timings I'd avoid using the next
//two functions unless only one or two data points are needed for some reason.
-(NSPoint)getNSPoint:(int)n
{
RGAPoint *pointBytes = (RGAPoint *)[[self data] bytes];
return pointBytes[n].p;
}
-(RGAPoint)getRGAPoint:(int)n
{
RGAPoint *pointBytes = (RGAPoint *)[[self data] bytes];
return pointBytes[n];
}
//NSCoder stuff
-(void)encodeWithCoder:(NSCoder *)coder
{
//NSLog(@"Encoding the PlotData object\n");
[coder encodeObject:data];
[coder encodeValueOfObjCType:@encode(int) at:&nPoints];
[coder encodeValueOfObjCType:@encode(float) at:&xMin];
[coder encodeValueOfObjCType:@encode(float) at:&xMax];
[coder encodeValueOfObjCType:@encode(float) at:&yMin];
[coder encodeValueOfObjCType:@encode(float) at:&yMax];
//[coder encodeValueOfObjCType:@encode(typeof(showMe)) at:&showMe]; //added at v2
}
-(id)initWithCoder:(NSCoder *)coder
{
int version;
if (self=[super init]){
//NSLog(@"Decoding the PlotData object\n");
version = [coder versionForClassName:@"PlotData"];
[self setData:[coder decodeObject]];
[coder decodeValueOfObjCType:@encode(int) at:&nPoints];
[coder decodeValueOfObjCType:@encode(float) at:&xMin];
[coder decodeValueOfObjCType:@encode(float) at:&xMax];
[coder decodeValueOfObjCType:@encode(float) at:&yMin];
[coder decodeValueOfObjCType:@encode(float) at:&yMax];
//version here
//if (version > 1){
// [coder decodeValueOfObjCType:@encode(typeof(showMe)) at:&showMe];
//}
}
return self;
}
- (void) dealloc
{
[[self data] release];
[super dealloc];
}
idAccessor(data,setData)
idAccessor(myView,setMyView)
intAccessor(nPoints,setNPoints);
floatAccessor(xMin,setXMin)
floatAccessor(xMax,setXMax)
floatAccessor(yMin,setYMin)
floatAccessor(yMax,setYMax)
boolAccessor(showMe,setShowMe)
@end