Ejemplo n.º 1
0
        public _Point Normalize(_Point p)
        {
            double sum = Math.Sqrt(p.X * p.X + p.Y * p.Y);
            _Point p2  = new _Point(p.X / sum, p.Y / sum);

            return(p2);
        }
Ejemplo n.º 2
0
        //TODO: this seems redundant, line has the same function (connectspoint)
        private static _Point FindCommonPointOnTwoLines(_Line firstMyLine, _Line nextMyLine)
        {
            _Point commonMyPoint = null;

            if (Equals(firstMyLine.StartPoint, nextMyLine.StartPoint))
            {
                commonMyPoint = nextMyLine.StartPoint;
            }
            if (Equals(firstMyLine.StartPoint, nextMyLine.EndPoint))
            {
                commonMyPoint = nextMyLine.EndPoint;
            }
            if (Equals(firstMyLine.EndPoint, nextMyLine.StartPoint))
            {
                commonMyPoint = nextMyLine.StartPoint;
            }
            if (Equals(firstMyLine.EndPoint, nextMyLine.EndPoint))
            {
                commonMyPoint = nextMyLine.EndPoint;
            }

            if (commonMyPoint == null)
            {
                throw new Exception("no common point");
            }
            return(commonMyPoint);
        }
Ejemplo n.º 3
0
        private void DoSomething(ref _Room room, List <_Line> overlappedlistinroom)
        {
            List <_Point> thesepointsexist = new List <_Point>();

            foreach (_Line line in overlappedlistinroom)
            {
                if (!thesepointsexist.Contains(line.StartPoint))
                {
                    thesepointsexist.Add(line.StartPoint);
                }
                if (!thesepointsexist.Contains(line.EndPoint))
                {
                    thesepointsexist.Add(line.EndPoint);
                }
            }
            thesepointsexist = thesepointsexist.OrderBy(i => i.X).OrderBy(i => i.Y).ToList();
            List <_Line> newLines = new List <_Line>();

            for (var index = 0; index < thesepointsexist.Count - 1; index += 1)
            {
                _Point p0 = thesepointsexist[index];
                _Point p1 = thesepointsexist[index + 1];
                newLines.Add(new _Line(p0, p1));
            }

            _Line l = overlappedlistinroom.Last();

            foreach (_Line newLine in newLines)
            {
                newLine.relatedrooms = l.relatedrooms;
            }

            room.Lines.RemoveAll(i => overlappedlistinroom.Contains(i));
            room.Lines.AddRange(newLines);
        }
Ejemplo n.º 4
0
        public double CalculateProportion()
        {
            double proportion = 0.0;

            try {
                List <_Point> bp = GetPoints();

                double[] X = bp.Select(i => i.X).ToArray();
                double[] Y = bp.Select(i => i.Y).ToArray();

                if (!X.Any() || !Y.Any())
                {
                    throw new Exception("bad proportion");
                }
                _Point max = new _Point(X.Max(), Y.Max());
                _Point min = new _Point(X.Min(), Y.Min());


                //TODO: avoid <80cm room sides

                proportion = (max.X - min.X) / (max.Y - min.Y);
                if (proportion < 1 && Math.Abs(proportion) > 0.01)
                {
                    proportion = 1 / proportion;
                }
            }
            catch (Exception exception) {
                Logger.WriteLog(exception);
            }

            return(proportion);
        }
Ejemplo n.º 5
0
        private bool AreNotParralel(_Line lineInLoop, _Line lineToMove)
        {
            _Point loopNormal = lineInLoop.GetNV(true);
            _Point moveNormal = lineToMove.GetNV(true);

            return(!(Equals(loopNormal, moveNormal) || Equals(loopNormal, moveNormal * -1) || Equals(loopNormal * -1, moveNormal)));
        }
Ejemplo n.º 6
0
        public static bool IsOnLineButNotEndPoint(_Point myPoint, _Line myLine)
        {
            if (myLine.EndPoint.Equals(myPoint) || myLine.StartPoint.Equals(myPoint))
            {
                return(false);
            }

            return(PointOnLine2D(myPoint, myLine.StartPoint, myLine.EndPoint));
        }
Ejemplo n.º 7
0
        public _Line Normalize(_Line _Line)
        {
            _Point sMyPoint    = _Line.StartPoint;
            _Point eMyPoint    = _Line.EndPoint;
            double length      = _Line.GetLength();
            _Point neweMyPoint = new _Point(((-sMyPoint.X + eMyPoint.X) / length), ((-sMyPoint.Y + eMyPoint.Y) / length));
            _Line  line2       = new _Line(new _Point(0, 0), neweMyPoint);

            return(line2);
        }
