//3维点的长度的平方 public static float Vector3fLengthSquared( Tuple3fT NewObj) { return (NewObj.s.X * NewObj.s.X) + (NewObj.s.Y * NewObj.s.Y) + (NewObj.s.Z * NewObj.s.Z); }
//3维点的长度 public static float Vector3fLength( Tuple3fT NewObj) { return Convert.ToSingle(Math.Sqrt(Vector3fLengthSquared(NewObj))) ; }
//3维点点积 public static float Vector3fDot( Tuple3fT NewObj, Tuple3fT v1) { return (NewObj.s.X * v1.s.X) + (NewObj.s.Y * v1.s.Y) + (NewObj.s.Z * v1.s.Z); }
//3维点矢积 public static void Vector3fCross(ref Tuple3fT NewObj, Tuple3fT v1, Tuple3fT v2) { NewObj.s.X = (v1.s.Y * v2.s.Z) - (v1.s.Z * v2.s.Y); NewObj.s.Y = (v1.s.Z * v2.s.X) - (v1.s.X * v2.s.Z); NewObj.s.Z = (v1.s.X * v2.s.Y) - (v1.s.Y * v2.s.X); }
//鼠标拖动计算旋转 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 _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)); } }