Exemplo n.º 1
0
		public static Matrix3D LookAt( this Point3D position, Point3D target, Vector3D up )
		{
			var vector = (Vector3D)position - (Vector3D)target;
			vector.Normalize();
			var vector2 = Vector3D.CrossProduct( up, vector );
			vector2.Normalize();
			var vector3 = Vector3D.CrossProduct( vector, vector2 );

			var matrix = new Matrix3D();
			matrix.M11 = vector2.X;
			matrix.M12 = vector3.X;
			matrix.M13 = vector.X;
			matrix.M14 = 0f;
			matrix.M21 = vector2.Y;
			matrix.M22 = vector3.Y;
			matrix.M23 = vector.Y;
			matrix.M24 = 0f;
			matrix.M31 = vector2.Z;
			matrix.M32 = vector3.Z;
			matrix.M33 = vector.Z;
			matrix.M34 = 0f;
			matrix.OffsetX = -Vector3D.DotProduct( vector2, (Vector3D)position );
			matrix.OffsetY = -Vector3D.DotProduct( vector3, (Vector3D)position );
			matrix.OffsetZ = -Vector3D.DotProduct( vector, (Vector3D)position );
			matrix.M44 = 1f;
			return matrix;
		}
Exemplo n.º 2
0
		public Quaternion( Vector3D axisOfRotation, double angleInDegrees )
		{
			angleInDegrees = angleInDegrees % 360;
			double num2 = angleInDegrees * 0.017453292519943295;
			double length = axisOfRotation.Length;
			if ( length == 0 )
			{
				throw new InvalidOperationException( "Zero axis specified" );
			}
			Vector3D vectord = (Vector3D)( ( axisOfRotation / length ) * Math.Sin( 0.5 * num2 ) );
			this._x = vectord.X;
			this._y = vectord.Y;
			this._z = vectord.Z;
			this._w = Math.Cos( 0.5 * num2 );
			this._isNotDistinguishedIdentity = true;
		}
Exemplo n.º 3
0
		public static double AngleBetween( Vector3D vector1, Vector3D vector2 )
		{
			double num;
			vector1.Normalize();
			vector2.Normalize();
			if ( DotProduct( vector1, vector2 ) < 0 )
			{
				Vector3D vectord2 = -vector1 - vector2;
				num = 3.1415926535897931 - ( 2 * Math.Asin( vectord2.Length / 2 ) );
			}
			else
			{
				Vector3D vectord = vector1 - vector2;
				num = 2 * Math.Asin( vectord.Length / 2 );
			}
			return M3DUtil.RadiansToDegrees( num );
		}
Exemplo n.º 4
0
        /// <summary>
        /// Determine if box intersects plane
        /// </summary>
        /// <param name="plane">Plane</param>
        /// <returns>Intersection type</returns>
        /// <remarks>Taken from XNA source via Reflector</remarks>
        public PlaneIntersectionType Intersects( Plane plane )
        {
            var vector = new Vector3D();
            var vector2 = new Vector3D();

            vector2.X = ( plane.Normal.X >= 0f ) ? min.X : max.X;
            vector2.Y = ( plane.Normal.Y >= 0f ) ? min.Y : max.Y;
            vector2.Z = ( plane.Normal.Z >= 0f ) ? min.Z : max.Z;
            vector.X = ( plane.Normal.X >= 0f ) ? max.X : min.X;
            vector.Y = ( plane.Normal.Y >= 0f ) ? max.Y : min.Y;
            vector.Z = ( plane.Normal.Z >= 0f ) ? max.Z : min.Z;

            double num = ( ( plane.Normal.X * vector2.X ) + ( plane.Normal.Y * vector2.Y ) ) + ( plane.Normal.Z * vector2.Z );
            if ( ( num + plane.D ) > 0f ) return PlaneIntersectionType.Front;

            num = ( ( plane.Normal.X * vector.X ) + ( plane.Normal.Y * vector.Y ) ) + ( plane.Normal.Z * vector.Z );
            if ( ( num + plane.D ) < 0f ) return PlaneIntersectionType.Back;

            return PlaneIntersectionType.Intersecting;
        }
Exemplo n.º 5
0
		public static Vector3D Multiply( Vector3D vector, Matrix3D matrix )
		{
			return matrix.Transform( vector );
		}
Exemplo n.º 6
0
		public static Vector3D Divide( Vector3D vector, double scalar )
		{
			return (Vector3D)( vector * ( 1 / scalar ) );
		}
