goat3d

annotate src/xform_node.cc @ 45:8da36540e2e9

fixed memory leak in readxml and reversed error return
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 08 Dec 2013 02:27:32 +0200
parents 3d669155709d
children 498ca7ac7047
rev   line source
nuclear@1 1 #include <assert.h>
nuclear@1 2 #include <algorithm>
nuclear@1 3 #include "xform_node.h"
nuclear@29 4 #include "anim.h"
nuclear@29 5 #include "track.h"
nuclear@1 6
nuclear@1 7 static inline anm_interpolator track_interpolator(Interp in);
nuclear@1 8 static inline anm_extrapolator track_extrapolator(Extrap ex);
nuclear@1 9
nuclear@1 10 XFormNode::XFormNode()
nuclear@1 11 {
nuclear@1 12 anm = new anm_node;
nuclear@1 13 anm_init_node(anm);
nuclear@30 14
nuclear@30 15 parent = 0;
nuclear@1 16 }
nuclear@1 17
nuclear@1 18 XFormNode::~XFormNode()
nuclear@1 19 {
nuclear@1 20 anm_destroy_node(anm);
nuclear@1 21 delete anm;
nuclear@1 22 }
nuclear@1 23
nuclear@1 24 void XFormNode::set_name(const char *name)
nuclear@1 25 {
nuclear@1 26 anm_set_node_name(anm, name);
nuclear@1 27 }
nuclear@1 28
nuclear@1 29 const char *XFormNode::get_name() const
nuclear@1 30 {
nuclear@1 31 return anm_get_node_name(anm);
nuclear@1 32 }
nuclear@1 33
nuclear@1 34 void XFormNode::set_interpolator(Interp in)
nuclear@1 35 {
nuclear@1 36 anm_set_interpolator(anm, track_interpolator(in));
nuclear@1 37 interp = in;
nuclear@1 38 }
nuclear@1 39
nuclear@1 40 Interp XFormNode::get_interpolator() const
nuclear@1 41 {
nuclear@1 42 return interp;
nuclear@1 43 }
nuclear@1 44
nuclear@1 45 void XFormNode::set_extrapolator(Extrap ex)
nuclear@1 46 {
nuclear@1 47 anm_set_extrapolator(anm, track_extrapolator(ex));
nuclear@1 48 extrap = ex;
nuclear@1 49 }
nuclear@1 50
nuclear@1 51 Extrap XFormNode::get_extrapolator() const
nuclear@1 52 {
nuclear@1 53 return extrap;
nuclear@1 54 }
nuclear@1 55
nuclear@1 56 void XFormNode::add_child(XFormNode *child)
nuclear@1 57 {
nuclear@1 58 children.push_back(child);
nuclear@1 59 anm_link_node(anm, child->anm);
nuclear@30 60 child->parent = this;
nuclear@1 61 }
nuclear@1 62
nuclear@1 63 void XFormNode::remove_child(XFormNode *child)
nuclear@1 64 {
nuclear@1 65 std::vector<XFormNode*>::iterator it;
nuclear@1 66 it = std::find(children.begin(), children.end(), child);
nuclear@1 67 if(it != children.end()) {
nuclear@1 68 children.erase(it);
nuclear@1 69 anm_unlink_node(anm, child->anm);
nuclear@1 70 }
nuclear@30 71 child->parent = 0;
nuclear@1 72 }
nuclear@1 73
nuclear@1 74 int XFormNode::get_children_count() const
nuclear@1 75 {
nuclear@1 76 return (int)children.size();
nuclear@1 77 }
nuclear@1 78
nuclear@1 79 XFormNode *XFormNode::get_child(int idx)
nuclear@1 80 {
nuclear@1 81 if(idx >= 0 && idx < get_children_count()) {
nuclear@1 82 return children[idx];
nuclear@1 83 }
nuclear@1 84 return 0;
nuclear@1 85 }
nuclear@1 86
nuclear@1 87 const XFormNode *XFormNode::get_child(int idx) const
nuclear@1 88 {
nuclear@1 89 if(idx >= 0 && idx < get_children_count()) {
nuclear@1 90 return children[idx];
nuclear@1 91 }
nuclear@1 92 return 0;
nuclear@1 93 }
nuclear@1 94
nuclear@30 95 XFormNode *XFormNode::get_parent() const
nuclear@30 96 {
nuclear@30 97 return parent;
nuclear@30 98 }
nuclear@30 99
nuclear@1 100 void XFormNode::set_position(const Vector3 &pos, long tmsec)
nuclear@1 101 {
nuclear@1 102 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
nuclear@1 103 }
nuclear@1 104
nuclear@1 105 Vector3 XFormNode::get_node_position(long tmsec) const
nuclear@1 106 {
nuclear@1 107 vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
nuclear@1 108 return Vector3(p.x, p.y, p.z);
nuclear@1 109 }
nuclear@1 110
nuclear@1 111 void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
nuclear@1 112 {
nuclear@1 113 anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
nuclear@1 114 }
nuclear@1 115
nuclear@1 116 Quaternion XFormNode::get_node_rotation(long tmsec) const
nuclear@1 117 {
nuclear@1 118 quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
nuclear@1 119 return Quaternion(q.w, q.x, q.y, q.z);
nuclear@1 120 }
nuclear@1 121
nuclear@1 122 void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
nuclear@1 123 {
nuclear@1 124 anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
nuclear@1 125 }
nuclear@1 126
nuclear@1 127 Vector3 XFormNode::get_node_scaling(long tmsec) const
nuclear@1 128 {
nuclear@1 129 vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
nuclear@1 130 return Vector3(s.x, s.y, s.z);
nuclear@1 131 }
nuclear@1 132
nuclear@1 133 // these take hierarchy into account
nuclear@1 134 Vector3 XFormNode::get_position(long tmsec) const
nuclear@1 135 {
nuclear@1 136 vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
nuclear@1 137 return Vector3(v.x, v.y, v.z);
nuclear@1 138 }
nuclear@1 139
nuclear@1 140 Quaternion XFormNode::get_rotation(long tmsec) const
nuclear@1 141 {
nuclear@1 142 quat_t q = anm_get_rotation(anm, tmsec);
nuclear@1 143 return Quaternion(q.w, q.x, q.y, q.z);
nuclear@1 144 }
nuclear@1 145
nuclear@1 146 Vector3 XFormNode::get_scaling(long tmsec) const
nuclear@1 147 {
nuclear@1 148 vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
nuclear@1 149 return Vector3(v.x, v.y, v.z);
nuclear@1 150 }
nuclear@1 151
nuclear@1 152 void XFormNode::set_pivot(const Vector3 &pivot)
nuclear@1 153 {
nuclear@1 154 anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
nuclear@1 155 }
nuclear@1 156
nuclear@1 157 Vector3 XFormNode::get_pivot() const
nuclear@1 158 {
nuclear@1 159 vec3_t p = anm_get_pivot(anm);
nuclear@1 160 return Vector3(p.x, p.y, p.z);
nuclear@1 161 }
nuclear@1 162
nuclear@1 163 void XFormNode::set_local_matrix(const Matrix4x4 &mat)
nuclear@1 164 {
nuclear@1 165 local_matrix = mat;
nuclear@1 166 }
nuclear@1 167
nuclear@1 168 const Matrix4x4 &XFormNode::get_local_matrix() const
nuclear@1 169 {
nuclear@1 170 return local_matrix;
nuclear@1 171 }
nuclear@1 172
nuclear@1 173 void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
nuclear@1 174 {
nuclear@1 175 bone_matrix = bmat;
nuclear@1 176 }
nuclear@1 177
nuclear@1 178 const Matrix4x4 &XFormNode::get_bone_matrix() const
nuclear@1 179 {
nuclear@1 180 return bone_matrix;
nuclear@1 181 }
nuclear@1 182
nuclear@1 183 #define FOO
nuclear@1 184
nuclear@1 185 void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
nuclear@1 186 {
nuclear@1 187 anm_time_t tm = ANM_MSEC2TM(tmsec);
nuclear@1 188
nuclear@1 189 if(mat) {
nuclear@1 190 anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
nuclear@1 191 #ifdef FOO
nuclear@1 192 *mat = local_matrix * *mat;
nuclear@1 193 #else
nuclear@1 194 *mat = *mat * local_matrix;
nuclear@1 195 #endif
nuclear@1 196 }
nuclear@1 197 if(inv_mat) {
nuclear@1 198 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
nuclear@1 199 }
nuclear@1 200 }
nuclear@1 201
nuclear@1 202 void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
nuclear@1 203 {
nuclear@1 204 anm_time_t tm = ANM_MSEC2TM(tmsec);
nuclear@1 205
nuclear@1 206 if(mat) {
nuclear@1 207 anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
nuclear@1 208 #ifdef FOO
nuclear@1 209 *mat = local_matrix * *mat;
nuclear@1 210 #else
nuclear@1 211 *mat = *mat * local_matrix;
nuclear@1 212 #endif
nuclear@1 213 }
nuclear@1 214 if(inv_mat) {
nuclear@1 215 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
nuclear@1 216 }
nuclear@1 217 }
nuclear@1 218
nuclear@1 219
nuclear@1 220 // ---- Track ----
nuclear@1 221
nuclear@1 222 Track::Track()
nuclear@1 223 {
nuclear@1 224 trk = new anm_track;
nuclear@1 225 anm_init_track(trk);
nuclear@1 226 }
nuclear@1 227
nuclear@1 228 Track::~Track()
nuclear@1 229 {
nuclear@1 230 anm_destroy_track(trk);
nuclear@1 231 delete trk;
nuclear@1 232 }
nuclear@1 233
nuclear@1 234 Track::Track(const Track &rhs)
nuclear@1 235 {
nuclear@1 236 trk = new anm_track;
nuclear@1 237 anm_init_track(trk);
nuclear@1 238 anm_copy_track(trk, rhs.trk);
nuclear@1 239 interp = rhs.interp;
nuclear@1 240 extrap = rhs.extrap;
nuclear@1 241 }
nuclear@1 242
nuclear@1 243 Track &Track::operator =(const Track &rhs)
nuclear@1 244 {
nuclear@1 245 if(&rhs == this) {
nuclear@1 246 return *this;
nuclear@1 247 }
nuclear@1 248
nuclear@1 249 anm_copy_track(trk, rhs.trk);
nuclear@1 250 interp = rhs.interp;
nuclear@1 251 extrap = rhs.extrap;
nuclear@1 252 return *this;
nuclear@1 253 }
nuclear@1 254
nuclear@1 255
nuclear@1 256 void Track::set_interpolator(Interp in)
nuclear@1 257 {
nuclear@1 258 anm_set_track_interpolator(trk, track_interpolator(in));
nuclear@1 259 interp = in;
nuclear@1 260 }
nuclear@1 261
nuclear@1 262 Interp Track::get_interpolator() const
nuclear@1 263 {
nuclear@1 264 return interp;
nuclear@1 265 }
nuclear@1 266
nuclear@1 267 void Track::set_extrapolator(Extrap ex)
nuclear@1 268 {
nuclear@1 269 anm_set_track_extrapolator(trk, track_extrapolator(ex));
nuclear@1 270 extrap = ex;
nuclear@1 271 }
nuclear@1 272
nuclear@1 273 Extrap Track::get_extrapolator() const
nuclear@1 274 {
nuclear@1 275 return extrap;
nuclear@1 276 }
nuclear@1 277
nuclear@1 278 void Track::set_default(double def)
nuclear@1 279 {
nuclear@1 280 anm_set_track_default(trk, def);
nuclear@1 281 }
nuclear@1 282
nuclear@1 283 void Track::set_value(float val, long tmsec)
nuclear@1 284 {
nuclear@1 285 anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
nuclear@1 286 }
nuclear@1 287
nuclear@1 288 float Track::get_value(long tmsec) const
nuclear@1 289 {
nuclear@1 290 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
nuclear@1 291 }
nuclear@1 292
nuclear@1 293 float Track::operator ()(long tmsec) const
nuclear@1 294 {
nuclear@1 295 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
nuclear@1 296 }
nuclear@1 297
nuclear@1 298
nuclear@1 299 // ---- Track3 ----
nuclear@1 300
nuclear@1 301 void Track3::set_interpolator(Interp in)
nuclear@1 302 {
nuclear@1 303 for(int i=0; i<3; i++) {
nuclear@1 304 track[i].set_interpolator(in);
nuclear@1 305 }
nuclear@1 306 }
nuclear@1 307
nuclear@1 308 Interp Track3::get_interpolator() const
nuclear@1 309 {
nuclear@1 310 return track[0].get_interpolator();
nuclear@1 311 }
nuclear@1 312
nuclear@1 313 void Track3::set_extrapolator(Extrap ex)
nuclear@1 314 {
nuclear@1 315 for(int i=0; i<3; i++) {
nuclear@1 316 track[i].set_extrapolator(ex);
nuclear@1 317 }
nuclear@1 318 }
nuclear@1 319
nuclear@1 320 Extrap Track3::get_extrapolator() const
nuclear@1 321 {
nuclear@1 322 return track[0].get_extrapolator();
nuclear@1 323 }
nuclear@1 324
nuclear@1 325 void Track3::set_default(const Vector3 &def)
nuclear@1 326 {
nuclear@1 327 for(int i=0; i<3; i++) {
nuclear@1 328 track[i].set_default(def[i]);
nuclear@1 329 }
nuclear@1 330 }
nuclear@1 331
nuclear@1 332 void Track3::set_value(const Vector3 &val, long tmsec)
nuclear@1 333 {
nuclear@1 334 for(int i=0; i<3; i++) {
nuclear@1 335 track[i].set_value(val[i], tmsec);
nuclear@1 336 }
nuclear@1 337 }
nuclear@1 338
nuclear@1 339 Vector3 Track3::get_value(long tmsec) const
nuclear@1 340 {
nuclear@1 341 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
nuclear@1 342 }
nuclear@1 343
nuclear@1 344 Vector3 Track3::operator ()(long tmsec) const
nuclear@1 345 {
nuclear@1 346 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
nuclear@1 347 }
nuclear@1 348
nuclear@1 349
nuclear@1 350 static inline anm_interpolator track_interpolator(Interp in)
nuclear@1 351 {
nuclear@1 352 switch(in) {
nuclear@1 353 case INTERP_STEP:
nuclear@1 354 return ANM_INTERP_STEP;
nuclear@1 355 case INTERP_LINEAR:
nuclear@1 356 return ANM_INTERP_LINEAR;
nuclear@1 357 case INTERP_CUBIC:
nuclear@1 358 return ANM_INTERP_CUBIC;
nuclear@1 359 }
nuclear@1 360
nuclear@1 361 assert(0);
nuclear@1 362 return ANM_INTERP_STEP;
nuclear@1 363 }
nuclear@1 364
nuclear@1 365 static inline anm_extrapolator track_extrapolator(Extrap ex)
nuclear@1 366 {
nuclear@1 367 switch(ex) {
nuclear@1 368 case EXTRAP_EXTEND:
nuclear@1 369 return ANM_EXTRAP_EXTEND;
nuclear@1 370 case EXTRAP_CLAMP:
nuclear@1 371 return ANM_EXTRAP_CLAMP;
nuclear@1 372 case EXTRAP_REPEAT:
nuclear@1 373 return ANM_EXTRAP_REPEAT;
nuclear@1 374 }
nuclear@1 375
nuclear@1 376 assert(0);
nuclear@1 377 return ANM_EXTRAP_EXTEND;
nuclear@1 378 }
nuclear@1 379