private bool RangeIsInWeld(Weld weld, IntVector2 range)
        {
            var oldRange = weld.GlobalSizeRange;

            if (oldRange.Equals(range))
            {
                return(true);
            }
            else if (oldRange.X >= range.Y || oldRange.Y <= range.X) // not touching
            {
                return(false);
            }
            else if (oldRange.X <= range.X && oldRange.Y >= range.Y) // new is inside old
            {
                return(true);
            }
            else if (oldRange.X >= range.X && oldRange.Y <= range.Y) // old is inside new
            {
                return(true);
            }
            else if (oldRange.X <= WeldLineConstants.MIN_RANGE || oldRange.Y >= WeldLineConstants.MAX_RANGE)
            {
                return(true);
            }
            else
            {
                Preconditions.Fail($"E43 misaligned ranges: old: {oldRange} new {range}");
                return(false);
            }
        }
        public void RemoveTerrain(int weldingTerrainId)
        {
            foreach (var weld in _welds)
            {
                if (weld.First != null && weld.First.Terrain.WeldingInputTerrainId == weldingTerrainId)
                {
                    weld.First = null;
                }
                if (weld.Second != null && weld.Second.Terrain.WeldingInputTerrainId == weldingTerrainId)
                {
                    weld.Second = null;
                }
            }

            var newList    = new List <Weld>();
            var activeWeld = _welds[0];

            for (int i = 0; i < _welds.Count; i++)
            {
                if (i == _welds.Count - 1)
                {
                    newList.Add(activeWeld);
                }
                else
                {
                    var next = _welds[i + 1];
                    if (CanBeMerged(activeWeld, next))
                    {
                        var newWeld = new Weld()
                        {
                            First           = activeWeld.First,
                            Second          = activeWeld.Second,
                            GlobalSizeRange = new IntVector2(activeWeld.GlobalSizeRange.X, next.GlobalSizeRange.Y)
                        };
                        activeWeld = newWeld;
                    }
                    else
                    {
                        newList.Add(activeWeld);
                        activeWeld = next;
                    }
                }
            }
            _welds = newList;
        }
 private bool CanBeMerged(Weld w1, Weld w2)
 {
     return(((w1.First == null && w2.First == null) || (w1?.First?.Terrain.WeldingInputTerrainId == w2?.First?.Terrain.WeldingInputTerrainId)) &&
            ((w1.Second == null && w2.Second == null) || (w1?.Second?.Terrain.WeldingInputTerrainId == w2?.Second?.Terrain.WeldingInputTerrainId)));
 }
        private WeldSplicingResult SpliceWeld(Weld weld, IntVector2 range)
        {
            var oldRange = weld.GlobalSizeRange;

            if (oldRange.Equals(range))
            {
                return(new WeldSplicingResult()
                {
                    NewWeld = weld.Clone(),
                    RestOfWelds = new List <Weld>()
                });
            }
            else if (oldRange.X >= range.Y || oldRange.Y <= range.X) // not touching
            {
                Preconditions.Fail("Welds are not touching!");
                return(null);
            }
            else if (oldRange.X <= range.X && oldRange.Y >= range.Y) // new is inside old
            {
                if (oldRange.X == range.X)
                {
                    return(new WeldSplicingResult()
                    {
                        NewWeld = weld.Subelement(range.X, range.Y),
                        RestOfWelds = new List <Weld>()
                        {
                            weld.Subelement(range.Y, oldRange.Y)
                        }
                    });
                }
                else if (oldRange.Y == range.Y)
                {
                    return(new WeldSplicingResult()
                    {
                        NewWeld = weld.Subelement(range.X, range.Y),
                        RestOfWelds = new List <Weld>()
                        {
                            weld.Subelement(oldRange.X, range.X)
                        }
                    });
                }
                else
                {
                    return(new WeldSplicingResult()
                    {
                        NewWeld = weld.Subelement(range.X, range.Y),
                        RestOfWelds = new List <Weld>()
                        {
                            weld.Subelement(oldRange.X, range.X),
                            weld.Subelement(range.Y, oldRange.Y)
                        }
                    });
                }
            }
            else if (oldRange.X >= range.X && oldRange.Y <= range.Y) // old is inside new
            {
                return(new WeldSplicingResult()
                {
                    NewWeld = weld.Clone(),
                    RestOfWelds = new List <Weld>()
                });
            }
            if (oldRange.X < range.X)
            {
                return(new WeldSplicingResult()
                {
                    NewWeld = weld.Subelement(range.X, oldRange.Y),
                    RestOfWelds = new List <Weld>()
                    {
                        weld.Subelement(oldRange.X, range.X)
                    }
                });
            }
            else if (oldRange.Y > range.Y)
            {
                return(new WeldSplicingResult()
                {
                    NewWeld = weld.Subelement(oldRange.X, range.Y),
                    RestOfWelds = new List <Weld>()
                    {
                        weld.Subelement(range.Y, oldRange.Y)
                    }
                });
            }
            else
            {
                // slight misalighment

                Preconditions.Fail($"E438 misaligned ranges: old: {oldRange} new {range}");
                return(null);
            }
        }