A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
outdoor-random-walk-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 * Copyright (c) 2019, University of Padova, Dep. of Information Engineering, SIGNET lab
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Nicola Baldo <nbaldo@cttc.es> for the code adapted from the lena-dual-stripe.cc example
8 * Author: Michele Polese <michele.polese@gmail.com> for this version
9 */
10
11#include "ns3/buildings-module.h"
12#include "ns3/core-module.h"
13#include "ns3/mobility-module.h"
14#include "ns3/network-module.h"
15
16using namespace ns3;
17
18NS_LOG_COMPONENT_DEFINE("OutdoorRandomWalkExample");
19
20/**
21 * Print the buildings list in a format that can be used by Gnuplot to draw them.
22 *
23 * \param filename The output filename.
24 */
25void
27{
28 std::ofstream outFile;
29 outFile.open(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
30 if (!outFile.is_open())
31 {
32 NS_LOG_ERROR("Can't open file " << filename);
33 return;
34 }
35 uint32_t index = 0;
36 for (auto it = BuildingList::Begin(); it != BuildingList::End(); ++it)
37 {
38 ++index;
39 Box box = (*it)->GetBoundaries();
40 outFile << "set object " << index << " rect from " << box.xMin << "," << box.yMin << " to "
41 << box.xMax << "," << box.yMax << std::endl;
42 }
43}
44
45/**
46 * This is an example on how to use the RandomWalk2dOutdoorMobilityModel class.
47 * The script outdoor-random-walk-example.sh can be used to visualize the
48 * positions visited by the random walk.
49 */
50int
51main(int argc, char* argv[])
52{
53 LogComponentEnable("RandomWalk2dOutdoor", LOG_LEVEL_LOGIC);
54 CommandLine cmd(__FILE__);
55 cmd.Parse(argc, argv);
56
57 // create a grid of buildings
58 double buildingSizeX = 100; // m
59 double buildingSizeY = 50; // m
60 double streetWidth = 25; // m
61 double buildingHeight = 10; // m
62 uint32_t numBuildingsX = 10;
63 uint32_t numBuildingsY = 10;
64 double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
65 double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
66
67 std::vector<Ptr<Building>> buildingVector;
68 for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
69 {
70 for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
71 {
72 Ptr<Building> building;
73 building = CreateObject<Building>();
74
75 building->SetBoundaries(Box(buildingIdX * (buildingSizeX + streetWidth),
76 buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
77 buildingIdY * (buildingSizeY + streetWidth),
78 buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
79 0.0,
80 buildingHeight));
81 building->SetNRoomsX(1);
82 building->SetNRoomsY(1);
83 building->SetNFloors(1);
84 buildingVector.push_back(building);
85 }
86 }
87
88 // print the list of buildings to file
90
91 // create one node
93 nodes.Create(1);
94
95 // set the RandomWalk2dOutdoorMobilityModel mobility model
97 mobility.SetMobilityModel(
98 "ns3::RandomWalk2dOutdoorMobilityModel",
99 "Bounds",
100 RectangleValue(Rectangle(-streetWidth, maxAxisX, -streetWidth, maxAxisY)));
101 // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility
102 // model
105 xPos->SetAttribute("Min", DoubleValue(-streetWidth));
106 xPos->SetAttribute("Max", DoubleValue(maxAxisX));
108 yPos->SetAttribute("Min", DoubleValue(-streetWidth));
109 yPos->SetAttribute("Max", DoubleValue(maxAxisY));
110 position->SetAttribute("X", PointerValue(xPos));
111 position->SetAttribute("Y", PointerValue(yPos));
112 mobility.SetPositionAllocator(position);
113 // install the mobility model
114 mobility.Install(nodes.Get(0));
115
116 // enable the traces for the mobility model
117 AsciiTraceHelper ascii;
118 MobilityHelper::EnableAsciiAll(ascii.CreateFileStream("mobility-trace-example.mob"));
119
123
124 return 0;
125}
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
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
static Iterator End()
static Iterator Begin()
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Helper class used to assign positions and mobility models to nodes.
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
a 2d rectangle
Definition rectangle.h:24
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition log.h:99
mobility
Definition third.py:92
void PrintGnuplottableBuildingListToFile(std::string filename)
Print the buildings list in a format that can be used by Gnuplot to draw them.