/* Connect a Vertex to a Vertex. */ public static void Connect(Vertex src, Vertex dest) { if (src == dest) { // Handle self connection. Vertex v = src; GameObject link = new GameObject(v.name + "->" + v.name); Undo.RegisterCreatedObjectUndo(link, "Created " + "self-edge " + v.name); link.transform.position = v.transform.position + 1.5f * v.radius * Vector3.right; Edge e = link.AddComponent(GetType(v.settings.edgeClass)) as Edge; e.fromVertex = v; e.toVertex = v; v.AddEdge(e); } else { // Handle directed connection from src to dest. Vertex v_a = src; Vertex v_b = dest; GameObject link = new GameObject(v_a.name + "->" + v_b.name); Undo.RegisterCreatedObjectUndo(link, "Created " + "edge " + v_a.name + "->" + v_b.name); Vector3 relDistance = v_b.transform.position - v_a.transform.position; link.transform.position = v_a.transform.position + relDistance.normalized * relDistance.magnitude/2f; Edge e = link.AddComponent(GetType(v_a.settings.edgeClass)) as Edge; e.fromVertex = v_a; e.toVertex = v_b; v_a.AddEdge(e); } }
void ConstructVertex(Vertex v, Vector3 position, Vector3 scale, Quaternion rotation, int count, GameObject parentObject, GameObject rootObject) { vertexCount++; if (vertexCount > VERTEX_COUNT_MAX) { Debug.Log("Constructed more than max vertex count. Stopping."); return; } GameObject o; o = Object.Instantiate(v.gameObject, position, rotation) as GameObject; o.name = v.name; o.transform.localScale = Vector3.Scale(scale, v.transform.localScale); if (connectJoints && o.hingeJoint && parentObject && parentObject.rigidbody != null) { o.hingeJoint.connectedBody = parentObject.rigidbody; } if (stripVertexComponents) { Destroy(o.GetComponent<Vertex>()); } if (rootObject != null) o.transform.parent = rootObject.transform; foreach (Edge e in v.fromEdges) { if (e == null || (v == e.toVertex && count > e.count)) continue; ConstructVertex(e.toVertex, position + rotation * Vector3.Scale(scale, (e.transform.position - v.transform.position)), Vector3.Scale(scale, e.transform.localScale), rotation * e.transform.rotation * e.toVertex.transform.rotation, // If we recurse, we keep count; otherwise, start at 1. v == e.toVertex ? count + 1 : 1, o, rootObject); } }
void RenderVertex(Vertex v, Vector3 position, Vector3 scale, Quaternion rotation, int count) { vertexCount++; if (vertexCount > VERTEX_COUNT_MAX) { Debug.Log("Constructed more than max vertex count. Stopping."); return; } Vertex.DrawLocalCube(position, rotation, Vector3.Scale(scale, v.transform.localScale), v.settings.vertexColor); foreach (Edge e in v.fromEdges) { if (e == null || (v == e.toVertex && count > e.count)) continue; RenderVertex(e.toVertex, position + rotation * Vector3.Scale(scale, (e.transform.position - v.transform.position)), Vector3.Scale(scale, e.transform.localScale), rotation * e.transform.rotation * e.toVertex.transform.rotation, // If we recurse, we keep count; otherwise, start at 1. v == e.toVertex ? count + 1 : 1); } }
public static bool IsConnectedTo(this Vertex a, Vertex b) { foreach (Edge e in a.fromEdges) { if (e.toVertex == b) { return true; } } return false; }