Swaths_test.cpp
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 #include <gtest/gtest.h>
8 #include <random>
9 #include "fields2cover/types.h"
10 
11 std::vector<F2CLinearRing> create_polygon_border(void) {
12  std::vector<F2CLinearRing> v_line;
13  F2CLinearRing line;
14  line.addPoint(-10, 0);
15  line.addPoint(-10, 20);
16  line.addPoint(10, 20);
17  line.addPoint(10, 0);
18  line.addPoint(-10, 0);
19  v_line.emplace_back(line);
20  return v_line;
21 }
22 
23 TEST(fields2cover_types_swaths, append) {
24  auto rings = create_polygon_border();
25  F2CCells polys;
26  F2CCell polygon;
27  polygon.addRing(rings[0]);
28  polys.addGeometry(polygon);
29  F2CLineString line;
30  line.addPoint(-20, -10);
31  line.addPoint(-5, 5);
32 
34  EXPECT_EQ(swaths.size(), 0);
35  swaths.append(line, polys, 1.0);
36  EXPECT_EQ(swaths.size(), 1);
37  swaths.append(line, polys, 2.0);
38  EXPECT_EQ(swaths.size(), 2);
39  line.addPoint(-19.9, -10);
40  line.addPoint(-5, 5.1);
41  swaths.append(line, polys, 3.0);
42  EXPECT_EQ(swaths.size(), 4);
44  lines.addGeometry(line);
45  lines.addGeometry(F2CLineString({F2CPoint(-10,0),F2CPoint(10,20)}));
46  swaths.append(lines, polygon, 4);
47  EXPECT_EQ(swaths.size(), 8);
48  swaths.append(lines, polys, 5);
49  EXPECT_EQ(swaths.size(), 12);
50  lines.addGeometry(F2CLineString({F2CPoint(1e5, 1e5),F2CPoint(1e5+1, 1e5+1)}));
51  swaths.append(lines, polys, 6);
52  EXPECT_EQ(swaths.size(), 16);
53 }
54 
55 
56 TEST(fields2cover_types_swaths, sort) {
57  const int n = 5;
59  for (int i = n - 1; i >= 1; --i) {
60  swaths.emplace_back(F2CLineString({F2CPoint(i, i), F2CPoint(i, i + 1)}), i, i);
61  }
62  auto rng = std::default_random_engine {};
63  std::shuffle(swaths.begin(), swaths.end(), rng);
64 
65  swaths.sort();
66  for (int i = 1; i < n; ++i) {
67  EXPECT_EQ(swaths[i-1].getWidth(), i);
68  }
69 }
70 
71 
72 TEST(fields2cover_types_swaths, reverseDirOddSwaths) {
73  const int n = 10;
75  for (int i = 1; i <= n; ++i) {
76  swaths.emplace_back(F2CLineString({F2CPoint(i, i), F2CPoint(i, i + 1)}), i);
77  }
78  swaths.reverseDirOddSwaths();
79  for (int i = 1; i < n; ++i) {
80  EXPECT_FALSE(swaths[i].hasSameDir(swaths[i-1]));
81  }
82 }
83 
84 
85 TEST(fields2cover_types_swaths, push_back) {
86  const int n = 10;
88  std::vector<F2CSwath> v_swath;
89  for (int i = 1; i <= n; ++i) {
90  F2CSwath swath {F2CLineString({F2CPoint(i, i), F2CPoint(i, i + 1)}), 1.0*i, i};
91  swaths1.push_back(swath);
92  v_swath.push_back(swath);
93  }
94 
95  F2CSwaths swaths2{v_swath};
96 
97  for (int i = 1; i < n; ++i) {
98  EXPECT_EQ(swaths1.at(i).getId(), i+1);
99  EXPECT_EQ(swaths2.at(i).getId(), i+1);
100  }
101 }
102 
103 
104 TEST(fields2cover_types_swaths, move_to) {
105  const int n = 10;
107  for (int i = 1; i <= n; ++i) {
108  F2CSwath swath {F2CLineString({F2CPoint(i, i), F2CPoint(i, i + 1)}), 1.0*i, i};
109  swaths.push_back(swath);
110  }
111 
112  F2CSwaths init_swaths = swaths.clone();
113  F2CPoint pt(1.0, 2.0);
114  swaths.moveTo(pt);
115 
116  for (int i = 1; i < n; ++i) {
117  EXPECT_EQ(swaths.at(i).startPoint(), init_swaths.at(i).startPoint() + pt);
118  }
119 }
120 
121 
122 
5_route_planning.swaths
swaths
Definition: 5_route_planning.py:58
f2c::types::Cell::addRing
void addRing(const LinearRing &ring)
Definition: Cell.cpp:123
create_polygon_border
std::vector< F2CLinearRing > create_polygon_border(void)
Definition: Swaths_test.cpp:11
types.h
f2c::types::Cells::addGeometry
void addGeometry(const Cell &c)
Definition: Cells.cpp:126
f2c::types::Swaths::at
Swath & at(size_t i)
Definition: Swaths.cpp:70
f2c::types::Swath
Definition: Swath.h:23
f2c::types::LinearRing::addPoint
void addPoint(double x, double y, double z=0)
Definition: LinearRing.cpp:105
2_objective_functions.swaths1
swaths1
Definition: 2_objective_functions.py:37
f2c::types::MultiLineString
Definition: MultiLineString.h:18
f2c::types::LinearRing
Definition: LinearRing.h:18
f2c::types::Cell
Definition: Cell.h:32
f2c::types::LineString
Definition: LineString.h:19
f2c::types::Swath::startPoint
Point startPoint() const
Definition: Swath.cpp:92
f2c::types::Cells
Definition: Cells.h:21
F2CLineString
f2c::types::LineString F2CLineString
Definition: types.h:40
f2c::types::LineString::addPoint
void addPoint(double x, double y, double z=0)
Definition: LineString.cpp:107
f2c::types::Point
Definition: Point.h:21
TEST
TEST(fields2cover_types_swaths, append)
Definition: Swaths_test.cpp:23
1_basic_types.lines
lines
Definition: 1_basic_types.py:73
f2c::types::Swaths
Definition: Swaths.h:20
F2CPoint
f2c::types::Point F2CPoint
Definition: types.h:38


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