private void move(Vector2 pos) { Vector2 tempPos = new Vector2(transform.position.x, transform.position.y); float angle = Vector2.Angle(pos - tempPos, new Vector2(0, -1)); if (pos.x - tempPos.x < 0) angle = 360 - angle; if (angle > 45 && angle < 135) { lineVector = LineVector.RIGHT; RLBody.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f); BodyHide(RLBody); } if (angle > 135 && angle < 225) { lineVector = LineVector.TOP; BodyHide(UpBody); } if (angle > 225 && angle < 315) { lineVector = LineVector.LEFT; RLBody.transform.localScale = new Vector3(-0.3f, 0.3f, 0.3f); BodyHide(RLBody); } if (angle < 45 || angle > 315) { lineVector = LineVector.BOTTOM; BodyHide(DownBody); } transform.position += new Vector3(pos.x - tempPos.x, pos.y - tempPos.y).normalized * speed * Time.deltaTime; GetComponent<UpdateOrder>().SetOrder(); if (animator != null) { animator.SetInteger("MoveLine", (int) lineVector); } }
/// <summary> /// Starts the action associated with this tool at the location given. /// </summary> /// <param name="rawLocation"> /// The raw location to start the action. /// Should be transformed by the <see cref="IView.InverseViewMatrix" /> for the /// <see cref="Controller">Controllers</see> <see cref="EscherTiler.Controllers.Controller.View" /> to get the /// location in the tiling itself. /// </param> /// <returns> /// The action that was performed, or null if no action was performed. /// </returns> public override Action StartAction(Vector2 rawLocation) { Matrix3x2 viewMatrix = Controller.View.ViewMatrix; // If there is a line selected, see if one of the control points has been selected if (_selectedLine != null) { Matrix3x2 transform = _selectedLine.EdgePart.GetLineTransform() * _selectedLine.Tile.Transform * viewMatrix; // Gets the closest point to the location Tuple <LineVector, float> closest = _selectedLine.EdgePart.Lines.SelectMany(l => l.Points) .Where(p => !p.IsFixed) .Select(p => GetDist(p, transform, rawLocation)) .OrderBy(t => t.Item2) .FirstOrDefault(); // If the closest point is within the toelrance, select it if (closest != null && closest.Item2 < (_tolerance * _tolerance)) { Debug.Assert(closest.Item1 != null, "closest.Item1 != null"); // Need the inverse transform for updating the line from a raw location Matrix3x2 inverseTransform; if (!Matrix3x2.Invert(_selectedLine.Tile.Transform, out inverseTransform)) { throw new InvalidOperationException(); } inverseTransform = Controller.View.InverseViewMatrix * inverseTransform * _selectedLine.EdgePart.GetLineTransform(true); _selectedVector = closest.Item1; return(new EditPointAction(closest.Item1, inverseTransform, this)); } } _selectedVector = null; _selectedLine = Controller.GetSelected(rawLocation, _tolerance); if (_selectedLine == null) { RemoveOption(ChangeLineOption); return(null); } ChangeLineOption.Value = _selectedLine.Line.GetType(); AddOption(ChangeLineOption); return(InstantAction.PureInstance); }
public static ConnectionLeg DrawLegFromPoint(Point FromPoint, LineVector Vector) { // draw line depending on the side of the shape. var legCoor = new LineCoordinates(FromPoint, Vector); var leg = new ConnectionLeg() { Start = FromPoint, LineCoor = legCoor, Direction = Vector.Direction }; return(leg); }
/// <summary> /// Initializes a new instance of the <see cref="EditPointAction" /> class. /// </summary> /// <param name="lineVector">The line vector of the point to edit.</param> /// <param name="inverseTransform">The inverse transform matrix.</param> /// <param name="tool">The tool.</param> public EditPointAction( [NotNull] LineVector lineVector, Matrix3x2 inverseTransform, [NotNull] EditLineTool tool) { Debug.Assert(lineVector != null, "lineVector != null"); Debug.Assert(tool != null, "tool != null"); Debug.Assert(!lineVector.IsFixed, "!lineVector.IsFixed"); _lineVector = lineVector; _inverseTransform = inverseTransform; _initial = lineVector; _tool = tool; }
private void OnMouseDown() { if (!IsIntercat()) { return; } if (!DragObject) { return; } _moseVector3 = Input.mousePosition; _dragg = false; if (!_lineDrag) { _lineDrag = gameObject.AddComponent <LineVector>(); } _lineDrag.LineWidth = 1; _lineDrag.Enabled = false; }
private void move(Vector2 pos) { Vector2 tempPos = new Vector2(transform.position.x, transform.position.y); float angle = Vector2.Angle(pos - tempPos, new Vector2(0, -1)); if (pos.x - tempPos.x < 0) { angle = 360 - angle; } if (angle > 45 && angle < 135) { lineVector = LineVector.RIGHT; RLBody.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f); BodyHide(RLBody); } if (angle > 135 && angle < 225) { lineVector = LineVector.TOP; BodyHide(UpBody); } if (angle > 225 && angle < 315) { lineVector = LineVector.LEFT; RLBody.transform.localScale = new Vector3(-0.3f, 0.3f, 0.3f); BodyHide(RLBody); } if (angle < 45 || angle > 315) { lineVector = LineVector.BOTTOM; BodyHide(DownBody); } transform.position += new Vector3(pos.x - tempPos.x, pos.y - tempPos.y).normalized *speed *Time.deltaTime; GetComponent <UpdateOrder>().SetOrder(); if (animator != null) { animator.SetInteger("MoveLine", (int)lineVector); } }
private void OnMouseUp() { if (!IsIntercat()) { return; } if (!_dragg) { OnMouseClick(); } if (!DragObject) { return; } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits = Physics.RaycastAll(ray, Mathf.Infinity); foreach (RaycastHit hit in hits) { MoveObject otherObject = hit.collider.GetComponentInParent <MoveObject>(); if (otherObject && otherObject.GetHashCode() != this.GetHashCode()) { EndDrag(otherObject); break; } } if (_lineDrag) { Destroy(_lineDrag); } _lineDrag = null; _dragg = false; }
public float Length(LineVector line) => (line.Coordinate1 - line.Coordinate2).Length();
public void ToVector2(LineVector line, out Vector2 Vector2One, out Vector2 Vector2Two) { Vector2One = line.Coordinate1; Vector2Two = line.Coordinate2; }
public MatrixImage <byte>?Invoke(CancellationToken ct = default) { if (UseOpenCV) { MatrixImage <byte> img = (MatrixImage <byte>)ImageLib.Controller.MainController.CurrentController.Storage["img"]; img = img.Clone(); Mat mat; if (img.LayerCount > 1) { img.Split(false)[1].ForEachPixelsSet(a => (byte)(a / 1.5)); img.Split(false)[2].ForEachPixelsSet(a => (byte)(a / 1.25)); mat = img.GetCVMat(ColorConversionCodes.BGR2HSV) .ExtractChannel(1); } else { mat = img.GetCVMat(); } HierarchyIndex[] indices; //mat = mat.ExtractChannel(1).AdaptiveThreshold(MaxValue, AdaptiveThresholdTypes.MeanC, ThresholdType, 3, Saturation); mat = mat.Threshold(Saturation, MaxValue, ThresholdType) .Erode(new Mat()).Dilate(new Mat()); Mat view; if (ShowSaturation) { if (img.LayerCount > 1) { view = img.GetCVMat(ColorConversionCodes.BGR2HSV); } else { view = img.GetCVMat(); } } else { view = ((MatrixImage <byte>)ImageLib.Controller.MainController.CurrentController.Storage["img"]).GetCVMat(); } Map map = (Map)MainController.CurrentController.Storage["Map"]; try { map.CasheDotDistances = true; map.Clear(); OpenCvSharp.Point[][] points = mat.FindContoursAsArray(RetrievalModes.External, ContourApproximationModes.ApproxTC89KCOS); //mat.FindContours(out points, out indices, RetrievalModes.External, ContourApproximationModes.ApproxTC89KCOS); int length = Length; for (int i = 0; i < points.Length /*&& !CancellationToken.IsCancellationRequested*/; i++) { //if(i>5000)break; if (points[i].Length < length) { continue; } var rect = OpenCvSharp.Cv2.BoundingRect(points[i]); //OpenCvSharp.Cv2.Rectangle(hsv, rect, Scalar.Black); var rrect = Cv2.MinAreaRect(points[i]); Point2f[] ps = rrect.Points(); //Point2f edge1 = ps[0] - ps[1]; //Point2f edge2 = ps[2] - ps[1]; var p0 = new PointF(ps[0].X, ps[0].Y); var p1 = new PointF(ps[1].X, ps[1].Y); var p2 = new PointF(ps[2].X, ps[2].Y); var p3 = new PointF(ps[3].X, ps[3].Y); var edge1 = LineVector.FindVector(p0, p1); var edge2 = LineVector.FindVector(p2, p1); //if (edge1.Length * 2 < edge2.Length || edge2.Length * 2 < edge1.Length) //{ // PointF dp1, dp2; // if (edge1.Length > edge2.Length) // { // var tmp = new PointF(edge2.X / 2f, edge2.Y / 2f); // //dp1 = new PointF(edge1.X / 2f, edge1.Y / 2f); // dp1 = new PointF(p0.X - tmp.X, p0.Y - tmp.Y); // dp2 = new PointF(dp1.X + edge1.X, dp1.Y + edge1.Y); // dp1 = new PointF(dp1.X + edge1.X / 10, dp1.Y + edge1.Y / 10); // dp2 = new PointF(dp2.X - edge1.X / 10, dp2.Y - edge1.Y / 10); // //dp2 = new PointF(p0.X + tmp.X, p0.Y + tmp.Y); // } // else // { // var tmp = new PointF(edge1.X / 2f, edge1.Y / 2f); // dp1 = new PointF(p0.X + tmp.X, p0.Y + tmp.Y); // dp2 = new PointF(dp1.X - edge2.X, dp1.Y - edge2.Y); // dp1 = new PointF(dp1.X - edge2.X / 10, dp1.Y - edge2.Y / 10); // dp2 = new PointF(dp2.X + edge2.X / 10, dp2.Y + edge2.Y / 10); // } // var dot1 = map.CreateDot(dp1); // var dot2 = map.CreateDot(dp2); // dot1.startConnect = dot2; // dot2.startConnect = dot1; //} //else { map.CreateDot(new System.Drawing.PointF( rect.Location.X + rect.Width / 2, rect.Location.Y + rect.Height / 2)); } if (DrawRects) { if (ShowTreshold) { mat.Line((int)ps[0].X, (int)ps[0].Y, (int)ps[1].X, (int)ps[1].Y, Scalar.AntiqueWhite); mat.Line((int)ps[1].X, (int)ps[1].Y, (int)ps[2].X, (int)ps[2].Y, Scalar.AntiqueWhite); mat.Line((int)ps[2].X, (int)ps[2].Y, (int)ps[3].X, (int)ps[3].Y, Scalar.AntiqueWhite); mat.Line((int)ps[3].X, (int)ps[3].Y, (int)ps[0].X, (int)ps[0].Y, Scalar.AntiqueWhite); } else { view.Line((int)ps[0].X, (int)ps[0].Y, (int)ps[1].X, (int)ps[1].Y, Scalar.AntiqueWhite); view.Line((int)ps[1].X, (int)ps[1].Y, (int)ps[2].X, (int)ps[2].Y, Scalar.AntiqueWhite); view.Line((int)ps[2].X, (int)ps[2].Y, (int)ps[3].X, (int)ps[3].Y, Scalar.AntiqueWhite); view.Line((int)ps[3].X, (int)ps[3].Y, (int)ps[0].X, (int)ps[0].Y, Scalar.AntiqueWhite); } } } //Debug.WriteLine(indices.Length); //Debugger.Break(); //var ret = new MatrixImage<byte>(new[]{ CurrentImage.ToByteImage(false).Split(false)[0] }, true); MatrixImage <byte> ret; if (ShowTreshold) { ret = new MatrixImage <byte>(img.Width, img.Height, 1); ret.SetCVMat(mat); } else { if (ShowSaturation) { ret = new MatrixImage <byte>(img.Width, img.Height, 1); ret.SetCVMat(view.ExtractChannel(1)); } //else if (DrawRects) //{ ret = new MatrixImage <byte>(img.Width, img.Height, 3); ret.SetCVMat(view); //} //else //{ // //ret = new MatrixImage<byte>(img.Width, img.Height, img.LayerCount); // //ret.SetCVMat(empty); // ret = img; //} } //CurrentController.SetImage(ret); return(ret); } finally { map.CasheDotDistances = true; if (UseStep) { map.BuildLineSets(cancellationToken: ct, step: StepSync.Instance); } map.BuildLineSets(); mat?.Dispose(); view?.Dispose(); } } else { if (UseStep) { Map.Instance.BuildLineSets(cancellationToken: ct, step: StepSync.Instance); } else { Map.Instance.BuildLineSets(); } return(null); } }
private static Tuple <LineVector, float> GetDist(LineVector vector, Matrix3x2 transform, Vector2 loc) => Tuple.Create(vector, Vector2.DistanceSquared(Vector2.Transform(vector, transform), loc));
/// <summary> /// Starts the action associated with this tool at the location given. /// </summary> /// <param name="rawLocation"> /// The raw location to start the action. /// Should be transformed by the <see cref="IView.InverseViewMatrix" /> for the /// <see cref="Controller">Controllers</see> <see cref="EscherTiler.Controllers.Controller.View" /> to get the /// location in the tiling itself. /// </param> /// <returns> /// The action that was performed, or null if no action was performed. /// </returns> public override Action StartAction(Vector2 rawLocation) { Matrix3x2 viewMatrix = Controller.View.ViewMatrix; // If there is a line selected, see if one of the control points has been selected if (_selectedLine != null) { Matrix3x2 transform = _selectedLine.EdgePart.GetLineTransform() * _selectedLine.Tile.Transform * viewMatrix; // Gets the closest point to the location Tuple<LineVector, float> closest = _selectedLine.EdgePart.Lines.SelectMany(l => l.Points) .Where(p => !p.IsFixed) .Select(p => GetDist(p, transform, rawLocation)) .OrderBy(t => t.Item2) .FirstOrDefault(); // If the closest point is within the toelrance, select it if (closest != null && closest.Item2 < (_tolerance * _tolerance)) { Debug.Assert(closest.Item1 != null, "closest.Item1 != null"); // Need the inverse transform for updating the line from a raw location Matrix3x2 inverseTransform; if (!Matrix3x2.Invert(_selectedLine.Tile.Transform, out inverseTransform)) throw new InvalidOperationException(); inverseTransform = Controller.View.InverseViewMatrix * inverseTransform * _selectedLine.EdgePart.GetLineTransform(true); _selectedVector = closest.Item1; return new EditPointAction(closest.Item1, inverseTransform, this); } } _selectedVector = null; _selectedLine = Controller.GetSelected(rawLocation, _tolerance); if (_selectedLine == null) { RemoveOption(ChangeLineOption); return null; } ChangeLineOption.Value = _selectedLine.Line.GetType(); AddOption(ChangeLineOption); return InstantAction.PureInstance; }
private static Tuple<LineVector, float> GetDist(LineVector vector, Matrix3x2 transform, Vector2 loc) => Tuple.Create(vector, Vector2.DistanceSquared(Vector2.Transform(vector, transform), loc));