def post(self):
     #get the variables from the post request
     descriptive_name = self.request.get('sight_name_location_name')
     points_string = self.request.get('points_string_name')
     #convert the points string to a list of point objects
     points_list = FormatConverter()\
         .convert_points_string_to_points_list(points_string)
     #create the polygon object
     polygon = Polygon()
     polygon.create_polygon_by_points(points_list)
     if not polygon.is_valid_polygon():
         #handle bad polygon here
         pass
     #get user object
     this_user = User().by_user_name(self.user_name)
     location_management = LocationManagement()
     #insert the location into the database
     pl_key = location_management.location_creation(this_user, polygon,
                                                    descriptive_name)
     if pl_key:
         #if insert was successful redirect use to editing page
         url_safe_key = pl_key.urlsafe()
         self.redirect('/edit_location/' + url_safe_key)
     else:
         #if insert was unsuccessful handle it
         self.write('catch this error')
    def post(self, resource):
        """
        Handles an edited location
        :param resource: url safe location key
        :return:
        """
        self.get_resources(resource)

        if self.request.get('delete_location'):
            LocationManagement().delete_location(self.location.key)
            self.redirect('/manage_locations')

        elif self.request.get('save_location'):
            #get the variables from the post request
            descriptive_name = self.request.get('descriptive_name')
            active = self.request.get('active')
            camera = self.request.get('camera')
            camera_text = self.request.get('camera_text')
            video = self.request.get('video')
            video_text = self.request.get('video_text')
            points_string = self.request.get('points_string_name')
            zip_code = self.request.get('zip_code')
            #convert the points string to a list of points
            points_list = FormatConverter()\
                .convert_points_string_to_points_list(points_string)

            #create new polygon
            new_polygon = Polygon()
            new_polygon.create_polygon_by_points(points_list)

            #check to see if the polygon is valid
            if not new_polygon.is_valid_polygon():
                #if not valid redirect to unedited version of location
                url_safe_key = self.location.key.urlsafe()
                self.redirect('/edit_location/' + url_safe_key)
                return
            #check to see if the polygon has changed
            polygons_match = new_polygon\
                .polygon_is_equivalent(self.location.polygon)
            if not polygons_match:
                '''if the polygon was edited remove all old children and
                inserts new children for the location'''
                self.location.polygon = new_polygon
                LocationManagement()\
                    .edit_location_with_polygon_change(new_polygon,
                                                       self.location.key)
            #set fields of db object and put it to the db
            self.location.descriptive_name = descriptive_name
            self.location.status.active = self.convert_true_false(active)
            self.location.camera.active = self.convert_true_false(camera)
            self.location.camera.message_text = camera_text
            self.location.video.active = self.convert_true_false(video)
            self.location.video.message_text = video_text
            self.location.zip_code = zip_code
            self.location.put()

            #redirect to the location edit page
            self.url_safe_key = self.location.key.urlsafe()
            self.redirect('/edit_location/' + self.url_safe_key)
Example #3
0
    def onAddPolygon(self, button):
        self.printToLog("onAddPolygon")

        name = self.builder.get_object("PolygonName").get_text()
        polygon = Polygon(name)

        for point in self.temp_polygon:
            polygon.addCoords(float(point["x"]), float(point["y"]))

        self.display_file.addObject(polygon)
        self.temp_polygon = []
        self.add_object_window.hide()
Example #4
0
def test_polygon_volume():
    polygon = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 0.5], [1.0, 1.0],
                        [0.0, 1.0]])
    polynomial_order = 1
    p = Polygon(polygon, polynomial_order)
    expected_volume = 1.0
    assert np.abs(p.volume - expected_volume) < 1.0e-9
Example #5
0
def test_square_centroid():
    polygon = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]])
    polynomial_order = 1
    p = Polygon(polygon, polynomial_order)
    expected_centroid = np.array([0.5, 0.5])
    assert (np.abs(p.centroid - expected_centroid) < np.full(
        (2, ), 1.0e-9)).all() and p.centroid.shape == (2, )
    def importFile(self, path):
        self.display_file.wipeOut()
        #print("{}".format(path))
        vertices = dict()
        vertice_counter = 0
        name = ""

        self.file = open(path, "r+")  # read and write

        for line in self.file:
            if (line[0] == "v"):  # store vertices in a dictionary
                vertice_counter += 1
                vertices[vertice_counter] = line

            elif (line[0] == "o"
                  ):  # store temporarily the name of the object to come
                match = re.findall(r"\S+", line)
                name = match[1]

            elif (line[0] == "p"):  # TODO: FINISH THIS
                match = re.findall(r"\S+", line)
                vertice_for_point = vertices[float(match[1])]
                match = re.findall(r"\S+", vertice_for_point)
                coord = {"x": float(match[1]), "y": float(match[2])}

                p1 = Point(name)
                p1.addCoords(coord["x"], coord["y"])
                self.display_file.addObject(p1)

            elif (line[0] == "l"):
                match = re.findall(r"\S+", line)

                if (len(match) == 3):  # line
                    l = Line(name)
                else:  # polygon
                    l = None

                    if (match[1] == match[-1]):
                        l = Polygon(name)
                    else:
                        l = Curve(name)

                for item in match:
                    if (
                            item != "l"
                    ):  # ignore the first character, only compute coordinates
                        vertice_for_point = vertices[float(item)]
                        match_vertice = re.findall(r"\S+", vertice_for_point)
                        coord = {
                            "x": float(match_vertice[1]),
                            "y": float(match_vertice[2])
                        }
                        l.addCoords(coord["x"], coord["y"])

                if (match[1] == match[-1]
                    ):  # if polygon (last coords == first coords)
                    l.popCoords()  # remove repeated coords

                self.display_file.addObject(l)
