1 package com.github.celldynamics.quimp.plugin.utils;
2
3 import java.util.Collections;
4 import java.util.Vector;
5
6 import org.scijava.vecmath.Matrix3d;
7 import org.scijava.vecmath.Point3d;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11
12
13
14
15
16
17
18
19
20 class RectangleBox {
21
22 static final Logger LOGGER = LoggerFactory.getLogger(RectangleBox.class.getName());
23
24 private Vector<Double> px;
25 private Vector<Double> py;
26
27
28
29
30
31
32
33
34
35
36
37
38 @SuppressWarnings("unchecked")
39 public RectangleBox(Vector<Double> x, Vector<Double> y) throws IllegalArgumentException {
40 this.px = (Vector<Double>) x.clone();
41 this.py = (Vector<Double>) y.clone();
42
43 if (x.isEmpty() || y.isEmpty()) {
44 throw new IllegalArgumentException("Input vectors are empty");
45 }
46 if (x.size() != y.size()) {
47 throw new IllegalArgumentException("Input vectors are not equal");
48 }
49 double centerX = getAverage(x);
50 double centerY = getAverage(y);
51
52 for (int i = 0; i < x.size(); i++) {
53 this.px.set(i, x.get(i) - centerX);
54 this.py.set(i, y.get(i) - centerY);
55 }
56 }
57
58
59
60
61
62
63
64 public RectangleBox(double width, double height) {
65 px = new Vector<Double>();
66 py = new Vector<Double>();
67
68
69 px.add(-width / 2);
70 px.add(width / 2);
71 px.add(width / 2);
72 px.add(-width / 2);
73
74 py.add(height / 2);
75 py.add(height / 2);
76 py.add(-height / 2);
77 py.add(-height / 2);
78 }
79
80
81
82
83
84
85 public void rotateBoundingBox(double angle) {
86
87
88
89 double angleRad = angle * Math.PI / 180.0;
90
91
92 Matrix3d rot = new Matrix3d();
93
94
95 rot.rotZ(-angleRad);
96
97
98
99 Point3d[] cornerTable = new Point3d[4];
100 cornerTable[0] = new Point3d(px.get(0), py.get(0), 0);
101 cornerTable[1] = new Point3d(px.get(1), py.get(1), 0);
102 cornerTable[2] = new Point3d(px.get(2), py.get(2), 0);
103 cornerTable[3] = new Point3d(px.get(3), py.get(3), 0);
104
105 int i = 0;
106
107 for (Point3d p : cornerTable) {
108 rot.transform(p);
109 px.set(i, p.x);
110 py.set(i, p.y);
111 i++;
112 }
113 }
114
115
116
117
118
119
120 public double getWidth() {
121 return Math.abs(Collections.max(px) - Collections.min(px));
122 }
123
124
125
126
127
128
129 public double getHeight() {
130 return Math.abs(Collections.max(py) - Collections.min(py));
131 }
132
133
134
135
136
137
138
139 private double getAverage(Vector<Double> x) {
140 double sum = 0;
141 for (Double val : x) {
142 sum += val;
143 }
144 return sum / x.size();
145 }
146 }