protected void UpdatePosOnlySnapOn(object sender) { // update positions var endTransf = this.endHandles.Select(x => (x.Transform as MatrixTransform3D)).ToArray(); var endMatrices = endTransf.Select(x => x.Value).ToArray(); var pos_NEW = endMatrices.Select(x => x.ToMatrix().TranslationVector).ToArray(); int n = pos_NEW.Count(); int i; for (i = 0; i < n; i++) { Vector3 diff = pos_NEW[i] - this.pos_current[i]; Vector3D diffA = AdjustForSnap(diff.ToVector3D()); this.pos[i] = this.pos_current[i] + diffA.ToVector3(); } // update sender // ENDPOINT handles for (i = 0; i < n; i++) { if (sender == this.endHandles[i]) { var T = Matrix3DExtensions.Translate3D(pos[i].ToVector3D()); this.endHandles[i].Transform = new MatrixTransform3D(T); var child = (HelixToolkit.SharpDX.Wpf.GeometryModel3D) this.Children[this.endH_indInChildren[i]]; if (child != null) { child.Transform = new MatrixTransform3D(T); } } } }
public List <HitInfo> IntersectTriangle(Triangle triangle) { List <HitInfo> res = new List <HitInfo>(); for (int i = 0; i < vertices.Count; i++) { LineSegment2D l1 = new LineSegment2D(V2(vertices[i]), V2(vertices[(i + 1) % vertices.Count])); for (int j = 0; j < 3; j++) { LineSegment2D l2 = new LineSegment2D(V2(triangle.Vertex(j)), V2(triangle.Vertex((j + 1) % 3))); Vector2D point; if (l1.LineIntersect(l2, out point)) { double distance = (point - V2(vertices[i])).magnitude; double segmentLength = l1.Length(); //if (l1.DoLinesIntersect(l2)){ double normalizedDistance = distance / segmentLength; Vector3D point3D = Vector3D.Lerp(new Vector3D(vertices[i]), new Vector3D(vertices[(i + 1) % vertices.Count]), normalizedDistance); res.Add(new HitInfo(i, j, point3D.ToVector3())); } /*Vector2 intersectionPoint; * if (LineLineIntersection(from,to, tFrom, tTo,out intersectionPoint)){ * float normalizedDistance = Vector3.Dot(to-from, tTo-tFrom); * Vector3 point3D = Vector3.Lerp (vertices[i], vertices[(i+1)%vertices.Count], normalizedDistance); * res.Add(new HitInfo(i,j,point3D)); * }*/ } } return(res); }
/// <summary> /// Un-project a point from the screen (2D) to a point on plane (3D) /// </summary> /// <param name="viewport"> /// The viewport. /// </param> /// <param name="p"> /// The 2D point. /// </param> /// <param name="position"> /// plane position /// </param> /// <param name="normal"> /// plane normal /// </param> /// <returns> /// A 3D point. /// </returns> public static Point3D?UnProjectOnPlane(this Viewport3DX viewport, Point p, Point3D position, Vector3D normal) { if (viewport.UnProjectOnPlane(p.ToVector2(), position.ToVector3(), normal.ToVector3(), out var intersection)) { return(intersection.ToPoint3D()); } else { return(null); } }
public override Task <Tuple <bool, double> > CheckPanelIntersectionAsync(bool targetLinkState, Vector3D linkDirection) { if (targetLinkState) { var ptPanel = ColliderExtensions.GetPanel(); if (ptPanel.HasValue) { if (Points.Count > 0) { var zDir = GetPanelFaceDirectionForLinkImpact(linkDirection); var retPanel = ptPanel.Value; var panelMin = retPanel.Location.ToVector3(); var panelMax = panelMin + new Vector3((float)retPanel.SizeX, (float)retPanel.SizeY, (float)retPanel.SizeZ); var panel = new Box3(panelMin, panelMax); var tt = TotalTransformation.Value; var planePoint = panelMax; var tasks = new List <Task <Tuple <bool, double> > >(); foreach (var p in Points) { var point = p; tasks.Add(Task.Run(() => { var pp = tt.Transform(point); var ray = new Ray3D(pp.ToVector3(), linkDirection.ToVector3()); if (ray.PlaneIntersection(planePoint, zDir.ToVector3(), out Vector3 intersect) && panel.Intersects(new Sphere3(intersect, 0.001f))) { var pi = intersect.ToPoint3D(); var intersectValue = Vector3D.DotProduct(pi - pp, linkDirection); return(new Tuple <bool, double>(true, intersectValue)); } else { return(new Tuple <bool, double>(false, 0.0)); } })); } return(Task.WhenAll(tasks).ContinueWith((t) => t.Result.FirstOrDefault(r => r.Item1) ?? new Tuple <bool, double>(false, 0.0))); } else { throw new ArgumentOutOfRangeException("The number of points of collider must be greater than 0!"); } } } return(Task.FromResult(new Tuple <bool, double>(false, 0.0))); }
public void ToVector3() { double x = 23.4; double y = -11.0; double z = 0.0; double[] elementsD = new[] { x, y, z }; float[] elementsF = new[] { (float)x, (float)y, (float)z }; Vector3D vectorD = new Vector3D(elementsD); Vector3 vectorF = vectorD.ToVector3(); Assert.AreEqual(new Vector3(elementsF), vectorF); }
public SharpDX.Vector3 GetDirection(DateTime currentTime, double latitude, double longitude) { Vector3D position = WGS84.ToWorld(latitude, longitude); Vector3 up = Vector3.Normalize(position.ToVector3()); Vector3 left = Vector3.Normalize(Vector3.Cross(up, Vector3.UnitZ)); // up x world_up Vector3 north = Vector3.Normalize(Vector3.Cross(left, up)); double azimuth, altitude; GetAziumuthAltitude(currentTime, latitude, longitude, out azimuth, out altitude); Matrix azimuthRot = Matrix.RotationAxis(up, (float)azimuth); Matrix altitudeRot = Matrix.RotationAxis(left, (float)altitude); Matrix rot = azimuthRot * altitudeRot; var sunDir = Vector3.Transform(north, rot); return(new Vector3(sunDir.X, sunDir.Y, sunDir.Z)); }
public override Tuple <bool, double> CheckPanelIntersection(bool targetLinkState, Vector3D linkDirection) { bool result = false; double intersectValue = 0.0; var zDir = new Vector3D(0.0, 0.0, 1.0); if (targetLinkState) { var ptPanel = ColliderExtensions.GetPanel(); if (ptPanel.HasValue) { var retPanel = ptPanel.Value; var panelMin = retPanel.Location.ToVector3(); var panelMax = panelMin + new Vector3((float)retPanel.SizeX, (float)retPanel.SizeY, (float)retPanel.SizeZ); var panel = new Box3(panelMin, panelMax); var tt = TotalTransformation.Value; var planePoint = panelMax; foreach (var p in Points) { var pp = tt.Transform(p); var ray = new Ray3D(pp.ToVector3(), linkDirection.ToVector3()); if (ray.PlaneIntersection(planePoint, zDir.ToVector3(), out Vector3 intersect) && panel.Intersects(new Sphere3(intersect, 0.001f))) { var pi = intersect.ToPoint3D(); result = true; intersectValue = Vector3D.DotProduct(pi - pp, linkDirection); break; } } } } return(new Tuple <bool, double>(result, intersectValue)); }
/// <summary> /// Un-project a point from the screen (2D) to a point on plane (3D) /// </summary> /// <param name="viewport"> /// The viewport. /// </param> /// <param name="p"> /// The 2D point. /// </param> /// <param name="position"> /// plane position /// </param> /// <param name="normal"> /// plane normal /// </param> /// <returns> /// A 3D point. /// </returns> public static Point3D?UnProjectOnPlane(this Viewport3DX viewport, Point p, Point3D position, Vector3D normal) { return(UnProjectOnPlane(viewport, p.ToVector2(), position.ToVector3(), normal.ToVector3())?.ToPoint3D()); }