Example #1
0
 def test_polygon_equality(self):
     p2 = Polygon()
     p2.points = self.polygon1.points
     p2.anchor_point = self.polygon1.anchor_point
     self.assertEqual(self.polygon1.points, p2.points)
     self.assertEqual(self.polygon1.anchor_point, p2.anchor_point)
     self.assertEqual(self.polygon1, p2)
 def test_polygon_equality(self):
    p2 = Polygon();
    p2.points = self.polygon1.points
    p2.anchor_point = self.polygon1.anchor_point
    self.assertEqual(self.polygon1.points, p2.points)
    self.assertEqual(self.polygon1.anchor_point, p2.anchor_point)
    self.assertEqual(self.polygon1, p2)
Example #3
0
    def setUp(self):
        self.polygon1 = Polygon.from_relative_coordinates((100, 200),
                                                          [(0, 0), (10, 0),
                                                           (10, 10), (0, 10)])

        self.polygon2 = Polygon.from_absolute_coordinates([(-20, 30), (0, -10),
                                                           (20, -30),
                                                           (40, -10)])
   def test_is_self_crossing(self):
      l_shaped = Polygon.from_absolute_coordinates(
         [ (0, 0), (10, 0), (10, 10), (5, 10), (5, 5), (0,5), (0,0) ] )
      crossing = Polygon.from_absolute_coordinates(
         [ (0, 0),          (10, 10), (5, 10), (5, 5), (0,5) ] )

      self.assertFalse(l_shaped.is_self_crossing())
      self.assertTrue(crossing.is_self_crossing())
      print(crossing.is_self_crossing())
   def setUp(self):
      self.polygon1 = Polygon.from_relative_coordinates(
         (100, 200),
         [ (0, 0), (10, 0), (10, 10), (0, 10) ]
      )

      self.polygon2 = Polygon.from_absolute_coordinates(
         [ (-20, 30), (0, -10), (20, -30), (40, -10) ]
      )
Example #6
0
    def test_is_self_crossing(self):
        l_shaped = Polygon.from_absolute_coordinates([(0, 0), (10, 0),
                                                      (10, 10), (5, 10),
                                                      (5, 5), (0, 5), (0, 0)])
        crossing = Polygon.from_absolute_coordinates([(0, 0), (10, 10),
                                                      (5, 10), (5, 5), (0, 5)])

        self.assertFalse(l_shaped.is_self_crossing())
        self.assertTrue(crossing.is_self_crossing())
        print(crossing.is_self_crossing())
   def setUp(self):
      # 10x10 square beginning on origin
      self.polygon1 = Polygon.from_relative_coordinates((0, 0), [(0,0),(10,0),(10,10),(0,10)])

      # 10x10 diamond shape centered at origin
      self.polygon2 = Polygon.from_relative_coordinates((0, 0), [(10,0), (0, 10), (-10, 0), (0, -10)])

      # L-shaped polygon
      self.polygon3 = Polygon.from_relative_coordinates((0, 0), [(0,0),(10,0),(10,5),(5,5),(5,10),(0,10)])

      self.polygon4 = Polygon.from_absolute_coordinates([(0,0),(5,0),(5,5),(10,5),(10,10),(0,10)])
Example #8
0
   def _extract_entities(self):
      self._rooms          = []
      self._texts          = []
      self._wall_lines     = []
      self._window_lines   = []

      for ent in self._grabber.entities:
         if self._is_valid_room(ent):
            points = [(p[0], -p[1]) for p in ent.points]

            polygon = Polygon.from_absolute_coordinates(points)
            polygon.ensure_is_closed(tollerance = 0.8)
            polygon.simplify_close_points(tollerance = 0.8)

            if polygon.is_self_crossing():
               Logger.warning("Self crossing room is not valid: "+str(polygon))
               continue

            self._rooms.append(
               Room(
                  polygon
               )
            )
         elif self._is_valid_text(ent):
            self._texts.append(
               Text(
                  ent.plain_text().strip(), Point(ent.insert[0], -ent.insert[1])
               )
            )
         elif self._is_valid_wall_line(ent):
            start = Point(ent.start[0], -ent.start[1])
            end   = Point(ent.end[0], -ent.end[1])
            line  = Segment(start, end)
            self._wall_lines.append( line )

         elif self._is_valid_wall_polyline(ent):
            points = [(p[0], -p[1]) for p in ent.points]
            polygon = Polygon.from_relative_coordinates((0,0), points)
            polygon.ensure_is_closed(tollerance = 1)
            polygon.simplify_close_points(tollerance = 1)

            self._wall_lines.extend( polygon.as_segment_list() )

         elif self._is_valid_window_line(ent):
            start = Point(ent.start[0], -ent.start[1])
            end   = Point(ent.end[0], -ent.end[1])
            line  = Segment(start, end)
            self._window_lines.append( line )
