UnityMol  0.9.6-875
UnityMol viewer / In developement
VolumetricDensity.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System.Collections;
68 using Molecule.Model;
69 
70 public class VolumetricDensity : Volumetric {
71  private GameObject pdb2den;
73  private float maxDensity = float.MinValue;
74  private float minDensity = float.MaxValue;
75  private float densityAmplitude;
76 
77  private static Vector3 inverseDelta ;
78  private static Vector3 fudgeFactor;
79 
80  public override void Init () {
81  // First we use PDBtoDen to create a density grid.
82  if(pdb2den == null){
83  pdb2den = new GameObject();
84  pdb2den.name = "pdb2den";
85  pdb2den.AddComponent<PDBtoDEN>();
86  }
87  genDensity = pdb2den.GetComponent<PDBtoDEN>();
88 
89  float delta_scalar = 2.0f;
90  delta = new Vector3(delta_scalar, delta_scalar, delta_scalar);
91  genDensity.TranPDBtoDEN (delta_scalar);
93  fudgeFactor = PDBtoDEN.fudgeFactor;
94 
95  // We get that grid and its delta.
97  //delta = PDBtoDEN.delta;
98 
99  inverseDelta = new Vector3( 1f/delta.x,
100  1f/delta.y,
101  1f/delta.z );
102 
103  // We need to refresh the molecule's origin when it's not the first molecule for which we generate a volumetric density.
105 
106  // We determine the amplitude of the density.
107  foreach(float f in density)
108  {
109  if(f > maxDensity)
110  maxDensity = f;
111  if(f < minDensity)
112  minDensity = f;
113  }
114  densityAmplitude = maxDensity - minDensity ;
115 
116  // Creating the dynamic particle list, building the static particle array,
117  // setting it to the particle system, and enabling the renderer.
119  } // End of Init
120 
121 
122  public override void CreatePoints() {
123  // Getting the size of the density grid.
124  int dim0 = density.GetLength(0);
125  int dim1 = density.GetLength(1);
126  int dim2 = density.GetLength(2);
127 
128  Vector3 indices = Vector3.zero;
129 
130  for(int x=0; x<dim0; x++) {
131  for(int y=0; y<dim1; y++) {
132  for(int z=0; z<dim2; z++) {
133  indices.x = x;
134  indices.y = y;
135  indices.z = z;
136 
137  Color c = Color.blue; // arbitrary
138  c.a = (density[x,y,z] / densityAmplitude); // Somewhat arbitrary too, but guarantees 0 <= c.a <= 1.
139 
140  // Here, we cull.
141  float size = particleScale * inverseDelta.x; // Assumes delta.x == delta.y == delta.z. For now, that's true, but it might change.
142  if (c.a > ALPHA_THRESHOLD) {
143  ParticleSystem.Particle particle = new ParticleSystem.Particle();
144  Vector3 p = new Vector3(0f,0f,0f);
145  p = Vector3.Scale(inverseDelta,(indices - fudgeFactor)) + origin;
146  particle.position = p;
147  particle.startColor = c;
148  particle.startSize = size;
149  dynPoints.Add(particle);
150  }
151  }
152  }
153  }
154  } // End of CreatePoints
155 
156 }
float ALPHA_THRESHOLD
Definition: Volumetric.cs:72
static Vector3 delta
Definition: PDBtoDEN.cs:92
void TranPDBtoDEN(float resolution=DEFAULT_RESOLUTION, bool cap=true)
Definition: PDBtoDEN.cs:141
List< ParticleSystem.Particle > dynPoints
Definition: Volumetric.cs:80
static float[,,] GridS
Definition: PDBtoDEN.cs:77
static float particleScale
Definition: Volumetric.cs:71
void SetParticleSystem()
Sets the particle system.
Definition: Volumetric.cs:140
static Vector3 inverseDelta
static Vector3 delta
Definition: Volumetric.cs:82
override void Init()
Initializes this instance.
static Vector3 fudgeFactor
Definition: PDBtoDEN.cs:90
static Vector3 fudgeFactor
static Vector3 MinValue
The "smallest" corner of the bounding box that encloses the molecule.
float[,,] density
Definition: Volumetric.cs:77
override void CreatePoints()
Creates the points for this particle system.
static Vector3 origin
Definition: Volumetric.cs:83