Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix appending features behind all the zeros occurring at coordinates … #508

Open
wants to merge 1 commit into
base: humble
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions depthai_bridge/src/TrackedFeaturesConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ void TrackedFeaturesConverter::toRosMsg(std::shared_ptr<dai::TrackedFeatures> in
msg.header.frame_id = _frameName;
msg.features.resize(inFeatures->trackedFeatures.size());

for(const auto& feature : inFeatures->trackedFeatures) {
depthai_ros_msgs::msg::TrackedFeature ft;
ft.header = msg.header;
ft.position.x = feature.position.x;
ft.position.y = feature.position.y;
ft.age = feature.age;
ft.id = feature.id;
ft.harris_score = feature.harrisScore;
ft.tracking_error = feature.trackingError;
msg.features.emplace_back(ft);
Comment on lines 34 to -45
Copy link

@wouterio wouterio Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What actually goes wrong here is that msg.features.resize(inFeatures->trackedFeatures.size()) expands the msg.features vector with value-initialized feature elements. That causes all the zero features mentioned in this issue. Then later on msg.features.emplace_back(ft) appends the actual features.

So replacing msg.features.resize with msg.features.reserve might solve the problem too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @wouterio, although vec[i] might be faster than emplace_back in some cases, I would leave it as is and only change the resize part

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He @Serafadam, thanks for addressing this!

Copy link

@wouterio wouterio Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also expect emplace_back to be the right choice here, because the link you mentioned concludes with this:

In principle, if the type stored in the vector is a simple type (not a class or a struct), the loop is often vectorizable and one could expect to see similar numbers as we have seen here. With larger classes, it is the opposite: vectorization doesn’t pay off because of the low memory efficiency, so the operator[] version will in principle slower.

Since the features we're inserting are class/struct types, so we can expect emplace_back to be faster. But of course:

as always with software performance, the runtime numbers are the ones that give the final verdict.

for(int i = 0; i < inFeatures->trackedFeatures.size(); ++i) {
msg.features[i].header = msg.header;
msg.features[i].position.x = inFeatures->trackedFeatures[i].position.x;
msg.features[i].position.y = inFeatures->trackedFeatures[i].position.y;
msg.features[i].age = inFeatures->trackedFeatures[i].age;
msg.features[i].id = inFeatures->trackedFeatures[i].id;
msg.features[i].harris_score = inFeatures->trackedFeatures[i].harrisScore;
msg.features[i].tracking_error = inFeatures->trackedFeatures[i].trackingError;
}
featureMsgs.push_back(msg);
}
Expand Down