diff --git a/src/DynamoManipulation/DynamoManipulation.csproj b/src/DynamoManipulation/DynamoManipulation.csproj
index 8abbecd33d5..f444315f3a3 100644
--- a/src/DynamoManipulation/DynamoManipulation.csproj
+++ b/src/DynamoManipulation/DynamoManipulation.csproj
@@ -42,6 +42,10 @@
+
+
+
+
True
diff --git a/src/DynamoManipulation/Gizmo.cs b/src/DynamoManipulation/Gizmo.cs
index bd3a6c40b0d..a34fb6a34c0 100644
--- a/src/DynamoManipulation/Gizmo.cs
+++ b/src/DynamoManipulation/Gizmo.cs
@@ -1,10 +1,10 @@
-using System;
+using System;
using System.Windows;
using System.Windows.Media.Media3D;
using Dynamo.Visualization;
using Dynamo.Wpf.ViewModels.Watch3D;
-using Point = Autodesk.DesignScript.Geometry.Point;
-using Vector = Autodesk.DesignScript.Geometry.Vector;
+using Point = Autodesk.GeometryPrimitives.Dynamo.Geometry.Point;
+using Vector = Autodesk.GeometryPrimitives.Dynamo.Math.Vector3d;
namespace Dynamo.Manipulation
{
@@ -90,10 +90,7 @@ protected IRenderPackageFactory RenderPackageFactory
///
/// Physical location of the manipulator lying on the geometry being manipulated
///
- protected Point ManipulatorOrigin
- {
- get { return manipulator.Origin; }
- }
+ protected Point ManipulatorOrigin => manipulator.Origin;
private string name = "gizmo";
public string Name
@@ -117,29 +114,26 @@ protected Point Origin
{
get
{
- if(origin != null) origin.Dispose();
+ var cameraPos = cameraPosition != null
+ ? new Point(cameraPosition.Value.X, cameraPosition.Value.Y, cameraPosition.Value.Z)
+ : null;
- using (var cameraPos = cameraPosition != null
- ? Point.ByCoordinates(cameraPosition.Value.X, cameraPosition.Value.Y, cameraPosition.Value.Z)
- : null)
+ if (cameraPos == null)
{
-
- if (cameraPos == null)
- {
- // cameraPos will be null if HelixWatch3DViewModel is not initialized
- // this happens on an out of memory exception in SharpDX probably due to
- // DynamoEffectsManager not being disposed off promptly.
- // TODO: revisit to fix this properly later
- // For the time being return a default position instead of throwing an exception
- // to the effect that camerPos should not be null
- return Point.ByCoordinates(ManipulatorOrigin.X, ManipulatorOrigin.Y, ManipulatorOrigin.Z);
- }
-
- using (var vec = Vector.ByTwoPoints(cameraPos, ManipulatorOrigin).Normalized())
- {
- origin = cameraPos.Add(vec.Scale(zDepth));
- }
+ // cameraPos will be null if HelixWatch3DViewModel is not initialized
+ // this happens on an out of memory exception in SharpDX probably due to
+ // DynamoEffectsManager not being disposed off promptly.
+ // TODO: revisit to fix this properly later
+ // For the time being return a default position instead of throwing an exception
+ // to the effect that camerPos should not be null
+ return new Point(ManipulatorOrigin.Position.X, ManipulatorOrigin.Position.Y,
+ ManipulatorOrigin.Position.Z);
}
+
+ var vec = (ManipulatorOrigin.Position - cameraPos.Position).Unit;
+ vec.Scale(zDepth);
+ origin = new Point(cameraPos.Position + vec);
+
return origin;
}
}
@@ -212,8 +206,6 @@ public void Dispose()
{
Dispose(true);
- if(origin != null) origin.Dispose();
-
BackgroundPreviewViewModel.ViewCameraChanged -= OnViewCameraChanged;
}
}
diff --git a/src/DynamoManipulation/MousePointManipulator.cs b/src/DynamoManipulation/MousePointManipulator.cs
index 51759b2b9e2..e661bf981f9 100644
--- a/src/DynamoManipulation/MousePointManipulator.cs
+++ b/src/DynamoManipulation/MousePointManipulator.cs
@@ -1,11 +1,11 @@
-using System;
+using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
-using Autodesk.DesignScript.Geometry;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.ZeroTouch;
-using Point = Autodesk.DesignScript.Geometry.Point;
+using Autodesk.GeometryPrimitives.Dynamo.Geometry;
+using Vector = Autodesk.GeometryPrimitives.Dynamo.Math.Vector3d;
namespace Dynamo.Manipulation
{
@@ -20,13 +20,14 @@ public INodeManipulator Create(NodeModel node, DynamoManipulationExtension manip
public class MousePointManipulator : NodeManipulator
{
private Point origin;
- internal override Point Origin { get { return origin; } }
+ internal override Point Origin => origin;
private TranslationGizmo gizmo;
// Holds manipulator axis and input node pair for each input port.
// This collection is accessed from multiple threads
- private ConcurrentDictionary> indexedAxisNodePairs = new ConcurrentDictionary>();
+ private readonly ConcurrentDictionary> indexedAxisNodePairs
+ = new ConcurrentDictionary>();
internal MousePointManipulator(DSFunction node, DynamoManipulationExtension manipulatorContext)
: base(node, manipulatorContext)
@@ -38,15 +39,14 @@ internal MousePointManipulator(DSFunction node, DynamoManipulationExtension mani
protected override void AssignInputNodes()
{
//Default axes
- var axes = new Vector[] { Vector.XAxis(), Vector.YAxis(), Vector.ZAxis() };
+ var axes = new Vector[] { Vector.XAxis, Vector.YAxis, Vector.ZAxis };
indexedAxisNodePairs.Clear();
for (int i = 0; i < 3; i++)
{
//First find out if the input can be manipulated.
- NodeModel node = null;
- if (!CanManipulateInputNode(i, out node))
+ if (!CanManipulateInputNode(i, out var node))
{
continue;
}
@@ -69,7 +69,7 @@ protected override void AssignInputNodes()
{
//Combine old axis with this axis
axis = item.Value.Item1;
- axis = axis.Add(axes[i]);
+ axis = axis + axes[i];
idx = item.Key;
break;
}
@@ -87,10 +87,9 @@ protected override void AssignInputNodes()
// Normalize all axes in indexedAxisNodePairs
for (int i = 0; i < 3; i++)
{
- Tuple pair;
- if (indexedAxisNodePairs.TryGetValue(i, out pair))
+ if (indexedAxisNodePairs.TryGetValue(i, out var pair))
{
- indexedAxisNodePairs[i] = Tuple.Create(pair.Item1.Normalized(), pair.Item2);
+ indexedAxisNodePairs[i] = Tuple.Create(pair.Item1.Unit, pair.Item2);
}
}
}
@@ -109,7 +108,7 @@ protected override IEnumerable GetGizmos(bool createOrUpdate)
yield break;
//No axis data, so no gizmo.
- if (!indexedAxisNodePairs.Any())
+ if (indexedAxisNodePairs.IsEmpty)
yield break;
if (createOrUpdate)
@@ -134,11 +133,10 @@ protected override IEnumerable OnGizmoClick(IGizmo gizmoInAction, obj
if(axis1 == null)
{
//Hit object is a plane, two axes will be updated simultaneously.
- var plane = hitObject as Plane;
- if(plane != null)
+ if(hitObject is Plane plane)
{
- axis1 = plane.XAxis;
- axis2 = plane.YAxis;
+ axis1 = plane.UAxis;
+ axis2 = plane.Normal * plane.UAxis;
}
}
@@ -149,11 +147,8 @@ protected override IEnumerable OnGizmoClick(IGizmo gizmoInAction, obj
var node = item.Value.Item2;
if (v.Equals(axis1) || v.Equals(axis2))
{
- if (node == null)
- {
- node = CreateAndConnectInputNode(0, item.Key);
- }
-
+ node ??= CreateAndConnectInputNode(0, item.Key);
+
nodes.Add(item.Key, node);
}
}
@@ -178,9 +173,8 @@ protected override IEnumerable OnGizmoClick(IGizmo gizmoInAction, obj
/// Offset by which the gizmo has moved.
protected override void OnGizmoMoved(IGizmo gizmoInAction, Vector offset)
{
- var offsetPos = origin.Add(offset);
- origin.Dispose();
- origin = offsetPos;
+ var offsetPos = origin.Position + offset;
+ origin = new Point(offsetPos);
}
protected override List<(NodeModel inputNode, double amount)> InputNodesToUpdateAfterMove(Vector offset)
@@ -192,15 +186,13 @@ protected override void OnGizmoMoved(IGizmo gizmoInAction, Vector offset)
// When more than one input is connected to the same slider, this
// method will decompose the axis corresponding to each input.
- using (var v = GetFirstAxisComponent(item.Value.Item1))
- {
- var amount = offset.Dot(v);
+ var v = GetFirstAxisComponent(item.Value.Item1);
+ var amount = offset % v;
- if (Math.Abs(amount) > MIN_OFFSET_VAL)
- {
- dynamic uiNode = item.Value.Item2;
- inputNodes.Add((uiNode, uiNode.Value + amount));
- }
+ if (Math.Abs(amount) > MIN_OFFSET_VAL)
+ {
+ dynamic uiNode = item.Value.Item2;
+ inputNodes.Add((uiNode, uiNode.Value + amount));
}
}
return inputNodes;
@@ -213,18 +205,15 @@ protected override bool UpdatePosition()
{
if (Node == null || !indexedAxisNodePairs.Any()) return false;
- if (origin == null)
- {
- origin = Point.Origin(); //First time initialization
- }
+ origin ??= new Point(0, 0, 0);
//Node output could be a collection, consider the first item as origin.
- Point pt = GetFirstValueFromNode(Node) as Point;
+ var pt = GetFirstValueFromNode(Node) as Autodesk.DesignScript.Geometry.Point;
if (null == pt) return false; //The node output is not Point, could be a function object.
//Don't cache pt directly here, we need to create a copy, because
//pt may be GC'ed by VM.
- origin = Point.ByCoordinates(pt.X, pt.Y, pt.Z);
+ origin = new Point(pt.X, pt.Y, pt.Z);
return true;
}
@@ -266,15 +255,15 @@ private void UpdateGizmo()
private Vector GetFirstAxisComponent(Vector vector)
{
var tol = 0.0001;
- var v1 = Vector.ByCoordinates(vector.X, 0, 0);
- if (v1.Length > tol)
- return v1.Normalized();
+ var v1 = new Vector(vector.X, 0, 0);
+ if (v1.Magnitude > tol)
+ return v1.Unit;
- var v2 = Vector.ByCoordinates(0, vector.Y, 0);
- if (v2.Length > tol)
- return v2.Normalized();
+ var v2 = new Vector(0, vector.Y, 0);
+ if (v2.Magnitude > tol)
+ return v2.Unit;
- return vector.Normalized();
+ return vector.Unit;
}
#endregion
diff --git a/src/DynamoManipulation/NodeManipulator.cs b/src/DynamoManipulation/NodeManipulator.cs
index e195c3766b8..20533a92925 100644
--- a/src/DynamoManipulation/NodeManipulator.cs
+++ b/src/DynamoManipulation/NodeManipulator.cs
@@ -1,4 +1,3 @@
-using Autodesk.DesignScript.Geometry;
using CoreNodeModels.Input;
using Dynamo.Extensions;
using Dynamo.Graph.Nodes;
@@ -15,8 +14,8 @@
using System.Threading;
using System.Windows.Input;
using System.Windows.Media.Media3D;
-using Point = Autodesk.DesignScript.Geometry.Point;
-using Vector = Autodesk.DesignScript.Geometry.Vector;
+using Autodesk.GeometryPrimitives.Dynamo.Geometry;
+using Vector = Autodesk.GeometryPrimitives.Dynamo.Math.Vector3d;
namespace Dynamo.Manipulation
{
@@ -51,25 +50,13 @@ public abstract class NodeManipulator : INodeManipulator
#region properties
- protected IWorkspaceModel WorkspaceModel
- {
- get { return manipulatorContext.WorkspaceModel; }
- }
+ protected IWorkspaceModel WorkspaceModel => manipulatorContext.WorkspaceModel;
- protected ICommandExecutive CommandExecutive
- {
- get { return manipulatorContext.CommandExecutive; }
- }
+ protected ICommandExecutive CommandExecutive => manipulatorContext.CommandExecutive;
- protected string UniqueId
- {
- get { return manipulatorContext.UniqueId; }
- }
+ protected string UniqueId => manipulatorContext.UniqueId;
- protected string ExtensionName
- {
- get { return manipulatorContext.Name; }
- }
+ protected string ExtensionName => manipulatorContext.Name;
protected IGizmo GizmoInAction { get; private set; }
@@ -81,15 +68,9 @@ protected string ExtensionName
///
internal abstract Point Origin { get; }
- internal IWatch3DViewModel BackgroundPreviewViewModel
- {
- get { return manipulatorContext.BackgroundPreviewViewModel; }
- }
+ internal IWatch3DViewModel BackgroundPreviewViewModel => manipulatorContext.BackgroundPreviewViewModel;
- internal IRenderPackageFactory RenderPackageFactory
- {
- get { return manipulatorContext.RenderPackageFactory; }
- }
+ internal IRenderPackageFactory RenderPackageFactory => manipulatorContext.RenderPackageFactory;
internal Point3D? CameraPosition { get; private set; }
@@ -173,7 +154,6 @@ private bool IsValidNode
///
protected virtual void Dispose(bool disposing)
{
- if (Origin != null) Origin.Dispose();
}
///
@@ -188,8 +168,8 @@ protected virtual void MouseDown(object sender, MouseButtonEventArgs mouseButton
active = UpdatePosition();
if (Origin != null )
{
- originBeforeMove = Point.ByCoordinates(Origin.X, Origin.Y, Origin.Z);
- originAfterMove = Point.ByCoordinates(Origin.X, Origin.Y, Origin.Z);
+ originBeforeMove = new Point(Origin.Position.X, Origin.Position.Y, Origin.Position.Z);
+ originAfterMove = new Point(Origin.Position.X, Origin.Position.Y, Origin.Position.Z);
}
GizmoInAction = null; //Reset Drag.
@@ -202,21 +182,18 @@ protected virtual void MouseDown(object sender, MouseButtonEventArgs mouseButton
foreach (var item in gizmos)
{
- using(var originPt = ray.GetOriginPoint())
- using (var dirVec = ray.GetDirectionVector())
+ var originPt = ray.GetOriginPoint();
+ var dirVec = ray.GetDirectionVector();
+ if (item.HitTest(originPt, dirVec, out var hitObject))
{
- object hitObject;
- if (item.HitTest(originPt, dirVec, out hitObject))
+ GizmoInAction = item;
+
+ var nodes = OnGizmoClick(item, hitObject).ToList();
+ if (nodes.Count != 0)
{
- GizmoInAction = item;
-
- var nodes = OnGizmoClick(item, hitObject).ToList();
- if (nodes.Any())
- {
- WorkspaceModel.RecordModelsForModification(nodes);
- }
- return;
+ WorkspaceModel.RecordModelsForModification(nodes);
}
+ return;
}
}
}
@@ -232,7 +209,8 @@ protected virtual void MouseUp(object sender, MouseButtonEventArgs e)
if (originBeforeMove != null && originAfterMove != null)
{
- var inputNodesToManipulate = InputNodesToUpdateAfterMove(Vector.ByTwoPoints(originBeforeMove, originAfterMove));
+ var inputNodesToManipulate = InputNodesToUpdateAfterMove(
+ originAfterMove.Position - originBeforeMove.Position);
foreach (var (inputNode, amount) in inputNodesToManipulate)
{
if (inputNode == null) continue;
@@ -271,12 +249,11 @@ protected virtual void MouseMove(object sender, MouseEventArgs mouseEventArgs)
}
var offset = GizmoInAction.GetOffset(clickRay.GetOriginPoint(), clickRay.GetDirectionVector());
- if (offset.Length < 0.01) return;
+ if (offset.Magnitude < 0.01) return;
if (originAfterMove != null)
{
- var offsetPos = originAfterMove.Add(offset);
- originAfterMove.Dispose();
+ var offsetPos = new Point(originAfterMove.Position + offset);
originAfterMove = offsetPos;
}
@@ -354,8 +331,7 @@ protected bool CanManipulateInputNode(int inputPortIndex, out NodeModel inputNod
{
bool manipulate = false;
inputNode = null;
- Tuple val;
- if (Node.InputNodes.TryGetValue(inputPortIndex, out val))
+ if (Node.InputNodes.TryGetValue(inputPortIndex, out var val))
{
if (val != null)
{
@@ -408,8 +384,10 @@ protected NodeModel CreateAndConnectInputNode(int outputPortIndex, int inputPort
if (inputNode != null)
{
// Assign the input slider to the default value of the node's input port
- var doubleNode = Node.InPorts[inputPortIndex].DefaultValue as DoubleNode;
- if (doubleNode != null) inputNode.Value = doubleNode.Value;
+ if (Node.InPorts[inputPortIndex].DefaultValue is DoubleNode doubleNode)
+ {
+ inputNode.Value = doubleNode.Value;
+ }
}
return inputNode;
}
@@ -553,15 +531,12 @@ private void HighlightGizmoOnRollOver(IRay clickRay)
{
item.UnhighlightGizmo();
- using (var originPt = clickRay.GetOriginPoint())
- using (var dirVec = clickRay.GetDirectionVector())
+ var originPt = clickRay.GetOriginPoint();
+ var dirVec = clickRay.GetDirectionVector();
+ if (item.HitTest(originPt, dirVec, out _))
{
- object hitObject;
- if (item.HitTest(originPt, dirVec, out hitObject))
- {
- item.HighlightGizmo();
- return;
- }
+ item.HighlightGizmo();
+ return;
}
}
}
@@ -594,12 +569,6 @@ public void Dispose()
Node.ClearTransientWarning(warning);
}
- if (originBeforeMove != null)
- originBeforeMove.Dispose();
-
- if (originAfterMove != null)
- originAfterMove.Dispose();
-
DeleteGizmos();
DetachHandlers();
}
@@ -670,12 +639,12 @@ internal static class PointExtensions
{
public static Point ToPoint(this Point3D point)
{
- return Point.ByCoordinates(point.X, point.Y, point.Z);
+ return new Point(point.X, point.Y, point.Z);
}
public static Vector ToVector(this Vector3D vec)
{
- return Vector.ByCoordinates(vec.X, vec.Y, vec.Z);
+ return new Vector(vec.X, vec.Y, vec.Z);
}
}
@@ -686,26 +655,32 @@ internal static class RayExtensions
public static Line ToLine(this IRay ray)
{
- using(var origin = ray.Origin.ToPoint())
- using (var direction = ray.Direction.ToVector())
- {
- return Line.ByStartPointEndPoint(origin, origin.Add(direction.Scale(rayScaleFactor)));
- }
+ var origin = ray.Origin.ToPoint();
+ var direction = ray.Direction.ToVector();
+ direction.Scale(rayScaleFactor);
+ return new Line(origin.Position, direction);
}
public static Line ToOriginCenteredLine(this IRay ray)
{
- using(var origin = ray.Origin.ToPoint())
- using (var direction = ray.Direction.ToVector())
- {
- return ToOriginCenteredLine(origin, direction);
- }
+ var origin = ray.Origin.ToPoint();
+ var direction = ray.Direction.ToVector();
+ return ToOriginCenteredLine(origin, direction);
}
public static Line ToOriginCenteredLine(Point origin, Vector axis)
{
- return Line.ByStartPointEndPoint(origin.Add(axis.Scale(-axisScaleFactor)),
- origin.Add(axis.Scale(axisScaleFactor)));
+ var vec1 = new Vector(axis.X, axis.Y, axis.Z);
+ vec1.Scale(-axisScaleFactor);
+
+ var vec2 = new Vector(axis.X, axis.Y, axis.Z);
+ vec2.Scale(axisScaleFactor);
+
+ var startPos = origin.Position + vec1;
+ var endPos = origin.Position + vec2;
+ var dir = endPos - startPos;
+
+ return new Line(startPos, dir);
}
public static Point GetOriginPoint(this IRay ray)
diff --git a/src/DynamoManipulation/PointOnCurveManipulator.cs b/src/DynamoManipulation/PointOnCurveManipulator.cs
index 11c9156c537..ef1fe47d5fe 100644
--- a/src/DynamoManipulation/PointOnCurveManipulator.cs
+++ b/src/DynamoManipulation/PointOnCurveManipulator.cs
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
-using Autodesk.DesignScript.Geometry;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.ZeroTouch;
+using Autodesk.GeometryPrimitives.Dynamo.Math;
+using Autodesk.GeometryPrimitives.Dynamo.Geometry;
+using Vector = Autodesk.GeometryPrimitives.Dynamo.Math.Vector3d;
namespace Dynamo.Manipulation
{
@@ -18,7 +20,7 @@ class PointOnCurveManipulator : NodeManipulator
{
private Point pointOnCurve;
- private Curve curve;
+ private Autodesk.DesignScript.Geometry.Curve curve;
private Vector tangent;
@@ -33,10 +35,7 @@ internal PointOnCurveManipulator(DSFunction node, DynamoManipulationExtension ma
#region abstract method implementation
- internal override Point Origin
- {
- get { return pointOnCurve; }
- }
+ internal override Point Origin => pointOnCurve;
///
/// Returns all the gizmos supported by this manipulator
@@ -69,8 +68,7 @@ private void UpdateGizmo()
protected override void AssignInputNodes()
{
curve = null;
- NodeModel curveNode;
- CanManipulateInputNode(0, out curveNode);
+ CanManipulateInputNode(0, out var curveNode);
if (null == curveNode)
return; //Not enough input to manipulate
@@ -79,34 +77,60 @@ protected override void AssignInputNodes()
try
{
- curve = GetFirstValueFromNode(curveNode) as Curve;
+ curve = GetFirstValueFromNode(curveNode) as Autodesk.DesignScript.Geometry.Curve;
if (null == curve)
return;
}
- catch (Exception)
- {
- return;
+ catch
+ {
}
}
+ // TODO: Implement this method
+ private static Point PointAtParameter(Autodesk.DesignScript.Geometry.Curve curve, double param)
+ {
+ var pt = curve.PointAtParameter(param);
+ return new Point(pt.X, pt.Y, pt.Z);
+ }
+
+ // TODO: Implement this method
+ private static double ParameterAtPoint(Autodesk.DesignScript.Geometry.Curve curve, Point pt)
+ {
+ var point = Autodesk.DesignScript.Geometry.Point.ByCoordinates(
+ pt.Position.X, pt.Position.Y, pt.Position.Z);
+ return curve.ParameterAtPoint(point);
+ }
+
+ // TODO: Implement this method
+ private static Vector TangentAtParameter(Autodesk.DesignScript.Geometry.Curve curve, double param)
+ {
+ var tangent = curve.TangentAtParameter(param);
+ return new Vector(tangent.X, tangent.Y, tangent.Z);
+ }
+
+ // TODO: Implement this method
+ private static Point ClosestPointTo(Autodesk.DesignScript.Geometry.Curve curve, Point3d pt)
+ {
+ var point = Autodesk.DesignScript.Geometry.Point.ByCoordinates(
+ pt.X, pt.Y, pt.Z);
+ var closestPt = curve.ClosestPointTo(point);
+ return new Point(closestPt.X, closestPt.Y, closestPt.Z);
+ }
+
protected override bool UpdatePosition()
{
if (curve == null) //Curve is not initialized, can't be manipulated now.
return false;
- if (pointOnCurve == null)
- pointOnCurve = curve.StartPoint;
+ pointOnCurve ??= PointAtParameter(curve, 0);
//Node output could be a collection, consider the first item as origin.
- Point pt = GetFirstValueFromNode(Node) as Point;
+ var pt = GetFirstValueFromNode(Node) as Autodesk.DesignScript.Geometry.Point;
if (pt == null) return false; //The node output is not Point, could be a function object.
- var param = curve.ParameterAtPoint(pt);
- tangent = curve.TangentAtParameter(param);
-
- //Don't cache pt directly here, need to create a copy, because
- //pt may be GC'ed by VM.
- pointOnCurve = Point.ByCoordinates(pt.X, pt.Y, pt.Z);
+ pointOnCurve = new Point(pt.X, pt.Y, pt.Z);
+ var param = ParameterAtPoint(curve, pointOnCurve);
+ tangent = TangentAtParameter(curve, param);
return tangent != null;
}
@@ -127,29 +151,23 @@ protected override IEnumerable OnGizmoClick(IGizmo gizmoInAction, obj
protected override void OnGizmoMoved(IGizmo gizmoInAction, Vector offset)
{
double param;
- using (var offsetPosition = pointOnCurve.Add(offset))
- {
- using (var closestPosition = curve.ClosestPointTo(offsetPosition))
- {
- param = curve.ParameterAtPoint(closestPosition);
- }
- }
+ var offsetPosition = pointOnCurve.Position + offset;
+ var closestPosition = ClosestPointTo(curve, offsetPosition);
+ param = ParameterAtPoint(curve, closestPosition);
param = Math.Round(param, ROUND_UP_PARAM);
- tangent = curve.TangentAtParameter(param);
- pointOnCurve = curve.PointAtParameter(param);
+ tangent = TangentAtParameter(curve, param);
+ pointOnCurve = PointAtParameter(curve, param);
}
protected override List<(NodeModel inputNode, double amount)> InputNodesToUpdateAfterMove(Vector offset)
{
- double param = curve.ParameterAtPoint(pointOnCurve);
+ double param = ParameterAtPoint(curve, pointOnCurve);
return new List<(NodeModel, double)>() { (inputNode, param) };
}
protected override void Dispose(bool disposing)
{
- if(tangent != null) tangent.Dispose();
-
base.Dispose(disposing);
}
diff --git a/src/DynamoManipulation/TranslationGizmo.cs b/src/DynamoManipulation/TranslationGizmo.cs
index b73e96b7cc7..29afaf931d0 100644
--- a/src/DynamoManipulation/TranslationGizmo.cs
+++ b/src/DynamoManipulation/TranslationGizmo.cs
@@ -1,12 +1,15 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
-using Autodesk.DesignScript.Geometry;
using Autodesk.DesignScript.Interfaces;
using Dynamo.Visualization;
using Dynamo.Wpf.ViewModels.Watch3D;
+using Autodesk.GeometryPrimitives.Dynamo.Geometry;
+using Vector = Autodesk.GeometryPrimitives.Dynamo.Math.Vector3d;
+using Matrix = Autodesk.GeometryPrimitives.Dynamo.Math.Matrix3d;
+
namespace Dynamo.Manipulation
{
@@ -37,6 +40,11 @@ enum Axes
}
#region Private members
+ ///
+ /// Hit tolerance
+ ///
+ private const double tolerance = 0.15;
+
///
/// An offset distance from the gizmo Origin
@@ -78,7 +86,7 @@ enum Axes
public TranslationGizmo(NodeManipulator manipulator, Vector axis1, double size)
: base(manipulator)
{
- ReferenceCoordinateSystem = CoordinateSystem.Identity();
+ ReferenceCoordinateSystem = Matrix.Identity;
UpdateGeometry(axis1, null, null, size);
}
@@ -92,12 +100,12 @@ public TranslationGizmo(NodeManipulator manipulator, Vector axis1, double size)
public TranslationGizmo(NodeManipulator manipulator, Vector axis1, Vector axis2, double size)
: base(manipulator)
{
- ReferenceCoordinateSystem = CoordinateSystem.Identity();
+ ReferenceCoordinateSystem = Matrix.Identity;
UpdateGeometry(axis1, axis2, null, size);
}
///
- /// Construcs a 3D gizmo, can be manipulated in all three directions.
+ /// Constructs a 3D gizmo, can be manipulated in all three directions.
///
///
/// First axis of freedom
@@ -107,12 +115,12 @@ public TranslationGizmo(NodeManipulator manipulator, Vector axis1, Vector axis2,
public TranslationGizmo(NodeManipulator manipulator, Vector axis1, Vector axis2, Vector axis3, double size)
: base(manipulator)
{
- ReferenceCoordinateSystem = CoordinateSystem.Identity();
+ ReferenceCoordinateSystem = Matrix.Identity;
UpdateGeometry(axis1, axis2, axis3, size);
}
///
- /// Construcs a 3D gizmo, can be manipulated in all three directions.
+ /// Constructs a 3D gizmo, can be manipulated in all three directions.
///
/// First axis of freedom
/// Second axis of freedom
@@ -120,7 +128,7 @@ public TranslationGizmo(NodeManipulator manipulator, Vector axis1, Vector axis2,
/// Visual size of the Gizmo
internal void UpdateGeometry(Vector axis1, Vector axis2, Vector axis3, double size)
{
- if (axis1 == null) throw new ArgumentNullException("axis1");
+ if (axis1 == null) throw new ArgumentNullException(nameof(axis1));
//Reset the dataset, but don't reset the cached hitAxis or hitPlane.
//hitAxis and hitPlane are used to compute the offset for a move.
@@ -133,16 +141,16 @@ internal void UpdateGeometry(Vector axis1, Vector axis2, Vector axis3, double si
if (axis2 != null)
{
axes.Add(axis2);
- planes.Add(Plane.ByOriginXAxisYAxis(Origin, axis1, axis2));
+ planes.Add(new Plane(Origin.Position, axis1 * axis2, axis1));
}
if (axis3 != null)
{
axes.Add(axis3);
if (axis2 != null)
{
- planes.Add(Plane.ByOriginXAxisYAxis(Origin, axis2, axis3));
+ planes.Add(new Plane(Origin.Position, axis2 * axis3, axis2));
}
- planes.Add(Plane.ByOriginXAxisYAxis(Origin, axis3, axis1));
+ planes.Add(new Plane(Origin.Position, axis3 * axis1, axis3));
}
if (axes.Count == 1 && hitAxis != null)
@@ -156,7 +164,7 @@ internal void UpdateGeometry(Vector axis1, Vector axis2, Vector axis3, double si
#region private methods
///
- /// Returs default color for a given axis
+ /// Returns default color for a given axis
///
/// Axes
/// Color
@@ -173,8 +181,6 @@ private Color GetAxisColor(Axes axis)
return Color.FromRgb(0, 0, col);
case Axes.randomAxis:
break;
- default:
- break;
}
const byte colR = 0;
@@ -190,15 +196,86 @@ private Color GetAxisColor(Axes axis)
/// Axis
private Axes GetAlignedAxis(Vector axis)
{
- if (axis.IsParallel(ReferenceCoordinateSystem.XAxis))
+ var tol = 0.0001;
+ var xAxis = new Vector(ReferenceCoordinateSystem[0, 0], ReferenceCoordinateSystem[0, 1], ReferenceCoordinateSystem[0, 2]);
+ if ((axis * xAxis).IsNull(tol))
return Axes.xAxis;
- if (axis.IsParallel(ReferenceCoordinateSystem.YAxis))
+
+ var yAxis = new Vector(ReferenceCoordinateSystem[1, 0], ReferenceCoordinateSystem[1, 1], ReferenceCoordinateSystem[1, 2]);
+ if ((axis * yAxis).IsNull(tol))
return Axes.yAxis;
- if (axis.IsParallel(ReferenceCoordinateSystem.ZAxis))
+
+ var zAxis = new Vector(ReferenceCoordinateSystem[2, 0], ReferenceCoordinateSystem[2, 1], ReferenceCoordinateSystem[2, 2]);
+ if ((axis * zAxis).IsNull(tol))
return Axes.zAxis;
return Axes.randomAxis;
}
+
+ private static double DistanceTo(Line line, Point pt)
+ {
+ var p = pt.Position;
+ var sp = line.Position;
+ var dir = line.Direction;
+
+ // closest_dist = |Vector(p - sp) x dir| / |dir|
+ var num = ((p - sp) * dir).Magnitude;
+ var den = dir.Magnitude;
+ return num/den;
+ }
+
+ private static double DistanceTo(Line line, Line ray)
+ {
+ var a1 = line.Position;
+ var a2 = ray.Position;
+ var b1 = line.Direction;
+ var b2 = ray.Direction;
+
+ // closest_dist = |(a1 - a2) % (b1 * b2) / |b1 * b2||
+ return Math.Abs((a1 - a2) % (b1 * b2).Unit);
+ }
+
+ private static Point Intersect(Line line, Plane plane)
+ {
+ var d = line.Direction;
+ var sp = line.Position;
+ var n = plane.Normal;
+ var p = plane.Origin;
+
+ // intersection point (ip) = sp + t * d
+ // equation of plane: n % (ip - p) = 0
+ // substituting the intersection point into the plane equation:
+ if(n% d == 0) return null; // line is parallel to the plane
+
+ var t = (n % (p - sp)) / (n % d);
+ var ip = sp + d * t;
+
+ return new Point(ip);
+ }
+
+ private Point ClosestPointTo(Line line, Line ray)
+ {
+ var p1 = line.Position;
+ var p2 = ray.Position;
+ var d1 = line.Direction;
+ var d2 = ray.Direction;
+
+ var d1Crossd2 = d1 * d2;
+ var sqrMagnitude = Math.Pow(d1Crossd2.Magnitude, 2);
+
+ var planarFactor = (p2 - p1) % d1Crossd2;
+
+ // Check if the lines are coplanar and not parallel
+ if (Math.Abs(planarFactor) < tolerance && sqrMagnitude > tolerance)
+ {
+ var t1 = ((p2 - p1) * d2) % d1Crossd2 / sqrMagnitude;
+ //var t2 = ((p2 - p1) * d1) % d1Crossd2 / Math.Pow(d1Crossd2.Magnitude, 2);
+
+ return new Point(p1 + d1 * t1);
+ }
+ return Origin;
+ }
+
///
///
///
@@ -207,48 +284,45 @@ private Axes GetAlignedAxis(Vector axis)
///
private object HitTest(Point source, Vector direction)
{
- double tolerance = 0.15; //Hit tolerance
-
- using (var ray = GetRayGeometry(source, direction))
+
+ var ray = GetRayGeometry(source, direction);
+ //First hit test for position
+ if (DistanceTo(ray, Origin) < tolerance)
{
- //First hit test for position
- if (ray.DistanceTo(Origin) < tolerance)
+ if (planes.Any())
{
- if (planes.Any())
- {
- return planes.First(); //Xy or first available plane is hit
- }
- return axes.First(); //xAxis or first axis is hit
+ return planes.First(); //Xy or first available plane is hit
}
+ return axes.First(); //xAxis or first axis is hit
+ }
+
+ foreach (var plane in planes)
+ {
+ // plane needs to be up-to-date at this time with the current value of Origin
+ var pt = Intersect(ray, plane);
+ if (pt == null) continue;
- foreach (var plane in planes)
+ var vec = new Vector(pt.Position.X - Origin.Position.X, pt.Position.Y - Origin.Position.Y,
+ pt.Position.Z - Origin.Position.Z);
+ var dot1 = plane.UAxis % vec;
+
+ var planeYAxis = plane.Normal * plane.UAxis;
+ var dot2 = planeYAxis % vec;
+ if (dot1 > 0 && dot2 > 0 && dot1 < scale / 2 && dot2 < scale / 2)
{
- // plane needs to be up-to-date at this time with the current value of Origin
- using (var pt = plane.Intersect(ray).FirstOrDefault() as Point)
- {
- if (pt == null) continue;
-
- using (var vec = Vector.ByTwoPoints(Origin, pt))
- {
- var dot1 = plane.XAxis.Dot(vec);
- var dot2 = plane.YAxis.Dot(vec);
- if (dot1 > 0 && dot2 > 0 && dot1 < scale/2 && dot2 < scale/2)
- {
- return plane; //specific plane is hit
- }
- }
- }
+ return plane; //specific plane is hit
}
+ }
+
+ foreach (var axis in axes)
+ {
+ var vec = axis.Unit;
+ vec.Scale(scale);
+ var line = new Line(Origin.Position, vec);
- foreach (var axis in axes)
+ if (DistanceTo(line, ray) < tolerance)
{
- using (var line = Line.ByStartPointDirectionLength(Origin, axis, scale))
- {
- if (line.DistanceTo(ray) < tolerance)
- {
- return axis; //specific axis is hit.
- }
- }
+ return axis; //specific axis is hit.
}
}
return null;
@@ -262,8 +336,10 @@ private object HitTest(Point source, Vector direction)
///
private Line GetRayGeometry(Point source, Vector direction)
{
- double size = ManipulatorOrigin.DistanceTo(source) * 100;
- return Line.ByStartPointDirectionLength(source, direction, size);
+ double size = (ManipulatorOrigin.Position - source.Position).Magnitude * 100;
+ var vec = direction.Unit;
+ vec.Scale(size);
+ return new Line(source.Position, vec);
}
#endregion
@@ -273,7 +349,7 @@ private Line GetRayGeometry(Point source, Vector direction)
///
/// Reference coordinate system for the Gizmo
///
- public CoordinateSystem ReferenceCoordinateSystem { get; set; }
+ public Matrix ReferenceCoordinateSystem { get; set; }
///
/// Performs hit test on Gizmo to find out hit object. The returned
@@ -306,29 +382,23 @@ public override bool HitTest(Point source, Vector direction, out object hitObjec
public override Vector GetOffset(Point newPosition, Vector viewDirection)
{
Point hitPoint = Origin;
- using (var ray = GetRayGeometry(newPosition, viewDirection))
+ var ray = GetRayGeometry(newPosition, viewDirection);
+ if (hitPlane != null)
{
- if (hitPlane != null)
- {
- using (var testPlane = Plane.ByOriginXAxisYAxis(ManipulatorOrigin, hitPlane.XAxis, hitPlane.YAxis))
- {
- hitPoint = testPlane.Intersect(ray).FirstOrDefault() as Point;
- }
- }
- else if (hitAxis != null)
- {
- using (var axisLine = RayExtensions.ToOriginCenteredLine(ManipulatorOrigin, hitAxis))
- {
- hitPoint = axisLine.ClosestPointTo(ray);
- }
- }
+ var testPlane = new Plane(ManipulatorOrigin.Position, hitPlane.Normal, hitPlane.UAxis);
+ hitPoint = Intersect(ray, testPlane);
+ }
+ else if (hitAxis != null)
+ {
+ var axisLine = RayExtensions.ToOriginCenteredLine(ManipulatorOrigin, hitAxis);
+ hitPoint = ClosestPointTo(axisLine, ray);
}
if (hitPoint == null)
{
- return Vector.ByCoordinates(0, 0, 0);
+ return new Vector(0, 0, 0);
}
- return Vector.ByTwoPoints(ManipulatorOrigin, hitPoint);
+ return new Vector(hitPoint.Position.X - ManipulatorOrigin.Position.X, hitPoint.Position.Y - ManipulatorOrigin.Position.Y, hitPoint.Position.Z - ManipulatorOrigin.Position.Z);
}
///
@@ -361,7 +431,7 @@ public override void UpdateGizmoGraphics()
{
// Update gizmo geometry wrt to current Origin
var newPlanes = planes.Select(
- plane => Plane.ByOriginXAxisYAxis(Origin, plane.XAxis, plane.YAxis)).ToList();
+ plane => new Plane(Origin.Position, plane.Normal, plane.UAxis)).ToList();
planes.Clear();
@@ -371,7 +441,7 @@ public override void UpdateGizmoGraphics()
public override void DeleteTransientGraphics()
{
- var identifier = string.Format("{0}_{1}", RenderDescriptions.AxisLine, Name);
+ var identifier = $"{RenderDescriptions.AxisLine}_{Name}";
BackgroundPreviewViewModel.DeleteGeometryForIdentifier(identifier);
}
@@ -395,11 +465,11 @@ public override RenderPackageCache GetDrawablesForTransientGraphics()
if (null != hitPlane)
{
IRenderPackage package = RenderPackageFactory.CreateRenderPackage();
- DrawAxisLine(ref package, hitPlane.XAxis, "xAxisLine");
+ DrawAxisLine(ref package, hitPlane.UAxis, "xAxisLine");
drawables.Add(package);
package = RenderPackageFactory.CreateRenderPackage();
- DrawAxisLine(ref package, hitPlane.YAxis, "yAxisLine");
+ DrawAxisLine(ref package, hitPlane.Normal * hitPlane.UAxis, "yAxisLine");
drawables.Add(package);
}
@@ -414,16 +484,16 @@ public override RenderPackageCache GetDrawablesForTransientGraphics()
///
private void DrawAxisLine(ref IRenderPackage package, Vector axis, string name)
{
- package.Description = string.Format("{0}_{1}_{2}", RenderDescriptions.AxisLine, Name, name);
- using (var line = RayExtensions.ToOriginCenteredLine(Origin, axis))
- {
- var color = GetAxisColor(GetAlignedAxis(axis));
- package.AddLineStripVertexCount(2);
- package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
- package.AddLineStripVertex(line.StartPoint.X, line.StartPoint.Y, line.StartPoint.Z);
- package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
- package.AddLineStripVertex(line.EndPoint.X, line.EndPoint.Y, line.EndPoint.Z);
- }
+ package.Description = $"{RenderDescriptions.AxisLine}_{Name}_{name}";
+
+ var line = RayExtensions.ToOriginCenteredLine(Origin, axis);
+ var color = GetAxisColor(GetAlignedAxis(axis));
+ package.AddLineStripVertexCount(2);
+ package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
+ package.AddLineStripVertex(line.Position.X, line.Position.Y, line.Position.Z);
+ package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
+ var endPoint = line.Position + line.Direction;
+ package.AddLineStripVertex(endPoint.X, endPoint.Y, endPoint.Z);
}
///
@@ -434,30 +504,29 @@ private void DrawAxisLine(ref IRenderPackage package, Vector axis, string name)
///
private void DrawPlane(ref IRenderPackage package, Plane plane, Planes name)
{
- package.Description = string.Format("{0}_{1}_{2}", RenderDescriptions.ManipulatorPlane, Name, name);
- using (var vec1 = plane.XAxis.Scale(scale/3))
- using (var vec2 = plane.YAxis.Scale(scale/3))
- using (var vec3 = plane.YAxis.Scale(scale/3))
- {
- using (var p1 = Origin.Add(vec1))
- using (var p2 = p1.Add(vec2))
- using (var p3 = Origin.Add(vec3))
- {
- var axis = plane.Normal;
- var color = GetAxisColor(GetAlignedAxis(axis));
+ package.Description = $"{RenderDescriptions.ManipulatorPlane}_{Name}_{name}";
- package.AddLineStripVertexCount(3);
- package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
- package.AddLineStripVertex(p1.X, p1.Y, p1.Z);
+ var xAxis = plane.UAxis;
+ xAxis.Scale(scale / 3);
- package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
- package.AddLineStripVertex(p2.X, p2.Y, p2.Z);
+ var yAxis = plane.Normal * plane.UAxis;
+ yAxis.Scale(scale / 3);
- package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
- package.AddLineStripVertex(p3.X, p3.Y, p3.Z);
-
- }
- }
+ var p1 = Origin.Position + xAxis;
+ var p2 = p1 + yAxis;
+ var p3 = Origin.Position + yAxis;
+ var axis = plane.Normal;
+ var color = GetAxisColor(GetAlignedAxis(axis));
+
+ package.AddLineStripVertexCount(3);
+ package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
+ package.AddLineStripVertex(p1.X, p1.Y, p1.Z);
+
+ package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
+ package.AddLineStripVertex(p2.X, p2.Y, p2.Z);
+
+ package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
+ package.AddLineStripVertex(p3.X, p3.Y, p3.Z);
}
///
@@ -468,30 +537,23 @@ private void DrawPlane(ref IRenderPackage package, Plane plane, Planes name)
private void DrawAxis(ref IRenderPackage package, Vector axis)
{
var axisType = GetAlignedAxis(axis);
- package.Description = string.Format("{0}_{1}_{2}", RenderDescriptions.ManipulatorAxis, Name, axisType);
-
- using (var axisStart = Origin.Add(axis.Scale(axisOriginOffset)))
- using (var axisEnd = Origin.Add(axis.Scale(scale)))
- {
- var color = GetAxisColor(axisType);
- package.AddLineStripVertexCount(2);
- package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
- package.AddLineStripVertex(axisStart.X, axisStart.Y, axisStart.Z);
- package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
- package.AddLineStripVertex(axisEnd.X, axisEnd.Y, axisEnd.Z);
- }
+ package.Description = $"{RenderDescriptions.ManipulatorAxis}_{Name}_{axisType}";
+
+ var axis1 = new Vector(axis.X, axis.Y, axis.Z);
+ axis1.Scale(axisOriginOffset);
+ var axisStart = Origin.Position + axis1;
+
+ var axis2 = new Vector(axis.X, axis.Y, axis.Z);
+ axis2.Scale(scale);
+ var axisEnd = Origin.Position + axis2;
+ var color = GetAxisColor(axisType);
+ package.AddLineStripVertexCount(2);
+ package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
+ package.AddLineStripVertex(axisStart.X, axisStart.Y, axisStart.Z);
+ package.AddLineStripVertexColor(color.R, color.G, color.B, color.A);
+ package.AddLineStripVertex(axisEnd.X, axisEnd.Y, axisEnd.Z);
}
#endregion
-
- protected override void Dispose(bool disposing)
- {
- axes.ForEach(x => x.Dispose());
- planes.ForEach(x => x.Dispose());
-
- if(ReferenceCoordinateSystem != null) ReferenceCoordinateSystem.Dispose();
-
- base.Dispose(disposing);
- }
}
}