#include <config.h>
#include <utils/shapes/PointOfInterest.h>
#include <utils/shapes/ShapeContainer.h>
#include <utils/geom/GeomHelper.h>
#include <microsim/MSNet.h>
#include <libsumo/TraCIConstants.h>
#include "Polygon.h"
#include "POI.h"
#include "Helper.h"
namespace libsumo {
SubscriptionResults POI::mySubscriptionResults;
ContextSubscriptionResults POI::myContextSubscriptionResults;
NamedRTree* POI::myTree(nullptr);
std::vector<std::string>
POI::getIDList() {
std::vector<std::string> ids;
MSNet::getInstance()->getShapeContainer().getPOIs().insertIDs(ids);
return ids;
}
int
POI::getIDCount() {
return (int)getIDList().size();
}
std::string
POI::getType(const std::string& poiID) {
return getPoI(poiID)->getShapeType();
}
TraCIColor
POI::getColor(const std::string& poiID) {
return Helper::makeTraCIColor(getPoI(poiID)->getShapeColor());
}
TraCIPosition
POI::getPosition(const std::string& poiID, const bool includeZ) {
return Helper::makeTraCIPosition(*getPoI(poiID), includeZ);
}
double
POI::getWidth(const std::string& poiID) {
return getPoI(poiID)->getWidth();
}
double
POI::getHeight(const std::string& poiID) {
return getPoI(poiID)->getHeight();
}
double
POI::getAngle(const std::string& poiID) {
return getPoI(poiID)->getShapeNaviDegree();
}
std::string
POI::getImageFile(const std::string& poiID) {
return getPoI(poiID)->getShapeImgFile();
}
std::string
POI::getParameter(const std::string& poiID, const std::string& key) {
return getPoI(poiID)->getParameter(key, "");
}
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(POI)
void
POI::setType(const std::string& poiID, const std::string& poiType) {
getPoI(poiID)->setShapeType(poiType);
}
void
POI::setPosition(const std::string& poiID, double x, double y) {
getPoI(poiID);
MSNet::getInstance()->getShapeContainer().movePOI(poiID, Position(x, y));
}
void
POI::setColor(const std::string& poiID, const TraCIColor& c) {
getPoI(poiID)->setShapeColor(Helper::makeRGBColor(c));
}
void
POI::setWidth(const std::string& poiID, double width) {
getPoI(poiID)->setWidth(width);
}
void
POI::setHeight(const std::string& poiID, double height) {
getPoI(poiID)->setHeight(height);
}
void
POI::setAngle(const std::string& poiID, double angle) {
getPoI(poiID)->setShapeNaviDegree(angle);
}
void
POI::setImageFile(const std::string& poiID, const std::string& imageFile) {
getPoI(poiID)->setShapeImgFile(imageFile);
}
bool
POI::add(const std::string& poiID, double x, double y, const TraCIColor& color, const std::string& poiType,
int layer, const std::string& imgFile, double width, double height, double angle, const std::string& icon) {
ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
bool ok = shapeCont.addPOI(poiID, poiType, Helper::makeRGBColor(color),
Position(x, y), false, "", 0, false, 0, icon, layer,
angle, imgFile, width, height);
if (ok && myTree != nullptr) {
PointOfInterest* p = shapeCont.getPOIs().get(poiID);
const float cmin[2] = {(float)p->x(), (float)p->y()};
const float cmax[2] = {(float)p->x(), (float)p->y()};
myTree->Insert(cmin, cmax, p);
}
return ok;
}
bool
POI::remove(const std::string& poiID, int ) {
ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
PointOfInterest* p = shapeCont.getPOIs().get(poiID);
if (p != nullptr && myTree != nullptr) {
const float cmin[2] = {(float)p->x(), (float)p->y()};
const float cmax[2] = {(float)p->x(), (float)p->y()};
myTree->Remove(cmin, cmax, p);
}
return shapeCont.removePOI(poiID);
}
void
POI::highlight(const std::string& poiID, const TraCIColor& col, double size, const int alphaMax, const double duration, const int type) {
PointOfInterest* poi = getPoI(poiID);
Position* center = dynamic_cast<Position*>(poi);
if (size <= 0) {
size = sqrt(poi->getHeight() * poi->getHeight() + poi->getWidth() * poi->getWidth()) * 0.7;
}
const unsigned int nPoints = 34;
const PositionVector circlePV = GeomHelper::makeRing(size, size + 1., *center, nPoints);
TraCIPositionVector circle = Helper::makeTraCIPositionVector(circlePV);
#ifdef DEBUG_DYNAMIC_SHAPES
std::cout << SIMTIME << " Vehicle::highlight() for vehicle '" << vehicleID << "'\n"
<< " circle: " << circlePV << std::endl;
#endif
int i = 0;
std::string polyID = poi->getID() + "_hl" + toString(i);
while (Polygon::exists(polyID)) {
polyID = poi->getID() + "_hl" + toString(++i);
}
double lw = 0.;
double lyr = 0.;
if (MSNet::getInstance()->isGUINet()) {
lyr = poi->getShapeLayer();
lyr += (type + 1) / 257.;
}
Polygon::addHighlightPolygon(poiID, type, polyID, circle, col, true, "highlight", (int)lyr, lw);
double maxAttack = 1.0;
std::vector<double> timeSpan;
if (duration > 0.) {
timeSpan = {0, MIN2(maxAttack, duration / 3.), 2.*duration / 3., duration};
}
std::vector<double> alphaSpan;
if (alphaMax > 0.) {
alphaSpan = {0., (double) alphaMax, ((double) alphaMax) / 3., 0.};
}
Polygon::addDynamics(polyID, "", timeSpan, alphaSpan, false, false);
}
void
POI::setParameter(const std::string& poiID, const std::string& key, const std::string& value) {
PointOfInterest* p = getPoI(poiID);
p->setParameter(key, value);
}
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(POI, POI)
PointOfInterest*
POI::getPoI(const std::string& id) {
PointOfInterest* sumoPoi = MSNet::getInstance()->getShapeContainer().getPOIs().get(id);
if (sumoPoi == nullptr) {
throw TraCIException("POI '" + id + "' is not known");
}
return sumoPoi;
}
NamedRTree*
POI::getTree() {
if (myTree == nullptr) {
myTree = new NamedRTree();
ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
for (const auto& i : shapeCont.getPOIs()) {
const float cmin[2] = {(float)i.second->x(), (float)i.second->y()};
const float cmax[2] = {(float)i.second->x(), (float)i.second->y()};
myTree->Insert(cmin, cmax, i.second);
}
}
return myTree;
}
void
POI::cleanup() {
delete myTree;
myTree = nullptr;
}
void
POI::storeShape(const std::string& id, PositionVector& shape) {
shape.push_back(*getPoI(id));
}
std::shared_ptr<VariableWrapper>
POI::makeWrapper() {
return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
}
bool
POI::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
switch (variable) {
case TRACI_ID_LIST:
return wrapper->wrapStringList(objID, variable, getIDList());
case ID_COUNT:
return wrapper->wrapInt(objID, variable, getIDCount());
case VAR_TYPE:
return wrapper->wrapString(objID, variable, getType(objID));
case VAR_COLOR:
return wrapper->wrapColor(objID, variable, getColor(objID));
case VAR_POSITION:
return wrapper->wrapPosition(objID, variable, getPosition(objID));
case VAR_POSITION3D:
return wrapper->wrapPosition(objID, variable, getPosition(objID, true));
case VAR_WIDTH:
return wrapper->wrapDouble(objID, variable, getWidth(objID));
case VAR_HEIGHT:
return wrapper->wrapDouble(objID, variable, getHeight(objID));
case VAR_ANGLE:
return wrapper->wrapDouble(objID, variable, getAngle(objID));
case VAR_IMAGEFILE:
return wrapper->wrapString(objID, variable, getImageFile(objID));
case libsumo::VAR_PARAMETER:
paramData->readUnsignedByte();
return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
case libsumo::VAR_PARAMETER_WITH_KEY:
paramData->readUnsignedByte();
return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
default:
return false;
}
}
}