A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
9
10#include <ns3/simulator.h>
11
12using namespace ns3;
13
14/**
15 * This TestSuite tests the intersection of a line segment
16 * between two 3D positions with a 3D box. It generates two
17 * positions from a set of predefined positions (see GeneratePosition method),
18 * and then tests the intersection of a line segments between them with a box
19 * of predefined dimensions.
20 */
21
23 : TestSuite("box-line-intersection", Type::UNIT)
24{
25 // Box in the positive x-plane to check the intersection with.
26 Box box = Box(890.0, 990.0, 840.0, 870.0, 0.0, 6.0);
27 bool intersect;
28 // Test #1 :
29 // pos1 (index 3) is outside the box and below the height of the box.
30 // pos2 (index 6) is outside the box and above the height of the box.
31 // Expected result: No intersection. The box is between the two position,
32 // however, pos2 is above the height of the box.
33 intersect = false;
34 AddTestCase(new BoxLineIntersectionTestCase(3, 6, box, intersect), TestCase::Duration::QUICK);
35
36 // Test #2 :
37 // pos1 (index 1) is inside the box.
38 // pos2 (index 2) is inside the box.
39 // Expected result: Intersection.
40 intersect = true;
41 AddTestCase(new BoxLineIntersectionTestCase(1, 2, box, intersect), TestCase::Duration::QUICK);
42
43 // Test #3 :
44 // pos1 (index 3) is outside the box.
45 // pos2 (index 1) is inside the box.
46 // Expected result: Intersection.
47 intersect = true;
48 AddTestCase(new BoxLineIntersectionTestCase(3, 1, box, intersect), TestCase::Duration::QUICK);
49
50 // Test #4:
51 // pos1 (index 4) is outside the box.
52 // pos2 (index 5) is outside the box.
53 // Expected result: Intersection because box is in between the two positions.
54 intersect = true;
55 AddTestCase(new BoxLineIntersectionTestCase(4, 5, box, intersect), TestCase::Duration::QUICK);
56}
57
58static BoxLineIntersectionTestSuite boxLineIntersectionTestSuite; //!< boxLineIntersectionTestSuite
59
60/**
61 * TestCase
62 */
63
65 uint16_t indexPos2,
66 Box box,
67 bool intersect)
68 : TestCase(BuildNameString(indexPos1, indexPos2, box, intersect)),
69 m_indexPos1(indexPos1),
70 m_indexPos2(indexPos2),
71 m_box(box),
72 m_intersect(intersect)
73{
74}
75
79
80std::string
82 uint16_t indexPos2,
83 Box box,
84 bool intersect)
85{
86 std::ostringstream oss;
87 oss << "Box line intersection test : checking"
88 << " pos1 index " << indexPos1 << " and pos2 index " << indexPos2
89 << " intersection with the box (" << box.xMin << ", " << box.xMax << ", " << box.yMin
90 << ", " << box.yMax << ", " << box.zMin << ", " << box.zMax
91 << "). The expected intersection flag = " << intersect << " ";
92 return oss.str();
93}
94
95void
97{
98 Vector pos1 = CreatePosition(m_indexPos1, (m_box.zMax - m_box.zMin));
99 Vector pos2 = CreatePosition(m_indexPos2, (m_box.zMax - m_box.zMin));
100
101 bool intersect = m_box.IsIntersect(pos1, pos2);
102
103 NS_TEST_ASSERT_MSG_EQ(intersect,
105 "Unexpected result of box and line segment intersection!");
107}
108
109Vector
110BoxLineIntersectionTestCase::CreatePosition(uint16_t index, double boxHeight)
111{
112 Vector pos;
113
114 switch (index)
115 {
116 case 1:
117 pos = Vector(934.0, 852.0, 1.5);
118 break;
119 case 2:
120 pos = Vector(931.0, 861.0, 1.5);
121 break;
122 case 3:
123 pos = Vector(484.0, 298.0, 1.5);
124 break;
125 case 4:
126 pos = Vector(1000.0, 850.0, 1.5);
127 break;
128 case 5:
129 pos = Vector(850.0, 850.0, 1.5);
130 break;
131 case 6:
132 pos = Vector(934.0, 852.0, boxHeight + 14.0);
133 break;
134 default:
135 NS_FATAL_ERROR("Unknown position index");
136 break;
137 }
138
139 return pos;
140}
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:24
double yMax
The y coordinate of the top bound of the box.
Definition box.h:105
double xMin
The x coordinate of the left bound of the box.
Definition box.h:99
double yMin
The y coordinate of the bottom bound of the box.
Definition box.h:103
double xMax
The x coordinate of the right bound of the box.
Definition box.h:101
double zMin
The z coordinate of the down bound of the box.
Definition box.h:107
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:133
double zMax
The z coordinate of the up bound of the box.
Definition box.h:109
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#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:134
Every class exported by the ns3 library is enclosed in the ns3 namespace.