Пример #1
0
 def test_simple_polygon_reverse(self):
     """Test generating a mesh from a simple polygon."""
     outside_poly = [(0, 10, 0), (0, 20, 0), (0, 30, 0), (0, 40, 0),
                     (0, 50, 0), (0, 60, 0), (0, 70, 0), (0, 80, 0),
                     (0, 90, 0), (0, 100, 0), (10, 100, 0), (20, 100, 0),
                     (30, 100, 0), (40, 100, 0), (50, 100, 0), (60, 100, 0),
                     (70, 100, 0), (80, 100, 0), (90, 100, 0),
                     (100, 100, 0), (100, 90, 0), (100, 80, 0),
                     (100, 70, 0), (100, 60, 0), (100, 50, 0), (100, 40, 0),
                     (100, 30, 0), (100, 20, 0), (100, 10, 0), (100, 0, 0),
                     (90, 0, 0), (80, 0, 0), (70, 0, 0), (60, 0, 0),
                     (50, 0, 0), (40, 0, 0), (30, 0, 0), (20, 0, 0),
                     (10, 0, 0), (0, 0, 0)]
     inside_polys = [[(40, 40, 0), (50, 40, 0), (60, 40, 0), (60, 50, 0),
                      (60, 60, 0), (50, 60, 0), (40, 60, 0), (40, 50, 0)]]
     outside_poly.reverse()
     inside_polys[0].reverse()
     input_poly = PolyInput(outside_polygon=outside_poly,
                            inside_polygons=inside_polys)
     meshing_input = MultiPolyMesherIo(polygons=[input_poly])
     status, error = mesh_utils.generate_mesh(meshing_input)
     self.assertEqual(139, len(meshing_input.points))
     self.assertEqual(1150, len(meshing_input.cells))
     self.assertTrue(status)
     self.assertEqual(error, '')
    def test_constructor(self):
        """Test the PolyInput constructor."""
        outside_poly = ((1, 2, 0), (5, 2, 0), (5, 9, 0), (1, 9, 0))
        inside_polys = (((3, 3, 0), (2.5, 4, 0), (2, 3, 0)),
                        ((4, 8, 0), (3, 7, 0), (2, 8, 0)))
        poly_corners = (0, 1, 2, 3)
        bias = 3.14159
        pts = ((1, 0, 0), (10, 0, 0), (10, 10, 0))
        size_func = InterpLinear(pts)
        elev_func = InterpIdw(pts)

        pi = PolyInput(outside_polygon=outside_poly,
                       inside_polygons=inside_polys,
                       bias=bias,
                       size_function=size_func,
                       polygon_corners=poly_corners,
                       elevation_function=elev_func)
        self.assertIsInstance(pi, PolyInput)
        self.assert_arrays_equal(outside_poly, pi.outside_polygon)
        self.assert_inside_polys_equal(inside_polys, pi.inside_polygons)
        self.assertEqual(bias, pi.bias)
        # self.assertEqual(size_func.to_string(), pi.size_function.to_string())
        # self.assertEqual(elev_func.to_string(), pi.elevation_function.to_string())
        self.assertEqual(-1, pi.constant_size_bias)
        self.assertEqual(-1, pi.constant_size_function)
        self.assertEqual(False, pi.remove_internal_four_triangle_points)
