From f3109f619e4a71055bf10b8cd7979cfb9ad01aba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Remacle=20=28students=29?=
 <jean-francois.remacle@uclouvain.be>
Date: Wed, 17 Jun 2009 13:30:26 +0000
Subject: [PATCH] Changed the return type of GetOBB from SOrientedBoundingBox*
 to SOrientedBoundingBox

---
 Geo/GEdge.cpp                |  4 +--
 Geo/GEdge.h                  |  2 +-
 Geo/GEntity.h                |  2 +-
 Geo/GFace.cpp                |  4 +--
 Geo/GFace.h                  |  2 +-
 Geo/GRegion.cpp              |  4 +--
 Geo/GRegion.h                |  2 +-
 Geo/GeomMeshMatcher.cpp      | 38 ++++++++++-----------
 Geo/SOrientedBoundingBox.cpp | 64 +++++++++++++++++++++++++++++++-----
 Geo/SOrientedBoundingBox.h   |  6 ++--
 10 files changed, 88 insertions(+), 40 deletions(-)

diff --git a/Geo/GEdge.cpp b/Geo/GEdge.cpp
index 501e63cd03..ebbb1eb82c 100644
--- a/Geo/GEdge.cpp
+++ b/Geo/GEdge.cpp
@@ -107,7 +107,7 @@ SBoundingBox3d GEdge::bounds() const
   return bbox;
 }
 