Example #9
0
   def setUp(self):
      # 10x10 square beginning on origin
      self.polygon1 = Polygon.from_absolute_coordinates([(0,0),(10,0),(10,10),(0,10)])
      self.room1 = Room(self.polygon1,
         [
            Text("1234",Point(3,3)),
            Text("Super Cool",Point(4,7)),
            Text("Corner Text!",Point(1,10))
         ])

      # 10x10 diamond shape centered at origin
      self.polygon2 = Polygon.from_absolute_coordinates([(10,0), (0, 10), (-10, 0), (0, -10)])
      self.room2 = Room(self.polygon2)

      # L-shaped room
      self.polygon3 = Polygon.from_absolute_coordinates([(0,0),(10,0),(10,5),(5,5),(5,10),(0,10)])
      self.room3 = Room(self.polygon3)
   def test_point_to_right_of_line(self):
      self.assertTrue(Polygon._compare_line_and_point( Point(10, 0), Point(0, 10), Point(9.9, 9.9)) > 0 )
      self.assertTrue(Polygon._compare_line_and_point( Point(0, 0), Point(1, 9), Point(1, 2)) > 0 )
      self.assertTrue(Polygon._compare_line_and_point( Point(0, 0), Point(1, 9), Point(1, 8)) > 0 )
      self.assertTrue(Polygon._compare_line_and_point( Point(0, 0), Point(1, 9), Point(1, 8.999)) > 0 )

      # Diagonal line
      self.assertFalse(Polygon._compare_line_and_point( Point(1, 1), Point(9, 9), Point(1, 2)) > 0 )
      self.assertFalse(Polygon._compare_line_and_point( Point(0, 0), Point(9, 9), Point(1, 8)) > 0 )
      self.assertFalse(Polygon._compare_line_and_point( Point(9, 9), Point(5, 5), Point(1, 6)) > 0 )
      self.assertTrue(Polygon._compare_line_and_point( Point(0, 0), Point(2, 10), Point(1, 5)) == 0 )

      # Horizontal line with point aligned
      self.assertTrue(Polygon._compare_line_and_point( Point(0, 0), Point(10, 0), Point(4, 0)) == 0 )

      # vertical line with point aligned
      self.assertTrue(Polygon._compare_line_and_point( Point(0, 0), Point(0, 10), Point(0, 5)) == 0 )
Example #11
0
    def test_simplify_close_points(self):
        p1 = Polygon.from_relative_coordinates(Point(
            0, 0), [(-20, 30), (-20.35, 30.25), (0, -10), (-0.3, -10.36),
                    (40, -10)]).simplify_close_points()

        self.assertEqual(p1.points, [(-20, 30), (0, -10), (40, -10)])

        p2 = Polygon.from_relative_coordinates(Point(
            0, 0), [(-20, 30), (-20.25, 30.15), (0, -10), (-0.13, -10.16),
                    (15, 15), (-20, 30)]).simplify_close_points()

        self.assertEqual(p2.points, [(0, -10), (15, 15), (-20, 30), (0, -10)])

        p3 = Polygon.from_relative_coordinates(Point(
            0, 0), [(0, 0), (0, 0.49), (0, 0.495), (0, 0.396), (0, 0.9),
                    (0, 1.3), (0, 1.41)]).simplify_close_points()

        self.assertEqual(p3.points, [(0, 0), (0, 0.9), (0, 1.41)])
   def test_simplify_close_points(self):
      p1 = Polygon.from_relative_coordinates( Point(0,0),
         [ (-20, 30), (-20.35, 30.25), (0, -10), (-0.3, -10.36), (40, -10) ]
      ).simplify_close_points()

      self.assertEqual(p1.points, [ (-20, 30), (0, -10), (40, -10) ])

      p2 = Polygon.from_relative_coordinates( Point(0,0),
         [ (-20, 30), (-20.25, 30.15), (0, -10), (-0.13, -10.16), (15, 15), (-20, 30) ]
      ).simplify_close_points()

      self.assertEqual(p2.points, [ (0, -10), (15, 15), (-20, 30), (0, -10) ])

      p3 = Polygon.from_relative_coordinates( Point(0,0),
         [ (0, 0), (0, 0.49), (0, 0.495), (0, 0.396), (0, 0.9), (0, 1.3), (0, 1.41) ]
      ).simplify_close_points()

      self.assertEqual(p3.points, [ (0, 0), (0, 0.9), (0, 1.41) ])
