A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
8
#include "
box-line-intersection-test.h
"
9
10
#include "ns3/simulator.h"
11
12
using 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
22
BoxLineIntersectionTestSuite::BoxLineIntersectionTestSuite
()
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
58
static
BoxLineIntersectionTestSuite
boxLineIntersectionTestSuite
;
//!< boxLineIntersectionTestSuite
59
60
/**
61
* TestCase
62
*/
63
64
BoxLineIntersectionTestCase::BoxLineIntersectionTestCase
(uint16_t
indexPos1
,
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
76
BoxLineIntersectionTestCase::~BoxLineIntersectionTestCase
()
77
{
78
}
79
80
std::string
81
BoxLineIntersectionTestCase::BuildNameString
(uint16_t
indexPos1
,
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
95
void
96
BoxLineIntersectionTestCase::DoRun
()
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
,
104
m_intersect
,
105
"Unexpected result of box and line segment intersection!"
);
106
Simulator::Destroy
();
107
}
108
109
Vector
110
BoxLineIntersectionTestCase::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
}
boxLineIntersectionTestSuite
static BoxLineIntersectionTestSuite boxLineIntersectionTestSuite
boxLineIntersectionTestSuite
Definition
box-line-intersection-test.cc:58
box-line-intersection-test.h
BoxLineIntersectionTestCase
TestCase to check the box line intersection.
Definition
box-line-intersection-test.h:35
BoxLineIntersectionTestCase::m_indexPos2
uint16_t m_indexPos2
Second position index.
Definition
box-line-intersection-test.h:79
BoxLineIntersectionTestCase::BoxLineIntersectionTestCase
BoxLineIntersectionTestCase(uint16_t indexPos1, uint16_t indexPos2, Box box, bool intersect)
Create BoxLineIntersectionTestCase.
Definition
box-line-intersection-test.cc:64
BoxLineIntersectionTestCase::m_indexPos1
uint16_t m_indexPos1
First position index.
Definition
box-line-intersection-test.h:78
BoxLineIntersectionTestCase::DoRun
void DoRun() override
Setup the simulation according to the configuration set by the class constructor, run it,...
Definition
box-line-intersection-test.cc:96
BoxLineIntersectionTestCase::BuildNameString
std::string BuildNameString(uint16_t indexPos1, uint16_t indexPos2, Box box, bool intersect)
Builds the test name string based on provided parameter values.
Definition
box-line-intersection-test.cc:81
BoxLineIntersectionTestCase::m_intersect
bool m_intersect
Flag to indicate the intersection.
Definition
box-line-intersection-test.h:81
BoxLineIntersectionTestCase::CreatePosition
Vector CreatePosition(uint16_t index, double boxHeight)
Create the position as per the given index.
Definition
box-line-intersection-test.cc:110
BoxLineIntersectionTestCase::~BoxLineIntersectionTestCase
~BoxLineIntersectionTestCase() override
Destructor.
Definition
box-line-intersection-test.cc:76
BoxLineIntersectionTestCase::m_box
Box m_box
The box to check the intersection with.
Definition
box-line-intersection-test.h:80
BoxLineIntersectionTestSuite
Test suite for.
Definition
box-line-intersection-test.h:23
BoxLineIntersectionTestSuite::BoxLineIntersectionTestSuite
BoxLineIntersectionTestSuite()
Constructor.
Definition
box-line-intersection-test.cc:22
ns3::Box
a 3d box
Definition
box.h:24
ns3::Box::zMin
double zMin
The z coordinate of the down bound of the box.
Definition
box.h:107
ns3::Box::IsIntersect
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
ns3::Box::zMax
double zMax
The z coordinate of the up bound of the box.
Definition
box.h:109
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition
fatal-error.h:168
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
NS_TEST_ASSERT_MSG_EQ
#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
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
mobility
test
box-line-intersection-test.cc
Generated on Mon Dec 15 2025 15:22:00 for ns-3 by
1.9.8