X11Renderer.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.8 $
00022 // $Date: 2003/02/26 18:56:09 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/renderer/X11Renderer.cpp,v $
00024                                                                         
00025                                                                         
00026 // File: ~/renderer/X11Renderer.C
00027 //
00028 // Written: fmk 
00029 // Created: 10/98
00030 // Revision: A
00031 //
00032 // Description: This file contains the class definition for X11Renderer.
00033 // X11Renderer is an class which diplays using X11 or openGL.
00034 //
00035 // What: "@(#) X11Renderer.h, revA"
00036 
00037 #include <X11Renderer.h>
00038 #include <ColorMap.h>
00039 #include <string.h>
00040 
00041 #include <db.H>
00042 
00043 #include <Matrix.h>
00044 #include <Vector.h>
00045 #include <View.h>
00046 #include <Projection.h>
00047 #include <Viewport.h>
00048 #include <Clipping.h>
00049 #include <WindowDevice.h>
00050 #include <Scan.h>
00051 
00052 #include <iomanip>
00053 using std::ios;
00054 
00055 #define WIRE_MODE 1
00056 #define FILL_MODE 0
00057 //#define PARALLEL_MODE 0
00058 //#define PERSPECTIVE_MODE 1
00059 
00060 X11Renderer::X11Renderer(const char *title, int xLoc, int yLoc, int width, int height,
00061                          ColorMap &_theMap)
00062   :Renderer(_theMap), theFileName(0)
00063 {
00064     theView = new View;
00065     theProjection = new Projection;
00066     theClipping = new Clipping;
00067     theViewport = new Viewport;
00068     theScan = new ScanLineConverter;
00069     theDevice = new WindowDevice;
00070     
00071     theScan->setDevice(*theDevice);
00072     theScan->setProjection(*theProjection);  
00073     theViewport->setDevice(*theDevice);
00074   
00075     theDevice->WINOPEN(title, xLoc, yLoc, width, height);
00076     theScan->setFillMode(0);    
00077 
00078     aFile = 0;
00079 }
00080 
00081 
00082 X11Renderer::X11Renderer(const char *title, int xLoc, int yLoc, int width, int height,
00083                          ColorMap &_theMap,
00084                          const char *fileName)
00085   :Renderer(_theMap)
00086 {
00087     theView = new View;
00088     theProjection = new Projection;
00089     theClipping = new Clipping;
00090     theViewport = new Viewport;
00091     theScan = new ScanLineConverter;
00092     theDevice = new WindowDevice;
00093     
00094     theScan->setDevice(*theDevice);
00095     theScan->setProjection(*theProjection);  
00096     theViewport->setDevice(*theDevice);
00097   
00098     theDevice->WINOPEN(title, xLoc, yLoc, width, height);
00099     theScan->setFillMode(0);    
00100 
00101     // if file is specified, copy name and open the file
00102     if (fileName != 0) {
00103       theFileName = new char[strlen(fileName)+1]; 
00104       if (theFileName == 0) {
00105         opserr <<"WARNING - X11Renderer::X11Renderer() - out of memory ccopying file name: " << *fileName << endln;
00106         exit(-1);
00107       } else {
00108         strcpy(theFileName, fileName);    
00109         theFile.open(fileName, ios::out);
00110         if (!theFile) {
00111           opserr <<"WARNING - X11Renderer::X11Renderer() - could not open file: " << fileName << endln;;          
00112           aFile = 0;
00113         } else {
00114           aFile = 1;
00115           theFile << title << endln;
00116           theFile << xLoc << " " << yLoc << " " << width << " " << height << endln;
00117         }
00118       } 
00119     }
00120 }
00121 
00122 X11Renderer::~X11Renderer()
00123 {
00124     delete    theView;
00125     delete    theProjection;
00126     delete    theClipping;
00127     delete    theViewport;
00128     delete    theScan;
00129     delete    theDevice;
00130     
00131     if (aFile == 1)
00132       theFile.close();
00133     
00134     if (theFileName != 0)
00135       delete [] theFileName;
00136 }
00137 
00138 
00139 int 
00140 X11Renderer::clearImage(void)
00141 {
00142   theDevice->CLEAR();
00143   return 0;
00144 }
00145 
00146 int 
00147 X11Renderer::startImage(void)
00148 {
00149   theView->update();
00150   theProjection->update();
00151   theClipping->update();
00152   theViewport->update();
00153   theScan->update();
00154   theDevice->STARTIMAGE();
00155 
00156   if (aFile == 1) {
00157     theFile << "StartImage\n";
00158     theFile << "VRP " << theView->vrp[0] << " " << theView->vrp[1] << " " << theView->vrp[2] << " " << endln;
00159     theFile << "VPN " << theView->vpn[0] << " " << theView->vpn[1] << " " << theView->vpn[2] << " " << endln;
00160     theFile << "VUV " << theView->vuv[0] << " " << theView->vuv[1] << " " << theView->vuv[2] << " " << endln;
00161     theFile << "COP " << theProjection->cop[0] << " " << theProjection->cop[1] << " " << theProjection->cop[2] << " " << endln;
00162     theFile << "PROJECTIONMODE " << theProjection->projection_mode << endln;
00163     theFile << "VPWINDOW " << theProjection->vpwindow[0] << " " << theProjection->vpwindow[1] << " "
00164             << theProjection->vpwindow[2] << " " << theProjection->vpwindow[3] << " " << endln;
00165     theFile << "PLANES " << theProjection->planedist[0] << " " << theProjection->planedist[2] << " " << endln;
00166     theFile << "PORTWINDOW " << theViewport->portwindow[0] << " " << theViewport->portwindow[1] << " "
00167             << theViewport->portwindow[2] << " " << theViewport->portwindow[3] << " " << endln;
00168 
00169   } 
00170 
00171   return 0;
00172 }
00173 
00174 int 
00175 X11Renderer::doneImage(void)
00176 {
00177   theDevice->ENDIMAGE();
00178 
00179   if (aFile == 1) {
00180     theFile << "DoneImage\n";
00181   }
00182   return 0;
00183 }
00184 
00185 
00186 int 
00187 X11Renderer::drawPoint(const Vector &pos1, float V1, int numPixels)
00188 {
00189     float r, g, b;
00190     r = theMap->getRed(V1);
00191     g = theMap->getGreen(V1);
00192     b = theMap->getBlue(V1);
00193 
00194     if (aFile == 1) {
00195         theFile << "Point\n" << pos1(0) << " " << pos1(1) << " " << pos1(2) 
00196             << " " << r << " " << g << " " << b << " " << endln;
00197     }
00198 
00199     return 0;  
00200 }
00201 
00202 
00203 int 
00204 X11Renderer::drawPoint(const Vector &pos1, const Vector &rgb, int numPixels)
00205 {
00206     float r, g, b;
00207     r = rgb(0);
00208     g = rgb(1);
00209     b = rgb(2);
00210 
00211     if (aFile == 1  ) {
00212         theFile << "Point\n" << pos1(0) << " " << pos1(1) << " " << pos1(2) 
00213             << " " << r << " " << g << " " << b << " " << endln;
00214     }
00215 
00216     return 0;  
00217 }
00218 
00219 
00220 
00221 
00222 int 
00223 X11Renderer::drawLine(const Vector &pos1, const Vector &pos2, 
00224                        float V1, float V2, int width, int style)
00225 {
00226   FACE *theFace = new FACE();   
00227   
00228   int size;
00229   float x,y,z, r, g, b;
00230   MYPOINT *point;
00231 
00232   // add POINTs to the FACE  
00233   size = pos1.Size();
00234   if (size == 1) {
00235     x = pos1(0);
00236     y = 0;
00237     z = 0;
00238   } else if (size == 2) {
00239     x = pos1(0);
00240     y = pos1(1);
00241     z = 0;
00242   } else {
00243     x = pos1(0);
00244     y = pos1(1);
00245     z = pos1(2);
00246   }  
00247   point = new MYPOINT(1,x,y,z);
00248   r = theMap->getRed(V1);
00249   g = theMap->getGreen(V1);
00250   b = theMap->getBlue(V1);
00251   point->r = r;
00252   point->g = g;
00253   point->b = b;
00254     
00255   theFace->AddPoint(*point);
00256 
00257   if (aFile == 1) {
00258     theFile << "Line\n" << x << " " << y << " " << z << " " << r << " " << g << " " << b << " " << endln;
00259   }
00260 
00261   size = pos2.Size();
00262   if (size == 1) {
00263     x = pos2(0);
00264     y = 0;
00265     z = 0;
00266   } else if (size == 2) {
00267     x = pos2(0);
00268     y = pos2(1);
00269     z = 0;
00270   } else {
00271     x = pos2(0);
00272     y = pos2(1);
00273     z = pos2(2);
00274   }  
00275   point = new MYPOINT(2,x,y,z);
00276   r = theMap->getRed(V2);
00277   g = theMap->getGreen(V2);
00278   b = theMap->getBlue(V2);
00279   point->r = r;
00280   point->g = g;
00281   point->b = b;
00282     
00283   theFace->AddPoint(*point);
00284 
00285   if (aFile == 1) {
00286     theFile << x << " " << y << " " << z << " " << r << " " << g << " " << b << " " << endln;
00287   }
00288 
00289   FACE &res1 = theView->transform(*theFace);
00290   // opserr << "X11Renderer: face after view " << theFace;      
00291   FACE &res2 = theProjection->transform(res1);
00292   // opserr << "X11Renderer: face after projection " << theFace;        
00293   FACE &res3 = theClipping->transform(res2);
00294   // opserr << "X11Renderer: face after clipping " << theFace;          
00295   FACE &res4 = theViewport->transform(res3);
00296   // opserr << "X11Renderer: face after viewport " << theFace;            
00297   theScan->scanLine(res4);
00298   return 0;  
00299 }
00300 
00301 int 
00302 X11Renderer::drawLine(const Vector &pos1, const Vector &pos2, 
00303                       const Vector &rgb1, const Vector &rgb2,
00304                       int width, int style)
00305 {
00306   FACE *theFace = new FACE();   
00307   
00308   int size;
00309   float x,y,z, r, g, b;
00310   MYPOINT *point;
00311 
00312   // add POINTs to the FACE  
00313   size = pos1.Size();
00314   if (size == 1) {
00315     x = pos1(0);
00316     y = 0;
00317     z = 0;
00318   } else if (size == 2) {
00319     x = pos1(0);
00320     y = pos1(1);
00321     z = 0;
00322   } else {
00323     x = pos1(0);
00324     y = pos1(1);
00325     z = pos1(2);
00326   }  
00327   point = new MYPOINT(1,x,y,z);
00328   r = rgb1(0);
00329   g = rgb1(1);
00330   b = rgb1(2);
00331   point->r = r;
00332   point->g = g;
00333   point->b = b;
00334     
00335   theFace->AddPoint(*point);
00336 
00337   if (aFile == 1) {
00338     theFile << "Line\n" << x << " " << y << " " << z << " " << r << " " << g << " " << b << " " << endln;
00339   }
00340 
00341   size = pos2.Size();
00342   if (size == 1) {
00343     x = pos2(0);
00344     y = 0;
00345     z = 0;
00346   } else if (size == 2) {
00347     x = pos2(0);
00348     y = pos2(1);
00349     z = 0;
00350   } else {
00351     x = pos2(0);
00352     y = pos2(1);
00353     z = pos2(2);
00354   }  
00355   point = new MYPOINT(2,x,y,z);
00356   r = rgb2(0);
00357   g = rgb2(1);
00358   b = rgb2(2);
00359   point->r = r;
00360   point->g = g;
00361   point->b = b;
00362     
00363   theFace->AddPoint(*point);
00364 
00365   if (aFile == 1) {
00366     theFile << x << " " << y << " " << z << " " << r << " " << g << " " << b << " " << endln;
00367   }
00368 
00369   FACE &res1 = theView->transform(*theFace);
00370   // opserr << "X11Renderer: face after view " << theFace;      
00371   FACE &res2 = theProjection->transform(res1);
00372   // opserr << "X11Renderer: face after projection " << theFace;        
00373   FACE &res3 = theClipping->transform(res2);
00374   // opserr << "X11Renderer: face after clipping " << theFace;          
00375   FACE &res4 = theViewport->transform(res3);
00376   // opserr << "X11Renderer: face after viewport " << theFace;            
00377   theScan->scanLine(res4);
00378   return 0;  
00379 }
00380 
00381 
00382 
00383 
00384 
00385 int 
00386 X11Renderer::drawPolygon(const Matrix &pos, const Vector &data)
00387 
00388 {
00389 #ifdef _G3DEBUG
00390   if (pos.noCols() != 3) {
00391     opserr <<"X11Renderer::drawPolygon - matrix needs 3 cols\n";
00392     return -1;
00393   }
00394   if (pos.noRows() != data.Size()) {
00395     opserr <<"X11Renderer::drawPolygon - matrix & vector incompatable\n";
00396     return -1;
00397   }
00398 #endif
00399 
00400   FACE *theFace = new FACE();   
00401 
00402   float posX,posY,posZ, r, g, b;
00403   double value;
00404   MYPOINT *point;
00405 
00406   // add POINTs to the FACE  
00407   int numRows = pos.noRows();
00408   for (int i=0; i<numRows; i++) {
00409     posX = pos(i,0);
00410     posY = pos(i,1);
00411     posZ = pos(i,2);
00412     value = data(i);
00413     r = theMap->getRed(value);
00414     g = theMap->getGreen(value);
00415     b = theMap->getBlue(value);      
00416 
00417     if (aFile == 1) {
00418       theFile << posX << " " << posY << " " << posZ << " " << r 
00419               << " " << g << " " << b << " " << endln;
00420     }   
00421 
00422     point = new MYPOINT(1,posX,posY,posZ);
00423     point->r = r;
00424     point->g = g;
00425     point->b = b;
00426     
00427     theFace->AddPoint(*point);
00428   }
00429 
00430   // display the face
00431   FACE &res1 = theView->transform(*theFace);
00432   // opserr << "X11Renderer: face after view " << theFace;      
00433   FACE &res2 = theProjection->transform(res1);
00434   // opserr << "X11Renderer: face after projection " << theFace;        
00435   FACE &res3 = theClipping->transform(res2);
00436   // opserr << "X11Renderer: face after clipping " << theFace;          
00437   FACE &res4 = theViewport->transform(res3);
00438   // opserr << "X11Renderer: face after viewport " << theFace;            
00439   theScan->scanPolygon(res4);
00440   return 0;  
00441 }
00442 
00443 
00444 int 
00445 X11Renderer::drawPolygon(const Matrix &pos, const Matrix &data)
00446 
00447 {
00448 #ifdef _G3DEBUG
00449   if (pos.noCols() != 3 || data.noCols() != 3) {
00450     opserr <<"X11Renderer::drawPolygon - matrix needs 3 cols\n";
00451     return -1;
00452   }
00453   if (pos.noRows() != data.noRows()) {
00454     opserr <<"X11Renderer::drawPolygon - matrix & vector incompatable\n";
00455     return -1;
00456   }
00457 #endif
00458 
00459   FACE *theFace = new FACE();   
00460 
00461   float posX,posY,posZ, r, g, b;
00462   MYPOINT *point;
00463 
00464   // add POINTs to the FACE  
00465   int numRows = pos.noRows();
00466   for (int i=0; i<numRows; i++) {
00467     posX = pos(i,0);
00468     posY = pos(i,1);
00469     posZ = pos(i,2);
00470     r = data(i,0);
00471     g = data(i,1);
00472     b = data(i,2);      
00473 
00474     if (aFile == 1) {
00475       theFile << posX << " " << posY << " " << posZ << " " << r 
00476               << " " << g << " " << b << " " << endln;
00477     }   
00478 
00479     point = new MYPOINT(1,posX,posY,posZ);
00480     point->r = r;
00481     point->g = g;
00482     point->b = b;
00483     
00484     theFace->AddPoint(*point);
00485   }
00486 
00487   // display the face
00488   FACE &res1 = theView->transform(*theFace);
00489   // opserr << "X11Renderer: face after view " << theFace;      
00490   FACE &res2 = theProjection->transform(res1);
00491   // opserr << "X11Renderer: face after projection " << theFace;        
00492   FACE &res3 = theClipping->transform(res2);
00493   // opserr << "X11Renderer: face after clipping " << theFace;          
00494   FACE &res4 = theViewport->transform(res3);
00495   // opserr << "X11Renderer: face after viewport " << theFace;            
00496   theScan->scanPolygon(res4);
00497   return 0;  
00498 }
00499 
00500 
00501 
00502 int 
00503 X11Renderer::drawText(const Vector &pos, char *text, int length,
00504                       char horizontalJustify, char verticalJustify)
00505 {
00506   MYPOINT *point;
00507 
00508   // add POINTs to the FACE  
00509   int size = pos.Size();
00510   float x,y,z;
00511   if (size == 1) {
00512     x = pos(0);
00513     y = 0;
00514     z = 0;
00515   } else if (size == 2) {
00516     x = pos(0);
00517     y = pos(1);
00518     z = 0;
00519   } else {
00520     x = pos(0);
00521     y = pos(1);
00522     z = pos(2);
00523   }  
00524   point = new MYPOINT(1,x,y,z);
00525 
00526   MYPOINT *res =0;
00527   res = theView->transformP(point);
00528   if (res != 0) {
00529       res = theProjection->transformP(res);
00530   }
00531   if (res != 0) {
00532       res = theClipping->transformP(res);
00533   }
00534 
00535   if (res != 0) {
00536       res = theViewport->transformP(res);
00537   }
00538   if (res != 0) {
00539       float x = res->p[0];
00540       float y = res->p[1];
00541       theDevice->drawText(x,y, text, length);
00542   }
00543   
00544   if (res != 0)
00545       delete res;
00546   
00547   return 0;
00548 }
00549 
00550 /*
00551 int 
00552 X11Renderer::drawLText(const Vector &pos, char *text, int length,
00553                        char horizontalJustify, char verticalJustify)
00554 {
00555   // add POINTs to the FACE  
00556   int size = pos.Size();
00557   float x,y;
00558   if (size == 1) {
00559     x = pos(0);
00560     y = 0;
00561   } else if (size == 2) {
00562     x = pos(0);
00563     y = pos(1);
00564   } else {
00565     x = pos(0);
00566     y = pos(1);
00567   }  
00568 
00569   theDevice->drawText(x,y, text, length);
00570   
00571   return 0;
00572 }
00573 */
00574 
00575 
00576 
00577 int
00578 X11Renderer::displayFace(FACE &theFace)
00579 {
00580   // opserr << "X11Renderer: input face " << theFace;    
00581   FACE &res1 = theView->transform(theFace);
00582   // opserr << "X11Renderer: face after view " << theFace;      
00583   FACE &res2 = theProjection->transform(res1);
00584   // opserr << "X11Renderer: face after projection " << theFace;        
00585   FACE &res3 = theClipping->transform(res2);
00586   // opserr << "X11Renderer: face after clipping " << theFace;          
00587   FACE &res4 = theViewport->transform(res3);
00588   // opserr << "X11Renderer: face after viewport " << theFace;            
00589   theScan->scanPolygon(res4);
00590   return 0;
00591 }
00592 
00593 
00594 
00595 
00596 int 
00597 X11Renderer::setVRP(float x, float y, float z)
00598 {
00599   theView->vrp[0] = x;
00600   theView->vrp[1] = y;
00601   theView->vrp[2] = z;
00602 
00603   return 0;
00604 }
00605 
00606 int 
00607 X11Renderer::setVPN(float x, float y, float z)
00608 {
00609   theView->vpn[0] = x;
00610   theView->vpn[1] = y;
00611   theView->vpn[2] = z;
00612 
00613   return 0;
00614 }
00615 
00616 int 
00617 X11Renderer::setVUP(float x, float y, float z)
00618 {
00619   theView->vuv[0] = x;
00620   theView->vuv[1] = y;
00621   theView->vuv[2] = z;
00622 
00623   return 0;
00624 }
00625 
00626 int 
00627 X11Renderer::setViewWindow(float umin, float umax, float vmin, float vmax)
00628 {
00629   if (umin > umax || vmin > vmax) {
00630       opserr << "X11Renderer::setViewWindow() - invalid window ";
00631       opserr << umin << " "<< umax << " "<< vmin << " "<< vmax << endln;
00632       return -1;
00633   }
00634 
00635   theProjection->vpwindow[0] = umin;
00636   theProjection->vpwindow[1] = umax;
00637   theProjection->vpwindow[2] = vmin;
00638   theProjection->vpwindow[3] = vmax;
00639 
00640   return 0;
00641 }
00642 
00643 int 
00644 X11Renderer::setPlaneDist(float anear, float afar) 
00645 {
00646 
00647    if ((anear < 0.0) || (afar < 0.0)) {
00648       opserr << "X11Renderer::setPlaneDist() - invalid planes";
00649       opserr << anear << " " << afar << endln;
00650       return -1;
00651   }
00652   
00653   theProjection->planedist[0] = anear;
00654   theProjection->planedist[2] = afar;
00655 
00656   return 0;
00657 }
00658 
00659 int 
00660 X11Renderer::setProjectionMode(const char *newMode)
00661 {
00662   int projectionMode = 0;
00663   if ((strcmp(newMode, "parallel") == 0) || (strcmp(newMode, "Parallel") == 0))
00664     projectionMode = PARALLEL_MODE;
00665   else if ((strcmp(newMode, "perspective") == 0) || (strcmp(newMode, "Perspective") == 0))
00666 
00667   theProjection->projection_mode = projectionMode;
00668   return 0;
00669 }
00670 
00671 int 
00672 X11Renderer::setFillMode(const char *newMode)
00673 {
00674   int fillMode = 0;
00675   if ((strcmp(newMode, "wire") == 0) || (strcmp(newMode, "Wire") == 0))
00676     fillMode = WIRE_MODE;
00677   else if ((strcmp(newMode, "fill") == 0) || (strcmp(newMode, "Fill") == 0))
00678     fillMode = FILL_MODE;
00679 
00680   theScan->setFillMode(fillMode);
00681   return 0;
00682 }
00683 
00684 // eye location
00685 int 
00686 X11Renderer::setPRP(float u, float v, float n){
00687   theProjection->cop[0] = u;
00688   theProjection->cop[1] = v;
00689   theProjection->cop[2] = n;
00690 
00691   return 0;
00692 }
00693     
00694 int 
00695 X11Renderer::setPortWindow(float left, float right, 
00696                              float bottom, float top)
00697 {
00698   if (left < -1 || right > 1 || bottom < -1 || top > 1
00699       || left > right || bottom > top) {
00700       
00701       opserr << "X11Renderer::setPortWindow() - bounds invalid ";
00702       opserr << left << " "<< right << " "<< bottom << " "<< top << endln;
00703       return -1;
00704   }
00705 
00706   theViewport->portwindow[0] = left;
00707   theViewport->portwindow[1] = right;
00708   theViewport->portwindow[2] = bottom;
00709   theViewport->portwindow[3] = top;
00710 
00711   return 0;
00712 }
00713 

Generated on Mon Oct 23 15:05:27 2006 for OpenSees by doxygen 1.5.0