def test_remove_fragments(self, client_type): """ Remove a fragment. This test is basically the integration test for 'test_dibbler.test_updateFragments_partial'. """ # Get the client for this test. client = self.clients[client_type] # Convenience. objID = 1 # The original template has the following three fragments: frags_orig = { 'fname_1': getFragRaw(), 'fname_2': getFragDae(), 'fname_3': getFragRaw(), } t1 = getTemplate('t1', fragments=frags_orig) # Add a new template and spawn it. assert client.addTemplates([t1]).ok new_obj = {'templateID': t1.aid, 'rbs': {'position': (1, 1, 1), 'velocityLin': (-1, -1, -1)}} assert client.spawn([new_obj]) == (True, None, [objID]) # Query the fragment geometries and Body State to verify that both # report three fragments. ret = client.getFragments([objID]) assert ret.ok and len(ret.data[objID]) == 3 ret = client.getObjectStates(objID) assert ret.ok and len(ret.data[objID]['frag']) == 3 # Update the fragments as follows: keep the first intact, remove the # second, and modify the third one. frags_new = { 'fname_2': getFragNone()._asdict(), 'fname_3': getFragDae()._asdict() } assert client.setFragments({objID: frags_new}).ok # After the last update there must now only be two fragments. ret = client.getFragments([objID]) assert ret.ok and len(ret.data[objID]) == 2 ret = client.getObjectStates(objID) assert ret.ok and len(ret.data[objID]['frag']) == 2
def test_spawn_and_get_state_variables(self, client_type): """ Spawn a new object and query its state variables. """ # Get the client for this test. client = self.clients[client_type] # Constants and parameters for this test. templateID, objID_1 = '_templateEmpty', 1 # Query the state variables for a non existing object. objID = 100 assert client.getRigidBodies(objID) == (True, None, {objID: None}) del objID # Instruct Clerk to spawn a new object. Its objID must be '1'. pos, vlin = (0, 1, 2), (-3, 4, -5) body = getRigidBody(position=pos, velocityLin=vlin) init = { 'templateID': templateID, 'rbs': {'position': body.position, 'velocityLin': body.velocityLin}, } ret = client.spawn([init]) assert ret.ok and ret.data == [objID_1] # The body parameters of the new object must match the inital state # (plus the tweaks provided to the spawn command). ret = client.getRigidBodies(objID_1) assert ret.ok and (set(ret.data.keys()) == {1}) assert ret.data[objID_1]['rbs'].position == pos assert ret.data[objID_1]['rbs'].velocityLin == vlin # Same test but this time get all of them. assert client.getRigidBodies(None) == ret # Query just the state variables instead of the entire rigid body. assert client.getObjectStates(None) == client.getObjectStates([objID_1]) ret = client.getObjectStates([objID_1]) assert ret.ok r = ret.data[objID_1] assert set(r.keys()) == set(['rbs', 'frag']) r = ret.data[objID_1]['rbs'] assert r['position'] == list(pos) assert r['velocityLin'] == list(vlin)
def test_update_FragmentStates(self, client_type): """ Query and modify fragment states. Note that fragment states are updated via 'setFragments'. """ # Get the client for this test. client = self.clients[client_type] # Convenience. objID = 1 # Add a new template and spawn it. temp = getTemplate('t1', fragments={'bar': getFragRaw()}) assert client.addTemplates([temp]).ok new_obj = {'templateID': temp.aid, 'rbs': {'position': (1, 1, 1), 'velocityLin': (-1, -1, -1)}} ret = client.spawn([new_obj]) assert ret.ok and ret.data == [objID] del temp, new_obj, ret # Query the Body State to get the Fragment States. Then verify the # Fragment State named 'bar'. ret = client.getObjectStates(objID) ref = {'bar': {'scale': 1, 'position': [0, 0, 0], 'rotation': [0, 0, 0, 1]}} assert ret.ok assert ret.data[objID]['frag'] == ref # Modify and update the fragment states in Azrael, then query and # verify it worked. newStates = {objID: {'bar': {'scale': 2.2, 'position': [1, 2, 3], 'rotation': [1, 0, 0, 0]}}} assert client.setFragments(newStates).ok ret = client.getObjectStates(objID) assert ret.ok ret = ret.data[objID]['frag']['bar'] assert ret == newStates[objID]['bar']
def test_create_constraints_with_physics(self, client_type): """ Spawn two rigid bodies and define a Point2Point constraint among them. Then apply a force onto one of them and verify the second one moves accordingly. """ # Reset the constraint database. igor = azrael.igor.Igor() assert igor.reset().ok # Get the client for this test. client = self.clients[client_type] # Reset the database and instantiate a Leonard. leo = getLeonard(azrael.leonard.LeonardBullet) # Spawn two bodies. pos_a, pos_b = [-2, 0, 0], [2, 0, 0] new_objs = [ {'templateID': '_templateSphere', 'rbs': {'position': pos_a}}, {'templateID': '_templateSphere', 'rbs': {'position': pos_b}}, ] id_1, id_2 = 1, 2 assert client.spawn(new_objs) == (True, None, [id_1, id_2]) # Verify the position of the bodies. ret = client.getObjectStates([id_1, id_2]) assert ret.ok assert ret.data[id_1]['rbs']['position'] == pos_a assert ret.data[id_2]['rbs']['position'] == pos_b # Define- and add the constraints. con = [getP2P(rb_a=id_1, rb_b=id_2, pivot_a=pos_b, pivot_b=pos_a)] assert client.addConstraints(con) == (True, None, 1) # Apply a force that will pull the left object further to the left. # However, both objects must move the same distance in the same # direction because they are now linked together. assert client.setForce(id_1, [-10, 0, 0]).ok leo.processCommandsAndSync() leo.step(1.0, 60) # Query the object positions. Due to some database timings is sometimes # happen that the objects appear to not have moved. In that case retry # the query a few times before moving to the comparison. for ii in range(10): assert ii < 9 # Query the objects and put their positions into convenience # variables. ret = client.getRigidBodies([id_1, id_2]) pos_a2 = ret.data[id_1]['rbs'].position pos_b2 = ret.data[id_2]['rbs'].position # Exit this loop if both objects have moved. if (pos_a != pos_a2) and (pos_b != pos_b2): break time.sleep(0.1) # Verify that the objects have moved to the left and maintained their # distance. delta_a = np.array(pos_a2) - np.array(pos_a) delta_b = np.array(pos_b2) - np.array(pos_b) assert delta_a[0] < pos_a[0] assert np.allclose(delta_a, delta_b)