A Discrete-Event Network Simulator
API
box-line-intersection-test.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
21 #include <ns3/simulator.h>
22 
23 using namespace ns3;
24 
35  : TestSuite ("box-line-intersection", UNIT)
36 {
37  // Box in the positive x-plane to check the intersection with.
38  Box box = Box (890.0, 990.0, 840.0, 870.0, 0.0, 6.0);
39  bool intersect;
40  // Test #1 :
41  // pos1 (index 3) is outside the box and below the height of the box.
42  // pos2 (index 6) is outside the box and above the height of the box.
43  // Expected result: No intersection. The box is between the two position,
44  // however, pos2 is above the height of the box.
45  intersect = false;
46  AddTestCase (new BoxLineIntersectionTestCase (3, 6, box, intersect), TestCase::QUICK);
47 
48  // Test #2 :
49  // pos1 (index 1) is inside the box.
50  // pos2 (index 2) is inside the box.
51  // Expected result: Intersection.
52  intersect = true;
53  AddTestCase (new BoxLineIntersectionTestCase (1, 2, box, intersect), TestCase::QUICK);
54 
55  // Test #3 :
56  // pos1 (index 3) is outside the box.
57  // pos2 (index 1) is inside the box.
58  // Expected result: Intersection.
59  intersect = true;
60  AddTestCase (new BoxLineIntersectionTestCase (3, 1, box, intersect), TestCase::QUICK);
61 
62  // Test #4:
63  // pos1 (index 4) is outside the box.
64  // pos2 (index 5) is outside the box.
65  // Expected result: Intersection because box is in between the two positions.
66  intersect = true;
67  AddTestCase (new BoxLineIntersectionTestCase (4, 5, box, intersect), TestCase::QUICK);
68 }
69 
71 
72 
77 BoxLineIntersectionTestCase::BoxLineIntersectionTestCase (uint16_t indexPos1, uint16_t indexPos2, Box box, bool intersect)
78  : TestCase (BuildNameString (indexPos1, indexPos2, box, intersect)),
79  m_indexPos1 (indexPos1),
80  m_indexPos2 (indexPos2),
81  m_box (box),
82  m_intersect (intersect)
83 {
84 }
85 
87 {
88 }
89 
90 std::string
91 BoxLineIntersectionTestCase::BuildNameString (uint16_t indexPos1, uint16_t indexPos2, Box box, bool intersect)
92 {
93  std::ostringstream oss;
94  oss << "Box line intersection test : checking"
95  << " pos1 index " << indexPos1 << " and pos2 index " << indexPos2
96  << " intersection with the box (" << box.xMin << ", " << box.xMax
97  << ", " << box.yMin << ", " << box.yMax
98  << ", " << box.zMin << ", " << box.zMax
99  << "). The expected intersection flag = " << intersect << " ";
100  return oss.str ();
101 }
102 
103 void
105 {
106 
107  Vector pos1 = CreatePosition (m_indexPos1, (m_box.zMax - m_box.zMin));
108  Vector pos2 = CreatePosition (m_indexPos2, (m_box.zMax - m_box.zMin));
109 
110  bool intersect = m_box.IsIntersect (pos1, pos2);
111 
112  NS_TEST_ASSERT_MSG_EQ (intersect, m_intersect, "Unexpected result of box and line segment intersection!");
113  Simulator::Destroy ();
114 }
115 
116 Vector
117 BoxLineIntersectionTestCase::CreatePosition (uint16_t index, double boxHeight)
118 {
119  Vector pos;
120 
121  switch (index)
122  {
123  case 1:
124  pos = Vector (934.0, 852.0, 1.5);
125  break;
126  case 2:
127  pos = Vector (931.0,861.0, 1.5);
128  break;
129  case 3:
130  pos = Vector (484.0, 298.0, 1.5);
131  break;
132  case 4:
133  pos = Vector (1000.0, 850.0, 1.5);
134  break;
135  case 5:
136  pos = Vector (850.0, 850.0, 1.5);
137  break;
138  case 6:
139  pos = Vector (934.0, 852.0, boxHeight + 14.0);
140  break;
141  default:
142  NS_FATAL_ERROR ("Unknown position index");
143  break;
144  }
145 
146  return pos;
147 }
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.
virtual void DoRun(void)
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.
virtual ~BoxLineIntersectionTestCase()
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:147
double zMax
The z coordinate of the up bound of the box.
Definition: box.h:120
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
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:165
#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:141
Every class exported by the ns3 library is enclosed in the ns3 namespace.