Exemplo n.º 1
0
 def test_parents_property(self):
     root = Sofa.Node("rootNode")
     c1 = root.addChild(Sofa.Node("child1"))
     c2 = root.addChild(Sofa.Node("child2"))
     d = c1.addChild(Sofa.Node("subchild"))
     d = c2.addChild(Sofa.Node("subchild"))
     self.assertEqual(len(d.parents), 1)
     c1.addChild(d)
     self.assertEqual(len(d.parents), 2)
Exemplo n.º 2
0
 def test_messages(self):
     """Test that the message are correctly sended and does not generates exceptions"""
     for fname in ["msg_info", "msg_warning", "msg_deprecated"]:
         f = getattr(Sofa.Helper, fname)
         f("Simple message")
         f("Emitter", "Simple message")
         f("Simple message with attached source info", "sourcefile.py", 10)
         f(Sofa.Node("node"), "Simple message to an object")
         f(Sofa.Node("node"),
           "Simple message to an object with attached source info",
           "sourcefile.py", 10)
Exemplo n.º 3
0
    def test_children_property(self):
        root = Sofa.Node("rootNode")
        c1 = root.addChild(Sofa.Node("child1"))
        self.assertEqual(len(root.children), 1)
        c2 = root.addChild(Sofa.Node("child2"))
        self.assertEqual(len(root.children), 2)

        self.assertEqual(root.children.at(0).name, c1.name)
        self.assertEqual(root.children.at(1).name, c2.name)
        root.children.remove_at(0)
        self.assertEqual(len(root.children), 1)
        self.assertEqual(root.children.at(0).name, c2.name)
Exemplo n.º 4
0
 def test_removeChild(self):
     root = Sofa.Node("rootNode")
     c = root.addChild(Sofa.Node("child1"))
     c2 = root.addChild(Sofa.Node("child2"))
     self.assertEqual(len(root.children), 2)
     self.assertTrue(hasattr(root, "child1"))
     self.assertTrue(hasattr(root, "child2"))
     root.removeChild(c)
     self.assertEqual(len(root.children), 1)
     self.assertFalse(hasattr(root, "child1"))
     root.removeChild("child2")
     self.assertFalse(hasattr(root, "child2"))
     self.assertEqual(len(root.children), 0)
Exemplo n.º 5
0
    def test_DataAsContainerNumpyArrayRepeat(self):
        root = Sofa.Node("rootNode")
        v = numpy.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])
        c = root.createObject("MechanicalObject",
                              name="t",
                              position=v.tolist())

        with c.position.writeable() as wa:
            wa *= 2.0
            self.assertEqual(wa.shape, (4, 3))
            self.assertEqual(wa[0, 0], 0.0)
            self.assertEqual(wa[1, 1], 2.0)
            self.assertEqual(wa[2, 2], 4.0)
            numpy.testing.assert_array_equal(wa, v * 2.0)

        ### Checks that the data are correctly changed in the writeable block
        numpy.testing.assert_array_equal(c.position.toarray(), v * 2.0)

        ### Checks that trying to access wa object in write correctly raise
        ### an error.
        def t(c):
            c[0, 0] = 1.0

        self.assertRaises(ValueError, (lambda c: t(c)), wa)

        ### Checks that the previously defined blocks is correctly re-created.
        with c.position.writeable() as wa:
            wa *= 2.0
            self.assertEqual(wa.shape, (4, 3))
            self.assertEqual(wa[0, 0], 0.0)
            self.assertEqual(wa[1, 1], 4.0)
            self.assertEqual(wa[2, 2], 8.0)
            numpy.testing.assert_array_equal(wa, v * 4.0)
Exemplo n.º 6
0
 def test_data_property(self):
     root = Sofa.Node("rootNode")
     self.assertTrue(hasattr(root, "__data__"))
     self.assertGreater(len(root.__data__), 0)
     self.assertTrue("name" in root.__data__)
     self.assertFalse(hasattr(root.__data__, "invalidEntry"))
     self.assertTrue(isinstance(root.__data__, Sofa.Core.DataDict))
