UnityMol  0.9.6-875
UnityMol viewer / In developement
ReadJson.cs
Go to the documentation of this file.
1 
66 namespace ParseData.ParsePDB
67 {
68 using UnityEngine;
69 using System.Collections;
70 using System.Collections.Generic;
71 using System.IO;
72 
73 public class ReadJson
74 {
75 
76  private List<List<Vector3>> m_fieldlines;
77 
78  public List<List<Vector3>> GetFieldLines()
79  {
80  return m_fieldlines;
81  }
82 
83 
84  public List<List<Vector3>> ReadFile(string fieldlines_file_content, Vector3 Offset)
85  {
86  List<List<Vector3>> linelist=new List<List<Vector3>>();
87  StringReader sr = new StringReader(fieldlines_file_content);
88  string line = null;
89  line = sr.ReadLine();
90  string[] tok = line.Split(' ');
91 
92  if(tok[0] == "n")
93  {
94  // We have an .apf file (animated potential fieldlines)
95  List<Vector3> particlelist = new List<Vector3>();
96  while(line != null)
97  {
98  tok = line.Split(' ');
99  if(tok[0] == "n" && particlelist.Count > 0)
100  {
101  linelist.Add(particlelist);
102  particlelist = new List<Vector3>();
103  }
104  if(tok[0] == "v")
105  {
106  //Unity has a left-handed coordinates system while PDBs are right-handed
107  //So we have to inverse the X coordinates
108  Vector3 values = new Vector3(-float.Parse(tok[1]) + Offset.x,
109  float.Parse(tok[2]) + Offset.y,
110  float.Parse(tok[3]) + Offset.z
111  );
112  particlelist.Add(values);
113  }
114  line = sr.ReadLine();
115  }
116  if(particlelist.Count > 0)
117  linelist.Add(particlelist);
118  else
119  Debug.Log("Reading Fieldlines apf ERROR");
120  }
121  else
122  {
123  // We have a JSON file
124  object value=(object)MiniJSON.JsonDecode(fieldlines_file_content);
125 
126  Debug.Log(((Hashtable)value)["lines"]);
127  ArrayList FieldLinesarray=(ArrayList)(((Hashtable)value)["lines"]);
128 
129  // Hashtable FieldLines = (Hashtable)value;
130 
131  Debug.Log(FieldLinesarray.Count);
132 
133  for (int i=0; i<FieldLinesarray.Count; i++)
134  {
135  List<Vector3> particlelist=new List<Vector3>();
136  for(int j=0;j<((ArrayList)FieldLinesarray[i]).Count;j+=3)
137  {
138  // Debug.Log("FieldLine: " +((ArrayList)FieldLinesarray[i])[j]+", "+((ArrayList)FieldLinesarray[i])[j+1]+", "+((ArrayList)FieldLinesarray[i])[j+2]);
139 
140  //Unity has a left-handed coordinates system while PDBs are right-handed
141  //So we have to inverse the X coordinates
142  double x=-(double)(((ArrayList)FieldLinesarray[i])[j]);
143  double y=(double)(((ArrayList)FieldLinesarray[i])[j+1]);
144  double z=(double)(((ArrayList)FieldLinesarray[i])[j+2]);
145  particlelist.Add(new Vector3(float.Parse(System.Convert.ToString(x))+Offset.x,float.Parse(System.Convert.ToString(y))+Offset.y,float.Parse(System.Convert.ToString(z))+Offset.z));
146 
147  // Debug.Log("FieldLine: " +((Vector3)particlelist[particlelist.Count-1]).z);
148 
149  // Debug.Log("FieldLine: " +((ArrayList)FieldLinesarray[i])[j]);
150  }
151  linelist.Add(particlelist);
152  }
153  }
154 
155  m_fieldlines = linelist;
156  return GetFieldLines();
157 
158  }
159 }
160 }
List< List< Vector3 > > ReadFile(string fieldlines_file_content, Vector3 Offset)
Definition: ReadJson.cs:84
List< List< Vector3 > > m_fieldlines
Definition: ReadJson.cs:76
This class encodes and decodes JSON strings.
Definition: MiniJSON.cs:84
static object JsonDecode(string json)
Parses the string json into a value
Definition: MiniJSON.cs:114
List< List< Vector3 > > GetFieldLines()
Definition: ReadJson.cs:78