• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>for_each_segment</title>
5<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../../index.html" title="Chapter 1. Geometry">
8<link rel="up" href="../for_each.html" title="for_each">
9<link rel="prev" href="for_each_point.html" title="for_each_point">
10<link rel="next" href="../intersection.html" title="intersection">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="for_each_point.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../for_each.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../intersection.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h5 class="title">
27<a name="geometry.reference.algorithms.for_each.for_each_segment"></a><a class="link" href="for_each_segment.html" title="for_each_segment">for_each_segment</a>
28</h5></div></div></div>
29<p>
30            <a class="indexterm" name="idm45635696171952"></a>
31Applies function <span class="bold"><strong>f</strong></span> to each segment.
32          </p>
33<h6>
34<a name="geometry.reference.algorithms.for_each.for_each_segment.h0"></a>
35            <span class="phrase"><a name="geometry.reference.algorithms.for_each.for_each_segment.description"></a></span><a class="link" href="for_each_segment.html#geometry.reference.algorithms.for_each.for_each_segment.description">Description</a>
36          </h6>
37<p>
38            Applies a function <span class="bold"><strong>f</strong></span> (functor, having
39            operator() defined) to each segment making up the geometry
40          </p>
41<h6>
42<a name="geometry.reference.algorithms.for_each.for_each_segment.h1"></a>
43            <span class="phrase"><a name="geometry.reference.algorithms.for_each.for_each_segment.synopsis"></a></span><a class="link" href="for_each_segment.html#geometry.reference.algorithms.for_each.for_each_segment.synopsis">Synopsis</a>
44          </h6>
45<p>
46</p>
47<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Geometry</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Functor</span><span class="special">&gt;</span>
48<span class="identifier">Functor</span> <span class="identifier">for_each_segment</span><span class="special">(</span><span class="identifier">Geometry</span> <span class="special">&amp;</span> <span class="identifier">geometry</span><span class="special">,</span> <span class="identifier">Functor</span> <span class="identifier">f</span><span class="special">)</span></pre>
49<p>
50          </p>
51<h6>
52<a name="geometry.reference.algorithms.for_each.for_each_segment.h2"></a>
53            <span class="phrase"><a name="geometry.reference.algorithms.for_each.for_each_segment.parameters"></a></span><a class="link" href="for_each_segment.html#geometry.reference.algorithms.for_each.for_each_segment.parameters">Parameters</a>
54          </h6>
55<div class="informaltable"><table class="table">
56<colgroup>
57<col>
58<col>
59<col>
60<col>
61</colgroup>
62<thead><tr>
63<th>
64                    <p>
65                      Type
66                    </p>
67                  </th>
68<th>
69                    <p>
70                      Concept
71                    </p>
72                  </th>
73<th>
74                    <p>
75                      Name
76                    </p>
77                  </th>
78<th>
79                    <p>
80                      Description
81                    </p>
82                  </th>
83</tr></thead>
84<tbody>
85<tr>
86<td>
87                    <p>
88                      Geometry &amp;
89                    </p>
90                  </td>
91<td>
92                    <p>
93                      Any type fulfilling a Geometry Concept
94                    </p>
95                  </td>
96<td>
97                    <p>
98                      geometry
99                    </p>
100                  </td>
101<td>
102                    <p>
103                      A model of the specified concept
104                    </p>
105                  </td>
106</tr>
107<tr>
108<td>
109                    <p>
110                      Functor
111                    </p>
112                  </td>
113<td>
114                    <p>
115                      Function or class with operator()
116                    </p>
117                  </td>
118<td>
119                    <p>
120                      f
121                    </p>
122                  </td>
123<td>
124                    <p>
125                      Unary function, taking a segment as argument
126                    </p>
127                  </td>
128</tr>
129</tbody>
130</table></div>
131<h6>
132<a name="geometry.reference.algorithms.for_each.for_each_segment.h3"></a>
133            <span class="phrase"><a name="geometry.reference.algorithms.for_each.for_each_segment.header"></a></span><a class="link" href="for_each_segment.html#geometry.reference.algorithms.for_each.for_each_segment.header">Header</a>
134          </h6>
135<p>
136            Either
137          </p>
138<p>
139            <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
140          </p>
141<p>
142            Or
143          </p>
144<p>
145            <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">algorithms</span><span class="special">/</span><span class="identifier">for_each</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
146          </p>
147<h6>
148<a name="geometry.reference.algorithms.for_each.for_each_segment.h4"></a>
149            <span class="phrase"><a name="geometry.reference.algorithms.for_each.for_each_segment.conformance"></a></span><a class="link" href="for_each_segment.html#geometry.reference.algorithms.for_each.for_each_segment.conformance">Conformance</a>
150          </h6>
151<p>
152            The function for_each_segment is not defined by OGC.
153          </p>
154<p>
155            The function for_each_segment conforms to the std::for_each function
156            of the C++ std-library.
157          </p>
158<h6>
159<a name="geometry.reference.algorithms.for_each.for_each_segment.h5"></a>
160            <span class="phrase"><a name="geometry.reference.algorithms.for_each.for_each_segment.example"></a></span><a class="link" href="for_each_segment.html#geometry.reference.algorithms.for_each.for_each_segment.example">Example</a>
161          </h6>
162<p>
163            Sample using for_each_segment, using a functor to get the minimum and
164            maximum length of a segment in a linestring
165          </p>
166<p>
167</p>
168<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
169
170<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
171<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">linestring</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
172<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">point_xy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
173
174<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">assign</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
175
176
177<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Segment</span><span class="special">&gt;</span>
178<span class="keyword">struct</span> <span class="identifier">gather_segment_statistics</span>
179<span class="special">{</span>
180    <span class="comment">// Remember that if coordinates are integer, the length might be floating point</span>
181    <span class="comment">// So use "double" for integers. In other cases, use coordinate type</span>
182    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">select_most_precise</span>
183        <span class="special">&lt;</span>
184            <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">coordinate_type</span><span class="special">&lt;</span><span class="identifier">Segment</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">,</span>
185            <span class="keyword">double</span>
186        <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">type</span><span class="special">;</span>
187
188    <span class="identifier">type</span> <span class="identifier">min_length</span><span class="special">,</span> <span class="identifier">max_length</span><span class="special">;</span>
189
190    <span class="comment">// Initialize min and max</span>
191    <span class="identifier">gather_segment_statistics</span><span class="special">()</span>
192        <span class="special">:</span> <span class="identifier">min_length</span><span class="special">(</span><span class="number">1e38</span><span class="special">)</span>
193        <span class="special">,</span> <span class="identifier">max_length</span><span class="special">(-</span><span class="number">1</span><span class="special">)</span>
194    <span class="special">{}</span>
195
196    <span class="comment">// This operator is called for each segment</span>
197    <span class="keyword">inline</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Segment</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">s</span><span class="special">)</span>
198    <span class="special">{</span>
199        <span class="identifier">type</span> <span class="identifier">length</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">length</span><span class="special">(</span><span class="identifier">s</span><span class="special">);</span>
200        <span class="keyword">if</span> <span class="special">(</span><span class="identifier">length</span> <span class="special">&lt;</span> <span class="identifier">min_length</span><span class="special">)</span> <span class="identifier">min_length</span> <span class="special">=</span> <span class="identifier">length</span><span class="special">;</span>
201        <span class="keyword">if</span> <span class="special">(</span><span class="identifier">length</span> <span class="special">&gt;</span> <span class="identifier">max_length</span><span class="special">)</span> <span class="identifier">max_length</span> <span class="special">=</span> <span class="identifier">length</span><span class="special">;</span>
202    <span class="special">}</span>
203<span class="special">};</span>
204
205<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
206<span class="special">{</span>
207    <span class="comment">// Bring "+=" for a vector into scope</span>
208    <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">assign</span><span class="special">;</span>
209
210    <span class="comment">// Define a type</span>
211    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">d2</span><span class="special">::</span><span class="identifier">point_xy</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">point</span><span class="special">;</span>
212
213    <span class="comment">// Declare a linestring</span>
214    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">linestring</span><span class="special">&lt;</span><span class="identifier">point</span><span class="special">&gt;</span> <span class="identifier">polyline</span><span class="special">;</span>
215
216    <span class="comment">// Use Boost.Assign to initialize a linestring</span>
217    <span class="identifier">polyline</span> <span class="special">+=</span> <span class="identifier">point</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">),</span> <span class="identifier">point</span><span class="special">(</span><span class="number">3</span><span class="special">,</span> <span class="number">3</span><span class="special">),</span> <span class="identifier">point</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">1</span><span class="special">),</span> <span class="identifier">point</span><span class="special">(</span><span class="number">6</span><span class="special">,</span> <span class="number">2</span><span class="special">),</span>
218                <span class="identifier">point</span><span class="special">(</span><span class="number">8</span><span class="special">,</span> <span class="number">0</span><span class="special">),</span> <span class="identifier">point</span><span class="special">(</span><span class="number">4</span><span class="special">,</span> <span class="special">-</span><span class="number">4</span><span class="special">),</span> <span class="identifier">point</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="special">-</span><span class="number">1</span><span class="special">),</span> <span class="identifier">point</span><span class="special">(</span><span class="number">3</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
219
220
221    <span class="comment">// Declare the gathering class...</span>
222    <span class="identifier">gather_segment_statistics</span>
223        <span class="special">&lt;</span>
224            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">referring_segment</span><span class="special">&lt;</span><span class="identifier">point</span><span class="special">&gt;</span>
225        <span class="special">&gt;</span> <span class="identifier">functor</span><span class="special">;</span>
226
227    <span class="comment">// ... and use it, the essention.</span>
228    <span class="comment">// As also in std::for_each it is a const value, so retrieve it as a return value.</span>
229    <span class="identifier">functor</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">for_each_segment</span><span class="special">(</span><span class="identifier">polyline</span><span class="special">,</span> <span class="identifier">functor</span><span class="special">);</span>
230
231    <span class="comment">// Output the results</span>
232    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
233        <span class="special">&lt;&lt;</span> <span class="string">"Min segment length: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">functor</span><span class="special">.</span><span class="identifier">min_length</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span>
234        <span class="special">&lt;&lt;</span> <span class="string">"Max segment length: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">functor</span><span class="special">.</span><span class="identifier">max_length</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
235
236    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
237<span class="special">}</span>
238</pre>
239<p>
240          </p>
241<p>
242            Output:
243          </p>
244<pre class="programlisting">Min segment length: 1.41421
245Max segment length: 5.65685
246</pre>
247</div>
248<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
249<td align="left"></td>
250<td align="right"><div class="copyright-footer">Copyright © 2009-2019 Barend Gehrels, Bruno Lalande, Mateusz Loskot, Adam
251      Wulkiewicz, Oracle and/or its affiliates<p>
252        Distributed under the Boost Software License, Version 1.0. (See accompanying
253        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
254      </p>
255</div></td>
256</tr></table>
257<hr>
258<div class="spirit-nav">
259<a accesskey="p" href="for_each_point.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../for_each.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../intersection.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
260</div>
261</body>
262</html>
263