Developer Documentation
PolyLineSelectionFunctions.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 #include "PolyLineSelectionPlugin.hh"
46 
48 
49  BaseObjectData* object = 0;
50 
51  if(!PluginFunctions::getObject(_objectId, object)) {
52  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
53  return;
54  }
55 
56  PolyLine* polyline = PluginFunctions::polyLine(object);
57  if(polyline) {
58 
59  for(unsigned int i = 0; i < polyline->n_vertices(); ++i) {
60  polyline->select_vertex(i);
61  }
62  }
63 
64  emit scriptInfo("selectAllVertices(ObjectId)");
65 }
66 
68 
69  BaseObjectData* object = 0;
70 
71  if(!PluginFunctions::getObject(_objectId, object)) {
72  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
73  return;
74  }
75 
76  PolyLine* polyline = PluginFunctions::polyLine(object);
77  if(polyline) {
78 
79  for(unsigned int i = 0; i < polyline->n_vertices(); ++i) {
80  polyline->deselect_vertex(i);
81  }
82  }
83 
84  emit scriptInfo("deselectAllVertices(ObjectId)");
85 }
86 
88 
89  BaseObjectData* object = 0;
90 
91  if(!PluginFunctions::getObject(_objectId, object)) {
92  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
93  return;
94  }
95 
96  PolyLine* polyline = PluginFunctions::polyLine(object);
97  if(polyline) {
98 
99  for(unsigned int i = 0; i < polyline->n_vertices(); ++i) {
100 
101  if(polyline->vertex_selected(i))
102  polyline->deselect_vertex(i);
103  else
104  polyline->select_vertex(i);
105  }
106  }
107 
108  emit scriptInfo("invertVertexSelection(ObjectId)");
109 }
110 
112 
113  BaseObjectData* object = 0;
114 
115  if(!PluginFunctions::getObject(_objectId, object)) {
116  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
117  return;
118  }
119 
120  PolyLine* polyline = PluginFunctions::polyLine(object);
121  if(polyline) {
122 
123  bool breakWhile = false;
124  while(!breakWhile) {
125 
126  // Go over all vertices and delete the selected ones
127  bool oneFound = false;
128  unsigned int i = 0;
129  for(; i < polyline->n_vertices(); ++i) {
130 
131  if(polyline->vertex_selected(i)) {
132  polyline->delete_point(i);
133  oneFound = true;
134  }
135  if(oneFound)
136  break;
137  }
138 
139  if(i == polyline->n_vertices() && !oneFound) {
140  // We are through
141  breakWhile = true;
142  }
143  }
144  }
145 
146  emit scriptInfo("deleteSelectedVertices(ObjectId)");
147 }
148 
149 void PolyLineSelectionPlugin::selectVertices(int _objectId, const IdList& _ids, bool _deselect) {
150 
151  if(_ids.empty() ) return;
152 
153  BaseObjectData* object = 0;
154 
155  if(!PluginFunctions::getObject(_objectId, object)) {
156  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
157  return;
158  }
159 
160  PolyLine* polyline = PluginFunctions::polyLine(object);
161  if(polyline) {
162  for(IdList::const_iterator it = _ids.begin(); it != _ids.end(); ++it) {
163  if(*it < (int)polyline->n_vertices()) {
164  if(_deselect) polyline->deselect_vertex(*it);
165  else polyline->select_vertex(*it);
166  }
167  }
168  }
169 
170  QString selection = "selectVertices(ObjectId, [ " + QString::number(_ids[0]);
171 
172  for (uint i = 1 ; i < _ids.size(); ++i) {
173  selection += ", " + QString::number(_ids[i]);
174  }
175 
176  selection += " ])";
177 
178  emit scriptInfo(selection);
179 }
180 
182 
183  BaseObjectData* object = 0;
184 
185  IdList list;
186 
187  if(!PluginFunctions::getObject(_objectId, object)) {
188  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
189  return list;
190  }
191 
192  PolyLine* polyline = PluginFunctions::polyLine(object);
193  if(polyline) {
194 
195  for(unsigned int i = 0; i < polyline->n_vertices(); ++i) {
196  if(polyline->vertex_selected(i))
197  list.push_back(i);
198  }
199  }
200 
201  return list;
202 }
203 
204 //=======================================================================================
205 
207 
208  BaseObjectData* object = 0;
209 
210  if (!PluginFunctions::getObject(_objectId, object)) {
211  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
212  return;
213  }
214 
215  PolyLine* polyline = PluginFunctions::polyLine(object);
216  if (polyline) {
217 
218  for(unsigned int i = 0; i < polyline->n_edges(); ++i) {
219  polyline->select_edge(i);
220  }
221  }
222 
223  emit scriptInfo("selectAllEdges(ObjectId)");
224 }
225 
227 
228  BaseObjectData* object = 0;
229 
230  if (!PluginFunctions::getObject(_objectId, object)) {
231  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
232  return;
233  }
234 
235  PolyLine* polyline = PluginFunctions::polyLine(object);
236  if (polyline) {
237 
238  for(unsigned int i = 0; i < polyline->n_edges(); ++i) {
239  polyline->deselect_edge(i);
240  }
241  }
242 
243  emit scriptInfo("deselectAllEdges(ObjectId)");
244 }
245 
247 
248  BaseObjectData* object = 0;
249 
250  if (!PluginFunctions::getObject(_objectId, object)) {
251  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
252  return;
253  }
254 
255  PolyLine* polyline = PluginFunctions::polyLine(object);
256  if (polyline) {
257 
258  for(unsigned int i = 0; i < polyline->n_edges(); ++i) {
259  if(polyline->edge_selected(i))
260  polyline->deselect_edge(i);
261  else
262  polyline->select_edge(i);
263  }
264  }
265 
266  emit scriptInfo("deselectAllEdges(ObjectId)");
267 }
268 
270 
271  BaseObjectData* object = 0;
272 
273  if(!PluginFunctions::getObject(_objectId, object)) {
274  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
275  return;
276  }
277 
278  PolyLine* polyline = PluginFunctions::polyLine(object);
279  if(polyline) {
280 
281  bool breakWhile = false;
282  while(!breakWhile) {
283 
284  // Go over all vertices and delete the selected ones
285  bool oneFound = false;
286  unsigned int i = 0;
287  for(; i < polyline->n_edges(); ++i) {
288 
289  if(polyline->edge_selected(i)) {
290  if(i+1 < polyline->n_vertices() /*Should always be the case*/) {
291 
292  if(!polyline->is_closed() && (i == 0 || i == polyline->n_edges()-1)) {
293  // Test if we're considering a boundary edge
294  // (only in not closed polylines)
295 
296  // Only delete one point (the boundary vertex)
297  if(i == 0)
298  polyline->delete_point(i);
299  else
300  polyline->delete_point(i+1);
301 
302  oneFound = true;
303 
304  } else {
305  // Delete both incident points
306  polyline->delete_point(i);
307  // Note: Index i+1 is now i after deletion
308  polyline->delete_point(i);
309  oneFound = true;
310  }
311  }
312  }
313  if(oneFound)
314  break;
315  }
316 
317  if(i == polyline->n_edges() && !oneFound) {
318  // We are through
319  breakWhile = true;
320  }
321  }
322  }
323 
324  emit scriptInfo("deleteSelectedEdges(ObjectId)");
325 }
326 
327 void PolyLineSelectionPlugin::selectEdges(int _objectId, const IdList& _ids, bool _deselect) {
328 
329  if(_ids.empty() ) return;
330 
331  BaseObjectData* object = 0;
332 
333  if (!PluginFunctions::getObject(_objectId, object)) {
334  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
335  return;
336  }
337 
338  PolyLine* polyline = PluginFunctions::polyLine(object);
339  if (polyline) {
340  for (IdList::const_iterator it = _ids.begin(); it != _ids.end(); ++it) {
341 
342  if(*it < (int)polyline->n_edges()) {
343  if(_deselect) polyline->deselect_edge(*it);
344  else polyline->select_edge(*it);
345  }
346  }
347  }
348 
349  QString selection = "selectEdges(ObjectId, [ " + QString::number(_ids[0]);
350 
351  for (uint i = 1 ; i < _ids.size(); ++i) {
352  selection += ", " + QString::number(_ids[i]);
353  }
354 
355  selection += " ])";
356 
357  emit scriptInfo(selection);
358 }
359 
361 
362  BaseObjectData* object = 0;
363 
364  IdList list;
365 
366  if(!PluginFunctions::getObject(_objectId, object)) {
367  emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
368  return list;
369  }
370 
371  PolyLine* polyline = PluginFunctions::polyLine(object);
372  if(polyline) {
373 
374  for(unsigned int i = 0; i < polyline->n_edges(); ++i) {
375  if(polyline->edge_selected(i))
376  list.push_back(i);
377  }
378  }
379 
380  return list;
381 }
IdList getEdgeSelection(int _objectId)
Get current edge selection.
void invertEdgeSelection(int _objectId)
Invert edge selection.
PolyLine * polyLine(BaseObjectData *_object)
Get a poly Line from an object.
void deselectAllVertices(int _objectId)
Deselect all vertices of a skeleton.
void selectAllEdges(int _objectId)
Select all edges of a skeleton.
void deselectAllEdges(int _objectId)
Deselect all edges of a skeleton.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
void selectEdges(int _objectId, const IdList &_ids, bool _deselect=false)
Select specific edges of a skeleton.
void deleteSelectedVertices(int _objectId)
Delete selected vertices.
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:181
void selectAllVertices(int _objectId)
Select all vertices of a skeleton.
void selectVertices(int _objectId, const IdList &_ids, bool _deselect=false)
Select specific vertices of a skeleton.
void delete_point(int _idx)
Delete point at _idx.
void deleteSelectedEdges(int _objectId)
Delete selected edges.
void invertVertexSelection(int _objectId)
Invert vertex selection.
size_t n_vertices() const
Get number of vertices.
Definition: PolyLineT.hh:116
IdList getVertexSelection(int _objectId)
Get current vertex selection.
bool is_closed() const
Check if the polyline is marked as closed.
Definition: PolyLineT.hh:107
size_t n_edges() const
Get number of edges.