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.
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
12  ** Unless required by applicable law or agreed to in writing, software
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 <stdlib.h>
25 #include "helpers.h"
26 #include "ndminx.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 = MAX(abs(x), abs(y));
47  if (max_extent > MAX_INT16)
48  factor = max_extent / MAX_INT16 + 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  if (fwrite(&xcoord, sizeof(xcoord), 1, fp) != 1) return false;
65  if (fwrite(&ycoord, sizeof(ycoord), 1, fp) != 1) return false;
66  return true;
67 }
68 // Reads from the given file. Returns false in case of error.
69 // If swap is true, assumes a big/little-endian swap is needed.
70 bool ICOORD::DeSerialize(bool swap, FILE* fp) {
71  if (fread(&xcoord, sizeof(xcoord), 1, fp) != 1) return false;
72  if (fread(&ycoord, sizeof(ycoord), 1, fp) != 1) return false;
73  if (swap) {
74  ReverseN(&xcoord, sizeof(xcoord));
75  ReverseN(&ycoord, sizeof(ycoord));
76  }
77  return true;
78 }
79
80 // Setup for iterating over the pixels in a vector by the well-known
81 // Bresenham rendering algorithm.
82 // Starting with major/2 in the accumulator, on each step add major_step,
83 // and then add minor to the accumulator. When the accumulator >= major
84 // subtract major and step a minor step.
85
86 void ICOORD::setup_render(ICOORD* major_step, ICOORD* minor_step,
87  int* major, int* minor) const {
88  int abs_x = abs(xcoord);
89  int abs_y = abs(ycoord);
90  if (abs_x >= abs_y) {
91  // X-direction is major.
92  major_step->xcoord = sign(xcoord);
93  major_step->ycoord = 0;
94  minor_step->xcoord = 0;
95  minor_step->ycoord = sign(ycoord);
96  *major = abs_x;
97  *minor = abs_y;
98  } else {
99  // Y-direction is major.
100  major_step->xcoord = 0;
101  major_step->ycoord = sign(ycoord);
102  minor_step->xcoord = sign(xcoord);
103  minor_step->ycoord = 0;
104  *major = abs_y;
105  *minor = abs_x;
106  }
107 }
108
109 // Returns the standard feature direction corresponding to this.
110 // See binary_angle_plus_pi below for a description of the direction.
112  return binary_angle_plus_pi(angle());
113 }
114 // Sets this with a unit vector in the given standard feature direction.
119 }
120
121 // Converts an angle in radians (from ICOORD::angle or FCOORD::angle) to a
122 // standard feature direction as an unsigned angle in 256ths of a circle
123 // measured anticlockwise from (-1, 0).
125  return Modulo(IntCastRounded((radians + M_PI) * 128.0 / M_PI), 256);
126 }
127 // Inverse of binary_angle_plus_pi returns an angle in radians for the
128 // given standard feature direction.
130  return direction * M_PI / 128.0 - M_PI;
131 }
132
133 // Returns the point on the given line nearest to this, ie the point such
134 // that the vector point->this is perpendicular to the line.
135 // The line is defined as a line_point and a dir_vector for its direction.
137  const FCOORD& dir_vector) const {
138  FCOORD point_vector(*this - line_point);
139  // The dot product (%) is |dir_vector||point_vector|cos theta, so dividing by
140  // the square of the length of dir_vector gives us the fraction of dir_vector
141  // to add to line1 to get the appropriate point, so
142  // result = line1 + lambda dir_vector.
143  double lambda = point_vector % dir_vector / dir_vector.sqlength();
144  return line_point + (dir_vector * lambda);
145 }
FCOORD nearest_pt_on_line(const FCOORD &line_point, const FCOORD &dir_vector) const
Definition: points.cpp:136
inT16 ycoord
Definition: points.h:158
#define MAX(x, y)
Definition: ndminx.h:24
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
bool Serialize(FILE *fp) const
Definition: points.cpp:63
int Modulo(int a, int b)
Definition: helpers.h:157
static double angle_from_direction(uinT8 direction)
Definition: points.cpp:129
bool DeSerialize(bool swap, FILE *fp)
Definition: points.cpp:70
float angle() const
find angle
Definition: points.h:249
uinT8 to_direction() const
Definition: points.cpp:111
ELISTIZE(ICOORDELT) bool FCOORD
Definition: points.cpp:30
integer coordinate
Definition: points.h:30
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
void from_direction(uinT8 direction)
Definition: points.cpp:115
int IntCastRounded(double x)
Definition: helpers.h:172
#define MAX_INT16
Definition: host.h:119
float sqlength() const
find sq length
Definition: points.h:225
static uinT8 binary_angle_plus_pi(double angle)
Definition: points.cpp:124
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
Definition: points.h:189
void setup_render(ICOORD *major_step, ICOORD *minor_step, int *major, int *minor) const
Definition: points.cpp:86
inT16 xcoord
Definition: points.h:157
unsigned char uinT8
Definition: host.h:99