Fréchet View  1.6.0
A Tool for Exploring Fréchet Distance Algorithms
grid.cpp
Go to the documentation of this file.
1 
2 #include <grid.h>
3 
4 using namespace frechet;
5 using namespace data;
6 using namespace fs;
7 
9  : std::vector<Interval>(),
10  defaultLineStyle(SOLID),
11  lineStyles()
12 { }
13 
15 {
16  clear();
17 
18  int n = P.size();
19 
20  double x = 0.0;
21  for(int i=0; i < n-1; ++i) {
22  double x2 = x + numeric::segment_length(P,i);
23  push_back(Interval(x,x2));
24  x = x2;
25  }
26 }
27 
28 void GridAxis::setLineStyles(const LineStyleVector& styles, LineStyle defaultStyle)
29 {
30  lineStyles = styles;
31  Q_ASSERT (defaultStyle != DEFAULT); // DEFAULT := DEFAULT makes no sense
32  defaultLineStyle = defaultStyle;
33 }
34 
36 {
37  Q_ASSERT(i >= 0 && i <= size());
38  if ((i >= lineStyles.size()) || (lineStyles[i]==DEFAULT))
39  return defaultLineStyle;
40  else
41  return lineStyles[i];
42 }
43 
44 double GridAxis::map(double x) const {
45  if (empty())
46  return 0.0;
47  if (x==size())
48  return back().upper();
49  int i = trunc(x);
50  Q_ASSERT(i >= 0 && i < size());
51  return (*this)[i].mapFromUnitInterval(x-i);
52 }
53 
54 Interval GridAxis::map(const Interval &other) const {
55  double lower = map(other.lower());
56  double upper = map(other.upper());
57  return Interval(lower,upper);
58 }
59 
60 double GridAxis::length() const {
61  return back().upper();
62 }
63 
64 void Grid::setCurves(const Curve &P, const Curve &Q)
65 {
66  _hor.setCurve(P);
67  _vert.setCurve(Q);
68 }
69 
70 QSizeF Grid::extent() const {
71  return QSizeF(
72  _hor.empty() ? 0.0 : _hor.back().upper(),
73  _vert.empty() ? 0.0 : _vert.back().upper());
74 }
75 
76 QRectF Grid::cellBounds(int i, int j) const {
77  Q_ASSERT(i>=0 && i < _hor.size());
78  Q_ASSERT(j>=0 && j < _vert.size());
79 
80  QRectF r;
81  r.setCoords(
82  _hor[i].lower(), _vert[j].lower(),
83  _hor[i].upper(), _vert[j].upper());
84  return r;
85 }
86 
88  double x = _hor.map(p.x());
89  double y = _vert.map(p.y());
90  return Point(x,y);
91 }
92 
93 QLineF Grid::mapLine(QLineF l) {
94  Point p1 = mapPoint(l.p1());
95  Point p2 = mapPoint(l.p2());
96  return QLineF(p1,p2);
97 }
98 
99 QRectF Grid::mapRect(QRectF r) {
100  Point topleft = mapPoint(r.topLeft());
101  Point bottomright = mapPoint(r.bottomRight());
102  return QRectF(topleft,bottomright);
103 }
104 
106  Curve result;
107  foreach(Point p, c)
108  result.push_back(mapPoint(p));
109  return result;
110 }
111 
113 {
114  QLineF line (i,0.0, i,vert().size());
115  return mapLine(line);
116 }
117 
119 {
120  QLineF line (0.0,j, hor().size(),j);
121  return mapLine(line);
122 }
123 
124 QRectF Grid::sceneRect() {
125  QRectF r;
126  r.setCoords(0.0,0.0, hor().size(), vert().size());
127  return mapRect(r);
128 }
129 
130 Point Grid::mapToPoint(const Curve &C, double x)
131 {
132  Q_ASSERT(C.isClosed() || (x >= 0.0) && (x <= (C.size()-1)));
133  if (C.isClosed())
134  x = std::fmod(x,C.size()-1);
135  int i = (int)floor(x);
136  x -= i;
137  Point p = C[i % C.size()];
138  if (x==0.0) return p;
139 
140  Point q = C[(i+1) % C.size()];
141  return (1-x)*p + x*q;
142 }
143 
144 QLineF Grid::mapToSegment(const Curve &C, double x1, double x2)
145 {
146  return QLineF(mapToPoint(C, x1), mapToPoint(C, x2));
147 }
148 
149 Curve Grid::mapToSequence(const Curve& C, double x1, double x2)
150 {
151  Curve result;
152  int i1 = ceil(x1);
153  if (x1 < i1)
154  result.append(mapToPoint(C,x1));
155 
156  for( ; i1 < x2; i1 += 1)
157  result.append(mapToPoint(C,i1));
158 
159  result.append(mapToPoint(C,x2));
160  return result;
161 }
162 
163 Curve Grid::mapToSequence(const Curve& C, const std::vector<double>& seq)
164 {
165  Curve result;
166  for(int i=0; i < seq.size(); ++i)
167  result.push_back(mapToPoint(C,seq[i]));
168  return result;
169 }
static Curve mapToSequence(const Curve &C, double x1, double x2)
given a polygonal curve and two offsets, compute the sequence of line segment connecting both offsets
Definition: grid.cpp:149
QSizeF extent() const
Definition: grid.cpp:70
double map(double x) const
maps a value from [0..n] onto the axis [0..|P|]
Definition: grid.cpp:44
QRectF sceneRect()
Definition: grid.cpp:124
GridAxis _vert
arc lengths of Q == vertical grid coordinates
Definition: grid.h:124
LineStyle lineStyle(int i) const
Definition: grid.cpp:35
QSizeF size() const
Definition: grid.h:154
const GridAxis & vert() const
Definition: grid.h:143
static Point mapToPoint(const Curve &C, double x)
given a polygonal curve and an offset, compute the point on the curve
Definition: grid.cpp:130
global definitions for all algorithms.
std::vector< LineStyle > LineStyleVector
Definition: grid.h:26
LineStyle defaultLineStyle
default line style for drawing a grid line
Definition: grid.h:46
LineStyleVector lineStyles
custom line styles for grid lines
Definition: grid.h:48
static QLineF mapToSegment(const Curve &C, double x1, double x2)
given a polygonal curve and two offsets, compute the corresponding line segment
Definition: grid.cpp:144
void setCurve(const Curve &P)
compute the length of each polygon segment
Definition: grid.cpp:14
void setLineStyles(const LineStyleVector &linesStyles, LineStyle defaultLineStyle)
assign line styles for visualisation
Definition: grid.cpp:28
QLineF verticalGridLine(int i)
map a vertical line
Definition: grid.cpp:112
QRectF mapRect(QRectF r)
map a rectangle from [0..n]x[0..m] to the grid coordinates [0..|P|]x[0..|Q|]
Definition: grid.cpp:99
draw a solid line
Definition: grid.h:19
double upper() const
Definition: interval.h:96
QPointF Point
a point in the plane; with double floating point precision. This type is heavily used throughout all ...
Definition: types.h:14
void setCurves(const Curve &P, const Curve &Q)
calculate grid from input curves
Definition: grid.cpp:64
QPolygonF Curve
a polygonal curve in the plane; with double floating point precision. This type is heavily used throu...
Definition: types.h:20
Curve mapCurve(Curve c)
map a sequence of line segments from [0..n]x[0..m] to the grid coordinates [0..|P|]x[0....
Definition: grid.cpp:105
double lower() const
Definition: interval.h:92
double segment_length(const Curve &c, int i)
compute the length of a polygon segment
Definition: numeric.h:306
Definition: grid.h:18
GridAxis()
empty constructor
Definition: grid.cpp:8
Point mapPoint(Point p)
map a point from [0..n]x[0..m] to the grid coordinates [0..|P|]x[0..|Q|]
Definition: grid.cpp:87
const GridAxis & hor() const
Definition: grid.h:141
LineStyle
display style of grid lines in free-space diagram
Definition: grid.h:15
QRectF cellBounds(int i, int j) const
Definition: grid.cpp:76
GridAxis _hor
arc lengths of P == horizontal grid coordinates
Definition: grid.h:122
QLineF horizontalGridLine(int j)
map a horizontal line
Definition: grid.cpp:118
an interval of two double values.
Definition: interval.h:31
double length() const
Definition: grid.cpp:60
QLineF mapLine(QLineF l)
map a line segment from [0..n]x[0..m] to the grid coordinates [0..|P|]x[0..|Q|]
Definition: grid.cpp:93