-SOrientedBoundingBox* GEdge::getOBB() {
+SOrientedBoundingBox GEdge::getOBB() {
   if (!(this->_obb)) {
     vector<SPoint3> vertices;
     if(this->getNumMeshVertices() > 0) {
@@ -137,7 +137,7 @@ SOrientedBoundingBox* GEdge::getOBB() {
     }
     this->_obb = SOrientedBoundingBox::buildOBB(vertices);
   }
-  return (this->_obb);
+  return (SOrientedBoundingBox(this->_obb));
 }
 
 void GEdge::setVisibility(char val, bool recursive)
diff --git a/Geo/GEdge.h b/Geo/GEdge.h
index fc5695b8ef..9f30e1e386 100644
--- a/Geo/GEdge.h
+++ b/Geo/GEdge.h
@@ -62,7 +62,7 @@ class GEdge : public GEntity {
   virtual SBoundingBox3d bounds() const;
 
   // get the oriented bounding box
-  virtual SOrientedBoundingBox* getOBB();
+  virtual SOrientedBoundingBox getOBB();
 
   // faces that this entity bounds
   virtual std::list<GFace*> faces() const { return l_faces; }
diff --git a/Geo/GEntity.h b/Geo/GEntity.h
index f8634f1945..8396c26578 100644
--- a/Geo/GEntity.h
+++ b/Geo/GEntity.h
@@ -212,7 +212,7 @@ class GEntity {
   virtual SBoundingBox3d bounds() const { return SBoundingBox3d(); }
 
   //  get the oriented bounding box
-  virtual SOrientedBoundingBox* getOBB() {return new SOrientedBoundingBox(); }
+  virtual SOrientedBoundingBox getOBB() {return SOrientedBoundingBox(); }
   
   // get/set the visibility flag
   virtual char getVisibility();
diff --git a/Geo/GFace.cpp b/Geo/GFace.cpp
index c76a1e20c9..dc07b30cf3 100644
--- a/Geo/GFace.cpp
+++ b/Geo/GFace.cpp
@@ -146,7 +146,7 @@ SBoundingBox3d GFace::bounds() const
   return res;
 }
 
-SOrientedBoundingBox* GFace::getOBB() {
+SOrientedBoundingBox GFace::getOBB() {
   if (!(this->_obb)) {
     vector<SPoint3> vertices;
     if(this->getNumMeshVertices() > 0) {
@@ -193,7 +193,7 @@ SOrientedBoundingBox* GFace::getOBB() {
     }
     this->_obb =  SOrientedBoundingBox::buildOBB(vertices);
   }
-  return (this->_obb);
+  return (SOrientedBoundingBox(this->_obb));
 }
 
 
diff --git a/Geo/GFace.h b/Geo/GFace.h
index 1e340dab95..6631cb4e52 100644
--- a/Geo/GFace.h
+++ b/Geo/GFace.h
@@ -111,7 +111,7 @@ class GFace : public GEntity
   virtual SBoundingBox3d bounds() const;
 
   // get the oriented bounding box
-  virtual SOrientedBoundingBox* getOBB();
+  virtual SOrientedBoundingBox getOBB();
   
   // retrieve surface params
   virtual surface_params getSurfaceParams() const;
diff --git a/Geo/GRegion.cpp b/Geo/GRegion.cpp
index 3b203b54ca..b78c4012b3 100644
--- a/Geo/GRegion.cpp
+++ b/Geo/GRegion.cpp
@@ -111,7 +111,7 @@ SBoundingBox3d GRegion::bounds() const
   return res;
 }
 
-SOrientedBoundingBox* GRegion::getOBB() {
+SOrientedBoundingBox GRegion::getOBB() {
   if (!(this->_obb)) {
     vector<SPoint3> vertices;
     list<GFace*> b_faces = this->faces();
@@ -159,7 +159,7 @@ SOrientedBoundingBox* GRegion::getOBB() {
     }
     this->_obb = SOrientedBoundingBox::buildOBB(vertices);
   }
-  return (this->_obb);
+  return (SOrientedBoundingBox(this->_obb));
 }
 
 void GRegion::setVisibility(char val, bool recursive)
diff --git a/Geo/GRegion.h b/Geo/GRegion.h
index 0a57988749..7c6d4cec9a 100644
--- a/Geo/GRegion.h
+++ b/Geo/GRegion.h
@@ -50,7 +50,7 @@ class GRegion : public GEntity {
   virtual SBoundingBox3d bounds() const;
 
   // get the oriented bounding box
-  virtual SOrientedBoundingBox* getOBB();
+  virtual SOrientedBoundingBox getOBB();
 
   // check if the region is connected to another region by an edge
   bool edgeConnected(GRegion *r) const;
diff --git a/Geo/GeomMeshMatcher.cpp b/Geo/GeomMeshMatcher.cpp
index 75e69c3a28..aa5d7c8cf6 100644
--- a/Geo/GeomMeshMatcher.cpp
+++ b/Geo/GeomMeshMatcher.cpp
@@ -191,13 +191,13 @@ vector<Pair<GEdge*,GEdge*> >*  GeomMeshMatcher::matchEdges(GModel* m1, GModel* m
       Msg::Info("There are %i edges that could match.",common_edges.size());
       // So, first step is to build an array of points taken on the geo entity
       // Then, compute the minimal bounding box
-      SOrientedBoundingBox* geo_obb = ((GEdge*)(*entity1))->getOBB();
+      SOrientedBoundingBox geo_obb = ((GEdge*)(*entity1))->getOBB();
 
      GEdge* choice = 0;
       double best_score = DBL_MAX;
       // Next, let's iterate over the mesh entities.
       for (vector<GEdge*>::iterator candidate = common_edges.begin(); candidate != common_edges.end(); candidate++) {
-          SOrientedBoundingBox* mesh_obb = (*candidate)->getOBB();
+          SOrientedBoundingBox mesh_obb = (*candidate)->getOBB();
             Msg::Info("Comparing score : %f", SOrientedBoundingBox::compare(geo_obb,mesh_obb));
 
 	    //if (geo_obb->intersects(mesh_obb)) {
@@ -273,26 +273,26 @@ vector<Pair<GFace*,GFace*> >* GeomMeshMatcher:: matchFaces(GModel* m1, GModel* m
 
       int N;
       // Then, compute the minimal bounding box
-      SOrientedBoundingBox* geo_obb = ((GFace*)(*entity1))->getOBB();
+      SOrientedBoundingBox geo_obb = ((GFace*)(*entity1))->getOBB();
 
 
       GFace* choice = 0;
       double best_score = DBL_MAX;
       // Next, let's iterate over the mesh entities.
       for (vector<GFace*>::iterator candidate = common_faces.begin(); candidate != common_faces.end(); candidate++) {
-          SOrientedBoundingBox* mesh_obb = (*candidate)->getOBB();
+          SOrientedBoundingBox mesh_obb = (*candidate)->getOBB();
           Msg::Info("Comparing score : %f", SOrientedBoundingBox::compare(geo_obb,mesh_obb));
 
-          double cen_dist1 = geo_obb->getCenter()[0]-mesh_obb->getCenter()[0];
-          double cen_dist2 = geo_obb->getCenter()[1]-mesh_obb->getCenter()[1];
-          double cen_dist3 = geo_obb->getCenter()[2]-mesh_obb->getCenter()[2];
+          double cen_dist1 = geo_obb.getCenter()[0]-mesh_obb.getCenter()[0];
+          double cen_dist2 = geo_obb.getCenter()[1]-mesh_obb.getCenter()[1];
+          double cen_dist3 = geo_obb.getCenter()[2]-mesh_obb.getCenter()[2];
           double score1 = sqrt(   cen_dist1*cen_dist1
                                 + cen_dist2*cen_dist2
                                 + cen_dist3*cen_dist3);
 
-          double score2 = fabs(geo_obb->getSize()[0]-mesh_obb->getSize()[0]);
-          double score3 = fabs(geo_obb->getSize()[1]-mesh_obb->getSize()[1]);
-          double score4 = fabs(geo_obb->getSize()[2]-mesh_obb->getSize()[2]);
+          double score2 = fabs(geo_obb.getSize()[0]-mesh_obb.getSize()[0]);
+          double score3 = fabs(geo_obb.getSize()[1]-mesh_obb.getSize()[1]);
+          double score4 = fabs(geo_obb.getSize()[2]-mesh_obb.getSize()[2]);
 
 
           if (fmax(fmax(score1,score2),fmax(score3,score4)) < best_score) {
@@ -379,7 +379,7 @@ vector<Pair<GRegion*,GRegion*> >* GeomMeshMatcher:: matchRegions(GModel* m1, GMo
       double** vertices = new double*[N_total];
      
       // Then, compute the minimal bounding box
-      SOrientedBoundingBox* geo_obb = ((GRegion*) *entity1)->getOBB();
+      SOrientedBoundingBox geo_obb = ((GRegion*) *entity1)->getOBB();
 
 
       GRegion* choice = 0;
@@ -387,20 +387,20 @@ vector<Pair<GRegion*,GRegion*> >* GeomMeshMatcher:: matchRegions(GModel* m1, GMo
       // Next, let's iterate over the mesh entities.
       for (vector<GRegion*>::iterator candidate = common_regions.begin(); candidate != common_regions.end(); candidate++) {
           // Again, build an array with the vertices.
-          SOrientedBoundingBox* mesh_obb = (*candidate)->getOBB();
+          SOrientedBoundingBox mesh_obb = (*candidate)->getOBB();
           Msg::Info("Comparing score : %f", SOrientedBoundingBox::compare(geo_obb,mesh_obb));
 
-          if (geo_obb->intersects(mesh_obb)) {
-            double cen_dist1 = geo_obb->getCenter()[0]-mesh_obb->getCenter()[0];
-            double cen_dist2 = geo_obb->getCenter()[1]-mesh_obb->getCenter()[1];
-            double cen_dist3 = geo_obb->getCenter()[2]-mesh_obb->getCenter()[2];
+          if (geo_obb.intersects(mesh_obb)) {
+            double cen_dist1 = geo_obb.getCenter()[0]-mesh_obb.getCenter()[0];
+            double cen_dist2 = geo_obb.getCenter()[1]-mesh_obb.getCenter()[1];
+            double cen_dist3 = geo_obb.getCenter()[2]-mesh_obb.getCenter()[2];
             double score1 = sqrt(   cen_dist1*cen_dist1
                                   + cen_dist2*cen_dist2
                                   + cen_dist3*cen_dist3);
   
-            double score2 = fabs(geo_obb->getSize()[0]-mesh_obb->getSize()[0]);
-            double score3 = fabs(geo_obb->getSize()[1]-mesh_obb->getSize()[1]);
-            double score4 = fabs(geo_obb->getSize()[2]-mesh_obb->getSize()[2]);
+            double score2 = fabs(geo_obb.getSize()[0]-mesh_obb.getSize()[0]);
+            double score3 = fabs(geo_obb.getSize()[1]-mesh_obb.getSize()[1]);
+            double score4 = fabs(geo_obb.getSize()[2]-mesh_obb.getSize()[2]);
   
 
             if (fmax(fmax(score1,score2),fmax(score3,score4)) < best_score) {
diff --git a/Geo/SOrientedBoundingBox.cpp b/Geo/SOrientedBoundingBox.cpp
index 494566f8af..893b2115cf 100644
--- a/Geo/SOrientedBoundingBox.cpp
+++ b/Geo/SOrientedBoundingBox.cpp
@@ -102,8 +102,54 @@ SOrientedBoundingBox::SOrientedBoundingBox(SVector3& center,
   p8y = center[1] + (axisX[1]*dx) + (axisY[1]*dy) + (axisZ[1]*dz);
   p8z = center[2] + (axisX[2]*dx) + (axisY[2]*dy) + (axisZ[2]*dz);
 
+}
+
+//-----------------------------------------------------------------------------
+
+SOrientedBoundingBox::SOrientedBoundingBox(SOrientedBoundingBox* other) {
+
+  this->size = other->getSize();
+  this->axisX = other->getAxis(0);
+  this->axisY = other->getAxis(1);
+  this->axisZ = other->getAxis(2);
+  this->center = other->getCenter();
+
+  double dx = 0.5*size[0];
+  double dy = 0.5*size[1];
+  double dz = 0.5*size[2];
+
+  p1x = center[0] - (axisX[0]*dx) - (axisY[0]*dy) - (axisZ[0]*dz);
+  p1y = center[1] - (axisX[1]*dx) - (axisY[1]*dy) - (axisZ[1]*dz);
+  p1z = center[2] - (axisX[2]*dx) - (axisY[2]*dy) - (axisZ[2]*dz);
+
+  p2x = center[0] + (axisX[0]*dx) - (axisY[0]*dy) - (axisZ[0]*dz);
+  p2y = center[1] + (axisX[1]*dx) - (axisY[1]*dy) - (axisZ[1]*dz);
+  p2z = center[2] + (axisX[2]*dx) - (axisY[2]*dy) - (axisZ[2]*dz);
+
+  p3x = center[0] - (axisX[0]*dx) + (axisY[0]*dy) - (axisZ[0]*dz);
+  p3y = center[1] - (axisX[1]*dx) + (axisY[1]*dy) - (axisZ[1]*dz);
+  p3z = center[2] - (axisX[2]*dx) + (axisY[2]*dy) - (axisZ[2]*dz);
+
+  p4x = center[0] + (axisX[0]*dx) + (axisY[0]*dy) - (axisZ[0]*dz);
+  p4y = center[1] + (axisX[1]*dx) + (axisY[1]*dy) - (axisZ[1]*dz);
+  p4z = center[2] + (axisX[2]*dx) + (axisY[2]*dy) - (axisZ[2]*dz);
+
+  p5x = center[0] - (axisX[0]*dx) - (axisY[0]*dy) + (axisZ[0]*dz);
+  p5y = center[1] - (axisX[1]*dx) - (axisY[1]*dy) + (axisZ[1]*dz);
+  p5z = center[2] - (axisX[2]*dx) - (axisY[2]*dy) + (axisZ[2]*dz);
+
+  p6x = center[0] + (axisX[0]*dx) - (axisY[0]*dy) + (axisZ[0]*dz);
+  p6y = center[1] + (axisX[1]*dx) - (axisY[1]*dy) + (axisZ[1]*dz);
+  p6z = center[2] + (axisX[2]*dx) - (axisY[2]*dy) + (axisZ[2]*dz);
 
+  p7x = center[0] - (axisX[0]*dx) + (axisY[0]*dy) + (axisZ[0]*dz);
+  p7y = center[1] - (axisX[1]*dx) + (axisY[1]*dy) + (axisZ[1]*dz);
+  p7z = center[2] - (axisX[2]*dx) + (axisY[2]*dy) + (axisZ[2]*dz);
 
+  p8x = center[0] + (axisX[0]*dx) + (axisY[0]*dy) + (axisZ[0]*dz);
+  p8y = center[1] + (axisX[1]*dx) + (axisY[1]*dy) + (axisZ[1]*dz);
+  p8z = center[2] + (axisX[2]*dx) + (axisY[2]*dy) + (axisZ[2]*dz);
+  
 }
 
 //-----------------------------------------------------------------------------
@@ -134,24 +180,24 @@ SVector3 SOrientedBoundingBox::getAxis(int axis) {
 
 //-----------------------------------------------------------------------------
 
-bool SOrientedBoundingBox::intersects(SOrientedBoundingBox* obb) {
+bool SOrientedBoundingBox::intersects(SOrientedBoundingBox& obb) {
 
   SVector3 collide_axes[15];
   for (int i = 0; i < 3; i ++) {
     collide_axes[i] = this->getAxis(i);
-    collide_axes[i+3] = obb->getAxis(i);
+    collide_axes[i+3] = obb.getAxis(i);
   }
 
   SVector3 sizes[2];
   sizes[0] = this->getSize();
-  sizes[1] = obb->getSize();
+  sizes[1] = obb.getSize();
 
   for(unsigned int i=0 ; i<3 ; i++) {
     for(unsigned int j=3 ; j<6 ; j++) {
       collide_axes[3*i+j+3] = crossprod(collide_axes[i],collide_axes[j]);
     }
   }
-  SVector3 T = obb->getCenter() - this->getCenter();
+  SVector3 T = obb.getCenter() - this->getCenter();
 
   for(unsigned int i=0 ; i<15 ; i++) {
     double val = 0.0;
@@ -516,23 +562,23 @@ SOrientedBoundingBox* SOrientedBoundingBox::buildOBB(vector<SPoint3> vertices) {
           size[0], size[1], size[2], Axis1, Axis2, Axis3));
 }
 
-double SOrientedBoundingBox::compare(SOrientedBoundingBox* obb1, SOrientedBoundingBox* obb2) {
+double SOrientedBoundingBox::compare(SOrientedBoundingBox& obb1, SOrientedBoundingBox& obb2) {
 
   // "center term"
-  double center_term = norm(obb1->getCenter() - obb2->getCenter());
+  double center_term = norm(obb1.getCenter() - obb2.getCenter());
 
   // "size term"
   double size_term = 0.0;
   for (int i = 0; i < 3; i++) {
-    if ((obb1->getSize())(i) + (obb2->getSize())(i) != 0) {
-      size_term += fabs((obb1->getSize())(i) - (obb2->getSize())(i)) / ((obb1->getSize())(i) + (obb2->getSize())(i));
+    if ((obb1.getSize())(i) + (obb2.getSize())(i) != 0) {
+      size_term += fabs((obb1.getSize())(i) - (obb2.getSize())(i)) / ((obb1.getSize())(i) + (obb2.getSize())(i));
     }
   }
 
   // "orientation term"
   double orientation_term = 0.0;
   for (int i = 0; i < 3; i++) {
-    orientation_term += 1 - fabs(dot(obb1->getAxis(i),obb2->getAxis(i)));
+    orientation_term += 1 - fabs(dot(obb1.getAxis(i),obb2.getAxis(i)));
   }
 
   return (center_term + size_term + orientation_term);
diff --git a/Geo/SOrientedBoundingBox.h b/Geo/SOrientedBoundingBox.h
index 32c9272330..3c595da7cf 100644
--- a/Geo/SOrientedBoundingBox.h
+++ b/Geo/SOrientedBoundingBox.h
@@ -59,6 +59,8 @@ class SOrientedBoundingBox {
                         const SVector3& axisY,
                         const SVector3& axisZ);
 
+    // Copy constructor
+    SOrientedBoundingBox(SOrientedBoundingBox* other);
     ~SOrientedBoundingBox();
 
     SVector3 getCenter()  {return (this->center);};
@@ -75,9 +77,9 @@ class SOrientedBoundingBox {
 
     static SOrientedBoundingBox* buildOBB(vector<SPoint3> vertices);
 
-    bool intersects(SOrientedBoundingBox* obb);
+    bool intersects(SOrientedBoundingBox& obb);
 
-    static double compare(SOrientedBoundingBox* obb1, SOrientedBoundingBox* obb2);
+    static double compare(SOrientedBoundingBox& obb1, SOrientedBoundingBox& obb2);
 
   private:
     SVector3 center;
-- 
GitLab