Clipping.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.2 $
00022 // $Date: 2003/02/14 23:01:57 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/renderer/Clipping.cpp,v $
00024                                                                         
00025                                                                         
00026 #include "Clipping.h"
00027 #include "Projection.h"
00028 
00029 #ifndef TRUE
00030 #define TRUE 0
00031 #endif
00032 
00033 #ifndef FALSE
00034 #define FALSE 1
00035 #endif
00036 
00037 Clipping::Clipping()
00038 {
00039 
00040 }
00041 
00042 Clipping::~Clipping()
00043 {
00044 
00045 }
00046 
00047 int
00048 Clipping::update(void){
00049   X = 1.0;
00050   Y = 1.0;
00051   Zfar = -1.0;
00052   Znear = 0.0;
00053   return 0;
00054 }
00055 
00056 FACE &CLIP_X(FACE &, float, float, float, float); // creates new, deletes old
00057 FACE &CLIP_Y(FACE &, float, float, float, float); // creates new, deletes old
00058 FACE &CLIP_Z(FACE &, float, float, float, float); // creates new, deletes old
00059 
00060 FACE &
00061 Clipping::transform(FACE &input)
00062 {
00063   // opserr << "Clipping:1\n" << input;
00064   FACE &clippedX = CLIP_X(input,X,Y,Zfar,Znear);
00065   // opserr << "Clipping:2\n" << clippedX;
00066   FACE &clippedY = CLIP_Y(clippedX,X,Y,Zfar,Znear);
00067   // opserr << "Clipping:3\n" << clippedY;
00068   FACE &clippedZ = CLIP_Z(clippedY,X,Y,Zfar,Znear);
00069   // opserr << "Clipping:4\n" << clippedZ;
00070   return clippedZ;
00071 }
00072 
00073 
00074 MYPOINT *
00075 Clipping::transformP(MYPOINT *input)
00076 {
00077     if (input != 0) {
00078         float x = input->p[0];
00079         float y = input->p[1];
00080         float z = input->p[2];
00081         
00082         if (x < -X || x > X || y < -Y || Y > Y ||
00083             z > Znear || z < Zfar) {
00084             
00085             delete input;
00086             return 0;
00087         }
00088     }
00089     return input;
00090 }
00091 
00092 
00093 FACE &
00094 CLIP_X(FACE &input, float X, float Y, float Zfar, float Znear)
00095 {
00096 // this first procedure is used to clip against pos and
00097   FACE  *output;
00098   MYPOINT *Point1, *Point2, *P1, *P2;
00099   float x1,y1,z1,x2,y2,z2;
00100   float sx, sy, sz, t;
00101   float svwx,svwy,svwz, vwx1,vwx2,vwy1,vwy2,vwz1,vwz2; // point viewing stuff
00102   int out1, out1a, out2;
00103  // float snx,sny,snz, nx1,nx2,ny1,ny2,nz1,nz2;          // the point normals
00104   float r1,r2, g1,g2, b1, b2, sr, sg, sb;
00105 
00106   // set up the new face
00107   output = new FACE();
00108 
00109   // check for quick return
00110   MYPOINT *point = input.pointList.GetHead();
00111   if (point == 0) {  
00112       delete &input;
00113       return *output;      
00114   }
00115   
00116   // first clip against the pos X axis, remember I have a cube so it's easy
00117   FOR_EACH_PAIR(Point1,Point2,input.pointList,{
00118       x1 = Point1->X();    x2 = Point2->X();
00119       y1 = Point1->Y();    y2 = Point2->Y();
00120       z1 = Point1->Z();    z2 = Point2->Z();
00121 
00122       r1 = Point1->r;     r2 = Point2->r;
00123       g1 = Point1->g;     g2 = Point2->g;
00124       b1 = Point1->b;     b2 = Point2->b;
00125 
00126       vwx1 = Point1->Viewp[0]; vwx2 = Point2->Viewp[0];
00127       vwy1 = Point1->Viewp[1]; vwy2 = Point2->Viewp[1];
00128       vwz1 = Point1->Viewp[2]; vwz2 = Point2->Viewp[2];
00129 
00130        
00131       // first determine in or out
00132       if (x1 > X) 
00133           out1 = TRUE;
00134       else
00135           out1 = FALSE;
00136       if (x2 > X)
00137           out2 = TRUE;
00138       else
00139           out2 = FALSE;
00140       
00141       if (out1 == FALSE || out2 == FALSE) {
00142           // clip if either of points is in against pos X
00143               
00144           sx = x2-x1;
00145           sy = y2-y1;
00146           sz = z2-z1;
00147       
00148           sr = r2 - r1;  sg = g2 - g1;  sb = b2 - b1;
00149           svwx = vwx2 - vwx1; svwy = vwy2 - vwy1; svwz = vwz2 - vwz1;
00150 
00151           if (out1 == TRUE) {
00152               t = (X-x1)/sx;
00153               x1 = x1 +t*sx;
00154               y1 = y1 +t*sy;
00155               z1 = z1 +t*sz;
00156         
00157               r1 = r1 + t*sr;
00158               g1 = g1 + t*sg;
00159               b1 = b1 + t*sb;
00160 
00161               vwx1 = vwx1 + t*svwx;
00162               vwy1 = vwy1 + t*svwy;
00163               vwz1 = vwz1 + t*svwz;
00164               
00165           }
00166           if (out2 == TRUE) {
00167               t = (X-x1)/sx;
00168               x2 = x1 +t*sx;
00169               y2 = y1 +t*sy;
00170               z2 = z1 +t*sz;
00171                   
00172               r2 = r1 + t*sr;
00173               g2 = g1 + t*sg;
00174               b2 = b1 + t*sb;
00175 
00176               vwx2 = vwx1 + t*svwx;
00177               vwy2 = vwy1 + t*svwy;
00178               vwz2 = vwz1 + t*svwz;
00179           }          
00180 
00181           // now clip against -X
00182           // first determine in or out
00183           out1a = out1;
00184           if (x1 < -X) 
00185               out1 = TRUE;
00186           else
00187               out1 = FALSE;
00188           if (x2 < -X)
00189               out2 = TRUE;
00190           else
00191               out2 = FALSE;
00192       
00193       // clip if either in
00194       if (out1 == FALSE || out2 == FALSE) {
00195   
00196         sx = x2-x1;
00197         sy = y2-y1;
00198         sz = z2-z1;
00199 
00200         sr = r2 - r1;  sg = g2 - g1;  sb = b2 - b1;
00201         svwx = vwx2 - vwx1; svwy = vwy2 - vwy1; svwz = vwz2 - vwz1;
00202         
00203         if (out1 == TRUE) {
00204           t = (-X-x1)/sx;
00205           x1 = x1 +t*sx;
00206           y1 = y1 +t*sy;
00207           z1 = z1 +t*sz;
00208           
00209           r1 = r1 + t*sr;
00210           g1 = g1 + t*sg;
00211           b1 = b1 + t*sb;
00212 
00213           vwx1 = vwx1 + t*svwx;
00214           vwy1 = vwy1 + t*svwy;
00215           vwz1 = vwz1 + t*svwz;
00216           
00217         }
00218         if (out2 == TRUE) {
00219           t = (-X-x1)/sx;
00220           x2 = x1 +t*sx;
00221           y2 = y1 +t*sy;
00222           z2 = z1 +t*sz;
00223           
00224           r2 = r1 + t*sr;
00225           g2 = g1 + t*sg;
00226           b2 = b1 + t*sb;
00227 
00228           vwx2 = vwx1 + t*svwx;
00229           vwy2 = vwy1 + t*svwy;
00230           vwz2 = vwz1 + t*svwz;
00231 
00232         }          
00233         
00234         // only add new P1 if P1 not inside both
00235         if ((out1 == TRUE && out1a == FALSE) ||
00236             (out1 == FALSE && out1a == TRUE))  {  
00237           P1 = new MYPOINT(0,x1,y1,z1,0,0,0,vwx1,vwy1,vwz1);
00238           P1->r = r1; P1->b = b1; P1->g = g1;
00239           output->AddPoint(*P1);
00240         }
00241         P2 = new MYPOINT(0,x2,y2,z2,0,0,0,vwx2,vwy2,vwz2);
00242         P2->r = r2; P2->b = b2; P2->g = g2;
00243         output->AddPoint(*P2);
00244       }  // else do nothing
00245     } // else do nothing again
00246   });
00247 
00248   delete &input;
00249   return *output;
00250 }
00251 
00252 
00253 
00254 
00255 FACE &
00256 CLIP_Y(FACE &input, float X, float Y, float Zfar, float Znear)
00257 {
00258   // this first procedure is used to clip against pos and
00259   FACE  *output;
00260   MYPOINT *Point1, *Point2, *P1, *P2;
00261   float x1,y1,z1,x2,y2,z2;
00262   float sx, sy, sz, t;
00263   int out1, out1a, out2;
00264 
00265   float svwx,svwy,svwz, vwx1,vwx2,vwy1,vwy2,vwz1,vwz2; // point viewing stuff
00266   float r1,r2, g1,g2, b1, b2, sr, sg, sb;
00267 
00268   output = new FACE();
00269 
00270   // check for quick return
00271   MYPOINT *point = input.pointList.GetHead();
00272   if (point == 0) {  
00273       delete &input;
00274       return *output;      
00275   }
00276   
00277   
00278   
00279   // first clip against the pos X axis, remember I have a cube so it's easy
00280   FOR_EACH_PAIR(Point1,Point2,input.pointList,{
00281     x1 = Point1->X();
00282     x2 = Point2->X();
00283     y1 = Point1->Y();
00284     y2 = Point2->Y();
00285     z1 = Point1->Z();
00286     z2 = Point2->Z();
00287 
00288     vwx1 = Point1->Viewp[0]; vwx2 = Point2->Viewp[0];
00289     vwy1 = Point1->Viewp[1]; vwy2 = Point2->Viewp[1];
00290     vwz1 = Point1->Viewp[2]; vwz2 = Point2->Viewp[2];
00291 
00292     r1 = Point1->r;     r2 = Point2->r;
00293     g1 = Point1->g;     g2 = Point2->g;
00294     b1 = Point1->b;     b2 = Point2->b;
00295 
00296 
00297     // first determine in or out
00298     if (y1 > Y) 
00299       out1 = TRUE;
00300     else
00301       out1 = FALSE;
00302     if (y2 > Y)
00303       out2 = TRUE;
00304     else
00305       out2 = FALSE;
00306 
00307     if (out1 == FALSE || out2 == FALSE) {
00308       // clip if either of points is in against pos X
00309 
00310       sx = x2-x1;
00311       sy = y2-y1;
00312       sz = z2-z1;
00313 
00314       sr = r2 - r1;  sg = g2 - g1;  sb = b2 - b1;
00315       svwx = vwx2 - vwx1; svwy = vwy2 - vwy1; svwz = vwz2 - vwz1;
00316 
00317       if (out1 == TRUE) {
00318         t = (Y-y1)/sy;
00319         x1 = x1 +t*sx;
00320         y1 = y1 +t*sy;
00321         z1 = z1 +t*sz;
00322 
00323         r1 = r1 + t*sr;
00324         g1 = g1 + t*sg;
00325         b1 = b1 + t*sb;
00326 
00327         vwx1 = vwx1 + t*svwx;
00328         vwy1 = vwy1 + t*svwy;
00329         vwz1 = vwz1 + t*svwz;
00330 
00331       }
00332       if (out2 == TRUE) {
00333         t = (Y-y1)/sy;
00334         x2 = x1 +t*sx;
00335         y2 = y1 +t*sy;
00336         z2 = z1 +t*sz;
00337         
00338         r2 = r1 + t*sr;
00339         g2 = g1 + t*sg;
00340         b2 = b1 + t*sb;
00341 
00342         vwx2 = vwx1 + t*svwx;
00343         vwy2 = vwy1 + t*svwy;
00344         vwz2 = vwz1 + t*svwz;
00345 
00346       }          
00347 
00348       // now clip against -Y
00349 
00350       // first determine in or out
00351       out1a = out1;
00352       if (y1 < -Y) 
00353         out1 = TRUE;
00354       else
00355         out1 = FALSE;
00356       if (y2 < -Y)
00357         out2 = TRUE;
00358       else
00359         out2 = FALSE;
00360 
00361       // clip if either in
00362       if (out1 == FALSE || out2 == FALSE) {
00363         
00364         sx = x2-x1;
00365         sy = y2-y1;
00366         sz = z2-z1;
00367 
00368         sr = r2 - r1;  sg = g2 - g1;  sb = b2 - b1;     
00369         svwx = vwx2 - vwx1; svwy = vwy2 - vwy1; svwz = vwz2 - vwz1;
00370         
00371         if (out1 == TRUE) {
00372           t = (-Y-y1)/sy;
00373           x1 = x1 +t*sx;
00374           y1 = y1 +t*sy;
00375           z1 = z1 +t*sz;
00376 
00377           r1 = r1 + t*sr;
00378           g1 = g1 + t*sg;
00379           b1 = b1 + t*sb;
00380 
00381           vwx1 = vwx1 + t*svwx;
00382           vwy1 = vwy1 + t*svwy;
00383           vwz1 = vwz1 + t*svwz;
00384 
00385         }
00386         if (out2 == TRUE) {
00387           t = (-Y-y1)/sy;
00388           x2 = x1 +t*sx;
00389           y2 = y1 +t*sy;
00390           z2 = z1 +t*sz;
00391           
00392           r2 = r1 + t*sr;
00393           g2 = g1 + t*sg;
00394           b2 = b1 + t*sb;
00395 
00396           vwx2 = vwx1 + t*svwx;
00397           vwy2 = vwy1 + t*svwy;
00398           vwz2 = vwz1 + t*svwz;
00399           
00400         }          
00401 
00402         // only add new P1 if P1 not inside both
00403         if ((out1 == TRUE && out1a == FALSE) ||
00404             (out1 == FALSE && out1a == TRUE))  {  
00405           P1 = new MYPOINT(0,x1,y1,z1,0,0,0,vwx1,vwy1,vwz1);
00406           P1->r = r1; P1->b = b1; P1->g = g1;
00407           output->AddPoint(*P1);
00408         }
00409         P2 = new MYPOINT(0,x2,y2,z2,0,0,0,vwx2,vwy2,vwz2);
00410         P2->r = r2; P2->b = b2; P2->g = g2;
00411         output->AddPoint(*P2);
00412       }  // else do nothing
00413     } // else do nothing again
00414   });
00415 
00416   delete &input;
00417   return *output;
00418 }
00419 
00420 
00421 FACE &
00422 CLIP_Z(FACE &input, float X, float Y, float Zfar, float Znear)
00423 {
00424   // this first procedure is used to clip against pos and
00425   FACE  *output;
00426   MYPOINT *Point1, *Point2, *P1, *P2;
00427   float x1,y1,z1,x2,y2,z2;
00428   float sx, sy, sz, t;
00429   int out1, out1a, out2;
00430 
00431   float svwx,svwy,svwz, vwx1,vwx2,vwy1,vwy2,vwz1,vwz2; 
00432   float sr, sg, sb, r1,r2, g1,g2, b1, b2;// r,g,b;
00433 
00434   // set up the new face
00435   output = new FACE();
00436 
00437   // check for quick return
00438   MYPOINT *point = input.pointList.GetHead();
00439   if (point == 0) {  
00440       delete &input;
00441       return *output;      
00442   }  
00443   
00444   // first clip against the pos X axis, remember I have a cube so it's easy
00445   FOR_EACH_PAIR(Point1,Point2,input.pointList,{
00446     x1 = Point1->X();
00447     x2 = Point2->X();
00448     y1 = Point1->Y();
00449     y2 = Point2->Y();
00450     z1 = Point1->Z();
00451     z2 = Point2->Z();
00452 
00453     r1 = Point1->r;     r2 = Point2->r;
00454     g1 = Point1->g;     g2 = Point2->g;
00455     b1 = Point1->b;     b2 = Point2->b;
00456     
00457 
00458     vwx1 = Point1->Viewp[0]; vwx2 = Point2->Viewp[0];
00459     vwy1 = Point1->Viewp[1]; vwy2 = Point2->Viewp[1];
00460     vwz1 = Point1->Viewp[2]; vwz2 = Point2->Viewp[2];
00461 
00462     r1 = Point1->r;     r2 = Point2->r;
00463     g1 = Point1->g;     g2 = Point2->g;
00464     b1 = Point1->b;     b2 = Point2->b;
00465 
00466     // first determine in or out
00467     if (z1 < Zfar) 
00468       out1 = TRUE;
00469     else
00470       out1 = FALSE;
00471     if (z2 < Zfar)
00472       out2 = TRUE;
00473     else
00474       out2 = FALSE;
00475     
00476     if (out1 == FALSE || out2 == FALSE) {
00477       // clip if either of points is in against Zfar
00478       
00479       sx = x2-x1;
00480       sy = y2-y1;
00481       sz = z2-z1;
00482 
00483       sr = r2 - r1;  sg = g2 - g1;  sb = b2 - b1;             
00484       svwx = vwx2 - vwx1; svwy = vwy2 - vwy1; svwz = vwz2 - vwz1;
00485       
00486       if (out1 == TRUE) {
00487         t = (Zfar-z1)/sz;
00488         x1 = x1 +t*sx;
00489         y1 = y1 +t*sy;
00490         z1 = z1 +t*sz;
00491 
00492         r1 = r1 + t*sr;
00493         g1 = g1 + t*sg;
00494         b1 = b1 + t*sb;
00495         
00496         vwx1 = vwx1 + t*svwx;
00497         vwy1 = vwy1 + t*svwy;
00498         vwz1 = vwz1 + t*svwz;
00499 
00500       }
00501       if (out2 == TRUE) {
00502         t = (Zfar-z1)/sz;
00503         x2 = x1 +t*sx;
00504         y2 = y1 +t*sy;
00505         z2 = z1 +t*sz;
00506 
00507         r2 = r1 + t*sr;
00508         g2 = g1 + t*sg;
00509         b2 = b1 + t*sb;
00510 
00511         vwx2 = vwx1 + t*svwx;
00512         vwy2 = vwy1 + t*svwy;
00513         vwz2 = vwz1 + t*svwz;
00514         
00515       }          
00516 
00517       // now clip against Znear
00518       // first determine in or out
00519       out1a = out1;
00520       if (z1 > Znear) 
00521         out1 = TRUE;
00522       else
00523         out1 = FALSE;
00524       if (z2 > Znear)
00525         out2 = TRUE;
00526       else
00527         out2 = FALSE;
00528 
00529 
00530       // clip if either in
00531       if (out1 == FALSE || out2 == FALSE) {
00532   
00533         sx = x2-x1;
00534         sy = y2-y1;
00535         sz = z2-z1;
00536         
00537         sr = r2 - r1;  sg = g2 - g1;  sb = b2 - b1;           
00538         svwx = vwx2 - vwx1; svwy = vwy2 - vwy1; svwz = vwz2 - vwz1;
00539         
00540         if (out1 == TRUE) {
00541           t = (Znear-z1)/sz;
00542           x1 = x1 +t*sx;
00543           y1 = y1 +t*sy;
00544           z1 = z1 +t*sz;
00545 
00546           r1 = r1 + t*sr;
00547           g1 = g1 + t*sg;
00548           b1 = b1 + t*sb;         
00549 
00550           vwx1 = vwx1 + t*svwx;
00551           vwy1 = vwy1 + t*svwy;
00552           vwz1 = vwz1 + t*svwz;
00553 
00554         }
00555         if (out2 == TRUE) {
00556           t = (Znear-z1)/sz;
00557           x2 = x1 +t*sx;
00558           y2 = y1 +t*sy;
00559           z2 = z1 +t*sz;
00560           
00561           r2 = r1 + t*sr;
00562           g2 = g1 + t*sg;
00563           b2 = b1 + t*sb;
00564 
00565           vwx2 = vwx1 + t*svwx;
00566           vwy2 = vwy1 + t*svwy;
00567           vwz2 = vwz1 + t*svwz;
00568           
00569         }          
00570         
00571         if ((out1 == TRUE && out1a == FALSE) ||
00572             (out1 == FALSE && out1a == TRUE))  {  
00573 
00574 
00575           // only add new P1 if P1 not inside both
00576           P1 = new MYPOINT(0,x1,y1,z1,0,0,0,vwx1,vwy1,vwz1);
00577           P1->r = r1; P1->b = b1; P1->g = g1;
00578           output->AddPoint(*P1);
00579         }
00580         P2 = new MYPOINT(0,x2,y2,z2,0,0,0,vwx2,vwy2,vwz2);
00581         P2->r = r1; P2->b = b1; P2->g = g1;
00582         output->AddPoint(*P2);
00583       }  // else do nothing
00584     } // else do nothing again
00585   });
00586 
00587   delete &input;
00588   return *output;
00589 }
00590 

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