using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using System;
public class Recup_donnees_7 : MonoBehaviour {
private class Data_struct {
public String marker_name ;
public Vector3 [] positions ;
}
private Data_struct [] nv_data ;
private bool Skip_lines;
private Vector3[] forces;
private Vector3[] forces_dup;
private Vector3[] forces_virtual;
private Vector3[] forces_real;
private GameObject [] body = new GameObject[55];
string[][] datas;
string [][] datas_F;
string [] forces_virtuelles;
string [] forces_reelles;
int cpt =0;
int cpt_F = 0;
int frame ;
float delta_time;
float acceleration;
float ancienne_vitesse;
float vitesse;
float ancienne_Energie_Cinetique;
public Moindre_carre m1;
Moindre_carre m2 = new Moindre_carre ();
const int valurs_nb = 20;
float coef_index_acc = 0.2f;
double[] Y;
double[] t;
double[] sol;
double [] acc;
double [] vit;
double [] pos;
double pos_moindre = 0;
int index_acc = 0;
int indice_2 = -1;
int indice_3 = -1;
void Start() {
body = new GameObject[55];
body [0] = GameObject.Find ("Right_Hip");
body [1] = GameObject.Find ("Left_Hip");
body [4] = GameObject.Find ("Spine");
body [6] = GameObject.Find ("Right_Shoulder");
body [7] = GameObject.Find ("Left_Shoulder");
body [8] = GameObject.Find ("Throat");
body [12] = GameObject.Find ("Spine0");
body [13] = GameObject.Find ("Head");
body [17] = GameObject.Find ("Right_Elbow");
body [21] = GameObject.Find ("Right_Hand");
body [26] = GameObject.Find ("Left_Elbow");
body [30] = GameObject.Find ("Left_Hand");
body [35] = GameObject.Find ("Right_Knee");
body [44] = GameObject.Find ("Right_Foot");
body [45] = GameObject.Find ("Left_Knee");
body [54] = GameObject.Find ("Left_Foot");
delta_time = 0;
acceleration = 0;
ancienne_vitesse = 0;
m2.degre = 3;
m2.nombre_data = valurs_nb;
Skip_lines = false;
frame = 0;
StreamReader reader = new StreamReader ("Suj01_PL_DP_C20_3.txt");
using (reader) {
string line = " ";
int lineNumber = 10;
for (int n = 0; n < lineNumber; n++) {
line = reader.ReadLine ();
if (line == null)
return;
}
string line_10;
line_10 = line;
string[] names = line_10.Split (new String[] {",",",,,"}, StringSplitOptions.RemoveEmptyEntries);
nv_data = new Data_struct[names.Length];
for (int x =0; x< names.Length; ++x) {
nv_data [x] = new Data_struct ();
nv_data [x].marker_name = names [x];
}
line = reader.ReadLine ();
datas = new string[4000][];
int counter = 0;
while (line != null) {
counter++;
line = reader.ReadLine ();
if (line == "ANALOG")
break;
if (Skip_lines = true && (counter % 19) != 1)
continue;
string lines_datas;
lines_datas = line;
datas [cpt] = lines_datas.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries);
cpt ++;
}
for (int j = 1; j < names.Length+1; j++) {
nv_data [j - 1].positions = new Vector3[cpt];
}
for (int i = 0; i < cpt-1; ++i) {
for (int j = 1; j < names.Length+1; j++) {
if (((float.Parse(datas [i] [j * 3 - 2]).CompareTo(float.NaN)) == 0) && ((float.Parse(datas [i] [j * 3 - 1]).CompareTo(float.NaN)) == 0)
&& ((float.Parse(datas [i] [j * 3]).CompareTo(float.NaN)) == 0))
continue;
else
{
nv_data [j - 1].positions [i].x = float.Parse (datas [i] [j * 3 - 2]);
nv_data [j - 1].positions [i].z = float.Parse (datas [i] [j * 3 - 1]);
nv_data [j - 1].positions [i].y = float.Parse (datas [i] [j * 3]);
}
}
}
for (int m = 0; m < 3; m++) {
line = reader.ReadLine ();
}
datas_F = new string[8000][];
float Somme_x = 0;
float Somme_y = 0;
float Somme_z = 0;
int counter_F2 = 0;
while (line != null) {
counter_F2++;
line = reader.ReadLine ();
if (line == null)
break;
if (line != null && line.Length < 5)
break;
if ((counter_F2 % 200) != 0) {
string [] split_datas_F;
split_datas_F = line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries);
if (split_datas_F [3].Equals ("NaN"))
continue;
Somme_x += float.Parse (line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries) [3]);
Somme_y += float.Parse (line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries) [4]);
Somme_z += float.Parse (line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries) [5]);
} else {
datas_F [cpt_F] = line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries);
Somme_x /= 199;
Somme_y /= 199;
Somme_z /= 199;
datas_F [cpt_F] [3] = Somme_x.ToString ();
datas_F [cpt_F] [4] = Somme_y.ToString ();
datas_F [cpt_F] [5] = Somme_z.ToString ();
Somme_x = 0;
Somme_y = 0;
Somme_z = 0;
cpt_F ++;
}
}
Vector3[] forces = new Vector3[cpt_F];
for (int o = 0; o < cpt_F-1; ++o) {
forces [o].x = float.Parse (datas_F [o] [3]);
forces [o].y = float.Parse (datas_F [o] [4]);
forces [o].z = float.Parse (datas_F [o] [5]);
}
forces_dup = forces;
}
acc = new double[(nv_data [59].positions.Length / m2.nombre_data)];
vit = new double[(nv_data [59].positions.Length / m2.nombre_data)];
pos = new double[(nv_data [59].positions.Length / m2.nombre_data)];
Y = new double[m2.nombre_data];
t = new double[m2.nombre_data];
sol = new double[m2.degre];
while (index_acc + valurs_nb < nv_data[59].positions.Length) {
Y [index_acc % valurs_nb] = (double)nv_data [59].positions [index_acc].z / 1000;
t [index_acc % valurs_nb] = index_acc * 0.01;
index_acc++;
if (index_acc % valurs_nb == 0) {
indice_2 ++;
pos_moindre = ((acc [indice_2] / 2) * Math.Pow (index_acc * coef_index_acc, 2)) + vit [indice_2] * index_acc * coef_index_acc + pos [indice_2];
if (Math.Abs (pos_moindre - nv_data [59].positions [index_acc].z / 1000) > 0.01) {
m2.y = Y;
m2.t = t;
sol = m2.resolution ();
acc [indice_2] = 2 * sol [0];
vit [indice_2] = sol [1];
pos [indice_2] = sol [2];
} else {
if (indice_2 > 1) {
acc [indice_2] = acc [indice_2 - 1];
vit [indice_2] = vit [indice_2 - 1];
pos [indice_2] = pos [indice_2 - 1];
}
}
}
}
StreamWriter writer_1 = new StreamWriter ("Masse-fois-Acceleration_prediction__Time", true);
using (writer_1) {
for (int i = 0; i < indice_2; i++)
writer_1.WriteLine ((80*acc[i]) + "\t" + (i*coef_index_acc));
}
}
void FixedUpdate() {
delta_time += Time.deltaTime;
Skip_lines = true;
if (frame < nv_data[0].positions.Length) {
for (int k = 0; k < body.Length; ++k) {
if (body[k] != null) {
body[k].transform.localPosition = nv_data[k].positions[frame] / 1000;
}
else
continue;
}
if (frame < forces_dup.Length) {
if (frame >= 1) {
float delta_x = (((nv_data[59].positions[frame] - nv_data[59].positions[frame-1]).z)*0.001f);
vitesse = delta_x/Time.deltaTime;
acceleration = (vitesse-ancienne_vitesse)/Time.deltaTime;
ancienne_vitesse = vitesse;
indice_3 ++;
StreamWriter writer = new StreamWriter ("Masse-fois-Accelerations_FR_5Hz", true);
using (writer) {
writer.WriteLine(Time.time + "\t" + 80*acceleration + "\t" + forces_dup[indice_3].x);
}
}
}
frame++;
}
}
}