SplinePath.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include <cassert>
25 #include <cmath>
26 
28 #include "SplinePath.h"
29 
31 {
32 }
33 
34 
35 QPainterPath armarx::SplinePath::simplePath(armarx::QPointList simpleControlPoints)
36 {
37  //assert((simpleControlPoints.size() == 2) || (simpleControlPoints.size() % 3 == 1));
38 
39  QPainterPath path;
40 
41  if (simpleControlPoints.size() == 2)
42  {
43  // ARMARX_DEBUG_S << "Direct Line";
44  path.moveTo(simpleControlPoints.first());
45  path.lineTo(simpleControlPoints.last());
46  }
47  else if (simpleControlPoints.size() == 3)
48  {
49  // ARMARX_DEBUG_S << "Quad. path";
50  path.moveTo(simpleControlPoints.first());
51  path.quadTo(simpleControlPoints.at(1),
52  simpleControlPoints.last());
53  }
54  else
55  {
56  // ARMARX_DEBUG_S << "complex path";
57  path = complexPath(simpleControlPoints);
58  }
59 
60  return path;
61 
62  /*
63  * this doesn't work
64  *
65  QPointVector fullControlPoints;
66 
67  if(simpleControlPoints.size() > 1)
68  {
69  //add first given simpleControlPoint also as first complexControlPoint
70  auto iter = simpleControlPoints.begin();
71  fullControlPoints.push_back(*iter);
72 
73  //create point between the first two simpleControlPoints
74  QPoint2Array linePoints = line(*iter, *(iter + 1));
75  iter++;
76  //add the new point and the second simpleControlPoint to the fullControlPoints
77  fullControlPoints.insert(fullControlPoints.end(), linePoints.begin(), linePoints.end());
78 
79  while(iter < simpleControlPoints.end() - 1)
80  {
81  QPoint2Array linePoints = line(*iter, *(iter + 1));
82  iter++;
83  fullControlPoints.insert(fullControlPoints.end(), linePoints.begin(), linePoints.end());
84 
85  if(iter < simpleControlPoints.end() - 2)
86  {
87  QPoint4Array transitionPoints = smoothTransition(*iter, *(iter + 1), *(iter + 2));
88  iter = iter + 2;
89  fullControlPoints.insert(fullControlPoints.end(), transitionPoints.begin(), transitionPoints.end());
90  }
91  }
92  }
93 
94  //ensure that there are enough control points to create a cubic Bezierspline
95  while((fullControlPoints.size() % 3 != 1) || (fullControlPoints.size() < 4))
96  {
97  fullControlPoints.insert(fullControlPoints.begin(), simpleControlPoints.front());
98  }
99 
100  return complexPath(fullControlPoints);
101  */
102 }
103 
105 {
106  assert((fullControlPoints.size() % 3 == 1) && (fullControlPoints.size() > 3));
107 
108  QPainterPath path {fullControlPoints.at(0)};
109 
110  for (int i = 1; i < fullControlPoints.size(); i += 3)
111  {
112  assert(i + 2 < fullControlPoints.size()); //size of controlPoints == 1 mod 3
113 
114  path.cubicTo(fullControlPoints.at(i), fullControlPoints.at(i + 1), fullControlPoints.at(i + 2));
115  }
116 
117  /*
118  //print circles on control points
119  for(QPointF controlPoint : fullControlPoints)
120  {
121  path.addEllipse(controlPoint, 5, 5);
122  }
123  */
124 
125  //only for debugging
126  //pathToString(path);
127  return path;
128 }
129 
130 armarx::QPoint2Array armarx::SplinePath::line(QPointF start, QPointF end)
131 {
132  QPointF direction = end - start; //vector from start to end
133  QPointF middle = start + 0.5 * direction;
134 
135  QPoint2Array array {{middle, end}};
136  return array;
137 }
138 
139 armarx::QPoint4Array armarx::SplinePath::smoothTransition(QPointF start, QPointF middle, QPointF end)
140 {
141  QPointF parallelLine = end - start; //vector from start to end
142 
143  //put the two new points on the line parallel to parallelLine that goes through middle.
144  QPointF new1 = middle - relativeDistance * parallelLine;
145  QPointF new2 = middle + relativeDistance * parallelLine;
146 
147  QPoint4Array array {{new1, middle, new2, end}};
148  return array;
149 }
150 
151 void armarx::SplinePath::pathToString(QPainterPath path)
152 {
153  // ARMARX_LOG_S << "New path begins:";
154  for (size_t i = 0; i < static_cast<size_t>(path.elementCount()); i++)
155  {
156  QPainterPath::Element elem = path.elementAt(i);
157 
158  std::string type;
159 
160  switch (elem.type)
161  {
162  case QPainterPath::MoveToElement:
163  {
164  type = "Move";
165  break;
166  }
167 
168  case QPainterPath::LineToElement:
169  {
170  type = "Line";
171  break;
172  }
173 
174  case QPainterPath::CurveToElement:
175  {
176  type = "Curve";
177  break;
178  }
179 
180  default:
181  break;
182  }
183 
184  if (elem.type != QPainterPath::CurveToDataElement)
185  {
186  // ARMARX_LOG_S << type << " at (" << elem.x << ", " << elem.y << ")";
187  }
188  }
189 }
armarx::QPointList
QList< QPointF > QPointList
Definition: Transition.h:39
armarx::SplinePath::SplinePath
SplinePath()
Definition: SplinePath.cpp:30
armarx::SplinePath::simplePath
static QPainterPath simplePath(QPointList simpleControlPoints)
simplePath Constructs a cubic Beziercurve with an arbitrary number of control points.
Definition: SplinePath.cpp:35
armarx::QPoint2Array
std::array< QPointF, 2 > QPoint2Array
Definition: SplinePath.h:36
armarx::SplinePath::complexPath
static QPainterPath complexPath(QPointList fullControlPoints)
complexPath Constructs a cubic Beziercurve with a correctly specified vector of control points.
Definition: SplinePath.cpp:104
SplinePath.h
Logging.h
armarx::QPoint4Array
std::array< QPointF, 4 > QPoint4Array
Definition: SplinePath.h:37