-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathpyRoot_TEST.py
328 lines (260 loc) · 10.1 KB
/
pyRoot_TEST.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# Copyright (C) 2022 Open Source Robotics Foundation
# Licensed under the Apache License, version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from gz.math7 import Vector3d, Pose3d
from sdformat import (Error, Model, Light, Root, SDF_VERSION,
SDFErrorsException, SDF_PROTOCOL_VERSION, World)
import sdformat as sdf
import unittest
class RootTEST(unittest.TestCase):
def test_default_construction(self):
root = Root()
self.assertEqual(SDF_VERSION, root.version())
self.assertFalse(root.world_name_exists("default"))
self.assertFalse(root.world_name_exists(""))
self.assertEqual(0, root.world_count())
self.assertTrue(root.world_by_index(0) == None)
self.assertTrue(root.world_by_index(1) == None)
self.assertEqual(None, root.model())
self.assertEqual(None, root.light())
# self.assertEqual(None, root.Actor())
def test_string_model_sdf_parse(self):
sdf = """<?xml version="1.0"?>
<sdf version="1.8">
<model name='shapes'>
<link name='link'>
<collision name='box_col'>
<geometry>
<box>
<size>3 4 5</size>
</box>
</geometry>
</collision>
</link>
</model>
</sdf>"""
root = Root()
root.load_sdf_string(sdf)
model = root.model()
self.assertNotEqual(None, model)
self.assertEqual("shapes", model.name())
self.assertEqual(1, model.link_count())
link = model.link_by_index(0)
self.assertNotEqual(None, link)
self.assertEqual("link", link.name())
self.assertEqual(1, link.collision_count())
collision = link.collision_by_index(0)
self.assertNotEqual(None, collision)
self.assertEqual("box_col", collision.name())
self.assertEqual(None, root.light())
# self.assertEqual(None, root.Actor())
self.assertEqual(0, root.world_count())
# Test cloning
root2 = copy.deepcopy(root)
model2 = root2.model()
self.assertNotEqual(None, model2)
self.assertEqual("shapes", model2.name())
self.assertEqual(1, model2.link_count())
link2 = model2.link_by_index(0)
self.assertNotEqual(None, link2)
self.assertEqual("link", link2.name())
self.assertEqual(1, link2.collision_count())
collision2 = link2.collision_by_index(0)
self.assertNotEqual(None, collision2)
self.assertEqual("box_col", collision2.name())
self.assertEqual(None, root2.light())
# self.assertEqual(None, root2.Actor())
self.assertEqual(0, root2.world_count())
def test_string_light_sdf_parse(self):
sdf = """<?xml version="1.0"?>"
<sdf version="1.8">"
<light type='directional' name='sun'>"
<direction>-0.5 0.1 -0.9</direction>"
</light>"
</sdf>"""
root = Root()
root.load_sdf_string(sdf)
light = root.light()
self.assertNotEqual(None, light)
self.assertEqual("sun", light.name())
self.assertEqual(None, root.model())
# self.assertEqual(None, root.Actor())
self.assertEqual(0, root.world_count())
# /////////////////////////////////////////////////
# TEST(DOMRoot, StringActorSdfParse)
# {
# std::string sdf = "<?xml version=\"1.0\"?>"
# " <sdf version=\"1.8\">"
# " <actor name='actor_test'>"
# " <pose>0 0 1.0 0 0 0</pose>"
# " <skin>"
# " <filename>/fake/path/to/mesh.dae</filename>"
# " <scale>1.0</scale>"
# " </skin>"
# " <animation name='run'>"
# " <filename>/fake/path/to/mesh.dae</filename>"
# " <scale>0.055</scale>"
# " <interpolate_x>true</interpolate_x>"
# " </animation>"
# " <script>"
# " <loop>true</loop>"
# " <delay_start>5.0</delay_start>"
# " <auto_start>true</auto_start>"
# " </script>"
# " </actor>"
# " </sdf>"
#
# root = Root()
# errors = root.load_sdf_string(sdf)
# self.assertTrue(errors.empty())
#
# const sdf::Actor *actor = root.Actor()
# self.assertNotEqual(None, actor)
# self.assertEqual("actor_test", actor.name())
# self.assertNotEqual(None, actor.Element())
#
# self.assertEqual(None, root.model())
# self.assertEqual(None, root.Light())
# self.assertEqual(0, root.world_count())
# }
def test_set(self):
root = Root()
self.assertEqual(SDF_VERSION, root.version())
root.set_version(SDF_PROTOCOL_VERSION)
self.assertEqual(SDF_PROTOCOL_VERSION, root.version())
def test_frame_semantics_on_move(self):
sdfString1 = """
<sdf version="1.8">
<world name="default">
<frame name="frame1">
<pose>0 1 0 0 0 0</pose>
</frame>
</world>
</sdf>"""
sdfString2 = """
<sdf version="1.8">
<world name="default">
<frame name="frame2">
<pose>1 1 0 0 0 0</pose>
</frame>
</world>
</sdf>"""
def testFrame1(_root):
world = _root.world_by_index(0)
self.assertNotEqual(None, world)
frame = world.frame_by_index(0)
self.assertNotEqual(None, frame)
self.assertEqual("frame1", frame.name())
pose = frame.semantic_pose().resolve()
self.assertEqual(Pose3d(0, 1, 0, 0, 0, 0), pose)
def testFrame2(_root):
world = _root.world_by_index(0)
self.assertNotEqual(None, world)
frame = world.frame_by_index(0)
self.assertNotEqual(None, frame)
self.assertEqual("frame2", frame.name())
pose = frame.semantic_pose().resolve()
self.assertEqual(Pose3d(1, 1, 0, 0, 0, 0), pose)
root1 = Root()
root1.load_sdf_string(sdfString1)
testFrame1(root1)
root2 = Root()
root2.load_sdf_string(sdfString2)
testFrame2(root2)
def test_add_world(self):
root = Root()
self.assertEqual(0, root.world_count())
world = World()
world.set_name("world1")
root.add_world(world)
self.assertEqual(1, root.world_count())
with self.assertRaises(SDFErrorsException) as cm:
root.add_world(world)
errors = cm.exception.errors
self.assertEqual(1, len(errors))
self.assertEqual(sdf.ErrorCode.DUPLICATE_NAME, errors[0].code())
self.assertEqual(1, root.world_count())
root.clear_worlds()
self.assertEqual(0, root.world_count())
root.add_world(world)
self.assertEqual(1, root.world_count())
worldFromRoot = root.world_by_index(0)
self.assertNotEqual(None, worldFromRoot)
self.assertEqual(worldFromRoot.name(), world.name())
def test_mutable_by_index(self):
root = Root()
self.assertEqual(0, root.world_count())
world = World()
world.set_name("world1")
root.add_world(world)
self.assertEqual(1, root.world_count())
# Modify the world
w = root.world_by_index(0)
self.assertNotEqual(None, w)
self.assertEqual("world1", w.name())
w.set_name("world2")
self.assertEqual("world2", root.world_by_index(0).name())
def test_to_element_empty(self):
root = Root()
root2 = Root()
root2.load_sdf_string(root.to_string())
self.assertEqual(SDF_VERSION, root2.version())
def test_to_element_model(self):
root = Root()
# sdf::Actor actor1
# actor1.set_name("actor1")
# root.SetActor(actor1)
light1 = Light()
light1.set_name("light1")
root.set_light(light1)
model1 = Model()
model1.set_name("model1")
root.set_model(model1)
self.assertNotEqual(None, root.model())
self.assertEqual(None, root.light())
# self.assertEqual(None, root.Actor())
self.assertEqual(0, root.world_count())
root2 = Root()
try:
root2.load_sdf_string(root.to_string())
except SDFErrorsException:
pass
self.assertEqual(SDF_VERSION, root2.version())
self.assertNotEqual(None, root2.model())
self.assertEqual("model1", root2.model().name())
# ASSERT_EQ(None, root2.Actor())
self.assertEqual(None, root2.light())
self.assertEqual(0, root2.world_count())
def test_element_to_light(self):
root = Root()
model1 = Model()
model1.set_name("model1")
root.set_model(model1)
# sdf::Actor actor1
# actor1.set_name("actor1")
# root.SetActor(actor1)
light1 = Light()
light1.set_name("light1")
root.set_light(light1)
self.assertNotEqual(None, root.light())
self.assertEqual(None, root.model())
# self.assertEqual(None, root.Actor())
self.assertEqual(0, root.world_count())
root2 = Root()
root2.load_sdf_string(root.to_string())
self.assertEqual(SDF_VERSION, root2.version())
self.assertNotEqual(None, root2.light())
self.assertEqual("light1", root2.light().name())
self.assertEqual(None, root2.model())
# ASSERT_EQ(None, root2.Actor())
self.assertEqual(0, root2.world_count())
if __name__ == '__main__':
unittest.main()