swath_generator_checker.hpp
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 #pragma once
8 #ifndef SWATH_GENERATOR_CHECKER_HPP_
9 #define SWATH_GENERATOR_CHECKER_HPP_
10 
11 #include <gtest/gtest.h>
12 #include <fstream>
13 #include "fields2cover.h"
14 
15 inline testing::AssertionResult isSwathGenerationCorrect(
17  const F2CCell& poly, double swath_width = 1.0, int checks = 100) {
18  auto swaths = sw_gen.generateBestSwaths(obj, swath_width, poly);
19  if (swaths.size() < 1) {
20  return testing::AssertionFailure() <<
21  "Error 2001: No swaths were generated.";
22  } else if (swaths.size() > poly.area() / (swath_width * swath_width)) {
23  return testing::AssertionFailure() <<
24  "Error 2002: " << swaths.size() << " swaths were generated." <<
25  "Expected less than " << poly.area() / (swath_width * swath_width) <<
26  "swaths.";
27  }
29  for(int i = 0; i < checks; ++i) {
30  double rand_ang = rand.getAngleRandom();
31  auto swaths2 = sw_gen.generateSwaths(rand_ang, swath_width, poly);
32  if (obj.computeCostWithMinimizingSign(swaths) >
33  obj.computeCostWithMinimizingSign(swaths2)) {
34  return testing::AssertionFailure() <<
35  "Error 2003: Cost (signed) of the optimal angle is " <<
36  obj.computeCostWithMinimizingSign(swaths) <<
37  ", but the cost (signed) of a non-optimal angle (" <<
38  rand_ang << ") is " <<
39  obj.computeCostWithMinimizingSign(swaths2) << ".";
40  } else if (swaths.size() > poly.area() / (swath_width * swath_width)) {
41  return testing::AssertionFailure() << "Error 2004: " << swaths2.size() <<
42  " swaths were generated with angle " << rand_ang <<
43  "Expected less than " << poly.area() / (swath_width * swath_width) <<
44  "swaths.";
45  }
46  }
47  return testing::AssertionSuccess();
48 }
49 
50 inline testing::AssertionResult isSwathGenerationCorrect(
52  const F2CCells& poly, double swath_width = 1.0, int checks = 100) {
53  auto swaths_by_cells = sw_gen.generateBestSwaths(obj, swath_width, poly);
54  EXPECT_EQ(swaths_by_cells.size(), poly.size());
55 
56  for (auto&& swaths : swaths_by_cells) {
57  if (swaths.size() < 1) {
58  return testing::AssertionFailure() <<
59  "Error 2001: No swaths were generated.";
60  } else if (swaths.size() > poly.area() / (swath_width * swath_width)) {
61  return testing::AssertionFailure() <<
62  "Error 2002: " << swaths.size() << " swaths were generated." <<
63  "Expected less than " << poly.area() / (swath_width * swath_width) <<
64  "swaths.";
65  }
66  }
68  for(int i = 0; i < checks; ++i) {
69  double rand_ang = rand.getAngleRandom();
70  auto swaths_by_cells2 = sw_gen.generateSwaths(rand_ang, swath_width, poly);
71  EXPECT_EQ(swaths_by_cells2.size(), poly.size());
72  for (int j = 0; j < swaths_by_cells2.size(); ++j) {
73  if (obj.computeCostWithMinimizingSign(swaths_by_cells[j]) >
74  obj.computeCostWithMinimizingSign(swaths_by_cells2[j])) {
75  return testing::AssertionFailure() <<
76  "Error 2003: Cost (signed) of the optimal angle is " <<
77  obj.computeCostWithMinimizingSign(swaths_by_cells[j]) <<
78  ", but the cost (signed) of a non-optimal angle (" <<
79  rand_ang << ") is " <<
80  obj.computeCostWithMinimizingSign(swaths_by_cells2[j]) << ".";
81  } else if (swaths_by_cells[j].size() > poly.area() / (swath_width * swath_width)) {
82  return testing::AssertionFailure() << "Error 2004: " << swaths_by_cells2[j].size() <<
83  " swaths were generated with angle " << rand_ang <<
84  "Expected less than " << poly.area() / (swath_width * swath_width) <<
85  "swaths.";
86  }
87  }
88  }
89  return testing::AssertionSuccess();
90 }
91 
92 #endif // SWATH_GENERATOR_CHECKER_HPP_
f2c::types::Geometries::area
double area() const
Compute area of the geometry.
Definition: Geometries_impl.hpp:14
5_route_planning.swaths
swaths
Definition: 5_route_planning.py:58
f2c::sg::SwathGeneratorBase::generateBestSwaths
virtual F2CSwaths generateBestSwaths(f2c::obj::SGObjective &obj, double op_width, const F2CCell &poly)
Definition: swath_generator_base.cpp:19
3_headland_generator.rand
rand
Definition: 3_headland_generator.py:11
f2c::types::Cells::size
size_t size() const
Definition: Cells.cpp:135
fields2cover.h
f2c::obj::SGObjective
Base class for objective functions of swath generators.
Definition: sg_objective.h:21
f2c::types::Cell
Definition: Cell.h:32
7_decomposition.obj
obj
Definition: 7_decomposition.py:32
f2c::sg::SwathGeneratorBase
Definition: swath_generator_base.h:17
isSwathGenerationCorrect
testing::AssertionResult isSwathGenerationCorrect(f2c::sg::SwathGeneratorBase &sw_gen, f2c::obj::SGObjective &obj, const F2CCell &poly, double swath_width=1.0, int checks=100)
Definition: swath_generator_checker.hpp:15
f2c::types::Cells
Definition: Cells.h:21
f2c::Random
Definition: random.h:23
f2c::sg::SwathGeneratorBase::generateSwaths
virtual F2CSwaths generateSwaths(double angle, double op_width, const F2CCell &poly)
Definition: swath_generator_base.cpp:43


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