Blender V2.61 - r43446

KX_SG_BoneParentNodeRelationship.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include <iostream>
00034  
00035 #include "KX_SG_BoneParentNodeRelationship.h"
00036 
00037 #include "MT_Matrix4x4.h"
00038 #include "BL_ArmatureObject.h"
00039 
00040 
00049     KX_BoneParentRelation *
00050 KX_BoneParentRelation::
00051 New(Bone* bone
00052 ) {
00053     return new KX_BoneParentRelation(bone);
00054 }       
00055 
00056     bool
00057 KX_BoneParentRelation::
00058 UpdateChildCoordinates(
00059     SG_Spatial * child,
00060     const SG_Spatial * parent,
00061     bool& parentUpdated 
00062 ){
00063     MT_assert(child != NULL);
00064     
00065     // This way of accessing child coordinates is a bit cumbersome
00066     // be nice to have non constant reference access to these values.
00067 
00068     const MT_Vector3 & child_scale = child->GetLocalScale();
00069     const MT_Point3 & child_pos = child->GetLocalPosition();
00070     const MT_Matrix3x3 & child_rotation = child->GetLocalOrientation();
00071     // we don't know if the armature has been updated or not, assume yes
00072     parentUpdated = true;
00073 
00074     // the childs world locations which we will update. 
00075     
00076     MT_Vector3 child_w_scale;
00077     MT_Point3 child_w_pos;
00078     MT_Matrix3x3 child_w_rotation;
00079     
00080     bool valid_parent_transform = false;
00081     
00082     if (parent)
00083     {
00084         BL_ArmatureObject *armature = (BL_ArmatureObject*)(parent->GetSGClientObject());
00085         if (armature)
00086         {
00087             MT_Matrix4x4 parent_matrix;
00088             if (armature->GetBoneMatrix(m_bone, parent_matrix))
00089             {
00090                 // Get the child's transform, and the bone matrix.
00091                 MT_Matrix4x4 child_transform ( 
00092                     MT_Transform(child_pos + MT_Vector3(0.0, armature->GetBoneLength(m_bone), 0.0), 
00093                         child_rotation.scaled(
00094                             child_scale[0], 
00095                             child_scale[1], 
00096                             child_scale[2])));
00097                 
00098                 // The child's world transform is parent * child
00099                 parent_matrix = parent->GetWorldTransform() * parent_matrix;
00100                 child_transform = parent_matrix * child_transform;
00101                 
00102                 // Recompute the child transform components from the transform.
00103                 child_w_scale.setValue( 
00104                     MT_Vector3(child_transform[0][0], child_transform[0][1], child_transform[0][2]).length(),
00105                     MT_Vector3(child_transform[1][0], child_transform[1][1], child_transform[1][2]).length(),
00106                     MT_Vector3(child_transform[2][0], child_transform[2][1], child_transform[2][2]).length());
00107                 child_w_rotation.setValue(child_transform[0][0], child_transform[0][1], child_transform[0][2], 
00108                     child_transform[1][0], child_transform[1][1], child_transform[1][2], 
00109                     child_transform[2][0], child_transform[2][1], child_transform[2][2]);
00110                 child_w_rotation.scale(1.0/child_w_scale[0], 1.0/child_w_scale[1], 1.0/child_w_scale[2]);
00111                     
00112                 child_w_pos = MT_Point3(child_transform[0][3], child_transform[1][3], child_transform[2][3]);
00113                     
00114                 valid_parent_transform = true;
00115             }
00116         }
00117     } 
00118     
00119     if (valid_parent_transform)
00120     {
00121         child->SetWorldScale(child_w_scale);
00122         child->SetWorldPosition(child_w_pos);
00123         child->SetWorldOrientation(child_w_rotation);
00124     }
00125     else {
00126         child->SetWorldFromLocalTransform();
00127     }
00128     child->ClearModified();
00129     // this node must always be updated, so reschedule it for next time
00130     child->ActivateRecheduleUpdateCallback();
00131     return valid_parent_transform;
00132 }
00133 
00134     SG_ParentRelation *
00135 KX_BoneParentRelation::
00136 NewCopy(
00137 ){
00138     KX_BoneParentRelation* bone_parent = new KX_BoneParentRelation(m_bone);
00139     return bone_parent;
00140 }
00141 
00142 KX_BoneParentRelation::
00143 ~KX_BoneParentRelation(
00144 ){
00145     //nothing to do
00146 }
00147 
00148 
00149 KX_BoneParentRelation::
00150 KX_BoneParentRelation(Bone* bone
00151 )
00152 : m_bone(bone)
00153 {
00154     // nothing to do
00155 }