예제 #1
0
        private void Contract(int facesCount)
        {
            SelectValidPairs();

            int afterContract = m_Faces.Count - facesCount;

            while (m_Faces.Count > afterContract)
            {
                double minError = (double)int.MaxValue;

                //KeyValuePair<VertexPair, double> min;
                VertexPair minPair = new VertexPair()
                {
                    First = 0, Second = 0
                };

                foreach (KeyValuePair <VertexPair, double> e in m_Errors)
                {
                    if (e.Value < minError)
                    {
                        minError = e.Value;
                        minPair  = e.Key;
                    }
                }

                Vector3 error;
                ComputeError(minPair.First, minPair.Second, out error);

                Split split = new Split();
                split.V1.Position = m_Vertices[minPair.First].Position;
                split.V2.Position = m_Vertices[minPair.Second].Position;
                split.VF.Position = error;
                m_Splits.PushBack(split);

                var vertex = m_Vertices[minPair.First];
                vertex.Position = error;

                m_Quadrics[minPair.First] = m_Quadrics[minPair.First] + m_Quadrics[minPair.Second];

                for (int i = m_Faces.Count; i != 0;)
                {
                    var face = m_Faces[i];

                    for (int j = 0; j < 3; ++j)
                    {
                        if (face[j] == minPair.Second)
                        {
                            if (face[0] == minPair.First || face[1] == minPair.First || face[2] == minPair.First)
                            {
                                m_Faces.Remove(face);
                            }
                            else
                            {
                                face[j] = minPair.First;
                            }
                            --i;
                            break;
                        }
                        else if (j == 2)
                        {
                            --i;
                        }
                    }
                }

                m_Vertices.Remove(minPair.Second);

                KeyValuePair <VertexPair, double> pair;

                for (int iter = m_Errors.Count; iter != 0;)
                {
                    pair = m_Errors.ElementAt(iter);

                    if (pair.Key.First == minPair.Second && pair.Key.Second != minPair.First)
                    {
                        m_Errors.Remove(m_Errors.ElementAt(iter).Key);

                        m_Errors.Add(
                            new VertexPair()
                        {
                            First = Math.Min(minPair.First, pair.Key.Second), Second = Math.Max(minPair.First, pair.Key.Second)
                        },
                            0.0);
                        --iter;
                    }
                    else if (pair.Key.Second == minPair.Second && pair.Key.First != minPair.First)
                    {
                        m_Errors.Remove(m_Errors.ElementAt(iter).Key);

                        m_Errors.Add(
                            new VertexPair()
                        {
                            First = Math.Min(minPair.First, pair.Key.First), Second = Math.Max(minPair.First, pair.Key.First)
                        },
                            0.0);
                        --iter;
                    }
                    else
                    {
                        --iter;
                    }
                }

                m_Errors.Remove(minPair);

                foreach (var e in m_Errors)
                {
                    var p = e.Key;
                    if (p.First == minPair.First)
                    {
                        m_Errors[p] = ComputeError(minPair.First, p.Second);
                    }

                    if (p.Second == minPair.First)
                    {
                        m_Errors[p] = ComputeError(minPair.First, p.First);
                    }
                }
            }
        }
예제 #2
0
 public bool Equals(VertexPair pair)
 {
     return(this.First == pair.First && this.Second == pair.Second);
 }