/// <summary> /// Generates the octave code. /// </summary> /// <returns>The octave code.</returns> /// <param name="fbl">four-bar linkage to be used.</param> /// <param name="divisor">Number of steps in which the left angle-span is to be divided. Each angle is used to compute a configuration.</param> private static string GenerateOctaveCode(FourBarLinkage fbl, int divisor) { string result = TemporaryFileManager.GetTemporaryFilename(); using (TextWriter tw = File.CreateText(result)) { tw.WriteLine(DeclareVariable("a", fbl.a)); tw.WriteLine(DeclareVariable("b", fbl.b)); tw.WriteLine(DeclareVariable("c", fbl.c)); tw.WriteLine(DeclareVariable("d", fbl.d)); tw.Write(executable); tw.Write(string.Format("r = compute({0});", divisor)); foreach (var varName in new string[] { "alpha_left", "alpha_right", "beta_left", "beta_right" }) { tw.Write(string.Format("disp({0});", varName)); } tw.WriteLine("disp(r);"); tw.WriteLine("puts(\"----\\n\");"); tw.WriteLine(string.Format("disp(compute_trajectory({0},{1}, r));", fbl.PtX, fbl.PtY)); } return(result); }
/// <summary> /// Initialize the specified uri and all the parameters decoding the data received in the HTTP header. /// </summary> /// <param name="uri">URI this class responds to.</param> /// <param name="variables">Variables of the GET HTTP method.</param> /// <param name="postVariables">variables of the POST HTTP method.</param> public override void Initialize(string remoteIp, string uri, NameValueCollection variables, NameValueCollection postVariables) { this.RemoteIp = remoteIp; double a = GetVariableValue(3.0, "a", postVariables); double b = GetVariableValue(5.0, "b", postVariables); double c = GetVariableValue(4.0, "c", postVariables); double d = GetVariableValue(6.0, "d", postVariables); Scale = GetVariableValue(1.0, "scale", postVariables); if (postVariables.Get("zoomout") != null) { if (Scale < 16) { Scale *= 2; } } else if (postVariables.Get("zoomin") != null) { if (Scale > 0.125) { Scale /= 2; } } if (postVariables.Get("noform") != null) { IsForm = false; } else { IsForm = true; } fourBarLinkage = new FourBarLinkage(a, b, c, d); fourBarLinkage.PtX = GetVariableValue(0.0, "px", postVariables); fourBarLinkage.PtY = GetVariableValue(0.0, "py", postVariables); fourBarLinkage.ConfigurationComputer = new OctaveConfigurationComputer(); fourBarLinkage.ComputeConfigurations(); }
/// <summary> /// Compute configurations of the passed four bar linkage. /// The steps the method performs are the following. /// 1. Generate the octave code using the parameters defining the linkage. /// 2. Call octave and get the results back from the stdout stream. /// 3. Decode the results of the octave code storing the computed values into the object describing the instance of four-bar linkage to be simulated. /// </summary> /// <param name="device">Four-bar linkage whose configuration is to be computed.</param> public bool Compute(FourBarLinkage device) { System.Diagnostics.Process octave = new System.Diagnostics.Process(); octave.StartInfo.UseShellExecute = false; octave.StartInfo.CreateNoWindow = false; octave.StartInfo.FileName = "octave"; string tempFilename = GenerateOctaveCode(device, 300); octave.StartInfo.Arguments = "-qf " + tempFilename; octave.StartInfo.RedirectStandardInput = true; octave.StartInfo.RedirectStandardError = true; octave.StartInfo.RedirectStandardOutput = true; // Launch the octave process octave.Start(); string output = octave.StandardOutput.ReadToEnd(); string error = octave.StandardError.ReadToEnd(); if (error.Length > 0) { // If there is something in the stderr file it means that there is some error. // The stdout content as well as the octave source code (with the line numbers) are shown string[] code = File.ReadAllLines(tempFilename); for (int i = 0; i < code.Length; i++) { Console.WriteLine("{0,4} {1}", i + 1, code [i]); } Console.WriteLine("Error: " + error); } int rowCounter = 0; bool bSaveConfig = true; //Console.WriteLine ("output " + output); using (TextWriter tw = File.CreateText("./output.txt")) { tw.Write(output); } // The results are sent back trough the stdout stream // The sequence is as follows // 1. The extrema angles // 2. The configuration (list of alpha, beta angles) // 3. The trajectory (list of P (x,y) coordinates) foreach (var line in output.Split('\n')) { if (rowCounter++ < 4) { double val = double.Parse(line); // Console.WriteLine (string.Format ("{0} - {1}", rowCounter, val)); switch (rowCounter) { case 1: device.alpha_left = val; break; case 2: device.alpha_right = val; break; case 3: device.beta_left = val; break; case 4: device.beta_right = val; break; } } else { if (line.StartsWith("----")) { bSaveConfig = false; } var ss = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (ss.Length < 2) { continue; } if (bSaveConfig) { double alpha = double.Parse(ss [0]); double beta = double.Parse(ss [1]); device.AddConfiguration(alpha, beta); } else { double x = double.Parse(ss [0]); double y = double.Parse(ss [1]); device.AddPoint(x, y); } } } TemporaryFileManager.DeleteTemporaryFile(tempFilename); return(true); }
/// <summary> /// Initialize the specified uri and all the parameters decoding the data received in the HTTP header. /// </summary> /// <param name="uri">URI this class responds to.</param> /// <param name="variables">Variables of the GET HTTP method.</param> /// <param name="postVariables">variables of the POST HTTP method.</param> public override void Initialize(string remoteIp, string uri, NameValueCollection variables, NameValueCollection postVariables) { this.RemoteIp = remoteIp; double a = GetVariableValue (3.0, "a", postVariables); double b = GetVariableValue (5.0, "b", postVariables); double c = GetVariableValue (4.0, "c", postVariables); double d = GetVariableValue (6.0, "d", postVariables); Scale = GetVariableValue (1.0, "scale", postVariables); if (postVariables.Get("zoomout") != null) { if (Scale < 16) { Scale *= 2; } } else if (postVariables.Get("zoomin") != null) { if (Scale > 0.125) { Scale /= 2; } } if (postVariables.Get ("noform") != null) { IsForm = false; } else { IsForm = true; } fourBarLinkage = new FourBarLinkage (a, b, c, d); fourBarLinkage.PtX = GetVariableValue (0.0, "px", postVariables); fourBarLinkage.PtY = GetVariableValue (0.0, "py", postVariables); fourBarLinkage.ConfigurationComputer = new OctaveConfigurationComputer (); fourBarLinkage.ComputeConfigurations (); }
/// <summary> /// Generates the octave code. /// </summary> /// <returns>The octave code.</returns> /// <param name="fbl">four-bar linkage to be used.</param> /// <param name="divisor">Number of steps in which the left angle-span is to be divided. Each angle is used to compute a configuration.</param> private static string GenerateOctaveCode(FourBarLinkage fbl, int divisor) { string result = TemporaryFileManager.GetTemporaryFilename (); using (TextWriter tw = File.CreateText (result)) { tw.WriteLine (DeclareVariable("a", fbl.a)); tw.WriteLine (DeclareVariable("b", fbl.b)); tw.WriteLine (DeclareVariable("c", fbl.c)); tw.WriteLine (DeclareVariable("d", fbl.d)); tw.Write (executable); tw.Write (string.Format("r = compute({0});", divisor)); foreach (var varName in new string[] {"alpha_left", "alpha_right", "beta_left", "beta_right"}) { tw.Write (string.Format("disp({0});", varName)); } tw.WriteLine ("disp(r);"); tw.WriteLine ("puts(\"----\\n\");"); tw.WriteLine (string.Format("disp(compute_trajectory({0},{1}, r));", fbl.PtX, fbl.PtY)); } return result; }
/// <summary> /// Compute configurations of the passed four bar linkage. /// The steps the method performs are the following. /// 1. Generate the octave code using the parameters defining the linkage. /// 2. Call octave and get the results back from the stdout stream. /// 3. Decode the results of the octave code storing the computed values into the object describing the instance of four-bar linkage to be simulated. /// </summary> /// <param name="device">Four-bar linkage whose configuration is to be computed.</param> public bool Compute(FourBarLinkage device) { System.Diagnostics.Process octave = new System.Diagnostics.Process (); octave.StartInfo.UseShellExecute = false; octave.StartInfo.CreateNoWindow = false; octave.StartInfo.FileName = "octave"; string tempFilename = GenerateOctaveCode (device, 300); octave.StartInfo.Arguments = "-qf " + tempFilename; octave.StartInfo.RedirectStandardInput = true; octave.StartInfo.RedirectStandardError = true; octave.StartInfo.RedirectStandardOutput = true; // Launch the octave process octave.Start (); string output = octave.StandardOutput.ReadToEnd (); string error = octave.StandardError.ReadToEnd (); if (error.Length > 0) { // If there is something in the stderr file it means that there is some error. // The stdout content as well as the octave source code (with the line numbers) are shown string[] code = File.ReadAllLines (tempFilename); for (int i = 0; i < code.Length; i++) { Console.WriteLine ("{0,4} {1}", i + 1, code [i]); } Console.WriteLine ("Error: " + error); } int rowCounter =0; bool bSaveConfig = true; //Console.WriteLine ("output " + output); using (TextWriter tw = File.CreateText ("./output.txt")) { tw.Write (output); } // The results are sent back trough the stdout stream // The sequence is as follows // 1. The extrema angles // 2. The configuration (list of alpha, beta angles) // 3. The trajectory (list of P (x,y) coordinates) foreach (var line in output.Split('\n')) { if (rowCounter++ < 4) { double val = double.Parse (line); // Console.WriteLine (string.Format ("{0} - {1}", rowCounter, val)); switch (rowCounter) { case 1: device.alpha_left = val; break; case 2: device.alpha_right = val; break; case 3: device.beta_left = val; break; case 4: device.beta_right = val; break; } } else { if (line.StartsWith("----")) { bSaveConfig = false; } var ss = line.Split (new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (ss.Length < 2) { continue; } if (bSaveConfig) { double alpha = double.Parse (ss [0]); double beta = double.Parse (ss [1]); device.AddConfiguration (alpha, beta); } else { double x = double.Parse (ss [0]); double y = double.Parse (ss [1]); device.AddPoint (x, y); } } } TemporaryFileManager.DeleteTemporaryFile(tempFilename); return true; }