/// <summary> /// Collects data from the specified edge /// </summary> /// /// <param name="e"> /// Edge to collect data from /// </param> private void AddEdgeData(TEdge e) { EdgeGroupData ed; var key = new KeyPair(e.Source.ID, e.Target.ID); edgeGroupData.TryGetValue(key, out ed); if (ed == null) { var p1 = VertexPositions[e.Source]; // e.Vertices[0].Location; var p2 = VertexPositions[e.Target]; //e.Vertices[1].Location; ed = new EdgeGroupData(); ed.v1 = p1; ed.v2 = p2; ed.id = key; var 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; var p1 = ed1.v1; var p2 = ed1.v2; var q1 = ed2.v1; var q2 = ed2.v2; var pn = new Point(); pn.X = p1.Y - p2.Y; pn.Y = p2.X - p1.X; var pn1 = VectorTools.Plus(pn, p1); var pn2 = VectorTools.Plus(pn, p2); var i1 = new Point(); var i2 = new Point(); 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); } var im = VectorTools.MidPoint(i1, i2); var qm = ed2.middle; var i = VectorTools.Distance(i1, i2); var 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 Point[1]; ed.newControlPoints = new Point[1]; ed.controlPoints[0] = ed.middle; return; } var sPoints = ed.controlPoints; var sPointsDoubled = new Point[subdivisionPoints * 2 + 1]; ed.newControlPoints = new Point[subdivisionPoints * 2 + 1]; for (var i = 0; i < subdivisionPoints; i++) { sPointsDoubled[i * 2 + 1] = sPoints[i]; } for (var 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(TEdge e) { EdgeGroupData ed; var key = new KeyPair(e.Source.ID, e.Target.ID); edgeGroupData.TryGetValue(key, out ed); if (ed == null) { var p1 = VertexPositions[e.Source]; // e.Vertices[0].Location; var p2 = VertexPositions[e.Target]; //e.Vertices[1].Location; ed = new EdgeGroupData(); ed.v1 = p1; ed.v2 = p2; ed.id = key; var mid = VectorTools.MidPoint(p1, p2); ed.middle = mid; ed.length = VectorTools.Distance(p1, p2); ed.controlPoints = e.RoutingPoints; //e.GetValue(ReservedMetadataKeys.PerEdgeIntermediateCurvePoints); if (subdivisionPoints == 0) { subdivisionPoints = ed.controlPoints.Length; } ed.newControlPoints = new Point[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++; }