Fit a curve in angle line? - c++

I have two lines that start and end at random locations on a screen and create an angle. I then have an object follow these two lines. However at the intersection between the first and second line, the object rapidly rotates to go down the second line. And I don't want this.
So what I want to do is be able to create a curved version of this line that would have a more of a U at the intersection rather then a hard turn. I looked into curve fitting papers and can't seem to find that that would allow me to create a U out of a V.
Sorry for the terrible images... I want to take the one of the left, and generate the one on the right (same start, end, and intersection points). Another example, http://en.wikipedia.org/wiki/Curve_fitting
Any ideas?

You should take a look at http://en.wikipedia.org/wiki/Bezier_curve
Or just http://upload.wikimedia.org/wikipedia/commons/thumb/2/2d/Bezier_2_big.gif/240px-Bezier_2_big.gif
If the coordinates of the start/end points of the two lines are known, you can simply calculate an bezier curve follows the methods in the link above.
If not (for example with an bitmap like what you post), you can do Hough Transform first to extract the coordinates

Related

Project a line segment onto a mesh

What are the graphic/mathematical algorithms I have to look for in order to achieve the red line in the following image?
Explaining it better: I need to plot two points on the mesh and then generate a straight line segment from one point to the next. This line segment would be formed by new vertices created on every single edge in its way.
I'm currently working with CGAL and Libigl, but none of them seem to have the solution. I have tried CGAL::Surface_mesh_shortest_path but it adds too much overhead (code runs very slowly) and the line would not be guaranteed to be straight depending on the mesh deformation.
Ignoring whatever you mean as "straight", here is one simple algorithm I can think of that would produce images to the one similar shown in the question. There is no guarantee of what is produced being the shortest path. I'm just spitballing here with no knowledge on the topic, there are probably better ways.
Pick 4 variables:
The starting point
The ending point
The line's normal
A marching constant
Let's calculate a few constants from the variables:
Direction = ending point - starting point
Increment vector = normalize(Direction) * marching constant.
Begin from the starting point and march towards your ending point by some constant, checking above and below your current position for where you are on the mesh. You use the line's normal to understand the "up" and "down" directions in order to perform intersection tests.
On each intersection test, if you do not intersect for both the up and down directions, then the normal you chose will not work for the given two points and mesh, and you'll have to try a different normal. If you do end up intersecting from one of the directions, you will need to add 2 points to your final line: a point on the calculated direction line closest to the start that lies on the triangle, and a point on the calculated direction line farthest from the start that lies on the triangle. If there's both an intersection on the up and down directions, choose the up direction to work with.

Determine Orientation of a Sketched Arrow with OpenCV

