Esempio n. 1
0
    public void HandleNewClick(Vector2 clickPoint)
    {
        /// Currently
        PolyPoint nextPoint    = null;
        bool      invalidPoint = false;
        bool      snapped      = TrySnap(clickPoint, out nextPoint, out invalidPoint); // Try to found if the player clicked on an existing point;

        if (invalidPoint)
        {
            return;
        }

        if (_pointList.Count > 0)
        {
            if (snapped == true && nextPoint == _pointList[0])
            {
                TryClosePolygon(nextPoint);
            }
            else
            {
                TryAddNewSegment(nextPoint, snapped);
            }
        }
        else
        {
            // this is the first point we draw, we don't need to check any intersections
            _pointList.Add(nextPoint);
            UpdateLinePositions(new Vector3(nextPoint.X, nextPoint.Y, Polygon.LINE_Z_OFFSET));
        }
    }
Esempio n. 2
0
    /// <summary>
    /// Test if the segment sent as parameter intersects with another one
    /// </summary>
    /// <param name="a">Start point of the new segment</param>
    /// <param name="b">End point of the new segment</param>
    /// <returns>resturns true if an intersection was found, returns false otherwise</returns>
    private bool IsNewIntersection(PolyPoint a, PolyPoint b)
    {
        //Check if the new point doesn't create a intersecting segment, we don't test the last segment because is is always has at least a point in common
        for (int i = 0; i < _segmentList.Count - 1; i++)
        {
            PolySegment seg = _segmentList[i];

            if (seg.IsInterecting(a.asVector2, b.asVector2))
            {
                // An intersection was detected
                Debug.LogWarning("Cannot create this new segment, it would intersect with segment : " + seg.ID);
                return(true);
            }
        }

        //Check for segments from existing polygons
        foreach (Polygon polygon in _cadastre.Polygons)
        {
            foreach (PolySegment seg in polygon.Segments)
            {
                if (seg.IsInterecting(a.asVector2, b.asVector2))
                {
                    // An intersection was detected
                    Debug.LogWarning("Cannot create this new segment, it would intersect with segment : " + seg.ID);
                    return(true);
                }
            }
        }

        // No intersection was detected
        return(false);
    }
Esempio n. 3
0
    private void TryClosePolygon(PolyPoint nextPoint)
    {
        // Did we draw at least 3 point ?
        if (_pointList.Count >= 3)
        {
            // Check if the new segment we're drawing will intersect with another one
            if (IsNewIntersection(GetPreviousPoint(), nextPoint) == false)
            {
                //No intersection was detected, we can create this segment and close the polygon
                PolySegment newSeg = new PolySegment(GetPreviousPoint(), nextPoint);
                _segmentList.Add(newSeg);
                UpdateLinePositions(new Vector3(nextPoint.X, nextPoint.Y, Polygon.LINE_Z_OFFSET)); // Add the last line position

                //Debug.Log("CLOSING POLY : " + _linePositionList.Count);
                ClosePolygon();
            }
            else
            {
                Debug.Log("Cannot close the polygon from this point, it would overlap with anoter one");
            }
        }
        else
        {
            Debug.Log("You are trying to close this polygon, but it doesn't have enough points");
        }
    }
        private static PolyPoint[] ReadPolyPoints([NotNull] string[] lines, ref int index)
        {
            index += 2;

            var polyCount = lines[index++].ConvInt32(15);

            if (polyCount == 0)
            {
                return(null);
            }

            var result = new PolyPoint[polyCount];

            for (var i = 0; i < polyCount; ++i)
            {
                index += 2;

                var subtick = lines[index++].ConvInt32(20);
                var posx    = lines[index++].ConvSingle(19);

                result[i] = new PolyPoint {
                    subtick = subtick,
                    posx    = posx
                };
            }

            return(result);
        }
Esempio n. 5
0
        private IEnumerable <ValidationResult> Validate(ValidationContext validationContext, ActionDBTypeEnum actionDBType)
        {
            string    retStr    = "";
            Enums     enums     = new Enums(LanguageRequest);
            PolyPoint polyPoint = validationContext.ObjectInstance as PolyPoint;

            polyPoint.HasErrors = false;

            if (polyPoint.XCoord < -180 || polyPoint.XCoord > 180)
            {
                polyPoint.HasErrors = true;
                yield return(new ValidationResult(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "XCoord", "-180", "180"), new[] { "XCoord" }));
            }

            if (polyPoint.YCoord < -90 || polyPoint.YCoord > 90)
            {
                polyPoint.HasErrors = true;
                yield return(new ValidationResult(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "YCoord", "-90", "90"), new[] { "YCoord" }));
            }

            //Z has no Range Attribute

            retStr = "";      // added to stop compiling CSSPError
            if (retStr != "") // will never be true
            {
                polyPoint.HasErrors = true;
                yield return(new ValidationResult("AAA", new[] { "AAA" }));
            }
        }
Esempio n. 6
0
            public void Deserialize(string s)
            {
                string[] strs  = s.Split('/');
                int      index = 0;

                Deserialize(strs, ref index);
                if (index < strs.Length)
                {
                    if (strs[index] == "L")
                    {
                        index++;
                        LeftCurve = new PolyPoint();
                        LeftCurve.Deserialize(strs, ref index);
                    }

                    if (index < strs.Length)
                    {
                        if (strs[index] == "R")
                        {
                            index++;
                            RightCurve = new PolyPoint();
                            RightCurve.Deserialize(strs, ref index);
                        }
                    }
                }
            }
Esempio n. 7
0
        /// <summary>
        /// Insert point to desired place of polyline
        /// recalculateBounds();
        /// Method is slow do not use this
        /// </summary>
        /// <param name="index">Index of place from zero to count</param>
        /// <param name="localX">local x - relative to left-top point</param>
        /// <param name="localY">local y - relative to left-top point</param>
        /// <param name="pointType">deprecated</param>
        protected PolyPoint insertPoint(int index, float localX, float localY, byte pointType)
        {
            PolyPoint result;

            pointsCollection.Insert(index, result = new PolyPoint(localX, localY));
            RecalculateBounds();
            return(result);
        }
Esempio n. 8
0
        /// <summary>
        /// Add point to end of polyline, need to recalculate bounds after add
        /// First point must have zero coordinate and zero type.
        /// Recalculate bounds.
        /// Method is slow do not use this.
        /// </summary>
        /// <param name="localX">local x - relative to left-top point</param>
        /// <param name="localY">local y - relative to left-top point</param>
        /// <param name="pointType">depreceted</param>
        protected PolyPoint addPoint(float localX, float localY, byte pointType)
        {
            PolyPoint result;

            pointsCollection.Add(result = new PolyPoint(localX, localY));
            RecalculateBounds();
            return(result);
        }
Esempio n. 9
0
    public PolySegment(PolyPoint a, PolyPoint b)
    {
        _a  = a;
        _b  = b;
        _id = _currentID;

        _currentID++;
        //Debug.Log("Created segment " + _id + " with points " + _a.ID + " and " + _b.ID);
    }
