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; }
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; }
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 ); }
/// <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; }
public static Vector3D Multiply( Vector3D vector, Matrix3D matrix ) { return matrix.Transform( vector ); }
public static Vector3D Divide( Vector3D vector, double scalar ) { return (Vector3D)( vector * ( 1 / scalar ) ); }
public static Rect3D Offset( Rect3D rect, Vector3D offsetVector ) { rect.Offset( offsetVector._x, offsetVector._y, offsetVector._z ); return rect; }
private void canvas_MouseButtonDown( object sender, MouseEventArgs e ) { mouseDown = true; anchorPosition = Trackball.ProjectToTrackball( Width, Height, e.GetPosition( root ) ); }
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; }
public static Vector3D CrossProduct( Vector3D vector1, Vector3D vector2 ) { Vector3D vectord; CrossProduct( ref vector1, ref vector2, out vectord ); return vectord; }
/// <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; }
/// <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; }
/// <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 ) ) { }
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; }
public static Vector3D Subtract( Point3D point1, Point3D point2 ) { Vector3D result = new Vector3D(); Subtract( ref point1, ref point2, out result ); return result; }
public static Point3D Subtract( Point3D point, Vector3D vector ) { return new Point3D( point._x - vector._x, point._y - vector._y, point._z - vector._z ); }
public static Point3D Add( Point3D point, Vector3D vector ) { return new Point3D( point._x + vector._x, point._y + vector._y, point._z + vector._z ); }
public static double DotProduct( Vector3D vector1, Vector3D vector2 ) { return DotProduct( ref vector1, ref vector2 ); }
internal static double DotProduct( ref Vector3D vector1, ref Vector3D vector2 ) { return ( ( ( vector1._x * vector2._x ) + ( vector1._y * vector2._y ) ) + ( vector1._z * vector2._z ) ); }
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; }
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 ); }
internal Rect3D( Point3D point, Vector3D vector ) : this( point, point + vector ) { }
public bool Equals( Vector3D value ) { return Equals( this, value ); }
public static Vector3D Subtract( Vector3D vector1, Vector3D vector2 ) { return new Vector3D( vector1._x - vector2._x, vector1._y - vector2._y, vector1._z - vector2._z ); }
public static Vector3D Add( Vector3D vector1, Vector3D vector2 ) { return new Vector3D( vector1._x + vector2._x, vector1._y + vector2._y, vector1._z + vector2._z ); }
public static Point3D Add( Vector3D vector, Point3D point ) { return new Point3D( vector._x + point._x, vector._y + point._y, vector._z + point._z ); }
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 ); } }
public static Point3D Subtract( Vector3D vector, Point3D point ) { return new Point3D( vector._x - point._x, vector._y - point._y, vector._z - point._z ); }
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; } }
public static Vector3D Multiply( double scalar, Vector3D vector ) { return new Vector3D( vector._x * scalar, vector._y * scalar, vector._z * scalar ); }