OSMDroid - Check if Polyline circle contain a GeoPoint - osmdroid

I am trying to figure out how to detect if a Geopoint is within a polyline radius
Polyline circle = new Polyline(mMapView);
circle.setPoints(Polygon.pointsAsCircle(new Geopoint(123, 456),
2000.0));
circle.getOutlinePaint().setColor(Color.rgb(0, 255, 0));
mMapView.getOverlays().add(circle);
//
MotionEvent parameters
long downTime = android.os.SystemClock.uptimeMillis();
long eventTime = android.os.SystemClock.uptimeMillis();
int action = MotionEvent.ACTION_DOWN;
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action,
(float)mMapView.getProjection().toProjectedPixels(transport.GetRoute().get(ii),
null).x,
(float)(float)mMapView.getProjection().toProjectedPixels(transport.GetRoute().get(ii),
null).y, 0);
circle.contains(event);
But it gives me this error on the circle.contains(event):
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
android.graphics.Path.isEmpty()' on a null object reference at
org.osmdroid.views.overlay.PolyOverlayWithIW.contains(PolyOverlayWithIW.java:565)
Anyone has any idea how to solve this ? Is there another way to check if a geopoint is within a polyline circle with OSMDroid?
I tried using the toPixels to convert latitude and longitude to screenpixels but this approach doesn~t work for me as I want to detect even even if a geopoint is offscreen
Thks in advance

Related

How are you supposed to use a controller with a URDF

I'm trying to use the LQR Controller to move the wheels on this really simple URDF linked here
I'm trying to understand how to use the LQR controller for a model loaded from a URDF so this is a mix of the LQR example and the PR2 Example
systems::DiagramBuilder<double> builder;
auto pair = AddMultibodyPlantSceneGraph(
&builder,
std::make_unique<MultibodyPlant<double>>(
FLAGS_mbp_discrete_update_period));
MultibodyPlant<double>& plant = pair.plant;
const std::string full_name ="doublependulum/fetch/urdf/freight.urdf";
auto parser = multibody::Parser(&plant);
parser.package_map().PopulateFromFolder("/root/");
parser.AddModelFromFile(full_name);
// Add model of the ground.
const double static_friction = 0.5;
const Vector4<double> green(0.5, 1.0, 0.5, 1.0);
plant.RegisterVisualGeometry(plant.world_body(), RigidTransformd(),
geometry::HalfSpace(), "GroundVisualGeometry",
green);
// For a time-stepping model only static friction is used.
const multibody::CoulombFriction<double> ground_friction(static_friction,
static_friction);
plant.RegisterCollisionGeometry(plant.world_body(), RigidTransformd(),
geometry::HalfSpace(),
"GroundCollisionGeometry", ground_friction);
plant.Finalize();
plant.set_penetration_allowance(FLAGS_penetration_allowance);
// Set the speed tolerance (m/s) for the underlying Stribeck friction model
// For two points in contact, this is the maximum allowable drift speed at the
// edge of the friction cone, an approximation to true stiction.
plant.set_stiction_tolerance(FLAGS_stiction_tolerance);
const drake::multibody::Body<double>& base = plant.GetBodyByName("base_link");
ConnectContactResultsToDrakeVisualizer(&builder, plant);
geometry::DrakeVisualizer::AddToBuilder(&builder, pair.scene_graph);
auto diagram = builder.Build();
/// Need to add actuators to the models in this form
// Create a context for this system:
std::unique_ptr<systems::Context<double>> diagram_context =
diagram->CreateDefaultContext();
systems::Context<double>& plant_context =
diagram->GetMutableSubsystemContext(plant, diagram_context.get());
Eigen::MatrixXd Q(2, 2);
Q << 10, 0, 0, 1;
Eigen::MatrixXd R(1, 1);
R << 1;
auto controller =
builder.AddSystem(systems::controllers::LinearQuadraticRegulator(
plant, plant_context, Q, R));
builder.Connect(plant.get_state_output_port(),
controller->get_input_port());
builder.Connect(controller->get_output_port(), plant.get_input_port());
However, I get a runtime error:
what(): System::FixInputPortTypeCheck(): expected value of type drake::geometry::QueryObject<drake::AutoDiffXd> for input port 'geometry_query' (index 0) but the actual type was drake::geometry::QueryObject<double>. (System ::plant)
Can someone explain how to use the LQR controller for a Model
There are a few problems here. The first, and the one generating the error, is the fact that you've passed a plant_context (only) into the LinearQuadraticRegulator. Because your system has collision geometry, you need a SceneGraph to be connected for the dynamics to be evaluated. You would want to pass the diagram containing both the MultibodyPlant and the SceneGraph into the LQR call.
But the real problem is going to be deeper than that. LQR is not going to work out of the box for you on this model. That's not a Drake issue, it's a math issue. The system you've described not only has collision dynamics, but also is not going to be controllable in the linearization. When people use LQR to stabilizing wheeled robots, they do it in a minimal coordinates which assumes that the wheels are attached to the ground at a point. In drake, that would mean writing your own LeafSystem with the vehicle dynamics.

