/// <summary> /// Draw a polygon onto a bool array using a scanline algorithm /// </summary> /// <param name="polygon"></param> /// <returns></returns> private bool[] drawPolygon2(PlanarPolygon polygon) { bool[] data = new bool[Rows * Columns]; // loop through each row and find intersecting x coords to draw between for (int row = 0; row < Rows; row++) { double[] xcoords = polygon.GetFixedYLineIntersections(GetCoordinate(row, YRange)); if (xcoords.Length > 0) { for (int i = 0; i < xcoords.Length - 1; i += 2) { int xi1 = GetIndex(xcoords[i], XRange); int xi2 = GetIndex(xcoords[i + 1], XRange); for (int col = xi1; col <= xi2; col++) { SetByRowCol(row, col, true, data); } } } } return(data); }
public void AddPolygon(PlanarPolygon polygon) { if (!hasPolygon) { XRange = polygon.XRange; YRange = polygon.YRange; Rows = (int)(polygon.YRange.Length / GridSpacing); Columns = (int)(polygon.XRange.Length / GridSpacing); // +2 to rows and cols to allow buffer of one row of false on top, left, bottom right. InsideBinaryData = new bool[(Rows) * (Columns)]; } // Draw the new polygon to a boolean array bool[] polygonData = drawPolygon2(polygon); int numTrue = polygonData.Count(x => x == true); int numOrigTrue = InsideBinaryData.Count(x => x == true); // Add the new polygon to the current inside binary data InsideBinaryData = BinaryMath.Xor(polygonData, InsideBinaryData); hasPolygon = true; }
public void AddPolygon(PlanarPolygon polygon) { Polygons.Add(polygon); }
public void Load(DicomFile[] files, StructureSet structureSet) { DicomFile file = files[0]; structureSet.Name = file.Dataset.Get <string>(DicomTag.StructureSetLabel, ""); Dictionary <int, string> roi_names = new Dictionary <int, string>(); DicomSequence structs = file.Dataset.Get <DicomSequence>(DicomTag.StructureSetROISequence); foreach (DicomDataset item in structs) { roi_names.Add(item.Get <int>(DicomTag.ROINumber), item.Get <string>(DicomTag.ROIName)); } List <RegionOfInterest> rois = new List <RegionOfInterest>(); DicomSequence s = file.Dataset.Get <DicomSequence>(DicomTag.ROIContourSequence); foreach (DicomDataset item in s.Items) { RegionOfInterest roi = new RegionOfInterest(); int[] color = item.Get <int[]>(DicomTag.ROIDisplayColor); roi.Color = DicomColor.FromRgb(color[0], color[1], color[2]); roi.ROINumber = item.Get <int>(DicomTag.ReferencedROINumber); if (roi_names.ContainsKey(roi.ROINumber)) { roi.Name = roi_names[roi.ROINumber]; } DicomSequence roi_definitions; try { roi_definitions = item.Get <DicomSequence>(DicomTag.ContourSequence); } catch (Exception e) { continue; } double xmin = double.MaxValue, ymin = double.MaxValue, zmin = double.MaxValue, xmax = double.MinValue, ymax = double.MinValue, zmax = double.MinValue; foreach (DicomDataset contourSlice in roi_definitions.Items) { int vertex_count = contourSlice.Get <int>(DicomTag.NumberOfContourPoints); double[] vertices = contourSlice.Get <double[]>(DicomTag.ContourData); PlanarPolygon poly = new PlanarPolygon(); // we divide the number of vertices here by 1.5 because we are going from a 3d poly to a 2d poly on the z plane poly.Vertices = new double[(int)(vertices.Length / 1.5)]; double zcoord = vertices[2]; int polyIndex = 0; RegionOfInterestSlice slice = roi.GetSlice(zcoord); if (slice == null) { slice = new RegionOfInterestSlice() { ZCoord = zcoord, } } ; for (int i = 0; i < vertices.Length; i += 3) { poly.Vertices[polyIndex] = vertices[i]; poly.Vertices[polyIndex + 1] = vertices[i + 1]; if (vertices[i] < xmin) { xmin = vertices[i]; } if (vertices[i] > xmax) { xmax = vertices[i]; } if (vertices[i + 1] < ymin) { ymin = vertices[i + 1]; } if (vertices[i + 1] > ymax) { ymax = vertices[i + 1]; } if (zcoord < zmin) { zmin = zcoord; } if (zcoord > zmax) { zmax = zcoord; } polyIndex += 2; } if (zmin < roi.ZRange.Minimum) { roi.ZRange.Minimum = zmin; } if (zmax > roi.ZRange.Maximum) { roi.ZRange.Maximum = zmax; } slice.AddPolygon(poly); roi.AddSlice(slice, zcoord); } roi.XRange = new Geometry.Range(xmin, xmax); roi.YRange = new Geometry.Range(ymin, ymax); roi.ZRange = new Geometry.Range(zmin, zmax); for (int i = 0; i < roi.RegionOfInterestSlices.Count; i++) { for (int j = 0; j < roi.RegionOfInterestSlices[i].Polygons.Count; j++) { roi.RegionOfInterestSlices[i].Polygons[j].XRange = new Geometry.Range(xmin, xmax); roi.RegionOfInterestSlices[i].Polygons[j].YRange = new Geometry.Range(ymin, ymax); } roi.RegionOfInterestSlices[i].ComputeBinaryMask(); } rois.Add(roi); } GC.Collect(); structureSet.ROIs = rois; } }