private static Point3D?CastRay_LinesCircles(IEnumerable <RayHitTestParameters> lines, IEnumerable <CircleDefinition> circles, Point3D testPoint) { Point3D?retVal = null; double distance = double.MaxValue; if (lines != null) { // Cast onto each line, and return the point that's closest to the test point foreach (RayHitTestParameters line in lines) { Point3D point = Math3D.GetClosestPoint_Line_Point(line.Origin, line.Direction, testPoint); double localDistance = (point - testPoint).LengthSquared; if (retVal == null || localDistance < distance) { retVal = point; distance = localDistance; } } } if (circles != null) { // Cast onto each circle, and return the point that's closest to the test point foreach (CircleDefinition circle in circles) { Point3D?point = Math3D.GetClosestPoint_Circle_Point(circle.Plane, circle.Center, circle.Radius, testPoint); if (point != null) { double localDistance = (point.Value - testPoint).LengthSquared; if (retVal == null || localDistance < distance) { retVal = point.Value; distance = localDistance; } } } } return(retVal); }
/// <summary> /// This overload returns a point on the shape that is closest to the point passed in /// </summary> public Point3D?CastRay(Point3D point) { Point3D?retVal = null; switch (_shape) { case ShapeType.Line: #region Line retVal = Math3D.GetClosestPoint_Line_Point(_point, _direction, point); #endregion break; case ShapeType.Lines: #region Lines retVal = CastRay_LinesCircles(_lines, null, point); #endregion break; case ShapeType.Plane: #region Plane retVal = Math3D.GetClosestPoint_Plane_Point(_plane, point); #endregion break; case ShapeType.Circle: #region Circle retVal = Math3D.GetClosestPoint_Circle_Point(_plane, _point, _radius, point); #endregion break; case ShapeType.Circles: #region Circles retVal = CastRay_LinesCircles(null, _circles, point); #endregion break; case ShapeType.LinesCircles: #region LinesCircles retVal = CastRay_LinesCircles(_lines, _circles, point); #endregion break; case ShapeType.Cylinder: #region Cylinder retVal = Math3D.GetClosestPoint_Cylinder_Point(_point, _direction, _radius, point); #endregion break; case ShapeType.Sphere: #region Sphere retVal = Math3D.GetClosestPoint_Sphere_Point(_point, _radius, point); #endregion break; case ShapeType.Mesh: throw new ApplicationException("finish this"); case ShapeType.None: retVal = null; break; default: throw new ApplicationException("Unknown ShapeType: " + _shape.ToString()); } return(retVal); }