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