public static void GetRotatedVector(this Quaternion quaternion, Point3D[] points) { Matrix3D matrix = new Matrix3D(); matrix.Rotate(quaternion); MatrixTransform3D transform = new MatrixTransform3D(matrix); transform.Transform(points); }
//------------------------------------------------------ // // Public Properties // //------------------------------------------------------ private static void MatrixPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { MatrixTransform3D target = ((MatrixTransform3D)d); target.PropertyChanged(MatrixProperty); }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var scale = (float)values[0]; var transform = (Numerics.Matrix4x4)values[1]; var tGroup = new Media3D.Transform3DGroup(); if (scale != 1) { var tform = new Media3D.ScaleTransform3D(scale, scale, scale); tform.Freeze(); tGroup.Children.Add(tform); } if (!transform.IsIdentity) { var tform = new Media3D.MatrixTransform3D(transform.ToMatrix3D()); tform.Freeze(); tGroup.Children.Add(tform); } tGroup.Freeze(); return(tGroup); }
/// <summary> /// Action to make when the mouse move /// </summary> public void MouseMove() { try { if (targetModel != null) { // Definition of new matrix value Matrix3D m3D = targetModel.Transform.Value; m3D.OffsetX = BaseOffsetX + jMover.manipulator.TargetTransform.Value.OffsetX; m3D.OffsetY = BaseOffsetY + jMover.manipulator.TargetTransform.Value.OffsetY; m3D.OffsetZ = BaseOffsetZ + jMover.manipulator.TargetTransform.Value.OffsetZ; MatrixTransform3D mt3D = new MatrixTransform3D(m3D); //Sauvegarde des données dans le model joint.Matrix[Session.CurrentSession.CurrentProject.CurrentModel3D.Animation.Tick] = m3D; // TODO : Improve //joint.Matrix.LerpMatrix(m3D); Session.CurrentSession.CurrentProject.CurrentModel3D.ReplaceInContent(joint); } } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Rotates the double vector around the angle in degrees /// </summary> public DoubleVector GetRotatedVector(Vector3D axis, double angle) { Matrix3D matrix = new Matrix3D(); matrix.Rotate(new Quaternion(axis, angle)); MatrixTransform3D transform = new MatrixTransform3D(matrix); return new DoubleVector(transform.Transform(this.Standard), transform.Transform(this.Orth)); }
public UserControl1() { InitializeComponent(); ax3d = new AxisAngleRotation3D(new Vector3D(0, 2, 0), 1); RotateTransform3D myRotateTransform = new RotateTransform3D(ax3d); var ModelVisual3D = this.GetModel(); MatrixTransform3D mTransform = new MatrixTransform3D(); ModelVisual3D.Transform = myRotateTransform; //model //// }
private Tuple <Material, Media3D.Transform3D> LoadSkull() { var m = new VolumeTextureDiffuseMaterial(); var data = VolumeTextureRawDataMaterialCore.LoadRAWFile("male128x256x256.raw", 128, 256, 256); m.Texture = ProcessData(data.VolumeTextures, data.Width, data.Height, data.Depth, out var transferMap); m.Color = new Color4(0.6f, 0.6f, 0.6f, 1f); m.TransferMap = transferMap; m.Freeze(); var rotate = Matrix.RotationAxis(new Vector3(1, 0, 0), (float)Math.PI); var transform = new Media3D.MatrixTransform3D(rotate.ToMatrix3D()); transform.Freeze(); return(new Tuple <Material, Media3D.Transform3D>(m, transform)); }
/// <summary> /// Provides transform that will map one set of bounds to another /// </summary> public static MatrixTransform3D BoundsMapping(Cuboid sourceBounds, Cuboid destinationBounds) { Point3D graphMin = sourceBounds.minimum; Point3D graphMax = sourceBounds.maximum; Point3D modelMin = destinationBounds.minimum; Point3D modelMax = destinationBounds.maximum; double scaleX = (modelMax.X - modelMin.X) / (graphMax.X - graphMin.X); double scaleY = (modelMax.Y - modelMin.Y) / (graphMax.Y - graphMin.Y); double scaleZ = (modelMax.Z - modelMin.Z) / (graphMax.Z - graphMin.Z); double offX = -graphMin.X * scaleX + modelMin.X; double offY = -graphMin.Y * scaleY + modelMin.Y; double offZ = -graphMin.Z * scaleZ + modelMin.Z; Matrix3D transform = new Matrix3D(scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, scaleZ, 0, offX, offY, offZ, 1); MatrixTransform3D matrixTransform = new MatrixTransform3D(transform); return matrixTransform; }
private Tuple <Material, Media3D.Transform3D> LoadTeapot() { //var m = new VolumeTextureRawDataMaterial(); //m.Texture = VolumeTextureRawDataMaterialCore.LoadRAWFile("teapot256x256x178.raw", 256, 256, 178); var m = new VolumeTextureDiffuseMaterial(); var data = VolumeTextureRawDataMaterialCore.LoadRAWFile("teapot256x256x178.raw", 256, 256, 178); m.Texture = ProcessData(data.VolumeTextures, data.Width, data.Height, data.Depth, out var transferMap); m.Color = new Color4(1, 1, 1, 0.4f); m.TransferMap = transferMap; m.Freeze(); var scale = Matrix.Scaling(2, 2, 178 / 256f * 2); var rotate = Matrix.RotationAxis(new Vector3(1, 0, 0), (float)Math.PI); var t = new Media3D.MatrixTransform3D((scale * rotate).ToMatrix3D()); t.Freeze(); return(new Tuple <Material, Media3D.Transform3D>(m, t)); }
private void Filter_FilterUpdate(object sender, EventArgs e) { Quaternion corrected = new Quaternion(); corrected.X = filter.Transform.Y; corrected.Y = filter.Transform.Z; corrected.Z = -filter.Transform.X; corrected.W = filter.Transform.W; var transform = new QuaternionRotation3D(corrected); Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => { var rot = new RotateTransform3D(); rot.Rotation = transform; Transform = new MatrixTransform3D(rot.Value); this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Transform")); })); }
private Media3D.Transform3D GetTransform(float scale, Matrix4x4 transform) { var tGroup = new Media3D.Transform3DGroup(); if (scale != 1) { var tform = new Media3D.ScaleTransform3D(scale, scale, scale); tform.Freeze(); tGroup.Children.Add(tform); } if (!transform.IsIdentity) { var tform = new Media3D.MatrixTransform3D(transform.ToMatrix3D()); tform.Freeze(); tGroup.Children.Add(tform); } tGroup.Freeze(); return(tGroup); }
/// <summary> /// Build a joint for drawing /// </summary> /// <param name="joint">Joint</param> /// <returns>Joint created</returns> private GeometryModel3D BuildJoint(Joint joint) { MeshBuilder jointBuilder = new MeshBuilder(); jointBuilder.AddSphere(new Point3D(0, 0, 0), jointDiameter, thetaDiv, 10); GeometryModel3D jointModel = new GeometryModel3D(jointBuilder.ToMesh(), Materials.Red); jointModel.SetName(joint.Name); MatrixTransform3D transform = new MatrixTransform3D(joint.Matrix.ElementAtOrDefault(Session.CurrentSession.CurrentProject.CurrentModel3D.Animation.Tick)); jointModel.Transform = transform; return jointModel; }
/// <summary> /// This overload will rotate arrays of different types. Just pass null if you don't have any of that type /// </summary> public static void GetRotatedVector(this Quaternion quaternion, Vector3D[] vectors, Point3D[] points, DoubleVector[] doubleVectors) { Matrix3D matrix = new Matrix3D(); matrix.Rotate(quaternion); MatrixTransform3D transform = new MatrixTransform3D(matrix); if (vectors != null) { transform.Transform(vectors); } if (points != null) { transform.Transform(points); } if (doubleVectors != null) { for (int cntr = 0; cntr < doubleVectors.Length; cntr++) { doubleVectors[cntr] = new DoubleVector(transform.Transform(doubleVectors[cntr].Standard), transform.Transform(doubleVectors[cntr].Orth)); } } }
/// <summary> /// Rotates the model around its axis /// </summary> /// <param name="x">Rotation around the x-axis in degrees</param> /// <param name="y">Rotation around the y-axis in degrees</param> /// <param name="z">Rotation around the z-axis in degrees</param> /// <param name="safe">True if too high values should be ignored, false otherwise</param> public void rotate(double x, double y, double z, bool safe) { if(safe && (Math.Abs(x) > 20 || Math.Abs(y) > 20 || Math.Abs(z) > 20)) return; //ignore if safe is checked and any operation is > 50 MatrixTransform3D matrixTransform = new MatrixTransform3D(); matrixTransform.Matrix = CalculateRotationMatrix(x, y, z); transformations.Children.Add(matrixTransform); model3DGroup.Transform = transformations; angle += z; angle %= 360; }
protected virtual void NewTransform() { if (lockCount != 0) return; Matrix3D m = new Matrix3D(); m.Scale(new Vector3D(ScaleX, ScaleY, ScaleZ)); m.Rotate(Rotation1); m.Rotate(Rotation2); m.Translate(new Vector3D(Position.X, Position.Y, Position.Z)); m.Rotate(Rotation3); Transform = new MatrixTransform3D(m); }
public void SetTransformMatrix(Matrix3D matrix) { MatrixTransform3D t3d = new MatrixTransform3D(matrix); this.GetModel().Transform = t3d; }
/// <summary> /// Transforms from local to world coordinates. /// </summary> /// <param name="vector"> /// The vector (local coordinates). /// </param> /// <returns> /// Transformed vector (world coordinates). /// </returns> protected Vector3D ToWorld(Vector3D vector) { var mat = Visual3DHelper.GetTransform(this); var t = new MatrixTransform3D(mat); return t.Transform(vector); }
/// <summary> /// Rotates the vector around the angle in degrees /// </summary> public static Vector3D GetRotatedVector(this Vector3D vector, Vector3D axis, double angle) { Matrix3D matrix = new Matrix3D(); matrix.Rotate(new Quaternion(axis, angle)); MatrixTransform3D transform = new MatrixTransform3D(matrix); return transform.Transform(vector); }
public DraggableGeometryModel3D() : base() { this.dragTransform = new MatrixTransform3D(this.Transform.Value); }
protected void UpdateGraphToWorld() { Point3D graphMin = (Point3D)GetValue(GraphMinProperty); Point3D graphMax = (Point3D)GetValue(GraphMaxProperty); double scaleX = (worldMax.X - worldMin.X) / (graphMax.X - graphMin.X); double scaleY = (worldMax.Y - worldMin.Y) / (graphMax.Y - graphMin.Y); double scaleZ = (worldMax.Z - worldMin.Z) / (graphMax.Z - graphMin.Z); double offX = -graphMin.X * scaleX + worldMin.X; double offY = -graphMin.Y * scaleY + worldMin.Y; double offZ = -graphMin.Z * scaleZ + worldMin.Z; Matrix3D transform = new Matrix3D(scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, scaleZ, 0, offX, offY, offZ, 1); MatrixTransform3D matrixTransform = new MatrixTransform3D(); matrixTransform.Matrix = transform; SetValue(GraphToWorldProperty, matrixTransform); }
public void SetLayer2D(Canvas canvas, MatrixTransform3D modelToWorld) { layer2D = new SharpDXLayer2D(canvas, this, modelToWorld); }
/// <summary> /// Construct a new display object. /// </summary> /// <param name="Key">The key it is stored at.</param> /// <param name="pRenderer">The renderer which created it.</param> internal Display(object Key, Renderer pRenderer) { // Store the renderer. this.Renderer = pRenderer; this.Key = Key; // Identities. HomographyTransform = new MatrixTransform3D(); // Create the model visual. Viewport2DVisual3D = new Viewport2DVisual3D(); // Geom (create a texturemapped rectangle). var pMeshGeometry3D = new MeshGeometry3D(); pMeshGeometry3D.Positions.Add(new Point3D(0, 0, 0)); pMeshGeometry3D.Positions.Add(new Point3D(0, 1, 0)); pMeshGeometry3D.Positions.Add(new Point3D(1, 0, 0)); pMeshGeometry3D.Positions.Add(new Point3D(1, 1, 0)); pMeshGeometry3D.TextureCoordinates.Add(new Point(0, 0)); pMeshGeometry3D.TextureCoordinates.Add(new Point(1, 0)); pMeshGeometry3D.TextureCoordinates.Add(new Point(0, 1)); pMeshGeometry3D.TextureCoordinates.Add(new Point(1, 1)); //pMeshGeometry3D.TextureCoordinates.Add(new Point(0, 1)); //pMeshGeometry3D.TextureCoordinates.Add(new Point(0, 0)); //pMeshGeometry3D.TextureCoordinates.Add(new Point(1, 1)); //pMeshGeometry3D.TextureCoordinates.Add(new Point(1, 0)); pMeshGeometry3D.TriangleIndices.Add(0); pMeshGeometry3D.TriangleIndices.Add(2); pMeshGeometry3D.TriangleIndices.Add(1); pMeshGeometry3D.TriangleIndices.Add(2); pMeshGeometry3D.TriangleIndices.Add(3); pMeshGeometry3D.TriangleIndices.Add(1); Viewport2DVisual3D.Geometry = pMeshGeometry3D; // Create a visual brush which we apply to our material. VisualBrush = new VisualBrush(); VisualBrush.Visual = Content; // Create a material which wraps this brush. var pDiffuse = new DiffuseMaterial(); pDiffuse.Brush = Brushes.White; Viewport2DVisual3D.SetIsVisualHostMaterial(pDiffuse, true); Viewport2DVisual3D.Material = new DiffuseMaterial(VisualBrush); Viewport2DVisual3D.Visual = Content; // Transform. Viewport2DVisual3D.Transform = HomographyTransform; }
/// <summary> /// Transforms from world to local coordinates. /// </summary> /// <param name="worldPoint"> /// The point (world coordinates). /// </param> /// <returns> /// Transformed vector (local coordinates). /// </returns> protected Point3D ToLocal(Point3D worldPoint) { var mat = Visual3DHelper.GetTransform(this); mat.Invert(); var t = new MatrixTransform3D(mat); return t.Transform(worldPoint); }
// I copy the code in each of these overloads, rather than make a private method to increase speed //TODO: I don't use these rotate extension methods anymore, but it would be worth testing whether to use this matrix transform, or use: new RotateTransform3D(new QuaternionRotation3D(quaternion)) public static Vector3D GetRotatedVector(this Quaternion quaternion, Vector3D vector) { Matrix3D matrix = new Matrix3D(); matrix.Rotate(quaternion); MatrixTransform3D transform = new MatrixTransform3D(matrix); return transform.Transform(vector); }
/// <summary> /// Transforms from local to world coordinates. /// </summary> /// <param name="point"> /// The point (local coordinates). /// </param> /// <returns> /// Transformed point (world coordinates). /// </returns> protected Point3D ToWorld(Point3D point) { var mat = Visual3DHelper.GetTransform(this); var t = new MatrixTransform3D(mat); return t.Transform(point); }
public SharpDXLayer2D(Canvas canvas, ViewportImage ViewportImage, MatrixTransform3D ModelToWorld) { this.canvas = canvas; this.ModelToWorld = ModelToWorld; this.ViewportImage = ViewportImage; }
public PBRViewModel(IEffectsManager manager) { EffectsManager = manager; this.Camera = new PerspectiveCamera { Position = new Point3D(0, 60, 60), LookDirection = new Vector3D(0, -60, -60), UpDirection = new Vector3D(0, 1, 0) }; var builder = new MeshBuilder(); builder.AddSphere(Vector3.Zero, 2); SphereModel = builder.ToMesh(); var normalMap = TextureModel.Create(new System.Uri("TextureNoise1_dot3.dds", System.UriKind.RelativeOrAbsolute).ToString()); for (int i = -Row; i < Row; ++i) { for (int j = -Col; j < Col; ++j) { var m = new PBRMaterial() { AlbedoColor = albedoColor.ToColor4(), RoughnessFactor = 1.0 / (2 * Row) * Math.Abs(i + Row), MetallicFactor = 1.0 / (2 * Col) * Math.Abs(j + Col), RenderEnvironmentMap = true, EnableAutoTangent = true, NormalMap = normalMap, RenderShadowMap = true }; materials.Add(m); Models.Add(new MeshGeometryModel3D() { CullMode = SharpDX.Direct3D11.CullMode.Back, Geometry = SphereModel, Material = m, IsThrowingShadow = true, Transform = new Media3D.TranslateTransform3D(new Vector3D(i * 6, 0, j * 6)) }); } } builder = new MeshBuilder(); builder.AddSphere(Vector3.Zero, 8, 12, 12); Model = builder.ToMesh(); Material = new PBRMaterial() { AlbedoColor = albedoColor.ToColor4(), RenderEnvironmentMap = true, AlbedoMap = TextureModel.Create("Engraved_Metal_COLOR.jpg"), NormalMap = TextureModel.Create("Engraved_Metal_NORM.jpg"), DisplacementMap = TextureModel.Create("Engraved_Metal_DISP.png"), RoughnessMetallicMap = TextureModel.Create("Engraved_Metal_RMC.png"), DisplacementMapScaleMask = new Vector4(0.1f, 0.1f, 0.1f, 0), EnableAutoTangent = true, EnableTessellation = true, MaxDistanceTessellationFactor = 2, MinDistanceTessellationFactor = 4 }; ModelTransform = new Media3D.MatrixTransform3D(Matrix.Translation(0, 30, 0).ToMatrix3D()); builder = new MeshBuilder(); builder.AddBox(Vector3.Zero, 100, 0.5, 100); var floorGeo = builder.ToMesh(); for (int i = 0; i < floorGeo.TextureCoordinates.Count; ++i) { floorGeo.TextureCoordinates[i] *= 5; } FloorModel = floorGeo; FloorMaterial = new PBRMaterial() { AlbedoMap = TextureModel.Create("Wood_Planks_COLOR.jpg"), NormalMap = TextureModel.Create("Wood_Planks_NORM.jpg"), DisplacementMap = TextureModel.Create("Wood_Planks_DISP.png"), RoughnessMetallicMap = TextureModel.Create("Wood_Planks_RMA.png"), AmbientOcculsionMap = TextureModel.Create("Wood_Planks_RMA.png"), DisplacementMapScaleMask = new Vector4(1f, 1f, 1f, 0), RoughnessFactor = 0.8, MetallicFactor = 0.2, RenderShadowMap = true, EnableAutoTangent = true, }; FloorModelTransform = new Media3D.MatrixTransform3D(Matrix.Translation(0, -5, 0).ToMatrix3D()); }
void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) { switch (connectionId) { case 1: this.tbM11 = ((System.Windows.Controls.TextBox)(target)); return; case 2: this.tbM21 = ((System.Windows.Controls.TextBox)(target)); return; case 3: this.tbM31 = ((System.Windows.Controls.TextBox)(target)); return; case 4: this.tbM41 = ((System.Windows.Controls.TextBox)(target)); return; case 5: this.tbM12 = ((System.Windows.Controls.TextBox)(target)); return; case 6: this.tbM22 = ((System.Windows.Controls.TextBox)(target)); return; case 7: this.tbM32 = ((System.Windows.Controls.TextBox)(target)); return; case 8: this.tbM42 = ((System.Windows.Controls.TextBox)(target)); return; case 9: this.tbM13 = ((System.Windows.Controls.TextBox)(target)); return; case 10: this.tbM23 = ((System.Windows.Controls.TextBox)(target)); return; case 11: this.tbM33 = ((System.Windows.Controls.TextBox)(target)); return; case 12: this.tbM43 = ((System.Windows.Controls.TextBox)(target)); return; case 13: this.tbM14 = ((System.Windows.Controls.TextBox)(target)); return; case 14: this.tbM24 = ((System.Windows.Controls.TextBox)(target)); return; case 15: this.tbM34 = ((System.Windows.Controls.TextBox)(target)); return; case 16: this.tbM44 = ((System.Windows.Controls.TextBox)(target)); return; case 17: this.rbOrthographic = ((System.Windows.Controls.RadioButton)(target)); return; case 18: this.rbPerspective = ((System.Windows.Controls.RadioButton)(target)); return; case 19: this.btnApply = ((System.Windows.Controls.Button)(target)); #line 112 "..\..\MatrixTransformation.xaml" this.btnApply.Click += new System.Windows.RoutedEventHandler(this.btnApply_Click); #line default #line hidden return; case 20: this.myViewport = ((System.Windows.Controls.Viewport3D)(target)); return; case 21: this.myCameraMatrix = ((System.Windows.Media.Media3D.MatrixCamera)(target)); return; case 22: this.myTransform = ((System.Windows.Media.Media3D.MatrixTransform3D)(target)); return; } this._contentLoaded = true; }
private void LoadFile() { loader = new CMOReader(); var obj3Ds = loader.Read("Sphere_Bot_test.cmo"); foreach (var group in loader.AnimationHierarchy) { var boneGroup = new BoneGroupModel3D(); var skeletonGroup = new BoneGroupModel3D(); foreach (var obj3D in group.Meshes) { if (obj3D.Geometry is BoneSkinnedMeshGeometry3D boneMesh) { var model = new BoneSkinMeshGeometryModel3D() { Geometry = obj3D.Geometry, FrontCounterClockwise = false, CullMode = CullMode.Back, Material = obj3D.Material.ConvertToMaterial(), IsThrowingShadow = true }; boneGroup.Children.Add(model); } else if (obj3D.Geometry is MeshGeometry3D) { Models.Add(new MeshGeometryModel3D() { Geometry = obj3D.Geometry, CullMode = CullMode.Back, FrontCounterClockwise = false }); } } var skeletonModel = new BoneSkinMeshGeometryModel3D() { Geometry = BoneSkinnedMeshGeometry3D.CreateSkeletonMesh(group.Bones, 0.1f), CullMode = CullMode.Back, Material = BoneMaterial, PostEffects = "xray" }; skeletonGroup.Children.Add(skeletonModel); Models.Add(boneGroup); BoneModels.Add(skeletonGroup); BoneGroupsDictionary.Add(group.GUID, new List <BoneGroupModel3D>() { boneGroup, skeletonGroup }); } var diffuse = new MemoryStream(); using (var file = File.OpenRead(@"Sphere_Bot_Rusty_UVMap_color.png")) { file.CopyTo(diffuse); } var normal = new MemoryStream(); using (var file = File.OpenRead(@"Sphere_Bot_Rusty_UVMap_nmap.png")) { file.CopyTo(normal); } foreach (var group in Models) { if (group is GroupElement3D g) { foreach (var subModel in g.Children) { if (subModel is MaterialGeometryModel3D model) { var m = model.Material as PhongMaterial; m.EmissiveColor = Colors.Black.ToColor4(); m.DiffuseMap = diffuse; m.NormalMap = normal; m.RenderShadowMap = true; } } } } Animations = loader.UniqueAnimations.Keys.ToArray(); ModelTransform = new Media3D.MatrixTransform3D((Matrix.Scaling(10, 10, 10) * Matrix.RotationAxis(Vector3.UnitX, -(float)Math.PI / 2)).ToMatrix3D()); foreach (var group in loader.AnimationHierarchy) { foreach (var ani in group.Animations) { if (!keyframeUpdaters.ContainsKey(ani.Key)) { keyframeUpdaters.Add(ani.Key, new List <KeyValuePair <Guid, KeyFrameUpdater> >()); } keyframeUpdaters[ani.Key].Add(new KeyValuePair <Guid, KeyFrameUpdater>(group.GUID, new KeyFrameUpdater(ani.Value, group.Bones))); } } }
/// <summary> /// Translates (moves) the model in the 3D-space /// </summary> /// <param name="x">Movement in x-direction</param> /// <param name="y">Movement in y-direction</param> /// <param name="z">Movement in z-direction</param> /// <param name="safe">True if too high values should be ignored, false otherwise</param> public void translate(double x, double y, double z, bool safe) { if (safe && (x > 20 || y > 20 || z > 20)) { return; } MatrixTransform3D matrixTransform = new MatrixTransform3D(); matrixTransform.Matrix = calculateTranslationMatrix(x, y, z); double xtmp = offsetX + matrixTransform.Matrix.OffsetX; double ytmp = offsetY + matrixTransform.Matrix.OffsetY; double ztmp = offsetZ + matrixTransform.Matrix.OffsetZ; if (!safe || (xtmp < Room.ROOM_X2 && xtmp > Room.ROOM_X1 && ytmp < Room.ROOM_Y2 && ytmp > Room.ROOM_Y1 && ztmp < Room.ROOM_Z2 && ztmp > Room.ROOM_Z1)) { //######## right coordinates logic should be moved to room ##### if (xtmp >= lochX1 && xtmp <= lochX2 && ytmp <= lochY1 && ytmp >= lochY2) { rightCoords = true; } else rightCoords = false; if (rightCoords || ztmp > Room.WALL_Z || !safe) { //######## right coordinates logic should be moved to room ##### offsetX += matrixTransform.Matrix.OffsetX; offsetY += matrixTransform.Matrix.OffsetY; offsetZ += matrixTransform.Matrix.OffsetZ; transformations.Children.Add(matrixTransform); model3DGroup.Transform = transformations; } } //Console.WriteLine(offsetX + " " + offsetY + " " + offsetZ + " " + angle); }
/// <summary> /// Returns the transform or the inverse transform between this visual and the specified ancestor. /// If inverse is requested but does not exist (if the transform is not invertible), null is returned. /// </summary> /// <param name="ancestor">Ancestor visual.</param> /// <param name="inverse">Returns inverse if this argument is true.</param> private GeneralTransform3D InternalTransformToAncestor(Visual3D ancestor, bool inverse) { Debug.Assert(ancestor != null); // used to track if all the collected transforms on the way to the ancestor were valid bool success = true; DependencyObject g = this; Visual3D lastVisual3D = null; Matrix3D m = Matrix3D.Identity; GeneralTransform3DGroup group = null; // This while loop will walk up the visual tree until we encounter the ancestor. // As it does so, it will accumulate the descendent->ancestor transform. // In most cases, this is simply a matrix, though if we encounter a 2D node we need to // transform from 3D out in to 2D and then back in to 3D and continue the parent walk. // We will accumulate the current transform in a matrix until we encounter a 2D parent, // at which point we will add the matrix's current value and the transform from 3D to 2D to 3D // to the GeneralTransform3DGroup and continue to accumulate further transforms in the matrix again. // At the end of this loop, we will have 0 or more transforms in the GeneralTransform3DGroup // and the matrix which, if not identity, should be appended to the GeneralTransform3DGroup. // If, as is commonly the case, this loop terminates without encountering a 2D parent // we will simply use the Matrix3D. while ((VisualTreeHelper.GetParent(g) != null) && (g != ancestor)) { Visual3D gAsVisual3D = g as Visual3D; if (gAsVisual3D != null) { Transform3D transform = gAsVisual3D.Transform; if (transform != null) { transform.Append(ref m); } lastVisual3D = gAsVisual3D; g = VisualTreeHelper.GetParent(gAsVisual3D); } else { if (group == null) { group = new GeneralTransform3DGroup(); } group.Children.Add(new MatrixTransform3D(m)); m = Matrix3D.Identity; // construct the 3D to 2D to 3D transform Visual gAsVisual = g as Visual; GeneralTransform3DTo2D transform3DTo2D = lastVisual3D.TransformToAncestor(gAsVisual); // now find the 3D parent of the 2D object Visual3D containing3DParent = VisualTreeHelper.GetContainingVisual3D(gAsVisual); // if containing3DParent is null, then the ancestor parameter is not really an ancestor // break out of the loop to allow it to fail if (containing3DParent == null) { break; } GeneralTransform2DTo3D transform2DTo3D = gAsVisual.TransformToAncestor(containing3DParent); // if either transform ends up being null then we don't have a transform if (transform3DTo2D == null || transform2DTo3D == null) { // we don't want to break here because although we own't be able to create a valid transformation // we also want to throw an exception if the ancestor passed in is not a valid ancestor. We then // continue the tree walk to make sure. success = false; } else { group.Children.Add(new GeneralTransform3DTo2DTo3D(transform3DTo2D, transform2DTo3D)); } // the last visual3D found is where we continue the search g = containing3DParent; } } if (g != ancestor) { throw new System.InvalidOperationException(SR.Get(inverse ? SRID.Visual_NotADescendant : SRID.Visual_NotAnAncestor)); } // construct the generaltransform3d to return and invert it if necessary GeneralTransform3D finalTransform = null; // if we successfully found a transform then we can create it here, otherwise finalTransform stays null if (success) { if (group != null) { finalTransform = group; } else { finalTransform = new MatrixTransform3D(m); } if (inverse) { finalTransform = finalTransform.Inverse; } } if (finalTransform != null) { finalTransform.Freeze(); } return finalTransform; }
public static Rect3D TransformBy(this Rect3D rect, Matrix3D matrix3d) { MatrixTransform3D m3d = new MatrixTransform3D(matrix3d); return m3d.TransformBounds(rect); }
/// <summary> /// The sort children. /// </summary> private void SortChildren() { var vp = Visual3DHelper.GetViewport3D(this); if (vp == null) { return; } var cam = vp.Camera as ProjectionCamera; if (cam == null) { return; } var cameraPos = cam.Position; var transform = new MatrixTransform3D(Visual3DHelper.GetTransform(this)); IList<Visual3D> transparentChildren = new List<Visual3D>(); IList<Visual3D> opaqueChildren = new List<Visual3D>(); if (this.CheckForOpaqueVisuals) { foreach (var child in this.Children) { if (this.IsVisualTransparent(child)) { transparentChildren.Add(child); } else { opaqueChildren.Add(child); } } } else { transparentChildren = this.Children; } // sort the children by distance from camera (note that OrderBy is a stable sort algorithm) var sortedTransparentChildren = transparentChildren.OrderBy(item => -this.GetCameraDistance(item, cameraPos, transform)).ToList(); this.Children.Clear(); // add the opaque children foreach (var c in opaqueChildren) { this.Children.Add(c); } // add the sorted transparent children foreach (var c in sortedTransparentChildren) { this.Children.Add(c); } }
public static void GetRotatedVector(this Quaternion quaternion, DoubleVector[] doubleVectors) { Matrix3D matrix = new Matrix3D(); matrix.Rotate(quaternion); MatrixTransform3D transform = new MatrixTransform3D(matrix); for (int cntr = 0; cntr < doubleVectors.Length; cntr++) { doubleVectors[cntr] = new DoubleVector(transform.Transform(doubleVectors[cntr].Standard), transform.Transform(doubleVectors[cntr].Orth)); } }
public static Point3D GetRotatedVector(this Quaternion quaternion, Point3D point) { Matrix3D matrix = new Matrix3D(); matrix.Rotate(quaternion); MatrixTransform3D transform = new MatrixTransform3D(matrix); return transform.Transform(point); }
/// <summary> /// rotate model by angle /// </summary> /// <param name="axis">axis for rotate</param> /// <param name="rotateAngle">rotation angle</param> public void RotateObjectByAngle(Vector3D axis, double rotateAngle) { try { if (_CurrentModel != null) { Transform3DGroup group = _CurrentModel.Content.Transform as Transform3DGroup; if (group.Children.Count > 0) { for (int i = 0; i < group.Children.Count; i++) { MatrixTransform3D rtrans = group.Children[i] as MatrixTransform3D; if (rtrans != null) { Matrix3D matrix = rtrans.Matrix; matrix.Rotate(new Quaternion(axis, rotateAngle)); //get current radius of X axis double rotaterad = Math.Acos(matrix.M22); if ((rotaterad * (180 / Math.PI)) > _MaxRotateXDegree) { matrix.Rotate(new Quaternion(axis, -rotateAngle)); } MatrixTransform3D mat = new MatrixTransform3D(matrix); rtrans = mat; group.Children[i] = rtrans; } } } else { Matrix3D matrix = new Matrix3D(); matrix.Rotate(new Quaternion(axis, rotateAngle)); MatrixTransform3D mat = new MatrixTransform3D(matrix); group.Children.Add(mat); } } } catch (Exception ex) { throw ex; } }
public static DoubleVector GetRotatedVector(this Quaternion quaternion, DoubleVector doubleVector) { Matrix3D matrix = new Matrix3D(); matrix.Rotate(quaternion); MatrixTransform3D transform = new MatrixTransform3D(matrix); return new DoubleVector(transform.Transform(doubleVector.Standard), transform.Transform(doubleVector.Orth)); }
private Robot robot; //the robot to display! #endregion Fields #region Constructors public ViewPlatform(Viewport3D viewport, MotorManager motorManager, Robot robot) { this.robot = robot; robot.intialise(viewport); this.motorManager = motorManager; //Add myself as a listener for the motor manager: this.motorManager.addListener(this); //Create a focussed camera to watch the origin: focussedCamera = new FocussedCamera(200, 0, 0); //Create the perspective camera! camera = getCamera(focussedCamera.Location, focussedCamera.Direction); viewport.Camera = camera; //Now to construct the light: light = getLight(Colors.White,focussedCamera.Direction); ModelVisual3D visual = new ModelVisual3D(); visual.Content = light; viewport.Children.Add(visual); ModelVisual3D visual2 = new ModelVisual3D(); //Now add in the robot too! updateMotors(); outerModel = new Model3DGroup(); outerModel.Children.Add(robot.getRobot()); currentMatTransform = (MatrixTransform3D)MatrixTransform3D.Identity; TranslateTransform3D trans = new TranslateTransform3D(); outerModel.Children[0] = Transforms.applyTransform((Model3DGroup)outerModel.Children[0],currentMatTransform); visual2.Content = outerModel; viewport.Children.Add(visual2); //Phew! That should be it... viewport.ClipToBounds = true; }