tesseract  4.00.00dev
ipoints.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: ipoints.h (Formerly icoords.h)
3  * Description: Inline functions for coords.h.
4  * Author: Ray Smith
5  * Created: Fri Jun 21 15:14:21 BST 1991
6  *
7  * (C) Copyright 1991, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #ifndef IPOINTS_H
21 #define IPOINTS_H
22 
23 #include <math.h>
24 
25 /**********************************************************************
26  * operator!
27  *
28  * Rotate an ICOORD 90 degrees anticlockwise.
29  **********************************************************************/
30 
31 inline ICOORD
32 operator! ( //rotate 90 deg anti
33 const ICOORD & src //thing to rotate
34 ) {
35  ICOORD result; //output
36 
37  result.xcoord = -src.ycoord;
38  result.ycoord = src.xcoord;
39  return result;
40 }
41 
42 
43 /**********************************************************************
44  * operator-
45  *
46  * Unary minus of an ICOORD.
47  **********************************************************************/
48 
49 inline ICOORD
50 operator- ( //unary minus
51 const ICOORD & src //thing to minus
52 ) {
53  ICOORD result; //output
54 
55  result.xcoord = -src.xcoord;
56  result.ycoord = -src.ycoord;
57  return result;
58 }
59 
60 
61 /**********************************************************************
62  * operator+
63  *
64  * Add 2 ICOORDS.
65  **********************************************************************/
66 
67 inline ICOORD
68 operator+ ( //sum vectors
69 const ICOORD & op1, //operands
70 const ICOORD & op2) {
71  ICOORD sum; //result
72 
73  sum.xcoord = op1.xcoord + op2.xcoord;
74  sum.ycoord = op1.ycoord + op2.ycoord;
75  return sum;
76 }
77 
78 
79 /**********************************************************************
80  * operator+=
81  *
82  * Add 2 ICOORDS.
83  **********************************************************************/
84 
85 inline ICOORD &
86 operator+= ( //sum vectors
87 ICOORD & op1, //operands
88 const ICOORD & op2) {
89  op1.xcoord += op2.xcoord;
90  op1.ycoord += op2.ycoord;
91  return op1;
92 }
93 
94 
95 /**********************************************************************
96  * operator-
97  *
98  * Subtract 2 ICOORDS.
99  **********************************************************************/
100 
101 inline ICOORD
102 operator- ( //subtract vectors
103 const ICOORD & op1, //operands
104 const ICOORD & op2) {
105  ICOORD sum; //result
106 
107  sum.xcoord = op1.xcoord - op2.xcoord;
108  sum.ycoord = op1.ycoord - op2.ycoord;
109  return sum;
110 }
111 
112 
113 /**********************************************************************
114  * operator-=
115  *
116  * Subtract 2 ICOORDS.
117  **********************************************************************/
118 
119 inline ICOORD &
120 operator-= ( //sum vectors
121 ICOORD & op1, //operands
122 const ICOORD & op2) {
123  op1.xcoord -= op2.xcoord;
124  op1.ycoord -= op2.ycoord;
125  return op1;
126 }
127 
128 
129 /**********************************************************************
130  * operator%
131  *
132  * Scalar product of 2 ICOORDS.
133  **********************************************************************/
134 
135 inline inT32
136 operator% ( //scalar product
137 const ICOORD & op1, //operands
138 const ICOORD & op2) {
139  return op1.xcoord * op2.xcoord + op1.ycoord * op2.ycoord;
140 }
141 
142 
143 /**********************************************************************
144  * operator*
145  *
146  * Cross product of 2 ICOORDS.
147  **********************************************************************/
148 
149 inline inT32 operator *( //cross product
150  const ICOORD &op1, //operands
151  const ICOORD &op2) {
152  return op1.xcoord * op2.ycoord - op1.ycoord * op2.xcoord;
153 }
154 
155 
156 /**********************************************************************
157  * operator*
158  *
159  * Scalar multiply of an ICOORD.
160  **********************************************************************/
161 
162 inline ICOORD operator *( //scalar multiply
163  const ICOORD &op1, //operands
164  inT16 scale) {
165  ICOORD result; //output
166 
167  result.xcoord = op1.xcoord * scale;
168  result.ycoord = op1.ycoord * scale;
169  return result;
170 }
171 
172 
173 inline ICOORD operator *( //scalar multiply
174  inT16 scale,
175  const ICOORD &op1 //operands
176  ) {
177  ICOORD result; //output
178 
179  result.xcoord = op1.xcoord * scale;
180  result.ycoord = op1.ycoord * scale;
181  return result;
182 }
183 
184 
185 /**********************************************************************
186  * operator*=
187  *
188  * Scalar multiply of an ICOORD.
189  **********************************************************************/
190 
191 inline ICOORD &
192 operator*= ( //scalar multiply
193 ICOORD & op1, //operands
194 inT16 scale) {
195  op1.xcoord *= scale;
196  op1.ycoord *= scale;
197  return op1;
198 }
199 
200 
201 /**********************************************************************
202  * operator/
203  *
204  * Scalar divide of an ICOORD.
205  **********************************************************************/
206 
207 inline ICOORD
208 operator/ ( //scalar divide
209 const ICOORD & op1, //operands
210 inT16 scale) {
211  ICOORD result; //output
212 
213  result.xcoord = op1.xcoord / scale;
214  result.ycoord = op1.ycoord / scale;
215  return result;
216 }
217 
218 
219 /**********************************************************************
220  * operator/=
221  *
222  * Scalar divide of an ICOORD.
223  **********************************************************************/
224 
225 inline ICOORD &
226 operator/= ( //scalar divide
227 ICOORD & op1, //operands
228 inT16 scale) {
229  op1.xcoord /= scale;
230  op1.ycoord /= scale;
231  return op1;
232 }
233 
234 
235 /**********************************************************************
236  * ICOORD::rotate
237  *
238  * Rotate an ICOORD by the given (normalized) (cos,sin) vector.
239  **********************************************************************/
240 
241 inline void ICOORD::rotate( //rotate by vector
242  const FCOORD& vec) {
243  inT16 tmp;
244 
245  tmp = (inT16) floor (xcoord * vec.x () - ycoord * vec.y () + 0.5);
246  ycoord = (inT16) floor (ycoord * vec.x () + xcoord * vec.y () + 0.5);
247  xcoord = tmp;
248 }
249 
250 
251 /**********************************************************************
252  * operator!
253  *
254  * Rotate an FCOORD 90 degrees anticlockwise.
255  **********************************************************************/
256 
257 inline FCOORD
258 operator! ( //rotate 90 deg anti
259 const FCOORD & src //thing to rotate
260 ) {
261  FCOORD result; //output
262 
263  result.xcoord = -src.ycoord;
264  result.ycoord = src.xcoord;
265  return result;
266 }
267 
268 
269 /**********************************************************************
270  * operator-
271  *
272  * Unary minus of an FCOORD.
273  **********************************************************************/
274 
275 inline FCOORD
276 operator- ( //unary minus
277 const FCOORD & src //thing to minus
278 ) {
279  FCOORD result; //output
280 
281  result.xcoord = -src.xcoord;
282  result.ycoord = -src.ycoord;
283  return result;
284 }
285 
286 
287 /**********************************************************************
288  * operator+
289  *
290  * Add 2 FCOORDS.
291  **********************************************************************/
292 
293 inline FCOORD
294 operator+ ( //sum vectors
295 const FCOORD & op1, //operands
296 const FCOORD & op2) {
297  FCOORD sum; //result
298 
299  sum.xcoord = op1.xcoord + op2.xcoord;
300  sum.ycoord = op1.ycoord + op2.ycoord;
301  return sum;
302 }
303 
304 
305 /**********************************************************************
306  * operator+=
307  *
308  * Add 2 FCOORDS.
309  **********************************************************************/
310 
311 inline FCOORD &
312 operator+= ( //sum vectors
313 FCOORD & op1, //operands
314 const FCOORD & op2) {
315  op1.xcoord += op2.xcoord;
316  op1.ycoord += op2.ycoord;
317  return op1;
318 }
319 
320 
321 /**********************************************************************
322  * operator-
323  *
324  * Subtract 2 FCOORDS.
325  **********************************************************************/
326 
327 inline FCOORD
328 operator- ( //subtract vectors
329 const FCOORD & op1, //operands
330 const FCOORD & op2) {
331  FCOORD sum; //result
332 
333  sum.xcoord = op1.xcoord - op2.xcoord;
334  sum.ycoord = op1.ycoord - op2.ycoord;
335  return sum;
336 }
337 
338 
339 /**********************************************************************
340  * operator-=
341  *
342  * Subtract 2 FCOORDS.
343  **********************************************************************/
344 
345 inline FCOORD &
346 operator-= ( //sum vectors
347 FCOORD & op1, //operands
348 const FCOORD & op2) {
349  op1.xcoord -= op2.xcoord;
350  op1.ycoord -= op2.ycoord;
351  return op1;
352 }
353 
354 
355 /**********************************************************************
356  * operator%
357  *
358  * Scalar product of 2 FCOORDS.
359  **********************************************************************/
360 
361 inline float
362 operator% ( //scalar product
363 const FCOORD & op1, //operands
364 const FCOORD & op2) {
365  return op1.xcoord * op2.xcoord + op1.ycoord * op2.ycoord;
366 }
367 
368 
369 /**********************************************************************
370  * operator*
371  *
372  * Cross product of 2 FCOORDS.
373  **********************************************************************/
374 
375 inline float operator *( //cross product
376  const FCOORD &op1, //operands
377  const FCOORD &op2) {
378  return op1.xcoord * op2.ycoord - op1.ycoord * op2.xcoord;
379 }
380 
381 
382 /**********************************************************************
383  * operator*
384  *
385  * Scalar multiply of an FCOORD.
386  **********************************************************************/
387 
388 inline FCOORD operator *( //scalar multiply
389  const FCOORD &op1, //operands
390  float scale) {
391  FCOORD result; //output
392 
393  result.xcoord = op1.xcoord * scale;
394  result.ycoord = op1.ycoord * scale;
395  return result;
396 }
397 
398 
399 inline FCOORD operator *( //scalar multiply
400  float scale,
401  const FCOORD &op1 //operands
402  ) {
403  FCOORD result; //output
404 
405  result.xcoord = op1.xcoord * scale;
406  result.ycoord = op1.ycoord * scale;
407  return result;
408 }
409 
410 
411 /**********************************************************************
412  * operator*=
413  *
414  * Scalar multiply of an FCOORD.
415  **********************************************************************/
416 
417 inline FCOORD &
418 operator*= ( //scalar multiply
419 FCOORD & op1, //operands
420 float scale) {
421  op1.xcoord *= scale;
422  op1.ycoord *= scale;
423  return op1;
424 }
425 
426 
427 /**********************************************************************
428  * operator/
429  *
430  * Scalar divide of an FCOORD.
431  **********************************************************************/
432 
433 inline FCOORD
434 operator/ ( //scalar divide
435 const FCOORD & op1, //operands
436 float scale) {
437  FCOORD result; //output
438 
439  if (scale != 0) {
440  result.xcoord = op1.xcoord / scale;
441  result.ycoord = op1.ycoord / scale;
442  }
443  return result;
444 }
445 
446 
447 /**********************************************************************
448  * operator/=
449  *
450  * Scalar divide of an FCOORD.
451  **********************************************************************/
452 
453 inline FCOORD &
454 operator/= ( //scalar divide
455 FCOORD & op1, //operands
456 float scale) {
457  if (scale != 0) {
458  op1.xcoord /= scale;
459  op1.ycoord /= scale;
460  }
461  return op1;
462 }
463 
464 
465 /**********************************************************************
466  * rotate
467  *
468  * Rotate an FCOORD by the given (normalized) (cos,sin) vector.
469  **********************************************************************/
470 
471 inline void FCOORD::rotate( //rotate by vector
472  const FCOORD vec) {
473  float tmp;
474 
475  tmp = xcoord * vec.x () - ycoord * vec.y ();
476  ycoord = ycoord * vec.x () + xcoord * vec.y ();
477  xcoord = tmp;
478 }
479 
480 inline void FCOORD::unrotate(const FCOORD& vec) {
481  rotate(FCOORD(vec.x(), -vec.y()));
482 }
483 
484 #endif
ICOORD operator+(const ICOORD &op1, const ICOORD &op2)
Definition: ipoints.h:68
void rotate(const FCOORD &vec)
Definition: ipoints.h:241
friend ICOORD operator/(const ICOORD &, inT16)
divide
Definition: ipoints.h:208
Definition: points.h:189
int32_t inT32
Definition: host.h:38
void rotate(const FCOORD vec)
Definition: ipoints.h:471
friend ICOORD operator+(const ICOORD &, const ICOORD &)
add
Definition: ipoints.h:68
friend ICOORD & operator*=(ICOORD &, inT16)
multiply
Definition: ipoints.h:192
ICOORD & operator*=(ICOORD &op1, inT16 scale)
Definition: ipoints.h:192
inT32 operator%(const ICOORD &op1, const ICOORD &op2)
Definition: ipoints.h:136
ICOORD operator-(const ICOORD &src)
Definition: ipoints.h:50
friend ICOORD operator!(const ICOORD &)
rotate 90 deg anti
Definition: ipoints.h:32
inT16 ycoord
Definition: points.h:158
int16_t inT16
Definition: host.h:36
ICOORD & operator/=(ICOORD &op1, inT16 scale)
Definition: ipoints.h:226
friend inT32 operator*(const ICOORD &, const ICOORD &)
cross product
Definition: ipoints.h:149
friend ICOORD operator-(const ICOORD &)
unary minus
Definition: ipoints.h:50
ICOORD & operator+=(ICOORD &op1, const ICOORD &op2)
Definition: ipoints.h:86
void unrotate(const FCOORD &vec)
Definition: ipoints.h:480
inT32 operator*(const ICOORD &op1, const ICOORD &op2)
Definition: ipoints.h:149
ICOORD operator/(const ICOORD &op1, inT16 scale)
Definition: ipoints.h:208
friend ICOORD & operator/=(ICOORD &, inT16)
divide
Definition: ipoints.h:226
float y() const
Definition: points.h:212
ICOORD operator!(const ICOORD &src)
Definition: ipoints.h:32
friend class FCOORD
Definition: points.h:32
friend inT32 operator%(const ICOORD &, const ICOORD &)
scalar product
Definition: ipoints.h:136
float x() const
Definition: points.h:209
inT16 xcoord
Definition: points.h:157
ICOORD & operator-=(ICOORD &op1, const ICOORD &op2)
Definition: ipoints.h:120
friend ICOORD & operator-=(ICOORD &, const ICOORD &)
subtract
Definition: ipoints.h:120
integer coordinate
Definition: points.h:30
friend ICOORD & operator+=(ICOORD &, const ICOORD &)
add
Definition: ipoints.h:86