#include "Perception.h" #include "Angle.h" #include "DistanceSensor.h" #include "Switch.h" #include Perception::Perception() {} Perception::Perception(Placement *sensors, unsigned int sensorCount) { this->sensorCount = sensorCount; this->sensorPlacements = (Placement *)sensors; } unsigned int Perception::GetSensorCount() { return this->sensorCount; } Sensor *Perception::GetSensor(unsigned int sensorId) { if (sensorId >= this->sensorCount) return nullptr; Thing *thing = this->sensorPlacements[sensorId].thing; if (thing->IsSensor()) return (Sensor *)thing; return nullptr; } float Perception::GetDistance(float direction, float range) { float minDistance = INFINITY; if (range < 0) range = -range; for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) { Placement placement = sensorPlacements[sensorIx]; // This still needs support for angles wrapping around 180 degrees !!!! if (placement.horizontalDirection > direction - range && placement.horizontalDirection < direction + range) { Thing *thing = placement.thing; if (thing == nullptr) continue; if (thing->type == Thing::DistanceSensorType) { DistanceSensor *distanceSensor = (DistanceSensor *)thing; if (distanceSensor != nullptr && distanceSensor->ObjectNearby()) minDistance = fmin(minDistance, distanceSensor->GetDistance()); } } } return minDistance; } float Perception::GetDistance(float horizontalDirection, float verticalDirection, float range) { float minDistance = INFINITY; if (range < 0) range = -range; for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) { Placement placement = sensorPlacements[sensorIx]; // This still needs support for angles wrapping around 180 degrees !!!! if (placement.horizontalDirection > horizontalDirection - range && placement.horizontalDirection < horizontalDirection + range && placement.verticalDirection > verticalDirection - range && placement.verticalDirection < verticalDirection + range) { Thing *thing = placement.thing; if (thing == nullptr) continue; if (thing->type == Thing::DistanceSensorType) { DistanceSensor *distanceSensor = (DistanceSensor *)thing; if (distanceSensor != nullptr && distanceSensor->ObjectNearby()) minDistance = fmin(minDistance, distanceSensor->GetDistance()); } } } return minDistance; } bool Perception::ObjectNearby(float direction, float range) { if (range < 0) range = -range; for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) { Placement placement = sensorPlacements[sensorIx]; if (placement.horizontalDirection > direction - range && placement.horizontalDirection < direction + range) { Thing *thing = placement.thing; if (thing == nullptr) continue; if (thing->type == Thing::DistanceSensorType) { DistanceSensor *distanceSensor = (DistanceSensor *)thing; if (distanceSensor != nullptr && distanceSensor->ObjectNearby()) return true; } else if (thing->type == Thing::SwitchType) { Switch *switchSensor = (Switch *)thing; if (switchSensor != nullptr && switchSensor->IsOn()) return true; } } } return false; }