Blender  V2.93
BCSampleData.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #include "BCSampleData.h"
21 #include "collada_utils.h"
22 
24 {
25  BCBoneMatrixMap::iterator it;
26  for (it = bonemats.begin(); it != bonemats.end(); ++it) {
27  delete it->second;
28  }
29 }
30 
31 void BCSample::add_bone_matrix(Bone *bone, Matrix &mat)
32 {
33  BCMatrix *matrix;
34  BCBoneMatrixMap::const_iterator it = bonemats.find(bone);
35  if (it != bonemats.end()) {
36  throw std::invalid_argument("bone " + std::string(bone->name) + " already defined before");
37  }
38  matrix = new BCMatrix(mat);
39  bonemats[bone] = matrix;
40 }
41 
42 /* Get channel value */
43 bool BCSample::get_value(std::string channel_target, const int array_index, float *val) const
44 {
45  std::string bname = bc_string_before(channel_target, ".");
46  std::string channel_type = bc_string_after(channel_target, ".");
47 
48  const BCMatrix *matrix = &obmat;
49  if (bname != channel_target) {
50  bname = bname.substr(2);
51  bname = bc_string_before(bname, "\"");
52  BCBoneMatrixMap::const_iterator it;
53  for (it = bonemats.begin(); it != bonemats.end(); ++it) {
54  Bone *bone = it->first;
55  if (bname == bone->name) {
56  matrix = it->second;
57  break;
58  }
59  }
60  }
61  else {
62  matrix = &obmat;
63  }
64 
65  if (channel_type == "location") {
66  *val = matrix->location()[array_index];
67  }
68  else if (channel_type == "scale") {
69  *val = matrix->scale()[array_index];
70  }
71  else if (channel_type == "rotation" || channel_type == "rotation_euler") {
72  *val = matrix->rotation()[array_index];
73  }
74  else if (channel_type == "rotation_quaternion") {
75  *val = matrix->quat()[array_index];
76  }
77  else {
78  *val = 0;
79  return false;
80  }
81 
82  return true;
83 }
84 
85 const BCMatrix *BCSample::get_matrix(Bone *bone) const
86 {
87  BCBoneMatrixMap::const_iterator it = bonemats.find(bone);
88  if (it == bonemats.end()) {
89  return nullptr;
90  }
91  return it->second;
92 }
93 
95 {
96  return obmat;
97 }
float(& rotation() const)[3]
Definition: BCMath.cpp:237
float(& scale() const)[3]
Definition: BCMath.cpp:242
float(& location() const)[3]
Definition: BCMath.cpp:232
float(& quat() const)[4]
Definition: BCMath.cpp:247
const BCMatrix & get_matrix() const
void add_bone_matrix(Bone *bone, Matrix &mat)
bool get_value(std::string channel_target, const int array_index, float *val) const
std::string bc_string_after(const std::string &s, const std::string probe)
std::string bc_string_before(const std::string &s, const std::string probe)
char name[64]