|
| 1 | +// Copyright 2024 Robotec.AI |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <graph/NodesCore.hpp> |
| 16 | +#include <scene/Scene.hpp> |
| 17 | + |
| 18 | +RadarTrackObjectsNode::RadarTrackObjectsNode() { fieldData.emplace(XYZ_VEC3_F32, createArray<HostPinnedArray>(XYZ_VEC3_F32)); } |
| 19 | + |
| 20 | +void RadarTrackObjectsNode::setParameters(float distanceThreshold, float azimuthThreshold, float elevationThreshold, |
| 21 | + float radialSpeedThreshold) |
| 22 | +{ |
| 23 | + this->distanceThreshold = distanceThreshold; |
| 24 | + this->azimuthThreshold = azimuthThreshold; |
| 25 | + this->elevationThreshold = elevationThreshold; |
| 26 | + this->radialSpeedThreshold = radialSpeedThreshold; |
| 27 | +} |
| 28 | + |
| 29 | +void RadarTrackObjectsNode::validateImpl() { IPointsNodeSingleInput::validateImpl(); } |
| 30 | + |
| 31 | +void RadarTrackObjectsNode::enqueueExecImpl() |
| 32 | +{ |
| 33 | + xyzHostPtr->copyFrom(input->getFieldData(XYZ_VEC3_F32)); |
| 34 | + distanceHostPtr->copyFrom(input->getFieldData(DISTANCE_F32)); |
| 35 | + azimuthHostPtr->copyFrom(input->getFieldData(AZIMUTH_F32)); |
| 36 | + elevationHostPtr->copyFrom(input->getFieldData(ELEVATION_F32)); |
| 37 | + radialSpeedHostPtr->copyFrom(input->getFieldData(RADIAL_SPEED_F32)); |
| 38 | + |
| 39 | + const int detectionsCount = input->getPointCount(); |
| 40 | + |
| 41 | + objectStates.clear(); // TODO(Pawel): Remove this when we start working on tracking. |
| 42 | + |
| 43 | + // Top level in this list is for objects. Bottom level is for detections that belong to specific objects. Below is an initialization of a helper |
| 44 | + // structure for region growing, which starts with a while-loop. |
| 45 | + std::list<std::list<int>> objectIndices; |
| 46 | + for (auto i = 0; i < detectionsCount; ++i) { |
| 47 | + objectIndices.emplace_back(1, i); |
| 48 | + } |
| 49 | + |
| 50 | + auto leftIndex = 0; |
| 51 | + while (leftIndex < objectIndices.size() - 1) { |
| 52 | + auto ownIt = objectIndices.begin(); |
| 53 | + std::advance(ownIt, leftIndex); |
| 54 | + auto checkedIt = std::next(ownIt); |
| 55 | + bool reorganizationTookPlace = false; |
| 56 | + |
| 57 | + while (checkedIt != objectIndices.end()) { |
| 58 | + auto& checkedIndices = *checkedIt; |
| 59 | + |
| 60 | + // TODO(Pawel): In theory, I do not have to check the same indices multiple times when growing this subset in next iterations. |
| 61 | + for (auto ownDetectionId : *ownIt) { |
| 62 | + const auto isPartOfSameObject = [&, distance = distanceHostPtr->at(ownDetectionId), |
| 63 | + azimuth = azimuthHostPtr->at(ownDetectionId), |
| 64 | + elevation = elevationHostPtr->at(ownDetectionId), |
| 65 | + radialSpeed = radialSpeedHostPtr->at(ownDetectionId)](int checkedDetectionId) { |
| 66 | + return std::abs(distanceHostPtr->at(checkedDetectionId) - distance) <= distanceThreshold && |
| 67 | + std::abs(azimuthHostPtr->at(checkedDetectionId) - azimuth) <= azimuthThreshold && |
| 68 | + std::abs(elevationHostPtr->at(checkedDetectionId) - elevation) <= elevationThreshold && |
| 69 | + std::abs(radialSpeedHostPtr->at(checkedDetectionId) - radialSpeed) <= radialSpeedThreshold; |
| 70 | + }; |
| 71 | + |
| 72 | + if (std::any_of(checkedIndices.cbegin(), checkedIndices.cend(), isPartOfSameObject)) { |
| 73 | + ownIt->splice(ownIt->cend(), checkedIndices); |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + if (checkedIndices.empty()) { |
| 78 | + checkedIt = objectIndices.erase(checkedIt); |
| 79 | + reorganizationTookPlace = true; |
| 80 | + } else { |
| 81 | + ++checkedIt; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (!reorganizationTookPlace) { |
| 86 | + // We are done with growing this object, no more matching detections are available. |
| 87 | + ++leftIndex; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + fieldData[XYZ_VEC3_F32]->resize(objectIndices.size(), true, false); |
| 92 | + auto* xyzPtr = static_cast<Vec3f*>(fieldData[XYZ_VEC3_F32]->getRawWritePtr()); |
| 93 | + uint32_t objectIndex = 0; |
| 94 | + |
| 95 | + for (const auto& separateObjectIndices : objectIndices) { |
| 96 | + auto objectCenter = Vec3f(0.0f, 0.0f, 0.0f); |
| 97 | + for (const auto index : separateObjectIndices) { |
| 98 | + objectCenter += xyzHostPtr->at(index); |
| 99 | + } |
| 100 | + |
| 101 | + auto& objectState = objectStates.emplace_back(); |
| 102 | + objectState.id = objectIndex++; |
| 103 | + objectState.framesCount = 1; // This will be updated with object tracking introduction. |
| 104 | + objectState.creationTime = static_cast<uint32_t>(Scene::instance().getTime().value_or(Time::zero()).asMilliseconds()); |
| 105 | + objectState.objectStatus = ObjectStatus::New; |
| 106 | + objectState.movementStatus = |
| 107 | + MovementStatus::Invalid; // Newly created object does not have velocity data (no 'previous frame' exists). |
| 108 | + objectState.position.addSample(objectCenter / static_cast<float>(separateObjectIndices.size())); |
| 109 | + |
| 110 | + // TODO(Pawel): This will require modyfing radar postprocess node - I will require some bounding box here. |
| 111 | + objectState.orientation.addSample(0.0f); |
| 112 | + objectState.absVelocity.addSample(Vec2f(0.0f, 0.0f)); |
| 113 | + objectState.relVelocity.addSample(Vec2f(0.0f, 0.0f)); |
| 114 | + objectState.absAccel.addSample(Vec2f(0.0f, 0.0f)); |
| 115 | + objectState.relAccel.addSample(Vec2f(0.0f, 0.0f)); |
| 116 | + objectState.orientationRate.addSample(0.0f); |
| 117 | + objectState.length.addSample(0.0f); |
| 118 | + objectState.width.addSample(0.0f); |
| 119 | + |
| 120 | + // TODO(Pawel): Be careful with indexing here, when working on tracking - id will not match the index for not new objects. |
| 121 | + xyzPtr[objectState.id] = 1 / static_cast<float>(separateObjectIndices.size()) * objectCenter; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +std::vector<rgl_field_t> RadarTrackObjectsNode::getRequiredFieldList() const |
| 126 | +{ |
| 127 | + return {XYZ_VEC3_F32, DISTANCE_F32, AZIMUTH_F32, ELEVATION_F32, RADIAL_SPEED_F32}; |
| 128 | +} |
0 commit comments