fa181d4a6e91ec0c2ea3f698d635217367edf35f
[blender.git] / source / blender / collada / DocumentImporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/collada/DocumentImporter.cpp
24  *  \ingroup collada
25  */
26
27 // TODO:
28 // * name imported objects
29 // * import object rotation as euler
30
31 #include <string>
32 #include <map>
33 #include <algorithm> // sort()
34
35 #include "COLLADAFWRoot.h"
36 #include "COLLADAFWStableHeaders.h"
37 #include "COLLADAFWColorOrTexture.h"
38 #include "COLLADAFWIndexList.h"
39 #include "COLLADAFWMeshPrimitiveWithFaceVertexCount.h"
40 #include "COLLADAFWPolygons.h"
41 #include "COLLADAFWSampler.h"
42 #include "COLLADAFWTypes.h"
43 #include "COLLADAFWVisualScene.h"
44 #include "COLLADAFWArrayPrimitiveType.h"
45 #include "COLLADAFWLibraryNodes.h"
46 #include "COLLADAFWCamera.h"
47 #include "COLLADAFWLight.h"
48
49 #include "COLLADASaxFWLLoader.h"
50 #include "COLLADASaxFWLIExtraDataCallbackHandler.h"
51
52 extern "C" {
53 #include "BLI_listbase.h"
54 #include "BLI_math.h"
55 #include "BLI_string.h"
56 #include "BLI_utildefines.h"
57
58 #include "BKE_camera.h"
59 #include "BKE_main.h"
60 #include "BKE_lamp.h"
61 #include "BKE_library.h"
62 #include "BKE_texture.h"
63 #include "BKE_fcurve.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_scene.h"
66 #include "BKE_global.h"
67 #include "BKE_material.h"
68 #include "BKE_image.h"
69
70 #include "BLI_path_util.h"
71
72 #include "DNA_camera_types.h"
73 #include "DNA_lamp_types.h"
74
75 #include "RNA_access.h"
76
77 #include "MEM_guardedalloc.h"
78
79 }
80
81 #include "ExtraHandler.h"
82 #include "ErrorHandler.h"
83 #include "DocumentImporter.h"
84 #include "TransformReader.h"
85
86 #include "collada_internal.h"
87 #include "collada_utils.h"
88
89
90 /*
91  * COLLADA Importer limitations:
92  * - no multiple scene import, all objects are added to active scene
93  */
94
95 // #define COLLADA_DEBUG
96 // creates empties for each imported bone on layer 2, for debugging
97 // #define ARMATURE_TEST
98
99 DocumentImporter::DocumentImporter(bContext *C, const char *filename) :
100         mImportStage(General),
101         mFilename(filename),
102         mContext(C),
103         armature_importer(&unit_converter, &mesh_importer, &anim_importer, CTX_data_scene(C)),
104         mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C)),
105         anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C))
106 {
107 }
108
109 DocumentImporter::~DocumentImporter()
110 {
111         TagsMap::iterator etit;
112         etit = uid_tags_map.begin();
113         while (etit != uid_tags_map.end()) {
114                 delete etit->second;
115                 etit++;
116         }
117 }
118
119 bool DocumentImporter::import()
120 {
121         ErrorHandler errorHandler;
122         COLLADASaxFWL::Loader loader(&errorHandler);
123         COLLADAFW::Root root(&loader, this);
124         ExtraHandler *ehandler = new ExtraHandler(this, &(this->anim_importer));
125         
126         loader.registerExtraDataCallbackHandler(ehandler);
127
128         // deselect all to select new objects
129         BKE_scene_base_deselect_all(CTX_data_scene(mContext));
130
131         if (!root.loadDocument(mFilename)) {
132                 fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 1st pass\n");
133                 return false;
134         }
135         
136         if (errorHandler.hasError())
137                 return false;
138         
139         /** TODO set up scene graph and such here */
140         
141         mImportStage = Controller;
142         
143         COLLADASaxFWL::Loader loader2;
144         COLLADAFW::Root root2(&loader2, this);
145         
146         if (!root2.loadDocument(mFilename)) {
147                 fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 2nd pass\n");
148                 return false;
149         }
150         
151         
152         delete ehandler;
153
154         mesh_importer.bmeshConversion();
155
156         return true;
157 }
158
159 void DocumentImporter::cancel(const COLLADAFW::String& errorMessage)
160 {
161         // TODO: if possible show error info
162         //
163         // Should we get rid of invisible Meshes that were created so far
164         // or maybe create objects at coordinate space origin?
165         //
166         // The latter sounds better.
167 }
168
169 void DocumentImporter::start()
170 {
171 }
172
173 void DocumentImporter::finish()
174 {
175         if (mImportStage != General)
176                 return;
177                 
178         /** TODO Break up and put into 2-pass parsing of DAE */
179         std::vector<const COLLADAFW::VisualScene *>::iterator it;
180         for (it = vscenes.begin(); it != vscenes.end(); it++) {
181                 PointerRNA sceneptr, unit_settings;
182                 PropertyRNA *system, *scale;
183                 // TODO: create a new scene except the selected <visual_scene> - use current blender scene for it
184                 Scene *sce = CTX_data_scene(mContext);
185                 
186                 // for scene unit settings: system, scale_length
187                 RNA_id_pointer_create(&sce->id, &sceneptr);
188                 unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
189                 system = RNA_struct_find_property(&unit_settings, "system");
190                 scale = RNA_struct_find_property(&unit_settings, "scale_length");
191                 
192                 switch (unit_converter.isMetricSystem()) {
193                         case UnitConverter::Metric:
194                                 RNA_property_enum_set(&unit_settings, system, USER_UNIT_METRIC);
195                                 break;
196                         case UnitConverter::Imperial:
197                                 RNA_property_enum_set(&unit_settings, system, USER_UNIT_IMPERIAL);
198                                 break;
199                         default:
200                                 RNA_property_enum_set(&unit_settings, system, USER_UNIT_NONE);
201                                 break;
202                 }
203                 RNA_property_float_set(&unit_settings, scale, unit_converter.getLinearMeter());
204                 
205                 const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
206
207                 for (unsigned int i = 0; i < roots.getCount(); i++) {
208                         write_node(roots[i], NULL, sce, NULL, false);
209                 }
210         }
211
212
213         mesh_importer.optimize_material_assignements();
214
215         armature_importer.set_tags_map(this->uid_tags_map);
216         armature_importer.make_armatures(mContext);
217         armature_importer.make_shape_keys();
218
219 #if 0
220         armature_importer.fix_animation();
221 #endif
222
223         for (std::vector<const COLLADAFW::VisualScene *>::iterator it = vscenes.begin(); it != vscenes.end(); it++) {
224                 const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
225
226                 for (unsigned int i = 0; i < roots.getCount(); i++)
227                         translate_anim_recursive(roots[i], NULL, NULL);
228         }
229
230         if (libnode_ob.size()) {
231                 Scene *sce = CTX_data_scene(mContext);
232
233                 fprintf(stderr, "got %d library nodes to free\n", (int)libnode_ob.size());
234                 // free all library_nodes
235                 std::vector<Object *>::iterator it;
236                 for (it = libnode_ob.begin(); it != libnode_ob.end(); it++) {
237                         Object *ob = *it;
238
239                         Base *base = BKE_scene_base_find(sce, ob);
240                         if (base) {
241                                 BLI_remlink(&sce->base, base);
242                                 BKE_libblock_free_us(&G.main->object, base->object);
243                                 if (sce->basact == base)
244                                         sce->basact = NULL;
245                                 MEM_freeN(base);
246                         }
247                 }
248                 libnode_ob.clear();
249
250                 DAG_scene_sort(CTX_data_main(mContext), sce);
251                 DAG_ids_flush_update(CTX_data_main(mContext), 0);
252         }
253 }
254
255
256 void DocumentImporter::translate_anim_recursive(COLLADAFW::Node *node, COLLADAFW::Node *par = NULL, Object *parob = NULL)
257 {
258
259         // The split in #29246, rootmap must point at actual root when
260         // calculating bones in apply_curves_as_matrix. - actual root is the root node.
261         // This has to do with inverse bind poses being world space
262         // (the sources for skinned bones' restposes) and the way
263         // non-skinning nodes have their "restpose" recursively calculated.
264         // XXX TODO: design issue, how to support unrelated joints taking
265         // part in skinning.
266         if (par) { // && par->getType() == COLLADAFW::Node::JOINT) {
267                 // par is root if there's no corresp. key in root_map
268                 if (root_map.find(par->getUniqueId()) == root_map.end())
269                         root_map[node->getUniqueId()] = node;
270                 else
271                         root_map[node->getUniqueId()] = root_map[par->getUniqueId()];
272         }
273
274 #if 0
275         COLLADAFW::Transformation::TransformationType types[] = {
276                 COLLADAFW::Transformation::ROTATE,
277                 COLLADAFW::Transformation::SCALE,
278                 COLLADAFW::Transformation::TRANSLATE,
279                 COLLADAFW::Transformation::MATRIX
280         };
281
282         Object *ob;
283 #endif
284         unsigned int i;
285
286         //for (i = 0; i < 4; i++)
287         //    ob =
288         anim_importer.translate_Animations(node, root_map, object_map, FW_object_map);
289
290         COLLADAFW::NodePointerArray &children = node->getChildNodes();
291         for (i = 0; i < children.getCount(); i++) {
292                 translate_anim_recursive(children[i], node, NULL);
293         }
294 }
295
296 /** When this method is called, the writer must write the global document asset.
297  * \return The writer should return true, if writing succeeded, false otherwise.*/
298 bool DocumentImporter::writeGlobalAsset(const COLLADAFW::FileInfo *asset)
299 {
300         unit_converter.read_asset(asset);
301
302         return true;
303 }
304
305 /** When this method is called, the writer must write the scene.
306  * \return The writer should return true, if writing succeeded, false otherwise.*/
307 bool DocumentImporter::writeScene(const COLLADAFW::Scene *scene)
308 {
309         // XXX could store the scene id, but do nothing for now
310         return true;
311 }
312 Object *DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera, Scene *sce)
313 {
314         const COLLADAFW::UniqueId& cam_uid = camera->getInstanciatedObjectId();
315         if (uid_camera_map.find(cam_uid) == uid_camera_map.end()) {
316                 // fprintf(stderr, "Couldn't find camera by UID.\n");
317                 return NULL;
318         }
319
320         Object *ob = bc_add_object(sce, OB_CAMERA, NULL);
321         Camera *cam = uid_camera_map[cam_uid];
322         Camera *old_cam = (Camera *)ob->data;
323         ob->data = cam;
324         old_cam->id.us--;
325         if (old_cam->id.us == 0)
326                 BKE_libblock_free(&G.main->camera, old_cam);
327         return ob;
328 }
329
330 Object *DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Scene *sce)
331 {
332         const COLLADAFW::UniqueId& lamp_uid = lamp->getInstanciatedObjectId();
333         if (uid_lamp_map.find(lamp_uid) == uid_lamp_map.end()) {
334                 fprintf(stderr, "Couldn't find lamp by UID.\n");
335                 return NULL;
336         }
337
338         Object *ob = bc_add_object(sce, OB_LAMP, NULL);
339         Lamp *la = uid_lamp_map[lamp_uid];
340         Lamp *old_lamp = (Lamp *)ob->data;
341         ob->data = la;
342         old_lamp->id.us--;
343         if (old_lamp->id.us == 0)
344                 BKE_libblock_free(&G.main->lamp, old_lamp);
345         return ob;
346 }
347
348 Object *DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Node *source_node, COLLADAFW::Node *instance_node, Scene *sce, bool is_library_node)
349 {
350         fprintf(stderr, "create <instance_node> under node id=%s from node id=%s\n", instance_node ? instance_node->getOriginalId().c_str() : NULL, source_node ? source_node->getOriginalId().c_str() : NULL);
351
352         Object *obn = BKE_object_copy(source_ob);
353         obn->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
354         BKE_scene_base_add(sce, obn);
355
356         if (instance_node) {
357                 anim_importer.read_node_transform(instance_node, obn);
358                 // if we also have a source_node (always ;), take its
359                 // transformation matrix and apply it to the newly instantiated
360                 // object to account for node hierarchy transforms in
361                 // .dae
362                 if (source_node) {
363                         COLLADABU::Math::Matrix4 mat4 = source_node->getTransformationMatrix();
364                         COLLADABU::Math::Matrix4 bmat4 = mat4.transpose(); // transpose to get blender row-major order
365                         float mat[4][4];
366                         for (int i = 0; i < 4; i++) {
367                                 for (int j = 0; j < 4; j++) {
368                                         mat[i][j] = bmat4[i][j];
369                                 }
370                         }
371                         // calc new matrix and apply
372                         mult_m4_m4m4(obn->obmat, obn->obmat, mat);
373                         BKE_object_apply_mat4(obn, obn->obmat, 0, 0);
374                 }
375         }
376         else {
377                 anim_importer.read_node_transform(source_node, obn);
378         }
379
380         /*DAG_scene_sort(CTX_data_main(mContext), sce);
381         DAG_ids_flush_update(CTX_data_main(mContext), 0);*/
382
383         COLLADAFW::NodePointerArray &children = source_node->getChildNodes();
384         if (children.getCount()) {
385                 for (unsigned int i = 0; i < children.getCount(); i++) {
386                         COLLADAFW::Node *child_node = children[i];
387                         const COLLADAFW::UniqueId& child_id = child_node->getUniqueId();
388                         if (object_map.find(child_id) == object_map.end())
389                                 continue;
390                         COLLADAFW::InstanceNodePointerArray &inodes = child_node->getInstanceNodes();
391                         Object *new_child = NULL;
392                         if (inodes.getCount()) { // \todo loop through instance nodes
393                                 const COLLADAFW::UniqueId& id = inodes[0]->getInstanciatedObjectId();
394                                 fprintf(stderr, "Doing %d child nodes\n", (int)node_map.count(id));
395                                 new_child = create_instance_node(object_map.find(id)->second, node_map[id], child_node, sce, is_library_node);
396                         }
397                         else {
398                                 new_child = create_instance_node(object_map.find(child_id)->second, child_node, NULL, sce, is_library_node);
399                         }
400                         bc_set_parent(new_child, obn, mContext, true);
401
402                         if (is_library_node)
403                                 libnode_ob.push_back(new_child);
404                 }
405         }
406
407         return obn;
408 }
409
410 // to create constraints off node <extra> tags. Assumes only constraint data in
411 // current <extra> with blender profile.
412 void DocumentImporter::create_constraints(ExtraTags *et, Object *ob){
413         if ( et && et->isProfile("blender")){
414                 std::string name;
415                 short* type = 0;
416                 et->setData("type", type);
417                 BKE_add_ob_constraint(ob, "Test_con", *type);
418                 
419         }
420 }
421
422 void DocumentImporter::write_node(COLLADAFW::Node *node, COLLADAFW::Node *parent_node, Scene *sce, Object *par, bool is_library_node)
423 {
424         Object *ob = NULL;
425         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
426         bool read_transform = true;
427
428         std::vector<Object *> *objects_done = new std::vector<Object *>();
429
430         if (is_joint) {
431                 armature_importer.add_joint(node, parent_node == NULL || parent_node->getType() != COLLADAFW::Node::JOINT, par, sce);
432         }
433         else {
434                 COLLADAFW::InstanceGeometryPointerArray &geom = node->getInstanceGeometries();
435                 COLLADAFW::InstanceCameraPointerArray &camera = node->getInstanceCameras();
436                 COLLADAFW::InstanceLightPointerArray &lamp = node->getInstanceLights();
437                 COLLADAFW::InstanceControllerPointerArray &controller = node->getInstanceControllers();
438                 COLLADAFW::InstanceNodePointerArray &inst_node = node->getInstanceNodes();
439                 size_t geom_done = 0;
440                 size_t camera_done = 0;
441                 size_t lamp_done = 0;
442                 size_t controller_done = 0;
443                 size_t inst_done = 0;
444
445                 // XXX linking object with the first <instance_geometry>, though a node may have more of them...
446                 // maybe join multiple <instance_...> meshes into 1, and link object with it? not sure...
447                 // <instance_geometry>
448                 while (geom_done < geom.getCount()) {
449                         ob = mesh_importer.create_mesh_object(node, geom[geom_done], false, uid_material_map,
450                                                               material_texture_mapping_map);
451                         if (ob == NULL) {
452                                 std::string id   = node->getOriginalId();
453                                 std::string name = node->getName();
454                                 fprintf(stderr,
455                                                 "<node id=\"%s\", name=\"%s\" >...contains a reference to an unknown instance_mesh.\n",
456                                                 id.c_str(),
457                                                 name.c_str());
458                         }
459                         else {
460                                 objects_done->push_back(ob);
461                         }
462                         ++geom_done;
463                 }
464                 while (camera_done < camera.getCount()) {
465                         ob = create_camera_object(camera[camera_done], sce);
466                         if (ob == NULL) {
467                                 std::string id   = node->getOriginalId();
468                                 std::string name = node->getName();
469                                 fprintf(stderr, "<node id=\"%s\", name=\"%s\" >...contains a reference to an unknown instance_camera.\n", id.c_str(), name.c_str());
470                         }
471                         else
472                                 objects_done->push_back(ob);
473                         ++camera_done;
474                 }
475                 while (lamp_done < lamp.getCount()) {
476                         ob = create_lamp_object(lamp[lamp_done], sce);
477                         objects_done->push_back(ob);
478                         ++lamp_done;
479                 }
480                 while (controller_done < controller.getCount()) {
481                         COLLADAFW::InstanceGeometry *geom = (COLLADAFW::InstanceGeometry *)controller[controller_done];
482                         ob = mesh_importer.create_mesh_object(node, geom, true, uid_material_map, material_texture_mapping_map);
483                         objects_done->push_back(ob);
484                         ++controller_done;
485                 }
486                 // XXX instance_node is not supported yet
487                 while (inst_done < inst_node.getCount()) {
488                         const COLLADAFW::UniqueId& node_id = inst_node[inst_done]->getInstanciatedObjectId();
489                         if (object_map.find(node_id) == object_map.end()) {
490                                 fprintf(stderr, "Cannot find object for node referenced by <instance_node name=\"%s\">.\n", inst_node[inst_done]->getName().c_str());
491                                 ob = NULL;
492                         }
493                         else {
494                                 std::pair<std::multimap<COLLADAFW::UniqueId, Object *>::iterator, std::multimap<COLLADAFW::UniqueId, Object *>::iterator> pair_iter = object_map.equal_range(node_id);
495                                 for (std::multimap<COLLADAFW::UniqueId, Object *>::iterator it2 = pair_iter.first; it2 != pair_iter.second; it2++) {
496                                         Object *source_ob = (Object *)it2->second;
497                                         COLLADAFW::Node *source_node = node_map[node_id];
498                                         ob = create_instance_node(source_ob, source_node, node, sce, is_library_node);
499                                 }
500                         }
501                         if (ob != NULL) objects_done->push_back(ob);
502                         ++inst_done;
503
504                         read_transform = false;
505                 }
506
507                 // if node is empty - create empty object
508                 // XXX empty node may not mean it is empty object, not sure about this
509                 if ( (geom_done + camera_done + lamp_done + controller_done + inst_done) < 1) {
510                         //Check if Object is armature, by checking if immediate child is a JOINT node.
511                         if(is_armature(node))
512                                 ob = bc_add_object(sce, OB_ARMATURE, NULL);
513                         else ob = bc_add_object(sce, OB_EMPTY, NULL);
514
515                         objects_done->push_back(ob);
516                 }
517                 
518                 // XXX: if there're multiple instances, only one is stored
519
520                 if (!ob) return;
521                 for (std::vector<Object *>::iterator it = objects_done->begin(); it != objects_done->end(); ++it) {
522                         ob = *it;
523                         std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
524                         rename_id(&ob->id, (char *)nodename.c_str());
525                         object_map.insert(std::make_pair<COLLADAFW::UniqueId, Object *>(node->getUniqueId(), ob));
526                         node_map[node->getUniqueId()] = node;
527
528                         if (is_library_node)
529                                 libnode_ob.push_back(ob);
530                 }
531
532                 //create_constraints(et,ob);
533
534         }
535
536         for (std::vector<Object *>::iterator it = objects_done->begin(); it != objects_done->end(); ++it) {
537                 ob = *it;
538
539                 if (read_transform)
540                         anim_importer.read_node_transform(node, ob);  // overwrites location set earlier
541
542                 if (!is_joint) {
543                         // if par was given make this object child of the previous
544                         if (par && ob)
545                                 bc_set_parent(ob, par, mContext);
546                 }
547         }
548         // if node has child nodes write them
549         COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes();
550         ob = *objects_done->begin();
551         for (unsigned int i = 0; i < child_nodes.getCount(); i++) {
552                 write_node(child_nodes[i], node, sce, ob, is_library_node);
553         }
554 }
555
556 /** When this method is called, the writer must write the entire visual scene.
557  * \return The writer should return true, if writing succeeded, false otherwise.*/
558 bool DocumentImporter::writeVisualScene(const COLLADAFW::VisualScene *visualScene)
559 {
560         if (mImportStage != General)
561                 return true;
562                 
563         // this method called on post process after writeGeometry, writeMaterial, etc.
564
565         // for each <node> in <visual_scene>:
566         // create an Object
567         // if Mesh (previously created in writeGeometry) to which <node> corresponds exists, link Object with that mesh
568
569         // update: since we cannot link a Mesh with Object in
570         // writeGeometry because <geometry> does not reference <node>,
571         // we link Objects with Meshes here
572
573         vscenes.push_back(visualScene);
574         
575         return true;
576 }
577
578 /** When this method is called, the writer must handle all nodes contained in the 
579  * library nodes.
580  * \return The writer should return true, if writing succeeded, false otherwise.*/
581 bool DocumentImporter::writeLibraryNodes(const COLLADAFW::LibraryNodes *libraryNodes)
582 {
583         if (mImportStage != General)
584                 return true;
585                 
586         Scene *sce = CTX_data_scene(mContext);
587
588         const COLLADAFW::NodePointerArray& nodes = libraryNodes->getNodes();
589
590         for (unsigned int i = 0; i < nodes.getCount(); i++) {
591                 write_node(nodes[i], NULL, sce, NULL, true);
592         }
593
594         return true;
595 }
596
597 /** When this method is called, the writer must write the geometry.
598  * \return The writer should return true, if writing succeeded, false otherwise.*/
599 bool DocumentImporter::writeGeometry(const COLLADAFW::Geometry *geom)
600 {
601         if (mImportStage != General)
602                 return true;
603                 
604         return mesh_importer.write_geometry(geom);
605 }
606
607 /** When this method is called, the writer must write the material.
608  * \return The writer should return true, if writing succeeded, false otherwise.*/
609 bool DocumentImporter::writeMaterial(const COLLADAFW::Material *cmat)
610 {
611         if (mImportStage != General)
612                 return true;
613                 
614         const std::string& str_mat_id = cmat->getName().size() ? cmat->getName() : cmat->getOriginalId();
615         Material *ma = BKE_material_add(G.main, (char *)str_mat_id.c_str());
616         
617         this->uid_effect_map[cmat->getInstantiatedEffect()] = ma;
618         this->uid_material_map[cmat->getUniqueId()] = ma;
619         
620         return true;
621 }
622
623 // create mtex, create texture, set texture image
624 MTex *DocumentImporter::create_texture(COLLADAFW::EffectCommon *ef, COLLADAFW::Texture &ctex, Material *ma,
625                                        int i, TexIndexTextureArrayMap &texindex_texarray_map)
626 {
627         COLLADAFW::SamplerPointerArray& samp_array = ef->getSamplerPointerArray();
628         COLLADAFW::Sampler *sampler = samp_array[ctex.getSamplerId()];
629                 
630         const COLLADAFW::UniqueId& ima_uid = sampler->getSourceImage();
631         
632         if (uid_image_map.find(ima_uid) == uid_image_map.end()) {
633                 fprintf(stderr, "Couldn't find an image by UID.\n");
634                 return NULL;
635         }
636         
637         ma->mtex[i] = add_mtex();
638         ma->mtex[i]->texco = TEXCO_UV;
639         ma->mtex[i]->tex = add_texture(G.main, "Texture");
640         ma->mtex[i]->tex->type = TEX_IMAGE;
641         ma->mtex[i]->tex->ima = uid_image_map[ima_uid];
642         
643         texindex_texarray_map[ctex.getTextureMapId()].push_back(ma->mtex[i]);
644         
645         return ma->mtex[i];
646 }
647
648 void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
649 {
650         COLLADAFW::EffectCommon::ShaderType shader = ef->getShaderType();
651         
652         // blinn
653         if (shader == COLLADAFW::EffectCommon::SHADER_BLINN) {
654                 ma->spec_shader = MA_SPEC_BLINN;
655                 ma->spec = ef->getShininess().getFloatValue();
656         }
657         // phong
658         else if (shader == COLLADAFW::EffectCommon::SHADER_PHONG) {
659                 ma->spec_shader = MA_SPEC_PHONG;
660                 ma->har = ef->getShininess().getFloatValue();
661         }
662         // lambert
663         else if (shader == COLLADAFW::EffectCommon::SHADER_LAMBERT) {
664                 ma->diff_shader = MA_DIFF_LAMBERT;
665         }
666         // default - lambert
667         else {
668                 ma->diff_shader = MA_DIFF_LAMBERT;
669                 fprintf(stderr, "Current shader type is not supported, default to lambert.\n");
670         }
671         // reflectivity
672         ma->ray_mirror = ef->getReflectivity().getFloatValue();
673         // index of refraction
674         ma->ang = ef->getIndexOfRefraction().getFloatValue();
675         
676         int i = 0;
677         COLLADAFW::Color col;
678         MTex *mtex = NULL;
679         TexIndexTextureArrayMap texindex_texarray_map;
680         
681         // DIFFUSE
682         // color
683         if (ef->getDiffuse().isColor()) {
684                 col = ef->getDiffuse().getColor();
685                 ma->r = col.getRed();
686                 ma->g = col.getGreen();
687                 ma->b = col.getBlue();
688         }
689         // texture
690         else if (ef->getDiffuse().isTexture()) {
691                 COLLADAFW::Texture ctex = ef->getDiffuse().getTexture(); 
692                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
693                 if (mtex != NULL) {
694                         mtex->mapto = MAP_COL;
695                         ma->texact = (int)i;
696                         i++;
697                 }
698         }
699         // AMBIENT
700         // color
701         if (ef->getAmbient().isColor()) {
702                 col = ef->getAmbient().getColor();
703                 ma->ambr = col.getRed();
704                 ma->ambg = col.getGreen();
705                 ma->ambb = col.getBlue();
706         }
707         // texture
708         else if (ef->getAmbient().isTexture()) {
709                 COLLADAFW::Texture ctex = ef->getAmbient().getTexture(); 
710                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
711                 if (mtex != NULL) {
712                         mtex->mapto = MAP_AMB; 
713                         i++;
714                 }
715         }
716         // SPECULAR
717         // color
718         if (ef->getSpecular().isColor()) {
719                 col = ef->getSpecular().getColor();
720                 ma->specr = col.getRed();
721                 ma->specg = col.getGreen();
722                 ma->specb = col.getBlue();
723         }
724         // texture
725         else if (ef->getSpecular().isTexture()) {
726                 COLLADAFW::Texture ctex = ef->getSpecular().getTexture(); 
727                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
728                 if (mtex != NULL) {
729                         mtex->mapto = MAP_SPEC; 
730                         i++;
731                 }
732         }
733         // REFLECTIVE
734         // color
735         if (ef->getReflective().isColor()) {
736                 col = ef->getReflective().getColor();
737                 ma->mirr = col.getRed();
738                 ma->mirg = col.getGreen();
739                 ma->mirb = col.getBlue();
740         }
741         // texture
742         else if (ef->getReflective().isTexture()) {
743                 COLLADAFW::Texture ctex = ef->getReflective().getTexture(); 
744                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
745                 if (mtex != NULL) {
746                         mtex->mapto = MAP_REF; 
747                         i++;
748                 }
749         }
750         // EMISSION
751         // color
752         if (ef->getEmission().isColor()) {
753                 // XXX there is no emission color in blender
754                 // but I am not sure
755         }
756         // texture
757         else if (ef->getEmission().isTexture()) {
758                 COLLADAFW::Texture ctex = ef->getEmission().getTexture(); 
759                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
760                 if (mtex != NULL) {
761                         mtex->mapto = MAP_EMIT; 
762                         i++;
763                 }
764         }
765         
766         if (ef->getOpacity().isTexture()) {
767                 COLLADAFW::Texture ctex = ef->getOpacity().getTexture();
768                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
769                 if (mtex != NULL) {
770                         mtex->mapto = MAP_ALPHA;
771                         i++;
772                         ma->spectra = ma->alpha = 0;
773                         ma->mode |= MA_ZTRANSP | MA_TRANSP;
774                 }
775         }
776         // TRANSPARENT
777         // color
778 #if 0
779         if (ef->getOpacity().isColor()) {
780                 // XXX don't know what to do here
781         }
782         // texture
783         else if (ef->getOpacity().isTexture()) {
784                 ctex = ef->getOpacity().getTexture();
785                 if (mtex != NULL) mtex->mapto &= MAP_ALPHA;
786                 else {
787                         mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
788                         if (mtex != NULL) mtex->mapto = MAP_ALPHA;
789                 }
790         }
791 #endif
792         material_texture_mapping_map[ma] = texindex_texarray_map;
793 }
794
795 /** When this method is called, the writer must write the effect.
796  * \return The writer should return true, if writing succeeded, false otherwise.*/
797
798 bool DocumentImporter::writeEffect(const COLLADAFW::Effect *effect)
799 {
800         if (mImportStage != General)
801                 return true;
802         
803         const COLLADAFW::UniqueId& uid = effect->getUniqueId();
804         
805         if (uid_effect_map.find(uid) == uid_effect_map.end()) {
806                 fprintf(stderr, "Couldn't find a material by UID.\n");
807                 return true;
808         }
809         
810         Material *ma = uid_effect_map[uid];
811         std::map<COLLADAFW::UniqueId, Material *>::iterator iter;
812         for (iter = uid_material_map.begin(); iter != uid_material_map.end(); iter++) {
813                 if (iter->second == ma) {
814                         this->FW_object_map[iter->first] = effect;
815                         break;
816                 }
817         }
818         COLLADAFW::CommonEffectPointerArray common_efs = effect->getCommonEffects();
819         if (common_efs.getCount() < 1) {
820                 fprintf(stderr, "Couldn't find <profile_COMMON>.\n");
821                 return true;
822         }
823         // XXX TODO: Take all <profile_common>s
824         // Currently only first <profile_common> is supported
825         COLLADAFW::EffectCommon *ef = common_efs[0];
826         write_profile_COMMON(ef, ma);
827         this->FW_object_map[effect->getUniqueId()] = effect;
828                 
829         return true;
830 }
831
832
833 /** When this method is called, the writer must write the camera.
834  * \return The writer should return true, if writing succeeded, false otherwise.*/
835 bool DocumentImporter::writeCamera(const COLLADAFW::Camera *camera)
836 {
837         if (mImportStage != General)
838                 return true;
839                 
840         Camera *cam = NULL;
841         std::string cam_id, cam_name;
842         
843         cam_id = camera->getOriginalId();
844         cam_name = camera->getName();
845         if (cam_name.size()) cam = (Camera *)BKE_camera_add(G.main, (char *)cam_name.c_str());
846         else cam = (Camera *)BKE_camera_add(G.main, (char *)cam_id.c_str());
847         
848         if (!cam) {
849                 fprintf(stderr, "Cannot create camera.\n");
850                 return true;
851         }
852         cam->clipsta = camera->getNearClippingPlane().getValue();
853         cam->clipend = camera->getFarClippingPlane().getValue();
854         
855         COLLADAFW::Camera::CameraType type = camera->getCameraType();
856         switch (type) {
857                 case COLLADAFW::Camera::ORTHOGRAPHIC:
858                 {
859                         cam->type = CAM_ORTHO;
860                 }
861                 break;
862                 case COLLADAFW::Camera::PERSPECTIVE:
863                 {
864                         cam->type = CAM_PERSP;
865                 }
866                 break;
867                 case COLLADAFW::Camera::UNDEFINED_CAMERATYPE:
868                 {
869                         fprintf(stderr, "Current camera type is not supported.\n");
870                         cam->type = CAM_PERSP;
871                 }
872                 break;
873         }
874         
875         switch (camera->getDescriptionType()) {
876                 case COLLADAFW::Camera::ASPECTRATIO_AND_Y:
877                 {
878                         switch (cam->type) {
879                                 case CAM_ORTHO:
880                                 {
881                                         double ymag = 2 * camera->getYMag().getValue();
882                                         double aspect = camera->getAspectRatio().getValue();
883                                         double xmag = aspect * ymag;
884                                         cam->ortho_scale = (float)xmag;
885                                 }
886                                 break;
887                                 case CAM_PERSP:
888                                 default:
889                                 {
890                                         double yfov = camera->getYFov().getValue();
891                                         double aspect = camera->getAspectRatio().getValue();
892
893                                         // NOTE: Needs more testing (As we curretnly have no official test data for this)
894
895                                         double xfov = 2.0f * atanf(aspect * tanf(DEG2RADF(yfov) * 0.5f));
896                                         cam->lens = fov_to_focallength(xfov, cam->sensor_x);
897                                 }
898                                 break;
899                         }
900                 }
901                 break;
902                 /* XXX correct way to do following four is probably to get also render
903                  * size and determine proper settings from that somehow */
904                 case COLLADAFW::Camera::ASPECTRATIO_AND_X:
905                 case COLLADAFW::Camera::SINGLE_X:
906                 case COLLADAFW::Camera::X_AND_Y:
907                 {
908                         switch (cam->type) {
909                                 case CAM_ORTHO:
910                                         cam->ortho_scale = (float)camera->getXMag().getValue() * 2;
911                                         break;
912                                 case CAM_PERSP:
913                                 default:
914                                 {
915                                         double x = camera->getXFov().getValue();
916                                         // x is in degrees, cam->lens is in millimiters
917                                         cam->lens = fov_to_focallength(DEG2RADF(x), cam->sensor_x);
918                                 }
919                                 break;
920                         }
921                 }
922                 break;
923                 case COLLADAFW::Camera::SINGLE_Y:
924                 {
925                         switch (cam->type) {
926                                 case CAM_ORTHO:
927                                         cam->ortho_scale = (float)camera->getYMag().getValue();
928                                         break;
929                                 case CAM_PERSP:
930                                 default:
931                                 {
932                                         double yfov = camera->getYFov().getValue();
933                                         // yfov is in degrees, cam->lens is in millimiters
934                                         cam->lens = fov_to_focallength(DEG2RADF(yfov), cam->sensor_x);
935                                 }
936                                 break;
937                         }
938                 }
939                 break;
940                 case COLLADAFW::Camera::UNDEFINED:
941                         // read nothing, use blender defaults.
942                         break;
943         }
944         
945         this->uid_camera_map[camera->getUniqueId()] = cam;
946         this->FW_object_map[camera->getUniqueId()] = camera;
947         // XXX import camera options
948         return true;
949 }
950
951 /** When this method is called, the writer must write the image.
952  * \return The writer should return true, if writing succeeded, false otherwise.*/
953 bool DocumentImporter::writeImage(const COLLADAFW::Image *image)
954 {
955         if (mImportStage != General)
956                 return true;
957                 
958         // XXX maybe it is necessary to check if the path is absolute or relative
959         const std::string& filepath = image->getImageURI().toNativePath();
960         const char *filename = (const char *)mFilename.c_str();
961         char dir[FILE_MAX];
962         char full_path[FILE_MAX];
963         
964         BLI_split_dir_part(filename, dir, sizeof(dir));
965         BLI_join_dirfile(full_path, sizeof(full_path), dir, filepath.c_str());
966         Image *ima = BKE_image_load_exists(full_path);
967         if (!ima) {
968                 fprintf(stderr, "Cannot create image.\n");
969                 return true;
970         }
971         this->uid_image_map[image->getUniqueId()] = ima;
972         
973         return true;
974 }
975
976 /** When this method is called, the writer must write the light.
977  * \return The writer should return true, if writing succeeded, false otherwise.*/
978 bool DocumentImporter::writeLight(const COLLADAFW::Light *light)
979 {
980         if (mImportStage != General)
981                 return true;
982
983         Lamp *lamp = NULL;
984         std::string la_id, la_name;
985
986         ExtraTags *et = getExtraTags(light->getUniqueId());
987         /*TagsMap::iterator etit;
988         ExtraTags *et = 0;
989         etit = uid_tags_map.find(light->getUniqueId().toAscii());
990         if (etit != uid_tags_map.end())
991                 et = etit->second;*/
992
993         la_id = light->getOriginalId();
994         la_name = light->getName();
995         if (la_name.size()) lamp = (Lamp *)BKE_lamp_add(G.main, (char *)la_name.c_str());
996         else lamp = (Lamp *)BKE_lamp_add(G.main, (char *)la_id.c_str());
997
998         if (!lamp) {
999                 fprintf(stderr, "Cannot create lamp.\n");
1000                 return true;
1001         }
1002
1003         // if we find an ExtraTags for this, use that instead.
1004         if (et && et->isProfile("blender")) {
1005                 et->setData("type", &(lamp->type));
1006                 et->setData("flag", &(lamp->flag));
1007                 et->setData("mode", &(lamp->mode));
1008                 et->setData("gamma", &(lamp->k));
1009                 et->setData("red", &(lamp->r));
1010                 et->setData("green", &(lamp->g));
1011                 et->setData("blue", &(lamp->b));
1012                 et->setData("shadow_r", &(lamp->shdwr));
1013                 et->setData("shadow_g", &(lamp->shdwg));
1014                 et->setData("shadow_b", &(lamp->shdwb));
1015                 et->setData("energy", &(lamp->energy));
1016                 et->setData("dist", &(lamp->dist));
1017                 et->setData("spotsize", &(lamp->spotsize));
1018                 et->setData("spotblend", &(lamp->spotblend));
1019                 et->setData("halo_intensity", &(lamp->haint));
1020                 et->setData("att1", &(lamp->att1));
1021                 et->setData("att2", &(lamp->att2));
1022                 et->setData("falloff_type", &(lamp->falloff_type));
1023                 et->setData("clipsta", &(lamp->clipsta));
1024                 et->setData("clipend", &(lamp->clipend));
1025                 et->setData("shadspotsize", &(lamp->shadspotsize));
1026                 et->setData("bias", &(lamp->bias));
1027                 et->setData("soft", &(lamp->soft));
1028                 et->setData("compressthresh", &(lamp->compressthresh));
1029                 et->setData("bufsize", &(lamp->bufsize));
1030                 et->setData("samp", &(lamp->samp));
1031                 et->setData("buffers", &(lamp->buffers));
1032                 et->setData("filtertype", &(lamp->filtertype));
1033                 et->setData("bufflag", &(lamp->bufflag));
1034                 et->setData("buftype", &(lamp->buftype));
1035                 et->setData("ray_samp", &(lamp->ray_samp));
1036                 et->setData("ray_sampy", &(lamp->ray_sampy));
1037                 et->setData("ray_sampz", &(lamp->ray_sampz));
1038                 et->setData("ray_samp_type", &(lamp->ray_samp_type));
1039                 et->setData("area_shape", &(lamp->area_shape));
1040                 et->setData("area_size", &(lamp->area_size));
1041                 et->setData("area_sizey", &(lamp->area_sizey));
1042                 et->setData("area_sizez", &(lamp->area_sizez));
1043                 et->setData("adapt_thresh", &(lamp->adapt_thresh));
1044                 et->setData("ray_samp_method", &(lamp->ray_samp_method));
1045                 et->setData("shadhalostep", &(lamp->shadhalostep));
1046                 et->setData("sun_effect_type", &(lamp->shadhalostep));
1047                 et->setData("skyblendtype", &(lamp->skyblendtype));
1048                 et->setData("horizon_brightness", &(lamp->horizon_brightness));
1049                 et->setData("spread", &(lamp->spread));
1050                 et->setData("sun_brightness", &(lamp->sun_brightness));
1051                 et->setData("sun_size", &(lamp->sun_size));
1052                 et->setData("backscattered_light", &(lamp->backscattered_light));
1053                 et->setData("sun_intensity", &(lamp->sun_intensity));
1054                 et->setData("atm_turbidity", &(lamp->atm_turbidity));
1055                 et->setData("atm_extinction_factor", &(lamp->atm_extinction_factor));
1056                 et->setData("atm_distance_factor", &(lamp->atm_distance_factor));
1057                 et->setData("skyblendfac", &(lamp->skyblendfac));
1058                 et->setData("sky_exposure", &(lamp->sky_exposure));
1059                 et->setData("sky_colorspace", &(lamp->sky_colorspace));
1060         }
1061         else {
1062                 float constatt = light->getConstantAttenuation().getValue();
1063                 float linatt = light->getLinearAttenuation().getValue();
1064                 float quadatt = light->getQuadraticAttenuation().getValue();
1065                 float d = 25.0f;
1066                 float att1 = 0.0f;
1067                 float att2 = 0.0f;
1068                 float e = 1.0f;
1069
1070                 if (light->getColor().isValid()) {
1071                         COLLADAFW::Color col = light->getColor();
1072                         lamp->r = col.getRed();
1073                         lamp->g = col.getGreen();
1074                         lamp->b = col.getBlue();
1075                 }
1076
1077                 if (IS_EQ(linatt, 0.0f) && quadatt > 0.0f) {
1078                         att2 = quadatt;
1079                         d = sqrt(1.0f / quadatt);
1080                 }
1081                 // linear light
1082                 else if (IS_EQ(quadatt, 0.0f) && linatt > 0.0f) {
1083                         att1 = linatt;
1084                         d = (1.0f / linatt);
1085                 }
1086                 else if (IS_EQ(constatt, 1.0f)) {
1087                         att1 = 1.0f;
1088                 }
1089                 else {
1090                         // assuming point light (const att = 1.0);
1091                         att1 = 1.0f;
1092                 }
1093                 
1094                 d *= (1.0f / unit_converter.getLinearMeter());
1095
1096                 lamp->energy = e;
1097                 lamp->dist = d;
1098
1099                 COLLADAFW::Light::LightType type = light->getLightType();
1100                 switch (type) {
1101                         case COLLADAFW::Light::AMBIENT_LIGHT:
1102                         {
1103                                 lamp->type = LA_HEMI;
1104                         }
1105                         break;
1106                         case COLLADAFW::Light::SPOT_LIGHT:
1107                         {
1108                                 lamp->type = LA_SPOT;
1109                                 lamp->att1 = att1;
1110                                 lamp->att2 = att2;
1111                                 if (IS_EQ(att1, 0.0f) && att2 > 0)
1112                                         lamp->falloff_type = LA_FALLOFF_INVSQUARE;
1113                                 if (IS_EQ(att2, 0.0f) && att1 > 0)
1114                                         lamp->falloff_type = LA_FALLOFF_INVLINEAR;
1115                                 lamp->spotsize = light->getFallOffAngle().getValue();
1116                                 lamp->spotblend = light->getFallOffExponent().getValue();
1117                         }
1118                         break;
1119                         case COLLADAFW::Light::DIRECTIONAL_LIGHT:
1120                         {
1121                                 /* our sun is very strong, so pick a smaller energy level */
1122                                 lamp->type = LA_SUN;
1123                                 lamp->mode |= LA_NO_SPEC;
1124                         }
1125                         break;
1126                         case COLLADAFW::Light::POINT_LIGHT:
1127                         {
1128                                 lamp->type = LA_LOCAL;
1129                                 lamp->att1 = att1;
1130                                 lamp->att2 = att2;
1131                                 if (IS_EQ(att1, 0.0f) && att2 > 0)
1132                                         lamp->falloff_type = LA_FALLOFF_INVSQUARE;
1133                                 if (IS_EQ(att2, 0.0f) && att1 > 0)
1134                                         lamp->falloff_type = LA_FALLOFF_INVLINEAR;
1135                         }
1136                         break;
1137                         case COLLADAFW::Light::UNDEFINED:
1138                         {
1139                                 fprintf(stderr, "Current lamp type is not supported.\n");
1140                                 lamp->type = LA_LOCAL;
1141                         }
1142                         break;
1143                 }
1144         }
1145
1146         this->uid_lamp_map[light->getUniqueId()] = lamp;
1147         this->FW_object_map[light->getUniqueId()] = light;
1148         return true;
1149 }
1150
1151 // this function is called only for animations that pass COLLADAFW::validate
1152 bool DocumentImporter::writeAnimation(const COLLADAFW::Animation *anim)
1153 {
1154         if (mImportStage != General)
1155                 return true;
1156                 
1157         // return true;
1158         return anim_importer.write_animation(anim);
1159 }
1160
1161 // called on post-process stage after writeVisualScenes
1162 bool DocumentImporter::writeAnimationList(const COLLADAFW::AnimationList *animationList)
1163 {
1164         if (mImportStage != General)
1165                 return true;
1166                 
1167         // return true;
1168         return anim_importer.write_animation_list(animationList);
1169 }
1170
1171 /** When this method is called, the writer must write the skin controller data.
1172  * \return The writer should return true, if writing succeeded, false otherwise.*/
1173 bool DocumentImporter::writeSkinControllerData(const COLLADAFW::SkinControllerData *skin)
1174 {
1175         return armature_importer.write_skin_controller_data(skin);
1176 }
1177
1178 // this is called on postprocess, before writeVisualScenes
1179 bool DocumentImporter::writeController(const COLLADAFW::Controller *controller)
1180 {
1181         if (mImportStage != General)
1182                 return true;
1183                 
1184         return armature_importer.write_controller(controller);
1185 }
1186
1187 bool DocumentImporter::writeFormulas(const COLLADAFW::Formulas *formulas)
1188 {
1189         return true;
1190 }
1191
1192 bool DocumentImporter::writeKinematicsScene(const COLLADAFW::KinematicsScene *kinematicsScene)
1193 {
1194         return true;
1195 }
1196
1197 ExtraTags *DocumentImporter::getExtraTags(const COLLADAFW::UniqueId &uid)
1198 {
1199         if (uid_tags_map.find(uid.toAscii()) == uid_tags_map.end()) {
1200                 return NULL;
1201         }
1202         return uid_tags_map[uid.toAscii()];
1203 }
1204
1205 bool DocumentImporter::addExtraTags(const COLLADAFW::UniqueId &uid, ExtraTags *extra_tags)
1206 {
1207         uid_tags_map[uid.toAscii()] = extra_tags;
1208         return true;
1209 }
1210
1211 bool DocumentImporter::is_armature(COLLADAFW::Node *node){
1212         COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes();
1213         for (unsigned int i = 0; i < child_nodes.getCount(); i++) {     
1214                 if(child_nodes[i]->getType() == COLLADAFW::Node::JOINT) return true;
1215                 else continue;
1216         }
1217
1218         //no child is JOINT
1219         return false;
1220
1221 }