public void FitToPoints(Vector3d[] pnts, int w, int h) { List <Vector2> vv = new List <Vector2>(); foreach (var vertex in pnts) { var p = MouseRay.Project(new Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z), ProjectionMatrix, ViewMatrix, WorldMatrix, viewport); vv.Add(p.Xy); } //prjs->xy coords var minx = vv.Min(z => z.X); var maxx = vv.Max(z => z.X); var miny = vv.Min(z => z.Y); var maxy = vv.Max(z => z.Y); var dx = (maxx - minx); var dy = (maxy - miny); var cx = dx / 2; var cy = dy / 2; var dir = CamTo - CamFrom; //center back to 3d var mr = new MouseRay(cx + minx, cy + miny, this); var v0 = mr.Start; CamFrom = v0; CamTo = CamFrom + dir; var aspect = w / (float)(h); dx /= w; dx *= OrthoWidth; dy /= h; dy *= OrthoWidth; OrthoWidth = Math.Max(dx, dy); }
private void Control_MouseWheel(object sender, MouseEventArgs e) { float zoomK = 20; var cur = Control.PointToClient(Cursor.Position); Control.MakeCurrent(); //MouseRay.UpdateMatrices(); MouseRay mr = new MouseRay(cur.X, cur.Y, Camera); //MouseRay mr0 = new MouseRay(Control.Width / 2, Control.Height / 2, Camera); var camera = Camera; if (camera.IsOrtho) { var shift = mr.Start - Camera.CamFrom; shift.Normalize(); //var old = camera.OrthoWidth / Control.Width; if (e.Delta > 0) { camera.OrthoWidth /= 1.2f; ////var pxn = new Vector2(cur.X,cur.Y)-(new Vector2(Control.Width/2,Control.Height/2)); //Camera cam2 = new Camera(); //cam2.CamFrom = camera.CamFrom; //cam2.CamTo = camera.CamTo; //cam2.CamUp = camera.CamUp; //cam2.OrthoWidth = camera.OrthoWidth; //cam2.IsOrtho = camera.IsOrtho; //cam2.UpdateMatricies(Control); //MouseRay mr2 = new MouseRay(cur.X, cur.Y, cam2); ////var a1 = pxn * camera.OrthoWidth / Control.Width; //var diff = mr.Start - mr2.Start; //shift *= diff.Length; //camera.CamFrom += shift; //camera.CamTo += shift; } else { camera.OrthoWidth *= 1.2f; } /*var pxn = new Vector2(cur.X, cur.Y) - (new Vector2(Control.Width / 2, Control.Height / 2)); * * var a1 = pxn * camera.OrthoWidth / Control.Width;*/ Camera cam2 = new Camera(); cam2.CamFrom = camera.CamFrom; cam2.CamTo = camera.CamTo; cam2.CamUp = camera.CamUp; cam2.OrthoWidth = camera.OrthoWidth; cam2.IsOrtho = camera.IsOrtho; cam2.UpdateMatricies(Control); MouseRay mr2 = new MouseRay(cur.X, cur.Y, cam2); var diff = mr.Start - mr2.Start; shift *= diff.Length; if (e.Delta > 0) { camera.CamFrom += shift; camera.CamTo += shift; } else { camera.CamFrom -= shift; camera.CamTo -= shift; } return; } if ( Control.ClientRectangle.IntersectsWith(new Rectangle(Control.PointToClient(Cursor.Position), new System.Drawing.Size(1, 1)))) { var dir = mr.Dir; dir.Normalize(); if (e.Delta > 0) { camera.CamFrom += dir * zoomK; camera.CamTo += dir * zoomK; } else { camera.CamFrom -= dir * zoomK; camera.CamTo -= dir * zoomK; } } }
public virtual void Control_MouseDown(object sender, MouseEventArgs e) { Control.MakeCurrent(); var pos = CursorPosition; startPosX = pos.X; startPosY = pos.Y; cameraFromStart = Camera.CamFrom; cameraToStart = Camera.CamTo; cameraUpStart = Camera.CamUp; if (e.Button == MouseButtons.Right) { var mr = new MouseRay(pos.X, pos.Y, Camera); var d1 = Camera.CamFrom - Camera.CamTo; //var plane1 : forw var crs1 = Vector3.Cross(cameraUpStart, d1); var z1 = Vector3.UnitZ; if (SnapModePlane) { var inter = lineIntersection(Vector3.Zero, Vector3.UnitZ, Camera.CamFrom, Camera.CamTo - Camera.CamFrom); if (inter != null) { drag = true; //var shift = Camera.CamTo - inter.Value; var dl = Camera.DirLen; bool fixedLen = false; if (fixedLen) { var shift2 = Camera.CamFrom - Camera.CamTo; Camera.CamTo = inter.Value; Camera.CamFrom = Camera.CamTo + shift2; cameraToStart = Camera.CamTo; cameraFromStart = Camera.CamFrom; } else { Camera.CamTo = inter.Value; cameraToStart = Camera.CamTo; } } } else if (SnapMode) { var inter = lineIntersection(Camera.CamTo, crs1, Vector3.Zero, Vector3.UnitX); if (inter != null) { drag = true; Camera.CamTo = inter.Value; cameraToStart = Camera.CamTo; } } else { drag = true; } lshiftcmd = lshift; } if (e.Button == MouseButtons.Left) { drag2 = true; //Camera.CamTo=Drawer.tubec } }