Example #13
0
    def _extract_entities(self):
        self._rooms = []
        self._texts = []
        self._wall_lines = []
        self._window_lines = []

        for ent in self._grabber.entities:
            if self._is_valid_room(ent):
                points = [(p[0], -p[1]) for p in ent.points]

                polygon = Polygon.from_absolute_coordinates(points)
                polygon.ensure_is_closed(tollerance=0.8)
                polygon.simplify_close_points(tollerance=0.8)

                if polygon.is_self_crossing():
                    Logger.warning("Self crossing room is not valid: " +
                                   str(polygon))
                    continue

                self._rooms.append(Room(polygon))
            elif self._is_valid_text(ent):
                self._texts.append(
                    Text(ent.plain_text().strip(),
                         Point(ent.insert[0], -ent.insert[1])))
            elif self._is_valid_wall_line(ent):
                start = Point(ent.start[0], -ent.start[1])
                end = Point(ent.end[0], -ent.end[1])
                line = Segment(start, end)
                self._wall_lines.append(line)

            elif self._is_valid_wall_polyline(ent):
                points = [(p[0], -p[1]) for p in ent.points]
                polygon = Polygon.from_relative_coordinates((0, 0), points)
                polygon.ensure_is_closed(tollerance=1)
                polygon.simplify_close_points(tollerance=1)

                self._wall_lines.extend(polygon.as_segment_list())

            elif self._is_valid_window_line(ent):
                start = Point(ent.start[0], -ent.start[1])
                end = Point(ent.end[0], -ent.end[1])
                line = Segment(start, end)
                self._window_lines.append(line)
Example #14
0
    def setUp(self):
        # 10x10 square beginning on origin
        self.polygon1 = Polygon.from_absolute_coordinates([(0, 0), (10, 0),
                                                           (10, 10), (0, 10)])
        self.room1 = Room(self.polygon1, [
            Text("1234", Point(3, 3)),
            Text("Super Cool", Point(4, 7)),
            Text("Corner Text!", Point(1, 10))
        ])

        # 10x10 diamond shape centered at origin
        self.polygon2 = Polygon.from_absolute_coordinates([(10, 0), (0, 10),
                                                           (-10, 0), (0, -10)])
        self.room2 = Room(self.polygon2)

        # L-shaped room
        self.polygon3 = Polygon.from_absolute_coordinates([(0, 0), (10, 0),
                                                           (10, 5), (5, 5),
                                                           (5, 10), (0, 10)])
        self.room3 = Room(self.polygon3)
Example #15
0
    def test_floor_encoding_and_decoding(self):
        p2 = Polygon.from_absolute_coordinates([(12, 0), (22, 0), (22, 10), (12, 10)])
        r2 = Room(p2)
        f1 = Floor("Building cool name", "Floor cool name", [self.room1, r2])

        f_dump = json.dumps(f1.to_serializable())
        f_load = json.loads(f_dump)

        f2 = Floor.from_serializable(f_load)

        self.assertEqual(f1, f2)
Example #16
0
   def test_floor_room_addition(self):
      self.f.add_room(self.room1)

      self.assertEqual(self.f.rooms[0], self.room1)

      p2 = Polygon.from_absolute_coordinates( [(4, 4), (22, 14), (53, 53)] )
      r2 = Room(p2)
      self.f.add_room(r2)

      self.assertEqual(len(self.f.rooms), 2)
      self.assertTrue( r2 in self.f.rooms )
Example #17
0
   def test_floor_encoding_and_decoding(self):
      p2 = Polygon.from_absolute_coordinates([(12,0),(22,0),(22,10),(12,10)])
      r2 = Room(p2)
      f1 = Floor("Building cool name","Floor cool name", [self.room1,r2])

      f_dump = json.dumps(f1.to_serializable())
      f_load = json.loads(f_dump)

      f2 = Floor.from_serializable(f_load)

      self.assertEqual(f1,f2)
