public PolygonGroupExport(PolygonGroup poly) { Name = poly.Name; Polygons = new List<PolygonExport>(poly.Children.Count); foreach (Polygon polygon in poly.Children) { Polygons.Add(new PolygonExport(polygon)); } }
public static void Clean(PolygonGroup group) { var shape = group.ToShape(); var partitioned = new Dictionary<int, List<List<Vector2>>>(); for (var i = 0; i < shape.Vertices.Count; i++) { var polygon = shape.Vertices[i]; var farseerVertices = new Vertices(polygon); if (farseerVertices.IsConvex()) { partitioned.Add(i, new List<List<Vector2>> { polygon }); continue; } var simplified = SimplifyTools.CollinearSimplify(farseerVertices); var partition = Triangulate.ConvexPartition(simplified, TriangulationAlgorithm.Bayazit); var vertices = partition.Select(verts => verts.Select(v => new Vector2(v.X, v.Y)).ToList()).ToList(); partitioned.Add(i, vertices); } var polygons = @group.Children.Cast<Polygon>().ToList(); group.Children.Clear(); var index = 0; var hash = polygons.ToDictionary(k => index++, v=>v.Name); for (var i = 0; i < partitioned.Count; i++) { var name = hash[i]; var partition = partitioned[i]; if (partition.Count == 1) { TransformPolygon(@group, partition[0], name); } else { for (var j = 0; j < partition.Count; j++) { var n = j > 0 ? name + "-" + (j + 1) : name; var p = partition[j]; TransformPolygon(@group, p, n); } } } }
public bool CheckAgainstOtherGroup(PolygonGroup otherGroup) { //if the names aren't the same we shouldn't even compare kids if (Name != otherGroup.Name) return false; //obv, if the count of poly's in the group is different, then it's different. if (Children.Count != otherGroup.Children.Count) return false; //check each poly using the same index because if the polys are in a different order then like above, it's diff for (int index = 0; index < Children.Count; index++) { Polygon child = Children[index] as Polygon; Polygon otherChild = otherGroup.Children[index] as Polygon; //check to see if they are the same polygroup if (child.Name == otherChild.Name) { if (child.Children.Count == 0 && otherChild.Children.Count == 0) { return true; } //then we compare all the children within that polygon (after doing a count check first) if (child.Children.Count != otherChild.Children.Count) { return false; } //We'll compare both points with one forloop because either way, if the points have different coords but have the same //count, then it's different anyways therefore needs user INTERVENTION (damn alcholic polyPoints...) for (int i = 0; i < child.Children.Count; i++) { PolyPoint point = child.Children[i] as PolyPoint; PolyPoint otherPoint = otherChild.Children[i] as PolyPoint; if (point.X != otherPoint.X || point.Y != otherPoint.Y) { return false; } //if it gets through even once that the points aren't the same then we should just stop //and say it's not the same therefore it needs to be reviewed. } } } return true; }
public ImageViewer() { _instance = this; _mode = Mode.Center; _paused = false; _poly = null; _polyGroup = null; _moving = null; if (!DesignMode) { _arrowNormCursor = new Cursor("Cursors/ArrowNorm.cur"); _arrowOverCursor = new Cursor("Cursors/ArrowOver.cur"); _penCursor = new Cursor("Cursors/Pen.cur"); _penOverCursor = new Cursor("Cursors/PenOver.cur"); _movingCursor = Cursors.Hand; } Timer refreshTimer = new Timer(); refreshTimer.Interval = (int)(1000 * 1 / 30.0f); refreshTimer.Tick += Refresh; refreshTimer.Start(); }
//when we hit enter in the mainwindow and we are on a pgroup we want to jump to the next frame and open the same p group public void JumpToNextImageFrame() { //if we have an polygroup selected go up to the parent imagedata and go to the next child frame and then open to the polygroup. if (_currentSelectedNode is PolygonGroup) { //Close all the previous children var parentFrame = ((_currentSelectedNode as PolygonGroup).Parent as ImageFrame); if ( parentFrame != null) { parentFrame.Expanded = false; foreach (var child in parentFrame.Children) { (child as NodeWithName).Expanded = false; (child as NodeWithName).IsSelected = false; } } //Get the index of the current Frame from the ImageData and close and deselect the imageFrame var index = (parentFrame.Parent as ImageData).Children.IndexOf(parentFrame); ((parentFrame.Parent as ImageData).Children[index] as ImageFrame).IsSelected = false; ((parentFrame.Parent as ImageData).Children[index] as ImageFrame).Expanded = false; //Check to see if there is another frame or not if (index + 1 < ((parentFrame.Parent as ImageData).Children.Count)) { ++index; //Set the viewmodellocator varialbes and Open the next Frame _viewModelLocator.ImageFrameView.Frame = ((parentFrame.Parent as ImageData).Children[index] as ImageFrame); _viewModelLocator.ImageFrameView.Frame.Expanded = true; _viewModelLocator.ImageFrameView.Polygon = null; _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true; _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false; CurrentView = _viewModelLocator.ImageFrameView; var pGroup = new PolygonGroup(); foreach (var groups in _viewModelLocator.ImageFrameView.Frame.Children) { if (groups.Name == _lastPolygonGroupName) { pGroup = groups as PolygonGroup; } } if (pGroup.Name == "New Polygon Group" && _viewModelLocator.ImageFrameView.Frame.Children.Count >0) { pGroup = _viewModelLocator.ImageFrameView.Frame.Children[0] as PolygonGroup; } else if (_viewModelLocator.ImageFrameView.Frame.Children.Count == 0) { _viewModelLocator.ImageFrameView.Frame.IsSelected = true; return; } (pGroup as PolygonGroup).Expanded = true; _viewModelLocator.ImageFrameView.PolygonGroup = pGroup as PolygonGroup; //Set the current node to the first Polygon of the group if ((pGroup as NodeWithName).Children.Count != 0) { _currentSelectedNode = (pGroup as NodeWithName).Children[0] as Polygon; ((pGroup as PolygonGroup).Children[0] as Polygon).IsSelected = true; } else { (pGroup as PolygonGroup).IsSelected = true; _currentSelectedNode = (pGroup as NodeWithName); } } //if we are about to go OVER the count of children, we should try to go to the next image entirely else { var folder = _viewModelLocator.ImageView.Image.Parent as Folder; //find the image index in the folder(since every image has to at some point be in a folder this shouldn't be a problem var imageIndex = folder.Images.IndexOf(parentFrame.Parent as ImageData); if (imageIndex + 1 < folder.Images.Count) { ++imageIndex; } var nextImage = folder.Images[imageIndex]; //right before we change image and frames, we de expand them first. parentFrame.Expanded = false; (parentFrame.Parent as ImageData).Expanded = false; //Set the viewmodellocator varialbes and Open the next Frame _viewModelLocator.ImageFrameView.Frame = nextImage.Children[0] as ImageFrame; _viewModelLocator.ImageFrameView.Frame.Expanded = true; (_viewModelLocator.ImageFrameView.Frame.Parent as ImageData).Expanded = true; _viewModelLocator.ImageFrameView.Polygon = null; _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true; _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false; CurrentView = _viewModelLocator.ImageFrameView; var pGroup = new PolygonGroup(); foreach (var groups in _viewModelLocator.ImageFrameView.Frame.Children) { if (groups.Name == _lastPolygonGroupName) { pGroup = groups as PolygonGroup; } } if (pGroup.Name == "New Polygon Group") { pGroup = _viewModelLocator.ImageFrameView.Frame.Children[0] as PolygonGroup; } (pGroup as PolygonGroup).Expanded = true; _viewModelLocator.ImageFrameView.PolygonGroup = pGroup as PolygonGroup; //Set the current node to the first Polygon of the group if ((pGroup as NodeWithName).Children.Count != 0) { _currentSelectedNode = (pGroup as NodeWithName).Children[0] as Polygon; ((pGroup as PolygonGroup).Children[0] as Polygon).IsSelected = true; } else { (pGroup as PolygonGroup).IsSelected = true; _currentSelectedNode = (pGroup as NodeWithName); } } } //pretty the same as the above just with the currentselected as the polygon itself so you don't have to go back up to the parent Polygroup else if (_currentSelectedNode is Polygon) { //Close all the previous children var parentFrame = ((_currentSelectedNode as Polygon).Parent as PolygonGroup).Parent as ImageFrame; if (parentFrame != null) { parentFrame.Expanded = false; foreach (var child in parentFrame.Children) { (child as NodeWithName).Expanded = false; (child as NodeWithName).IsSelected = false; } } var index = (parentFrame.Parent as ImageData).Children.IndexOf(parentFrame); ((parentFrame.Parent as ImageData).Children[index] as ImageFrame).IsSelected = false; ((parentFrame.Parent as ImageData).Children[index] as ImageFrame).Expanded = false; if (index + 1 < ((parentFrame.Parent as ImageData).Children.Count)) { ++index; _viewModelLocator.ImageFrameView.Frame = ((parentFrame.Parent as ImageData).Children[index] as ImageFrame); _viewModelLocator.ImageFrameView.Frame.Expanded = true; _viewModelLocator.ImageFrameView.Polygon = null; _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true; _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false; CurrentView = _viewModelLocator.ImageFrameView; if(_viewModelLocator.ImageFrameView.Frame.Children.Count >0) { var pGroup = _viewModelLocator.ImageFrameView.Frame.Children.First(t => t.Name == _lastPolygonGroupName); (pGroup as PolygonGroup).Expanded = true; //double check if there is a polygon to set onto next frame else just select the group if ((pGroup as PolygonGroup).Children.Count != 0) { ((pGroup as PolygonGroup).Children[0] as Polygon).IsSelected = true; _currentSelectedNode = (pGroup as NodeWithName).Children[0] as Polygon; } else { (pGroup as PolygonGroup).IsSelected = true; _currentSelectedNode = (pGroup as NodeWithName); } _viewModelLocator.ImageFrameView.PolygonGroup = pGroup as PolygonGroup; } else { _viewModelLocator.ImageFrameView.Frame.IsSelected = true; } } //if we are about to go OVER the count of children, we should try to go to the next image entirely else { var folder = _viewModelLocator.ImageView.Image.Parent as Folder; //find the image index in the folder(since every image has to at some point be in a folder this shouldn't be a problem var imageIndex = folder.Images.IndexOf(parentFrame.Parent as ImageData); if (imageIndex + 1 < folder.Images.Count) { ++imageIndex; } var nextImage = folder.Images[imageIndex]; //right before we change image and frames, we de expand them first. parentFrame.Expanded = false; (parentFrame.Parent as ImageData).Expanded = false; //Set the viewmodellocator varialbes and Open the next Frame _viewModelLocator.ImageFrameView.Frame = nextImage.Children[0] as ImageFrame; _viewModelLocator.ImageFrameView.Frame.Expanded = true; (_viewModelLocator.ImageFrameView.Frame.Parent as ImageData).Expanded = true; _viewModelLocator.ImageFrameView.Polygon = null; _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true; _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false; CurrentView = _viewModelLocator.ImageFrameView; var pGroup = _viewModelLocator.ImageFrameView.Frame.Children.First(t => t.Name == _lastPolygonGroupName); (pGroup as PolygonGroup).Expanded = true; _viewModelLocator.ImageFrameView.PolygonGroup = pGroup as PolygonGroup; //Set the current node to the first Polygon of the group if ((pGroup as NodeWithName).Children.Count != 0) { _currentSelectedNode = (pGroup as NodeWithName).Children[0] as Polygon; ((pGroup as PolygonGroup).Children[0] as Polygon).IsSelected = true; } else { (pGroup as PolygonGroup).IsSelected = true; _currentSelectedNode = (pGroup as NodeWithName); } } } else if (CurrentSelectedNode is ImageFrame) { //Close all the previous children var parentFrame = _currentSelectedNode as ImageFrame; if (parentFrame != null) { parentFrame.Expanded = false; foreach (var child in parentFrame.Children) { (child as NodeWithName).Expanded = false; (child as NodeWithName).IsSelected = false; } } //Close the last imageframe we had open. var index = (parentFrame.Parent as ImageData).Children.IndexOf(parentFrame); ((parentFrame.Parent as ImageData).Children[index] as ImageFrame).IsSelected = false; ((parentFrame.Parent as ImageData).Children[index] as ImageFrame).Expanded = false; //if there's frames left in the image, we'll open the next one if (index + 1 < ((parentFrame.Parent as ImageData).Children.Count)) { ++index; _viewModelLocator.ImageFrameView.Frame = ((parentFrame.Parent as ImageData).Children[index] as ImageFrame); _viewModelLocator.ImageFrameView.Frame.Expanded = true; _viewModelLocator.ImageFrameView.Polygon = null; _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true; _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false; CurrentView = _viewModelLocator.ImageFrameView; if (_viewModelLocator.ImageFrameView.Frame.Children.Count > 0) { (_viewModelLocator.ImageFrameView.Frame.Children[0] as PolygonGroup).IsSelected = true; } else { _viewModelLocator.ImageFrameView.Frame.IsSelected = true; } } else { var folder = _viewModelLocator.ImageView.Image.Parent as Folder; //find the image index in the folder(since every image has to at some point be in a folder this shouldn't be a problem var imageIndex = folder.Images.IndexOf(parentFrame.Parent as ImageData); if (imageIndex + 1 < folder.Images.Count) { ++imageIndex; } var nextImage = folder.Images[imageIndex]; //right before we change image and frames, we de expand them first. parentFrame.Expanded = false; (parentFrame.Parent as ImageData).Expanded = false; //Set the viewmodellocator varialbes and Open the next Frame _viewModelLocator.ImageFrameView.Frame = nextImage.Children[0] as ImageFrame; //expand and select it. _viewModelLocator.ImageFrameView.Frame.Expanded = true; _viewModelLocator.ImageFrameView.Frame.IsSelected = true; //expand the image itself to show the frames (_viewModelLocator.ImageFrameView.Frame.Parent as ImageData).Expanded = true; _viewModelLocator.ImageFrameView.Polygon = null; _viewModelLocator.ImageFrameView.ShowPolygonGroupTextBox = true; _viewModelLocator.ImageFrameView.ShowPolygonTextBox = false; CurrentView = _viewModelLocator.ImageFrameView; //Try to open the first polygroup if there is one if (_viewModelLocator.ImageFrameView.Frame.Children.Count > 0) { (_viewModelLocator.ImageFrameView.Frame.Children[0] as PolygonGroup).IsSelected = true; } else { _viewModelLocator.ImageFrameView.Frame.IsSelected = true; } } } }
public void ExecutePasteCommand(object o) { //Setting the data to the Polygon itself doesn't raise propertyChanged so you have to do it directly to the collection //This updates in the view automatically as well. This only changes the points within the poly and nothing else //(makes sense to me that it doesn't change the name and such but that is easy to implement as well if desired) if(_copyData is Polygon) { var clone = new Polygon().ClonePolygon(_copyData as Polygon); switch (_currentSelectedNode.Type) { case "Polygon": //set the parent of the clone to the polygongroup that you're pasting into. clone.Parent = _viewModelLocator.ImageFrameView.PolygonGroup; _viewModelLocator.ImageFrameView.Polygon.Children = clone.Children; break; case "PolygonGroup": clone.Parent = _viewModelLocator.ImageFrameView.PolygonGroup; if (_viewModelLocator.ImageFrameView.PolygonGroup.Children.Count != 0) _viewModelLocator.ImageFrameView.PolygonGroup.Children[0] = clone; else _viewModelLocator.ImageFrameView.PolygonGroup.Children.Add(clone); break; case "ImageFrame": //in the case of wanting to drop a polygon into a image frame, we'll check the Polygroup that it's from and see if there is a //group that matches, if there is, we then paste the polygon into the first spot, overwriting whatever is there. foreach (var child in _currentSelectedNode.Children) { if (child.Name == clone.Parent.Name) { clone.Parent = child; if (child.Children.Count == 0) child.Children.Add(clone); else child.Children[0] = clone; break; } } break; } } else if(_copyData is PolygonGroup) { var clone = new PolygonGroup(); foreach (var child in _copyData.Children) { var temp = new Polygon().ClonePolygon(child as Polygon); temp.Parent = clone; clone.Children.Add(temp); } clone.Name = _copyData.Name; switch (_currentSelectedNode.Type) { case "PolygonGroup": clone.Parent = _currentSelectedNode.Parent; _currentSelectedNode.Children = clone.Children; break; case "ImageFrame": //in the case of wanting to drop a polygon into a image frame, we'll check the Polygroup that it's from and see if there is a //group that matches, if there is, we then paste the polygon into the first spot, overwriting whatever is there. var target =_viewModelLocator.ImageFrameView.Frame.Children.FirstOrDefault(t => t.Name == clone.Name); if (target != null) { //rewire the parents of the children to be the target now target.Children.Clear(); foreach (var child in clone.Children) { child.Parent = target; target.Children.Add(child); } } else { clone.Parent = _viewModelLocator.ImageFrameView.Frame; _viewModelLocator.ImageFrameView.Frame.AddChild(clone); } break; } } }
public void ExecuteNewPolygonGroupCommand(object o) { var polygonGroup = new PolygonGroup(); polygonGroup.Initialize(); AddChild(polygonGroup); }
public void ExecuteAutoTraceCommand(object o) { var dialog = new AutoTraceWindow(); dialog.ShowDialog(); var vm = dialog.DataContext as AutoTraceWindowVM; if (vm.IsOk) { using (var ms = new MemoryStream(Data)) { var imageBitmap = Image.FromStream(ms); var errorBuilder = new StringBuilder(); var shape = TraceService.CreateComplexShape(imageBitmap, 20000, errorBuilder, vm.HullTolerence, vm.AlphaTolerence, vm.MultipartDetection, vm.HoleDetection); if (shape != null) { var polygonGroup = new PolygonGroup("Body"); polygonGroup.Initialize(); var count = 1; foreach (var polygon in shape.Vertices) { var poly = new Polygon() { Name = "Polygon " + count }; foreach (var point in polygon) { poly.AddChild(new PolyPoint((int)point.X, (int)point.Y)); } polygonGroup.AddChild(poly); count++; } AddChild(polygonGroup); } ms.Close(); } } }
private static void AddPlatformBoxStub(ImageFrame frame) { var platformGroup = new PolygonGroup { Name = "Platform", Parent = frame }; platformGroup.Initialize(); frame.AddChild(platformGroup); var attack = new Polygon { Name = "Polygon 1", Parent = platformGroup }; platformGroup.AddChild(attack); }
private static void AddLandingBoxStub(ImageFrame frame) { var landingGroup = new PolygonGroup { Name = "Landing", Parent = frame }; landingGroup.Initialize(); frame.AddChild(landingGroup); var attack = new Polygon { Name = "Polygon 1", Parent = landingGroup}; landingGroup.AddChild(attack); }
private static void AddDefaultFootBox(ImageFrame frame) { var footGroup = new PolygonGroup { Name = "Foot", Parent = frame }; footGroup.Initialize(); frame.AddChild(footGroup); var foot = new Polygon { Name = "Foot", Parent = footGroup }; var bottom = frame.TrimRectangle.Bottom - 1; var left = frame.TrimRectangle.Left; var right = frame.TrimRectangle.Right; var width = frame.TrimRectangle.Width; var tl = new PolyPoint(left + (int)(width * 0.25f), bottom - 2) { Parent = foot }; var tr = new PolyPoint(right - (int)(width * 0.25f), bottom - 2) { Parent = foot }; var br = new PolyPoint(right - (int)(width * 0.25f), bottom) { Parent = foot }; var bl = new PolyPoint(left + (int)(width * 0.25f), bottom) { Parent = foot }; foot.AddChild(tl); foot.AddChild(tr); foot.AddChild(br); foot.AddChild(bl); footGroup.AddChild(foot); }
private static void AddDefaultDepthBox(ImageFrame frame) { var depthGroup = new PolygonGroup { Name = "Depth", Parent = frame }; depthGroup.Initialize(); frame.AddChild(depthGroup); var depth = new Polygon { Name = "Depth", Parent = depthGroup }; var bottom = frame.TrimRectangle.Bottom - 1; var left = frame.TrimRectangle.Left; var right = frame.TrimRectangle.Right; var width = frame.TrimRectangle.Width; var defaultDepthPercentage = (int)(frame.TrimRectangle.Height * 0.125f); const float defaultWidthBorder = 0.9f; // 10% on each side = 80% var tl = new PolyPoint(left + (int)(width * defaultWidthBorder), bottom - defaultDepthPercentage) { Parent = depth }; var tr = new PolyPoint(right - (int)(width * defaultWidthBorder), bottom - defaultDepthPercentage) { Parent = depth }; var br = new PolyPoint(right - (int)(width * defaultWidthBorder), bottom) { Parent = depth }; var bl = new PolyPoint(left + (int)(width * defaultWidthBorder), bottom) { Parent = depth }; depth.AddChild(tl); depth.AddChild(tr); depth.AddChild(br); depth.AddChild(bl); depthGroup.AddChild(depth); }
private static void AddBodyTrace(ImageFrame frame) { using (var ms = new MemoryStream(frame.Data)) { var imageBitmap = Image.FromStream(ms); var errorBuilder = new StringBuilder(); var bodyGroup = new PolygonGroup { Name = "Body", Parent = frame }; bodyGroup.Initialize(); frame.AddChild(bodyGroup); var shape = TraceService.CreateSimpleShape(imageBitmap, 200, errorBuilder); if (shape == null) { frame.FailsAutoTrace = true; return; } var count = 1; foreach (var polygon in shape.Vertices) { var poly = new Polygon { Name = "Polygon " + count, Parent = bodyGroup }; foreach (var point in polygon) { var x = (int)ConvertUnits.ToDisplayUnits(point.X); var y = (int)ConvertUnits.ToDisplayUnits(point.Y); x += (int)(frame.Width * 0.5f); y += (int)(frame.Height * 0.5f); poly.AddChild(new PolyPoint(x, y) { Parent = poly }); } bodyGroup.AddChild(poly); count++; } } }
private static void AddAttackBoxStub(ImageFrame frame) { var attackGroup = new PolygonGroup { Name = "Attack", Parent = frame}; attackGroup.Initialize(); frame.AddChild(attackGroup); var attack = new Polygon { Name = "Polygon 1", Parent = attackGroup }; attackGroup.AddChild(attack); }