-
Notifications
You must be signed in to change notification settings - Fork 6
/
GraphView.m
243 lines (197 loc) · 5.49 KB
/
GraphView.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
#import "GraphView.h"
#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>
#import "GraphPoint.h"
#import <sys/time.h>
@implementation GraphView
- (void)awakeFromNib{
lock = [[NSLock alloc] init];
datax = [[NSMutableArray array] retain];
datay = [[NSMutableArray array] retain];
dataz = [[NSMutableArray array] retain];
_freshGrid = TRUE;
}
- (void) resizeView : (NSRect) rect {
glViewport( (GLint) rect.origin.x , (GLint) rect.origin.y,
(GLint) rect.size.width, (GLint) rect.size.height );
}
- (void)setIRPointX:(float)x Y:(float)y{
_x = x;
_y = y;
}
- (id) initWithFrame : (NSRect) frameRect{
_x = _y = -2;
NSOpenGLPixelFormatAttribute attr[] = {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated ,
NSOpenGLPFAStencilSize , 32,
NSOpenGLPFAColorSize , 32,
NSOpenGLPFADepthSize , 32,
0
};
NSOpenGLPixelFormat* pFormat;
pFormat = [ [ [ NSOpenGLPixelFormat alloc ] initWithAttributes : attr ] autorelease ];
self = [ super initWithFrame : frameRect pixelFormat : pFormat ];
[ [ self openGLContext ] makeCurrentContext ];
glClearColor( 1.0, 1.0, 1.0, 1.0 );
[self display];
return( self );
}
-(void)startTimer{
animTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(drawAnimation:) userInfo:nil repeats:YES];
}
- (void)stopTimer{
[animTimer invalidate];
}
- (void) drawAnimation: (NSTimer*)timer{
[self display];
}
- (void) drawGrid{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Draw a grid in gray and black before drawing data so the grid will be under the data.
// This creates the horizontal lines for the grid.
glBegin (GL_LINE_STRIP);
{
int i;
glColor4f(0.7, 0.7, 0.7, 1.0);
for (i = 0; i < 10; i++){
float test = sin(i * 3.14159265358979);
float x = 1.1 * test / fabs(test);
float y = i * 0.2 - 1.0;
glVertex2f(x, y);
glVertex2f(-x, y);
}
}
glEnd();
// This creates stable vertical lines for the grid.
glBegin (GL_LINE_STRIP);
{
int i;
glColor4f(0.7, 0.7, 0.7, 1.0);
for (i = 0; i < 10; i++){
float test = sin(i * 3.14159265358979);
float y = 1.1 * test / fabs(test);
float x = i * 0.2 - 1.0;
glVertex2f(x, y);
glVertex2f(x, -y);
}
}
glEnd();
// This makes the two axes black instead of gray.
glBegin (GL_LINE_STRIP); //Draw a double line so the axis is a little thicker than other lines.
{
glColor4f(0.0, 0.0, 0.0, 1.0);
float x1 = 0.001;
float y = 1.1;
glVertex2f(x1, y);
glVertex2f(x1, -y);
float x2 = -0.001;
glVertex2f(x2, -y);
glVertex2f(x2, y);
}
glEnd();
glBegin (GL_LINE_STRIP); //Draw a double line so the axis is a little thicker than other lines.
{
glColor4f(0.0, 0.0, 0.0, 1.0);
float y1 = 0.01;
float x = 1.1;
glVertex2f(x, y1);
glVertex2f(-x, y1);
float y2 = -0.01;
glVertex2f(-x, y2);
glVertex2f(x, y2);
}
glEnd();
}
- (void) drawRect : (NSRect) rect{
[self resizeView: rect];
struct timeval tval;
struct timezone tzone;
gettimeofday(&tval, &tzone);
float scale = [scaleField floatValue];
if (_freshGrid) {
[self drawGrid];
glFinish();
_freshGrid = FALSE;
}
while( [datax count] && [datay count] && [dataz count] && ![self shouldDraw:[[datax objectAtIndex:0] timeValue] now:tval]){
[datax removeObjectAtIndex: 0];
[datay removeObjectAtIndex: 0];
[dataz removeObjectAtIndex: 0];
}
if (![datax count] || ![datay count] || ![dataz count])
return;
[self drawGrid];
//Now we're plotting data.
glBegin (GL_LINE_STRIP);
{
int i;
glColor4f(1.0, 0.0, 0.0, 1.0); //red
for (i = 0; i < [datax count]; i++){
GraphPoint* p = [datax objectAtIndex:i];
float y = [p value] / scale;
float x = [self timeDif:[p timeValue] subtract:tval]/5 + 1.0;
glVertex2f(x, y);
}
}
glEnd();
glBegin (GL_LINE_STRIP);
{
int i;
glColor4f(0.0, 1.0, 0.0, 1.0); //green
for (i = 0; i < [datay count]; i++){
GraphPoint* p = [datay objectAtIndex:i];
float y = [p value] / scale;
float x = [self timeDif:[p timeValue] subtract:tval]/5 + 1.0;
glVertex2f(x, y);
}
}
glEnd();
glBegin (GL_LINE_STRIP);
{
int i;
glColor4f(0.0, 0.0, 1.0, 1.0); //blue
for (i = 0; i < [dataz count]; i++){
GraphPoint* p = [dataz objectAtIndex:i];
float y = [p value] / scale;
float x = [self timeDif:[p timeValue] subtract:tval]/5 + 1.0;
glVertex2f(x, y);
}
}
glEnd();
if (_x > -2){
glColor4f(1.0, 1.0, 0.0, 1.0);
glRectf( _x - 0.05* (rect.size.height / rect.size.width), _y - 0.05, _x + 0.05 * (rect.size.height / rect.size.width), _y + 0.05 );
}
glFinish();
[[self openGLContext] flushBuffer];
}
- (float)timeDif:(struct timeval)timeVal1 subtract:(struct timeval)timeVal2{
float dif = (float)(timeVal1.tv_sec - timeVal2.tv_sec) + (float)(timeVal1.tv_usec - timeVal2.tv_usec) / (float)1000000.0;
return dif;
}
- (BOOL)shouldDraw:(struct timeval)tval now:(struct timeval)now {
double dif = now.tv_sec - tval.tv_sec + (double)(now.tv_usec - tval.tv_usec) / 1000000.0;
if (dif > SAMPLETIME){
return NO;
}else{
return YES;
}
}
-(void)setData:(float)x y:(float)y z:(float)z{
struct timeval tval;
struct timezone tzone;
gettimeofday(&tval, &tzone);
GraphPoint* pointX = [[GraphPoint alloc] initWithValue:(float)x time:tval];
GraphPoint* pointY = [[GraphPoint alloc] initWithValue:(float)y time:tval];
GraphPoint* pointZ = [[GraphPoint alloc] initWithValue:(float)z time:tval];
[datax addObject:pointX];
[datay addObject:pointY];
[dataz addObject:pointZ];
[pointX release];
[pointY release];
[pointZ release];
}
@end