public void OnLeftMousePressed(Vector2 mousePos) { if (Keyboard.IsKeyDown(Key.A)) { dragMode = MouseDragMode.CameraRadiusChange; } else if (Keyboard.IsKeyDown(Key.LeftCtrl)) { dragMode = MouseDragMode.CameraRotate; } else if (Keyboard.IsKeyDown(Key.LeftAlt)) { dragMode = MouseDragMode.CameraMove; } else if (Keyboard.IsKeyDown(Key.C)) { dragMode = MouseDragMode.CursorMove; } else if (Keyboard.IsKeyDown(Key.S)) { dragMode = MouseDragMode.SelectPoints; dragStartPosition = dragActualPosition = MousePosition; } else // dragging point { CatPoint clicked; if (SelectClickedPoint(mousePos, out clicked)) { dragMode = MouseDragMode.PointMove; draggedPoint = clicked; pointPlane = new Plane(draggedPoint.Position, (draggedPoint.Position - ActiveCamera.CameraPosition).Normalized()); } } }
internal void Replace(CatPoint replacement) { if (this != replacement) { OnReplace?.Invoke(this, replacement); } }
internal CatPoint CreateHiddenCatPoint(Vector3 pos) { var point = new CatPoint(pos); hiddenPoints.Add(point); return(point); }
private void ClearScene() { CatPoint.ResetID(); Model.ResetId(); Models.Clear(); Points.Clear(); hiddenPoints.Clear(); }
internal void RemovePoint(CatPoint point) { if (!point.Removeable) { return; } point.CleanUp(); Points.Remove(point); hiddenPoints.Remove(point); }
internal CatPoint CreateCatPoint(Vector3 pos, bool addToSelected = true) { var point = new CatPoint(pos); points.Add(point); if (addToSelected) { var selected = Models.Where(x => x.IsSelected).ToList(); if (selected.Count > 0 && selected[0] is IChangeablePointCount) { var p = (IChangeablePointCount)selected[0]; p.AddPoint(point); } } return(point); }
private bool SelectClickedPoint(Vector2 position, out CatPoint model) { //Ray cameraRay = ActiveCamera.GetViewRay(position); List <ClickData> clicks = new List <ClickData>(); var cameraMatrix = ActiveCamera.ViewProjectionMatrix; var pos = ScreenSize * position; foreach (var point in GetPoints()) { if (!point.Visible) { continue; } var pt = (cameraMatrix * new Vector4(point.Position)).ToNormalizedVector3(); var dt = new Vector2(pt.X, pt.Y) * ScreenSize - pos; var distance = dt.Length(); if (distance < 8) { clicks.Add(new ClickData(pt.Z, point)); } } ClickData selected; selected.ClickedModel = null; if (clicks.Count > 0) { clicks.Sort((x, y) => y.Distance.CompareTo(x.Distance)); var clicked = clicks.First(); if (clicked.ClickedModel != null) { selected = clicked; } } if (selected.ClickedModel != null) { model = selected.ClickedModel; return(true); } model = null; return(false); }
public void OnLeftMouseReleased() { if (dragMode == MouseDragMode.SelectPoints) { var dragEnd = MousePosition; var minX = System.Math.Min(dragEnd.X, dragStartPosition.X) / ScreenSize.X; var maxX = System.Math.Max(dragEnd.X, dragStartPosition.X) / ScreenSize.X; var minY = System.Math.Min(dragEnd.Y, dragStartPosition.Y) / ScreenSize.Y; var maxY = System.Math.Max(dragEnd.Y, dragStartPosition.Y) / ScreenSize.Y; var ptsList = new List <CatPoint>(); var cameraMatrix = ActiveCamera.ViewProjectionMatrix; foreach (var catPoint in GetPoints().Where(x => x.Visible)) { var pt = (cameraMatrix * new Vector4(catPoint.Position)).ToNormalizedVector3() / 2 + 0.5f; if (pt.X < maxX && pt.X > minX && pt.Y < maxY && pt.Y > minY) { ptsList.Add(catPoint); } } if (!Keyboard.IsKeyDown(Key.LeftCtrl)) { foreach (var catPoint in Points) { catPoint.IsSelected = false; } } foreach (var catPoint in ptsList) { catPoint.IsSelected = true; } } dragMode = MouseDragMode.None; draggedPoint = null; }
public void LoadFromFile(string filename) { var p = new XMLSerializer(); Scene scene; try { scene = p.DeserializeFromFile(filename); } catch (Exception) { return; } ClearScene(); var catPoints = new List <CatPoint>(scene.Points.Length); for (int i = 0; i < scene.Points.Length; i++) { var pos = scene.Points[i].Position; var pt = CreateCatPoint(new Vector3(pos.X, pos.Y, pos.Z), false); pt.Name = scene.Points[i].Name; catPoints.Add(pt); } foreach (var bezierCurveC0 in scene.BezierCurvesC0) { var curve = new Bezier(bezierCurveC0.Points.Select(x => catPoints[x]), this) { Name = bezierCurveC0.Name }; if (bezierCurveC0.DisplayPolygonSpecified) { curve.ShowPolygon = bezierCurveC0.DisplayPolygon; } AddNewModel(curve); } foreach (var bezierCurveC2 in scene.BezierCurvesC2) { var curve = new BezierC2(bezierCurveC2.Points.Select(x => catPoints[x]), this) { Name = bezierCurveC2.Name }; if (bezierCurveC2.DisplayPolygonSpecified) { curve.ShowPolygon = bezierCurveC2.DisplayPolygon; } AddNewModel(curve); } foreach (var interpolationBezierCurveC2 in scene.InterpolationBezierCurvesC2) { var curve = new BsplineInterpolator(interpolationBezierCurveC2.Points.Select(x => catPoints[x]), this) { Name = interpolationBezierCurveC2.Name }; if (interpolationBezierCurveC2.DisplayPolygonSpecified) { curve.ShowPolygon = interpolationBezierCurveC2.DisplayPolygon; } AddNewModel(curve); } foreach (var bezierSurfaceC0 in scene.BezierSurfacesC0) { var patches = new List <Patch>(); var ptches = new Patch[bezierSurfaceC0.PatchesV, bezierSurfaceC0.PatchesU]; foreach (var bezierSurfaceC0Patch in bezierSurfaceC0.Patches) { var pts = new CatPoint[4, 4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { pts[i, j] = catPoints[bezierSurfaceC0Patch.Points[j, i]]; } } var patch = new BezierPatch(pts) { ShowPolygon = false, HeightDiv = bezierSurfaceC0Patch.SurfaceDivisionsV, WidthDiv = bezierSurfaceC0Patch.SurfaceDivisionsU, Name = bezierSurfaceC0Patch.Name, VPos = bezierSurfaceC0Patch.PatchU, UPos = bezierSurfaceC0Patch.PatchV }; AddNewModel(patch); patches.Add(patch); ptches[bezierSurfaceC0Patch.PatchV, bezierSurfaceC0Patch.PatchU] = patch; } var surfacePoints = patches.SelectMany(x => x.EnumerateCatPoints()).Distinct().ToList(); var surface = new Surface(SurfaceType.Bezier, ptches, surfacePoints, this, false, false) { Name = bezierSurfaceC0.Name, PatchesU = bezierSurfaceC0.PatchesU, PatchesV = bezierSurfaceC0.PatchesV, }; AddNewModel(surface); } foreach (var bezierSurfaceC2 in scene.BezierSurfacesC2) { var patches = new List <Patch>(); var ptches = new Patch[bezierSurfaceC2.PatchesV, bezierSurfaceC2.PatchesU]; foreach (var bezierSurfaceC2Patch in bezierSurfaceC2.Patches) { var pts = new CatPoint[4, 4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { pts[i, j] = catPoints[bezierSurfaceC2Patch.Points[j, i]]; } } var patch = new BSplinePatch(pts) { ShowPolygon = false, HeightDiv = bezierSurfaceC2Patch.SurfaceDivisionsV, WidthDiv = bezierSurfaceC2Patch.SurfaceDivisionsU, Name = bezierSurfaceC2Patch.Name, UPos = bezierSurfaceC2Patch.PatchU, VPos = bezierSurfaceC2Patch.PatchV }; AddNewModel(patch); patches.Add(patch); ptches[bezierSurfaceC2Patch.PatchV, bezierSurfaceC2Patch.PatchU] = patch; } var loopedV = true; var loopedU = true; for (int i = 0; i < 3; i++) { var pat = ptches[0, 0].GetCatPoint(i, 0); var pat2 = ptches[0, bezierSurfaceC2.PatchesU - 1].GetCatPoint(i + 1, 0); if (pat != pat2 || ptches[0, 0].GetCatPoint(i, 1) != ptches[0, bezierSurfaceC2.PatchesU - 1].GetCatPoint(i + 1, 1) || ptches[0, 0].GetCatPoint(i, 2) != ptches[0, bezierSurfaceC2.PatchesU - 1].GetCatPoint(i + 1, 2)) { loopedU = false; break; } } if (loopedU) { for (int i = 0; i < ptches.GetLength(0); i++) { if (ptches[i, 0].GetCatPoint(0, 3) != ptches[i, bezierSurfaceC2.PatchesU - 1].GetCatPoint(1, 3) || ptches[i, 0].GetCatPoint(1, 3) != ptches[i, bezierSurfaceC2.PatchesU - 1].GetCatPoint(2, 3) || ptches[i, 0].GetCatPoint(2, 3) != ptches[i, bezierSurfaceC2.PatchesU - 1].GetCatPoint(3, 3)) { loopedU = false; break; } } } for (int i = 0; i < 3; i++) { if (ptches[0, 0].GetCatPoint(0, i) != ptches[bezierSurfaceC2.PatchesV - 1, 0].GetCatPoint(0, i + 1) || ptches[0, 0].GetCatPoint(0, i) != ptches[bezierSurfaceC2.PatchesV - 1, 0].GetCatPoint(0, i + 1) || ptches[0, 0].GetCatPoint(0, i) != ptches[bezierSurfaceC2.PatchesV - 1, 0].GetCatPoint(0, i + 1)) { loopedV = false; break; } } if (loopedV) { for (int i = 0; i < ptches.GetLength(1); i++) { if (ptches[0, i].GetCatPoint(3, 0) != ptches[bezierSurfaceC2.PatchesV - 1, i].GetCatPoint(3, 1) || ptches[0, i].GetCatPoint(3, 1) != ptches[bezierSurfaceC2.PatchesV - 1, i].GetCatPoint(3, 2) || ptches[0, i].GetCatPoint(3, 2) != ptches[bezierSurfaceC2.PatchesV - 1, i].GetCatPoint(3, 3)) { loopedV = false; break; } } } if (loopedU == loopedV) { Console.WriteLine($"Surf: {bezierSurfaceC2.Name}"); } var surfacePoints = patches.SelectMany(x => x.EnumerateCatPoints()).Distinct().ToList(); var surface = new Surface(SurfaceType.BSpline, ptches, surfacePoints, this, loopedU, loopedV) { Name = bezierSurfaceC2.Name, PatchesU = bezierSurfaceC2.PatchesU, PatchesV = bezierSurfaceC2.PatchesV }; AddNewModel(surface); } }
internal PointWrapper(DataStructures.CatPoint point) { this.point = point; }