From ab084a9399a95c503abbf01028f40acb5d3b0d19 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 09:34:36 +0200 Subject: [PATCH] move around rectangle class and move IsInside method from Position to Rectangle --- SConstruct | 8 ++-- src/node/position.cc | 9 ---- src/node/position.h | 3 -- src/node/random-walk-2d-mobility-model.cc | 2 +- src/{core => node}/rectangle-default-value.cc | 0 src/{core => node}/rectangle-default-value.h | 2 +- src/{core => node}/rectangle.cc | 45 +++++++++++++++++++ src/{core => node}/rectangle.h | 11 +++++ src/node/static-speed-helper.cc | 2 +- 9 files changed, 63 insertions(+), 19 deletions(-) rename src/{core => node}/rectangle-default-value.cc (100%) rename src/{core => node}/rectangle-default-value.h (98%) rename src/{core => node}/rectangle.cc (55%) rename src/{core => node}/rectangle.h (86%) diff --git a/SConstruct b/SConstruct index 2fea9dad1..cac6a76a6 100644 --- a/SConstruct +++ b/SConstruct @@ -63,8 +63,6 @@ core.add_sources([ 'uid-manager.cc', 'default-value.cc', 'random-variable-default-value.cc', - 'rectangle-default-value.cc', - 'rectangle.cc', 'command-line.cc', 'type-name.cc', 'component-manager.cc', @@ -97,8 +95,6 @@ core.add_inst_headers([ 'rng-stream.h', 'default-value.h', 'random-variable-default-value.h', - 'rectangle-default-value.h', - 'rectangle.h', 'command-line.h', 'type-name.h', 'component-manager.h', @@ -266,6 +262,8 @@ node.add_sources ([ 'speed.cc', 'static-speed-helper.cc', 'random-waypoint-mobility-model.cc', + 'rectangle-default-value.cc', + 'rectangle.cc', ]) node.add_inst_headers ([ 'node.h', @@ -299,6 +297,8 @@ node.add_inst_headers ([ 'speed.h', 'static-speed-helper.h', 'random-waypoint-mobility-model.h', + 'rectangle-default-value.h', + 'rectangle.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/position.cc b/src/node/position.cc index b4efadb7f..802fe3bb7 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -1,5 +1,4 @@ #include "position.h" -#include "ns3/rectangle.h" #include namespace ns3 { @@ -17,14 +16,6 @@ Position::Position () z (0.0) {} -bool -Position::IsInside (const Rectangle &rectangle) const -{ - return - x <= rectangle.xMax && x >= rectangle.xMin && - y <= rectangle.yMax && y >= rectangle.yMin; -} - double CalculateDistance (const Position &a, const Position &b) { diff --git a/src/node/position.h b/src/node/position.h index 7a97dede7..261ba2e99 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -3,14 +3,11 @@ namespace ns3 { -class Rectangle; - class Position { public: Position (double x, double y, double z); Position (); - bool IsInside (const Rectangle &rectangle) const; double x; double y; double z; diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc index 9b85fed57..c96ab24c5 100644 --- a/src/node/random-walk-2d-mobility-model.cc +++ b/src/node/random-walk-2d-mobility-model.cc @@ -170,7 +170,7 @@ RandomWalk2dMobilityModel::DoGet (void) const void RandomWalk2dMobilityModel::DoSet (const Position &position) { - NS_ASSERT (position.IsInside (m_parameters->m_bounds)); + NS_ASSERT (m_parameters->m_bounds.IsInside (position)); m_helper.InitializePosition (position); Simulator::Remove (m_event); m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); diff --git a/src/core/rectangle-default-value.cc b/src/node/rectangle-default-value.cc similarity index 100% rename from src/core/rectangle-default-value.cc rename to src/node/rectangle-default-value.cc diff --git a/src/core/rectangle-default-value.h b/src/node/rectangle-default-value.h similarity index 98% rename from src/core/rectangle-default-value.h rename to src/node/rectangle-default-value.h index 9e0448290..047e7bbb6 100644 --- a/src/core/rectangle-default-value.h +++ b/src/node/rectangle-default-value.h @@ -22,7 +22,7 @@ #define RECTANGLE_DEFAULT_VALUE_H #include -#include "default-value.h" +#include "ns3/default-value.h" #include "rectangle.h" namespace ns3 { diff --git a/src/core/rectangle.cc b/src/node/rectangle.cc similarity index 55% rename from src/core/rectangle.cc rename to src/node/rectangle.cc index 9fdd739d5..87ca20690 100644 --- a/src/core/rectangle.cc +++ b/src/node/rectangle.cc @@ -19,6 +19,9 @@ * Author: Mathieu Lacage */ #include "rectangle.h" +#include "position.h" +#include +#include namespace ns3 { @@ -37,4 +40,46 @@ Rectangle::Rectangle () yMax (0.0) {} +bool +Rectangle::IsInside (const Position &position) const +{ + return + position.x <= xMax && position.x >= xMin && + position.y <= yMax && position.y >= yMin; +} + +Rectangle::Side +Rectangle::GetClosestSide (const Position &position) const +{ + double xMinDist = std::abs (position.x - xMin); + double xMaxDist = std::abs (xMax - position.x); + double yMinDist = std::abs (position.y - yMin); + double yMaxDist = std::abs (yMax - position.y); + double minX = std::min (xMinDist, xMaxDist); + double minY = std::min (yMinDist, yMaxDist); + if (minX < minY) + { + if (xMinDist < xMaxDist) + { + return LEFT; + } + else + { + return RIGHT; + } + } + else + { + if (yMinDist < yMaxDist) + { + return BOTTOM; + } + else + { + return TOP; + } + } +} + + } // namespace ns3 diff --git a/src/core/rectangle.h b/src/node/rectangle.h similarity index 86% rename from src/core/rectangle.h rename to src/node/rectangle.h index b108f9c21..a6de28ba1 100644 --- a/src/core/rectangle.h +++ b/src/node/rectangle.h @@ -23,12 +23,23 @@ namespace ns3 { +class Position; + class Rectangle { public: + enum Side { + RIGHT, + LEFT, + TOP, + BOTTOM + }; Rectangle (double _xMin, double _xMax, double _yMin, double _yMax); Rectangle (); + bool IsInside (const Position &position) const; + Side GetClosestSide (const Position &position) const; + double xMin; double xMax; double yMin; diff --git a/src/node/static-speed-helper.cc b/src/node/static-speed-helper.cc index c56eb5dd9..92d811287 100644 --- a/src/node/static-speed-helper.cc +++ b/src/node/static-speed-helper.cc @@ -105,7 +105,7 @@ StaticSpeedHelper::GetDelayToNextPosition (const Rectangle &bounds, Time delayLe Position nextPosition = m_position; nextPosition.x += m_speed.dx * delayLeft.GetSeconds (); nextPosition.y += m_speed.dy * delayLeft.GetSeconds (); - if (nextPosition.IsInside (bounds)) + if (bounds.IsInside (nextPosition)) { return delayLeft; }