← Back to team overview

yade-dev team mailing list archive

[Branch ~yade-pkg/yade/git-trunk] Rev 3875: Replace some "#define"(s) by functions.

 

------------------------------------------------------------
revno: 3875
committer: Anton Gladky <gladky.anton@xxxxxxxxx>
timestamp: Mon 2016-05-30 21:21:13 +0200
message:
  Replace some "#define"(s) by functions.
  
  It is a good practice not to use macroses
  if it is not really necessary.
  
  http://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives
modified:
  lib/opengl/OpenGLWrapper.hpp
  pkg/dem/Polyhedra.cpp
  pkg/dem/Polyhedra_support.cpp
  pkg/dem/Tetra.cpp


--
lp:yade
https://code.launchpad.net/~yade-pkg/yade/git-trunk

Your team Yade developers is subscribed to branch lp:yade.
To unsubscribe from this branch go to https://code.launchpad.net/~yade-pkg/yade/git-trunk/+edit-subscription
=== modified file 'lib/opengl/OpenGLWrapper.hpp'
--- lib/opengl/OpenGLWrapper.hpp	2014-10-15 06:44:01 +0000
+++ lib/opengl/OpenGLWrapper.hpp	2016-05-30 19:21:13 +0000
@@ -214,4 +214,18 @@
 template< > inline void glMaterialv< Vector3r >	( GLenum face, GLenum pname, const Vector3r params )	{	const GLfloat _p[3]={(float) params[0], (float) params[1], (float) params[2]}; glMaterialfv(face,pname,_p);	};
 template< > inline void glMaterialv< Vector3i >		( GLenum face, GLenum pname, const Vector3i params )	{	glMaterialiv(face,pname,(int*)&params);	};
 
+template< typename Type > inline void glOneWire(Type & t, unsigned int a, unsigned int b) { glVertex3v(t->v[a]); glVertex3v(t->v[b]); }
+ 
+template< typename Type > inline void glOneFace(Type & t, unsigned int a, unsigned int b, unsigned int c) {
+	const Vector3r center = (t->v[0]+t->v[1]+t->v[2]+t->v[3])*.25;
+	Vector3r n=(t->v[b]-t->v[a]).cross(t->v[c]-t->v[a]);
+	n.normalize();
+	const Vector3r faceCenter=(t->v[a]+t->v[b]+t->v[c])/3.;
+	if((faceCenter-center).dot(n)<0) n=-n;
+	glNormal3v(n);
+	glVertex3v(t->v[a]);
+	glVertex3v(t->v[b]);
+	glVertex3v(t->v[c]);
+}
+
 #undef LDOUBL

=== modified file 'pkg/dem/Polyhedra.cpp'
--- pkg/dem/Polyhedra.cpp	2016-04-28 12:51:32 +0000
+++ pkg/dem/Polyhedra.cpp	2016-05-30 19:21:13 +0000
@@ -281,8 +281,9 @@
 /* Plotting */
 
 #ifdef YADE_OPENGL
-	#include<lib/opengl/OpenGLWrapper.hpp>
+	#include <lib/opengl/OpenGLWrapper.hpp>
 	bool Gl1_Polyhedra::wire;
+	
 	void Gl1_Polyhedra::go(const shared_ptr<Shape>& cm, const shared_ptr<State>&,bool wire2,const GLViewInfo&)
 	{
 		glMaterialv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,Vector3r(cm->color[0],cm->color[1],cm->color[2]));
@@ -293,20 +294,33 @@
 		if (wire || wire2) { 
 			glDisable(GL_LIGHTING);
 			glBegin(GL_LINES);
-				#define __ONEWIRE(a,b) glVertex3v(t->v[a]);glVertex3v(t->v[b])
-					for(int tri=0; tri < (int) faceTri.size(); tri+=3) {__ONEWIRE(faceTri[tri],faceTri[tri+1]); __ONEWIRE(faceTri[tri],faceTri[tri+2]); __ONEWIRE(faceTri[tri+1],faceTri[tri+2]);}
-				#undef __ONEWIRE
+			for(int tri=0; tri < (int) faceTri.size(); tri+=3) {
+				glOneWire(t, faceTri[tri], faceTri[tri+1]);
+				glOneWire(t, faceTri[tri], faceTri[tri+2]);
+				glOneWire(t, faceTri[tri+1], faceTri[tri+2]);
+			}
 			glEnd();
 		}
 		else
 		{
 			Vector3r centroid = t->GetCentroid();
-			Vector3r faceCenter, n;
 			glDisable(GL_CULL_FACE); glEnable(GL_LIGHTING);
 			glBegin(GL_TRIANGLES);
-				#define __ONEFACE(a,b,c) n=(t->v[b]-t->v[a]).cross(t->v[c]-t->v[a]); n.normalize(); faceCenter=(t->v[a]+t->v[b]+t->v[c])/3.; if((faceCenter-centroid).dot(n)<0)n=-n; glNormal3v(n); glVertex3v(t->v[a]); glVertex3v(t->v[b]); glVertex3v(t->v[c]);
-					for(int tri=0; tri < (int) faceTri.size(); tri+=3) {__ONEFACE(faceTri[tri],faceTri[tri+1],faceTri[tri+2]);}
-				#undef __ONEFACE
+			for(int tri=0; tri < (int) faceTri.size(); tri+=3) {
+				const auto a = faceTri[tri+0];
+				const auto b = faceTri[tri+1];
+				const auto c = faceTri[tri+2];
+				
+				Vector3r n=(t->v[b]-t->v[a]).cross(t->v[c]-t->v[a]);
+				n.normalize();
+				Vector3r faceCenter=(t->v[a]+t->v[b]+t->v[c])/3.;
+				if((faceCenter-centroid).dot(n)<0) n=-n;
+				
+				glNormal3v(n);
+				glVertex3v(t->v[a]);
+				glVertex3v(t->v[b]);
+				glVertex3v(t->v[c]);
+			}
 			glEnd();
 		}
 	}

