tesseract  4.00.00dev
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  // Returns the standard feature direction corresponding to this.
253  // See binary_angle_plus_pi below for a description of the direction.
254  uinT8 to_direction() const;
255  // Sets this with a unit vector in the given standard feature direction.
256  void from_direction(uinT8 direction);
257 
258  // Converts an angle in radians (from ICOORD::angle or FCOORD::angle) to a
259  // standard feature direction as an unsigned angle in 256ths of a circle
260  // measured anticlockwise from (-1, 0).
261  static uinT8 binary_angle_plus_pi(double angle);
262  // Inverse of binary_angle_plus_pi returns an angle in radians for the
263  // given standard feature direction.
264  static double angle_from_direction(uinT8 direction);
265  // Returns the point on the given line nearest to this, ie the point such
266  // that the vector point->this is perpendicular to the line.
267  // The line is defined as a line_point and a dir_vector for its direction.
268  // dir_vector need not be a unit vector.
269  FCOORD nearest_pt_on_line(const FCOORD& line_point,
270  const FCOORD& dir_vector) const;
271 
273  bool normalise();
274 
276  BOOL8 operator== (const FCOORD & other) {
277  return xcoord == other.xcoord && ycoord == other.ycoord;
278  }
280  BOOL8 operator!= (const FCOORD & other) {
281  return xcoord != other.xcoord || ycoord != other.ycoord;
282  }
284  friend FCOORD operator! (const FCOORD &);
286  friend FCOORD operator- (const FCOORD &);
288  friend FCOORD operator+ (const FCOORD &, const FCOORD &);
290  friend FCOORD & operator+= (FCOORD &, const FCOORD &);
292  friend FCOORD operator- (const FCOORD &, const FCOORD &);
294  friend FCOORD & operator-= (FCOORD &, const FCOORD &);
296  friend float operator% (const FCOORD &, const FCOORD &);
298  friend float operator *(const FCOORD &, const FCOORD &);
300  friend FCOORD operator *(const FCOORD &, float);
302  friend FCOORD operator *(float, const FCOORD &);
303 
305  friend FCOORD & operator*= (FCOORD &, float);
307  friend FCOORD operator/ (const FCOORD &, float);
310  void rotate(const FCOORD vec);
311  // unrotate - undo a rotate(vec)
312  // @param vec by vector
313  void unrotate(const FCOORD &vec);
315  friend FCOORD & operator/= (FCOORD &, float);
316 
317  private:
318  float xcoord; //2 floating coords
319  float ycoord;
320 };
321 
322 #include "ipoints.h" /*do inline funcs */
323 #endif
void setup_render(ICOORD *major_step, ICOORD *minor_step, int *major, int *minor) const
Definition: points.cpp:86
void rotate(const FCOORD &vec)
Definition: ipoints.h:241
float length() const
find length
Definition: points.h:78
float angle() const
find angle
Definition: points.h:97
friend ICOORD operator/(const ICOORD &, inT16)
divide
Definition: ipoints.h:208
Definition: points.h:189
int32_t inT32
Definition: host.h:38
float pt_to_pt_dist(const FCOORD &pt) const
Distance between pts.
Definition: points.h:244
ICOORD(inT16 xin, inT16 yin)
Definition: points.h:42
float sqlength() const
find sq length
Definition: points.h:73
~ICOORD()
destructor
Definition: points.h:48
#define ELISTIZEH(CLASSNAME)
Definition: elst.h:948
friend ICOORD operator+(const ICOORD &, const ICOORD &)
add
Definition: ipoints.h:68
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
friend ICOORD & operator*=(ICOORD &, inT16)
multiply
Definition: ipoints.h:192
float angle() const
find angle
Definition: points.h:249
inT16 x() const
access function
Definition: points.h:52
FCOORD(ICOORD icoord)
Definition: points.h:203
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
friend ICOORD operator!(const ICOORD &)
rotate 90 deg anti
Definition: ipoints.h:32
inT16 ycoord
Definition: points.h:158
ICOORDELT()
empty constructor
Definition: points.h:166
void set_x(float xin)
rewrite function
Definition: points.h:216
bool DeSerialize(bool swap, FILE *fp)
Definition: points.cpp:70
void set_y(float yin)
rewrite function
Definition: points.h:220
int16_t inT16
Definition: host.h:36
friend inT32 operator*(const ICOORD &, const ICOORD &)
cross product
Definition: ipoints.h:149
friend ICOORD operator-(const ICOORD &)
unary minus
Definition: ipoints.h:50
unsigned char BOOL8
Definition: host.h:44
float sqlength() const
find sq length
Definition: points.h:225
inT16 y() const
access_function
Definition: points.h:56
float pt_to_pt_sqdist(const FCOORD &pt) const
sq dist between pts
Definition: points.h:235
bool Serialize(FILE *fp) const
Definition: points.cpp:63
float pt_to_pt_dist(const ICOORD &pt) const
Distance between pts.
Definition: points.h:92
ICOORD()
empty constructor
Definition: points.h:36
FCOORD(float xvalue, float yvalue)
Definition: points.h:198
ICOORDELT(ICOORD icoord)
constructor from ICOORD
Definition: points.h:169
BOOL8 operator!=(const ICOORD &other) const
test inequality
Definition: points.h:106
ICOORDELT(inT16 xin, inT16 yin)
Definition: points.h:174
friend ICOORD & operator/=(ICOORD &, inT16)
divide
Definition: ipoints.h:226
float y() const
Definition: points.h:212
float pt_to_pt_sqdist(const ICOORD &pt) const
sq dist between pts
Definition: points.h:83
uint8_t uinT8
Definition: host.h:35
static ICOORDELT * deep_copy(const ICOORDELT *src)
Definition: points.h:180
#define DLLSYM
Definition: platform.h:25
void set_with_shrink(int x, int y)
Set from the given x,y, shrinking the vector to fit if needed.
Definition: points.cpp:43
float length() const
find length
Definition: points.h:230
FCOORD()
empty constructor
Definition: points.h:193
BOOL8 operator==(const ICOORD &other) const
test equality
Definition: points.h:102
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
void set_y(inT16 yin)
rewrite function
Definition: points.h:65
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