Пример #3
0
    def test_case_4(self):
        """Test generating a mesh from a simple polygon."""
        # build test case 4 polys
        out_a = (0, 60, 10, 60, 20, 60, 30, 60, 30, 50, 30, 40, 30, 30, 30, 20,
                 30, 10, 30, 0, 20, 0, 10, 0, 0, 0, 0, 10, 0, 20, 0, 30, 0, 40,
                 0, 50)
        in_a1 = (10, 50, 10, 40, 20, 40, 20, 50)
        in_a2 = (10, 20, 10, 10, 20, 10, 20, 20)
        outside_poly = self.array_to_vec_pt3d(out_a)
        inside_polys = (self.array_to_vec_pt3d(in_a1),
                        self.array_to_vec_pt3d(in_a2))
        bias = 1.0
        poly_input_a = PolyInput(outside_polygon=outside_poly,
                                 inside_polygons=inside_polys,
                                 bias=bias)

        out_b = (30, 60, 40, 60, 50, 60, 60, 60, 70, 60, 70, 50, 70, 40, 70,
                 30, 70, 20, 70, 10, 70, 0, 60, 0, 50, 0, 40, 0, 40, 10, 30,
                 10, 30, 20, 40, 20, 40, 30, 30, 30, 30, 40, 40, 40, 40, 50,
                 30, 50)
        in_b1 = (50, 50, 50, 40, 60, 40, 60, 50)
        in_b2 = (50, 20, 50, 10, 60, 10, 60, 20)

        outside_poly_b = self.array_to_vec_pt3d(out_b)
        inside_polys_b = (self.array_to_vec_pt3d(in_b1),
                          self.array_to_vec_pt3d(in_b2))
        bias_b = 1.0
        poly_input_b = PolyInput(outside_poly_b, inside_polys_b, bias=bias_b)

        io = MultiPolyMesherIo(())
        io.polygons = (poly_input_a, poly_input_b)

        # Value Error when file not specified
        with self.assertRaises(ValueError) as context:
            mesh_utils.generate_2dm(io, '')
        self.assertTrue('file_name not specifed. Aborting mesh procedure.' in
                        str(context.exception))

        # mesh the polys
        (success, result) = mesh_utils.generate_2dm(io, "out_file.2dm", 3)
        self.assertTrue(success)
        self.assertTrue(os.path.isfile("out_file.2dm"))
        base_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 "..", "files", "out_file.2dm")
        self.assert_file_lines_equal(base_file, "out_file.2dm")
Пример #4
0
    def test_generate_mesh_api_assert(self):
        """Ensure we get expected exception with bad input."""
        outside_polygon = []
        polygon_input = PolyInput(outside_polygon=outside_polygon)

        runtime_error = "---Error: Outer polygon index 0 is empty."
        with self.assertRaises(RuntimeError) as context:
            _ = mesher.generate_mesh(polygon_inputs=[polygon_input])
        err = context.exception

        self.assertIn(runtime_error, str(err))
Пример #5
0
    def test_repeated_first_and_last(self):
        """Test generating a mesh from a simple polygon."""
        # build test case 4 polys
        out_a = (0, 60, 10, 60, 20, 60, 30, 60, 30, 50, 30, 40, 30, 30, 30, 20,
                 30, 10, 30, 0, 20, 0, 10, 0, 0, 0, 0, 10, 0, 20, 0, 30, 0, 40,
                 0, 50, 0, 60)
        in_a1 = (10, 50, 10, 40, 20, 40, 20, 50, 10, 50)
        in_a2 = (10, 20, 10, 10, 20, 10, 20, 20, 10, 20)
        outside_poly = self.array_to_vec_pt3d(out_a)
        inside_polys = (self.array_to_vec_pt3d(in_a1),
                        self.array_to_vec_pt3d(in_a2))
        bias = 1.0
        poly_input_a = PolyInput(outside_polygon=outside_poly,
                                 inside_polygons=inside_polys,
                                 bias=bias)

        out_b = (30, 60, 40, 60, 50, 60, 60, 60, 70, 60, 70, 50, 70, 40, 70,
                 30, 70, 20, 70, 10, 70, 0, 60, 0, 50, 0, 40, 0, 40, 10, 30,
                 10, 30, 20, 40, 20, 40, 30, 30, 30, 30, 40, 40, 40, 40, 50,
                 30, 50)
        in_b1 = (50, 50, 50, 40, 60, 40, 60, 50)
        in_b2 = (50, 20, 50, 10, 60, 10, 60, 20)

        outside_poly_b = self.array_to_vec_pt3d(out_b)
        inside_polys_b = (self.array_to_vec_pt3d(in_b1),
                          self.array_to_vec_pt3d(in_b2))
        bias_b = 1.0
        poly_input_b = PolyInput(outside_polygon=outside_poly_b,
                                 inside_polygons=inside_polys_b,
                                 bias=bias_b)

        io = MultiPolyMesherIo(polygons=[poly_input_a, poly_input_b])

        # mesh the polys
        (success,
         result) = mesh_utils.generate_2dm(io, "out_file_02_repeated.2dm", 3)
        self.assertTrue(success)
        self.assertTrue(os.path.isfile("out_file_02_repeated.2dm"))
        base_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 "..", "files", "out_file.2dm")
        self.assert_file_lines_equal(base_file, "out_file_02_repeated.2dm")
    def test_properties(self):
        """Test the MultiPolyMesherIo properties."""
        io = MultiPolyMesherIo(())
        self.assertIsInstance(io, MultiPolyMesherIo)

        io.check_topology = True
        self.assertEqual(True, io.check_topology)

        io.return_cell_polygons = False
        self.assertEqual(False, io.return_cell_polygons)

        out_poly = ((0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0))
        pi1 = PolyInput(out_poly)
        pi1.bias = 2.718
        pi2 = PolyInput(out_poly)
        pi2.bias = 0.618
        io.polygon_inputs = (pi1, pi2)
        self.assert_tuple_strings_equal((pi1, pi2), io.polygon_inputs)

        rp1 = RefinePoint((5, 0, -3), 3.1, False)
        rp2 = RefinePoint((-2, -2, 1), -0.4, True)
        ref_points = [rp1, rp2]
        io.refine_points = ref_points
        io_refine_points = io.refine_points
        for i in range(len(io_refine_points)):
            self.assertTrue(ref_points[i] == io_refine_points[i])
 def test_constructor_simple(self):
     """Test the PolyInput constructor."""
     out_poly = ((0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0))
     pi = PolyInput(out_poly)
     self.assertIsInstance(pi, PolyInput)
     self.assertEqual(4, len(pi.outside_polygon))
     self.assertEqual(0, len(pi.inside_polygons))
     self.assertEqual(1.0, pi.bias)
     self.assertEqual(None, pi.size_function)
     self.assertEqual(None, pi.elevation_function)
     self.assertEqual(-1, pi.constant_size_bias)
     self.assertEqual(-1, pi.constant_size_function)
     self.assertEqual(False, pi.remove_internal_four_triangle_points)
