Error in Hit-Testing Geometry

I have a DrawingVisual element that represents the path that the geometry is described by this syntax :

"m106.59.3c0-1.98,0.0-4.95,0.989-3.96,0.989-13.8,3.96-20.8,4.95-6.92,0-14.8-3.96-17.8-3.96-1.98,2.97,3.96,10.9, 7.91, 13.8.2.97.1.98.9.89.3.96.14.8.3.96.4.95-0.989.10.9-2.97.13.8-6.92.2.97-2.97.5.93-10.9.6.92-12.9z "

For visualization, I use the MyCanvas class, which provides testing functionality:

public class MyCanvas : Panel { public List<Visual> Visuals = new List<Visual>(); private List<DrawingVisual> Hits = new List<DrawingVisual>(); public void AddVisual(Visual Visual) { this.Visuals.Add(Visual); base.AddVisualChild(Visual); base.AddLogicalChild(Visual); } public List<DrawingVisual> GetVisuals(Geometry Region) { GeometryHitTestParameters Parameters = new GeometryHitTestParameters(Region); this.Hits.Clear(); HitTestResultCallback Callback = new HitTestResultCallback(this.HitTestCallBack); VisualTreeHelper.HitTest(this, null, Callback, Parameters); return this.Hits; } private HitTestResultBehavior HitTestCallBack(HitTestResult Result) { GeometryHitTestResult GeometryRes = (GeometryHitTestResult)Result; DrawingVisual DVisual = Result.VisualHit as DrawingVisual; if (DVisual != null && GeometryRes.IntersectionDetail == IntersectionDetail.FullyInside) this.Hits.Add(DVisual); return HitTestResultBehavior.Continue; } protected override Visual GetVisualChild(int Index) { return this.Visuals[Index]; } protected override int VisualChildrenCount { get { return this.Visuals.Count; } } } 

When I draw my (red) path, this is the result:

If the mesh size is 50x50. Now I'm trying to get visual effects, for example, in this region:

 MyCanvas my_canvas = new MyCanvas(); RectangleGeometry MyRegion = new RectangleGeometry(new Rect(50, 50, 250, 250)); DrawingVisual MyPath = new DrawingVisual(); using (DrawingContext context = MyPath.RenderOpen()) { context.PushTransform(new TranslateTransform(50, 50)); context.PushTransform(new ScaleTransform(2, 2)); context.DrawGeometry(Brushes.Red, new Pen(), MyGeometry); } my_canvas.AddVisual(MyPath); List<DrawingVisual> result = my_canvas.GetVisuals(MyRegion); 

But MyPath is not the result, why? How should I do the hit test correctly? Thanks.

+6
source share
1 answer

Hit-testing seems to be considering the position of the figures to which the reverse order of the transforms has been applied. This explains why my path intersects only, and not completely, inside the RectangleGeometry argument of the MyCanvas.GetVisuals method.

Expecting a better answer, I applied hit-checking using the non-hit-testing test method, now part of the MyCanvas class:

 public List<DrawingVisual> GetVisuals(Rect Area) { this.Hits.Clear(); foreach (DrawingVisual DVisual in this.Visuals) { if (Area.Contains(DVisual.DescendantBounds)) this.Hits.Add(DVisual); } return this.Hits; } 

EDIT:

As Mike Danes (moderator on the MSDN forum) explains in this thread:

"Is it possible that this is a bug in testing geometry?"

I am 99% sure that this is a mistake. When drawing and testing strokes, the same transformation order should be used. The reason that it works correctly with TransformGroup is that in this way you only click on one transformation in the drawing context, and this avoids the wrong order of multiplication in the context of the test test graph. Note that this has nothing to do with the fact that the order used in the TranformGroup is different from the push order.

+5
source

Source: https://habr.com/ru/post/914909/


All Articles