Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
VertexFunctions.cc
1 
2 /*===========================================================================*\
3  * *
4  * OpenFlipper *
5  * Copyright (c) 2001-2015, RWTH-Aachen University *
6  * Department of Computer Graphics and Multimedia *
7  * All rights reserved. *
8  * www.openflipper.org *
9  * *
10  *---------------------------------------------------------------------------*
11  * This file is part of OpenFlipper. *
12  *---------------------------------------------------------------------------*
13  * *
14  * Redistribution and use in source and binary forms, with or without *
15  * modification, are permitted provided that the following conditions *
16  * are met: *
17  * *
18  * 1. Redistributions of source code must retain the above copyright notice, *
19  * this list of conditions and the following disclaimer. *
20  * *
21  * 2. Redistributions in binary form must reproduce the above copyright *
22  * notice, this list of conditions and the following disclaimer in the *
23  * documentation and/or other materials provided with the distribution. *
24  * *
25  * 3. Neither the name of the copyright holder nor the names of its *
26  * contributors may be used to endorse or promote products derived from *
27  * this software without specific prior written permission. *
28  * *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
32  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
33  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
34  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
35  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
36  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
37  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
38  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
39  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
40  * *
41 \*===========================================================================*/
42 
43 /*===========================================================================*\
44  * *
45  * $Revision$ *
46  * $LastChangedBy$ *
47  * $Date$ *
48  * *
49 \*===========================================================================*/
50 
51 
52 
53 
54 #include "MeshRepairPlugin.hh"
56 
57 //-----------------------------------------------------------------------------
58 
59 
60 void MeshRepairPlugin::detectFlatValence3Vertices(int _objectId, double _angle) {
61 
62  unsigned int count(0);
63 
64  // get the target mesh
65  TriMesh* mesh = 0;
66  PluginFunctions::getMesh(_objectId,mesh);
67 
68  if ( mesh ) {
69 
70  // Clear current triangle selection
71  MeshSelection::clearVertexSelection(mesh);
72 
73  TriMesh::VertexIter v_it, v_end(mesh->vertices_end());
74  TriMesh::VVIter vv_it;
75  TriMesh::VFIter vf_it;
76  TriMesh::FaceHandle fh;
77  std::vector<TriMesh::VertexHandle> vh(3);
78  TriMesh::Scalar cosangle(cos(_angle/180.0*M_PI));
79 
80  for (v_it=mesh->vertices_begin(); v_it!=v_end; ++v_it)
81  {
82  if (!mesh->status(*v_it).deleted() && !mesh->is_boundary(*v_it) && mesh->valence(*v_it) == 3)
83  {
84  vf_it = mesh->vf_iter(*v_it);
85  const TriMesh::Normal& n0 = mesh->normal(*vf_it);
86  const TriMesh::Normal& n1 = mesh->normal(*(++vf_it));
87  const TriMesh::Normal& n2 = mesh->normal(*(++vf_it));
88 
89  if ( (n0|n1) > cosangle &&
90  (n0|n2) > cosangle &&
91  (n1|n2) > cosangle )
92  {
93 
94  mesh->status(*v_it).set_selected(true);
95  ++count;
96  }
97  }
98  }
99  }
100  else {
101  emit log(LOGERR, "Cannot detect flat triangles on non-trimesh " + QString::number(_objectId) + ".");
102  }
103 
104  if (count > 0) {
105  emit updatedObject(_objectId, UPDATE_SELECTION);
106  emit createBackup(_objectId, "Select vertices", UPDATE_SELECTION);
107  }
108 
109  emit log (LOGINFO,"Selected " + QString::number(count) + " vertices on object " + QString::number(_objectId) + " with face angle difference smaller than " + QString::number(_angle) + ".");
110  emit scriptInfo( "detectFlatValence3Vertices(" + QString::number(_objectId) + ", " + QString::number(_angle) + ")" );
111 
112 }
113 
114 //-----------------------------------------------------------------------------
115 
117 
118  unsigned int count = 0;
119 
120  // get the target mesh
121  TriMesh* mesh = 0;
122  PluginFunctions::getMesh(_objectId, mesh);
123 
124  if (mesh) {
125 
126  TriMesh::VertexIter v_it, v_end(mesh->vertices_end());
127  TriMesh::VVIter vv_it;
128  TriMesh::VFIter vf_it;
129  int i;
130  std::vector<TriMesh::VertexHandle> vh(3);
131 
132  for (v_it = mesh->vertices_begin(); v_it != v_end; ++v_it) {
133  vf_it = mesh->vf_iter(*v_it);
134  if ((mesh->status(*v_it).selected()) && !mesh->status(*v_it).feature() && mesh->valence(*v_it) == 3) {
135  for (i = 0, vv_it = mesh->vv_iter(*v_it); vv_it.is_valid(); ++vv_it, ++i)
136  vh[2 - i] = *vv_it;
137 
138  mesh->delete_vertex(*v_it, false);
139  mesh->add_face(vh);
140 
141  ++count;
142  }
143  }
144  if (count > 0)
145  mesh->garbage_collection();
146  }
147 
148  if (count > 0) {
149  emit updatedObject(_objectId, UPDATE_ALL);
150  emit createBackup(_objectId, "Delete/merge selected vertices", UPDATE_ALL);
151  }
152  emit log("Deleted " + QString::number(count) + " vertices on object " + QString::number(_objectId) + ".");
153 }
154 
155 //-----------------------------------------------------------------------------
void removeSelectedVal3Vertices(int _objectId)
Remove all selected valence 3 vertices.
bool getMesh(int _identifier, PolyMesh *&_mesh)
Get the Poly Mesh which has the given identifier.
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
void detectFlatValence3Vertices(int _objectId, double _angle)
Detect valence 3 vertices with faces that lie in the plane of their adjacent triangles.
const UpdateType UPDATE_SELECTION(UpdateTypeSet(1)<< 4)
Selection updated.
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:87
Functions for selection on a mesh.