Developer Documentation
HalfedgeSelection.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  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 #include "MeshObjectSelectionPlugin.hh"
51 
53 
54 //=========================================================
55 //==== Halfedge selections
56 //=========================================================
57 
58 void MeshObjectSelectionPlugin::selectHalfedges(int objectId , IdList _halfedgeList) {
59 
60  if(_halfedgeList.empty() ) return;
61 
62  BaseObjectData* object = 0;
63  if (! PluginFunctions::getObject(objectId,object)) {
64  emit log(LOGERR,tr("selectHalfedges: unable to get object"));
65  return;
66  }
67 
68  if (object->dataType() == DATA_TRIANGLE_MESH)
69  MeshSelection::selectHalfedges(PluginFunctions::triMesh(object), _halfedgeList);
70  else if (object->dataType() == DATA_POLY_MESH)
71  MeshSelection::selectHalfedges(PluginFunctions::polyMesh(object), _halfedgeList);
72  else {
73  emit log(LOGERR,tr("selectHalfedges: Unsupported object Type"));
74  return;
75  }
76 
77  QString selection = "selectHalfedges(ObjectId(" + QString::number(objectId) + ") , [ " + QString::number(_halfedgeList[0]);
78 
79  for (uint i = 1 ; i < _halfedgeList.size(); ++i) {
80  selection += " , " + QString::number(_halfedgeList[i]);
81  }
82 
83  selection += " ])";
84 
85  emit updatedObject(object->id(), UPDATE_SELECTION_EDGES);
86  emit scriptInfo(selection);
87 }
88 
89 //=========================================================
90 
91 void MeshObjectSelectionPlugin::unselectHalfedges(int objectId , IdList _halfedgeList) {
92 
93  if(_halfedgeList.empty() ) return;
94 
95  BaseObjectData* object = 0;
96  if (! PluginFunctions::getObject(objectId,object)) {
97  emit log(LOGERR,tr("unselectHalfedges: unable to get object"));
98  return;
99  }
100 
101  if (object->dataType() == DATA_TRIANGLE_MESH)
102  MeshSelection::unselectHalfedges(PluginFunctions::triMesh(object), _halfedgeList);
103  else if (object->dataType() == DATA_POLY_MESH)
104  MeshSelection::unselectHalfedges(PluginFunctions::polyMesh(object), _halfedgeList);
105  else {
106  emit log(LOGERR,tr("unselectHalfedges: Unsupported object Type"));
107  return;
108  }
109 
110  QString selection = "unselectVertices(ObjectId(" + QString::number(objectId) + ") , [ " + QString::number(_halfedgeList[0]);
111 
112  for (uint i = 1 ; i < _halfedgeList.size(); ++i) {
113  selection += " , " + QString::number(_halfedgeList[i]);
114  }
115 
116  selection += " ])";
117 
118  emit updatedObject(object->id(), UPDATE_SELECTION_EDGES);
119  emit scriptInfo(selection);
120 }
121 
122 //=========================================================
123 
125 
126  BaseObjectData* object;
127  if (! PluginFunctions::getObject(objectId,object)) {
128  emit log(LOGERR,tr("selectAllVertices: unable to get object"));
129  return;
130  }
131 
132  if (object->dataType() == DATA_TRIANGLE_MESH)
133  MeshSelection::selectAllHalfedges(PluginFunctions::triMesh(object));
134  else if (object->dataType() == DATA_POLY_MESH)
135  MeshSelection::selectAllHalfedges(PluginFunctions::polyMesh(object));
136  else {
137  emit log(LOGERR,tr("selectAllHalfedges: Unsupported object Type"));
138  return;
139  }
140 
141  emit updatedObject(object->id(), UPDATE_SELECTION_EDGES);
142  emit scriptInfo("selectAllHalfedges(ObjectId(" + QString::number(objectId) + "))");
143 }
144 
145 //=========================================================
146 
148 
149  BaseObjectData* object;
150  if (! PluginFunctions::getObject(objectId,object)) {
151  emit log(LOGERR,tr("clearHalfedgeSelection: unable to get object"));
152  return;
153  }
154 
155  if (object->dataType() == DATA_TRIANGLE_MESH)
156  MeshSelection::clearHalfedgeSelection(PluginFunctions::triMesh(object));
157  else if (object->dataType() == DATA_POLY_MESH)
158  MeshSelection::clearHalfedgeSelection(PluginFunctions::polyMesh(object));
159  else {
160  emit log(LOGERR,tr("clearHalfedgeSelection: Unsupported object Type"));
161  return;
162  }
163 
164  emit updatedObject(object->id(), UPDATE_SELECTION_EDGES);
165  emit scriptInfo("clearHalfedgeSelection(ObjectId(" + QString::number(objectId) + "))");
166 }
167 
168 //=========================================================
169 
171 
172  BaseObjectData* object;
173  if (! PluginFunctions::getObject(objectId,object)) {
174  emit log(LOGERR,tr("invertHalfedgeSelection: unable to get object"));
175  return;
176  }
177 
178  if (object->dataType() == DATA_TRIANGLE_MESH)
179  MeshSelection::invertHalfedgeSelection(PluginFunctions::triMesh(object));
180  else if (object->dataType() == DATA_POLY_MESH)
181  MeshSelection::invertHalfedgeSelection(PluginFunctions::polyMesh(object));
182  else {
183  emit log(LOGERR,tr("invertHalfedgeSelection: Unsupported object Type"));
184  return;
185  }
186 
187  emit updatedObject(object->id(), UPDATE_SELECTION_EDGES);
188  emit scriptInfo("invertHalfedgeSelection(ObjectId(" + QString::number(objectId) + "))");
189 }
190 
191 //=========================================================
192 
194 
195  BaseObjectData* object;
196  if (! PluginFunctions::getObject(objectId,object)) {
197  emit log(LOGERR,tr("selectBoundaryHalfedges: unable to get object"));
198  return;
199  }
200 
201  if (object->dataType() == DATA_TRIANGLE_MESH)
202  MeshSelection::selectBoundaryHalfedges(PluginFunctions::triMesh(object));
203  else if (object->dataType() == DATA_POLY_MESH)
204  MeshSelection::selectBoundaryHalfedges(PluginFunctions::polyMesh(object));
205  else {
206  emit log(LOGERR,tr("selectBoundaryHalfedges: Unsupported object Type"));
207  return;
208  }
209 
210  emit updatedObject(object->id(), UPDATE_SELECTION_EDGES);
211  emit scriptInfo("selectBoundaryHalfedges(ObjectId(" + QString::number(objectId) + "))");
212 }
213 
214 //=========================================================
215 
217 
218  BaseObjectData* object;
219  if (! PluginFunctions::getObject(objectId,object)) {
220  emit log(LOGERR,tr("getHalfedgeSelection: unable to get object"));
221  return IdList(0);
222  }
223 
224  emit scriptInfo("getHalfedgeSelection(ObjectId(" + QString::number(objectId) + "))");
225 
226  if (object->dataType() == DATA_TRIANGLE_MESH)
227  return MeshSelection::getHalfedgeSelection(PluginFunctions::triMesh(object));
228  else if (object->dataType() == DATA_POLY_MESH)
229  return MeshSelection::getHalfedgeSelection(PluginFunctions::polyMesh(object));
230  else {
231  emit log(LOGERR,tr("getHalfedgeSelection: Unsupported object Type"));
232  return IdList(0);
233  }
234 
235  return IdList(0);
236 
237 }
238 
239 //=========================================================
240 
242 
243  IdList vertex_pairs;
244 
245  BaseObjectData* object = 0;
246  if ( !PluginFunctions::getObject(_id,object) ) {
247  emit log(LOGERR,tr("Cannot find object for id ") + QString::number(_id));
248  return IdList(0);
249  }
250 
251  if(object->dataType() == DATA_TRIANGLE_MESH) {
252 
253  TriMeshObject* obj = 0;
254  if(!PluginFunctions::getObject(_id, obj)) {
255  emit log(LOGERR, "Could not get mesh object!");
256  return IdList(0);
257  }
258 
259  TriMesh* mesh = obj->mesh();
260 
261  for(IdList::const_iterator it = _halfedges.begin(); it != _halfedges.end(); ++it) {
262  vertex_pairs.push_back(mesh->from_vertex_handle(TriMesh::HalfedgeHandle(*it)).idx());
263  vertex_pairs.push_back(mesh->to_vertex_handle(TriMesh::HalfedgeHandle(*it)).idx());
264  }
265 
266  } else if(object->dataType() == DATA_POLY_MESH) {
267 
268  PolyMeshObject* obj = 0;
269  if(!PluginFunctions::getObject(_id, obj)) {
270  emit log(LOGERR, "Could not get mesh object!");
271  return IdList(0);
272  }
273 
274  PolyMesh* mesh = obj->mesh();
275 
276  for(IdList::const_iterator it = _halfedges.begin(); it != _halfedges.end(); ++it) {
277  vertex_pairs.push_back(mesh->from_vertex_handle(PolyMesh::HalfedgeHandle(*it)).idx());
278  vertex_pairs.push_back(mesh->to_vertex_handle(PolyMesh::HalfedgeHandle(*it)).idx());
279  }
280  }
281 
282  return vertex_pairs;
283 }
284 
285 //=========================================================
286 
288 
289  if(_vertices.size() % 2 != 0) {
290  emit log(LOGERR, "Number of vertices is not even!");
291  return IdList(0);
292  }
293 
294  IdList halfedges;
295 
296  BaseObjectData* object = 0;
297  if ( !PluginFunctions::getObject(_id,object) ) {
298  emit log(LOGERR,tr("Cannot find object for id ") + QString::number(_id));
299  return IdList(0);
300  }
301 
302  if(object->dataType() == DATA_TRIANGLE_MESH) {
303 
304  TriMeshObject* obj = 0;
305  if(!PluginFunctions::getObject(_id, obj)) {
306  emit log(LOGERR, "Could not get mesh object!");
307  return IdList(0);
308  }
309 
310  TriMesh* mesh = obj->mesh();
311 
312  for(IdList::const_iterator it = _vertices.begin(); it != _vertices.end(); it+=2) {
314  if(!vh.is_valid()) continue;
315  for(TriMesh::VertexOHalfedgeIter voh_it = mesh->voh_iter(vh);
316  voh_it.is_valid(); ++voh_it) {
317  if(mesh->to_vertex_handle(*voh_it).idx() == *(it+1)) {
318  halfedges.push_back(voh_it->idx());
319  continue;
320  }
321  }
322  }
323 
324  } else if(object->dataType() == DATA_POLY_MESH) {
325 
326  PolyMeshObject* obj = 0;
327  if(!PluginFunctions::getObject(_id, obj)) {
328  emit log(LOGERR, "Could not get mesh object!");
329  return IdList(0);
330  }
331 
332  PolyMesh* mesh = obj->mesh();
333 
334  for(IdList::const_iterator it = _vertices.begin(); it != _vertices.end(); it+=2) {
336  if(!vh.is_valid()) continue;
337  for(PolyMesh::VertexOHalfedgeIter voh_it = mesh->voh_iter(vh);
338  voh_it.is_valid(); ++voh_it) {
339  if(mesh->to_vertex_handle(*voh_it).idx() == *(it+1)) {
340  halfedges.push_back(voh_it->idx());
341  continue;
342  }
343  }
344  }
345  }
346 
347  return halfedges;
348 }
349 
350 //=========================================================
351 
353 void MeshObjectSelectionPlugin::colorizeHalfedgeSelection(int objectId, int r, int g, int b, int a) {
354 
355  BaseObjectData* object;
356  if (! PluginFunctions::getObject(objectId,object)) {
357  emit log(LOGERR,"colorizeHalfedgeSelection: unable to get object");
358  return;
359  }
360 
361  if (object->dataType() == DATA_TRIANGLE_MESH) {
363  } else if (object->dataType() == DATA_POLY_MESH) {
365  } else {
366  emit log(LOGERR,"colorizeHalfedgeSelection: Unsupported object Type");
367  return;
368  }
369 
370  emit scriptInfo("colorizeHalfedgeSelection(ObjectId(" + QString::number(objectId) + "), "
371  + QString::number(r) + ", " + QString::number(g) + ", " + QString::number(b) + ")");
372 
373  emit updatedObject(object->id(), UPDATE_COLOR);
374 }
void colorizeHalfedgeSelection(int objectId, int r, int g, int b, int a)
Colorize the edge selection.
void selectAllHalfedges(int objectId)
Select all Halfedges.
#define DATA_POLY_MESH
Definition: PolyMesh.hh:59
Kernel::VertexOHalfedgeIter VertexOHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:163
void selectBoundaryHalfedges(int objectId)
Select boundary edges.
void invertHalfedgeSelection(int objectId)
Unselect all Halfedges.
void unselectHalfedges(int objectId, IdList _vertexList)
Unselect given Halfedges.
int id() const
Definition: BaseObject.cc:190
bool dataType(DataType _type) const
Definition: BaseObject.cc:221
MeshT * mesh()
return a pointer to the mesh
void colorizeSelection(MeshT *_mesh, PrimitiveType _primitiveTypes, int _red, int _green, int _blue, int _alpha)
Colorize the selection.
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
TriMesh * triMesh(BaseObjectData *_object)
Get a triangle mesh from an object.
void clearHalfedgeSelection(int objectId)
Invert the current edge selection.
IdList convertHalfedgesToVertexPairs(int _id, const IdList &_halfedges)
Convert halfedge ids to vertex pairs.
IdList getHalfedgeSelection(int objectId)
Return a list of all selected edges.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
Type for a MeshObject containing a triangle mesh.
Definition: TriangleMesh.hh:67
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:179
Functions for selection on a mesh.
IdList convertVertexPairsToHalfedges(int _id, const IdList &_vertices)
Inverse of function above.
void selectHalfedges(int objectId, IdList _vertexList)
Select given Halfedges.
PolyMesh * polyMesh(BaseObjectData *_object)
Get a poly mesh from an object.
#define DATA_TRIANGLE_MESH
Definition: TriangleMesh.hh:60
const UpdateType UPDATE_SELECTION_EDGES(UpdateTypeSet(1)<< 6)
Edge selection has changed.
const UpdateType UPDATE_COLOR(UpdateTypeSet(1)<< 10)
Colors have changed.
Type for a Meshobject containing a poly mesh.
Definition: PolyMesh.hh:65
SelectionInterface::PrimitiveType halfedgeType_
Handle to selection environment.