21 #ifndef OPENHRP_COLLADA_UTIL_H 22 #define OPENHRP_COLLADA_UTIL_H 25 #include <dae/daeErrorHandler.h> 26 #include <1.5/dom/domCOLLADA.h> 27 #include <dae/domAny.h> 28 #include <1.5/dom/domConstants.h> 29 #include <1.5/dom/domTriangles.h> 30 #include <dae/daeDocument.h> 31 #include <1.5/dom/domTypes.h> 32 #include <1.5/dom/domImage.h> 33 #include <1.5/dom/domElements.h> 34 #include <1.5/dom/domKinematics.h> 35 #include <dae/daeStandardURIResolver.h> 45 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) 46 # if (BOOST_VERSION > 104400) 47 # define BOOST_ENABLE_ASSERT_HANDLER 50 # define BOOST_ENABLE_ASSERT_HANDLER 53 #include <boost/date_time/posix_time/posix_time.hpp> 54 #include <boost/date_time/time_facet.hpp> 55 #include <boost/algorithm/string.hpp> 56 #include <boost/format.hpp> 57 #include <boost/shared_ptr.hpp> 58 #include <boost/array.hpp> 59 #include <boost/lexical_cast.hpp> 62 #include <hrpCorba/ModelLoader.hh> 65 #ifndef __PRETTY_FUNCTION__ 66 #define __PRETTY_FUNCTION__ __FUNCDNAME__ 70 #ifndef __PRETTY_FUNCTION__ 71 #define __PRETTY_FUNCTION__ __func__ 74 #define COLLADA_ASSERT(b) { if( !(b) ) { std::stringstream ss; ss << "ikfast exception: " << __FILE__ << ":" << __LINE__ << ": " <<__PRETTY_FUNCTION__ << ": Assertion '" << #b << "' failed"; throw OpenHRP::ModelLoader::ModelLoaderException(ss.str().c_str()); } } 88 std::cout <<
"Collada Info: " << s << std::endl;
91 std::cout <<
"Collada Warning: " << s << std::endl;
94 std::cout <<
"Collada Error: " << s << std::endl;
101 inline void PoseMultVector(OpenHRP::DblArray3& vnew,
const OpenHRP::DblArray12& m,
const OpenHRP::DblArray3& v)
103 dReal
x = v[0],
y = v[1], z = v[2];
104 vnew[0] = m[4*0+0] * x + m[4*0+1] * y + m[4*0+2] * z + m[4*0+3];
105 vnew[1] = m[4*1+0] * x + m[4*1+1] * y + m[4*1+2] * z + m[4*1+3];
106 vnew[2] = m[4*2+0] * x + m[4*2+1] * y + m[4*2+2] * z + m[4*2+3];
109 inline void PoseRotateVector(OpenHRP::DblArray3& vnew,
const OpenHRP::DblArray12& m,
const OpenHRP::DblArray3& v)
111 dReal
x = v[0],
y = v[1], z = v[2];
112 vnew[0] = m[4*0+0] * x + m[4*0+1] * y + m[4*0+2] * z;
113 vnew[1] = m[4*1+0] * x + m[4*1+1] * y + m[4*1+2] * z;
114 vnew[2] = m[4*2+0] * x + m[4*2+1] * y + m[4*2+2] * z;
119 pose[0] = 1; pose[1] = 0; pose[2] = 0; pose[3] = 0;
120 pose[4] = 0; pose[5] = 1; pose[6] = 0; pose[7] = 0;
121 pose[8] = 0; pose[9] = 0; pose[10] = 1; pose[11] = 0;
124 inline void PoseInverse(OpenHRP::DblArray12& poseinv,
const OpenHRP::DblArray12& pose)
127 poseinv[0] = pose[0]; poseinv[1] = pose[4]; poseinv[2] = pose[8]; poseinv[3] = -pose[3]*pose[0]-pose[7]*pose[4]-pose[11]*pose[8];
128 poseinv[4] = pose[1]; poseinv[5] = pose[5]; poseinv[6] = pose[9]; poseinv[7] = -pose[3]*pose[1]-pose[7]*pose[5]-pose[11]*pose[9];
129 poseinv[8] = pose[2]; poseinv[9] = pose[6]; poseinv[10] = pose[10]; poseinv[11] = -pose[3]*pose[2]-pose[7]*pose[6]-pose[11]*pose[10];
132 template <
typename T>
133 inline void PoseMult(OpenHRP::DblArray12& mres,
const T& m0,
const OpenHRP::DblArray12& m1)
135 COLLADA_ASSERT((
void*)&mres != (
void*)&m0 && (
void*)&mres != (
void*)&m1);
136 mres[0*4+0] = m0[0*4+0]*m1[0*4+0]+m0[0*4+1]*m1[1*4+0]+m0[0*4+2]*m1[2*4+0];
137 mres[0*4+1] = m0[0*4+0]*m1[0*4+1]+m0[0*4+1]*m1[1*4+1]+m0[0*4+2]*m1[2*4+1];
138 mres[0*4+2] = m0[0*4+0]*m1[0*4+2]+m0[0*4+1]*m1[1*4+2]+m0[0*4+2]*m1[2*4+2];
139 mres[1*4+0] = m0[1*4+0]*m1[0*4+0]+m0[1*4+1]*m1[1*4+0]+m0[1*4+2]*m1[2*4+0];
140 mres[1*4+1] = m0[1*4+0]*m1[0*4+1]+m0[1*4+1]*m1[1*4+1]+m0[1*4+2]*m1[2*4+1];
141 mres[1*4+2] = m0[1*4+0]*m1[0*4+2]+m0[1*4+1]*m1[1*4+2]+m0[1*4+2]*m1[2*4+2];
142 mres[2*4+0] = m0[2*4+0]*m1[0*4+0]+m0[2*4+1]*m1[1*4+0]+m0[2*4+2]*m1[2*4+0];
143 mres[2*4+1] = m0[2*4+0]*m1[0*4+1]+m0[2*4+1]*m1[1*4+1]+m0[2*4+2]*m1[2*4+1];
144 mres[2*4+2] = m0[2*4+0]*m1[0*4+2]+m0[2*4+1]*m1[1*4+2]+m0[2*4+2]*m1[2*4+2];
145 mres[3] = m1[3] * m0[0] + m1[7] * m0[1] + m1[11] * m0[2] + m0[3];
146 mres[7] = m1[3] * m0[4] + m1[7] * m0[5] + m1[11] * m0[6] + m0[7];
147 mres[11] = m1[3] * m0[8] + m1[7] * m0[9] + m1[11] * m0[10] + m0[11];
150 template <
typename T>
153 dReal tr = mat[4*0+0] + mat[4*1+1] + mat[4*2+2];
156 quat[1] = (mat[4*2+1] - mat[4*1+2]);
157 quat[2] = (mat[4*0+2] - mat[4*2+0]);
158 quat[3] = (mat[4*1+0] - mat[4*0+1]);
162 if (mat[4*1+1] > mat[4*0+0]) {
163 if (mat[4*2+2] > mat[4*1+1]) {
164 quat[3] = (mat[4*2+2] - (mat[4*0+0] + mat[4*1+1])) + 1;
165 quat[1] = (mat[4*2+0] + mat[4*0+2]);
166 quat[2] = (mat[4*1+2] + mat[4*2+1]);
167 quat[0] = (mat[4*1+0] - mat[4*0+1]);
170 quat[2] = (mat[4*1+1] - (mat[4*2+2] + mat[4*0+0])) + 1;
171 quat[3] = (mat[4*1+2] + mat[4*2+1]);
172 quat[1] = (mat[4*0+1] + mat[4*1+0]);
173 quat[0] = (mat[4*0+2] - mat[4*2+0]);
176 else if (mat[4*2+2] > mat[4*0+0]) {
177 quat[3] = (mat[4*2+2] - (mat[4*0+0] + mat[4*1+1])) + 1;
178 quat[1] = (mat[4*2+0] + mat[4*0+2]);
179 quat[2] = (mat[4*1+2] + mat[4*2+1]);
180 quat[0] = (mat[4*1+0] - mat[4*0+1]);
183 quat[1] = (mat[4*0+0] - (mat[4*1+1] + mat[4*2+2])) + 1;
184 quat[2] = (mat[4*0+1] + mat[4*1+0]);
185 quat[3] = (mat[4*2+0] + mat[4*0+2]);
186 quat[0] = (mat[4*2+1] - mat[4*1+2]);
189 dReal fnorm = std::sqrt(quat[0]*quat[0]+quat[1]*quat[1]+quat[2]*quat[2]+quat[3]*quat[3]);
199 dReal sinang = quat[1]*quat[1]+quat[2]*quat[2]+quat[3]*quat[3];
209 for(
int i = 0;
i < 4; ++
i) {
214 for(
int i = 0;
i < 4; ++
i) {
218 sinang = sqrt(sinang);
219 rotation[0] = _quat[1]/sinang;
220 rotation[1] = _quat[2]/sinang;
221 rotation[2] = _quat[3]/sinang;
222 rotation[3] = 2.0*atan2(sinang,_quat[0]);
226 template <
typename T>
229 dReal axislen = sqrt(rotation[0]*rotation[0]+rotation[1]*rotation[1]+rotation[2]*rotation[2]);
237 dReal angle = rotation[3] * 0.5*fanglemult;
238 dReal sang = sin(angle)/axislen;
239 quat[0] = cos(angle);
240 quat[1] = rotation[0]*sang;
241 quat[2] = rotation[1]*sang;
242 quat[3] = rotation[2]*sang;
246 inline void PoseFromQuat(OpenHRP::DblArray12& pose,
const OpenHRP::DblArray4& quat)
248 dReal qq1 = 2*quat[1]*quat[1];
249 dReal qq2 = 2*quat[2]*quat[2];
250 dReal qq3 = 2*quat[3]*quat[3];
251 pose[4*0+0] = 1 - qq2 - qq3;
252 pose[4*0+1] = 2*(quat[1]*quat[2] - quat[0]*quat[3]);
253 pose[4*0+2] = 2*(quat[1]*quat[3] + quat[0]*quat[2]);
255 pose[4*1+0] = 2*(quat[1]*quat[2] + quat[0]*quat[3]);
256 pose[4*1+1] = 1 - qq1 - qq3;
257 pose[4*1+2] = 2*(quat[2]*quat[3] - quat[0]*quat[1]);
259 pose[4*2+0] = 2*(quat[1]*quat[3] - quat[0]*quat[2]);
260 pose[4*2+1] = 2*(quat[2]*quat[3] + quat[0]*quat[1]);
261 pose[4*2+2] = 1 - qq1 - qq2;
267 OpenHRP::DblArray4 quat;
270 pose[3] = translation[0];
271 pose[7] = translation[1];
272 pose[11] = translation[2];
277 OpenHRP::DblArray4 quat;
280 translation[0] = pose[3];
281 translation[1] = pose[7];
282 translation[2] = pose[11];
285 inline std::vector<dReal>
toVector(
const OpenHRP::DblSequence& seq)
287 std::vector<dReal> v;
288 v.resize(seq.length());
289 for(
size_t i = 0;
i < v.size(); ++
i) {
299 std::transform(d.begin(), d.end(), d.begin(), ::tolower);
308 inline void assertion_failed(
char const * expr,
char const *
function,
char const * file,
long line) {
309 throw OpenHRP::ModelLoader::ModelLoaderException(boost::str(boost::format(
"[%s:%d] -> %s, expr: %s")%file%line%
function%expr).c_str());
void AxisAngleFromQuat(OpenHRP::DblArray4 &rotation, const OpenHRP::DblArray4 &quat)
void PoseInverse(OpenHRP::DblArray12 &poseinv, const OpenHRP::DblArray12 &pose)
std::string tolowerstring(const std::string &s)
Modifications controlling boost library behavior.
void COLLADALOG_DEBUG(const std::string &s)
void PoseMult(OpenHRP::DblArray12 &mres, const T &m0, const OpenHRP::DblArray12 &m1)
void COLLADALOG_ERROR(const std::string &s)
std::vector< dReal > toVector(const OpenHRP::DblSequence &seq)
void PoseRotateVector(OpenHRP::DblArray3 &vnew, const OpenHRP::DblArray12 &m, const OpenHRP::DblArray3 &v)
#define COLLADA_ASSERT(b)
void PoseMultVector(OpenHRP::DblArray3 &vnew, const OpenHRP::DblArray12 &m, const OpenHRP::DblArray3 &v)
void PoseFromAxisAngleTranslation(OpenHRP::DblArray12 &pose, const OpenHRP::DblArray4 &rotation, const OpenHRP::DblArray3 &translation)
void AxisAngleTranslationFromPose(OpenHRP::DblArray4 &rotation, OpenHRP::DblArray3 &translation, const OpenHRP::DblArray12 &pose)
void PoseIdentity(OpenHRP::DblArray12 &pose)
void COLLADALOG_WARN(const std::string &s)
void COLLADALOG_INFO(const std::string &s)
void QuatFromMatrix(OpenHRP::DblArray4 &quat, const T &mat)
void assertion_failed(char const *expr, char const *function, char const *file, long line)
void PoseFromQuat(OpenHRP::DblArray12 &pose, const OpenHRP::DblArray4 &quat)
void QuatFromAxisAngle(OpenHRP::DblArray4 &quat, const T &rotation, dReal fanglemult=1)
void COLLADALOG_VERBOSE(const std::string &s)