Ejemplo n.º 1
0
        /// <summary>
        /// When <see cref="DebugReport"/> is set to true, the <see cref="Update"/> function calls
        /// this function at its end.
        /// </summary>
        private void PrintDebugReport()
        {
            try
            {
                if (!CanSeeHorizon)
                {
                    ScreenDebugger.Print("Can't see the horizon");
                }

                if (IsUpsideDown)
                {
                    ScreenDebugger.Print("Device is upside down");
                }

                if (!HasHeading)
                {
                    ScreenDebugger.Print("No heading available yet");
                }

                if (camT == null)
                {
                    ScreenDebugger.Print("  Please set the MainCamera tag on a camera in the scene");
                }
                else if (UseFakeHeading)
                {
                    ScreenDebugger.Print("  Using a static heading");
                }
                else if (!UnityInput.compass.enabled)
                {
                    ScreenDebugger.Print("  Compass not available");
                }
                else
                {
                    var deltaHeading   = CompassHeading - CameraHeading;
                    var sampleProgress = samples.Count / (float)averagingIterations;
                    ScreenDebugger.Print($"  Raw Magnetometer {CompassHeading.Label(UnitOfMeasure.Degrees, 4)}");
                    ScreenDebugger.Print($"  Camera Angle {CameraHeading.Label(UnitOfMeasure.Degrees, 4)}");
                    ScreenDebugger.Print($"  Delta {deltaHeading.Label(UnitOfMeasure.Degrees, 4)}");
                    ScreenDebugger.Print($"  To North {toNorthHeading.Label(UnitOfMeasure.Degrees, 4)}");
                    ScreenDebugger.Print($"  North {toNorthHeading.Label(UnitOfMeasure.Degrees, 4)} ({samples.Count}/{averagingIterations}: {sampleProgress.Label(UnitOfMeasure.Proportion)}, σ = {samples.StandardDeviation})");
                }
            }
            catch (Exception exp)
            {
                ScreenDebugger.PrintException(exp, "CompassRose");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Re-arranges the points in the polyline to form an orthogonal line.
        /// </summary>
        /// <param name="endPt1Heading"></param>
        /// <param name="endPt2Heading"></param>
        /// <param name="padLeft"></param>
        /// <param name="padRight"></param>
        /// <param name="padTop"></param>
        /// <param name="padBottom"></param>
        public void MakeOrthogonal(CompassHeading endPt1Heading, CompassHeading endPt2Heading, float padLeft, float padRight, float padTop, float padBottom)
        {
            PointF[] pts = this.GetPoints();

            if (pts != null && pts.Length > 1)
            {
                PointF endPt1 = pts[0];
                PointF endPt2 = pts[pts.Length - 1];

                PointF[] orthogonalPts = Geometry.CalcOrthogonalPoints(endPt1, endPt1Heading, endPt2, endPt2Heading, padLeft, padRight, padTop, padBottom);

                if (orthogonalPts != null)
                {
                    this.SetPoints(orthogonalPts);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="heading"></param>
        /// <returns></returns>
        public static System.Drawing.Size CompassHeadingToVector(CompassHeading heading)
        {
            System.Drawing.Size szVector = new System.Drawing.Size(0, 0);

            switch (heading)
            {
            case CompassHeading.East:
                szVector.Width = 1;
                break;

            case CompassHeading.West:
                szVector.Width = -1;
                break;

            case CompassHeading.North:
                szVector.Height = -1;
                break;

            case CompassHeading.South:
                szVector.Height = 1;
                break;

            case CompassHeading.Northeast:
                szVector.Height = -1;
                szVector.Width  = 1;
                break;

            case CompassHeading.Northwest:
                szVector.Height = -1;
                szVector.Width  = -1;
                break;

            case CompassHeading.Southeast:
                szVector.Height = 1;
                szVector.Width  = 1;
                break;

            case CompassHeading.Southwest:
                szVector.Height = 1;
                szVector.Width  = -1;
                break;
            }

            return(szVector);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Calculate the optimal directions for the two specified points.
        /// </summary>
        /// <param name="pt1">First point in the line</param>
        /// <param name="pt2">Second point in the line</param>
        /// <param name="pt1Heading">Heading calculated for the first point</param>
        /// <param name="pt2Heading">Heading calculated for the second point</param>
        /// <remarks>
        /// This method is used for orthogonal lines. Given two points, this method
        /// determines the compass headings for the two points. The compass heading
        /// for each point determines which direction the line will attach to the
        /// point.
        /// </remarks>
        public static void CalcEndpointDirections(PointF pt1, PointF pt2, out CompassHeading pt1Heading, out CompassHeading pt2Heading)
        {
            float horzDiff = pt1.X - pt2.Y;
            float vertDiff = pt1.X - pt2.Y;

            if (Math.Abs(horzDiff) > Math.Abs(vertDiff))
            {
                // Endpoints are further apart horizontally than vertically so make
                // the line segments attached to the endpoints horizontal.

                if (horzDiff > 0)
                {
                    pt1Heading = CompassHeading.East;
                    pt2Heading = CompassHeading.West;
                }
                else
                {
                    pt1Heading = CompassHeading.West;
                    pt2Heading = CompassHeading.East;
                }
            }
            else
            {
                // Endpoints are further apart vertically than horizontally so make
                // the line segments attached to the endpoints vertical.

                if (vertDiff > 0)
                {
                    pt1Heading = CompassHeading.South;
                    pt2Heading = CompassHeading.North;
                }
                else
                {
                    pt1Heading = CompassHeading.North;
                    pt2Heading = CompassHeading.South;
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ptEnd1"></param>
        /// <param name="endPt1Heading"></param>
        /// <param name="ptEnd2"></param>
        /// <param name="endPt2Heading"></param>
        /// <param name="padLeft"></param>
        /// <param name="padRight"></param>
        /// <param name="padTop"></param>
        /// <param name="padBottom"></param>
        /// <returns></returns>
        public static PointF[] CalcOrthogonalPoints(PointF ptEnd1, CompassHeading endPt1Heading, PointF ptEnd2, CompassHeading endPt2Heading, float padLeft, float padRight, float padTop, float padBottom)
        {
            System.Collections.ArrayList ptsOut = new System.Collections.ArrayList();

            System.Drawing.Size frontHeadingVector = Geometry.CompassHeadingToVector(endPt1Heading);
            System.Drawing.Size backHeadingVector  = Geometry.CompassHeadingToVector(endPt2Heading);

            // Maximum of 6 points from a 25x25 grid
            int[] row   = new int[] { 2, -1, -1, -1, -1, 2 };
            int[] col   = new int[] { 2, -1, -1, -1, -1, 2 };
            int   front = 0;
            int   back  = 5;

            // Determine the row and column in the grid
            // for each endpoint
            if (ptEnd1.X < ptEnd2.X)
            {
                col[front] = 1;
                col[back]  = 3;
            }
            else if (ptEnd1.X > ptEnd2.X)
            {
                col[front] = 3;
                col[back]  = 1;
            }
            else
            {
                col[front] = col[front] + (1 * frontHeadingVector.Width);
                col[back]  = col[back] + (1 * backHeadingVector.Width);
            }

            if (ptEnd1.Y < ptEnd2.Y)
            {
                row[front] = 1;
                row[back]  = 3;
            }
            else if (ptEnd1.Y > ptEnd2.Y)
            {
                row[front] = 3;
                row[back]  = 1;
            }
            else
            {
                row[front] = row[front] + (1 * frontHeadingVector.Height);
                row[back]  = row[back] + (1 * backHeadingVector.Height);
            }

            row[front + 1] = row[front] + (1 * frontHeadingVector.Height);
            col[front + 1] = col[front] + (1 * frontHeadingVector.Width);
            front++;

            row[back - 1] = row[back] + (1 * backHeadingVector.Height);
            col[back - 1] = col[back] + (1 * backHeadingVector.Width);
            back--;

            bool hasMoved     = true;
            bool isOrthogonal = (row[front] == row[back]) || (col[front] == col[back]);

            while (!isOrthogonal && front <= back)
            {
                // Determine if vectors will intersect
                PointF ptFront0         = new PointF(col[front], row[front]);
                PointF ptFront1         = new PointF(col[front] + (10 * frontHeadingVector.Width), row[front] + (10 * frontHeadingVector.Height));
                PointF ptBack0          = new PointF(col[back], row[back]);
                PointF ptBack1          = new PointF(col[back] + (10 * backHeadingVector.Width), row[back] + (10 * backHeadingVector.Height));
                bool   vectorsIntersect = Geometry.LinesIntersect(ptFront0, ptFront1, ptBack0, ptBack1);

                // Determine if vectors are pointed towards each other
                SizeF szShift         = frontHeadingVector + backHeadingVector;
                bool  vectorsOpposite = (szShift.Width == 0 && szShift.Height == 0);
                bool  vectorsSame     = (frontHeadingVector == backHeadingVector);

                if (!hasMoved || (!vectorsIntersect && !vectorsSame))
                {
                    if (vectorsOpposite || vectorsSame)
                    {
                        int turnDirection = 1;                          // 1 == counterclockwise, -1 = clockwise

                        // Change direction so that vectors are at right angles

                        if (frontHeadingVector.Width > 0)
                        {
                            // Pointing right
                            if (row[front] < row[back])
                            {
                                turnDirection = 1;
                            }
                            else
                            {
                                turnDirection = -1;
                            }
                        }
                        else if (frontHeadingVector.Width < 0)
                        {
                            // Pointing left
                            if (row[front] < row[back])
                            {
                                turnDirection = -1;
                            }
                            else
                            {
                                turnDirection = 1;
                            }
                        }
                        else if (frontHeadingVector.Height > 0)
                        {
                            // Pointing down
                            if (col[front] < col[back])
                            {
                                turnDirection = 1;
                            }
                            else
                            {
                                turnDirection = -1;
                            }
                        }
                        else if (frontHeadingVector.Height < 0)
                        {
                            // Pointing up
                            if (col[front] < col[back])
                            {
                                turnDirection = -1;
                            }
                            else
                            {
                                turnDirection = 1;
                            }
                        }

                        System.Drawing.Size szTmp = frontHeadingVector;
                        frontHeadingVector.Width  = (szTmp.Height * turnDirection);
                        frontHeadingVector.Height = (szTmp.Width * turnDirection);
                    }
                    else
                    {
                        frontHeadingVector = backHeadingVector;
                    }

                    // Go to next front point
                    row[front + 1] = row[front];
                    col[front + 1] = col[front];
                    front++;
                }

                hasMoved = false;

                // Calculate next front grid point and test to see if
                // the line segments are orthogonal yet.
                int rowFront = row[front] + (1 * frontHeadingVector.Height);
                rowFront = (rowFront < 4) ? (rowFront) : (4);
                rowFront = (rowFront > 0) ? (rowFront) : (0);
                if (rowFront != row[front])
                {
                    hasMoved   = true;
                    row[front] = rowFront;
                }

                int colFront = col[front] + (1 * frontHeadingVector.Width);
                colFront = (colFront < 4) ? (colFront) : (4);
                colFront = (colFront > 0) ? (colFront) : (0);
                if (colFront != col[front])
                {
                    hasMoved   = true;
                    col[front] = colFront;
                }

                isOrthogonal = (row[front] == row[back]) || (col[front] == col[back]);
            }

            int numEmptySlots = back - front - 1;

            numEmptySlots = (numEmptySlots > 0) ? (numEmptySlots) : (0);
            int numPoints = 6 - numEmptySlots;

            // First point is always endpoint 1
            ptsOut.Add(ptEnd1);

            // Compress array and elimate unused points

            while ((front < back) && (back < 6))
            {
                if (row[front] == -1 || col[front] == -1)
                {
                    row[front] = row[back];
                    col[front] = col[back];
                    row[back]  = -1;
                    col[back]  = -1;
                    back++;
                }
                front++;
            }

            // Look at the other four points and see if they should
            // be added.
            for (int addIdx = 1; addIdx < numPoints - 1; addIdx++)
            {
                if ((row[addIdx - 1] != row[addIdx + 1]) && (col[addIdx - 1] != col[addIdx + 1]))
                {
                    PointF ptAdd = Geometry.GetOrthogonalPoint(ptEnd1, ptEnd2, row[addIdx], col[addIdx], padLeft, padRight, padTop, padBottom);

                    bool found = false;
                    for (int ptIdx = 0; !found && ptIdx < ptsOut.Count; ptIdx++)
                    {
                        found = (ptAdd == (PointF)ptsOut[ptIdx]);
                    }

                    if (!found)
                    {
                        ptsOut.Add(ptAdd);
                    }
                }
            }

            // Last point is always endpoint 2
            ptsOut.Add(ptEnd2);

            return((PointF[])ptsOut.ToArray(typeof(PointF)));
        }
Ejemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pts"></param>
        /// <returns></returns>
        protected PointF[] MakeOrthogonal(PointF[] pts)
        {
            if (pts == null)
            {
                return(null);
            }

            int  numPts          = pts.Length;
            bool headingsChanged = false;

            PointF[] orthogonalPts = null;

            if (this.AutomaticHeadings)
            {
                CompassHeading headingPt1 = (CompassHeading)this.propertyValues["HeadingEndPoint1"];
                CompassHeading headingPt2 = (CompassHeading)this.propertyValues["HeadingEndPoint2"];
                CompassHeading newHeadingPt1;
                CompassHeading newHeadingPt2;

                Geometry.CalcEndpointDirections(pts[0], pts[numPts - 1], out newHeadingPt1, out newHeadingPt2);

                if (newHeadingPt1 != headingPt1)
                {
                    this.propertyValues["HeadingEndPoint1"] = newHeadingPt1;
                    headingsChanged = true;
                }

                if (newHeadingPt2 != headingPt2)
                {
                    this.propertyValues["HeadingEndPoint2"] = newHeadingPt2;
                    headingsChanged = true;
                }
            }

            if (headingsChanged)
            {
                orthogonalPts = Geometry.CalcOrthogonalPoints(pts[0],
                                                              this.HeadingEndPoint1,
                                                              pts[numPts - 1],
                                                              this.HeadingEndPoint2,
                                                              this.PaddingLeft,
                                                              this.PaddingRight,
                                                              this.PaddingTop,
                                                              this.PaddingBottom);
            }
            else if (!Geometry.IsOrthogonalLine(pts))
            {
                orthogonalPts = Geometry.CalcOrthogonalPoints(pts[0],
                                                              this.HeadingEndPoint1,
                                                              pts[numPts - 1],
                                                              this.HeadingEndPoint2,
                                                              this.PaddingLeft,
                                                              this.PaddingRight,
                                                              this.PaddingTop,
                                                              this.PaddingBottom);
            }
            else
            {
                orthogonalPts = pts;
            }

            return(orthogonalPts);
        }