From 0d6d5e09ef2906451ad329f96086ef210f33f15a Mon Sep 17 00:00:00 2001
From: Karsten Rink <karsten.rink@ufz.de>
Date: Fri, 4 Jul 2014 12:35:21 +0200
Subject: [PATCH] replaced tet-volume calculation with function calls to basic
 implementation

---
 GeoLib/AnalyticalGeometry.cpp           |  9 +++++++++
 GeoLib/AnalyticalGeometry.h             |  7 +++++++
 MathLib/MathTools.cpp                   |  7 +------
 MathLib/MathTools.h                     |  5 -----
 MeshLib/Elements/TemplateHex-impl.h     | 14 +++++++-------
 MeshLib/Elements/TemplatePrism-impl.h   |  8 ++++----
 MeshLib/Elements/TemplatePyramid-impl.h |  6 +++---
 MeshLib/Elements/TemplateTet-impl.h     |  4 ++--
 8 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/GeoLib/AnalyticalGeometry.cpp b/GeoLib/AnalyticalGeometry.cpp
index 362b8451d8c..3b9bccae479 100644
--- a/GeoLib/AnalyticalGeometry.cpp
+++ b/GeoLib/AnalyticalGeometry.cpp
@@ -250,6 +250,14 @@ double calcTriangleArea(GeoLib::Point const& a, GeoLib::Point const& b, GeoLib::
 	return 0.5 * w.getLength();
 }
 
+double calcTetrahedronVolume(const double* x1, const double* x2, const double* x3, const double* x4)
+{
+	const MathLib::Vector3 ab(x1, x2);
+	const MathLib::Vector3 ac(x1, x3);
+	const MathLib::Vector3 ad(x1, x4);
+	return GeoLib::scalarTriple(ab, ac, ad) / 6.0;
+}
+
 // NewellPlane from book Real-Time Collision detection p. 494
 void getNewellPlane(const std::vector<GeoLib::Point*>& pnts, MathLib::Vector3 &plane_normal, double& d)
 {
@@ -459,4 +467,5 @@ void computeAndInsertAllIntersectionPoints(GeoLib::PointVec &pnt_vec,
 	}
 }
 
+
 } // end namespace GeoLib
diff --git a/GeoLib/AnalyticalGeometry.h b/GeoLib/AnalyticalGeometry.h
index 9a705b5d1e2..5ca4905023f 100644
--- a/GeoLib/AnalyticalGeometry.h
+++ b/GeoLib/AnalyticalGeometry.h
@@ -106,6 +106,12 @@ void rotatePointsToXZ(std::vector<GeoLib::Point*> &pnts);
  */
 double calcTriangleArea(GeoLib::Point const& a, GeoLib::Point const& b, GeoLib::Point const& c);
 
+/**
+ * Calculates the volume of a tetrahedron.
+ * The formula is V=1/6*|a(b x c)| with a=x1->x2, b=x1->x3 and c=x1->x4.
+ */
+double calcTetrahedronVolume(const double* x1, const double* x2, const double* x3, const double* x4);
+
 bool isPointInTriangle (const GeoLib::Point* p,
 		const GeoLib::Point* a, const GeoLib::Point* b, const GeoLib::Point* c);
 
@@ -194,6 +200,7 @@ void computeAndInsertAllIntersectionPoints(
 	GeoLib::PointVec &pnt_vec,
 	std::vector<GeoLib::Polyline*> & plys);
 
+
 } // end namespace GeoLib
 
 #endif /* ANALYTICAL_GEOMETRY_H_ */
diff --git a/MathLib/MathTools.cpp b/MathLib/MathTools.cpp
index a086660e996..fe2bbc9673e 100644
--- a/MathLib/MathTools.cpp
+++ b/MathLib/MathTools.cpp
@@ -62,11 +62,6 @@ double getAngle (const double p0[3], const double p1[3], const double p2[3])
 	return acos (scalarProduct<double,3> (v0,v1) / (sqrt(scalarProduct<double,3>(v0,v0)) * sqrt(scalarProduct<double,3>(v1,v1))));
 }
 
-double calcTetrahedronVolume(const double* x1, const double* x2, const double* x3, const double* x4)
-{
-	return fabs((x1[0] - x4[0]) * ((x2[1] - x4[1]) * (x3[2] - x4[2]) - (x2[2] - x4[2]) * (x3[1] - x4[1]))
-	          - (x1[1] - x4[1]) * ((x2[0] - x4[0]) * (x3[2] - x4[2]) - (x2[2] - x4[2]) * (x3[0] - x4[0]))
-	          + (x1[2] - x4[2]) * ((x2[0] - x4[0]) * (x3[1] - x4[1]) - (x2[1] - x4[1]) * (x3[0] - x4[0]))) / 6.0;
-}
+
 
 } // namespace
