A Discrete-Event Network Simulator
API
box-line-intersection-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  */
18 
20 
21 #include <ns3/simulator.h>
22 
23 using namespace ns3;
24 
34  : TestSuite("box-line-intersection", UNIT)
35 {
36  // Box in the positive x-plane to check the intersection with.
37  Box box = Box(890.0, 990.0, 840.0, 870.0, 0.0, 6.0);
38  bool intersect;
39  // Test #1 :
40  // pos1 (index 3) is outside the box and below the height of the box.
41  // pos2 (index 6) is outside the box and above the height of the box.
42  // Expected result: No intersection. The box is between the two position,
43  // however, pos2 is above the height of the box.
44  intersect = false;
45  AddTestCase(new BoxLineIntersectionTestCase(3, 6, box, intersect), TestCase::QUICK);
46 
47  // Test #2 :
48  // pos1 (index 1) is inside the box.
49  // pos2 (index 2) is inside the box.
50  // Expected result: Intersection.
51  intersect = true;
52  AddTestCase(new BoxLineIntersectionTestCase(1, 2, box, intersect), TestCase::QUICK);
53 
54  // Test #3 :
55  // pos1 (index 3) is outside the box.
56  // pos2 (index 1) is inside the box.
57  // Expected result: Intersection.
58  intersect = true;
59  AddTestCase(new BoxLineIntersectionTestCase(3, 1, box, intersect), TestCase::QUICK);
60 
61  // Test #4:
62  // pos1 (index 4) is outside the box.
63  // pos2 (index 5) is outside the box.
64  // Expected result: Intersection because box is in between the two positions.
65  intersect = true;
66  AddTestCase(new BoxLineIntersectionTestCase(4, 5, box, intersect), TestCase::QUICK);
67 }
68 
70 
76  uint16_t indexPos2,
77  Box box,
78  bool intersect)
79  : TestCase(BuildNameString(indexPos1, indexPos2, box, intersect)),
80  m_indexPos1(indexPos1),
81  m_indexPos2(indexPos2),
82  m_box(box),
83  m_intersect(intersect)
84 {
85 }
86 
88 {
89 }
90 
91 std::string
93  uint16_t indexPos2,
94  Box box,
95  bool intersect)
96 {
97  std::ostringstream oss;
98  oss << "Box line intersection test : checking"
99  << " pos1 index " << indexPos1 << " and pos2 index " << indexPos2
100  << " intersection with the box (" << box.xMin << ", " << box.xMax << ", " << box.yMin
101  << ", " << box.yMax << ", " << box.zMin << ", " << box.zMax
102  << "). The expected intersection flag = " << intersect << " ";
103  return oss.str();
104 }
105 
106 void
108 {
109  Vector pos1 = CreatePosition(m_indexPos1, (m_box.zMax - m_box.zMin));
110  Vector pos2 = CreatePosition(m_indexPos2, (m_box.zMax - m_box.zMin));
111 
112  bool intersect = m_box.IsIntersect(pos1, pos2);
113 
114  NS_TEST_ASSERT_MSG_EQ(intersect,
115  m_intersect,
116  "Unexpected result of box and line segment intersection!");
117  Simulator::Destroy();
118 }
119 
120 Vector
121 BoxLineIntersectionTestCase::CreatePosition(uint16_t index, double boxHeight)
122 {
123  Vector pos;
124 
125  switch (index)
126  {
127  case 1:
128  pos = Vector(934.0, 852.0, 1.5);
129  break;
130  case 2:
131  pos = Vector(931.0, 861.0, 1.5);
132  break;
133  case 3:
134  pos = Vector(484.0, 298.0, 1.5);
135  break;
136  case 4:
137  pos = Vector(1000.0, 850.0, 1.5);
138  break;
139  case 5:
140  pos = Vector(850.0, 850.0, 1.5);
141  break;
142  case 6:
143  pos = Vector(934.0, 852.0, boxHeight + 14.0);
144  break;
145  default:
146  NS_FATAL_ERROR("Unknown position index");
147  break;
148  }
149 
150  return pos;
151 }
static BoxLineIntersectionTestSuite boxLineIntersectionTestSuite
boxLineIntersectionTestSuite
TestCase to check the box line intersection.
uint16_t m_indexPos2
Second position index.
BoxLineIntersectionTestCase(uint16_t indexPos1, uint16_t indexPos2, Box box, bool intersect)
Create BoxLineIntersectionTestCase.
uint16_t m_indexPos1
First position index.
void DoRun() override
Setup the simulation according to the configuration set by the class constructor, run it,...
std::string BuildNameString(uint16_t indexPos1, uint16_t indexPos2, Box box, bool intersect)
Builds the test name string based on provided parameter values.
bool m_intersect
Flag to indicate the intersection.
Vector CreatePosition(uint16_t index, double boxHeight)
Create the position as per the given index.
~BoxLineIntersectionTestCase() override
Destructor.
Box m_box
The box to check the intersection with.
a 3d box
Definition: box.h:35
double yMax
The y coordinate of the top bound of the box.
Definition: box.h:116
double xMin
The x coordinate of the left bound of the box.
Definition: box.h:110
double yMin
The y coordinate of the bottom bound of the box.
Definition: box.h:114
double xMax
The x coordinate of the right bound of the box.
Definition: box.h:112
double zMin
The z coordinate of the down bound of the box.
Definition: box.h:118
bool IsIntersect(const Vector &l1, const Vector &l2) const
Checks if a line-segment between position l1 and position l2 intersects a box.
Definition: box.cc:144
double zMax
The z coordinate of the up bound of the box.
Definition: box.h:120
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:144
Every class exported by the ns3 library is enclosed in the ns3 namespace.