=== modified file 'pkg/dem/Polyhedra_support.cpp'
--- pkg/dem/Polyhedra_support.cpp	2016-04-19 20:18:46 +0000
+++ pkg/dem/Polyhedra_support.cpp	2016-05-30 19:21:13 +0000
@@ -12,15 +12,15 @@
 //EMPRIRICAL CONSTANTS - ADJUST IF SEGMENTATION FAULT OCCUR, IT IS A PROBLEM OF CGAL. THESE ARE USED TO CHECK CGAL IMPUTS
 //DISTANCE_LIMIT controls numerical issues in calculating intersection. It should be small enough to neglect only extremely
 //small overlaps, but large enough to prevent errors during computation of convex hull
-#define DISTANCE_LIMIT 2E-11//-11
+const Real DISTANCE_LIMIT = 2E-11;
 //MERGE_PLANES_LIMIT - if two facets of two intersecting polyhedron differ less, then they are treated ose one only
-#define MERGE_PLANES_LIMIT 1E-18 //18
+const Real MERGE_PLANES_LIMIT = 1E-18; //18
 //SIMPLIFY_LIMIT - if two facets of one polyhedron differ less, then they are joint into one facet
-#define SIMPLIFY_LIMIT 1E-19 //19
+const Real SIMPLIFY_LIMIT = 1E-19; //19
 //FIND_NORMAL_LIMIT - to determine which facet of intersection belongs to which polyhedron
-#define FIND_NORMAL_LIMIT 1E-40
+const Real FIND_NORMAL_LIMIT = 1E-40;
 //SPLITTER_GAP - creates gap between splitted polyhedrons
-#define SPLITTER_GAP 1E-8
+const Real SPLITTER_GAP = 1E-8;
 
 
 //**********************************************************************************

=== modified file 'pkg/dem/Tetra.cpp'
--- pkg/dem/Tetra.cpp	2015-11-06 20:00:30 +0000
+++ pkg/dem/Tetra.cpp	2016-05-30 19:21:13 +0000
@@ -981,7 +981,8 @@
 }
 
 #ifdef YADE_OPENGL
-	#include<lib/opengl/OpenGLWrapper.hpp>
+	#include <lib/opengl/OpenGLWrapper.hpp>
+	
 	bool Gl1_Tetra::wire;
 	void Gl1_Tetra::go(const shared_ptr<Shape>& cm, const shared_ptr<State>&,bool wire2,const GLViewInfo&)
 	{
@@ -991,22 +992,22 @@
 		if (wire && wire2) { // wireframe, as for Tetrahedron
 			glDisable(GL_LIGHTING);
 			glBegin(GL_LINES);
-				#define __ONEWIRE(a,b) glVertex3v(t->v[a]);glVertex3v(t->v[b])
-					__ONEWIRE(0,1);__ONEWIRE(0,2);__ONEWIRE(0,3);__ONEWIRE(1,2);__ONEWIRE(1,3);__ONEWIRE(2,3);
-				#undef __ONEWIRE
+				glOneWire(t, 0, 1);
+				glOneWire(t, 0, 2);
+				glOneWire(t, 0, 3);
+				glOneWire(t, 1, 2);
+				glOneWire(t, 1, 3);
+				glOneWire(t, 2, 3);
 			glEnd();
 		}
 		else
 		{
-			Vector3r center = (t->v[0]+t->v[1]+t->v[2]+t->v[3])*.25, faceCenter, n;
 			glDisable(GL_CULL_FACE); glEnable(GL_LIGHTING);
 			glBegin(GL_TRIANGLES);
-				#define __ONEFACE(a,b,c) n=(t->v[b]-t->v[a]).cross(t->v[c]-t->v[a]); n.normalize(); faceCenter=(t->v[a]+t->v[b]+t->v[c])/3.; if((faceCenter-center).dot(n)<0)n=-n; glNormal3v(n); glVertex3v(t->v[a]); glVertex3v(t->v[b]); glVertex3v(t->v[c]);
-					__ONEFACE(0,2,1);
-					__ONEFACE(0,1,3);
-					__ONEFACE(1,2,3);
-					__ONEFACE(0,3,2);
-				#undef __ONEFACE
+				glOneFace (t, 0,2,1);
+				glOneFace (t, 0,1,3);
+				glOneFace (t, 1,2,3);
+				glOneFace (t, 0,3,2);
 			glEnd();
 		}
 	}