MCPcopy Create free account
hub / github.com/AstroImageJ/astroimagej / convertLineToArea

Method convertLineToArea

ij/src/main/java/ij/gui/Roi.java:2794–2895  ·  view source on GitHub ↗

Converts a line selection into an area (polygon or composite) selection. Author: Michael Schmid

(Roi line)

Source from the content-addressed store, hash-verified

2792 * Author: Michael Schmid
2793 */
2794 public static Roi convertLineToArea(Roi line) {
2795 if (line==null || !line.isLine())
2796 throw new IllegalArgumentException("Line selection required");
2797 double lineWidth = line.getStrokeWidth();
2798 if (lineWidth<1.0)
2799 lineWidth = 1.0;
2800 Roi roi2 = null;
2801 if (line.getType()==Roi.LINE) {
2802 FloatPolygon p = ((Line)line).getFloatPoints();
2803 roi2 = new RotatedRectRoi(p.xpoints[0],p.ypoints[0],p.xpoints[1],p.ypoints[1],lineWidth);
2804 line.setStrokeWidth(lineWidth);
2805 } else {
2806 Rectangle bounds = line.getBounds();
2807 double width = bounds.x+bounds.width + lineWidth;
2808 double height = bounds.y+bounds.height + lineWidth;
2809 ByteProcessor ip = new ByteProcessor((int)Math.round(width), (int)Math.round(height));
2810 PolygonFiller polygonFiller = new PolygonFiller();
2811 //ip.setColor(255);
2812 double radius = lineWidth/2.0;
2813 FloatPolygon p = line.getFloatPolygon();
2814 int n = p.npoints;
2815 float[] xv = new float[4]; //vertex points of rectangle will be filled for each line segment
2816 float[] yv = new float[4];
2817 float[] xt = new float[3]; //vertex points of triangle will be filled between line segments
2818 float[] yt = new float[3];
2819 double dx1 = p.xpoints[1]-p.xpoints[0];
2820 double dy1 = p.ypoints[1]-p.ypoints[0];
2821 double l = length(dx1, dy1);
2822 dx1 = dx1/l; //unit vector along current line segment
2823 dy1 = dy1/l;
2824 double dx0 = dx1;
2825 double dy0 = dy1;
2826 double xfrom = p.xpoints[0] - 0.5*dx1;
2827 double yfrom = p.ypoints[0] - 0.5*dy1;
2828 //Overlay ovly = new Overlay();
2829 for (int i=1; i<n; i++) { //line segment from point i-1 ("from") to point i ("to")
2830 double xto = p.xpoints[i];
2831 double yto = p.ypoints[i];
2832 if (i==n-1) {
2833 xto += 0.5*dx1;
2834 yto += 0.5*dy1;
2835 }
2836 xv[0] = (float)(xfrom + radius*dy1);
2837 yv[0] = (float)(yfrom - radius*dx1);
2838 xv[1] = (float)(xfrom - radius*dy1);
2839 yv[1] = (float)(yfrom + radius*dx1);
2840 xv[2] = (float)(xto - radius*dy1);
2841 yv[2] = (float)(yto + radius*dx1);
2842 xv[3] = (float)(xto + radius*dy1);
2843 yv[3] = (float)(yto - radius*dx1);
2844 polygonFiller.setPolygon(xv, yv, 4, 0.5f, 0.5f); //offset 0.5 pxl: line vs area coordinate convention
2845 polygonFiller.fillByteProcessorMask(ip);
2846 //ovly.add(new PolygonRoi(xv,yv,Roi.POLYGON));
2847 if (i>1) { //fill triangle to previous line segment
2848 boolean rightTurn=(dx1*dy0>dx0*dy1);
2849 xt[0] = (float)xfrom;
2850 yt[0] = (float)yfrom;
2851 if (rightTurn) {

Callers 9

lineToAreaMethod · 0.95
combineRoisMethod · 0.95
fillMethod · 0.95
measureLengthMethod · 0.95
roiToShapeMethod · 0.95
getContainedPointsMethod · 0.95
convertToPolygonMethod · 0.95
RoiPointsIteratorMaskMethod · 0.95

Calls 15

setStrokeWidthMethod · 0.95
lengthMethod · 0.95
setPolygonMethod · 0.95
fillByteProcessorMaskMethod · 0.95
setThresholdMethod · 0.95
convertMethod · 0.95
transferPropertiesMethod · 0.95
getStrokeColorMethod · 0.95
setStrokeColorMethod · 0.95
getStrokeWidthMethod · 0.80
getFloatPointsMethod · 0.80
getRedMethod · 0.80

Tested by

no test coverage detected