Grasshopper

algorithmic modeling for Rhino

Hi All,

Anyone knows how to convert Surface to Sphere in C#??

 

I am trying to make a sphere.. from a surface. But couldn't figure it out..

 

Please help me..

 

 

    if(srf.IsSphere()){
      Sphere sh = ???
      Point3d cen = sh.Center;
      Circle cir = new Circle(cen, sh.Radius);
      Cylinder cl = new Cylinder(cir, 10.0);
      A = cl;
    }

Views: 538

Replies to This Discussion

I would suspect something like this:

Sphere outSphere = outSphere.Unset;
if (Surface.TryGetSphere(out outSphere))

{

     A = outSphere;

}

Thx!!! It worked!

In case that fails:

private void RunScript(Surface S, ref object A)
  {
    Sphere sphere;
    if (S.TryGetSphere(out sphere))
    {
      if(!sphere.IsValid)
        sphere = new Sphere(sphere.Center, sphere.Radius);
    }
    else
      sphere = BuildSphere(S);

    A = sphere;
  }

  // <Custom additional code>
  static Random random = new Random(24601);
  int n = 1000;

  Sphere BuildSphere(Surface surface)
  {
    var points = new List<Point3d>();
    var center = Point3d.Origin;
    for(int i = 0;i < n;i++)
    {
      Point3d point = surface.PointAt(surface.Domain(0).ParameterAt(random.NextDouble()), surface.Domain(1).ParameterAt(random.NextDouble()));
      center += point / n;
      points.Add(point);
    }

    double radius = 0;
    foreach(var point in points)
      radius += center.DistanceTo(point) / n;

    Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Sphere had to be reconstructed.");
    return new Sphere(center, radius);
  }
  // </Custom additional code>

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service