Example #7
0
def test_polygon_quadrature_2(k, expected):
    # vertices = np.array([[0.0, 0.0], [1.0, 1.0], [2.0, 0.0]])
    polygon = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]])
    p = Polygon(polygon, k)
    # polygon = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]])
    # t = polygon(polygon, k)
    numerical_integral = np.sum([
        quadrature_weight *
        functions_polygon[k - 1](quadrature_point[1], quadrature_point[0])
        for quadrature_point, quadrature_weight in zip(p.quadrature_points,
                                                       p.quadrature_weights)
    ])
    assert np.abs(numerical_integral - expected) < 1.0e-9
Example #8
0
    def build_figure(figure):
        if figure["type"] == "point":
            return Point.build(figure)

        if figure["type"] == "circle":
            return Circle.build(figure)

        if figure["type"] == "polygon":
            return Polygon.build(figure)

        if figure["type"] == "rectangle":
            return Rectangle.build(figure)

        if figure["type"] == "square":
            return Square.build(figure)

        raise ValueError("incorrect type")
Example #9
0
 def get_polyhedron_simplicial_partition(vertices: Mat,
                                         connectivity_matrix: Mat,
                                         barycenter: Mat) -> Mat:
     """
     ================================================================================================================
     Method :
     ================================================================================================================
     Computes the partition of a polyhedron into tetrahedra. Each tetrahedron consists in the barycenter of the polygon, and a triangle that composing the simplicial partition of a face.
     ================================================================================================================
     Parameters :
     ================================================================================================================
     - vertices : the matrix containing the vertices coordinates as vectors.
     - connectivity_matrix : the matrix that specifies the connection between the vertices of the polyhedron and its 
     faces, as a list of indices corresponding to the vertex indices for each face. The number of rows is the number of faces, and for each row, the number of columns is the number of vertices composing the face.
     - polynomial_order : the polynomial order of integration over the polyhedron.
     ================================================================================================================
     Returns :
     ================================================================================================================
     - simplicial_sub_domains : the list of matrices containing the vertices of each tetrahedron composing the
     partition of the polyhedron.
     """
     simplicial_sub_domains = []
     for face_vertices_indexes in connectivity_matrix:
         face_vertices = vertices[face_vertices_indexes]
         if face_vertices.shape[0] > 3:
             face_barycenter = Domain.get_domain_barycenter_vector(
                 face_vertices)
             sub_faces = Polygon.get_polygon_simplicial_partition(
                 face_vertices, face_barycenter)
             for sub_face in sub_faces:
                 b = np.resize(barycenter, (1, 3))
                 tetra = np.concatenate((sub_face, b), axis=0)
                 simplicial_sub_domains.append(tetra)
         else:
             b = np.resize(barycenter, (1, 3))
             tetra = np.concatenate((face_vertices, b), axis=0)
             simplicial_sub_domains.append(tetra)
     return simplicial_sub_domains