Example #18
0
   def test_calculate_scale_amount_and_trasform(self):
      polygon = Polygon.from_absolute_coordinates(
            [(0,0),(1024,0),(1024,1024),(2048,1024),(2048,2048),(0,2048)]
         )
      room = Room(polygon)
      f = Floor("Pippo", "disneyland", [room])
      self.assertEqual(f.max_output_size / 2048, f.calculate_scale_amount())

      f.normalize()
      for point in f.rooms[0].polygon.points:
         self.assertTrue(point.x <= f.max_output_size)
         self.assertTrue(point.y <= f.max_output_size)
Example #19
0
 def test_floor_equal(self):
    p2 = Polygon.from_absolute_coordinates([(12,0),(22,0),(22,10),(12,10)])
    r2 = Room(p2)
    floor = Floor("Building 1", "Floor1", [self.room1,r2])
    floor2 = Floor("Building 1", "Floor1",[self.room1,r2])
    self.assertEqual(floor,floor2)
    floor3 = Floor("Building 1", "Floor", [self.room1,r2])
    self.assertNotEqual(floor, floor3)
    floor3 = Floor("Building", "Floor1", [self.room1,r2])
    self.assertNotEqual(floor, floor3)
    floor3 = Floor("Building 1", "Floor1", [self.room1])
    self.assertNotEqual(floor, floor3)
Example #20
0
   def test_floor_to_serializable(self):
      p2 = Polygon.from_absolute_coordinates([(12,0),(22,0),(22,10),(12,10)])
      r2 = Room(p2)
      f  = Floor("Building cool name","Floor cool name", [self.room1, r2])

      self.assertEqual( f.to_serializable() ,
         {
            "walls"     : [],
            "windows"   : [],
            "b_id"      : f.b_id,
            "f_id"      : f.f_id,
            "rooms"     : [self.room1.to_serializable(), r2.to_serializable()]
         })
Example #21
0
    def _create_polygon(klass, poly):
        """
      Create a polygon from a room's polyline.

      Arguments:
      - poly: a dictionary representing the room's polygon informations.

      Returns:
      - a polygon object.
      """
        polygon = Polygon.from_serializable(poly)
        polygon.absolutize()
        return polygon
Example #22
0
   def _create_polygon(klass, poly):
      """
      Create a polygon from a room's polyline.

      Arguments:
      - poly: a dictionary representing the room's polygon informations.

      Returns:
      - a polygon object.
      """
      polygon  = Polygon.from_serializable(poly)
      polygon.absolutize()
      return polygon
Example #23
0
   def test_associate_text_to_rooms(self):
      p2 = Polygon.from_absolute_coordinates([(12,0),(22,0),(22,10),(12,10)])
      r2 = Room(p2)
      t1 = Text("Text room 1", Point(5,5))
      t2 = Text("Text room 2", Point(15,8))
      t3 = Text("Text outside",Point(11,5))

      floor = Floor("Building 1", "Floor1",[self.room1, r2])
      floor.associate_room_texts([t1,t2,t3])

      self.assertEqual( len(self.room1.texts), 1 )
      self.assertTrue( len(r2.texts) == 1 )
      self.assertTrue( t1 in self.room1.texts )
      self.assertTrue( t2 in r2.texts )
Example #24
0
    def test_point_to_right_of_line(self):
        self.assertTrue(
            Polygon._compare_line_and_point(Point(10, 0), Point(0, 10),
                                            Point(9.9, 9.9)) > 0)
        self.assertTrue(
            Polygon._compare_line_and_point(Point(0, 0), Point(1, 9),
                                            Point(1, 2)) > 0)
        self.assertTrue(
            Polygon._compare_line_and_point(Point(0, 0), Point(1, 9),
                                            Point(1, 8)) > 0)
        self.assertTrue(
            Polygon._compare_line_and_point(Point(0, 0), Point(1, 9),
                                            Point(1, 8.999)) > 0)

        # Diagonal line
        self.assertFalse(
            Polygon._compare_line_and_point(Point(1, 1), Point(9, 9),
                                            Point(1, 2)) > 0)
        self.assertFalse(
            Polygon._compare_line_and_point(Point(0, 0), Point(9, 9),
                                            Point(1, 8)) > 0)
        self.assertFalse(
            Polygon._compare_line_and_point(Point(9, 9), Point(5, 5),
                                            Point(1, 6)) > 0)
        self.assertTrue(
            Polygon._compare_line_and_point(Point(0, 0), Point(2, 10),
                                            Point(1, 5)) == 0)

        # Horizontal line with point aligned
        self.assertTrue(
            Polygon._compare_line_and_point(Point(0, 0), Point(10, 0),
                                            Point(4, 0)) == 0)

        # vertical line with point aligned
        self.assertTrue(
            Polygon._compare_line_and_point(Point(0, 0), Point(0, 10),
                                            Point(0, 5)) == 0)
