/// <summary> /// Inersection of plane with line /// </summary> /// <param name="line"></param> /// <param name="inter"></param> /// <returns>true if intersection exists</returns> public bool intersectLine(Geom3DLine line, Geom3DVector inter) { float q = normal.x * (origin.x - line.point.x) + normal.y * (origin.y - line.point.y) + normal.z * (origin.z - line.point.z); float d = normal.x * line.dir.x + normal.y * line.dir.y + normal.z * line.dir.z; if (d == 0) { inter.x = inter.y = inter.z = 0; return false; } float r = q / d; inter.x = line.point.x + r * line.dir.x; inter.y = line.point.y + r * line.dir.y; inter.z = line.point.z + r * line.dir.z; return true; }
/// <summary> /// Inersection of plane with line /// </summary> /// <param name="line"></param> /// <param name="inter"></param> /// <returns>true if intersection exists</returns> public bool intersectLine(Geom3DLine line, Geom3DVector inter) { float q = normal.x * (origin.x - line.point.x) + normal.y * (origin.y - line.point.y) + normal.z * (origin.z - line.point.z); float d = normal.x * line.dir.x + normal.y * line.dir.y + normal.z * line.dir.z; if (d == 0) { inter.x = inter.y = inter.z = 0; return(false); } float r = q / d; inter.x = line.point.x + r * line.dir.x; inter.y = line.point.y + r * line.dir.y; inter.z = line.point.z + r * line.dir.z; return(true); }
private ThreeDModel Picktest(int x,int y) { if (view == null) return null; // int x = Mouse.X; // int y = Mouse.Y; // Console.WriteLine("X:" + x + " Y:" + y); gl.MakeCurrent(); uint[] selectBuffer = new uint[128]; GL.SelectBuffer(128, selectBuffer); GL.RenderMode(RenderingMode.Select); SetupViewport(); GL.MatrixMode(MatrixMode.Projection); GL.PushMatrix(); GL.LoadIdentity(); int[] viewport = new int[4]; GL.GetInteger(GetPName.Viewport, viewport); Matrix4 m = GluPickMatrix(x, viewport[3] - y, 1, 1, viewport); GL.MultMatrix(ref m); //GluPerspective(45, 32 / 24, 0.1f, 100.0f); //Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, 1, 0.1f, 100.0f); GL.MultMatrix(ref view.persp); GL.MatrixMode(MatrixMode.Modelview); GL.ClearColor(Main.threeDSettings.background.BackColor); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.DepthTest); view.lookAt = Matrix4.LookAt(view.userPosition.X, view.userPosition.Y, view.userPosition.Z, view.viewCenter.X, view.viewCenter.Y, view.viewCenter.Z, 0, 0, 1.0f); // Intersection on bottom plane int window_y = (viewport[3] - y) - viewport[3]/2; double norm_y = (double)window_y/(double)(viewport[3]/2); int window_x = x - viewport[2]/2; double norm_x = (double)window_x/(double)(viewport[2]/2); float fpy = (float)(view.nearHeight * 0.5 * norm_y) * (toolParallelProjection.Checked ? 4f : 1f); float fpx = (float)(view.nearHeight * 0.5 * view.aspectRatio * norm_x) * (toolParallelProjection.Checked ? 4f : 1f); Vector4 frontPointN = (toolParallelProjection.Checked ? new Vector4(fpx, fpy, 0, 1) : new Vector4(0, 0, 0, 1)); Vector4 dirN = (toolParallelProjection.Checked ? new Vector4(0, 0, -view.nearDist, 0) : new Vector4(fpx, fpy, -view.nearDist, 0)); Matrix4 rotx = Matrix4.CreateFromAxisAngle(new Vector3(1, 0, 0), (float)(view.rotX*Math.PI/180.0)); Matrix4 rotz = Matrix4.CreateFromAxisAngle(new Vector3(0, 0, 1), (float)(view.rotZ * Math.PI / 180.0)); Matrix4 trans = Matrix4.CreateTranslation(-ps.BedLeft-ps.PrintAreaWidth * 0.5f,-ps.BedFront -ps.PrintAreaDepth * 0.5f, -0.5f * ps.PrintAreaHeight); Matrix4 ntrans = view.lookAt; ntrans = Matrix4.Mult(rotx,ntrans); ntrans = Matrix4.Mult(rotz,ntrans); ntrans = Matrix4.Mult(trans,ntrans ); ntrans = Matrix4.Invert(ntrans); Vector4 frontPoint = (toolParallelProjection.Checked ? Vector4.Transform(frontPointN,ntrans) : ntrans.Row3); Vector4 dirVec = Vector4.Transform(dirN, ntrans); pickLine = new Geom3DLine(new Geom3DVector(frontPoint.X / frontPoint.W, frontPoint.Y / frontPoint.W, frontPoint.Z / frontPoint.W), new Geom3DVector(dirVec.X , dirVec.Y , dirVec.Z ), true); dirN = new Vector4(0, 0, -view.nearDist, 0); dirVec = Vector4.Transform(dirN, ntrans); viewLine = new Geom3DLine(new Geom3DVector(frontPoint.X / frontPoint.W, frontPoint.Y / frontPoint.W, frontPoint.Z / frontPoint.W), new Geom3DVector(dirVec.X, dirVec.Y, dirVec.Z), true); viewLine.dir.normalize(); pickLine.dir.normalize(); /* Geom3DPlane plane = new Geom3DPlane(new Geom3DVector(0, 0, 0), new Geom3DVector(0, 0, 1)); Geom3DVector cross = new Geom3DVector(0, 0, 0); plane.intersectLine(pickLine, cross); Main.conn.log("Linie: " + pickLine, false, 3); Main.conn.log("Schnittpunkt: " + cross, false, 3); */ GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref view.lookAt); GL.Rotate(view.rotX, 1, 0, 0); GL.Rotate(view.rotZ, 0, 0, 1); GL.Translate(-ps.BedLeft-ps.PrintAreaWidth * 0.5f, -ps.BedFront-ps.PrintAreaDepth * 0.5f, -0.5f * ps.PrintAreaHeight); GL.InitNames(); int pos = 0; foreach (ThreeDModel model in view.models) { GL.PushName(pos++); GL.PushMatrix(); model.AnimationBefore(); GL.Translate(model.Position.x, model.Position.y, model.Position.z); GL.Rotate(model.Rotation.z, Vector3.UnitZ); GL.Rotate(model.Rotation.y, Vector3.UnitY); GL.Rotate(model.Rotation.x, Vector3.UnitX); GL.Scale(model.Scale.x, model.Scale.y, model.Scale.z); model.Paint(); model.AnimationAfter(); GL.PopMatrix(); GL.PopName(); } GL.MatrixMode(MatrixMode.Projection); GL.PopMatrix(); GL.MatrixMode(MatrixMode.Modelview); int hits = GL.RenderMode(RenderingMode.Render); ThreeDModel selected = null; if (hits > 0) { selected = view.models.ElementAt((int)selectBuffer[3]); lastDepth = selectBuffer[1]; for (int i = 1; i < hits; i++) { if (selectBuffer[4 * i + 1] < lastDepth) { lastDepth = selectBuffer[i * 4 + 1]; selected = view.models.ElementAt((int)selectBuffer[i * 4 + 3]); } } double dfac = (double)lastDepth/uint.MaxValue; dfac = -(view.farDist * view.nearDist) / (dfac * (view.farDist - view.nearDist) - view.farDist); Geom3DVector crossPlanePoint = new Geom3DVector(viewLine.dir).scale((float)dfac).add(viewLine.point); Geom3DPlane objplane = new Geom3DPlane(crossPlanePoint, viewLine.dir); objplane.intersectLine(pickLine, pickPoint); //Main.conn.log("Objekttreffer: " + pickPoint, false, 3); } //PrinterConnection.logInfo("Hits: " + hits); return selected; }
public void UpdatePickLine(int x, int y) { if (view == null) return; // Intersection on bottom plane int window_y = (Height - y) - Height / 2; double norm_y = (double)window_y / (double)(Height / 2); int window_x = x - Width / 2; double norm_x = (double)window_x / (double)(Width / 2); float fpy = (float)(view.nearHeight * 0.5 * norm_y)*(toolParallelProjection.Checked ? 4f : 1f); float fpx = (float)(view.nearHeight * 0.5 * view.aspectRatio * norm_x) * (toolParallelProjection.Checked ? 4f : 1f); Vector4 frontPointN = (toolParallelProjection.Checked ? new Vector4(fpx, fpy, 0, 1) : new Vector4(0, 0, 0, 1)); Vector4 dirN = (toolParallelProjection.Checked ? new Vector4(0, 0, -view.nearDist, 0) : new Vector4(fpx, fpy, -view.nearDist, 0)); Matrix4 rotx = Matrix4.CreateFromAxisAngle(new Vector3(1, 0, 0), (float)(view.rotX * Math.PI / 180.0)); Matrix4 rotz = Matrix4.CreateFromAxisAngle(new Vector3(0, 0, 1), (float)(view.rotZ * Math.PI / 180.0)); Matrix4 trans = Matrix4.CreateTranslation(-ps.BedLeft-ps.PrintAreaWidth * 0.5f,-ps.BedFront -ps.PrintAreaDepth * 0.5f, -0.5f * ps.PrintAreaHeight); Matrix4 ntrans = Matrix4.LookAt(view.userPosition.X, view.userPosition.Y, view.userPosition.Z, view.viewCenter.X, view.viewCenter.Y, view.viewCenter.Z, 0, 0, 1.0f); ; ntrans = Matrix4.Mult(rotx, ntrans); ntrans = Matrix4.Mult(rotz, ntrans); ntrans = Matrix4.Mult(trans, ntrans); ntrans = Matrix4.Invert(ntrans); Vector4 frontPoint = (toolParallelProjection.Checked ? Vector4.Transform(frontPointN, ntrans) : ntrans.Row3); //Vector4 frontPoint = (toolParallelProjection.Checked ? frontPointN : ntrans.Row3); Vector4 dirVec = Vector4.Transform(dirN, ntrans); pickLine = new Geom3DLine(new Geom3DVector(frontPoint.X / frontPoint.W, frontPoint.Y / frontPoint.W, frontPoint.Z / frontPoint.W), new Geom3DVector(dirVec.X, dirVec.Y, dirVec.Z), true); pickLine.dir.normalize(); /*Geom3DPlane plane = new Geom3DPlane(new Geom3DVector(0, 0, 0), new Geom3DVector(0, 0, 1)); Geom3DVector cross = new Geom3DVector(0, 0, 0); plane.intersectLine(pickLine, cross); */ }
public void UpdatePickLine(int x, int y) { // Intersection on bottom plane int window_y = (Height - y) - Height / 2; double norm_y = (double)window_y / (double)(Height / 2); int window_x = x - Width / 2; double norm_x = (double)window_x / (double)(Width / 2); float fpy = (float)(view.nearHeight * 0.5 * norm_y); float fpx = (float)(view.nearHeight * 0.5 * view.aspectRatio * norm_x); Vector4 frontPointN = new Vector4(0, 0, 0, 1); Vector4 dirN = new Vector4(fpx, fpy, -view.nearDist, 0); Matrix4 rotx = Matrix4.CreateFromAxisAngle(new Vector3(1, 0, 0), (float)(view.rotX * Math.PI / 180.0)); Matrix4 rotz = Matrix4.CreateFromAxisAngle(new Vector3(0, 0, 1), (float)(view.rotZ * Math.PI / 180.0)); Matrix4 trans = Matrix4.CreateTranslation(-ps.BedLeft-ps.PrintAreaWidth * 0.5f,-ps.BedFront -ps.PrintAreaDepth * 0.5f, -0.5f * ps.PrintAreaHeight); Matrix4 ntrans = view.lookAt; ntrans = Matrix4.Mult(rotx, ntrans); ntrans = Matrix4.Mult(rotz, ntrans); ntrans = Matrix4.Mult(trans, ntrans); ntrans = Matrix4.Invert(ntrans); Vector4 frontPoint = ntrans.Row3; Vector4 dirVec = Vector4.Transform(dirN, ntrans); pickLine = new Geom3DLine(new Geom3DVector(frontPoint.X / frontPoint.W, frontPoint.Y / frontPoint.W, frontPoint.Z / frontPoint.W), new Geom3DVector(dirVec.X, dirVec.Y, dirVec.Z), true); pickLine.dir.normalize(); /*Geom3DPlane plane = new Geom3DPlane(new Geom3DVector(0, 0, 0), new Geom3DVector(0, 0, 1)); Geom3DVector cross = new Geom3DVector(0, 0, 0); plane.intersectLine(pickLine, cross); */ }
public void UpdatePickLine(int x, int y) { if (view == null) return; // Intersection on bottom plane int window_y = (Height - y) - Height / 2; double norm_y = (double)window_y / (double)(Height / 2); int window_x = x - Width / 2; double norm_x = (double)window_x / (double)(Width / 2); float fpy = (float)(nearHeight * 0.5 * norm_y) * (toolParallelProjection.Checked ? 1f : 1f); float fpx = (float)(nearHeight * 0.5 * aspectRatio * norm_x) * (toolParallelProjection.Checked ? 1f : 1f); Vector4 frontPointN = (toolParallelProjection.Checked ? new Vector4(fpx, fpy, 0, 1) : new Vector4(0, 0, 0, 1)); Vector4 dirN = (toolParallelProjection.Checked ? new Vector4(0, 0, -nearDist, 0) : new Vector4(fpx, fpy, -nearDist, 0)); Matrix4 ntrans; Vector3 camPos = cam.CameraPosition; ntrans = Matrix4.LookAt(camPos.X, camPos.Y, camPos.Z, cam.viewCenter.X, cam.viewCenter.Y, cam.viewCenter.Z, 0, 0, 1.0f); ntrans = Matrix4.Invert(ntrans); Vector4 frontPoint = (toolParallelProjection.Checked ? Vector4.Transform(frontPointN, ntrans) : ntrans.Row3); //Vector4 frontPoint = (toolParallelProjection.Checked ? frontPointN : ntrans.Row3); Vector4 dirVec = Vector4.Transform(dirN, ntrans); pickLine = new Geom3DLine(new Geom3DVector(frontPoint.X / frontPoint.W, frontPoint.Y / frontPoint.W, frontPoint.Z / frontPoint.W), new Geom3DVector(dirVec.X, dirVec.Y, dirVec.Z), true); pickLine.dir.normalize(); /*Geom3DPlane plane = new Geom3DPlane(new Geom3DVector(0, 0, 0), new Geom3DVector(0, 0, 1)); Geom3DVector cross = new Geom3DVector(0, 0, 0); plane.intersectLine(pickLine, cross); */ }