Tuesday, February 19, 2013

fail implementation of GetIsometriLine, too many bugs

#region fail implementation of GetIsometriLine, too many bugs
        public SqlGeometry GetIsometricLine(SqlGeometry rd, ref Vector startpt, ref Vector endpt, ref bool shortRd, SqlGeometry imp, bool IntersectAtStart, double validLen, double lnLen)
        {
            SqlGeometry jnct =new SqlGeometry();
            Vector jnctpt, rdend, impend;
            if (IntersectAtStart)
                jnct = rd.STStartPoint();
            else
                jnct = rd.STEndPoint();
            jnctpt = new Vector(jnct.STX.Value, jnct.STY.Value);
            if (shortRd)
                rdend = startpt;
            else
            {
                if (IntersectAtStart)
                {                                        
                    if (startpt != new Vector(-9999, -9999))
                        rdend = startpt;
                    else
                    {                        
                        if (rd.STLength() < validLen)
                        {
                            SqlGeometry centpt = GeoUtils.Reduce2NumPoints(rd,1000).STBuffer(0.00002).STCentroid();
                            rdend = new Vector(centpt.STX.Value, centpt.STY.Value);
                            startpt = rdend;
                            endpt = rdend;
                            shortRd = true;
                        }
                        else
                        {
                            int np = rd.STNumPoints().Value;
                            if (np == 2)
                            {
                                rdend = new Vector(rd.STEndPoint().STX.Value, rd.STEndPoint().STY.Value);
                                startpt = rdend;                                
                            }
                            else
                            {
                                double len = jnct.STDistance(rd.STPointN(2)).Value;
                                Vector pt = new Vector(rd.STPointN(2).STX.Value, rd.STPointN(2).STY.Value);
                                int p=2;
                                while (len < validLen)
                                {
                                    p++;
                                    Vector ppt = new Vector(rd.STPointN(p).STX.Value, rd.STPointN(p).STY.Value);
                                    len += rd.STPointN(p-1).STDistance(rd.STPointN(p)).Value;
                                    pt = (pt + ppt) / 2;
                                }
                                rdend = pt;
                                startpt=rdend;
                            }                            
                        }
                    }                    
                }
                else
                {
                    if (endpt != new Vector(-9999, -9999))
                        rdend = endpt;
                    else
                    {                        
                        if (rd.STLength() < validLen)
                        {
                            SqlGeometry centpt = GeoUtils.Reduce2NumPoints(rd,1000).STBuffer(0.00002).STCentroid();
                            rdend = new Vector(centpt.STX.Value, centpt.STY.Value);
                            startpt = rdend;
                            endpt = rdend;
                            shortRd = true;
                        }
                        else
                        {
                            int np = rd.STNumPoints().Value;
                            if (np == 2)
                            {
                                rdend = new Vector(rd.STStartPoint().STX.Value, rd.STStartPoint().STY.Value);                                
                                endpt = rdend;
                            }
                            else
                            {
                                double len = jnct.STDistance(rd.STPointN(np-1)).Value;
                                Vector pt = new Vector(rd.STPointN(np-1).STX.Value, rd.STPointN(np-1).STY.Value);
                                int p=np-1;
                                while (len < validLen)
                                {
                                    p--;
                                    Vector ppt = new Vector(rd.STPointN(p).STX.Value, rd.STPointN(p).STY.Value);
                                    len += rd.STPointN(p+1).STDistance(rd.STPointN(p)).Value;
                                    pt = (pt + ppt) / 2;
                                }
                                rdend = pt;
                                endpt=rdend;
                            }                            
                        }
                    }
                }
            }            
            //now the other arm
            if (imp.STLength() < validLen)
            {
                SqlGeometry centpt = GeoUtils.Reduce2NumPoints(imp,1000).STBuffer(0.00002).STCentroid();
                impend = new Vector(centpt.STX.Value, centpt.STY.Value);
            }
            else
            {
                SqlGeometry interpt = imp.STStartPoint();
                Vector pt = new Vector(interpt.STX.Value, interpt.STY.Value);
                int np = imp.STNumPoints().Value, p = 1, increment = 1;
                if ((pt - jnctpt).Length > 0.00002)
                {
                    interpt = imp.STEndPoint();
                    pt = new Vector(interpt.STX.Value, interpt.STX.Value);
                    p = np;
                    increment = -1;
                }
                if (np == 2)
                {
                    if (increment == 1)
                        impend = new Vector(imp.STEndPoint().STX.Value, imp.STEndPoint().STY.Value);
                    else
                        impend = new Vector(imp.STStartPoint().STX.Value, imp.STStartPoint().STY.Value);
                }
                else
                {
                    double len = interpt.STDistance(imp.STPointN(p + increment)).Value;
                    pt = new Vector(imp.STPointN(p + increment).STX.Value, imp.STPointN(p + increment).STY.Value);
                    while (len < validLen)
                    {
                        p = p + increment;
                        Vector ppt = new Vector(imp.STPointN(p).STX.Value, imp.STPointN(p).STY.Value);
                        len += imp.STPointN(p).STDistance(imp.STPointN(p + increment)).Value;
                        pt = (pt + ppt) / 2;
                    }
                    impend = pt;
                }
            }
            Vector rdvec = (rdend - jnctpt), impvec = (impend - jnctpt);
            if (Math.Abs(Vector.AngleBetween(rdvec, impvec)) < 10)
                return SqlGeometry.Null;
            rdvec.Normalize(); impvec.Normalize();
            Vector isopt = jnctpt + (rdvec + impvec) / 2, isovec = isopt - jnctpt;
            if (isovec.Length < 0.00002)
            {
                isovec = new Vector(rdvec.Y * -1, rdvec.X);
            }
            isovec.Normalize();
            try
            {
                return Point2LineGeometry(new Vector[2] { jnctpt - isovec * lnLen, jnctpt + isovec * lnLen });
            }
            catch
            {
                return SqlGeometry.Null;
            }
        }
        #endregion GetIsometricLine

No comments:

Post a Comment