Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
points.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: points.h (Formerly coords.h)
3  * Description: Coordinate class definitions.
4  * Author: Ray Smith
5  * Created: Fri Mar 15 08:32:45 GMT 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 POINTS_H
21 #define POINTS_H
22 
23 #include <stdio.h>
24 #include <math.h>
25 #include "elst.h"
26 
27 class FCOORD;
28 
30 class ICOORD
31 {
32  friend class FCOORD;
33 
34  public:
36  ICOORD() {
37  xcoord = ycoord = 0; //default zero
38  }
43  inT16 yin) {
44  xcoord = xin;
45  ycoord = yin;
46  }
48  ~ICOORD () {
49  }
50 
52  inT16 x() const {
53  return xcoord;
54  }
56  inT16 y() const {
57  return ycoord;
58  }
59 
61  void set_x(inT16 xin) {
62  xcoord = xin; //write new value
63  }
65  void set_y(inT16 yin) { //value to set
66  ycoord = yin;
67  }
68 
70  void set_with_shrink(int x, int y);
71 
73  float sqlength() const {
74  return (float) (xcoord * xcoord + ycoord * ycoord);
75  }
76 
78  float length() const {
79  return (float) sqrt (sqlength ());
80  }
81 
83  float pt_to_pt_sqdist(const ICOORD &pt) const {
84  ICOORD gap;
85 
86  gap.xcoord = xcoord - pt.xcoord;
87  gap.ycoord = ycoord - pt.ycoord;
88  return gap.sqlength ();
89  }
90 
92  float pt_to_pt_dist(const ICOORD &pt) const {
93  return (float) sqrt (pt_to_pt_sqdist (pt));
94  }
95 
97  float angle() const {
98  return (float) atan2 ((double) ycoord, (double) xcoord);
99  }
100 
102  BOOL8 operator== (const ICOORD & other) const {
103  return xcoord == other.xcoord && ycoord == other.ycoord;
104  }
106  BOOL8 operator!= (const ICOORD & other) const {
107  return xcoord != other.xcoord || ycoord != other.ycoord;
108  }
110  friend ICOORD operator! (const ICOORD &);
112  friend ICOORD operator- (const ICOORD &);
114  friend ICOORD operator+ (const ICOORD &, const ICOORD &);
116  friend ICOORD & operator+= (ICOORD &, const ICOORD &);
118  friend ICOORD operator- (const ICOORD &, const ICOORD &);
120  friend ICOORD & operator-= (ICOORD &, const ICOORD &);
122  friend inT32 operator% (const ICOORD &, const ICOORD &);
124  friend inT32 operator *(const ICOORD &,
125  const ICOORD &);
127  friend ICOORD operator *(const ICOORD &,
128  inT16);
130  friend ICOORD operator *(inT16,
131  const ICOORD &);
133  friend ICOORD & operator*= (ICOORD &, inT16);
135  friend ICOORD operator/ (const ICOORD &, inT16);
137  friend ICOORD & operator/= (ICOORD &, inT16);
140  void rotate(const FCOORD& vec);
141 
147  void setup_render(ICOORD* major_step, ICOORD* minor_step,
148  int* major, int* minor) const;
149 
150  // Writes to the given file. Returns false in case of error.
151  bool Serialize(FILE* fp) const;
152  // Reads from the given file. Returns false in case of error.
153  // If swap is true, assumes a big/little-endian swap is needed.
154  bool DeSerialize(bool swap, FILE* fp);
155 
156  protected:
157  inT16 xcoord; //< x value
158  inT16 ycoord; //< y value
159 };
160 
161 class DLLSYM ICOORDELT:public ELIST_LINK, public ICOORD
162  //embedded coord list
163 {
164  public:
167  }
169  ICOORDELT (ICOORD icoord):ICOORD (icoord) {
170  }
175  inT16 yin) {
176  xcoord = xin;
177  ycoord = yin;
178  }
179 
180  static ICOORDELT* deep_copy(const ICOORDELT* src) {
181  ICOORDELT* elt = new ICOORDELT;
182  *elt = *src;
183  return elt;
184  }
185 
186 };
187 
190 {
191  public:
193  FCOORD() {
194  }
198  FCOORD(float xvalue,
199  float yvalue) {
200  xcoord = xvalue; //set coords
201  ycoord = yvalue;
202  }
203  FCOORD( //make from ICOORD
204  ICOORD icoord) { //coords to set
205  xcoord = icoord.xcoord;
206  ycoord = icoord.ycoord;
207  }
208 
209  float x() const { //get coords
210  return xcoord;
211  }
212  float y() const {
213  return ycoord;
214  }
216  void set_x(float xin) {
217  xcoord = xin; //write new value
218  }
220  void set_y(float yin) { //value to set
221  ycoord = yin;
222  }
223 
225  float sqlength() const {
226  return xcoord * xcoord + ycoord * ycoord;
227  }
228 
230  float length() const {
231  return (float) sqrt (sqlength ());
232  }
233 
235  float pt_to_pt_sqdist(const FCOORD &pt) const {
236  FCOORD gap;
237 
238  gap.xcoord = xcoord - pt.xcoord;
239  gap.ycoord = ycoord - pt.ycoord;
240  return gap.sqlength ();
241  }
242 
244  float pt_to_pt_dist(const FCOORD &pt) const {
245  return (float) sqrt (pt_to_pt_sqdist (pt));
246  }
247 
249  float angle() const {
250  return (float) atan2 (ycoord, xcoord);
251  }
252 
254  bool normalise();
255 
257  BOOL8 operator== (const FCOORD & other) {
258  return xcoord == other.xcoord && ycoord == other.ycoord;
259  }
261  BOOL8 operator!= (const FCOORD & other) {
262  return xcoord != other.xcoord || ycoord != other.ycoord;
263  }
265  friend FCOORD operator! (const FCOORD &);
267  friend FCOORD operator- (const FCOORD &);
269  friend FCOORD operator+ (const FCOORD &, const FCOORD &);
271  friend FCOORD & operator+= (FCOORD &, const FCOORD &);
273  friend FCOORD operator- (const FCOORD &, const FCOORD &);
275  friend FCOORD & operator-= (FCOORD &, const FCOORD &);
277  friend float operator% (const FCOORD &, const FCOORD &);
279  friend float operator *(const FCOORD &, const FCOORD &);
281  friend FCOORD operator *(const FCOORD &, float);
283  friend FCOORD operator *(float, const FCOORD &);
284 
286  friend FCOORD & operator*= (FCOORD &, float);
288  friend FCOORD operator/ (const FCOORD &, float);
291  void rotate(const FCOORD vec);
292  // unrotate - undo a rotate(vec)
293  // @param vec by vector
294  void unrotate(const FCOORD &vec);
296  friend FCOORD & operator/= (FCOORD &, float);
297 
298  private:
299  float xcoord; //2 floating coords
300  float ycoord;
301 };
302 
303 #include "ipoints.h" /*do inline funcs */
304 #endif