From 68f736dda3c411ea8018431a28ba7b39a776cd70 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Radek=20O=C5=A1lej=C5=A1ek?= <oslejsek@fi.muni.cz>
Date: Thu, 3 Mar 2022 18:35:44 +0100
Subject: [PATCH] Resolve "Visualize bouding box for cutting plane"

---
 .../analyst/scene/DrawableCuttingPlane.java   |  5 +-
 .../fidentis/analyst/scene/DrawablePlane.java | 72 ++++++++++++++++++-
 .../java/cz/fidentis/analyst/scene/Scene.java |  2 +-
 .../analyst/symmetry/ProfilesAction.java      | 10 +--
 4 files changed, 79 insertions(+), 10 deletions(-)

diff --git a/GUI/src/main/java/cz/fidentis/analyst/scene/DrawableCuttingPlane.java b/GUI/src/main/java/cz/fidentis/analyst/scene/DrawableCuttingPlane.java
index 2a8dab3b..7c0d9d57 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/scene/DrawableCuttingPlane.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/scene/DrawableCuttingPlane.java
@@ -21,9 +21,10 @@ public class DrawableCuttingPlane extends DrawablePlane {
      * 
      * @param plane plane
      * @param bbox bounding box used to estimate cutting pale shape (rectangle)
+     * @param showBBox whether to render bounding box
      */
-    public DrawableCuttingPlane(Plane plane, BBox bbox) {
-        super(plane, bbox);
+    public DrawableCuttingPlane(Plane plane, BBox bbox, boolean showBBox) {
+        super(plane, bbox, showBBox);
     }
     
     /**
diff --git a/GUI/src/main/java/cz/fidentis/analyst/scene/DrawablePlane.java b/GUI/src/main/java/cz/fidentis/analyst/scene/DrawablePlane.java
index 0796f9d7..7a580019 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/scene/DrawablePlane.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/scene/DrawablePlane.java
@@ -17,6 +17,7 @@ public class DrawablePlane extends Drawable {
     
     private Plane plane;
     private final BBox bbox;
+    private boolean showBoundingBox;
     
     
     /**
@@ -24,10 +25,12 @@ public class DrawablePlane extends Drawable {
      * 
      * @param plane plane
      * @param bbox bounding box used to estimate cutting pale shape (rectangle)
+     * @param showBBox whether to render bounding box
      */
-    public DrawablePlane(Plane plane, BBox bbox) {
+    public DrawablePlane(Plane plane, BBox bbox, boolean showBBox) {
         this.plane = plane;
         this.bbox = bbox;
+        this.showBoundingBox = showBBox;
     }
 
     /**
@@ -39,12 +42,30 @@ public class DrawablePlane extends Drawable {
         super(drPlane);
         this.plane = new Plane(drPlane.getPlane());
         this.bbox = drPlane.bbox;
+        this.showBoundingBox = drPlane.showBoundingBox;
     }
     
     public Plane getPlane() {
         return plane;
     }
     
+    /**
+     * Returns {@code true} if the bounding box is to be rendered.
+     * @return {@code true} if the bounding box is to be rendered.
+     */
+    public boolean isShownBBobx() {
+        return this.showBoundingBox;
+    }
+    
+    /**
+     * Switches on/off bounding box rendering.
+     * 
+     * @param show If {@code true], then the bounding box is rendered as well
+     */
+    public void setShowBBox(boolean show) {
+        this.showBoundingBox = show;
+    }
+    
     protected void setPlane(Plane plane) {
         this.plane = plane;
     }
@@ -59,11 +80,56 @@ public class DrawablePlane extends Drawable {
         // get the normal and tex coords indicies for face i  
         for (int v = 0; v < facet.getCornerTable().getSize(); v++) {
             Point3d vert = facet.getVertices().get(facet.getCornerTable().getRow(v).getVertexIndex()).getPosition();
-            if (v == 0) {
-            }
             gl.glVertex3d(vert.x, vert.y, vert.z);
         }
         gl.glEnd();
+        
+        if (showBoundingBox) {
+            Point3d min = bbox.getMinPoint();
+            Point3d max = bbox.getMaxPoint();
+            gl.glBegin(GL2.GL_LINES); 
+
+            // bottom rectangle:
+            gl.glVertex3d(min.x, min.y, min.z);
+            gl.glVertex3d(max.x, min.y, min.z);
+            
+            gl.glVertex3d(max.x, min.y, min.z);
+            gl.glVertex3d(max.x, min.y, max.z);
+            
+            gl.glVertex3d(max.x, min.y, max.z);
+            gl.glVertex3d(min.x, min.y, max.z);
+            
+            gl.glVertex3d(min.x, min.y, max.z);
+            gl.glVertex3d(min.x, min.y, min.z);
+
+            // top rectangle:
+            gl.glVertex3d(min.x, max.y, min.z);
+            gl.glVertex3d(max.x, max.y, min.z);
+            
+            gl.glVertex3d(max.x, max.y, min.z);
+            gl.glVertex3d(max.x, max.y, max.z);
+            
+            gl.glVertex3d(max.x, max.y, max.z);
+            gl.glVertex3d(min.x, max.y, max.z);
+            
+            gl.glVertex3d(min.x, max.y, max.z);
+            gl.glVertex3d(min.x, max.y, min.z);
+            
+            // vertical edges:
+            gl.glVertex3d(min.x, min.y, min.z);
+            gl.glVertex3d(min.x, max.y, min.z);
+            
+            gl.glVertex3d(max.x, min.y, min.z);
+            gl.glVertex3d(max.x, max.y, min.z);
+            
+            gl.glVertex3d(max.x, min.y, max.z);
+            gl.glVertex3d(max.x, max.y, max.z);
+            
+            gl.glVertex3d(min.x, min.y, max.z);
+            gl.glVertex3d(min.x, max.y, max.z);
+            
+            gl.glEnd();
+        }
     }
     
     protected BBox getBBox() {
diff --git a/GUI/src/main/java/cz/fidentis/analyst/scene/Scene.java b/GUI/src/main/java/cz/fidentis/analyst/scene/Scene.java
index 22fbdc2f..c8ef2126 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/scene/Scene.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/scene/Scene.java
@@ -221,7 +221,7 @@ public class Scene {
         }
         
         if (drawableSymmetryPlanes.get(slot) == null) { // add new 
-            drawableSymmetryPlanes.set(slot, new DrawablePlane(face.getSymmetryPlane(), face.getBoundingBox()));
+            drawableSymmetryPlanes.set(slot, new DrawablePlane(face.getSymmetryPlane(), face.getBoundingBox(), false));
         } else { // replace existing
             drawableSymmetryPlanes.get(slot).setPlane(face.getSymmetryPlane());
         }
diff --git a/GUI/src/main/java/cz/fidentis/analyst/symmetry/ProfilesAction.java b/GUI/src/main/java/cz/fidentis/analyst/symmetry/ProfilesAction.java
index e79f9fd1..3534229e 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/symmetry/ProfilesAction.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/symmetry/ProfilesAction.java
@@ -274,7 +274,7 @@ public class ProfilesAction extends ControlPanelAction implements HumanFaceListe
     protected void computeVerticalCuttingPlanes() {
         BBox bbox = getCanvas().getPrimaryFace().getBoundingBox(); 
         Plane plane = new Plane(new Vector3d(1,0,0), bbox.getMidPoint().x);
-        DrawableCuttingPlane cuttingPlane = new DrawableCuttingPlane(plane, bbox);
+        DrawableCuttingPlane cuttingPlane = new DrawableCuttingPlane(plane, bbox, true);
         cuttingPlane.setTransparency(0.5f);
         if (priCuttingPlaneSlot == -1) {
             priCuttingPlaneSlot = getCanvas().getScene().getFreeSlotForOtherDrawables();
@@ -284,7 +284,7 @@ public class ProfilesAction extends ControlPanelAction implements HumanFaceListe
         if (getSecondaryDrawableFace() != null) {
             bbox = getCanvas().getSecondaryFace().getBoundingBox(); 
             plane = new Plane(new Vector3d(1,0,0), bbox.getMidPoint().x);
-            cuttingPlane = new DrawableCuttingPlane(plane, bbox);
+            cuttingPlane = new DrawableCuttingPlane(plane, bbox, true);
             cuttingPlane.setTransparency(0.5f);
             if (secCuttingPlaneSlot == -1) {
                 secCuttingPlaneSlot = getCanvas().getScene().getFreeSlotForOtherDrawables();
@@ -305,7 +305,8 @@ public class ProfilesAction extends ControlPanelAction implements HumanFaceListe
         }
         DrawableCuttingPlane cuttingPlane = new DrawableCuttingPlane(
                 new Plane(symmetryPlane), 
-                getCanvas().getPrimaryFace().getBoundingBox()
+                getCanvas().getPrimaryFace().getBoundingBox(),
+                false
         );
         cuttingPlane.setTransparency(0.5f);
         if (priCuttingPlaneSlot == -1) {
@@ -321,7 +322,8 @@ public class ProfilesAction extends ControlPanelAction implements HumanFaceListe
             }
             cuttingPlane = new DrawableCuttingPlane(
                     new Plane(symmetryPlane), 
-                    getCanvas().getSecondaryFace().getBoundingBox()
+                    getCanvas().getSecondaryFace().getBoundingBox(),
+                    false
             );
             cuttingPlane.setTransparency(0.5f);
             if (secCuttingPlaneSlot == -1) {
-- 
GitLab