Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:18:08

0001 // Tell emacs that this is a C++ source
0002 //  -*- C++ -*-.
0003 #ifndef G4MAIN_PHBBOX_H
0004 #define G4MAIN_PHBBOX_H
0005 
0006 #include <bitset>
0007 #include <iomanip>
0008 #include <iostream>
0009 
0010 // Clip a line using the Cohen-Southerland algorithm
0011 
0012 class PHBBox
0013 {
0014  public:
0015   //! Construct with the boundaries in x and y
0016   PHBBox(const double x0, const double y0, const double x1, const double y1)
0017     : _x0(x0)
0018     , _y0(y0)
0019     , _x1(x1)
0020     , _y1(y1)
0021   {
0022   }
0023 
0024   //! Given a line, clip it.  Return true if the line should appear in the
0025   //! bounding box.  The endpoints are updated to reflect the clipping
0026   bool ClipLine(double& x0, double& y0, double& x1, double& y1) const
0027   {
0028     int clipCode0 = ClipCode(x0, y0);  // clipping code for end point 0
0029     int clipCode1 = ClipCode(x1, y1);  // clipping code for end point 1
0030 
0031     while (clipCode0 || clipCode1)
0032     {
0033       // std::cout << "clipCode0 = " << std::bitset<4>(clipCode0).to_string() << std::endl;
0034       // std::cout << "clipCode1 = " << std::bitset<4>(clipCode1).to_string() << std::endl;
0035 
0036       if (clipCode0 & clipCode1) return false;
0037 
0038       int code = 0;
0039       if (clipCode0 > 0)
0040         code = clipCode0;  // clip the first point
0041       else
0042         code = clipCode1;  // clip the last point
0043 
0044       double x = 0, y = 0;
0045 
0046       if ((code & BOTTOM) == BOTTOM)
0047       {
0048         // Clip the line to the bottom of the box
0049         // std::cout << "Clip the line to the bottom of the box" << std::endl;
0050         y = _y0;
0051         x = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
0052       }
0053       else if ((code & TOP) == TOP)
0054       {
0055         // Clip the line to the top of the box
0056         // std::cout << "Clip the line to the top of the box" << std::endl;
0057         y = _y1;
0058         x = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
0059       }
0060       else if ((code & LEFT) == LEFT)
0061       {
0062         // std::cout << "Clip the line to the left of the box" << std::endl;
0063         x = _x0;
0064         y = y0 + (y1 - y0) * (x - x0) / (x1 - x0);
0065       }
0066       else if ((code & RIGHT) == RIGHT)
0067       {
0068         // std::cout << "Clip the line to the right of the box" << std::endl;
0069         x = _x1;
0070         y = y0 + (y1 - y0) * (x - x0) / (x1 - x0);
0071       }
0072 
0073       // std::cout << "x = " << x << ", y = " << y << std::endl;
0074 
0075       if (code == clipCode0)
0076       {
0077         // modify the first coord
0078         // std::cout << "modify the first coord" << std::endl;
0079         x0 = x;
0080         y0 = y;
0081         clipCode0 = ClipCode(x0, y0);
0082       }
0083       else
0084       {
0085         // modify the second coord
0086         // std::cout << "modify the second coord" << std::endl;
0087         x1 = x;
0088         y1 = y;
0089         clipCode1 = ClipCode(x1, y1);
0090       }
0091     }
0092 
0093     return true;
0094   }
0095 
0096   void Print(std::ostream& os = std::cout)
0097   {
0098     os << _x0 << " " << _y0 << ", " << _x1 << " " << _y1 << std::endl;
0099   }
0100 
0101  private:
0102   enum
0103   {
0104     RIGHT = 1,
0105     BOTTOM = 2,
0106     LEFT = 4,
0107     TOP = 8
0108   };
0109 
0110   int ClipCode(const double x, const double y) const
0111   {
0112     int code = 0;
0113     if (x > _x1)
0114       code |= RIGHT;
0115     else if (x < _x0)
0116       code |= LEFT;
0117     if (y > _y1)
0118       code |= TOP;
0119     else if (y < _y0)
0120       code |= BOTTOM;
0121     return code;
0122   }
0123 
0124   double _x0;
0125   double _y0;
0126   double _x1;
0127   double _y1;
0128 };
0129 
0130 #endif  // __PHBBOX_H__