def test_text_equality(self): t = Text("ABC", Point(20, 20)) t2 = Text("ABC", Point(20, 20)) d1 = Text("ABc", Point(20, 20)) d2 = Text("ABC", Point(20, 21)) d3 = Text("ABC", Point(21, 20)) self.assertEqual(t, t2) self.assertNotEqual(t, d1) self.assertNotEqual(t, d2) self.assertNotEqual(t, d3)
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 )
def test_text_cloning(self): t = Text("ABC", Point(20, 20)) t2 = t.clone() self.assertTrue(t is not t2) self.assertTrue(t.anchor_point == t2.anchor_point) self.assertTrue(t.text == t2.text)
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 )
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_room_encoding_and_decoding(self): r = Room([(1,2), (3, 4), (5, 6)]) self.room1.add_text(Text("Encoded cool text", Point([1,1]))) s1 = json.dumps(self.room1.to_serializable(), indent = 3) d1 = json.loads(s1) r2 = Room.from_serializable(d1) self.assertEqual(self.room1.polygon.points, r2.polygon.points) self.assertEqual(self.room1.texts, r2.texts) s2 = json.dumps(self.room1.to_serializable(), indent = 4) d2 = json.loads(s2) self.assertEqual(d1, d2)
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)
def _extract_texts_from_cartiglio(self, grabber): """ Returns text from the layers who contain the cartiglio. Arguments: - grabber: instance of dxfgrabber result of the dxf parsing. Returns: a list of texts. Auxiliary method for inference from cartiglio """ def get_text(p): return self.sanitize_layer_name( (hasattr(p, "text") and p.text or p.plain_text()) or (hasattr(p, "rawtext") and p.rawtext) or "") return [ Text(get_text(p), Point(p.insert)) for p in grabber.entities if re.match("CARTIGLIO", p.layer, re.I) and ( p.dxftype == "MTEXT" or p.dxftype == "TEXT") ]
def test_room_text_serializable(self): t1 = Text("Encoded cool text", Point([1, 1])) self.assertEqual(t1.to_serializable(), {"text": t1.text, "anchor_point": t1.anchor_point.to_serializable()}) t2 = Text.from_serializable(t1.to_serializable()) self.assertEqual(t1, t2)
def test_room_contains_text(self): t1 = Text("In text!", Point(5, 5)) t2 = Text("Out text!", Point(11, 5)) self.assertTrue(self.room1.contains_text(t1)) self.assertFalse(self.room1.contains_text(t2))
def test_room_text_serializable(self): t1 = Text("Encoded cool text", Point([1,1])) self.assertEqual(t1.to_serializable(), { "text": t1.text, "anchor_point": t1.anchor_point.to_serializable() }) t2 = Text.from_serializable(t1.to_serializable()) self.assertEqual(t1, t2)
def test_room_to_serializable(self): self.polygon1.to_serializable = MagicMock(return_value="pippo_serializzato") self.room1.add_text(Text("Encoded cool text", Point([1,1]))) self.assertEqual(self.room1.to_serializable(), { "polygon": "pippo_serializzato", "texts": serialize_list(self.room1.texts) })