示例#1
0
        /// <summary>
        /// Create a polygon coplanar with the current plane that extends to the given extent
        /// </summary>
        /// <param name="fExtent"></param>
        /// <returns></returns>
        public Polygon3D        CreatePolygon(float fExtent)
        {
            int a, b, c;

            float[] n = new float[3] {
                Math.Abs(Normal.X),
                Math.Abs(Normal.Y),
                Math.Abs(Normal.Z)
            };

            if (n[0] >= n[1] && n[0] >= n[2])
            {
                a = 1;  b = 2;  c = 0;
            }
            else if (n[1] >= n[0] && n[1] >= n[2])
            {
                a = 0;  b = 2;  c = 1;
            }
            else if (n[2] >= n[0] && n[2] >= n[1])
            {
                a = 0;  b = 1;  c = 2;
            }
            else
            {
                a = b = c = -1;
                Debug.Assert(false);
            }

            //Debug.WriteLine( " normal[" + Normal.ToString() + " a[" + a + "] b[" + b + "] c[" + c + "]" );

            int[] aSigns = new int[4] {
                1, 1, -1, -1
            };
            int[] bSigns = new int[4] {
                1, -1, -1, 1
            };

            Polygon3D poly = new Polygon3D();

            for (int i = 0; i < 4; i++)
            {
                Vector3D pt = new Vector3D();

                pt[a] = fExtent * aSigns[i];
                pt[b] = fExtent * bSigns[i];
                pt[c] = (Constant - Normal[a] * pt[a] - Normal[b] * pt[b]) / Normal[c];

                //Debug.WriteLine( " pt[" + i + "] = " + pt.ToString() + " dTp = " + DistanceToPlane( pt ) );

                poly.Points.Add(pt);
            }

            //Debug.WriteLine( " plane.Normal = " + Normal );
            //Debug.WriteLine( " plane.Constant = " + Constant );

            if (Vector3D.Dot(Normal, poly.GetNormal()) < 0)
            {
                poly.Flip();
            }

            //Debug.WriteLine( " polygon.Normal = " + poly.Normal().ToString() );

            return(poly);
        }
示例#2
0
        //--------------------------------------------------------------------------------

        /// <summary>
        /// Clip the given polygon by the plane so that only the regions
        /// located positive of the plane remain.
        /// </summary>
        /// <param name="polygon"></param>
        public void    ClipPolygon(Polygon3D polygon)
        {
            Vector3D ptNormal = polygon.GetNormal();

            Vector3DCollection vNewPoints = new Vector3DCollection();
            int iPoints = polygon.Points.Count;

            for (int i = 0; i < iPoints; i++)
            {
                Vector3D pt0 = polygon.Points[(i + iPoints - 1) % iPoints];
                Vector3D pt1 = polygon.Points[i];

                int sign0 = (int)Math3D.GetSign(this.GetDistanceToPlane(pt0), Math3D.EpsilonF);
                int sign1 = (int)Math3D.GetSign(this.GetDistanceToPlane(pt1), Math3D.EpsilonF);

                if (sign0 > 0)
                {
                    // line is infront
                    if (sign1 >= 0)
                    {
                        vNewPoints.Add(pt1);
                    }
                    // line is entering plane
                    else if (sign1 < 0)
                    {
                        Debug.Assert(sign0 > 0 && sign1 < 0);
                        vNewPoints.Add(this.GetIntersection(pt0, pt1));
                    }
                }
                else if (sign0 == 0)
                {
                    // line is infront
                    if (sign1 > 0)
                    {
                        vNewPoints.Add(pt1);
                    }
                    // line is coplanar
                    else if (sign1 == 0)
                    {
                        vNewPoints.Add(pt1);
                    }
                    // line is behind
                    else if (sign1 < 0)
                    {
                    }
                }
                else if (sign0 < 0)
                {
                    // line is leaving plane
                    if (sign1 > 0)
                    {
                        Debug.Assert(sign0 < 0 && sign1 > 0);
                        vNewPoints.Add(this.GetIntersection(pt0, pt1));
                        vNewPoints.Add(pt1);
                    }
                    // line is leaving plane
                    else if (sign1 == 0)
                    {
                        vNewPoints.Add(pt1);
                    }
                    // line is behind
                    else if (sign1 < 0)
                    {
                    }
                }
            }

            // set new points
            polygon.Points.Clear();
            polygon.Points.AddRange(vNewPoints);

            /*if( this.Points.Count >= 3 ) {
             *      if( Vector3D.Dot( this.GetNormal(), ptNormal ) < 0 ) {
             *              this.Flip();
             *      }
             * }  */

            polygon.Optimize();
        }