Exemplo n.º 7
0
    def test_animation(self):
        node = Sofa.Node("TestAnimation")
        node.addObject("OglLineAxis")
        node.addObject("RequiredPlugin", name="SofaSparseSolver")
        node.addObject("DefaultAnimationLoop", name="loop")
        node.addObject("EulerImplicit")
        node.addObject("CGLinearSolver", tolerance=1e-12, threshold=1e-12)
        #node.addObject("SparseLDLSolver")

        #object1.addChild( MyForceField("customFF", ks=5.0) )
        a = node.addChild(
            RestShapeObject(MyForceField("customFF", ks=5.0),
                            name="python",
                            position=[[i * 1 - 10.0, 0, 0]
                                      for i in range(200)]))
        a.mechanical.showColor = [1.0, 0.0, 0.0, 1.0]
        b = node.addChild(
            RestShapeObject(CreateObject("RestShapeSpringsForceField",
                                         stiffness=5.0),
                            name="c++",
                            position=[[i * 0.5 - 1.0, 0, 0]
                                      for i in range(1)]))
        b.mechanical.showColor = [1.0, 1.0, 0.0, 1.0]

        return node
Exemplo n.º 8
0
 def test_wrapAround(self):
     n = Sofa.Node("node")
     m = n.addObject("MechanicalObject",
                     position=[[1.0, 1.1, 1.2], [2.0, 2.1, 2.2],
                               [3.0, 3.1, 3.2]])
     c = Vec3(m.position)
     print("Vec3, ", c)
Exemplo n.º 9
0
    def test_getItem(self):
        root = Sofa.Node("root")
        node1 = root.addChild('node1')
        object1 = root.addObject("MechanicalObject", name="object1")
        node2 = node1.addChild('node2')
        object2 = node2.addObject("MechanicalObject", name="object2")

        # All those are allowed syntaxes:
        self.assertEqual(root[""].name, root.name)
        self.assertEqual(root["."].name, root.name)
        self.assertEqual(root[".name"], root.name)
        self.assertEqual(root["name"], root.name)

        self.assertEqual(object1["name"], object1.name)
        self.assertEqual(object1[".name"], object1.name)
        self.assertEqual(object1["."].name, object1.name)
        self.assertEqual(object1[""].name, object1.name)

        self.assertEqual(root["node1"].name, node1.name)
        self.assertEqual(root["object1"], object1)
        self.assertEqual(root["node1.node2"].name, node2.name)
        self.assertEqual(root["name"], root.name)
        self.assertEqual(root["node1.node2.object2.name"], object2.name)

        self.assertEqual(root["node1.node2.object2.name"],
                         root.node1.node2.object2.name)
Exemplo n.º 10
0
 def test_constructorOverriddenWithKWArgs(self):
     root = Sofa.Node("rootNode")
     root.addObject(
         MyController("controller",
                      kval1="value1",
                      kval2="value2",
                      kval3="value3"))
Exemplo n.º 11
0
 def test_objects_property(self):
     root = Sofa.Node("rootNode")
     root.createObject("MechanicalObject", name="name1")
     root.createObject("MechanicalObject", name="name2")
     self.assertEqual(len(root.objects), 2)
     root.createObject("MechanicalObject", name="name2")
     self.assertEqual(len(root.objects), 3)
Exemplo n.º 12
0
 def test_typeName(self):
     root = Sofa.Node("rootNode")
     c = root.createObject("MechanicalObject",
                           name="t",
                           position=[[0, 0, 0], [1, 1, 1], [2, 2, 2]])
     self.assertEqual(c.position.typeName(), "vector<Vec3d>")
     self.assertEqual(c.showColor.typeName(), "RGBAColor")
Exemplo n.º 13
0
 def test_DataArray2DResizeFromArray(self):
     v = [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]]
     root = Sofa.Node("rootNode")
     c = root.createObject("MechanicalObject", name="t", position=v)
     zeros = numpy.zeros((100, 3), dtype=numpy.float64)
     c.position = zeros
     numpy.testing.assert_array_equal(c.position.toarray(), zeros)
