Example #1
0
        /// <summary>
        /// Clip-Testing a circular inking segment again a hitting point.
        ///
        /// What need to find out a doulbe value s, which is between 0 and 1, such that
        ///     DistanceOf(hit - s*spine) = beginRadius + s * (endRadius - beginRadius)
        /// That is
        ///     (hit.X-s*spine.X)^2 + (hit.Y-s*spine.Y)^2 = [beginRadius + s*(endRadius-beginRadius)]^2
        /// Rearrange
        ///     A*s^2 + B*s + C = 0
        /// where the value of A, B and C are described in the source code.
        /// Solving for s:
        ///             s = (-B + sqrt(B^2-4A*C))/(2A)  or s = (-B - sqrt(B^2-4A*C))/(2A)
        /// The smaller value between 0 and 1 is the one we want and discard the other one.
        /// </summary>
        /// <param name="spine">Represent the spine of the inking segment pointing from the beginNode to endNode</param>
        /// <param name="beginRadius">Radius of the beginNode</param>
        /// <param name="endRadius">Radius of the endNode</param>
        /// <param name="hit">The hitting point</param>
        /// <returns>A double which represents the location for cutting</returns>
        private static double ClipTest(Vector spine, double beginRadius, double endRadius, Vector hit)
        {
            double radDiff = endRadius - beginRadius;
            double A       = spine.X * spine.X + spine.Y * spine.Y - radDiff * radDiff;
            double B       = -2.0f * (hit.X * spine.X + hit.Y * spine.Y + beginRadius * radDiff);
            double C       = hit.X * hit.X + hit.Y * hit.Y - beginRadius * beginRadius;

            // There checks are here since if either fail no real solution can be caculated and we may
            // as well bail out now and save the caculations that are below.
            if (DoubleUtil.IsZero(A) || !DoubleUtil.GreaterThanOrClose(B * B, 4.0f * A * C))
            {
                return(1d);
            }

            double tmp = Math.Sqrt(B * B - 4.0f * A * C);
            double s1  = (-B + tmp) / (2.0f * A);
            double s2  = (-B - tmp) / (2.0f * A);
            double findex;

            if (DoubleUtil.IsBetweenZeroAndOne(s1) && DoubleUtil.IsBetweenZeroAndOne(s1))
            {
                findex = Math.Min(s1, s2);
            }
            else if (DoubleUtil.IsBetweenZeroAndOne(s1))
            {
                findex = s1;
            }
            else if (DoubleUtil.IsBetweenZeroAndOne(s2))
            {
                findex = s2;
            }
            else
            {
                // There is still possiblity that value like 1.0000000000000402 is not considered
                // as "IsOne" by DoubleUtil class. We should be at either one of the following two cases:
                // 1. s1/s2 around 0 but not close enough (say -0.0000000000001)
                // 2. s1/s2 around 1 but not close enought (say 1.0000000000000402)

                if (s1 > 1d && s2 > 1d)
                {
                    findex = 1d;
                }
                else if (s1 < 0d && s2 < 0d)
                {
                    findex = 0d;
                }
                else
                {
                    findex = Math.Abs(Math.Min(s1, s2) - 0d) < Math.Abs(Math.Max(s1, s2) - 1d) ? 0d : 1d;
                }
            }
            return(AdjustFIndex(findex));
        }
