geometry/polygon: adds polygon triangulation + polygon abstraction
[nit.git] / benchmarks / polygons / nit / bench_polygon.nit
1 #This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2015 Romain Chanoir <romain.chanoir@viacesi.fr>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # a bench for the polygons
18 module bench_polygon
19
20 intrude import geometry::polygon
21 import opts
22
23 # Bench adding random vertices
24 fun bench_add_vertices(n: Int) do
25 var randompoints = generate_points(n + 1)
26 var points = randompoints.clone
27 points.remove_at(points.length -1)
28 var poly = new ConvexPolygon(randompoints)
29 poly.sort_ccw
30 poly.add_vertex(randompoints.last)
31 end
32
33 # Bench the convex hull algorithm
34 fun bench_convex_hull(n: Int) do
35 srand_from(50)
36 var randompoints = new Array[Point[Float]]
37 for i in [0..n] do
38 var point = new Point[Float](300.0.rand, 300.0.rand)
39 randompoints.add(point)
40 end
41 convex_hull(randompoints)
42 end
43
44
45 # Bench the convexity verificatioon
46 fun bench_convexity(n : Int) do
47 var randompoints = generate_points(n)
48 var poly = new ConvexPolygon(randompoints)
49 poly.sort_ccw
50 poly.is_convex
51 end
52
53 # Bench the point in polygon algorithm
54 fun bench_contain(n : Int) do
55 srand_from(50)
56 var randompoints = generate_points(n)
57 var poly = new ConvexPolygon(randompoints)
58 poly.sort_ccw
59 var point = new Point[Float](0.0, 0.0)
60 poly.contain(point)
61 end
62
63 # Bench the sorting of the vertices
64 fun bench_sorting(n : Int) do
65 var randompoints = generate_points(n)
66 randompoints.shuffle
67 var poly = new ConvexPolygon(randompoints)
68 poly.sort_ccw
69
70 end
71
72 # Bench the intersection test between two polygons
73 fun bench_intersection(n : Int) do
74 var r1 = generate_points(n)
75 var r2 = generate_points(n)
76 var poly1 = new ConvexPolygon(r1)
77 var poly2 = new ConvexPolygon(r2)
78 poly1.sort_ccw
79 poly2.sort_ccw
80 poly1.intersects(poly2)
81 end
82
83 # Get `n` points from a circle
84 fun get_points_on_circle(radius: Float, n: Int): Array[Point[Float]] do
85 srand_from(50)
86 var points = new Array[Point[Float]]
87 for i in n.times do
88 var angle = 1000.0.rand * pi * 2.0
89 var point = new Point[Float](angle.cos * radius, angle.sin * radius)
90 points.add(point)
91 end
92 return points
93 end
94
95 # Helper for `get_points_on_circle`
96 fun generate_points(n: Int): Array[Point[Float]] do
97 return get_points_on_circle(100.0, n)
98 end
99
100 var opts = new OptionContext
101 var mode = new OptionEnum(["add_vertex","sort_vertices","intersection","convex_hull","is_convex","contain"], "Mode", -1, "-m")
102 var nb_points = new OptionInt("number of points generated for the bench", -1, "--nbpts")
103 opts.add_option(mode, nb_points)
104
105 opts.parse(args)
106
107 if nb_points.value == -1 then
108 opts.usage
109 exit(-1)
110 end
111
112 var modval = mode.value
113 var n = nb_points.value
114 if modval == 0 then
115 print "bench_add_vertices"
116 bench_add_vertices(n)
117 else if modval == 1 then
118 print "bench_sorting"
119 bench_sorting(n)
120 else if modval == 2 then
121 print "bench_intersection"
122 bench_intersection(n)
123 else if modval == 3 then
124 print "bench_convex_hull"
125 bench_convex_hull(n)
126 else if modval == 4 then
127 print "bench_convexity"
128 bench_convexity(n)
129 else if modval == 5 then
130 print "bench_contain"
131 bench_contain(n)
132 else
133 opts.usage
134 exit(-1)
135 end