INET Framework for OMNeT++/OMNEST
InstrumentUtil Class Reference

#include <InstrumentUtil.h>

Static Public Member Functions

static bool CohenSutherlandLineClip (double &x0, double &y0, double &x1, double &y1, double xmin, double xmax, double ymin, double ymax)
 Cohen–Sutherland clipping algorithm clips a line from P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with diagonal from (xmin, ymin) to (xmax, ymax). More...
 

Private Types

typedef int OutCode
 

Static Private Member Functions

static OutCode ComputeOutCode (double x, double y, double xmin, double xmax, double ymin, double ymax)
 

Static Private Attributes

static const int INSIDE = 0
 
static const int LEFT = 1
 
static const int RIGHT = 2
 
static const int BOTTOM = 4
 
static const int TOP = 8
 

Member Typedef Documentation

typedef int InstrumentUtil::OutCode
private

Member Function Documentation

bool InstrumentUtil::CohenSutherlandLineClip ( double &  x0,
double &  y0,
double &  x1,
double &  y1,
double  xmin,
double  xmax,
double  ymin,
double  ymax 
)
static

Cohen–Sutherland clipping algorithm clips a line from P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with diagonal from (xmin, ymin) to (xmax, ymax).

Returns true if the line segment is intersecting with the rectangle, false otherwise.

Referenced by PlotFigure::refresh().

39 {
40  // compute outcodes for P0, P1, and whatever point lies outside the clip rectangle
41  OutCode outcode0 = ComputeOutCode(x0, y0, xmin, xmax, ymin, ymax);
42  OutCode outcode1 = ComputeOutCode(x1, y1, xmin, xmax, ymin, ymax);
43  bool accept = false;
44 
45  while (true) {
46  if (!(outcode0 | outcode1)) { // Bitwise OR is 0. Trivially accept and get out of loop
47  accept = true;
48  break;
49  } else if (outcode0 & outcode1) { // Bitwise AND is not 0. Trivially reject and get out of loop
50  break;
51  } else {
52  // failed both tests, so calculate the line segment to clip
53  // from an outside point to an intersection with clip edge
54  double x, y;
55 
56  // At least one endpoint is outside the clip rectangle; pick it.
57  OutCode outcodeOut = outcode0 ? outcode0 : outcode1;
58 
59  // Now find the intersection point;
60  // use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0)
61  if (outcodeOut & TOP) { // point is above the clip rectangle
62  x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
63  y = ymax;
64  } else if (outcodeOut & BOTTOM) { // point is below the clip rectangle
65  x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
66  y = ymin;
67  } else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle
68  y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
69  x = xmax;
70  } else if (outcodeOut & LEFT) { // point is to the left of clip rectangle
71  y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
72  x = xmin;
73  }
74 
75  // Now we move outside point to intersection point to clip
76  // and get ready for next pass.
77  if (outcodeOut == outcode0) {
78  x0 = x;
79  y0 = y;
80  outcode0 = ComputeOutCode(x0, y0, xmin, xmax, ymin, ymax);
81  } else {
82  x1 = x;
83  y1 = y;
84  outcode1 = ComputeOutCode(x1, y1, xmin, xmax, ymin, ymax);
85  }
86  }
87  }
88  return accept;
89 }
static const int LEFT
Definition: InstrumentUtil.h:26
static const int RIGHT
Definition: InstrumentUtil.h:27
int OutCode
Definition: InstrumentUtil.h:23
static const int BOTTOM
Definition: InstrumentUtil.h:28
static const int TOP
Definition: InstrumentUtil.h:29
static OutCode ComputeOutCode(double x, double y, double xmin, double xmax, double ymin, double ymax)
Definition: InstrumentUtil.cc:20
InstrumentUtil::OutCode InstrumentUtil::ComputeOutCode ( double  x,
double  y,
double  xmin,
double  xmax,
double  ymin,
double  ymax 
)
staticprivate

Referenced by CohenSutherlandLineClip().

21 {
22  OutCode code;
23 
24  code = INSIDE; // initialised as being inside of [[clip window]]
25 
26  if (x < xmin) // to the left of clip window
27  code |= LEFT;
28  else if (x > xmax) // to the right of clip window
29  code |= RIGHT;
30  if (y < ymin) // below the clip window
31  code |= BOTTOM;
32  else if (y > ymax) // above the clip window
33  code |= TOP;
34 
35  return code;
36 }
static const int LEFT
Definition: InstrumentUtil.h:26
static const int RIGHT
Definition: InstrumentUtil.h:27
int OutCode
Definition: InstrumentUtil.h:23
static const int BOTTOM
Definition: InstrumentUtil.h:28
static const int TOP
Definition: InstrumentUtil.h:29
static const int INSIDE
Definition: InstrumentUtil.h:25

Member Data Documentation

const int InstrumentUtil::BOTTOM = 4
staticprivate
const int InstrumentUtil::INSIDE = 0
staticprivate

Referenced by ComputeOutCode().

const int InstrumentUtil::LEFT = 1
staticprivate
const int InstrumentUtil::RIGHT = 2
staticprivate
const int InstrumentUtil::TOP = 8
staticprivate

The documentation for this class was generated from the following files: