Tesseract  3.02
tesseract-ocr/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 
00254     bool normalise();
00255 
00257     BOOL8 operator== (const FCOORD & other) {
00258       return xcoord == other.xcoord && ycoord == other.ycoord;
00259     }
00261     BOOL8 operator!= (const FCOORD & other) {
00262       return xcoord != other.xcoord || ycoord != other.ycoord;
00263     }
00265     friend FCOORD operator! (const FCOORD &);
00267     friend FCOORD operator- (const FCOORD &);
00269     friend FCOORD operator+ (const FCOORD &, const FCOORD &);
00271     friend FCOORD & operator+= (FCOORD &, const FCOORD &);
00273     friend FCOORD operator- (const FCOORD &, const FCOORD &);
00275     friend FCOORD & operator-= (FCOORD &, const FCOORD &);
00277     friend float operator% (const FCOORD &, const FCOORD &);
00279     friend float operator *(const FCOORD &, const FCOORD &);
00281     friend FCOORD operator *(const FCOORD &, float);
00283     friend FCOORD operator *(float, const FCOORD &);
00284 
00286     friend FCOORD & operator*= (FCOORD &, float);
00288     friend FCOORD operator/ (const FCOORD &, float);
00291     void rotate(const FCOORD vec);
00292     // unrotate - undo a rotate(vec)
00293     // @param vec by vector
00294     void unrotate(const FCOORD &vec);
00296     friend FCOORD & operator/= (FCOORD &, float);
00297 
00298   private:
00299     float xcoord;                //2 floating coords
00300     float ycoord;
00301 };
00302 
00303 #include          "ipoints.h"    /*do inline funcs */
00304 #endif