Ejemplo n.º 1
0
            /// <summary>
            /// Combine one line loop with another
            /// </summary>
            /// <param name="other"></param>
            /// <returns>Array of LineLoops which don't intersect (will be one or two loops)</returns>
            public static LineLoop Intersect(LineLoop one, LineLoop another, Vector3 normal)
            {
                LineStrip one2 = new LineStrip();
                List<Vector3> vv = new List<Vector3>(one.Vertices);
                for (int i = 0; i < vv.Count; i++)
                {
                    int j = (i + 2) % vv.Count;
                    one2.Append(vv[j]);
                }
                one2.Append(one2.GetVertex(0));
                one = new LineLoop(one2);

                GL.PointSize(19);
                GL.Color3(Color.GreenYellow);
                GL.Begin(BeginMode.Points);
                GL.Vertex3(one.GetVertex(0));
                GL.End();
                GL.PointSize(1);

                LineLoop following = one;
                LineLoop searching = another;

                Vector3 startPoint = following.GetVertex(0);
                LineStrip ls = new LineStrip();
                List<LineLoop> loops = new List<LineLoop>();
                for (int i = 0; i < following.indices.Count(); i++)
                {
                    Vector3 v1 = following.GetVertex(i);
                    Vector3 v2 = following.GetVertex((i + 1) % following.indices.Count());

                    //if (ls.ContainsVertex(v1))
                   // {
                        bool matched = false;
                        LineStrip ls2 = new LineStrip();
                        foreach (Vector3 v in ls.Vertices)
                        {
                            if (!matched && (v - v1).Length < 0.001)
                            {
                                matched = true;
                            }
                            if (matched)
                            {
                                ls2.Append(v);
                            }
                        }
                        if (matched)
                        {

                            ls2.Append(v1);
                            GL.PushMatrix();
                            GL.Translate(0, 0, 50);
                            ls2.Draw();
                            GL.PopMatrix();
                            // Done!  Got a loop
                            LineLoop l = new LineLoop(ls2);
                            return l;
                        }
                    //}
                    ls.Append(v1);

                    int searchingCount = searching.indices.Count();
                    for (int j = 0; j < searchingCount; j++)
                    {
                        Vector3 v3 = searching.GetVertex(j);
                        Vector3 v4 = searching.GetVertex((j + 1) % searchingCount);

                        Vector3 n1 = (v2 - v1);
                        Vector3 n2 = (v4 - v3);

                        n1.Normalize();
                        n2.Normalize();

                        float d1 = DistanceToCylinder(v1, v2, v3);
                        float d2 = DistanceToCylinder(v3, v4, v1);

                        if (d2 < 10)
                        {
                        }

                        // Point v3 is on Line v1-v2:
                        if (DistanceToCylinder(v1, v2, v3) < 1)
                        {
                            // Follow the line most to the outside
                            float angle = Angle(n1, n2, normal);
                            if (angle < OpenTK.MathHelper.Pi)
                            {
                                GL.PointSize(10);
                                GL.Color3(Color.Orange);
                                GL.Begin(BeginMode.Points);
                                GL.Vertex3(v3);
                                GL.End();
                                GL.PointSize(1);
                                ls.Append(v3);
                                // Follow the line v3-v4
                                LineLoop temp = searching;
                                searching = following;
                                following = temp;
                                i = j;
                                break;
                            }
                            else
                            {
                                // Continue following the line v1-v2
                            }
                        }
                        // Point v2 is on the line v3-v4
                        else if (DistanceToCylinder(v3, v4, v1) < 1)
                        {
                            // Follow the line most to the outside
                            float angle = Angle(n1, n2, normal);
                            if (angle < OpenTK.MathHelper.Pi)
                            {
                                GL.PointSize(10);
                                GL.Color3(Color.Orange);
                                GL.Begin(BeginMode.Points);
                                GL.Vertex3(v1);
                                GL.End();
                                GL.PointSize(1);
                                //ls.Append(v1);
                                // Follow the line v3-v4
                                LineLoop temp = searching;
                                searching = following;
                                following = temp;
                                i = j;
                                break;
                            }
                            else
                            {
                                // Continue following the line v1-v2
                            }
                        }
                        // TODO: Add case for intersecting lines!
                        //else if (0)
                        //{
                        //}
                    }

                }
                //return loops.ToArray();

                ls.Append(following.GetVertex(following.indices[0]));
                //if (ls.ContainsVertex(v1))
                //{
                    //ls.Append(v1);
                    GL.PushMatrix();
                    GL.Translate(0, 0, 50);
                    ls.Draw();
                    GL.PopMatrix();
                    // Done!  Got a loop
                    LineLoop l2 = new LineLoop(ls);
                    return l2;
                //}
            }