Xmipp  v3.23.11-Nereus
sorting.cpp
Go to the documentation of this file.
1 #include "sorting.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5 
6 //#define DEBUG_SORTING
7 
8 /****************************************************************************
9  PRIVATE FUNCTION HEADERS
10 *****************************************************************************/
11 int move_Pivot( struct Dcel_Vertex_T *origin, struct Dcel_Vertex_T *list, int init_Index, int final_Index);
12 void quicksort( struct Dcel_Vertex_T *origin, struct Dcel_Vertex_T *list, int init_Index, int final_Index);
13 
14 
15 /****************************************************************************
16  PUBLIC FUNCTION BODIES
17 *****************************************************************************/
18 void sort(struct DCEL_T *dcel)
19 {
20  // Call quicksort with DCEL structure.
21  quicksort( &dcel->vertex[0], &dcel->vertex[1], 0, dcel->nVertex-2);
22 }
23 
24 /***************************************************************************
25 * Name: clutter
26 * IN: N/A
27 * OUT: N/A
28 * IN/OUT: dcel dcel data
29 * RETURN: N/A
30 * Description: clutters the set of point of the DCEL.
31 ***************************************************************************/
32 void clutter(struct DCEL_T *dcel)
33 {
34  int i=0; // Loop counter.
35  int index1=0, index2=0; // Array indexes.
36 
37  // Set seed.
38  srand(time(NULL));
39 
40  // Loop set of points.
41  for (i=0; i<dcel->nVertex ;i++)
42  {
43  // Generate random indexes to swap.
44  index1 = rand() % dcel->nVertex;
45  index2 = rand() % dcel->nVertex;
46 
47  // Swap elements.
48  swap_Vertex( dcel, index1, index2);
49  }
50 }
51 
52 void set_Highest_First(struct DCEL_T *dcel, int (*f)(struct Point_T *, struct Point_T *))
53 {
54  int i=0; // Loop counter.
55  int length=0; // Number of vertexes in DCEL.
56  int highest_Index=0; // Index of highest point.
57 
58  // Get number of vertexes.
59  length = get_Number_Vertex( dcel);
60  highest_Index=0;
61 
62  // Find index of highest point.
63  for (i=1; i<length ;i++)
64  {
65  if ((*f) ( &dcel->vertex[i].vertex, &dcel->vertex[highest_Index].vertex))
66  {
67  highest_Index = i;
68  }
69  }
70 
71  // Swap highest vertex and vertex at position 0.
72  swap_Vertex( dcel, 0, highest_Index);
73 
74 #ifdef DEBUG_SORTING
75  printf("Highest point at position %d.", highest_Index);
76  print_Point( &dcel->vertex[0].vertex);
77  fflush(NULL);
78 #endif
79 }
80 
81 /****************************************************************************
82  PRIVATE FUNCTION BODIES
83 *****************************************************************************/
84 int move_Pivot( struct Dcel_Vertex_T *origin, struct Dcel_Vertex_T *list, int init_Index, int final_Index)
85 {
86  int i=0, first_Index=0; // Loop variables.
87  int pivot_Index=0; // Pivot index.
88  struct Dcel_Vertex_T pivot_Point; // Pivot point.
89  struct Dcel_Vertex_T temp; // Register to exchange elements in list.
90 
91  // Get position and value of pivot point.
92  pivot_Index = init_Index;
93  pivot_Point = list[pivot_Index];
94 
95  // Set first index.
96  first_Index = init_Index + 1;
97 
98  // Loop.
99  for (i=first_Index; i<=final_Index; i++)
100  {
101  // If turn is right then angle is lower in list[i] than in pivot element.
102  if ((check_Turn( &origin->vertex, &pivot_Point.vertex, &list[i].vertex) == RIGHT_TURN) ||
103  ((check_Turn( &origin->vertex, &pivot_Point.vertex, &list[i].vertex) == COLINEAR) &&
104  (pivot_Point.vertex.x > list[i].vertex.x)))
105  {
106  // Increase pivot index.
107  pivot_Index++;
108 
109  // Move current element to lower position in set.
110  temp = list[i];
111  list[i] = list[pivot_Index];
112  list[pivot_Index] = temp;
113  }
114  }
115 
116  // Move pivot point.
117  temp = list[init_Index];
118  list[init_Index] = list[pivot_Index];
119  list[pivot_Index] = temp;
120 
121  return pivot_Index;
122 }
123 
124 void quicksort( struct Dcel_Vertex_T *origin, struct Dcel_Vertex_T *list, int init_Index, int final_Index)
125 {
126  int pivot_Index=0; // Index of the element used as pivot.
127 
128  // Check if list is one element long.
129  if(init_Index < final_Index)
130  {
131  // Move first element to its position.
132  pivot_Index = move_Pivot( origin, list, init_Index, final_Index);
133 
134  // Order from initial element to previous element to pivot.
135  quicksort( origin, list, init_Index, pivot_Index-1);
136 
137  // Order from next element to pivot until end of set.
138  quicksort( origin, list, pivot_Index+1, final_Index);
139  }
140 }
141 
142 
143 
Definition: point.h:12
Definition: dcel.h:50
int move_Pivot(struct Dcel_Vertex_T *origin, struct Dcel_Vertex_T *list, int init_Index, int final_Index)
Definition: sorting.cpp:84
POINT_T x
Definition: point.h:14
#define i
int get_Number_Vertex(struct DCEL_T *dcel)
Definition: dcel.cpp:638
Definition: point.h:10
void print_Point(struct Point_T *point)
Definition: point.cpp:67
double * f
void quicksort(struct Dcel_Vertex_T *origin, struct Dcel_Vertex_T *list, int init_Index, int final_Index)
Definition: sorting.cpp:124
__host__ __device__ float length(float2 v)
void sort(struct DCEL_T *dcel)
Definition: sorting.cpp:18
struct Dcel_Vertex_T * vertex
Definition: dcel.h:57
struct Point_T vertex
Definition: dcel.h:32
int nVertex
Definition: dcel.h:55
int swap_Vertex(struct DCEL_T *dcel, int index1, int index2)
Definition: dcel.cpp:838
enum Turn_T check_Turn(struct Point_T *p1, struct Point_T *p2, struct Point_T *p3)
Definition: point.cpp:73
void clutter(struct DCEL_T *dcel)
Definition: sorting.cpp:32
void set_Highest_First(struct DCEL_T *dcel, int(*f)(struct Point_T *, struct Point_T *))
Definition: sorting.cpp:52