Skip to content

Commit

Permalink
feat: initial code showing intersections on a osm map
Browse files Browse the repository at this point in the history
  • Loading branch information
mvirgo committed Apr 25, 2021
1 parent d6820c4 commit 951f0ae
Show file tree
Hide file tree
Showing 207 changed files with 256,007 additions and 942 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.DS_Store
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.11.3)

# set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 17)
project(Rideshare_Simulator)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread")

Expand All @@ -10,9 +10,11 @@ include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIBRARY_DIRS})
add_definitions(${OpenCV_DEFINITIONS})

add_subdirectory(thirdparty/pugixml)

# Find all executables
file(GLOB project_SRCS src/*.cpp) #src/*.h
file(GLOB_RECURSE project_SRCS src/*.cpp src/*.h)

# Add project executable
add_executable(rideshare_simulation ${project_SRCS})
target_link_libraries(rideshare_simulation ${OpenCV_LIBRARIES})
target_link_libraries(rideshare_simulation pugixml ${OpenCV_LIBRARIES})
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
My Capstone project for the C++ Nanodegree at Udacity, a rideshare simulator. It extends the concurrency project based on a traffic simulation, as well as taking in parts of the earlier route planning project, in order to simulate ridesharing apps that are able to pick up passengers.

## Dependencies for Running Locally
* cmake >= 2.8
* cmake >= 3.11
* All OSes: [click here for installation instructions](https://cmake.org/install/)
* make >= 4.1 (Linux, Mac), 3.81 (Windows)
* Linux: make is installed by default on most Linux distros
Expand Down
162,921 changes: 162,921 additions & 0 deletions data/arc-paris.osm

Large diffs are not rendered by default.

Binary file added data/arc-paris.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45,616 changes: 45,616 additions & 0 deletions data/downtown-kc.osm

Large diffs are not rendered by default.

Binary file added data/downtown-kc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed data/nyc.jpg
Binary file not shown.
Binary file removed data/paris.jpg
Binary file not shown.
Binary file removed data/traffic_simulation.gif
Binary file not shown.
75 changes: 75 additions & 0 deletions src/BasicGraphics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <chrono>
#include <iostream>
#include <thread>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include "BasicGraphics.h"

BasicGraphics::BasicGraphics(float minLat, float minLon, float maxLat, float maxLon)
{
this->minLat = minLat;
this->minLon = minLon;
this->maxLat = maxLat;
this->maxLon = maxLon;
}

void BasicGraphics::simulate()
{
this->loadBackgroundImg();
while (true)
{
// sleep at every iteration to reduce CPU usage
// TODO: Shorten below delay once ready for active updating
std::this_thread::sleep_for(std::chrono::milliseconds(10000));

// update graphics
this->drawIntersections();
}
}

void BasicGraphics::loadBackgroundImg()
{
// create window
_windowName = "Rideshare Simulation";
cv::namedWindow(_windowName, cv::WINDOW_NORMAL);

// load image and create copy to be used for semi-transparent overlay
cv::Mat background = cv::imread(_bgFilename);
_images.push_back(background); // first element is the original background
_images.push_back(background.clone()); // second element will be the transparent overlay
_images.push_back(background.clone()); // third element will be the result image for display
}

void BasicGraphics::drawIntersections()
{
// reset images
_images.at(1) = _images.at(0).clone();
_images.at(2) = _images.at(0).clone();

// Get image rows and columns to help with coordinate adjustments for image
float imgRows = _images.at(0).rows;
float imgCols = _images.at(0).cols;

// create overlay from intersections
for (auto intersect : _intersections)
{
std::vector<float> position = intersect.getPosition();

// Adjust the position based on lat & lon in image
position[0] = (position[0] - minLon) / (maxLon - minLon);
position[1] = (maxLat - position[1]) / (maxLat - minLat);

// set color according to traffic light and draw the intersection as a circle
//std::cout << "Position at: " << (int)(position[0] * imgCols) << "," << (int)(position[1] * imgRows) << std::endl;
cv::circle(_images.at(1), cv::Point2d((int)(position[0] * imgCols), (int)(position[1] * imgRows)), 25, cv::Scalar(0, 255, 0), -1);
}

float opacity = 0.85;
cv::addWeighted(_images.at(1), opacity, _images.at(0), 1.0 - opacity, 0, _images.at(2));
std::cout << "Image size is: " << _images.at(0).rows << " rows, and " << _images.at(0).cols << " columns." << std::endl;

// display background and overlay image
cv::imshow(_windowName, _images.at(2));
cv::waitKey(33);
}
35 changes: 35 additions & 0 deletions src/BasicGraphics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef BASICGRAPHICS_H
#define BASICGRAPHICS_H

#include <string>
#include <vector>
#include <opencv2/core.hpp>
#include "BasicIntersection.h"

class BasicGraphics
{
public:
// constructor / destructor
BasicGraphics(float minLat, float minLon, float maxLat, float maxLon);

// getters / setters
void setBgFilename(std::string filename) { _bgFilename = filename; }
void setIntersections(std::vector<BasicIntersection> &intersections) { _intersections = intersections; };

// typical behaviour methods
void simulate();

private:
// typical behaviour methods
void loadBackgroundImg();
void drawIntersections();

// member variables
float minLat, minLon, maxLat, maxLon;
std::vector<BasicIntersection> _intersections;
std::string _bgFilename;
std::string _windowName;
std::vector<cv::Mat> _images;
};

#endif
20 changes: 20 additions & 0 deletions src/BasicIntersection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef BASICINTERSECTION_H
#define BASICINTERSECTION_H

#include <vector>

class BasicIntersection
{
public:
// constructor / destructor

// getters / setters
void setPosition(float x, float y) { _x = x; _y = y;}
std::vector<float> getPosition() { return {_x, _y}; }

private:
// member variables
float _x, _y;
};

#endif
72 changes: 0 additions & 72 deletions src/Graphics.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions src/Graphics.h

This file was deleted.

Loading

0 comments on commit 951f0ae

Please sign in to comment.