Merge: More keep going
[nit.git] / benchmarks / polygons / java / code / BenchPolygon.java
1 import java.io.BufferedWriter;
2 import java.io.File;
3 import java.io.FileWriter;
4 import java.io.IOException;
5 import java.text.SimpleDateFormat;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Date;
9 import java.util.Random;
10
11 /**
12 *
13 * @author Johan Kayser, Romain Chanoir
14 */
15 /**
16 * Runs the benchmarks for the most important operations and generates a file
17 * with time execution results
18 */
19 public class BenchPolygon {
20
21 public static void main(String[] args) throws IOException {
22 int n = 0;
23 if(args[1] != null){
24 n = Integer.parseInt(args[1]);
25 }else {
26 n = 100000;
27 }
28 switch (args[0]){
29 case "add_vertex":
30 testAddVertex(n);
31 break;
32 case "sort_vertices":
33 testSortVertices(n);
34 break;
35 case "intersection":
36 testIntersects(n);
37 break;
38 case "convex_hull":
39 testConvexHull(n);
40 break;
41 case "convexity":
42 testIsConvex(n);
43 break;
44 case "contain":
45 testContain(n);
46 break;
47 default:
48 break;
49 }
50 }
51
52 /**
53 * addVertex bench: adds a vertex to a polygon with the given number of
54 * vertices
55 */
56 public static void testAddVertex(int nb) throws IOException {
57 ArrayList<PointDouble> points = new ArrayList<>();
58 ArrayList<PointDouble> randomPoints = new ArrayList<>();
59 randomPoints = generatePoints(nb + 1);
60
61 for (int i = 0; i < nb; ++i) {
62 points.add(randomPoints.remove(0));
63 }
64 ConvexPolygon test = new ConvexPolygon(points);
65 test.sortVertices(new AntiClockSort(test.getVertices()));
66
67 test.addVertex(randomPoints.remove(0));
68 }
69
70 /**
71 * sortVertices bench: sorts the given number of vertices in the ArrayList
72 * of a polygon
73 */
74 public static void testSortVertices(int nb) throws IOException {
75 ArrayList<PointDouble> randomPoints = new ArrayList<>();
76 randomPoints = generatePoints(nb);
77 Collections.shuffle(randomPoints);
78
79 ConvexPolygon test = new ConvexPolygon(randomPoints);
80 test.sortVertices(new AntiClockSort(test.getVertices()));
81
82 }
83
84 /**
85 * intersects bench: tests the intersection between two polygons with the
86 * given number of vertices
87 */
88 public static void testIntersects(int nb) throws IOException {
89 ArrayList<PointDouble> points1 = new ArrayList<>();
90 ArrayList<PointDouble> points2 = new ArrayList<>();
91 points1 = generatePoints(nb);
92 points2 = generatePoints(nb);
93 ConvexPolygon test1 = new ConvexPolygon(points1);
94 ConvexPolygon test2 = new ConvexPolygon(points2);
95 test1.sortVertices(new AntiClockSort(test1.getVertices()));
96 test2.sortVertices(new AntiClockSort(test2.getVertices()));
97
98 Boolean rez = test1.intersects(test2);
99 }
100
101 /**
102 * convexHull bench: gets the convex hull of the given number of points
103 */
104 public static void testConvexHull(int nb) throws IOException {
105 ArrayList<PointDouble> randomPoints = new ArrayList<>();
106 randomPoints = generatePoints(nb);
107 Collections.shuffle(randomPoints);
108 ConvexPolygon test = new ConvexPolygon(randomPoints);
109
110 ConvexPolygon rez = test.convexHull(randomPoints);
111 }
112
113 /**
114 * isConvex bench: checks if the polygon with the given number of vertices
115 * is convex (we test the worst case -> polygon vertices are ordered)
116 */
117 public static void testIsConvex(int nb) throws IOException {
118 ArrayList<PointDouble> randomPoints = new ArrayList<>();
119 randomPoints = generatePoints(nb);
120 ConvexPolygon test = new ConvexPolygon(randomPoints);
121 test.sortVertices(new AntiClockSort(test.getVertices()));
122
123 Boolean rez = test.isConvex();
124 }
125
126 /**
127 * contain bench: checks if the polygon with the given number of vertices
128 * contains a randomly generated point
129 */
130 public static void testContain(int nb) throws IOException {
131 ArrayList<PointDouble> randomPoints = new ArrayList<>();
132 randomPoints = generatePoints(nb);
133 ConvexPolygon test = new ConvexPolygon(randomPoints);
134 test.sortVertices(new AntiClockSort(test.getVertices()));
135
136 Boolean rez = test.contain(new PointDouble(0.0, 0.0));
137 }
138
139 /**
140 * generates some points making it easier to use convex polygons
141 */
142 public static ArrayList<PointDouble> generatePoints(int nb) {
143 ArrayList<PointDouble> pts = new ArrayList<>();
144 pts = PointDouble.getNPointsOnCircle(100.0, nb);
145 return pts;
146 }
147 }