tesseract  4.0.0-1-g2a2b
points.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: points.cpp (Formerly coords.c)
3  * Description: Member functions for coordinate classes.
4  * Author: Ray Smith
5  * Created: Fri Mar 15 08:58:17 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 #ifdef _MSC_VER
21 #define _USE_MATH_DEFINES
22 #endif // _MSC_VER
23 
24 #include <algorithm>
25 #include <cstdlib>
26 #include "helpers.h"
27 #include "serialis.h"
28 #include "points.h"
29 
30 ELISTIZE (ICOORDELT) //turn to list
31 bool FCOORD::normalise() { //Convert to unit vec
32  float len = length ();
33 
34  if (len < 0.0000000001) {
35  return false;
36  }
37  xcoord /= len;
38  ycoord /= len;
39  return true;
40 }
41 
42 // Set from the given x,y, shrinking the vector to fit if needed.
43 void ICOORD::set_with_shrink(int x, int y) {
44  // Fit the vector into an ICOORD, which is 16 bit.
45  int factor = 1;
46  int max_extent = std::max(abs(x), abs(y));
47  if (max_extent > INT16_MAX)
48  factor = max_extent / INT16_MAX + 1;
49  xcoord = x / factor;
50  ycoord = y / factor;
51 }
52 
53 // The fortran/basic sgn function returns -1, 0, 1 if x < 0, x == 0, x > 0
54 // respectively.
55 static int sign(int x) {
56  if (x < 0)
57  return -1;
58  else
59  return x > 0 ? 1 : 0;
60 }
61 
62 // Writes to the given file. Returns false in case of error.
63 bool ICOORD::Serialize(FILE* fp) const {
64  return tesseract::Serialize(fp, &xcoord) &&
66 }
67 // Reads from the given file. Returns false in case of error.
68 // If swap is true, assumes a big/little-endian swap is needed.
69 bool ICOORD::DeSerialize(bool swap, FILE* fp) {
70  if (!tesseract::DeSerialize(fp, &xcoord)) return false;
71  if (!tesseract::DeSerialize(fp, &ycoord)) return false;
72  if (swap) {
73  ReverseN(&xcoord, sizeof(xcoord));
74  ReverseN(&ycoord, sizeof(ycoord));
75  }
76  return true;
77 }
78 
79 // Setup for iterating over the pixels in a vector by the well-known
80 // Bresenham rendering algorithm.
81 // Starting with major/2 in the accumulator, on each step add major_step,
82 // and then add minor to the accumulator. When the accumulator >= major
83 // subtract major and step a minor step.
84 
85 void ICOORD::setup_render(ICOORD* major_step, ICOORD* minor_step,
86  int* major, int* minor) const {
87  int abs_x = abs(xcoord);
88  int abs_y = abs(ycoord);
89  if (abs_x >= abs_y) {
90  // X-direction is major.
91  major_step->xcoord = sign(xcoord);
92  major_step->ycoord = 0;
93  minor_step->xcoord = 0;
94  minor_step->ycoord = sign(ycoord);
95  *major = abs_x;
96  *minor = abs_y;
97  } else {
98  // Y-direction is major.
99  major_step->xcoord = 0;
100  major_step->ycoord = sign(ycoord);
101  minor_step->xcoord = sign(xcoord);
102  minor_step->ycoord = 0;
103  *major = abs_y;
104  *minor = abs_x;
105  }
106 }
107 
108 // Returns the standard feature direction corresponding to this.
109 // See binary_angle_plus_pi below for a description of the direction.
110 uint8_t FCOORD::to_direction() const {
111  return binary_angle_plus_pi(angle());
112 }
113 // Sets this with a unit vector in the given standard feature direction.
115  double radians = angle_from_direction(direction);
116  xcoord = cos(radians);
117  ycoord = sin(radians);
118 }
119 
120 // Converts an angle in radians (from ICOORD::angle or FCOORD::angle) to a
121 // standard feature direction as an unsigned angle in 256ths of a circle
122 // measured anticlockwise from (-1, 0).
123 uint8_t FCOORD::binary_angle_plus_pi(double radians) {
124  return Modulo(IntCastRounded((radians + M_PI) * 128.0 / M_PI), 256);
125 }
126 // Inverse of binary_angle_plus_pi returns an angle in radians for the
127 // given standard feature direction.
129  return direction * M_PI / 128.0 - M_PI;
130 }
131 
132 // Returns the point on the given line nearest to this, ie the point such
133 // that the vector point->this is perpendicular to the line.
134 // The line is defined as a line_point and a dir_vector for its direction.
136  const FCOORD& dir_vector) const {
137  FCOORD point_vector(*this - line_point);
138  // The dot product (%) is |dir_vector||point_vector|cos theta, so dividing by
139  // the square of the length of dir_vector gives us the fraction of dir_vector
140  // to add to line1 to get the appropriate point, so
141  // result = line1 + lambda dir_vector.
142  double lambda = point_vector % dir_vector / dir_vector.sqlength();
143  return line_point + (dir_vector * lambda);
144 }
bool Serialize(FILE *fp) const
Definition: points.cpp:63
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
bool normalise()
Convert to unit vec.
int16_t y() const
access_function
Definition: points.h:57
int Modulo(int a, int b)
Definition: helpers.h:153
float angle() const
find angle
Definition: points.h:248
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
void from_direction(uint8_t direction)
Definition: points.cpp:114
void setup_render(ICOORD *major_step, ICOORD *minor_step, int *major, int *minor) const
Definition: points.cpp:85
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178
static uint8_t binary_angle_plus_pi(double angle)
Definition: points.cpp:123
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53
float sqlength() const
find sq length
Definition: points.h:224
int IntCastRounded(double x)
Definition: helpers.h:168
int16_t xcoord
Definition: points.h:158
static double angle_from_direction(uint8_t direction)
Definition: points.cpp:128
float length() const
find length
Definition: points.h:229
Definition: points.h:189
int16_t ycoord
Definition: points.h:159
uint8_t to_direction() const
Definition: points.cpp:110
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27
bool DeSerialize(bool swap, FILE *fp)
Definition: points.cpp:69
FCOORD nearest_pt_on_line(const FCOORD &line_point, const FCOORD &dir_vector) const
Definition: points.cpp:135
ELISTIZE(ICOORDELT) bool FCOORD
Definition: points.cpp:30