//2维点相加 public static void Point2fAdd(ref Tuple2fT NewObj, Tuple2fT t1) { NewObj.s.X += t1.s.X; NewObj.s.Y += t1.s.Y; }
//2维点相减 public static void Point2fSub(ref Tuple2fT NewObj, Tuple2fT t1) { NewObj.s.X -= t1.s.X; NewObj.s.Y -= t1.s.Y; }
//鼠标拖动计算旋转 public void drag(Tuple2fT NewPt, ref Tuple4fT NewRot) { //新的位置 _mapToSphere(NewPt, ref EnVec); //计算旋转 //if (NewRot) { Tuple3fT Perp = new Tuple3fT(); //计算旋转轴 CMatrixMath.Vector3fCross(ref Perp, StVec, EnVec); //如果不为0 if (CMatrixMath.Vector3fLength(Perp) > CMatrixMath.Epsilon) { //记录旋转轴 NewRot.s.X = Perp.s.X; NewRot.s.Y = Perp.s.Y; NewRot.s.Z = Perp.s.Z; //在四元数中,w=cos(a/2),a为旋转的角度 NewRot.s.W = CMatrixMath.Vector3fDot(StVec, EnVec); } //是0,说明没有旋转 else { NewRot.s.X = NewRot.s.Y = NewRot.s.Z = NewRot.s.W = 0.0f; } } }
//鼠标点击 public void click(Tuple2fT NewPt) { _mapToSphere(NewPt, ref StVec); }
public void _mapToSphere(Tuple2fT NewPt, ref Tuple3fT NewVec) { Tuple2fT TempPt; float length; //复制到临时变量 TempPt = NewPt; //把长宽调整到[-1 ... 1]区间 TempPt.s.X = (TempPt.s.X * this.AdjustWidth) - 1.0f; TempPt.s.Y = 1.0f - (TempPt.s.Y * this.AdjustHeight); //计算长度的平方 length = (TempPt.s.X * TempPt.s.X) + (TempPt.s.Y * TempPt.s.Y); //如果点映射到球的外面 if (length > 1.0f) { float norm; //缩放到球上 norm = 1.0f / Convert.ToSingle(Math.Sqrt(length)); //设置z坐标为0 NewVec.s.X = TempPt.s.X * norm; NewVec.s.Y = TempPt.s.Y * norm; NewVec.s.Z = 0.0f; } //如果在球内 else { //利用半径的平方为1,求出z坐标 NewVec.s.X = TempPt.s.X; NewVec.s.Y = TempPt.s.Y; NewVec.s.Z = Convert.ToSingle(Math.Sqrt(1.0f - length)); } }
//更新鼠标状态 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; }