public static ClosestPointsOnTwoLines ( Vector3 &closestPointLine1, Vector3 &closestPointLine2, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2 ) : bool | ||
closestPointLine1 | Vector3 | |
closestPointLine2 | Vector3 | |
linePoint1 | Vector3 | |
lineVec1 | Vector3 | |
linePoint2 | Vector3 | |
lineVec2 | Vector3 | |
return | bool |
/// <summary> /// Rediculously slow function to attempt to fully connect a set of nodes /// TO DO: Think of a better way to do this /// </summary> /// <param name="nodes"></param> public void GenerateIntersectionNodes(List <Edge> edges) { var directions = new List <Vector3>(); var directionStartNodes = new List <Node>(); foreach (Edge e in edges) { Vector3 dir1 = (e.Nodes[1].transform.position - e.Nodes[0].transform.position).normalized; Vector3 dir2 = (e.Nodes[0].transform.position - e.Nodes[1].transform.position).normalized; directions.Add(dir1); directions.Add(dir2); directionStartNodes.Add(e.Nodes[0]); directionStartNodes.Add(e.Nodes[1]); List <Node> neighborsToRemove = new List <Node>(); neighborsToRemove.Add(e.Nodes[1]); RemoveNeighbors(e.Nodes[0], neighborsToRemove); neighborsToRemove.Clear(); neighborsToRemove.Add(e.Nodes[0]); RemoveNeighbors(e.Nodes[1], neighborsToRemove); neighborsToRemove.Clear(); } for (int i = 0; i < directionStartNodes.Count; i++) { for (int j = 0; j < directionStartNodes.Count; j++) { if (i > j) { Vector3 closestPoint1 = new Vector3(0, 0, 0); Vector3 closestPoint2 = new Vector3(0, 0, 0); Vector3 nodeHitPosition1 = directionStartNodes[i].transform.position; Vector3 nodeHitPosition2 = directionStartNodes[j].transform.position; RaycastHit hit; // TO DO: Add Layermask Ray ray1 = new Ray(directionStartNodes[i].transform.position, directions[i]); Ray ray2 = new Ray(directionStartNodes[j].transform.position, directions[j]); if (Physics.Raycast(ray1, out hit)) { Node n = hit.transform.gameObject.GetComponent <Node>(); if (n != null) { nodeHitPosition1 = n.transform.position; } } if (Physics.Raycast(ray2, out hit)) { Node n = hit.transform.gameObject.GetComponent <Node>(); if (n != null) { nodeHitPosition2 = n.transform.position; } } //Debug.Log("Checking for intersections between " + directionStartNodes[i].name + " and " + directionStartNodes[j].name); if (Math3D.ClosestPointsOnTwoLines(out closestPoint1, out closestPoint2, directionStartNodes[i].transform.position, directions[i], directionStartNodes[j].transform.position, directions[j])) { Debug.Log("Intersect found!"); float distance2Intersect1 = Vector3.Distance(directionStartNodes[i].transform.position, closestPoint1); float distance2Node1 = Vector3.Distance(directionStartNodes[i].transform.position, nodeHitPosition1); if (distance2Intersect1 < distance2Node1) { if (!Physics.CheckSphere(closestPoint1, 0.5f)) { Debug.Log("No node found at intersect point, adding a node"); AddNode(closestPoint1); } } } } } } for (int i = 0; i < directionStartNodes.Count; i++) { RaycastHit hit; // TO DO: Add Layermask Vector3 dir = directions[i]; Ray ray = new Ray(directionStartNodes[i].transform.position, dir); if (Physics.Raycast(ray, out hit)) { Node n = hit.transform.gameObject.GetComponent <Node>(); if (n != null) { Debug.Log(n.name); AddNeighbor(directionStartNodes[i], n); } } } }
void GenerateElbow(int index, List <Vector3> vertices, List <Vector3> normals, List <int> triangles, Vector3 point1, Vector3 point2, Vector3 point3) { // generates the elbow around the area of point2, connecting the cylinders // corresponding to the segments point1-point2 and point2-point3 Vector3 offset1 = (point2 - point1).normalized * elbowRadius; Vector3 offset2 = (point3 - point2).normalized * elbowRadius; Vector3 startPoint = point2 - offset1; Vector3 endPoint = point2 + offset2; // auxiliary vectors to calculate lines parallel to the edge of each // cylinder, so the point where they meet can be the center of the elbow Vector3 perpendicularToBoth = Vector3.Cross(offset1, offset2); Vector3 startDir = Vector3.Cross(perpendicularToBoth, offset1).normalized; Vector3 endDir = Vector3.Cross(perpendicularToBoth, offset2).normalized; // calculate torus arc center as the place where two lines projecting // from the edges of each cylinder intersect Vector3 torusCenter1; Vector3 torusCenter2; Math3D.ClosestPointsOnTwoLines(out torusCenter1, out torusCenter2, startPoint, startDir, endPoint, endDir); Vector3 torusCenter = 0.5f * (torusCenter1 + torusCenter2); // calculate actual torus radius based on the calculated center of the // torus and the point where the arc starts float actualTorusRadius = (torusCenter - startPoint).magnitude; float angle = Vector3.Angle(startPoint - torusCenter, endPoint - torusCenter); float radiansPerSegment = (angle * Mathf.Deg2Rad) / elbowSegments; Vector3 lastPoint = point2 - startPoint; for (int i = 0; i <= elbowSegments; i++) { // create a coordinate system to build the circular arc // for the torus segments center positions Vector3 xAxis = (startPoint - torusCenter).normalized; Vector3 yAxis = (endPoint - torusCenter).normalized; Vector3.OrthoNormalize(ref xAxis, ref yAxis); Vector3 circleCenter = torusCenter + (actualTorusRadius * Mathf.Cos(radiansPerSegment * i) * xAxis) + (actualTorusRadius * Mathf.Sin(radiansPerSegment * i) * yAxis); Vector3 direction = circleCenter - lastPoint; lastPoint = circleCenter; if (i == elbowSegments) { // last segment should always have the same orientation // as the next segment of the pipe direction = endPoint - point2; } else if (i == 0) { // first segment should always have the same orientation // as the how the previous segmented ended direction = point2 - startPoint; } GenerateCircleAtPoint(vertices, normals, circleCenter, direction); if (i > 0) { MakeElbowTriangles(vertices, triangles, i, index); } } }