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

◆ OutCode

typedef int InstrumentUtil::OutCode
private

Member Function Documentation

◆ CohenSutherlandLineClip()

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.

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

Referenced by inet::PlotFigure::plot().

◆ ComputeOutCode()

InstrumentUtil::OutCode InstrumentUtil::ComputeOutCode ( double  x,
double  y,
double  xmin,
double  xmax,
double  ymin,
double  ymax 
)
staticprivate
11 {
12  OutCode code;
13 
14  code = INSIDE; // initialised as being inside of [[clip window]]
15 
16  if (x < xmin) // to the left of clip window
17  code |= LEFT;
18  else if (x > xmax) // to the right of clip window
19  code |= RIGHT;
20  if (y < ymin) // below the clip window
21  code |= BOTTOM;
22  else if (y > ymax) // above the clip window
23  code |= TOP;
24 
25  return code;
26 }

Referenced by CohenSutherlandLineClip().

Member Data Documentation

◆ BOTTOM

const int InstrumentUtil::BOTTOM = 4
staticprivate

◆ INSIDE

const int InstrumentUtil::INSIDE = 0
staticprivate

Referenced by ComputeOutCode().

◆ LEFT

const int InstrumentUtil::LEFT = 1
staticprivate

◆ RIGHT

const int InstrumentUtil::RIGHT = 2
staticprivate

◆ TOP

const int InstrumentUtil::TOP = 8
staticprivate

The documentation for this class was generated from the following files:
InstrumentUtil::INSIDE
static const int INSIDE
Definition: InstrumentUtil.h:15
InstrumentUtil::LEFT
static const int LEFT
Definition: InstrumentUtil.h:16
InstrumentUtil::OutCode
int OutCode
Definition: InstrumentUtil.h:13
InstrumentUtil::ComputeOutCode
static OutCode ComputeOutCode(double x, double y, double xmin, double xmax, double ymin, double ymax)
Definition: InstrumentUtil.cc:10
InstrumentUtil::RIGHT
static const int RIGHT
Definition: InstrumentUtil.h:17
InstrumentUtil::BOTTOM
static const int BOTTOM
Definition: InstrumentUtil.h:18
InstrumentUtil::TOP
static const int TOP
Definition: InstrumentUtil.h:19