Q3DSurface selected point signal

I am using QtDataVisualization (Q3DSurface in particular) to make a simple 3D surface graph.
The Q3DSurface supports selection of a point on the graph by showing a highlighted ball on the data point where the user has clicked. The selection pointer shows the coordinates of the point. It looks like this: surface with selected point
However, I'm not able to find a signal that is emitted when the selection happens. Having read through the documentation of Q3DSurface and QSurface3DSeries, I failed to find any corresponding signal. There is only a selectedPointChanged(const QPoint &position) in QSurface3DSeries, but it operates with a two-dimensional QPoint which is not suitable for the case.
What I am trying to do is store a history of selected points, that is why I need such a signal to keep track of previous coordinates. I tried looking into implementing a custom Q3DInputHandler, but not sure that it can resolve the issue. I would be grateful for any advice on the solution.
The desired signal is itemLabelChanged(const QString &label) in QAbstract3DSeries
It is emitted when a label, which represents the chosen point's coordinates, is changed. By connecting this signal to the slot, it is possible to retrieve the label's text (const QString &label parameter).
A good solution can be found here
Briefly, it is possible to get the 3D position by using const QSurfaceDataItem *QSurfaceDataProxy::itemAt(const QPoint &position) being position the value provided by the QAbstract3DGraph signal selectedElementChanged(QAbstract3DGraph::ElementType type).
For example:
Q3DSurface* surface = new Q3DSurface();
surface->setSelectionMode(QAbstract3DGraph::SelectionRowAndColumn);
QSurface3DSeries* surfaceSeries = new QSurface3DSeries();
surfaceSeries->dataProxy()->resetArray(dataArray); // QSurfaceDataArray* dataArray containing the surface data
surface->addSeries(surfaceSeries);
QObject::connect(surfaceSeries, &QSurface3DSeries::selectedPointChanged, this, [surfaceSeries](const QPoint &pos)
{
if ((pos.x() >= 0) && (pos.y() >= 0)) {
QVector3D vector = surfaceSeries->dataProxy()->itemAt(pos)->position();
qDebug() << vector;
}
});

How to appropriately get position of QGraphicsRectItem after drag-release?

