Developer Documentation
ComponentsScripting.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 #include "ComponentsPlugin.hh"
43 
44 #include <MeshTools/MeshInfoT.hh>
45 
46 //------------------------------------------------------------------------------
47 
52 
53  emit setSlotDescription("splitComponents(int)","Split the mesh into connected components. The original mesh is deleted.",
54  QStringList("objectId"), QStringList("Id of an object"));
55 
56  emit setSlotDescription("biggestComponent(int)","Get the biggest component and delete the smaller ones.",
57  QStringList("objectId"),QStringList("Id of an object"));
58 
59 }
60 
61 //------------------------------------------------------------------------------
62 
64 
65  // get object
66  BaseObjectData *obj;
67  PluginFunctions::getObject(_objectId, obj);
68 
69  if (obj == 0){
70  emit log(LOGERR,"Unable to get object");
71  return IdList();
72  }
73 
74  QFileInfo fi(obj->name());
75 
76  if ( obj->dataType(DATA_TRIANGLE_MESH) ) {
77  TriMesh* mesh = PluginFunctions::triMesh(obj);
78 
79  if ( mesh == 0 ) {
80  emit log(LOGERR,"Unable to get mesh");
81  return IdList();
82  }
83 
84  int components = MeshInfo::componentCount( mesh );
85 
86  //create empty meshes
87  IdList newIDs;
88 
89  newIDs.push_back( _objectId );
90 
91  if (components == 1){
92  return newIDs;
93  }
94 
95  QString currentName = obj->name();
96  QString extension = currentName.section('.', -1);
97  currentName = currentName.section('.',0,-2);
98 
99  // Update name of original object
100  obj->setName(currentName+"_component_"+QString::number(0)+"."+extension);
101 
102 
103  for(int i=0; i < components-1; i++){
104 
105  // Copy original Object
106  int id;
107  emit copyObject(obj->id(), id);
108 
109  if (id == -1){
110  emit log(LOGERR, "Unable to generate a copy of object " + QString::number(obj->id()) );
111  return IdList();
112  }
113 
114  // Get the target Object which will contain one component and is the copy of the original
115  BaseObjectData *curObj;
116  PluginFunctions::getObject(id, curObj);
117  curObj->setName(currentName+"_component_"+QString::number(i+1)+"."+extension);
118 
119  TriMesh* curMesh = PluginFunctions::triMesh(curObj);
120 
121  if ( curMesh == 0 ) {
122  emit log(LOGERR,"Unable to get mesh");
123  return IdList();
124  }
125 
126  // Takes one component out of mesh, deletes it from mesh and adds it to curMesh
127  splitComponent( mesh, curMesh);
128 
129  newIDs.push_back(id);
130  }
131 
132  emit updatedObject(_objectId,UPDATE_ALL);
133 
134  for (uint i=0; i < newIDs.size(); i++)
135  emit updatedObject(newIDs[i],UPDATE_ALL);
136 
137  emit updateView();
138  return newIDs;
139 
140  } else if( obj->dataType(DATA_POLY_MESH) ) {
141  PolyMesh* mesh = PluginFunctions::polyMesh(obj);
142 
143  if ( mesh == 0 ) {
144  emit log(LOGERR,"Unable to get mesh");
145  return IdList();
146  }
147 
148  int components = MeshInfo::componentCount( mesh );
149 
150  //create empty meshes
151  std::vector< int > newIDs;
152 
153  if (components == 1){
154  newIDs.push_back( _objectId );
155  return newIDs;
156  }
157 
158  QString currentName = obj->name();
159  QString extension = currentName.section('.', -1);
160  currentName = currentName.section('.',0,-2);
161 
162  // Update name of original object
163  obj->setName(currentName+"_component_"+QString::number(0)+"."+extension);
164 
165  for(int i=0; i < components-1; i++){
166 
167  // Copy original Object
168  int id;
169  emit copyObject(obj->id(), id);
170 
171  if (id == -1){
172  emit log(LOGERR, "Unable to generate a copy of object " + QString::number(obj->id()) );
173  return IdList();
174  }
175 
176  // Get the target Object which will contain one component and is the copy of the original
177  BaseObjectData *curObj;
178  PluginFunctions::getObject(id, curObj);
179  curObj->setName(currentName+"_component_"+QString::number(i+1)+"."+extension);
180 
181  PolyMesh* curMesh = PluginFunctions::polyMesh(curObj);
182 
183  if ( curMesh == 0 ) {
184  emit log(LOGERR,"Unable to get mesh");
185  return IdList();
186  }
187 
188  // Takes one component out of mesh, deletes it from mesh and adds it to curMesh
189  splitComponent( mesh, curMesh);
190 
191  newIDs.push_back(id);
192  }
193 
194  emit updatedObject(_objectId,UPDATE_ALL);
195 
196  for (uint i=0; i < newIDs.size(); i++)
197  emit updatedObject(newIDs[i],UPDATE_ALL);
198 
199  emit updateView();
200 
201  return newIDs;
202 
203  }else {
204  emit log(LOGERR,"Splitting into components currently only supported for meshes");
205  return IdList();
206  }
207 }
208 
210 {
211 
212  BaseObjectData *obj;
213  PluginFunctions::getObject(_objId, obj);
214 
215  if (obj == 0){
216  emit log(LOGERR,"Unable to get object");
217  return;
218  }
219  if ( obj->dataType(DATA_TRIANGLE_MESH) )
220  {
223  }
224  else if ( obj->dataType(DATA_POLY_MESH) )
225  {
228  }
229 }
void splitComponent(MeshT *_mesh, MeshT *_copy)
Split mesh into components.
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
void splitComponents(QMouseEvent *_event)
Split Components of picked object.
#define DATA_POLY_MESH
Definition: PolyMesh.hh:59
int id() const
Definition: BaseObject.cc:190
void biggestComponent(QMouseEvent *_event)
Split into Components Button was hit.
void selectBiggestComponent(MeshT *_mesh)
Select the biggest component of the mesh.
bool dataType(DataType _type) const
Definition: BaseObject.cc:221
void setDescriptions()
set scripting slot descriptions
void deleteUnselectedFaces(MeshT *_mesh)
Deletes all faces of a mesh that are not selected.
TriMesh * triMesh(BaseObjectData *_object)
Get a triangle mesh from an object.
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:179
PolyMesh * polyMesh(BaseObjectData *_object)
Get a poly mesh from an object.
#define DATA_TRIANGLE_MESH
Definition: TriangleMesh.hh:60