I've got a bunch of arrow sketches and I want to determine their orientation, the starting point coordinates, and the ending point coordinates.
I'm using goodFeaturesToTrack but I'm really clueless as to what to do next.
This is what I've got
There is no restriction on the shape of the arrows, they can be as windy as you want them or they could be composed of straight lines and right angles.
I'd really appreciate any ideas because I really am lost. The project is about transforming a sketch of an Finite State Machine into a VHDL code and I've got the circles and characters covered so this is the last component but I have no idea how to approach the problem.
Instead of using goodFeaturesToTrack you can use HoughLines which will give you all lines in an image so I suggest that you do some segmentation first to get each arrow separately. HoughLines gives you all lines in polar coordinates which is perfect for you case as you can know the orientation of the lines. You can read more here: https://docs.opencv.org/3.4.0/d9/db0/tutorial_hough_lines.html
For straight lines your task is easy you need to find 3 line segments that intersect in one point (http://answers.opencv.org/question/92164/how-to-find-coordinates-of-intersection-of-lines-after-using-houghlines/). This will be the end point of your arrow, you can find the start point using the equation of the middle line.
For curved lines, it is a bit more tricky as you may get several hough lines for the curved segment or none at all. So in this case I suggest you get the intersection of the the 2 lines as your end point and then use goodFeaturesToTrack then starting from the closest point to your end point start moving to the next closer until you reach the end and pick that as the starting point.
I do not know if this would be the best solution and I did not try it myself but I hope it helps you or give you some direction.

C++ How to calculate an arc between two 3D points

I read through the forum and as I am sure this question has been asked before, but I couldn't really find what I was looking for.
My problem is the following:
I have an AI-Character moving along a spline. Should that path be blocked, the character should move in an arc around it and then continue on it's path.
For arguments sake lets assume that the spline has a length of 7000 units.
Therefore, I have two 3D (x,y,z) vectors. The first vector is the current position of the AI-bot and the second vector the position past the obstacle. For the time being lets just say: current spline position + 400 units; later on I could do a line trace to get the dimension of the obstacle etc. but for now I don't care about it.
Now I would like to compute an alternative path to avoid aforementioned obstacle - hence compute the arc between these two points - How do I do this?
I am really terrible at maths but looked at projectile trajectory because I thought that it would be sort of the same, just was unable to really understand it :<
It doesn't have to be an arc. You can solve this problem recursively in a very simple way.
Consider you're at position A, and the obstacle is at position B. You can do the following moves:
From current position to A+V(B[x]+height(B),0,0)
From current position to A+V(0,B[y]+width(B),0)
From current position to A+V(B[x]-height(B),0,0)
where V is a vector with components V(x,y,z), width(B) is the width of the obstacle and B[x] is the x component of the position of B. This way you moved around it along a rectangle. You can now smoothen the path by subdividing that rectangle in halves. 3 subdivisions are enough to make this smooth enough. To subdivide, take the middle point the first path, and draw a line to the middle of the second path. The same you do from the second path to the third one, and now your rectangle becomes an octagon. If that's not smooth enough, do a few more steps. This will create a new spline that you can use.
I would look at a combination of splines and the EQS system. The spline defines the ideal path to follow. The EQS system finds locations near or on the path, while still doing obstacle avoidance. EQS can return all valid destinations so you can manually order them by custom critera.
Actors set on a spline do work, but there's a whole bunch o' mess when making them stop following a spline, creating a new one at the correct point, attaching the actor the new spline, and so on.
I arrived at this conclusion yesterday after exactly going the messy way of adding spline points etc. The only problem i see is that I find the EQS system very difficult to understand. Not following the examples as such, but modifying it in the way I need it. Lets see, i keep you posted.

How to split a general closed polygon by a line segment

I need a good (robust) algorithm for splitting a polygon into two sets(left/right) by a line segment. My polygon representation is simply a list of integer coordinates(ordered clock wise, never self intersecting) and the line segment is represented by a start and end point. The line always starts and ends outside the polygon, i.e. intersects the polygon an even number of times.
Here is an example:
The output of the algorithm should be the two sets(travelling clock wise):
Left: HABCH, FGDEF
Right: HCDGH, BAB, FEF
I can identify the points A-H by iterating the polygon and checking if a polygon segment crosses the line, taking care to respect border cases. I can also determine which side each multi-line belongs to. I cannot though, for the life of me, decide how to string these segment together.
Before you suggest a general purpose clipping library: I am using boost polygon which is very good at clipping polygons against each other, but I haven't found any library which let's you clip a polygon against a line segment and it is not possible in general to turn the line segment into a polygon which I could clip with.
EDIT: I had missed FEF and the fact that a polygon can have parts on both sides of the line segment.
Ok, here is a rather simple recipe of how to arrive at the answer:
Start with the set of intersection points ordered by traveling the contour clockwise:
ABCDEFGH
Sort them according to distance from the start of line:
HCFEDGBA
We also need to remember for each point if it is a left-to-right or right-to-left intersection.
Start with any point. Let's say G. Follow the contour clockwise and add GH
to the current polygon.
Now we need to travel along the line. The
direction depends on which side of the line we are. We are on the
right side, so we need to pick the value to the right of H in the
sorted set: C. Add HC to the current polygon.
Follow the contour clockwise and add CD to the current polygon.
We are on the right side, so we need to pick the value to the right of D in the sorted set: G. Add DG to the current polygon.
We have now reached the
starting point, so let's save the polygon(GHCDG) and remove used
points from the list.
Start over with another point.
For each intersection of the polygon border with the line segment:
Add a new point to the polygon.
Remember the new points in a new-point set.
Add the original polygon to the polygon set.
For each pair of points in the new-point set:
For each polygon in the current polygon set:
If the line segment between the points is completely inside the polygon.
Replace the polygon in the polygon set with two polygons
generated by dividing the original polygon along the line
segment between the points.
For each polygon in the polygon set:
Add it to the Left result set or the Right result set.
(Note this may not be possible.
Consider your example of the segment starting between C and F:
You will end up with a polygon (GABCFG) that touches both
sides of the dividing segment. Is that a Left or a Right?
I've solved something similar once and I gave up trying to be clever.
Run round all the vertices making them into connected line segments,
starting a new segment with a new point every time you intersect the
cutting line.
Find all segments which share an end point and join them back up into one longer one.
Connect all the open ends.

RANSAC line3d fitting by 3d line segments

I am having many 3d line segments. some of them are nearly parallel
and some are oriented in to different direction. I want to avoid
outliers and to get the best line 3d to represent the given 3d line
segments.
I am bit confused how RANSAC method apply for this case...
should i find a random line first or should i consider this as a given 3d point problem.?
can anyone post me the stucture to be followed when implenting this in c++. thanks
RANSAC is a good tool to fit data to a model. If you had a single 3D line in a collection of segments, by running RANSAC and selecting the line that maximized the amount of inliers would be enough. However, since you have many lines in the collection, you should try a different approach (even a non-RANSAC one, as I tell you later).
For example, you can run first RANSAC trying to find the line that matches as many segments as possible. After finding that line, remove the inlier segments from the collection and run RANSAC again.
To create a line, you only need a segment, so building the line model is quite easy.
To decide on whether a segment fits a line, you may compute the angle between both with the dot product (the closer to 0 the better) and the distance from the middle point of the segment to the line.
Also note that you can filter out very small segments at as first step. You could save some iterations later and avoid noisy results.
I can think of a Hough transform approach as well. Since you can create a line from each segment, you can get the parameters of its line (normal or directional vector and distance to origin), quantize them to some acceptable bin-size and add a vote to those parameters in a matrix. Finally, your lines lie in the peaks of the vote matrix.