I wanted to have an online monitoring system that could tell where the shape is currently, but am getting very weird coordinates of the item, also the dimensions of it get higher by 1 each time I create new one and drag it.
Initial position (map size is 751 by 751, checked by outputting to qDebug(), scene bound to yellow space) :
Dragging it to the left top corner.
As you can see in the beginning it was on (200;200), but after dragging it is on (-201;-196). After deleting it and creating new shape on the same position with the same properties, new shape can't be seen because it is outside of the map, which suggests that edits don't show correct data.
Here is the code of updating the edits:
void CallableGraphicsRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsRectItem::mouseReleaseEvent(event);
ptr->updateEdits(this);
}
Here is what I managed to cut down into updateEdits():
void MainWindow::updateEdits(QAbstractGraphicsShapeItem* item)
{
//stuff not related to scene
auto posReal = item->scenePos();
auto pos = posReal.toPoint();
//create QString from coordinates
QString coordinate;
coordinate.setNum(pos.x());
ui->leftXEdit->setText(coordinate);
coordinate.setNum(pos.y());
ui->upperYEdit->setText(coordinate);
//get width and height for rect, radius for circle
auto boundingRectReal = item->sceneBoundingRect();
auto boundingRect = boundingRectReal.toRect();
ui->widthEdit->setText(QString::number(boundingRect.width()));
//disables height edit for circles, not really relevant
if (!items[currentShapeIndex].isRect)
{
ui->heightEdit->setDisabled(true);
}
else
{
ui->heightEdit->setDisabled(false);
ui->heightEdit->setText(QString::number(boundingRect.height()));
}
}
Here is how I anchor the QGraphicsScene to the left top corner of the yellow area:
scene->setSceneRect(0, 0, mapSize.width() - 20, mapSize.height() - 20);
ui->graphicsView->setScene(scene);
How can I report the right data to the edits?
You're better off overriding the itemChange method and using the ItemPositionHasChanged notification. You have to set the ItemSendsGeometryChanges flag on the item so that it receives these notifications.
I'm not sure that your item's final position has been set when you're still in the mouseReleaseEvent method. Tracking it in itemChange will ensure that the data is valid, and this kind of thing is what it's for.
Also, note that "pos" is in the item's parent coordinates, and "boundingRect" is in the item's coordinate space. You should use "scenePos" and "sceneBoundingRect" if you want to be sure you're using scene coordinates. If the item doesn't have a parent, then "pos" and "scenePos" will return the same values, but "boundingRect" and "sceneBoundingRect" will generally differ.

Histogram with QWT in Microsoft Visual Studio and Qt addin which closes immediately

