1 /* -------------------------------------------------------------------------- *
2  *                      OpenSim:  PropertyTransform.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 /*  Author: Ayman Habib
25  */
26 
27 
28 //============================================================================
29 // INCLUDES
30 //============================================================================
31 #include "PropertyTransform.h"
32 
33 
34 
35 
36 using namespace OpenSim;
37 using namespace std;
38 
39 
40 //=============================================================================
41 // CONSTRUCTOR(S)
42 //=============================================================================
43 //_____________________________________________________________________________
44 /**
45  * Constructor.
46  */
47 PropertyTransform::
PropertyTransform(const string & aName,const SimTK::Transform & aTransform)48 PropertyTransform(const string &aName,
49     const SimTK::Transform& aTransform) :
50 PropertyDblArray(aName, OpenSim::Array<double>(0., 6)),
51     _transform(aTransform)
52 {
53     setType(Transform);
54     getRotationsAndTranslationsAsArray6(&_array[0]);
55     setAllowableListSize(6);
56 }
57 //_____________________________________________________________________________
58 /**
59  * Constructor.
60  */
61 PropertyTransform::
PropertyTransform(const string & aName,const Array<double> & aArray)62 PropertyTransform(const string &aName,
63     const Array<double> &aArray) :
64 PropertyDblArray(aName, aArray)
65 {
66     setType(Transform);
67     assert(aArray.getSize()==6);
68     _transform.updR().setRotationToBodyFixedXYZ(SimTK::Vec3::getAs(&aArray[0]));
69     _transform.updP() = SimTK::Vec3::getAs(&aArray[3]);
70     setAllowableListSize(6);
71 }
72 //_____________________________________________________________________________
73 /**
74  * Default Constructor.
75  */
76 PropertyTransform::
PropertyTransform()77 PropertyTransform() :
78     PropertyDblArray("TransformPropertyName", OpenSim::Array<double>(0., 6))
79 {
80     setType(Transform);
81     setAllowableListSize(6);
82 }
83 //_____________________________________________________________________________
84 /**
85  * Copy constructor.
86  *
87  * @param aProperty Property_Deprecated to be copied.
88  */
PropertyTransform(const PropertyTransform & aProperty)89 PropertyTransform::PropertyTransform(const PropertyTransform &aProperty) :
90     PropertyDblArray(aProperty)
91 {
92     _transform = aProperty._transform;
93 }
94 //_____________________________________________________________________________
95 /**
96  * Construct and return a copy of this property.
97  * The property is allocated using the new operator, so the caller is
98  * responsible for deleting the returned object.
99  *
100  * @return Copy of this property.
101  */
clone() const102 PropertyTransform* PropertyTransform::clone() const
103 {
104     PropertyTransform *property = new PropertyTransform(*this);
105     return(property);
106 }
107 
108 
109 //=============================================================================
110 // OPERATORS
111 //=============================================================================
112 //-----------------------------------------------------------------------------
113 // ASSIGNMENT
114 //-----------------------------------------------------------------------------
115 //_____________________________________________________________________________
116 /**
117  * Assign this property to another.
118  *
119  * @param aProperty Property_Deprecated to which to assign this property.
120  * @return Reference to this property.
121  */
122 PropertyTransform& PropertyTransform::
operator =(const PropertyTransform & aProperty)123 operator=(const PropertyTransform &aProperty)
124 {
125     PropertyDblArray::operator =(aProperty);
126     _transform = aProperty._transform;
127     return(*this);
128 }
129 
130 
131 //=============================================================================
132 // GET AND SET
133 //=============================================================================
134 //-----------------------------------------------------------------------------
135 // TYPE AS STRING
136 //-----------------------------------------------------------------------------
137 //_____________________________________________________________________________
138 /**
139  * Get the type of this property as a string.
140  *
141  * @return Type of the property.
142  */
143 std::string PropertyTransform::
getTypeName() const144 getTypeName() const
145 {
146     return("Transform");
147 }
148 
149 //-----------------------------------------------------------------------------
150 // VALUE
151 //-----------------------------------------------------------------------------
152 //_____________________________________________________________________________
153 /**
154  * Set the value of this property.
155  *
156  * @param aArray Array to which this property is to be assigned.
157  *
158 void PropertyTransform::
159 setValue(const Array<double>& aArray)
160 {
161     for(int i=0;i<3; i++)
162         _transform[i] = aArray[i];
163 }
164 */
165 //_____________________________________________________________________________
166 /**
167  * Set the value of this property.
168  *
169  * @param aArray Array to which this property is to be assigned.
170  */
171 void PropertyTransform::
setValue(const SimTK::Transform & aTransform)172 setValue(const SimTK::Transform &aTransform)
173 {
174     _transform = aTransform;
175     getRotationsAndTranslationsAsArray6(&_array[0]);
176 }
177 void PropertyTransform::
setValue(int aSize,const double aArray[])178 setValue(int aSize,const double aArray[])
179 {
180     assert(aSize==6);
181     PropertyDblArray::setValue(aSize, aArray);
182     _transform.updR().setRotationToBodyFixedXYZ(SimTK::Vec3::getAs(&aArray[0]));
183     _transform.updP() = SimTK::Vec3::getAs(&aArray[3]);
184 }
185 //_____________________________________________________________________________
186 /**
187  * Get a reference to the value of this property.  Note that the returned
188  * reference can be used to change the value of this property.
189  *
190  * @return Reference to the value of this property.
191  */
192 SimTK::Transform& PropertyTransform::
getValueTransform()193 getValueTransform()
194 {
195     return(_transform);
196 }
197 //_____________________________________________________________________________
198 /**
199  * Get a constant reference to the value of this property.
200  *
201  * @return Reference to the value of this property.
202  */
203 const SimTK::Transform& PropertyTransform::
getValueTransform() const204 getValueTransform() const
205 {
206     return(_transform);
207 }
getRotationsAndTranslationsAsArray6(double aArray[]) const208 void PropertyTransform::getRotationsAndTranslationsAsArray6(double aArray[]) const
209 {
210     SimTK::Vec3 translations = _transform.p();
211     SimTK::Vec3 rotations = _transform.R().convertRotationToBodyFixedXYZ();
212     int i=0;
213     for(i=0; i<3; i++){
214         aArray[i] = rotations[i];
215         aArray[i+3] = translations[i];
216     }
217 }
218 //_____________________________________________________________________________
219 /**
220  * Get a constant String representing the value of this property.
221  *
222  * @return Constant String representing the value of this property.
223  */
224 string PropertyTransform::
toString() const225 toString() const
226 {
227     string str = "(";
228     char pad[256];
229     double rawData[6];
230     getRotationsAndTranslationsAsArray6(rawData);
231     sprintf(pad, "%g %g %g %g %g %g", rawData[0], rawData[1], rawData[2], rawData[3], rawData[4], rawData[5]);
232     str += string(pad);
233     str += ")";
234     return str;
235 }
236