/// <summary> /// Crear triangulo. /// Calcula su plano /// </summary> public Triangle(TGCVector3 a, TGCVector3 b, TGCVector3 c, TgcBoundingSphere sphere) { A = a; B = b; C = c; Plane = TGCPlane.FromPoints(a, b, c); BoundingSphere = sphere; }
/// <summary> /// Crear triangulo. /// Calcula su plano y BoundingSphere /// </summary> public Triangle(TGCVector3 a, TGCVector3 b, TGCVector3 c) { A = a; B = b; C = c; Plane = TGCPlane.FromPoints(a, b, c); BoundingSphere = TgcBoundingSphere.computeFromPoints(new[] { a, b, c }).toClass(); }
/// <summary> /// Tomar un mesh cargar todas las estructuras internas necesarias para poder editarlo /// </summary> private void loadMesh(TgcMesh origMesh) { //Obtener vertices del mesh mesh = origMesh; mesh.AutoTransformEnable = true; var origVertices = getMeshOriginalVertexData(origMesh); var origTriCount = origVertices.Count / 3; //Iterar sobre los triangulos y generar data auxiliar unificada Vertices = new List <EditPolyVertex>(); Edges = new List <EditPolyEdge>(); Polygons = new List <EditPolyPolygon>(); IndexBuffer = new short[origTriCount * 3]; var attributeBuffer = origMesh.D3dMesh.LockAttributeBufferArray(LockFlags.ReadOnly); origMesh.D3dMesh.UnlockAttributeBuffer(attributeBuffer); for (var i = 0; i < origTriCount; i++) { var v1 = origVertices[i * 3]; var v2 = origVertices[i * 3 + 1]; var v3 = origVertices[i * 3 + 2]; //Agregar vertices a la lista, si es que son nuevos var v1Idx = EditablePolyUtils.addVertexToListIfUnique(Vertices, v1); var v2Idx = EditablePolyUtils.addVertexToListIfUnique(Vertices, v2); var v3Idx = EditablePolyUtils.addVertexToListIfUnique(Vertices, v3); v1 = Vertices[v1Idx]; v2 = Vertices[v2Idx]; v3 = Vertices[v3Idx]; //Crear edges var e1 = new EditPolyEdge(); e1.a = v1; e1.b = v2; var e2 = new EditPolyEdge(); e2.a = v2; e2.b = v3; var e3 = new EditPolyEdge(); e3.a = v3; e3.b = v1; //Crear poligono para este triangulo var p = new EditPolyPolygon(); p.vertices = new List <EditPolyVertex>(); p.vertices.Add(v1); p.vertices.Add(v2); p.vertices.Add(v3); p.edges = new List <EditPolyEdge>(); p.edges.Add(e1); p.edges.Add(e2); p.edges.Add(e3); p.vbTriangles = new List <int>(); p.vbTriangles.Add(i * 3); p.TGCPlane = TGCPlane.FromPoints(v1.position, v2.position, v3.position); p.TGCPlane.Normalize(); p.matId = attributeBuffer[i]; //Agregar triangulo al index buffer IndexBuffer[i * 3] = (short)v1Idx; IndexBuffer[i * 3 + 1] = (short)v2Idx; IndexBuffer[i * 3 + 2] = (short)v3Idx; //Agregar a lista de poligonos Polygons.Add(p); /* * //Buscar si hay un poligono ya existente al cual sumarnos (coplanar y que compartan una arista) * EditPolyPolygon coplanarP = null; * for (int j = 0; j < polygons.Count; j++) * { * //Coplanares y con igual material ID * EditPolyPolygon p0 = polygons[j]; * if (p0.matId == p.matId && EditablePolyUtils.sameTGCPlane(p0.TGCPlane, p.TGCPlane)) * { * //Buscar si tienen una arista igual * int p0SharedEdgeIdx; * int pSharedEdgeIdx; * if (EditablePolyUtils.findShareEdgeBetweenPolygons(p0, p, out p0SharedEdgeIdx, out pSharedEdgeIdx)) * { * //Obtener el tercer vertice del triangulo que no es parte de la arista compartida * EditPolyEdge sharedEdge = p0.edges[p0SharedEdgeIdx]; * EditPolyVertex thirdVert; * if (p.vertices[0] != sharedEdge.a && p.vertices[0] != sharedEdge.b) * thirdVert = p.vertices[0]; * else if (p.vertices[1] != sharedEdge.a && p.vertices[1] != sharedEdge.b) * thirdVert = p.vertices[1]; * else * thirdVert = p.vertices[2]; * * //Agregar el tercer vertice al poligno existente * EditablePolyUtils.addVertexToPolygon(p0, sharedEdge, thirdVert); * * //Quitar arista compartida * p0.edges.Remove(sharedEdge); * * //Agregar al poligono dos nuevas aristas que conectar los extremos de la arista compartida hacia el tercer vertice * EditPolyEdge newPolEdge1 = new EditPolyEdge(); * newPolEdge1.a = sharedEdge.a; * newPolEdge1.b = thirdVert; * p0.edges.Add(newPolEdge1); * * EditPolyEdge newPolEdge2 = new EditPolyEdge(); * newPolEdge2.a = thirdVert; * newPolEdge2.b = sharedEdge.b; * p0.edges.Add(newPolEdge2); * * //Agregar indice de triangulo del vertexBuffer que se sumo al poligono * p0.vbTriangles.Add(p.vbTriangles[0]); * * coplanarP = p0; * } * } * } * //Es un nuevo poligono, agregarlo * if (coplanarP == null) * { * polygons.Add(p); * } */ } //Unificar aristas de los poligonos foreach (var p in Polygons) { for (var i = 0; i < p.edges.Count; i++) { bool newEdgeAdded; var eIdx = EditablePolyUtils.addEdgeToListIfUnique(Edges, p.edges[i], out newEdgeAdded); var e = Edges[eIdx]; //Nueva arista incorporada a la lista if (newEdgeAdded) { e.faces = new List <EditPolyPolygon>(); //Agregar referencia a vertices que usan la arista e.a.edges.Add(e); e.b.edges.Add(e); } //Se usa arista existente de la lista else { //Reemplazar en poligono por la nueva p.edges[i] = e; } //Indicar a la arista que pertenece al poligono actual e.faces.Add(p); } } setDirtyValues(false); }