random_test.py
Go to the documentation of this file.
1 #==============================================================================
2 # Copyright (C) 2021-2024 Wageningen University - All Rights Reserved
3 # Author: Gonzalo Mier
4 # BSD-3 License
5 #==============================================================================
6 
7 import pytest
8 import fields2cover as f2c
9 import math
10 import numpy as np
11 
12 def near(a, b, error = 1e-7):
13  assert abs(a - b) < error
14 
15 
17  ring = f2c.LinearRing(f2c.VectorPoint([f2c.Point(-10,0), f2c.Point(-10,20), \
18  f2c.Point(10,20), f2c.Point(10,0), f2c.Point(-10,0)]));
19  return ring;
20 
22  polys = [create_polygons_test()];
23  near(polys[0].getDimMinX(), -10);
24  near(polys[0].getDimMinY(), 0);
25  near(polys[0].getDimMaxX(), 10);
26  near(polys[0].getDimMaxY(), 20);
27  near(polys[0].getHeight(), 20);
28  near(polys[0].getWidth(), 20);
29  near(polys[0].getMinSafeLength(), 40);
30 
31 
33  rand = f2c.Random();
34  rand_val = 0.0;
35  two_pi = 2.0 * math.pi;
36  for i in range(10000):
37  rand_val = rand.getAngleRandom();
38  assert (rand_val < two_pi);
39  assert (rand_val >= 0.0);
40 
42  rand = f2c.Random();
43  N = 10000;
44  sum_val = 0.0;
45  for i in range(N):
46  rand_val = rand.getRandomDouble();
47  assert (rand_val < 1.0);
48  assert (rand_val > 0.0);
49  sum_val += rand_val;
50  near(sum_val, N/2, N/10);
51 
53  rand = f2c.Random();
54  rand_val = 0.0;
55  for i in range(10000):
56  max_rand = rand.getRandomDouble();
57  min_rand = rand.getRandomDouble() - 1.0;
58  rand_val = rand.getRandomLinear(min_rand, max_rand);
59  assert (rand_val < max_rand);
60  assert (rand_val >= min_rand);
61 
63  rand = f2c.Random();
64  rand_val = 0.0;
65  for i in range(100):
66  max_rand = rand.getRandomDouble() + 1.5;
67  min_rand = rand.getRandomDouble() + 0.5;
68  rand_val = rand.getRandomExp(min_rand, max_rand);
69  assert (rand_val < max_rand);
70  assert (rand_val >= min_rand);
71 
73  rand = f2c.Random();
74  mean = 0.0;
75  lambda_param = 1.5;
76  epsilon = 0.1;
77  steps = 3000
78 
79  for i in range(steps):
80  mean += rand.getRandomExpDist(lambda_param);
81 
82  mean /= steps;
83  assert (mean < (1/lambda_param) + epsilon);
84  assert (mean >= (1/lambda_param) - epsilon);
85 
87  rand = f2c.Random();
88  pi = math.pi;
89  for i in range(1000):
90  a = rand.getAngleRandom();
91  b = a + 2.0 * pi * math.floor(1000.0 * rand.getRandomDouble() - 500.0);
92  c = b + pi/7;
93  d = b - pi/9;
94  near(f2c.Point.getAngleDiffAbs(a, b), 0, 1e-7);
95  near(f2c.Point.getAngleDiffAbs(b, a), f2c.Point.getAngleDiffAbs(b, a), 1e-7);
96  near(f2c.Point.getAngleDiffAbs(a, c), pi/7, 1e-7);
97  near(f2c.Point.getAngleDiffAbs(c, a), f2c.Point.getAngleDiffAbs(c, a), 1e-7);
98  near(f2c.Point.getAngleDiffAbs(a, d), pi/9, 1e-7);
99  near(f2c.Point.getAngleDiffAbs(d, a), f2c.Point.getAngleDiffAbs(d, a), 1e-7);
100 
101  ang1 = rand.getAngleRandom();
102  ang2 = rand.getAngleRandom();
103  near(f2c.Point.getAngleDiffAbs(ang1, ang2), f2c.Point.getAngleDiffAbs(ang2, ang1), 1e-7);
104 
105 
107  pi = math.pi;
108  degree = pi / 180.0;
109  p1 = f2c.Point(1.0, 0.0);
110  p2 = f2c.Point(0.0, 1.0);
111  p3 = f2c.Point(1.0, 1.0);
112  p4 = f2c.Point(-1.0, 0.0);
113  p5 = f2c.Point(math.cos(20 * degree), math.sin(20 * degree));
114  near(p1.getAngleFromPoints(p1, p1), 0, 0.01);
115  near(p1.getAngleFromPoints(p1, p4), pi, 0.01);
116  near(p1.getAngleFromPoints(p1, p3), pi / 4.0, 0.01);
117  near(p1.getAngleFromPoints(p1, p2), pi / 2.0, 0.01);
118  near(p2.getAngleFromPoints(p2, p1), 3 * pi / 2.0, 0.01);
119  near(p1.getAngleFromPoints(p1, p5), 20.0 * degree, 0.01);
120 
122  pi = math.pi;
123  p = f2c.Point(0.0, 1.0);
124  ref = f2c.Point(1.0, 1.0);
125  res = p.getPointFromAngle(pi/4.0, math.sqrt(2.0));
126  near(res.getX(), ref.getX(), 0.01);
127 
129  ring = create_polygons_test();
130  polys = f2c.Cells();
131  polygon = f2c.Cell();
132  polygon.addRing(ring);
133  polys.addGeometry(polygon);
134  line = f2c.LineString();
135  line.addPoint(-20, -10);
136  line.addPoint(-5, 5);
137 
138  lines = f2c.MultiLineString();
139  lines.addGeometry(line);
140 
141  reduced_line = polygon.getLinesInside(line);
142  assert (polys.crosses(line));
143  near(reduced_line.length(), math.sqrt(2.0)*5, 0.01);
144 
145  reduced_line = polygon.getLinesInside(lines);
146  assert (polygon.crosses(lines));
147  near(reduced_line.length(), math.sqrt(2.0)*5, 0.01);
148 
149  path = polys.getLinesInside(line);
150  near(path.length(), math.sqrt(2.0)*5, 0.01);
151  line.addPoint(20, 30);
152  reduced_line = polys.getLinesInside(line);
153  near(reduced_line.length(), math.sqrt(2.0)*20, 0.01);
154 
155  lines2 = f2c.MultiLineString();
156  lines2.addGeometry(line);
157  reduced_line = polys.getLinesInside(lines2);
158  near(reduced_line.length(), math.sqrt(2.0)*20, 0.01);
159 
161  for i in np.arange(0.01, 100.0, 0.1):
162  ang = i;
163  near((ang % (2.0 * math.pi)), f2c.Point.mod_2pi(ang), 1e-3);
164  near(i, ang);
165  near(((1e5 * 2.0 * math.pi - i) % (2.0 * math.pi)), f2c.Point.mod_2pi(-i), 1e-3);
166 
167 
169  rand = f2c.Random(4);
170  n = 100;
171  field = rand.generateRandField(1e2, 3);
172  poly = field.getField().getCell(0);
173  convex_fields = 0;
174  non_convex_fields = 0;
175  for i in range(n):
176  field = rand.genConvexField(1e2);
177  if (field.getField().isConvex()):
178  convex_fields += 1;
179  assert (convex_fields/n > 0.9999);
180 
181  for i in range(n):
182  field = rand.genNonConvexField(1e2);
183  if (not field.getField().isConvex()):
184  non_convex_fields += 1;
185  assert (non_convex_fields/n > 0.9999);
186 
187  assert not (f2c.Cell().isConvex());
188 
190  p_in = f2c.Point(1, 0);
191  p_out = f2c.Point(1, 0);
192  pi = math.pi;
193  p_out = f2c.Point(0,0).rotateFromPoint(pi/2.0, p_out);
194  near(p_out.getX(), 0, 1e-7);
195  near(p_out.getY(), 1, 1e-7);
196  p_out = f2c.Point(0,0).rotateFromPoint(pi/2.0, p_out);
197  near(p_out.getX(), -1, 1e-7);
198  near(p_out.getY(), 0, 1e-7);
199  p_out = f2c.Point(0,0).rotateFromPoint(pi/2.0, p_out);
200  near(p_out.getX(), 0, 1e-7);
201  near(p_out.getY(), -1, 1e-7);
202  p_out = f2c.Point(0,0).rotateFromPoint(pi/2.0, p_out);
203  near(p_out.getX(), 1, 1e-7);
204  near(p_out.getY(), 0, 1e-7);
205  p_out = p_in.rotateFromPoint(pi/2.0, p_out);
206  near(p_out.getX(), 1, 1e-7);
207  near(p_out.getY(), 0, 1e-7);
208  p_out = f2c.Point(0.5, 0).rotateFromPoint(pi/2.0, p_out);
209  near(p_out.getX(), 0.5, 1e-7);
210  near(p_out.getY(), 0.5, 1e-7);
211 
212  line = f2c.LineString();
213  line.addPoint(1, 2);
214  line.addPoint(3, 4);
215  p_center = f2c.Point(5, 6)
216  line = p_center.rotateFromPoint(pi, line);
217  p_out = line.startPoint();
218  near(p_out.getX(), 9, 1e-7);
219  near(p_out.getY(), 10, 1e-7);
220  p_out = line.endPoint();
221  near(p_out.getX(), 7, 1e-7);
222  near(p_out.getY(), 8, 1e-7);
223 
224 
random_test.test_fields2cover_utils_GeometryOp_getLinesInPolygons
def test_fields2cover_utils_GeometryOp_getLinesInPolygons()
Definition: random_test.py:128
random_test.near
def near(a, b, error=1e-7)
Definition: random_test.py:12
random_test.test_fields2cover_utils_GeometryOp_getDims
def test_fields2cover_utils_GeometryOp_getDims()
Definition: random_test.py:21
random_test.create_polygons_test
def create_polygons_test()
Definition: random_test.py:16
random_test.test_fields2cover_utils_random_getAngleRandom
def test_fields2cover_utils_random_getAngleRandom()
Definition: random_test.py:32
random_test.test_fields2cover_utils_GeometryOp_getPointFromAngle
def test_fields2cover_utils_GeometryOp_getPointFromAngle()
Definition: random_test.py:121
random_test.test_fields2cover_utils_GeometryOp_getAngleFromPoints
def test_fields2cover_utils_GeometryOp_getAngleFromPoints()
Definition: random_test.py:106
random_test.test_fields2cover_utils_GeometryOp_isConvex
def test_fields2cover_utils_GeometryOp_isConvex()
Definition: random_test.py:168
random_test.test_fields2cover_utils_random_getExpDistRandom
def test_fields2cover_utils_random_getExpDistRandom()
Definition: random_test.py:72
random_test.test_fields2cover_utils_random_getLinearRandom
def test_fields2cover_utils_random_getLinearRandom()
Definition: random_test.py:52
random_test.test_fields2cover_utils_random_getExpRandom
def test_fields2cover_utils_random_getExpRandom()
Definition: random_test.py:62
f2c::Random
Definition: random.h:23
random_test.test_fields2cover_utils_random_getRandomDouble
def test_fields2cover_utils_random_getRandomDouble()
Definition: random_test.py:41
random_test.test_fields2cover_utils_GeometryOp_rotateFromPoint
def test_fields2cover_utils_GeometryOp_rotateFromPoint()
Definition: random_test.py:189
random_test.test_fields2cover_utils_GeometryOp_mod2pi
def test_fields2cover_utils_GeometryOp_mod2pi()
Definition: random_test.py:160
random_test.test_fields2cover_utils_random_getAngleDiffAbs
def test_fields2cover_utils_random_getAngleDiffAbs()
Definition: random_test.py:86


fields2cover
Author(s):
autogenerated on Fri Apr 25 2025 02:18:31