moveit2
The MoveIt Motion Planning Framework for ROS 2.
collision_linear_model.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016, CITEC, Bielefeld University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Robert Haschke */
36 
39 
40 #include <QItemSelection>
41 #include <QPainter>
42 #include <cmath>
43 namespace moveit_setup
44 {
45 namespace srdf_setup
46 {
47 CollisionLinearModel::CollisionLinearModel(CollisionMatrixModel* src, QObject* parent) : QAbstractProxyModel(parent)
48 {
49  setSourceModel(src);
50 }
52 {
53  delete sourceModel();
54 }
55 
56 QModelIndex CollisionLinearModel::mapFromSource(const QModelIndex& sourceIndex) const
57 {
58  // map (row,column) index to linear index k
59  // http://stackoverflow.com/questions/27086195/linear-index-upper-triangular-matrix
60  int r = sourceIndex.row(), c = sourceIndex.column();
61  int n = this->sourceModel()->columnCount();
62  if (r == c)
63  return QModelIndex(); // main diagonal elements are invalid
64  if (r > c) // only consider upper triagonal matrix
65  std::swap(r, c); // swap r,c if below diagonal
66 
67  int k = (n * (n - 1) / 2) - (n - r) * ((n - r) - 1) / 2 + c - r - 1;
68  return index(k, 2);
69 }
70 
71 QModelIndex CollisionLinearModel::mapToSource(const QModelIndex& proxyIndex) const
72 {
73  // map linear index k to (row, column)
74  // http://stackoverflow.com/questions/27086195/linear-index-upper-triangular-matrix
75  int n = sourceModel()->columnCount();
76  int k = proxyIndex.row(); // linear (row) index
77  int r = n - 2 - static_cast<int>(sqrt(-8 * k + 4 * n * (n - 1) - 7) / 2.0 - 0.5);
78  int c = k + r + 1 - n * (n - 1) / 2 + (n - r) * ((n - r) - 1) / 2;
79  return sourceModel()->index(r, c);
80 }
81 
82 int CollisionLinearModel::rowCount(const QModelIndex& /*parent*/) const
83 {
84  int n = this->sourceModel()->rowCount();
85  return (n * (n - 1) / 2);
86 }
87 
88 int CollisionLinearModel::columnCount(const QModelIndex& /*parent*/) const
89 {
90  return 4;
91 }
92 
93 QModelIndex CollisionLinearModel::index(int row, int column, const QModelIndex& /*parent*/) const
94 {
95  return createIndex(row, column);
96 }
97 
98 QModelIndex CollisionLinearModel::parent(const QModelIndex& /*child*/) const
99 {
100  return QModelIndex();
101 }
102 
103 QVariant CollisionLinearModel::data(const QModelIndex& index, int role) const
104 {
105  QModelIndex src_index = this->mapToSource(index);
106  switch (index.column())
107  {
108  case 0: // link name 1
109  if (role != Qt::DisplayRole)
110  return QVariant();
111  else
112  return this->sourceModel()->headerData(src_index.row(), Qt::Horizontal, Qt::DisplayRole);
113  case 1: // link name 2
114  if (role != Qt::DisplayRole)
115  return QVariant();
116  return this->sourceModel()->headerData(src_index.column(), Qt::Vertical, Qt::DisplayRole);
117  case 2: // checkbox
118  if (role != Qt::CheckStateRole)
119  return QVariant();
120  else
121  return this->sourceModel()->data(src_index, Qt::CheckStateRole);
122  case 3: // reason
123  if (role != Qt::DisplayRole)
124  return QVariant();
125  else
126  return this->sourceModel()->data(src_index, Qt::ToolTipRole);
127  }
128  return QVariant();
129 }
130 
132 {
133  QModelIndex src_index = this->mapToSource(index(row, 0));
134  return qobject_cast<CollisionMatrixModel*>(sourceModel())->reason(src_index);
135 }
136 
137 bool CollisionLinearModel::setData(const QModelIndex& index, const QVariant& value, int role)
138 {
139  QModelIndex src_index = this->mapToSource(index);
140 
141  if (role == Qt::CheckStateRole)
142  {
143  sourceModel()->setData(src_index, value, role);
144  int r = index.row();
145  Q_EMIT dataChanged(this->index(r, 2), this->index(r, 3)); // reason changed too
146  return true;
147  }
148  return false; // reject all other changes
149 }
150 
151 void CollisionLinearModel::setEnabled(const QItemSelection& selection, bool value)
152 {
153  for (const auto idx : selection.indexes())
154  {
155  if (idx.column() != 2) // only consider checkbox indexes
156  continue;
157  setData(idx, value ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
158  }
159 }
160 
161 Qt::ItemFlags CollisionLinearModel::flags(const QModelIndex& index) const
162 {
163  if (index.column() == 2)
164  return Qt::ItemIsUserCheckable | QAbstractItemModel::flags(index);
165  else
166  return QAbstractItemModel::flags(index);
167 }
168 
169 QVariant CollisionLinearModel::headerData(int section, Qt::Orientation orientation, int role) const
170 {
171  if (role != Qt::DisplayRole)
172  return QVariant();
173 
174  if (orientation == Qt::Horizontal)
175  {
176  switch (section)
177  {
178  case 0:
179  return "Link A";
180  case 1:
181  return "Link B";
182  case 2:
183  return "Disabled";
184  case 3:
185  return "Reason to Disable";
186  }
187  }
188  else if (orientation == Qt::Vertical)
189  {
190  return section + 1;
191  }
192  return QVariant();
193 }
194 
195 SortFilterProxyModel::SortFilterProxyModel(QObject* parent) : QSortFilterProxyModel(parent), show_all_(false)
196 {
197 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
198  connect(this, SIGNAL(sourceModelChanged()), this, SLOT(initSorting()));
199 #endif
200 
201  // by default: sort by link A (col 0), then link B (col 1)
202  sort_columns_ << 0 << 1;
203  sort_orders_ << Qt::AscendingOrder << Qt::AscendingOrder;
204 }
205 
206 QVariant SortFilterProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
207 {
208  if (role == Qt::DisplayRole && orientation == Qt::Vertical)
209  return section + 1; // simply enumerate rows
210  else
211  return QSortFilterProxyModel::headerData(section, orientation, role);
212 }
213 
214 void SortFilterProxyModel::setEnabled(const QItemSelection& selection, bool value)
215 {
216  static_cast<CollisionLinearModel*>(sourceModel())->setEnabled(mapSelectionToSource(selection), value);
217 }
218 
219 void SortFilterProxyModel::initSorting()
220 {
221  int cols = sourceModel()->columnCount();
222  int prev_size = sort_columns_.size();
223  sort_columns_.resize(cols);
224  sort_orders_.resize(cols);
225 
226  // initialize new entries to -1
227  for (int i = prev_size, end = sort_columns_.size(); i < end; ++i)
228  sort_columns_[i] = -1;
229 }
230 
232 {
233  if (show_all_ == show_all)
234  return;
235 
236  show_all_ = show_all;
237  invalidateFilter();
238 }
239 
240 bool SortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
241 {
242  CollisionLinearModel* m = qobject_cast<CollisionLinearModel*>(sourceModel());
243  if (!(show_all_ || m->reason(source_row) <= ALWAYS ||
244  m->data(m->index(source_row, 2), Qt::CheckStateRole) == Qt::Checked))
245  return false; // not accepted due to check state
246 
247  const QRegExp regexp = this->filterRegExp();
248  if (regexp.isEmpty())
249  return true;
250 
251  return m->data(m->index(source_row, 0, source_parent), Qt::DisplayRole).toString().contains(regexp) ||
252  m->data(m->index(source_row, 1, source_parent), Qt::DisplayRole).toString().contains(regexp);
253 }
254 
255 // define a fallback comparison operator for QVariants
256 bool compareVariants(const QVariant& left, const QVariant& right)
257 {
258  if (left.userType() == QVariant::Type::Int)
259  return left.toInt() < right.toInt();
260  else
261  return left.toString() < right.toString();
262 }
263 
264 bool SortFilterProxyModel::lessThan(const QModelIndex& src_left, const QModelIndex& src_right) const
265 {
266  int row_left = src_left.row();
267  int row_right = src_right.row();
268  QAbstractItemModel* m = sourceModel();
269 
270  for (int i = 0, end = sort_columns_.size(); i < end && sort_columns_[i] >= 0; ++i)
271  {
272  int sc = sort_columns_[i];
273  int role = sc == 2 ? Qt::CheckStateRole : Qt::DisplayRole;
274  QVariant value_left = m->data(m->index(row_left, sc), role);
275  QVariant value_right = m->data(m->index(row_right, sc), role);
276 
277  if (value_left == value_right)
278  continue;
279 
280  bool smaller = compareVariants(value_left, value_right);
281  if (sort_orders_[i] == Qt::DescendingOrder)
282  smaller = !smaller;
283  return smaller;
284  }
285  return false;
286 }
287 
288 void SortFilterProxyModel::sort(int column, Qt::SortOrder order)
289 {
290  beginResetModel();
291  if (column < 0)
292  initSorting();
293  else
294  {
295  // remember sorting history
296  int prev_idx = sort_columns_.indexOf(column);
297  if (prev_idx < 0)
298  prev_idx = sort_columns_.size() - 1;
299  // remove old entries
300  sort_columns_.remove(prev_idx);
301  sort_orders_.remove(prev_idx);
302  // add new entries at front
303  sort_columns_.insert(0, column);
304  sort_orders_.insert(0, order);
305  }
306  QSortFilterProxyModel::sort(column, Qt::AscendingOrder);
307  endResetModel();
308 }
309 } // namespace srdf_setup
310 } // namespace moveit_setup
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
CollisionLinearModel(CollisionMatrixModel *src, QObject *parent=nullptr)
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
int columnCount(const QModelIndex &parent) const override
int rowCount(const QModelIndex &parent) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void setEnabled(const QItemSelection &selection, bool value)
QVariant data(const QModelIndex &index, int role) const override
QModelIndex parent(const QModelIndex &child) const override
bool lessThan(const QModelIndex &src_left, const QModelIndex &src_right) const override
void setEnabled(const QItemSelection &selection, bool value)
void sort(int column, Qt::SortOrder order) override
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
DisabledReason
Reasons for disabling link pairs. Append "in collision" for understanding. NOT_DISABLED means the lin...
bool compareVariants(const QVariant &left, const QVariant &right)
r
Definition: plan.py:56