Exemplo n.º 7
0
		public static Rect3D Offset( Rect3D rect, Vector3D offsetVector )
		{
			rect.Offset( offsetVector._x, offsetVector._y, offsetVector._z );
			return rect;
		}
Exemplo n.º 8
0
		private void canvas_MouseButtonDown( object sender, MouseEventArgs e )
		{
			mouseDown = true;
			anchorPosition = Trackball.ProjectToTrackball( Width, Height, e.GetPosition( root ) );
		}
Exemplo n.º 9
0
		private ICollection<BSPImage> PreparePhotos( IEnumerable<string> urls )
		{
			var items = new List<BSPImage>();
			var count = urls.Count();
			var size = (int)Math.Ceiling( Math.Pow( count, 1.0 / 3 ) );
			// TODO: Properly split BSP items in tree if they cross planes; then don't need to force even-valued sizez
			if ( ( size & 1 ) == 1 ) ++size;
			var offset = -( ( (double)size - 1 ) / 2 );
			var origin = new Vector3D( offset, offset, offset );

			pending = 0;
			var positions = new Dictionary<string, string>();
			var rand = new Random();

			foreach ( var url in urls )
			{
				Point3D point;
				string key;

				do
				{
					point = new Point3D(
						rand.Next( size ),
						rand.Next( size ),
						rand.Next( size ) );

					key = point.X + "," + point.Y + "," + point.Z;
				}
				while ( positions.ContainsKey( key ) );

				positions[ key ] = url;

				var image = new BSPImage( point + origin, new Uri( url ) );
				image.Loaded += delegate { if ( Interlocked.Decrement( ref pending ) < 1 ) loading.Stop(); };
				image.Failed += delegate { if ( Interlocked.Decrement( ref pending ) < 1 ) loading.Stop(); };
				image.Click += ItemClick;
				image.WebClick += ItemWebClick;

				items.Add( image );
			}

			pending = items.Count;

			return items;
		}
Exemplo n.º 10
0
		public static Vector3D CrossProduct( Vector3D vector1, Vector3D vector2 )
		{
			Vector3D vectord;
			CrossProduct( ref vector1, ref vector2, out vectord );
			return vectord;
		}
