Beispiel #1
0
		public static Quaternion CreateQuaternion( this Matrix3D matrix )
		{
			double num8 = ( matrix.M11 + matrix.M22 ) + matrix.M33;
			Quaternion quaternion = new Quaternion();
			if ( num8 > 0f )
			{
				float num = (float)Math.Sqrt( (double)( num8 + 1f ) );
				quaternion.W = num * 0.5f;
				num = 0.5f / num;
				quaternion.X = ( matrix.M23 - matrix.M32 ) * num;
				quaternion.Y = ( matrix.M31 - matrix.M13 ) * num;
				quaternion.Z = ( matrix.M12 - matrix.M21 ) * num;
				return quaternion;
			}
			if ( ( matrix.M11 >= matrix.M22 ) && ( matrix.M11 >= matrix.M33 ) )
			{
				float num7 = (float)Math.Sqrt( (double)( ( ( 1f + matrix.M11 ) - matrix.M22 ) - matrix.M33 ) );
				float num4 = 0.5f / num7;
				quaternion.X = 0.5f * num7;
				quaternion.Y = ( matrix.M12 + matrix.M21 ) * num4;
				quaternion.Z = ( matrix.M13 + matrix.M31 ) * num4;
				quaternion.W = ( matrix.M23 - matrix.M32 ) * num4;
				return quaternion;
			}
			if ( matrix.M22 > matrix.M33 )
			{
				float num6 = (float)Math.Sqrt( (double)( ( ( 1f + matrix.M22 ) - matrix.M11 ) - matrix.M33 ) );
				float num3 = 0.5f / num6;
				quaternion.X = ( matrix.M21 + matrix.M12 ) * num3;
				quaternion.Y = 0.5f * num6;
				quaternion.Z = ( matrix.M32 + matrix.M23 ) * num3;
				quaternion.W = ( matrix.M31 - matrix.M13 ) * num3;
				return quaternion;
			}
			float num5 = (float)Math.Sqrt( (double)( ( ( 1f + matrix.M33 ) - matrix.M11 ) - matrix.M22 ) );
			float num2 = 0.5f / num5;
			quaternion.X = ( matrix.M31 + matrix.M13 ) * num2;
			quaternion.Y = ( matrix.M32 + matrix.M23 ) * num2;
			quaternion.Z = 0.5f * num5;
			quaternion.W = ( matrix.M12 - matrix.M21 ) * num2;
			return quaternion;
		}
		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;
			}
		}
		private void SelectItem( BSPImage image )
		{
			if ( selected == image )
			{
				image.Selected = false;
				image.RefreshState();
				selected = null;
				to = from;
				fromZ = camera.Position.Z;
				toZ = -10;
				slerp = 1;
				jumpAngle = 0;
			}
			else
			{
				if ( selected != null )
				{
					var oldImage = (BSPImage)selected;
					oldImage.Selected = false;
					oldImage.RefreshState();
				}

				image.Selected = true;
				image.Visited = true;
				image.RefreshState();
				selected = image;

				Point3D center = image.Position;
				Vector3D dir = (Vector3D)center;
				dir.Normalize();

				Point3D viewpoint = center + ( dir * image.ViewDistance );

				var axis = Vector3D.CrossProduct( (Vector3D)camera.Position, (Vector3D)viewpoint );
				var angle = Vector3D.AngleBetween( (Vector3D)camera.Position, (Vector3D)viewpoint );

				to = ( axis != new Vector3D() )
					? new Quaternion( axis, angle )
					: new Quaternion( new Vector3D( 0, 1, 0 ), angle );

				to.Normalize();

				toZ = -( (Vector3D)viewpoint ).Length;

				slerp = 1;
				jumpAngle = Vector3D.AngleBetween(
					(Vector3D)camera.GetRotationMatrix().Transform( camera.Position ),
					(Vector3D)viewpoint );
			}
		}
		private void ItemClick( object sender, EventArgs e )
		{
			if ( !mouseCaptured )
			{
				fromZ = camera.Position.Z;
				from = camera.Rotation;
				from.Normalize();

				SelectItem( (BSPImage)sender );
			}
		}