I am using Qt add in for Visual Studio C++ for my GUI.
And on my GUI, I have a button called the plotButton which will draw an histogram of the image when clicked. My plotting option is via the usage of QWT.
However, it does not seem to be plotting anything and closes almost immediately. Tried sleep(), but it doesn't seem to work either. Could the problem be with my code?
Here is my code for reference:
void qt5test1 ::on_plotButton_clicked()
{
//Convert to grayscale
cv::cvtColor(image, image, CV_BGR2GRAY);
int histSize[1] = {256}; // number of bins
float hranges[2] = {0.0, 255.0}; // min andax pixel value
const float* ranges[1] = {hranges};
int channels[1] = {0}; // only 1 channel used
cv::MatND hist;
// Compute histogram
calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
double minVal, maxVal;
cv::minMaxLoc(hist, &minVal, &maxVal);//Locate max and min values
QwtPlot plot; //Create plot widget
plot.setTitle( "Plot Demo" ); //Name the plot
plot.setCanvasBackground( Qt::black ); //Set the Background colour
plot.setAxisScale( QwtPlot::yLeft, minVal, maxVal ); //Scale the y-axis
plot.setAxisScale(QwtPlot::xBottom,0,255); //Scale the x-axis
plot.insertLegend(new QwtLegend()); //Insert a legend
QwtPlotCurve *curve = new QwtPlotCurve(); // Create a curve
curve->setTitle("Count"); //Name the curve
curve->setPen( Qt::white, 2);//Set colour and thickness for drawing the curve
//Use Antialiasing to improve plot render quality
curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
/*Insert the points that should be plotted on the graph in a
Vector of QPoints or a QPolgonF */
QPolygonF points;
for( int h = 0; h < histSize[0]; ++h) {
float bin_value = hist.at<float>(h);
points << QPointF((float)h, bin_value);
}
curve->setSamples( points ); //pass points to be drawn on the curve
curve->attach( &plot ); // Attach curve to the plot
plot.resize( 600, 400 ); //Resize the plot
plot.replot();
plot.show(); //Show plot
Sleep(100);
}
Upon clicking this button after loading the image, a window appear and disappear immediately. Under the output window, the lines can be found.
First-chance exception at 0x75d54b32 (KernelBase.dll) in qt5test1.exe: Microsoft C++ exception: cv::Exception at memory location 0x0044939c..
Unhandled exception at 0x75d54b32 (KernelBase.dll) in qt5test1.exe: Microsoft C++ exception: cv::Exception at memory location 0x0044939c..
Does anybody have any idea what could be wrong with my code? Thanks. Please note once again that the program is build, and written within Microsoft Visual Studio C++. Thanks.
The problem is that you are constructing a stack object here:
QwtPlot plot; //Create plot widget
That is true that you are trying to show the plot at the end of the method, but the show() method is not blocking with an event loop like the QDialog classes when you use the exec() call on them.
It would be processed, but you are leaving the scope right after the call either way.
There are several ways of addressing this issue, but I would strive for the Qt parent/child hierarchy where the deletion will come automatically when using pointers.
1) Qt parent/child relation
QwtPlot *plot = new QwtPlot(this);
^^^^
2) Make "plot" a class member
plot.show();
and construct it in the class constructor.
3) Use a smart pointer
QSharedPointer<QwtPlot> plot = QSharedPointer<QwtPlot>(new QwtPlot());
It depends on your further context of the class which way to pick up, so try to understand these approaches, and take your peek.
plot should be created using new. Now you create it on stack, so it will be deleted immediately when on_plotButton_clicked function finished. Sleep should not be used here, you won't get any good from it.
QwtPlot* plot = new QwtPlot();
plot->setTitle( "Plot Demo" ); //Name the plot
//...
plot->show(); //Show plot
The problem might be that your QwtPlot is just a local variable and even because the sleep is also in the main thread you won't be able to draw it in time before the function returns. Then when it finish sleeping it destroys your local QwtPlot object and returns, so you are luck enough if you get a blink of the window like that.
To make it work you will have to call it like this:
QwtPlot* plot = new QwtPlot(this);
where this is the parent window that will host your plot (if any). That way your widget will remain alive until you close it or its parent destroy it at the end of the program execution.

Box2D creating rectangular bounding boxes around angled line bodies

