A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
hierarchical-mobility-model.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2007 INRIA
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
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
#include "
hierarchical-mobility-model.h
"
21
#include "ns3/pointer.h"
22
23
namespace
ns3 {
24
25
NS_OBJECT_ENSURE_REGISTERED
(HierarchicalMobilityModel);
26
27
TypeId
28
HierarchicalMobilityModel::GetTypeId
(
void
)
29
{
30
static
TypeId
tid =
TypeId
(
"ns3::HierarchicalMobilityModel"
)
31
.
SetParent
<
MobilityModel
> ()
32
.AddConstructor<HierarchicalMobilityModel> ()
33
.AddAttribute (
"Child"
,
"The child mobility model."
,
34
PointerValue
(),
35
MakePointerAccessor (&
HierarchicalMobilityModel::SetChild
,
36
&
HierarchicalMobilityModel::GetChild
),
37
MakePointerChecker<MobilityModel> ())
38
.AddAttribute (
"Parent"
,
"The parent mobility model."
,
39
PointerValue
(),
40
MakePointerAccessor (&
HierarchicalMobilityModel::SetParent
,
41
&
HierarchicalMobilityModel::GetParent
),
42
MakePointerChecker<MobilityModel> ())
43
;
44
return
tid;
45
}
46
47
HierarchicalMobilityModel::HierarchicalMobilityModel
()
48
: m_child (0),
49
m_parent (0)
50
{
51
}
52
53
void
54
HierarchicalMobilityModel::SetChild
(
Ptr<MobilityModel>
model)
55
{
56
Ptr<MobilityModel>
oldChild =
m_child
;
57
Vector
pos;
58
if
(
m_child
)
59
{
60
pos =
GetPosition
();
61
m_child
->
TraceDisconnectWithoutContext
(
"CourseChange"
,
MakeCallback
(&
HierarchicalMobilityModel::ChildChanged
,
this
));
62
}
63
m_child
= model;
64
m_child
->
TraceConnectWithoutContext
(
"CourseChange"
,
MakeCallback
(&
HierarchicalMobilityModel::ChildChanged
,
this
));
65
66
// if we had a child before, then we had a valid position before;
67
// try to preserve the old absolute position.
68
if
(oldChild)
69
{
70
SetPosition
(pos);
71
}
72
}
73
74
void
75
HierarchicalMobilityModel::SetParent
(
Ptr<MobilityModel>
model)
76
{
77
Vector
pos;
78
if
(
m_child
)
79
{
80
pos =
GetPosition
();
81
}
82
if
(
m_parent
)
83
{
84
m_parent
->
TraceDisconnectWithoutContext
(
"CourseChange"
,
MakeCallback
(&
HierarchicalMobilityModel::ParentChanged
,
this
));
85
}
86
m_parent
= model;
87
if
(
m_parent
)
88
{
89
m_parent
->
TraceConnectWithoutContext
(
"CourseChange"
,
MakeCallback
(&
HierarchicalMobilityModel::ParentChanged
,
this
));
90
}
91
// try to preserve the old position across parent changes
92
if
(
m_child
)
93
{
94
SetPosition
(pos);
95
}
96
}
97
98
99
Ptr<MobilityModel>
100
HierarchicalMobilityModel::GetChild
(
void
)
const
101
{
102
return
m_child
;
103
}
104
105
Ptr<MobilityModel>
106
HierarchicalMobilityModel::GetParent
(
void
)
const
107
{
108
return
m_parent
;
109
}
110
111
Vector
112
HierarchicalMobilityModel::DoGetPosition
(
void
)
const
113
{
114
if
(!
m_parent
)
115
{
116
return
m_child
->
GetPosition
();
117
}
118
Vector
parentPosition =
m_parent
->
GetPosition
();
119
Vector
childPosition =
m_child
->
GetPosition
();
120
return
Vector
(parentPosition.
x
+ childPosition.
x
,
121
parentPosition.
y
+ childPosition.
y
,
122
parentPosition.
z
+ childPosition.
z
);
123
}
124
void
125
HierarchicalMobilityModel::DoSetPosition
(
const
Vector
&position)
126
{
127
if
(
m_child
== 0)
128
{
129
return
;
130
}
131
// This implementation of DoSetPosition is really an arbitraty choice.
132
// anything else would have been ok.
133
if
(
m_parent
)
134
{
135
Vector
parentPosition =
m_parent
->
GetPosition
();
136
Vector
childPosition (position.
x
- parentPosition.
x
,
137
position.
y
- parentPosition.
y
,
138
position.
z
- parentPosition.
z
);
139
m_child
->
SetPosition
(childPosition);
140
}
141
else
142
{
143
m_child
->
SetPosition
(position);
144
}
145
}
146
Vector
147
HierarchicalMobilityModel::DoGetVelocity
(
void
)
const
148
{
149
if
(
m_parent
)
150
{
151
Vector
parentSpeed =
m_parent
->
GetVelocity
();
152
Vector
childSpeed =
m_child
->
GetVelocity
();
153
Vector
speed (parentSpeed.
x
+ childSpeed.
x
,
154
parentSpeed.
y
+ childSpeed.
y
,
155
parentSpeed.
z
+ childSpeed.
z
);
156
return
speed;
157
}
158
else
159
{
160
return
m_child
->
GetVelocity
();
161
}
162
}
163
164
void
165
HierarchicalMobilityModel::ParentChanged
(
Ptr<const MobilityModel>
model)
166
{
167
MobilityModel::NotifyCourseChange
();
168
}
169
170
void
171
HierarchicalMobilityModel::ChildChanged
(
Ptr<const MobilityModel>
model)
172
{
173
MobilityModel::NotifyCourseChange
();
174
}
175
176
177
178
}
// namespace ns3
src
mobility
model
hierarchical-mobility-model.cc
Generated on Tue May 14 2013 11:08:29 for ns-3 by
1.8.1.2