public virtual void RemoveVertex(T vertex) { if (vertex == null) { Log.Error("Vertex is null."); return; } var affectedEdges = Edges .Where(e => e.Source == vertex || e.Target == vertex) .ToArray(); var neighbors = affectedEdges .Select(e => e.Opposite(vertex)) .Except(new[] { vertex }); // a self edge would make the vertex its own neighbor foreach (var edge in affectedEdges) { RemoveEdge(edge); } if (!VerticesData.Remove(vertex)) { Log.Error("Vertex is not a member: " + vertex); return; } // maintain root data recalcIsTree = true; RootVertices.Remove(vertex); // add the targets that have no more in-edges RootVertices.UnionWith(neighbors.Where(v => !HasParents(v))); }
private void Start() { ClosestChunkHasChanged(); //Set variables for UpdateChunks() UpdateChunks(); VerticesData finalData = CollectData(verticesData); vertices = finalData.verticesP(); normals = finalData.normalsP(); }
public void SetPos(T vertex, Vector2 pos) { VertexData <T, P> data; if (!VerticesData.TryGetValue(vertex, out data)) { Log.Error("Graph.SetPos: unknown vertex " + vertex); return; } data.pos = pos; }
public Vector2 GetPos(T vertex) { VertexData <T, P> data; if (!VerticesData.TryGetValue(vertex, out data)) { Log.Error("Graph.GetPos: unknown vertex " + vertex); return(Vector2.zero); } return(data.pos); }
public virtual void AddVertex(T vertex) { if (vertex == null) { Log.Error("Vertex is null."); return; } if (VerticesData.ContainsKey(vertex)) { Log.Error("Vertex is already part of the graph: " + vertex); return; } VerticesData[vertex] = new VertexData <T, P>(vertex); recalcIsTree = true; RootVertices.Add(vertex); }
private void CalculateNormals() { int numNormals = (chunkSize / reason + 1) * (chunkSize / reason + 1); Vector3[] vertexNormals = new Vector3[numNormals]; int triangleCount = triangles.Length / 3; for (int i = 0; i < triangleCount; i++) { int normalTriangleIndex = i * 3; int vertexIndexA = triangles[normalTriangleIndex]; int vertexIndexB = triangles[normalTriangleIndex + 1]; int vertexIndexC = triangles[normalTriangleIndex + 2]; Vector3 triangleNormal = SurfaceNormalFromIndices(vertexIndexA, vertexIndexB, vertexIndexC); vertexNormals[vertexIndexA] += triangleNormal; vertexNormals[vertexIndexB] += triangleNormal; vertexNormals[vertexIndexC] += triangleNormal; } //Border triangles int borderTriangleCount = borderTriangles.Length / 3; for (int i = 0; i < borderTriangleCount; i++) { int normalTriangleIndex = i * 3; int vertexIndexA = borderTriangles[normalTriangleIndex]; int vertexIndexB = borderTriangles[normalTriangleIndex + 1]; int vertexIndexC = borderTriangles[normalTriangleIndex + 2]; Vector3 triangleNormal = SurfaceNormalFromIndices(vertexIndexA, vertexIndexB, vertexIndexC); vertexNormals[vertexIndexA] += triangleNormal; vertexNormals[vertexIndexB] += triangleNormal; vertexNormals[vertexIndexC] += triangleNormal; } for (int i = 0; i < vertexNormals.Length; i++) { vertexNormals[i].Normalize(); } mesh.normals = vertexNormals; data = new VerticesData(vertices, vertexNormals); }
private void GenerateChunks() { chunks = new List <Chunk>(); List <VerticesData> verticesData = new List <VerticesData>(); centers = new List <Vector3>(); GenerateChunksOfFace(chunks, "xy", verticesData); //GenerateChunksOfFace(chunks, "xyz", verticesData); GenerateChunksOfFace(chunks, "zy", verticesData); GenerateChunksOfFace(chunks, "zyx", verticesData); GenerateChunksOfFace(chunks, "xz", verticesData); GenerateChunksOfFace(chunks, "xzy", verticesData); VerticesData finalData = CollectData(verticesData); vertices = finalData.verticesP(); normals = finalData.normalsP(); }
public virtual void AddEdge(Relation <T, P> relation) { if (relation == null || relation.Source == null || relation.Target == null) { Log.Error("Relation or vertex is null."); return; } if (!VerticesData.ContainsKey(relation.Source)) { Log.Error("Relation source is missing from the graph: " + relation.Source); return; } if (!VerticesData.ContainsKey(relation.Target)) { Log.Error("Relation target is missing from the graph: " + relation.Target); return; } if (!Edges.Add(relation)) { Log.Error("Relation is already part of the graph: " + relation); return; } VerticesData[relation.Source].OutEdges.Add(relation); VerticesData[relation.Target].InEdges.Add(relation); // maintain root data if (!relation.IsSelfRelation) { recalcIsTree = true; RootVertices.Remove(relation.Target); } }
private void CreateVertices() { int v; int uvCont; switch (face) { case "xy": v = fromV; uvCont = fromUVCont; for (int y = fromY; y <= (fromY + chunkSize); y += reason) { for (int x = fromX; x <= toX; x += reason) { float height = noiseMap[x, y] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, x, y, 0); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); //verticesParcial[v - 1] = realVertice; float coorX = (float)x / (float)gridSize; float coorY = (float)y / (float)gridSize; uvs[uvCont++] = new Vector2(coorX, coorY); } } break; case "xyz": v = fromV; uvCont = fromUVCont; for (int y = fromY; y <= (fromY + chunkSize); y += reason) { for (int x = toX; x >= fromX; x -= reason) { float height = noiseMap[x, y] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, x, y, gridSize); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); //verticesParcial[v - 1] = realVertice; float coorX = (float)x / (float)gridSize; float coorY = (float)y / (float)gridSize; uvs[uvCont++] = new Vector2(coorX, coorY); } } break; case "zy": v = fromV; uvCont = fromUVCont; for (int y = fromY; y <= (fromY + chunkSize); y += reason) { for (int z = toZ; z >= fromZ; z -= reason) { float height = noiseMap[z, y] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, 0, y, z); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); //verticesParcial[v - 1] = realVertice; float coorX = (float)z / (float)gridSize; float coorY = (float)y / (float)gridSize; uvs[uvCont++] = new Vector2(coorX, coorY); } } break; case "zyx": v = fromV; uvCont = fromUVCont; for (int y = fromY; y <= (fromY + chunkSize); y += reason) { for (int z = fromZ; z <= toZ; z += reason) { float height = noiseMap[z, y] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, gridSize, y, z); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); //verticesParcial[v - 1] = realVertice; float coorX = (float)z / (float)gridSize; float coorY = (float)y / (float)gridSize; uvs[uvCont++] = new Vector2(coorX, coorY); } } break; case "xz": v = fromV; uvCont = fromUVCont; for (int z = fromZ; z <= (fromZ + chunkSize); z += reason) { for (int x = toX; x >= fromX; x -= reason) { float height = noiseMap[x, z] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, x, 0, z); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); //verticesParcial[v - 1] = realVertice; float coorX = (float)x / (float)gridSize; float coorY = (float)z / (float)gridSize; uvs[uvCont++] = new Vector2(coorX, coorY); } } break; case "xzy": v = fromV; uvCont = fromUVCont; for (int z = fromZ; z <= (fromZ + chunkSize); z += reason) { for (int x = fromX; x <= toX; x += reason) { float height = noiseMap[x, z] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, x, gridSize, z); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); //verticesParcial[v-1] = realVertice; float coorX = (float)x / (float)gridSize; float coorY = (float)z / (float)gridSize; uvs[uvCont++] = new Vector2(coorX, coorY); } } break; } data = new VerticesData(verticesParcial, normalsParcial); }
public void CleanNullRefs() { VerticesData.RemoveWhere(pair => Util.IsBadRef(pair.Key)); Edges.RemoveWhere(edge => Util.IsBadRef(edge.Source) || Util.IsBadRef(edge.Target)); }
public bool ContainsVertex(T vertex) { return(VerticesData.ContainsKey(vertex)); }
private void CreateVertices() { int numVertices = (chunkSize / reason + 1) * (chunkSize / reason + 1); Vector3[] verticesParcial = new Vector3[numVertices]; Vector3[] normalsParcial = new Vector3[verticesParcial.Length]; Vector2[] uvs = new Vector2[(chunkSize / reason + 1) * (chunkSize / reason + 1)]; int v; int cont = 0; switch (face) { case "xy": v = 0; cont = 0; for (int y = fromY; y <= (fromY + chunkSize); y += reason) { for (int x = fromX; x <= (fromX + chunkSize); x += reason) { float height = noiseMap[x, y] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, x, y, 0); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); verticesParcial[v - 1] = realVertice; float coorX = (float)x / (float)gridSize; float coorY = (float)y / (float)gridSize; uvs[cont++] = new Vector2(coorX, coorY); } } break; case "xyz": v = 0; cont = 0; for (int y = fromY; y <= (fromY + chunkSize); y += reason) { for (int x = (fromX + chunkSize); x >= fromX; x -= reason) { float height = noiseMap[x, y] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, x, y, gridSize); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); verticesParcial[v - 1] = realVertice; float coorX = (float)x / (float)gridSize; float coorY = (float)y / (float)gridSize; uvs[cont++] = new Vector2(coorX, coorY); } } break; case "zy": v = 0; cont = 0; for (int y = fromY; y <= (fromY + chunkSize); y += reason) { for (int z = (fromZ + chunkSize); z >= fromZ; z -= reason) { float height = noiseMap[z, y] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, 0, y, z); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); verticesParcial[v - 1] = realVertice; float coorX = (float)z / (float)gridSize; float coorY = (float)y / (float)gridSize; uvs[cont++] = new Vector2(coorX, coorY); } } break; case "zyx": v = 0; cont = 0; for (int y = fromY; y <= (fromY + chunkSize); y += reason) { for (int z = fromZ; z <= (fromZ + chunkSize); z += reason) { float height = noiseMap[z, y] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, gridSize, y, z); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); verticesParcial[v - 1] = realVertice; float coorX = (float)z / (float)gridSize; float coorY = (float)y / (float)gridSize; uvs[cont++] = new Vector2(coorX, coorY); } } break; case "xz": v = 0; cont = 0; for (int z = fromZ; z <= (fromZ + chunkSize); z += reason) { for (int x = (fromX + chunkSize); x >= fromX; x -= reason) { float height = noiseMap[x, z] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, x, 0, z); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); verticesParcial[v - 1] = realVertice; float coorX = (float)x / (float)gridSize; float coorY = (float)z / (float)gridSize; uvs[cont++] = new Vector2(coorX, coorY); } } break; case "xzy": v = 0; cont = 0; for (int z = fromZ; z <= (fromZ + chunkSize); z += reason) { for (int x = fromX; x <= (fromX + chunkSize); x += reason) { float height = noiseMap[x, z] * heightMultiplier; SetVertex(verticesParcial, normalsParcial, v++, x, gridSize, z); Vector3 realVertice = verticesParcial[v - 1] + verticesParcial[v - 1] * (height / radius); verticesParcial[v - 1] = realVertice; float coorX = (float)x / (float)gridSize; float coorY = (float)z / (float)gridSize; uvs[cont++] = new Vector2(coorX, coorY); } } break; } mesh.vertices = verticesParcial; mesh.normals = normalsParcial; mesh.uv = uvs; data = new VerticesData(verticesParcial, normalsParcial); }