Example #25
0
    def test_floor_to_serializable(self):
        p2 = Polygon.from_absolute_coordinates([(12, 0), (22, 0), (22, 10), (12, 10)])
        r2 = Room(p2)
        f = Floor("Building cool name", "Floor cool name", [self.room1, r2])

        self.assertEqual(
            f.to_serializable(),
            {
                "walls": [],
                "windows": [],
                "b_id": f.b_id,
                "f_id": f.f_id,
                "rooms": [self.room1.to_serializable(), r2.to_serializable()],
            },
        )
Example #26
0
    def setUp(self):
        # 10x10 square beginning on origin
        self.polygon1 = Polygon.from_relative_coordinates((0, 0), [(0, 0),
                                                                   (10, 0),
                                                                   (10, 10),
                                                                   (0, 10)])

        # 10x10 diamond shape centered at origin
        self.polygon2 = Polygon.from_relative_coordinates((0, 0), [(10, 0),
                                                                   (0, 10),
                                                                   (-10, 0),
                                                                   (0, -10)])

        # L-shaped polygon
        self.polygon3 = Polygon.from_relative_coordinates((0, 0), [(0, 0),
                                                                   (10, 0),
                                                                   (10, 5),
                                                                   (5, 5),
                                                                   (5, 10),
                                                                   (0, 10)])

        self.polygon4 = Polygon.from_absolute_coordinates([(0, 0), (5, 0),
                                                           (5, 5), (10, 5),
                                                           (10, 10), (0, 10)])
Example #27
0
   def test_associate_text_to_rooms2(self):
      p2 = Polygon.from_absolute_coordinates([(6,0),(12,0),(12,10),(11,10),(11,4),(6,4)])
      r2 = Room(p2)
      t1_1     = Text("Text room 1",Point(2,2))
      t1_2     = Text("Text room 1",Point(8,8))
      t2_1     = Text("Text room 2",Point(7,2))
      t2_2     = Text("Text room 2",Point(11,8))
      t_none   = Text("Text none",Point(5,12))

      floor = Floor("Building 1", "Floor1",[ self.room1,r2])
      floor.associate_room_texts([t1_1,t1_2,t2_1,t2_2,t_none])

      self.assertTrue( len(self.room1.texts) == 2 )
      self.assertTrue( len(r2.texts) == 2 )
      self.assertTrue( t1_1 in self.room1.texts )
      self.assertTrue( t1_2 in self.room1.texts )
      self.assertTrue( t2_1 in r2.texts )
      self.assertTrue( t2_2 in r2.texts )
      self.assertTrue( t1_1 in self.room1.texts )
      self.assertTrue( t_none not in self.room1.texts )
Example #28
0
 def setUp(self):
     self.polygon1 = Polygon.from_absolute_coordinates([(1, 2), (3, 4), (5, 6)])
     self.room1 = Room(self.polygon1)
Example #29
0
 def setUp(self):
    self.polygon1 = Polygon.from_absolute_coordinates([(1,2), (3, 4), (5, 6)])
    self.room1 = Room(self.polygon1)
Example #30
0
 def test_polygon_from_serializable(self):
     p_dump = json.dumps(self.polygon1.to_serializable())
     p_load = json.loads(p_dump)
     pol2 = Polygon.from_serializable(p_load)
     self.assertEqual(self.polygon1, pol2)
Example #31
0
 def test_polygon_from_serializable(self):
    p_dump = json.dumps(self.polygon1.to_serializable())
    p_load = json.loads(p_dump)
    pol2 = Polygon.from_serializable(p_load)
    self.assertEqual(self.polygon1, pol2)
Example #32
0
 def setUp(self):
    self.f = Floor("Pippo", "disneyland")
    self.polygon1 = Polygon.from_absolute_coordinates([(0,0),(5,0),(5,5),(10,5),(10,10),(0,10)])
    self.room1 = Room(self.polygon1)