ug4
simplify_polychain.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG_simplify_polychain
34 #define __H__UG_simplify_polychain
35 
36 #include <algorithm>
37 #include <vector>
38 #include "lib_grid/lg_base.h"
42 
43 namespace ug{
44 
45 struct IndCmp{
46  IndCmp(const std::vector<number>& vals) : m_vals(vals) {}
47  bool operator() (size_t i1, size_t i2){
48  return m_vals[i1] < m_vals[i2];
49  }
50  const std::vector<number>& m_vals;
51 };
52 
53 template <class TEdgeIter, class TAAPos>
54 void SimplifyPolylines(Grid& grid, TEdgeIter edgesBegin, TEdgeIter edgesEnd,
55  number curvatureThreshold, TAAPos aaPos)
56 {
57  using namespace std;
58  typedef typename TAAPos::ValueType vector_t;
59 
60  grid.begin_marking();
61 
62  number curvatureThresholdRad = deg_to_rad(curvatureThreshold);
63 
64  vector<Vertex*> candidates;
65 
66  for(TEdgeIter eiter = edgesBegin; eiter != edgesEnd; ++eiter){
67  Edge* e = *eiter;
68  grid.mark(e);
69  for(int i = 0; i < 2; ++i){
70  if(!grid.is_marked(e->vertex(i))){
71  grid.mark(e->vertex(i));
72  candidates.push_back(e->vertex(i));
73  }
74  }
75  }
76 
77 // some predeclarations
78  vector<Vertex*> vrts;
79  vrts.reserve(candidates.size());
80  vector<number> curvatures;
81  curvatures.reserve(candidates.size());
82  vector<size_t> indices;
83  indices.reserve(candidates.size());
85  AssocElemIter<Vertex, Edge> assIter(cb);
86 
87 
88  while(!candidates.empty()){
89  // find the subset of candidates which actually could be removed and add them to
90  // the vrts array
91  vrts.clear();
92  curvatures.clear();
93  for(size_t icandidate = 0; icandidate < candidates.size(); ++icandidate){
94  Vertex* vrt = candidates[icandidate];
95  grid.unmark(vrt);
96  Vertex* cvrt[2];
97  int numConVrts = 0;
98  for(assIter.reinit(grid, vrt); assIter.valid(); ++assIter){
99  if(numConVrts < 2)
100  cvrt[numConVrts] = GetConnectedVertex(*assIter, vrt);
101  ++numConVrts;
102  }
103 
104  if(numConVrts == 2){
105  vector_t dir[2];
106  VecSubtract(dir[0], aaPos[vrt], aaPos[cvrt[0]]);
107  VecSubtract(dir[1], aaPos[cvrt[1]], aaPos[vrt]);
108  number angle = VecAngle(dir[0], dir[1]);
109  if(angle <= curvatureThresholdRad){
110  vrts.push_back(vrt);
111  curvatures.push_back(angle);
112  }
113  }
114  }
115 
116  // if we havn't found any vertices to remove, we're done.
117  if(vrts.empty())
118  break;
119 
120  // set up index-permutation array
121  indices.clear();
122  for(size_t i = 0; i < vrts.size(); ++i)
123  indices.push_back(i);
124 
125  // this class helps in creating a permutation of indices sorted by curvature
126  IndCmp cmp(curvatures);
127 
128  sort(indices.begin(), indices.end(), cmp);
129 
130  // perform the actual removal.
131  // At this point all vertices in vrts are unmarked.
132  // candidates are collected anew
133  candidates.clear();
134  for(size_t ivrt = 0; ivrt < vrts.size(); ++ivrt){
135  Vertex* vrt = vrts[indices[ivrt]];
136  // marked vertices are candidates for the next iteration
137  if(grid.is_marked(vrt))
138  continue;
139 
140  // the vertex has to be removed. Create a new edge between
141  // the two connected vertices and mark it. Unmark connected vertices.
142  Edge* cedge[2] = {NULL, NULL};
143  Vertex* cvrt[2] = {NULL, NULL};
144  for(assIter.reinit(grid, vrt); assIter.valid(); ++assIter){
145  if(!cvrt[0]){
146  cedge[0] = *assIter;
147  cvrt[0] = GetConnectedVertex(*assIter, vrt);
148  }
149  else{
150  cedge[1] = *assIter;
151  cvrt[1] = GetConnectedVertex(*assIter, vrt);
152  }
153  }
154 
155  if(!cvrt[1]){
156  UG_LOG("ALGORITHM ERROR in SimplifyPolylines: two connected vertices "
157  "should always be found for a marked candidate... "
158  "ignoring vertex.\n");
159  continue;
160  }
161  if(grid.get_edge(cvrt[0], cvrt[1]))
162  continue;
163 
164  //todo: one could check for separate subsets and prohibit removal of vrt in this case.
165  Edge* e = *grid.create<RegularEdge>(EdgeDescriptor(cvrt[0], cvrt[1]), cedge[0]);
166  grid.mark(e);
167  grid.erase(vrt);
168  for(size_t i = 0; i < 2; ++i){
169  if(!grid.is_marked(cvrt[i])){
170  grid.mark(cvrt[i]);
171  candidates.push_back(cvrt[i]);
172  }
173  }
174  }
175  }
176 
177  grid.end_marking();
178 }
179 
180 
181 template <class TEdgeIter, class TAAPos>
182 void SimplifySmoothedPolylines(Grid& grid, TEdgeIter edgesBegin, TEdgeIter edgesEnd,
183  number curvatureThreshold,
184  TAAPos aaPos,
185  number smoothingAlpha,
186  int smoothingIterations)
187 {
189  grid.attach_to_vertices(aSmoothPos);
190  TAAPos aaSmoothPos(grid, aSmoothPos);
191 
192  for(VertexIterator iter = grid.begin<Vertex>(); iter != grid.end<Vertex>(); ++iter)
193  aaSmoothPos[*iter] = aaPos[*iter];
194 
195  LaplacianSmooth(grid, grid.begin<Vertex>(), grid.end<Vertex>(), aaSmoothPos,
196  smoothingAlpha, smoothingIterations);
197 
198  SimplifyPolylines(grid, edgesBegin, edgesEnd, curvatureThreshold, aaSmoothPos);
199 
200  grid.detach_from_vertices(aSmoothPos);
201 }
202 
203 }// end of namespace
204 
205 #endif //__H__UG_simplify_polychain
Iterator that allows to traverse associated elements of a given element.
Definition: associated_elements_iterator.h:51
bool valid() const
Definition: associated_elements_iterator.h:94
void reinit(Grid &grid, TElem *elem)
Definition: associated_elements_iterator.h:73
A generic specialization of IAttachment.
Definition: attachment_pipe.h:263
Can be used to store information about an edge and to construct an edge.
Definition: grid_base_objects.h:464
Base-class for edges.
Definition: grid_base_objects.h:397
virtual Vertex * vertex(size_t index) const
Definition: grid_base_objects.h:366
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
void end_marking()
ends a marking sequence. Call this method when you're done with marking.
Definition: grid.cpp:1285
void unmark(GridObject *obj)
unmarks the object. Calls are only valid between calls to Grid::begin_marking and Grid::end_marking.
Definition: grid_impl.hpp:808
bool is_marked(GridObject *obj) const
returns true if the object is marked, false if not.
Definition: grid_impl.hpp:843
Edge * get_edge(Vertex *v1, Vertex *v2)
returns the edge between v1 and v2, if it exists. Returns NULL if not.
Definition: grid.cpp:1069
void mark(GridObject *obj)
marks the object. Calls are only valid between calls to Grid::begin_marking and Grid::end_marking.
Definition: grid_impl.hpp:773
void erase(GridObject *geomObj)
Definition: grid.cpp:459
void begin_marking()
begin marking.
Definition: grid.cpp:1262
void detach_from_vertices(IAttachment &attachment)
Definition: grid.h:787
geometry_traits< TGeomObj >::iterator begin()
Definition: grid_impl.hpp:164
void attach_to_vertices(IAttachment &attachment, bool passOnValues)
Definition: grid.h:728
geometry_traits< TGeomObj >::iterator create(GridObject *pParent=NULL)
create a custom element.
Definition: grid_impl.hpp:69
geometry_traits< TGeomObj >::iterator end()
Definition: grid_impl.hpp:175
Element callback that returns true, if an element is marked.
Definition: basic_callbacks.h:71
Edges connect two vertices.
Definition: grid_objects_1d.h:66
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
Vertex * GetConnectedVertex(Edge *e, Vertex *v)
returns the vertex that is connected to v via e.
Definition: vertex_util.cpp:78
#define UG_LOG(msg)
Definition: log.h:367
double number
Definition: types.h:124
TNumber deg_to_rad(TNumber deg)
Definition: math_util_impl.hpp:49
void VecSubtract(vector_t &vOut, const vector_t &v1, const vector_t &v2)
subtracts v2 from v1 and stores the result in a vOut
Definition: math_vector_functions_common_impl.hpp:226
vector_t::value_type VecAngle(const vector_t &v1, const vector_t &v2)
returns the angle between two vectors in radiants
Definition: math_vector_functions_common_impl.hpp:401
Definition: smart_pointer.h:814
the ug namespace
ElementStorage< Vertex >::SectionContainer::iterator VertexIterator
This Iterator will be used as base-class for iterators of specialized geometric objects.
Definition: grid_base_object_traits.h:73
void SimplifyPolylines(Grid &grid, TEdgeIter edgesBegin, TEdgeIter edgesEnd, number curvatureThreshold, TAAPos aaPos)
Definition: simplify_polychain.h:54
void SimplifySmoothedPolylines(Grid &grid, TEdgeIter edgesBegin, TEdgeIter edgesEnd, number curvatureThreshold, TAAPos aaPos, number smoothingAlpha, int smoothingIterations)
Definition: simplify_polychain.h:182
void LaplacianSmooth(Grid &grid, TIterator vrtsBegin, TIterator vrtsEnd, AAPosVRT &aaPos, number alpha, int numIterations)
Definition: manifold_smoothing.h:44
boost::function< bool(base_object *)> callback
callback type for the elements base type.
Definition: grid.h:150
Definition: simplify_polychain.h:45
IndCmp(const std::vector< number > &vals)
Definition: simplify_polychain.h:46
bool operator()(size_t i1, size_t i2)
Definition: simplify_polychain.h:47
const std::vector< number > & m_vals
Definition: simplify_polychain.h:50