UnityMol  0.9.6-875
UnityMol viewer / In developement
RingBlending.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using Molecule.Model;
6 using System;
7 using System.Runtime.InteropServices;
8 
9 
15 public class RingBlending
16 {
17 
18  public ArrayList listOfPolygon = new ArrayList();
19  public int nbrMono;
20  public bool showRingBlending;
21 
22  public List<float[]> coordinates = new List<float[]>();
23  public float[] points = new float[3];
24 
25  public List<Mesh> meshList = new List<Mesh>();
26 
27  public static int index = 0;
28 
29  public List<GraphVertex> molAsGraph = new List<GraphVertex>(); //convert the molecule as a graph to after find cycle.
30  public List<List<int>> cycles = new List<List<int>>(); // each element countains a list of atoms ID who compose a cycle.
31  public List<string> residueslist = new List<string>(); // list of resname to get the color of each cycle
32 
33  public List<float[]> atomsLocationlist = new List<float[]>();
34 
35 
36  public RingBlending(){
37  atomsLocationlist = MoleculeModel.atomsLocationlist;
38 
39  //We initialize all vertex (an atom is a vertex here)
40  for (int i=0; i<atomsLocationlist.Count; i++){
41  GraphVertex tempVertex = new GraphVertex();
42  tempVertex.coordinate=(new Vector3(atomsLocationlist[i][0] ,
43  atomsLocationlist[i][1],
44  atomsLocationlist[i][2]));
46  tempVertex.type=MoleculeModel.atomsNamelist[i][0];
47  tempVertex.id=i;
48  molAsGraph.Add(tempVertex);
49  }
50 
51  /* We connect all dots
52  * But we don't need to calculate over 30 atoms after the atom 'i'
53  * and an hydrogen can't be a part of a cycle because an hydrogen can only have 1 bond.
54  */
55  for (int i=0; i < MoleculeModel.bondEPList.Count; i++){
56  //Debug.Log("0= "+MoleculeModel.bondList[i][0]+" 1="+MoleculeModel.bondList[i][1]);
57  if(molAsGraph[MoleculeModel.bondEPList[i][1]].type!='H'){
58  if (MoleculeModel.bondEPList[i][1] < i+30){
59  if ((MoleculeModel.bondEPList[i][0]==123) || (MoleculeModel.bondEPList[i][0]==129))
60  Debug.Log ("connect : "+MoleculeModel.bondEPList[i][0]+" >>> "+MoleculeModel.bondEPList[i][1]);
61  molAsGraph[MoleculeModel.bondEPList[i][0]].neighbor.Add(molAsGraph[MoleculeModel.bondEPList[i][1]]);
62  molAsGraph[MoleculeModel.bondEPList[i][1]].neighbor.Add(molAsGraph[MoleculeModel.bondEPList[i][0]]);
63  }
64  }
65  }
66 
67 
68  /* We search cycle
69  * It's a "search in graph" algorithm. we search inside each object (atoms here).
70  */
71  for (int i=0; i<molAsGraph.Count; i++){
72  List<int> indexCycle = new List<int>();
73  molAsGraph[i].SearchCycle(indexCycle, i);
74 
75 
76  //We reset all flag.
77  for (int j=0; j<molAsGraph.Count; j++)
78  molAsGraph[j].flag=false;
79 
80  if (indexCycle.Count>0){
81  //We add vertex in the list wo will countain all vertex for all cycle
82  if(!cycles.Any (x => x.OrderBy(y => y).SequenceEqual(indexCycle.OrderBy(z=>z)))){
83  cycles.Add(indexCycle);
84  residueslist.Add(molAsGraph[i].resname);
85  }
86  }
87  }
88 
89 
90  }
91 
92 
99  public List<GameObject> CreateRingBlending(){
100 
101  /* we finaly draw all mesh, now that we detect all cycle in the molecule*/
102  return createAllMesh ();
103  } /* End of CreateRingBlending*/
104 
105 
110  public List<GameObject> createAllMesh(){
111  float x, y, z;
112  List<GameObject> result = new List<GameObject>();
113 
114  for (int i=0; i< cycles.Count; i++) {
115  for (int j=0; j<cycles[i].Count; j++) {
116  x = atomsLocationlist [cycles [i] [j]] [0];//+ MoleculeModel.Offset.x;
117  y = atomsLocationlist [cycles [i] [j]] [1];//+ MoleculeModel.Offset.y;
118  z = atomsLocationlist [cycles [i] [j]] [2];//+ MoleculeModel.Offset.z;
119 
120  coordinates.Add (new float[3]{x,y,z});
121  }
122  string res = residueslist[i];
123  Mesh[] twomesh =createAMeshPC (coordinates);
124  //Mesh mesh = createAMeshPC (coordinates);
125  //Mesh backMesh=returnBackMesh(mesh);
126  Mesh mesh = twomesh[0];
127  Mesh backMesh = twomesh[1];
128  result.Add(createPCObj (mesh, res));
129  result.Add(createPCObj (backMesh, res));
130 
131  coordinates.Clear ();
132  }
133  return result;
134 
135  }/* end for CeateAllMesh*/
136 
137 
145  public static Mesh[] createAMeshPC(List<float[]> cxLocation)
146  {
147  List<Color32> ColorList = new List<Color32>();
148  List<Vector3> atomPositionList = new List<Vector3>(); //list of atoms position (x,y,z)
149  List<Vector3> vertices = new List<Vector3>();
150  List<int> triangles = new List<int>();
151  List<int> trianglesback = new List<int>();
152 
153  Mesh mymesh = new Mesh();
154  Mesh backmesh = new Mesh();
155  float xTot=0, yTot=0, zTot=0;
156 
157  Vector3 barycenter = new Vector3();
158  //we save all coordinate and prepare barycenter calculation
159  for (int i=0; i<cxLocation.Count; i++){
160 
161  atomPositionList.Add(new Vector3(cxLocation[i][0],cxLocation[i][1],cxLocation[i][2]));
162  xTot=xTot+cxLocation[i][0];
163  yTot=yTot+cxLocation[i][1];
164  zTot=zTot+cxLocation[i][2];
165  }
166  //calculation of barycenter
167  barycenter.x=xTot/cxLocation.Count;
168  barycenter.y=yTot/cxLocation.Count;
169  barycenter.z=zTot/cxLocation.Count;
170 
171 
172 
173  //we create all vertex
174  for (int i=0; i < atomPositionList.Count; i++){
175  vertices.Add(atomPositionList[i]);
176 
177  if(i==atomPositionList.Count-1){ //If it's the last triangles
178  vertices.Add(barycenter); // we add the barycenter
179  triangles.Add(i); //P0
180  triangles.Add(0); //P1
181  triangles.Add(atomPositionList.Count); //Barycenter
182  trianglesback.Add (atomPositionList.Count);
183  trianglesback.Add(0);
184  trianglesback.Add (i);
185  }else{
186  triangles.Add(i); //P0
187  triangles.Add(i+1); //P1
188  triangles.Add(atomPositionList.Count); //Barycenter
189  trianglesback.Add (atomPositionList.Count);
190  trianglesback.Add(i+1);
191  trianglesback.Add(i);
192  }
193  }
194 
195  index=index+atomPositionList.Count;
196 
197  mymesh.vertices=vertices.ToArray();
198  mymesh.triangles=triangles.ToArray();
199  mymesh.RecalculateNormals();
200  mymesh.colors32=ColorList.ToArray();
201  backmesh.vertices=vertices.ToArray();
202  backmesh.triangles=trianglesback.ToArray();
203  backmesh.RecalculateNormals();
204  backmesh.colors32=ColorList.ToArray();
205 
206  Mesh[] TWOMESH= new Mesh[2]{mymesh, backmesh};
207  return TWOMESH;
208  } /* end of createAMeshPC */
209 
210 
211 
219  public GameObject createPCObj(Mesh mesh, string res){
220  GameObject papobj = new GameObject("RingBlending");
221  papobj.tag = "RingBlending";
222  papobj.AddComponent<MeshFilter>();
223  papobj.AddComponent<MeshRenderer>();
224  papobj.GetComponent<MeshFilter>().mesh = mesh;
225  papobj.GetComponent<Renderer>().material = new Material(Shader.Find("Transparent/Diffuse"));
226  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(res))
227  papobj.GetComponent<Renderer>().material.color=UI.GUIDisplay.colorByResiduesDict[res];
228  else
229  papobj.GetComponent<Renderer>().material.color=new Color(0.7f,0.7f,0.7f,0.5f);
230 
231  return papobj;
232  } //end of createPCObj
233 
234 
235  public GameObject CreateOxygenSphere(int pos){
236  GameObject oxysphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
237  oxysphere.tag = "OxySphere";
238  Vector3 point = new Vector3(atomsLocationlist [pos] [0],atomsLocationlist [pos] [1],atomsLocationlist [pos] [2]);
239  // oxysphere.AddComponent<Animation>();
240  oxysphere.transform.position = point;
241  oxysphere.GetComponent<Renderer>().material.color = Color.red;
242  oxysphere.transform.localScale = new Vector3(1, 1, 1);
243  return oxysphere;
244 
245  }
246 
247  public List<GameObject> ShowOxySphere(){
248  List<GameObject> result = new List<GameObject>();
249  for (int i=0; i<this.cycles.Count; i++) {
250  for (int j=0; j<this.cycles[i].Count; j++) {
251  if (MoleculeModel.atomsNamelist[cycles[i][j]][0] == 'O'){
252  result.Add(CreateOxygenSphere (cycles [i] [j]));
253  }
254  }
255  }
256  return result;
257  }
258 
259 
260 
261 
262 }
string resname
Definition: GraphVertex.cs:17
List< GameObject > createAllMesh()
this fonction will call the other functions to create all mesh and also create gameobject.
GameObject CreateOxygenSphere(int pos)
float[] points
Definition: RingBlending.cs:23
Vector3 coordinate
Definition: GraphVertex.cs:18
ArrayList listOfPolygon
Definition: RingBlending.cs:18
List< float[]> atomsLocationlist
Definition: RingBlending.cs:33
List< Mesh > meshList
Definition: RingBlending.cs:25
this class is made to fill cycle inside a molecule with a semi-transparent polygon to have a effect o...
Definition: RingBlending.cs:15
List< string > residueslist
Definition: RingBlending.cs:31
static List< string > atomsNamelist
The name of each atom.
List< GameObject > CreateRingBlending()
This function will convert the molecule to a "graph" (an object "GraphVertex" is made from each atoms...
Definition: RingBlending.cs:99
List< GameObject > ShowOxySphere()
GameObject createPCObj(Mesh mesh, string res)
Create an object "RingBlending" (polygon which fill a cycle).
static int index
Definition: RingBlending.cs:27
List< GraphVertex > molAsGraph
Definition: RingBlending.cs:29
!WiP manage GUI, and provide static strings for the GUI.
Definition: GUIDisplay.cs:94
List< float[]> coordinates
Definition: RingBlending.cs:22
static List< int[]> bondEPList
The bonds between atoms.
List< List< int > > cycles
Definition: RingBlending.cs:30
This class is made to convert an atom (postion X,Y,Z) to a object with a list of neighbor.
Definition: GraphVertex.cs:10
static List< string > atomsResnamelist
The name of the residue to which each atom belongs.
static Dictionary< string, Color > colorByResiduesDict
Definition: GUIDisplay.cs:179
bool showRingBlending
Definition: RingBlending.cs:20
static Mesh[] createAMeshPC(List< float[]> cxLocation)
This function will create two mesh.
Definition: GUIDisplay.cs:66
static List< float[]> atomsLocationlist
The coordinates of each atom.