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

[jsk_pcl_ros_utils][polygon_magnifier] Allow negative param value to shrink #2053

Merged
merged 1 commit into from
May 2, 2017

Conversation

furushchev
Copy link
Member

@furushchev furushchev commented Apr 17, 2017

  • allow negative value for magnify_distance param to shrink polygons.
  • updated docs with an image
  • fixed a bug to drop labels and likelihood.

@furushchev furushchev changed the title [jsk_pcl_ros_utils][polygon_magnifier] Allow negative param value to shrink [WIP][jsk_pcl_ros_utils][polygon_magnifier] Allow negative param value to shrink Apr 17, 2017
@furushchev furushchev changed the title [WIP][jsk_pcl_ros_utils][polygon_magnifier] Allow negative param value to shrink [jsk_pcl_ros_utils][polygon_magnifier] Allow negative param value to shrink Apr 17, 2017

ret_polygon_array.likelihood.reserve(msg->likelihood.size());
std::copy(msg->likelihood.begin(), msg->likelihood.end(),
std::back_inserter(ret_polygon_array.likelihood));
Copy link
Member

Choose a reason for hiding this comment

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

@furushchev Could you please explain what these lines do?

Copy link
Member Author

@furushchev furushchev Apr 20, 2017

Choose a reason for hiding this comment

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

@wkentaro Thanks for reviewing! What is done in this part is just to copy all elements of vector. Original code drops labels and likelihood from original message even if they have some values.

ref:

fixed a bug to drop labels and likelihood.

Copy link
Member

Choose a reason for hiding this comment

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

Why you can't firstly copy all elements from input msg?

Copy link
Member Author

Choose a reason for hiding this comment

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

@wkentaro Because I didn't know the way. Updated.

@furushchev
Copy link
Member Author

@wkentaro Thanks for very kind tutorials! Updated.

new_vertices[i] = (vertices_[i] - c).normalized() * distance + vertices_[i];
Eigen::Vector3f diff_vec;
if (distance > 0.0) diff_vec = vertices_[i] - c;
else diff_vec = c - vertices_[i];
Copy link
Member

Choose a reason for hiding this comment

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

distance is not used in this if block.
could u explain why?

Copy link
Member Author

Choose a reason for hiding this comment

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

@wkentaro instead abs(distance) is multiplied. Original code may make polygons which has opposite directed normals. (I could not know why though)

Copy link
Member

Choose a reason for hiding this comment

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

abs(distance) is out of if block, right?

Copy link
Member

Choose a reason for hiding this comment

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

I mean why the sign of distance decides the sign of diff_vec.

Copy link
Member Author

Choose a reason for hiding this comment

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

@wkentaro Please read the code, if distance is negative, we need to compute a vector of opposite direction.

Copy link
Member

@wkentaro wkentaro Apr 21, 2017

Choose a reason for hiding this comment

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

I asked @mmurooka for review.

Copy link
Member

@mmurooka mmurooka Apr 21, 2017

Choose a reason for hiding this comment

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

I double checked with @wkentaro ,
and this change is not necessary (i.e. this does not change behavior at all).
Because if distance is negative
diff_vec is c - vertices_[i], and diff_vec * abs(distance) becames diff_vec * (- distance) = (vertices_[i] - c) * distance.
This is same result with original source code.

Copy link
Member

Choose a reason for hiding this comment

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

abs returns integer. please use fabs.

Copy link
Member Author

Choose a reason for hiding this comment

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

this part was reverted and no abs is used any more. Thanks anyway.

@@ -0,0 +1,65 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
Copy link
Member

Choose a reason for hiding this comment

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

Please add license. BSD, MIT, or something.

Copy link
Member Author

Choose a reason for hiding this comment

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

But I could not find other nodes that have license header.
Even if this is preferred, I think we should create another PR to update it for all these nodes.

Copy link
Member

Choose a reason for hiding this comment

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

In other nodes, the license is estimated by package.xml (probably BSD), but you specify copyright, and I thought it was under another license.

Copy link
Member

Choose a reason for hiding this comment

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

If you write Author: ..., I can ignore it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, this line is automatically added by emacs. Removed.

@furushchev
Copy link
Member Author

furushchev commented Apr 21, 2017 via email

def publish(self, event):
# update timestamp
self.msg.header.stamp = event.current_real
for p in self.msg.polygons: p.header.stamp = event.current_real
Copy link
Member

Choose a reason for hiding this comment

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

Please add newline.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

Copy link
Member

Choose a reason for hiding this comment

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

Really?

# validate params
assert isinstance(param, list), "polygons must be list"

has_label = any(map(lambda p: "label" in p, param))
Copy link
Member

Choose a reason for hiding this comment

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

has_label = any('label' in p for p in param)

assert isinstance(param, list), "polygons must be list"

has_label = any(map(lambda p: "label" in p, param))
has_likelihood = any(map(lambda p: "likelihood" in p, param))
Copy link
Member

Choose a reason for hiding this comment

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

has_likelihood = any('likelihood' in p for p in param)

for polygon in param:
ps = PolygonStamped()
ps.header.frame_id = frame_id
ps.polygon.points = map(lambda v: Point32(v[0], v[1], v[2]), polygon["points"])
Copy link
Member

Choose a reason for hiding this comment

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

[Point32(*v) for v in polygon['points']]

@mmurooka
Copy link
Member

Yes, but result is different.(in original code, normal direction of plane is sometimes flipped) I don't know why. Do you know.??

