public LineSegment(Line source, Line itx1, Line itx2, int idOnSource, SortedIntersectionData ds)
 {
     Source                  = source;
     FirstIntersection       = itx1;
     SecondIntersection      = itx2;
     IdOnSource              = idOnSource;
     FirstIntersectionCoord  = ds[source.Id].LineToCoordinate.GetOrAdd(itx1, source.Intersection(itx1));
     SecondIntersectionCoord = ds[source.Id].LineToCoordinate.GetOrAdd(itx2, source.Intersection(itx2));
 }
        public void PopulateSegmentData(Line myLine, SortedIntersectionData ds, bool reset = true)
        {
            Segments     = new LineSegment[count - 1];
            _segmentsInv = new LineSegment[count - 1];
            var i = 0;

            foreach (var cellWall in LineList.Zip(LineList.Skip(1), (a, b) => new { First = a, Second = b }))
            {
                Segments[i]     = new LineSegment(myLine, cellWall.First, cellWall.Second, i, ds);
                _segmentsInv[i] = new LineSegment(myLine, cellWall.Second, cellWall.First, i, ds);
                i++;
            }
            if (reset)
            {
                ResetVisitationData();
            }
        }
        public void AddLineToExistingData(Line cellline, SortedIntersectionData ds)
        {
            var icoord = cellline.Intersection(this.MyLine);

            lock (locker)
            {
                if (!CoordinateToLine.ContainsKey(icoord))
                {
                    CoordinateToLine.Add(icoord, new HashSet <Line>());
                }
                CoordinateToLine[icoord].Add(cellline);
                CoordinateToLine[icoord].Add(this.MyLine);
                LineToCoordinate.GetOrAdd(cellline, icoord);
                var lineLst = LineList.ToList();
                var loc     = lineLst.BinarySearch(cellline, new LineIntersectionComparer(LineToCoordinate));
                if (loc < 0)
                {
                    lineLst.Insert(~loc, cellline);
                }
                else
                {
                    throw new Exception("something is bad.");
                }

                var intersectionlst =
                    lineLst.Select(l => new { line = l, intersection = LineToCoordinate[l] })
                    .GroupBy(l => l.intersection);

                count = 0;
                var tmpLinelist = new List <Line>();
                foreach (var g in intersectionlst)
                {
                    var tcount = 0;
                    foreach (var line in g.Select(l => l.line))
                    {
                        ranks[line.Id] = count;
                        if (tcount == 0)
                        {
                            tmpLinelist.Add(line);
                        }
                        tcount++;
                    }
                    if (StaticConfigParams.ComputeSanityChecks && tcount > 1)
                    {
                        throw new ArgumentException("We found more than two lines intersecting at the same point, this is not supported currently.");
                    }
                    count++;
                }
                LineList = tmpLinelist.ToArray();

                if (Segments.Length != (count - 1))
                {
                    var seglst    = Segments.ToList();
                    var invseglst = _segmentsInv.ToList();
                    LeftCellsVisited = new bool[count];

                    var _leftCellsVisited  = LeftCellsVisited.ToList();
                    var _rightCellsVisited = RightCellsVisited.ToList();
                    var _segmentToCellMap  = SegmentToCellMap.ToList();

                    var idx = tmpLinelist.IndexOf(cellline);
                    if (idx == 0)
                    {
                        seglst.Insert(0, new LineSegment(MyLine, cellline, tmpLinelist[1], 0, ds));
                        invseglst.Insert(0, new LineSegment(MyLine, tmpLinelist[1], cellline, 0, ds));
                        _leftCellsVisited.Insert(0, false);
                        _rightCellsVisited.Insert(0, false);
                        _segmentToCellMap.Insert(0, null);
                    }
                    else if (idx == tmpLinelist.Count - 1)
                    {
                        seglst.Insert(idx - 1, new LineSegment(MyLine, tmpLinelist[idx - 1], cellline, idx, ds));
                        invseglst.Insert(idx - 1, new LineSegment(MyLine, cellline, tmpLinelist[idx - 1], idx, ds));
                        _leftCellsVisited.Insert(idx - 1, false);
                        _rightCellsVisited.Insert(idx - 1, false);
                        _segmentToCellMap.Insert(idx - 1, null);
                    }
                    else
                    {
                        //insert
                        seglst.RemoveAt(idx - 1);
                        invseglst.RemoveAt(idx - 1);
                        _leftCellsVisited.RemoveAt(idx - 1);
                        _rightCellsVisited.RemoveAt(idx - 1);
                        _segmentToCellMap.RemoveAt(idx - 1);
                        seglst.Insert(idx - 1, new LineSegment(MyLine, tmpLinelist[idx - 1], cellline, idx, ds));
                        invseglst.Insert(idx - 1, new LineSegment(MyLine, cellline, tmpLinelist[idx - 1], idx, ds));
                        _leftCellsVisited.Insert(idx - 1, false);
                        _rightCellsVisited.Insert(idx - 1, false);
                        _segmentToCellMap.Insert(idx - 1, null);
                        seglst.Insert(idx, new LineSegment(MyLine, cellline, tmpLinelist[idx + 1], idx + 1, ds));
                        invseglst.Insert(idx, new LineSegment(MyLine, tmpLinelist[idx + 1], cellline, idx + 1, ds));
                        _leftCellsVisited.Insert(idx, false);
                        _rightCellsVisited.Insert(idx, false);
                        _segmentToCellMap.Insert(idx, null);
                    }
                    for (var i = idx; i < seglst.Count; i++)
                    {
                        seglst[i].IdOnSource    = i;
                        invseglst[i].IdOnSource = i;
                    }

                    /*
                     * foreach (var cellWall in LineList.Zip(LineList.Skip(1), (a, b) => new { First = a, Second = b }))
                     * {
                     *  if (cellWall.First == cellline || cellWall.Second == cellline)
                     *  {
                     *      if (i > 0 && i < Segments.Length)
                     *      {
                     *          seglst[i] = new LineSegment(MyLine, cellWall.First, cellWall.Second, i, ds);
                     *          invseglst[i] = new LineSegment(MyLine, cellWall.Second, cellWall.First, i, ds);
                     *      }
                     *      else
                     *      {
                     *          seglst.Insert(i, new LineSegment(MyLine, cellWall.First, cellWall.Second, i, ds));
                     *          invseglst.Insert(i, new LineSegment(MyLine, cellWall.Second, cellWall.First, i, ds));
                     *      }
                     *  }
                     *  else
                     *  {
                     *      seglst[i].IdOnSource = i;
                     *      invseglst[i].IdOnSource = i;
                     *  }
                     *  i++;
                     * }
                     */
                    Segments          = seglst.ToArray();
                    _segmentsInv      = invseglst.ToArray();
                    LeftCellsVisited  = _leftCellsVisited.ToArray();
                    RightCellsVisited = _rightCellsVisited.ToArray();
                    SegmentToCellMap  = _segmentToCellMap.ToArray();
                }
            }
        }