/// <summary> /// Returns the intersection point of the three planes: `b`, `c`, /// and this plane. If no intersection is found, `null` is returned. /// </summary> /// <param name="b">One of the three planes to use in the calculation.</param> /// <param name="c">One of the three planes to use in the calculation.</param> /// <returns>The intersection, or `null` if none is found.</returns> public Vector3d?Intersect3(Planed b, Planed c) { double denom = _normal.Cross(b._normal).Dot(c._normal); if (Mathd.IsZeroApprox(denom)) { return(null); } Vector3d result = b._normal.Cross(c._normal) * D + c._normal.Cross(_normal) * b.D + _normal.Cross(b._normal) * c.D; return(result / denom); }
/// <summary> /// Returns the intersection point of a ray consisting of the /// position `from` and the direction normal `dir` with this plane. /// If no intersection is found, `null` is returned. /// </summary> /// <param name="from">The start of the ray.</param> /// <param name="dir">The direction of the ray, normalized.</param> /// <returns>The intersection, or `null` if none is found.</returns> public Vector3d?IntersectRay(Vector3d from, Vector3d dir) { double den = _normal.Dot(dir); if (Mathd.IsZeroApprox(den)) { return(null); } double dist = (_normal.Dot(from) - D) / den; // This is a ray, before the emitting pos (from) does not exist if (dist > Mathd.Epsilon) { return(null); } return(from + dir * -dist); }
/// <summary> /// Returns the intersection point of a line segment from /// position `begin` to position `end` with this plane. /// If no intersection is found, `null` is returned. /// </summary> /// <param name="begin">The start of the line segment.</param> /// <param name="end">The end of the line segment.</param> /// <returns>The intersection, or `null` if none is found.</returns> public Vector3d?IntersectSegment(Vector3d begin, Vector3d end) { Vector3d segment = begin - end; double den = _normal.Dot(segment); if (Mathd.IsZeroApprox(den)) { return(null); } double dist = (_normal.Dot(begin) - D) / den; // Only allow dist to be in the range of 0 to 1, with tolerance. if (dist < -Mathd.Epsilon || dist > 1.0f + Mathd.Epsilon) { return(null); } return(begin + segment * -dist); }