/// <summary> /// Rotate camera /// </summary> /// <param name="mouseEvent">Mouse event</param> private void RotateCamera(MouseEventArgs mouseEvent) { if (this.IsMouseOver && mouseEvent.LeftButton == MouseButtonState.Pressed) { Point lastPosition = this.glControl.PointFromScreen(this.lastMousePosition); Point currentPosition = mouseEvent.GetPosition(this.glControl); Point delta = (Point)((lastPosition - currentPosition) * 0.5); Vector3D direction = this.camera.GetLookDirection(); Vector3D up = this.camera.Up; up.Normalize(); Vector3D side = Vector3D.CrossProduct(direction, up); side.Normalize(); up = Vector3D.CrossProduct(direction, side); up.Normalize(); Point3D vectorBase = this.camera.Position + (direction * 15.0); Point3D rotationPoint = this.camera.Position; RotateTransform3D transform3D = new RotateTransform3D(new QuaternionRotation3D(new Quaternion(side, delta.Y)), rotationPoint); this.camera.Position = transform3D.Transform(this.camera.Position); vectorBase = transform3D.Transform(vectorBase); transform3D = new RotateTransform3D(new AxisAngleRotation3D(up, -delta.X), rotationPoint); this.camera.Position = transform3D.Transform(this.camera.Position); vectorBase = transform3D.Transform(vectorBase); Vector3D newLook = vectorBase - this.camera.Position; newLook.Normalize(); this.camera.SetLookDirection(newLook); } }
public void SliderOverChange(double slidervalue) { //Slider_Zoom CamPosition = new Point3D(SliderValue_Zoom, 0, 0); //Slider_Over SliderValue_Over = slidervalue; SliderCamRotationAngle_Over = 9 * SliderValue_Over; Vector3D CamPositionVector = new Vector3D(CamPosition.X, CamPosition.Y, CamPosition.Z); Vector3D RotationaxisofSlider_Over = MathForSimulation.CrossP(CamPositionVector, y_achse); SliderAxisRotation_Over = new AxisAngleRotation3D(RotationaxisofSlider_Over, SliderCamRotationAngle_Over); SliderAxisTransform_Over = new RotateTransform3D(SliderAxisRotation_Over); CamPosition_rotated_Over = SliderAxisTransform_Over.Transform(CamPosition); CamLookDirection_rotated_Over = SliderAxisTransform_Over.Transform(CamLookDirection); LightDirection1_rotated_Over = SliderAxisTransform_Over.Transform(LightDirection1); //Slider_XZ Anwendung SliderAxisRotation = new AxisAngleRotation3D(y_achse, SliderCamRotationAngle); SliderAxisTransform = new RotateTransform3D(SliderAxisRotation); CamPosition_rotated = SliderAxisTransform.Transform(CamPosition_rotated_Over); Cam.Position = CamPosition_rotated; CamLookDirection_rotated = SliderAxisTransform.Transform(CamLookDirection_rotated_Over); Cam.LookDirection = CamLookDirection_rotated; LightDirection1_rotated = SliderAxisTransform.Transform(LightDirection1_rotated_Over); LightSource1.Direction = LightDirection1_rotated; }
/// <summary> /// The rotate trackball. /// </summary> /// <param name="p1"> /// The previous mouse position. /// </param> /// <param name="p2"> /// The current mouse position. /// </param> /// <param name="rotateAround"> /// The point to rotate around. /// </param> private void RotateTrackball(Point p1, Point p2, Point3D rotateAround) { // http://viewport3d.com/trackball.htm // http://www.codeplex.com/3DTools/Thread/View.aspx?ThreadId=22310 var v1 = ProjectToTrackball(p1, this.Viewport.ActualWidth, this.Viewport.ActualHeight); var v2 = ProjectToTrackball(p2, this.Viewport.ActualWidth, this.Viewport.ActualHeight); // transform the trackball coordinates to view space var viewZ = this.Camera.LookDirection; var viewX = Vector3D.CrossProduct(this.Camera.UpDirection, viewZ); var viewY = Vector3D.CrossProduct(viewX, viewZ); viewX.Normalize(); viewY.Normalize(); viewZ.Normalize(); var u1 = (viewZ * v1.Z) + (viewX * v1.X) + (viewY * v1.Y); var u2 = (viewZ * v2.Z) + (viewX * v2.X) + (viewY * v2.Y); // Could also use the Camera ViewMatrix // var vm = Viewport3DHelper.GetViewMatrix(this.ActualCamera); // vm.Invert(); // var ct = new MatrixTransform3D(vm); // var u1 = ct.Transform(v1); // var u2 = ct.Transform(v2); // Find the rotation axis and angle var axis = Vector3D.CrossProduct(u1, u2); if (axis.LengthSquared < 1e-8) { return; } double angle = Vector3D.AngleBetween(u1, u2); // Create the transform var delta = new Quaternion(axis, -angle * this.RotationSensitivity * 5); var rotate = new RotateTransform3D(new QuaternionRotation3D(delta)); // Find vectors relative to the rotate-around point var relativeTarget = rotateAround - this.Camera.Target; var relativePosition = rotateAround - this.Camera.Position; // Rotate the relative vectors var newRelativeTarget = rotate.Transform(relativeTarget); var newRelativePosition = rotate.Transform(relativePosition); var newUpDirection = rotate.Transform(this.Camera.UpDirection); // Find new camera position var newTarget = rotateAround - newRelativeTarget; var newPosition = rotateAround - newRelativePosition; this.Camera.LookDirection = newTarget - newPosition; if (this.CameraMode == CameraMode.Inspect) { this.Camera.Position = newPosition; } this.Camera.UpDirection = newUpDirection; }
/// <summary> /// Rotates around the camera up and right axes. /// </summary> /// <param name="p1"> /// The previous mouse position. /// </param> /// <param name="p2"> /// The current mouse position. /// </param> /// <param name="rotateAround"> /// The point to rotate around. /// </param> private void RotateAroundUpAndRight(Point p1, Point p2, Point3D rotateAround) { var dp = p2 - p1; // Rotate around the camera up direction var delta1 = new Quaternion(this.CameraUpDirection, -dp.X * this.RotationSensitivity); // Rotate around the camera right direction var delta2 = new Quaternion( Vector3D.CrossProduct(this.CameraUpDirection, this.CameraLookDirection), dp.Y * this.RotationSensitivity); var delta = delta1 * delta2; var rotate = new RotateTransform3D(new QuaternionRotation3D(delta)); var relativeTarget = rotateAround - this.CameraTarget; var relativePosition = rotateAround - this.CameraPosition; var newRelativeTarget = rotate.Transform(relativeTarget); var newRelativePosition = rotate.Transform(relativePosition); var newUpDirection = rotate.Transform(this.CameraUpDirection); var newTarget = rotateAround - newRelativeTarget; var newPosition = rotateAround - newRelativePosition; this.CameraLookDirection = newTarget - newPosition; if (CameraMode == CameraMode.Inspect) { this.CameraPosition = newPosition; } this.CameraUpDirection = newUpDirection; }
private void UpdateModelFromMouse(Vector mousePositionDelta) { if (this.cameraElement == null || !(this.cameraElement is ProjectionCameraElement)) { return; } if (this.mouseMovementMode == CameraOrbitToolBehavior.MovementMode.Rotate) { this.totalAzimuthDelta += -mousePositionDelta.X / 2.0; this.totalElevationDelta += -mousePositionDelta.Y / 2.0; double angle1 = this.totalAzimuthDelta; if (this.IsShiftDown) { angle1 = CameraOrbitToolBehavior.shiftSnapAngle * Math.Round(angle1 / CameraOrbitToolBehavior.shiftSnapAngle); } RotateTransform3D rotateTransform3D1 = new RotateTransform3D(Vector3D.DotProduct((Vector3D)this.cameraElement.GetComputedValue(ProjectionCameraElement.UpDirectionProperty), this.cameraInitialUp) >= 0.0 ? (Rotation3D) new AxisAngleRotation3D(this.cameraInitialUp, angle1) : (Rotation3D) new AxisAngleRotation3D(-this.cameraInitialUp, angle1), this.cameraInitialLookAt); Point3D point = rotateTransform3D1.Transform(this.cameraInitialPosition); Vector3D vector3D1 = rotateTransform3D1.Transform(this.cameraInitialUp); Vector3D axis = Vector3D.CrossProduct(this.cameraInitialLookAt - point, vector3D1); double angle2 = this.totalElevationDelta; if (this.IsShiftDown) { angle2 = CameraOrbitToolBehavior.shiftSnapAngle * Math.Round(angle2 / CameraOrbitToolBehavior.shiftSnapAngle); } if (axis.LengthSquared == 0.0) { return; } RotateTransform3D rotateTransform3D2 = new RotateTransform3D((Rotation3D) new AxisAngleRotation3D(axis, angle2), this.cameraInitialLookAt); Point3D point3D1 = rotateTransform3D2.Transform(point); Vector3D vector3D2 = rotateTransform3D2.Transform(vector3D1); Point3D point3D2 = RoundingHelper.RoundPosition(point3D1); Vector3D vector3D3 = RoundingHelper.RoundDirection(vector3D2); Vector3D vector3D4 = RoundingHelper.RoundDirection(this.cameraInitialLookAt - point3D2); this.cameraElement.SetValue(ProjectionCameraElement.PositionProperty, (object)point3D2); this.cameraElement.SetValue(ProjectionCameraElement.UpDirectionProperty, (object)vector3D3); this.cameraElement.SetValue(ProjectionCameraElement.LookDirectionProperty, (object)vector3D4); } else { Matrix3D matrix3D = Helper3D.CameraRotationMatrix((Camera)this.cameraElement.ViewObject.PlatformSpecificObject); if (this.mouseMovementMode == CameraOrbitToolBehavior.MovementMode.TranslateXY) { Vector3D vector3D1 = new Vector3D(matrix3D.M11, matrix3D.M21, matrix3D.M31); Vector3D vector3D2 = new Vector3D(matrix3D.M12, matrix3D.M22, matrix3D.M32); this.cameraInitialPosition += this.scale * (-mousePositionDelta.X * vector3D1 + mousePositionDelta.Y * vector3D2); } else { Vector3D vector3D1 = new Vector3D(matrix3D.M13, matrix3D.M23, matrix3D.M33); this.cameraInitialPosition += this.scale * mousePositionDelta.Y * vector3D1; Vector3D vector3D2 = RoundingHelper.RoundDirection(this.cameraInitialLookAt - this.cameraInitialPosition); this.cameraElement.SetValue(ProjectionCameraElement.LookDirectionProperty, (object)vector3D2); } this.cameraInitialPosition = RoundingHelper.RoundPosition(this.cameraInitialPosition); this.cameraElement.SetValue(ProjectionCameraElement.PositionProperty, (object)this.cameraInitialPosition); } }
private void Rotate(Rotation rot) { var trans = new RotateTransform3D(rot); Up = trans.Transform(Up); Up.Normalize(); Forward = trans.Transform(Forward); Forward.Normalize(); }
public DraggableModifierRing(Quaternion orientation, double radius, EditorColors editorColors) : base(editorColors) { _initialOrientation = orientation; _radius = radius; _initialRotateTransform = new RotateTransform3D(new QuaternionRotation3D(_initialOrientation)); _intialAxisX = _initialRotateTransform.Transform(new Vector3D(1, 0, 0)); _intialAxisY = _initialRotateTransform.Transform(new Vector3D(0, 1, 0)); _intialAxisZ = _initialRotateTransform.Transform(new Vector3D(0, 0, 1)); this.Model = new ModelVisual3D(); this.Model.Content = GetRing(); }
private void Look(Vector3D vec, double amount) { vec = GetWorldVector(ref vec); var rotation = new AxisAngleRotation3D(vec, amount * LOOK_SPEED); var transform = new RotateTransform3D { Rotation = rotation }; lookDirection = transform.Transform(lookDirection); upDirection = transform.Transform(upDirection); RaisePropertyChanged(() => LookDirection); RaisePropertyChanged(() => UpDirection); RaisePropertyChanged(() => RightDirection); }
/// <summary> /// Animates the position of the camera around the origin. /// (When orbit kind is Vertical, the view may change in the viewport /// because of the Camera.UpDirection value) /// </summary> /// <param name="angleOffset">Rotation angle</param> /// <param name="ok">Orbit kind (horizontal or vertical)</param> private void CameraOrbitOrigin(double angleOffset, OrbitKind ok) { PerspectiveCamera camera = (PerspectiveCamera)_viewport.Camera; _cameraPositionRotation.Angle = angleOffset; if (ok != _lastOk) { if (ok == OrbitKind.Horizontal) { _cameraPositionRotation.Axis = _yAxis; } else { _cameraPositionRotation.Axis = Vector3D.CrossProduct(camera.LookDirection, _yAxis); } _cameraLookDirectionRotation.Axis = _cameraPositionRotation.Axis; _lastOk = ok; } Point3D newPosition = _cameraPositionRotateTransform.Transform(camera.Position); camera.Position = newPosition; _cameraLookDirectionRotateTransform.CenterX = newPosition.X; _cameraLookDirectionRotateTransform.CenterY = newPosition.Y; _cameraLookDirectionRotateTransform.CenterZ = newPosition.Z; _cameraLookDirectionRotation.Angle = angleOffset; camera.LookDirection = _cameraLookDirectionRotateTransform.Transform(camera.LookDirection); }
private Point3DCollection CreateSnakePositions(Point3D startPosition, double segmentsLength, double startAngle, double maxAngle) { var point3DCollection = new Point3DCollection(); point3DCollection.Add(startPosition); double angle = startAngle; bool negateAngle = false; var currentPosition = startPosition; var initialDirectionVector = new Vector3D(segmentsLength, 0, 0); var axisAngleRotation3D = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0); var rotateTransform3D = new RotateTransform3D(axisAngleRotation3D); while (angle <= maxAngle) { axisAngleRotation3D.Angle = negateAngle ? -angle : angle; var currentDirectionVector = rotateTransform3D.Transform(initialDirectionVector); currentPosition += currentDirectionVector; point3DCollection.Add(currentPosition); angle += 5; negateAngle = !negateAngle; } return(point3DCollection); }
public static GeometryModel3D Make3DGeometry_Over3Points(Point3D p1_, Point3D p2_, double thickness) { Point3D[] PointsToMesch = new Point3D[8]; MeshGeometry3D m = new MeshGeometry3D(); Vector3D v12 = new Vector3D(p2_.X - p1_.X, p2_.Y - p1_.Y, p2_.Z - p1_.Z);//vektro senkrecht auf die ebene in der der punkt Q liegen wird. es ist die Achse der Rotation Point3D Q1 = GetQ(v12, p1_, thickness); AxisAngleRotation3D rotation_90_1 = new AxisAngleRotation3D(v12, -90); RotateTransform3D rot3D_90_1 = new RotateTransform3D(rotation_90_1, p1_); Point3D resultatQ1_90 = new Point3D(); resultatQ1_90 = rot3D_90_1.Transform(Q1); AxisAngleRotation3D rotation_180_1 = new AxisAngleRotation3D(v12, -180); RotateTransform3D rot3D_180_1 = new RotateTransform3D(rotation_180_1, p1_); Point3D resultatQ1_180 = new Point3D(); resultatQ1_180 = rot3D_180_1.Transform(Q1); AxisAngleRotation3D rotation_270_1 = new AxisAngleRotation3D(v12, -270); RotateTransform3D rot3D_270_1 = new RotateTransform3D(rotation_270_1, p1_); Point3D resultatQ1_270 = new Point3D(); resultatQ1_270 = rot3D_270_1.Transform(Q1); Point3D Q2 = GetQ(v12, p2_, thickness); AxisAngleRotation3D rotation_90_2 = new AxisAngleRotation3D(v12, -90); RotateTransform3D rot3D_90_2 = new RotateTransform3D(rotation_90_2, p2_); Point3D resultatQ2_90 = new Point3D(); resultatQ2_90 = rot3D_90_2.Transform(Q2); AxisAngleRotation3D rotation_180_2 = new AxisAngleRotation3D(v12, -180); RotateTransform3D rot3D_180_2 = new RotateTransform3D(rotation_180_2, p2_); Point3D resultatQ2_180 = new Point3D(); resultatQ2_180 = rot3D_180_2.Transform(Q2); AxisAngleRotation3D rotation_270_2 = new AxisAngleRotation3D(v12, -270); RotateTransform3D rot3D_270_2 = new RotateTransform3D(rotation_270_2, p2_); Point3D resultatQ2_270 = new Point3D(); resultatQ2_270 = rot3D_270_2.Transform(Q2); PointsToMesch[0] = Q1; PointsToMesch[1] = resultatQ1_90; PointsToMesch[2] = resultatQ1_270; PointsToMesch[3] = resultatQ1_180; PointsToMesch[4] = Q2; PointsToMesch[5] = resultatQ2_90; PointsToMesch[6] = resultatQ2_270; PointsToMesch[7] = resultatQ2_180; DiffuseMaterial DiffuseMaterial = new DiffuseMaterial(System.Windows.Media.Brushes.White); MaterialGroup MaterialGroup = new MaterialGroup(); MaterialGroup.Children.Add(DiffuseMaterial); System.Windows.Media.Color c = new System.Windows.Media.Color(); c.ScA = 255; c.ScB = 255; c.ScR = 255; c.ScG = 255; EmissiveMaterial EmissiveMaterial = new EmissiveMaterial(new SolidColorBrush(c)); MaterialGroup.Children.Add(EmissiveMaterial); return(DrawCubus(PointsToMesch, MaterialGroup)); }
public static Vector3D RotateVector(Vector3D vector, Vector3D rotateAxis, double degrees) { var transform = new RotateTransform3D(new AxisAngleRotation3D(rotateAxis, degrees)); var result = transform.Transform(vector); return(result); }
private void RotateUpDirection(Vector3D axis) { var rotation = new AxisAngleRotation3D(axis, rotationSpeed); var transform = new RotateTransform3D(rotation); Camera.UpDirection = transform.Transform(Camera.UpDirection); }
private void ShowNearbySketches(SketchSample[] nearby, Point3D center3D, RayHitTestParameters cameraRay, Point center2D) { // Get a plane that is perpendicular to the look ray ITriangle plane = Math3D.GetPlane(center3D, cameraRay.Direction); // Project the points onto that plane var nearbyOnPlane = nearby. Select(o => new { Sketch = o, PlanePoint = Math3D.GetClosestPoint_Plane_Point(plane, o.Position) }). ToArray(); RotateTransform3D rotate = new RotateTransform3D(new QuaternionRotation3D(Math3D.GetRotation(new DoubleVector(cameraRay.Direction, _camera.UpDirection), new DoubleVector(new Vector3D(0, 0, -1), new Vector3D(0, 1, 0))))); // Lay these images down along the directions of the projected points // nearby is sorted by distance from the center image #region Attempt1 double halfLarge = SKETCHSIZE_LARGE / 2; double halfSmall = SKETCHSIZE_SMALL / 2; double stepDist = SKETCHSIZE_SMALL * .05; int startIncrement = Convert.ToInt32(Math.Floor((halfSmall + halfLarge) / stepDist)); List <Rect> existing = new List <Rect>(); existing.Add(new Rect(center2D.X - halfLarge, center2D.Y - halfLarge, SKETCHSIZE_LARGE, SKETCHSIZE_LARGE)); foreach (var nextSketch in nearbyOnPlane) { Vector direction = rotate.Transform(nextSketch.PlanePoint - center3D).ToVector2D(); Vector dirUnit = direction.ToUnit(); if (Math2D.IsInvalid(dirUnit)) { dirUnit = Math3D.GetRandomVector_Circular_Shell(1).ToVector2D(); } dirUnit = new Vector(dirUnit.X, -dirUnit.Y); Point point = new Point(); Rect rect = new Rect(); for (int cntr = startIncrement; cntr < 1000; cntr++) { point = center2D + (dirUnit * (stepDist * cntr)); rect = new Rect(point.X - halfSmall, point.Y - halfSmall, SKETCHSIZE_SMALL, SKETCHSIZE_SMALL); if (!existing.Any(o => o.IntersectsWith(rect))) { break; } } existing.Add(rect); //ShowImage(nextSketch.Sketch, center2D + (dirUnit * 100), false); ShowImage(nextSketch.Sketch, point, false); } #endregion }
private void ShowNearbySketches(Dot[] nearby, Point3D center3D, RayHitTestParameters cameraRay, Point center2D) { // Get a plane that is perpendicular to the look ray ITriangle plane = Math3D.GetPlane(center3D, cameraRay.Direction); // Project the points onto that plane var nearbyOnPlane = nearby. Select(o => new { Sketch = o, PlanePoint = Math3D.GetClosestPoint_Plane_Point(plane, o.Position) }). ToArray(); RotateTransform3D rotate = new RotateTransform3D(new QuaternionRotation3D(Math3D.GetRotation(new DoubleVector(cameraRay.Direction, _camera.UpDirection), new DoubleVector(new Vector3D(0, 0, -1), new Vector3D(0, 1, 0))))); // Lay these images down along the directions of the projected points // nearby is sorted by distance from the center image double halfLarge = SKETCHSIZE_LARGE / 2; double halfSmall = SKETCHSIZE_SMALL / 2; double stepDist = SKETCHSIZE_SMALL * .05; // Don't start counter at a distance of zero, that's just wasted steps. Figure out what cntr to use that is the distance of the two images touching int startIncrement = Convert.ToInt32(Math.Floor((halfSmall + halfLarge) / stepDist)); // Remember the locations of each image rect List <Rect> existing = new List <Rect>(); existing.Add(new Rect(center2D.X - halfLarge, center2D.Y - halfLarge, SKETCHSIZE_LARGE, SKETCHSIZE_LARGE)); foreach (var nextSketch in nearbyOnPlane) { // Get the 2D direction this sketch is from the main Vector direction = rotate.Transform(nextSketch.PlanePoint - center3D).ToVector2D(); Vector dirUnit = direction.ToUnit(true); if (Math2D.IsInvalid(dirUnit)) { dirUnit = Math3D.GetRandomVector_Circular_Shell(1).ToVector2D(); // sitting on top of each other, just push it in a random direction } dirUnit = new Vector(dirUnit.X, -dirUnit.Y); Point point = new Point(); Rect rect = new Rect(); // Keep walking along that direction until the rectangle doesn't intersect any existing sketches for (int cntr = startIncrement; cntr < 1000; cntr++) { point = center2D + (dirUnit * (stepDist * cntr)); rect = new Rect(point.X - halfSmall, point.Y - halfSmall, SKETCHSIZE_SMALL, SKETCHSIZE_SMALL); if (!existing.Any(o => o.IntersectsWith(rect))) { break; } } existing.Add(rect); ShowImage(nextSketch.Sketch, point, false); } }
/// <summary> /// Do the tessellation and return the <see cref="MeshGeometry3D" />. /// </summary> /// <returns> /// A triangular mesh geometry. /// </returns> protected override MeshGeometry3D Tessellate() { this.lengthDirection = this.LengthDirection; this.lengthDirection.Normalize(); // #136, chrkon, 2015-03-26 // if NormalVector and LenghtDirection are not perpendicular then overwrite LengthDirection if (Vector3D.DotProduct(this.Normal, this.LengthDirection) != 0.0) { this.lengthDirection = this.Normal.FindAnyPerpendicular(); this.lengthDirection.Normalize(); } // create WidthDirection by rotating lengthDirection vector 90° around normal vector var rotate = new RotateTransform3D(new AxisAngleRotation3D(this.Normal, 90.0)); this.widthDirection = rotate.Transform(this.lengthDirection); this.widthDirection.Normalize(); // #136 var mesh = new MeshBuilder(true, false); double minX = -this.Width / 2; double minY = -this.Length / 2; double maxX = this.Width / 2; double maxY = this.Length / 2; double x = minX; double eps = this.MinorDistance / 10; while (x <= maxX + eps) { double t = this.Thickness; if (IsMultipleOf(x, this.MajorDistance)) { t *= 2; } this.AddLineX(mesh, x, minY, maxY, t); x += this.MinorDistance; } double y = minY; while (y <= maxY + eps) { double t = this.Thickness; if (IsMultipleOf(y, this.MajorDistance)) { t *= 2; } this.AddLineY(mesh, y, minX, maxX, t); y += this.MinorDistance; } var m = mesh.ToMesh(); m.Freeze(); return(m); }
private void UpdateLightDirection() { RotateTransform3D rotate = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), DirectionSlider.Value)); Vector3D direction = rotate.Transform(new Vector3D(0, -0.5, -1)); DirectionalLight1.Direction = direction; }
/// <summary> /// Reads a triangulated mesh from a byte array /// </summary> /// <param name="mesh">the binary data of the mesh</param> /// <param name="transform">transforms the mesh if the matrix is not null</param> public void Read(byte[] mesh, XbimMatrix3D?transform = null) { int indexBase = _unfrozenPositions.Count; var qrd = new RotateTransform3D(); Matrix3D?matrix3D = null; if (transform.HasValue) { var xq = transform.Value.GetRotationQuaternion(); var quaternion = new Quaternion(xq.X, xq.Y, xq.Z, xq.W); if (!quaternion.IsIdentity) { qrd.Rotation = new QuaternionRotation3D(quaternion); } else { qrd = null; } matrix3D = transform.Value.ToMatrix3D(); } using (var ms = new MemoryStream(mesh)) { using (var br = new BinaryReader(ms)) { var t = br.ReadShapeTriangulation(); List <float[]> pts; List <int> idx; t.ToPointsWithNormalsAndIndices(out pts, out idx); // add to unfrozen list // _unfrozenPositions.Capacity += pts.Count; _unfrozenNormals.Capacity += pts.Count; _unfrozenIndices.Capacity += idx.Count; foreach (var floatsArray in pts) { var wpfPosition = new Point3D(floatsArray[0], floatsArray[1], floatsArray[2]); if (matrix3D.HasValue) { wpfPosition = matrix3D.Value.Transform(wpfPosition); } _unfrozenPositions.Add(wpfPosition); var wpfNormal = new Vector3D(floatsArray[3], floatsArray[4], floatsArray[5]); if (qrd != null) //transform the normal if we have to { wpfNormal = qrd.Transform(wpfNormal); } _unfrozenNormals.Add(wpfNormal); } foreach (var index in idx) { _unfrozenIndices.Add(index + indexBase); } } } }
/// <summary> /// Rotation of a vector around an axis according to a given angle. /// </summary> /// <param name="source">Source vector.</param> /// <param name="angle">Rotation angle (in degree).</param> /// <param name="axis">Axis vector.</param> /// <returns>A new Vector3D object corresponding to the rotation</returns> public static Vector3D RotateVector(Vector3D source, double angle, Vector3D axis) { AxisAngleRotation3D rotation = new AxisAngleRotation3D(); rotation.Angle = angle; rotation.Axis = axis; RotateTransform3D transform = new RotateTransform3D(rotation); return(transform.Transform(source)); }
private void UpdateGeometries(Quaternion orientation) { Matrix3D matrix3D = new RotateTransform3D((Rotation3D) new AxisAngleRotation3D(orientation.Axis, orientation.Angle), new Point3D()).Value; Vector3D vector3D1 = matrix3D.Transform(new Vector3D(1.0, 0.0, 0.0)); Vector3D vector3D2 = matrix3D.Transform(new Vector3D(0.0, 1.0, 0.0)); Vector3D vector3D3 = matrix3D.Transform(new Vector3D(0.0, 0.0, 1.0)); PathGeometry frontGeometry; PathGeometry backGeometry; this.GetArcGeometry(vector3D1, vector3D2, vector3D3, out frontGeometry, out backGeometry); this.XYFrontGeometry = (Geometry)frontGeometry; this.XYBackGeometry = (Geometry)backGeometry; this.GetArcGeometry(vector3D1, vector3D3, -vector3D2, out frontGeometry, out backGeometry); this.XZFrontGeometry = (Geometry)frontGeometry; this.XZBackGeometry = (Geometry)backGeometry; this.GetArcGeometry(vector3D2, vector3D3, vector3D1, out frontGeometry, out backGeometry); this.YZFrontGeometry = (Geometry)frontGeometry; this.YZBackGeometry = (Geometry)backGeometry; }
private Quaternion GetAngle(Point3D pointOnCircle) { RotateTransform3D transform = new RotateTransform3D(new QuaternionRotation3D(_circleOrientation.Value)); Vector3D axisX = transform.Transform(_intialAxisX); Vector3D requestAxis = pointOnCircle - _circleCenter.Value; double angle = Vector3D.AngleBetween(axisX, requestAxis); Vector3D axis = Vector3D.CrossProduct(axisX, requestAxis); return(new Quaternion(axis, angle)); }
public void ReadTransform() { if (!this.IsEnabled) { return; } this.Position = this.ViewModel.Position; this.Rotation = this.ViewModel.Rotation; this.Scale = this.ViewModel.Scale; // Convert the character-relative transform into a parent-relative transform Point3D position = this.Position.ToMedia3DPoint(); Quaternion rotation = this.Rotation.ToMedia3DQuaternion(); if (this.Parent != null) { TransformViewModel parentTransform = this.Parent.ViewModel; Point3D parentPosition = parentTransform.Position.ToMedia3DPoint(); Quaternion parentRot = parentTransform.Rotation.ToMedia3DQuaternion(); parentRot.Invert(); // relative position position = (Point3D)(position - parentPosition); // relative rotation rotation = parentRot * rotation; // unrotate bones, since we will transform them ourselves. RotateTransform3D rotTrans = new RotateTransform3D(new QuaternionRotation3D(parentRot)); position = rotTrans.Transform(position); } // Store the new parent-relative transform info this.Position = position.ToCmVector(); this.Rotation = rotation.ToCmQuaternion(); // Set the Media3D hierarchy transforms this.rotation.Rotation = new QuaternionRotation3D(rotation); this.position.OffsetX = position.X; this.position.OffsetY = position.Y; this.position.OffsetZ = position.Z; // Draw a line for visualization if (this.Parent != null && this.lineToParent != null) { Point3D p = this.lineToParent.Points[1]; p.X = position.X; p.Y = position.Y; p.Z = position.Z; this.lineToParent.Points[1] = p; } }
/// <summary> /// Rotate the rectangle by a quaternion /// </summary> /// <param name="rot">The quaternion to rotate by</param> /// <returns>The current rectangle instance</returns> public Rectangle3D Rotate(Quaternion rot) { foreach (var k in new List <string>(Corners.Keys)) { var q = new QuaternionRotation3D(new System.Windows.Media.Media3D.Quaternion(rot.X, rot.Y, rot.Z, rot.W)); var r = new RotateTransform3D(q, ToPoint3D(Center)); Corners[k] = ToVector3(r.Transform(ToPoint3D(Corners[k]))); } Position = Corners["000"]; GenerateEdges(); GenerateFaces(); return(this); }
private Vector3D GetDirectionModelCoords() { var worldCoords = base.GetWorldLocation(); // Calculate the difference between the world orientation and model orientation Quaternion toModelQuat = worldCoords.Item2.ToUnit() * this.Orientation.ToUnit(); RotateTransform3D toModelTransform = new RotateTransform3D(new QuaternionRotation3D(toModelQuat)); Vector3D directionWorld = _homePoint - worldCoords.Item1; // Rotate into model coords return(toModelTransform.Transform(directionWorld)); }
/// <summary> /// Find the line perpendicular to the function at a point /// </summary> /// <param name="centerPoint">The center of the circle at this point</param> /// <param name="slope">The normal line at this point</param> /// <param name="phi">The angle to create the perpendicular line</param> /// <returns></returns> private Vector3D GetPerpendicular(Vector3D centerPoint, Vector3D slope, double phi) { var phi0Normal = Vector3D.CrossProduct(slope, new Vector3D(1, 0, 0)); if (phi0Normal.LengthSquared == 0) { phi0Normal = Vector3D.CrossProduct(slope, new Vector3D(0, 1, 0)); } var rotation = new AxisAngleRotation3D(slope, DongUtility.UtilityFunctions.RadiansToDegrees(phi)); var transform = new RotateTransform3D(rotation); return((Vector3D)transform.Transform((Point3D)phi0Normal)); }
private void AnimateTranslateTransform3D(TranslateTransform3D translateTransform3D, double time, double speed) { Point3D boxPosition = new Point3D(translateTransform3D.OffsetX, translateTransform3D.OffsetY, translateTransform3D.OffsetZ); // Rotate the box position for the secondsDiff * SpeedSlider.Value degrees AxisAngleRotation3D axisAngleRotation3D = new AxisAngleRotation3D(new Vector3D(0, 1, 0), time * speed); RotateTransform3D rotateTransform3D = new RotateTransform3D(axisAngleRotation3D); Point3D rotatedPosition = rotateTransform3D.Transform(boxPosition); translateTransform3D.OffsetX = rotatedPosition.X; translateTransform3D.OffsetY = rotatedPosition.Y; translateTransform3D.OffsetZ = rotatedPosition.Z; }
/// <summary> /// Adds the lights to the element. /// </summary> /// <param name="lightGroup"> /// The light group. /// </param> protected override void AddLights(Model3DGroup lightGroup) { var t1 = new RotateTransform3D(new AxisAngleRotation3D(this.azimuthAxis, this.Azimuth)); var t2 = new RotateTransform3D(new AxisAngleRotation3D(this.altitudeAxis, this.Altitude)); var dir = t1.Transform(t2.Transform(new Vector3D(1, 0, 0))); var i = (byte)(255 * this.Brightness); lightGroup.Children.Add(new DirectionalLight(Color.FromRgb(i, i, i), dir)); var ai = (byte)(255 * this.Ambient); lightGroup.Children.Add(new AmbientLight(Color.FromRgb(ai, ai, ai))); }
private void OnChanged() { // movement in the Y dimension is interpretted as rotation about the horizontal axis RotateTransform3D startTransform = new RotateTransform3D(new QuaternionRotation3D(m_StartRotation)); Quaternion yRotation = new Quaternion(startTransform.Inverse.Transform(new Point3D(1, 0, 0)).ToVector3D(), this.m_YRotation); RotateTransform3D yTransform = new RotateTransform3D(new QuaternionRotation3D(yRotation)); // movement in the X dimension is interpretted as rotation about the vertical axis. var xAxis = yTransform.Transform(new Point3D(0, 0, 1)).ToVector3D(); Quaternion xRotation = new Quaternion(xAxis, this.m_XRotation); this.Rotation = m_StartRotation * (xRotation * yRotation); if (Changed != null) { Changed(this, EventArgs.Empty); } }
private static PMXVector3 Rotate(PMXVector3 centre, PMXVector3 point, double angle, float weight) { PMXVector3 offset = point - centre; if (offset.Value == 0) { return(point); } RotateTransform3D xrotation = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), angle)); //WPF for whatever reasons uses degrees and not Radian PMXVector3 rotated = xrotation.Transform(offset.ToPoint3D()).ToPMXVector3(); PMXVector3 actualRotation = (rotated - offset) * weight + offset; return(centre + actualRotation); }
private void AddBranch(MeshBuilder mesh, Point3D p0, Vector3D direction, int p) { double angle = GetAngleBetween(direction, UpVector); bool isStem = angle < 10; double h = isStem ? 2.5 : 2; double r = (Level + 1 - p) * 0.1; mesh.AddCone(p0, direction, r, r * 0.8, h, false, false, 12); var p1 = p0 + direction * h; if (p == Level) { return; } if (isStem) { var rightVector = direction.FindAnyPerpendicular(); var t0 = new RotateTransform3D(new AxisAngleRotation3D(rightVector, GetRandom(3))); AddBranch(mesh, p1, t0.Transform(direction), p + 1); var t1 = new RotateTransform3D(new AxisAngleRotation3D(rightVector, 95 + GetRandom(5))); var d1 = t1.Transform(direction); int nBranches = 5 + GetRandom(2); for (int i = 0; i < nBranches; i++) { double a = 360.0 * i / nBranches + GetRandom(25); var t2 = new RotateTransform3D(new AxisAngleRotation3D(UpVector, a)); AddBranch(mesh, p1, t2.Transform(d1), p + 1); } } else { var rightVector = Vector3D.CrossProduct(direction, UpVector); var t1 = new RotateTransform3D(new AxisAngleRotation3D(rightVector, -5 + GetRandom(5))); var t2 = new RotateTransform3D(new AxisAngleRotation3D(UpVector, 45 + GetRandom(10))); var t3 = new RotateTransform3D(new AxisAngleRotation3D(UpVector, -45 + GetRandom(10))); var d1 = t1.Transform(direction); AddBranch(mesh, p1, d1, p + 1); AddBranch(mesh, p1, t2.Transform(d1), p + 1); AddBranch(mesh, p1, t3.Transform(d1), p + 1); } }