private Axis GetClosestAxis(Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4, Ray ray) { //v1, v2, v4 --YZ //v2, v3, v4 --ZX //v3, v1, v4 --XY Axis[] axises = new Axis[] { Axis.X, Axis.Y, Axis.Z }; Vector3[,] triangles = new Vector3[, ] { { v1, v2, v4 }, { v2, v3, v4 }, { v3, v1, v4 } }; Axis axis = Axis.X; int min = -1; float distance = float.MaxValue; for (int i = 0; i < 3; i++) { float t, u, v; if (Helper3D.RayTriangleIntersect(ray.Position, ray.Direction, triangles[i, 0], triangles[i, 1], triangles[i, 2], out t, out u, out v, false)) { if (t < distance) { distance = t; min = i; } } } if (min >= 0) { axis = GetClosestAxis(ray.Position + ray.Position * distance, v4, axises[min]); } return(axis); }
/// <summary> /// A method for building the texture coordinates of the mesh. /// </summary> protected override void MapTexture() { for (int i = 0; i < 6; i++) { Helper3D.MapSquareTexture(VertexPositionTextureNormals, i * 6, 0.0f, 0.0f, 1.0f, 1.0f); } }
protected override bool OnButtonDown(Point pointerPosition) { this.isActive = false; Viewport3DElement firstHitViewport3D = this.ActiveView.GetFirstHitViewport3D(pointerPosition); if (firstHitViewport3D == null || firstHitViewport3D.ViewObject.PlatformSpecificObject == null) { return(false); } this.cameraElement = firstHitViewport3D.Camera; if (this.cameraElement == null || !this.cameraElement.IsSelectable) { return(false); } this.scale = Helper3D.UnitsPerPixel((Viewport3D)firstHitViewport3D.ViewObject.PlatformSpecificObject, new Point3D(0.0, 0.0, 0.0)); this.rootToViewport3DMatrix = ElementUtilities.GetComputedTransform(firstHitViewport3D.Visual != null ? firstHitViewport3D.Visual.PlatformSpecificObject as Visual : (Visual)null, (Visual)this.ActiveSceneViewModel.DefaultView.ViewRootContainer); this.pointerInViewportCoordinates = pointerPosition * this.rootToViewport3DMatrix; this.SetMovementMode(); this.cameraInitialUp = (Vector3D)this.cameraElement.GetComputedValue(ProjectionCameraElement.UpDirectionProperty); this.cameraInitialPosition = (Point3D)this.cameraElement.GetComputedValue(ProjectionCameraElement.PositionProperty); this.cameraInitialLookAt = this.cameraInitialPosition + (Vector3D)this.cameraElement.GetComputedValue(ProjectionCameraElement.LookDirectionProperty); this.totalAzimuthDelta = 0.0; this.totalElevationDelta = 0.0; this.lastPoint = this.pointerInViewportCoordinates; this.EnsureEditTransaction(); this.isActive = true; return(true); }
protected override void ButtonDownAction() { Vector3D vector = new Vector3D(); switch (this.ActiveAdorner.Axis) { case Adorner3D.TransformVia.XAxis: vector = new Vector3D(0.0, -AdornedToolBehavior3D.sqrt2div2, AdornedToolBehavior3D.sqrt2div2); break; case Adorner3D.TransformVia.YAxis: vector = new Vector3D(AdornedToolBehavior3D.sqrt2div2, 0.0, -AdornedToolBehavior3D.sqrt2div2); break; case Adorner3D.TransformVia.ZAxis: vector = new Vector3D(-AdornedToolBehavior3D.sqrt2div2, AdornedToolBehavior3D.sqrt2div2, 0.0); break; default: vector = new Vector3D(0.0, AdornedToolBehavior3D.sqrt2div2, -AdornedToolBehavior3D.sqrt2div2); break; } this.mouseMovementAxis = AdornedToolBehavior3D.Vector3DInViewport3D(this.Selected3DElement, vector); this.mouseMovementAxis.Normalize(); this.rotationAxis = this.ActiveAdorner.RotationAxis; this.initialQuaternion = Helper3D.QuaternionFromEulerAngles(this.Selected3DElement.CanonicalRotationAngles); this.previousQuaternion = this.initialQuaternion; this.previousAngle = new double?(); this.lastUnsnappedAngle = 0.0; }
/// <summary> /// It checks for the collision between a collision sphere and a collision model. /// </summary> protected bool TestSphereintersectModel(BoundingSphere sphere, Vector3[] vertices, Matrix transform, out Vector3 intersect, out Vector3 normal, out float distance) { distance = 0.0f; intersect = Vector3.Zero; normal = Vector3.Zero; for (int i = 0; i < vertices.Length; i += 3) { // Transform the three vertex positions into world space Vector3 v1 = Vector3.Transform(vertices[i], transform); Vector3 v2 = Vector3.Transform(vertices[i + 1], transform); Vector3 v3 = Vector3.Transform(vertices[i + 2], transform); totalCollidingCount++; // Check collision if (Helper3D.SphereIntersectTriangle(sphere.Center, sphere.Radius, v1, v2, v3, out intersect, out distance)) { normal = Vector3.Normalize(Vector3.Cross(v3 - v1, v2 - v1)); return(true); } } return(false); }
/// <summary> /// It checks for the collision between a collision ray and a collision model. /// </summary> protected bool TestRayintersectModel(Ray ray, Vector3[] vertices, Matrix transform, out Vector3 intersect, out Vector3 normal, out float distance) { Triangle outTriangle = new Triangle(Vector3.Zero, Vector3.Zero, Vector3.Zero); distance = 0.0f; intersect = Vector3.Zero; normal = Vector3.Zero; totalCollidingCount += vertices.Length; // Test ray with the model float?checkDistance = Helper3D.RayIntersectTriangle(ray, vertices, transform, out outTriangle); if (checkDistance != null) { // Retry test for intersect point and normal return(Helper3D.PointIntersect(ray.Position, ray.Direction, outTriangle, out distance, out intersect, out normal)); } return(false); }
/// <summary> /// A method for building the TextureCoordinates collection of the mesh. /// </summary> // protected override void CreateTextureCoordinates() internal override void MapTexture() { Mesh.TextureCoordinates.Clear(); for (int i = 0; i < 7; i++) { Helper3D.MapSquareTexture(Mesh, 0.0, 0.0, 1.0, 1.0); } }
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); } }
protected override void UpdateModelFromMouse(Base3DElement selected3DElement, Vector mousePositionDelta) { Camera camera = (Camera)selected3DElement.Viewport.Camera.ViewObject.PlatformSpecificObject; Matrix3D matrix3D1 = Helper3D.CameraRotationMatrix(camera); Matrix3D matrix3D2 = camera.Transform.Value; if (matrix3D2.HasInverse) { matrix3D2.Invert(); matrix3D1 *= matrix3D2; } Vector3D vector1 = new Vector3D(matrix3D1.M11, matrix3D1.M21, matrix3D1.M31); Vector3D vector2_1 = new Vector3D(matrix3D1.M12, matrix3D1.M22, matrix3D1.M32); Vector3D vector2_2 = new Vector3D(matrix3D1.M13, matrix3D1.M23, matrix3D1.M33); Base3DElement base3Delement = selected3DElement.Parent as Base3DElement; Matrix3D matrix3D3 = Matrix3D.Identity; if (base3Delement != null) { matrix3D3 = base3Delement.GetComputedTransformFromRoot3DElementToElement(); matrix3D3.Invert(); } if (this.mouseMovementMode == ObjectRotateTranslateBehavior.MovementMode.Rotate) { mousePositionDelta /= 2.0; Vector3D axisOfRotation = Vector3D.CrossProduct(new Vector3D(-mousePositionDelta.X, mousePositionDelta.Y, 0.0), vector2_2); double length = axisOfRotation.Length; if (length <= 0.0) { return; } Vector3D vector3D = Helper3D.EulerAnglesFromQuaternion(new Quaternion(axisOfRotation, length) * Helper3D.QuaternionFromEulerAngles(selected3DElement.CanonicalRotationAngles)); vector3D = new Vector3D(RoundingHelper.RoundAngle(vector3D.X), RoundingHelper.RoundAngle(vector3D.Y), RoundingHelper.RoundAngle(vector3D.Z)); selected3DElement.CanonicalRotationAngles = vector3D; } else { Vector3D vector3D1 = new Vector3D(selected3DElement.CanonicalTranslationX, selected3DElement.CanonicalTranslationY, selected3DElement.CanonicalTranslationZ); Point lastMousePosition = this.LastMousePosition; Point endPoint = lastMousePosition + mousePositionDelta; Vector3D vector3D2; if (this.mouseMovementMode == ObjectRotateTranslateBehavior.MovementMode.TranslateXY) { Plane3D plane = new Plane3D(Vector3D.CrossProduct(vector1, vector2_1), this.hitPoint); Vector3D vector = Helper3D.VectorBetweenPointsOnPlane((Viewport3D)selected3DElement.Viewport.ViewObject.PlatformSpecificObject, plane, lastMousePosition, endPoint); Vector3D vector3D3 = matrix3D3.Transform(vector); vector3D2 = vector3D1 + vector3D3; } else { double scale = this.Scale; vector3D2 = vector3D1 + scale * -mousePositionDelta.Y * vector2_2; } selected3DElement.CanonicalTranslationX = RoundingHelper.RoundLength(vector3D2.X); selected3DElement.CanonicalTranslationY = RoundingHelper.RoundLength(vector3D2.Y); selected3DElement.CanonicalTranslationZ = RoundingHelper.RoundLength(vector3D2.Z); } }
internal static Point Point3DInViewport3D(Viewport3D viewport, Matrix3D viewportToWorld, Point3D point) { Camera camera = viewport.Camera; Point3D point1 = viewportToWorld.Transform(point); Point3D point3D = Helper3D.CameraRotationTranslationMatrix(camera).Transform(point1); Point4D point2 = new Point4D(point3D.X, point3D.Y, point3D.Z, 1.0); Point4D point4D = AdornedToolBehavior3D.ProjectionMatrix(viewport.ActualWidth, viewport.ActualHeight, camera).Transform(point2); Point point3 = Math.Abs(point4D.W) >= 0.0 / 1.0 ? new Point(point4D.X / point4D.W, point4D.Y / point4D.W) : new Point(0.0, 0.0); return new Point((point3.X + 1.0) * viewport.ActualWidth / 2.0, viewport.ActualHeight * ((1.0 - point3.Y) / 2.0)); }
/// <summary> /// Initializes the Points and Triangles collections. /// Called By Sculptor.BuildMesh() /// </summary> /// <remarks>The cull mode is supposed to be clockwise (front faces are counter-clockwise).</remarks> protected override void CreateTriangles() { Vector3 radiusVector = new Vector3(_radius, 0, 0); PolygonSculptor pv1 = new PolygonSculptor(); pv1.Initialize(_circumferenceSideCount, _initialAngle); pv1.RoundingRate = _roundingRate; pv1.RoundCorner(); int circumferencePointCount = pv1.Points.Count; foreach (Vector3 p in pv1.Points) { Vector3 p1 = new Vector3(p.X, p.Y, p.Z) + radiusVector; this.Points.Add(p1); } float thetaStep = 2 * MathHelper.Pi / _segmentCount; float theta = 0.0f; for (int i = 1; i <= _segmentCount; i++) { theta = (thetaStep * i); Vector3 thetaVector = new Vector3( (float)Math.Cos(theta), 0, (float)Math.Sin(theta)); for (int i1 = 0; i1 < circumferencePointCount; i1++) { Vector3 p2 = Helper3D.RotateVector(Points[i1], theta, AxisDirection.Y); this.Points.Add(p2); } for (int j = 0; j < circumferencePointCount; j++) { int index1Min = (i - 1) * circumferencePointCount; int index1 = index1Min + j; int index2Min = i * circumferencePointCount; int index2 = index2Min + j; int indexLimit = index2Min + circumferencePointCount - 1; if (index2 < indexLimit) { this.Triangles.Add(new Vector3Triplet(Points[index2], Points[index2 + 1], Points[index1])); this.Triangles.Add(new Vector3Triplet(Points[index2 + 1], Points[index1 + 1], Points[index1])); } else { this.Triangles.Add(new Vector3Triplet(Points[index2], Points[index2Min], Points[index1])); this.Triangles.Add(new Vector3Triplet(Points[index2Min], Points[index1Min], Points[index1])); } } } }
protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); this.UpdateGeometries(Helper3D.QuaternionFromRotation3D((Rotation3D)this.GetValue(ArcBallPresenter.OrientationProperty))); drawingContext.DrawRectangle((Brush)Brushes.Transparent, (Pen)null, new Rect(0.0, 0.0, this.ActualWidth, this.ActualHeight)); drawingContext.DrawGeometry((Brush)null, this.backPen, this.XYBackGeometry); drawingContext.DrawGeometry((Brush)null, this.backPen, this.XZBackGeometry); drawingContext.DrawGeometry((Brush)null, this.backPen, this.YZBackGeometry); drawingContext.DrawGeometry((Brush)null, this.frontPen, this.XYFrontGeometry); drawingContext.DrawGeometry((Brush)null, this.frontPen, this.XZFrontGeometry); drawingContext.DrawGeometry((Brush)null, this.frontPen, this.YZFrontGeometry); }
private void CameraOrient(Direction?d) { PerspectiveCamera camera = (PerspectiveCamera)_viewport.Camera; if (d == null) { // Stops the animation // testing _cameraOrienting is not reliable // if (_cameraOrienting && _viewport.Camera.HasAnimatedProperties) if (_viewport.Camera.HasAnimatedProperties) { Vector3D currentDirection = camera.LookDirection; camera.ApplyAnimationClock(PerspectiveCamera.LookDirectionProperty, null); camera.LookDirection = currentDirection; //_cameraOrienting = false; } } else { Vector3D v = camera.LookDirection; Vector3D vY = new Vector3D(0.0, 1.0, 0.0); Vector3D vRelativeHorizontalAxis = Vector3D.CrossProduct(v, vY); Vector3D vRelativeVerticalAxis = Vector3D.CrossProduct(v, vRelativeHorizontalAxis); double targetAngle = 45.0; // MUST be under 90.0 (if 90.0, Forward and Forward|Left have the same target vector) // this code supports direction combinations if ((d & Direction.Forward) == Direction.Forward) { v = Helper3D.RotateVector(v, targetAngle, vRelativeHorizontalAxis); } if ((d & Direction.Backward) == Direction.Backward) { v = Helper3D.RotateVector(v, -targetAngle, vRelativeHorizontalAxis); } if ((d & Direction.Left) == Direction.Left) { v = Helper3D.RotateVector(v, -targetAngle, vRelativeVerticalAxis); } if ((d & Direction.Right) == Direction.Right) { v = Helper3D.RotateVector(v, targetAngle, vRelativeVerticalAxis); } if (v != camera.LookDirection) { _cameraLookDirectionAnimation.To = v; camera.ApplyAnimationClock(PerspectiveCamera.LookDirectionProperty, _cameraLookDirectionAnimation.CreateClock()); //_cameraOrienting = true; } } }
private Vec3f ParseVertexes(string data) { var vertex = data.Split(' ').Skip(1).Select(e => float.Parse(e.Replace('.', ','))); if (vertex.Count() > 3) { return(Helper3D.Triangulate(vertex.Skip(1).ToList())); } else { return(new Vec3f(vertex.ElementAt(0), vertex.ElementAt(1), vertex.ElementAt(2))); } }
/// <summary> /// Round the polygon's corners. /// </summary> internal void RoundCorner() { if (_roundingRate > 0.0) { if (!_rounded) { // ne doit être exécutée en principe qu'une fois // pour permettre la binding et // pour éviter les arrondis d'arrondis... // inconvénient : si de nouveaux points sont ajoutés à PointList, on ne peut pas les arrondir // la méthode publique InitializeNotRoundedPointList() est donc fournie pour forcer la réinitialisation InitializeNotRoundedPointList(); } Point3DCollection points = new Point3DCollection(); for (int i = 0; i <= _notRoundedPoints.Count - 1; i++) { Point3D pA = new Point3D(); Point3D pB = new Point3D(); Point3D pC = new Point3D(); pB = _notRoundedPoints[i]; if (i >= 1) { pA = _notRoundedPoints[i - 1]; } else if (i == 0) { pA = _notRoundedPoints[_notRoundedPoints.Count - 1]; } if (i < _notRoundedPoints.Count - 1) { pC = _notRoundedPoints[i + 1]; } else if (i == _notRoundedPoints.Count - 1) { pC = _notRoundedPoints[0]; } Point3DCollection pl = Helper3D.RoundCorner(pA, pB, pC, _roundingRate); Helper3D.CopyPoints(pl, points); } Helper3D.ClonePoints(points, Points); _rounded = true; } }
private bool ReadCanonicalForm(Transform3D transform, bool useIfChangeable) { if (!CanonicalTransform3D.IsCanonical(transform)) { return(false); } Transform3DGroup transform3Dgroup = (Transform3DGroup)transform; ScaleTransform3D scaleTransform3D = transform3Dgroup.Children[1] as ScaleTransform3D; RotateTransform3D rotateTransform3D = transform3Dgroup.Children[2] as RotateTransform3D; TranslateTransform3D translateTransform3D1 = transform3Dgroup.Children[3] as TranslateTransform3D; TranslateTransform3D translateTransform3D2 = transform3Dgroup.Children[4] as TranslateTransform3D; this.center = new Point3D(translateTransform3D1.OffsetX, translateTransform3D1.OffsetY, translateTransform3D1.OffsetZ); this.scale = new Vector3D(scaleTransform3D.ScaleX, scaleTransform3D.ScaleY, scaleTransform3D.ScaleZ); this.translation = new Vector3D(translateTransform3D2.OffsetX, translateTransform3D2.OffsetY, translateTransform3D2.OffsetZ); if (useIfChangeable && !transform3Dgroup.IsFrozen) { this.transformGroup = transform3Dgroup; } else { this.InitializeTransformGroup(); Vector3D? nullable = (Vector3D?)rotateTransform3D.GetValue(CanonicalTransform3D.EulerAnglesProperty); bool flag = nullable.HasValue; AxisAngleRotation3D axisAngleRotation3D = rotateTransform3D.Rotation as AxisAngleRotation3D; Quaternion orientation = Quaternion.Identity; if (axisAngleRotation3D != null) { orientation = new Quaternion(axisAngleRotation3D.Axis, axisAngleRotation3D.Angle); } if (flag) { Quaternion quaternion = Helper3D.QuaternionFromEulerAngles(nullable.Value); if (!Tolerances.AreClose(quaternion.Angle, orientation.Angle) || !Tolerances.AreClose(quaternion.Axis, orientation.Axis)) { flag = false; } } if (!flag) { nullable = new Vector3D?(Helper3D.EulerAnglesFromQuaternion(orientation)); } this.RotationAngles = nullable.Value; } return(true); }
internal static Point3D ProjectionPoint3DTranslatedToMatchingOrthographicPosition(Viewport3D viewport, Matrix3D pointToWorldTransform, OrthographicCamera ortho, Point3D point) { Camera camera = viewport.Camera; Point3D point1 = pointToWorldTransform.Transform(point); Point3D point3D = Helper3D.CameraRotationTranslationMatrix(camera).Transform(point1); Point4D point2 = new Point4D(point3D.X, point3D.Y, point3D.Z, 1.0); Point4D point4D1 = AdornedToolBehavior3D.ProjectionMatrix(viewport.ActualWidth, viewport.ActualHeight, camera).Transform(point2); Point4D point3 = new Point4D(point4D1.X / point4D1.W, point4D1.Y / point4D1.W, 0.0, 1.0); Matrix3D matrix3D1 = AdornedToolBehavior3D.ProjectionMatrix(viewport.ActualWidth, viewport.ActualHeight, (Camera) ortho); if (Math.Abs(matrix3D1.Determinant) > 1E-16) matrix3D1.Invert(); Point4D point4D2 = matrix3D1.Transform(point3); Point3D point4 = new Point3D(point4D2.X / point4D2.W, point4D2.Y / point4D2.W, point3D.Z); Matrix3D matrix3D2 = Helper3D.CameraRotationTranslationMatrix((Camera) ortho); matrix3D2.Invert(); return matrix3D2.Transform(point4); }
private void exportButton_Click(object sender, RoutedEventArgs e) { var element = FindName("house") as GeometryElement3D; var encoder = new StlEncoder(); encoder.ModelName = "House"; encoder.Sculptor = element.Sculptor; // Useful for MakerBot Replicator 2X / MakerWare Matrix3D mXRotation = Helper3D.GetXRotationMatrix(90.0); Matrix3D mScaling = Helper3D.GetScaleMatrix(10.0); MatrixTransform3D transform = new MatrixTransform3D(mXRotation * mScaling); encoder.PointsTransform = transform; encoder.Save(); }
private object ApplyRelativeTransform(object relativeTransform, object currentTransform) { object instance1 = Activator.CreateInstance(this.ComponentType); object instance2 = Activator.CreateInstance(this.ComponentType); object target = instance2; if (currentTransform != null) { if (this.PropertyLookup.TransformType == TransformType.PlaneProjection || this.PropertyLookup.IsCompositeSupported) { target = currentTransform; } else { target = Activator.CreateInstance(this.ComponentType, new object[1] { currentTransform }); } } foreach (PropertyReferenceProperty property in this.PropertyLookup.ActiveProperties) { if (property != null && property.Reference.FirstStep.TargetType == this.ComponentType) { if (((PropertyEntry)property).get_PropertyType() == typeof(double) && !(property.Reference.ShortPath == "RotationAngleX") && (!(property.Reference.ShortPath == "RotationAngleY") && !(property.Reference.ShortPath == "RotationAngleZ"))) { double num1 = (double)property.Reference.GetValue(relativeTransform); double num2 = (double)property.Reference.GetValue(target); double num3 = (double)property.Reference.GetValue(instance2); double num4 = !this.ComposesByMultiplication(property, instance2) ? num2 + (num1 - num3) : num2 * num1; double num5 = !((PropertyEntry)property).get_PropertyName().Contains("Angle") ? (!((PropertyEntry)property).get_PropertyName().Contains("Scale") ? RoundingHelper.RoundLength(num4) : RoundingHelper.RoundScale(num4)) : RoundingHelper.RoundAngle(num4); if (num5 != num3) { property.Reference.SetValue(instance1, (object)num5); } } else if (property.Reference.ShortPath == "RotationAngles") { Quaternion orientation = Helper3D.QuaternionFromEulerAngles((Vector3D)property.Reference.GetValue(relativeTransform)) * Helper3D.QuaternionFromEulerAngles((Vector3D)property.Reference.GetCurrentValue(target)); property.Reference.SetValue(instance1, (object)Helper3D.EulerAnglesFromQuaternion(orientation)); } } } return(instance1); }
/// <summary> /// Build a 3D mesh geometry from the Sculptor object. /// Points should be initialized before the call. /// </summary> public void BuildMesh() { CreateTriangles(); _vertexPositionTextureNormal = new VertexPositionTextureNormal[this.Triangles.Count * 3]; for (int i = 0; i < Triangles.Count; i++) { Vector3 normal = Helper3D.CalculateNormal( Triangles[i].Points[0], Triangles[i].Points[1], Triangles[i].Points[2]); normal.Normalize(); for (int j = 0; j < 3; j++) { _vertexPositionTextureNormal[i * 3 + j] = new VertexPositionTextureNormal(Triangles[i].Points[j], normal); } } MapTexture(); }
protected override sealed bool OnButtonDown(Point pointerPosition) { if (this.ActiveAdorner != null) this.selected3DElement = this.EditingElement as Base3DElement; if (this.selected3DElement == null) this.selected3DElement = this.ActiveSceneViewModel.ElementSelectionSet.PrimarySelection as Base3DElement; if (this.selected3DElement == null || this.selected3DElement.Viewport.Visual == null) return false; this.Scale = Helper3D.UnitsPerPixel((Viewport3D) this.Selected3DElement.Viewport.ViewObject.PlatformSpecificObject, this.Selected3DElement); this.rootToViewport3DMatrix = ElementUtilities.GetComputedTransform(this.selected3DElement.Viewport.Visual.PlatformSpecificObject as Visual, (Visual) this.ActiveSceneViewModel.DefaultView.ViewRootContainer); this.pointerInViewportCoordinates = pointerPosition * this.rootToViewport3DMatrix; this.shiftDown = this.IsShiftDown; this.controlDown = this.IsControlDown; this.altDown = this.IsAltDown; this.ButtonDownAction(); this.lastMousePosition = this.pointerInViewportCoordinates; this.EnsureEditTransaction(); return true; }
/// <summary> /// Round the polygon's corners. /// </summary> internal void RoundCorner() { if (_roundingRate > 0.0) { if (!_rounded) { // Must be executed only once InitializeNotRoundedPointList(); } Collection <Vector3> points = new Collection <Vector3>(); for (int i = 0; i <= _notRoundedPoints.Count - 1; i++) { Vector3 pA = new Vector3(); Vector3 pB = new Vector3(); Vector3 pC = new Vector3(); pB = _notRoundedPoints[i]; if (i >= 1) { pA = _notRoundedPoints[i - 1]; } else if (i == 0) { pA = _notRoundedPoints[_notRoundedPoints.Count - 1]; } if (i < _notRoundedPoints.Count - 1) { pC = _notRoundedPoints[i + 1]; } else if (i == _notRoundedPoints.Count - 1) { pC = _notRoundedPoints[0]; } Collection <Vector3> pl = Helper3D.RoundCorner(pA, pB, pC, _roundingRate); Helper3D.CopyVectors(pl, points); } Helper3D.CloneVectors(points, Points); _rounded = true; } }
protected override void UpdateModelFromMouse(Base3DElement selected3DElement, Vector mousePositionDelta) { this.lastUnsnappedAngle += this.ActiveView.Zoom * this.mouseMovementAxis * mousePositionDelta; double angleInDegrees1 = this.lastUnsnappedAngle; if (this.ShiftKeyDepressed) { angleInDegrees1 = RotateBehavior3D.shiftSnapAngle * Math.Round(angleInDegrees1 / RotateBehavior3D.shiftSnapAngle); } double angleInDegrees2 = !this.previousAngle.HasValue ? angleInDegrees1 : angleInDegrees1 - this.previousAngle.Value; this.previousAngle = new double?(angleInDegrees1); Vector3D unitEulerAngles = RotateBehavior3D.GetUnitEulerAngles(Helper3D.EulerAnglesFromQuaternion(this.previousQuaternion * new Quaternion(this.rotationAxis, angleInDegrees2)) - selected3DElement.CanonicalRotationAngles); this.previousQuaternion = this.initialQuaternion * new Quaternion(this.rotationAxis, angleInDegrees1); Vector3D vector3D = selected3DElement.CanonicalRotationAngles + unitEulerAngles; vector3D = new Vector3D(RoundingHelper.RoundAngle(vector3D.X), RoundingHelper.RoundAngle(vector3D.Y), RoundingHelper.RoundAngle(vector3D.Z)); selected3DElement.CanonicalRotationAngles = vector3D; }
protected override void Postprocess(BaseFrameworkElement originalElement, BaseFrameworkElement newElement, Dictionary <IPropertyId, SceneNode> properties, Rect layoutRect) { Viewport3DElement viewport = (Viewport3DElement)newElement; this.AddModel3DContainerToViewport(viewport, (Model3DElement)this.newGeometryElement); Camera perspectiveCamera = Helper3D.CreateEnclosingPerspectiveCamera(45.0, layoutRect.Width / layoutRect.Height, this.newGeometryElement.DesignTimeBounds, 1.0); viewport.Camera = (CameraElement)this.DesignerContext.ActiveSceneViewModel.CreateSceneNode((object)perspectiveCamera); AmbientLightElement ambientLightElement = (AmbientLightElement)this.DesignerContext.ActiveSceneViewModel.CreateSceneNode(typeof(AmbientLight)); ambientLightElement.Name = "Ambient"; ambientLightElement.SetValue(LightElement.ColorProperty, (object)Color.FromRgb((byte)sbyte.MinValue, (byte)sbyte.MinValue, (byte)sbyte.MinValue)); this.AddModel3DContainerToViewport(viewport, (Model3DElement)ambientLightElement); DirectionalLightElement directionalLightElement = (DirectionalLightElement)this.DesignerContext.ActiveSceneViewModel.CreateSceneNode(typeof(DirectionalLight)); directionalLightElement.Name = "Directional"; directionalLightElement.SetValue(LightElement.ColorProperty, (object)Color.FromRgb((byte)127, (byte)127, (byte)127)); directionalLightElement.SetValue(DirectionalLightElement.DirectionProperty, (object)new Vector3D(0.0, 0.0, -1.0)); directionalLightElement.Transform = (Transform3D) new TranslateTransform3D(new Vector3D(0.0, 0.0, 3.0)); this.AddModel3DContainerToViewport(viewport, (Model3DElement)directionalLightElement); }
protected override void ModifyValue(PropertyReference propertyReference, object valueToSet, SceneNode.Modification modification, int index) { bool flag = DesignTimeProperties.EulerAnglesProperty.Equals((object)propertyReference.LastStep); if (!flag || !this.ViewModel.AnimationEditor.IsKeyFraming || this.ViewModel.IsForcingBaseValue) { base.ModifyValue(propertyReference, valueToSet, modification, index); } if (!flag) { return; } propertyReference = propertyReference.Subreference(0, propertyReference.Count - 2); propertyReference = propertyReference.Append(Base3DElement.RotateTransform3DRotationProperty); if (valueToSet is Vector3D) { Quaternion quaternion = Helper3D.QuaternionFromEulerAngles((Vector3D)valueToSet); valueToSet = (object)new AxisAngleRotation3D(RoundingHelper.RoundDirection(quaternion.Axis), RoundingHelper.RoundAngle(quaternion.Angle)); } base.ModifyValue(propertyReference, valueToSet, modification, index); }
public static void DrawCube(DrawingContext drawingContext, Matrix matrix, Base3DElement element, Pen pen) { Viewport3DElement viewport1 = element.Viewport; if (viewport1 == null) { return; } Viewport3D viewport2 = viewport1.ViewObject.PlatformSpecificObject as Viewport3D; Rect3D localSpaceBounds = element.LocalSpaceBounds; Matrix3D viewport3DtoElement = element.GetComputedTransformFromViewport3DToElement(); Matrix3D matrix3D = Helper3D.CameraRotationTranslationMatrix(viewport2.Camera); Matrix3D cameraToObject = viewport3DtoElement * matrix3D; KeyValuePair <Point, bool> keyValuePair1 = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location); KeyValuePair <Point, bool> keyValuePair2 = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(localSpaceBounds.SizeX, 0.0, 0.0)); KeyValuePair <Point, bool> keyValuePair3 = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(localSpaceBounds.SizeX, localSpaceBounds.SizeY, 0.0)); KeyValuePair <Point, bool> keyValuePair4 = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(0.0, localSpaceBounds.SizeY, 0.0)); KeyValuePair <Point, bool> keyValuePair5 = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(0.0, 0.0, localSpaceBounds.SizeZ)); KeyValuePair <Point, bool> keyValuePair6 = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(localSpaceBounds.SizeX, 0.0, localSpaceBounds.SizeZ)); KeyValuePair <Point, bool> keyValuePair7 = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(localSpaceBounds.SizeX, localSpaceBounds.SizeY, localSpaceBounds.SizeZ)); KeyValuePair <Point, bool> keyValuePair8 = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(0.0, localSpaceBounds.SizeY, localSpaceBounds.SizeZ)); StreamGeometry streamGeometry = new StreamGeometry(); StreamGeometryContext context = streamGeometry.Open(); HighlightAdorner3D.DrawLine(context, keyValuePair1, keyValuePair2); HighlightAdorner3D.DrawLine(context, keyValuePair2, keyValuePair3); HighlightAdorner3D.DrawLine(context, keyValuePair3, keyValuePair4); HighlightAdorner3D.DrawLine(context, keyValuePair4, keyValuePair1); HighlightAdorner3D.DrawLine(context, keyValuePair5, keyValuePair6); HighlightAdorner3D.DrawLine(context, keyValuePair6, keyValuePair7); HighlightAdorner3D.DrawLine(context, keyValuePair7, keyValuePair8); HighlightAdorner3D.DrawLine(context, keyValuePair8, keyValuePair5); HighlightAdorner3D.DrawLine(context, keyValuePair6, keyValuePair2); HighlightAdorner3D.DrawLine(context, keyValuePair7, keyValuePair3); HighlightAdorner3D.DrawLine(context, keyValuePair5, keyValuePair1); HighlightAdorner3D.DrawLine(context, keyValuePair8, keyValuePair4); context.Close(); streamGeometry.Freeze(); drawingContext.DrawGeometry((Brush)null, pen, (System.Windows.Media.Geometry)streamGeometry); }
private bool BeginUpdate() { ICommand beginUpdateCommand = this.BeginUpdateCommand; Quaternion quaternion = Helper3D.QuaternionFromRotation3D(this.Orientation); if (beginUpdateCommand != null) { if (this.Execute(beginUpdateCommand)) { this.initialPosition = this.GetPositionFromPointer(); this.initialOrientation = quaternion; this.isCurrentlyUpdating = true; } } else { this.initialPosition = this.GetPositionFromPointer(); this.initialOrientation = quaternion; this.isCurrentlyUpdating = true; } return(this.isCurrentlyUpdating); }
/// <summary> /// plays the specified particle. /// </summary> /// <param name="id">an index of particle</param> /// <param name="position">The position of a particle</param> /// <param name="normal">The normal vector of a particle</param> /// <param name="axis">The axis vector of a particle</param> /// <returns>the plaied particle</returns> public ParticleSequence PlayParticle(int id, Vector3 position, Vector3 normal, Matrix axis) { if (particleOn == false) { return(null); } if (id == -1) { throw new ArgumentException("Cannot find particle : " + id); } ParticleSequence particle = FindFreeParticle(particleSequencesList[id]); if (particle != null) { Matrix transform; if (Math.Abs(1.0f - Vector3.Dot(normal, Vector3.Up)) < 0.0001f) { transform = Helper3D.MakeMatrixWithAt(normal, Vector3.Forward); } else { transform = Helper3D.MakeMatrixWithAt(normal, Vector3.Up); } transform.Translation = position; particle.WorldTransform = axis * transform; PlayParticle(particle); return(particle); } return(null); }
private void Page_Loaded(object sender, RoutedEventArgs e) { Material grayMaterial; SolidColorBrush solidColorBrush = new SolidColorBrush(); solidColorBrush.Color = Colors.LightGray; grayMaterial = new DiffuseMaterial(solidColorBrush); double initialAngle = 30.0; double angle = 15.0; Point3D p1 = new Point3D(0.0, 1.0, 0.0); p1 *= Helper3D.GetXRotationMatrix(initialAngle); DisplayPoint(p1, xWorkshop, grayMaterial); p1 *= Helper3D.GetXRotationMatrix(angle); DisplayPoint(p1, xWorkshop); Point3D p2 = new Point3D(1.0, 0.0, 0.0); p2 *= Helper3D.GetYRotationMatrix(initialAngle); DisplayPoint(p2, yWorkshop, grayMaterial); p2 *= Helper3D.GetYRotationMatrix(angle); DisplayPoint(p2, yWorkshop); Point3D p3 = new Point3D(1.0, 0.0, 0.0); p3 *= Helper3D.GetZRotationMatrix(initialAngle); DisplayPoint(p3, zWorkshop, grayMaterial); p3 *= Helper3D.GetZRotationMatrix(angle); DisplayPoint(p3, zWorkshop); Matrix3D mXRotation = Helper3D.GetXRotationMatrix(45.0); Matrix3D mZRotation = Helper3D.GetZRotationMatrix(45.0); MatrixTransform3D mt = new MatrixTransform3D(mXRotation * mZRotation); box.Transform = mt; }
/// <summary> /// Create the points of the polygon. /// </summary> private void CreatePoints() { if (_circumferenceSideCount < 3) { throw new ArgumentException("_circumferenceSideCount < 3"); } this.Points.Clear(); _center = new Point3D(0, 0, 0); _centered = true; double angle1 = 2 * Math.PI / _circumferenceSideCount; double angle = 0.0; for (int i = 1; i <= _circumferenceSideCount; i++) { // angle = (angle1 * i); angle = (angle1 * i) + Helper3D.DegreeToRadian(_initialAngle); Point3D p = new Point3D(); p.X = Math.Cos(angle); p.Y = Math.Sin(angle); p.Z = 0; this.Points.Add(p); } }