Tesseract 3.01
/data/source/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) {
00103       return xcoord == other.xcoord && ycoord == other.ycoord;
00104     }
00106     BOOL8 operator!= (const ICOORD & other) {
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   protected:
00151     inT16 xcoord;                //< x value
00152     inT16 ycoord;                //< y value
00153 };
00154 
00155 class DLLSYM ICOORDELT:public ELIST_LINK, public ICOORD
00156                                  //embedded coord list
00157 {
00158   public:
00160     ICOORDELT() {  
00161     }
00163     ICOORDELT (ICOORD icoord):ICOORD (icoord) {
00164     }
00168     ICOORDELT(inT16 xin,
00169               inT16 yin) {
00170       xcoord = xin;
00171       ycoord = yin;
00172     }
00173 
00174     static ICOORDELT* deep_copy(const ICOORDELT* src) {
00175       ICOORDELT* elt = new ICOORDELT;
00176       *elt = *src;
00177       return elt;
00178     }
00179 
00180 };
00181 
00182 ELISTIZEH (ICOORDELT)
00183 class DLLSYM FCOORD
00184 {
00185   public:
00187     FCOORD() {
00188     }
00192     FCOORD(float xvalue,
00193            float yvalue) {
00194       xcoord = xvalue;           //set coords
00195       ycoord = yvalue;
00196     }
00197     FCOORD(                  //make from ICOORD
00198            ICOORD icoord) {  //coords to set
00199       xcoord = icoord.xcoord;
00200       ycoord = icoord.ycoord;
00201     }
00202 
00203     float x() const {  //get coords
00204       return xcoord;
00205     }
00206     float y() const {
00207       return ycoord;
00208     }
00210     void set_x(float xin) {
00211       xcoord = xin;              //write new value
00212     }
00214     void set_y(float yin) {  //value to set
00215       ycoord = yin;
00216     }
00217 
00219     float sqlength() const {
00220       return xcoord * xcoord + ycoord * ycoord;
00221     }
00222 
00224     float length() const {
00225       return (float) sqrt (sqlength ());
00226     }
00227 
00229     float pt_to_pt_sqdist(const FCOORD &pt) const {
00230       FCOORD gap;
00231 
00232       gap.xcoord = xcoord - pt.xcoord;
00233       gap.ycoord = ycoord - pt.ycoord;
00234       return gap.sqlength ();
00235     }
00236 
00238     float pt_to_pt_dist(const FCOORD &pt) const {
00239       return (float) sqrt (pt_to_pt_sqdist (pt));
00240     }
00241 
00243     float angle() const {
00244       return (float) atan2 (ycoord, xcoord);
00245     }
00246 
00248     bool normalise();
00249 
00251     BOOL8 operator== (const FCOORD & other) {
00252       return xcoord == other.xcoord && ycoord == other.ycoord;
00253     }
00255     BOOL8 operator!= (const FCOORD & other) {
00256       return xcoord != other.xcoord || ycoord != other.ycoord;
00257     }
00259     friend FCOORD operator! (const FCOORD &);
00261     friend FCOORD operator- (const FCOORD &);
00263     friend FCOORD operator+ (const FCOORD &, const FCOORD &);
00265     friend FCOORD & operator+= (FCOORD &, const FCOORD &);
00267     friend FCOORD operator- (const FCOORD &, const FCOORD &);
00269     friend FCOORD & operator-= (FCOORD &, const FCOORD &);
00271     friend float operator% (const FCOORD &, const FCOORD &);
00273     friend float operator *(const FCOORD &, const FCOORD &);
00275     friend FCOORD operator *(const FCOORD &, float);
00277     friend FCOORD operator *(float, const FCOORD &);
00278 
00280     friend FCOORD & operator*= (FCOORD &, float);
00282     friend FCOORD operator/ (const FCOORD &, float);
00285     void rotate(const FCOORD vec);
00286     // unrotate - undo a rotate(vec)
00287     // @param vec by vector
00288     void unrotate(const FCOORD &vec);
00290     friend FCOORD & operator/= (FCOORD &, float);
00291 
00292   private:
00293     float xcoord;                //2 floating coords
00294     float ycoord;
00295 };
00296 
00297 #include          "ipoints.h"    /*do inline funcs */
00298 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines