UnityMol  0.9.6-875
UnityMol viewer / In developement
ReadDX.cs
Go to the documentation of this file.
1 
66  using UnityEngine;
67  using System.Collections;
68  using System.IO;
69  using System.Net;
70  using System;
71  using Molecule.Model;
72  using Molecule.Control;
73  using System.Xml;
74  using System.Text;
75  using System.Text.RegularExpressions;
76  using UI;
77 public class ReadDX {
78 //
79 // public int NX;
80 // public int NY;
81 // public int NZ;
82 // public int NBline;
83 //
84 // public float[,,] Grille;
85 //
86  // Use this for initialization
87 
88  public float[,,] _grid;
89 // public float[] _gridplate;
90  private Vector3[,,] gradient;
91  private int[] _dim;
92  private Vector3 _delta;
93  private Vector3 _origin;
94  public int X;
95  public int Y;
96  public int Z;
97 
98  private bool loaded = false;
99  public bool Loaded {
100  get{ return loaded; }
101  }
102 //
103  public Vector3 GetDelta() {
104  Debug.Log (_delta);
105  return _delta;
106  }
107 
108  public Vector3 GetOrigin() {
109  Debug.Log(" ori: "+ _origin);
110  return _origin;
111  }
112 
113 
114  public float getVal(int x, int y, int z) {
115  return _grid[x,y,z];
116  }
117 
118  public Vector3 getGradient(int x, int y, int z) {
119  return gradient[x,y,z];
120  }
121 //
122  public void ReadFile(string file_name) {
123  ReadFile(file_name, Vector3.zero);
124  }
125 
126  public void ReadFile(string file_name, Vector3 offset){
127 
128  // lecture en-tĂȘte du fichier .dx
129 
130 // Debug.Log("temps : "+temps);
131 
132  StreamReader sr;
133 
134  #if UNITY_WEBPLAYER
135  HttpWebRequest request =(HttpWebRequest) WebRequest.Create(file_name);
136  // If required by the server, set the credentials.
137  request.Credentials = CredentialCache.DefaultCredentials;
138  // Get the response.
139  HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
140  // Get the stream containing content returned by the server.
141  Stream dataStream = response.GetResponseStream ();
142  // Open the stream using a StreamReader for easy access.
143  sr = new StreamReader (dataStream);
144  #else
145  try {
146  FileInfo file = new FileInfo(file_name);
147  sr = file.OpenText();
148 
149  } catch (Exception e) {
150  // Something went wrong, so lets get information about it.
151  Debug.Log(e.ToString());
152  return;
153  }
154 
155  #endif
156 
157  ReadFile(sr, offset);
158 // String all = sr.ReadToEnd(); // Grille plate
159 // string[] allline = all.Split('\n');
160  }
161 
162 
163  public void ReadFile(TextReader sr, Vector3 offset) {
164  DateTime temps = DateTime.Now;
165  string line = "#"; // decalage jusqu'a la taille de la grille
166  while (line[0] == '#')line = sr.ReadLine(); // lecture de la taille de la grille
167  string[] size = line.Split(' ');
168 // string[] size = allline[4].Split(' '); // grille plate (GP)
169 
170  X = int.Parse(size[5]);
171  Y = int.Parse(size[6]);
172  Z = int.Parse(size[7]);
173 // int nScalar;
174 // nScalar = X*Y*Z;
175  DateTime temps1 = DateTime.Now;
176  Debug.Log(" taille de la grille : "+ X +" "+ Y +" "+ Z);
177 
178  line = sr.ReadLine(); // lecture de l'origine
179  string[] origs = line.Split(' ');
180 // string[] origs = allline[5].Split(' '); //GP
181  float oX = float.Parse(origs[1]);
182  float oY = float.Parse(origs[2]);
183  float oZ = float.Parse(origs[3]);
184 
185  DateTime temps2 = DateTime.Now;
186 
187  // lecture du delta
188  float dX,dY,dZ;
189  line = sr.ReadLine();
190  string[] delt = line.Split(' ');
191 // string[] delt = allline[6].Split(' '); // GP
192 
193  dX = float.Parse(delt[1]);
194  line = sr.ReadLine();
195  delt= line.Split(' ');
196 // delt = allline[7].Split(' '); // GP
197 
198  dY = float.Parse(delt[2]);
199  line = sr.ReadLine();
200  delt= line.Split(' ');
201 // delt = allline[8].Split(' '); // GP
202 
203  dZ = float.Parse(delt[3]);
204 
205  DateTime temps3 = DateTime.Now;
206  //lecture des commentaire
207  line = sr.ReadLine();
208  line = sr.ReadLine();
209 
210  DateTime temps4 = DateTime.Now;
211  // test du nScalaile a faire pour plus de securitĂ©
212 // int test_nScalar;
213 // test_nScalar = 0;
214 
215 // line = sr.ReadLine();
216 
217 
218  // declaration de la grille
219 
220  _dim = new int[3];
221  _dim[0] = X; _dim[1] = Y; _dim[2] = Z;
222 
223  _grid = new float[X,Y,Z];
224 // _gridplate = new float[X*Y*Z]; GP
225 // for(int i=0; i<X; i++)
226 // {
227 // for(int j=0; j<Y; j++)
228 // {
229 // for(int k=0; k<Z; k++)
230 // {
231 // _grid[i,j,k] = 0.0f;
232 // }
233 // }
234 // }
235  _delta = new Vector3();
236  _delta.x = dX;
237  _delta.y = dY;
238  _delta.z = dZ;
239  Debug.Log("ReadDX :: Delta DX - " + _delta);
240 
241  _origin = new Vector3();
242  _origin.x = oX - offset.x;
243  _origin.y = oY + offset.y;
244  _origin.z = oZ + offset.z;
245  Debug.Log("ReadDX :: Origin DX - " + _origin);
246  Debug.Log("ReadDX :: Offset DX - " + offset);
247 
248 
249 // line = sr.ReadLine();
250  string[] vals= line.Split(' ');
251  Debug.Log (line);
252  float val ;
253  int l=10;
254 // int nbline = 11; // GP
255 // string[] vals = allline[nbline].Split(' ');
256  int test_compteur=0;
257  for(int i=0; i<X; i++){
258  for(int j=0; j<Y; j++){
259  for(int k=0; k<Z; k++){
260 
261  if (l<3){
262  if (test_compteur<(X*Y*Z)-1){
263  val = float.Parse(vals[l]);
264  _grid[i,j,k]=val;
265  // if (i==50 && j==50)
266  // if (val < 1.75)
267  // Debug.Log(" ijk" +i +" "+j+" "+k+" valeur dans la grille : "+ _grid[i,j,k]+" vals: "+ vals[l] + " val: " + val);
268  //
269  l++;
270  }
271  }else{
272  line = sr.ReadLine();
273  test_compteur+=3;
274  vals = line.Split(' ');
275  val = float.Parse(vals[0]);
276  _grid[i,j,k]=val;
277  l=1;
278  }
279  }
280  }
281  }
282  // Debug.Log("ReadDX :: test compteur - "+test_compteur);
283 
284  // lecture sur grille plate avec _gridplate
285 // for (int i = 0 ;i < X*Y*Z/3;i++){
286 // vals = allline[nbline++].Split(' ');
287 // _gridplate[i*3] = float.Parse(vals[0]);
288 // _gridplate[i*3+1]= float.Parse(vals[1]);
289 // _gridplate[i*3+2] = float.Parse(vals[2]);
290 // }
291 //
292 
293  DateTime temps5 = DateTime.Now;
294  Debug.Log ("ReadDX :: taille - " +(temps1-temps));
295  Debug.Log("ReadDX :: lecture origine - " + (temps2 -temps1));
296  Debug.Log("ReadDX :: lecture de delta - " + (temps3 -temps2));
297  Debug.Log("ReadDX :: ligne vide - "+(temps4 -temps3));
298  Debug.Log("ReadDX :: grille lu - " + (temps5-temps4));
299  loaded = true;
300  }
301 
302 
303  public void calGradient(){
304  float scale = 1f;
305  float valprec;
306  float valnext;
307  float val;
308 
309 
310  gradient = new Vector3[X,Y,Z];
311 
312  for(int i=0; i<X; i++){
313  for(int j=0; j<Y; j++){
314  for(int k=0; k<Z; k++){
315  if (i==0 || j==0 || k==0 || i ==(X-1) || j ==(Y-1) || k == (Z-1)){
316  gradient[i,j,k]=new Vector3(0f,0f,0f);
317  }else{
318  val = getVal(i,j,k);
319  valprec=getVal(i-1,j,k);
320  valnext=getVal(i+1,j,k);
321  //grad.setX(((val - valprec) + (valsucc - val))/(_deltax*ForceField::ANGSTROM2METER*2.0));
322  gradient[i,j,k].x = ((val - valprec) + (valnext - val))/(_delta[0]*1.0f);
323 
324 
325  val=getVal(i,j,k);
326  valprec=getVal(i,j-1,k);
327  valnext=getVal(i,j+1,k);
328  //grad.setY(((val - valprec) + (valsucc - val))/(_deltay*ForceField::ANGSTROM2METER*2.0));
329  gradient[i,j,k].y = ((val - valprec) + (valnext - val))/(_delta[1]*1.0f);
330 
331  val=getVal(i,j,k);
332  valprec=getVal(i,j,k-1);
333  valnext=getVal(i,j,k+1);
334  //grad.setZ(((val - valprec) + (valsucc - val))/(_deltaz*ForceField::ANGSTROM2METER*2.0));
335  gradient[i,j,k].z = ((val - valprec) + (valnext - val))/(_delta[2]*1.0f);
336 
337  gradient[i,j,k] = -(gradient[i,j,k]*scale);
338 // if (i==1 && j==1){
339 // Debug.Log(" ijk" +i +" "+j+" "+k+" valeur dans la grille : "+ gradient[i,j,k]);
340 // }
341  }
342  }
343  }
344  }
345 
346  }
347 
348  private void RightHandedToLeftHanded(Mesh m) {
349  Vector3[] vertices = m.vertices;
350  for(int i=0; i < vertices.Length; i++)
351  vertices[i].x = -vertices[i].x;
352 
353  int[] triangles = m.triangles;
354 
355  for(int tri=0; tri < triangles.Length; tri=tri+3) {
356  int tmp = triangles[tri];
357  triangles[tri] = triangles[tri+2];
358  triangles[tri+2] = tmp;
359  }
360 
361  Color[] colors = m.colors;
362  m.Clear();
363  m.vertices = vertices;
364  m.triangles = triangles;
365  m.colors = colors;
366  m.RecalculateNormals();
367  }
368 
369  public void isoSurface(float threshold, Color color, int isPos, bool transparency=false){
370 
371  Vector4[] points;
372  points = new Vector4[ (X) * (Y) * (Z)];
373  Color[] colors = new Color[(X) * (Y) * (Z)];
374 
375  for (int j = 0; j < Y; j++) {
376  for (int i = 0; i < Z; i++) {
377  for (int k = 0; k < X; k++) {
378 // if (_grid[k,j,i] > 0) Debug.Log ("superieur a 0");
379 // if (_grid[k,j,i] > -0.5 ) Debug.Log ("superieur a -0,5:" + _grid[k,j,i]);
380 // if (i==1 || j==1 || k==1 || i ==Z-2 || j ==Y-2 || k == X-2)
381 // points[j*(Z)*(X) + i*(X) + k] = new Vector4 (i, j, k,-1000f);
382 // else
383  points[j*(Z)*(X) + i*(X) + k] = new Vector4 (k, j, i , _grid[k,j,i]);
384  colors[j*(Z)*(X) + i*(X) + k] = color;
385  }
386  }
387  }
388 
389 
390 
391 
392 // NZ = 4;
393 // NY = 6;
394 // NX = 5;
395 //
396 // float[,,] Gdata = new float[4,6,5] {
397 // {{ 0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}},
398 // {{ 0,0,0,0,0},{0,0,0,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,0,0,0},{0,0,0,0,0}},
399 // {{ 0,0,0,0,0},{0,0,0,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,0,0,0},{0,0,0,0,0}},
400 // {{ 0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}},
401 // } ;
402 //
403 // points = new Vector4[(int)(NZ+1)*(int)(NY+1)*(int)(NX+1)];
404 
405 // for (int i = 0; i < NZ; i++) {
406 // for (int j = 0; j < NY; j++) {
407 // for (int k = 0; k < NX; k++) {
408 // points[(i*(int)NX*(int)NY)+(j*(int)NY)+k] = new Vector4 (i,j,k,Gdata[i,j,k]);
409 // }
410 // }
411 // }
412 
413 
414 
415 
416 
417  Debug.Log("Entering :: before marching cubes instance");
418  //MarchingCubesRec MCInstance;
419  //MCInstance = new MarchingCubesRec();
420  // PDBtoDEN.DestroySurface();
421 
422 
423  /*
424 
425  if (threshold >= 0){
426  MCInstance.MCRecMain(X, Y, Z, threshold, points, 0f,false, _delta, _origin,colors,tag);
427  }else {
428  MCInstance.MCRecMain(X, Y, Z, threshold, points, 0f,true , _delta, _origin,colors,tag);
429  // GameObject iso_neg = GameObject.FindGameObjectWithTag("Elect_iso_negative");
430  // Mesh mesh_neg = iso_neg.GetComponent<MeshFilter>().mesh;
431  // //Unity has a left-handed coordinates system while Molecular obj are right-handed
432  // //So we have to negate the x axis and change the winding order
433  // RightHandedToLeftHanded(mesh_neg);
434  }
435 
436 
437  */
438 
439 
440  // Works for a single mesh
441  //GenerateMesh.RegularGM(_grid, threshold, _delta, _origin, colors, tag);
442 
443 
444  GenerateMesh.CreateSurfaceObjects(_grid, threshold, _delta, _origin, colors, isPos, true);
445 
446 
447  points = null;
448  GC.GetTotalMemory(true);
449  GC.Collect();
450  Debug.Log("Exiting :: finished marching cubes");
451  GameObject[] iso_pos = null;
452  if(isPos == -1)
454  else if (isPos == 1)
456  else
458 
459  Transform isoparent = GameObject.Find("ElectIsoManager").transform;
460  foreach(GameObject iso in iso_pos)
461  {
462  iso.transform.parent = isoparent;
463  Mesh mesh_pos = iso.GetComponent<MeshFilter>().mesh;
464  // //Unity has a left-handed coordinates system while Molecular obj are right-handed
465  // //So we have to negate the x axis and change the winding order
466  RightHandedToLeftHanded(mesh_pos);
467  if(transparency) {
468  // TransparentZ is a custom shader that uses transparent objects, but with per-triangle transparency sorting,
469  // versus Unity's default per-object transparency sorting. This is necessary because of our intersecting,
470  // non-convex transparent objects.
471  iso.GetComponent<Renderer>().material.shader = Shader.Find("Transparent/Zsorted");
472  color.a = 0.5f;
473  }
474  else {
475  iso.GetComponent<Renderer>().material.shader = Shader.Find("Diffuse");
476  //iso.renderer.material.shader = Shader.Find("Mat Cap Cut");
477  //iso.renderer.material.SetTexture("_MatCap",(Texture)Resources.Load("lit_spheres/divers/daphz1"));
478  }
479  iso.GetComponent<Renderer>().material.SetColor("_Color", color);
480  }
481 // MCInstance.MCRecMain(NX, NY, NZ, 0.5f, points, 0f);
482 
483 
484 // Vector4[] points;
485 // points = new Vector4[ (X+1) * (X+1) * (Z+1)];
486 //
487 // for (int i = 0; i < Z; i++) {
488 // for (int j = 0; j < X; j++) {
489 // for (int k = 0; k < X; k++) {
490 //
491 // points[i*(X)*(X) + j*(X) + k] = new Vector4 (i, j, k, _grid[k][j][i]);
493 // }
494 // }
495 // }
496 //
497 // MarchingCubesRec MCInstance;
498 // MCInstance = new MarchingCubesRec();
499 // MCInstance.MCRecMain(0, Z, X, X, 2.0f, points, 0);
500 //
501 //
502 
503 // Vector4[] points;
504 // points = new Vector4[ 129 * 129 * 129];
505 //
506 // for (int i = 0; i < 128; i++) {
507 // for (int j = 0; j < 128; j++) {
508 // for (int k = 0; k < 128; k++) {
509 //
510 // points[i*(128)*(128) + j*(128) + k] = new Vector4 (i, j, k, _grid[k][j][i]);
512 // }
513 // }
514 // }
515 //
516 // MarchingCubesRec MCInstance;
517 // MCInstance = new MarchingCubesRec();
518 // MCInstance.MCRecMain(0, 128, 128, 128, -10.0f, points, 0);
519 
520  }
521 
522 
523 
524 }
static void CreateSurfaceObjects(List< Mesh > meshes, int isPos)
void ReadFile(string file_name)
Definition: ReadDX.cs:122
float[,,] _grid
Definition: ReadDX.cs:88
void calGradient()
Definition: ReadDX.cs:303
int[] _dim
Definition: ReadDX.cs:91
Vector3 GetDelta()
Definition: ReadDX.cs:103
Definition: ReadDX.cs:77
Vector3 _delta
Definition: ReadDX.cs:92
int Z
Definition: ReadDX.cs:96
Vector3 _origin
Definition: ReadDX.cs:93
GameObject[] getSurfaces()
Vector3 getGradient(int x, int y, int z)
Definition: ReadDX.cs:118
GameObject[] getSurfacesPos()
GameObject[] getSurfacesNeg()
float getVal(int x, int y, int z)
Definition: ReadDX.cs:114
int X
Definition: ReadDX.cs:94
bool loaded
Definition: ReadDX.cs:98
void ReadFile(TextReader sr, Vector3 offset)
Definition: ReadDX.cs:163
Vector3 GetOrigin()
Definition: ReadDX.cs:108
void isoSurface(float threshold, Color color, int isPos, bool transparency=false)
Definition: ReadDX.cs:369
int Y
Definition: ReadDX.cs:95
void RightHandedToLeftHanded(Mesh m)
Definition: ReadDX.cs:348
bool Loaded
Definition: ReadDX.cs:99
Vector3[,,] gradient
Definition: ReadDX.cs:90
static SurfaceManager getSurfaceManager()
Definition: UnityMolMain.cs:24
Definition: GUIDisplay.cs:66
void ReadFile(string file_name, Vector3 offset)
Definition: ReadDX.cs:126