Initial commit
authormorozov@geomdm.geomagic.com
Mon, 12 Jun 2006 09:33:54 -0400
changeset 0 1168ca87fd4e
child 1 9ac2dc8d1e24
Initial commit
VEFViewer.cpp
VEFViewer.h
VEFViewer.pro
main.cpp
viewerInterface.Qt4.ui
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VEFViewer.cpp	Mon Jun 12 09:33:54 2006 -0400
@@ -0,0 +1,170 @@
+#include "VEFViewer.h"
+#include <QtDebug>
+#include <cstdio>
+
+VEFViewer::VEFViewer(QWidget* parent): QGLViewer(parent)
+{
+	restoreStateFromFile();
+}
+
+void VEFViewer::addFile(QString s)
+{
+	GLuint display_list;
+	
+	QFileInfo fi(s);
+	QString ext = fi.suffix();
+
+	if (ext == "vrt")
+		display_list = readVertexFile(s);
+	else if (ext == "edg")
+		display_list = readEdgeFile(s);
+	else if (ext == "stl")
+		display_list = readSTLFile(s);
+	else
+		return;
+	
+	modelList->addItem(new QDisplayListLWI(fi.baseName(), display_list));
+}
+
+// Draws a spiral
+void VEFViewer::draw()
+{
+	for (int i = 0; i < modelList->count(); i++)
+	{
+		QDisplayListLWI* dl = static_cast<QDisplayListLWI*>(modelList->item(i));
+		glColor3f(1., 1., 1.);
+		glCallList(dl->getDisplayList());
+	}
+}
+
+QString VEFViewer::helpString() const
+{
+  QString text("<h2>VEFViewer</h2>");
+  text += "Displays vertices (vrt), edges (edg), and face (stl) files.";
+  return text;
+}
+
+void VEFViewer::keyPressEvent(QKeyEvent* e) 
+{
+	const Qt::KeyboardModifiers modifiers = e->modifiers();
+
+	bool handled = false;
+	if ((e->key() == Qt::Key_O) && (modifiers == Qt::NoButton))
+	{
+		QString s = QFileDialog::getOpenFileName(this, "Choose file to open", ".", "3D data (*.vrt *.edg *.stl)");
+		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)
+{
+	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_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();
+	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)
+{
+	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;
+}
+
+void VEFViewer::updateMinMax(float x, float y, float z)
+{
+	if (x < min.x)	min.x = x;
+	if (y < min.y)	min.y = y;
+	if (z < min.z)	min.z = z;
+	
+	if (x > max.x)	max.x = x;
+	if (y > max.y)	max.y = y;
+	if (z > max.z)	max.z = z;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VEFViewer.h	Mon Jun 12 09:33:54 2006 -0400
@@ -0,0 +1,51 @@
+#ifndef __VEFVIEWER_H__
+#define __VEFVIEWER_H__
+
+#include <QGLViewer/qglviewer.h>
+#include <QFileDialog>
+#include <QListWidgetItem>
+
+class VEFViewer: public QGLViewer
+{
+	public:
+		VEFViewer(QWidget* parent);
+		void addFile(QString s);
+		void setModelList(QListWidget* l)			{ modelList = l; }
+		
+	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);
+		
+		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
+{
+	public:
+		QDisplayListLWI(QString name, GLuint dl):
+			QListWidgetItem(name), display_list(dl)
+		{}
+		GLuint getDisplayList() const	{ return display_list; }
+		~QDisplayListLWI()				{ glDeleteLists(display_list, 1); }
+
+	private:
+		GLuint display_list;
+				
+};
+
+#endif // __VEFVIEWER_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VEFViewer.pro	Mon Jun 12 09:33:54 2006 -0400
@@ -0,0 +1,15 @@
+TEMPLATE = app
+TARGET   = VEFViewer
+CONFIG  += qt opengl warn_on release thread
+
+HEADERS  = VEFViewer.h
+SOURCES  = VEFViewer.cpp main.cpp
+FORMS   *= viewerInterface.Qt4.ui
+
+# The rest of this configuration file is pretty complex since it tries to automatically
+# detect system paths and configuration. In your applications, you can probably simply use:
+unix:LIBS *= -lQGLViewer
+win32:LIBS *= QGLViewer222.lib (with Visual 6, use QGLViewer2.lib or QGLViewer.lib instead)
+
+# Used by Qt4 only. Adds appropriate include paths.
+QT += xml opengl
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jun 12 09:33:54 2006 -0400
@@ -0,0 +1,18 @@
+#include <qapplication.h>
+#include "ui_viewerInterface.Qt4.h"
+#include "VEFViewer.h"
+
+class ViewerInterface: public QDialog, public Ui::Dialog
+{
+	public:
+		ViewerInterface() { setupUi(this); viewer->setModelList(listWidget); }
+};
+
+int main(int argc, char** argv)
+{
+  QApplication application(argc,argv);
+  ViewerInterface vi;
+  vi.show();
+
+  return application.exec();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/viewerInterface.Qt4.ui	Mon Jun 12 09:33:54 2006 -0400
@@ -0,0 +1,57 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>Dialog</class>
+ <widget class="QDialog" name="Dialog" >
+  <property name="geometry" >
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>642</width>
+    <height>475</height>
+   </rect>
+  </property>
+  <property name="windowTitle" >
+   <string>VEFViewer</string>
+  </property>
+  <layout class="QVBoxLayout" >
+   <property name="margin" >
+    <number>9</number>
+   </property>
+   <property name="spacing" >
+    <number>6</number>
+   </property>
+   <item>
+    <widget class="QSplitter" name="" >
+     <property name="orientation" >
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <widget class="QListWidget" name="listWidget" >
+      <property name="sizePolicy" >
+       <sizepolicy>
+        <hsizetype>1</hsizetype>
+        <vsizetype>7</vsizetype>
+        <horstretch>0</horstretch>
+        <verstretch>0</verstretch>
+       </sizepolicy>
+      </property>
+     </widget>
+     <widget class="VEFViewer" name="viewer" />
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <pixmapfunction></pixmapfunction>
+ <customwidgets>
+  <customwidget>
+   <class>VEFViewer</class>
+   <extends>QWidget</extends>
+   <header>VEFViewer.h</header>
+   <container>0</container>
+   <pixmap></pixmap>
+  </customwidget>
+ </customwidgets>
+ <resources/>
+ <connections/>
+</ui>