private void ReverseWinding() { //reverse the boundary int cnt = bndLine.Count; CBndPt[] arr = new CBndPt[cnt]; cnt--; bndLine.CopyTo(arr); bndLine.Clear(); for (int i = cnt; i >= 0; i--) { arr[i].heading -= Math.PI; if (arr[i].heading < 0) { arr[i].heading += glm.twoPI; } bndLine.Add(arr[i]); } }
public void CalculateBoundaryHeadings() { //to calc heading based on next and previous points to give an average heading. int cnt = bndLine.Count; CBndPt[] arr = new CBndPt[cnt]; cnt--; bndLine.CopyTo(arr); bndLine.Clear(); //first point needs last, first, second points CBndPt pt3 = arr[0]; pt3.heading = Math.Atan2(arr[1].easting - arr[cnt].easting, arr[1].northing - arr[cnt].northing); if (pt3.heading < 0) { pt3.heading += glm.twoPI; } bndLine.Add(pt3); //middle points for (int i = 1; i < cnt; i++) { pt3 = arr[i]; pt3.heading = Math.Atan2(arr[i + 1].easting - arr[i - 1].easting, arr[i + 1].northing - arr[i - 1].northing); if (pt3.heading < 0) { pt3.heading += glm.twoPI; } bndLine.Add(pt3); } //last and first point pt3 = arr[cnt]; pt3.heading = Math.Atan2(arr[0].easting - arr[cnt - 1].easting, arr[0].northing - arr[cnt - 1].northing); if (pt3.heading < 0) { pt3.heading += glm.twoPI; } bndLine.Add(pt3); }
private void btnLoadBoundaryFromGE_Click(object sender, EventArgs e) { string fileAndDirectory; { //create the dialog instance OpenFileDialog ofd = new OpenFileDialog { //set the filter to text KML only Filter = "KML files (*.KML)|*.KML", //the initial directory, fields, for the open dialog InitialDirectory = mf.fieldsDirectory + mf.currentFieldDirectory }; //was a file selected if (ofd.ShowDialog() == DialogResult.Cancel) { return; } else { fileAndDirectory = ofd.FileName; } } //start to read the file string line = null; int index; using (System.IO.StreamReader reader = new System.IO.StreamReader(fileAndDirectory)) { //int count = 0; //while (!reader.EndOfStream) //{ // line = reader.ReadLine(); // index = line.IndexOf("<coordinates>"); // if (index != -1) count++; //} //reader.DiscardBufferedData(); //reader.BaseStream.Seek(0, SeekOrigin.Begin); //reader.BaseStream.Position = 0; bool done = false; try { while (!reader.EndOfStream && !done) { line = reader.ReadLine(); index = line.IndexOf("coord"); if (index != -1) { line = reader.ReadLine(); line = line.Trim(); char[] delimiterChars = { ' ', '\t', '\r', '\n' }; string[] numberSets = line.Split(delimiterChars); done = true; //at least 3 points if (numberSets.Length > 2) { //reset boundary mf.bnd.bndArr[mf.bnd.boundarySelected].ResetBoundary(); foreach (var item in numberSets) { string[] fix = item.Split(','); double.TryParse(fix[0], NumberStyles.Float, CultureInfo.InvariantCulture, out lonK); double.TryParse(fix[1], NumberStyles.Float, CultureInfo.InvariantCulture, out latK); double[] xy = mf.pn.DecDeg2UTM(latK, lonK); //match new fix to current position easting = xy[0] - mf.pn.utmEast; northing = xy[1] - mf.pn.utmNorth; //fix the azimuth error easting = (Math.Cos(-mf.pn.convergenceAngle) * easting) - (Math.Sin(-mf.pn.convergenceAngle) * northing); northing = (Math.Sin(-mf.pn.convergenceAngle) * easting) + (Math.Cos(-mf.pn.convergenceAngle) * northing); //add the point to boundary CBndPt bndPt = new CBndPt(easting, northing, 0); mf.bnd.bndArr[mf.bnd.boundarySelected].bndLine.Add(bndPt); } //fix the points if there are gaps bigger then mf.bnd.bndArr[mf.bnd.boundarySelected].CalculateBoundaryHeadings(); mf.bnd.bndArr[mf.bnd.boundarySelected].PreCalcBoundaryLines(); mf.bnd.bndArr[mf.bnd.boundarySelected].FixBoundaryLine(mf.bnd.boundarySelected); //boundary area, pre calcs etc mf.bnd.bndArr[mf.bnd.boundarySelected].CalculateBoundaryArea(); mf.bnd.bndArr[mf.bnd.boundarySelected].PreCalcBoundaryLines(); mf.bnd.bndArr[mf.bnd.boundarySelected].isSet = true; { mf.FileSaveBoundary(); } } else { mf.TimedMessageBox(2000, "Error reading KML", "Choose or Build a Different one"); } } } UpdateChart(); cboxSelectBoundary.Enabled = true; } catch (Exception) { return; } } }
public void FixBoundaryLine(int bndNum) { //count the points from the boundary int ptCount = bndLine.Count; //first find out which side is inside the boundary double oneSide = glm.PIBy2; vec3 point = new vec3(bndLine[3].easting - (Math.Sin(oneSide + bndLine[3].heading) * 2.0), bndLine[3].northing - (Math.Cos(oneSide + bndLine[3].heading) * 2.0), 0.0); double spacing = 4; //make sure boundaries are wound correctly if (bndNum == 0) { //outside an outer boundary means its wound clockwise if (!IsPointInsideBoundary(point)) { ReverseWinding(); } } else { //inside an inner boundary means its wound clockwise if (IsPointInsideBoundary(point)) { ReverseWinding(); } spacing = 3; } //make sure distance isn't too small between points on headland int bndCount = bndLine.Count; double distance; for (int i = 0; i < bndCount - 1; i++) { distance = glm.Distance(bndLine[i], bndLine[i + 1]); if (distance < spacing) { bndLine.RemoveAt(i + 1); bndCount = bndLine.Count; i--; } } //make sure distance isn't too big between points on boundary bndCount = bndLine.Count; spacing *= 1.33; for (int i = 0; i < bndCount; i++) { int j = i + 1; if (j == bndCount) { j = 0; } distance = glm.Distance(bndLine[i], bndLine[j]); if (distance > spacing) { CBndPt pointB = new CBndPt((bndLine[i].easting + bndLine[j].easting) / 2.0, (bndLine[i].northing + bndLine[j].northing) / 2.0, bndLine[i].heading); bndLine.Insert(j, pointB); bndCount = bndLine.Count; i--; } } //make sure distance isn't too big between points on boundary bndCount = bndLine.Count; spacing *= 1.33; for (int i = 0; i < bndCount; i++) { int j = i + 1; if (j == bndCount) { j = 0; } distance = glm.Distance(bndLine[i], bndLine[j]); if (distance > spacing) { CBndPt pointB = new CBndPt((bndLine[i].easting + bndLine[j].easting) / 2.0, (bndLine[i].northing + bndLine[j].northing) / 2.0, bndLine[i].heading); bndLine.Insert(j, pointB); bndCount = bndLine.Count; i--; } } //make sure headings are correct for calculated points CalculateBoundaryHeadings(); }
public static double Distance(CBndPt first, CTurnPt second) { return(Math.Sqrt( Math.Pow(first.easting - second.easting, 2) + Math.Pow(first.northing - second.northing, 2))); }
private void btnLoadBoundaryFromGE_Click(object sender, EventArgs e) { if (sender is Button button) { Selectedreset = true; btnLoadBoundaryFromGE.Enabled = false; btnDelete.Enabled = false; string fileAndDirectory; { //create the dialog instance OpenFileDialog ofd = new OpenFileDialog { //set the filter to text KML only Filter = "KML files (*.KML)|*.KML", //the initial directory, fields, for the open dialog InitialDirectory = mf.fieldsDirectory + mf.currentFieldDirectory }; //was a file selected if (ofd.ShowDialog() == DialogResult.Cancel) { return; } else { fileAndDirectory = ofd.FileName; } } //start to read the file string line = null; string coordinates = null; int startIndex; int i = 0; using (System.IO.StreamReader reader = new System.IO.StreamReader(fileAndDirectory)) { if (button.Name == "btnLoadMultiBoundaryFromGE") { ResetAllBoundary(); } else { i = mf.bnd.boundarySelected; } try { while (!reader.EndOfStream) { line = reader.ReadLine(); startIndex = line.IndexOf("<coordinates>"); if (startIndex != -1) { while (true) { int endIndex = line.IndexOf("</coordinates>"); if (endIndex == -1) { //just add the line if (startIndex == -1) { coordinates = coordinates + line.Substring(0); } else { coordinates = coordinates + line.Substring(startIndex + 13); } } else { if (startIndex == -1) { coordinates = coordinates + line.Substring(0, endIndex); } else { coordinates = coordinates + line.Substring(startIndex + 13, endIndex - (startIndex + 13)); } break; } line = reader.ReadLine(); line = line.Trim(); startIndex = -1; } line = coordinates; char[] delimiterChars = { ' ', '\t', '\r', '\n' }; string[] numberSets = line.Split(delimiterChars); //at least 3 points if (numberSets.Length > 2) { mf.bnd.bndArr.Add(new CBoundaryLines()); mf.turn.turnArr.Add(new CTurnLines()); mf.gf.geoFenceArr.Add(new CGeoFenceLines()); foreach (var item in numberSets) { string[] fix = item.Split(','); double.TryParse(fix[0], NumberStyles.Float, CultureInfo.InvariantCulture, out lonK); double.TryParse(fix[1], NumberStyles.Float, CultureInfo.InvariantCulture, out latK); double[] xy = mf.pn.DecDeg2UTM(latK, lonK); //match new fix to current position easting = xy[0] - mf.pn.utmEast; northing = xy[1] - mf.pn.utmNorth; double east = easting; double nort = northing; //fix the azimuth error easting = (Math.Cos(-mf.pn.convergenceAngle) * east) - (Math.Sin(-mf.pn.convergenceAngle) * nort); northing = (Math.Sin(-mf.pn.convergenceAngle) * east) + (Math.Cos(-mf.pn.convergenceAngle) * nort); //add the point to boundary CBndPt bndPt = new CBndPt(easting, northing, 0); mf.bnd.bndArr[i].bndLine.Add(bndPt); } //fix the points if there are gaps bigger then mf.bnd.bndArr[i].CalculateBoundaryHeadings(); mf.bnd.bndArr[i].PreCalcBoundaryLines(); mf.bnd.bndArr[i].FixBoundaryLine(i, mf.vehicle.toolWidth); //boundary area, pre calcs etc mf.bnd.bndArr[i].CalculateBoundaryArea(); mf.bnd.bndArr[i].PreCalcBoundaryLines(); mf.bnd.bndArr[i].isSet = true; //if (i == 0) mf.bnd.bndArr[i].isOwnField = true; //else mf.bnd.bndArr[i].isOwnField = false; coordinates = ""; i++; } else { mf.TimedMessageBox(2000, gStr.gsErrorreadingKML, gStr.gsChooseBuildDifferentone); } if (button.Name == "btnLoadBoundaryFromGE") { break; } } } mf.FileSaveBoundary(); mf.fd.UpdateFieldBoundaryGUIAreas(); UpdateChart(); } catch (Exception) { return; } } } }