Esempio n. 10
0
        public override void MouseDown(int xPos, int yPos)
        {
            PolyPoint p = editorForm.SelectPoint(xPos, yPos);

            point = p;
            if (p != null)
            {
                curX = xPos;
                curY = yPos;
            }
        }
Esempio n. 11
0
    /// <summary>
    /// Check if the mouse click was close enough to an existing point to assume that the player was clicking on it.
    /// We can only snap to an existing point of the cadastre and to the first one of the polygon we are currently drawing.
    /// </summary>
    /// <param name="hit">The clicked position</param>
    /// <param name="result">Either returns and existing point, or an new intialized PolyPoint</param>
    /// <returns></returns>
    private bool TrySnap(Vector2 hit, out PolyPoint result, out bool invalidPoint)
    {
        result       = null;
        invalidPoint = false;
        float dist;

        if (_pointList.Count > 0)
        {
            // Try to snap the first of the polygon we are drawing
            dist = Vector2.Distance(hit, _pointList[0].asVector2);

            if (dist < Cadastre.SNAP_DISTANCE)
            {
                //Debug.Log("Snapping to first point, trying to close polygon");
                result = _pointList[0];
                return(true);
            }
            else if (_pointList.Count > 1) // Just a protection, for the case where the player clicks too close from an invalid point
            {
                // We check all the points from the currently drawn polygon
                for (int i = 1; i < _pointList.Count; i++)
                {
                    dist = Vector2.Distance(hit, _pointList[i].asVector2);

                    if (dist < Cadastre.SNAP_DISTANCE)
                    {
                        Debug.Log("This point is invalid : non-closing point of the currently drawn polygon");
                        result       = _pointList[i];
                        invalidPoint = true;
                        return(true);
                    }
                }
            }
        }

        // Try to snap to existing points of the cadastre's polygons
        foreach (Polygon polygon in _cadastre.Polygons)
        {
            foreach (PolyPoint existingPoint in polygon.Points)
            {
                dist = Vector2.Distance(hit, existingPoint.asVector2);
                if (dist < Cadastre.SNAP_DISTANCE)
                {
                    //Debug.Log("Existing point found, snapping to point " + existingPoint.ID);
                    result = existingPoint;
                    return(true);
                }
            }
        }

        result = _pointsPool.GetFromQueue();
        result.InitPoint(hit);
        return(false);
    }
Esempio n. 12
0
        private PolyPoint GetPseudoPoint(PolyPoint start, PolyPoint end)
        {
            float vecX = end.X - start.X;
            float vecY = end.Y - start.Y;

            float distance = (float)Math.Sqrt(vecX * vecX + vecY * vecY);

            vecX = vecX / 3;
            vecY = vecY / 3;

            return(new PolyPoint(start.X + vecX, start.Y + vecY));
        }
Esempio n. 13
0
            /* three doubly linked lists (points list,reflective points list, ears list) are
             * maintained in the "PolyPointList" array points list is a cyclic list while other
             * two aren't 0 index of the Point list is kept only for entering the lists -1 means undefined link */
            private void FillPolyPoints()
            {
                PolyPoint p = new PolyPoint()
                {
                    NextP = 1, PrevP = -1, NextEar = -1, PrevEar = -1, NextRefL = -1, PrevRefL = -1, isEar = false
                };

                polyPoints[0] = p;

                int reflective = -1;
                int convex     = -1;

                for (int i = 1; i <= pointsCount; i++)
                {
                    polyPoints[i]       = p;
                    polyPoints[i].PrevP = i == 1 ? pointsCount : i - 1;
                    polyPoints[i].NextP = i % pointsCount + 1;

                    if (IsReflective(i))
                    {
                        polyPoints[i].PrevRefL = reflective;
                        polyPoints[reflective == -1 ? 0 : reflective].NextRefL = i;
                        reflective             = i;
                        polyPoints[i].NextRefL = -1;
                        polyPoints[i].PrevEar  = -1;
                        polyPoints[i].NextEar  = -1;
                    }
                    else
                    {
                        polyPoints[i].PrevRefL = -1;
                        polyPoints[i].NextRefL = -1;
                        polyPoints[i].isEar    = true;
                        polyPoints[i].PrevEar  = convex;
                        polyPoints[convex == -1 ? 0 : convex].NextEar = i;
                        convex = i;
                        polyPoints[i].NextEar = -1;
                    }
                }

                int con = polyPoints[0].NextEar;

                while (con != -1)
                {
                    if (!IsCleanEar(con))
                    {
                        RemoveEar(con);
                    }
                    con = polyPoints[con].NextEar;
                }
            }
Esempio n. 14
0
            internal PolyPoint Clone()
            {
                PolyPoint result = new PolyPoint(x, y);

                if (LeftCurve != null)
                {
                    result.LeftCurve = LeftCurve.Clone();
                }
                if (RightCurve != null)
                {
                    result.RightCurve = RightCurve.Clone();
                }
                return(result);
            }
Esempio n. 15
0
 public override void MouseDrag(int xPos, int yPos)
 {
     if (selectedPolygon != null)
     {
         PolyPoint point = selectedPolygon.First;
         do
         {
             point.X += xPos - curX;
             point.Y += yPos - curY;
             point    = point.Next;
         } while (point != selectedPolygon.First);
         curX = xPos;
         curY = yPos;
         editorForm.Redraw();
     }
 }
Esempio n. 16
0
        private PolyPoint GetFilledRandomPolyPoint(string OmitPropName)
        {
            PolyPoint polyPoint = new PolyPoint();

            if (OmitPropName != "XCoord")
            {
                polyPoint.XCoord = GetRandomDouble(-180.0D, 180.0D);
            }
            if (OmitPropName != "YCoord")
            {
                polyPoint.YCoord = GetRandomDouble(-90.0D, 90.0D);
            }
            // should implement a Range for the property Z and type PolyPoint

            return(polyPoint);
        }
