Click here to Skip to main content
16,012,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have some problems about FixedUpdate : the animation is not played because some datas are "NaN" instead of a float value.
I want to the program doesn't save datas which have "NaN". How can I do that ?

C#
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 paused;
	
	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;

		
		paused = true;
		frame = 0;
		
		StreamReader reader = new StreamReader ("Suj01_PL_DT_C00_4.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 (); 
			
			// At this point we are starting to read datas lines and it's here that we have to indicate to don't save "NaN"
			datas = new string[4000][]; 
			
			int counter = 0;
			
			while (line != null) {
				counter++;
				line = reader.ReadLine ();
				
				if (line == "ANALOG") 
					break;
 				
				if ((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++) { 
					
					
					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;
		}
	}
	
	void FixedUpdate() {
		
// or it's here that we have to indicate to don't save "NaN"

		delta_time += Time.deltaTime; 

		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++; 	
		}

	}
}



I have indicated in comments where we have to don't save "NaN"

Thank you :)
Posted
Comments
Andy Lanng 17-Apr-15 4:14am    
Small clarification:
Do you mean that you want to skip when the line equals the string "NaN", or do you mean that you need to detect if the string is not a number (i.e. "18" is ok but "1B" is not)?
Thanks
Coralie B 17-Apr-15 4:19am    
I have to skip, because otherwise the animation is stopped car it can't to read this data
Andy Lanng 17-Apr-15 4:45am    
That comment adds nothing.
I will do my best to answer. Please clarify if I mis-understood

So - I 'guess' that the code breaks here:
C#
for (int i = 0; i < cpt-1; ++i) { 
 for (int j = 1; j < names.Length+1; j++) { 
				
  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]);

 }
}


If breaks because something that is not a float get into datas[i].

If that is the case then you need to test the datas[i] string to ensure that it is a float. You can do this:
C#
for (int i = 0; i < cpt-1; ++i) { 
 for (int j = 1; j < names.Length+1; j++) { 
  float x, y, z;
  if(float.TyrParse(datas [i] [j * 3 - 2],out x) &&
     float.TyrParse(datas [i] [j * 3 - 1],out z) &&
     float.TyrParse(datas [i] [j * 3],out y)){
   nv_data [j - 1].positions [i].x = x; 
   nv_data [j - 1].positions [i].z = z;
   nv_data [j - 1].positions [i].y = y;
  }
  else{
   //idk.  throw something?  continue? break?
  }
 }
}
 
Share this answer
 
Comments
Coralie B 20-Apr-15 2:27am    
Thank you. But it doesn't run.. The code is correct, but when I play the animation, I have the same error message than before : "transform.localPosition assign attempt for 'Left_Elbow' is not valid. Input localPosition is { NaN, NaN, NaN }.
UnityEngine.Transform:set_localPosition(Vector3)"
Coralie B 20-Apr-15 3:07am    
Have you got any other ideas, please ?
Coralie B 20-Apr-15 3:08am    
I indicate "continue" after "else".
Coralie B 20-Apr-15 3:37am    
I found a solution !

But thank you again :)
C#
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]);

        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900