Ejemplo n.º 8
0
        private void TryToDivideRoomLinesWithL1L2(_Room room, _Line l1, _Line l2)
        {
            bool isComplete = room.CanGetBoundarySorted(); //this might be bad

            if (!isComplete)
            {
                for (int i = 0; i < room.Lines.Count; i++)
                {
                    _Line chosenLine = room.Lines.ElementAt(i);

                    if (chosenLine.Equals(l1) || chosenLine.Equals(l2))
                    {
                        continue;
                    }

                    var connectsPoint = chosenLine.ConnectsPoint(l1);
                    if (connectsPoint != null)
                    {
                        _Point otherPoint = l1.EndPoint.Equals(connectsPoint) ? l1.StartPoint : l1.EndPoint;
                        if (IsOnLine(otherPoint, chosenLine))
                        {
                            if (chosenLine.EndPoint.Equals(connectsPoint))
                            {
                                chosenLine.EndPoint = otherPoint;
                            }
                            else
                            {
                                chosenLine.StartPoint = otherPoint;
                            }
                        }
                    }

                    connectsPoint = chosenLine.ConnectsPoint(l2);
                    if (connectsPoint != null)
                    {
                        _Point otherPoint = l2.EndPoint.Equals(connectsPoint) ? l2.StartPoint : l2.EndPoint;
                        if (IsOnLine(otherPoint, chosenLine))
                        {
                            if (chosenLine.EndPoint.Equals(connectsPoint))
                            {
                                chosenLine.EndPoint = otherPoint;
                            }
                            else
                            {
                                chosenLine.StartPoint = otherPoint;
                            }
                        }
                    }
                }
            }
            isComplete = room.CanGetBoundarySorted(); //we need to check it after the split
        }
Ejemplo n.º 9
0
        /// <summary>
        /// This calls sortlines, too
        /// and overrides Points
        /// </summary>
        public void SortPoints()
        {
            SortLines();
            Points = new List <_Point>();

            for (var index = 0; index < lines.Count; index++)
            {
                _Line  firstMyLine   = lines.ElementAt(index);
                _Line  nextMyLine    = lines.ElementAt((index + 1) % lines.Count);
                _Point commonMyPoint = FindCommonPointOnTwoLines(firstMyLine, nextMyLine);
                Points.Add(commonMyPoint);
            }
        }