Exemplo n.º 14
0
 def test_createObjectWithInvalidParamName(self):
     ## This one should raise an error because of 'v' should rise a type error.
     root = Sofa.Node("rootNode")
     self.assertRaises(TypeError,
                       root.createObject,
                       "MechanicalObject",
                       name="tt",
                       v=[[0, 0, 0], [1, 1, 1], [2, 2, 2]])
Exemplo n.º 15
0
    def test_wrapInvalidSize(self):
        n = Sofa.Node("node")
        m = n.addObject("MechanicalObject")

        def d():
            return Vec3(m.showColor)

        self.assertRaises(AttributeError, d)
Exemplo n.º 16
0
 def test_DataArray2DSetFromList(self):
     v = [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]]
     root = Sofa.Node("rootNode")
     c = root.createObject("MechanicalObject", name="t", position=v)
     c.position = [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]
     numpy.testing.assert_array_equal(c.position.toarray(),
                                      [[1.0, 1.0, 1.0], [2.0, 2.0, 2.0],
                                       [3.0, 3.0, 3.0], [4.0, 4.0, 4.0]])
Exemplo n.º 17
0
 def test_DataAsArray2D(self):
     root = Sofa.Node("rootNode")
     v = [[0, 0, 0], [1, 1, 1], [2, 2, 2]]
     c = root.createObject("MechanicalObject", name="t", position=v)
     self.assertEqual(len(c.position), 3)
     self.assertSequenceEqual(list(c.position[0]), v[0])
     self.assertSequenceEqual(list(c.position[1]), v[1])
     self.assertSequenceEqual(list(c.position[2]), v[2])
Exemplo n.º 18
0
    def test_DataArray2DOperationInPlace(self):
        root = Sofa.Node("rootNode")
        v = numpy.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])
        c = root.addObject("MechanicalObject", name="t", position=v.tolist())
        c.position.value *= 2.0

        numpy.testing.assert_array_equal(c.position.array(), v * 2.0)
        c.position.value += 3.0
        numpy.testing.assert_array_equal(c.position.array(), (v * 2.0) + 3.0)
Exemplo n.º 19
0
    def test_GetAttr(self):
        root = Sofa.Node("rootNode")
        c = root.createChild("child1")
        self.assertTrue(c is not None)
        self.assertTrue(root.child1 is not None)

        o = c.createObject("MechanicalObject", name="mechanical")
        self.assertTrue(o is not None)
        self.assertTrue(root.child1.mechanical is not None)
Exemplo n.º 20
0
 def test_data_property(self):
         root = Sofa.Node("rootNode")
         c = root.addObject("MechanicalObject", name="t", position=[[0,0,0],[1,1,1],[2,2,2]])
         self.assertTrue(hasattr(c, "__data__"))
         self.assertGreater(len(c.__data__), 0)
         self.assertTrue("name" in c.__data__)
         self.assertTrue("position" in c.__data__)
         self.assertFalse(hasattr(c.__data__, "invalidEntry"))
         self.assertTrue(isinstance(c.__data__, Sofa.Core.DataDict))
Exemplo n.º 21
0
    def test_UnknowAttribute(self):
        """ Access a non-existing attribute of a data field so this should trigger AttributeError"""
        root = Sofa.Node("root")  ##< Create a new node
        c = root.addObject("MechanicalObject",
                           name="t")  ##< Create a new object
        p = c.position.value  ##< Retrive its position

        ##< Check that accessing an invalid data field rise an attribute error and nothing
        ##< else.
        self.assertRaises(AttributeError, (lambda aa: aa.unvalid), p)
Exemplo n.º 22
0
    def test_DataArray2DInvalidResizeFromArray(self):
        v = [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]]
        root = Sofa.Node("rootNode")
        c = root.createObject("MechanicalObject", name="t", position=v)
        zeros = numpy.zeros((4, 100), dtype=numpy.float64)

        def d():
            c.position = zeros

        self.assertRaises(IndexError, d)