Пример #8
0
    def test_check_for_intersections_4(self):
        """Test intersection checks."""
        io = MultiPolyMesherIo(())
        io.check_topology = True

        outside_poly1 = ((0, 0, 0), (0, 100, 0), (100, 100, 0), (100, 0, 0))
        outside_poly2 = ((10, 10, 0), (10, 110, 0), (110, 110, 0), (110, 10,
                                                                    0))
        poly_input1 = PolyInput(outside_poly1)
        poly_input2 = PolyInput(outside_poly2)
        io.polygons = (poly_input1, poly_input2)

        expected = \
            "---Error: Input polygon segments intersect. The segment defined by points 1 and 2 of outer " \
            "polygon 0 intersects with the segment defined by points 0 and 1 of outer polygon 1.\n" \
            "Error: Input polygon segments intersect. The segment defined by points 2 and 3 of outer " \
            "polygon 0 intersects with the segment defined by points 3 and 0 of outer polygon 1.\n" \
            "\n\n"

        (success, errors) = mesh_utils.generate_mesh(io)
        self.assertEqual(False, success)
        self.assertEqual(expected, errors)
Пример #9
0
    def test_check_for_intersections(self):
        """Test intersection checks."""
        io = MultiPolyMesherIo(())
        poly_input = PolyInput(outside_polygon=((0, 0, 0), (100, 0, 0),
                                                (100, 10, 0), (0, -10, 0)))
        io.polygons = (poly_input, )

        expected = \
            "Error: Input polygon segments intersect. The segment defined by points 0 and 1 of outer " \
            "polygon 0 intersects with the segment defined by points 2 and 3 of outer polygon 0.\n"

        (success, errors) = mesh_utils.check_mesh_input_topology(io)
        self.assertEqual(False, success)
        self.assertEqual(expected, errors)
Пример #10
0
    def test_check_for_intersections_2(self):
        """Test intersection checks."""
        io = MultiPolyMesherIo(())
        io.check_topology = True

        outside_poly = ((0, 0, 0), (100, 0, 0), (100, 100, 0), (0, 100, 0))
        inside_polys = (((10, 50, 0), (90, 50, 0), (90, 90, 0), (10, 10, 0)), )
        poly_input = PolyInput(outside_poly, inside_polys)
        io.polygons = (poly_input, )

        expected = \
            "---Error: Input polygon segments intersect. The segment defined by points 0 and 1 of inner " \
            "polygon 0 of outer polygon 0 intersects with the segment defined by points 2 and 3 of inner " \
            "polygon 0 of outer polygon 0.\n" \
            "\n\n"

        (success, errors) = mesh_utils.generate_mesh(io)
        self.assertEqual(False, success)
        self.assertEqual(expected, errors)
