Developer Documentation
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 #include "MeshRepairPlugin.hh"
47 
48 //-----------------------------------------------------------------------------
49 
50 
51 void MeshRepairPlugin::detectFlatValence3Vertices(int _objectId, double _angle) {
52 
53  unsigned int count(0);
54 
55  // get the target mesh
56  TriMesh* mesh = 0;
57  PluginFunctions::getMesh(_objectId,mesh);
58 
59  if ( mesh ) {
60 
61  // Clear current triangle selection
62  MeshSelection::clearVertexSelection(mesh);
63 
64  TriMesh::VertexIter v_it, v_end(mesh->vertices_end());
65  TriMesh::VVIter vv_it;
66  TriMesh::VFIter vf_it;
67  TriMesh::FaceHandle fh;
68  std::vector<TriMesh::VertexHandle> vh(3);
69  TriMesh::Scalar cosangle(cos(_angle/180.0*M_PI));
70 
71  for (v_it=mesh->vertices_begin(); v_it!=v_end; ++v_it)
72  {
73  if (!mesh->status(*v_it).deleted() && !mesh->is_boundary(*v_it) && mesh->valence(*v_it) == 3)
74  {
75  vf_it = mesh->vf_iter(*v_it);
76  const TriMesh::Normal& n0 = mesh->normal(*vf_it);
77  const TriMesh::Normal& n1 = mesh->normal(*(++vf_it));
78  const TriMesh::Normal& n2 = mesh->normal(*(++vf_it));
79 
80  if ( (n0|n1) > cosangle &&
81  (n0|n2) > cosangle &&
82  (n1|n2) > cosangle )
83  {
84 
85  mesh->status(*v_it).set_selected(true);
86  ++count;
87  }
88  }
89  }
90  }
91  else {
92  emit log(LOGERR, "Cannot detect flat triangles on non-trimesh " + QString::number(_objectId) + ".");
93  }
94 
95  if (count > 0) {
96  emit updatedObject(_objectId, UPDATE_SELECTION);
97  emit createBackup(_objectId, "Select vertices", UPDATE_SELECTION);
98  }
99 
100  emit log (LOGINFO,"Selected " + QString::number(count) + " vertices on object " + QString::number(_objectId) + " with face angle difference smaller than " + QString::number(_angle) + ".");
101  emit scriptInfo( "detectFlatValence3Vertices(" + QString::number(_objectId) + ", " + QString::number(_angle) + ")" );
102 
103 }
104 
105 //-----------------------------------------------------------------------------
106 
108 
109  unsigned int count = 0;
110 
111  // get the target mesh
112  TriMesh* mesh = 0;
113  PluginFunctions::getMesh(_objectId, mesh);
114 
115  if (mesh) {
116 
117  TriMesh::VertexIter v_it, v_end(mesh->vertices_end());
118  TriMesh::VVIter vv_it;
119  TriMesh::VFIter vf_it;
120  int i;
121  std::vector<TriMesh::VertexHandle> vh(3);
122 
123  for (v_it = mesh->vertices_begin(); v_it != v_end; ++v_it) {
124  vf_it = mesh->vf_iter(*v_it);
125  if ((mesh->status(*v_it).selected()) && !mesh->status(*v_it).feature() && mesh->valence(*v_it) == 3) {
126  for (i = 0, vv_it = mesh->vv_iter(*v_it); vv_it.is_valid(); ++vv_it, ++i)
127  vh[2 - i] = *vv_it;
128 
129  mesh->delete_vertex(*v_it, false);
130  mesh->add_face(vh);
131 
132  ++count;
133  }
134  }
135  if (count > 0)
136  mesh->garbage_collection();
137  }
138 
139  if (count > 0) {
140  emit updatedObject(_objectId, UPDATE_ALL);
141  emit createBackup(_objectId, "Delete/merge selected vertices", UPDATE_ALL);
142  }
143  emit log("Deleted " + QString::number(count) + " vertices on object " + QString::number(_objectId) + ".");
144 }
145 
146 //-----------------------------------------------------------------------------
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
const UpdateType UPDATE_SELECTION(UpdateTypeSet(16))
Selection updated.
Functions for selection on a mesh.
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:110
bool getMesh(int _identifier, PolyMesh *&_mesh)
Get the Poly Mesh which has the given identifier.
void detectFlatValence3Vertices(int _objectId, double _angle)
Detect valence 3 vertices with faces that lie in the plane of their adjacent triangles.
void removeSelectedVal3Vertices(int _objectId)
Remove all selected valence 3 vertices.