private void Calculate_Click(object sender, System.EventArgs e)
            {
                Pach_GetModel_Command Model = new Pach_GetModel_Command();
                Source[] Source;


                if (PachydermAc_PlugIn.Instance.Source(out Source) && !object.ReferenceEquals(FileLocation.SelectedPath, "")) Rhino.RhinoApp.WriteLine("Model geometry not specified... Exiting calculation...");
                ParticleRays[] RTParticles = new ParticleRays[Source.Length];

                Calculate.Enabled = false;
                PachydermAc_PlugIn plugin = PachydermAc_PlugIn.Instance;
                Scene Sc;
                if (PachydermAc_PlugIn.Instance.Geometry_Spec() == 0) Sc = PachTools.Get_NURBS_Scene(0, (double)Air_Temp.Value, 0, 0, false);
                else Sc = PachTools.Get_Poly_Scene(0, (double)Air_Temp.Value, 0, 0, false);
                
                for (int i = 0; i < Source.Length; i++)
                {
                    if (Source != null)
                    {
                        List<Point3d> L = new List<Point3d>();
                        List<Rhino.Geometry.Point3d> L_RC = new List<Rhino.Geometry.Point3d>();
                        for (int j = 0; j < Source.Length; j++)
                        {
                            L.Add(Source[j].Origin());
                            L_RC.Add(new Rhino.Geometry.Point3d(Source[j].Origin().X, Source[j].Origin().Y, Source[j].Origin().Z));
                        }

                        if (plugin.Geometry_Spec() == 0)
                        {
                            Sc.partition(L_RC, 15);
                            RTParticles[i] = new ParticleRays(Source[i], Sc, (int)RT_Count.Value, CutOffLength());
                        }
                        else if (plugin.Geometry_Spec() == 1)
                        {
                            Sc.partition(L, 15);
                            RTParticles[i] = new ParticleRays(Source[i], Sc, (int)RT_Count.Value, CutOffLength());
                        }
                        RTParticles[i].Begin();
                    }
                    else
                    {
                        Rhino.RhinoApp.WriteLine("Model geometry not specified... Exiting calculation...");
                    }

                    Pachyderm_Acoustic.Visualization.Phonon P;
                    if (ParticleChoice.SelectedIndex == 0)
                    { P = new Tetrahedron(); }
                    else if (ParticleChoice.SelectedIndex == 1)
                    {
                        P = new Icosahedron();
                    }
                    else { P = new Geodesic_sphere(); }
                    RenderParticles(RTParticles, (double)(Frame_Rate.Value * Seconds.Value), P);
                    Rhino.RhinoDoc.ActiveDoc.Views.Redraw();
                }
                Calculate.Enabled = true;
            }
 public WaveConduit(ParticleRays[] PR_in, Pach_Graphics.colorscale C_in, double[] V_Bounds_in, Scene S)
 {
     min = Utilities.PachTools.HPttoRPt(S.Min());
     Hare.Geometry.Vector range = S.Max() - S.Min();
     nx = (int)System.Math.Ceiling(range.x);
     ny = (int)System.Math.Ceiling(range.y);
     nz = (int)System.Math.Ceiling(range.z);
     ptgrid = new List<int>[nx, ny, nz];
     for(int x = 0; x < nx; x++)
         for (int y = 0; y < ny; y++)
             for (int z = 0; z < nz; z++)
             {
                 ptgrid[x, y, z] = new List<int>();
             }
     dx = range.x / nx;
     dy = range.y / ny;
     dz = range.z / nz;
     C = C_in;
     V_Bounds = V_Bounds_in;
     PR = PR_in;
 }
            private void Calculate_Click(object sender, System.EventArgs e)
            {
                Pach_GetModel_Command Model = new Pach_GetModel_Command();

                Source[] Source;


                if (PachydermAc_PlugIn.Instance.Source(out Source) && !object.ReferenceEquals(FileLocation.SelectedPath, ""))
                {
                    Rhino.RhinoApp.WriteLine("Model geometry not specified... Exiting calculation...");
                }
                ParticleRays[] RTParticles = new ParticleRays[Source.Length];

                Calculate.Enabled = false;
                PachydermAc_PlugIn plugin = PachydermAc_PlugIn.Instance;
                Scene Sc;

                if (PachydermAc_PlugIn.Instance.Geometry_Spec() == 0)
                {
                    Sc = PachTools.Get_NURBS_Scene(0, (double)Air_Temp.Value, 0, 0, false);
                }
                else
                {
                    Sc = PachTools.Get_Poly_Scene(0, (double)Air_Temp.Value, 0, 0, false);
                }

                for (int i = 0; i < Source.Length; i++)
                {
                    if (Source != null)
                    {
                        List <Point3d> L = new List <Point3d>();
                        List <Rhino.Geometry.Point3d> L_RC = new List <Rhino.Geometry.Point3d>();
                        for (int j = 0; j < Source.Length; j++)
                        {
                            L.Add(Source[j].Origin());
                            L_RC.Add(new Rhino.Geometry.Point3d(Source[j].Origin().X, Source[j].Origin().Y, Source[j].Origin().Z));
                        }

                        if (plugin.Geometry_Spec() == 0)
                        {
                            Sc.partition(L_RC, 15);
                            RTParticles[i] = new ParticleRays(Source[i], Sc, (int)RT_Count.Value, CutOffLength());
                        }
                        else if (plugin.Geometry_Spec() == 1)
                        {
                            Sc.partition(L, 15);
                            RTParticles[i] = new ParticleRays(Source[i], Sc, (int)RT_Count.Value, CutOffLength());
                        }
                        RTParticles[i].Begin();
                    }
                    else
                    {
                        Rhino.RhinoApp.WriteLine("Model geometry not specified... Exiting calculation...");
                    }

                    Pachyderm_Acoustic.Visualization.Phonon P;
                    if (ParticleChoice.SelectedIndex == 0)
                    {
                        P = new Tetrahedron();
                    }
                    else if (ParticleChoice.SelectedIndex == 1)
                    {
                        P = new Icosahedron();
                    }
                    else
                    {
                        P = new Geodesic_sphere();
                    }
                    RenderParticles(RTParticles, (double)(Frame_Rate.Value * Seconds.Value), P);
                    Rhino.RhinoDoc.ActiveDoc.Views.Redraw();
                }
                Calculate.Enabled = true;
            }
            private void Preview_Click(object sender, EventArgs e)
            {
                Loop.Enabled       = false;
                ForwButton.Enabled = false;
                BackButton.Enabled = false;
                PachydermAc_PlugIn plugin = PachydermAc_PlugIn.Instance;

                if (PreviewDisplay != null)
                {
                    PreviewDisplay.Enabled = false;
                }

                max = (int)(Frame_Rate.Value * Seconds.Value);

                Point3d[] SPT;

                if (!plugin.SourceOrigin(out SPT))
                {
                    Rhino.RhinoApp.WriteLine("Model geometry not specified... Exiting calculation...");
                    return;
                }

                if (T != null && T.ThreadState == System.Threading.ThreadState.Running)
                {
                    T.Abort();
                }
                PreviewDisplay = null;

                Pach_GetModel_Command Model = Pach_GetModel_Command.Instance;

                if (object.ReferenceEquals(RoomSelection.SelectedItem, "Use Entire Model"))
                {
                    Model.Rel_Humidity = 50;   //(double)Rel_Humidity.Value;
                    Model.Air_Temp     = (double)Air_Temp.Value;
                    Model.Atm_pressure = 1000; //(double)Air_Pressure.Value;
                    Model.Atten_Choice = 0;    //Atten_Method.SelectedIndex;
                    Rhino.RhinoApp.RunScript("GetModel", false);
                }

                plugin.Source(out Source);

                if (MeshWave)
                {
                    for (int i = 0; i < Source.Length; i++)
                    {
                        Source[i] = new GeodesicMeshSource(plugin.GetSourceSWL(i), Source[i].Origin(), (int)RT_Count.Value, i);
                    }
                }

                ParticleRays[] RTParticles = new ParticleRays[Source.Length];
                List <Point3d> L           = new List <Point3d>();

                for (int i = 0; i < Source.Length; i++)
                {
                    L.Add(Source[i].Origin());
                }
                for (int j = 0; j < Source.Length; j++)
                {
                    if (plugin.Geometry_Spec() == 0)
                    {
                        Model.Ret_NURBS_Scene.partition(L);
                        RTParticles[j] = new ParticleRays(Source[j], Model.Ret_NURBS_Scene, (int)RT_Count.Value, CutOffLength());
                    }
                    else
                    {
                        Model.Ret_Mesh_Scene.partition(L);
                        RTParticles[j] = new ParticleRays(Source[j], Model.Ret_Mesh_Scene, (int)RT_Count.Value, CutOffLength());
                    }

                    RTParticles[j].Begin();
                }

                PreviewDisplay = new WaveConduit(RTParticles, scale, new double[2] {
                    (double)Param_Min.Value, (double)Param_Max.Value
                }, Model.Ret_Mesh_Scene);
                ForwButton.Enabled = true;
                BackButton.Enabled = true;
                Loop.Enabled       = true;
                Loop.Text          = "Stop";
                FC = new ForCall(Forw_proc);
                System.Threading.ParameterizedThreadStart St = new System.Threading.ParameterizedThreadStart(delegate { LoopStart((int)(this.Frame_Rate.Value * Seconds.Value)); });
                T = new System.Threading.Thread(St);
                T.Start();
            }
            private void Preview_Click(object sender, EventArgs e)
            {
                Loop.Enabled = false;
                ForwButton.Enabled = false;
                BackButton.Enabled = false;
                PachydermAc_PlugIn plugin = PachydermAc_PlugIn.Instance;

                if (PreviewDisplay != null) PreviewDisplay.Enabled = false;

                max = (int)(Frame_Rate.Value * Seconds.Value);

                Point3d[] SPT;

                if (!plugin.SourceOrigin(out SPT))
                {
                    Rhino.RhinoApp.WriteLine("Model geometry not specified... Exiting calculation...");
                    return;
                }

                if (T != null && T.ThreadState == System.Threading.ThreadState.Running) T.Abort();
                PreviewDisplay = null;

                Pach_GetModel_Command Model = Pach_GetModel_Command.Instance;
                if (object.ReferenceEquals(RoomSelection.SelectedItem, "Use Entire Model"))
                {
                    Model.Rel_Humidity = 50;//(double)Rel_Humidity.Value;
                    Model.Air_Temp = (double)Air_Temp.Value;
                    Model.Atm_pressure = 1000;//(double)Air_Pressure.Value;
                    Model.Atten_Choice = 0;//Atten_Method.SelectedIndex;
                    Rhino.RhinoApp.RunScript("GetModel", false);
                }

                plugin.Source(out Source);

                if (MeshWave)
                {
                    for (int i = 0; i < Source.Length; i++) Source[i] = new GeodesicMeshSource(plugin.GetSourceSWL(i), Source[i].Origin(), (int)RT_Count.Value, i);
                }

                ParticleRays[] RTParticles = new ParticleRays[Source.Length];
                List<Point3d> L = new List<Point3d>();
                for (int i = 0; i < Source.Length; i++)
                {
                    L.Add(Source[i].Origin());
                }
                for (int j = 0; j < Source.Length; j++)
                {
                    if (plugin.Geometry_Spec() == 0)
                    {
                        Model.Ret_NURBS_Scene.partition(L);
                        RTParticles[j] = new ParticleRays(Source[j], Model.Ret_NURBS_Scene, (int)RT_Count.Value, CutOffLength());
                    }
                    else
                    {
                        Model.Ret_Mesh_Scene.partition(L);
                        RTParticles[j] = new ParticleRays(Source[j], Model.Ret_Mesh_Scene, (int)RT_Count.Value, CutOffLength());
                    }

                    RTParticles[j].Begin();
                }

                PreviewDisplay = new WaveConduit(RTParticles, scale, new double[2] { (double)Param_Min.Value, (double)Param_Max.Value }, Model.Ret_Mesh_Scene);
                ForwButton.Enabled = true;
                BackButton.Enabled = true;
                Loop.Enabled = true;
                Loop.Text = "Stop";
                FC = new ForCall(Forw_proc);
                System.Threading.ParameterizedThreadStart St = new System.Threading.ParameterizedThreadStart(delegate { LoopStart((int)(this.Frame_Rate.Value * Seconds.Value)); });
                T = new System.Threading.Thread(St);
                T.Start();
            }
            private void RenderParticles(ParticleRays[] Rays, double total_Frames, Pachyderm_Acoustic.Visualization.Phonon P)
            {
                List<Guid> Particle_IDS = new List<Guid>();
                double Increment = CutOffLength() / (double)(Frame_Rate.Value * Seconds.Value);
                Point3d Point = default(Point3d);
                double PMin = (double)this.Param_Min.Value;
                double PMax = (double)this.Param_Max.Value;
                double PRange = PMax - PMin;

                for (int q = 0; q < (int)total_Frames; q++)
                {
                    for (int i = 0; i < Rays.Length; i++)
                    {
                        for (int p = 0; p < Rays[i].Count(); p++)
                        {
                            Point3d N;
                            double energy;
                            if (Rays[i].RayPt(p, Increment * q, 4, out energy, out N, out Point))
                            {
                                Vector3d V = new Vector3d(N - Point);
                                V.Unitize();
                                double SPL = AcousticalMath.SPL_Intensity(energy);
                                System.Drawing.Color C = scale.GetValue(SPL, PMin, PMax);
                                Mesh M = P.Generate(Point, V);
                                M.Scale((SPL - PMin) / PRange);
                                System.Guid PG = Rhino.RhinoDoc.ActiveDoc.Objects.AddMesh(M);
                                Rhino.RhinoDoc.ActiveDoc.Objects.Find(PG).Attributes.ObjectColor = C;
                                Particle_IDS.Add(PG);
                            }
                        }
                    }

                    string number;
                    if (q < 100)
                    {
                        if (q < 10) number = "00" + q.ToString();
                        else number = "0" + q.ToString();
                    }
                    else number = q.ToString();
                    Rhino.RhinoApp.RunScript("-ViewCaptureToFile " + Folder_Status.Text + "\\"[0] + "frame" + number + ".jpg Width=1280 Height=720 DrawGrid=No Enter", true);

                    //Clean Up Model
                    Rhino.RhinoDoc.ActiveDoc.Objects.Delete(Particle_IDS, true);
                    Particle_IDS.Clear();
                }
            }