File: plot.cc

package info (click to toggle)
qepcad 1.74%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,828 kB
  • sloc: ansic: 27,242; cpp: 2,995; makefile: 1,285; perl: 91
file content (412 lines) | stat: -rw-r--r-- 9,722 bytes parent folder | download | duplicates (2)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*****************************************************************
 * ADJ2D_plot - the renderer for QEPCADB's facilities for plotting
 * CADs of R^2.  The "rend" extension of QEPCADB has it's own
 * format for sending out data describing a CAD of R^2.  This
 * program reads that data and renders it in a window.
 *****************************************************************/
extern "C" {
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
};
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

#include "Point.h"
inline void glVertex2(const Point &p) { glVertex2d(p.x,p.y); }

/*****************************************************************
 ******  CLASS DEFINITIONS
 ******************************************************************/
class CADColors
{
public:
  void glSetColor(char c, char w) const; // w is 'L' or 'D'
};

class CADELT
{
public:
  virtual bool read(istream &in) = 0;
  virtual void glRend(const CADColors &C) = 0;
  virtual ~CADELT() {}
};

class SNoverSR : public CADELT
{
  char colorType;
  vector<Point> V;
public:
  bool read(istream &in);
  void glRend(const CADColors &C);
};

class SRoverSR : public CADELT
{
  char colorType;
  vector<Point> V;
public:
  bool read(istream &in);
  void glRend(const CADColors &C);
};

class SRoverSN : public CADELT
{
  char colorType;
  Point a, b;
public:
  bool read(istream &in);
  void glRend(const CADColors &C);
};

class SNoverSN : public CADELT
{
  char colorType;
  Point a;
public:
  bool read(istream &in);
  void glRend(const CADColors &C);
};

class SN : public CADELT
{
  char colorType;
  double x;
public:
  bool read(istream &in);
  void glRend(const CADColors &C);
};

class SR : public CADELT
{
  char colorType;
  double x1, x2;
public:
  bool read(istream &in);
  void glRend(const CADColors &C);
};

/*****************************************************************
 ******  GLOBALS
 ******************************************************************/
pthread_mutex_t M,F;
vector<CADELT*> CE;
bool newdata = true;
CADColors Colors;
string name;
istream *inp;
int d1,d2;

/*****************************************************************
 ******  Function for the data-reading thread
 ******************************************************************/
void* readdata(void *x)
{
  vector<CADELT*> E;
  istream &in = *inp;
  char c;
  do {
    while( in >> c )
    {
      if (c == 'L' || c == 'P' || c == 'R' || c == 'V' || c == 'C' || c == 'S') {
	CADELT *p = 0;
	if      (c == 'L') p = new SNoverSR;
	else if (c == 'P') p = new SRoverSR;
	else if (c == 'R') p = new SRoverSN;
	else if (c == 'V') p = new SN;
	else if (c == 'C') p = new SR;
	else if (c == 'S') p = new SNoverSN;
	p->read(in);
	E.push_back(p); }
      else if (c == 'F') { /******* Refresh it! **************/
	pthread_mutex_lock(&F);
	newdata = true;
	pthread_mutex_unlock(&F);
	pthread_mutex_lock(&M);
	swap(CE,E);
	pthread_mutex_unlock(&M);
	for(size_t i = 0; i < E.size(); i++) delete E[i];
	E.clear(); }
      else if (c == 'E') { /******* Exit! *******************/
	return 0;
      }
      else { cerr << "Unknown graphics command!" << endl; exit(1); }
    }

    return 0;
   
  }while(1);
  return 0;
}

/*****************************************************************
 ******  CALL-BACK FUNCTIONS
 ******************************************************************/
void display()
{
  pthread_mutex_lock(&M);
  glClear(GL_COLOR_BUFFER_BIT);
  for(size_t i = 0; i < CE.size(); i++)
    CE[i]->glRend(Colors);
  pthread_mutex_unlock(&M);  
  glutSwapBuffers();
}
void  idle(void)
{
  static int i = 0;
  ++i;
  if(i % 100000 == 0) cerr << "10K...";
  pthread_mutex_lock(&F);
  if (newdata) {
      glutPostRedisplay();
      newdata = false; }
  pthread_mutex_unlock(&F);  
}

// I'm using this instead of and "idle" function
void  checkForChanges(int t) 
{
  pthread_mutex_lock(&F);
  if (newdata) {
      glutPostRedisplay();
      newdata = false; }
  pthread_mutex_unlock(&F);  
  glutTimerFunc(30,checkForChanges,0);              	
}
void reshape(GLsizei w, GLsizei h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0, d1, 0, d2, -10, 10);
   glMatrixMode(GL_MODELVIEW);
   glutPostRedisplay();
}

/*****************************************************************
 ******  main function for plotting program.
 ******************************************************************/
