def __init__(self, vertex1=Vertex(), vertex2=Vertex(), vertex3=Vertex(), triangle1=None, triangle2=None, triangle3=None): """ Constructo de la clase triangulo. Convencion: triangle1 es el triangulo opuesto al vertice1 triangle2 es el triangulo opuesto al vertice2 triangle3 es el triangulo opuesto al vertice3 Los vertices deben ordenarse de tal forma que el triangulo se forme en sentido antihorario. :param vertex1: Vertex :param vertex2: Vertex :param vertex3: Vertex :param triangle1: Triangle :param triangle2: Triangle :param triangle3: Triangle """ # asociamos los vertices necesarios self.__vert1 = vertex1 self.__vert2 = vertex2 self.__vert3 = vertex3 # asociamos los triangulos adyacentes al triangulo self.__trian1 = triangle1 self.__trian2 = triangle2 self.__trian3 = triangle3
def test_getTriangleOppositeTo(self): triangle1 = Triangle(Vertex(Point(1, 1)), Vertex(Point(1, 2)), Vertex(Point(3, 4))) triangle2 = Triangle(Vertex(Point(1, 5)), Vertex(Point(1, 6)), Vertex(Point(2, 5))) mainTriang = Triangle(Vertex(Point(1, 2)), Vertex(Point(1, 3)), Vertex(Point(4, 1)), triangle1, triangle2) self.assertEqual( mainTriang.getTriangleOppositeTo( mainTriang.getVert1()) == triangle1, True, "Opposite to vert1") self.assertEqual( mainTriang.getTriangleOppositeTo( mainTriang.getVert2()) == triangle2, True, "Oposite to vert2") self.assertEqual( mainTriang.getTriangleOppositeTo(mainTriang.getVert3()) == None, True, "Non existen opposite triangle") self.assertEqual( mainTriang.getTriangleOppositeTo(Vertex(Point(1, 2))) == triangle1, True, "Opposite to a new vertex with same coordinates") return
def test_getNextVert(self): triangle1 = Triangle(Vertex(Point(1, 1)), Vertex(Point(3, 3)), Vertex(Point(2, 2))) # Comprobamos que sea circular self.assertEqual( triangle1.getNextVertex(triangle1.getVert1()).equal( triangle1.getVert2()), True, "Caso normal 1") self.assertEqual( triangle1.getNextVertex(triangle1.getVert2()).equal( triangle1.getVert3()), True, "Caso normal 2") self.assertEqual( triangle1.getNextVertex(triangle1.getVert3()).equal( triangle1.getVert1()), True, "Caso normal 3") # comprobamos vertices externos self.assertEqual( triangle1.getNextVertex(Vertex(Point(1, 1))).equal( triangle1.getVert2()), True, "No comprueba vertices creados externos al triangulo.") self.assertEqual( triangle1.getNextVertex( triangle1.getNextVertex(Vertex(Point(1, 1)))).equal( triangle1.getVert3()), True, "No es circular con vertices externos.")
def test_getBoundingTriangle2D(self): T = Triangulation() for i in range(15): v1 = Vertex( Point(np.random.random() * 54, np.random.random() * 100)) v2 = Vertex( Point(np.random.random() * 54, np.random.random() * 100)) v3 = Vertex( Point(np.random.random() * 54, np.random.random() * 100)) T.addVertex(v1) T.addVertex(v2) T.addVertex(v3) triangle = T.getBoundingTriangle2D() for vert in T.getVerts(): self.assertEqual( Triangulation.Orient2D(vert, triangle.getVert1(), triangle.getVert2()), True) self.assertEqual( Triangulation.Orient2D(vert, triangle.getVert2(), triangle.getVert3()), True) self.assertEqual( Triangulation.Orient2D(vert, triangle.getVert3(), triangle.getVert1()), True) return
def test_Orient2D(self): # comprueba que funciona para el caso normal v1 = Vertex(Point(-1, 0)) v2 = Vertex(Point(1, 0)) v3 = Vertex(Point(0.5, 0.5)) self.assertEqual(Triangulation.Orient2D(v1, v2, v3), True) self.assertEqual(Triangulation.Orient2D(v3, v2, v1), False) # comprueba que funciona para distancias muy pequeñas v4 = Vertex(Point(0.5, 0.5)) v5 = Vertex(Point(0.5, 0.56)) v6 = Vertex(Point(0.49, 0.5)) self.assertEqual(Triangulation.Orient2D(v4, v5, v6), True) self.assertEqual(Triangulation.Orient2D(v6, v5, v4), False) self.assertRaises(AssertionError, lambda: Triangulation.Orient2D(v6, v5, v6)) self.assertRaises(AssertionError, lambda: Triangulation.Orient2D(v1, v1, v6)) # comprueba que metodo resulta cuando se tratan de puntos colineales v7 = Vertex(Point(-1, 0)) v8 = Vertex(Point(0, 0)) v9 = Vertex(Point(1, 0)) self.assertEqual(Triangulation.Orient2D(v7, v8, v9), True) self.assertEqual(Triangulation.Orient2D(v9, v8, v7), True) return
def test_popTriangle(self): triangle1 = Triangle(Vertex(Point(-1, 0)), Vertex(Point(1, 0)), Vertex(Point(0, 1))) triangle2 = Triangle(Vertex(Point(-1, 0)), Vertex(Point(1, 0)), Vertex(Point(0, -1))) Tr = Triangulation() Tr.addTriangle(triangle1) Tr.addTriangle(triangle2) # quitar triangulo con la misma direccion self.assertEqual(len(Tr.getTriangles()) == 2, True) self.assertEqual(Tr.popTriangle(triangle2), True) self.assertEqual(Tr.popTriangle(triangle2), False) self.assertEqual(len(Tr.getTriangles()) == 1, True) # quitar triangulo con distinta direccion pero mismos vertices self.assertEqual( Tr.popTriangle( Triangle(Vertex(Point(-1, 0)), Vertex(Point(1, 0)), Vertex(Point(0, 1)))), True) self.assertEqual(len(Tr.getTriangles()) == 0, True) return
def test_distance(self): v1 = Vertex(Point(0, 0)) v2 = Vertex(Point(2, 0)) v3 = Vertex(Point(-3, -4)) v4 = Vertex(Point(3, 4)) self.assertEqual(v1.distance(v2), 2, "No calcula distancia normalmente") self.assertEqual(v3.distance(v4), 10, "No calcula distancia en diagonal o negativos") return
def test_getVertexNotShared(self): triangle1 = Triangle(Vertex(Point(1, 1)), Vertex(Point(1, 2)), Vertex(Point(2, 1))) triangle2 = Triangle(Vertex(Point(1, 1)), Vertex(Point(1, 2)), Vertex(Point(3, 1))) self.assertEqual( Triangulation.getVertexsNotShared(triangle1, triangle2)[0].equal( Vertex(Point(2, 1))), True) self.assertEqual( Triangulation.getVertexsNotShared(triangle1, triangle2)[1].equal( Vertex(Point(3, 1))), True) return
def test_deleteTriangleThatContains(self): v1 = Vertex(Point(1, 2)) v2 = Vertex(Point(2, 2)) v3 = Vertex(Point(3, 1)) v4 = Vertex(Point(5, 5)) t1 = Triangle(v1, v2, v3) t2 = Triangle(v2, v3, v4) T = Triangulation() T.addTriangle(t1) T.addTriangle(t2) self.assertEqual( T.getTriangles() == [t1, t2] or T.getTriangles() == [t2, t1], True, "No entrega los triangulos que se le agregaron.") T.deleteTrianglesThatContain(v4) self.assertEqual(T.getTriangles() == [t1], True, "No elimina triangulo que contiene el vertice v4.") T.deleteTrianglesThatContain(v4) self.assertEqual( T.getTriangles() == [t1], True, "Elimina triangulos aun cuando ningun triangulo contiene el vertice." ) T.deleteTrianglesThatContain(v2) self.assertEqual(T.getTriangles() == [], True, "No esta eliminando los triangulos.") return
def getNextVertex(self, vert): """ Dado un vertice, entrega el vertice siguiente en sentido anti horario. :param vert: Vertex :return: Vertex """ assert type(vert) == Vertex if vert.equal(self.getVert1()): return self.getVert2() if vert.equal(self.getVert2()): return self.getVert3() if vert.equal(self.getVert3()): return self.getVert1() #En caso de que no encuentre el vertice, devuelve el vertice por defecto return Vertex()
def test_getTriangleThatContain(self): T = Triangulation() v1 = Vertex(Point(-1, 0)) v2 = Vertex(Point(1, 0)) v3 = Vertex(Point(0, 1)) v4 = Vertex(Point(0, 0.5)) v5 = Vertex(Point(2, 2)) v6 = Vertex(Point(-1, 0)) v7 = Vertex(Point(1, 0)) v8 = Vertex(Point(0, -1)) v9 = Vertex(Point(0, 0)) triangle = Triangle(v1, v2, v3) triangle2 = Triangle(v6, v8, v7) triangle.setTriangle3(triangle2) triangle2.setTriangle2(triangle) T.addTriangle(triangle) T.addTriangle(triangle2) self.assertEqual(T.getTriangleThatContain(v4)[0], triangle) self.assertEqual(T.getTriangleThatContain(v5), []) self.assertEqual( T.getTriangleThatContain(v9)[0] == triangle or T.getTriangleThatContain(v9)[1] == triangle, True) self.assertEqual( T.getTriangleThatContain(v9)[0] == triangle2 or T.getTriangleThatContain(v9)[1] == triangle2, True) return
def test_InCircle(self): # comprueba que funciona en el caso normal v1 = Vertex(Point(-1, 0)) v2 = Vertex(Point(1, 0)) v3 = Vertex(Point(0, 0.5)) self.assertEqual( Triangulation.InCircle(v1, v2, v3, Vertex(Point(0, 0.25))), True) self.assertEqual( Triangulation.InCircle(v1, v2, v3, Vertex(Point(15, 15))), False) self.assertEqual( Triangulation.InCircle(v1, v2, v3, Vertex(Point(0, -0.1))), True) self.assertEqual( Triangulation.InCircle(v1, v2, v3, Vertex(Point(0.9, 0))), True) return
def test_setTriangleOppositeTo(self): triangle1 = Triangle() triangle2 = Triangle(Vertex(Point(1, 2)), Vertex(Point(2, 2)), Vertex(Point(1, 3))) self.assertEqual( triangle2.setTriangleOppositeTo(Vertex(Point(1, 2)), triangle1), True) self.assertEqual(triangle1 == triangle2.getTriangle1(), True) triangle3 = Triangle() self.assertEqual( triangle2.setTriangleOppositeTo(Vertex(Point(1, 5)), triangle1), False) self.assertEqual( triangle2.setTriangleOppositeTo(Vertex(Point(1, 3)), triangle3), True) self.assertEqual(triangle3 == triangle2.getTriangle3(), True) return
from Logic.Point import Point from Logic.Triangulation import Triangulation from Logic.Vertex import Vertex from Logic.Triangle import Triangle Nvertices = 50 T2 = Triangulation() verts = [] # Añadimos puntos al azar np.random.seed(12) for i in range(1, Nvertices): verts.append( Vertex(Point(np.random.random() * 1000, np.random.random() * 1000))) # iniciamos triangulacion T2.makeDelaunay(verts) # dibujamos el camino completo for triangle in T2.Lepp(T2.getTriangles()[-1])[0]: triangle.draw2D(show=False, width=2) # dibujamos primer triangulo para saber donde comenzamos T2.Lepp(T2.getTriangles()[-1])[0][0].draw2D(show=False, width=4) # dibujamos triangulacion T2.draw2D(lineWidth=0.5) T2.improveTriangle(T2.getTriangles()[-1])
# E-mail: [email protected] # Archivo que contiene un test visual sobre el proceso de delaunay. import numpy as np from Logic.Point import Point from Logic.Triangulation import Triangulation from Logic.Vertex import Vertex from Logic.Triangle import Triangle T = Triangulation() # annadimos un punto a la triangulacion P = [ Vertex(Point(6, 87)), Vertex(Point(35, 88)), Vertex(Point(34, 16)), Vertex(Point(87, 75)), Vertex(Point(50, 60)) ] #P = [Vertex(Point(6,87)),Vertex(Point(35,88))] T.makeDelaunay(P) T.printTriangles2D() T.draw2D() ##################################################################### Npuntos = 40 T2 = Triangulation()
from Logic.Point import Point from Logic.Triangulation import Triangulation from Logic.Vertex import Vertex from Logic.Triangle import Triangle Nvertices = 50 T2 = Triangulation() verts = [] # Añadimos puntos al azar np.random.seed(12) for i in range(1, Nvertices): verts.append(Vertex(Point(np.random.random() * 1000, np.random.random() * 1000))) # iniciamos triangulacion T2.makeDelaunay(verts) # dibujamos el camino completo for triangle in T2.Lepp(T2.getTriangles()[-25])[0]: triangle.draw2D(show=False, width=2) # dibujamos primer triangulo para saber donde comenzamos T2.Lepp(T2.getTriangles()[-25])[0][0].draw2D(show=False, width=4) # dibujamos triangulacion T2.draw2D(lineWidth=0.5) T2.improveTriangleCentroid(T2.getTriangles()[-25]) T2.draw2D()
from Logic.Point import Point from Logic.Triangulation import Triangulation from Logic.Vertex import Vertex from Logic.Triangle import Triangle Nvertices = 50 T2 = Triangulation() verts = [] # Añadimos puntos al azar for i in range(1, Nvertices): # Puntos que se añaden son muy lejanos unos de otros, el error cobra importancia en estos casos. verts.append(Vertex(Point(-500, 0))) verts.append(Vertex(Point(500, 0))) verts.append(Vertex(Point(0, 1))) # iniciamos triangulacion T2.makeDelaunay(verts) # dibujamos el camino completo for triangle in T2.Lepp(T2.getTriangles()[-1]): triangle.draw2D(show=False, width=2) # dibujamos primer triangulo para saber donde comenzamos T2.Lepp(T2.getTriangles()[-1])[0].draw2D(show=False, width=4) # dibujamos triangulacion T2.draw2D(lineWidth=0.5) T2.improveTriangle(T2.getTriangles()[-1])
# Author: Sergio Alvarez Medina # Creation Date: 30/03/2019 # E-mail: [email protected] # Insercion delaunay solo con puntos en los ejes import numpy as np from Logic.Point import Point from Logic.Triangulation import Triangulation from Logic.Vertex import Vertex from Logic.Triangle import Triangle Nvertices = 10 T2 = Triangulation() verts = [] # Añadimos puntos al azar for i in range(1, Nvertices): verts.append(Vertex(Point(-100 * i, 0))) verts.append(Vertex(Point(100 * i, 0))) verts.append(Vertex(Point(0, 100 * i))) verts.append(Vertex(Point(0, -100 * i))) T2.makeDelaunay(verts) T2.draw2D(lineWidth=0.5)
import numpy as np from Logic.Point import Point from Logic.Triangulation import Triangulation from Logic.Vertex import Vertex from Logic.Triangle import Triangle Nvertices = 10 T2 = Triangulation() verts = [] # Añadimos puntos al azar for i in range(1, Nvertices): verts.append(Vertex(Point(-1000 * i, 0))) verts.append(Vertex(Point(1000 * i, 0))) verts.append(Vertex(Point(0, 1000 * i))) verts.append(Vertex(Point(0, -1000 * i))) T2.makeDelaunay(verts) T2.draw2D(lineWidth=0.5) T2.addVertexToDelaunay(Vertex(Point(1000, 1000))) T2.draw2D() T2.addVertexToDelaunay(Vertex(Point(-1000, 1000))) T2.draw2D() T2.addVertexToDelaunay(Vertex(Point(-10000, 7000))) T2.draw2D()
# Insercion delaunay con grilla extremadamente grande. import numpy as np from Logic.Point import Point from Logic.Triangulation import Triangulation from Logic.Vertex import Vertex from Logic.Triangle import Triangle Npuntos = 20 T2 = Triangulation() verts = [] print("Annadiendo puntos") # Añadimos puntos al azar for i in range(Npuntos): for j in range(Npuntos): verts.append(Vertex(Point(i * 10, j * 10))) for i in range(Npuntos): for j in range(Npuntos): verts.append(Vertex(Point(i * 10 + 5, j * 10 + 5))) print("Ejecutando Delaunay...") T2.makeDelaunay(verts) print("Dibujando...") T2.draw2D(lineWidth=0.5)
# Comprueba triangulacion en un rectangulo para comprobar el insertar un punto en la segunda arista mas larga. import numpy as np import matplotlib as plt from Logic.Point import Point from Logic.Triangulation import Triangulation from Logic.Vertex import Vertex from Logic.Triangle import Triangle anguloMinimo = 30 T2 = Triangulation() # Añadimos puntos al azar t1 = Triangle(Vertex(Point(0, 0)), Vertex(Point(1000, 0)), Vertex(Point(1000, 100)), None, None, None) t2 = Triangle(Vertex(Point(0, 0)), Vertex(Point(1000, 100)), Vertex(Point(0, 100)), None, None, None) t1.setTriangle2(t2) t2.setTriangle3(t1) T2.addTriangle(t1) T2.addTriangle(t2) # mostramos la configuracion inicial T2.draw2D(lineWidth=0.5) # Mejoramos triangulacion mientras existan trianglos malos T2.mejorarTriangulos(angle=anguloMinimo,