Exemplo n.º 23
0
    def test_wrapAroundSingleField(self):
        n = Sofa.Node("node")
        m = n.addObject("MechanicalObject", translation=[1.0, 0.0, 0.0])
        c = Vec3(m.translation)
        self.assertEqual(c.tolist(), [1.0, 0.0, 0.0])

        c2 = Vec3(m.translation.value)
        self.assertEqual(c2.tolist(), [1.0, 0.0, 0.0])

        c3 = Vec3(m.translation.array())
        self.assertEqual(c3.tolist(), [1.0, 0.0, 0.0])
Exemplo n.º 24
0
    def test_DataAsContainerNumpyArray(self):
        root = Sofa.Node("rootNode")
        v = numpy.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])
        c = root.addObject("MechanicalObject", name="t", position=v.tolist())

        with c.position.writeableArray() as wa:
            self.assertEqual(wa.shape, (4, 3))
            self.assertEqual(wa[0, 0], 0.0)
            self.assertEqual(wa[1, 1], 1.0)
            self.assertEqual(wa[2, 2], 2.0)
            numpy.testing.assert_array_equal(c.position.array(), v)
Exemplo n.º 25
0
    def test_getsetData(self):
        root = Sofa.Node("root")
        c = root.createObject("MechanicalObject",
                              name="t",
                              position=[[0, 0, 0], [1, 1, 1], [2, 2, 2]])
        c.setToData("newdata", 1.0)
        c.setToDict("newdict", 1.0)

        self.assertTrue(isinstance(c.newdata, float))
        self.assertTrue(isinstance(c.newdict, float))

        self.assertTrue(isinstance(c.getFromData("newdata"), float))
        self.assertTrue(isinstance(c.getFromDict("newdict"), float))
Exemplo n.º 26
0
def RestShapeObject(impl, name="unnamed", position=[]):
        node = Sofa.Node(name)
        c = node.addObject("MechanicalObject", name="mechanical", position=position)
        c.showObject = True
        c.drawMode = 1

        m = node.addObject("UniformMass", name="mass", vertexMass=0.1)
        
        if isinstance(impl, CreateObject): 
                node.createObject(*impl.args, **impl.kwargs)        
        else:        
                d = node.addObject(impl)
        return node
Exemplo n.º 27
0
def main():
    # can be executed from terminal directly:

    # Register all the common component in the factory.
    SofaRuntime.importPlugin("SofaAllCommonComponents")

    _runAsPythonScript = True
    root = Sofa.Node()

    createScene(root)
    Sofa.Simulation.init(root)
    for i in range(0, 360):
        Sofa.Simulation.animate(root, root.dt.value)
        root.te.rotation.value[0] += 1
Exemplo n.º 28
0
    def test_events(self):
        """Test the event system."""
        node = Sofa.Node("root")
        node.addObject("DefaultAnimationLoop", name="loop")
        controller = node.addObject(MyController())

        self.assertTrue(hasattr(controller, "iterations"))

        SingleSimulation.init(node)
        for i in range(10):
            SingleSimulation.animate(node, 0.01)

        self.assertTrue(hasattr(controller, "iterations"))
        self.assertEqual(controller.iterations, 10)
Exemplo n.º 29
0
    def test_createObjectInvalid(self):
        root = Sofa.Node("rootNode")
        c = root.addChild("child1")

        def d():
            c.addObject([1.0, 0.0, 0.0])

        self.assertRaises(TypeError, d)

        def d1():
            c.addObject("NOEXISTINGCOMPONENT")

        self.assertRaises(ValueError, d1)
        root.init()
Exemplo n.º 30
0
    def test_DataArray2DOperation(self):
        root = Sofa.Node("rootNode")
        v = numpy.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])
        c = root.createObject("MechanicalObject",
                              name="t",
                              position=v.tolist())

        c2 = c.position * 2.0
        numpy.testing.assert_array_equal(c.position.toarray(), v)
        numpy.testing.assert_array_equal(c2, v * 2.0)

        c2 = c.position + 2.0
        numpy.testing.assert_array_equal(c.position.toarray(), v)
        numpy.testing.assert_array_equal(c2, v + 2.0)