private void UpdateTransforms() { Matrix3D[] cornerMatrix = this._ballHandles.Select(x => (x.Transform.Value)).ToArray(); //Matrix3D[] cornerMatrix = cornerTrafos.Select(x => (x).Value).ToArray(); Positions = cornerMatrix.Select(x => x.ToMatrix().TranslationVector).ToArray(); for (int i = 0; i < 3; i++) { var translateMat = Matrix3DExtensions.Translate3D(Positions[i]); Vector3 v1 = Positions[(i + 1) % 3] - Positions[i]; Vector3 xBar = new Vector3(1, 0, 0); Vector3 rotateAxis = Vector3.Cross(xBar, v1); rotateAxis.Normalize(); if (rotateAxis.IsZero) { rotateAxis.X = 0; rotateAxis.Y = 0; rotateAxis.Z = 1; } float theta = Convert.ToSingle(Math.Acos(Vector3.Dot(xBar, v1) / (v1.Length() * xBar.Length()))); Matrix rotateMat = Matrix.RotationAxis(rotateAxis, theta); var m = Matrix.Scaling(v1.Length(), 1, 1) * rotateMat * Matrix.Translation(Positions[i]); ((MatrixTransform3D)_cylinderHandles[i].Transform).Matrix = (m.ToMatrix3D()); } }
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); } } } }
/// <summary> /// /// </summary> public InteractionHandle3D() { this.Material = PhongMaterials.Orange; //selectionColor.EmissiveColor = Color.Blue; //selectionColor.SpecularColor = Color.Black; //selectionColor.ReflectiveColor = Color.Black; for (int i = 0; i < 4; i++) { var translate = Matrix3DExtensions.Translate3D(positions[i].ToVector3D()); this.cornerHandles[i] = new DraggableGeometryModel3D() { DragZ = false, Visibility = Visibility.Visible, Material = this.Material, Geometry = NodeGeometry, Transform = new MatrixTransform3D(translate), }; this.cornerHandles[i].MouseMove3D += OnNodeMouse3DMove; this.cornerHandles[i].MouseUp3D += OnNodeMouse3DUp; this.cornerHandles[i].MouseDown3D += OnNodeMouse3DDown; this.edgeHandles[i] = new MeshGeometryModel3D() { Geometry = (i % 2 == 0) ? EdgeHGeometry : EdgeVGeometry, Material = this.Material, Visibility = Visibility.Visible, Transform = new MatrixTransform3D(translate), }; this.edgeHandles[i].MouseMove3D += OnEdgeMouse3DMove; this.edgeHandles[i].MouseUp3D += OnEdgeMouse3DUp; this.edgeHandles[i].MouseDown3D += OnEdgeMouse3DDown; translate = Matrix3DExtensions.Translate3D(0.5 * (positions[i] + positions[(i + 1) % 4]).ToVector3D()); this.midpointHandles[i] = new DraggableGeometryModel3D() { DragZ = false, DragX = (i % 2 == 1), DragY = (i % 2 == 0), Material = this.Material, Geometry = BoxGeometry, Transform = new MatrixTransform3D(translate), }; this.midpointHandles[i].MouseMove3D += OnNodeMouse3DMove; this.midpointHandles[i].MouseUp3D += OnNodeMouse3DUp; this.midpointHandles[i].MouseDown3D += OnNodeMouse3DDown; this.Children.Add(cornerHandles[i]); this.Children.Add(edgeHandles[i]); this.Children.Add(midpointHandles[i]); } // 3 --- 2 // | | // 0 --- 1 var m0 = Matrix.Scaling(+2, 1, 1) * Matrix.Translation(positions[0]); this.edgeHandles[0].Transform = new MatrixTransform3D(m0.ToMatrix3D()); var m2 = Matrix.Scaling(+2, 1, 1) * Matrix.Translation(positions[3]); this.edgeHandles[2].Transform = new MatrixTransform3D(m2.ToMatrix3D()); var m1 = Matrix.Scaling(1, +2, 1) * Matrix.Translation(positions[1]); this.edgeHandles[1].Transform = new MatrixTransform3D(m1.ToMatrix3D()); var m3 = Matrix.Scaling(1, +2, 1) * Matrix.Translation(positions[0]); this.edgeHandles[3].Transform = new MatrixTransform3D(m3.ToMatrix3D()); this.dragTransform = new MatrixTransform3D(this.Transform.Value); }
private void UpdateTransforms(object sender) { var cornerTrafos = this.cornerHandles.Select(x => (x.Transform as MatrixTransform3D)).ToArray(); var cornerMatrix = cornerTrafos.Select(x => (x).Value).ToArray(); this.positions = cornerMatrix.Select(x => x.ToMatrix().TranslationVector).ToArray(); BoundingBox bb; if (sender == cornerHandles[0] || sender == cornerHandles[2]) { Application.Current.MainWindow.Cursor = Cursors.SizeNESW; bb = BoundingBox.FromPoints(new[] { positions[0], positions[2] }); } else if (sender == cornerHandles[1] || sender == cornerHandles[3]) { Application.Current.MainWindow.Cursor = Cursors.SizeNWSE; bb = BoundingBox.FromPoints(new[] { positions[1], positions[3] }); } else { if (sender == midpointHandles[0] || sender == midpointHandles[2]) { Application.Current.MainWindow.Cursor = Cursors.SizeNS; } else { Application.Current.MainWindow.Cursor = Cursors.SizeWE; } positions = this.midpointHandles.Select(x => x.Transform.Value.ToMatrix().TranslationVector).ToArray(); bb = BoundingBox.FromPoints(positions); } // 3 --- 2 // | | // 0 --- 1 positions[0].X = bb.Minimum.X; positions[1].X = bb.Maximum.X; positions[2].X = bb.Maximum.X; positions[3].X = bb.Minimum.X; positions[0].Y = bb.Minimum.Y; positions[1].Y = bb.Minimum.Y; positions[2].Y = bb.Maximum.Y; positions[3].Y = bb.Maximum.Y; for (int i = 0; i < 4; i++) { if (sender != cornerHandles[i]) { cornerTrafos[i].Matrix = Matrix3DExtensions.Translate3D(positions[i].ToVector3D()); } var m = Matrix3DExtensions.Translate3D(0.5 * (positions[i] + positions[(i + 1) % 4]).ToVector3D()); ((MatrixTransform3D)this.midpointHandles[i].Transform).Matrix = m; } // 3 --- 2 // | | // 0 --- 1 var m0 = Matrix.Scaling(positions[1].X - positions[0].X, 1, 1) * Matrix.Translation(positions[0]); ((MatrixTransform3D)this.edgeHandles[0].Transform).Matrix = (m0.ToMatrix3D()); var m2 = Matrix.Scaling(positions[1].X - positions[0].X, 1, 1) * Matrix.Translation(positions[3]); ((MatrixTransform3D)this.edgeHandles[2].Transform).Matrix = (m2.ToMatrix3D()); var m1 = Matrix.Scaling(1, positions[2].Y - positions[1].Y, 1) * Matrix.Translation(positions[1]); ((MatrixTransform3D)this.edgeHandles[1].Transform).Matrix = (m1.ToMatrix3D()); var m3 = Matrix.Scaling(1, positions[2].Y - positions[1].Y, 1) * Matrix.Translation(positions[0]); ((MatrixTransform3D)this.edgeHandles[3].Transform).Matrix = (m3.ToMatrix3D()); }
public DraggableTriangle(Point3D center) { Center = center; Positions = new Vector3[3] { new Vector3(Convert.ToSingle(Center.X + Math.Cos(_initialAngle / 180f * Math.PI) * _length), Convert.ToSingle(Center.Y + Math.Sin(_initialAngle / 180f * Math.PI) * _length), Convert.ToSingle(Center.Z)), new Vector3(Convert.ToSingle(Center.X + Math.Cos((_initialAngle + 120) / 180f * Math.PI) * _length), Convert.ToSingle(Center.Y + Math.Sin((_initialAngle + 120) / 180f * Math.PI) * _length), Convert.ToSingle(Center.Z)), new Vector3(Convert.ToSingle(Center.X + Math.Cos((_initialAngle + 240) / 180f * Math.PI) * _length), Convert.ToSingle(Center.Y + Math.Sin((_initialAngle + 240) / 180f * Math.PI) * _length), Convert.ToSingle(Center.Z)), }; for (int i = 0; i < 3; i++) { //平移圓球 var translateMat = Matrix3DExtensions.Translate3D(Positions[i]); //三顆球顏色不同 材料設置在迴圈外面,並沒有在這邊先設置 _ballHandles[i] = new MeshGeometryModel3D() { Visibility = Visibility.Visible, Geometry = NodeGeometry, Transform = new MatrixTransform3D(translateMat), }; //定義球的滑鼠事件 this._ballHandles[i].MouseMove3D += OnNodeMouse3DMove; this._ballHandles[i].MouseUp3D += OnNodeMouse3DUp; this._ballHandles[i].MouseDown3D += OnNodeMouse3DDown; //兩點組成向量 Vector3 v1 = Positions[(i + 1) % 3] - Positions[i]; //圓柱初始擺放位置在X軸上,所以向量為X Vector3 xBar = new Vector3(1, 0, 0); Vector3 rotateAxis = Vector3.Cross(xBar, v1); rotateAxis.Normalize(); if (rotateAxis.IsZero) { rotateAxis.X = 0; rotateAxis.Y = 0; rotateAxis.Z = 1; } float theta = Convert.ToSingle(Math.Acos(Vector3.Dot(xBar, v1) / (v1.Length() * xBar.Length()))); Matrix rotateMat = Matrix.RotationAxis(rotateAxis, theta); Matrix m = Matrix.Scaling(v1.Length(), 1, 1) * rotateMat * Matrix.Translation(Positions[i]); _cylinderHandles[i] = new MeshGeometryModel3D() { Geometry = EdgeGeometry, Material = SetMaterial(Color4.White), Visibility = Visibility.Visible, Transform = new MatrixTransform3D(m.ToMatrix3D()) }; //定義桿子的滑鼠事件 _cylinderHandles[i].MouseMove3D += OnEdgeMouse3DMove; _cylinderHandles[i].MouseUp3D += OnEdgeMouse3DUp; _cylinderHandles[i].MouseDown3D += OnEdgeMouse3DDown; } //因為會畫透明物件,所以在上面迴圈外先將球一次加進去 foreach (MeshGeometryModel3D ball in _ballHandles) { Children.Add(ball); } //加完球再加桿子 foreach (MeshGeometryModel3D cylinder in _cylinderHandles) { Children.Add(cylinder); } //設定三顆球的顏色 _ballHandles[0].Material = SetMaterial(new Color4(1.0f, 0.0f, 0.0f, 1.0f)); _ballHandles[1].Material = SetMaterial(new Color4(0.0f, 1.0f, 0.0f, 1.0f)); _ballHandles[2].Material = SetMaterial(new Color4(0.0f, 0.0f, 1.0f, 1.0f)); }