//设置3x3矩阵为单位矩阵 public static void Matrix3fSetIdentity(ref Matrix3fT NewObj) { Matrix3fSetZero(ref NewObj); NewObj.s.M00 = NewObj.s.M11 = NewObj.s.M22 = 1.0f; }
//3x3矩阵相乘 public static void Matrix3fMulMatrix3f(ref Matrix3fT NewObj, Matrix3fT m1) { Matrix3fT Result = new Matrix3fT(); Result.s.M00 = (NewObj.s.M00 * m1.s.M00) + (NewObj.s.M01 * m1.s.M10) + (NewObj.s.M02 * m1.s.M20); Result.s.M01 = (NewObj.s.M00 * m1.s.M01) + (NewObj.s.M01 * m1.s.M11) + (NewObj.s.M02 * m1.s.M21); Result.s.M02 = (NewObj.s.M00 * m1.s.M02) + (NewObj.s.M01 * m1.s.M12) + (NewObj.s.M02 * m1.s.M22); Result.s.M10 = (NewObj.s.M10 * m1.s.M00) + (NewObj.s.M11 * m1.s.M10) + (NewObj.s.M12 * m1.s.M20); Result.s.M11 = (NewObj.s.M10 * m1.s.M01) + (NewObj.s.M11 * m1.s.M11) + (NewObj.s.M12 * m1.s.M21); Result.s.M12 = (NewObj.s.M10 * m1.s.M02) + (NewObj.s.M11 * m1.s.M12) + (NewObj.s.M12 * m1.s.M22); Result.s.M20 = (NewObj.s.M20 * m1.s.M00) + (NewObj.s.M21 * m1.s.M10) + (NewObj.s.M22 * m1.s.M20); Result.s.M21 = (NewObj.s.M20 * m1.s.M01) + (NewObj.s.M21 * m1.s.M11) + (NewObj.s.M22 * m1.s.M21); Result.s.M22 = (NewObj.s.M20 * m1.s.M02) + (NewObj.s.M21 * m1.s.M12) + (NewObj.s.M22 * m1.s.M22); NewObj = Result; }
//从四元数设置旋转矩阵 public static void Matrix3fSetRotationFromQuat4f(ref Matrix3fT NewObj, Tuple4fT q1) { float n, s; float xs, ys, zs; float wx, wy, wz; float xx, xy, xz; float yy, yz, zz; n = (q1.s.X * q1.s.X) + (q1.s.Y * q1.s.Y) + (q1.s.Z * q1.s.Z) + (q1.s.W * q1.s.W); s = (n > 0.0f) ? (2.0f / n) : 0.0f; xs = q1.s.X * s; ys = q1.s.Y * s; zs = q1.s.Z * s; wx = q1.s.W * xs; wy = q1.s.W * ys; wz = q1.s.W * zs; xx = q1.s.X * xs; xy = q1.s.X * ys; xz = q1.s.X * zs; yy = q1.s.Y * ys; yz = q1.s.Y * zs; zz = q1.s.Z * zs; NewObj.s.XX = 1.0f - (yy + zz); NewObj.s.YX = xy - wz; NewObj.s.ZX = xz + wy; NewObj.s.XY = xy + wz; NewObj.s.YY = 1.0f - (xx + zz); NewObj.s.ZY = yz - wx; NewObj.s.XZ = xz - wy; NewObj.s.YZ = yz + wx; NewObj.s.ZZ = 1.0f - (xx + yy); }
//设置3x3矩阵为0矩阵 public static void Matrix3fSetZero(ref Matrix3fT NewObj) { NewObj.s.M00 = NewObj.s.M01 = NewObj.s.M02 = NewObj.s.M10 = NewObj.s.M11 = NewObj.s.M12 = NewObj.s.M20 = NewObj.s.M21 = NewObj.s.M22 = 0.0f; }
public object Clone() { Matrix3fT matrx = new Matrix3fT(); matrx.s = (matr33)this.s.Clone(); return matrx; }
//从3x3矩阵变为4x4的旋转矩阵 public static void Matrix4fSetRotationScaleFromMatrix3f(ref Matrix4fT NewObj, Matrix3fT m1) { NewObj.s.XX = m1.s.XX; NewObj.s.YX = m1.s.YX; NewObj.s.ZX = m1.s.ZX; NewObj.s.XY = m1.s.XY; NewObj.s.YY = m1.s.YY; NewObj.s.ZY = m1.s.ZY; NewObj.s.XZ = m1.s.XZ; NewObj.s.YZ = m1.s.YZ; NewObj.s.ZZ = m1.s.ZZ; }
//设置旋转矩阵 public static void Matrix4fSetRotationFromMatrix3f(ref Matrix4fT NewObj, Matrix3fT m1) { float scale; scale = Matrix4fSVD(NewObj); Matrix4fSetRotationScaleFromMatrix3f(ref NewObj, m1); Matrix4fMulRotationScale(ref NewObj, scale); }
//更新鼠标状态 public void upstate() { if (!this.isZooming && this.isRClicked) { // 开始拖动 this.isZooming = true; // 设置拖动为变量为true this.LastPt = this.MousePt; this.lastZoomRate = this.zoomRate; } else if (this.isZooming) {//正在拖动 if (this.isRClicked) { //拖动 CMatrixMath.Point2fSub(ref MousePt, LastPt); this.zoomRate = this.lastZoomRate + this.MousePt.s.X * this.AdjustWidth * 2; } else { //停止拖动 this.isZooming = false; } } else if (!this.isDragging && this.isClicked) { // 如果没有拖动 this.isDragging = true; // 设置拖动为变量为true this.LastRot = this.ThisRot; this.click(this.MousePt); } else if (this.isDragging) { if (this.isClicked) { //如果按住拖动 Tuple4fT ThisQuat = new Tuple4fT(); this.drag(MousePt, ref ThisQuat); // 更新轨迹球的变量 CMatrixMath.Matrix3fSetRotationFromQuat4f(ref ThisRot, ThisQuat); // 计算旋转量 CMatrixMath.Matrix3fMulMatrix3f(ref this.ThisRot, this.LastRot); CMatrixMath.Matrix4fSetRotationFromMatrix3f(ref this.Transform, this.ThisRot); } else // 如果放开鼠标,设置拖动为false this.isDragging = false; } }
public override bool OnTouchEvent(MotionEvent evt) { if (gMitcRender.TransState || gMitcRender.RotAState) { if (evt.Action == MotionEventActions.Down) { m_lastPos.Set(evt.GetX(), evt.GetY()); mMitcRender.GetWorldPoint(m_lastPos.X, m_lastPos.Y, ref m_lastwx, ref m_lastwy, ref m_lastwz); m_curPos.X = m_lastPos.X; m_curPos.Y = m_lastPos.Y; m_curwx = m_lastwx; m_curwy = m_lastwy; m_curwz = m_lastwz; Transing = true; //gMitcRender.updateMatrix(); if (gMitcRender.RotAState) { Tuple2fT MousePt = new Tuple2fT(); MousePt.s.X = evt.GetX(); MousePt.s.Y = evt.GetY(); isDragging = true; // string strWp = String.Format("Down ThisRot Rot:({0},{1},{2})", ThisRot.s.M00, ThisRot.s.M11, ThisRot.s.M22); // Log.Debug(TAG, strWp); LastRot = (Matrix3fT)ThisRot.Clone(); // strWp = String.Format("After Down LastRot Rot:({0},{1},{2})", LastRot.s.M00, LastRot.s.M11, LastRot.s.M22); // Log.Debug(TAG, strWp); m_arcball.click(MousePt); } } else if (evt.Action == MotionEventActions.Up) { Transing = false; if (gMitcRender.RotAState) { isDragging = false; } } else if (evt.Action == MotionEventActions.Move) { m_curPos.Set(evt.GetX(), evt.GetY()); mMitcRender.GetWorldPoint(m_curPos.X, m_curPos.Y, ref m_curwx, ref m_curwy, ref m_curwz); mMitcRender.buildTransform((m_curwx - m_lastwx), (m_curwy - m_lastwy), m_curwz - m_lastwz); m_lastPos.X = m_curPos.X; m_lastPos.Y = m_curPos.Y; m_lastwx = m_curwx; m_lastwy = m_curwy; m_lastwz = m_curwz; string strWp = String.Format("1.Move This Point:({0},{1},{2})", m_curwx, m_curwy, m_curwz); Log.Debug(TAG, strWp); if (gMitcRender.RotAState && isDragging) { Tuple4fT ThisQuat = new Tuple4fT(); Tuple2fT MousePt = new Tuple2fT(); MousePt.s.X = evt.GetX(); MousePt.s.Y = evt.GetY(); m_arcball.drag(MousePt, ref ThisQuat); //string strWp = String.Format("1.Move This Rot:({0},{1},{2})", ThisRot.s.M00, ThisRot.s.M11, ThisRot.s.M22); //Log.Debug(TAG, strWp); CMatrixMath.Matrix3fSetRotationFromQuat4f(ref ThisRot, ThisQuat); //strWp = String.Format("2.Move This Rot:({0},{1},{2})", ThisRot.s.M00, ThisRot.s.M11, ThisRot.s.M22); //Log.Debug(TAG, strWp); CMatrixMath.Matrix3fMulMatrix3f(ref ThisRot, LastRot); //strWp = String.Format("3.Move This Rot:({0},{1},{2})", ThisRot.s.M00, ThisRot.s.M11, ThisRot.s.M22); //Log.Debug(TAG, strWp); //strWp = String.Format("Move Last Rot:({0},{1},{2})", LastRot.s.M00, LastRot.s.M11, LastRot.s.M22); //Log.Debug(TAG, strWp); Matrix4fT _tempTransform = mMitcRender.Transform; CMatrixMath.Matrix4fSetRotationFromMatrix3f(ref _tempTransform, ThisRot); m_arcball.upstate(); } this.RequestRender(); } return true; } else if (gMitcRender.ScaleState) { int nCnt = evt.PointerCount; if ((evt.Action & MotionEventActions.Mask) == MotionEventActions.PointerDown && 2 == nCnt)//<span style="color:#ff0000;">2��ʾ������ָ</span> { m_lastPos.Set(evt.GetX(evt.GetPointerId(0)), evt.GetY(evt.GetPointerId(0))); m_curPos.Set(evt.GetX(evt.GetPointerId(nCnt - 1)), evt.GetY(evt.GetPointerId(nCnt - 1))); string strWp = String.Format("2 Down Point:({0}),({1})", m_lastPos.ToString(), m_curPos.ToString()); Log.Debug(TAG, strWp); mOldCounts = 2; mScaleFactor = 1.0f; //gMitcRender.updateMatrix(); } else if ((evt.Action & MotionEventActions.Mask) == MotionEventActions.PointerUp && 2 == nCnt) { m_lastPos.Set(evt.GetX(evt.GetPointerId(0)), evt.GetY(evt.GetPointerId(0))); m_curPos.Set(evt.GetX(evt.GetPointerId(nCnt - 1)), evt.GetY(evt.GetPointerId(nCnt - 1))); string strWp = String.Format("2 Up Point:({0}),({1})", m_lastPos.ToString(), m_curPos.ToString()); Log.Debug(TAG, strWp); mOldCounts = 0; mScaleFactor = 1.0f; } else if (evt.Action == MotionEventActions.Move && 2 == nCnt) { float lastlength = GetLength(m_lastPos, m_curPos); m_lastPos.Set(evt.GetX(evt.GetPointerId(0)), evt.GetY(evt.GetPointerId(0))); m_curPos.Set(evt.GetX(evt.GetPointerId(nCnt - 1)), evt.GetY(evt.GetPointerId(nCnt - 1))); float curlength = GetLength(m_lastPos, m_curPos); mScaleFactor = Math.Abs(lastlength) < 1.0e-6 ? 1.0f : (curlength / lastlength); mMitcRender.buildScale(mScaleFactor); this.RequestRender(); string strWp = String.Format("2 Move Point:({0}),({1})", m_lastPos.ToString(), m_curPos.ToString()); Log.Debug(TAG, strWp); } } return true; }