예제 #1
0
        public FltDomain Abs()
        {
            if (IsEmpty())
            {
                return(m_Empty);
            }

            if (m_Interval.Min >= 0)
            {
                return(this);
            }

            if (m_Interval.Max <= 0)
            {
                return(Negate());
            }

            FltDomain lower = -Intersect(double.MinValue, 0);
            FltDomain upper = Intersect(0, double.MaxValue);

            return(lower.Union(upper));
        }
예제 #2
0
        public FltDomain Union(FltDomain domain)
        {
            if (ReferenceEquals(domain, this))
            {
                return(this);
            }

            if (domain.IsEmpty())
            {
                return(this);
            }

            if (IsEmpty())
            {
                return(domain);
            }

            if (domain.IsSimple())
            {
                return(Union(domain.m_Interval));
            }

            if (IsSimple())
            {
                if (m_Interval.Contains(domain.m_Interval))
                {
                    return(this);
                }

                return(domain.Union(m_Interval));
            }
            else
            {
                FltDomain result = new FltDomain();
                result.m_Interval     = m_Interval.Union(domain.m_Interval);
                result.m_IntervalList = new List <FltInterval>(m_IntervalList.Count + domain.m_IntervalList.Count);

                // 1 : completely before
                if (domain.m_Interval.Max < m_Interval.Min)
                {
                    result.m_IntervalList.AddRange(domain.m_IntervalList);
                    result.m_IntervalList.AddRange(m_IntervalList);
                }

                // 6 : completely after
                else if (domain.m_Interval.Min > m_Interval.Max)
                {
                    result.m_IntervalList.AddRange(m_IntervalList);
                    result.m_IntervalList.AddRange(domain.m_IntervalList);
                }
                else
                {
                    int  idx    = 0;
                    int  domIdx = 0;
                    bool equal  = true;

                    while (idx < m_IntervalList.Count ||
                           domIdx < domain.m_IntervalList.Count)
                    {
                        // 6 :
                        if (idx == m_IntervalList.Count)
                        {
                            equal = false;

                            for ( ; domIdx < domain.m_IntervalList.Count; ++domIdx)
                            {
                                result.m_IntervalList.Add(domain.m_IntervalList[domIdx]);
                            }
                        }

                        // 1 : remaining intervals after
                        else if (domIdx == domain.m_IntervalList.Count)
                        {
                            for ( ; idx < m_IntervalList.Count; ++idx)
                            {
                                result.m_IntervalList.Add(m_IntervalList[idx]);
                            }
                        }
                        else
                        {
                            FltInterval intv    = m_IntervalList[idx];
                            FltInterval domIntv = domain.m_IntervalList[domIdx];

                            // 1 : completely before
                            if (domIntv.Max < intv.Min)
                            {
                                equal = false;

                                result.m_IntervalList.Add(domIntv);
                                ++domIdx;
                            }
                            // 6 : completely after
                            else if (domIntv.Min > intv.Max)
                            {
                                result.m_IntervalList.Add(intv);
                                ++idx;
                            }
                            // 3
                            else if (domIntv.Contains(intv))
                            {
                                equal = false;

                                ++idx;
                            }
                            // 3
                            else if (intv.Contains(domIntv))
                            {
                                ++domIdx;
                            }
                            // 2, 5
                            else
                            {
                                equal = false;

                                FltInterval intvTmp = intv.Union(domIntv);

                                ++idx;

                                // contains...
                                for ( ; idx < m_IntervalList.Count &&
                                      intvTmp.Contains(m_IntervalList[idx]);
                                      ++idx)
                                {
                                }
                                ;

                                // intersect...
                                while (idx < m_IntervalList.Count &&
                                       m_IntervalList[idx].IntersectsWith(intvTmp))
                                {
                                    intvTmp = intvTmp.Union(m_IntervalList[idx]);
                                    ++idx;
                                }

                                result.m_IntervalList.Add(intvTmp);

                                ++domIdx;
                            }
                        }
                    }

                    if (equal)
                    {
                        return(this);
                    }


                    result.CheckSimple();
                }

                return(result);
            }
        }