tesseract  5.0.0-alpha-619-ge9db
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  *
6  * (C) Copyright 1991, Hewlett-Packard Ltd.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #define _USE_MATH_DEFINES // for M_PI
20 
21 #include <algorithm>
22 #include <cmath> // for M_PI
23 #include <cstdlib>
24 #include <tesseract/helpers.h>
25 #include <tesseract/serialis.h>
26 #include "points.h"
27 
28 ELISTIZE (ICOORDELT) //turn to list
29 bool FCOORD::normalise() { //Convert to unit vec
30  float len = length ();
31 
32  if (len < 0.0000000001) {
33  return false;
34  }
35  xcoord /= len;
36  ycoord /= len;
37  return true;
38 }
39 
40 // Set from the given x,y, shrinking the vector to fit if needed.
41 void ICOORD::set_with_shrink(int x, int y) {
42  // Fit the vector into an ICOORD, which is 16 bit.
43  int factor = 1;
44  int max_extent = std::max(abs(x), abs(y));
45  if (max_extent > INT16_MAX)
46  factor = max_extent / INT16_MAX + 1;
47  xcoord = x / factor;
48  ycoord = y / factor;
49 }
50 
51 // The fortran/basic sgn function returns -1, 0, 1 if x < 0, x == 0, x > 0
52 // respectively.
53 static int sign(int x) {
54  if (x < 0)
55  return -1;
56  else
57  return x > 0 ? 1 : 0;
58 }
59 
60 // Writes to the given file. Returns false in case of error.
61 bool ICOORD::Serialize(FILE* fp) const {
62  return tesseract::Serialize(fp, &xcoord) &&
64 }
65 // Reads from the given file. Returns false in case of error.
66 // If swap is true, assumes a big/little-endian swap is needed.
67 bool ICOORD::DeSerialize(bool swap, FILE* fp) {
68  if (!tesseract::DeSerialize(fp, &xcoord)) return false;
69  if (!tesseract::DeSerialize(fp, &ycoord)) return false;
70  if (swap) {
71  ReverseN(&xcoord, sizeof(xcoord));
72  ReverseN(&ycoord, sizeof(ycoord));
73  }
74  return true;
75 }
76 
77 // Setup for iterating over the pixels in a vector by the well-known
78 // Bresenham rendering algorithm.
79 // Starting with major/2 in the accumulator, on each step add major_step,
80 // and then add minor to the accumulator. When the accumulator >= major
81 // subtract major and step a minor step.
82 
83 void ICOORD::setup_render(ICOORD* major_step, ICOORD* minor_step,
84  int* major, int* minor) const {
85  int abs_x = abs(xcoord);
86  int abs_y = abs(ycoord);
87  if (abs_x >= abs_y) {
88  // X-direction is major.
89  major_step->xcoord = sign(xcoord);
90  major_step->ycoord = 0;
91  minor_step->xcoord = 0;
92  minor_step->ycoord = sign(ycoord);
93  *major = abs_x;
94  *minor = abs_y;
95  } else {
96  // Y-direction is major.
97  major_step->xcoord = 0;
98  major_step->ycoord = sign(ycoord);
99  minor_step->xcoord = sign(xcoord);
100  minor_step->ycoord = 0;
101  *major = abs_y;
102  *minor = abs_x;
103  }
104 }
105 
106 // Returns the standard feature direction corresponding to this.
107 // See binary_angle_plus_pi below for a description of the direction.
108 uint8_t FCOORD::to_direction() const {
109  return binary_angle_plus_pi(angle());
110 }
111 // Sets this with a unit vector in the given standard feature direction.
112 void FCOORD::from_direction(uint8_t direction) {
113  double radians = angle_from_direction(direction);
114  xcoord = cos(radians);
115  ycoord = sin(radians);
116 }
117 
118 // Converts an angle in radians (from ICOORD::angle or FCOORD::angle) to a
119 // standard feature direction as an unsigned angle in 256ths of a circle
120 // measured anticlockwise from (-1, 0).
121 uint8_t FCOORD::binary_angle_plus_pi(double radians) {
122  return Modulo(IntCastRounded((radians + M_PI) * 128.0 / M_PI), 256);
123 }
124 // Inverse of binary_angle_plus_pi returns an angle in radians for the
125 // given standard feature direction.
126 double FCOORD::angle_from_direction(uint8_t direction) {
127  return direction * M_PI / 128.0 - M_PI;
128 }
129 
130 // Returns the point on the given line nearest to this, ie the point such
131 // that the vector point->this is perpendicular to the line.
132 // The line is defined as a line_point and a dir_vector for its direction.
133 FCOORD FCOORD::nearest_pt_on_line(const FCOORD& line_point,
134  const FCOORD& dir_vector) const {
135  FCOORD point_vector(*this - line_point);
136  // The dot product (%) is |dir_vector||point_vector|cos theta, so dividing by
137  // the square of the length of dir_vector gives us the fraction of dir_vector
138  // to add to line1 to get the appropriate point, so
139  // result = line1 + lambda dir_vector.
140  double lambda = point_vector % dir_vector / dir_vector.sqlength();
141  return line_point + (dir_vector * lambda);
142 }
ELISTIZE
ELISTIZE(ICOORDELT) bool FCOORD
Definition: points.cpp:27
FCOORD::angle
float angle() const
find angle
Definition: points.h:246
FCOORD::to_direction
uint8_t to_direction() const
Definition: points.cpp:107
FCOORD::from_direction
void from_direction(uint8_t direction)
Definition: points.cpp:111
ICOORD::DeSerialize
bool DeSerialize(bool swap, FILE *fp)
Definition: points.cpp:66
ICOORD
integer coordinate
Definition: points.h:30
IntCastRounded
int IntCastRounded(double x)
Definition: helpers.h:173
FCOORD::angle_from_direction
static double angle_from_direction(uint8_t direction)
Definition: points.cpp:125
ICOORD::x
int16_t x() const
access function
Definition: points.h:51
FCOORD
Definition: points.h:187
ICOORD::setup_render
void setup_render(ICOORD *major_step, ICOORD *minor_step, int *major, int *minor) const
Definition: points.cpp:82
FCOORD::length
float length() const
find length
Definition: points.h:227
ICOORD::xcoord
int16_t xcoord
x value
Definition: points.h:156
ICOORD::Serialize
bool Serialize(FILE *fp) const
Definition: points.cpp:60
helpers.h
FCOORD::normalise
bool normalise()
Convert to unit vec.
ICOORD::ycoord
int16_t ycoord
y value
Definition: points.h:157
FCOORD::nearest_pt_on_line
FCOORD nearest_pt_on_line(const FCOORD &line_point, const FCOORD &dir_vector) const
Definition: points.cpp:132
FCOORD::binary_angle_plus_pi
static uint8_t binary_angle_plus_pi(double angle)
Definition: points.cpp:120
Modulo
int Modulo(int a, int b)
Definition: helpers.h:156
ICOORD::set_with_shrink
void set_with_shrink(int x, int y)
Set from the given x,y, shrinking the vector to fit if needed.
Definition: points.cpp:40
FCOORD::sqlength
float sqlength() const
find sq length
Definition: points.h:222
serialis.h
ReverseN
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:183
tesseract::DeSerialize
bool DeSerialize(FILE *fp, char *data, size_t n=1)
Definition: serialis.cpp:41
ICOORDELT
Definition: points.h:160
tesseract::Serialize
bool Serialize(FILE *fp, const char *data, size_t n=1)
Definition: serialis.cpp:73
ICOORD::y
int16_t y() const
access_function
Definition: points.h:55
points.h