Snap() public method

public Snap ( Canguro activeView, Point mousePoint ) : float
activeView Canguro
mousePoint Point
return float
        /// <summary>
        /// Calculate the intersection point of two coplanar lines
        /// Source: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/
        /// </summary>
        /// <param name="l1">First LineMagnet</param>
        /// <param name="l2">Second LineMagnet</param>
        /// <returns>The intersection point magnet or null if none found</returns>
        private PointMagnet createIntersection(LineMagnet l1, LineMagnet l2, GraphicView activeView, System.Windows.Forms.MouseEventArgs e)
        {
            if (l1 != null && l2 != null)
            {
                float   numer, denom;
                float   d1, d2, d3, d4, d5;
                Vector3 p13 = l1.Position - l2.Position;
                Vector3 p21 = l1.Direction;
                Vector3 p43 = l2.Direction;

                d1 = p13.X * p43.X + p13.Y * p43.Y + p13.Z * p43.Z;
                d2 = p43.X * p21.X + p43.Y * p21.Y + p43.Z * p21.Z;
                d3 = p13.X * p21.X + p13.Y * p21.Y + p13.Z * p21.Z;
                d4 = p43.X * p43.X + p43.Y * p43.Y + p43.Z * p43.Z;
                d5 = p21.X * p21.X + p21.Y * p21.Y + p21.Z * p21.Z;

                denom = d5 * d4 - d2 * d2;
                if (Math.Abs(denom) < float.Epsilon)
                {
                    return(null);
                }
                numer = d1 * d2 - d3 * d4;

                float r = numer / denom;
                float s = (d1 + d2 * r) / d4;

                Vector3 pa = l1.Position + Vector3.Scale(p21, r);
                Vector3 pb = l2.Position + Vector3.Scale(p43, s);

                if ((pa - pb).Length() > 0.0001)
                {
                    return(null);
                }

                // Create magnet
                PointMagnet intPtMagnet = new PointMagnet(pa,
                                                          PointMagnetType.Intersection);

                if (intPtMagnet.Snap(activeView, e.Location) < SnapViewDistance)
                {
                    intPtMagnet.RelatedMagnets.Add(l1);
                    intPtMagnet.RelatedMagnets.Add(l2);
                    return(intPtMagnet);
                }
            }

            return(null);
        }
        /// <summary>
        /// Method to add a perpendicular point magnet if the mouse is close to where it is
        /// </summary>
        private PointMagnet addPerpendicularMagnet(LineMagnet lm, PointMagnet pm, float lmDot, GraphicView activeView, System.Windows.Forms.MouseEventArgs e)
        {
            float       r            = Vector3.Dot(lm.Direction, pm.Position - lm.Position) / lmDot;
            PointMagnet perpPtMagnet = new PointMagnet(lm.Position + Vector3.Scale(lm.Direction, r),
                                                       PointMagnetType.Perpendicular);

            if (perpPtMagnet.Snap(activeView, e.Location) < SnapViewDistance)
            {
                perpPtMagnet.RelatedMagnets.Add(pm);
                perpPtMagnet.RelatedMagnets.Add(lm);
                points.Add(perpPtMagnet);
                return(perpPtMagnet);
            }

            return(null);
        }
        /// <summary>
        /// Method to try to create a midpoint magnet if the mouse is close to one
        /// </summary>
        /// <param name="lm">The Line Magnet being followed</param>
        /// <param name="activeView">The view in which the snap is taking place</param>
        /// <param name="e">The MouseEventArgs of the last MouseMove event</param>
        /// <returns>The MidPoint magnet or null if none found</returns>
        private PointMagnet createMidPoint(LineMagnet lm, GraphicView activeView, System.Windows.Forms.MouseEventArgs e)
        {
            if (lm == null)
            {
                return(null);
            }

            LineElement l;

            if ((l = lm.Line) != null)
            {
                PointMagnet midPtMagnet = new PointMagnet(Vector3.Scale(l.I.Position + l.J.Position, 0.5f),
                                                          PointMagnetType.MidPoint);
                if (midPtMagnet.Snap(activeView, e.Location) < SnapViewDistance)
                {
                    midPtMagnet.RelatedMagnets.Add(lm);
                    points.Add(midPtMagnet);
                    return(midPtMagnet);
                }
            }

            return(null);
        }
        /// <summary>
        /// Method to perform calculations based on the user interaction by
        /// moving the mouse
        /// </summary>
        /// <param name="sender">The object calling this method</param>
        /// <param name="e">The Mouse event args</param>
        /// <returns>A boolean indicating whether a repaint is necessary</returns>
        public bool MouseMove(GraphicView activeView, System.Windows.Forms.MouseEventArgs e)
        {
            bool ret = true;

            paintMagnets.Clear();
            pickItems(e);
            recalcPrimaryDependant(activeView);

            // Check snap to acquired magnets
            points.Snap(activeView, e);
            lines.Snap(activeView, e);

            // Get max snap Magnet (the one closer to the mouse position)
            Magnet bestMagnet;
            float  snap, bestSnap, bestPointSnap = points.GetBestSnap(out bestMagnet);

            if (bestMagnet == null)
            {
                bestSnap = lines.GetBestSnap(out bestMagnet);
            }
            else
            {
                bestSnap = bestPointSnap;
            }

            // Update CurrentLine's positions if mouse moved over any of it's joints (if LineMagnet has a Line)
            if (lines.CurrentLine != null && bestMagnet != null && bestMagnet is PointMagnet &&
                lines.CurrentLine.Line != null && ((PointMagnet)bestMagnet).Joint != null)
            {
                Joint newJoint = ((PointMagnet)bestMagnet).Joint;

                if (newJoint == lines.CurrentLine.Line.I || newJoint == lines.CurrentLine.Line.J)
                {
                    lines.CurrentLine.Position = newJoint.Position;
                }
            }

            #region Intersection
            // Check intersections between LineMagnets. If any, create corresponding Magnet
            if (lines.CurrentLine != null && bestMagnet is LineMagnet && bestMagnet != lines.CurrentLine)
            {
                PointMagnet intersectionMagnet = createIntersection(lines.CurrentLine, bestMagnet as LineMagnet, activeView, e);
                if (intersectionMagnet != null)
                {
                    snap = intersectionMagnet.Snap(activeView, e.Location) + SnapEpsilon;
                    if (snap < SnapViewDistance)
                    {
                        // Paint helper line
                        paintMagnets.Add(bestMagnet);

                        // Paint intersection
                        paintMagnets.Add(intersectionMagnet);
                    }

                    if (snap < bestPointSnap && snap < EffectiveSnapDistance)
                    {
                        bestSnap   = snap;
                        bestMagnet = intersectionMagnet;
                    }
                }
            }
            #endregion

            #region Midpoint
            // Create midpoint magnet (if mouse is close to a Line)
            foreach (Magnet m in lines)
            {
                PointMagnet midMagnet = createMidPoint(m as LineMagnet, activeView, e);
                if (midMagnet != null)
                {
                    snap = midMagnet.Snap(activeView, e.Location) + SnapEpsilon;
                    if (snap < bestPointSnap && snap < EffectiveSnapDistance)
                    {
                        bestSnap   = snap;
                        bestMagnet = midMagnet;
                    }
                }
            }
            #endregion

            #region Perpendicular point
            // Create perpendicular magnets (if mouse is close to a line)
            PointMagnet perpMagnet = createPerpendicularMagnet(bestMagnet as LineMagnet, activeView, e);
            if (perpMagnet != null)
            {
                snap = perpMagnet.Snap(activeView, e.Location) + SnapEpsilon;
                if (snap < (bestPointSnap - SnapEpsilon) && snap < EffectiveSnapDistance)
                {
                    bestSnap   = snap;
                    bestMagnet = perpMagnet;
                }
            }
            #endregion

            #region Interesting Distance
            // If following a LineMagnet, then set next interesting distance as a PointMagnet
            PointMagnet distMagnet = createInterestingDistance(bestMagnet as LineMagnet, activeView, e);
            if (distMagnet != null)
            {
                snap = distMagnet.Snap(activeView, e.Location) + SnapEpsilon;
                if (snap < bestPointSnap)
                {
                    bestSnap   = snap;
                    bestMagnet = distMagnet;
                }
            }
            #endregion

            // Choose magnets to paint
            if ((bestMagnet is LineMagnet) && (lines.CurrentLine == null))
            {
                lines.CurrentLine = (LineMagnet)bestMagnet;
            }
            else if ((bestMagnet != null) && !bestMagnet.Equals(lines.CurrentLine))
            {
                paintMagnets.Add(bestMagnet);
            }

            // Set the Magnet to snap to if the user clicks the mouse
            snapMagnet = null;
            if (bestMagnet != null)
            {
                if (bestMagnet is PointMagnet && bestMagnet.LastSnapFitness < EffectiveSnapDistance)
                {
                    snapMagnet = bestMagnet;
                }
                else if (lines.CurrentLine != null)
                {
                    snapMagnet = lines.CurrentLine;
                }
                else if (bestMagnet is LineMagnet)
                {
                    snapMagnet = bestMagnet;
                }
            }
            else
            {
                area.Snap(activeView, e.Location);
                snapMagnet = (Magnet)area.Clone();
            }

            // TODO: Probably change the IsActive property to some enum, so as to activate snapping to
            // specific objects only (i.e. Joint snapping when CommandServices.GetJoint() is working)
            return(ret);
        }
        /// <summary>
        /// Method to try to create a midpoint magnet if the mouse is close to one
        /// </summary>
        /// <param name="lm">The Line Magnet being followed</param>
        /// <param name="activeView">The view in which the snap is taking place</param>
        /// <param name="e">The MouseEventArgs of the last MouseMove event</param>
        /// <returns>The MidPoint magnet or null if none found</returns>
        private PointMagnet createMidPoint(LineMagnet lm, GraphicView activeView, System.Windows.Forms.MouseEventArgs e)
        {
            if (lm == null) return null;

            LineElement l;
            if ((l = lm.Line) != null)
            {
                PointMagnet midPtMagnet = new PointMagnet(Vector3.Scale(l.I.Position + l.J.Position, 0.5f),
                    PointMagnetType.MidPoint);
                if (midPtMagnet.Snap(activeView, e.Location) < SnapViewDistance)
                {
                    midPtMagnet.RelatedMagnets.Add(lm);
                    points.Add(midPtMagnet);
                    return midPtMagnet;
                }
            }

            return null;
        }
        /// <summary>
        /// Calculate the intersection point of two coplanar lines
        /// Source: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/
        /// </summary>
        /// <param name="l1">First LineMagnet</param>
        /// <param name="l2">Second LineMagnet</param>
        /// <returns>The intersection point magnet or null if none found</returns>
        private PointMagnet createIntersection(LineMagnet l1, LineMagnet l2, GraphicView activeView, System.Windows.Forms.MouseEventArgs e)
        {
            if (l1 != null && l2 != null)
            {
                float numer, denom;
                float d1, d2, d3, d4, d5;
                Vector3 p13 = l1.Position - l2.Position;
                Vector3 p21 = l1.Direction;
                Vector3 p43 = l2.Direction;

                d1 = p13.X * p43.X + p13.Y * p43.Y + p13.Z * p43.Z;
                d2 = p43.X * p21.X + p43.Y * p21.Y + p43.Z * p21.Z;
                d3 = p13.X * p21.X + p13.Y * p21.Y + p13.Z * p21.Z;
                d4 = p43.X * p43.X + p43.Y * p43.Y + p43.Z * p43.Z;
                d5 = p21.X * p21.X + p21.Y * p21.Y + p21.Z * p21.Z;

                denom = d5 * d4 - d2 * d2;
                if (Math.Abs(denom) < float.Epsilon)
                    return null;
                numer = d1 * d2 - d3 * d4;

                float r = numer / denom;
                float s = (d1 + d2 * r) / d4;

                Vector3 pa = l1.Position + Vector3.Scale(p21, r);
                Vector3 pb = l2.Position + Vector3.Scale(p43, s);

                if ((pa - pb).Length() > 0.0001)
                    return null;

                // Create magnet
                PointMagnet intPtMagnet = new PointMagnet(pa,
                    PointMagnetType.Intersection);

                if (intPtMagnet.Snap(activeView, e.Location) < SnapViewDistance)
                {
                    intPtMagnet.RelatedMagnets.Add(l1);
                    intPtMagnet.RelatedMagnets.Add(l2);
                    return intPtMagnet;
                }
            }

            return null;
        }
        /// <summary>
        /// Method to add a perpendicular point magnet if the mouse is close to where it is
        /// </summary>
        private PointMagnet addPerpendicularMagnet(LineMagnet lm, PointMagnet pm, float lmDot, GraphicView activeView, System.Windows.Forms.MouseEventArgs e)
        {
            float r = Vector3.Dot(lm.Direction, pm.Position - lm.Position) / lmDot;
            PointMagnet perpPtMagnet = new PointMagnet(lm.Position + Vector3.Scale(lm.Direction, r),
                PointMagnetType.Perpendicular);
            if (perpPtMagnet.Snap(activeView, e.Location) < SnapViewDistance)
            {
                perpPtMagnet.RelatedMagnets.Add(pm);
                perpPtMagnet.RelatedMagnets.Add(lm);
                points.Add(perpPtMagnet);
                return perpPtMagnet;
            }

            return null;
        }