/// <summary>
 /// Calculates line-plane intersection point
 /// </summary>
 /// <param name="line">Intersecting line</param>
 /// <param name="planeNormal">intersected plane</param>
 /// <returns>A point of intersection</returns>
 /// <remarks>Please note that if line direction is perpendicular to planeNormal, the function will throw zero division exception.</remarks>
 internal static Vector3d CalculateLinePlaneIntersectionPoint(Line3D line, Vector3d planeNormal)
 {
     return(line.Position + line.Direction * -(planeNormal * line.Position) / (line.Direction * planeNormal));
 }
        /// <summary>
        /// Calculates an intersection line between planes
        /// </summary>
        /// <param name="plane1">First plane</param>
        /// <param name="plane2">Second plane</param>
        /// <param name="line">Resulting intersection</param>
        /// <returns>true if planes are not parallel</returns>
        internal static bool CalculateIntersection(PlaneCluster plane1, PlaneCluster plane2, out Line3D line)
        {
            Vector3d normal1 = plane1.AverageNormal;
            Vector3d normal2 = plane2.AverageNormal;
            Vector3d point1  = plane1.AveragePoint;
            Vector3d point2  = plane2.AveragePoint;

            double d1 = normal1 * point1;
            double d2 = normal2 * point2;

            if (Math.Abs(normal1 * normal2 - 1) < double.Epsilon)
            {
                line = new Line3D();
                return(false);
            }

            Vector3d lineNormal = Vector3d.Cross(normal1, normal2);

            Vector3d linePointAsVector = -Vector3d.Cross(normal1 * d2 - normal2 * d1, lineNormal) / lineNormal.LengthSquared;

            line = new Line3D {
                Position = linePointAsVector, Direction = lineNormal
            };

            return(true);
        }