Blender V2.61 - r43446

collada_internal.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov.
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00028 /* COLLADABU_ASSERT, may be able to remove later */
00029 #include "COLLADABUPlatform.h"
00030 
00031 #include "collada_internal.h"
00032 
00033 UnitConverter::UnitConverter() : unit(), up_axis(COLLADAFW::FileInfo::Z_UP) {}
00034 
00035 void UnitConverter::read_asset(const COLLADAFW::FileInfo* asset)
00036 {
00037     unit = asset->getUnit();
00038     up_axis = asset->getUpAxisType();
00039 }
00040 
00041 UnitConverter::UnitSystem UnitConverter::isMetricSystem()
00042 {
00043     switch(unit.getLinearUnitUnit()) {
00044         case COLLADAFW::FileInfo::Unit::MILLIMETER:
00045         case COLLADAFW::FileInfo::Unit::CENTIMETER:
00046         case COLLADAFW::FileInfo::Unit::DECIMETER:
00047         case COLLADAFW::FileInfo::Unit::METER:
00048         case COLLADAFW::FileInfo::Unit::KILOMETER:
00049             return UnitConverter::Metric;
00050         case COLLADAFW::FileInfo::Unit::INCH:
00051         case COLLADAFW::FileInfo::Unit::FOOT:
00052         case COLLADAFW::FileInfo::Unit::YARD:
00053             return UnitConverter::Imperial;
00054         default:
00055             return UnitConverter::None;
00056     }
00057 }
00058 
00059 float UnitConverter::getLinearMeter()
00060 {
00061     return (float)unit.getLinearUnitMeter();
00062 }
00063 
00064 void UnitConverter::convertVector3(COLLADABU::Math::Vector3 &vec, float *v)
00065 {
00066     v[0] = vec.x;
00067     v[1] = vec.y;
00068     v[2] = vec.z;
00069 }
00070 
00071 // TODO need also for angle conversion, time conversion...
00072 
00073 void UnitConverter::dae_matrix_to_mat4_(float out[][4], const COLLADABU::Math::Matrix4& in)
00074 {
00075     // in DAE, matrices use columns vectors, (see comments in COLLADABUMathMatrix4.h)
00076     // so here, to make a blender matrix, we swap columns and rows
00077     for (int i = 0; i < 4; i++) {
00078         for (int j = 0; j < 4; j++) {
00079             out[i][j] = in[j][i];
00080         }
00081     }
00082 }
00083 
00084 void UnitConverter::mat4_to_dae(float out[][4], float in[][4])
00085 {
00086     copy_m4_m4(out, in);
00087     transpose_m4(out);
00088 }
00089 
00090 void UnitConverter::mat4_to_dae_double(double out[][4], float in[][4])
00091 {
00092     float mat[4][4];
00093 
00094     mat4_to_dae(mat, in);
00095 
00096     for (int i = 0; i < 4; i++)
00097         for (int j = 0; j < 4; j++)
00098             out[i][j] = mat[i][j];
00099 }
00100 
00101 void TransformBase::decompose(float mat[][4], float *loc, float eul[3], float quat[4], float *size)
00102 {
00103     mat4_to_size(size, mat);
00104     if (eul) {
00105         mat4_to_eul(eul, mat);
00106     }
00107     if (quat) {
00108         mat4_to_quat(quat, mat);
00109     }
00110     copy_v3_v3(loc, mat[3]);
00111 }
00112 
00122 const unsigned char translate_start_name_map[256] = {
00123 95,  95,  95,  95,  95,  95,  95,  95,  95,
00124 95,  95,  95,  95,  95,  95,  95,  95,
00125 95,  95,  95,  95,  95,  95,  95,  95,
00126 95,  95,  95,  95,  95,  95,  95,  95,
00127 95,  95,  95,  95,  95,  95,  95,  95,
00128 95,  95,  95,  95,  95,  95,  95,  95,
00129 95,  95,  95,  95,  95,  95,  95,  95,
00130 95,  95,  95,  95,  95,  95,  95,  95,
00131 65,  66,  67,  68,  69,  70,  71,  72,
00132 73,  74,  75,  76,  77,  78,  79,  80,
00133 81,  82,  83,  84,  85,  86,  87,  88,
00134 89,  90,  95,  95,  95,  95,  95,  95,
00135 97,  98,  99,  100,  101,  102,  103,  104,
00136 105,  106,  107,  108,  109,  110,  111,  112,
00137 113,  114,  115,  116,  117,  118,  119,  120,
00138 121,  122,  95,  95,  95,  95,  95,  95,
00139 95,  95,  95,  95,  95,  95,  95,  95,
00140 95,  95,  95,  95,  95,  95,  95,  95,
00141 95,  95,  95,  95,  95,  95,  95,  95,
00142 95,  95,  95,  95,  95,  95,  95,  95,
00143 95,  95,  95,  95,  95,  95,  95,  95,
00144 95,  95,  95,  95,  95,  95,  95,  95,
00145 95,  95,  95,  95,  95,  95,  95,  95,
00146 95,  95,  95,  95,  95,  95,  95,  192,
00147 193,  194,  195,  196,  197,  198,  199,  200,
00148 201,  202,  203,  204,  205,  206,  207,  208,
00149 209,  210,  211,  212,  213,  214,  95,  216,
00150 217,  218,  219,  220,  221,  222,  223,  224,
00151 225,  226,  227,  228,  229,  230,  231,  232,
00152 233,  234,  235,  236,  237,  238,  239,  240,
00153 241,  242,  243,  244,  245,  246,  95,  248,
00154 249,  250,  251,  252,  253,  254,  255};
00155 
00156 const unsigned char translate_name_map[256] = {
00157 95,  95,  95,  95,  95,  95,  95,  95,  95,
00158 95,  95,  95,  95,  95,  95,  95,  95,
00159 95,  95,  95,  95,  95,  95,  95,  95,
00160 95,  95,  95,  95,  95,  95,  95,  95,
00161 95,  95,  95,  95,  95,  95,  95,  95,
00162 95,  95,  95,  95,  45,  95,  95,  48,
00163 49,  50,  51,  52,  53,  54,  55,  56,
00164 57,  95,  95,  95,  95,  95,  95,  95,
00165 65,  66,  67,  68,  69,  70,  71,  72,
00166 73,  74,  75,  76,  77,  78,  79,  80,
00167 81,  82,  83,  84,  85,  86,  87,  88,
00168 89,  90,  95,  95,  95,  95,  95,  95,
00169 97,  98,  99,  100,  101,  102,  103,  104,
00170 105,  106,  107,  108,  109,  110,  111,  112,
00171 113,  114,  115,  116,  117,  118,  119,  120,
00172 121,  122,  95,  95,  95,  95,  95,  95,
00173 95,  95,  95,  95,  95,  95,  95,  95,
00174 95,  95,  95,  95,  95,  95,  95,  95,
00175 95,  95,  95,  95,  95,  95,  95,  95,
00176 95,  95,  95,  95,  95,  95,  95,  95,
00177 95,  95,  95,  95,  95,  95,  95,  95,
00178 95,  95,  95,  95,  95,  95,  95,  95,
00179 95,  95,  95,  95,  95,  95,  183,  95,
00180 95,  95,  95,  95,  95,  95,  95,  192,
00181 193,  194,  195,  196,  197,  198,  199,  200,
00182 201,  202,  203,  204,  205,  206,  207,  208,
00183 209,  210,  211,  212,  213,  214,  95,  216,
00184 217,  218,  219,  220,  221,  222,  223,  224,
00185 225,  226,  227,  228,  229,  230,  231,  232,
00186 233,  234,  235,  236,  237,  238,  239,  240,
00187 241,  242,  243,  244,  245,  246,  95,  248,
00188 249,  250,  251,  252,  253,  254,  255};
00189 
00190 typedef std::map< std::string, std::vector<std::string> > map_string_list;
00191 map_string_list global_id_map;
00192 
00193 void clear_global_id_map()
00194 {
00195     global_id_map.clear();
00196 }
00197 
00199 std::string translate_id(const std::string &id)
00200 {
00201     if (id.size() == 0)
00202     { return id; }
00203     std::string id_translated = id;
00204     id_translated[0] = translate_start_name_map[(unsigned int)id_translated[0]];
00205     for (unsigned int i=1; i < id_translated.size(); i++)
00206     {
00207         id_translated[i] = translate_name_map[(unsigned int)id_translated[i]];
00208     }
00209     // It's so much workload now, the if() should speed up things.
00210     if (id_translated != id)
00211     {
00212         // Search duplicates
00213         map_string_list::iterator iter = global_id_map.find(id_translated);
00214         if (iter != global_id_map.end())
00215         {
00216             unsigned int i = 0;
00217             bool found = false;
00218             for (i=0; i < iter->second.size(); i++)
00219             {
00220                 if (id == iter->second[i])
00221                 { 
00222                     found = true;
00223                     break;
00224                 }
00225             }
00226             bool convert = false;
00227             if (found)
00228             {
00229               if (i > 0)
00230               { convert = true; }
00231             }
00232             else
00233             { 
00234                 convert = true;
00235                 global_id_map[id_translated].push_back(id);
00236             }
00237             if (convert)
00238             {
00239                 std::stringstream out;
00240                 out << ++i;
00241                 id_translated += out.str();
00242             }
00243         }
00244         else { global_id_map[id_translated].push_back(id); }
00245     }
00246     return id_translated;
00247 }
00248 
00249 std::string id_name(void *id)
00250 {
00251     return ((ID*)id)->name + 2;
00252 }
00253 
00254 std::string get_geometry_id(Object *ob)
00255 {
00256     return translate_id(id_name(ob->data)) + "-mesh";
00257 }
00258 
00259 std::string get_light_id(Object *ob)
00260 {
00261     return translate_id(id_name(ob)) + "-light";
00262 }
00263 
00264 std::string get_joint_id(Bone *bone, Object *ob_arm)
00265 {
00266     return translate_id(/*id_name(ob_arm) + "_" +*/ bone->name);
00267 }
00268 
00269 std::string get_camera_id(Object *ob)
00270 {
00271     return translate_id(id_name(ob)) + "-camera";
00272 }
00273 
00274 std::string get_material_id(Material *mat)
00275 {
00276     return translate_id(id_name(mat)) + "-material";
00277 }
00278 
00279 bool has_object_type(Scene *sce, short obtype)
00280 {
00281     Base *base= (Base*) sce->base.first;
00282     while(base) {
00283         Object *ob = base->object;
00284             
00285         if (ob->type == obtype && ob->data) {
00286             return true;
00287         }
00288         base= base->next;
00289     }
00290     return false;
00291 }