Tesseract  3.02
tesseract-ocr/ccstruct/ipoints.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        ipoints.h  (Formerly icoords.h)
00003  * Description: Inline functions for coords.h.
00004  * Author:                                      Ray Smith
00005  * Created:                                     Fri Jun 21 15:14:21 BST 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           IPOINTS_H
00021 #define           IPOINTS_H
00022 
00023 #include          <math.h>
00024 
00025 /**********************************************************************
00026  * operator!
00027  *
00028  * Rotate an ICOORD 90 degrees anticlockwise.
00029  **********************************************************************/
00030 
00031 inline ICOORD
00032 operator! (                      //rotate 90 deg anti
00033 const ICOORD & src               //thing to rotate
00034 ) {
00035   ICOORD result;                 //output
00036 
00037   result.xcoord = -src.ycoord;
00038   result.ycoord = src.xcoord;
00039   return result;
00040 }
00041 
00042 
00043 /**********************************************************************
00044  * operator-
00045  *
00046  * Unary minus of an ICOORD.
00047  **********************************************************************/
00048 
00049 inline ICOORD
00050 operator- (                      //unary minus
00051 const ICOORD & src               //thing to minus
00052 ) {
00053   ICOORD result;                 //output
00054 
00055   result.xcoord = -src.xcoord;
00056   result.ycoord = -src.ycoord;
00057   return result;
00058 }
00059 
00060 
00061 /**********************************************************************
00062  * operator+
00063  *
00064  * Add 2 ICOORDS.
00065  **********************************************************************/
00066 
00067 inline ICOORD
00068 operator+ (                      //sum vectors
00069 const ICOORD & op1,              //operands
00070 const ICOORD & op2) {
00071   ICOORD sum;                    //result
00072 
00073   sum.xcoord = op1.xcoord + op2.xcoord;
00074   sum.ycoord = op1.ycoord + op2.ycoord;
00075   return sum;
00076 }
00077 
00078 
00079 /**********************************************************************
00080  * operator+=
00081  *
00082  * Add 2 ICOORDS.
00083  **********************************************************************/
00084 
00085 inline ICOORD &
00086 operator+= (                     //sum vectors
00087 ICOORD & op1,                    //operands
00088 const ICOORD & op2) {
00089   op1.xcoord += op2.xcoord;
00090   op1.ycoord += op2.ycoord;
00091   return op1;
00092 }
00093 
00094 
00095 /**********************************************************************
00096  * operator-
00097  *
00098  * Subtract 2 ICOORDS.
00099  **********************************************************************/
00100 
00101 inline ICOORD
00102 operator- (                      //subtract vectors
00103 const ICOORD & op1,              //operands
00104 const ICOORD & op2) {
00105   ICOORD sum;                    //result
00106 
00107   sum.xcoord = op1.xcoord - op2.xcoord;
00108   sum.ycoord = op1.ycoord - op2.ycoord;
00109   return sum;
00110 }
00111 
00112 
00113 /**********************************************************************
00114  * operator-=
00115  *
00116  * Subtract 2 ICOORDS.
00117  **********************************************************************/
00118 
00119 inline ICOORD &
00120 operator-= (                     //sum vectors
00121 ICOORD & op1,                    //operands
00122 const ICOORD & op2) {
00123   op1.xcoord -= op2.xcoord;
00124   op1.ycoord -= op2.ycoord;
00125   return op1;
00126 }
00127 
00128 
00129 /**********************************************************************
00130  * operator%
00131  *
00132  * Scalar product of 2 ICOORDS.
00133  **********************************************************************/
00134 
00135 inline inT32
00136 operator% (                      //scalar product
00137 const ICOORD & op1,              //operands
00138 const ICOORD & op2) {
00139   return op1.xcoord * op2.xcoord + op1.ycoord * op2.ycoord;
00140 }
00141 
00142 
00143 /**********************************************************************
00144  * operator*
00145  *
00146  * Cross product of 2 ICOORDS.
00147  **********************************************************************/
00148 
00149 inline inT32 operator *(                    //cross product
00150                         const ICOORD &op1,  //operands
00151                         const ICOORD &op2) {
00152   return op1.xcoord * op2.ycoord - op1.ycoord * op2.xcoord;
00153 }
00154 
00155 
00156 /**********************************************************************
00157  * operator*
00158  *
00159  * Scalar multiply of an ICOORD.
00160  **********************************************************************/
00161 
00162 inline ICOORD operator *(                    //scalar multiply
00163                          const ICOORD &op1,  //operands
00164                          inT16 scale) {
00165   ICOORD result;                 //output
00166 
00167   result.xcoord = op1.xcoord * scale;
00168   result.ycoord = op1.ycoord * scale;
00169   return result;
00170 }
00171 
00172 
00173 inline ICOORD operator *(                   //scalar multiply
00174                          inT16 scale,
00175                          const ICOORD &op1  //operands
00176                         ) {
00177   ICOORD result;                 //output
00178 
00179   result.xcoord = op1.xcoord * scale;
00180   result.ycoord = op1.ycoord * scale;
00181   return result;
00182 }
00183 
00184 
00185 /**********************************************************************
00186  * operator*=
00187  *
00188  * Scalar multiply of an ICOORD.
00189  **********************************************************************/
00190 
00191 inline ICOORD &
00192 operator*= (                     //scalar multiply
00193 ICOORD & op1,                    //operands
00194 inT16 scale) {
00195   op1.xcoord *= scale;
00196   op1.ycoord *= scale;
00197   return op1;
00198 }
00199 
00200 
00201 /**********************************************************************
00202  * operator/
00203  *
00204  * Scalar divide of an ICOORD.
00205  **********************************************************************/
00206 
00207 inline ICOORD
00208 operator/ (                      //scalar divide
00209 const ICOORD & op1,              //operands
00210 inT16 scale) {
00211   ICOORD result;                 //output
00212 
00213   result.xcoord = op1.xcoord / scale;
00214   result.ycoord = op1.ycoord / scale;
00215   return result;
00216 }
00217 
00218 
00219 /**********************************************************************
00220  * operator/=
00221  *
00222  * Scalar divide of an ICOORD.
00223  **********************************************************************/
00224 
00225 inline ICOORD &
00226 operator/= (                     //scalar divide
00227 ICOORD & op1,                    //operands
00228 inT16 scale) {
00229   op1.xcoord /= scale;
00230   op1.ycoord /= scale;
00231   return op1;
00232 }
00233 
00234 
00235 /**********************************************************************
00236  * ICOORD::rotate
00237  *
00238  * Rotate an ICOORD by the given (normalized) (cos,sin) vector.
00239  **********************************************************************/
00240 
00241 inline void ICOORD::rotate(  //rotate by vector
00242                            const FCOORD& vec) {
00243   inT16 tmp;
00244 
00245   tmp = (inT16) floor (xcoord * vec.x () - ycoord * vec.y () + 0.5);
00246   ycoord = (inT16) floor (ycoord * vec.x () + xcoord * vec.y () + 0.5);
00247   xcoord = tmp;
00248 }
00249 
00250 
00251 /**********************************************************************
00252  * operator!
00253  *
00254  * Rotate an FCOORD 90 degrees anticlockwise.
00255  **********************************************************************/
00256 
00257 inline FCOORD
00258 operator! (                      //rotate 90 deg anti
00259 const FCOORD & src               //thing to rotate
00260 ) {
00261   FCOORD result;                 //output
00262 
00263   result.xcoord = -src.ycoord;
00264   result.ycoord = src.xcoord;
00265   return result;
00266 }
00267 
00268 
00269 /**********************************************************************
00270  * operator-
00271  *
00272  * Unary minus of an FCOORD.
00273  **********************************************************************/
00274 
00275 inline FCOORD
00276 operator- (                      //unary minus
00277 const FCOORD & src               //thing to minus
00278 ) {
00279   FCOORD result;                 //output
00280 
00281   result.xcoord = -src.xcoord;
00282   result.ycoord = -src.ycoord;
00283   return result;
00284 }
00285 
00286 
00287 /**********************************************************************
00288  * operator+
00289  *
00290  * Add 2 FCOORDS.
00291  **********************************************************************/
00292 
00293 inline FCOORD
00294 operator+ (                      //sum vectors
00295 const FCOORD & op1,              //operands
00296 const FCOORD & op2) {
00297   FCOORD sum;                    //result
00298 
00299   sum.xcoord = op1.xcoord + op2.xcoord;
00300   sum.ycoord = op1.ycoord + op2.ycoord;
00301   return sum;
00302 }
00303 
00304 
00305 /**********************************************************************
00306  * operator+=
00307  *
00308  * Add 2 FCOORDS.
00309  **********************************************************************/
00310 
00311 inline FCOORD &
00312 operator+= (                     //sum vectors
00313 FCOORD & op1,                    //operands
00314 const FCOORD & op2) {
00315   op1.xcoord += op2.xcoord;
00316   op1.ycoord += op2.ycoord;
00317   return op1;
00318 }
00319 
00320 
00321 /**********************************************************************
00322  * operator-
00323  *
00324  * Subtract 2 FCOORDS.
00325  **********************************************************************/
00326 
00327 inline FCOORD
00328 operator- (                      //subtract vectors
00329 const FCOORD & op1,              //operands
00330 const FCOORD & op2) {
00331   FCOORD sum;                    //result
00332 
00333   sum.xcoord = op1.xcoord - op2.xcoord;
00334   sum.ycoord = op1.ycoord - op2.ycoord;
00335   return sum;
00336 }
00337 
00338 
00339 /**********************************************************************
00340  * operator-=
00341  *
00342  * Subtract 2 FCOORDS.
00343  **********************************************************************/
00344 
00345 inline FCOORD &
00346 operator-= (                     //sum vectors
00347 FCOORD & op1,                    //operands
00348 const FCOORD & op2) {
00349   op1.xcoord -= op2.xcoord;
00350   op1.ycoord -= op2.ycoord;
00351   return op1;
00352 }
00353 
00354 
00355 /**********************************************************************
00356  * operator%
00357  *
00358  * Scalar product of 2 FCOORDS.
00359  **********************************************************************/
00360 
00361 inline float
00362 operator% (                      //scalar product
00363 const FCOORD & op1,              //operands
00364 const FCOORD & op2) {
00365   return op1.xcoord * op2.xcoord + op1.ycoord * op2.ycoord;
00366 }
00367 
00368 
00369 /**********************************************************************
00370  * operator*
00371  *
00372  * Cross product of 2 FCOORDS.
00373  **********************************************************************/
00374 
00375 inline float operator *(                    //cross product
00376                         const FCOORD &op1,  //operands
00377                         const FCOORD &op2) {
00378   return op1.xcoord * op2.ycoord - op1.ycoord * op2.xcoord;
00379 }
00380 
00381 
00382 /**********************************************************************
00383  * operator*
00384  *
00385  * Scalar multiply of an FCOORD.
00386  **********************************************************************/
00387 
00388 inline FCOORD operator *(                    //scalar multiply
00389                          const FCOORD &op1,  //operands
00390                          float scale) {
00391   FCOORD result;                 //output
00392 
00393   result.xcoord = op1.xcoord * scale;
00394   result.ycoord = op1.ycoord * scale;
00395   return result;
00396 }
00397 
00398 
00399 inline FCOORD operator *(                   //scalar multiply
00400                          float scale,
00401                          const FCOORD &op1  //operands
00402                         ) {
00403   FCOORD result;                 //output
00404 
00405   result.xcoord = op1.xcoord * scale;
00406   result.ycoord = op1.ycoord * scale;
00407   return result;
00408 }
00409 
00410 
00411 /**********************************************************************
00412  * operator*=
00413  *
00414  * Scalar multiply of an FCOORD.
00415  **********************************************************************/
00416 
00417 inline FCOORD &
00418 operator*= (                     //scalar multiply
00419 FCOORD & op1,                    //operands
00420 float scale) {
00421   op1.xcoord *= scale;
00422   op1.ycoord *= scale;
00423   return op1;
00424 }
00425 
00426 
00427 /**********************************************************************
00428  * operator/
00429  *
00430  * Scalar divide of an FCOORD.
00431  **********************************************************************/
00432 
00433 inline FCOORD
00434 operator/ (                      //scalar divide
00435 const FCOORD & op1,              //operands
00436 float scale) {
00437   FCOORD result;                 //output
00438 
00439   if (scale != 0) {
00440     result.xcoord = op1.xcoord / scale;
00441     result.ycoord = op1.ycoord / scale;
00442   }
00443   return result;
00444 }
00445 
00446 
00447 /**********************************************************************
00448  * operator/=
00449  *
00450  * Scalar divide of an FCOORD.
00451  **********************************************************************/
00452 
00453 inline FCOORD &
00454 operator/= (                     //scalar divide
00455 FCOORD & op1,                    //operands
00456 float scale) {
00457   if (scale != 0) {
00458     op1.xcoord /= scale;
00459     op1.ycoord /= scale;
00460   }
00461   return op1;
00462 }
00463 
00464 
00465 /**********************************************************************
00466  * rotate
00467  *
00468  * Rotate an FCOORD by the given (normalized) (cos,sin) vector.
00469  **********************************************************************/
00470 
00471 inline void FCOORD::rotate(  //rotate by vector
00472                            const FCOORD vec) {
00473   float tmp;
00474 
00475   tmp = xcoord * vec.x () - ycoord * vec.y ();
00476   ycoord = ycoord * vec.x () + xcoord * vec.y ();
00477   xcoord = tmp;
00478 }
00479 
00480 inline void FCOORD::unrotate(const FCOORD& vec) {
00481   rotate(FCOORD(vec.x(), -vec.y()));
00482 }
00483 
00484 #endif