Converts a line selection into an area (polygon or composite) selection. Author: Michael Schmid
(Roi line)
| 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) { |
no test coverage detected