Beispiel #5
0
/*		public static Quaternion Parse( string source )
		{
			Quaternion identity;
			IFormatProvider cultureInfo = CultureInfo.GetCultureInfo( "en-us" );
			TokenizerHelper helper = new TokenizerHelper( source, cultureInfo );
			string str = helper.NextTokenRequired();
			if ( str == "Identity" )
			{
				identity = Identity;
			}
			else
			{
				identity = new Quaternion( Convert.ToDouble( str, cultureInfo ), Convert.ToDouble( helper.NextTokenRequired(), cultureInfo ), Convert.ToDouble( helper.NextTokenRequired(), cultureInfo ), Convert.ToDouble( helper.NextTokenRequired(), cultureInfo ) );
			}
			helper.LastTokenRequired();
			return identity;
		}

		public override string ToString()
		{
			return this.ConvertToString( null, null );
		}

		public string ToString( IFormatProvider provider )
		{
			return this.ConvertToString( null, provider );
		}

		string IFormattable.ToString( string format, IFormatProvider provider )
		{
			return this.ConvertToString( format, provider );
		}

		internal string ConvertToString( string format, IFormatProvider provider )
		{
			if ( this.IsIdentity )
			{
				return "Identity";
			}
			char numericListSeparator = TokenizerHelper.GetNumericListSeparator( provider );
			return string.Format( provider, "{1:" + format + "}{0}{2:" + format + "}{0}{3:" + format + "}{0}{4:" + format + "}", new object[] { numericListSeparator, this._x, this._y, this._z, this._w } );
		}
*/
		static Quaternion()
		{
			c_identityHashCode = GetIdentityHashCode();
			s_identity = GetIdentity();
		}
Beispiel #6
0
		public static bool Equals( Quaternion quaternion1, Quaternion quaternion2 )
		{
			if ( quaternion1.IsDistinguishedIdentity || quaternion2.IsDistinguishedIdentity )
			{
				return ( quaternion1.IsIdentity == quaternion2.IsIdentity );
			}
			if ( ( quaternion1.X.Equals( quaternion2.X ) && quaternion1.Y.Equals( quaternion2.Y ) ) && quaternion1.Z.Equals( quaternion2.Z ) )
			{
				return quaternion1.W.Equals( quaternion2.W );
			}
			return false;
		}
Beispiel #7
0
		public bool Equals( Quaternion value )
		{
			return Equals( this, value );
		}
Beispiel #8
0
		public static Quaternion Slerp( Quaternion from, Quaternion to, double t, bool useShortestPath )
		{
			double num2;
			double num3;
			if ( from.IsDistinguishedIdentity )
			{
				from._w = 1;
			}
			if ( to.IsDistinguishedIdentity )
			{
				to._w = 1;
			}
			double num4 = from.Length();
			double num9 = to.Length();
			from.Scale( 1 / num4 );
			to.Scale( 1 / num9 );
			double d = ( ( ( from._x * to._x ) + ( from._y * to._y ) ) + ( from._z * to._z ) ) + ( from._w * to._w );
			if ( useShortestPath )
			{
				if ( d < 0 )
				{
					d = -d;
					to._x = -to._x;
					to._y = -to._y;
					to._z = -to._z;
					to._w = -to._w;
				}
			}
			else if ( d < -1 )
			{
				d = -1;
			}
			if ( d > 1 )
			{
				d = 1;
			}
			if ( d > 0.999999 )
			{
				num3 = 1 - t;
				num2 = t;
			}
			else if ( d < -0.9999999999 )
			{
				to = new Quaternion( -from.Y, from.X, -from.W, from.Z );
				double num8 = t * 3.1415926535897931;
				num3 = Math.Cos( num8 );
				num2 = Math.Sin( num8 );
			}
			else
			{
				double num7 = Math.Acos( d );
				double num6 = Math.Sqrt( 1 - ( d * d ) );
				num3 = Math.Sin( ( 1 - t ) * num7 ) / num6;
				num2 = Math.Sin( t * num7 ) / num6;
			}
			double num5 = num4 * Math.Pow( num9 / num4, t );
			num3 *= num5;
			num2 *= num5;
			return new Quaternion( ( num3 * from._x ) + ( num2 * to._x ), ( num3 * from._y ) + ( num2 * to._y ), ( num3 * from._z ) + ( num2 * to._z ), ( num3 * from._w ) + ( num2 * to._w ) );
		}
Beispiel #9
0
		public static Quaternion Slerp( Quaternion from, Quaternion to, double t )
		{
			return Slerp( from, to, t, true );
		}
Beispiel #10
0
		public static Quaternion Multiply( Quaternion left, Quaternion right )
		{
			return ( left * right );
		}
Beispiel #11
0
		public static Quaternion Subtract( Quaternion left, Quaternion right )
		{
			return ( left - right );
		}
Beispiel #12
0
		public static Quaternion Add( Quaternion left, Quaternion right )
		{
			return ( left + right );
		}