/// <summary> /// Calc offset to align /// </summary> /// <returns></returns> Point CalcOffsetToAlign(Matrix33 m) { double offx = 0; double offy = 0; var lt = m.Transform(0, 0); var rb = m.Transform(_bmp.PixelWidth, _bmp.PixelHeight); var dx = rb.X - lt.X; var dy = rb.Y - lt.Y; if (dx < this.ActualWidth) { var x = this.ActualWidth / 2 - dx / 2; offx = x - lt.X; } else { if (lt.X > 0) offx = -lt.X; if (rb.X < this.ActualWidth) offx = this.ActualWidth - rb.X; } if (dy < this.ActualHeight) { var y = this.ActualHeight / 2 - dy / 2; offy = y - lt.Y; } else { if (lt.Y > 0) offy = -lt.Y; if (rb.Y < this.ActualHeight) offy = this.ActualHeight - rb.Y; } return new Point(offx, offy); }
/// <summary> /// Update matrix /// </summary> /// <param name="mat">Matrix33 object</param> void UpdateMatrix(Matrix33 mat) { _mat = mat; _trans.Matrix = _mat.ToMatrix(); }
/// <summary> /// Begin matrix animation /// </summary> /// <param name="beginmat">Source matrix</param> /// <param name="endmat">Target matrix</param> void BeginMatrixAnimation( Matrix33 beginmat, Matrix33 endmat, int durationmillis) { _anibeginmat = beginmat; _aniendmat = endmat; if (_sbflick == null) { var ani = new DoubleAnimation { From = 0, To = 1, EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }, Duration = TimeSpan.FromMilliseconds(FLICK_DURATION_IN_MILLIS) }; Storyboard.SetTarget(ani, this); Storyboard.SetTargetProperty(ani, new PropertyPath(AniTimelinePropertyName)); _sbflick = new Storyboard(); _sbflick.Children.Add(ani); } var doubleani = _sbflick.Children[0] as DoubleAnimation; doubleani.Duration = TimeSpan.FromMilliseconds(durationmillis); _sbflick.Begin(); }
/// <summary> /// Pinch started /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event parameter</param> void GestureListener_PinchStarted( object sender, PinchStartedGestureEventArgs e) { _pinchdownmat = _mat; _pinchdown = _mat.Transform(e.GetPosition(_img)); }
/// <summary> /// Pinch delta /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event parameter</param> void GestureListener_PinchDelta( object sender, PinchGestureEventArgs e) { var scale = e.DistanceRatio; var mat = _pinchdownmat; mat.PostTranslate(-_pinchdown.X, -_pinchdown.Y); mat.PostScale(scale, scale); mat.PostTranslate(_pinchdown.X, _pinchdown.Y); _mat = mat; AlignPhoto(); if (_mat.Sx < _fitmat.Sx || _mat.Sy < _fitmat.Sy) { UpdateMatrix(_fitmat); } }
public Matrix33(Matrix33 m) : this() { this = m; }
/// <summary> /// Bitmap to fit /// </summary> /// <param name="bmp"></param> void BitmapToFit(BitmapSource bmp) { _img.Width = bmp.PixelWidth; _img.Height = bmp.PixelHeight; _img.Source = bmp; _mat = Matrix33 .Create() .SetRectToRect(new Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight), new Rect(0, 0, 480, 800), Stretch.Uniform); _trans.Matrix = _mat.ToMatrix(); _fitmat = _mat; }
public bool IsSame(Matrix33 rhs) { return n00 == rhs.n00 && n01 == rhs.n01 && n02 == rhs.n02 && n10 == rhs.n10 && n11 == rhs.n11 && n12 == rhs.n12 && n20 == rhs.n20 && n21 == rhs.n21 && n22 == rhs.n22; }
public static Matrix33 Interpolate( Matrix33 a, Matrix33 b, double t) { double invt = 1.0 - t; var m = Matrix33.Create(); m.n00 = (a.n00 * invt + b.n00 * t); m.n01 = (a.n01 * invt + b.n01 * t); m.n02 = (a.n02 * invt + b.n02 * t); m.n10 = (a.n10 * invt + b.n10 * t); m.n11 = (a.n11 * invt + b.n11 * t); m.n12 = (a.n12 * invt + b.n12 * t); m.n20 = (a.n20 * invt + b.n20 * t); m.n21 = (a.n21 * invt + b.n21 * t); m.n22 = (a.n22 * invt + b.n22 * t); return m; }
/// <summary> /// This guy returns the inverse of matrix. /// The code is from Ogre3d. /// </summary> /// <returns></returns> public Matrix33 Inverse() { Matrix33 inv = new Matrix33 { n00 = n11 * n22 - n12 * n21, n01 = n02 * n21 - n01 * n22, n02 = n01 * n12 - n02 * n11, n10 = n12 * n20 - n10 * n22, n11 = n00 * n22 - n02 * n20, n12 = n02 * n10 - n00 * n12, n20 = n10 * n21 - n11 * n20, n21 = n01 * n20 - n00 * n21, n22 = n00 * n11 - n01 * n10 }; double det = n00 * inv.n00 + n01 * inv.n10 + n02 * inv.n20; Debug.Assert(det >= 0.00001); return inv.MulScala(1 / det); }
public static Matrix33 Mul(Matrix33 m1, Matrix33 m2) { return new Matrix33 { n00 = m1.n00 * m2.n00 + m1.n01 * m2.n10 + m1.n02 * m2.n20, n01 = m1.n00 * m2.n01 + m1.n01 * m2.n11 + m1.n02 * m2.n21, n02 = m1.n00 * m2.n02 + m1.n01 * m2.n12 + m1.n02 * m2.n22, n10 = m1.n10 * m2.n00 + m1.n11 * m2.n10 + m1.n12 * m2.n20, n11 = m1.n10 * m2.n01 + m1.n11 * m2.n11 + m1.n12 * m2.n21, n12 = m1.n10 * m2.n02 + m1.n11 * m2.n12 + m1.n12 * m2.n22, n20 = m1.n20 * m2.n00 + m1.n21 * m2.n10 + m1.n22 * m2.n20, n21 = m1.n20 * m2.n01 + m1.n21 * m2.n11 + m1.n22 * m2.n21, n22 = m1.n20 * m2.n02 + m1.n21 * m2.n12 + m1.n22 * m2.n22 }; }
public Matrix33 Mul(Matrix33 m) { this = Mul(this, m); return this; }