int main(int argc, char* argv[])
{
  //char B[11] = {'\0'};
  //cin.rdbuf()->pubsetbuf(B,1);
  inp = &cin;
  istream &in = cin;

  /***************************************
  ** read in window size.  Window is not allowed to be larger than
  ** 1000x1000, and the arrays winX and winY are defined to contain
  ** the coordinates of the window corners.
  ****************************************/
  in >> d1 >> d2;
  if (!in) { cerr << "Couldn't read window dimensions!" << endl; exit(1); }
  d1 = d1 > 1000 ? 1000 : d1;
  d2 = d2 > 1000 ? 1000 : d2;


  /***************************************
  ** GL and GLUT initialization.  
  ****************************************/
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  glutInitWindowPosition(0,0);
  glutInitWindowSize(d1,d2);
  glutInit(&argc, argv);
  glutCreateWindow("CADPlotter");

  glViewport(0, 0, d1, d2);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,d1,0,d2,-10,10);
  glMatrixMode(GL_MODELVIEW);
  glClearColor(1.0, 1.0, 1.0, 0.0);

  glEnable(GL_LINE_SMOOTH);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  // glDepthFunc(GL_LEQUAL);
  //  glEnable(GL_DEPTH_TEST);

  glPointSize(2);
  glLineWidth(2);
  glutDisplayFunc(display);
  //  glutIdleFunc(idle); <-- I removed this because it's simply called too often
  glutReshapeFunc(reshape);

  // CREATE MUTEX
  int ecmM = pthread_mutex_init(&M,NULL);
  int ecmF = pthread_mutex_init(&F,NULL);
  if (ecmM || ecmF) { cerr << "Mutex could not be created!" << endl; exit(1); }
  
  // CREATE THREAD
  pthread_t t;
  int ect = pthread_create(&t,0,readdata,0);
  if (ect) { cerr << "Thread could not be created!" << endl; exit(1); }

  glutTimerFunc(30,checkForChanges,0);  
  glutMainLoop();
  return 0;
}

/*****************************************************************
 ******  MEMBER FUNCTION DEFINITIONS
 ******************************************************************/
void CADColors::glSetColor(char c, char w) const
{
  if (c == 'T' && w == 'D') { glColor3f(1,0,0); }
  if (c == 'T' && w == 'L') { glColor3f(153.0/255.0,1,1); }
  if (c == 'F' && w == 'D') { glColor3f(153/255.0,102.0/255.0,0); }
  if (c == 'F' && w == 'L') { glColor3f(1,1,204.0/255.0); }
  if (c == 'U' && w == 'D') { glColor3f(102.0/255.0,102.0/255.0,102.0/255.0); }
  if (c == 'U' && w == 'L') { glColor3f(204.0/255.0,204.0/255.0,204.0/255.0); }
}

bool SNoverSR::read(istream &in)
{
  in >> colorType;
  int N;
  in >> N;
  V.resize(N);
  for(int i = 0; i < N; i++)
    in >> V[i];
  return in.good();
}
  
void SNoverSR::glRend(const CADColors &C)
{  
  // Note: it appears tht GL_LINE_STRIP has a limit of
  // 128 or so points that can appear.
  C.glSetColor(colorType,'D');
  glBegin(GL_LINE_STRIP);
  for(size_t i = 0; i < V.size(); i++)
    glVertex2(V[i]);
  glEnd();
}

bool SRoverSR::read(istream &in)
{
  in >> colorType;
  int N;
  in >> N;
  V.resize(N);
  for(int i = 0; i < N; i++)
    in >> V[i];

  // Check that the x coordinates are a palindrome! & correct if needed
  bool pal = true;
  for(int i = 0, j = N-1; i < j; ++i,--j)
    pal = pal && (V[i].x == V[j].x);
  if (!pal) {
    if (V[0].x == V[N-1].x && V[1].x == V[2].x)
    {
      vector<Point> W(2*(N-2));
      double Y = V[0].y;
      for(int i = 2, j = W.size()-1; i < N; ++i, --j) {
	W[i-2] = V[i];
	W[j].x = V[i].x;
	W[j].y = Y; }
      swap(W,V);
    }
    else if (V[0].x == V[N-1].x && V[N-2].x == V[N-3].x)
    {
      vector<Point> W(2*(N-2));
      double Y = V[N-1].y;
      for(int i = 0, j = W.size()-1; i < N-2; ++i, --j) {
	W[i] = V[i];
	W[j].x = V[i].x;
	W[j].y = Y; }
      swap(W,V);
    }
    else 
    {
      cerr << "Sector over sector in unknown format!" << endl;
      for(size_t i = 0; i < V.size(); i++)
	cerr << V[i] << endl;
      exit(1);
    }
  }

  return in.good();
}
  
void SRoverSR::glRend(const CADColors &C)
{  
  glBegin(GL_QUAD_STRIP);
  C.glSetColor(colorType,'L');
  for(int i = 0, j = V.size() - 1; i < j; i++, j--) {
    glVertex2(V[i]);
    glVertex2(V[j]); }
  if (V.size() % 2) { int i = V.size()/2; glVertex2(V[i]); glVertex2(V[i]); }
  glEnd();
}

bool SRoverSN::read(istream &in)
{
  in >> colorType >> a >> b.y;
  b.x = a.x;
  return in.good();
}

void SRoverSN::glRend(const CADColors &C)
{  
  glBegin(GL_LINE_STRIP);
  C.glSetColor(colorType,'D');
  glVertex2(a);
  glVertex2(b);
  glEnd();
}

bool SNoverSN::read(istream &in)
{
  in >> colorType >> a;
  return in.good();
}
  
void SNoverSN::glRend(const CADColors &C)
{  
  GLUquadricObj* p = gluNewQuadric();
  C.glSetColor(colorType,'D');
  glPushMatrix();
  glTranslated(a.x,a.y,0);
  gluDisk(p,4,5,20,1);
  glPopMatrix();
}

bool SN::read(istream &in)
{
  in >> colorType >> x;
  return in.good();
}
  
void SN::glRend(const CADColors &C)
{  
  glBegin(GL_LINE_STRIP);
  C.glSetColor(colorType,'D');
  glVertex2d(x,0);
  glVertex2d(x,d2);
  glEnd();
}

bool SR::read(istream &in)
{
  in >> colorType >> x1 >> x2;
  return in.good();
}
  
void SR::glRend(const CADColors &C)
{  
  glBegin(GL_QUAD_STRIP);
  C.glSetColor(colorType,'L');
  glVertex2d(x1,0);
  glVertex2d(x1,d2);
  glVertex2d(x2,0);
  glVertex2d(x2,d2);
  glEnd();
}