1 /* -------------------------------------------------------------------------- *
2  *                 OpenSim:  testSerializeOpenSimObjects.cpp                  *
3  * -------------------------------------------------------------------------- *
4  * The OpenSim API is a toolkit for musculoskeletal modeling and simulation.  *
5  * See http://opensim.stanford.edu and the NOTICE file for more information.  *
6  * OpenSim is developed at Stanford University and supported by the US        *
7  * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA    *
8  * through the Warrior Web program.                                           *
9  *                                                                            *
10  * Copyright (c) 2005-2017 Stanford University and the Authors                *
11  * Author(s): Ayman Habib                                                     *
12  *                                                                            *
13  * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
14  * not use this file except in compliance with the License. You may obtain a  *
15  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
16  *                                                                            *
17  * Unless required by applicable law or agreed to in writing, software        *
18  * distributed under the License is distributed on an "AS IS" BASIS,          *
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
20  * See the License for the specific language governing permissions and        *
21  * limitations under the License.                                             *
22  * -------------------------------------------------------------------------- */
23 
24 #include <OpenSim/Auxiliary/auxiliaryTestFunctions.h>
25 #include <OpenSim/Common/osimCommon.h>
26 #include <OpenSim/Simulation/osimSimulation.h>
27 #include <OpenSim/Actuators/osimActuators.h>
28 #include <OpenSim/Analyses/osimAnalyses.h>
29 
30 using namespace OpenSim;
31 using namespace std;
32 
33 void testPropertiesDump(const OpenSim::Object& aObject);
34 
indent(int nSpaces)35 static void indent(int nSpaces) {
36     for (int i=0; i<nSpaces; ++i) cout << " ";
37 }
38 
39 // Recursively dump out contents of an object and its properties.
dumpObj(const Object & obj,int nSpaces)40 static void dumpObj(const Object& obj, int nSpaces) {
41     indent(nSpaces);
42     cout << obj.getConcreteClassName() << " Object "
43          << (obj.getName().empty()?"NONAME":obj.getName())
44          << endl;
45     for (int p=0; p < obj.getNumProperties(); ++p) {
46         const AbstractProperty& ap = obj.getPropertyByIndex(p);
47         indent(nSpaces+2);
48         cout << ap.getName() << "=" << ap.toString() << endl;
49         // Check return values from Property API for debugging purposes
50         bool t1 = ap.isListProperty();
51         bool t2 = ap.isObjectProperty();
52         bool t3 = ap.isOneObjectProperty();
53         bool t4 = ap.isOneValueProperty();
54         string ts = ap.getTypeName();
55         indent(nSpaces+2);
56         cout << "isList, isObject, isOneObject, isOneValue, typeName = " <<
57             t1 <<", "<< t2 <<", "<< t3 <<", "<< t4 <<", "<< ts << endl;
58         if (ap.isObjectProperty()) {
59             for (int i=0; i < ap.size(); ++i)
60                 dumpObj(ap.getValueAsObject(i), nSpaces+4);
61         }
62     }
63 }
64 
65 
main()66 int main()
67 {
68     // Actuators library is not loaded automatically (unless using clang).
69     #if !defined(__clang__)
70         LoadOpenSimLibrary("osimActuators");
71     #endif
72 
73     try {
74         Model testModel;
75         srand((unsigned)time(0));
76 
77         //Test serialization for all ModelComponents
78         ArrayPtrs<OpenSim::ModelComponent> availableComponentTypes;
79         Object::getRegisteredObjectsOfGivenType<OpenSim::ModelComponent>(availableComponentTypes);
80 
81         for (int i=0; i< availableComponentTypes.getSize(); i++){
82             Object* clone = availableComponentTypes[i]->clone();
83             Object* randClone = randomize(clone);
84             try {
85                 ModelComponent* comp = ModelComponent::safeDownCast(randClone);
86                 testModel.addModelComponent(comp);
87             } //Ignore the validity of the property values
88             catch (const InvalidPropertyValue&) {
89                 // const string& errMsg = err.getMessage();
90                 //std::cout << errMsg << std::endl;
91             }
92         }
93 
94         int nc = testModel.getMiscModelComponentSet().getSize();
95         cout << nc << " model components were serialized in testModel." << endl;
96 
97 
98         //Serialize all the components
99         testModel.print("allComponents.osim");
100 
101         Model deserializedModel("allComponents.osim");
102         deserializedModel.print("allComponents_reserialized.osim");
103 
104         nc = deserializedModel.getMiscModelComponentSet().getSize();
105         cout << nc << " model components were deserialized from file." << endl;
106 
107         ASSERT(testModel == deserializedModel,
108             "deserializedModel FAILED to match original model.");
109 
110         //Might as well test cloning and assignment
111         Model* cloneModel = testModel.clone();
112 
113         ASSERT(testModel == *cloneModel,
114             "cloneModel FAILED to match original model.");
115 
116         Model assignedModel = *cloneModel;
117 
118         delete cloneModel;
119 
120         ASSERT(testModel == assignedModel,
121             "assignedModel FAILED to match original model.");
122 
123     }
124     catch (const Exception& e) {
125         e.print(cerr);
126         return 1;
127     }
128     cout << "Done" << endl;
129     return 0;
130 }
131 
testPropertiesDump(const Object & aObject)132 void testPropertiesDump(const Object& aObject)
133 {
134     dumpObj(aObject, 4);
135 }
136