Example #2
0
        /// <summary>
        /// Clip-Testing a circluar inking segment against a linear segment.
        /// See http://tabletpc/longhorn/Specs/Rendering%20and%20Hit-Testing%20Ink%20in%20Avalon%20M11.doc section
        /// 5.4.4.14	Clip-Testing a Circular Inking Segment against a Linear Segment for details of the algorithm
        /// </summary>
        /// <param name="spineVector">Represent the spine of the inking segment pointing from the beginNode to endNode</param>
        /// <param name="beginRadius">Radius of the beginNode</param>
        /// <param name="endRadius">Radius of the endNode</param>
        /// <param name="hitBegin">Hitting segment start point</param>
        /// <param name="hitEnd">Hitting segment end point</param>
        /// <returns>A double which represents the location for cutting</returns>
        private static double ClipTest(Vector spineVector, double beginRadius, double endRadius, Vector hitBegin, Vector hitEnd)
        {
            // First handle the special case when the spineVector is (0,0). In other words, this is the case
            // when the stylus stays at the the location but pressure changes.
            if (DoubleUtil.IsZero(spineVector.X) && DoubleUtil.IsZero(spineVector.Y))
            {
                System.Diagnostics.Debug.Assert(DoubleUtil.AreClose(beginRadius, endRadius) == false);

                Vector nearest = GetNearest(hitBegin, hitEnd);
                double radius;
                if (nearest.X == 0)
                {
                    radius = Math.Abs(nearest.Y);
                }
                else if (nearest.Y == 0)
                {
                    radius = Math.Abs(nearest.X);
                }
                else
                {
                    radius = nearest.Length;
                }
                return(AdjustFIndex((radius - beginRadius) / (endRadius - beginRadius)));
            }

            // This change to ClipTest with a point if the two hitting segment are close enough.
            if (DoubleUtil.AreClose(hitBegin, hitEnd))
            {
                return(ClipTest(spineVector, beginRadius, endRadius, hitBegin));
            }

            double findex;
            Vector hitVector = hitEnd - hitBegin;

            if (DoubleUtil.IsZero(Vector.Determinant(spineVector, hitVector)))
            {
                // hitVector and spineVector are parallel
                findex = ClipTest(spineVector, beginRadius, endRadius, GetNearest(hitBegin, hitEnd));
                System.Diagnostics.Debug.Assert(!double.IsNaN(findex));
            }
            else
            {
                // On the line defined by the segment find point P1Xp, the nearest to the beginNode.Position
                double x    = GetProjectionFIndex(hitBegin, hitEnd);
                Vector P1Xp = hitBegin + (hitVector * x);
                if (P1Xp.LengthSquared < (beginRadius * beginRadius))
                {
                    System.Diagnostics.Debug.Assert(DoubleUtil.IsBetweenZeroAndOne(x) == false);
                    findex = ClipTest(spineVector, beginRadius, endRadius, (0 > x) ? hitBegin : hitEnd);
                    System.Diagnostics.Debug.Assert(!double.IsNaN(findex));
                }
                else
                {
                    // Find the projection point P of endNode.Position to the line (beginNode.Position, B).
                    Vector P1P2p = spineVector + GetProjection(-spineVector, P1Xp - spineVector);

                    //System.Diagnostics.Debug.Assert(false == DoubleUtil.IsZero(P1P2p.LengthSquared));
                    //System.Diagnostics.Debug.Assert(false == DoubleUtil.IsZero(endRadius - beginRadius + P1P2p.Length));
                    // There checks are here since if either fail no real solution can be caculated and we may
                    // as well bail out now and save the caculations that are below.
                    if (DoubleUtil.IsZero(P1P2p.LengthSquared) || DoubleUtil.IsZero(endRadius - beginRadius + P1P2p.Length))
                    {
                        return(1d);
                    }

                    // Calculate the findex of the point to split the ink segment at.
                    findex = (P1Xp.Length - beginRadius) / (endRadius - beginRadius + P1P2p.Length);
                    System.Diagnostics.Debug.Assert(!double.IsNaN(findex));

                    // Find the projection of the split point to the line of this segment.
                    Vector S = spineVector * findex;

                    double r = GetProjectionFIndex(hitBegin - S, hitEnd - S);

                    // If the nearest point misses the segment, then find the findex
                    // of the node nearest to the segment.
                    if (false == DoubleUtil.IsBetweenZeroAndOne(r))
                    {
                        findex = ClipTest(spineVector, beginRadius, endRadius, (0 > r) ? hitBegin : hitEnd);
                        System.Diagnostics.Debug.Assert(!double.IsNaN(findex));
                    }
                }
            }
            return(AdjustFIndex(findex));
        }
Example #3
0
        private Point[] FilterPoints(Point[] path)
        {
            System.Diagnostics.Debug.Assert(path.Length > 1);
            Point        back2, back1;
            int          i;
            List <Point> newPath = new List <Point>();

            if (_nodeIterator.Count == 0)
            {
                newPath.Add(path[0]);
                newPath.Add(path[1]);
                back2 = path[0];
                back1 = path[1];
                i     = 2;
            }
            else
            {
                newPath.Add(path[0]);
                back2 = _nodeIterator[_nodeIterator.Count - 1].Position;
                back1 = path[0];
                i     = 1;
            }

            while (i < path.Length)
            {
                if (DoubleUtil.AreClose(back1, path[i]))
                {
                    // Filter out duplicate points
                    i++;
                    continue;
                }

                Vector begin = back2 - back1;
                Vector end   = path[i] - back1;
                //On a line defined by begin & end,  finds the findex of the point nearest to the origin (0,0).
                double findex = StrokeNodeOperations.GetProjectionFIndex(begin, end);

                if (DoubleUtil.IsBetweenZeroAndOne(findex))
                {
                    Vector v = (begin + (end - begin) * findex);
                    if (v.LengthSquared < CollinearTolerance)
                    {
                        // The point back1 can be considered as on the line from back2 to the toTest StrokeNode.
                        // Modify the previous point.
                        newPath[newPath.Count - 1] = path[i];
                        back1 = path[i];
                        i++;
#if POINTS_FILTER_TRACE
                        _collinearPointsScreened++;
#endif
                        continue;
                    }
                }

                // Add the surviving point into the list.
                newPath.Add(path[i]);
                back2 = back1;
                back1 = path[i];
                i++;
            }
#if POINTS_FILTER_TRACE
            _totalPointsScreened += path.Length - newPath.Count;
#endif
            return(newPath.ToArray());
        }