/// <summary> /// reads a file and populates the map receiver instance. /// </summary> /// <returns></returns> public static bool Read_pachm(out Mapping.PachMapReceiver[] Map) { System.Windows.Forms.OpenFileDialog of = new System.Windows.Forms.OpenFileDialog(); of.DefaultExt = ".pachm"; of.AddExtension = true; of.Filter = "Pachyderm Mapping Data File (*.pachm)|*.pachm|" + "All Files|"; if (of.ShowDialog() != System.Windows.Forms.DialogResult.OK) { Map = null; return false; } System.IO.BinaryReader sr = new System.IO.BinaryReader(System.IO.File.Open(of.FileName, System.IO.FileMode.Open)); //1. Write calculation type. (string) string CalcType = sr.ReadString(); if (CalcType != "Type;Map_Data" && CalcType != "Type;Map_Data_NoDir") throw new Exception("Map Data File Expected"); bool Directional = (CalcType == "Type;Map_Data"); //2. Write the number of samples in each histogram. (int) int SampleCT = (int)sr.ReadUInt32(); //3. Write the sample rate. (int) int SampleRate = (int)sr.ReadUInt32(); //4. Write the number of Receivers (int) int Rec_CT = (int)sr.ReadUInt32(); //4.5 Write the version number double version = 1.1; double rev = 0; //5. Announce that the following data pertains to the form of the analysis mesh. (string) int s_ct=1; Rhino.Geometry.Mesh Map_Mesh = new Rhino.Geometry.Mesh(); Map = new Mapping.PachMapReceiver[1]; //Map[0] = new Pach_Map_Receiver(); //double[] Rho_C = null; double[] delay; do { switch (sr.ReadString()) { case "Version": //Pach1.7 = Versioning functionality added. string v = sr.ReadString(); version = double.Parse(v.Substring(0, 3)); rev = int.Parse(v.Split(new char[1] { '.' })[3]); break; case "Mesh Information": //6. Announce Mesh Vertices (string) //Write the number of vertices & faces (int) (int) if (sr.ReadString() != "Mesh Vertices") throw new Exception("Mesh Vertices Expected"); int VC = (int)sr.ReadUInt32(); int FC = (int)sr.ReadUInt32(); for (int i = 0; i < VC; i++) { //Write Vertex: (double) (double) (double) Map_Mesh.Vertices.Add(new Rhino.Geometry.Point3d(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle())); } //7. Announce Mesh Faces (string) if (sr.ReadString() != "Mesh Faces") throw new Exception("Mesh Faces Expected"); for (int i = 0; i < FC; i++) { // Write mesh vertex indices: (int) (int) (int) (int) Map_Mesh.Faces.AddFace((int)sr.ReadUInt32(), (int)sr.ReadUInt32(), (int)sr.ReadUInt32(), (int)sr.ReadUInt32()); } break; case "Sources": //7.5: Announce the number of sources. s_ct = sr.ReadInt32(); delay = new double[s_ct]; Map = new Mapping.PachMapReceiver[s_ct]; //7.5a Announce the type of source. for (int s = 0; s < s_ct; s++) { Map[s] = new Mapping.PachMapReceiver(); Map[s].CutOffTime = (double)SampleCT / (double)SampleRate; Map[s].SampleCT = SampleCT; Map[s].SampleRate = SampleRate; Map[s].Map_Mesh = Map_Mesh; Map[s].Rec_List = new Mapping.PachMapReceiver.Map_Receiver[Rec_CT]; Map[s].SrcType = sr.ReadString(); //4.4 Source delay (ms) if (version > 2.0 || (version == 2.0 && rev >= 1)) { delay[s] = sr.ReadDouble(); } } break; case "SourceswLoc": //7.5: Announce the number of sources. s_ct = sr.ReadInt32(); delay = new double[s_ct]; Map = new Mapping.PachMapReceiver[s_ct]; //7.5a Announce the type of source. for (int s = 0; s < s_ct; s++) { Map[s] = new Mapping.PachMapReceiver(); Map[s].CutOffTime = (double)SampleCT / (double)SampleRate * 1000; Map[s].SampleCT = SampleCT; Map[s].SampleRate = SampleRate; Map[s].Map_Mesh = Map_Mesh; Map[s].Rec_List = new Mapping.PachMapReceiver.Map_Receiver[Rec_CT]; Map[s].Src = new Rhino.Geometry.Point3d(sr.ReadDouble(), sr.ReadDouble(), sr.ReadDouble()); Map[s].SrcType = sr.ReadString(); //4.4 Source delay (ms) if (version > 2.0 || (version == 2.0 && rev >= 1)) { delay[s] = sr.ReadDouble(); } } break; case "Receiver Hit Data": if (Map[0] == null) { Map = new Mapping.PachMapReceiver[1]; Map[0] = new Mapping.PachMapReceiver(); Map[0].CutOffTime = (double)SampleCT / (double)SampleRate; Map[0].SampleRate = SampleRate; Map[0].SampleCT = SampleCT; Map[0].Map_Mesh = Map_Mesh; Map[0].Rec_List = new Mapping.PachMapReceiver.Map_Receiver[Rec_CT]; Map[0].SrcType = "Geodesic"; } //8. Announce that the following data pertains to the receiver histograms (string) //8a. Announce whether or not data is linked to vertices rather than faces (bool) bool vert_Receiver = sr.ReadBoolean(); for (int s = 0; s < s_ct; s++) { Map[s].Rec_Vertex = vert_Receiver; for (int i = 0; i < Map[s].Rec_List.Length; i++) { //for version 1.7 and up, write direct sound arrival time. //Write Receiver Index (int) int j = (int)sr.ReadUInt32(); //Write Direct Sound Arrival Time. double Direct_Time; if (version >= 1.7) Direct_Time = sr.ReadDouble(); else Direct_Time = (Utilities.PachTools.RPttoHPt(Map[s].Src) - Map[s].Rec_List[i].H_Origin).Length() / 343f; //Write Impedance of Air double Rho_C = version >= 2.0 ? sr.ReadDouble() : 400; if (vert_Receiver) { Map[s].Rec_List[i] = new Mapping.PachMapReceiver.Map_Receiver(Map_Mesh.Vertices[i], new Rhino.Geometry.Point3f((float)Map[s].Src.X, (float)Map[s].Src.Y, (float)Map[s].Src.Z), Direct_Time, Rho_C, i, SampleRate, SampleCT, Directional); } else { Rhino.Geometry.Point3d RecLoc = Map_Mesh.Faces.GetFaceCenter(i); Map[s].Rec_List[i] = new Mapping.PachMapReceiver.Map_Receiver(new Rhino.Geometry.Point3f((float)RecLoc.X, (float)RecLoc.Y, (float)RecLoc.Z), new Rhino.Geometry.Point3f((float)Map[s].Src.X, (float)Map[s].Src.Y, (float)Map[s].Src.Z), Direct_Time, Rho_C, i, SampleRate, SampleCT, Directional); } for (int Octave = 0; Octave < 8; Octave++) { //Write Octave (int) int Oct_out = (int)sr.ReadUInt32(); if (Oct_out != Octave) throw new Exception(string.Format("Octave {0} Expected", Octave)); double[] Hist = Map[s].Rec_List[i].GetEnergyHistogram(Octave); if (Directional) { if (version < 1.7) { for (int e = 0; e < SampleCT; e++) Map[s].Rec_List[i].Combine_Sample(e, sr.ReadDouble(), new Hare.Geometry.Vector(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()), new Hare.Geometry.Vector(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()), Octave); } else { for (int e = 0; e < SampleCT; e++) Map[s].Rec_List[i].Combine_Sample(e, sr.ReadDouble(), new Hare.Geometry.Vector(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()), new Hare.Geometry.Vector(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()), Octave); } } else { if (version < 1.7) { for (int e = 0; e < SampleCT; e++) Map[s].Rec_List[i].Combine_Sample(e, sr.ReadDouble(), new Hare.Geometry.Vector(0, 0, 0), new Hare.Geometry.Vector(0, 0, 0), Octave); } else { for (int e = 0; e < SampleCT; e++) Map[s].Rec_List[i].Combine_Sample(e, sr.ReadDouble(), new Hare.Geometry.Vector(0, 0, 0), new Hare.Geometry.Vector(0,0,0), Octave); } } } if (sr.ReadString() != "End_Receiver_Hits") throw new Exception("End of Receiver Hits Expected"); } } break; case "End_of_File": sr.Close(); return true; } } while (true); throw new Exception("Unsuccessful Read"); }
/// <summary> /// reads a file and populates the map receiver instance. /// </summary> /// <returns></returns> public static bool Read_pachm(string path, ref Mapping.PachMapReceiver[] Map) { System.IO.BinaryReader sr = new System.IO.BinaryReader(System.IO.File.Open(path, System.IO.FileMode.Open)); //1. Write calculation type. (string) string CalcType = sr.ReadString(); if (CalcType != "Type;Map_Data" && CalcType != "Type;Map_Data_NoDir") { throw new Exception("Map Data File Expected"); } bool Directional = (CalcType == "Type;Map_Data"); //2. Write the number of samples in each histogram. (int) int SampleCT = (int)sr.ReadUInt32(); //3. Write the sample rate. (int) int SampleRate = (int)sr.ReadUInt32(); //4. Write the number of Receivers (int) int Rec_CT = (int)sr.ReadUInt32(); //4.5 Write the version number double version = 1.1; double rev = 0; //5. Announce that the following data pertains to the form of the analysis mesh. (string) int s_ct = 1; Rhino.Geometry.Mesh Map_Mesh = new Rhino.Geometry.Mesh(); Map = new Mapping.PachMapReceiver[1]; //Map[0] = new Pach_Map_Receiver(); //double[] Rho_C = null; double[] delay; do { switch (sr.ReadString()) { case "Version": //Pach1.7 = Versioning functionality added. string v = sr.ReadString(); version = double.Parse(v.Substring(0, 3)); rev = int.Parse(v.Split(new char[1] { '.' })[3]); break; case "Mesh Information": //6. Announce Mesh Vertices (string) //Write the number of vertices & faces (int) (int) if (sr.ReadString() != "Mesh Vertices") { throw new Exception("Mesh Vertices Expected"); } int VC = (int)sr.ReadUInt32(); int FC = (int)sr.ReadUInt32(); for (int i = 0; i < VC; i++) { //Write Vertex: (double) (double) (double) Map_Mesh.Vertices.Add(new Rhino.Geometry.Point3d(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle())); } //7. Announce Mesh Faces (string) if (sr.ReadString() != "Mesh Faces") { throw new Exception("Mesh Faces Expected"); } for (int i = 0; i < FC; i++) { // Write mesh vertex indices: (int) (int) (int) (int) Map_Mesh.Faces.AddFace((int)sr.ReadUInt32(), (int)sr.ReadUInt32(), (int)sr.ReadUInt32(), (int)sr.ReadUInt32()); } break; case "Sources": //7.5: Announce the number of sources. s_ct = sr.ReadInt32(); delay = new double[s_ct]; Map = new Mapping.PachMapReceiver[s_ct]; //7.5a Announce the type of source. for (int s = 0; s < s_ct; s++) { Map[s] = new Mapping.PachMapReceiver(); Map[s].CutOffTime = (double)SampleCT / (double)SampleRate; Map[s].SampleCT = SampleCT; Map[s].SampleRate = SampleRate; Map[s].Map_Mesh = Map_Mesh; Map[s].Rec_List = new Mapping.PachMapReceiver.Map_Receiver[Rec_CT]; Map[s].SrcType = sr.ReadString(); //4.4 Source delay (ms) if (version > 2.0 || (version == 2.0 && rev >= 1)) { delay[s] = sr.ReadDouble(); } Map[s].SWL = new double[8]; if (version > 2.0 || (version == 2.0 && rev >= 2)) { for (int j = 0; j < 8; j++) { Map[s].SWL[j] = sr.ReadDouble(); //v.2.0.0.2 } } else { Map[s].SWL = new double[8] { 120, 120, 120, 120, 120, 120, 120, 120 } }; } break; case "SourceswLoc": //7.5: Announce the number of sources. s_ct = sr.ReadInt32(); delay = new double[s_ct]; Map = new Mapping.PachMapReceiver[s_ct]; //7.5a Announce the type of source. for (int s = 0; s < s_ct; s++) { Map[s] = new Mapping.PachMapReceiver(); Map[s].CutOffTime = (double)SampleCT / (double)SampleRate * 1000; Map[s].SampleCT = SampleCT; Map[s].SampleRate = SampleRate; Map[s].Map_Mesh = Map_Mesh; Map[s].Rec_List = new Mapping.PachMapReceiver.Map_Receiver[Rec_CT]; Map[s].Src = new Rhino.Geometry.Point3d(sr.ReadDouble(), sr.ReadDouble(), sr.ReadDouble()); Map[s].SrcType = sr.ReadString(); //4.4 Source delay (ms) if (version > 2.0 || (version == 2.0 && rev >= 1)) { delay[s] = sr.ReadDouble(); } Map[s].SWL = new double[8]; if (version > 2.0 || (version == 2.0 && rev >= 2)) { for (int j = 0; j < 8; j++) { Map[s].SWL[j] = sr.ReadDouble(); //v.2.0.0.2 } } else { Map[s].SWL = new double[8] { 120, 120, 120, 120, 120, 120, 120, 120 } }; } break; case "Receiver Hit Data": if (Map[0] == null) { Map = new Mapping.PachMapReceiver[1]; Map[0] = new Mapping.PachMapReceiver(); Map[0].CutOffTime = (double)SampleCT / (double)SampleRate; Map[0].SampleRate = SampleRate; Map[0].SampleCT = SampleCT; Map[0].Map_Mesh = Map_Mesh; Map[0].Rec_List = new Mapping.PachMapReceiver.Map_Receiver[Rec_CT]; Map[0].SrcType = "Geodesic"; } //8. Announce that the following data pertains to the receiver histograms (string) //8a. Announce whether or not data is linked to vertices rather than faces (bool) bool vert_Receiver = sr.ReadBoolean(); for (int s = 0; s < s_ct; s++) { Map[s].Rec_Vertex = vert_Receiver; for (int i = 0; i < Map[s].Rec_List.Length; i++) { //for version 1.7 and up, write direct sound arrival time. //Write Receiver Index (int) int j = (int)sr.ReadUInt32(); //Write Direct Sound Arrival Time. double Direct_Time; if (version >= 1.7) { Direct_Time = sr.ReadDouble(); } else { Direct_Time = (Utilities.PachTools.RPttoHPt(Map[s].Src) - Map[s].Rec_List[i].H_Origin).Length() / 343f; } //Write Impedance of Air double Rho_C = version >= 2.0 ? sr.ReadDouble() : 400; if (vert_Receiver) { Map[s].Rec_List[i] = new Mapping.PachMapReceiver.Map_Receiver(Map_Mesh.Vertices[i], new Rhino.Geometry.Point3f((float)Map[s].Src.X, (float)Map[s].Src.Y, (float)Map[s].Src.Z), Direct_Time, Rho_C, i, SampleRate, SampleCT, Directional); } else { Rhino.Geometry.Point3d RecLoc = Map_Mesh.Faces.GetFaceCenter(i); Map[s].Rec_List[i] = new Mapping.PachMapReceiver.Map_Receiver(new Rhino.Geometry.Point3f((float)RecLoc.X, (float)RecLoc.Y, (float)RecLoc.Z), new Rhino.Geometry.Point3f((float)Map[s].Src.X, (float)Map[s].Src.Y, (float)Map[s].Src.Z), Direct_Time, Rho_C, i, SampleRate, SampleCT, Directional); } for (int Octave = 0; Octave < 8; Octave++) { //Write Octave (int) int Oct_out = (int)sr.ReadUInt32(); if (Oct_out != Octave) { throw new Exception(string.Format("Octave {0} Expected", Octave)); } double[] Hist = Map[s].Rec_List[i].GetEnergyHistogram(Octave); if (Directional) { if (version < 1.7) { for (int e = 0; e < SampleCT; e++) { Map[s].Rec_List[i].Combine_Sample(e, sr.ReadDouble(), new Hare.Geometry.Vector(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()), new Hare.Geometry.Vector(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()), Octave); } } else { for (int e = 0; e < SampleCT; e++) { Map[s].Rec_List[i].Combine_Sample(e, sr.ReadDouble(), new Hare.Geometry.Vector(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()), new Hare.Geometry.Vector(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()), Octave); } } } else { if (version < 1.7) { for (int e = 0; e < SampleCT; e++) { Map[s].Rec_List[i].Combine_Sample(e, sr.ReadDouble(), new Hare.Geometry.Vector(0, 0, 0), new Hare.Geometry.Vector(0, 0, 0), Octave); } } else { for (int e = 0; e < SampleCT; e++) { Map[s].Rec_List[i].Combine_Sample(e, sr.ReadDouble(), new Hare.Geometry.Vector(0, 0, 0), new Hare.Geometry.Vector(0, 0, 0), Octave); } } } } if (sr.ReadString() != "End_Receiver_Hits") { throw new Exception("End of Receiver Hits Expected"); } } } break; case "End_of_File": sr.Close(); return(true); } } while (true); throw new Exception("Unsuccessful Read"); }