public double TestAngle(Point pt, HitResult prevHit, Axis axis) { Ray3D ray = Ext3D.Unproject(pt, _viewpoint, _model.Transform, _inverseViewMatrix, _inverseProjectionMatrix); //Plane yz = new Plane(-1, 0, 0, prevHit.HitPoint.X); //Point3 pyz; double? d = yz.Intersects(ray); Debug.WriteLine(string.Format("second ray: {0}: distance: {1}", ray, (double)d)); //Debug.WriteLine(string.Format("second ray: {0}", ray)); double angle = 0; switch (axis) { case Axis.X: Plane3D yz = new Plane3D(new Vector3D(-1, 0, 0), prevHit.HitPoint.X); Point3D oyz = new Point3D(prevHit.HitPoint.X, 0, 0); Point3D pyz; yz.Intersect(ray, out pyz); Vector3D from = prevHit.HitPoint - oyz; Vector3D to = pyz - oyz; angle = Ext3D.AngleBetween(from, to); if (Vector3D.DotProduct(Ext3D.UnitX, Vector3D.CrossProduct(from, to)) < 0) { angle = -angle; } break; case Axis.Z: Plane3D xy = new Plane3D(new Vector3D(0, 0, -1), prevHit.HitPoint.Z); Point3D oxy = new Point3D(0, 0, prevHit.HitPoint.Z); Point3D pxy; xy.Intersect(ray, out pxy); from = prevHit.HitPoint - oxy; to = pxy - oxy; angle = Ext3D.AngleBetween(from, to); if (Vector3D.DotProduct(Ext3D.UnitZ, Vector3D.CrossProduct(from, to)) < 0) { angle = -angle; } break; case Axis.Y: Plane3D zx = new Plane3D(new Vector3D(0, -1, 0), prevHit.HitPoint.Y); Point3D ozx = new Point3D(0, prevHit.HitPoint.Y, 0); Point3D pzx; zx.Intersect(ray, out pzx); from = prevHit.HitPoint - ozx; to = pzx - ozx; angle = Ext3D.AngleBetween(from, to); if (Vector3D.DotProduct(Ext3D.UnitY, Vector3D.CrossProduct(from, to)) < 0) { angle = -angle; } break; } /* * if (angle < 2) * angle = null; * else * { * Debug.WriteLine(string.Format("axis: {0}, angle:{1}", axis, angle)); * } */ return(angle); }
public double?TestAngle(Point pt, HitResult prevHit, out Axis axis) { HitResult hitResult; bool hit = HitTest(pt, true, out hitResult); double? angle = null; axis = Axis.X; if (hit) { Vector3D from, to; angle = 0; axis = Axis.Z; Point3D oxy = new Point3D(0, 0, prevHit.HitPoint.Z); from = prevHit.HitPoint - oxy; to = new Point3D(hitResult.HitPoint.X, hitResult.HitPoint.Y, prevHit.HitPoint.Z) - oxy; double xyAngle = Ext3D.AngleBetween(from, to); angle = xyAngle; if (Vector3D.DotProduct(Ext3D.UnitZ, Vector3D.CrossProduct(from, to)) < 0) { angle = -angle; } //plane yz, where x = prevHit.X, Axis.X Point3D oyz = new Point3D(prevHit.HitPoint.X, 0, 0); from = prevHit.HitPoint - oyz; to = new Point3D(prevHit.HitPoint.X, hitResult.HitPoint.Y, hitResult.HitPoint.Z) - oyz; double yzAngle = Ext3D.AngleBetween(from, to); if (System.Math.Abs(yzAngle) > System.Math.Abs((double)angle)) { angle = yzAngle; axis = Axis.X; if (Vector3D.DotProduct(Ext3D.UnitX, Vector3D.CrossProduct(from, to)) < 0) { angle = -angle; } } //plane zx, where y = prevHit.Y, Axis.Y Point3D ozx = new Point3D(0, prevHit.HitPoint.Y, 0); from = prevHit.HitPoint - ozx; to = new Point3D(hitResult.HitPoint.X, prevHit.HitPoint.Y, hitResult.HitPoint.Z) - ozx; double zxAngle = Ext3D.AngleBetween(from, to); if (System.Math.Abs(zxAngle) > System.Math.Abs((double)angle)) { angle = zxAngle; axis = Axis.Y; if (Vector3D.DotProduct(Ext3D.UnitY, Vector3D.CrossProduct(from, to)) < 0) { angle = -angle; } } double threshold = Angle.DegreesToRadians(1.0); if (angle != null) { //Debug.WriteLine(string.Format("angle: {0}, threshold:{1}", angle, threshold)); if (System.Math.Abs((double)angle) < threshold) { //Debug.WriteLine("non null to null"); angle = null; } } //else //Debug.WriteLine("null angle"); } return(angle); }