Developer Documentation
VolumeMeshSelectionPluginT.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: 21016 $ *
45 * $LastChangedBy: schultz $ *
46 * $Date: 2015-07-16 16:48:42 +0200 (Do, 16 Jul 2015) $ *
47 * *
48 \*===========================================================================*/
49 
50 #include "VolumeMeshSelectionPlugin.hh"
51 #include <stack>
52 #include <vector>
53 #include <cmath>
54 
55 //***********************************************************************************
56 
66 template<class MeshT>
67 bool VolumeMeshSelectionPlugin::volumeSelection(MeshT* _mesh, ACG::GLState& _state, QRegion *_region,
68  PrimitiveType _primitiveType, bool _deselection) {
69  using namespace OpenVolumeMesh;
70 
71  ACG::Vec3d proj;
72  bool rv = false;
73 
74  //reset tagged status
75  StatusAttrib status(*_mesh);
76  VertexIter v_it(_mesh->vertices_begin()), v_end(_mesh->vertices_end());
77  for (; v_it != v_end; ++v_it)
78  status[*v_it].set_tagged(false);
79 
80  //tag all vertices that are projected into region
81  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it) {
82 
83  proj = _state.project(_mesh->vertex(*v_it));
84 
85  if (_region->contains(QPoint((int)proj[0], _state.context_height()- (int)proj[1]))) {
86 
87  status[*v_it].set_tagged(true);
88  if (_primitiveType & vertexType_) {
89  rv = true;
90  status[*v_it].set_selected(!_deselection);
91  }
92  }
93  }
94 
95  if (_primitiveType & edgeType_) {
96  EdgeIter e_it(_mesh->edges_begin()), e_end(_mesh->edges_end());
97 
98  for (; e_it != e_end; ++e_it)
99  {
100  VertexHandle v1 = _mesh->halfedge(_mesh->halfedge_handle(*e_it, 0)).to_vertex();
101  VertexHandle v2 = _mesh->halfedge(_mesh->halfedge_handle(*e_it, 1)).to_vertex();
102 
103  // select only edges whose both vertices are within the selection region
104  if (status[v1].tagged() && status[v2].tagged()) {
105  rv = true;
106  status[*e_it].set_selected(!_deselection);
107  }
108  }
109  }
110 
111  if (_primitiveType & faceType_) {
112  FaceIter f_it(_mesh->faces_begin()), f_end(_mesh->faces_end());
113 
114  for (; f_it != f_end; ++f_it) {
115  const std::vector<HalfEdgeHandle>& halfedges = _mesh->face(*f_it).halfedges();
116  bool all_fv_tagged = true;
117 
118  for (std::size_t i = 0; i < halfedges.size(); ++i) {
119  VertexHandle v = _mesh->halfedge(halfedges[i]).to_vertex();
120  all_fv_tagged &= status[v].tagged();
121  }
122 
123  // select only faces that are completely within the selection region
124  if (all_fv_tagged) {
125  rv = true;
126  status[*f_it].set_selected(!_deselection);
127  }
128  }
129  }
130 
131  if (_primitiveType & cellType_) {
132  CellIter c_it(_mesh->cells_begin()), c_end(_mesh->cells_end());
133 
134  for (; c_it != c_end; ++c_it) {
135  bool all_cv_tagged = true;
136  for (CellVertexIter cv_it(_mesh->cv_iter(*c_it)); cv_it.valid(); ++cv_it) {
137  all_cv_tagged &= status[*cv_it].tagged();
138  }
139 
140  if (all_cv_tagged) {
141  rv = true;
142  status[*c_it].set_selected(!_deselection);
143  }
144  }
145  }
146 
147  return rv;
148 }
149 
150 //***********************************************************************************
151 
160 template<class MeshT>
161 void VolumeMeshSelectionPlugin::floodFillSelection(MeshT* _mesh, uint _fh, double _maxAngle,
162  PrimitiveType _primitiveTypes, bool _deselection)
163 {
164  using namespace OpenVolumeMesh;
165 
166  FaceHandle hitFace(_fh);
167 
168  if (!_mesh->is_boundary(hitFace))
169  return;
170 
171  HalfFaceHandle hitHalfFace = _mesh->halfface_handle(hitFace, 0);
172 
173  if (!_mesh->is_boundary(hitHalfFace))
174  hitHalfFace = _mesh->halfface_handle(hitFace, 1);
175 
176  StatusAttrib status(*_mesh);
177 
178  // reset tagged status
179  HalfFaceIter hf_it(_mesh->halffaces_begin()), hf_end(_mesh->halffaces_end());
180  for (; hf_it != hf_end; ++hf_it)
181  {
182  status[*hf_it].set_tagged(false);
183  }
184 
185  status[hitHalfFace].set_tagged(true);
186 
187  std::stack<HalfFaceHandle> hf_handles;
188  hf_handles.push(hitHalfFace);
189 
190  NormalAttrib<MeshT> normals(*_mesh);
191  typename MeshT::PointT n1 = normals[hitHalfFace];
192  double maxAngle = _maxAngle / 180.0 * M_PI;
193 
194  // tag all half-faces whose normal does not deviate too much from the
195  // initial face
196  while (!hf_handles.empty())
197  {
198  HalfFaceHandle hf = hf_handles.top();
199  hf_handles.pop();
200 
201  for (BoundaryHalfFaceHalfFaceIter bhfhf_it(hf, _mesh) ; bhfhf_it.valid() ; ++bhfhf_it)
202  {
203  if (status[*bhfhf_it].tagged())
204  continue;
205 
206  typename MeshT::PointT n2 = normals[*bhfhf_it];
207 
208  double angle = std::acos(n1 | n2);
209 
210  if (angle <= maxAngle) {
211  status[*bhfhf_it].set_tagged(true);
212  hf_handles.push(*bhfhf_it);
213  }
214  }
215  }
216 
217  // now select all tagged primitives
218  for (hf_it = _mesh->halffaces_begin(); hf_it != hf_end; ++hf_it)
219  {
220  if (status[*hf_it].tagged())
221  {
222  FaceHandle fh = _mesh->face_handle(*hf_it);
223 
224  if (_primitiveTypes & vertexType_)
225  for (HalfFaceVertexIter hfv_it(*hf_it, _mesh); hfv_it.valid(); ++hfv_it)
226  status[*hfv_it].set_selected(!_deselection);
227 
228  if (_primitiveTypes & edgeType_)
229  {
230  std::vector<HalfEdgeHandle> const& halfedges = _mesh->face(fh).halfedges();
231 
232  for (std::size_t i = 0; i < halfedges.size(); ++i)
233  status[_mesh->edge_handle(halfedges[i])].set_selected(!_deselection);
234  }
235 
236  if (_primitiveTypes & faceType_)
237  status[fh].set_selected(!_deselection);
238  }
239  }
240 }
241 
PrimitiveType faceType_
Handle to selection environment.
int context_height() const
get gl context height
Definition: GLState.hh:833
void floodFillSelection(MeshT *_mesh, uint _fh, double _maxAngle, PrimitiveType _primitiveTypes, bool _deselection)
Select all entities that are connected (and do not exceed the maximum dihedral angle) ...
Vec3d project(const Vec3d &_point) const
project point in world coordinates to window coordinates
Definition: GLState.cc:638
PrimitiveType vertexType_
Primitive type handles:
bool volumeSelection(MeshT *_mesh, ACG::GLState &_state, QRegion *_region, PrimitiveType _primitiveTypes, bool _deselection)
Surface volume selection tool.
PrimitiveType edgeType_
Handle to selection environment.
PrimitiveType cellType_
Handle to selection environment.