From: Bruno Turcksin Date: Mon, 16 Nov 2015 17:31:16 +0000 (-0600) Subject: Allow to iterate on vertices when dim = 2 or 3. X-Git-Tag: v8.4.0-rc2~221^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=878b46bcc086c0dc6289ee371892c9f82cee7f1e;p=dealii.git Allow to iterate on vertices when dim = 2 or 3. --- diff --git a/include/deal.II/grid/tria.h b/include/deal.II/grid/tria.h index 997636d7ff..6609920cbd 100644 --- a/include/deal.II/grid/tria.h +++ b/include/deal.II/grid/tria.h @@ -1408,6 +1408,9 @@ public: typedef TriaIterator > face_iterator; typedef TriaActiveIterator > active_face_iterator; + typedef typename IteratorSelector::vertex_iterator vertex_iterator; + typedef typename IteratorSelector::active_vertex_iterator active_vertex_iterator; + typedef typename IteratorSelector::line_iterator line_iterator; typedef typename IteratorSelector::active_line_iterator active_line_iterator; @@ -2543,6 +2546,35 @@ public: */ face_iterator end_face () const; + /* + * @} + */ + + /*---------------------------------------*/ + /*---------------------------------------*/ + + /** + * @name Vertex iterator functions + * @{ + */ + + /** + * Iterator to the first used vertex. + */ + vertex_iterator begin_vertex() const; + + /** + * Iterator to the first active vertex. Because all vertices are active, + * begin_vertex() and begin_active_vertex() return the same vertex. + */ + active_vertex_iterator begin_active_vertex() const; + + /** + * Iterator past the end; this iterator serves for comparisons of iterators + * with past-the-end or before-the-beginning states. + */ + vertex_iterator end_vertex() const; + /* * @} */ @@ -3034,6 +3066,7 @@ private: */ typedef TriaRawIterator > raw_cell_iterator; typedef TriaRawIterator > raw_face_iterator; + typedef typename IteratorSelector::raw_vertex_iterator raw_vertex_iterator; typedef typename IteratorSelector::raw_line_iterator raw_line_iterator; typedef typename IteratorSelector::raw_quad_iterator raw_quad_iterator; typedef typename IteratorSelector::raw_hex_iterator raw_hex_iterator; diff --git a/include/deal.II/grid/tria_accessor.h b/include/deal.II/grid/tria_accessor.h index ed642565f1..aa5e0170f8 100644 --- a/include/deal.II/grid/tria_accessor.h +++ b/include/deal.II/grid/tria_accessor.h @@ -1546,11 +1546,9 @@ public: TriaAccessor (const InvalidAccessor &); /** - * Return the state of the iterator. Since an iterator to points can not be - * incremented or decremented, its state remains constant, and in particular - * equal to IteratorState::valid. + * Return the state of the iterator. */ - static IteratorState::IteratorStates state (); + IteratorState::IteratorStates state () const; /** * Level of this object. Vertices have no level, so this function always @@ -1571,16 +1569,14 @@ public: * @{ */ /** - * This operator advances the iterator to the next element. For points, this - * operation is not defined, so you can't iterate over point iterators. + * This operator advances the iterator to the next element. */ - void operator ++ () const; + void operator ++ (); /** - * This operator moves the iterator to the previous element. For points, - * this operation is not defined, so you can't iterate over point iterators. + * This operator moves the iterator to the previous element. */ - void operator -- () const; + void operator -- (); /** * Compare for equality. */ diff --git a/include/deal.II/grid/tria_accessor.templates.h b/include/deal.II/grid/tria_accessor.templates.h index 4ce7f713b1..03a760adfa 100644 --- a/include/deal.II/grid/tria_accessor.templates.h +++ b/include/deal.II/grid/tria_accessor.templates.h @@ -2193,9 +2193,12 @@ TriaAccessor<0, dim, spacedim>::copy_from (const TriaAccessor &t) template inline IteratorState::IteratorStates -TriaAccessor<0, dim, spacedim>::state () +TriaAccessor<0, dim, spacedim>::state () const { - return IteratorState::valid; + if (global_vertex_index != numbers::invalid_unsigned_int) + return IteratorState::valid; + else + return IteratorState::past_the_end; } @@ -2223,9 +2226,11 @@ TriaAccessor<0, dim, spacedim>::index () const template inline void -TriaAccessor<0, dim, spacedim>::operator ++ () const +TriaAccessor<0, dim, spacedim>::operator ++ () { - Assert (false, ExcNotImplemented()); + ++global_vertex_index; + if (global_vertex_index >= tria->n_vertices()) + global_vertex_index = numbers::invalid_unsigned_int; } @@ -2233,9 +2238,10 @@ TriaAccessor<0, dim, spacedim>::operator ++ () const template inline void -TriaAccessor<0, dim, spacedim>::operator -- () const +TriaAccessor<0, dim, spacedim>::operator -- () { - Assert (false, ExcNotImplemented()); + if (global_vertex_index!=numbers::invalid_unsigned_int) + --global_vertex_index; } diff --git a/include/deal.II/grid/tria_iterator_selector.h b/include/deal.II/grid/tria_iterator_selector.h index 6a990a7b1d..29c48a1272 100644 --- a/include/deal.II/grid/tria_iterator_selector.h +++ b/include/deal.II/grid/tria_iterator_selector.h @@ -73,7 +73,9 @@ namespace internal template struct Iterators<1,spacedim> { + typedef TriaRawIterator > raw_vertex_iterator; typedef TriaIterator > vertex_iterator; + typedef TriaActiveIterator > active_vertex_iterator; typedef TriaRawIterator > raw_line_iterator; typedef TriaIterator > line_iterator; @@ -134,7 +136,9 @@ namespace internal template struct Iterators<2,spacedim> { + typedef TriaRawIterator > raw_vertex_iterator; typedef TriaIterator > vertex_iterator; + typedef TriaActiveIterator > active_vertex_iterator; typedef TriaRawIterator > raw_line_iterator; typedef TriaIterator > line_iterator; @@ -176,7 +180,9 @@ namespace internal template struct Iterators<3,spacedim> { + typedef TriaRawIterator > raw_vertex_iterator; typedef TriaIterator > vertex_iterator; + typedef TriaActiveIterator > active_vertex_iterator; typedef TriaRawIterator > raw_line_iterator; typedef TriaIterator > line_iterator; diff --git a/source/grid/tria.cc b/source/grid/tria.cc index 05eff9f3e4..d37c7e20a3 100644 --- a/source/grid/tria.cc +++ b/source/grid/tria.cc @@ -10605,6 +10605,59 @@ Triangulation::end_face () const } +/*------------------------ Vertex iterator functions ------------------------*/ + + +template +typename Triangulation::vertex_iterator +Triangulation::begin_vertex() const +{ + if (dim==1) + { + Assert(false, ExcNotImplemented()); + return raw_vertex_iterator(); + } + else + { + vertex_iterator i = raw_vertex_iterator(const_cast*>(this), + 0, + 0); + if (i.state() != IteratorState::valid) + return i; + while (i->used() == false) + if ((++i).state() != IteratorState::valid) + return i; + return i; + } +} + + + +template +typename Triangulation::active_vertex_iterator +Triangulation::begin_active_vertex() const +{ + return begin_vertex(); +} + + + +template +typename Triangulation::vertex_iterator +Triangulation::end_vertex() const +{ + if (dim==1) + { + Assert(false, ExcNotImplemented()); + return raw_vertex_iterator(); + } + else + return raw_vertex_iterator(const_cast*>(this), + -1, + numbers::invalid_unsigned_int); +} + + /*------------------------ Line iterator functions ------------------------*/ diff --git a/tests/grid/vertex_iterator.cc b/tests/grid/vertex_iterator.cc index c9b6df4e31..32d11f8c81 100644 --- a/tests/grid/vertex_iterator.cc +++ b/tests/grid/vertex_iterator.cc @@ -32,6 +32,12 @@ void test () GridGenerator::hyper_cube (tria); tria.refine_global (2); + for (typename Triangulation<2>::vertex_iterator + vertex_it = tria.begin_vertex(); vertex_it != tria.end_vertex(); + ++vertex_it) + deallog << vertex_it->center() <::active_cell_iterator cell = tria.begin_active(); cell != tria.end(); ++cell) { diff --git a/tests/grid/vertex_iterator.output b/tests/grid/vertex_iterator.output index 7631dad293..2b7cf090de 100644 --- a/tests/grid/vertex_iterator.output +++ b/tests/grid/vertex_iterator.output @@ -1,4 +1,30 @@ +DEAL::0.00000 0.00000 +DEAL::1.00000 0.00000 +DEAL::0.00000 1.00000 +DEAL::1.00000 1.00000 +DEAL::0.500000 0.00000 +DEAL::0.00000 0.500000 +DEAL::1.00000 0.500000 +DEAL::0.500000 1.00000 +DEAL::0.500000 0.500000 +DEAL::0.250000 0.00000 +DEAL::0.750000 0.00000 +DEAL::0.00000 0.250000 +DEAL::0.00000 0.750000 +DEAL::1.00000 0.250000 +DEAL::1.00000 0.750000 +DEAL::0.250000 1.00000 +DEAL::0.750000 1.00000 +DEAL::0.500000 0.250000 +DEAL::0.500000 0.750000 +DEAL::0.250000 0.500000 +DEAL::0.750000 0.500000 +DEAL::0.250000 0.250000 +DEAL::0.750000 0.250000 +DEAL::0.250000 0.750000 +DEAL::0.750000 0.750000 +DEAL:: DEAL::0.00000 0.00000 DEAL::0.250000 0.00000 DEAL::0.00000 0.250000