1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
package ij.gui;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import ij.*;
import ij.plugin.frame.Recorder;
import ij.process.FloatPolygon;
import ij.measure.Calibration;
/** This class implements the rotated rectangle selection tool. */
public class RotatedRectRoi extends PolygonRoi {
private double xstart, ystart;
private static double DefaultRectWidth = 50;
private double rectWidth = DefaultRectWidth;
public RotatedRectRoi(double x1, double y1, double x2, double y2, double rectWidth) {
super(new float[4], new float[4], 4, FREEROI);
this.rectWidth = rectWidth;
makeRectangle(x1, y1, x2, y2);
state = NORMAL;
bounds = null;
}
public RotatedRectRoi(int sx, int sy, ImagePlus imp) {
super(sx, sy, imp);
type = FREEROI;
xstart = offScreenXD(sx);
ystart = offScreenYD(sy);
ImageWindow win = imp.getWindow();
int pixels = win!=null?(int)(win.getSize().height/win.getCanvas().getMagnification()):imp.getHeight();
if (IJ.debugMode) IJ.log("RotatedRectRoi: "+(int)rectWidth+" "+pixels);
if (rectWidth>pixels)
rectWidth = pixels/3;
setDrawOffset(false);
bounds = null;
}
public void draw(Graphics g) {
super.draw(g);
if (!overlay && ic!=null) {
double mag = ic.getMagnification();
for (int i=0; i<4; i++) {
if (i==3) //mark starting point
handleColor = strokeColor!=null?strokeColor:ROIColor;
else
handleColor=Color.white;
drawHandle(g, hxs(i), hys(i));
}
}
}
private int hxs(int index) {
int indexPlus1 = index<3?index+1:0;
return xp2[index]+(xp2[indexPlus1]-xp2[index])/2;
}
private int hys(int index) {
int indexPlus1 = index<3?index+1:0;
return yp2[index]+(yp2[indexPlus1]-yp2[index])/2;
}
private double hx(int index) {
int indexPlus1 = index<3?index+1:0;
return xpf[index]+(xpf[indexPlus1]-xpf[index])/2+x;
}
private double hy(int index) {
int indexPlus1 = index<3?index+1:0;
return ypf[index]+(ypf[indexPlus1]-ypf[index])/2+y;
}
protected void grow(int sx, int sy) {
double x1 = xstart;
double y1 = ystart;
double x2 = offScreenXD(sx);
double y2 = offScreenYD(sy);
makeRectangle(x1, y1, x2, y2);
imp.draw();
}
void makeRectangle(double x1, double y1, double x2, double y2) {
double length = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
double wsa = 0.5*rectWidth*(y2-y1)/length;
double wca = -0.5*rectWidth*(x2-x1)/length;
nPoints = 4;
xpf[3] = (float)(x1-wsa);
ypf[3] = (float)(y1-wca);
xpf[0] = (float)(x1+wsa);
ypf[0] = (float)(y1+wca);
xpf[1] = (float)(x2+wsa);
ypf[1] = (float)(y2+wca);
xpf[2] = (float)(x2-wsa);
ypf[2] = (float)(y2-wca);
makePolygonRelative();
cachedMask = null;
DefaultRectWidth = rectWidth;
showStatus();
}
public void showStatus() {
double[] p = getParams();
double dx = p[2] - p[0];
double dy = p[3] - p[1];
double length = Math.sqrt(dx*dx+dy*dy);
double width = p[4];
if (imp!=null && !IJ.altKeyDown()) {
Calibration cal = imp.getCalibration();
if (cal.scaled() && cal.pixelWidth==cal.pixelHeight) {
dx *= cal.pixelWidth;
dy *= cal.pixelHeight;
length = Math.sqrt(dx*dx+dy*dy);
width = p[4]*cal.pixelWidth;
}
}
double angle = getFloatAngle(p[0], p[1], p[2], p[3]);
IJ.showStatus("length=" + IJ.d2s(length)+", width=" + IJ.d2s(width)+", angle=" + IJ.d2s(angle));
}
public void nudgeCorner(int key) {
if (ic==null) return;
double[] p = getParams();
double x1 = p[0];
double y1 = p[1];
double x2 = p[2];
double y2 = p[3];
double inc = 1.0/ic.getMagnification();
switch(key) {
case KeyEvent.VK_UP: y2-=inc; break;
case KeyEvent.VK_DOWN: y2+=inc; break;
case KeyEvent.VK_LEFT: x2-=inc; break;
case KeyEvent.VK_RIGHT: x2+=inc; break;
}
makeRectangle(x1, y1, x2, y2);
imp.draw();
notifyListeners(RoiListener.MOVED);
showStatus();
}
void makePolygonRelative() {
FloatPolygon poly = new FloatPolygon(xpf, ypf, nPoints);
Rectangle r = poly.getBounds();
x = r.x;
y = r.y;
width = r.width;
height = r.height;
bounds = null;
for (int i=0; i<nPoints; i++) {
xpf[i] = xpf[i]-x;
ypf[i] = ypf[i]-y;
}
}
protected void handleMouseUp(int screenX, int screenY) {
nPoints = 4;
state = NORMAL;
if (Recorder.record) {
double[] p = getParams();
if (Recorder.scriptMode())
Recorder.recordCall("imp.setRoi(new RotatedRectRoi("+(int)p[0]+","+(int)p[1]+","+(int)p[2]+","+(int)p[3]+","+(int)p[4]+"));");
else
Recorder.record("makeRotatedRectangle", (int)p[0], (int)p[1], (int)p[2], (int)p[3], (int)p[4]);
}
}
protected void moveHandle(int sx, int sy) {
double ox = offScreenXD(sx);
double oy = offScreenYD(sy);
double x1 = hx(3);
double y1 = hy(3);
double x2 = hx(1);
double y2 = hy(1);
switch(activeHandle) {
case 0:
double dx = hx(2) - ox;
double dy = hy(2) - oy;
rectWidth = Math.sqrt(dx*dx+dy*dy);
break;
case 1:
x2 = ox;
y2 = oy;
break;
case 2:
dx = hx(0) - ox;
dy = hy(0) - oy;
rectWidth = Math.sqrt(dx*dx+dy*dy);
break;
case 3:
x1 = ox;
y1 = oy;
break;
}
makeRectangle(x1, y1, x2, y2);
imp.draw();
}
public int isHandle(int sx, int sy) {
int size = getHandleSize()+5;
int halfSize = size/2;
int index = -1;
for (int i=0; i<4; i++) {
int sx2 = (int)Math.round(hxs(i)-halfSize), sy2=(int)Math.round(hys(i)-halfSize);
if (sx>=sx2 && sx<=sx2+size && sy>=sy2 && sy<=sy2+size) {
index = i;
break;
}
}
return index;
}
/** Returns x1, y1, x2, y2 and width as a 5 element array. */
public double[] getParams() {
double[] params = new double[5];
params[0] = hx(3);
params[1] = hy(3);
params[2] = hx(1);
params[3] = hy(1);
params[4] = rectWidth;
return params;
}
/** Always returns true. */
public boolean subPixelResolution() {
return true;
}
}
|