Ejemplo n.º 10
0
 public override bool Equals(object obj)
 {
     //Check for null and compare RunSteps-time types.
     if ((obj == null) || !this.GetType().Equals(obj.GetType()))
     {
         return(false);
     }
     else
     {
         _Point p = (_Point)obj;
         return((Math.Abs(X - p.X) < 0.01) && (Math.Abs(Y - p.Y) < 0.01));
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// return normal vector of given line
        /// </summary>
        /// <param name="isNormalized"></param>
        /// <returns></returns>
        public _Point GetNV(bool isNormalized = false)   //if we define dx = x2 - x1 and dy = y2 - y1, then the normals are(-dy, dx) and(dy, -dx).
        {
            double dx = StartPoint.X - EndPoint.X;
            double dy = StartPoint.Y - EndPoint.Y;
            _Point p  = new _Point(-dy, dx);

            if (!isNormalized)
            {
                return(p);
            }
            else
            {
                return(Normalize(p));
            }
        }
Ejemplo n.º 12
0
        public static bool PointOnLine2D(_Point p, _Point a, _Point b, float t = 1E-03f)
        {
            // ensure points are collinear
            var zero = (b.X - a.X) * (p.Y - a.Y) - (p.X - a.X) * (b.Y - a.Y);

            if (zero > t || zero < -t)
            {
                return(false);
            }

            // check if X-coordinates are not equal
            if (a.X - b.X > t || b.X - a.X > t)
            {
                // ensure X is between a.X & b.X (use tolerance)
                return(a.X > b.X
                    ? p.X + t > b.X && p.X - t <a.X
                                                : p.X + t> a.X && p.X - t < b.X);
            }

            // ensure Y is between a.Y & b.Y (use tolerance)
            return(a.Y > b.Y
                ? p.Y + t > b.Y && p.Y - t <a.Y
                                            : p.Y + t> a.Y && p.Y - t < b.Y);
        }
Ejemplo n.º 13
0
 internal _Point Move(_Point moveVector)
 {
     //im not sure that it needs to return a new point
     return(new _Point(X - moveVector.X, Y - moveVector.Y));
 }
Ejemplo n.º 14
0
        /// <summary>
        /// this function tries to sort lines, but it can throw exception when it fails
        /// </summary>
        public void SortLines()
        {
            List <_Line> orderedLines   = new List <_Line>();
            int          actualIndex    = 0;//the basis of sorting is to loop and keep this actualindex
            int          boundCount     = Lines.Count;
            int          nullLinesCount = 0;

            for (int i = 0; i < boundCount; i++)
            {
                _Line loopLine = Lines[actualIndex];
                if (loopLine.StartPoint.Equals(loopLine.EndPoint))
                {
                    nullLinesCount++;
                    continue; //we remove null lines this way
                }

                orderedLines.Add(loopLine);
                actualIndex = 0;
                for (var j = 0; j < boundCount; j++)
                {
                    _Line innerLoopLine = Lines[j];
                    if (orderedLines.Contains(innerLoopLine))
                    {
                        actualIndex++; //so skip this line
                        continue;
                    }
                    //from these next statments, only one can be true
                    if (Equals(innerLoopLine.StartPoint, loopLine.StartPoint) && !Equals(innerLoopLine.EndPoint, loopLine.EndPoint))
                    {
                        break;
                    }

                    if (Equals(innerLoopLine.StartPoint, loopLine.EndPoint) && !Equals(innerLoopLine.EndPoint, loopLine.StartPoint))
                    {
                        break;
                    }

                    if (Equals(innerLoopLine.EndPoint, loopLine.EndPoint) && !Equals(innerLoopLine.StartPoint, loopLine.StartPoint))
                    {
                        break;
                    }

                    if (Equals(innerLoopLine.EndPoint, loopLine.StartPoint) && !Equals(innerLoopLine.StartPoint, loopLine.EndPoint))
                    {
                        break;
                    }

                    actualIndex++;
                }
            }
            _Point p1 = orderedLines.First().ConnectsPoint(orderedLines.Last());                           //this is where the first and last line joins
            _Point p2 = orderedLines.ElementAt(1).ConnectsPoint(orderedLines.ElementAt(0));                //this is where the first and second line joins
            _Point p3 = orderedLines.Last().ConnectsPoint(orderedLines.ElementAt(orderedLines.Count - 2)); //this is where the last and the line before last joins
            //so this is the point where the first and second line connect
            //so p0 is where it all started. the last line should have p0
            _Point p0 = orderedLines.First().StartPoint.Equals(p2)
                ? orderedLines.First().EndPoint
                : orderedLines.First().StartPoint;

            //to get full circle
            _Point p4 = orderedLines.Last().StartPoint.Equals(p3)
                ? orderedLines.Last().EndPoint
                : orderedLines.Last().StartPoint;

            bool isGoodOrdering = p1 != null && p1.Equals(p0) && p1.Equals(p4);

            bool isGoodCount = (orderedLines.Count + nullLinesCount) == Lines.Count;

            if (!isGoodOrdering)
            {
                throw new Exception("first and last line does not connect");
            }

            if (!isGoodCount)
            {
                throw new Exception("not enough lines");
            }

            Lines = orderedLines;
        }
Ejemplo n.º 15
0
 public _Line(_Point startPoint, _Point endPoint)
 {
     StartPoint = startPoint;
     EndPoint   = endPoint;
 }
Ejemplo n.º 16
0
 public static bool IsOnLine(_Point myPoint, _Line myLine)
 {
     return(PointOnLine2D(myPoint, myLine.StartPoint, myLine.EndPoint));
 }
Ejemplo n.º 17
0
 private bool AreNotParralel(_Point loopNormal, _Point moveNormal)
 {
     return(!(Equals(loopNormal, moveNormal) || Equals(loopNormal, moveNormal * -1) || Equals(loopNormal * -1, moveNormal)));
 }
Ejemplo n.º 18
0
 public void Move(_Point moveVector)
 {
     EndPoint   = EndPoint.Move(moveVector);
     StartPoint = StartPoint.Move(moveVector);
 }
Ejemplo n.º 19
0
        private List <_Room> roomsContainingTheLineToMove = new List <_Room>();                      //these rooms might need to change

        public void MoveLine(int distance, _Line lineToMove)
        {
            //if (lineToMove.length < 11) //return;

            roomsContainingTheLineToMove.Clear();
            roomsTouchingStartPoint.Clear();
            roomsTouchingEndPoint.Clear();
            roomsTouchingStartPointOnlyAndHaveNoParallelLines.Clear();
            roomsTouchingEndPointOnlyAndHaveNoParalellLines.Clear();

            _Line movedLine = lineToMove.DeepCopy();                             //first we copy the line we need to move

            movedLine.Name = $"Moved_in_step:{moveStepsCount}";                  //this is for debugging
            _Point moveVector = movedLine.GetNV(true) * distance;                //we scale it up

            movedLine.Move(moveVector);                                          //so we moved the copy (why not the actual?)

            _Line l1 = new _Line(lineToMove.StartPoint, lineToMove.StartPoint.Move(moveVector));
            _Line l2 = new _Line(lineToMove.EndPoint, lineToMove.EndPoint.Move(moveVector));

            l1.Name = $"New_Start_Line_{moveStepsCount}";
            l2.Name = $"New_End_Line_{moveStepsCount}";

            fillRelatedRoomListInfomration(lineToMove, ref l1, ref l2);

            List <_Line> linesToRemove = new List <_Line>();

            //this is the big function
            foreach (_Room room in roomsContainingTheLineToMove)
            {
                bool shouldBeTrue       = room.CanGetBoundarySorted();
                int  linetoMoveOriginal = room.Lines.FindIndex(a => a.Equals(lineToMove));
                if (linetoMoveOriginal == -1)
                {
                    linetoMoveOriginal = 0;
                }

                room.Lines.Remove(lineToMove);
                int linesCount = room.Lines.Count;
                for (var index = 0; index < linesCount; index++)
                {
                    _Line  lineInLoop = room.Lines[index];
                    _Point loopAndToMoveCommonPoint = lineInLoop.ConnectsPoint(lineToMove);
                    if (loopAndToMoveCommonPoint != null && loopAndToMoveCommonPoint.Equals(lineToMove.StartPoint)) //there is common point with startpoint, so this line touched the old startpoint
                    {
                        bool areweMovingOnLoopLine = IsOnLineButNotEndPoint(l1.EndPoint, lineInLoop);
                        //if there is a touching room, we need to keep the point. of course, might not in this room.
                        if (!roomsTouchingStartPoint.Any())
                        {
                            _Point loopNormal = lineInLoop.GetNV(true); //we need to either move it, if it is parallel, or keep it if it is merőleges
                            _Point moveNormal = lineToMove.GetNV(true); //THE LINE SHOULD MOVE - BOTH DIRECTIONS - MOVE P WITH MOVEVECTOR

                            bool NOTparallel = AreNotParralel(loopNormal, moveNormal);

                            if (lineInLoop.StartPoint.Equals(loopAndToMoveCommonPoint) && (NOTparallel))
                            {
                                lineInLoop.StartPoint = lineInLoop.StartPoint.Move(moveVector);
                            }

                            if (lineInLoop.EndPoint.Equals(loopAndToMoveCommonPoint) && (NOTparallel))
                            {
                                lineInLoop.EndPoint = lineInLoop.EndPoint.Move(moveVector);
                            }
                            if (lineInLoop.StartPoint.Equals(lineInLoop.EndPoint))
                            {
                                linesToRemove.Add(lineInLoop);
                            }
                            if ((lineInLoop.StartPoint.Equals(loopAndToMoveCommonPoint) || lineInLoop.EndPoint.Equals(loopAndToMoveCommonPoint)) && !NOTparallel && !l1.StartPoint.Equals(l1.EndPoint))
                            {
                                room.Lines.Add(l1);
                            }
                        }
                        //TODO:if there is room, we may move it, if there is no perpendicular line in that room -- but be careful of that other room
                        if (roomsTouchingStartPoint.Any() && !l1.StartPoint.Equals(l1.EndPoint))
                        {
                            if (!areweMovingOnLoopLine && !room.Lines.Contains(l1))
                            {
                                room.Lines.Add(l1);  //this makes it easier to remove the small lines later
                            }
                            foreach (_Room room1 in roomsTouchingStartPoint)
                            {
                                if (!room1.Lines.Contains(l1))
                                {
                                    room1.Lines.Add(l1);
                                }
                            }
                        }
                    }

                    if (loopAndToMoveCommonPoint != null && loopAndToMoveCommonPoint.Equals(lineToMove.EndPoint))
                    {
                        bool areweMovingOnLoopLine = _Model.IsOnLineButNotEndPoint(l2.EndPoint, lineInLoop);
                        if (!roomsTouchingEndPoint.Any())
                        {
                            _Point objA        = lineInLoop.GetNV(true);
                            _Point objB        = lineToMove.GetNV(true);
                            bool   NOTparallel = AreNotParralel(objA, objB);
                            if (lineInLoop.StartPoint.Equals(loopAndToMoveCommonPoint) && (NOTparallel))
                            {
                                lineInLoop.StartPoint = lineInLoop.StartPoint.Move(moveVector);
                            }

                            if (lineInLoop.EndPoint.Equals(loopAndToMoveCommonPoint) && (NOTparallel))
                            {
                                lineInLoop.EndPoint = lineInLoop.EndPoint.Move(moveVector);
                            }
                            if (lineInLoop.StartPoint.Equals(lineInLoop.EndPoint))
                            {
                                linesToRemove.Add(lineInLoop);
                            }
                            if ((lineInLoop.EndPoint.Equals(loopAndToMoveCommonPoint) || lineInLoop.StartPoint.Equals(loopAndToMoveCommonPoint)) && !NOTparallel && !l2.StartPoint.Equals(l2.EndPoint))
                            {
                                room.Lines.Add(l2);
                            }
                        }
                        if (roomsTouchingEndPoint.Any() && !l2.StartPoint.Equals(l2.EndPoint))
                        {
                            if (!areweMovingOnLoopLine && !room.Lines.Contains(l2))
                            {
                                room.Lines.Add(l2);
                            }
                            foreach (_Room room2 in roomsTouchingEndPoint)
                            {
                                if (!room2.Lines.Contains(l2))
                                {
                                    room2.Lines.Add(l2);
                                }
                            }
                        }
                    }
                }

                room.Lines.Insert(linetoMoveOriginal, movedLine);
                foreach (var line in linesToRemove)
                {
                    room.Lines.Remove(line);
                }
                //shouldBeTrue = room.CanGetBoundarySorted();
            }


            foreach (_Room room in roomsContainingTheLineToMove)
            {
                bool shouldBeTrue = room.CanGetBoundarySorted();
                TryToDivideRoomLinesWithL1L2(room, l1, l2);
                TryToCatchIfGlobalCase(room, movedLine);
                shouldBeTrue = room.CanGetBoundarySorted();
                if (!shouldBeTrue)
                {
                    //throw new Exception("should be sortable, if i cant figure it out, mark as invalid state");
                    IsInInvalidState = true;
                }
            }

            foreach (_Room room in roomsTouchingEndPoint)
            {
                TryToDivideRoomLinesWithL1L2(room, l1, l2);
                TryToRemoveRemainderLines(room, l1, l2);
                bool shouldBeTrue = room.CanGetBoundarySorted();
                if (!shouldBeTrue)
                {
                    //throw new Exception("should be sortable, if i cant figure it out, mark as invalid state
                    IsInInvalidState = true;
                }
            }

            foreach (_Room room in roomsTouchingStartPoint)
            {
                TryToDivideRoomLinesWithL1L2(room, l1, l2);
                TryToRemoveRemainderLines(room, l1, l2);
                bool shouldBeTrue = room.CanGetBoundarySorted();
                if (!shouldBeTrue)
                {
                    //throw new Exception("should be sortable, if i cant figure it out, mark as invalid state");
                    IsInInvalidState = true;
                }
            }


            foreach (_Room room in roomsTouchingStartPointOnlyAndHaveNoParallelLines)
            {
                TryToRemoveRemainderLines(room, l1, l2);
                bool shouldBeTrue = room.CanGetBoundarySorted();
                if (!shouldBeTrue)
                {
                    IsInInvalidState = true;
                }
            }
            foreach (_Room room in roomsTouchingEndPointOnlyAndHaveNoParalellLines)
            {
                TryToRemoveRemainderLines(room, l1, l2);
                bool shouldBeTrue = room.CanGetBoundarySorted();
                if (!shouldBeTrue)
                {
                    IsInInvalidState = true;
                }
            }

            List <_Room> sumrooms = new List <_Room>();

            sumrooms.AddRange(roomsContainingTheLineToMove);
            sumrooms.AddRange(roomsTouchingStartPoint);
            sumrooms.AddRange(roomsTouchingEndPoint);
            //this handles null lines, can be removed at any time, probably should do it before sorting
            foreach (_Room room in sumrooms)
            {
                for (var index = 0; index < room.Lines.Count; index++)
                {
                    _Line roomLine = room.Lines[index];
                    if (roomLine.StartPoint.Equals(roomLine.EndPoint))
                    {
                        room.Lines.Remove(roomLine);
                    }
                }
            }
            //GC.Collect();
            //only need to join the same line
            //but why after? we could handle upon creation

            moveStepsCount++;
        }