diff --git a/MathLib/MathTools.h b/MathLib/MathTools.h
index 8c78efca616..47357fc6368 100644
--- a/MathLib/MathTools.h
+++ b/MathLib/MathTools.h
@@ -148,11 +148,6 @@ float normalize(float min, float max, float val);
  */
 double getAngle (const double p0[3], const double p1[3], const double p2[3]);
 
-/**
- * Calculates the volume of a tetrahedron.
- * The formula is V=1/6*|a(b x c)| with a=x1->x2, b=x1->x3 and c=x1->x4.
- */
-double calcTetrahedronVolume(const double* x1, const double* x2, const double* x3, const double* x4);
 
 /**
  * simple power function that takes as a second argument an integer instead of a float
diff --git a/MeshLib/Elements/TemplateHex-impl.h b/MeshLib/Elements/TemplateHex-impl.h
index a88e1f9d504..45e123cbd9c 100644
--- a/MeshLib/Elements/TemplateHex-impl.h
+++ b/MeshLib/Elements/TemplateHex-impl.h
@@ -18,7 +18,7 @@
 #include "Quad.h"
 #include "Prism.h"
 
-#include "MathTools.h"
+#include "AnalyticalGeometry.h"
 
 namespace MeshLib {
 
@@ -105,12 +105,12 @@ TemplateHex<NNODES,CELLHEXTYPE>::~TemplateHex()
 template <unsigned NNODES, CellType CELLHEXTYPE>
 double TemplateHex<NNODES,CELLHEXTYPE>::computeVolume()
 {
-	return MathLib::calcTetrahedronVolume(_nodes[4]->getCoords(), _nodes[7]->getCoords(), _nodes[5]->getCoords(), _nodes[0]->getCoords())
-		 + MathLib::calcTetrahedronVolume(_nodes[5]->getCoords(), _nodes[3]->getCoords(), _nodes[1]->getCoords(), _nodes[0]->getCoords())
-		 + MathLib::calcTetrahedronVolume(_nodes[5]->getCoords(), _nodes[7]->getCoords(), _nodes[3]->getCoords(), _nodes[0]->getCoords())
-		 + MathLib::calcTetrahedronVolume(_nodes[5]->getCoords(), _nodes[7]->getCoords(), _nodes[6]->getCoords(), _nodes[2]->getCoords())
-		 + MathLib::calcTetrahedronVolume(_nodes[1]->getCoords(), _nodes[3]->getCoords(), _nodes[5]->getCoords(), _nodes[2]->getCoords())
-		 + MathLib::calcTetrahedronVolume(_nodes[3]->getCoords(), _nodes[7]->getCoords(), _nodes[5]->getCoords(), _nodes[2]->getCoords());
+	return GeoLib::calcTetrahedronVolume(_nodes[4]->getCoords(), _nodes[7]->getCoords(), _nodes[5]->getCoords(), _nodes[0]->getCoords())
+		 + GeoLib::calcTetrahedronVolume(_nodes[5]->getCoords(), _nodes[3]->getCoords(), _nodes[1]->getCoords(), _nodes[0]->getCoords())
+		 + GeoLib::calcTetrahedronVolume(_nodes[5]->getCoords(), _nodes[7]->getCoords(), _nodes[3]->getCoords(), _nodes[0]->getCoords())
+		 + GeoLib::calcTetrahedronVolume(_nodes[5]->getCoords(), _nodes[7]->getCoords(), _nodes[6]->getCoords(), _nodes[2]->getCoords())
+		 + GeoLib::calcTetrahedronVolume(_nodes[1]->getCoords(), _nodes[3]->getCoords(), _nodes[5]->getCoords(), _nodes[2]->getCoords())
+		 + GeoLib::calcTetrahedronVolume(_nodes[3]->getCoords(), _nodes[7]->getCoords(), _nodes[5]->getCoords(), _nodes[2]->getCoords());
 }
 
 template <unsigned NNODES, CellType CELLHEXTYPE>
diff --git a/MeshLib/Elements/TemplatePrism-impl.h b/MeshLib/Elements/TemplatePrism-impl.h
index 3029b240437..a91d0a2e3c7 100644
--- a/MeshLib/Elements/TemplatePrism-impl.h
+++ b/MeshLib/Elements/TemplatePrism-impl.h
@@ -20,7 +20,7 @@
 #include "Pyramid.h"
 #include "Quad.h"
 
-#include "MathTools.h"
+#include "AnalyticalGeometry.h"
 
 namespace MeshLib {
 
@@ -104,9 +104,9 @@ TemplatePrism<NNODES,CELLPRISMTYPE>::~TemplatePrism()
 template <unsigned NNODES, CellType CELLPRISMTYPE>
 double TemplatePrism<NNODES,CELLPRISMTYPE>::computeVolume()
 {
-	return MathLib::calcTetrahedronVolume(_nodes[0]->getCoords(), _nodes[1]->getCoords(), _nodes[2]->getCoords(), _nodes[3]->getCoords())
-		 + MathLib::calcTetrahedronVolume(_nodes[1]->getCoords(), _nodes[4]->getCoords(), _nodes[2]->getCoords(), _nodes[3]->getCoords())
-		 + MathLib::calcTetrahedronVolume(_nodes[2]->getCoords(), _nodes[4]->getCoords(), _nodes[5]->getCoords(), _nodes[3]->getCoords());
+	return GeoLib::calcTetrahedronVolume(_nodes[0]->getCoords(), _nodes[1]->getCoords(), _nodes[2]->getCoords(), _nodes[3]->getCoords())
+		 + GeoLib::calcTetrahedronVolume(_nodes[1]->getCoords(), _nodes[4]->getCoords(), _nodes[2]->getCoords(), _nodes[3]->getCoords())
+		 + GeoLib::calcTetrahedronVolume(_nodes[2]->getCoords(), _nodes[4]->getCoords(), _nodes[5]->getCoords(), _nodes[3]->getCoords());
 }
 
 template <unsigned NNODES, CellType CELLPRISMTYPE>
diff --git a/MeshLib/Elements/TemplatePyramid-impl.h b/MeshLib/Elements/TemplatePyramid-impl.h
index 4a93bab8bfc..5bcdd4d0ac2 100644
--- a/MeshLib/Elements/TemplatePyramid-impl.h
+++ b/MeshLib/Elements/TemplatePyramid-impl.h
@@ -20,7 +20,7 @@
 #include "Tet.h"
 #include "Quad.h"
 
-#include "MathTools.h"
+#include "AnalyticalGeometry.h"
 
 namespace MeshLib {
 
@@ -107,8 +107,8 @@ TemplatePyramid<NNODES,CELLPYRAMIDTYPE>::~TemplatePyramid()
 template <unsigned NNODES, CellType CELLPYRAMIDTYPE>
 double TemplatePyramid<NNODES,CELLPYRAMIDTYPE>::computeVolume()
 {
-	return MathLib::calcTetrahedronVolume(_nodes[0]->getCoords(), _nodes[1]->getCoords(), _nodes[2]->getCoords(), _nodes[4]->getCoords())
-		 + MathLib::calcTetrahedronVolume(_nodes[2]->getCoords(), _nodes[3]->getCoords(), _nodes[0]->getCoords(), _nodes[4]->getCoords());
+	return GeoLib::calcTetrahedronVolume(_nodes[0]->getCoords(), _nodes[1]->getCoords(), _nodes[2]->getCoords(), _nodes[4]->getCoords())
+		 + GeoLib::calcTetrahedronVolume(_nodes[2]->getCoords(), _nodes[3]->getCoords(), _nodes[0]->getCoords(), _nodes[4]->getCoords());
 }
 
 template <unsigned NNODES, CellType CELLPYRAMIDTYPE>
diff --git a/MeshLib/Elements/TemplateTet-impl.h b/MeshLib/Elements/TemplateTet-impl.h
index 37a06afed39..0f9b967dec8 100644
--- a/MeshLib/Elements/TemplateTet-impl.h
+++ b/MeshLib/Elements/TemplateTet-impl.h
@@ -17,7 +17,7 @@
 #include "Node.h"
 #include "Tri.h"
 
-#include "MathTools.h"
+#include "AnalyticalGeometry.h"
 
 namespace MeshLib {
 
@@ -99,7 +99,7 @@ TemplateTet<NNODES,CELLTETTYPE>::~TemplateTet()
 template <unsigned NNODES, CellType CELLTETTYPE>
 double TemplateTet<NNODES,CELLTETTYPE>::computeVolume()
 {
-	return MathLib::calcTetrahedronVolume(_nodes[0]->getCoords(), _nodes[1]->getCoords(), _nodes[2]->getCoords(), _nodes[3]->getCoords());
+	return GeoLib::calcTetrahedronVolume(_nodes[0]->getCoords(), _nodes[1]->getCoords(), _nodes[2]->getCoords(), _nodes[3]->getCoords());
 }
 
 template <unsigned NNODES, CellType CELLTETTYPE>
-- 
GitLab