Example #10
0
 def __init__(self, vertices: Mat, connectivity_matrix: Mat,
              quadrature_order: int):
     """
     ================================================================================================================
     Class :
     ================================================================================================================
     
     ================================================================================================================
     Parameters :
     ================================================================================================================
     
     ================================================================================================================
     Attributes :
     ================================================================================================================
     
     """
     # --------------------------------------------------------------------------------------------------------------
     # Building the cell
     # --------------------------------------------------------------------------------------------------------------
     cell_shape = Cell.get_cell_shape(vertices)
     if cell_shape == "SEGMENT":
         c = Segment(vertices, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     if cell_shape == "TRIANGLE":
         c = Triangle(vertices, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     if cell_shape == "POLYGON":
         c = Polygon(vertices, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     if cell_shape == "TETRAHEDRON":
         c = Tetrahedron(vertices, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     if cell_shape == "POLYHEDRON":
         c = Polyhedron(vertices, connectivity_matrix, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     super().__init__(centroid, volume, diameter, quadrature_points,
                      quadrature_weights)
Example #11
0
 def __init__(self, vertices: Mat, quadrature_order: int):
     """
     ================================================================================================================
     Class :
     ================================================================================================================
     
     ================================================================================================================
     Parameters :
     ================================================================================================================
     
     ================================================================================================================
     Attributes :
     ================================================================================================================
     
     en dim 1 : [[p0x]]
     en dim 2 : [[p0x, p0y], [p1x, p1y]]
     en dim 3 : [[p0x, p0y, p0z], [p1x, p1y, p1z], [p2x, p2y, p2z]]
     """
     # --------------------------------------------------------------------------------------------------------------
     # Building the face
     # --------------------------------------------------------------------------------------------------------------
     face_shape = Face.get_face_shape(vertices)
     # --------------------------------------------------------------------------------------------------------------
     # Computing the mapping from the cell reference frame into the face hyperplane
     # --------------------------------------------------------------------------------------------------------------
     self.reference_frame_transformation_matrix = self.get_face_reference_frame_transformation_matrix(vertices)
     # --------------------------------------------------------------------------------------------------------------
     # Computing the mapping from the cell reference frame into the face hyperplane
     # --------------------------------------------------------------------------------------------------------------
     vertices_in_face_reference_frame = Face.get_points_in_face_reference_frame(
         vertices, self.reference_frame_transformation_matrix
     )
     # --------------------------------------------------------------------------------------------------------------
     # Getting integration points and face data
     # --------------------------------------------------------------------------------------------------------------
     if face_shape == "POINT":
         f = Point(vertices_in_face_reference_frame)
         centroid = f.centroid
         volume = f.volume
         diameter = f.diameter
         quadrature_points = f.quadrature_points
         quadrature_weights = f.quadrature_weights
         del f
     if face_shape == "SEGMENT":
         f = Segment(vertices_in_face_reference_frame, quadrature_order)
         centroid = f.centroid
         volume = f.volume
         diameter = f.diameter
         quadrature_points = f.quadrature_points
         quadrature_weights = f.quadrature_weights
         del f
     if face_shape == "TRIANGLE":
         f = Triangle(vertices_in_face_reference_frame, quadrature_order)
         centroid = f.centroid
         volume = f.volume
         diameter = f.diameter
         quadrature_points = f.quadrature_points
         quadrature_weights = f.quadrature_weights
         del f
     if face_shape == "QUADRANGLE":
         f = Quadrangle(vertices_in_face_reference_frame, quadrature_order)
         centroid = f.centroid
         volume = f.volume
         diameter = f.diameter
         quadrature_points = f.quadrature_points
         quadrature_weights = f.quadrature_weights
         del f
     if face_shape == "POLYGON":
         f = Polygon(vertices_in_face_reference_frame, quadrature_order)
         centroid = f.centroid
         volume = f.volume
         diameter = f.diameter
         quadrature_points = f.quadrature_points
         quadrature_weights = f.quadrature_weights
         del f
     # --------------------------------------------------------------------------------------------------------------
     # Computing the normal component
     # --------------------------------------------------------------------------------------------------------------
     distance_to_origin = self.get_face_distance_to_origin(vertices)
     # --------------------------------------------------------------------------------------------------------------
     # Appending the normal component to quadrature points
     # --------------------------------------------------------------------------------------------------------------
     number_of_quadrature_points = quadrature_points.shape[0]
     face_distance_to_origin_vector = np.full((number_of_quadrature_points, 1), distance_to_origin)
     quadrature_points_in_face_reference_frame = np.concatenate(
         (quadrature_points, face_distance_to_origin_vector), axis=1
     )
     # --------------------------------------------------------------------------------------------------------------
     # Appending the normal component to the centroid
     # --------------------------------------------------------------------------------------------------------------
     face_distance_to_origin_vector = np.full((1,), distance_to_origin)
     centroid_in_face_reference_frame = np.concatenate((centroid, face_distance_to_origin_vector))
     # --------------------------------------------------------------------------------------------------------------
     # Inverting the mapping matrix from the cell reference frame to the face reference frame
     # --------------------------------------------------------------------------------------------------------------
     p_inv = np.linalg.inv(self.reference_frame_transformation_matrix)
     # --------------------------------------------------------------------------------------------------------------
     # Getting the quadrature points and nodes in the cell reference frame
     # --------------------------------------------------------------------------------------------------------------
     quadrature_points = (p_inv @ quadrature_points_in_face_reference_frame.T).T
     quadrature_weights = quadrature_weights
     centroid = (p_inv @ centroid_in_face_reference_frame.T).T
     # --------------------------------------------------------------------------------------------------------------
     # Building the face domain
     # --------------------------------------------------------------------------------------------------------------
     super().__init__(centroid, volume, diameter, quadrature_points, quadrature_weights)