Reads and displays vertices, edges, and faces
authormorozov@geomdm.geomagic.com
Mon, 12 Jun 2006 10:46:00 -0400
changeset 1 9ac2dc8d1e24
parent 0 1168ca87fd4e
child 2 f3516f064329
Reads and displays vertices, edges, and faces
VEFViewer.cpp
VEFViewer.h
--- a/VEFViewer.cpp	Mon Jun 12 09:33:54 2006 -0400
+++ b/VEFViewer.cpp	Mon Jun 12 10:46:00 2006 -0400
@@ -7,6 +7,13 @@
 	restoreStateFromFile();
 }
 
+void VEFViewer::init()
+{
+	setBackgroundColor(QColor(0xAA, 0xAA, 0xAA));
+	glEnable(GL_LIGHTING);
+	glEnable(GL_LIGHT0);
+}
+
 void VEFViewer::addFile(QString s)
 {
 	GLuint display_list;
@@ -15,25 +22,34 @@
 	QString ext = fi.suffix();
 
 	if (ext == "vrt")
-		display_list = readVertexFile(s);
+		readVertexFile(s);
 	else if (ext == "edg")
-		display_list = readEdgeFile(s);
+		readEdgeFile(s);
 	else if (ext == "stl")
-		display_list = readSTLFile(s);
+		readSTLFile(s);
 	else
 		return;
 	
-	modelList->addItem(new QDisplayListLWI(fi.baseName(), display_list));
+	setSceneBoundingBox(min, max);
+	setSceneCenter(center);
+	camera()->centerScene();
+	camera()->showEntireScene();
+
+	std::cout << "Min: " << min << std::endl;
+	std::cout << "Max: " << max << std::endl;
+	std::cout << "Center: " << center << std::endl;
 }
 
 // Draws a spiral
 void VEFViewer::draw()
 {
+	const GLfloat light_pos0[4] = {center.x, center.y, center.z, 1.0};
+	glLightfv(GL_LIGHT0, GL_POSITION, light_pos0);
+		
 	for (int i = 0; i < modelList->count(); i++)
 	{
 		QDisplayListLWI* dl = static_cast<QDisplayListLWI*>(modelList->item(i));
-		glColor3f(1., 1., 1.);
-		glCallList(dl->getDisplayList());
+		dl->display();
 	}
 }
 
@@ -55,108 +71,54 @@
 		if (!s.isNull())
 			addFile(s);
 	}
-	setSceneBoundingBox(min, max);
 
 	if (!handled)
 		QGLViewer::keyPressEvent(e);
 }
 
