public static MidPoint ( |
||
p1 | ||
p2 | ||
Результат |
/// <summary> /// Collects data from the specified edge /// </summary> /// /// <param name="e"> /// Edge to collect data from /// </param> private void AddEdgeData(IEdge e) { EdgeGroupData ed; KeyPair key = new KeyPair(e.Vertices[0].ID, e.Vertices[1].ID); edgeGroupData.TryGetValue(key, out ed); if (ed == null) { PointF p1 = e.Vertices[0].Location; PointF p2 = e.Vertices[1].Location; ed = new EdgeGroupData(); ed.v1 = p1; ed.v2 = p2; ed.id = key; PointF mid = VectorTools.MidPoint(p1, p2); ed.middle = mid; ed.length = VectorTools.Distance(p1, p2); ed.compatibleGroups = new Dictionary <KeyPair, GroupPairData>(); //ed.edges = new HashSet<int>(); ed.edgeCount = 0; edgeGroupData.Add(key, ed); } //ed.edges.Add(e.ID); ed.edgeCount++; }
/// <summary> /// Calculates visibility coefficient of the two edges. /// </summary> /// /// <param name="ed1"> /// First edge to be used in calculation /// </param> /// /// <param name="ed2"> /// Second edge to be used in calculation /// </param> /// /// <returns> /// Compatibility coefficient ranging from 0 to 1 /// </returns> private float VisibilityCoefficient(EdgeGroupData ed1, EdgeGroupData ed2) { float c; PointF p1 = ed1.v1; PointF p2 = ed1.v2; PointF q1 = ed2.v1; PointF q2 = ed2.v2; PointF pn = new PointF(); pn.X = p1.Y - p2.Y; pn.Y = p2.X - p1.X; PointF pn1 = pn + new SizeF(p1); PointF pn2 = pn + new SizeF(p2); PointF i1 = new PointF(); PointF i2 = new PointF(); float r1 = 0, r2 = 0; if (!Intersects(q1, q2, p1, pn1, ref i1, ref r1)) { return(0); } Intersects(q1, q2, p2, pn2, ref i2, ref r2); if ((r1 < 0 && r2 < 0) || (r1 > 1 && r2 > 1)) { return(0); } PointF im = VectorTools.MidPoint(i1, i2); PointF qm = ed2.middle; float i = VectorTools.Distance(i1, i2); float m = VectorTools.Distance(qm, im); if (i == 0) { return(0); } c = 1f - 2f * m / i; if (c < 0) { return(0); } else { return(c); } }
/// <summary> /// Doubles subdivision points for an edge by adding one new subdivision point between each two /// </summary> /// /// <param name="ed"> /// Edge data that contains subdivision points to be doubled /// </param> private void DoubleSubdivisionPoints(EdgeGroupData ed) { if (subdivisionPoints == 0) //make one subdivision point { ed.k = springConstant * 2 / ed.length; if (ed.k > 0.5f) { ed.k = 0.5f; } ed.controlPoints = new PointF[1]; ed.newControlPoints = new PointF[1]; ed.controlPoints[0] = ed.middle; return; } PointF[] sPoints = ed.controlPoints; PointF[] sPointsDoubled = new PointF[subdivisionPoints * 2 + 1]; ed.newControlPoints = new PointF[subdivisionPoints * 2 + 1]; for (int i = 0; i < subdivisionPoints; i++) { sPointsDoubled[i * 2 + 1] = sPoints[i]; } for (int i = 0; i < subdivisionPoints - 1; i++) { sPointsDoubled[i * 2 + 2] = VectorTools.MidPoint(sPoints[i], sPoints[i + 1]); } sPointsDoubled[0] = VectorTools.MidPoint(ed.v1, sPoints[0]); sPointsDoubled[subdivisionPoints * 2] = VectorTools.MidPoint(sPoints[subdivisionPoints - 1], ed.v2); //ed.K = springConstant * (subdivisionPoints * 2 + 2) / ed.Length; ed.k *= 2f; if (ed.k > 0.5f) { ed.k = 0.5f; } ed.controlPoints = sPointsDoubled; }
/// <summary> /// Collects data from the specified edge. /// Used for edges that already have control points metadata. /// </summary> /// /// <param name="e"> /// Edge to collect data from /// </param> private void AddExistingData(IEdge e) { EdgeGroupData ed; KeyPair key = new KeyPair(e.Vertices[0].ID, e.Vertices[1].ID); edgeGroupData.TryGetValue(key, out ed); if (ed == null) { PointF p1 = e.Vertices[0].Location; PointF p2 = e.Vertices[1].Location; ed = new EdgeGroupData(); ed.v1 = p1; ed.v2 = p2; ed.id = key; PointF mid = VectorTools.MidPoint(p1, p2); ed.middle = mid; ed.length = VectorTools.Distance(p1, p2); ed.controlPoints = (PointF[])e.GetValue(ReservedMetadataKeys.PerEdgeIntermediateCurvePoints); if (subdivisionPoints == 0) { subdivisionPoints = ed.controlPoints.Length; } ed.newControlPoints = new PointF[subdivisionPoints]; ed.k = springConstant * (subdivisionPoints + 1) / ed.length; if (ed.k > 0.5f) { ed.k = 0.5f; } //ed.edges = new HashSet<int>(); ed.edgeCount = 0; ed.compatibleGroups = new Dictionary <KeyPair, GroupPairData>(); edgeGroupData.Add(key, ed); } //ed.edges.Add(e.ID); ed.edgeCount++; }