public static Mesh GetCylinderFaceMesh(Vector3 center1, Vector3 center2, Vector3 normal, float radius) { int pieces = 48; float angle = 2 * Mathf.PI / pieces; Vector3[] vertices = new Vector3[pieces * 2]; int[] triangles = new int[6 * pieces]; Vector3 startPos = MHelperFunctions.RandomPointInCircle(center1, normal, radius); Vector3 pos = startPos; for (int i = 0; i < 2 * pieces; i++) { vertices[i] = pos; pos = (i == pieces - 1? startPos + center2 - center1: MHelperFunctions.CalcRotate(pos - center1, normal, angle) + center1); } for (int i = 0; i < pieces; i++) { triangles[6 * i] = i; triangles[6 * i + 1] = i + pieces; triangles[6 * i + 2] = pieces + (i + 1) % pieces; triangles[6 * i + 3] = i; triangles[6 * i + 4] = pieces + (i + 1) % pieces; triangles[6 * i + 5] = (i + 1) % pieces; } Mesh mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = triangles; return(mesh); }
public float CalcDistance(Vector3 point) { Vector3 projectionPoint = MHelperFunctions.PointProjectionInFace(point, normal, sortedPoints[0].position); Vector3 rotateAxis; float rotateAngle; MHelperFunctions.CalcRotateAxisAndAngle(out rotateAxis, out rotateAngle, normal, new Vector3(0, 0, 1)); List <Vector3> rotatePoints = new List <Vector3>(); foreach (MPoint p in sortedPoints) { rotatePoints.Add(MHelperFunctions.CalcRotate(p.position, rotateAxis, rotateAngle)); } projectionPoint = MHelperFunctions.CalcRotate(projectionPoint, rotateAxis, rotateAngle); if (MHelperFunctions.InPolygon(projectionPoint, rotatePoints)) { return(MHelperFunctions.DistanceP2F(point, normal, sortedPoints[0].position)); } else { float min = float.MaxValue; foreach (MLinearEdge edge in edgeList) { min = Mathf.Min(min, edge.CalcDistance(point)); } return(min); } }
public Vector3 SpecialPointFind(Vector3 point) { int index = 0; float min = float.MaxValue; float temp; Vector3 p1, p2, p3; int count = mesh.triangles.Length / 3; for (int i = 0; i < count; i++) { if (!boundingboxes[i].Contains(point, MDefinitions.POINT_PRECISION)) { continue; } p1 = mesh.vertices[mesh.triangles[3 * i]]; p2 = mesh.vertices[mesh.triangles[3 * i + 1]]; p3 = mesh.vertices[mesh.triangles[3 * i + 2]]; if ((temp = MHelperFunctions.DistanceP2T(point, p1, p2, p3)) < min) { min = temp; index = i; } } p1 = mesh.vertices[mesh.triangles[3 * index]]; p2 = mesh.vertices[mesh.triangles[3 * index + 1]]; p3 = mesh.vertices[mesh.triangles[3 * index + 2]]; Vector3 normal = Vector3.Cross(p2 - p1, p3 - p1).normalized; return(MHelperFunctions.PointProjectionInFace(point, normal, p1)); }
private void UpdateStretch() { Vector3 pos = obj.worldToLocalMatrix.MultiplyPoint(sceneManager.rightControllerPosition); MPoint a = activeEdges[0].start; MPoint b = activeEdges[0].end; MPoint c = activeEdges[1].end; float width = Vector3.Distance(a.position, b.position); Vector3 p = MHelperFunctions.VectorL2P(pos, activeEdges[0].direction, a.position); p = ReviseLength(p, width); c.SetPosition(p + b.position); if (activeFace != null && activeFace.IsValid()) { activeFace.Render(obj.localToWorldMatrix); foreach (MLinearEdge edge in activeEdges) { edge.Render(obj.localToWorldMatrix); } a.Render(obj.localToWorldMatrix); b.Render(obj.localToWorldMatrix); c.Render(obj.localToWorldMatrix); activeTextMesh.GetComponentInChildren <TextMesh>().text = "1:" + (p.magnitude / width).ToString(); activeTextMesh.transform.position = sceneManager.rightControllerPosition + MDefinitions.DEFAULT_ACTIVE_TEXT_OFFSET; if (sceneManager.camera != null) { activeTextMesh.transform.rotation = Quaternion.LookRotation((sceneManager.camera.transform.position - activeTextMesh.transform.position) * -1, Vector3.up); } } else { activeTextMesh.GetComponentInChildren <TextMesh>().text = ""; } }
public float CalcDistance(Vector3 point) { float d1 = Vector3.Dot(bottom.center.position - top.position, point - top.position); if (MHelperFunctions.FloatZero(d1) <= 0) { return(Vector3.Distance(point, top.position)); } float d2 = Vector3.Dot(bottom.center.position - top.position, bottom.center.position - top.position); if (d1 >= d2) { return(bottom.CalcDistance(point)); } Vector3 v = MHelperFunctions.PointProjectionInLine(point, bottom.center.position - top.position, top.position); float dis = Vector3.Distance(point, v); if (dis < MDefinitions.FLOAT_PRECISION) { return(Vector3.Distance(v, top.position) * bottom.radius / generatrix); } float h = Vector3.Distance(top.position, bottom.center.position); return(Mathf.Abs(dis - Vector3.Distance(v, top.position) * bottom.radius / h) * h / generatrix); }
public static Mesh GetConeFaceMesh(Vector3 bottom, Vector3 top, Vector3 normal, float radius) { int pieces = 48; float angle = 2 * Mathf.PI / pieces; Vector3[] vertices = new Vector3[pieces + 1]; int[] triangles = new int[3 * pieces]; Vector3 pos = MHelperFunctions.RandomPointInCircle(bottom, normal, radius); for (int i = 0; i < pieces; i++) { vertices[i] = pos; pos = MHelperFunctions.CalcRotate(pos - bottom, normal, angle) + bottom; } vertices[pieces] = top; for (int i = 0; i < pieces; i++) { triangles[3 * i] = pieces; triangles[3 * i + 1] = i; triangles[3 * i + 2] = (i + 1) % pieces; } Mesh mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = triangles; return(mesh); }
public Vector3 SpecialPointFind(Vector3 point) { Vector3 v = MHelperFunctions.PointProjectionInLine(point, bottom.center.position - top.position, top.position); float h = Vector3.Distance(top.position, bottom.center.position); return((point - v).normalized * Vector3.Distance(top.position, v) / h * bottom.radius + v); }
public float CalcDistance(Vector3 point) { Vector3 projectionPoint = MHelperFunctions.PointProjectionInFace(point, normal, points[0]); Vector3 rotateAxis; float rotateAngle; MHelperFunctions.CalcRotateAxisAndAngle(out rotateAxis, out rotateAngle, normal, new Vector3(0, 0, 1)); List <Vector3> rotatePoints = new List <Vector3>(); foreach (Vector3 p in points) { rotatePoints.Add(MHelperFunctions.CalcRotate(p, rotateAxis, rotateAngle)); } projectionPoint = MHelperFunctions.CalcRotate(projectionPoint, rotateAxis, rotateAngle); if (MHelperFunctions.InPolygon(projectionPoint, rotatePoints)) { return(MHelperFunctions.DistanceP2F(point, normal, points[0])); } else { float min = float.MaxValue; int count = points.Count; for (int i = 0; i < count; i++) { min = Mathf.Min(min, MHelperFunctions.DistanceP2S(point, points[i], points[(i + 1) % count])); } return(min); } }
public static Mesh GetCircleFaceMesh(Vector3 center, Vector3 normal, float radius) { int pieces = 48; float angle = 2 * Mathf.PI / pieces; Vector3[] vertices = new Vector3[pieces]; Vector3[] normals = new Vector3[pieces]; int[] triangles = new int[3 * (pieces - 2)]; Vector3 pos = MHelperFunctions.RandomPointInCircle(center, normal, radius); for (int i = 0; i < pieces; i++) { vertices[i] = pos; normals[i] = normal; pos = MHelperFunctions.CalcRotate(pos - center, normal, angle) + center; } for (int i = 0; i < pieces - 2; i++) { triangles[3 * i] = 0; triangles[3 * i + 1] = i + 1; triangles[3 * i + 2] = i + 2; } Mesh mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = normals; return(mesh); }
public Vector3 SpecialPointFind(Vector3 point) { Vector3 p = MHelperFunctions.PointProjectionInLine(point, direction, start.position); float ratio = Vector3.Distance(p, start.position) / Vector3.Distance(end.position, start.position); if (Mathf.Abs(ratio - 0.5f) <= MDefinitions.AUTO_REVISE_FACTOR) // 线段中点 { return((end.position - start.position) * 0.5f + start.position); } else if (Mathf.Abs(ratio - 1.0f / 3) <= MDefinitions.AUTO_REVISE_FACTOR) // 1/3点 { return((end.position - start.position) / 3 + start.position); } else if (Mathf.Abs(ratio - 2.0f / 3) <= MDefinitions.AUTO_REVISE_FACTOR) // 2/3点 { return((end.position - start.position) * 2 / 3 + start.position); } else if (Mathf.Abs(ratio - 0.25f) <= MDefinitions.AUTO_REVISE_FACTOR) // 1/4点 { return((end.position - start.position) * 0.25f + start.position); } else if (Mathf.Abs(ratio - 0.75f) <= MDefinitions.AUTO_REVISE_FACTOR) // 3/4点 { return((end.position - start.position) * 0.75f + start.position); } else { return(p); } }
public static Mesh GetLineMesh(Vector3 startPosition, Vector3 endPosition, float radius) { Vector3 normal = (endPosition - startPosition).normalized; float angle = Mathf.PI / 6; Vector3[] _Vertices = new Vector3[24]; Vector3 lastPos = MHelperFunctions.RandomPointInCircle(startPosition, normal, radius); _Vertices[0] = lastPos; for (int i = 1; i < 12; i++) { Vector3 curPos = MHelperFunctions.CalcRotate(lastPos - startPosition, normal, angle) + startPosition; _Vertices[i] = curPos; lastPos = curPos; } lastPos = _Vertices[0] + (endPosition - startPosition); _Vertices[12] = lastPos; for (int i = 13; i < 24; i++) { Vector3 curPos = MHelperFunctions.CalcRotate(lastPos - endPosition, normal, angle) + endPosition; _Vertices[i] = curPos; lastPos = curPos; } //生成所有面 int[] _Triangles = new int[72] { 0, 1, 12, 1, 13, 12, 1, 2, 13, 2, 14, 13, 2, 3, 14, 3, 15, 14, 3, 4, 15, 4, 16, 15, 4, 5, 16, 5, 17, 16, 5, 6, 17, 6, 18, 17, 6, 7, 18, 7, 19, 18, 7, 8, 19, 8, 20, 19, 8, 9, 20, 9, 21, 20, 9, 10, 21, 10, 22, 21, 10, 11, 22, 11, 23, 22, 11, 0, 23, 0, 12, 23 }; Mesh mesh = new Mesh(); mesh.vertices = _Vertices; mesh.triangles = _Triangles; return(mesh); }
public bool IsValid() { return(top.IsValid() && bottom.IsValid() && !top.center.Equals(bottom.center) && MHelperFunctions.FloatEqual(top.radius, bottom.radius) && MHelperFunctions.Parallel(top.normal, bottom.normal) && MHelperFunctions.Parallel(top.center.position - bottom.center.position, top.normal)); }
public float CalcDistance(Vector3 point) { float h = MHelperFunctions.DistanceP2F(point, normal, center.position); Vector3 p = MHelperFunctions.PointProjectionInFace(point, normal, center.position); float r = Vector3.Distance(p, center.position); r = Mathf.Abs(r - radius); return(Mathf.Sqrt(r * r + h * h)); }
public float CalcDistance(Vector3 point) { float min = float.MaxValue; int count = points.Count - 1; for (int i = 0; i < count; i++) { min = Mathf.Min(min, MHelperFunctions.DistanceP2S(point, points[i], points[i + 1])); } return(min); }
private void CalcSurface() { surface = 0; int count = sortedPoints.Count; Vector3 v = sortedPoints[0].position; for (int i = 1; i < count - 1; i++) { surface += MHelperFunctions.TriangleSurface(v, sortedPoints[i].position, sortedPoints[i + 1].position); } }
private void CalcSurface() { surface = 0; int count = points.Count; Vector3 v = points[0]; for (int i = 1; i < count - 1; i++) { surface += MHelperFunctions.TriangleSurface(v, points[i], points[i + 1]); } }
public Vector3 GetProjection(Vector3 target, Vector3 assistant) { Vector3 v = MHelperFunctions.PointProjectionInLine(target, bottom.center.position - top.center.position, top.center.position); if (Vector3.Distance(target, v) < MDefinitions.FLOAT_PRECISION) { Vector3 p = MHelperFunctions.PointProjectionInFace(assistant, top.normal, v); return((p - v).normalized * top.radius + v); } return((target - v).normalized * top.radius + v); }
// 线圈上任意一点 public static Vector3 RandomPointInCircle(Vector3 center, Vector3 normal, float radius) { while (true) { Vector3 dir = new Vector3(Random.value, Random.value, Random.value); if (!MHelperFunctions.Parallel(dir, normal)) { return((dir - Vector3.Dot(dir, normal) * normal).normalized * radius + center); } } }
private void InitMesh() { mesh = MPrefab.GetSphereMesh(); if (!MHelperFunctions.FloatEqual(radius, 0.5f)) { Vector3[] vertices = new Vector3[mesh.vertices.Length]; for (int i = 0; i < mesh.vertices.Length; i++) { vertices[i] = mesh.vertices[i] * radius * 2; } mesh.vertices = vertices; } }
private void CalcSurface() { surface = 0; int count = mesh.triangles.Length / 3; for (int i = 0; i < count; i++) { Vector3 p1 = mesh.vertices[mesh.triangles[3 * i]]; Vector3 p2 = mesh.vertices[mesh.triangles[3 * i + 1]]; Vector3 p3 = mesh.vertices[mesh.triangles[3 * i + 2]]; surface += MHelperFunctions.TriangleSurface(p1, p2, p3); } }
private void InitBoundingBoxes() { Vector3 p1, p2, p3; int count = mesh.triangles.Length / 3; boundingboxes = new AABB[count]; for (int i = 0; i < count; i++) { p1 = mesh.vertices[mesh.triangles[3 * i]]; p2 = mesh.vertices[mesh.triangles[3 * i + 1]]; p3 = mesh.vertices[mesh.triangles[3 * i + 2]]; boundingboxes[i] = new AABB(MHelperFunctions.Min(p1, p2, p3), MHelperFunctions.Max(p1, p2, p3)); } }
public static void AddValToDictionary(Dictionary <Vector3, List <KeyValuePair <Vector3, int> > > dictionary, Vector3 key, Vector3 val, int index) { List <KeyValuePair <Vector3, int> > list; if (MHelperFunctions.BlurTryGetValue(dictionary, key, out list)) { list.Add(new KeyValuePair <Vector3, int>(val, index)); } else { dictionary.Add(key, new List <KeyValuePair <Vector3, int> > { new KeyValuePair <Vector3, int>(val, index) }); } }
public float CalcDistance(Vector3 point) { Vector3 p = MHelperFunctions.PointProjectionInFace(point, circle.normal, circle.center.position); float r = Vector3.Distance(p, circle.center.position); float h = MHelperFunctions.DistanceP2F(point, circle.normal, circle.center.position); if (r <= circle.radius) { return(h); } else { r = Mathf.Abs(r - circle.radius); return(Mathf.Sqrt(r * r + h * h)); } }
public Vector3 GetProjection(Vector3 target, Vector3 assistant) { Vector3 v = MHelperFunctions.PointProjectionInLine(target, bottom.center.position - top.position, top.position); Vector3 p; if (Vector3.Distance(target, v) < MDefinitions.FLOAT_PRECISION) { p = MHelperFunctions.PointProjectionInFace(assistant, bottom.normal, bottom.center.position); } else { p = MHelperFunctions.PointProjectionInFace(target, bottom.normal, bottom.center.position); } p = (p - bottom.center.position).normalized * bottom.radius + bottom.center.position; return(MHelperFunctions.PointProjectionInLine(target, p - top.position, top.position)); }
public Vector3 SpecialPointFind(Vector3 point) { float min = float.MaxValue; int index = 0; float temp; int count = points.Count - 1; for (int i = 0; i < count; i++) { if ((temp = MHelperFunctions.DistanceP2S(point, points[i], points[i + 1])) < min) { min = temp; index = i; } } return(MHelperFunctions.PointProjectionInLine(point, points[index + 1] - points[index], points[index])); }
public float CalcDistance(Vector3 point) { float min = float.MaxValue; int count = mesh.triangles.Length / 3; for (int i = 0; i < count; i++) { if (!boundingboxes[i].Contains(point, MDefinitions.POINT_PRECISION)) { continue; } Vector3 p1 = mesh.vertices[mesh.triangles[3 * i]]; Vector3 p2 = mesh.vertices[mesh.triangles[3 * i + 1]]; Vector3 p3 = mesh.vertices[mesh.triangles[3 * i + 2]]; min = Mathf.Min(min, MHelperFunctions.DistanceP2T(point, p1, p2, p3)); } return(min); }
// 计算点到线段的距离 public static float DistanceP2S(Vector3 point, Vector3 start, Vector3 end) { float d1 = Vector3.Dot(end - start, point - start); if (MHelperFunctions.FloatZero(d1) <= 0) { return(Vector3.Distance(point, start)); } float d2 = Vector3.Dot(end - start, end - start); if (d1 >= d2) { return(Vector3.Distance(point, end)); } float r = d1 / d2; return(Vector3.Distance(point, r * end + (1 - r) * start)); }
public float CalcDistance(Vector3 point) { float d1 = Vector3.Dot(bottom.center.position - top.center.position, point - top.center.position); if (MHelperFunctions.FloatZero(d1) <= 0) { return(top.CalcDistance(point)); } float d2 = Vector3.Dot(bottom.center.position - top.center.position, bottom.center.position - top.center.position); if (d1 >= d2) { return(bottom.CalcDistance(point)); } float r = MHelperFunctions.DistanceP2L(point, bottom.center.position - top.center.position, top.center.position); return(Mathf.Abs(r - top.radius)); }
private void RightTriggerPressed(object sender, VRTK.ControllerInteractionEventArgs e) { if (sceneManager.activeEntity.entity != null && ((MEdge)sceneManager.activeEntity.entity).edgeType == MEdge.MEdgeType.LINEAR) { if (curObject == null) { curObject = sceneManager.activeEntity.obj; } else { if (sceneManager.activeEntity.obj != curObject) { ResetStatus(); return; } } MLinearEdge le = sceneManager.activeEntity.entity as MLinearEdge; if (selectEdges.Count > 1) { Vector3 normal = Vector3.Cross(selectEdges[0].direction, selectEdges[1].direction).normalized; if (!MHelperFunctions.Perpendicular(normal, le.direction)) { ResetStatus(); return; } } SelectEdge(le); if (selectEdges.Count < 3) { curObject = null; return; } MPolygonFace face = new MPolygonFace(selectEdges); if (face.IsValid()) { curObject.CreatePolygonFace(new List <MLinearEdge>(selectEdges)); ResetStatus(); } } else { ResetStatus(); } }
private void CalcNormal() { Vector3 v1 = points[1] - points[0]; Vector3 v2 = v1; int count = points.Count; int i; for (i = 1; i < count; i++) { v2 = points[(i + 1) % count] - points[i]; if (!MHelperFunctions.Parallel(v1, v2)) { break; } } if (i != count) { normal = Vector3.Normalize(Vector3.Cross(v1, v2)); } else { Debug.Log("MPolygonFace: CalcNormal: wrong edgeList"); buildSuccess = false; } if (buildSuccess) { Vector3 p = points[0]; for (i = 1; i < count; i++) { Vector3 v = MHelperFunctions.PointProjectionInFace(points[i], normal, points[0]); if (Vector3.Distance(points[i], v) < MDefinitions.VECTOR3_PRECISION) { points[i] = v; } else { buildSuccess = false; return; } } } }