-GLuint VEFViewer::readVertexFile(QString s)
-{
-	FILE* file;
-	if ((file = fopen(s.toLocal8Bit().data(), "r")) == NULL)
-	{
-		qDebug() << "Could not open file " << s << " for reading";
-		return 0;
-	}
-
-	GLuint list = glGenLists(1);
-	glNewList(list, GL_COMPILE);
-	glBegin(GL_POINTS);
-	
-		float x,y,z;
-		int result = fscanf(file, "%f %f %f", &x, &y, &z);
-		while(result != EOF)
-		{
-			glVertex3f(x,y,z);
-			updateMinMax(x,y,z);
-			result = fscanf(file, "%f %f %f", &x, &y, &z);
-		}
-
-	glEnd();
-	glEndList();
-	fclose(file);
-	
-	return list;
-}
-
-GLuint VEFViewer::readEdgeFile(QString s)
-{
-}
-
-GLuint VEFViewer::readSTLFile(QString s)
+void VEFViewer::readVertexFile(QString s)
 {
 	FILE* file;
 	if ((file = fopen(s.toLocal8Bit().data(), "r")) == NULL)
 	{
 		qDebug() << "Could not open file " << s << " for reading";
-		return 0;
+		return;
+	}
+
+	QFileInfo fi(s);
+	modelList->addItem(new QVerticesLWI(fi.baseName(), file, this));
+	fclose(file);
+}
+
+void VEFViewer::readEdgeFile(QString s)
+{
+	FILE* file;
+	if ((file = fopen(s.toLocal8Bit().data(), "r")) == NULL)
+	{
+		qDebug() << "Could not open file " << s << " for reading";
+		return;
 	}
 
-	GLuint list = glGenLists(1);
-	glNewList(list, GL_COMPILE);
-	glBegin(GL_TRIANGLES);
-	
-		float nx,ny,nz;
-		float x0,y0,z0;
-		float x1,y1,z1;
-		float x2,y2,z2;
-
-		int result = readFacet(file, nx,ny,nz, x0,y0,z0, x1,y1,z1, x2,y2,z2);
-		//while(result != EOF)
-		//{
-			glNormal3f(nx,ny,nz);
-			
-			glVertex3f(x0,y0,z0);
-			glVertex3f(x1,y1,z1);
-			glVertex3f(x2,y2,z2);
-
-			std::cout << "Adding triangle" << std::endl;
-			std::cout << nx << " " << ny << " " << nz << std::endl;
-			std::cout << x0 << " " << y0 << " " << z0 << std::endl;
-			std::cout << x1 << " " << y1 << " " << z1 << std::endl;
-			std::cout << x2 << " " << y2 << " " << z2 << std::endl;
-			
-			updateMinMax(x0,y0,z0);
-			updateMinMax(x1,y1,z1);
-			updateMinMax(x2,y2,z2);
-
-		//	result = readFacet(file, nx,ny,nz, x0,y0,z0, x1,y1,z1, x2,y2,z2);
-		//}
-
-	glEnd();
-	glEndList();
+	QFileInfo fi(s);
+	modelList->addItem(new QEdgesLWI(fi.baseName(), file, this));
 	fclose(file);
-	
-	return list;
 }
 
-int  VEFViewer::readFacet(FILE* file,
-						  float& nx, float& ny, float& nz, 
-						  float& x0, float& y0, float& z0,
-						  float& x1, float& y1, float& z1,
-						  float& x2, float& y2, float& z2)
+void VEFViewer::readSTLFile(QString s)
 {
-	int result = fscanf(file, "facet normal %f %f %f\n", &nx, &ny, &nz);
-		result = fscanf(file, "outer loop\n");
-		result = fscanf(file, "vertex %f %f %f\n", &x0, &y0, &z0);
-		result = fscanf(file, "vertex %f %f %f\n", &x1, &y1, &z1);
-		result = fscanf(file, "vertex %f %f %f\n", &x2, &y2, &z2);
-		result = fscanf(file, "endloop\n");
-		result = fscanf(file, "endfacet\n");
-	return result;
+	FILE* file;
+	if ((file = fopen(s.toLocal8Bit().data(), "r")) == NULL)
+	{
+		qDebug() << "Could not open file " << s << " for reading";
+		return;
+	}
+
+	QFileInfo fi(s);
+	modelList->addItem(new QFacesLWI(fi.baseName(), file, this));
+	fclose(file);
 }
 
