| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 20:30:35 +00:00
										 |  |  | /** \file blender/collada/collada_internal.cpp
 | 
					
						
							|  |  |  |  *  \ingroup collada | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-12 06:25:04 +00:00
										 |  |  | /* COLLADABU_ASSERT, may be able to remove later */ | 
					
						
							| 
									
										
										
										
											2011-02-17 22:57:56 +00:00
										 |  |  | #include "COLLADABUPlatform.h"
 | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | #include "collada_internal.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-12 21:25:29 +00:00
										 |  |  | #include "BLI_linklist.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-20 16:43:48 +00:00
										 |  |  | UnitConverter::UnitConverter() : unit(), up_axis(COLLADAFW::FileInfo::Z_UP) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* pass */ | 
					
						
							| 
									
										
										
										
											2012-06-12 22:05:33 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-12 22:05:33 +00:00
										 |  |  | void UnitConverter::read_asset(const COLLADAFW::FileInfo *asset) | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-09 21:17:14 +00:00
										 |  |  | 	unit = asset->getUnit(); | 
					
						
							|  |  |  | 	up_axis = asset->getUpAxisType(); | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 21:17:14 +00:00
										 |  |  | UnitConverter::UnitSystem UnitConverter::isMetricSystem() | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 	switch (unit.getLinearUnitUnit()) { | 
					
						
							| 
									
										
										
										
											2010-10-09 21:17:14 +00:00
										 |  |  | 		case COLLADAFW::FileInfo::Unit::MILLIMETER: | 
					
						
							|  |  |  | 		case COLLADAFW::FileInfo::Unit::CENTIMETER: | 
					
						
							|  |  |  | 		case COLLADAFW::FileInfo::Unit::DECIMETER: | 
					
						
							|  |  |  | 		case COLLADAFW::FileInfo::Unit::METER: | 
					
						
							|  |  |  | 		case COLLADAFW::FileInfo::Unit::KILOMETER: | 
					
						
							|  |  |  | 			return UnitConverter::Metric; | 
					
						
							|  |  |  | 		case COLLADAFW::FileInfo::Unit::INCH: | 
					
						
							|  |  |  | 		case COLLADAFW::FileInfo::Unit::FOOT: | 
					
						
							|  |  |  | 		case COLLADAFW::FileInfo::Unit::YARD: | 
					
						
							|  |  |  | 			return UnitConverter::Imperial; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return UnitConverter::None; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | float UnitConverter::getLinearMeter() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (float)unit.getLinearUnitMeter(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void UnitConverter::convertVector3(COLLADABU::Math::Vector3 &vec, float *v) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	v[0] = vec.x; | 
					
						
							|  |  |  | 	v[1] = vec.y; | 
					
						
							|  |  |  | 	v[2] = vec.z; | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TODO need also for angle conversion, time conversion...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void UnitConverter::dae_matrix_to_mat4_(float out[][4], const COLLADABU::Math::Matrix4& in) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// in DAE, matrices use columns vectors, (see comments in COLLADABUMathMatrix4.h)
 | 
					
						
							|  |  |  | 	// so here, to make a blender matrix, we swap columns and rows
 | 
					
						
							|  |  |  | 	for (int i = 0; i < 4; i++) { | 
					
						
							|  |  |  | 		for (int j = 0; j < 4; j++) { | 
					
						
							|  |  |  | 			out[i][j] = in[j][i]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void UnitConverter::mat4_to_dae(float out[][4], float in[][4]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	copy_m4_m4(out, in); | 
					
						
							|  |  |  | 	transpose_m4(out); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void UnitConverter::mat4_to_dae_double(double out[][4], float in[][4]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float mat[4][4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mat4_to_dae(mat, in); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < 4; i++) | 
					
						
							|  |  |  | 		for (int j = 0; j < 4; j++) | 
					
						
							|  |  |  | 			out[i][j] = mat[i][j]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TransformBase::decompose(float mat[][4], float *loc, float eul[3], float quat[4], float *size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	mat4_to_size(size, mat); | 
					
						
							|  |  |  | 	if (eul) { | 
					
						
							|  |  |  | 		mat4_to_eul(eul, mat); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (quat) { | 
					
						
							|  |  |  | 		mat4_to_quat(quat, mat); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	copy_v3_v3(loc, mat[3]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2012-03-09 18:28:30 +00:00
										 |  |  |  * Translation map. | 
					
						
							|  |  |  |  * Used to translate every COLLADA id to a valid id, no matter what "wrong" letters may be | 
					
						
							|  |  |  |  * included. Look at the IDREF XSD declaration for more. | 
					
						
							|  |  |  |  * Follows strictly the COLLADA XSD declaration which explicitly allows non-english chars, | 
					
						
							|  |  |  |  * like special chars (e.g. micro sign), umlauts and so on. | 
					
						
							|  |  |  |  * The COLLADA spec also allows additional chars for member access ('.'), these | 
					
						
							|  |  |  |  * must obviously be removed too, otherwise they would be heavily misinterpreted. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | const unsigned char translate_start_name_map[256] = { | 
					
						
							| 
									
										
										
										
											2012-06-12 22:05:33 +00:00
										 |  |  | 	95,  95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	65,  66,  67,  68,  69,  70,  71,  72, | 
					
						
							|  |  |  | 	73,  74,  75,  76,  77,  78,  79,  80, | 
					
						
							|  |  |  | 	81,  82,  83,  84,  85,  86,  87,  88, | 
					
						
							|  |  |  | 	89,  90,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	97,  98,  99,  100,  101,  102,  103,  104, | 
					
						
							|  |  |  | 	105,  106,  107,  108,  109,  110,  111,  112, | 
					
						
							|  |  |  | 	113,  114,  115,  116,  117,  118,  119,  120, | 
					
						
							|  |  |  | 	121,  122,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  192, | 
					
						
							|  |  |  | 	193,  194,  195,  196,  197,  198,  199,  200, | 
					
						
							|  |  |  | 	201,  202,  203,  204,  205,  206,  207,  208, | 
					
						
							|  |  |  | 	209,  210,  211,  212,  213,  214,  95,  216, | 
					
						
							|  |  |  | 	217,  218,  219,  220,  221,  222,  223,  224, | 
					
						
							|  |  |  | 	225,  226,  227,  228,  229,  230,  231,  232, | 
					
						
							|  |  |  | 	233,  234,  235,  236,  237,  238,  239,  240, | 
					
						
							|  |  |  | 	241,  242,  243,  244,  245,  246,  95,  248, | 
					
						
							|  |  |  | 	249,  250,  251,  252,  253,  254,  255 | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | const unsigned char translate_name_map[256] = { | 
					
						
							| 
									
										
										
										
											2012-06-12 22:05:33 +00:00
										 |  |  | 	95,  95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  45,  95,  95,  48, | 
					
						
							|  |  |  | 	49,  50,  51,  52,  53,  54,  55,  56, | 
					
						
							|  |  |  | 	57,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	65,  66,  67,  68,  69,  70,  71,  72, | 
					
						
							|  |  |  | 	73,  74,  75,  76,  77,  78,  79,  80, | 
					
						
							|  |  |  | 	81,  82,  83,  84,  85,  86,  87,  88, | 
					
						
							|  |  |  | 	89,  90,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	97,  98,  99,  100,  101,  102,  103,  104, | 
					
						
							|  |  |  | 	105,  106,  107,  108,  109,  110,  111,  112, | 
					
						
							|  |  |  | 	113,  114,  115,  116,  117,  118,  119,  120, | 
					
						
							|  |  |  | 	121,  122,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  183,  95, | 
					
						
							|  |  |  | 	95,  95,  95,  95,  95,  95,  95,  192, | 
					
						
							|  |  |  | 	193,  194,  195,  196,  197,  198,  199,  200, | 
					
						
							|  |  |  | 	201,  202,  203,  204,  205,  206,  207,  208, | 
					
						
							|  |  |  | 	209,  210,  211,  212,  213,  214,  95,  216, | 
					
						
							|  |  |  | 	217,  218,  219,  220,  221,  222,  223,  224, | 
					
						
							|  |  |  | 	225,  226,  227,  228,  229,  230,  231,  232, | 
					
						
							|  |  |  | 	233,  234,  235,  236,  237,  238,  239,  240, | 
					
						
							|  |  |  | 	241,  242,  243,  244,  245,  246,  95,  248, | 
					
						
							|  |  |  | 	249,  250,  251,  252,  253,  254,  255 | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef std::map< std::string, std::vector<std::string> > map_string_list; | 
					
						
							|  |  |  | map_string_list global_id_map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void clear_global_id_map() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	global_id_map.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** Look at documentation of translate_map */ | 
					
						
							|  |  |  | std::string translate_id(const std::string &id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 	if (id.size() == 0) { | 
					
						
							|  |  |  | 		return id; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 	std::string id_translated = id; | 
					
						
							|  |  |  | 	id_translated[0] = translate_start_name_map[(unsigned int)id_translated[0]]; | 
					
						
							| 
									
										
										
										
											2012-06-12 22:05:33 +00:00
										 |  |  | 	for (unsigned int i = 1; i < id_translated.size(); i++) { | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 		id_translated[i] = translate_name_map[(unsigned int)id_translated[i]]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-03-24 07:52:14 +00:00
										 |  |  | 	// It's so much workload now, the if () should speed up things.
 | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 	if (id_translated != id) { | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 		// Search duplicates
 | 
					
						
							|  |  |  | 		map_string_list::iterator iter = global_id_map.find(id_translated); | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 		if (iter != global_id_map.end()) { | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 			unsigned int i = 0; | 
					
						
							|  |  |  | 			bool found = false; | 
					
						
							| 
									
										
										
										
											2012-06-12 22:05:33 +00:00
										 |  |  | 			for (i = 0; i < iter->second.size(); i++) { | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 				if (id == iter->second[i]) { | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 					found = true; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			bool convert = false; | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 			if (found) { | 
					
						
							| 
									
										
										
										
											2012-06-12 22:05:33 +00:00
										 |  |  | 				if (i > 0) { | 
					
						
							|  |  |  | 					convert = true; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 			else { | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 				convert = true; | 
					
						
							|  |  |  | 				global_id_map[id_translated].push_back(id); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 			if (convert) { | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | 				std::stringstream out; | 
					
						
							|  |  |  | 				out << ++i; | 
					
						
							|  |  |  | 				id_translated += out.str(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { global_id_map[id_translated].push_back(id); } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return id_translated; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string id_name(void *id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-12 22:05:33 +00:00
										 |  |  | 	return ((ID *)id)->name + 2; | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string get_geometry_id(Object *ob) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-11-16 22:27:31 +00:00
										 |  |  | 	return translate_id(id_name(ob->data)) + "-mesh"; | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-26 22:46:28 +00:00
										 |  |  | std::string get_geometry_id(Object *ob, bool use_instantiation) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string geom_name = (use_instantiation) ? id_name(ob->data) : id_name(ob); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return translate_id(geom_name) + "-mesh"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | std::string get_light_id(Object *ob) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return translate_id(id_name(ob)) + "-light"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string get_joint_id(Bone *bone, Object *ob_arm) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-07-04 08:59:28 +00:00
										 |  |  | 	return translate_id(/*id_name(ob_arm) + "_" +*/ bone->name); | 
					
						
							| 
									
										
										
										
											2010-10-06 07:13:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string get_camera_id(Object *ob) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return translate_id(id_name(ob)) + "-camera"; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-02-28 14:24:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | std::string get_material_id(Material *mat) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return translate_id(id_name(mat)) + "-material"; | 
					
						
							|  |  |  | } |