00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef _DISPLAY_H
00010 #define _DISPLAY_H
00011
00012 #ifdef _windows
00013 #include "display_win.h"
00014 #else
00015
00016 #define _DISPLAY_VERSION 1.05
00017
00018 #ifdef NO_GENERAL
00019 #define bool int
00020 #define true 1
00021 #define false 0
00022 #endif
00023
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <X11/Xlib.h>
00027 #include <X11/Xutil.h>
00028 #include <X11/keysym.h>
00029 #include <X11/cursorfont.h>
00030
00031 #ifndef NO_XPM
00032 #include <X11/xpm.h>
00033 #endif
00034
00035 #ifndef NO_THREAD
00036 #include <pthread.h>
00037 #endif
00038
00039 #include <sys/ipc.h>
00040 #include <sys/sem.h>
00041
00042
00043 #if !defined(_mac)
00044
00045 #if !defined(_SEM_SEMUN_UNDEFINED)
00046
00047 #else
00048
00049 union semun
00050 {
00051 int val;
00052 struct semid_ds *buf;
00053 unsigned short int *array;
00054 struct seminfo *__buf;
00055 };
00056 #endif
00057
00058 #endif
00059
00060
00061 #ifndef NO_GENERAL
00062 #include "general.h"
00063 #endif
00064
00065
00066 #include <math.h>
00067 #include <errno.h>
00068 #include <string.h>
00069 #include <unistd.h>
00070
00071 #ifndef DEPTH
00072
00073 #define DEPTH 32
00074 #endif
00075
00076 #define DEPTH_REAL 8
00077 #if DEPTH == 16
00078 #define RGB RGB16
00079 #define GETRED RED16
00080 #define GETGREEN GREEN16
00081 #define GETBLUE BLUE16
00082 #elif DEPTH == 32
00083 #define RGB RGB32
00084 #define GETRED RED32
00085 #define GETGREEN GREEN32
00086 #define GETBLUE BLUE32
00087 #endif
00088
00089 #define RGB32(R, G, B) ((unsigned)(R)<<16)+((unsigned)(G)<<8)+((unsigned)(B))
00090 #define RGB16(R, G, B) ((unsigned)((R)&0xF8)<<8)+((unsigned)((G)&0xF8)<<3)+((unsigned)((B)>>3))
00091
00092 #define RGBany(R, G, B) ((((unsigned)(R)&CCT[0][0])<<CCT[0][1])>>CCT[0][2])\
00093 +((((unsigned)(G)&CCT[1][0])<<CCT[1][1])>>CCT[1][2])\
00094 +((((unsigned)(B)&CCT[2][0])<<CCT[2][1])>>CCT[2][2])
00095
00096 #define RED32(C) (((C&0xFF0000)>>16)*1.0/(0x00FF))
00097 #define GREEN32(C) (((C&0xFF00)>>8)*1.0/(0x00FF))
00098 #define BLUE32(C) (((C&0x00FF)*1.0/(0x00FF)))
00099
00100 #define RED16(C) (((C&0xF800)>>11)*1.0/(0x001F))
00101 #define GREEN16(C) (((C&0x07C0)>>6)*1.0/(0x001F))
00102 #define BLUE16(C) (((C&0x001F)*1.0/(0x001F)))
00103
00104 #define REDany(C) (((C&CCT[0][3])>>CCT[0][4])*1.0/(CCT[0][5]))
00105 #define GREENany(C) (((C&CCT[1][3])>>CCT[1][4])*1.0/(CCT[1][5]))
00106 #define BLUEany(C) (((C&CCT[2][3])>>CCT[2][4])*1.0/(CCT[2][5]))
00107
00108
00109
00110
00111
00112 enum { MaxPoints=40000, MaxLines=5000, rInterval=50, DSCLEN=30};
00113
00114
00115
00116 inline void delay(long sec, long usec)
00117 {
00118
00119
00120 struct timeval tv;
00121 tv.tv_sec=sec;
00122 tv.tv_usec=usec;
00123
00124 select(0,NULL,NULL,NULL,&tv);
00125 }
00126
00127 extern unsigned long colorconvert_16[3][6], colorconvert_32[3][6];
00128
00129 class SYWindow
00130 {
00131 public:
00132 enum{ ROTATION=0,TRANSLATION=1,SCALING=2,PROJECTION=3,
00133 PBCTRANSLATION=4,ASPECTRATIO=5,PBCX=6,PBCY=7,PBCZ=8,PBCGLIDE=9};
00134 struct YPoint
00135 {
00136 double x, y, z, r;
00137 unsigned long c;
00138 unsigned long attr;
00139 char dscrpt[DSCLEN];
00140 }Points[MaxPoints];
00141 struct YLine
00142 {
00143 double x0, y0, z0, x1, y1, z1, r;
00144 unsigned long c;
00145 unsigned long attr;
00146 }Lines[MaxLines];
00147 struct ZInd
00148 {
00149 double Z;
00150 int index;
00151 }Z_ind[MaxLines+MaxPoints];
00152
00153 static int cmp(const void *p1, const void *p2);
00154
00155 unsigned long CCT[3][6];
00156
00157 int nP, nL;
00158 int semID;
00159 int semID2;
00160 bool alive,pause,drawframe;
00161 int loop;
00162 int rinterval;
00163 int msrsp;
00164
00165 KeySym ksfeedback;
00166
00167 Display *theDisplay;
00168 int theScreen;
00169 XVisualInfo visinfo;
00170 Visual *vis;
00171 XSetWindowAttributes attr;
00172 int PixDepth;
00173 GC gc;
00174 Pixmap pixmap;
00175 Window theWindow, Root;
00176 unsigned int width, height;
00177 unsigned int depth;
00178
00179 Colormap cmap;
00180 unsigned long cblack, cwhite, bgcolor, framecolor;
00181
00182 double A11,A12,A13,A21,A22,A23,A31,A32,A33;
00183 double a11,a12,a13,a21,a22,a23,a31,a32,a33;
00184 double B11,B12,B13,B21,B22,B23,B31,B32,B33;
00185
00186 int R, B;
00187
00188 int Xp,Yp;
00189 int gifcount,pscount;
00190 unsigned long lastDrag;
00191
00192 bool dirty;
00193 int X0,Y0,X00,Y00;
00194 double Scale,Scale0,Aspr,Aspr0;
00195 double D,D0;
00196 bool square, sort;
00197
00198 bool autowritegif;
00199
00200 int scalepoints;
00201 int enable_pbc;
00202 double pbcshift[3];
00203 double maxpointradius, maxlinewidth;
00204
00205 double CXs(double x, double z)
00206 { return (width/2+(B*x/(D==0?1:(1-z/D)))*Scale+X0); }
00207 double CYs(double y, double z)
00208 { return (height/2-(B*y/(D==0?1:(1-z/D)))*Scale*Aspr+Y0); }
00209 double CXr(double x, double z)
00210 { return (width/2+(width/4*x/(D==0?1:(1-z/D)))*Scale+X0); }
00211 double CYr(double y, double z)
00212 { return (height/2-(height/4*y/(D==0?1:(1-z/D)))*Scale*Aspr+Y0); }
00213 double CX(double x, double z) {return (square)?CXs(x,z):CXr(x,z); }
00214 double CY(double y, double z) {return (square)?CYs(y,z):CYr(y,z); }
00215 double CR(double r, double z) {if(scalepoints) return CRs(r,z); else return CRf(r,z); }
00216 double CRs(double r, double z) { return (B*r*Scale/(D==0?1:(1-z/D))); }
00217 double CRf(double r, double z) { return r; }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 double DEG(double a) { return (M_PI*a/180); }
00230
00231 void initRot()
00232 {
00233 a11=a22=a33=B11=B22=B33=1;
00234 a12=a13=a21=a23=a31=a32=B12=B13=B21=B23=B31=B32=0;
00235 }
00236 void copyRot()
00237 { A11=a11;A12=a12;A13=a13;
00238 A21=a21;A22=a22;A23=a23;
00239 A31=a31;A32=a32;A33=a33; }
00240 void saveRot()
00241 { a11=A11;a12=A12;a13=A13;
00242 a21=A21;a22=A22;a23=A23;
00243 a31=A31;a32=A32;a33=A33; }
00244 void saveScale()
00245 { Scale0=Scale;}
00246 void saveView()
00247 { saveRot();Scale0=Scale;X00=X0;Y00=Y0;D0=D;Aspr0=Aspr;}
00248
00249 SYWindow(const SYWindow &){}
00250 const SYWindow &operator =(const SYWindow &yw){ return yw;}
00251 public:
00252 SYWindow(int width_hint, int height_hint,
00253 const char *winname, bool s=true, bool so=false,bool fm=false);
00254 virtual ~SYWindow();
00255 void setinterval(int i) {if (i>0) rinterval=i; }
00256 bool IsAlive() { return alive; }
00257 bool TogglePause() { pause=!pause; return pause;}
00258 bool IsPaused() { return pause; }
00259 #ifdef _NOLOCK
00260 void Lock()
00261 {
00262 }
00263 void Unlock()
00264 {
00265 }
00266 void InitSem()
00267 {
00268 }
00269 void InitSem2()
00270 {
00271 }
00272 void LockWritegif()
00273 {
00274 }
00275 void UnlockWritegif()
00276 {
00277 }
00278 #else
00279 #ifndef _MYOWNLOCK
00280 void Lock()
00281 {
00282 static sembuf sbl={0, -1, 0};
00283 semop(semID,&sbl,1);
00284
00285 }
00286 void Unlock()
00287 {
00288 static sembuf sbu={0, 1, 0};
00289 semop(semID,&sbu,1);
00290
00291 }
00292 void InitSem()
00293 {
00294 semID=semget(IPC_PRIVATE, 1, IPC_CREAT|0777);
00295
00296 if(semID==-1) printf("semget failure! use -D_MYOWNLOCK\n");
00297 }
00298 void InitSem2()
00299 {
00300 semID2=semget(IPC_PRIVATE, 1, IPC_CREAT|0777);
00301
00302 if(semID2==-1) printf("semget failure! use -D_MYOWNLOCK\n");
00303 }
00304 void LockWritegif()
00305 {
00306 static sembuf sbl={0, -1, 0};
00307 autowritegif=true;
00308 semop(semID2,&sbl,1);
00309
00310 }
00311 void UnlockWritegif()
00312 {
00313 static sembuf sbu={0, 1, 0};
00314 autowritegif=false;
00315 semop(semID2,&sbu,1);
00316 }
00317 #else
00318 void Lock()
00319 {
00320
00321 semID--;
00322 while(semID<0) sleep(1);
00323
00324 }
00325 void Unlock()
00326 {
00327
00328 semID++;
00329
00330 }
00331 void InitSem()
00332 {
00333 printf("-D_MYOWNLOCK defined, use semaphore mimic\n");
00334 semID=0;
00335 }
00336 void InitSem2()
00337 {
00338 printf("-D_MYOWNLOCK defined, use semaphore mimic\n");
00339 semID2=0;
00340 }
00341 void LockWritegif()
00342 {
00343
00344 semID2--;
00345 while(semID2<0) sleep(1);
00346 }
00347 void UnlockWritegif()
00348 {
00349
00350 semID2++;
00351 }
00352 #endif
00353 #endif
00354 void DrawPoint(double x, double y, double z, double r, unsigned long c,
00355 unsigned long attr=0);
00356 void DrawPoint(double x, double y, double z, double r, unsigned long c,
00357 char *ds, unsigned long attr=0);
00358 void DrawLine(double x0, double y0, double z0,
00359 double x1, double y1, double z1, unsigned long c,
00360 double r=0.01, unsigned long attr=0);
00361 void Draw3DLine(YLine);
00362 void Draw3DPixel(YPoint);
00363 void Draw3DLinetoPS(FILE *,YLine);
00364 void Draw3DPixeltoPS(FILE *,YPoint);
00365 unsigned long AllocNamedColor(char *name);
00366 unsigned long AllocRGBColor(unsigned r, unsigned g, unsigned b);
00367 unsigned long AllocShortRGBColor(unsigned r, unsigned g, unsigned b);
00368 unsigned long StdColor(int c)
00369 {
00370 return AllocShortRGBColor(c&1?0xff:0, c&2?0xff:0, c&4?0xff:0);
00371 }
00372
00373 void Clear() { nP=nL=0; }
00374 void Refresh() { dirty=true; }
00375
00376 virtual void paint();
00377 virtual void update();
00378 virtual void newGraph();
00379
00380 virtual void Evolve();
00381 virtual int identify(int px, int py);
00382 virtual void Routine()
00383 {
00384 while(alive)
00385 {
00386 if(rinterval==0) delay(0,1000*rInterval);
00387 else delay(0,1000*rinterval);
00388 Evolve();
00389 }
00390 }
00391 virtual int ExtKeyHandler(KeySym ks)
00392 {
00393 ksfeedback=ks;
00394 if((int)ks)return 0;else return 1;
00395 };
00396 virtual void FreeResource()
00397 {
00398 union semun su={0};
00399 if(alive)
00400 {
00401 XFreeGC(theDisplay, gc);
00402 XFreePixmap(theDisplay, pixmap);
00403 XFreeColormap(theDisplay, cmap);
00404 XDestroyWindow(theDisplay, theWindow);
00405 XCloseDisplay(theDisplay);
00406
00407 semctl(semID, 0, IPC_RMID, su);
00408 semctl(semID2, 0, IPC_RMID, su);
00409 }
00410 alive=false;
00411 pause=false;
00412
00413 }
00414 #ifndef NO_THREAD
00415 static void *thread_routine(void *p) ;
00416 void Run() ;
00417 #endif
00418
00419 #ifndef NO_XPM
00420 void writeXpm(char *name)
00421 {
00422 XpmWriteFileFromPixmap(theDisplay,name,pixmap,0,NULL);
00423 }
00424 void writegif();
00425 #endif
00426 void importgif();
00427 void writeps();
00428 void testcolor();
00429 void reversergb();
00430 };
00431
00432 class YWindow: public SYWindow
00433 {
00434
00435 public:
00436 bool enableRot;
00437 void update();
00438 void applyRot();
00439 void horizontalRot(double);
00440 void verticalRot(double);
00441 void spinRot(double);
00442 void zoom(double);
00443 void project(double);
00444 void rotateTo(XEvent);
00445 void translateTo(XEvent);
00446 void pbcshiftTo(XEvent,int);
00447 void pbcglideTo(XEvent);
00448 void pbcglide(double,double);
00449 void scaleTo(XEvent);
00450 void projectTo(XEvent);
00451 void applyRotate();
00452 void setWinSpec(int x0,int y0,double s,double d,double a[3][3]);
00453 YWindow(int w,int h,const char *n,bool s=true,bool so=true,bool fm=true):
00454 SYWindow(w,h,n,s,so,fm),enableRot(false){};
00455 void printWinSpec();
00456 virtual void Evolve();
00457 virtual void help();
00458 virtual void drawBoxFrame();
00459 };
00460
00461
00462
00463 #endif // _DISPLAY_H
00464
00465 #endif //ifdef _windows