Пример #11
0
 def test_generate_mesh_api(self):
     """Test generating a mesh using the api."""
     outside_polygon = [
         (0, 0, 0),
         (1, 0, 0),
         (2, 0, 0),
         (3, 0, 0),
         (3, 1, 0),
         (3, 2, 0),
         (3, 3, 0),
         (2, 3, 0),
         (1, 3, 0),
         (0, 3, 0),
         (0, 2, 0),
         (0, 1, 0),
         (0, 0, 0),
     ]
     polygon_input = PolyInput(outside_polygon=outside_polygon)
     xm_grid = mesher.generate_mesh(polygon_inputs=[polygon_input])
     self.assertEqual(20, xm_grid.cell_count)
     self.assertEqual(100, len(xm_grid.cellstream))
     self.assertEqual(((0, 0, 0), (3, 3, 0)), xm_grid.extents)
Пример #12
0
 def test_generate_mesh_api_with_refine(self):
     """Test generating a mesh with a refine point using the api."""
     outside_polygon = [
         (0, 0, 0),
         (1, 0, 0),
         (2, 0, 0),
         (3, 0, 0),
         (3, 1, 0),
         (3, 2, 0),
         (3, 3, 0),
         (2, 3, 0),
         (1, 3, 0),
         (0, 3, 0),
         (0, 2, 0),
         (0, 1, 0),
         (0, 0, 0),
     ]
     refine_point = RefinePoint((1.5, 1.5, 0), size=0.25)
     polygon_input = PolyInput(outside_polygon=outside_polygon)
     xm_grid = mesher.generate_mesh(polygon_inputs=[polygon_input],
                                    refine_points=[refine_point])
     self.assertEqual(50, xm_grid.cell_count)
     self.assertEqual(250, len(xm_grid.cellstream))
     self.assertEqual(((0, 0, 0), (3, 3, 0)), xm_grid.extents)
    def test_properties(self):
        """Test the PolyInput properties."""
        out_poly = ((0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0))
        pi = PolyInput(out_poly)
        outside_poly = ((1, 2, 0), (5, 2, 0), (5, 9, 0), (1, 9, 0))
        inside_polys = (((3, 3, 0), (2.5, 4, 0), (2, 3, 0)),
                        ((4, 8, 0), (3, 7, 0), (2, 8, 0)))
        poly_corners = (0, 1, 2, 3)
        pts = ((1, 2, 3), (4, 5, 6), (0, 5, 7))
        size_func = InterpLinear(pts)
        elev_func = InterpIdw(pts)
        seed_points = ((3, 3, 0), (4, 3, 0), (4, 8, 0), (3, 8, 0))
        relaxation_method = "spring_relaxation"

        self.assertEqual(4, len(pi.outside_polygon))
        pi.outside_polygon = outside_poly
        self.assert_arrays_equal(outside_poly, pi.outside_polygon)

        self.assertEqual(0, len(pi.inside_polygons))
        pi.inside_polygons = inside_polys
        self.assert_inside_polys_equal(inside_polys, pi.inside_polygons)

        self.assertEqual(0, len(pi.polygon_corners))
        pi.polygon_corners = poly_corners
        self.assert_arrays_equal(poly_corners, pi.polygon_corners)

        self.assertEqual(1.0, pi.bias)
        pi.bias = 0.3
        self.assertEqual(0.3, pi.bias)

        self.assertEqual(None, pi.size_function)
        pi.size_function = size_func
        # self.assertEqual(str(size_func), str(pi.size_function))

        self.assertEqual(None, pi.elevation_function)
        pi.elevation_function = elev_func
        # self.assertEqual(elev_func.to_string(), pi.elevation_function.to_string())

        self.assertEqual(-1, pi.constant_size_bias)
        pi.constant_size_bias = 4.0
        self.assertEqual(4.0, pi.constant_size_bias)

        self.assertEqual(-1, pi.constant_size_function)
        pi.constant_size_function = 1.2
        self.assertEqual(1.2, pi.constant_size_function)

        self.assertEqual(False, pi.remove_internal_four_triangle_points)
        pi.remove_internal_four_triangle_points = True
        self.assertEqual(True, pi.remove_internal_four_triangle_points)

        self.assertEqual(0, len(pi.seed_points))
        pi.seed_points = seed_points
        self.assert_arrays_equal(seed_points, pi.seed_points)

        self.assertEqual("", pi.relaxation_method)
        pi.relaxation_method = relaxation_method
        self.assertEqual(relaxation_method, pi.relaxation_method)