UnityMol  0.9.6-875
UnityMol viewer / In developement
SugarRibbons.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 
7 
8  public class SugarRibbons
9  {
10 
11 
12  public List<Mesh> CycleMesh = new List<Mesh>();
13  public List<Mesh> CycleBIGMesh = new List<Mesh>();
14 
15 
16  public List<Mesh> meshesRingUp =new List<Mesh>();
17  public List<Mesh> meshesRingDown = new List<Mesh>();
18  public List<Mesh> meshesSideRing = new List<Mesh> ();
19 
20  public List<Mesh> meshesBIGRingUp =new List<Mesh>();
21  public List<Mesh> meshesBIGRingDown = new List<Mesh>();
22  public List<Mesh> meshesBIGSideRing = new List<Mesh> ();
23 
24 
25  public List<Mesh> meshesBondUp = new List<Mesh>();
26  public List<Mesh> meshesBondDown = new List<Mesh>();
27  public List<Mesh> meshesBondSide = new List<Mesh>();
28 
29 
30  public List<GraphVertex> molAsGraph = new List<GraphVertex>(); //convert the molecule as a graph to after find cycle.
31  public List<List<int>> cycles = new List<List<int>>(); // each element countains a list of atoms ID who compose a cycle.
32  public List<string> residueslist = new List<string>(); // list of resname to get the color of each cycle
33  public List<int> residuesNumberlist = new List<int>(); // list of resname to get the color of each cycle
34 
35  public List<Vector3> barylist = new List<Vector3>(); // list of all barycenters (for each cycle)
36 
37 
38  private Vector3 LinearComb(float scalar0, Vector3 vector0, float scalar1, Vector3 vector1) {
39  return scalar0*vector0 + scalar1*vector1;
40  }
41 
42  public List<Vector3> vertices = new List<Vector3>();
43  public List<Color32> colors = new List<Color32>();
44  public List<Vector3> normals = new List<Vector3>();
45  public List<int> triangles = new List<int>();
46 
47  public float THICKNESS;
48  public float THICKNESS_LITTLE_MESH;
49  public float THICKNESS_BIG_MESH;
50  public float THICKNESS_BOND_6_C1_C4;
51  public float THICKNESS_BOND_6_OTHER;
52  public float THICKNESS_BOND_5;
55  public float OXYSPHERESIZE;
56  public bool SUGAR_ONLY;
57  public int COLOR_MODE_RING;
58  public int COLOR_MODE_BOND;
59  public int SUGAR=0;
60  public int CHAIN=1;
61  public int PICKER=2;
65 
66  public static float[] wideness = {1f, 2f, 1f};
67 
68  public int nbRes;
69  public List<string> sugarResname = new List<string>();
70 
71 
72  private Vector3 float3toVector3(float[] point){
73  Vector3 returnVector = new Vector3(point[0], point[1], point[2]);
74  return returnVector;
75  }
76 
77 
78  public List<float[]> atomsLocationlist = new List<float[]>();
79  public List<string> atomsResnamelist = new List<string>();
80  public List<string> atomsNamelist = new List<string>();
81  public List<string> resChainList = new List<string>();
82  public List<int[]> bondEPList = new List<int[]>();
83 
84  public Dictionary <int,List<int>> connectivityList = new Dictionary<int, List<int>>();
85  public Dictionary <int, Dictionary<int, int[]> > MeshIndexForAtom = new Dictionary<int, Dictionary<int, int[]>>();
86  public Dictionary <int, string> res_for_bond = new Dictionary<int, string>();
87 
88 
89  public SugarRibbons(bool SugarOnly){
90  if (SugarOnly){
91  atomsLocationlist = MoleculeModel.atomsSugarLocationlist;
92  atomsResnamelist = MoleculeModel.atomsSugarResnamelist;
93  atomsNamelist = MoleculeModel.atomsSugarNamelist;
94  bondEPList = MoleculeModel.bondEPSugarList;
95  resChainList = MoleculeModel.resSugarChainList;
96 
97 
98  }else{
99  atomsLocationlist = MoleculeModel.atomsLocationlist;
100  atomsResnamelist = MoleculeModel.atomsResnamelist;
101  atomsNamelist = MoleculeModel.atomsNamelist;
102  bondEPList = MoleculeModel.bondEPList;
103  resChainList = MoleculeModel.resChainList;
104  }
105  MeshIndexForAtom[6]=new Dictionary<int, int[]> () ;
106  MeshIndexForAtom[6][0] = new int[2]{1,2};
107  MeshIndexForAtom[6][1] = new int[2]{3,4};
108  MeshIndexForAtom[6][2] = new int[2]{5,6};
109  MeshIndexForAtom[6][3] = new int[2]{7,8};
110  MeshIndexForAtom[6][4] = new int[2]{9,10};
111  MeshIndexForAtom[6][5] = new int[2]{11,0};
112 
113  MeshIndexForAtom[5]=new Dictionary<int, int[]> () ;
114  MeshIndexForAtom[5][0]=new int[2]{0,1};
115  MeshIndexForAtom[5][1]=new int[2]{2,3};
116  MeshIndexForAtom[5][2]=new int[2]{4,5};
117  MeshIndexForAtom[5][3]=new int[2]{6,7};
118  MeshIndexForAtom[5][4]=new int[2]{8,9};
119 
120 
121 
122 
123  for (int i=0; i<atomsLocationlist.Count; i++){
124 
125  GraphVertex tempVertex = new GraphVertex();
126  tempVertex.coordinate=(new Vector3(atomsLocationlist[i][0],
127  atomsLocationlist[i][1],
128  atomsLocationlist[i][2]));
129 
130  tempVertex.resname=atomsResnamelist[i];
131  tempVertex.type=atomsNamelist[i][0];
132  tempVertex.id=i;
133  molAsGraph.Add(tempVertex);
134  }
135 
136  /* We connect all dots
137  * But we don't need to calculate over 30 atoms after the atom 'i'
138  * and an hydrogen can't be a part of a cycle because an hydrogen can only have 1 bond.
139  */
140  for (int i=0; i < bondEPList.Count; i++){
141  molAsGraph[bondEPList[i][0]].neighbor.Add(molAsGraph[bondEPList[i][1]]);
142  molAsGraph[bondEPList[i][1]].neighbor.Add(molAsGraph[bondEPList[i][0]]);
143  }
144 
145 
146  /* We search cycle
147  * It's a "search in graph" algorithm. we search inside each object (atoms here).
148  */
149  for (int i=0; i<molAsGraph.Count; i++){
150  List<int> indexCycle = new List<int>();
151  molAsGraph[i].SearchCycle(indexCycle, i);
152 
153 
154  //We reset all flag.
155  for (int j=0; j<molAsGraph.Count; j++)
156  molAsGraph[j].flag=false;
157 
158  if (indexCycle.Count>0){
159  //We add vertex in the list wo will countain all vertex for all cycle
160  if(!cycles.Any (x => x.OrderBy(y => y).SequenceEqual(indexCycle.OrderBy(z=>z)))){
161  cycles.Add(indexCycle);
162  residueslist.Add(molAsGraph[i].resname);
163  }
164  }
165  }
166 
167  for (int i=0; i<cycles.Count; i++){
168  for (int j=0; j<cycles[i].Count; j++){
169  molAsGraph[cycles[i][j]].idRing = i;
170  }
171  }
172 
173  //We reset all flag.
174 
175  for (int i=0; i<molAsGraph.Count; i++){
176  List<int> trashlish = new List<int>();
177  molAsGraph[i].SearchConnection(connectivityList,trashlish);
178 
179  for (int j=0; j<molAsGraph.Count; j++)
180  molAsGraph[j].flag=false;
181  }
182 
184 
185  }
186 
187 
188 
189  public List<List<GameObject>> createSugarRibs(float RibbonsThickness, bool sugarOnly, float thickness_Little, float thickness_BIG,
190  float thickness_bond_6_C1_C4, float thickness_6_other, float thickness_bond_5,
191  float lighter_color_factor_ring, float lighter_color_factor_bond, int color_mode_ring, int color_mode_bond,
192  ColorObject bondcolor, ColorObject ringcolor, float OxySphereSize, ColorObject OxySphereColor){
193  sugarResname.AddMany("ABE","ACE","ALT","API","ARA","DHA","FRU","FUC","GAL","GLC","GUL","IDO","DKN","KDO","MAN","NEG","RHA","RIB","SIA","TAG","TAL","XYL");
194  SUGAR_ONLY=sugarOnly;
195  THICKNESS_LITTLE_MESH=thickness_Little;
196  THICKNESS_BIG_MESH=thickness_BIG;
197  THICKNESS_BOND_6_C1_C4=thickness_bond_6_C1_C4;
198  THICKNESS_BOND_6_OTHER=thickness_6_other;
199  THICKNESS_BOND_5=thickness_bond_5;
200  LIGHTER_COLOR_FACTOR_RING = lighter_color_factor_ring;
201  LIGHTER_COLOR_FACTOR_BOND = lighter_color_factor_bond;
202  COLOR_MODE_RING = color_mode_ring;
203  COLOR_MODE_BOND = color_mode_bond;
204  BONDCOLOR = bondcolor;
205  RINGCOLOR = ringcolor;
206  OXYSPHERESIZE = OxySphereSize;
207  OXYSPHERECOLOR = OxySphereColor;
208  THICKNESS = RibbonsThickness;
209  //We initialize all vertex (an atom is a vertex here)
210 
211 
212  return createSugarRibbons();
213  /* we finaly draw all mesh, now that we detect all cycle in the molecule*/
214  } /* End of CreateTwister*/
215 
216 
217  public void changeColor(int which){
218  /* which=0 -> SUGAR RING
219  * which=1 -> BOND
220  * which=2 -> OXYGEN SPHERE
221  */
222 
223  switch (which){
224  case 0:
225  //for (int i=0; i<meshesRingUp.Count; i++){
226  //meshesRingUp[i].colors32 = Color.red;
227  //}
228  break;
229 
230  case 1:
231  break;
232 
233  case 2:
234  break;
235  }
236  }
237 
238 
239 
240  private void generateCycleMesh2(){
241 
242  string resname = "";
243  for (int i=0; i<cycles.Count; i++){
244  Mesh sugarMesh = new Mesh ();
245  List<int> trianglesSM = new List<int> ();
246  List<Vector3> verticesSM = new List<Vector3> ();
247  List<Color32> colorsSM = new List<Color32> ();
248  resname=residueslist[i];
249 
250 
251  for (int j=0; j<cycles[i].Count; j++){
252  verticesSM.Add(CycleAtomToVector3(i,j));
253 
254  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname))
255  colorsSM.Add(UI.GUIDisplay.colorByResiduesDict[resname]);
256  else
257  colorsSM.Add(new Color(0.7f,0.7f,0.7f,0.5f));
258  }
259 
260  int size = cycles[i].Count;
261  int nbiter = (size-2)/2;
262  int reste = (size-2)%2;
263 
264  for (int j=0; j<nbiter; j++){
265  trianglesSM.AddMany(j,size-1-j,(size-1)-(j+1));
266  trianglesSM.AddMany(j,(size-1)-(j+1),j+1);
267  if ((reste==1) && (j==nbiter-1)){
268  trianglesSM.AddMany(j+1,(size-1)-(j+1),j+2);
269  }
270  }
271 
272 
273 
274  sugarMesh.vertices = verticesSM.ToArray();
275  sugarMesh.triangles = trianglesSM.ToArray();
276  sugarMesh.colors32 = colorsSM.ToArray();
277 
278  CycleMesh.Add(sugarMesh);
279 
280  }
281  }
282 
283  public Vector3 calcMidPoint(Vector3 a, Vector3 b){
284  return ((a+b)/2);
285  }
286 
287  public List<Vector3> enlargeMesh(int i, float scaling){
288  List<Vector3> verticesEn = new List<Vector3>();
289  Vector3 vectDir;
290  Vector3 midpoint;
291 
292  if (cycles[i].Count == 6){
293  vectDir = CycleAtomToVector3(i,0) - CycleAtomToVector3(i,4);
294  vectDir.Normalize();
295  verticesEn.Add(CycleAtomToVector3(i,0) +vectDir * (scaling-0.1f));
296 
297  vectDir = CycleAtomToVector3(i,1) - CycleAtomToVector3(i,3);
298  vectDir.Normalize();
299  verticesEn.Add(CycleAtomToVector3(i,1) +vectDir * (scaling-0.1f));
300 
301  midpoint = calcMidPoint(CycleAtomToVector3(i,1), CycleAtomToVector3(i,3));
302  vectDir = CycleAtomToVector3(i,2) - midpoint;
303  vectDir.Normalize();
304  verticesEn.Add(CycleAtomToVector3(i,2) +vectDir * (scaling+0.3f));
305 
306  vectDir = CycleAtomToVector3(i,3) - CycleAtomToVector3(i,1);
307  vectDir.Normalize();
308  verticesEn.Add(CycleAtomToVector3(i,3) +vectDir * (scaling-0.1f));
309 
310  vectDir = CycleAtomToVector3(i,4) - CycleAtomToVector3(i,0);
311  vectDir.Normalize();
312  verticesEn.Add(CycleAtomToVector3(i,4) +vectDir * (scaling-0.1f));
313 
314  midpoint = calcMidPoint(CycleAtomToVector3(i,0), CycleAtomToVector3(i,4));
315  vectDir = CycleAtomToVector3(i,5) - midpoint;
316  vectDir.Normalize();
317  verticesEn.Add(CycleAtomToVector3(i,5) + vectDir * (scaling+0.3f));
318 
319 
320  }else if (cycles[i].Count == 5){
321  midpoint = calcMidPoint(CycleAtomToVector3(i,4), CycleAtomToVector3(i,1));
322  vectDir = CycleAtomToVector3(i,0) - barylist[i];
323  vectDir.Normalize();
324  verticesEn.Add (CycleAtomToVector3(i,0) + vectDir * (scaling));
325 
326  midpoint = calcMidPoint(CycleAtomToVector3(i,0), CycleAtomToVector3(i,2));
327  vectDir = CycleAtomToVector3(i,1) - barylist[i];
328  vectDir.Normalize();
329  verticesEn.Add (CycleAtomToVector3(i,1) + vectDir * (scaling));
330 
331  midpoint = calcMidPoint(CycleAtomToVector3(i,1), CycleAtomToVector3(i,3));
332  vectDir = CycleAtomToVector3(i,2) - barylist[i];
333  vectDir.Normalize();
334  verticesEn.Add (CycleAtomToVector3(i,2) + vectDir * (scaling));
335 
336  midpoint = calcMidPoint(CycleAtomToVector3(i,2), CycleAtomToVector3(i,4));
337  vectDir = CycleAtomToVector3(i,3) - barylist[i];
338  vectDir.Normalize();
339  verticesEn.Add (CycleAtomToVector3(i,3) + vectDir * (scaling));
340 
341  midpoint = calcMidPoint(CycleAtomToVector3(i,3), CycleAtomToVector3(i,0));
342  vectDir = CycleAtomToVector3(i,4) - barylist[i];
343  vectDir.Normalize();
344  verticesEn.Add (CycleAtomToVector3(i,4) + vectDir * (scaling));
345 
346 
347 
348  }else{
349  Debug.Log("WARNING ==> RING WITH "+cycles[i].Count+" :: REPRESENTATON NOT IMPLEMENTED");
350  }
351 
352  return verticesEn;
353  }
354 
355 
356  private List<Vector3> makeFlatVertex( List<Vector3> verticeEn){
357 
358  List<Vector3> newVertices = new List<Vector3>();
359 
360  if (verticeEn.Count == 6){
361  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[0], verticeEn[5], 1-THICKNESS_BOND_6_C1_C4));
362  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[0], verticeEn[5], THICKNESS_BOND_6_OTHER));
363  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[0], verticeEn[1], THICKNESS_BOND_6_OTHER));
364  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[1], verticeEn[0], THICKNESS_BOND_6_OTHER));
365  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[1], verticeEn[2], THICKNESS_BOND_6_OTHER));
366  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[1], verticeEn[2], 1-THICKNESS_BOND_6_C1_C4));
367  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[3], verticeEn[2], 1-THICKNESS_BOND_6_C1_C4));
368  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[3], verticeEn[2], THICKNESS_BOND_6_OTHER));
369  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[3], verticeEn[4], THICKNESS_BOND_6_OTHER));
370  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[4], verticeEn[3], THICKNESS_BOND_6_OTHER));
371  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[4], verticeEn[5], THICKNESS_BOND_6_OTHER));
372  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[4], verticeEn[5], 1-THICKNESS_BOND_6_C1_C4));
373 
374  }else{
375  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[0], verticeEn[4],THICKNESS_BOND_5));
376  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[0], verticeEn[1],THICKNESS_BOND_5));
377  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[1], verticeEn[0],THICKNESS_BOND_5));
378  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[1], verticeEn[2],THICKNESS_BOND_5));
379  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[2], verticeEn[1],THICKNESS_BOND_5));
380  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[2], verticeEn[3],THICKNESS_BOND_5));
381  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[3], verticeEn[2],THICKNESS_BOND_5));
382  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[3], verticeEn[4],THICKNESS_BOND_5));
383  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[4], verticeEn[3],THICKNESS_BOND_5));
384  newVertices.Add(calcullittleRibbonsControlPoint(verticeEn[4], verticeEn[0],THICKNESS_BOND_5));
385  }
386 
387  return newVertices;
388 
389 
390  }
391 
392 
393  private void generateBiggerCycleMesh(float scaling=0.60f){
394  string resname = "";
395  string chain = "";
396  for (int i=0; i<cycles.Count; i++){
397  Mesh sugarMesh = new Mesh ();
398  List<int> trianglesSM = new List<int> ();
399  List<Vector3> verticesSM = new List<Vector3> ();
400  List<Color32> colorsSM = new List<Color32> ();
401  resname=residueslist[i];
402  if (MoleculeModel.resChainList.Count>0)
403  chain=MoleculeModel.resChainList[i];
404 
405  verticesSM = enlargeMesh(i, scaling);
406  verticesSM = makeFlatVertex(verticesSM);
407  for (int j=0; j<verticesSM.Count; j++){
408  if (COLOR_MODE_RING == SUGAR){
409  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname))
410  colorsSM.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname], LIGHTER_COLOR_FACTOR_RING));
411  else
412  colorsSM.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_RING));
413  }else if (COLOR_MODE_RING == CHAIN){
414  if (UI.GUIDisplay.ChainColorDict.ContainsKey(chain))
415  colorsSM.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain], LIGHTER_COLOR_FACTOR_RING));
416  else
417  colorsSM.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_RING));
418  }else if (COLOR_MODE_RING == PICKER){
419  colorsSM.Add(lightColor(RINGCOLOR.color, LIGHTER_COLOR_FACTOR_RING));
420  }
421  }
422 
423 
424  int size = verticesSM.Count;
425  int nbiter = (size-2)/2;
426  int reste = (size-2)%2;
427 
428 
429  for (int j=0; j<nbiter; j++){
430  trianglesSM.AddMany(j,size-1-j,(size-1)-(j+1));
431  trianglesSM.AddMany(j,(size-1)-(j+1),j+1);
432  if ((reste==1) && (j==nbiter-1)){
433  trianglesSM.AddMany(j+1,(size-1)-(j+1),j+2);
434  }
435  }
436 
437 
438 
439  sugarMesh.vertices = verticesSM.ToArray();
440  sugarMesh.triangles = trianglesSM.ToArray();
441 
442  sugarMesh.colors32 = colorsSM.ToArray();
443 
444  CycleBIGMesh.Add(sugarMesh);
445 
446  }
447  }
448 
449 
450 
451 
452 
453 
454 
455 
456 
457  private void generateRibbonsMesh(Mesh centralMesh, int resnumber, float thicknessCenter=1f, bool center=true){
458  Mesh meshUP = new Mesh();
459  Mesh meshDOWN = new Mesh();
460  Mesh meshSide = new Mesh ();
461  List<Vector3> verticesSide = new List<Vector3>();
462  List<Color32> sideColors = new List<Color32> ();
463 
464 
465  /*RING MESH*/
466  Vector3 normalMean = calculMeanNormalMesh(centralMesh);
467  Vector3[] newVerticesUP = new Vector3[centralMesh.vertices.Length];
468  Vector3[] newVerticesDOWN = new Vector3[centralMesh.vertices.Length];
469  List<Color32> colors = new List<Color32>();
470  string resname = residueslist[resnumber];
471 
472 
473 
474  for (int j=0; j<centralMesh.vertices.Length; j++){
475  newVerticesUP[j].x = centralMesh.vertices[j].x + (normalMean.x*(THICKNESS*thicknessCenter));
476  newVerticesUP[j].y = centralMesh.vertices[j].y + (normalMean.y)*(THICKNESS*thicknessCenter);
477  newVerticesUP[j].z = centralMesh.vertices[j].z + (normalMean.z)*(THICKNESS*thicknessCenter);
478 
479  newVerticesDOWN[j].x = centralMesh.vertices[j].x - (normalMean.x*(THICKNESS*thicknessCenter));
480  newVerticesDOWN[j].y = centralMesh.vertices[j].y - (normalMean.y)*(THICKNESS*thicknessCenter);
481  newVerticesDOWN[j].z = centralMesh.vertices[j].z - (normalMean.z)*(THICKNESS*thicknessCenter);
482  }
483 
484  //COLORS SET
485  for (int j=0; j<centralMesh.vertices.Length; j++){
486  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname))
487  colors.Add(UI.GUIDisplay.colorByResiduesDict[resname]);
488  else
489  colors.Add(new Color(0.7f,0.7f,0.7f,0.5f));
490  }
491 
492  meshUP.vertices = newVerticesUP;
493  meshUP.triangles = centralMesh.triangles;
494  meshUP.RecalculateNormals();
495  meshUP.colors32 = centralMesh.colors32;
496 
497 
498 
499  meshDOWN.vertices = newVerticesDOWN;
500  meshDOWN.triangles = invertTrianglesMesh(centralMesh);
501  meshDOWN.RecalculateNormals();
502  meshDOWN.colors32 = centralMesh.colors32;
503 
504  /*SIDE MESH*/
505 
506 
507 
508  //SIDE VERTICES
509 
510  for (int i=0; i<meshUP.vertices.Length; i++){
511  verticesSide.AddMany(meshUP.vertices[i],meshDOWN.vertices[i]);
512  sideColors.AddMany(meshUP.colors32[i], meshDOWN.colors32[i]);
513  }
514 
515  meshSide.vertices = verticesSide.ToArray ();
516  meshSide.triangles = generateTriangleSide (meshSide);
517  meshSide.RecalculateNormals ();
518  meshSide.colors32 = sideColors.ToArray();
519 
520 
521  if (center){
522  meshesRingUp.Add (meshUP);
523  meshesRingDown.Add (meshDOWN);
524  meshesSideRing.Add (meshSide);
525  }else{
526  meshesBIGRingUp.Add (meshUP);
527  meshesBIGRingDown.Add (meshDOWN);
528  meshesBIGSideRing.Add (meshSide);
529  }
530  }
531 
532 
533  private void create_Bond(){
534  int key_index_for_bond_dict=-1;
535  foreach (int i in connectivityList.Keys){
536  string resname1 = residueslist[i];
537  string chain1 = MoleculeModel.resChainList[i];
538  for (int j=0; j<connectivityList[i].Count / 3; j++){
539  key_index_for_bond_dict++;
540  List<Vector3> verticesUP = new List<Vector3>();
541  List<Vector3> verticesDown = new List<Vector3>();
542  List<Vector3> VerticesSide = new List<Vector3>();
543 
544  List<int> TrianglesUP = new List<int>();
545  List<int> TrianglesDown = new List<int>();
546  List<int> TrianglesSide = new List<int>();
547 
548 
549  List<Color32> colorsUP = new List<Color32>();
550  List<Color32> colorsDown = new List<Color32>();
551  List<Color32> colorsSide = new List<Color32>();
552 
553 
554 
555  Mesh BondUP = new Mesh();
556  Mesh BondDown = new Mesh();
557  Mesh bondSide = new Mesh();
558 
559  Vector3 Au,Bu,Cu,Du, Ad,Bd, Cd, Dd;
560 
561  int atom1 = findRingAtomIndex(i, connectivityList[i][0+(3*j)]);
562  int atom2 = findRingAtomIndex(connectivityList[i][1+(3*j)], connectivityList[i][2+(3*j)]);
563  int res2 = connectivityList[i][1+(3*j)];
564  int size1 = cycles[i].Count;
565  int size2 = cycles[res2].Count;
566  string resname2 = residueslist[res2];
567  string chain2 = MoleculeModel.resChainList[res2];
568 
569 
570  Au=meshesBIGRingUp[i].vertices[MeshIndexForAtom[size1][atom1][0]];
571  Bu=meshesBIGRingUp[i].vertices[MeshIndexForAtom[size1][atom1][1]];
572  Cu=meshesBIGRingUp[connectivityList[i][1+(3*j)]].vertices[MeshIndexForAtom[size2][atom2][1]];
573  Du=meshesBIGRingUp[connectivityList[i][1+(3*j)]].vertices[MeshIndexForAtom[size2][atom2][0]];
574 
575  Ad=meshesBIGRingDown[i].vertices[MeshIndexForAtom[size1][atom1][0]];
576  Bd=meshesBIGRingDown[i].vertices[MeshIndexForAtom[size1][atom1][1]];
577  Cd=meshesBIGRingDown[connectivityList[i][1+(3*j)]].vertices[MeshIndexForAtom[size2][atom2][1]];
578  Dd=meshesBIGRingDown[connectivityList[i][1+(3*j)]].vertices[MeshIndexForAtom[size2][atom2][0]];
579 
580  //Angle for twist
581  float AngleU = Vector3.Angle((Au-Bu), (Cu-Du));
582 
583 
584  //Debug.Log("res:"+(i+1)+ "connected to+"+(connectivityList[i][1+(3*j)]+1) );
585  if (AngleU>70){
586  verticesUP.AddMany(Au,Bu,Dd,Cd);
587  verticesDown.AddMany(Ad,Bd,Du,Cu);
588  }else{
589  verticesUP.AddMany(Au,Bu,Cu,Du);
590  verticesDown.AddMany(Ad,Bd,Cd,Dd);
591  }
592 
593  VerticesSide.AddMany(verticesUP[0],verticesDown[0]);
594  VerticesSide.AddMany(verticesUP[1],verticesDown[1]);
595  VerticesSide.AddMany(verticesUP[3],verticesDown[3]);
596  VerticesSide.AddMany(verticesUP[2],verticesDown[2]);
597 
598  TrianglesUP.AddMany(0,1,2,
599  2,1,3);
600 
601  TrianglesDown.AddMany(3,2,1,
602  1,2,0);
603 
604  TrianglesSide.AddMany(0,1,2, 2,1,3, 2,3,4, 4,3,5,
605  4,5,6, 6,5,7, 6,7,0, 0,7,1);
606 
607 
608 
609  if (COLOR_MODE_BOND == SUGAR){
610 
611  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname1)){
612  colorsUP.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname1], LIGHTER_COLOR_FACTOR_BOND));
613  colorsDown.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname1], LIGHTER_COLOR_FACTOR_BOND));
614  colorsUP.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname1], LIGHTER_COLOR_FACTOR_BOND));
615  colorsDown.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname1], LIGHTER_COLOR_FACTOR_BOND));
616  }else{
617  colorsUP.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
618  colorsDown.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
619  colorsUP.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
620  colorsDown.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
621  }
622 
623  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname2)){
624  colorsUP.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname2], LIGHTER_COLOR_FACTOR_BOND));
625  colorsDown.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname2], LIGHTER_COLOR_FACTOR_BOND));
626  colorsUP.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname2], LIGHTER_COLOR_FACTOR_BOND));
627  colorsDown.Add(lightColor(UI.GUIDisplay.colorByResiduesDict[resname2], LIGHTER_COLOR_FACTOR_BOND));
628  }else{
629  colorsUP.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
630  colorsDown.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
631  colorsUP.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
632  colorsDown.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
633  }
634 
635  }else if (COLOR_MODE_BOND == CHAIN){
636  if (UI.GUIDisplay.ChainColorDict.ContainsKey(chain1)){
637  colorsUP.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain1], LIGHTER_COLOR_FACTOR_BOND));
638  colorsDown.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain1], LIGHTER_COLOR_FACTOR_BOND));
639  colorsUP.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain1], LIGHTER_COLOR_FACTOR_BOND));
640  colorsDown.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain1], LIGHTER_COLOR_FACTOR_BOND));
641  }else{
642  colorsUP.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
643  colorsDown.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
644  colorsUP.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
645  colorsDown.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
646  }
647  if (UI.GUIDisplay.ChainColorDict.ContainsKey(chain2)){
648  colorsUP.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain2], LIGHTER_COLOR_FACTOR_BOND));
649  colorsDown.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain2], LIGHTER_COLOR_FACTOR_BOND));
650  colorsUP.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain2], LIGHTER_COLOR_FACTOR_BOND));
651  colorsDown.Add(lightColor(UI.GUIDisplay.ChainColorDict[chain2], LIGHTER_COLOR_FACTOR_BOND));
652  }else{
653  colorsUP.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
654  colorsDown.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
655  colorsUP.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
656  colorsDown.Add(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
657  }
658 
659  }else if (COLOR_MODE_BOND == PICKER){
660  for (int k=0; k<4;k++){
661  colorsUP.Add(lightColor(BONDCOLOR.color, LIGHTER_COLOR_FACTOR_BOND));
662  colorsDown.Add(lightColor(BONDCOLOR.color, LIGHTER_COLOR_FACTOR_BOND));
663  }
664 
665 
666  }
667 
668  colorsSide.AddMany(colorsUP[0], colorsDown[0]);
669  colorsSide.AddMany(colorsUP[1], colorsDown[1]);
670  colorsSide.AddMany(colorsUP[2], colorsDown[2]);
671  colorsSide.AddMany(colorsUP[3], colorsDown[3]);
672 
673  BondUP.vertices = verticesUP.ToArray();
674  BondUP.triangles = TrianglesUP.ToArray();
675  BondUP.colors32 = colorsUP.ToArray();
676 
677  BondDown.vertices = verticesDown.ToArray();
678  BondDown.triangles = TrianglesDown.ToArray();
679  BondDown.colors32 = colorsDown.ToArray();
680 
681  bondSide.vertices = VerticesSide.ToArray();
682  bondSide.triangles = TrianglesSide.ToArray();//generateTriangleSide(BondUP);
683  bondSide.colors32 = colorsSide.ToArray();
684 
685  BondUP.RecalculateNormals();
686  BondDown.RecalculateNormals();
687  bondSide.RecalculateNormals();
688  meshesBondUp.Add(BondUP);
689  meshesBondDown.Add(BondDown);
690  meshesBondSide.Add(bondSide);
691 
692  res_for_bond[key_index_for_bond_dict] = resname1 +"_"+chain1+"_"+resname2+"_"+chain2;
693  }
694  }
695 
696  }
697 
698 
699 
700 
701  public GameObject CreateOxygenSphere(int pos, int res, int sugartype=0){
702  GameObject oxysphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
703  oxysphere.tag = "OxySphere";
704  Vector3 point = new Vector3(atomsLocationlist [pos] [0],atomsLocationlist [pos] [1],atomsLocationlist [pos] [2]);
705  oxysphere.transform.position = point;
706  string resname = residueslist[res];
707  string chain = resChainList[res];
708  if (sugartype==1){
709  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname))
710  oxysphere.GetComponent<Renderer>().material.color = UI.GUIDisplay.colorByResiduesDict[resname];
711  else
712  oxysphere.GetComponent<Renderer>().material.color = new Color(0.7f,0.7f,0.7f,0.5f);
713  }else if (sugartype==2){
714  oxysphere.GetComponent<Renderer>().material.color = UI.GUIDisplay.ChainColorDict[chain];
715  }else{
716  oxysphere.GetComponent<Renderer>().material.color = Color.red;
717  }
718  oxysphere.transform.localScale = new Vector3(OXYSPHERESIZE, OXYSPHERESIZE, OXYSPHERESIZE);
719  return oxysphere;
720 
721  }
722 
723 
724  public List<GameObject> ShowOxySphere(int colortype){
725  List<GameObject> result = new List<GameObject>();
726  for (int i=0; i<this.cycles.Count; i++) {
727  for (int j=0; j<this.cycles[i].Count; j++) {
728  if (atomsNamelist[cycles[i][j]][0] == 'O'){
729  result.Add(CreateOxygenSphere (cycles [i] [j], i,colortype));
730  }
731  }
732  }
733  return result;
734  }
735 
736 
737 
738 
744  public GameObject CreateSugarRibbonsObj(Mesh mesh, string res="", string chain="", string tag="SugarRibbons", int id=0){
745  GameObject SRobj;
746  if (tag=="SugarRibbons_BOND")
747  SRobj = new GameObject("SugarRibbons_"+res+"_"+chain+"_BOND_"+id);
748  else
749  SRobj = new GameObject("SugarRibbons_"+res+"_"+chain);
750  SRobj.tag = tag;
751  SRobj.AddComponent<MeshFilter>();
752  SRobj.AddComponent<MeshRenderer>();
753 
754 
755  Vector3[] vertices = mesh.vertices;
756  Vector2[] uvs = new Vector2[vertices.Length];
757  int i = 0;
758  while (i < uvs.Length) {
759  uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
760  i++;
761  }
762  mesh.uv = uvs;
763  Solve(mesh);
764 
765  SRobj.GetComponent<MeshFilter> ().mesh = mesh;
766  SRobj.GetComponent<Renderer>().material = new Material (Shader.Find ("Custom/Ribbons"));
767 
768  return SRobj;
769 
770 
771  }
772 
773  public static void Solve(Mesh mesh)
774  {
775  int triangleCount = mesh.triangles.Length / 3;
776  int vertexCount = mesh.vertices.Length;
777 
778  Vector3[] tan1 = new Vector3[vertexCount];
779  Vector3[] tan2 = new Vector3[vertexCount];
780  Vector4[] tangents = new Vector4[vertexCount];
781  int a = 0;
782  while(a < triangleCount)
783  {
784  long i1 = mesh.triangles[a++];
785  long i2 = mesh.triangles[a++];
786  long i3 = mesh.triangles[a++];
787 
788  Vector3 v1 = mesh.vertices[i1];
789  Vector3 v2 = mesh.vertices[i2];
790  Vector3 v3 = mesh.vertices[i3];
791 
792  Vector2 w1 = mesh.uv[i1];
793  Vector2 w2 = mesh.uv[i2];
794  Vector2 w3 = mesh.uv[i3];
795 
796  float x1 = v2.x - v1.x;
797  float x2 = v3.x - v1.x;
798  float y1 = v2.y - v1.y;
799  float y2 = v3.y - v1.y;
800  float z1 = v2.z - v1.z;
801  float z2 = v3.z - v1.z;
802 
803  float s1 = w2.x - w1.x;
804  float s2 = w3.x - w1.x;
805  float t1 = w2.y - w1.y;
806  float t2 = w3.y - w1.y;
807 
808  float r = 1.0f / (s1 * t2 - s2 * t1);
809 
810  Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
811  Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
812 
813  tan1[i1] += sdir;
814  tan1[i2] += sdir;
815  tan1[i3] += sdir;
816 
817  tan2[i1] += tdir;
818  tan2[i2] += tdir;
819  tan2[i3] += tdir;
820  }
821  for (a = 0; a < vertexCount; a++)
822  {
823  Vector3 n = mesh.normals[a];
824  Vector3 t = tan1[a];
825  tangents[a] = (t - n * Vector3.Dot(n, t)).normalized;
826  tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
827  }
828  mesh.tangents = tangents;
829  }
830 
831  private void debugline(Vector3 a, Vector3 b){
832  GameObject lineObject;
833  LineRenderer line;
834  lineObject = new GameObject("measureline");
835  line = lineObject.AddComponent<LineRenderer>();
836  line.material = new Material (Shader.Find("Particles/Alpha Blended"));
837  line.SetColors(Color.red, Color.red);
838  line.SetWidth(0.3f, 0.3f);
839  line.SetPosition(0, a);
840  line.SetPosition(1,b);
841 
842  }
843 
844 
845 
846 
847  public List<List<GameObject>> createSugarRibbons(){
850  List<List<GameObject>> result = new List<List<GameObject>>();
851  for(int i=0;i<3;i++)
852  result.Add(new List<GameObject>());
853 
854  for (int i=0; i<CycleMesh.Count; i++) {
855  //For center ring
856  generateRibbonsMesh(CycleMesh[i], i, THICKNESS_LITTLE_MESH,true);
857 
858  //For outer ring
859  generateRibbonsMesh(CycleBIGMesh[i], i, THICKNESS_BIG_MESH,false);
860  }
861 
862  create_Bond();
863  for (int i=0; i<meshesRingUp.Count; i++){
864  //For center ring
865  result[0].Add(CreateSugarRibbonsObj (meshesRingUp[i],residueslist[i],resChainList[i], "SugarRibbons_RING_little"));
866  result[0].Add(CreateSugarRibbonsObj (meshesRingDown[i],residueslist[i],resChainList[i],"SugarRibbons_RING_little"));
867  result[0].Add(CreateSugarRibbonsObj(meshesSideRing[i],residueslist[i],resChainList[i],"SugarRibbons_RING_little"));
868 
869  //For outer ring
870  result[1].Add(CreateSugarRibbonsObj (meshesBIGRingUp[i],residueslist[i],resChainList[i],"SugarRibbons_RING_BIG"));
871  result[1].Add(CreateSugarRibbonsObj (meshesBIGRingDown[i],residueslist[i],resChainList[i],"SugarRibbons_RING_BIG"));
872  result[1].Add(CreateSugarRibbonsObj(meshesBIGSideRing[i],residueslist[i],resChainList[i],"SugarRibbons_RING_BIG"));
873  }
874 
875  for (int i=0; i<meshesBondUp.Count; i++){
876  result[2].Add(CreateSugarRibbonsObj (meshesBondUp[i],residueslist[i],resChainList[i],"SugarRibbons_BOND",i));
877  result[2].Add(CreateSugarRibbonsObj (meshesBondDown[i],residueslist[i],resChainList[i],"SugarRibbons_BOND",i));
878  result[2].Add(CreateSugarRibbonsObj (meshesBondSide[i],residueslist[i],resChainList[i],"SugarRibbons_BOND",i));
879  }
880  return result;
881 
882  }
883 
884 
885 
886  /***********************************************
887  ************** TOOL FUNCTIONS *****************
888  ***********************************************/
889 
890  public void updateColorBond(List<GameObject> objs, ColorObject newColor){
891  for (int i=0; i<objs.Count; i++){
892  Color32[] colours = objs[i].GetComponent<MeshFilter> ().mesh.colors32;
893  for (int j=0; j< colours.Length; j++){
894  colours[j]=lightColor(newColor.color, LIGHTER_COLOR_FACTOR_BOND);
895  }
896  objs[i].GetComponent<MeshFilter> ().mesh.colors32 = colours;
897  }
898  }
899  public void updateColorRing(List<GameObject> objs, ColorObject newColor){
900  for (int i=0; i<objs.Count; i++){
901  Color32[] colours = objs[i].GetComponent<MeshFilter> ().mesh.colors32;
902 
903  for (int j=0; j< colours.Length; j++){
904  colours[j]=lightColor(newColor.color, LIGHTER_COLOR_FACTOR_RING);
905 
906  }
907  objs[i].GetComponent<MeshFilter> ().mesh.colors32 = colours;
908  }
909  }
910  public void updateColorBond(List<GameObject> objs, int type){
911 
912  for (int i=0; i<objs.Count; i++){
913  Color32[] colours = objs[i].GetComponent<MeshFilter> ().mesh.colors32;
914  int SIZE = objs[i].GetComponent<MeshFilter> ().mesh.vertices.Length;
915  int ID = int.Parse(objs[i].ToString().Split('_')[4].Split(' ')[0]);
916  string resname1 = res_for_bond[ID].Split('_')[0];
917  string resname2 = res_for_bond[ID].Split('_')[2];
918  string chain1 = res_for_bond[ID].Split('_')[1];
919  string chain2 = res_for_bond[ID].Split('_')[3];
920  Color32 col1;
921  Color32 col2;
922  if (type == SUGAR){
923  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname1))
924  col1 =(lightColor(UI.GUIDisplay.colorByResiduesDict[resname1], LIGHTER_COLOR_FACTOR_BOND));
925  else
926  col1 =lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND);
927  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname1))
928  col2=(lightColor(UI.GUIDisplay.colorByResiduesDict[resname2], LIGHTER_COLOR_FACTOR_BOND));
929  else
930  col2 =lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND);
931  }else {
932  if (UI.GUIDisplay.ChainColorDict.ContainsKey(chain1))
933  col1=(lightColor(UI.GUIDisplay.ChainColorDict[chain1], LIGHTER_COLOR_FACTOR_BOND));
934  else
935  col1 = (lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
936  if (UI.GUIDisplay.ChainColorDict.ContainsKey(chain2))
937  col2=(lightColor(UI.GUIDisplay.ChainColorDict[chain2], LIGHTER_COLOR_FACTOR_BOND));
938  else
939  col2 = (lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_BOND));
940  }
941  if (SIZE == 4){
942  colours[0]=col1;
943  colours[1]=col1;
944  colours[2]=col2;
945  colours[3]=col2;
946  }else {
947  colours[0]=col1;
948  colours[1]=col1;
949  colours[2]=col1;
950  colours[3]=col1;
951  colours[4]=col2;
952  colours[5]=col2;
953  colours[6]=col2;
954  colours[7]=col2;
955  }
956  objs[i].GetComponent<MeshFilter> ().mesh.colors32 = colours;
957  }
958  }
959 
960  public void updateColorRing(List<GameObject> objs, int type){
961  for (int i=0; i<objs.Count; i++){
962  Color32[] colours = objs[i].GetComponent<MeshFilter> ().mesh.colors32;
963  string resname = objs[i].ToString().Split('_')[1];
964  string chain = objs[i].ToString().Split('_')[2].Split(' ')[0];
965  for (int j=0; j<colours.Length; j++){
966  if (type == SUGAR){
967  if (UI.GUIDisplay.colorByResiduesDict.ContainsKey(resname))
968  colours[j]=(lightColor(UI.GUIDisplay.colorByResiduesDict[resname], LIGHTER_COLOR_FACTOR_RING));
969  else
970  colours[j]=(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_RING));
971  }else if (type == CHAIN){
972  if (UI.GUIDisplay.ChainColorDict.ContainsKey(chain))
973  colours[j]=(lightColor(UI.GUIDisplay.ChainColorDict[chain], LIGHTER_COLOR_FACTOR_RING));
974  else
975  colours[j]=(lightColor(new Color(0.7f,0.7f,0.7f,0.5f), LIGHTER_COLOR_FACTOR_RING));
976 
977  }
978  }
979  objs[i].GetComponent<MeshFilter> ().mesh.colors32 = colours;
980  }
981  }
982 
983 
984  private void CalculBarycenter(){
985  Debug.Log("Calculation of cycles barycenters");
986  Vector3 barycenter = new Vector3();
987  //we save all coordinate and prepare barycenter calculation
988  float xTot, yTot ,zTot;
989  for (int i=0; i<cycles.Count; i++){
990  xTot=0;
991  yTot=0;
992  zTot=0;
993  for (int j=0; j<cycles[i].Count; j++){
994  xTot=xTot+atomsLocationlist [cycles [i] [j]] [0];
995  yTot=yTot+atomsLocationlist [cycles [i] [j]] [1];
996  zTot=zTot+atomsLocationlist [cycles [i] [j]] [2];
997  }
998  //calculation of barycenter
999  barycenter.x=xTot/(cycles[i].Count);
1000  barycenter.y=yTot/(cycles[i].Count);
1001  barycenter.z=zTot/(cycles[i].Count);
1002  barylist.Add(barycenter);
1003  }
1004 
1005  }
1006 
1007  private Vector3 calculMeshBarycenter(Mesh mesh){
1008  float totX, totY, totZ;
1009  totX = totY = totZ = 0;
1010  Vector3 barycenter = new Vector3();
1011 
1012  for (int i=0; i<mesh.vertices.Length; i++){
1013  totX+=mesh.vertices[i][0];
1014  totY+=mesh.vertices[i][1];
1015  totZ+=mesh.vertices[i][2];
1016  }
1017  barycenter.x = totX/mesh.vertices.Length;
1018  barycenter.y = totY/mesh.vertices.Length;
1019  barycenter.z = totZ/mesh.vertices.Length;
1020 
1021  return barycenter;
1022  }
1023 
1024  private Vector3 calculMidPoint(Vector3 a, Vector3 b){
1025  float x, y, z;
1026  x = (a.x + b.x)/2;
1027  y = (a.y + b.y)/2;
1028  z = (a.z + b.z)/2;
1029 
1030  return new Vector3(x,y,z);
1031  }
1032 
1033  private int findRingAtomIndex(int ringIndex, int AtomNumber){
1034  for (int i=0; i<cycles[ringIndex].Count; i++){
1035  if (cycles[ringIndex][i] == AtomNumber)
1036  return i;
1037  }
1038  return -1;
1039  }
1040 
1041  private Vector3 CycleAtomToVector3(int index_res, int number){
1042  Vector3 v = new Vector3 (atomsLocationlist[cycles[index_res][number]][0],
1043  atomsLocationlist[cycles[index_res][number]][1],
1044  atomsLocationlist[cycles[index_res][number]][2]);
1045 
1046  return v;
1047 
1048  }
1049 
1050  private Vector3 calcullittleRibbonsControlPoint(Vector3 a, Vector3 b, float size=0.7f){
1051  Vector3 v = new Vector3 ();
1052  Vector3 dir= new Vector3();
1053 
1054  dir = a - b;
1055 
1056  v = a - (dir * size);
1057  return v;
1058 
1059  }
1060 
1061 
1062  private void createDebugSphere(Vector3 point, Color color, float size=1f){
1063  GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
1064 
1065  go.AddComponent<Animation>();
1066  go.transform.position = point;
1067  go.GetComponent<Renderer>().material.color = color;
1068  go.transform.localScale = new Vector3(size, size, size);
1069  }
1070 
1071 
1072  private Vector3 calculMeanNormalMesh(Mesh mesh){
1073  float xTot, yTot, zTot;
1074  xTot = yTot = zTot = 0;
1075  Vector3 normalMean = new Vector3();
1076  mesh.RecalculateNormals();
1077  for (int i=0; i<mesh.normals.Length; i++){
1078  xTot+=mesh.normals[i].x;
1079  yTot+=mesh.normals[i].y;
1080  zTot+=mesh.normals[i].z;
1081  }
1082 
1083  normalMean.x = xTot/mesh.normals.Length;
1084  normalMean.y = yTot/mesh.normals.Length;
1085  normalMean.z = zTot/mesh.normals.Length;
1086 
1087  return normalMean;
1088 
1089  }
1090 
1091 
1092  private int[] invertTrianglesMesh(Mesh mesh){
1093  List<int> newTriangles = new List<int>();
1094 
1095  for (int i=mesh.triangles.Length-1; i>=0; i--){
1096  newTriangles.Add(mesh.triangles[i]);
1097  }
1098 
1099  return newTriangles.ToArray();
1100  }
1101 
1102 
1103  private int[] generateTriangleSide(Mesh mesh){
1104  List<int> triangles = new List<int>();
1105  int size = mesh.vertices.Length;
1106 
1107  for (int i=0; i<size -2; i=i+2){
1108  triangles.AddMany(i, i+1, i+2);
1109  triangles.AddMany(i+2, i+1, i+3);
1110  }
1111 
1112  triangles.AddMany(size-2, size-1, 0);
1113  triangles.AddMany (0, size-1, 1);
1114 
1115  return triangles.ToArray();
1116  }
1117 
1118  public Color lightColor(Color color, float correctionFactor = 0.35f){
1119  if (correctionFactor>=0)
1120  return Color.Lerp(color, Color.white, correctionFactor);
1121  else{
1122  return Color.Lerp(color, Color.black, -correctionFactor);
1123  }
1124  }
1125 
1126  public void cleanup(){
1127  CycleMesh.Clear();
1128  CycleBIGMesh.Clear();
1129  meshesRingUp.Clear();
1130  meshesRingDown.Clear();
1131  meshesSideRing.Clear();
1132  meshesBIGRingUp.Clear();
1133  meshesBIGRingDown.Clear();
1134  meshesBIGSideRing.Clear();
1135  meshesBondUp.Clear();
1136  meshesBondDown.Clear();
1137  meshesBondSide.Clear();
1138  }
1139 
1140 
1141 
1142 
1143 }
ColorObject OXYSPHERECOLOR
Definition: SugarRibbons.cs:64
List< int > triangles
Definition: SugarRibbons.cs:45
string resname
Definition: GraphVertex.cs:17
List< Mesh > meshesBIGRingDown
Definition: SugarRibbons.cs:21
Vector3 float3toVector3(float[] point)
Definition: SugarRibbons.cs:72
int[] invertTrianglesMesh(Mesh mesh)
List< int[]> bondEPList
Definition: SugarRibbons.cs:82
List< Color32 > colors
Definition: SugarRibbons.cs:43
List< string > sugarResname
Definition: SugarRibbons.cs:69
Vector3 calculMeshBarycenter(Mesh mesh)
Vector3 coordinate
Definition: GraphVertex.cs:18
void generateRibbonsMesh(Mesh centralMesh, int resnumber, float thicknessCenter=1f, bool center=true)
int COLOR_MODE_BOND
Definition: SugarRibbons.cs:58
List< int > residuesNumberlist
Definition: SugarRibbons.cs:33
Vector3 calcMidPoint(Vector3 a, Vector3 b)
Color lightColor(Color color, float correctionFactor=0.35f)
Dictionary< int, string > res_for_bond
Definition: SugarRibbons.cs:86
void changeColor(int which)
List< string > residueslist
Definition: SugarRibbons.cs:32
float LIGHTER_COLOR_FACTOR_BOND
Definition: SugarRibbons.cs:54
List< string > atomsNamelist
Definition: SugarRibbons.cs:80
GameObject CreateOxygenSphere(int pos, int res, int sugartype=0)
List< Vector3 > barylist
Definition: SugarRibbons.cs:35
Vector3 LinearComb(float scalar0, Vector3 vector0, float scalar1, Vector3 vector1)
Definition: SugarRibbons.cs:38
List< Mesh > CycleBIGMesh
Definition: SugarRibbons.cs:13
float THICKNESS_BOND_5
Definition: SugarRibbons.cs:52
void generateCycleMesh2()
List< Vector3 > normals
Definition: SugarRibbons.cs:44
static List< string > atomsSugarNamelist
float THICKNESS_BIG_MESH
Definition: SugarRibbons.cs:49
int[] generateTriangleSide(Mesh mesh)
List< List< int > > cycles
Definition: SugarRibbons.cs:31
float THICKNESS_BOND_6_C1_C4
Definition: SugarRibbons.cs:50
static List< string > atomsNamelist
The name of each atom.
List< Mesh > meshesRingUp
Definition: SugarRibbons.cs:16
List< Mesh > meshesSideRing
Definition: SugarRibbons.cs:18
ColorObject BONDCOLOR
Definition: SugarRibbons.cs:62
float THICKNESS_BOND_6_OTHER
Definition: SugarRibbons.cs:51
int findRingAtomIndex(int ringIndex, int AtomNumber)
static List< float[]> atomsSugarLocationlist
List< GraphVertex > molAsGraph
Definition: SugarRibbons.cs:30
void generateBiggerCycleMesh(float scaling=0.60f)
Color color
Definition: ColorObject.cs:72
void debugline(Vector3 a, Vector3 b)
void createDebugSphere(Vector3 point, Color color, float size=1f)
static Dictionary< string, Color > ChainColorDict
Definition: GUIDisplay.cs:180
void create_Bond()
List< List< GameObject > > createSugarRibs(float RibbonsThickness, bool sugarOnly, float thickness_Little, float thickness_BIG, float thickness_bond_6_C1_C4, float thickness_6_other, float thickness_bond_5, float lighter_color_factor_ring, float lighter_color_factor_bond, int color_mode_ring, int color_mode_bond, ColorObject bondcolor, ColorObject ringcolor, float OxySphereSize, ColorObject OxySphereColor)
List< Mesh > meshesBIGRingUp
Definition: SugarRibbons.cs:20
List< Mesh > meshesRingDown
Definition: SugarRibbons.cs:17
List< Mesh > meshesBondSide
Definition: SugarRibbons.cs:27
void updateColorBond(List< GameObject > objs, int type)
float THICKNESS
Definition: SugarRibbons.cs:47
Vector3 calculMidPoint(Vector3 a, Vector3 b)
SugarRibbons(bool SugarOnly)
Definition: SugarRibbons.cs:89
static List< int[]> bondEPSugarList
List< GameObject > ShowOxySphere(int colortype)
List< string > atomsResnamelist
Definition: SugarRibbons.cs:79
Dictionary< int, Dictionary< int, int[]> > MeshIndexForAtom
Definition: SugarRibbons.cs:85
int COLOR_MODE_RING
Definition: SugarRibbons.cs:57
List< Vector3 > makeFlatVertex(List< Vector3 > verticeEn)
List< List< GameObject > > createSugarRibbons()
List< float[]> atomsLocationlist
Definition: SugarRibbons.cs:78
static void Solve(Mesh mesh)
List< Mesh > meshesBondDown
Definition: SugarRibbons.cs:26
!WiP manage GUI, and provide static strings for the GUI.
Definition: GUIDisplay.cs:94
Vector3 CycleAtomToVector3(int index_res, int number)
static List< int[]> bondEPList
The bonds between atoms.
ColorObject RINGCOLOR
Definition: SugarRibbons.cs:63
List< string > resChainList
Definition: SugarRibbons.cs:81
List< Vector3 > vertices
Definition: SugarRibbons.cs:42
This class is made to convert an atom (postion X,Y,Z) to a object with a list of neighbor.
Definition: GraphVertex.cs:10
Vector3 calcullittleRibbonsControlPoint(Vector3 a, Vector3 b, float size=0.7f)
GameObject CreateSugarRibbonsObj(Mesh mesh, string res="", string chain="", string tag="SugarRibbons", int id=0)
For each mesh, we create Two mesh (to have a ribbon with tchickness)
static List< string > resSugarChainList
static List< string > atomsResnamelist
The name of the residue to which each atom belongs.
static Dictionary< string, Color > colorByResiduesDict
Definition: GUIDisplay.cs:179
static List< string > resChainList
The chain of each residue (only work if residues are numbered by chain).
void updateColorRing(List< GameObject > objs, ColorObject newColor)
float OXYSPHERESIZE
Definition: SugarRibbons.cs:55
List< Vector3 > enlargeMesh(int i, float scaling)
List< Mesh > meshesBIGSideRing
Definition: SugarRibbons.cs:22
List< Mesh > CycleMesh
Definition: SugarRibbons.cs:12
Vector3 calculMeanNormalMesh(Mesh mesh)
Dictionary< int, List< int > > connectivityList
Definition: SugarRibbons.cs:84
static float[] wideness
Definition: SugarRibbons.cs:66
void updateColorBond(List< GameObject > objs, ColorObject newColor)
List< Mesh > meshesBondUp
Definition: SugarRibbons.cs:25
Definition: GUIDisplay.cs:66
void updateColorRing(List< GameObject > objs, int type)
float THICKNESS_LITTLE_MESH
Definition: SugarRibbons.cs:48
static List< string > atomsSugarResnamelist
static List< float[]> atomsLocationlist
The coordinates of each atom.
void CalculBarycenter()
float LIGHTER_COLOR_FACTOR_RING
Definition: SugarRibbons.cs:53