Developer Documentation

BSplineCurveSelectionFunctions.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 #include "BSplineCurveSelectionPlugin.hh"
45
46 void BSplineCurveSelectionPlugin::selectAllControlPoints(int _objectId) {
47
48 BaseObjectData* object = 0;
49
50 if(!PluginFunctions::getObject(_objectId, object)) {
51 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
52 return;
53 }
54
55 BSplineCurve* curve = PluginFunctions::splineCurve(object);
56 BSplineCurveObject* co = PluginFunctions::bsplineCurveObject(object);
57
58 if(curve) {
59 for(unsigned int i = 0; i < curve->n_control_points(); ++i) {
60 curve->select_controlpoint(i);
61 }
62 }
63
64 // Switch to control point selection mode
65 if(co)
66 co->splineCurveNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::CONTROLPOINT);
67
68 emit scriptInfo("selectAllControlPoints(ObjectId)");
69 }
70
71 void BSplineCurveSelectionPlugin::deselectAllControlPoints(int _objectId) {
72
73 BaseObjectData* object = 0;
74
75 if(!PluginFunctions::getObject(_objectId, object)) {
76 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
77 return;
78 }
79
80 BSplineCurve* curve = PluginFunctions::splineCurve(object);
81 if(curve) {
82 for(unsigned int i = 0; i < curve->n_control_points(); ++i) {
83 curve->deselect_controlpoint(i);
84 }
85 }
86
87 emit scriptInfo("deselectAllControlPoints(ObjectId)");
88 }
89
90 void BSplineCurveSelectionPlugin::invertControlPointSelection(int _objectId) {
91
92 BaseObjectData* object = 0;
93
94 if(!PluginFunctions::getObject(_objectId, object)) {
95 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
96 return;
97 }
98
99 BSplineCurve* curve = PluginFunctions::splineCurve(object);
100 BSplineCurveObject* co = PluginFunctions::bsplineCurveObject(object);
101
102 if(curve) {
103 for(unsigned int i = 0; i < curve->n_control_points(); ++i) {
104 if(curve->controlpoint_selected(i))
105 curve->deselect_controlpoint(i);
106 else
107 curve->select_controlpoint(i);
108 }
109 }
110
111 // Switch to control point selection mode
112 if(co)
113 co->splineCurveNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::CONTROLPOINT);
114
115 emit scriptInfo("invertControlPointSelection(ObjectId)");
116 }
117
118 void BSplineCurveSelectionPlugin::deleteSelectedControlPoints(int _objectId) {
119
120 BaseObjectData* object = 0;
121
122 if(!PluginFunctions::getObject(_objectId, object)) {
123 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
124 return;
125 }
126
127 BSplineCurve* curve = PluginFunctions::splineCurve(object);
128 if(curve) {
129
130 // I know this is a bit crappy, but control point indices
131 // change forcably after each delete operation so we have to
132 // start over each time...
133 bool breakWhile = false;
134 while(!breakWhile) {
135 bool oneFound = false;
136 unsigned int i = 0;
137 for(; i < curve->n_control_points(); ++i) {
138 if(curve->controlpoint_selected(i)) {
139 curve->delete_control_point(i);
140 oneFound = true;
141 break;
142 }
143 }
144
145 if((i >= curve->n_control_points()) && !oneFound) {
146 // We are through
147 breakWhile = true;
148 }
149 }
150 }
151
152 emit scriptInfo("deleteSelectedControlPoints(ObjectId)");
153 }
154
155 void BSplineCurveSelectionPlugin::selectControlPoints(int _objectId, const IdList& _ids, bool _deselect) {
156
157 if( _ids.empty()) return;
158
159 BaseObjectData* object = 0;
160
161 if(!PluginFunctions::getObject(_objectId, object)) {
162 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
163 return;
164 }
165
166 BSplineCurve* curve = PluginFunctions::splineCurve(object);
167 if(curve) {
168 for(IdList::const_iterator it = _ids.begin(); it != _ids.end(); ++it) {
169 if(*it < (int)curve->n_control_points()) {
170 if(_deselect) curve->deselect_controlpoint(*it);
171 else curve->select_controlpoint(*it);
172 }
173 }
174 }
175
176 QString selection = "selectControlPoints(ObjectId, [ " + QString::number(_ids[0]);
177
178 for (uint i = 1 ; i < _ids.size(); ++i) {
179 selection += ", " + QString::number(_ids[i]);
180 }
181
182 selection += " ])";
183
184
185 emit scriptInfo(selection);
186 }
187
188 IdList BSplineCurveSelectionPlugin::getControlPointSelection(int _objectId) {
189
190 BaseObjectData* object = 0;
191
192 IdList list;
193
194 if(!PluginFunctions::getObject(_objectId, object)) {
195 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
196 return list;
197 }
198
199 BSplineCurve* curve = PluginFunctions::splineCurve(object);
200 if(curve) {
201 for(unsigned int i = 0; i < curve->n_control_points(); ++i) {
202 if(curve->controlpoint_selected(i))
203 list.push_back((int)i);
204 }
205 }
206
207 return list;
208 }
209
210 //=====================================================================
211
212 void BSplineCurveSelectionPlugin::selectAllKnots(int _objectId) {
213
214 BaseObjectData* object = 0;
215
216 if(!PluginFunctions::getObject(_objectId, object)) {
217 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
218 return;
219 }
220
221 BSplineCurve* curve = PluginFunctions::splineCurve(object);
222 BSplineCurveObject* co = PluginFunctions::bsplineCurveObject(object);
223
224 if(curve) {
225 for(unsigned int i = 0; i < curve->n_knots(); ++i) {
226 curve->get_knotvector_ref()->select(i);
227 }
228 }
229
230 // Switch to control point selection mode
231 if(co)
232 co->splineCurveNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::KNOTVECTOR);
233
234 emit scriptInfo("selectAllKnots(ObjectId)");
235 }
236
237 void BSplineCurveSelectionPlugin::deselectAllKnots(int _objectId) {
238
239 BaseObjectData* object = 0;
240
241 if(!PluginFunctions::getObject(_objectId, object)) {
242 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
243 return;
244 }
245
246 BSplineCurve* curve = PluginFunctions::splineCurve(object);
247
248 if(curve) {
249 for(unsigned int i = 0; i < curve->n_knots(); ++i) {
250 curve->get_knotvector_ref()->deselect(i);
251 }
252 }
253
254 emit scriptInfo("deselectAllKnots(ObjectId)");
255 }
256
257 void BSplineCurveSelectionPlugin::invertKnotSelection(int _objectId) {
258
259 BaseObjectData* object = 0;
260
261 if(!PluginFunctions::getObject(_objectId, object)) {
262 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
263 return;
264 }
265
266 BSplineCurve* curve = PluginFunctions::splineCurve(object);
267 BSplineCurveObject* co = PluginFunctions::bsplineCurveObject(object);
268
269 if(curve) {
270 for(unsigned int i = 0; i < curve->n_knots(); ++i) {
271 if(curve->get_knotvector_ref()->selected(i))
272 curve->get_knotvector_ref()->deselect(i);
273 else
274 curve->get_knotvector_ref()->select(i);
275 }
276 }
277
278 // Switch to control point selection mode
279 if(co)
280 co->splineCurveNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::KNOTVECTOR);
281
282 emit scriptInfo("invertKnotSelection(ObjectId)");
283 }
284
285 void BSplineCurveSelectionPlugin::deleteSelectedKnots(int _objectId) {
286
287 BaseObjectData* object = 0;
288
289 if(!PluginFunctions::getObject(_objectId, object)) {
290 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
291 return;
292 }
293
294 BSplineCurve* curve = PluginFunctions::splineCurve(object);
295 if(curve) {
296
297 // I know this is a bit crappy, but knot indices
298 // change forcably after each delete operation so we have to
299 // start over each time...
300 bool breakWhile = false;
301 while(!breakWhile) {
302 bool oneFound = false;
303 unsigned int i = 0;
304 for(; i < curve->n_knots(); ++i) {
305 if(curve->get_knotvector_ref()->selected(i)) {
306 curve->get_knotvector_ref()->deleteKnot(i);
307 oneFound = true;
308 break;
309 }
310 }
311
312 if((i >= curve->n_knots()) && !oneFound) {
313 // We are through
314 breakWhile = true;
315 }
316 }
317 }
318
319 emit scriptInfo("deleteSelectedKnots(ObjectId)");
320 }
321
322 void BSplineCurveSelectionPlugin::selectKnots(int _objectId, const IdList& _ids, bool _deselect) {
323
324 if(_ids.empty()) return;
325
326 BaseObjectData* object = 0;
327
328 if(!PluginFunctions::getObject(_objectId, object)) {
329 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
330 return;
331 }
332
333 BSplineCurve* curve = PluginFunctions::splineCurve(object);
334 if(curve) {
335 for(IdList::const_iterator it = _ids.begin(); it != _ids.end(); ++it) {
336 if(*it < (int)curve->n_knots()) {
337 if(_deselect) curve->get_knotvector_ref()->deselect(*it);
338 else curve->get_knotvector_ref()->select(*it);
339 }
340 }
341 }
342
343 QString selection = "selectKnots(ObjectId, [ " + QString::number(_ids[0]);
344
345 for (uint i = 1 ; i < _ids.size(); ++i) {
346 selection += ", " + QString::number(_ids[i]);
347 }
348
349 selection += " ])";
350
351
352 emit scriptInfo(selection);
353 }
354
355 IdList BSplineCurveSelectionPlugin::getKnotSelection(int _objectId) {
356
357 BaseObjectData* object = 0;
358
359 IdList list;
360
361 if(!PluginFunctions::getObject(_objectId, object)) {
362 emit log(LOGERR, tr("Could not get object with id %1").arg(_objectId));
363 return list;
364 }
365
366 BSplineCurve* curve = PluginFunctions::splineCurve(object);
367 if(curve) {
368 for(unsigned int i = 0; i < curve->n_knots(); ++i) {
369 if(curve->get_knotvector_ref()->selected(i))
370 list.push_back((int)i);
371 }
372 }
373
374 return list;
375 }
BSplineCurveSelectionPlugin::deselectAllKnots
void deselectAllKnots(int _objectId)
Deselect all knots of a curve.
Definition: BSplineCurveSelectionFunctions.cc:237
BSplineCurveSelectionPlugin::deselectAllControlPoints
void deselectAllControlPoints(int _objectId)
Deselect all control points of a curve.
Definition: BSplineCurveSelectionFunctions.cc:71
ACG::BSplineCurveT::n_control_points
unsigned int n_control_points() const
Returns the number of control points.
Definition: BSplineCurveT.hh:164
BSplineCurveSelectionPlugin::deleteSelectedControlPoints
void deleteSelectedControlPoints(int _objectId)
Delete selected control points.
Definition: BSplineCurveSelectionFunctions.cc:118
BSplineCurveSelectionPlugin::deleteSelectedKnots
void deleteSelectedKnots(int _objectId)
Delete selected knots.
Definition: BSplineCurveSelectionFunctions.cc:285
BSplineCurveSelectionPlugin::getKnotSelection
IdList getKnotSelection(int _objectId)
Get current knot selection.
Definition: BSplineCurveSelectionFunctions.cc:355
BSplineCurveSelectionPlugin::invertControlPointSelection
void invertControlPointSelection(int _objectId)
Invert control point selection.
Definition: BSplineCurveSelectionFunctions.cc:90
PluginFunctions::bsplineCurveObject
BSplineCurveObject * bsplineCurveObject(BaseObjectData *_object)
Cast an BaseObject to a BSplineCurveObject if possible.
Definition: PluginFunctionsBSplineCurve.cc:100
ACG::SceneGraph::BSplineCurveNodeT
Definition: BSplineCurveNodeT.hh:88
LOGERR
Definition: LoggingInterface.hh:96
ACG::BSplineCurveT::get_knotvector_ref
Knotvector * get_knotvector_ref()
get a reference to the knotvector
Definition: BSplineCurveT.hh:106
ACG::BSplineCurveT::n_knots
unsigned int n_knots() const
Returns the number of knots.
Definition: BSplineCurveT.hh:167
BSplineCurveSelectionPlugin::selectControlPoints
void selectControlPoints(int _objectId, const IdList &_ids, bool _deselect=false)
Select specific control points of a curve.
Definition: BSplineCurveSelectionFunctions.cc:155
BSplineCurveSelectionPlugin::selectAllControlPoints
void selectAllControlPoints(int _objectId)
Select all control points of a curve.
Definition: BSplineCurveSelectionFunctions.cc:46
PluginFunctions::getObject
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
Definition: PluginFunctions.cc:294
BSplineCurveSelectionPlugin::invertKnotSelection
void invertKnotSelection(int _objectId)
Invert knot selection.
Definition: BSplineCurveSelectionFunctions.cc:257
ACG::BSplineCurveT
Definition: BSplineCurveT.hh:82
IdList
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:179
BSplineCurveObject::splineCurveNode
ACG::SceneGraph::BSplineCurveNodeT< BSplineCurve > * splineCurveNode()
Get the scenegraph Node.
Definition: BSplineCurveObject.cc:249
BSplineCurveSelectionPlugin::getControlPointSelection
IdList getControlPointSelection(int _objectId)
Get current control point selection.
Definition: BSplineCurveSelectionFunctions.cc:188
BSplineCurveSelectionPlugin::selectKnots
void selectKnots(int _objectId, const IdList &_ids, bool _deselect=false)
Select specific knots of a curve.
Definition: BSplineCurveSelectionFunctions.cc:322
BSplineCurveSelectionPlugin::selectAllKnots
void selectAllKnots(int _objectId)
Select all knots of a curve.
Definition: BSplineCurveSelectionFunctions.cc:212
PluginFunctions::splineCurve
BSplineCurve * splineCurve(BaseObjectData *_object)
Get a Bspline curve from an object.
Definition: PluginFunctionsBSplineCurve.cc:89
BSplineCurveObject
Definition: BSplineCurveObject.hh:78
BaseObjectData
Definition: BaseObjectData.hh:102
ACG::BSplineCurveT::delete_control_point
void delete_control_point(int _idx)
delete control point at given index
Definition: BSplineCurveT_impl.hh:205