Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
points.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 * File: points.c (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 #include "mfcpch.h" //precompiled headers
21 #include <stdlib.h>
22 #include "helpers.h"
23 #include "ndminx.h"
24 #include "serialis.h"
25 #include "points.h"
26
27 ELISTIZE (ICOORDELT) //turn to list
28 bool FCOORD::normalise() { //Convert to unit vec
29 float len = length ();
30
31 if (len < 0.0000000001) {
32 return false;
33 }
34 xcoord /= len;
35 ycoord /= len;
36 return true;
37 }
38
39 // Set from the given x,y, shrinking the vector to fit if needed.
40 void ICOORD::set_with_shrink(int x, int y) {
41 // Fit the vector into an ICOORD, which is 16 bit.
42 int factor = 1;
43 int max_extent = MAX(abs(x), abs(y));
44 if (max_extent > MAX_INT16)
45 factor = max_extent / MAX_INT16 + 1;
46 xcoord = x / factor;
47 ycoord = y / factor;
48 }
49
50 // The fortran/basic sgn function returns -1, 0, 1 if x < 0, x == 0, x > 0
51 // respectively.
52 static int sign(int x) {
53 if (x < 0)
54 return -1;
55 else
56 return x > 0 ? 1 : 0;
57 }
58
59 // Writes to the given file. Returns false in case of error.
60 bool ICOORD::Serialize(FILE* fp) const {
61 if (fwrite(&xcoord, sizeof(xcoord), 1, fp) != 1) return false;
62 if (fwrite(&ycoord, sizeof(ycoord), 1, fp) != 1) return false;
63 return true;
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 (fread(&xcoord, sizeof(xcoord), 1, fp) != 1) return false;
69 if (fread(&ycoord, sizeof(ycoord), 1, fp) != 1) 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 }