PCL
Rotate.h
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/Rotate.h - Released 2024-12-28T16:53:48Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All 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 names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Rotate_h
53 #define __PCL_Rotate_h
54 
55 #include <pcl/Defs.h>
56 
57 #include <pcl/Iterator.h>
58 #include <pcl/Utility.h>
59 
60 // Template formal parameters:
61 //
62 // FI Forward iterator
63 // BI Bidirectional iterator
64 // RI Random access iterator
65 // UP Unary predicate
66 // BP Binary predicate
67 // T Item type
68 // F Function
69 
70 // ### TODO: Document this file.
71 
72 namespace pcl
73 {
74 
75 // ----------------------------------------------------------------------------
76 // void Reverse( BI, BI )
77 // ----------------------------------------------------------------------------
78 
79 template <class BI> inline
80 void Reverse( BI i, BI j )
81 {
82  __reverse__( i, j, IteratorClass( i ) );
83 }
84 
85 template <class BI> inline
86 void __reverse__( BI i, BI j, BidirectionalIterator )
87 {
88  for ( ; i != j && i != --j; ++i )
89  Swap( *i, *j );
90 }
91 
92 template <class RI> inline
93 void __reverse__( RI i, RI j, RandomAccessIterator )
94 {
95  if ( i < j )
96  for ( ; i < --j; ++i )
97  Swap( *i, *j );
98 }
99 
100 // ----------------------------------------------------------------------------
101 // void Rotate( FI, FI, FI )
102 // ----------------------------------------------------------------------------
103 
104 template <class FI> inline
105 void Rotate( FI i, FI m, FI j )
106 {
107  if ( m != i && m != j )
108  __rotate__( i, m, j, IteratorClass( i ) );
109 }
110 
111 template <class FI> inline
112 void __rotate__( FI i, FI m, FI j, ForwardIterator )
113 {
114  for ( FI k = m; ; )
115  {
116  Swap( *i, *k );
117  if ( ++i == m )
118  if ( ++k == j )
119  break;
120  else
121  m = k;
122  else if ( ++k == j )
123  k = m;
124  }
125 }
126 
127 template <class BI> inline
128 void __rotate__( BI i, BI m, BI j, BidirectionalIterator )
129 {
130  Reverse( i, m );
131  Reverse( m, j );
132  Reverse( i, j );
133 }
134 
135 template <class RI> inline
136 void __rotate__( RI i, RI m, RI j, RandomAccessIterator )
137 {
138  __rotate_ri__( i, m, j, ItemType( i ) );
139 }
140 
141 template <class RI, class T> inline
142 void __rotate_ri__( RI i, RI m, RI j, const T* )
143 {
144  distance_type d = m-i;
145  distance_type n = j-i;
146 
147  for ( distance_type a = d; a != 0; )
148  {
149  distance_type b = n % a;
150  n = a;
151  a = b;
152  }
153 
154  if ( n < j-i )
155  for ( ; 0 < n; --n )
156  {
157  RI x = i+n;
158  RI y = x;
159  RI z = y+d;
160  if ( z == j )
161  z = i;
162 
163  T v = *x;
164 
165  while ( z != x )
166  {
167  *y = *z;
168  y = z;
169  z = (d < j-z) ? z+d : i+(d-(j-z));
170  }
171 
172  *y = v;
173  }
174 }
175 
176 // ----------------------------------------------------------------------------
177 // void Shift( FI, FI, FI, T )
178 // void ShiftLeft( FI, FI, FI, T )
179 // ----------------------------------------------------------------------------
180 
181 template <class FI, class T> inline
182 void Shift( FI i, FI m, FI j, const T& v )
183 {
184  if ( m != i )
185  {
186  for ( ; m != j; ++m, ++i )
187  *i = *m;
188  for ( ; i != j; ++i )
189  *i = v;
190  }
191 }
192 
193 template <class FI, class T> inline
194 void ShiftLeft( FI i, FI m, FI j, const T& v )
195 {
196  Shift( i, m, j, v );
197 }
198 
199 // ----------------------------------------------------------------------------
200 // void ShiftRight( FI, FI, FI, T )
201 // ----------------------------------------------------------------------------
202 
203 template <class FI, class T> inline
204 void ShiftRight( FI i, FI m, FI j, const T& v )
205 {
206  if ( i != j && m != j )
207  __shift_right__( i, m, j, v, IteratorClass( i ) );
208 }
209 
210 template <class FI, class T> inline
211 void __shift_right__( FI i, FI m, FI j, const T& v, ForwardIterator )
212 {
213  do
214  {
215  if ( m != i )
216  {
217  FI k = i;
218  do
219  Swap( *++k, *i );
220  while ( k != m );
221  *m = *i;
222  }
223 
224  *i = v;
225  ++i;
226  }
227  while ( ++m != j );
228 }
229 
230 template <class BI, class T> inline
231 void __shift_right__( BI i, BI m, BI j, const T& v, BidirectionalIterator )
232 {
233  while ( m != i )
234  *--j = *--m;
235  do
236  *m = v;
237  while ( ++m != j );
238 }
239 
240 // ----------------------------------------------------------------------------
241 
242 } // pcl
243 
244 #endif // __PCL_Rotate_h
245 
246 // ----------------------------------------------------------------------------
247 // EOF pcl/Rotate.h - Released 2024-12-28T16:53:48Z
void Rotate(T &x, T &y, T1 sa, T1 ca, T2 xc, T2 yc) noexcept
Definition: Math.h:2055
void Swap(GenericPoint< T > &p1, GenericPoint< T > &p2) noexcept
Definition: Point.h:1459
ptrdiff_t distance_type
Definition: Defs.h:615
PCL root namespace.
Definition: AbstractImage.h:77
C IteratorClass(const Iterator< C, T > &)
Definition: Iterator.h:117
T * ItemType(const Iterator< C, T > &)
Definition: Iterator.h:139