+
 void VEFViewer::updateMinMax(float x, float y, float z)
 {
 	if (x < min.x)	min.x = x;
@@ -166,5 +128,156 @@
 	if (x > max.x)	max.x = x;
 	if (y > max.y)	max.y = y;
 	if (z > max.z)	max.z = z;
+
+	updateWeightedCenter(x, y, z);
+}
+
+void VEFViewer::updateWeightedCenter(float x, float y, float z)
+{
+	vertex_count++;
+	center.x = float(vertex_count - 1)/vertex_count * center.x + x/vertex_count;
+	center.y = float(vertex_count - 1)/vertex_count * center.y + y/vertex_count;
+	center.z = float(vertex_count - 1)/vertex_count * center.z + z/vertex_count;
+}
+
+
+// QVertices
+void QVerticesLWI::display() const
+{
+	glColor3f(.5, .65, .65);
+	glCallList(display_list);
+}
+
+QVerticesLWI::QVerticesLWI(QString name, FILE* file, VEFViewer* v): 
+	QDisplayListLWI(name)
+{
+	display_list = glGenLists(1);
+	glNewList(display_list, GL_COMPILE);
+	glBegin(GL_POINTS);
+	
+		float x,y,z;
+		int result = fscanf(file, "%f %f %f", &x, &y, &z);
+		while(result != EOF)
+		{
+			glVertex3f(x,y,z);
+			v->updateMinMax(x,y,z);
+			result = fscanf(file, "%f %f %f", &x, &y, &z);
+		}
+
+	glEnd();
+	glEndList();
+}
+
+
+// QFaces
+void QFacesLWI::display() const
+{
+	glColor3f(0., .4, .6);
+	glCallList(display_list);
 }
 
+QFacesLWI::QFacesLWI(QString name, FILE* file, VEFViewer* v):
+	QDisplayListLWI(name)
+{
+	display_list = glGenLists(1);
+	glNewList(display_list, GL_COMPILE);
+	glBegin(GL_TRIANGLES);
+	
+		float nx,ny,nz;
+		float x0,y0,z0;
+		float x1,y1,z1;
+		float x2,y2,z2;
+
+		int result = readFacet(file, nx,ny,nz, x0,y0,z0, x1,y1,z1, x2,y2,z2);
+		while(result != EOF)
+		{
+			glNormal3f(nx,ny,nz);
+			
+			glVertex3f(x0,y0,z0);
+			glVertex3f(x1,y1,z1);
+			glVertex3f(x2,y2,z2);
+
+#if 0
+			std::cout << "Adding triangle" << std::endl;
+			std::cout << nx << " " << ny << " " << nz << std::endl;
+			std::cout << x0 << " " << y0 << " " << z0 << std::endl;
+			std::cout << x1 << " " << y1 << " " << z1 << std::endl;
+			std::cout << x2 << " " << y2 << " " << z2 << std::endl;
+#endif
+			
+			v->updateMinMax(x0,y0,z0);
+			v->updateMinMax(x1,y1,z1);
+			v->updateMinMax(x2,y2,z2);
+
+			result = readFacet(file, nx,ny,nz, x0,y0,z0, x1,y1,z1, x2,y2,z2);
+		}
+
+	glEnd();
+	glEndList();
+}
+
+int QFacesLWI::readFacet(FILE* file,
+						  float& nx, float& ny, float& nz, 
+						  float& x0, float& y0, float& z0,
+						  float& x1, float& y1, float& z1,
+						  float& x2, float& y2, float& z2)
+{
+	
+		
+	int result = fscanf(file, "%*s %*s %f %f %f", &nx, &ny, &nz);
+		result = fscanf(file, "%*s %*s");
+		result = fscanf(file, "%*s %f %f %f", &x0, &y0, &z0);
+		result = fscanf(file, "%*s %f %f %f", &x1, &y1, &z1);
+		result = fscanf(file, "%*s %f %f %f", &x2, &y2, &z2);
+		result = fscanf(file, "%*s");
+		result = fscanf(file, "%*s");
+	return result;
+}
+
+// QEdges
+void QEdgesLWI::display() const
+{
+	glColor3f(.6, .2, .6);
+	glCallList(display_list);
+}
+
+QEdgesLWI::QEdgesLWI(QString name, FILE* file, VEFViewer* v):
+	QDisplayListLWI(name)
+{
+	display_list = glGenLists(1);
+	glNewList(display_list, GL_COMPILE);
+	glBegin(GL_LINES);
+	
+		float nx,ny,nz;
+		float x0,y0,z0;
+		float x1,y1,z1;
+
+		int result = readEdge(file, x0,y0,z0, x1,y1,z1);
+		while(result != EOF)
+		{
+			glVertex3f(x0,y0,z0);
+			glVertex3f(x1,y1,z1);
+
+			v->updateMinMax(x0,y0,z0);
+			v->updateMinMax(x1,y1,z1);
+
+			result = readEdge(file, x0,y0,z0, x1,y1,z1);
+		}
+
+	glEnd();
+	glEndList();
+}
+
+int QEdgesLWI::readEdge  (FILE* file,
+						  float& x0, float& y0, float& z0,
+						  float& x1, float& y1, float& z1)
+{
+	int result = fscanf(file, "%f %f %f", &x0, &y0, &z0);
+		result = fscanf(file, "%f %f %f", &x1, &y1, &z1);
+
+	std::cout << x0 << ' ' << y0 << ' ' << z0 << std::endl;
+	std::cout << x1 << ' ' << y1 << ' ' << z1 << std::endl;
+	return result;
+}
+
+
--- a/VEFViewer.h	Mon Jun 12 09:33:54 2006 -0400
+++ b/VEFViewer.h	Mon Jun 12 10:46:00 2006 -0400
@@ -12,40 +12,74 @@
 		void addFile(QString s);
 		void setModelList(QListWidget* l)			{ modelList = l; }
 		
+		void updateMinMax(float x, float y, float z);
+		void init();
+		
 	protected:
 		virtual void draw();
 		virtual QString helpString() const;
 		virtual void keyPressEvent(QKeyEvent *e);
 
 	private:
-		GLuint readVertexFile(QString s);
-		GLuint readEdgeFile(QString s);
-		GLuint readSTLFile(QString s);
-		void updateMinMax(float x, float y, float z);
+		void readVertexFile(QString s);
+		void readEdgeFile(QString s);
+		void readSTLFile(QString s);
+		void updateWeightedCenter(float x, float y, float z);
 		
+		QListWidget* modelList;
+
+		qglviewer::Vec min, max, center;
+		unsigned int vertex_count;
+};
+
+class QDisplayListLWI: public QListWidgetItem
+{
+	public:
+		QDisplayListLWI(QString name):
+			QListWidgetItem(name)
+		{}
+		GLuint getDisplayList() const	{ return display_list; }
+		~QDisplayListLWI()				{ glDeleteLists(display_list, 1); }
+
+		virtual void display() const =0;
+
+	protected:
+		GLuint display_list;
+				
+};
+
+class QVerticesLWI: public QDisplayListLWI
+{
+	public:
+		QVerticesLWI(QString name, FILE* file, VEFViewer* v);
+		virtual void display() const;
+};
+
+class QFacesLWI: public QDisplayListLWI
+{
+	public:
+		QFacesLWI(QString name, FILE* file, VEFViewer* v);
+		virtual void display() const;
+
+	private:
 		int readFacet(FILE* file,
 					  float& nx, float& ny, float& nz,
                       float& x0, float& y0, float& z0,
                       float& x1, float& y1, float& z1,
                       float& x2, float& y2, float& z2);
-		
-		QListWidget* modelList;
-
-		qglviewer::Vec min, max;
 };
 
-class QDisplayListLWI: public QListWidgetItem
+class QEdgesLWI: public QDisplayListLWI
 {
 	public:
-		QDisplayListLWI(QString name, GLuint dl):
-			QListWidgetItem(name), display_list(dl)
-		{}
-		GLuint getDisplayList() const	{ return display_list; }
-		~QDisplayListLWI()				{ glDeleteLists(display_list, 1); }
+		QEdgesLWI(QString name, FILE* file, VEFViewer* v);
+		virtual void display() const;
 
 	private:
-		GLuint display_list;
-				
+		int readEdge (FILE* file,
+                      float& x0, float& y0, float& z0,
+                      float& x1, float& y1, float& z1);
 };
 
+
 #endif // __VEFVIEWER_H__