Page 1 of 1

Convert Screen Coordinates to Axis Values: View3D

Posted: Tue Feb 25, 2014 7:53 pm
by blakeadkins
I'm using View3D PointLineSeries3D and I'd like to get the x,y, z values under the mouse, not the screen coordinates.

I saw the example for ViewXY, but that doesn't seem work for the View3D.

Any help would be appreciated. Thanks!

Re: Convert Screen Coordinates to Axis Values: View3D

Posted: Tue Feb 25, 2014 9:35 pm
by ArctionPasi
Hi,

under the mouse, there's infinite count of positions (a vector from mouse towards the screen), and therefore 2D coordinate can't be directly converted into 3D coordinate. Are you looking for a method that solves the nearest point in PointLineSeries3D?

Re: Convert Screen Coordinates to Axis Values: View3D

Posted: Tue Feb 25, 2014 9:55 pm
by blakeadkins
Yeah, that might work.

Re: Convert Screen Coordinates to Axis Values: View3D

Posted: Tue Feb 25, 2014 10:03 pm
by ArctionPasi
In MouseMove or MouseClick event handler, seek it like this. Indicate the nearest point with another PLS3D with only one point as a marker.

Code: Select all


private void chart_MouseMove(object sender, MouseEventArgs e)
        {
            Point mouseScreen = e.GetPosition(m_chart);
        
            //Seek from data series 
            int iNearestIndex = GetNearestIndexToMouse(m_chart.View3D.PointLineSeries3D[0], mouseScreen.X, mouseScreen.Y);

            if (iNearestIndex >= 0)
            {
                PointDouble3D nearestPoint = m_chart.View3D.PointLineSeries3D[0].Points[iNearestIndex]; 
                
                m_chart.BeginUpdate();

                //Create another PointLineSeries to show the nearest point with a box. 
                if(m_chart.View3D.PointLineSeries3D.Count < 2)
                {
                    PointLineSeries3D marker = new PointLineSeries3D(m_chart.View3D, Axis3DBinding.Primary, Axis3DBinding.Primary, Axis3DBinding.Primary);
                    marker.PointStyle.Shape = PointShape3D.Box;
                    marker.PointStyle.Size.SetValues(4, 4, 4);
                    marker.Material.DiffuseColor = Color.Lime; 
                    marker.MouseInteraction = false; 
                    m_chart.View3D.PointLineSeries3D.Add(marker); 

                }

                m_chart.View3D.PointLineSeries3D[1].Points = new PointDouble3D[] { nearestPoint };

                m_chart.EndUpdate(); 
            }
       }

int GetNearestIndexToMouse(PointLineSeries3D pls, double mouseX, double mouseY)
        {
         
            PointDouble3D[] points = pls.Points;
            int iPointCount = pls.PointCount;
            PointInt p;
            
            double dMinDistSq = double.MaxValue;
            int iNearestIndex = -1;
            double dDistSq; 
            double x, y;

            //Seek nearest point, by using squared method. It's faster than calling Sqrt every time. 

            for(int i=0;i<iPointCount;i++)
            {
                p = m_chart.View3D.ConvertSeriesValueToScreenCoord(pls, points[i].X, points[i].Y, points[i].Z);
                x = (double) p.X;
                y = (double) p.Y;

                dDistSq = (y - mouseY) * (y - mouseY) + (x - mouseX) * (x - mouseX);
                if (dDistSq < dMinDistSq)
                {
                    dMinDistSq = dDistSq;
                    iNearestIndex = i;
                }
            }

            return iNearestIndex; 

        }

Here, the nearest point is indicated with the lime box.
Nearest 3D point indicated
Nearest 3D point indicated
nearest3DPoint.jpg (274.59 KiB) Viewed 16300 times
8-)

Re: Convert Screen Coordinates to Axis Values: View3D

Posted: Wed Feb 26, 2014 4:44 pm
by blakeadkins
Is there a way to always grab the closest point to the viewer?

Re: Convert Screen Coordinates to Axis Values: View3D

Posted: Wed Feb 26, 2014 8:08 pm
by ArctionPasi
Kind of. But the mouse must be right over a point.

PointLineSeries3D_0: a polyline, going through all the N points. LineVisible = true, PointsVisible = false, MouseInteraction = false.

PointLineSeries3D_1 ... PointLineSeries3D_N : A series for each point separately. LineVisible = false, PointsVisible = true, Title.ShowInLegendBox = false. Set MouseOverOn/MouseClick event handler for every series. The event handler will launch only for the series that is nearest to camera.

Re: Convert Screen Coordinates to Axis Values: View3D

Posted: Fri Feb 28, 2014 9:08 pm
by blakeadkins
Thanks! Does what I needed.