tesseract 4.1.1
Loading...
Searching...
No Matches
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 "helpers.h"
25#include "serialis.h"
26#include "points.h"
27
28ELISTIZE (ICOORDELT) //turn to list
29bool 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.
41void 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.
53static 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.
61bool 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.
67bool 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
83void 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.
108uint8_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.
112void 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).
121uint8_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.
126double 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.
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}
#define ELISTIZE(CLASSNAME)
Definition: elst.h:931
int Modulo(int a, int b)
Definition: helpers.h:158
int IntCastRounded(double x)
Definition: helpers.h:175
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:185
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:28
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:60
integer coordinate
Definition: points.h:32
void set_with_shrink(int x, int y)
Set from the given x,y, shrinking the vector to fit if needed.
Definition: points.cpp:41
int16_t y() const
access_function
Definition: points.h:56
int16_t ycoord
y value
Definition: points.h:158
void setup_render(ICOORD *major_step, ICOORD *minor_step, int *major, int *minor) const
Definition: points.cpp:83
bool Serialize(FILE *fp) const
Definition: points.cpp:61
int16_t xcoord
x value
Definition: points.h:157
int16_t x() const
access function
Definition: points.h:52
bool DeSerialize(bool swap, FILE *fp)
Definition: points.cpp:67
Definition: points.h:189
static double angle_from_direction(uint8_t direction)
Definition: points.cpp:126
uint8_t to_direction() const
Definition: points.cpp:108
void from_direction(uint8_t direction)
Definition: points.cpp:112
float angle() const
find angle
Definition: points.h:247
static uint8_t binary_angle_plus_pi(double angle)
Definition: points.cpp:121
float sqlength() const
find sq length
Definition: points.h:223
FCOORD nearest_pt_on_line(const FCOORD &line_point, const FCOORD &dir_vector) const
Definition: points.cpp:133