//-------------------------------------------------------------------------------- /// <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(); }
/// <summary> /// Get the sign of the point in relation to the plane /// </summary> /// <param name="pt"></param> /// <returns></returns> public Sign GetSign(Vector3D pt) { return(Math3D.GetSign(GetDistanceToPlane(pt), Math3D.EpsilonF)); }