@@ -70,7 +70,7 @@ public class RectangleIntersection {
7070 * Constructor getting list of boxes computed in autocrop class. Initialisation of a list of 2D rectangles and a
7171 * list of Z stack associated (zMin-zMax).
7272 *
73- * @param boxes List of boxes
73+ * @param boxes List of boxes
7474 * @param params Autocrop parameters
7575 */
7676 public RectangleIntersection (Map <Double , Box > boxes , AutocropParameters params ) {
@@ -114,12 +114,13 @@ public static double percentOf2Rectangles(Rectangle2D r1, Rectangle2D r2) {
114114 */
115115 public void runRectangleRecompilation () {
116116 this .newBoxesAdded = true ;
117- int tours = 0 ;
117+ int i = 0 ;
118118 while (newBoxesAdded ) {
119- tours ++;
120119 computeIntersection ();
121120 rectangleRegroup ();
122121 recompileRectangle ();
122+ i ++;
123+ LOGGER .trace ("Iteration {}: {} boxes detected" , i , listRectangle .size ());
123124 }
124125 }
125126
@@ -133,9 +134,7 @@ public void computeIntersection() {
133134 if (i != y &&
134135 !rectangleIntersect .contains (i + "-" + y ) &&
135136 !rectangleIntersect .contains (y + "-" + i )) {
136-
137137 if (listRectangle .get (i ).intersects (listRectangle .get (y ))) {
138-
139138 if (percentOf2Rectangles (listRectangle .get (i ), listRectangle .get (y )) >
140139 autocropParameters .getBoxesSurfacePercent () ||
141140 percentOf2Rectangles (listRectangle .get (y ), listRectangle .get (i )) >
@@ -211,28 +210,28 @@ public void recompileRectangle() {
211210 List <Rectangle > rectanglesToRemove = new ArrayList <>();
212211 for (String value : finalListRectangle ) {
213212 String [] splitList2 = value .split ("-" );
214- double xMixNewRectangle = 0 ;
215- double yMinNewRectangle = 0 ;
216- double maxWidth = 0 ;
217- double maxHeight = 0 ;
213+ int xMixNewRectangle = 0 ;
214+ int yMinNewRectangle = 0 ;
215+ int maxWidth = 0 ;
216+ int maxHeight = 0 ;
218217 int minZSlice = 0 ;
219218 int maxZSlice = 0 ;
220219 if (splitList2 .length > 1 ) {
221220 for (String s : splitList2 ) {
222221 int tmp = Integer .parseInt (s );
223222 if (listRectangle .get (tmp ).getX () < xMixNewRectangle || xMixNewRectangle == 0 ) {
224- xMixNewRectangle = listRectangle .get (tmp ).getX () ;
223+ xMixNewRectangle = listRectangle .get (tmp ).x ;
225224 }
226225 if (listRectangle .get (tmp ).getY () < yMinNewRectangle || yMinNewRectangle == 0 ) {
227- yMinNewRectangle = listRectangle .get (tmp ).getY () ;
226+ yMinNewRectangle = listRectangle .get (tmp ).y ;
228227 }
229228 if (listRectangle .get (tmp ).getX () + listRectangle .get (tmp ).getWidth () > maxWidth ||
230229 maxWidth == 0 ) {
231- maxWidth = listRectangle .get (tmp ).getX () + listRectangle .get (tmp ).getWidth () ;
230+ maxWidth = listRectangle .get (tmp ).x + listRectangle .get (tmp ).width ;
232231 }
233232 if (listRectangle .get (tmp ).getY () + listRectangle .get (tmp ).getHeight () > maxHeight ||
234233 maxHeight == 0 ) {
235- maxHeight = listRectangle .get (tmp ).getY () + listRectangle .get (tmp ).getHeight () ;
234+ maxHeight = listRectangle .get (tmp ).y + listRectangle .get (tmp ).height ;
236235 }
237236
238237 String [] zSliceTMP = zSlices .get (tmp ).split ("-" );
@@ -242,20 +241,14 @@ public void recompileRectangle() {
242241 if (Integer .parseInt (zSliceTMP [0 ] + Integer .valueOf (zSliceTMP [1 ])) > maxZSlice || maxZSlice == 0 ) {
243242 maxZSlice = Integer .parseInt (zSliceTMP [0 ]) + Integer .parseInt (zSliceTMP [1 ]);
244243 }
245- rectanglesToRemove .add (new Rectangle ((int ) listRectangle .get (tmp ).getX (),
246- (int ) listRectangle .get (tmp ).getY (),
247- (int ) listRectangle .get (tmp ).getWidth (),
248- (int ) listRectangle .get (tmp ).getHeight ()));
244+ rectanglesToRemove .add (new Rectangle (listRectangle .get (tmp )));
249245 }
250246
251247 maxZSlice -= minZSlice ;
252248 rectangleZSliceToAdd .add (minZSlice + "-" + maxZSlice );
253- maxWidth = (int ) maxWidth - (int ) xMixNewRectangle ;
254- maxHeight = (int ) maxHeight - (int ) yMinNewRectangle ;
255- rectanglesToAdd .add (new Rectangle ((int ) xMixNewRectangle ,
256- (int ) yMinNewRectangle ,
257- (int ) maxWidth ,
258- (int ) maxHeight ));
249+ maxWidth -= xMixNewRectangle ;
250+ maxHeight -= yMinNewRectangle ;
251+ rectanglesToAdd .add (new Rectangle (xMixNewRectangle , yMinNewRectangle , maxWidth , maxHeight ));
259252 }
260253 }
261254 LOGGER .debug ("{} boxes will be merged in {} new boxes" ,
@@ -287,18 +280,16 @@ public Map<Double, Box> getNewBoxes() {
287280
288281 for (int i = 0 ; i < listRectangle .size (); i ++) {
289282 String [] zSliceTMP = zSlices .get (i ).split ("-" );
290- short tmpXMax = (short ) (listRectangle .get (i ).getX () + listRectangle .get (i ).getWidth ());
291- short tmpYMax = (short ) (listRectangle .get (i ).getY () + listRectangle .get (i ).getHeight ());
292- short tmpZMax = (short ) (Short .parseShort (zSliceTMP [0 ]) + Short .parseShort (zSliceTMP [1 ]));
293- if (tmpZMax == 0 ) {
294- tmpZMax = 1 ;
283+ short xMin = (short ) listRectangle .get (i ).x ;
284+ short yMin = (short ) listRectangle .get (i ).y ;
285+ short zMin = Short .parseShort (zSliceTMP [0 ]);
286+ short xMax = (short ) (xMin + listRectangle .get (i ).width );
287+ short yMax = (short ) (yMin + listRectangle .get (i ).height );
288+ short zMax = (short ) (zMin + Short .parseShort (zSliceTMP [1 ]));
289+ if (zMax == 0 ) {
290+ zMax = 1 ;
295291 }
296- Box box = new Box ((short ) listRectangle .get (i ).getX (),
297- tmpXMax ,
298- (short ) listRectangle .get (i ).getY (),
299- tmpYMax ,
300- Short .parseShort (zSliceTMP [0 ]),
301- tmpZMax );
292+ Box box = new Box (xMin , xMax , yMin , yMax , zMin , zMax );
302293 boxes .put ((double ) i , box );
303294 }
304295 return boxes ;
0 commit comments