Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:rhinocommonsamples:principalcurvature [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Calculate Surface Curvature =====
 +
 +===== C# =====
 +<code c#>
 +[System.Runtime.InteropServices.Guid("​1309B0D7-0598-4B42-9460-10A3DFD6429B"​)]
 +public class PrincipalCurvatureCommand : Command
 +{
 +  public override string EnglishName { get { return "​csPrincipalCurvature";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    ObjRef obj_ref;
 +    var rc = RhinoGet.GetOneObject("​Select surface for curvature measurement",​ true, 
 +      ObjectType.Surface,​ out obj_ref);
 +    if (rc != Result.Success)
 +      return rc;
 +    var surface = obj_ref.Surface();​
 +
 +    var gp = new Rhino.Input.Custom.GetPoint();​
 +    gp.SetCommandPrompt("​Select point on surface for curvature measurement"​);​
 +    gp.Constrain(surface,​ false);
 +    gp.Get();
 +    if (gp.CommandResult() != Result.Success)
 +      return gp.CommandResult();​
 +    var point_on_surface = gp.Point();
 +
 +    double u, v;
 +    if (!surface.ClosestPoint(point_on_surface,​ out u, out v))
 +      return Result.Failure;​
 +
 +    var surface_curvature = surface.CurvatureAt(u,​ v);
 +    if (surface_curvature == null)
 +      return Result.Failure;​
 +
 +    RhinoApp.WriteLine(string.Format(
 +      "​Surface curvature evaluation at parameter: ({0}, {1})", u, v));
 +
 +    RhinoApp.WriteLine(string.Format(" ​ 3-D Point: ({0}, {1}, {2})",
 +      surface_curvature.Point.X,​
 +      surface_curvature.Point.Y,​
 +      surface_curvature.Point.Z));​
 +
 +    RhinoApp.WriteLine(string.Format(" ​ 3-D Normal: ({0}, {1}, {2})",
 +      surface_curvature.Normal.X,​
 +      surface_curvature.Normal.Y,​
 +      surface_curvature.Normal.Z));​
 +
 +    RhinoApp.WriteLine(string.Format(" ​ Maximum principal curvature: {0} ({1}, {2}, {3})", ​
 +      surface_curvature.Kappa(0), ​
 +      surface_curvature.Direction(0).X, ​
 +      surface_curvature.Direction(0).Y, ​
 +      surface_curvature.Direction(0).Z));​
 +
 +    RhinoApp.WriteLine(string.Format(" ​ Minimum principal curvature: {0} ({1}, {2}, {3})", ​
 +      surface_curvature.Kappa(1), ​
 +      surface_curvature.Direction(1).X, ​
 +      surface_curvature.Direction(1).Y, ​
 +      surface_curvature.Direction(1).Z));​
 +
 +    RhinoApp.WriteLine(string.Format(" ​ Gaussian curvature: {0}", ​
 +      surface_curvature.Gaussian));​
 +    RhinoApp.WriteLine(string.Format(" ​ Mean curvature: {0}", ​
 +      surface_curvature.Mean));​
 +
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +<​System.Runtime.InteropServices.Guid("​C0E6E3DD-A92A-44E2-AFE0-2DBA03F55C19"​)>​ _
 +Public Class PrincipalCurvatureCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbPrincipalCurvature"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    Dim obj_ref As ObjRef
 +    Dim rc = RhinoGet.GetOneObject("​Select surface for curvature measurement",​ True,
 +                                   ​ObjectType.Surface,​ obj_ref)
 +    If rc <> Result.Success Then
 +      Return rc
 +    End If
 +    Dim surface = obj_ref.Surface()
 +
 +    Dim gp = New Rhino.Input.Custom.GetPoint()
 +    gp.SetCommandPrompt("​Select point on surface for curvature measurement"​)
 +    gp.Constrain(surface,​ False)
 +    gp.[Get]()
 +    If gp.CommandResult() <> Result.Success Then
 +      Return gp.CommandResult()
 +    End If
 +    Dim point_on_surface = gp.Point()
 +
 +    Dim u As Double, v As Double
 +    If Not surface.ClosestPoint(point_on_surface,​ u, v) Then
 +      Return Result.Failure
 +    End If
 +
 +    Dim surface_curvature = surface.CurvatureAt(u,​ v)
 +    If surface_curvature Is Nothing Then
 +      Return Result.Failure
 +    End If
 +
 +    RhinoApp.WriteLine(String.Format(
 +      "​Surface curvature evaluation at parameter: ({0}, {1})", u, v))
 +
 +    RhinoApp.WriteLine(String.Format(" ​ 3-D Point: ({0}, {1}, {2})",
 +      surface_curvature.Point.X,​
 +      surface_curvature.Point.Y,​
 +      surface_curvature.Point.Z))
 +
 +    RhinoApp.WriteLine(String.Format(" ​ 3-D Normal: ({0}, {1}, {2})",
 +      surface_curvature.Normal.X,​
 +      surface_curvature.Normal.Y,​
 +      surface_curvature.Normal.Z))
 +
 +    RhinoApp.WriteLine(String.Format(" ​ Maximum principal curvature: {0} ({1}, {2}, {3})",
 +      surface_curvature.Kappa(0),​
 +      surface_curvature.Direction(0).X,​
 +      surface_curvature.Direction(0).Y,​
 +      surface_curvature.Direction(0).Z))
 +
 +    RhinoApp.WriteLine(String.Format(" ​ Minimum principal curvature: {0} ({1}, {2}, {3})",
 +      surface_curvature.Kappa(1),​
 +      surface_curvature.Direction(1).X,​
 +      surface_curvature.Direction(1).Y,​
 +      surface_curvature.Direction(1).Z))
 +
 +    RhinoApp.WriteLine(String.Format(" ​ Gaussian curvature: {0}",
 +      surface_curvature.Gaussian))
 +    RhinoApp.WriteLine(String.Format(" ​ Mean curvature: {0}",
 +      surface_curvature.Mean))
 +
 +    Return Result.Success
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +import rhinoscriptsyntax as rs
 +
 +surface_id,​_,​_,​_,​_,​_ = rs.GetSurfaceObject("​Select surface for curvature measurement"​)
 +point = rs.GetPointOnSurface(surface_id, ​
 +    "​Select point on surface for curvature measurement"​)
 +u,v = rs.SurfaceClosestPoint(surface_id,​ point)
 +
 +#point, normal, kappa_u, direction_u,​ kappa_v, direction_v,​ gaussian, mean =
 +surface_curvature = rs.SurfaceCurvature(surface_id,​ (u,v))
 +    ​
 +point, normal, kappa_u, direction_u,​ kappa_v, direction_v,​ gaussian, mean = surface_curvature
 +
 +print "​Surface curvature evaluation at parameter: ({0}, {1})"​.format(u,​v)
 +
 +print " ​ 3-D Point: ({0}, {1}, {2})"​.format(point.X,​ point.Y, point.Z)
 +
 +print " ​ 3-D Normal: ({0}, {1}, {2})"​.format(normal.X,​ normal.Y, normal.Z)
 +
 +print " ​ Maximum principal curvature: {0} ({1}, {2}, {3})"​.format(
 +  kappa_u, direction_u.X,​ direction_u.Y,​ direction_u.Z)
 +
 +print " ​ Minimum principal curvature: {0} ({1}, {2}, {3})"​.format(
 +  kappa_v, direction_v.X,​ direction_v.Y,​ direction_v.Z)
 +
 +print " ​ Gaussian curvature: {0}"​.format(gaussian)
 +print " ​ Mean curvature: {0}"​.format(mean)
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/principalcurvature.txt ยท Last modified: 2015/09/14 (external edit)