Exemplo n.º 11
0
 /// <summary>
 /// Normalize plane
 /// </summary>
 public void Normalize()
 {
     float mag = (float)Math.Sqrt( normal.X * normal.X + normal.Y * normal.Y + normal.Z * normal.Z );
     normal /= mag;
     d /= mag;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="normal">Plane normal</param>
 /// <param name="d">Distance of plane from origin</param>
 public Plane( Vector3D normal, double d )
 {
     this.normal = normal;
     this.d = d;
 }
Exemplo n.º 13
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="normal">Plane normal</param>
 /// <param name="d">Distance of plane from origin</param>
 public Plane( Vector3D point, Vector3D normal )
     : this( normal, -Vector3D.DotProduct( normal, (Vector3D)point ) )
 {
 }
Exemplo n.º 14
0
		internal static void Subtract( ref Point3D p1, ref Point3D p2, out Vector3D result )
		{
			result._x = p1._x - p2._x;
			result._y = p1._y - p2._y;
			result._z = p1._z - p2._z;
		}
Exemplo n.º 15
0
		public static Vector3D Subtract( Point3D point1, Point3D point2 )
		{
			Vector3D result = new Vector3D();
			Subtract( ref point1, ref point2, out result );
			return result;
		}
Exemplo n.º 16
0
		public static Point3D Subtract( Point3D point, Vector3D vector )
		{
			return new Point3D( point._x - vector._x, point._y - vector._y, point._z - vector._z );
		}
Exemplo n.º 17
0
		public static Point3D Add( Point3D point, Vector3D vector )
		{
			return new Point3D( point._x + vector._x, point._y + vector._y, point._z + vector._z );
		}
Exemplo n.º 18
0
		public static double DotProduct( Vector3D vector1, Vector3D vector2 )
		{
			return DotProduct( ref vector1, ref vector2 );
		}
Exemplo n.º 19
0
		internal static double DotProduct( ref Vector3D vector1, ref Vector3D vector2 )
		{
			return ( ( ( vector1._x * vector2._x ) + ( vector1._y * vector2._y ) ) + ( vector1._z * vector2._z ) );
		}
Exemplo n.º 20
0
		public static bool Equals( Vector3D vector1, Vector3D vector2 )
		{
			if ( vector1.X.Equals( vector2.X ) && vector1.Y.Equals( vector2.Y ) )
			{
				return vector1.Z.Equals( vector2.Z );
			}
			return false;
		}
Exemplo n.º 21
0
		internal static void CrossProduct( ref Vector3D vector1, ref Vector3D vector2, out Vector3D result )
		{
			result._x = ( vector1._y * vector2._z ) - ( vector1._z * vector2._y );
			result._y = ( vector1._z * vector2._x ) - ( vector1._x * vector2._z );
			result._z = ( vector1._x * vector2._y ) - ( vector1._y * vector2._x );
		}
Exemplo n.º 22
0
		internal Rect3D( Point3D point, Vector3D vector )
			: this( point, point + vector )
		{
		}
Exemplo n.º 23
0
		public bool Equals( Vector3D value )
		{
			return Equals( this, value );
		}
Exemplo n.º 24
0
		public static Vector3D Subtract( Vector3D vector1, Vector3D vector2 )
		{
			return new Vector3D( vector1._x - vector2._x, vector1._y - vector2._y, vector1._z - vector2._z );
		}
Exemplo n.º 25
0
		public static Vector3D Add( Vector3D vector1, Vector3D vector2 )
		{
			return new Vector3D( vector1._x + vector2._x, vector1._y + vector2._y, vector1._z + vector2._z );
		}
Exemplo n.º 26
0
		public static Point3D Add( Vector3D vector, Point3D point )
		{
			return new Point3D( vector._x + point._x, vector._y + point._y, vector._z + point._z );
		}
Exemplo n.º 27
0
		private void Refresh( IEnumerable<BSPImage> items, Point3D viewpoint )
		{
			refresh = false;

			if ( needsRefresh )
			{
				if ( Width > 0 && Height > 0 ) camera.AspectRatio = Width / Height;

				needsRefresh = false;
			}

			var display = camera.CreateDisplayMatrix( Width, Height );
			var maxDistance = 0.5;

			foreach ( var item in items )
			{
				var distance = camera.GetFrustum().Distance( item.Position );
				var visual = item.Visual;

				if ( distance <= maxDistance )
				{
					visual.Opacity = 1 - ( distance / maxDistance );

					Vector3D perp = Vector3D.CrossProduct( (Vector3D)item.Position, (Vector3D)viewpoint );
					perp.Normalize();

					var m = item.Transform;
					var translate = new Vector3D( m.OffsetX, m.OffsetY, m.OffsetZ );

					var points = new Point3D[]
					{
						(Point3D)item.Position + translate,
						(Point3D)( item.Position + translate + perp )
					};

					display.Transform( ref points );

					var halfWidth = ( points[ 1 ] - points[ 0 ] ).Length / 2;
					var transform = new TransformGroup();

					transform.Children.Add( new TranslateTransform
					{
						X = -0.5,
						Y = -0.5
					} );

					transform.Children.Add( new RotateTransform
					{
						Angle = item.Angle2D
					} );

					transform.Children.Add( new ScaleTransform
					{
						ScaleX = halfWidth * 2,
						ScaleY = halfWidth * 2
					} );

					transform.Children.Add( new TranslateTransform
					{
						X = points[ 0 ].X,
						Y = points[ 0 ].Y
					} );

					visual.RenderTransform = transform;
				}
				else
				{
					visual.Opacity = 0;
				}

				visual.IsHitTestVisible = ( visual.Opacity > 0.05 );
			}
		}
Exemplo n.º 28
0
		public static Point3D Subtract( Vector3D vector, Point3D point )
		{
			return new Point3D( vector._x - point._x, vector._y - point._y, vector._z - point._z );
		}
Exemplo n.º 29
0
		private void canvas_MouseMove( object sender, MouseEventArgs e )
		{
			if ( mouseDown /*&& physics == null*/ )
			{
				if ( !mouseCaptured )
				{
					root.CaptureMouse();
					mouseCaptured = true;
				}

				Vector3D position = Trackball.ProjectToTrackball( Width, Height, e.GetPosition( root ) );

				Vector3D axis = Vector3D.CrossProduct( anchorPosition, position );
				double angle = Vector3D.AngleBetween( anchorPosition, position );

				if ( axis != new Vector3D() )
				{
					axis.Y *= -1;
					Quaternion delta = new Quaternion( axis, angle );

					camera.Rotation *= delta;
					camera.UpDirection = camera.GetRotationMatrix().Transform( new Vector3D( 0, 1, 0 ) );
				}

				anchorPosition = position;
				needsRefresh = true;
			}
		}
Exemplo n.º 30
0
		public static Vector3D Multiply( double scalar, Vector3D vector )
		{
			return new Vector3D( vector._x * scalar, vector._y * scalar, vector._z * scalar );
		}