1 #This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2015 Romain Chanoir <romain.chanoir@viacesi.fr>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # a bench for the polygons
20 intrude import geometry
::polygon
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
)
30 poly
.add_vertex
(randompoints
.last
)
33 # Bench the convex hull algorithm
34 fun bench_convex_hull
(n
: Int) do
36 var randompoints
= new Array[Point[Float]]
38 var point
= new Point[Float](300.0.rand
, 300.0.rand
)
39 randompoints
.add
(point
)
41 convex_hull
(randompoints
)
45 # Bench the convexity verificatioon
46 fun bench_convexity
(n
: Int) do
47 var randompoints
= generate_points
(n
)
48 var poly
= new ConvexPolygon(randompoints
)
53 # Bench the point in polygon algorithm
54 fun bench_contain
(n
: Int) do
56 var randompoints
= generate_points
(n
)
57 var poly
= new ConvexPolygon(randompoints
)
59 var point
= new Point[Float](0.0, 0.0)
63 # Bench the sorting of the vertices
64 fun bench_sorting
(n
: Int) do
65 var randompoints
= generate_points
(n
)
67 var poly
= new ConvexPolygon(randompoints
)
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
)
80 poly1
.intersects
(poly2
)
83 # Get `n` points from a circle
84 fun get_points_on_circle
(radius
: Float, n
: Int): Array[Point[Float]] do
86 var points
= new Array[Point[Float]]
88 var angle
= 1000.0.rand
* pi
* 2.0
89 var point
= new Point[Float](angle
.cos
* radius
, angle
.sin
* radius
)
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
)
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
)
107 if nb_points
.value
== -1 then
112 var modval
= mode
.value
113 var n
= nb_points
.value
115 print
"bench_add_vertices"
116 bench_add_vertices
(n
)
117 else if modval
== 1 then
118 print
"bench_sorting"
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"
126 else if modval
== 4 then
127 print
"bench_convexity"
129 else if modval
== 5 then
130 print
"bench_contain"