tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/ccstruct/points.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        points.h  (Formerly coords.h)
00003  * Description: Coordinate class definitions.
00004  * Author:      Ray Smith
00005  * Created:     Fri Mar 15 08:32:45 GMT 1991
00006  *
00007  * (C) Copyright 1991, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef           POINTS_H
00021 #define           POINTS_H
00022 
00023 #include          <stdio.h>
00024 #include          <math.h>
00025 #include          "elst.h"
00026 
00027 class FCOORD;
00028 
00030 class ICOORD
00031 {
00032   friend class FCOORD;
00033 
00034   public:
00036     ICOORD() {
00037       xcoord = ycoord = 0;       //default zero
00038     }
00042     ICOORD(inT16 xin,
00043            inT16 yin) {
00044       xcoord = xin;
00045       ycoord = yin;
00046     }
00048     ~ICOORD () {
00049     }
00050 
00052     inT16 x() const {
00053       return xcoord;
00054     }
00056     inT16 y() const {
00057       return ycoord;
00058     }
00059 
00061     void set_x(inT16 xin) {
00062       xcoord = xin;              //write new value
00063     }
00065     void set_y(inT16 yin) {  //value to set
00066       ycoord = yin;
00067     }
00068 
00070     void set_with_shrink(int x, int y);
00071 
00073     float sqlength() const {
00074       return (float) (xcoord * xcoord + ycoord * ycoord);
00075     }
00076 
00078     float length() const {
00079       return (float) sqrt (sqlength ());
00080     }
00081 
00083     float pt_to_pt_sqdist(const ICOORD &pt) const {
00084       ICOORD gap;
00085 
00086       gap.xcoord = xcoord - pt.xcoord;
00087       gap.ycoord = ycoord - pt.ycoord;
00088       return gap.sqlength ();
00089     }
00090 
00092     float pt_to_pt_dist(const ICOORD &pt) const {
00093       return (float) sqrt (pt_to_pt_sqdist (pt));
00094     }
00095 
00097     float angle() const {
00098       return (float) atan2 ((double) ycoord, (double) xcoord);
00099     }
00100 
00102     BOOL8 operator== (const ICOORD & other) const {
00103       return xcoord == other.xcoord && ycoord == other.ycoord;
00104     }
00106     BOOL8 operator!= (const ICOORD & other) const {
00107       return xcoord != other.xcoord || ycoord != other.ycoord;
00108     }
00110     friend ICOORD operator! (const ICOORD &);
00112     friend ICOORD operator- (const ICOORD &);
00114     friend ICOORD operator+ (const ICOORD &, const ICOORD &);
00116     friend ICOORD & operator+= (ICOORD &, const ICOORD &);
00118     friend ICOORD operator- (const ICOORD &, const ICOORD &);
00120     friend ICOORD & operator-= (ICOORD &, const ICOORD &);
00122     friend inT32 operator% (const ICOORD &, const ICOORD &);
00124     friend inT32 operator *(const ICOORD &,
00125                             const ICOORD &);
00127     friend ICOORD operator *(const ICOORD &,
00128                              inT16);
00130     friend ICOORD operator *(inT16,
00131                              const ICOORD &);
00133     friend ICOORD & operator*= (ICOORD &, inT16);
00135     friend ICOORD operator/ (const ICOORD &, inT16);
00137     friend ICOORD & operator/= (ICOORD &, inT16);
00140     void rotate(const FCOORD& vec);
00141 
00147     void setup_render(ICOORD* major_step, ICOORD* minor_step,
00148                       int* major, int* minor) const;
00149 
00150     // Writes to the given file. Returns false in case of error.
00151     bool Serialize(FILE* fp) const;
00152     // Reads from the given file. Returns false in case of error.
00153     // If swap is true, assumes a big/little-endian swap is needed.
00154     bool DeSerialize(bool swap, FILE* fp);
00155 
00156   protected:
00157     inT16 xcoord;                //< x value
00158     inT16 ycoord;                //< y value
00159 };
00160 
00161 class DLLSYM ICOORDELT:public ELIST_LINK, public ICOORD
00162                                  //embedded coord list
00163 {
00164   public:
00166     ICOORDELT() {  
00167     }
00169     ICOORDELT (ICOORD icoord):ICOORD (icoord) {
00170     }
00174     ICOORDELT(inT16 xin,
00175               inT16 yin) {
00176       xcoord = xin;
00177       ycoord = yin;
00178     }
00179 
00180     static ICOORDELT* deep_copy(const ICOORDELT* src) {
00181       ICOORDELT* elt = new ICOORDELT;
00182       *elt = *src;
00183       return elt;
00184     }
00185 
00186 };
00187 
00188 ELISTIZEH (ICOORDELT)
00189 class DLLSYM FCOORD
00190 {
00191   public:
00193     FCOORD() {
00194     }
00198     FCOORD(float xvalue,
00199            float yvalue) {
00200       xcoord = xvalue;           //set coords
00201       ycoord = yvalue;
00202     }
00203     FCOORD(                  //make from ICOORD
00204            ICOORD icoord) {  //coords to set
00205       xcoord = icoord.xcoord;
00206       ycoord = icoord.ycoord;
00207     }
00208 
00209     float x() const {  //get coords
00210       return xcoord;
00211     }
00212     float y() const {
00213       return ycoord;
00214     }
00216     void set_x(float xin) {
00217       xcoord = xin;              //write new value
00218     }
00220     void set_y(float yin) {  //value to set
00221       ycoord = yin;
00222     }
00223 
00225     float sqlength() const {
00226       return xcoord * xcoord + ycoord * ycoord;
00227     }
00228 
00230     float length() const {
00231       return (float) sqrt (sqlength ());
00232     }
00233 
00235     float pt_to_pt_sqdist(const FCOORD &pt) const {
00236       FCOORD gap;
00237 
00238       gap.xcoord = xcoord - pt.xcoord;
00239       gap.ycoord = ycoord - pt.ycoord;
00240       return gap.sqlength ();
00241     }
00242 
00244     float pt_to_pt_dist(const FCOORD &pt) const {
00245       return (float) sqrt (pt_to_pt_sqdist (pt));
00246     }
00247 
00249     float angle() const {
00250       return (float) atan2 (ycoord, xcoord);
00251     }
00252     // Returns the standard feature direction corresponding to this.
00253     // See binary_angle_plus_pi below for a description of the direction.
00254     uinT8 to_direction() const;
00255     // Sets this with a unit vector in the given standard feature direction.
00256     void from_direction(uinT8 direction);
00257 
00258     // Converts an angle in radians (from ICOORD::angle or FCOORD::angle) to a
00259     // standard feature direction as an unsigned angle in 256ths of a circle
00260     // measured anticlockwise from (-1, 0).
00261     static uinT8 binary_angle_plus_pi(double angle);
00262     // Inverse of binary_angle_plus_pi returns an angle in radians for the
00263     // given standard feature direction.
00264     static double angle_from_direction(uinT8 direction);
00265     // Returns the point on the given line nearest to this, ie the point such
00266     // that the vector point->this is perpendicular to the line.
00267     // The line is defined as a line_point and a dir_vector for its direction.
00268     // dir_vector need not be a unit vector.
00269     FCOORD nearest_pt_on_line(const FCOORD& line_point,
00270                               const FCOORD& dir_vector) const;
00271 
00273     bool normalise();
00274 
00276     BOOL8 operator== (const FCOORD & other) {
00277       return xcoord == other.xcoord && ycoord == other.ycoord;
00278     }
00280     BOOL8 operator!= (const FCOORD & other) {
00281       return xcoord != other.xcoord || ycoord != other.ycoord;
00282     }
00284     friend FCOORD operator! (const FCOORD &);
00286     friend FCOORD operator- (const FCOORD &);
00288     friend FCOORD operator+ (const FCOORD &, const FCOORD &);
00290     friend FCOORD & operator+= (FCOORD &, const FCOORD &);
00292     friend FCOORD operator- (const FCOORD &, const FCOORD &);
00294     friend FCOORD & operator-= (FCOORD &, const FCOORD &);
00296     friend float operator% (const FCOORD &, const FCOORD &);
00298     friend float operator *(const FCOORD &, const FCOORD &);
00300     friend FCOORD operator *(const FCOORD &, float);
00302     friend FCOORD operator *(float, const FCOORD &);
00303 
00305     friend FCOORD & operator*= (FCOORD &, float);
00307     friend FCOORD operator/ (const FCOORD &, float);
00310     void rotate(const FCOORD vec);
00311     // unrotate - undo a rotate(vec)
00312     // @param vec by vector
00313     void unrotate(const FCOORD &vec);
00315     friend FCOORD & operator/= (FCOORD &, float);
00316 
00317   private:
00318     float xcoord;                //2 floating coords
00319     float ycoord;
00320 };
00321 
00322 #include          "ipoints.h"    /*do inline funcs */
00323 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines