Atlas 0.7.0
Networking protocol for the Worldforge system.
test_glue.py
1#!/usr/bin/env python2
2#test map object glueing
3
4#Copyright (C) 2002 by AIR-IX SUUNNITTELU/Ahiplan Oy
5
6#This library is free software; you can redistribute it and/or
7#modify it under the terms of the GNU Lesser General Public
8#License as published by the Free Software Foundation; either
9#version 2.1 of the License, or (at your option) any later version.
10
11#This library is distributed in the hope that it will be useful,
12#but WITHOUT ANY WARRANTY; without even the implied warranty of
13#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14#Lesser General Public License for more details.
15
16#You should have received a copy of the GNU Lesser General Public
17#License along with this library; if not, write to the Free Software
18#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20debug_flag = 0
21
22from types import *
23from copy import deepcopy
24import string
25import atlas
26import atlas.analyse
27from atlas.transport.file import read_file
28#generated by gen_simple_core.py or from http://purple.worldforge.org/~aloril/atlas/simple_core.atlas
29meadow_map = read_file("../reference/simple_core.atlas")
30#print len(meadow_map), type(meadow_map)
31
32obj_dict = {}
33for obj in meadow_map:
34 obj_dict[obj.id] = obj
35
36
37def get_absolute_points_and_ids(obj):
38 lst = []
39 def combine(a,b):
40 return tuple(a),b
41 dict = {}
42 if hasattr(obj, "_polyline"):
43 lst = lst + list(map(combine, obj._polyline, obj.polyline_ids))
44 if hasattr(obj, "_area") and obj._area:
45 for i in range(len(obj._area)):
46 lst = lst + list(map(combine, obj._area[i], obj.area_ids[i]))
47 if hasattr(obj, "_volume") and obj._volume:
48 for i in range(len(obj._volume)):
49 lst = lst + list(map(combine, obj._volume[i], obj.volume_ids[i]))
50 if not lst:
51 lst = [(tuple(obj._pos), obj.id+".pos")]
52 return lst
53
54def find_glued_ids(vector, obj, point_ids):
55 obj_ok = 0
56 all_ok = 1
57 #point_ids[1] = "brook1.polyline.0"
58 result = []
59 for id in point_ids:
60 obj_id = string.split(id, ".")[0]
61 id2 = obj_id + "._" + string.join(string.split(id, ".")[1:], ".")
62 vector2 = tuple(atlas.resolve_pointer(gm.objects, id2))
63 if obj_id==obj.id:
64 #get absolute position
65 if vector==vector2:
66 obj_ok = 1
67 if debug_flag:
68 print("this ok: %s: %s with %s glue_id from %s" % (obj_id, vector, id, point_ids))
69 else:
70 if debug_flag:
71 print("not this one: %s: %s!=%s with %s glue_id from %s" % (obj_id, vector, vector2, id, point_ids))
72 else:
73 if vector==vector2:
74 result.append(obj_id)
75 if debug_flag:
76 print("other ok: %s <-> %s: %s with %s glue_id from %s" % (obj.id, obj_id, vector, id, point_ids))
77 else:
78 if debug_flag:
79 print("other not ok: %s <-> %s: %s!=%s with %s glue_id from %s" % (obj.id, obj_id, vector, vector2, id, point_ids))
80 all_ok = 0
81 if obj_ok and not all_ok:
82 raise ValueError("object %s has %s as absolute position, but not every other is same position in list: %s" % (obj.id, vector, point_ids))
83 return result
84
85
86def process_glue(msg, obj, point_id, other_id, id_dict, to_process, check_id=1):
87 if other_id in id_dict:
88 obj2 = gm.objects[other_id]
89 point_id2 = id_dict[other_id]
90 to_process.append((obj2, point_id2))
91 if check_id:
92 if point_id!=point_id2:
93 raise ValueError("Not same point_id: %s:%s vs %s:%s" % (obj.id, point_id, obj2.id, point_id2))
94 if debug_flag:
95 print("%s: %s <-%s-> %s" % (msg, obj.id, point_id, obj2.id))
96 else:
97 if debug_flag:
98 print("%s: %s:%s <-> %s:%s" % (msg, obj.id, point_id, obj2.id, point_id2))
99 del id_dict[other_id]
100 return 1
101 return 0
102
103
104
105for obj in meadow_map:
106 if hasattr(obj, "polyline"): obj.polyline_ids = deepcopy(obj.polyline)
107 if hasattr(obj, "area"): obj.area_ids = deepcopy(obj.area)
108 if hasattr(obj, "volume"): obj.volume_ids = deepcopy(obj.volume)
109meadow_map.append(atlas.Object(id="polyline_ids", parents=["polyline"]))
110meadow_map.append(atlas.Object(id="area_ids", parents=["area"]))
111meadow_map.append(atlas.Object(id="volume_ids", parents=["volume"]))
112
113#resolve and fill attributes
114atlas.analyse.fill_attributes(meadow_map)
115
116#calculate absolute locations
117from atlas.geomap import GeoMap
118gm = GeoMap(obj_dict)
119
120if debug_flag:
121 print("-"*60)
122#create dictionary indexed by absolute coordinates
123point_dict = {}
124for obj in list(gm.objects.values()):
125 if hasattr(obj, "glue") and obj.glue:
126 if debug_flag:
127 print(obj.id)
128 for point,id in get_absolute_points_and_ids(obj):
129 dict = point_dict.get(point, {})
130 dict[obj.id] = id
131 point_dict[point] = dict
132 #print obj.id, point, dict
133if debug_flag:
134 print(point_dict)
135
136#make sure that all objects at each point are connected
137for vector, id_dict in list(point_dict.items()):
138 if debug_flag:
139 print("="*60)
140 print(vector, id_dict)
141 print("-"*60)
142 item, point_id = list(id_dict.items())[0]
143 #if id_dict.has_key("path1_2"): item, point_id = "path1_2", id_dict["path1_2"]
144 del id_dict[item]
145 to_process = [(gm.objects[item], point_id)]
146 done_dict = {}
147 while to_process:
148 obj, point_id = to_process.pop()
149 done_dict[obj.id] = point_id
150 for other in obj.glue:
151 if process_glue("implicit", obj, point_id, other, id_dict, to_process):
152 pass
153 else:
154 #explicit glues
155 obj2 = gm.all_objects[other]
156 if obj2.has_parent("glue_entity"):
157 if debug_flag:
158 print("glue_entity:", other, obj2.glue_ids)
159 for glued_point in obj2.glue_ids:
160 other_id_lst = find_glued_ids(vector, obj, glued_point)
161 if debug_flag:
162 print("found:", other_id_lst)
163 for other3 in other_id_lst:
164 process_glue("explicit", obj, point_id, other3, id_dict, to_process, check_id=0)
165 #connect objects at different detail levels as needed
166 process_glue("loc", obj, point_id, obj.loc.id, id_dict, to_process, check_id=0)
167 for obj2 in obj.contains:
168 process_glue("contains", obj, point_id, obj2.id, id_dict, to_process, check_id=0)
169 if id_dict:
170 raise ValueError("not glued: %s for %s" % (id_dict, vector))
def resolve_pointer(base_dict, id)
Definition: __init__.py:314