package com.akjava.bvh.client;

import java.util.HashMap;
import java.util.Map;

import com.akjava.gwt.three.client.THREE;
import com.akjava.gwt.three.client.core.Matrix4;
import com.akjava.gwt.three.client.core.Vector3;
import com.akjava.gwt.three.client.gwt.animation.AnimationBone;
import com.akjava.gwt.three.client.gwt.animation.AnimationUtils;
import com.google.gwt.core.client.JsArray;

public class AnimationBoneConverter {

	public JsArray<AnimationBone> convertJsonBone(BVH bvh){
		parentIdMaps=new HashMap<String,Integer>();
		for(int i=0;i<bvh.getNameAndChannels().size();i++){
			parentIdMaps.put(bvh.getNameAndChannels().get(i).getName(), i);
		}
		JsArray<AnimationBone> array=(JsArray<AnimationBone>)JsArray.createArray();
		
		
		Map<String,AnimationBone> bones=new HashMap<String,AnimationBone>();
		BVHNode root=bvh.getHiearchy();
		
		Matrix4 mx=THREE.Matrix4();
		Vector3 pos=BVHUtils.toVector3(root.getOffset());
		mx.setPosition(pos);
		
		AnimationBone bone=AnimationUtils.createAnimationBone();
		bone.setParent(-1);
		bone.setName(root.getName());
		bone.setPos(pos);
		//TODO support rot
		bones.put(root.getName(), bone);
		
		
		convert(bones,root,mx);
		
		for(int i=0;i<bvh.getNameAndChannels().size();i++){
			AnimationBone abone=bones.get(bvh.getNameAndChannels().get(i).getName());
			array.push(abone);
		}
		return array;
	}
	private Map<String,Integer> parentIdMaps;
	
	private  void convert(Map<String,AnimationBone> map,BVHNode parent,Matrix4 parentMx){
		for(BVHNode children:parent.getJoints()){
			Matrix4 mx=THREE.Matrix4();
			Vector3 pos=BVHUtils.toVector3(children.getOffset());
			mx.setPosition(pos);
			AnimationBone bone=AnimationUtils.createAnimationBone();
			bone.setParent(parentIdMaps.get(parent.getName()));
			bone.setName(children.getName());
			//TODO support rot
			mx=mx.multiply(parentMx, mx);
			
			Vector3 mxPos=THREE.Vector3();
			mxPos.setPositionFromMatrix(mx);
			bone.setPos(mxPos);
			map.put(children.getName(), bone);
			
			convert(map,children,mx);
		}
	}
}

@@ -44,23 +44,76 @@
 va.util.
-HashMap
+ArrayList;%0D%0Aimport java.util.HashMap;%0D%0Aimport java.util.List
 ;%0D%0Aimpor
@@ -406,16 +406,56 @@
 Utils;%0D%0A
+import com.google.gwt.core.client.GWT;%0D%0A
 import c
@@ -533,16 +533,57 @@
 er %7B%0D%0A%0D%0A
+%09private List%3CString%3E nameOrderList;%0D%0A%09%0D%0A
 %09public 
@@ -632,16 +632,370 @@
  bvh)%7B%0D%0A
+%09%09nameOrderList=new ArrayList%3CString%3E();%0D%0A%09%09String oldName=null;%0D%0A%09%09for(int i=0;i%3Cbvh.getNameAndChannels().size();i++)%7B%0D%0A%09%09%09String newName=bvh.getNameAndChannels().get(i).getName();%0D%0A%0D%0A%09%09%09if(newName==null)%7B%0D%0A%09%09%09%09GWT.log(%22null-name%22);%0D%0A%09%09%09%7D%0D%0A%09%09%09%0D%0A%09%09%09if(!newName.equals(oldName))%7B%0D%0A%09%09%09%09nameOrderList.add(newName);%0D%0A%09%09%09%09oldName=newName;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09%0D%0A
 %09%09parent
@@ -1725,32 +1725,21 @@
 0;i%3C
-bvh.getNameAndChannels()
+nameOrderList
 .siz
@@ -1786,48 +1786,27 @@
 get(
-bvh.getNameAndChannels().get(i).getName(
+nameOrderList.get(i
 ));%0D

fix convertr bone bug