Esempio n. 17
0
    /// <summary>
    /// Tests if the middle of the segment is inside of an existing polygon.
    /// </summary>
    /// <param name="a"> Start point of the segment</param>
    /// <param name="b">End point of the segment</param>
    /// <returns>True if the segment is inside an existing polygon</returns>
    private bool IsInsideExistingPoly(PolyPoint a, PolyPoint b)
    {
        float   middleX     = (a.X + b.X) / 2.0f;
        float   middleY     = (a.Y + b.Y) / 2.0f;
        Vector2 middlePoint = new Vector2(middleX, middleY);

        Polygon poly;

        if (_cadastre.IsPointInsideExistingPolygon(middlePoint, out poly))
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Esempio n. 18
0
        public override void MouseDown(int xPos, int yPos)
        {
            PolyPoint point = editorForm.SelectPoint(xPos, yPos);

            if (point != null)
            {
                if (!point.Remove())
                {
                    System.Windows.Forms.MessageBox.Show(
                        "Wielokąt musi składać się z co najmniej 3 wierzchołków",
                        "Nie udało się usunąć punktu",
                        System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                }
                else
                {
                    editorForm.Redraw();
                }
            }
        }
Esempio n. 19
0
    //mode 'o' for P outside Q, mode 'i' for P inside Q
    static List <PolyPoint> BuildClipPoly(PolyPoint start, List <PolyPoint> P, List <PolyPoint> Q, char mode)
    {
        List <PolyPoint> poly      = new List <PolyPoint>();
        int              i         = start.ip;
        PolyPoint        curr      = start;
        List <PolyPoint> currList  = P;
        int              loopCount = 0;
        int              loopMax   = P.Count + Q.Count + 2;

        do
        {
            curr.visited = true;
            poly.Add(curr);
            bool inter = curr.isIntersection;
            bool n     = curr.inbound;
            bool p     = currList == P;
            bool o     = mode == 'o';

            if (!o && (!inter && !p || inter && !n))    //Determine the next node to visit
            {
                i        = (curr.iq + 1) % Q.Count;
                currList = Q;
            }
            else if (o && (!inter && !p || inter && n))
            {
                i        = (curr.iq + Q.Count - 1) % Q.Count;
                currList = Q;
            }
            else
            {
                i        = (curr.ip + 1) % P.Count;
                currList = P;
            }

            ++loopCount;

            curr = currList[i];
        } while (curr != start && loopCount < loopMax);

        return(poly);
    }
Esempio n. 20
0
 /// <inheritdoc/>
 public override void Deserialize(FRReader reader)
 {
     base.Deserialize(reader);
     pointsCollection.Clear();
     if (reader.HasProperty("PolyPoints"))
     {
         string polyPoints = reader.ReadStr("PolyPoints");
         foreach (string str in polyPoints.Split('|'))
         {
             string[] point = str.Split('\\');
             if (point.Length == 3)
             {
                 float f1 = float.Parse(point[0].Replace(',', '.'), CultureInfo.InvariantCulture);
                 float f2 = float.Parse(point[1].Replace(',', '.'), CultureInfo.InvariantCulture);
                 pointsCollection.Add(new PolyPoint(f1, f2));
             }
         }
     }
     else if (reader.HasProperty("PolyPoints_v2"))
     {
         string polyPoints = reader.ReadStr("PolyPoints_v2");
         foreach (string str in polyPoints.Split('|'))
         {
             PolyPoint point = new PolyPoint();
             point.Deserialize(str);
             pointsCollection.Add(point);
         }
     }
     if (reader.HasProperty("CenterX"))
     {
         center.X = reader.ReadFloat("CenterX");
     }
     if (reader.HasProperty("CenterY"))
     {
         center.Y = reader.ReadFloat("CenterY");
     }
     //recalculateBounds();
 }
Esempio n. 21
0
    private void TryAddNewSegment(PolyPoint nextPoint, bool snapped)
    {
        // Get the previous point we drew
        PolyPoint previousPoint = _pointList[_pointList.Count - 1];

        // Check if the new segment we're drawing will intersect with another one
        if (IsNewIntersection(previousPoint, nextPoint) || IsInsideExistingPoly(previousPoint, nextPoint))
        {
            // An intersection was found, if this point isn't used elsewhere, send it back to the pool
            if (snapped == false) // If we snapped on this point it means it is used by another polygon
            {
                _pointsPool.AddToPool(nextPoint);
            }
        }
        else
        {
            //No intersection was detected, we can create this segment and add this point to our temporary list
            PolySegment newSeg = new PolySegment(previousPoint, nextPoint);
            _pointList.Add(nextPoint);
            _segmentList.Add(newSeg);
            UpdateLinePositions(new Vector3(nextPoint.X, nextPoint.Y, Polygon.LINE_Z_OFFSET));
        }
    }
        private void BuildRectPath()
        {
            if (!_build)
            {
                return;
            }

            // Get center position

            double lat_home   = _lat;
            double lon_home   = _lon;
            double lat_center = _lat;
            double lon_center = _lon;
            string path_name  = txtRectPathName.Text;

/*
 *          int poi_count = _wpg.POICount();
 *          POIPoints tmp_point;
 *          for (int i = 0; i < poi_count; i++)
 *          {
 *              tmp_point = _wpg.POIPointAt(i);
 *              string name = tmp_point.name;
 *              if (home_name == name)
 *              {
 *
 *                  lat_home = tmp_point.lat;
 *                  lon_home = tmp_point.lon;
 *              }
 *          }
 */
            double lat_camera;
            double lon_camera;

            LinkedList <WayPoints> new_list = new LinkedList <WayPoints>();

            // Get Values from input Boxes

            _video = radioVideo.Checked;
            bool OnePass   = chkOnePass.Checked;
            bool startend  = chkRectHome.Checked;
            bool camerapoi = chkRectCamPOI.Checked;
            int  poi_count;

            if (camerapoi)
            {
                string poi_name = cmbRectCamPOI.GetItemText(cmbRectCamPOI.SelectedItem);
                poi_count = _wpg.POICount();

                for (int i = 0; i < poi_count; i++)
                {
                    POIPoints tmp_point = _wpg.POIPointAt(i);
                    string    name      = tmp_point.name;
                    if (poi_name == name)
                    {
                        lat_camera = tmp_point.lat;
                        lon_camera = tmp_point.lon;
                    }
                }
            }

            double box_width  = Convert.ToDouble(txtGridLength.Text);
            double box_height = Convert.ToDouble(txtGridWidth.Text);
            double altitude   = Convert.ToDouble(txtPathAlt.Text);

            _camera_width  = (2 * (Math.Tan(GPS.DegreesToRadians(_camera_angle_hor / 2)) * altitude));
            _camera_height = (2 * (Math.Tan(GPS.DegreesToRadians(_camera_angle_ver / 2)) * altitude));

            if (!Form1.Globals.UnitsMetric)
            {
                box_width      = FeetToMeters(box_width);
                box_height     = FeetToMeters(box_height);
                _camera_width  = FeetToMeters(_camera_width);
                _camera_height = FeetToMeters(_camera_height);
            }
            //double box_rotate = Convert.ToDouble(txtGridRotation.Text);
            double box_rotate = Convert.ToDouble(txtGridRotation.Text) - 90.0;
            //double box_rotate = 0.0;

            double hor_overlap = _overlap_width / 100;
            double ver_overlap = _overlap_height / 100;



            double gps_radius = Form1.Globals.gps_radius;

            int[,] actions = new int[, ] {
                { 0, 2000 }, { 1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }
            };
            int[,] no_actions = new int[, ] {
                { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }
            };

            /* Start Calculating */


            double diagonal_length = Math.Sqrt((box_width / 2) * (box_width / 2) + (box_height / 2) * (box_height / 2));
            double diagonal_angle  = GPS.RadiansToDegrees(Math.Atan((box_width / 2) / (box_height / 2)));



            /* Calculate Top, Bottom, Left & Right Center Positions */

            double lat_top;
            double lon_top;
            double lat_bottom;
            double lon_bottom;
            double lat_left;
            double lon_left;
            double lat_right;
            double lon_right;

            lat_top = GPS.GPS_Lat_BearDist(lat_center, lon_center, 0, box_height / 2, gps_radius);
            lon_top = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, 0, box_height / 2, gps_radius);

            lat_bottom = GPS.GPS_Lat_BearDist(lat_center, lon_center, 180, box_height / 2, gps_radius);
            lon_bottom = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_bottom, 180, box_height / 2, gps_radius);

            lat_left = GPS.GPS_Lat_BearDist(lat_center, lon_center, 270, box_width / 2, gps_radius);
            lon_left = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_left, 270, box_width / 2, gps_radius);

            lat_right = GPS.GPS_Lat_BearDist(lat_center, lon_center, 90, box_width / 2, gps_radius);
            lon_right = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_right, 90, box_width / 2, gps_radius);

            /* Calculate 4 corner locations */

            double lat_top_left;
            double lon_top_left;
            double lat_bottom_left;
            double lon_bottom_left;
            double lat_top_right;
            double lon_top_right;
            double lat_bottom_right;
            double lon_bottom_right;

            double[,] polypnt = new double[4, 2];

            lat_top_left  = GPS.GPS_Lat_BearDist(lat_center, lon_center, -diagonal_angle, diagonal_length, gps_radius);
            lon_top_left  = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, -diagonal_angle, diagonal_length, gps_radius);
            polypnt[0, 0] = lat_top_left;
            polypnt[0, 1] = lon_top_left;

            lat_top_right = GPS.GPS_Lat_BearDist(lat_center, lon_center, diagonal_angle, diagonal_length, gps_radius);
            lon_top_right = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, diagonal_angle, diagonal_length, gps_radius);
            polypnt[1, 0] = lat_top_right;
            polypnt[1, 1] = lon_top_right;

            lat_bottom_right = GPS.GPS_Lat_BearDist(lat_center, lon_center, 180 - diagonal_angle, diagonal_length, gps_radius);
            lon_bottom_right = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, 180 - diagonal_angle, diagonal_length, gps_radius);
            polypnt[2, 0]    = lat_bottom_right;
            polypnt[2, 1]    = lon_bottom_right;

            lat_bottom_left = GPS.GPS_Lat_BearDist(lat_center, lon_center, 180 + diagonal_angle, diagonal_length, gps_radius);
            lon_bottom_left = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, 180 + diagonal_angle, diagonal_length, gps_radius);
            polypnt[3, 0]   = lat_bottom_left;
            polypnt[3, 1]   = lon_bottom_left;

            // Rotate Points

            double new_lat, new_lon;


            for (int i = 0; i < 4; i++)
            {
                double rot_bearing  = GPS.GPS_Bearing(lat_center, lon_center, polypnt[i, 0], polypnt[i, 1]);
                double rot_distance = GPS.GPS_Distance(lat_center, lon_center, polypnt[i, 0], polypnt[i, 1], gps_radius);

                // Calculate a new location by increasing the bearing

                new_lat       = GPS.GPS_Lat_BearDist(lat_center, lon_center, rot_bearing + box_rotate, rot_distance, gps_radius);
                new_lon       = GPS.GPS_Lon_BearDist(lat_center, lon_center, new_lat, rot_bearing + box_rotate, rot_distance, gps_radius);
                polypnt[i, 0] = new_lat;
                polypnt[i, 1] = new_lon;
            }

            // Generate Rectangle Polygon


            _poly.name    = "temp";
            _poly.visible = true;
            LinkedList <PolyPoint> shape_points = new LinkedList <PolyPoint>();

            for (int i = 0; i <= 3; i++)
            {
                PolyPoint newpnt = new PolyPoint();
                newpnt.lat = polypnt[i, 0];
                newpnt.lon = polypnt[i, 1];
                newpnt.alt = 10;
                shape_points.AddLast(newpnt);
            }
            PolyPoint pnt = new PolyPoint();

            pnt.lat = polypnt[0, 0];
            pnt.lon = polypnt[0, 1];
            pnt.alt = 10;
            shape_points.AddLast(pnt);
            _poly.points = shape_points;

            // Get Direction Flags

            double drone_heading = 90.0;
            int    ver_dir_flag  = 1;

            if (radioUpLeft.Checked)
            {
                ver_dir_flag = 1;
            }
            if (radioUpRight.Checked)
            {
                ver_dir_flag = 1; drone_heading = 270;
            }
            if (radioLowLeft.Checked)
            {
                ver_dir_flag = -1;
            }
            if (radioLowRight.Checked)
            {
                ver_dir_flag = -1; drone_heading = 270;
            }

            // Grid Calculations

            double lat_next;
            double next_lon_start;
            double next_lon_end;
            double lon_tmp;
            double row_increment = _camera_width * (1.0 - hor_overlap);
            int    num_rows      = Convert.ToInt16(box_height / row_increment);

            box_rotate = 0.0;

            // Create Waypoints

            int    gimblemode  = 2;
            double gimblepitch = -90.0;
            double curvesize   = 0;
            double rotdir      = 0;

            if (OnePass | num_rows == 1)
            {
                if (startend)
                {
                    _wp.Add_Waypoint_List(new_list, lat_home, lon_home, altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, no_actions);
                }

                if (radioUpLeft.Checked | radioLowLeft.Checked)
                {
                    _wp.Add_Leg_List(new_list, lat_left, lon_left, lat_right, lon_right,
                                     altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                }
                else
                {
                    _wp.Add_Leg_List(new_list, lat_right, lon_right, lat_left, lon_left,
                                     altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                }

                if (startend)
                {
                    _wp.Add_Waypoint_List(new_list, lat_home, lon_home, altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, no_actions);
                }
            }
            else
            {
                /* More than one row */

                if (num_rows == 2)
                {
                    double offset            = row_increment;
                    double diagonal_length_2 = Math.Sqrt((box_width / 2) * (box_width / 2) + (offset / 2) * (offset / 2));
                    double diagonal_angle_2  = GPS.RadiansToDegrees(Math.Atan((box_width / 2) / (offset / 2)));

                    lat_top_left = GPS.GPS_Lat_BearDist(lat_center, lon_center, -diagonal_angle_2 + box_rotate, diagonal_length_2, gps_radius);
                    lon_top_left = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, -diagonal_angle_2 + box_rotate, diagonal_length_2, gps_radius);

                    lat_top_right = GPS.GPS_Lat_BearDist(lat_center, lon_center, diagonal_angle_2 + box_rotate, diagonal_length_2, gps_radius);
                    lon_top_right = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, diagonal_angle_2 + box_rotate, diagonal_length_2, gps_radius);

                    lat_bottom_left = GPS.GPS_Lat_BearDist(lat_center, lon_center, 180 + diagonal_angle_2 + box_rotate, diagonal_length_2, gps_radius);
                    lon_bottom_left = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, 180 + diagonal_angle_2 + box_rotate, diagonal_length_2, gps_radius);

                    lat_bottom_right = GPS.GPS_Lat_BearDist(lat_center, lon_center, 180 - diagonal_angle_2 + box_rotate, diagonal_length_2, gps_radius);
                    lon_bottom_right = GPS.GPS_Lon_BearDist(lat_center, lon_center, lat_top, 180 - diagonal_angle_2 + box_rotate, diagonal_length_2, gps_radius);

                    if (startend)
                    {
                        _wp.Add_Waypoint_List(new_list, lat_home, lon_home, altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, no_actions);
                    }
                    if (radioLowLeft.Checked)
                    {
                        _wp.Add_Leg_List(new_list, lat_bottom_left, lon_bottom_left, lat_bottom_right, lon_bottom_right,
                                         altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                        _wp.Add_Leg_List(new_list, lat_top_right, lon_top_right, lat_top_left, lon_top_left,
                                         altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                    }

                    if (radioLowRight.Checked)
                    {
                        _wp.Add_Leg_List(new_list, lat_bottom_right, lon_bottom_right, lat_bottom_left, lon_bottom_left,
                                         altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                        _wp.Add_Leg_List(new_list, lat_top_left, lon_top_left, lat_top_right, lon_top_right,
                                         altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                    }

                    if (radioUpLeft.Checked)
                    {
                        _wp.Add_Leg_List(new_list, lat_top_left, lon_top_left, lat_top_right, lon_top_right,
                                         altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                        _wp.Add_Leg_List(new_list, lat_bottom_right, lon_bottom_right, lat_bottom_left, lon_bottom_left,
                                         altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                    }

                    if (radioUpRight.Checked)
                    {
                        _wp.Add_Leg_List(new_list, lat_top_right, lon_top_right, lat_top_left, lon_top_left,
                                         altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                        _wp.Add_Leg_List(new_list, lat_bottom_left, lon_bottom_left, lat_bottom_right, lon_bottom_right,
                                         altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                    }

                    if (startend)
                    {
                        _wp.Add_Waypoint_List(new_list, lat_home, lon_home, altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, no_actions);
                    }
                }
                else
                {
                    /* More than 2 rows */

                    double row_span = (num_rows - 1) * row_increment;
                    lat_top    = GPS.GPS_Lat_BearDist(lat_center, lon_center, 0 + box_rotate, row_span / 2, gps_radius);
                    lat_bottom = GPS.GPS_Lat_BearDist(lat_center, lon_center, 180 + box_rotate, row_span / 2, gps_radius);
                    double degrees_per_foot = (lat_top - lat_bottom) / row_span;

                    if (startend)
                    {
                        _wp.Add_Waypoint_List(new_list, lat_home, lon_home, altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, no_actions);
                    }

                    int row_count = 0;
                    next_lon_start = 0.0;
                    next_lon_end   = 0.0;
                    double lat_current = 0.0;

                    do
                    {
                        /* First Row */

                        if (row_count == 0)
                        {
                            if (radioLowLeft.Checked)
                            {
                                _wp.Add_Leg_List(new_list, lat_bottom, lon_left, lat_bottom, lon_right,
                                                 altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                                lat_current    = lat_bottom;
                                next_lon_start = lon_right;
                                next_lon_end   = lon_left;
                            }
                            if (radioLowRight.Checked)
                            {
                                _wp.Add_Leg_List(new_list, lat_bottom, lon_right, lat_bottom, lon_left,
                                                 altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                                lat_current    = lat_bottom;
                                next_lon_start = lon_left;
                                next_lon_end   = lon_right;
                            }
                            if (radioUpLeft.Checked)
                            {
                                _wp.Add_Leg_List(new_list, lat_top, lon_left, lat_top, lon_right,
                                                 altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                                lat_current    = lat_top;
                                next_lon_start = lon_right;
                                next_lon_end   = lon_left;
                            }
                            if (radioUpRight.Checked)
                            {
                                _wp.Add_Leg_List(new_list, lat_top, lon_right, lat_top, lon_left,
                                                 altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                                lat_current    = lat_top;
                                next_lon_start = lon_left;
                                next_lon_end   = lon_right;
                            }
                        }
                        else
                        {
                            /* Output 2nd, 3rd, etc. */

                            //lat_current = Globals.waypoints[Globals.waypoint_count - 1, 0];
                            lat_next = lat_current + (-ver_dir_flag * (degrees_per_foot * row_increment));
                            _wp.Add_Leg_List(new_list, lat_next, next_lon_start, lat_next, next_lon_end,
                                             altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, actions, _video, _camera_height, _overlap_height);
                            lon_tmp        = next_lon_start;
                            next_lon_start = next_lon_end;
                            next_lon_end   = lon_tmp;
                            lat_current    = lat_next;
                        }
                        row_count++;
                    } while (row_count < num_rows);

                    if (startend)
                    {
                        _wp.Add_Waypoint_List(new_list, lat_home, lon_home, altitude, drone_heading, curvesize, rotdir, gimblemode, gimblepitch, no_actions);
                    }
                }
            }


            // Rotate WayPoints about Center

            int    count = 0;
            double lat, lon, alt, head, bearing, distance;
            double lat_first = lat_center;
            double lon_first = lon_center;
            LinkedListNode <WayPoints> node = new_list.First;
            //LinkedListNode<WayPoints> next_node = node.Next;
            LinkedListNode <WayPoints> save_node;

            //lat_first = node.Value.lat;
            //lon_first = node.Value.lon;
            //alt_first = node.Value.alt;
            //head_first = node.Value.head;

            while (node != null)
            {
                if (count != 0)
                {
                    // Get Waypoint values

                    lat         = node.Value.lat;
                    lon         = node.Value.lon;
                    alt         = node.Value.alt;
                    head        = node.Value.head;
                    gimblemode  = node.Value.gimblemode;
                    gimblepitch = node.Value.gimblepitch;
                    curvesize   = node.Value.curvesize;
                    rotdir      = node.Value.rotationdir;
                    actions     = node.Value.actions;
                    box_rotate  = Convert.ToDouble(txtGridRotation.Text) - 90.0;

                    // Calculate Bearing and distance from first waypoint

                    bearing  = GPS.GPS_Bearing(lat_first, lon_first, lat, lon);
                    distance = GPS.GPS_Distance(lat_first, lon_first, lat, lon, gps_radius);

                    // Calculate a new location by increasing the bearing

                    new_lat = GPS.GPS_Lat_BearDist(lat_first, lon_first, bearing + box_rotate, distance, gps_radius);
                    new_lon = GPS.GPS_Lon_BearDist(lat_first, lon_first, new_lat, bearing + box_rotate, distance, gps_radius);

                    // Set new waypoint

                    WayPoints new_wp = new WayPoints();
                    new_wp.lat         = new_lat;
                    new_wp.lon         = new_lon;
                    new_wp.alt         = alt;
                    new_wp.head        = head + box_rotate;
                    new_wp.gimblemode  = gimblemode;
                    new_wp.gimblepitch = gimblepitch;
                    new_wp.curvesize   = curvesize;
                    new_wp.rotationdir = rotdir;
                    new_wp.actions     = actions;
                    save_node          = node;
                    new_list.AddBefore(node, new_wp);
                    node = node.Next;
                    new_list.Remove(save_node);
                }
                count++;
            }

            // Redo start and end points

            if (startend)
            {
                WayPoints wp = new_list.First.Value;
                new_list.RemoveFirst();
                wp.lat = lat_home;
                wp.lon = lon_home;
                new_list.AddFirst(wp);
                wp = new_list.Last.Value;
                new_list.RemoveLast();
                wp.lat = lat_home;
                wp.lon = lon_home;
                new_list.AddLast(wp);
            }

            // Save Path

            if (_new_path & _first_pass)
            {
                path_name = txtRectPathName.Text;
                if (path_name == "")
                {
                    path_name = "Untitled - Rectangular";
                }
                _path.Add_Path(_wpg, _gmap, path_name, "Rectangular", new_list);
                Path newpath = _wpg.PathAt(_wpg.PathCount() - 1);
                _current_intid = newpath.internal_id;
                _first_pass    = false;
            }
            else
            {
                _wpg.ChangePathWPIntId(_current_intid, new_list);
                //Models.Path path = _wpg.PathAt(_current_path_index);
                //_gmap.Delete_gMapPath(path);
                //_gmap.Add_gMapPath(path, false);
            }

            _gmap.ReDrawgMap();
            _gmap.Add_gMapPoly(_poly, true);
        }
Esempio n. 23
0
    public static void Clip2D(Vector3[] P, Vector3[] Q, out List <Vector3[]> Pi, out List <Vector3[]> Po)
    {
        P = Clockwise(P);
        Q = Clockwise(Q);
        int i, j;

        bool[] pIn = new bool[P.Length];

        List <Clip2DIntersection> intersections = new List <Clip2DIntersection>();

        for (i = 0; i < P.Length; ++i)
        {
            for (j = 0; j < Q.Length; ++j)
            {
                int   iN     = (i + 1) % P.Length;
                int   jN     = (j + 1) % Q.Length;
                float interA = IntersectionScalar(P[i], P[iN] - P[i], Q[j], Q[jN] - Q[j]);
                float interB = IntersectionScalar(Q[j], Q[jN] - Q[j], P[i], P[iN] - P[i]);
                if (0 <= interA && interA <= 1 && 0 <= interB && interB <= 1)
                {
                    bool outIn = Cross2(Q[jN] - Q[j], P[i] - Q[j]) >= 0 && Cross2(Q[jN] - Q[j], P[iN] - P[i]) < 0;
                    intersections.Add(new Clip2DIntersection(P[i] + (P[iN] - P[i]) * interA, i, j, outIn));
                }
            }
        }


        List <PolyPoint> Pann = new List <PolyPoint>(), Qann = new List <PolyPoint>(), interPoints = new List <PolyPoint>(); //P and Q annotated with intersections

        for (i = 0; i < intersections.Count; ++i)
        {
            PolyPoint p = new PolyPoint(intersections[i].position);
            p.inbound        = intersections[i].inbound;
            p.isIntersection = true;
            interPoints.Add(p);
        }

        i = 0;
        while (i < P.Length)
        {
            int       iN = (i + 1) % P.Length;
            PolyPoint p  = new PolyPoint(P[i]);
            p.ip = Pann.Count;
            Pann.Add(p);

            List <Clip2DIntersection> segInter = intersections.FindAll(x => x.ip == i);
            segInter.Sort((x, y) => (int)Mathf.Sign(Vector3.Dot(P[iN] - P[i], x.position - P[i]) - Vector3.Dot(P[iN] - P[i], y.position - P[i])));

            foreach (Clip2DIntersection inter in segInter)
            {
                PolyPoint interP = interPoints.Find(x => x.position == inter.position);
                interP.ip = Pann.Count;
                Pann.Add(interP);
            }
            ++i;
        }

        i = 0;
        while (i < Q.Length)
        {
            int       iN = (i + 1) % Q.Length;
            PolyPoint q  = new PolyPoint(Q[i]);
            q.iq = Qann.Count;
            Qann.Add(q);

            List <Clip2DIntersection> segInter = intersections.FindAll(x => x.iq == i);
            segInter.Sort((x, y) => (int)Mathf.Sign(Vector3.Dot(Q[iN] - Q[i], x.position - Q[i]) - Vector3.Dot(Q[iN] - Q[i], y.position - Q[i])));

            foreach (Clip2DIntersection inter in segInter)
            {
                PolyPoint interP = interPoints.Find(x => x.position == inter.position);
                interP.iq = Qann.Count;
                Qann.Add(interP);
            }
            ++i;
        }

        Pi = new List <Vector3[]>();
        Po = new List <Vector3[]>();

        foreach (PolyPoint p in interPoints)
        {
            if (p.inbound && !p.visited)
            {
                List <PolyPoint> res = BuildClipPoly(p, Pann, Qann, 'i');
                Pi.Add(new Vector3[res.Count]);
                for (i = 0; i < res.Count; ++i)
                {
                    Pi[Pi.Count - 1][i] = res[i].position;
                }
            }
        }

        foreach (PolyPoint p in interPoints)
        {
            p.visited = false;
        }
        foreach (PolyPoint p in interPoints)
        {
            if (p.inbound && !p.visited)
            {
                List <PolyPoint> res = BuildClipPoly(p, Pann, Qann, 'o');
                Po.Add(new Vector3[res.Count]);
                for (i = 0; i < res.Count; ++i)
                {
                    Po[Po.Count - 1][i] = res[i].position;
                }
            }
        }
    }
 public PolyPointTest()
 {
     polyPoint = new PolyPoint();
 }
Esempio n. 25
0
    private void FillLists()
    {
        /*
         * three doubly linked lists (points list,reflective points list, ears list) are
         * maintained in the "PolyPointList" arry.
         * points list is a cyclic list while other two arent.
         * 0 index of the Point list is kept only for entering the lists
         * -1 means undefined link
         */
        PolyPoint p = new PolyPoint();

        PolyPointList[0]           = p;
        PolyPointList[0].PointNext = 1;
        PolyPointList[0].PointPrev = -1;
        PolyPointList[0].EarNext   = -1;
        PolyPointList[0].EarPrev   = -1;
        PolyPointList[0].ReflNext  = -1;
        PolyPointList[0].ReflPrev  = -1;
        PolyPointList[0].isEar     = false;

        int T_Reflective = -1;
        int T_Convex     = -1;

        for (int i = 1; i <= Pointcount; i++)
        {
            PolyPointList[i] = p;

            if (i == 1)
            {
                PolyPointList[i].PointPrev = Pointcount;
            }
            else
            {
                PolyPointList[i].PointPrev = i - 1;
            }

            PolyPointList[i].PointNext = (i % Pointcount) + 1;

            if (isReflective(i))
            {
                PolyPointList[i].ReflPrev = T_Reflective;

                if (T_Reflective == -1)
                {
                    PolyPointList[0].ReflNext = i;
                }
                else
                {
                    PolyPointList[T_Reflective].ReflNext = i;
                }

                T_Reflective = i;
                PolyPointList[i].ReflNext = -1;

                PolyPointList[i].EarPrev = -1;
                PolyPointList[i].EarNext = -1;
            }
            else
            {
                PolyPointList[i].ReflPrev = -1;
                PolyPointList[i].ReflNext = -1;
                PolyPointList[i].isEar    = true;

                PolyPointList[i].EarPrev = T_Convex;

                if (T_Convex == -1)
                {
                    PolyPointList[0].EarNext = i;
                }
                else
                {
                    PolyPointList[T_Convex].EarNext = i;
                }

                T_Convex = i;

                PolyPointList[i].EarNext = -1;
            }
        }

        int Con = PolyPointList[0].EarNext;

        while (Con != -1)
        {
            if (!isCleanEar(Con))
            {
                RemoveEar(Con);
            }
            Con = PolyPointList[Con].EarNext;
        }
    }
Esempio n. 26
0
        private void btnReadKMLPoly_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                /* Get kml filename */

                string kml_file = openFileDialog1.FileName;

                if (kml_file.Contains(".kml"))
                {
                    System.IO.TextReader    stream = System.IO.File.OpenText(kml_file);
                    SharpKml.Engine.KmlFile file   = KmlFile.Load(stream);
                    Kml _kml = file.Root as Kml;

                    SharpKml.Dom.Placemark[] tempPlaceMarks = new SharpKml.Dom.Placemark[1000];
                    SharpKml.Dom.Placemark   tmp_placemark  = new SharpKml.Dom.Placemark();
                    CoordinateCollection     coordinates    = new CoordinateCollection();
                    Form1.Globals.poly_point_count = 0;

                    if (_kml != null)
                    {
                        SharpKml.Base.Vector vector;
                        double lat;
                        double lon;
                        double alt;
                        string name = "";

                        foreach (var placemark in _kml.Flatten().OfType <SharpKml.Dom.Placemark>())
                        {
                            name = placemark.Name;
                        }

                        Models.Shape shape = new Models.Shape();
                        shape.name = name;

                        foreach (var linering in _kml.Flatten().OfType <LinearRing>())
                        {
                            coordinates = linering.Coordinates;
                            int num = coordinates.Count;

                            LinkedList <PolyPoint> shape_points = new LinkedList <PolyPoint>();

                            for (int i = 0; i < num; i++)
                            {
                                PolyPoint point = new PolyPoint();
                                vector    = coordinates.ElementAt(i);
                                lat       = vector.Latitude;
                                lon       = vector.Longitude;
                                alt       = (double)vector.Altitude;
                                point.lat = lat;
                                point.lon = lon;
                                point.alt = alt;
                                shape_points.AddLast(point);
                                //dgvWaypoints.Rows.Add(Globals.waypoint_count, Convert.ToString(lat), Convert.ToString(lon), Convert.ToString(30));
                            }
                            shape.points  = shape_points;
                            shape.visible = true;
                            _wpg.AddShape(shape);
                            _gmap.Add_gMapPoly(shape, true);
                        }
                    }
                }
                GMAPTree.Update_GMapTree(_wpg, _treeview);
                this.Close();
            }
        }
Esempio n. 27
0
        private static void ReadPolygons(BinaryReader reader, INode parent, ICollection<INode> container)
        {
            var groupCount = reader.ReadInt32();
            for (var i = 0; i < groupCount; i++)
            {
                var polyGroup = new PolygonGroup {Name = reader.ReadString(), Parent = parent};
                var polyCount = reader.ReadInt32();
                polyGroup.Type = "PolygonGroup";

                for (var j = 0; j < polyCount; j++)
                {
                    var polygon = new Polygon {Name = reader.ReadString(), Parent = polyGroup};
                    var pointCount = reader.ReadInt32();
                    polygon.Type = "Polygon";
                    for (var k = 0; k < pointCount; k++)
                    {
                        var point = new PolyPoint(reader.ReadInt32(), reader.ReadInt32()) {Parent = polygon};
                        reader.ReadInt32(); // MappedX
                        reader.ReadInt32(); // MappedY
                        point.Type = "PolyPoint";
                        polygon.Children.Add(point);
                    }
                    if (polygon.Children.Count > Settings.Default.MaxVerts)
                    {
                        Debug.Assert(false, string.Format("{0} has more than {1} vets in group {2} in {3} image", polygon.Name,Settings.Default.MaxVerts, polyGroup.Name, parent.Name));
                    }
                    polyGroup.Children.Add(polygon);
                }
                container.Add(polyGroup);
            }
        }
Esempio n. 28
0
 public override void MouseUp(int xPos, int yPos)
 {
     point = null;
 }
Esempio n. 29
0
 public bool Near(PolyPoint p)
 {
     return((p != null) && (Math.Abs(x - p.x) < 0.0001) && (Math.Abs(y - p.y) < 0.0001));
 }
Esempio n. 30
0
        /// <summary>
        /// Calculate GraphicsPath for draw to page
        /// </summary>
        /// <param name="pen">Pen for lines</param>
        /// <param name="left">Left boundary</param>
        /// <param name="top">Top boundary</param>
        /// <param name="right">Right boundary</param>
        /// <param name="bottom">Bottom boundary</param>
        /// <param name="scaleX">scale by width</param>
        /// <param name="scaleY">scale by height</param>
        /// <returns>Always returns a non-empty path</returns>
        public GraphicsPath GetPath(Pen pen, float left, float top, float right, float bottom, float scaleX, float scaleY)
        {
            if (pointsCollection.Count == 0)
            {
                GraphicsPath result = new GraphicsPath();
                result.AddLine(left * scaleX, top * scaleX, (right + 1) * scaleX, (bottom + 1) * scaleX);
                return(result);
            }
            else if (pointsCollection.Count == 1)
            {
                GraphicsPath result = new GraphicsPath();
                left = left + CenterX + pointsCollection[0].X;
                top  = top + CenterY + pointsCollection[0].Y;
                result.AddLine(left * scaleX, top * scaleX, (left + 1) * scaleX, (top + 1) * scaleX);
                return(result);
            }

            List <PointF> aPoints    = new List <PointF>();
            List <byte>   pointTypes = new List <byte>();

            PolyPoint prev  = null;
            PolyPoint point = pointsCollection[0];

            aPoints.Add(new PointF((point.X + left + center.X) * scaleX, (point.Y + top + center.Y) * scaleY));
            pointTypes.Add(0);

            int count = pointsCollection.Count;

            if (this is PolygonObject)
            {
                count++;
            }

            for (int i = 1; i < count; i++)
            {
                prev  = point;
                point = pointsCollection[i];

                //is bezier?
                if (prev.RightCurve != null || point.LeftCurve != null)
                {
                    if (prev.RightCurve != null)
                    {
                        aPoints.Add(new PointF((prev.X + left + center.X + prev.RightCurve.X) * scaleX, (prev.Y + top + center.Y + prev.RightCurve.Y) * scaleY));
                        pointTypes.Add(3);
                    }
                    else
                    {
                        PolyPoint pseudo = GetPseudoPoint(prev, point);
                        aPoints.Add(new PointF((pseudo.X + left + center.X) * scaleX, (pseudo.Y + top + center.Y) * scaleY));
                        pointTypes.Add(3);
                    }

                    if (point.LeftCurve != null)
                    {
                        aPoints.Add(new PointF((point.X + left + center.X + point.LeftCurve.X) * scaleX, (point.Y + top + center.Y + point.LeftCurve.Y) * scaleY));
                        pointTypes.Add(3);
                    }
                    else
                    {
                        PolyPoint pseudo = GetPseudoPoint(point, prev);
                        aPoints.Add(new PointF((pseudo.X + left + center.X) * scaleX, (pseudo.Y + top + center.Y) * scaleY));
                        pointTypes.Add(3);
                    }

                    aPoints.Add(new PointF((point.X + left + center.X) * scaleX, (point.Y + top + center.Y) * scaleY));
                    pointTypes.Add(3);
                }
                else
                {
                    aPoints.Add(new PointF((point.X + left + center.X) * scaleX, (point.Y + top + center.Y) * scaleY));
                    pointTypes.Add(1);
                }
            }
            return(new GraphicsPath(aPoints.ToArray(), pointTypes.ToArray()));
        }
Esempio n. 31
0
        /// <summary>
        /// Recalculate position and size of element
        /// </summary>
        public void RecalculateBounds()
        {
            if (pointsCollection.Count > 0)
            {
                // init
                PolyPoint prev   = null;
                PolyPoint point  = pointsCollection[0];
                float     left   = point.X;
                float     top    = point.Y;
                float     right  = point.X;
                float     bottom = point.Y;
                int       count  = pointsCollection.Count;
                if (this is PolygonObject)
                {
                    count++;
                }

                // stage 1 calculate min bounds

                foreach (PolyPoint pnt in pointsCollection)
                {
                    if (pnt.X < left)
                    {
                        left = pnt.X;
                    }
                    else if (pnt.X > right)
                    {
                        right = pnt.X;
                    }

                    if (pnt.Y < top)
                    {
                        top = pnt.Y;
                    }
                    else if (pnt.Y > bottom)
                    {
                        bottom = pnt.Y;
                    }
                }

                // stage 2 check if one of bezier way point is outside

                for (int i = 1; i < count; i++)
                {
                    prev  = point;
                    point = pointsCollection[i];

                    bool haveToCalculate = false;


                    PolyPoint p_1 = null;
                    PolyPoint p_2 = null;

                    if (prev.RightCurve != null)
                    {
                        p_1 = new PolyPoint(prev.X + prev.RightCurve.X, prev.Y + prev.RightCurve.Y);

                        if (p_1.X < left)
                        {
                            haveToCalculate = true;
                        }
                        else if (p_1.X > right)
                        {
                            haveToCalculate = true;
                        }

                        if (p_1.Y < top)
                        {
                            haveToCalculate = true;
                        }
                        else if (p_1.Y > bottom)
                        {
                            haveToCalculate = true;
                        }
                    }

                    if (point.LeftCurve != null)
                    {
                        p_2 = new PolyPoint(point.X + point.LeftCurve.X, point.Y + point.LeftCurve.Y);
                        if (p_2.X < left)
                        {
                            haveToCalculate = true;
                        }
                        else if (p_2.X > right)
                        {
                            haveToCalculate = true;
                        }

                        if (p_2.Y < top)
                        {
                            haveToCalculate = true;
                        }
                        else if (p_2.Y > bottom)
                        {
                            haveToCalculate = true;
                        }
                    }

                    if (haveToCalculate)
                    {
                        if (p_1 == null)
                        {
                            p_1 = GetPseudoPoint(prev, point);
                        }

                        if (p_2 == null)
                        {
                            p_2 = GetPseudoPoint(point, prev);
                        }


                        // now calculate extrema

                        // x
                        float delta = RecalculateBounds_Delta(prev.X, p_1.X, p_2.X, point.X);
                        if (delta > 0)
                        {
                            delta = (float)Math.Sqrt(delta);
                            float t_1 = RecalculateBounds_Solve(prev.X, p_1.X, p_2.X, point.X, -delta);
                            if (0 < t_1 && t_1 < 1)
                            {
                                float x = RecalculateBounds_Value(prev.X, p_1.X, p_2.X, point.X, t_1);
                                if (x < left)
                                {
                                    left = x;
                                }
                                else if (x > right)
                                {
                                    right = x;
                                }
                            }
                            float t_2 = RecalculateBounds_Solve(prev.X, p_1.X, p_2.X, point.X, delta);
                            if (0 < t_2 && t_2 < 1)
                            {
                                float x = RecalculateBounds_Value(prev.X, p_1.X, p_2.X, point.X, t_2);
                                if (x < left)
                                {
                                    left = x;
                                }
                                else if (x > right)
                                {
                                    right = x;
                                }
                            }
                        }

                        // y
                        delta = RecalculateBounds_Delta(prev.Y, p_1.Y, p_2.Y, point.Y);
                        if (delta > 0)
                        {
                            delta = (float)Math.Sqrt(delta);
                            float t_1 = RecalculateBounds_Solve(prev.Y, p_1.Y, p_2.Y, point.Y, -delta);
                            if (0 < t_1 && t_1 < 1)
                            {
                                float y = RecalculateBounds_Value(prev.Y, p_1.Y, p_2.Y, point.Y, t_1);
                                if (y < top)
                                {
                                    top = y;
                                }
                                else if (y > bottom)
                                {
                                    bottom = y;
                                }
                            }
                            float t_2 = RecalculateBounds_Solve(prev.Y, p_1.Y, p_2.Y, point.Y, delta);
                            if (0 < t_2 && t_2 < 1)
                            {
                                float y = RecalculateBounds_Value(prev.Y, p_1.Y, p_2.Y, point.Y, t_2);
                                if (y < top)
                                {
                                    top = y;
                                }
                                else if (y > bottom)
                                {
                                    bottom = y;
                                }
                            }
                        }
                    }
                }


                // update
                float centerX = center.X;
                float centerY = center.Y;
                center.X    = -left;
                center.Y    = -top;
                base.Left  += left + centerX;
                base.Top   += top + centerY;
                base.Height = bottom - top;
                base.Width  = right - left;
            }
            else
            {
                CenterX     = 0;
                CenterY     = 0;
                base.Width  = 5;
                base.Height = 5;
            }
        }