I'm having a lot of trouble detecting collisions in a zero-G space game. Hopefully this image will help me explain:
http://i.stack.imgur.com/f7AHO.png
The white rectangle is a static body with a b2PolygonShape fixture attached, as such:
// Create the line physics body definition
b2BodyDef wallBodyDef;
wallBodyDef.position.Set(0.0f, 0.0f);
// Create the line physics body in the physics world
wallBodyDef.type = b2_staticBody; // Set as a static body
m_Body = world->CreateBody(&wallBodyDef);
// Create the vertex array which will be used to make the physics shape
b2Vec2 vertices[4];
vertices[0].Set(m_Point1.x, m_Point1.y); // Point 1
vertices[1].Set(m_Point1.x + (sin(angle - 90*(float)DEG_TO_RAD)*m_Thickness), m_Point1.y - (cos(angle - 90*(float)DEG_TO_RAD)*m_Thickness)); // Point 2
vertices[2].Set(m_Point2.x + (sin(angle - 90*(float)DEG_TO_RAD)*m_Thickness), m_Point2.y - (cos(angle - 90*(float)DEG_TO_RAD)*m_Thickness)); // Point 3
vertices[3].Set(m_Point2.x, m_Point2.y); // Point 3
int32 count = 4; // Vertex count
b2PolygonShape wallShape; // Create the line physics shape
wallShape.Set(vertices, count); // Set the physics shape using the vertex array above
// Define the dynamic body fixture
b2FixtureDef fixtureDef;
fixtureDef.shape = &wallShape; // Set the line shape
fixtureDef.density = 0.0f; // Set the density
fixtureDef.friction = 0.0f; // Set the friction
fixtureDef.restitution = 0.5f; // Set the restitution
// Add the shape to the body
m_Fixture = m_Body->CreateFixture(&fixtureDef);
m_Fixture->SetUserData("Wall");[/code]
You'll have to trust me that that makes the shape in the image. The physics simulation works perfectly, the player (small triangle) collides with the body with pixel perfect precision. However, I come to a problem when I try to determine when a collision takes place so I can remove health and what-not. The code I am using for this is as follows:
/*------ Check for collisions ------*/
if (m_Physics->GetWorld()->GetContactCount() > 0)
{
if (m_Physics->GetWorld()->GetContactList()->GetFixtureA()->GetUserData() == "Player" &&
m_Physics->GetWorld()->GetContactList()->GetFixtureB()->GetUserData() == "Wall")
{
m_Player->CollideWall();
}
}
I'm aware there are probably better ways to do collisions, but I'm just a beginner and haven't found anywhere that explains how to do listeners and callbacks well enough for me to understand. The problem I have is that GetContactCount() shows a contact whenever the player body enters the purple box above. Obviously there is a rectangular bounding box being created that encompasses the white rectangle.
I've tried making the fixture an EdgeShape, and the same thing happens. Does anyone have any idea what is going on here? I'd really like to get collision nailed so I can move on to other things. Thank you very much for any help.
The bounding box is an AABB (axis aligned bounding box) which means it will always be aligned with the the Cartesian axes. AABBs are normally used for broadphase collision detection because it's a relatively simple (and inexpensive) computation.
You need to make sure that you're testing against the OBB (oriented bounding box) for the objects if you want more accurate (but not pixel perfect, as Micah pointed out) results.
Also, I agree with Micah's answer that you will most likely need a more general system for handling collisions. Even if you only ever have just walls and the player, there's no guarantee that which object will be A and which will be B. And as you add other object types, this will quickly unravel.
Creating the contact listener isn't terribly difficult, from the docs (added to attempt to handle your situation):
class MyContactListener:public b2ContactListener
{
private:
PlayerClass *m_Player;
public:
MyContactListener(PlayerClass *player) : m_Player(player)
{ }
void BeginContact(b2Contact* contact)
{ /* handle begin event */ }
void EndContact(b2Contact* contact)
{
if (contact->GetFixtureA()->GetUserData() == m_Player
|| contact->GetFixtureB()->GetUserData() == m_Player)
{
m_Player->CollideWall();
}
}
/* we're not interested in these for the time being */
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{ /* handle pre-solve event */ }
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{ /* handle post-solve event */ }
};
This requires you to assign m_Player to the player's fixture's user data field. Then you can use the contact listener like so:
m_Physics->GetWorld()->SetContactListener(new MyContactListener(m_Player));
How do you know GetFixtureA is the player and B is the wall? Could it be reversed? Could there be an FixtureC? I would think you would need a more generic solution.
I've used a similar graphics framework (Qt) and it had something so you could grab any two objects and call something like 'hasCollided' which would return a bool. You could get away with not using a callback and just call it in the drawScene() or check it periodically.
In Box2D the existence of a contact just means that the AABBs of two fixtures overlaps. It does not necessarily mean that the shapes of the fixtures themselves are touching.
You can use the IsTouching() function of a contact to check if the shapes are actually touching, but the preferred way to deal with collisions is to use the callback feature to have the engine tell you whenever two fixtures start/stop touching. Using callbacks is much more efficient and easier to manage in the long run, though it may be a little more effort to set up initially and there are a few things to be careful about - see here for an example: http://www.iforce2d.net/b2dtut/collision-callbacks