UnityMol  0.9.6-875
UnityMol viewer / In developement
GenInterpolationPoint_BF.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System.Collections;
68 using System.Collections.Generic;
69 
71 
72  //public parameters
73  public GameObject SplineParent;
74  public float Duration = 1.0f;
75  public int lineCount=1000;
76  public ArrayList InputKeyNodes;
77  public List<string> InputTypeArray;
78  //C.R
79  public List<float> InputBfactArray;
80 
81  public List<float[]> OutputKeyNodes;
82  public List<string> OutputTypeArray;
83  //C.R
84  public List<float> OutputBfactArray;
85 
86 // private Transform[] mTransforms;
87  private List<SplineNode> mNodes;
88 
89  public static int smoothnessFactor = 8 ;
90 
91  //test the node data varibles
92 
93  public GameObject temp;
94  public float moveSpeed=10f;
95 // private int curIdx=0;
96 // private float curTime=0;
97 // private float moveTime=0;
98  private Vector3 NextPos;
99  private Vector3 CurPos;
100  private Quaternion CurRot;
101  private Quaternion NextRot;
102 
103  private Vector3[] Nodes;
104 
105 
106  public void CalculateSpline() {
107  lineCount=InputKeyNodes.Count*smoothnessFactor;
108 
110  OutputKeyNodes=new List<float[]>();
111  OutputTypeArray=new List<string>();
112  //C.R
113  OutputBfactArray = new List<float> ();
114 
115  Gizmos.color = new Color(0.0f, 0.0f, 1.0f);
116  for (int c=1; c <lineCount; c++) {
117  float currTime = c * Duration / lineCount;
118  Vector3 currPos = GetHermiteAtTime(currTime);
119 
120  float[] currposfloat=new float[3];
121  currposfloat[0]=currPos.x;
122  currposfloat[1]=currPos.y;
123  currposfloat[2]=currPos.z;
124  OutputKeyNodes.Add(currposfloat);
125  OutputTypeArray.Add(InputTypeArray[0]);
126 // MonoBehaviour.print("CalculateSpline");
127  }
128  }
129 
131  int c;
132  float step;
133  mNodes = new List<SplineNode>();
134  step = Duration/InputKeyNodes.Count;
135 
136  for (c = 0; c < InputKeyNodes.Count; c++) {
137  float[] vect=(float[])InputKeyNodes[c];
138  Vector3 inputnode=new Vector3(vect[0],vect[1],vect[2]);
139  mNodes.Add(new SplineNode(inputnode, step*c));
140  }
141 // SetAutoCloseMode(step*c);
142 
143  mNodes.Insert(0,mNodes[0]);
144  mNodes.Add(mNodes[mNodes.Count-1]);
145  }
146 
147 
148  public Vector3 GetHermiteInternal(int idxFirstPoint ,float t){
149  float t2 = t*t; float t3 = t2*t;
150 
151  Vector3 P0 = mNodes[idxFirstPoint-1].Point;
152  Vector3 P1 = mNodes[idxFirstPoint].Point;
153  Vector3 P2 = mNodes[idxFirstPoint+1].Point;
154  Vector3 P3 = mNodes[idxFirstPoint+2].Point;
155 
156  float tension = 0.5f; // 0.5 equivale a catmull-rom
157 
158  Vector3 T1 = tension * (P2 - P0);
159  Vector3 T2 = tension * (P3 - P1);
160 
161  float Blend1 = 2*t3 - 3*t2 + 1;
162  float Blend2 = -2*t3 + 3*t2;
163  float Blend3 = t3 - 2*t2 + t;
164  float Blend4 = t3 - t2;
165 
166  //C.R
167 
168  if (idxFirstPoint < mNodes.Count - 3) {
169  OutputBfactArray.Add (InputBfactArray [idxFirstPoint] + ((InputBfactArray [idxFirstPoint+1] - InputBfactArray [idxFirstPoint]) / smoothnessFactor) * (t * smoothnessFactor));
170  // Debug.Log ("resultat " + ((InputBfactArray [idxFirstPoint] - InputBfactArray [idxFirstPoint + 1]) / smoothnessFactor)* (t * smoothnessFactor));
171  }else
172  OutputBfactArray.Add (InputBfactArray [idxFirstPoint]);
173 
174  // C.R
175 
176  return Blend1*P1 + Blend2*P2 + Blend3*T1 + Blend4*T2;
177  }
178 
179  public Vector3 GetHermiteAtTime(float timeParam){
180  int c;
181  if (timeParam >= mNodes [mNodes.Count - 2].Time) {
182  // C.R
183  OutputBfactArray.Add (InputBfactArray[InputBfactArray.Count-1]);
184  //C.R
185  return mNodes [mNodes.Count - 2].Point;
186  }
187 
188  for (c = 1; c < mNodes.Count-2; c++){
189  if (mNodes[c].Time > timeParam)
190  break;
191  }
192 
193  int idx = c-1;
194  float param = (timeParam - mNodes[idx].Time) / (mNodes[idx+1].Time - mNodes[idx].Time);
195  param = MathUtils.Ease(param, 0.0f, 0.1f);
196 
197  return GetHermiteInternal(idx, param);
198  }
199 
200  //define the node class
201  class SplineNode{
202  public Vector3 Point;
203  public float Time;
204 
205  public SplineNode(Vector3 p,float t){
206  Point=p;
207  Time=t;
208  }
209 
210  public SplineNode(SplineNode o){
211  Point=o.Point;
212  Time=o.Time;
213  }
214  }
215 
216 }
static float Ease(float t, float k1, float k2)
Definition: MathUtils.cs:147
Vector3 GetHermiteAtTime(float timeParam)
Vector3 GetHermiteInternal(int idxFirstPoint, float t)