I don't know. Please describe normal direction of plane is sometimes flipped in more detail.
Do you check that in Rviz or polygon instance of jsk_recognition_utils/geo in C++ code?
I think that distance < 0 and |distance| is larger than input polygon size, the clockwise/counterclockwise of magnified polygon vertices might be flipped and then normal flipped.

@mmurooka
Copy link
Member

I executed sample_polygon_magnifier.launch with this PR update, and it worked (normal was not flipped).
Then, I reverted

jsk_pcl_ros_utils/src/polygon_magnifier_nodelet.cpp
jsk_recognition_utils/src/geo/convex_polygon.cpp

to origin/master, then build and executed same sample launch, and it also worked (normal was not flipped).
I need rosbag to check more. Could you give rosbag (or could you check once again) please?

@mmurooka
Copy link
Member

Sorry for many reply comments.
I found one possible reason that normal is flipped.
The shrunk polygon might become non-convex even if the input polygon is convex because the polygon is shrunk with constant distance for all verticies (not ratio).
In the readme figure you add is good example. Green shrunk polygon is not convex if you see carefully.
If two edges of non-convex parts are used for calculating normal, normal is flipped from original. Is that make sense?

This can happen regardless of your change in magnifyByDistance. My guess is that your experimental environment was changed slightly at the same timing that you updated the code.

@mmurooka
Copy link
Member

master...mmurooka:polygon-magnifier-normal-flip reproduce normal flipping.

screenshot from 2017-04-22 02 24 00

@furushchev
Copy link
Member Author

furushchev commented Apr 22, 2017

@mmurooka You are totally right!! Thanks! I'll write a code to warn if breaks original convex hull.

Green shrunk polygon is not convex if you see carefully.
If two edges of non-convex parts are used for calculating normal, normal is flipped from original. Is that make sense?

Sorry, if I understand correctly, you mean Red? (not Green).

@furushchev
Copy link
Member Author

@wkentaro @mmurooka Updated thanks to your many advices. If you approve, I rebase all commits into one.

= poly.magnifyByDistance(magnify_distance_);

if (!magnified_poly->isConvex()) {
ROS_ERROR("Magnified polygon %ld is not convex.", i);
Copy link
Member

Choose a reason for hiding this comment

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

I think it is OK to push magnified polygon even if it is non-convex. In some cases, we want the result even if it is non-convex. Keeping length of polygon array is easy to manage.
How about following (just add warn message if non-convex)?

    jsk_recognition_msgs::PolygonArray ret_polygon_array = *msg;

    for (size_t i = 0; i < msg->polygons.size(); i++) {
      jsk_recognition_utils::ConvexPolygon poly = jsk_recognition_utils::ConvexPolygon::fromROSMsg(msg->polygons[i].polygon);
      jsk_recognition_utils::ConvexPolygon::Ptr magnified_poly = poly.magnifyByDistance(magnify_distance_);
      if (!magnified_poly->isConvex()) {
              ROS_WARN("Magnified polygon %ld is not convex.", i);
      }

      ret_polygon_array.polygons[i].polygon = magnified_poly->toROSMsg();
    }
    pub_.publish(ret_polygon_array);

@mmurooka
Copy link
Member

Sorry, if I understand correctly, you mean Red? (not Green).

I'm saying about this figure in this comment. https://github.com/furushchev/jsk_recognition/blob/69b0e4c079bc35200d0862cd8be400252899c15a/doc/jsk_pcl_ros_utils/nodes/images/polygon_magnifier.png
green polygon is a little bit non-convex.

@mmurooka
Copy link
Member

If keeping convex (and not flipping normal) is important for your use case, I recommend you to use magnify by scale in nodelet, and add rosparam to select magnifying by distance or scale.
https://github.com/jsk-ros-pkg/jsk_recognition/blob/master/jsk_recognition_utils/src/geo/convex_polygon.cpp#L261
This is just a comment, and you don't need to do this in this PR.

@furushchev
Copy link
Member Author

@mmurooka Thanks for advice, updated / rebased and yes, I'll send another pull request to support magnify by scale factor when we want.

@mmurooka
Copy link
Member

LGTM!

def publish(self, event):
# update timestamp
self.msg.header.stamp = event.current_real
for p in self.msg.polygons: p.header.stamp = event.current_real
Copy link
Member

Choose a reason for hiding this comment

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

Really?

@@ -14,4 +17,10 @@ Magnify polygons by specified length.
## Parameters
* `~magnify_distance` (Double, default: `0.2`)

Length to scale polygon
Length to scale polygons. Default value `0.2` means the distance of each corresponding edges will be `0.2` m.
Copy link
Member

Choose a reason for hiding this comment

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

Please describe that this nodelet supports negative value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated.
Sorry I added newline in different line.

…nify

[jsk_pcl_ros_utils][polygon_magnifier] update docs

[jsk_recognition_utils] add polygon_array_publisher.py / sample_polygon_array_publisher.launch

[jsk_pcl_ros_utils] add sample / test for polygon_magnifier
@furushchev
Copy link
Member Author

Hmm, failed on indigo...

20:29:28   Could NOT find Cython (missing: CYTHON_EXECUTABLE)

20:29:28 Call Stack (most recent call first):

20:29:28   /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:315 (_FPHSA_FAILURE_MESSAGE)

20:29:28   cmake/FindCython.cmake:42 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)

20:29:28   cmake/UseCython.cmake:74 (find_package)

20:29:28   CMakeLists.txt:47 (include)

@furushchev
Copy link
Member Author

OK. Test passed!

@k-okada k-okada merged commit 508c672 into jsk-ros-pkg:master May 2, 2017
@furushchev furushchev deleted the polygon-magnifier branch May 21, 2017 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants