From ca1333ed7644c447ad84ef8f517ac4ce44300cd4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= <ulman@mpi-cbg.de>
Date: Mon, 10 Oct 2016 20:29:07 +0200
Subject: [PATCH] Added testing of surface fitting on a sphere. Seems to work
 well.

---
 cmath3d/TriangleMesh.cpp |  1 +
 cmath3d/TriangleMesh.h   | 17 +++++++++++++++++
 src/graphics.cpp         | 33 ++++++++++++++++++++++++++-------
 src/main.cpp             |  3 ++-
 4 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/cmath3d/TriangleMesh.cpp b/cmath3d/TriangleMesh.cpp
index 6f6a56b..b90eebd 100644
--- a/cmath3d/TriangleMesh.cpp
+++ b/cmath3d/TriangleMesh.cpp
@@ -526,6 +526,7 @@ bool ActiveMesh::GetPointOnQuadricSurface(const float x,const float y,
 
 	const float sqArg=b*b - 4*a*c;
 	if (sqArg < 0.f) return false;
+	if (a == 0.f) return false;
 
 	z1=(-b + sqrtf(sqArg)) / (2.f*a);
 	z2=(-b - sqrtf(sqArg)) / (2.f*a);
diff --git a/cmath3d/TriangleMesh.h b/cmath3d/TriangleMesh.h
index 2880433..4b3c5a9 100644
--- a/cmath3d/TriangleMesh.h
+++ b/cmath3d/TriangleMesh.h
@@ -46,6 +46,23 @@ class ActiveMesh
 	 */
 	int CalcQuadricSurface_Taubin(const int vertexID,
 	                              float (&coeffs)[10]);
+	int CalcQuadricSurface_sphere(const float radius,
+	                              const Vector3F& centre,
+	                              float (&coeffs)[10])
+	{
+		const float R=radius*radius;
+		coeffs[0]=-1.f + (centre.x*centre.x +centre.y*centre.y +centre.z*centre.z)/R;
+		coeffs[1]=-2.f*centre.x /R;
+		coeffs[2]=-2.f*centre.y /R;
+		coeffs[3]=-2.f*centre.z /R;
+		coeffs[4]=0.f;
+		coeffs[5]=0.f;
+		coeffs[6]=0.f;
+		coeffs[7]=1.f/R;
+		coeffs[8]=1.f/R;
+		coeffs[9]=1.f/R;
+		return(0);
+	}
 
 	/**
 	 * Calculate the 3rd coordinate (e.g., z) to complete a 3D point
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 7905c15..34cd595 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -149,7 +149,18 @@ void ActiveMesh::displayQuadricSurface(void)
 {
 	//get surface params
 	float surf_coeff[10];
-	CalcQuadricSurface_Taubin(VertexID,surf_coeff);
+	//CalcQuadricSurface_Taubin(VertexID,surf_coeff);
+	CalcQuadricSurface_sphere(5.0f,params.sceneCentre,surf_coeff);
+	std::cout << "\nsurface: " << surf_coeff[0] << " + "
+	          << surf_coeff[1] << "*x + "
+	          << surf_coeff[2] << "*y + "
+	          << surf_coeff[3] << "*z + "
+	          << surf_coeff[4] << "*xy + "
+	          << surf_coeff[5] << "*xz + "
+	          << surf_coeff[6] << "*yz + "
+	          << surf_coeff[7] << "*x*x + "
+	          << surf_coeff[8] << "*y*y + "
+	          << surf_coeff[9] << "*z*z = 0\n";
 
 	glPointSize(2.0f);
 	glBegin(GL_POINTS);
@@ -165,12 +176,16 @@ void ActiveMesh::displayQuadricSurface(void)
 
 	//FOR CYCLE BEGIN
 	//over all triangles
-	for (unsigned int t=0; t < neigsT.size(); ++t)
+	//for (unsigned int t=0; t < neigsT.size(); ++t)
+	for (unsigned int t=0; t < 2; ++t)
 	{
 		//determine triangle vertices
 		const Vector3FC& v1=Pos[ID[3*neigsT[t] +0]];
 		const Vector3FC& v2=Pos[ID[3*neigsT[t] +1]];
 		const Vector3FC& v3=Pos[ID[3*neigsT[t] +2]];
+		std::cout << "v1: (" << v1.x << "," << v1.y << "," << v1.z << ")\n";
+		std::cout << "v2: (" << v2.x << "," << v2.y << "," << v2.z << ")\n";
+		std::cout << "v3: (" << v3.x << "," << v3.y << "," << v3.z << ")\n";
 
 		//sweep (coarsely!) across current triangle
 		for (float c=0.1f; c <= 0.9f; c += 0.1f)
@@ -190,10 +205,14 @@ void ActiveMesh::displayQuadricSurface(void)
 			GetClosestPointOnQuadricSurface(point,surf_coeff);
 
 			//display the point
+			glColor3f(0.0f,1.0f,0.0f);
 			glVertex3f(point.x,point.y,point.z);
-			std::cout << "(" << origPoint.x << "," << origPoint.y << "," << origPoint.z
+			std::cout << "  (" << origPoint.x << "," << origPoint.y << "," << origPoint.z
 			          << ") -> ("
 			          << point.x << "," << point.y << "," << point.z << ")\n";
+
+			glColor3f(0.0f,1.0f,1.0f);
+			glVertex3f(origPoint.x,origPoint.y,origPoint.z);
 		}
 	}
 	//FOR CYCLE END
@@ -203,7 +222,7 @@ void ActiveMesh::displayQuadricSurface(void)
 
 void ActiveMesh::displayMesh(void)
 {
-	glColor3f(0.8f,0.8f,0.8f);
+	glColor3f(0.6f,0.6f,0.6f);
 
 	long unsigned int* id=ID.data();
 	for (unsigned int i=0; i < ID.size(); i+=3)
@@ -503,15 +522,15 @@ void reshape(int w, int h)
 void initializeGL(void)
 {
 	 int Argc=1;
-	 char msg[]="TRAgen: A Tool for Generation of Synthetic Time-Lapse Image Sequences of Living Cells";
+	 char msg[]="meshSurface GUI";
 	 char *Argv[10];
 	 Argv[0]=msg;
 
     glutInit(&Argc, Argv);
 
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
-    glutInitWindowSize(600, 600); 
-    glutInitWindowPosition(100, 100);
+    glutInitWindowSize(800, 800); 
+    glutInitWindowPosition(600, 100);
     glutCreateWindow(Argv[0]);
 
     glutKeyboardFunc(keyboard);
diff --git a/src/main.cpp b/src/main.cpp
index 60ac239..7cc5137 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,7 +17,8 @@ int main(void)
 
 	//load mesh
 	//char filename[]="../sample_input/samplecell.stl";
-	char filename[]="../sample_input/torus_100_30.stl";
+	//char filename[]="../sample_input/torus_100_30.stl";
+	char filename[]="../sample_input/sphere.stl";
 
 	int retval=mesh.ImportSTL(filename);
 	if (retval) 
-- 
GitLab