Xmipp  v3.23.11-Nereus
point.cpp
Go to the documentation of this file.
1 #include <math.h>
2 #include "point.h"
3 #include <stdio.h>
4 
5 
6 /*****************************************************************************
7  * Defines section
8  ****************************************************************************/
9 #define COLLINEAR_THRESHOLD 0.0
10 
11 /*****************************************************************************
12 * Private functions declaration
13 *****************************************************************************/
14 TYPE Euclidean( struct Point_T *p, struct Point_T *q);
15 
16 
17  /*****************************************************************************
18  * Public functions declaration
19  *****************************************************************************/
20 /*****************************************************************************
21 * Name: distance
22 * Input: point p point q func
23 * Description: distance from p to q.
24 * Output: computes the distance from p to q using a distance
25 * function passed as parameter.
26 * Complexity: O(1)
27 *****************************************************************************/
28 TYPE distance( struct Point_T *p, struct Point_T *q)
29 {
30  TYPE dist=0.0;
31 
32  // Compute distance.
33  dist = Euclidean( p, q);
34 
35  return(dist);
36 }
37 
38 
39 int equal_Point(struct Point_T *p1, struct Point_T *p2)
40 {
41  int equal=FALSE; // Return value.
42  POINT_T diff=0.0; // Coordinates difference value.
43 
44  // Check if points are too close.
45  diff = p1->x - p2->x;
46  if ((diff < DELTA_DIFF) && (diff > -DELTA_DIFF))
47  {
48  diff = p1->y - p2->y;
49  if ((diff < DELTA_DIFF) && (diff > -DELTA_DIFF))
50  {
51  equal = TRUE;
52  }
53  }
54  else
55  {
56  equal = FALSE;
57  }
58 
59  return(equal);
60 }
61 
62 int higher_Point(struct Point_T *p1, struct Point_T *p2, int (*f)(struct Point_T *, struct Point_T *))
63 {
64  return((*f)( p1, p2));
65 }
66 
67 void print_Point(struct Point_T *point)
68 {
69  printf("Point (%f, %f)\n", point->x, point->y);
70 }
71 
72 //#define DEBUG_CHECK_TURN
73 enum Turn_T check_Turn(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3)
74 {
75  TYPE area=0.0; // Signed area.
76  enum Turn_T turn; // Return value.
77 
78  // Compute signed area of the triangle formed by p1, p2 and p3.
79  area = signed_Area( p1, p2, p3);
80 
81 #ifdef DEBUG_CHECK_TURN
82  printf("Area %f. P (%lf,%lf). Q (%lf,%lf). R (%lf,%lf)\n", area, p1->x, p1->y, p2->x, p2->y, p3->x, p3->y);
83 #endif
84 
85  // Higher than zero -> turn left.
86  if (area > COLLINEAR_THRESHOLD)
87  {
88 #ifdef DEBUG_CHECK_TURN
89  printf("LEFT\n");
90 #endif
91  turn = LEFT_TURN;
92  }
93  // Lower than zero -> turn right.
94  else if (area < -COLLINEAR_THRESHOLD)
95  {
96 #ifdef DEBUG_CHECK_TURN
97  printf("RIGHT\n");
98 #endif
99  turn = RIGHT_TURN;
100  }
101  // If area is close to zero then points are collinear.
102  else
103  {
104 #ifdef DEBUG_CHECK_TURN
105  printf("COLLINEAR\n");
106 #endif
107  turn = COLINEAR;
108  }
109 
110  return(turn);
111 }
112 
113 
114 int in_Circle(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3, struct Point_T *q)
115 {
116  int ret=0; // Return value.
117  POINT_T value=0.0; // Determinant value.
118  POINT_T temp[9]; // Intermediate values.
119 
120  // Compute Ax - Dx, Ay - Dy and (Ax-Dx)² + (Ay-Dy)²
121  temp[0] = (p1->x - q->x);
122  temp[1] = (p1->y - q->y);
123  temp[2] = (POINT_T) (pow((p1->x - q->x), 2) + pow((p1->y - q->y), 2));
124 
125  // Compute Bx - Dx, By - Dy and (Bx-Dx)² + (By-Dy)²
126  temp[3] = (p2->x - q->x);
127  temp[4] = (p2->y - q->y);
128  temp[5] = (POINT_T) (pow((p2->x - q->x), 2) + pow((p2->y - q->y), 2));
129 
130  // Compute Cx - Dx, Cy - Dy and (Cx-Dx)² + (Cy-Dy)²
131  temp[6] = (p3->x - q->x);
132  temp[7] = (p3->y - q->y);
133  temp[8] = (POINT_T) (pow((p3->x - q->x), 2) + pow((p3->y - q->y), 2));
134 
135  // Compute determinant.
136  value = (temp[0]*temp[4]*temp[8]) +
137  (temp[1]*temp[5]*temp[6]) +
138  (temp[2]*temp[3]*temp[7]) -
139  (temp[2]*temp[4]*temp[6]) -
140  (temp[5]*temp[7]*temp[0]) -
141  (temp[8]*temp[1]*temp[3]);
142 
143  // If positive then point q belongs to p1-p2-p3 circumference.
144  if (value > 0.0)
145  {
146  ret = 1;
147  }
148 
149  return(ret);
150 }
151 
152 bool interior_Triangle(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3, struct Point_T *q)
153 {
154  bool is_In_Triangle=false; // Return value.
155 
156  // Check if new point is interior to triangle formed by p1, p2 and p3.
157  if ((signed_Area( p1, p2, q) > 0) &&
158  (signed_Area( p2, p3, q) > 0) &&
159  (signed_Area( p3, p1, q) > 0))
160  {
161  is_In_Triangle = true;
162  }
163  else
164  {
165  is_In_Triangle = false;
166  }
167 
168  return(is_In_Triangle);
169 }
170 
171 
173 {
174  int has_Extreme=FALSE; // Return value.
175 
176  if ((p->x >= MAX_X_COORD) || (p->y >= MAX_Y_COORD) ||
177  (p->x <= -MAX_X_COORD) || (p->y <= -MAX_Y_COORD))
178  {
179  has_Extreme = TRUE;
180  }
181 
182  return(has_Extreme);
183 }
184 
185 int lower_X(struct Point_T *p1, struct Point_T *p2)
186 {
187  // Return true if y coordinate is higher.
188  return(p1->x < p2->x);
189 }
190 
191 int higher_X(struct Point_T *p1, struct Point_T *p2)
192 {
193  // Return true if y coordinate is higher.
194  return(p1->x > p2->x);
195 }
196 
197 int higher_Y(struct Point_T *p1, struct Point_T *p2)
198 {
199  // Return true if y coordinate is higher.
200  return(p1->y > p2->y);
201 }
202 
203 /********************************************************************/
204 /* Name: lexicographic_Higher
205  * Input:
206  * Description:
207  * Output:
208  * Complexity: O(1)
209  * */
210 /********************************************************************/
211 int lexicographic_Higher(struct Point_T *p1, struct Point_T *p2)
212 {
213  int higher=FALSE; // Return value.
214 
215  // Check if Y coordinate is higher.
216  if (p1->y > p2->y)
217  {
218  higher = TRUE;
219  }
220  else
221  {
222  // If Y coordinate is equal then check X coordinate.
223  if ((p1->y == p2->y) &&
224  (p1->x > p2->x))
225  {
226  higher = TRUE;
227  }
228  }
229 
230  return(higher);
231 }
232 
233 /********************************************************************
234  * Name: Euclidean
235  * Input: *p1 reference to Point_T
236  * Output: *p2 reference to Point_T
237  * Description: copies p1 into p2.
238  * Output: p2.x = p1.x and p2.y = p1.y
239  * Complexity: O(1)
240 ********************************************************************/
241 void copy_Point(struct Point_T *p1, struct Point_T *p2)
242 {
243  // Copy p1 x and y values into p2.
244  p2->x = p1->x;
245  p2->y = p1->y;
246 }
247 
248 /********************************************************************/
249 /* Name: Euclidean
250  * Input: point p point q
251  * Description: computes the euclidean distance from p to q.
252  * Output: euclidean distance from point p to point q
253  * Complexity: O(1)
254  * */
255 /********************************************************************/
256 TYPE Euclidean( struct Point_T *p, struct Point_T *q)
257 {
258  TYPE dist=0.0;
259 
260  // Compute euclidean distance.
261  dist = sqrt(pow( p->x - q->x, 2) + pow( p->y - q->y, 2));
262 
263  return(dist);
264 }
265 
266 
267 TYPE signed_Area(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3)
268 {
269  double area=0.0; // Return value.
270 
271  area = (- (double) p2->x*p1->y + (double) p3->x*p1->y + (double) p1->x*p2->y -
272  (double) p3->x*p2->y - (double) p1->x*p3->y + (double) p2->x*p3->y);
273 
274  return(area);
275 }
276 
Definition: point.h:12
#define COLLINEAR_THRESHOLD
Definition: point.cpp:9
void sqrt(Image< double > &op)
Turn_T
Definition: point.h:10
#define POINT_T
Definition: defines.h:50
POINT_T x
Definition: point.h:14
int lexicographic_Higher(struct Point_T *p1, struct Point_T *p2)
Definition: point.cpp:211
Definition: point.h:10
#define TYPE
Definition: defines.h:47
void print_Point(struct Point_T *point)
Definition: point.cpp:67
int equal_Point(struct Point_T *p1, struct Point_T *p2)
Definition: point.cpp:39
int higher_Point(struct Point_T *p1, struct Point_T *p2, int(*f)(struct Point_T *, struct Point_T *))
Definition: point.cpp:62
int in_Circle(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3, struct Point_T *q)
Definition: point.cpp:114
double * f
TYPE Euclidean(struct Point_T *p, struct Point_T *q)
Definition: point.cpp:256
#define MAX_Y_COORD
Definition: defines.h:39
int higher_Y(struct Point_T *p1, struct Point_T *p2)
Definition: point.cpp:197
bool interior_Triangle(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3, struct Point_T *q)
Definition: point.cpp:152
POINT_T y
Definition: point.h:15
TYPE distance(struct Point_T *p, struct Point_T *q)
Definition: point.cpp:28
#define FALSE
Definition: defines.h:24
TYPE signed_Area(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3)
Definition: point.cpp:267
#define TRUE
Definition: defines.h:25
enum Turn_T check_Turn(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3)
Definition: point.cpp:73
int lower_X(struct Point_T *p1, struct Point_T *p2)
Definition: point.cpp:185
#define DELTA_DIFF
Definition: defines.h:31
#define MAX_X_COORD
Definition: defines.h:38
int higher_X(struct Point_T *p1, struct Point_T *p2)
Definition: point.cpp:191
void copy_Point(struct Point_T *p1, struct Point_T *p2)
Definition: point.cpp:241
int has_Extreme_Coordinates(struct Point_T *p)
Definition: point.cpp:172