Undesired End-Caps with StrokeStyle when using PathGeometry.Widen() - directx

I'm using SharpDX in order to draw polylines on .NET WinForms.
These polylines represent the profile of sheetmetal.
A PathGeometry object is composed, defining the polyline:
'reset m_pathGeometry (the core geometry of this sheetmetal profile)
m_pathGeometry = New PathGeometry(D2DCanvas.canvas_factory2D)
' pointsRef = collection of points showing the segments & bends in this sheetmetal profile
Dim pointsRef As List(Of Vector2) = calculateSegmentsPoints()
' Add lines to m_pathGeometry using our points collection
Using sink As SimplifiedGeometrySink = m_pathGeometry.Open()
sink.BeginFigure(pointsRef.First(), FigureBegin.Filled)
sink.AddLines(pointsRef.ToArray)
sink.EndFigure(FigureEnd.Open)
sink.Close()
End Using
Before being drawn to screen I apply a transform (to take into account transformation & scale):
' update m_fillTransformedGeometry based on m_pathGeometry & the current transform matrix3x2,
m_fillTransformedGeometry = New TransformedGeometry(D2DCanvas.canvas_factory2D, m_pathGeometry, Matrix3x2.Rotation(m_rotation, InsertPoint) * _transformMatrix)
' to draw the polyline
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_fillTransformedGeometry, m_fillBrush, plaatdikte * _transformMatrix.ScaleVector(1), m_strokeStyle)
The polyline gets drawn with a strokestyle, rounded line-joins and flat end-caps
With m_strokeStyleProps
.LineJoin = LineJoin.Round
.EndCap = CapStyle.Flat
.StartCap = CapStyle.Flat
End With
Result is a bit pale, could use a stroke:
First idea was to draw the same polyline in a darker color, with a slightly wider StrokeWidth:
' First draw a wider stroke
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_fillTransformedGeometry, m_strokeBrush, (plaatdikte + 0.2) * _transformMatrix.ScaleVector(1), m_strokeStyle)
' Then draw the fill
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_fillTransformedGeometry, m_fillBrush, plaatdikte * _transformMatrix.ScaleVector(1), m_strokeStyle)
The result is nice, but the end-caps don't get a stroke this way:
Second idea is to create a (slightly widened) PathGeometry for the stroke itself by calling the Widen() method on the original polyline-PathGeometry.
'define m_widenedPathGeometry (makes op the outline/stroke of this sheetmetal profile)
m_widenedPathGeometry = New PathGeometry(D2DCanvas.canvas_factory2D)
Using sink As SimplifiedGeometrySink = m_widenedPathGeometry.Open()
m_pathGeometry.Widen(0.2, sink)
sink.Close()
End Using
This widened geometry gets drawn (taking in account transformation) as a 'stroke' prior to drawing the 'fill' (both using the same StrokeStyle!):
' update m_transGeom based on m_widenedPathGeometry & the current transform matrix3x2,
m_transGeom = New TransformedGeometry(D2DCanvas.canvas_factory2D, m_widenedPathGeometry, Matrix3x2.Rotation(m_rotation, InsertPoint) * _transformMatrix)
' to draw the stroke of the Polyline
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_transGeom, m_strokeBrush, plaatdikte * _transformMatrix.ScaleVector(1), m_strokeStyle)
' update m_fillTransformedGeometry based on m_pathGeometry & the current transform matrix3x2,
m_fillTransformedGeometry = New TransformedGeometry(D2DCanvas.canvas_factory2D, m_pathGeometry, Matrix3x2.Rotation(m_rotation, InsertPoint) * _transformMatrix)
' to draw the fill of the PolyLine
D2DCanvas.canvas_renderTarget2D.DrawGeometry(m_fillTransformedGeometry, m_fillBrush, plaatdikte * _transformMatrix.ScaleVector(1), m_strokeStyle)
This generates an undesired effect on the End-Caps:
(Either rounded, wich isn't defined in the StrokeStyle or something in between)
This Stroke/Fill - method kindof works correctly when I redefine the used StrokeStyle to have 'rounded' endcaps:
But the 'flat' end-caps is really what I'm after.
Any ideas why this is happening, or how I could tackle this differently ?
Cheers !

Found out what was wrong here!
Writing this down for others that might encounter something similar to this.
When creating the widened geometry i've added that the 'FigureEnd' property is set to 'Open', this prevents the geometry of creating a 'closed loop' on itself, wich results in those artifacts.
m_widenedPathGeometry = New PathGeometry(D2DCanvas.canvas_factory2D)
Using sink As SimplifiedGeometrySink = m_widenedPathGeometry.Open()
m_pathGeometry.Widen(0.2, sink)
**sink.EndFigure(FigureEnd.Open)** <- adding this line leaves the stroke open!
sink.Close()
End Using

Related

Calculate area covered by a polygon on earth

I am trying to calculate the area covered by a polygon on a map in square kilometers.
Based on the code from [1] and the corresponding paper [2] I have this code:
double area = 0;
auto coords = QList<QGeoCoordinate>{(QGeoCoordinate(50.542908183, 6.2521438908), QGeoCoordinate(50.250550175, 6.2521438908), QGeoCoordinate(50.250550175, 6.4901310043), QGeoCoordinate(50.542908183, 6.4901310043))};
for(int i=0; i<coords.size()-1; i++)
{
const auto &p1 = coords[i];
const auto &p2 = coords[i+1];
area += qDegreesToRadians(p2.longitude() - p1.longitude()) *
(2 + qSin(qDegreesToRadians(p2.latitude())) +
qSin(qDegreesToRadians(p1.latitude())));
}
area = area * 6378137.0 * 6378137.0 / 2.0;
qDebug() << "Area:" << (area/1000000);
qDebug() << coords;
But the calculated area is completely wrong. Also moving the polyon's vertices around results in strange results: Depending on the vertex the calculated area gets smaller althought the polgon's area is increased and vice verse. The calculated area also seems to depend on which vertex is used as start vertex.
Interestingly the signed are of a ring algorithm (getArea from [1]) returns correct results, meaning that the calculated area increases/decreases when the polygon's size is changed.
The code for calculating the area on a sphere was also used elsewhere so I am pretty sure that something is wrong with my implementation.
[1] https://github.com/openlayers/openlayers/blob/v2.13.1/lib/OpenLayers/Geometry/LinearRing.js#L251
[2] https://trs.jpl.nasa.gov/bitstream/handle/2014/40409/JPL%20Pub%2007-3%20%20w%20Errata.pdf?sequence=3&isAllowed=y
[3] Polygon area calculation using Latitude and Longitude generated from Cartesian space and a world file
I still could not find the error in my code but switching to the ringArea method from https://github.com/mapbox/geojson-area/blob/master/index.js works.

Why do I get this error? TypeError: Expected Ptr<cv::UMat> for argument 'points'

contours2, hierarchy2 = cv2.findContours(gray, cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
((cx,cy),radius) = cv2.minEnclosingCircle(contours2)
area = radius * radius * np.pi
print (area)
I'm trying to get the area of this circle using this method
Why do I get this error? Thank you for taking time reading my post.
contours2 is a list. You should pass the contour you want to calculate the enclosing circle. Assuming you only have one contour, the code below should work with no errors:
((cx,cy),radius) = cv2.minEnclosingCircle(contours2[0])
There is a function specifically to calculate the area of a contour:
area = cv2.contourArea(contour2[0])

OpenLayers - lock rotation of box or rectangle geometry while modifying

Openlayers provides useful functions for drawing boxes and rectangles and also has ol.geom.Geometry.prototype.rotate(angle, anchor) for rotating a geometry around a certain anchor. Is it possible to lock the rotation of a box/rectangle while modifying it?
Using the OpenLayers example located here to draw a box with a certain rotation to illustrate the point:
I would like the box/rectangle to maintain its rotation while still being able to drag the sides longer and shorter. Is there a simple way to achieve this?
Answering with the solution I came up with.
First of all, add the feature(s) to a ModifyInteraction so you are able to modify by dragging the corners of the feature.
this.modifyInteraction = new Modify({
deleteCondition: eventsCondition.never,
features: this.drawInteraction.features,
insertVertexCondition: eventsCondition.never,
});
this.map.addInteraction(this.modifyInteraction);
Also, add event handlers upon the events "modifystart" and "modifyend".
this.modifyInteraction.on("modifystart", this.modifyStartFunction);
this.modifyInteraction.on("modifyend", this.modifyEndFunction);
The functions for "modifystart" and "modifyend" look like this.
private modifyStartFunction(event) {
const features = event.features;
const feature = features.getArray()[0];
this.featureAtModifyStart = feature.clone();
this.draggedCornerAtModifyStart = "";
feature.on("change", this.changeFeatureFunction);
}
private modifyEndFunction(event) {
const features = event.features;
const feature = features.getArray()[0];
feature.un("change", this.changeFeatureFunction);
// removing and adding feature to force reindexing
// of feature's snappable edges in OpenLayers
this.drawInteraction.features.clear();
this.drawInteraction.features.push(feature);
this.dispatchRettighetModifyEvent(feature);
}
The changeFeatureFunction is below. This function is called for every single change which is done to the geometry as long as the user is still modifying/dragging one of the corners. Inside this function, I made another function to adjust the modified rectangle into a rectangle again. This "Rectanglify"-function moves the corners which are adjacent to the corner which was just moved by the user.
private changeFeatureFunction(event) {
let feature = event.target;
let geometry = feature.getGeometry();
// Removing change event temporarily to avoid infinite recursion
feature.un("change", this.changeFeatureFunction);
this.rectanglifyModifiedGeometry(geometry);
// Reenabling change event
feature.on("change", this.changeFeatureFunction);
}
Without going into too much detail, the rectanglify-function needs to
find rotation of geometry in radians
inversely rotate with radians * -1 (e.g. geometry.rotate(radians * (-1), anchor) )
update neighboring corners of the dragged corner (easier to do when we have a rectangle which is parallel to the x and y axes)
rotate back with the rotation we found in 1
--
In order to get the rotation of the rectangle, we can do this:
export function getRadiansFromRectangle(feature: Feature): number {
const coords = getCoordinates(feature);
const point1 = coords[0];
const point2 = coords[1];
const deltaY = (point2[1] as number) - (point1[1] as number);
const deltaX = (point2[0] as number) - (point1[0] as number);
return Math.atan2(deltaY, deltaX);
}

Box2D: How to use b2ChainShape for a tile based map with squares

Im fighting here with the so called ghost collisions on a simple tile based map with a circle as player character.
When applying an impulse to the circle it first starts bouncing correctly, then sooner or later it bounces wrong (wrong angle).
Looking up on the internet i read about an issue in Box2D (i use iOS Swift with Box2d port for Swift).
Using b2ChainShape does not help, but it looks i misunderstood it. I also need to use the "prevVertex" and "nextVertex" properties to set up the ghost vertices.
But im confused. I have a simple map made up of boxes (simple square), all placed next to each other forming a closed room. Inside of it my circle i apply an impulse seeing the issue.
Now WHERE to place those ghost vertices for each square/box i placed on the view in order to solve this issue? Do i need to place ANY vertex close to the last and first vertice of chainShape or does it need to be one of the vertices of the next box to the current one? I dont understand. Box2D's manual does not explain where these ghost vertices coordinates are coming from.
Below you can see an image describing the problem.
Some code showing the physics parts for the walls and the circle:
First the wall part:
let bodyDef = b2BodyDef()
bodyDef.position = self.ptm_vec(node.position+self.offset)
let w = self.ptm(Constants.Config.wallsize)
let square = b2ChainShape()
var chains = [b2Vec2]()
chains.append(b2Vec2(-w/2,-w/2))
chains.append(b2Vec2(-w/2,w/2))
chains.append(b2Vec2(w/2,w/2))
chains.append(b2Vec2(w/2,-w/2))
square.createLoop(vertices: chains)
let fixtureDef = b2FixtureDef()
fixtureDef.shape = square
fixtureDef.filter.categoryBits = Constants.Config.PhysicsCategory.Wall
fixtureDef.filter.maskBits = Constants.Config.PhysicsCategory.Player
let wallBody = self.world.createBody(bodyDef)
wallBody.createFixture(fixtureDef)
The circle part:
let bodyDef = b2BodyDef()
bodyDef.type = b2BodyType.dynamicBody
bodyDef.position = self.ptm_vec(node.position+self.offset)
let circle = b2CircleShape()
circle.radius = self.ptm(Constants.Config.playersize)
let fixtureDef = b2FixtureDef()
fixtureDef.shape = circle
fixtureDef.density = 0.3
fixtureDef.friction = 0
fixtureDef.restitution = 1.0
fixtureDef.filter.categoryBits = Constants.Config.PhysicsCategory.Player
fixtureDef.filter.maskBits = Constants.Config.PhysicsCategory.Wall
let ballBody = self.world.createBody(bodyDef)
ballBody.linearDamping = 0
ballBody.angularDamping = 0
ballBody.createFixture(fixtureDef)
Not sure that I know of a simple solution in the case that each tile can potentially have different physics.
If your walls are all horizontal and/or vertical, you could write a class to take a row of boxes, create a single edge or rectangle body, and then on collision calculate which box (a simple a < x < b test) should interact with the colliding object, and apply the physics appropriately, manually calling the OnCollision method that you would otherwise specify as the callback for each individual box.
Alternatively, to avoid the trouble of manually testing intersection with different boxes, you could still merge all common straight edge boxes into one edge body for accurate reflections. However, you would still retain the bodies for the individual boxes. Extend the boxes so that they overlap the edge.
Now here's the trick: all box collision handlers return false, but they toggle flags on the colliding object (turning flags on OnCollision, and off OnSeparation). The OnCollision method for the edge body then processes the collision based on which flags are set.
Just make sure that the colliding body always passes through a box before it can touch an edge. That should be straightforward.

Center of rotation for cc.RotateBy applied on a cc.DrawNode in cocos2d-js

I made a drawNode to draw primitive using this code:
var.drawNode = cc.DrawNode.create();
drawNode.drawSegment(this.pos, cc.p(this.pos.x + this.length * Math.sin(this.rotation), this.pos.y + this.length * Math.cos(this.rotation)), STICK_THICKESS, cc.color(255,255,0,255));
It basically draws a line from this.pos to another point.
Now I want to rotate the line around this.pos, so I thought I just need to simply add this:
drawNode.setAnchorPoint(this.pos);
var rotate = cc.RotateBy.create(2, 360);
drawNode.runAction(rotate);
But it's still rotating around some random point.
Ugly but working method:
drawNode.setContentSize(1, 1);
drawNode.setAnchorPoint(this.pos);
drawNode.setPosition(this.pos);
var rotate = cc.RotateBy.create(2, 360);
drawNode.runAction(rotate);
BTW create methods are deprecated in Cocos2d-html5 3.0+. Use cc.rotateBy() instead of cc.RotateBy.Create(), new cc.DrawNode() instead of cc.DrawNode.create()

Resources