Closest Point

Hello, I got two technical questions:
1- I need to project a 3d point from a vector on to a curve
what is the best way for that ? my solution is O(range/tolerance) complexity
which is very slow …
2- when I use ClosetPoint() method of NurbsCurve in Rhino Common
does it use Rhino binary for computing ? or the .Net Frame work ? it’s dam fast ! :smiley:

My code:

// Grasshopper Script Instance
#region Usings
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

using Rhino;
using Rhino.Geometry;

using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
#endregion

public class Script_Instance : GH_ScriptInstance
{
    #region Notes
    /* 
      Members:
        RhinoDoc RhinoDocument
        GH_Document GrasshopperDocument
        IGH_Component Component
        int Iteration

      Methods (Virtual & overridable):
        Print(string text)
        Print(string format, params object[] args)
        Reflect(object obj)
        Reflect(object obj, string method_name)
    */
    #endregion

    private void RunScript(
	Point3d Pta,
	Point3d Ptb,
	double threshold,
	Curve crv,
	double range,
	ref object a,
	ref object b)
    {
        // Write your logic here
        a = test_point(Pta,Ptb,threshold);
        b = Point_On_Curve(
        Pta,
        Ptb-Pta,
        crv.ToNurbsCurve(),
        range,
        threshold
    );
    
    }
    // Project Point on curve
    public Point3d Point_On_Curve(
        Point3d input_point,
        Vector3d projection_vector,
        NurbsCurve input_crv,
        double range,
        double tolerance
    ){
        projection_vector.Unitize();
        projection_vector*=tolerance;
        Point3d result = new Point3d();
        input_crv.Domain = new Interval(0,1);
        double increment = tolerance;
        bool exit = false;
        while (increment<range)
        {
            for (double j=0.0;j<=1;j+=0.001)
            {
                if(test_point((input_point+projection_vector),input_crv.PointAt(j),0.01)){
                   result = input_point+projection_vector;
                   exit=true;
                   break;
                }
            }
            if(exit){
                break;
            }
            projection_vector.Unitize();
            projection_vector*=increment;
            increment+=tolerance;
            Console.WriteLine("{0}", projection_vector.Length);
        }
        return result;
    }

    bool test_point( Point3d pt_a, Point3d pt_b,double threshold)
    {
        if((pt_a-pt_b).Length<=threshold){
          return true;
        }else{
          return false;
        }
    }
}

PointProjection.gh (11.5 KB)

Get some ideas from the attached

Curve_ClosestPoint_V0.gh (16.6 KB)

1 Like

hey thank you I appreciate, let me take look… :smiley: