Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:crvdeviation [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Determine the Deviation between two Curves =====
 +
 +===== C# =====
 +<code c#>
 +class DeviationConduit : Rhino.Display.DisplayConduit
 +{
 +  private readonly Curve _curveA;
 +  private readonly Curve _curveB;
 +  private readonly Point3d _minDistPointA ;
 +  private readonly Point3d _minDistPointB ;
 +  private readonly Point3d _maxDistPointA ;
 +  private readonly Point3d _maxDistPointB ;
 +
 +  public DeviationConduit(Curve curveA, Curve curveB, Point3d minDistPointA,​ Point3d minDistPointB,​ Point3d maxDistPointA,​ Point3d maxDistPointB)
 +  {
 +    _curveA = curveA;
 +    _curveB = curveB;
 +    _minDistPointA = minDistPointA;​
 +    _minDistPointB = minDistPointB;​
 +    _maxDistPointA = maxDistPointA;​
 +    _maxDistPointB = maxDistPointB;​
 +  }
 +
 +  protected override void DrawForeground(Rhino.Display.DrawEventArgs e)
 +  {
 +    e.Display.DrawCurve(_curveA,​ Color.Red);
 +    e.Display.DrawCurve(_curveB,​ Color.Red);
 +
 +    e.Display.DrawPoint(_minDistPointA,​ Color.LawnGreen);​
 +    e.Display.DrawPoint(_minDistPointB,​ Color.LawnGreen);​
 +    e.Display.DrawLine(new Line(_minDistPointA,​ _minDistPointB),​ Color.LawnGreen);​
 +    e.Display.DrawPoint(_maxDistPointA,​ Color.Red);
 +    e.Display.DrawPoint(_maxDistPointB,​ Color.Red);
 +    e.Display.DrawLine(new Line(_maxDistPointA,​ _maxDistPointB),​ Color.Red);
 +  }
 +}
 +
 +[System.Runtime.InteropServices.Guid("​B2A5B553-FAB5-48B9-B4C8-0EF653983D04"​)]
 +public class CurveDeviationCommand : Command
 +{
 +  public override string EnglishName { get { return "​csCurveDeviation";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    doc.Objects.UnselectAll();​
 +
 +    ObjRef objRef1;
 +    var rc1 = RhinoGet.GetOneObject("​first curve",​ true, ObjectType.Curve,​ out objRef1);
 +    if (rc1 != Result.Success)
 +      return rc1;
 +    Curve curveA = null;
 +    if (objRef1 != null)
 +      curveA = objRef1.Curve();​
 +    if (curveA == null)
 +      return Result.Failure;​
 +
 +    // Since you already selected a curve if you don't unselect it
 +    // the next GetOneObject won't stop as it considers that curve 
 +    // input, i.e., curveA and curveB will point to the same curve.
 +    // Another option would be to use an instance of Rhino.Input.Custom.GetObject
 +    // instead of Rhino.Input.RhinoGet as GetObject has a DisablePreSelect() method.
 +    doc.Objects.UnselectAll();​
 +
 +    ObjRef objRef2;
 +    var rc2 = RhinoGet.GetOneObject("​second curve",​ true, ObjectType.Curve,​ out objRef2);
 +    if (rc2 != Result.Success)
 +      return rc2;
 +    Curve curveB = null;
 +    if (objRef2 != null)
 +      curveB = objRef2.Curve();​
 +    if (curveB == null)
 +      return Result.Failure;​
 +
 +    var tolerance = doc.ModelAbsoluteTolerance;​
 +
 +    double maxDistance;​
 +    double maxDistanceParameterA;​
 +    double maxDistanceParameterB;​
 +    double minDistance;​
 +    double minDistanceParameterA;​
 +    double minDistanceParameterB;​
 +
 +    DeviationConduit conduit;
 +    if (!Curve.GetDistancesBetweenCurves(curveA,​ curveB, tolerance, out maxDistance, ​
 +              out maxDistanceParameterA,​ out maxDistanceParameterB,​
 +              out minDistance,​ out minDistanceParameterA,​ out minDistanceParameterB))
 +    {
 +      Rhino.RhinoApp.WriteLine("​Unable to find overlap intervals."​);​
 +      return Result.Success;​
 +    } else
 +    {
 +      if (minDistance <= RhinoMath.ZeroTolerance)
 +        minDistance = 0.0;
 +      var maxDistPtA = curveA.PointAt(maxDistanceParameterA);​
 +      var maxDistPtB = curveB.PointAt(maxDistanceParameterB);​
 +      var minDistPtA = curveA.PointAt(minDistanceParameterA);​
 +      var minDistPtB = curveB.PointAt(minDistanceParameterB);​
 +
 +      conduit = new DeviationConduit(curveA,​ curveB, minDistPtA, minDistPtB, maxDistPtA, maxDistPtB) {Enabled = true};
 +      doc.Views.Redraw();​
 +
 +      RhinoApp.WriteLine(string.Format("​Minimum deviation = {0}   ​pointA({1},​ {2}, {3}), pointB({4}, {5}, {6})", minDistance, ​
 +        minDistPtA.X,​ minDistPtA.Y,​ minDistPtA.Z,​ minDistPtB.X,​ minDistPtB.Y,​ minDistPtB.Z));​
 +      RhinoApp.WriteLine(string.Format("​Maximum deviation = {0}   ​pointA({1},​ {2}, {3}), pointB({4}, {5}, {6})", maxDistance, ​
 +        maxDistPtA.X,​ maxDistPtA.Y,​ maxDistPtA.Z,​ maxDistPtB.X,​ maxDistPtB.Y,​ maxDistPtB.Z));​
 +    }
 +
 +    string str = "";​
 +    var s = RhinoGet.GetString("​Press Enter when done", true, ref str);
 +    conduit.Enabled = false;
 +
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Class DeviationConduit
 +  Inherits Rhino.Display.DisplayConduit
 +  Private ReadOnly _curveA As Curve
 +  Private ReadOnly _curveB As Curve
 +  Private ReadOnly _minDistPointA As Point3d
 +  Private ReadOnly _minDistPointB As Point3d
 +  Private ReadOnly _maxDistPointA As Point3d
 +  Private ReadOnly _maxDistPointB As Point3d
 +
 +  Public Sub New(curveA As Curve, curveB As Curve, minDistPointA As Point3d, minDistPointB As Point3d, maxDistPointA As Point3d, maxDistPointB As Point3d)
 +    _curveA = curveA
 +    _curveB = curveB
 +    _minDistPointA = minDistPointA
 +    _minDistPointB = minDistPointB
 +    _maxDistPointA = maxDistPointA
 +    _maxDistPointB = maxDistPointB
 +  End Sub
 +
 +  Protected Overrides Sub DrawForeground(e As Rhino.Display.DrawEventArgs)
 +    e.Display.DrawCurve(_curveA,​ Color.Red)
 +    e.Display.DrawCurve(_curveB,​ Color.Red)
 +
 +    e.Display.DrawPoint(_minDistPointA,​ Color.LawnGreen)
 +    e.Display.DrawPoint(_minDistPointB,​ Color.LawnGreen)
 +    e.Display.DrawLine(New Line(_minDistPointA,​ _minDistPointB),​ Color.LawnGreen)
 +    e.Display.DrawPoint(_maxDistPointA,​ Color.Red)
 +    e.Display.DrawPoint(_maxDistPointB,​ Color.Red)
 +    e.Display.DrawLine(New Line(_maxDistPointA,​ _maxDistPointB),​ Color.Red)
 +  End Sub
 +End Class
 +
 +<​System.Runtime.InteropServices.Guid("​C22DB90D-28F9-4D34-92F0-4358459FCAE4"​)>​ _
 +Public Class CurveDeviationCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbCurveDeviation"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    doc.Objects.UnselectAll()
 +
 +    Dim objRef1 As ObjRef
 +    Dim rc1 = RhinoGet.GetOneObject("​first curve",​ True, ObjectType.Curve,​ objRef1)
 +    If rc1 <> Result.Success Then
 +      Return rc1
 +    End If
 +    Dim curveA As Curve = Nothing
 +    If objRef1 IsNot Nothing Then
 +      curveA = objRef1.Curve()
 +    End If
 +    If curveA Is Nothing Then
 +      Return Result.Failure
 +    End If
 +
 +    ' Since you already selected a curve if you don't unselect it
 +    ' the next GetOneObject won't stop as it considers that curve 
 +    ' input, i.e., curveA and curveB will point to the same curve.
 +    ' Another option would be to use an instance of Rhino.Input.Custom.GetObject
 +    ' instead of Rhino.Input.RhinoGet as GetObject has a DisablePreSelect() method.
 +    doc.Objects.UnselectAll()
 +
 +    Dim objRef2 As ObjRef
 +    Dim rc2 = RhinoGet.GetOneObject("​second curve",​ True, ObjectType.Curve,​ objRef2)
 +    If rc2 <> Result.Success Then
 +      Return rc2
 +    End If
 +    Dim curveB As Curve = Nothing
 +    If objRef2 IsNot Nothing Then
 +      curveB = objRef2.Curve()
 +    End If
 +    If curveB Is Nothing Then
 +      Return Result.Failure
 +    End If
 +
 +    Dim tolerance = doc.ModelAbsoluteTolerance
 +
 +    Dim maxDistance As Double
 +    Dim maxDistanceParameterA As Double
 +    Dim maxDistanceParameterB As Double
 +    Dim minDistance As Double
 +    Dim minDistanceParameterA As Double
 +    Dim minDistanceParameterB As Double
 +
 +    Dim conduit As DeviationConduit
 +    If Not Curve.GetDistancesBetweenCurves(curveA,​ curveB, tolerance, maxDistance,​ maxDistanceParameterA,​ maxDistanceParameterB,​ _
 +      minDistance,​ minDistanceParameterA,​ minDistanceParameterB) Then
 +      Rhino.RhinoApp.WriteLine("​Unable to find overlap intervals."​)
 +      Return Result.Success
 +    Else
 +      If minDistance <= RhinoMath.ZeroTolerance Then
 +        minDistance = 0.0
 +      End If
 +      Dim maxDistPtA = curveA.PointAt(maxDistanceParameterA)
 +      Dim maxDistPtB = curveB.PointAt(maxDistanceParameterB)
 +      Dim minDistPtA = curveA.PointAt(minDistanceParameterA)
 +      Dim minDistPtB = curveB.PointAt(minDistanceParameterB)
 +
 +      conduit = New DeviationConduit(curveA,​ curveB, minDistPtA, minDistPtB, maxDistPtA, maxDistPtB)
 +      conduit.Enabled = True
 +
 +      doc.Views.Redraw()
 +
 +      RhinoApp.WriteLine(String.Format("​Minimum deviation = {0}   ​pointA({1},​ {2}, {3}), pointB({4}, {5}, {6})", minDistance,​ minDistPtA.X,​ minDistPtA.Y,​ minDistPtA.Z,​ minDistPtB.X,​ _
 +        minDistPtB.Y,​ minDistPtB.Z))
 +      RhinoApp.WriteLine(String.Format("​Maximum deviation = {0}   ​pointA({1},​ {2}, {3}), pointB({4}, {5}, {6})", maxDistance,​ maxDistPtA.X,​ maxDistPtA.Y,​ maxDistPtA.Z,​ maxDistPtB.X,​ _
 +        maxDistPtB.Y,​ maxDistPtB.Z))
 +    End If
 +
 +    Dim str As String = ""​
 +    Dim s = RhinoGet.GetString("​Press Enter when done", True, str)
 +    conduit.Enabled = False
 +
 +    Return Result.Success
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +import rhinoscriptsyntax as rs
 +import scriptcontext
 +import Rhino
 +
 +def RunCommand():​
 +  crvA = rs.GetCurveObject("​first curve"​)[0]
 +  crvA = rs.coercecurve(crvA)
 +  crvB = rs.GetCurveObject("​second curve"​)[0]
 +  crvB = rs.coercecurve(crvB)
 +  if crvA == None or crvB == None:
 +      return Rhino.Commands.Result.Failure
 +      ​
 +  maxa, maxb, maxd, mina, minb, mind = rs.CurveDeviation(crvA,​ crvB)
 +  ​
 +  if mind <= Rhino.RhinoMath.ZeroTolerance:​
 +      mind = 0.0;
 +  maxDistPtA = crvA.PointAt(maxa)
 +  maxDistPtB = crvB.PointAt(maxb)
 +  minDistPtA = crvA.PointAt(mina)
 +  minDistPtB = crvB.PointAt(minb)
 +
 +  print "​Minimum deviation = {0}   ​pointA({1},​ {2}, {3}), pointB({4}, {5}, {6})"​.format(
 +    mind, minDistPtA.X,​ minDistPtA.Y,​ minDistPtA.Z,​ minDistPtB.X,​ minDistPtB.Y,​ minDistPtB.Z)
 +  print "​Maximum deviation = {0}   ​pointA({1},​ {2}, {3}), pointB({4}, {5}, {6})"​.format(
 +    maxd, maxDistPtA.X,​ maxDistPtA.Y,​ maxDistPtA.Z,​ maxDistPtB.X,​ maxDistPtB.Y,​ maxDistPtB.Z)
 +
 +if __name__=="​__main__":​
 +  RunCommand()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/crvdeviation.txt ยท Last modified: 2015/09/14 (external edit)