internal static Polygon2d GetBoundingBoxfromLines(List <Line2d> lineList) { if (lineList == null) { return(null); } List <Point2d> ptList = new List <Point2d>(); for (int i = 0; i < lineList.Count; i++) { ptList.Add(LineUtility.LineMidPoint(lineList[i])); } return(new Polygon2d(FromPointsGetBoundingPoly(ptList))); }
//removes duplicate lines from a list, based on the lines from another list internal static List <Line2d> RemoveDuplicateLinesBasedOnMidPt(List <Line2d> lineListOrig, List <Line2d> otherLineList) { List <Line2d> lineEditedList = new List <Line2d>(); for (int i = 0; i < lineListOrig.Count; i++) { lineEditedList.Add(lineListOrig[i]); } List <bool> duplicateTagList = new List <bool>(); for (int i = 0; i < lineListOrig.Count; i++) { bool duplicate = false; for (int j = 0; j < otherLineList.Count; j++) { Point2d midPtOrig = LineUtility.LineMidPoint(lineListOrig[i]); Point2d midPtOther = LineUtility.LineMidPoint(otherLineList[j]); //if (midPtOrig.Compare(midPtOther)) { duplicate = true; break; } if (ValidateObject.CheckPointsWithinRange(midPtOrig, midPtOther, 4)) { duplicate = true; break; } } duplicateTagList.Add(duplicate); } int count = 0; for (int i = 0; i < duplicateTagList.Count; i++) { if (duplicateTagList[i]) { lineEditedList.RemoveAt(i - count); count += 1; } } return(lineEditedList); }
//removes duplicate lines from a list, based on line adjacency check internal static List <Line2d> RemoveDuplicateLinesBasedOnAdjacency(List <Line2d> lineListOrig, List <Line2d> otherLineList) { List <Line2d> lineEditedList = new List <Line2d>(); for (int i = 0; i < lineListOrig.Count; i++) { lineEditedList.Add(lineListOrig[i]); } List <bool> duplicateTagList = new List <bool>(); for (int i = 0; i < lineListOrig.Count; i++) { bool duplicate = false; for (int j = 0; j < otherLineList.Count; j++) { Point2d midPtOrig = LineUtility.LineMidPoint(lineListOrig[i]); Point2d midPtOther = LineUtility.LineMidPoint(otherLineList[j]); if (GraphicsUtility.LineAdjacencyCheck(lineListOrig[i], otherLineList[j])) { duplicate = true; break; } } duplicateTagList.Add(duplicate); } int count = 0; for (int i = 0; i < duplicateTagList.Count; i++) { if (duplicateTagList[i]) { lineEditedList.RemoveAt(i - count); count += 1; } } return(lineEditedList); }
internal static Dictionary <string, object> FindOuterLinesAndOffsets(Polygon2d poly, double patientRoomDepth = 16, double extension = 8000, double recompute = 5) { if (!ValidateObject.CheckPoly(poly)) { return(null); } Polygon2d polyReg = new Polygon2d(poly.Points); List <Line2d> hLines = new List <Line2d>(); List <Line2d> vLines = new List <Line2d>(); List <Point2d> hMidPt = new List <Point2d>(); List <Point2d> vMidPt = new List <Point2d>(); List <Line2d> nonOrthoLines = new List <Line2d>(); int countOrtho = 0, countNonOrtho = 0; for (int i = 0; i < polyReg.Points.Count; i++) { int a = i, b = i + 1; if (i == polyReg.Points.Count - 1) { b = 0; } Line2d line = new Line2d(polyReg.Points[a], polyReg.Points[b]); int lineType = ValidateObject.CheckLineOrient(line); if (lineType > -1) { if (lineType == 0) { Line2d extendedLine = LineUtility.ExtendLine(line, extension); hLines.Add(extendedLine); hMidPt.Add(LineUtility.LineMidPoint(line)); } if (lineType == 1) { Line2d extendedLine = LineUtility.ExtendLine(line, extension); vLines.Add(extendedLine); vMidPt.Add(LineUtility.LineMidPoint(line)); } countOrtho += 1; } else { countNonOrtho += 1; nonOrthoLines.Add(line); } } List <Line2d> selectedHLines = new List <Line2d>(); List <Line2d> selectedVLines = new List <Line2d>(); int hIndLow = CodeToBeTested.ReturnLowestPointFromList(hMidPt); int hIndHigh = CodeToBeTested.ReturnHighestPointFromList(hMidPt); int vIndLow = PointUtility.LowestPointFromList(vMidPt); int vIndHigh = PointUtility.HighestPointFromList(vMidPt); if (hIndLow > -1) { selectedHLines.Add(hLines[hIndLow]); } if (hIndHigh > -1) { selectedHLines.Add(hLines[hIndHigh]); } if (vIndLow > -1) { selectedVLines.Add(vLines[vIndLow]); } if (vIndHigh > -1) { selectedVLines.Add(vLines[vIndHigh]); } List <Line2d> allSplitLines = new List <Line2d>(); allSplitLines.AddRange(selectedHLines); allSplitLines.AddRange(selectedVLines); List <double> splitLineLength = new List <double>(); for (int i = 0; i < allSplitLines.Count; i++) { splitLineLength.Add(allSplitLines[i].Length); } List <int> sortedIndices = BasicUtility.Quicksort(splitLineLength); if (sortedIndices != null) { sortedIndices.Reverse(); } List <Line2d> offsetLines = new List <Line2d>(); List <Point2d> midPtsOffsets = new List <Point2d>(); for (int i = 0; i < allSplitLines.Count; i++) { offsetLines.Add(LineUtility.Offset(allSplitLines[i], patientRoomDepth)); midPtsOffsets.Add(LineUtility.NudgeLineMidPt(allSplitLines[i], poly, patientRoomDepth)); } List <Line2d> offsetSortedLines = new List <Line2d>(); for (int i = 0; i < offsetLines.Count; i++) { offsetSortedLines.Add(offsetLines[sortedIndices[i]]); } return(new Dictionary <string, object> { { "SplittableLines", (allSplitLines) }, { "OffsetLines", (offsetSortedLines) }, { "SortedIndices", (sortedIndices) }, { "OffsetMidPts", (midPtsOffsets) }, { "NonOrthoLines", (nonOrthoLines) } }); }
internal static Dictionary <string, object> AddPointToFitPoly(Polygon2d poly, Polygon2d containerPoly, double distance = 16, double area = 0, double thresDistance = 10, double recompute = 5) { if (!ValidateObject.CheckPoly(poly)) { return(null); } if (distance < 1) { return(null); } Dictionary <string, object> lineOffsetCheckObj = ValidateObject.CheckLinesOffsetInPoly(poly, containerPoly, distance); List <int> indicesFalse = (List <int>)lineOffsetCheckObj["IndicesFalse"]; List <List <Point2d> > pointsFalse = (List <List <Point2d> >)lineOffsetCheckObj["PointsOutside"]; List <Point2d> probPointList = new List <Point2d>(); List <Point2d> polyNewPoints = new List <Point2d>(); List <Line2d> falseLines = new List <Line2d>(); Point2d ptNewEnd = new Point2d(0, 0); Point2d otherPt = new Point2d(0, 0); Point2d probPt = new Point2d(0, 0); Line2d line = new Line2d(ptNewEnd, otherPt); int count = 0, maxTry = 50; double ratio = 0, increment = .25; bool added = false, checkOffPtNew = false; for (int i = 0; i < poly.Points.Count; i++) { int a = i, b = i + 1; if (i == poly.Points.Count - 1) { b = 0; } polyNewPoints.Add(poly.Points[a]); if (indicesFalse[i] > -1) { falseLines.Add(poly.Lines[i]); } if (poly.Lines[i].Length > thresDistance && indicesFalse[i] > -1 && pointsFalse[i] != null && pointsFalse[i].Count == 1 && !added && LayoutUtility.CheckLineGetsExternalWall(poly.Lines[i], containerPoly)) { probPointList.AddRange(pointsFalse[i]); probPt = pointsFalse[i][0]; line = poly.Lines[i]; Point2d midPt = LineUtility.LineMidPoint(line); if (line.StartPoint.Compare(probPt)) { otherPt = line.EndPoint; } else { otherPt = line.StartPoint; } Vector2d vecToOther = new Vector2d(probPt, otherPt); while (!checkOffPtNew && count < maxTry && ratio < 0.9) { ratio += increment; ptNewEnd = VectorUtility.VectorAddToPoint(probPt, vecToOther, ratio); Point2d offPtNew = LineUtility.OffsetLinePointInsidePoly(line, ptNewEnd, poly, distance); checkOffPtNew = GraphicsUtility.PointInsidePolygonTest(poly, offPtNew); count += 1; } polyNewPoints.Add(ptNewEnd); added = true; } } Polygon2d polyAdded = new Polygon2d(polyNewPoints, 0); return(new Dictionary <string, object> { { "PolyAddedPts", (polyAdded) }, { "ProblemPoint", (probPt) }, { "IsAdded", (added) }, { "PointAdded", (ptNewEnd) }, { "Trials", (count) }, { "FinalRatio", (ratio) }, { "ProblemLine", (line) }, { "ProblemPtsList", (probPointList) }, { "FalseLineList", (falseLines) } }); }
public static Dictionary <string, object> MakeProgCirculationPolys(List <Line2d> circulationNetwork, List <Polygon2d> polyProgList, double circulationWidth = 8, double circulationFrequency = 0.5, int designSeed = 10) { if (!ValidateObject.CheckPolyList(polyProgList)) { return(null); } if (circulationNetwork == null || circulationNetwork.Count == 0) { return(null); } List <Line2d> flatLineList = new List <Line2d>(); List <bool> IsDuplicateList = new List <bool>(); polyProgList = PolygonUtility.SmoothPolygonList(polyProgList, 5); //flatten all the polys in each depts to make it one list List <Polygon2d> circulationPolyList = new List <Polygon2d>(); List <Polygon2d> updatedProgPolyList = new List <Polygon2d>(); List <int> deptIdList = new List <int>(); double allowedCircRatio = 4; List <double> areaProgPolyList = new List <double>(); for (int i = 0; i < polyProgList.Count; i++) { areaProgPolyList.Add(PolygonUtility.AreaPolygon(polyProgList[i])); } double maxArea = areaProgPolyList.Max(); areaProgPolyList.Sort(); int value = (int)(areaProgPolyList.Count / 3); double areaThresh = areaProgPolyList[value]; Random ran = new Random(designSeed); for (int i = 0; i < circulationNetwork.Count; i++) { Line2d splitter = circulationNetwork[i]; double someNumber = BasicUtility.RandomBetweenNumbers(ran, 1, 0); if (someNumber > circulationFrequency) { continue; } for (int j = 0; j < polyProgList.Count; j++) { Polygon2d progPoly = polyProgList[j]; double areaPoly = PolygonUtility.AreaPolygon(progPoly); Point2d midPt = LineUtility.LineMidPoint(splitter); Point2d nudgedMidPt = LineUtility.NudgeLineMidPt(splitter, progPoly, 0.5); bool checkInside = GraphicsUtility.PointInsidePolygonTest(progPoly, nudgedMidPt); if (checkInside) { Dictionary <string, object> splitResult = SplitObject.SplitByLine(progPoly, splitter, circulationWidth); List <Polygon2d> polyAfterSplit = (List <Polygon2d>)(splitResult["PolyAfterSplit"]); if (ValidateObject.CheckPolyList(polyAfterSplit)) { double areaA = PolygonUtility.AreaPolygon(polyAfterSplit[0]), areaB = PolygonUtility.AreaPolygon(polyAfterSplit[1]); if (areaA < areaB) { if (polyAfterSplit[0].Points != null) { if (ValidateObject.CheckPolyBBox(polyAfterSplit[0], allowedCircRatio)) { circulationPolyList.Add(polyAfterSplit[0]); } } updatedProgPolyList.Add(polyAfterSplit[1]); } else { if (polyAfterSplit[1].Points != null) { if (ValidateObject.CheckPolyBBox(polyAfterSplit[1], allowedCircRatio)) { circulationPolyList.Add(polyAfterSplit[1]); } } updatedProgPolyList.Add(polyAfterSplit[0]); } } // end of if loop checking polylist } // end of check inside } // end of for loop j } // end of for loop i return(new Dictionary <string, object> { { "ProgCirculationPoly", (circulationPolyList) } }); }
public static Dictionary <string, object> MakeDeptCirculationPolys(List <DeptData> deptData, List <List <Line2d> > circulationNetwork, double circulationWidth = 8) { if (deptData == null || deptData.Count == 0 || circulationNetwork == null || circulationNetwork.Count == 0) { return(null); } List <Line2d> cleanLineList = LineUtility.FlattenLine2dList(circulationNetwork); List <Polygon2d> allDeptPolyList = new List <Polygon2d>(); List <Polygon2d> circulationPolyList = new List <Polygon2d>(); List <Polygon2d> updatedDeptPolyList = new List <Polygon2d>(); List <int> deptIdList = new List <int>(); for (int i = 0; i < deptData.Count; i++) { List <Polygon2d> deptPolyList = deptData[i].PolyAssignedToDept; if (!ValidateObject.CheckPolyList(deptPolyList)) { continue; } for (int j = 0; j < deptPolyList.Count; j++) { deptIdList.Add(i); allDeptPolyList.Add(deptPolyList[j]); } } for (int i = 0; i < cleanLineList.Count; i++) { Line2d splitter = cleanLineList[i]; for (int j = 0; j < allDeptPolyList.Count; j++) { Polygon2d deptPoly = allDeptPolyList[j]; Point2d midPt = LineUtility.LineMidPoint(splitter); Point2d nudgedMidPt = LineUtility.NudgeLineMidPt(splitter, deptPoly, 0.5); if (GraphicsUtility.PointInsidePolygonTest(deptPoly, nudgedMidPt)) { Dictionary <string, object> splitResult = SplitObject.SplitByLine(deptPoly, splitter, circulationWidth); List <Polygon2d> polyAfterSplit = (List <Polygon2d>)(splitResult["PolyAfterSplit"]); if (ValidateObject.CheckPolyList(polyAfterSplit)) { double areaA = PolygonUtility.AreaPolygon(polyAfterSplit[0]); double areaB = PolygonUtility.AreaPolygon(polyAfterSplit[1]); if (areaA < areaB) { circulationPolyList.Add(polyAfterSplit[0]); updatedDeptPolyList.Add(polyAfterSplit[1]); } else { circulationPolyList.Add(polyAfterSplit[1]); updatedDeptPolyList.Add(polyAfterSplit[0]); } } } // end of check inside } // end of for loop j } // end of for loop i List <List <Polygon2d> > deptPolyBranchedInList = new List <List <Polygon2d> >(); List <int> distinctIdList = deptIdList.Distinct().ToList(); for (int i = 0; i < distinctIdList.Count; i++) { List <Polygon2d> polyForDeptBranch = new List <Polygon2d>(); for (int j = 0; j < deptIdList.Count; j++) { if (deptIdList[j] == i) { if (j < updatedDeptPolyList.Count) { polyForDeptBranch.Add(updatedDeptPolyList[j]); } } } deptPolyBranchedInList.Add(polyForDeptBranch); } return(new Dictionary <string, object> { { "DeptCirculationPoly", (circulationPolyList) }, { "UpdatedDeptPolys", (deptPolyBranchedInList) } }); }
//Builds Dept Topology Matrix , finds all the shared edges between dept polys, and updates department polygon2d's. /// <summary> /// Builds the department topology matrix internally and finds circulation network lines between department polygon2d's. /// </summary> /// <param name="deptData">DeptData Object</param> /// <param name="leftOverPoly">Polygon2d not assigned to any department.</param> /// <param name="limit">Maximum distance allowed to be considered as a neighbor of a department.</param> /// <returns name="CirculationNetwork">List of line2d geometry representing circulation network between department polygon2d's.</returns> /// <search> /// Department Circulation Network, Shared Edges between departments /// </search> internal static List <Line2d> RemoveNetworkRedundancy(List <Line2d> networkLines, double circulationFrequency = 10) { if (networkLines == null) { return(null); } circulationFrequency = 1 - circulationFrequency; Polygon2d bBox = ReadData.GetBoundingBoxfromLines(networkLines); List <double> spans = PolygonUtility.GetSpansXYFromPolygon2d(bBox.Points);// horizontal span 1st, then vertical span List <Line2d> horizLines = new List <Line2d>(), vertLines = new List <Line2d>(); for (int i = 0; i < networkLines.Count; i++) { if (ValidateObject.CheckLineOrient(networkLines[i]) == 0) { horizLines.Add(networkLines[i]); } else if (ValidateObject.CheckLineOrient(networkLines[i]) == 1) { vertLines.Add(networkLines[i]); } } List <Line2d> selectedHorizLines = new List <Line2d>(), selectedVertLines = new List <Line2d>(); for (int i = 0; i < horizLines.Count; i++) { double thresDistance = circulationFrequency * spans[0]; int a = i, b = i + 1; if (i == horizLines.Count - 1) { b = 0; } Point2d midA = LineUtility.LineMidPoint(horizLines[a]), midB = LineUtility.LineMidPoint(horizLines[b]); double xDist = Math.Abs(midA.Y - midB.Y); if (xDist > thresDistance) { selectedHorizLines.Add(horizLines[a]); } } for (int i = 0; i < vertLines.Count; i++) { double thresDistance = circulationFrequency * spans[1]; int a = i, b = i + 1; if (i == vertLines.Count - 1) { b = 0; } Point2d midA = LineUtility.LineMidPoint(vertLines[a]), midB = LineUtility.LineMidPoint(vertLines[b]); double yDist = Math.Abs(midA.X - midB.X); if (yDist > thresDistance) { selectedVertLines.Add(vertLines[a]); } } List <Line2d> reducedLines = new List <Line2d>(); reducedLines.AddRange(selectedHorizLines); reducedLines.AddRange(selectedVertLines); return(reducedLines); }
//removes duplicates lines from a list of lines public static List <Line2d> CleanLines(List <Line2d> lineList) { List <Line2d> cleanList = new List <Line2d>(); List <bool> taggedList = new List <bool>(); for (int i = 0; i < lineList.Count; i++) { Line2d lin = new Line2d(lineList[i].StartPoint, lineList[i].EndPoint); cleanList.Add(lin); taggedList.Add(false); } for (int i = 0; i < lineList.Count; i++) { double eps = 1; Line2d lineA = lineList[i]; for (int j = i + 1; j < lineList.Count; j++) { Line2d lineB = lineList[j]; int orientA = ValidateObject.CheckLineOrient(lineA); int orientB = ValidateObject.CheckLineOrient(lineB); if (orientA != orientB) { continue; } else { Point2d midA = LineUtility.LineMidPoint(lineA); Point2d midB = LineUtility.LineMidPoint(lineB); if (orientA == 0) { //lines are horizontal if ((midA.Y - eps < midB.Y && midB.Y < midA.Y + eps) || (midB.Y - eps < midA.Y && midA.Y < midB.Y + eps)) { // lines are duplicate check length, whichever has longer length will be added to list double lenA = lineA.Length; double lenB = lineB.Length; if (lenA > lenB) { taggedList[i] = true; } else { taggedList[j] = true; } }// end of if statement } else { //lines are vertical if ((midA.X - eps < midB.X && midB.X < midA.X + eps) || (midB.X - eps < midA.X && midA.X < midB.X + eps)) { double lenA = lineA.Length; double lenB = lineB.Length; if (lenA > lenB) { cleanList.Add(lineA); } else { cleanList.Add(lineB); } }// end of if statement } } } } return(cleanList); }