VEFViewer.h
author Dmitriy Morozov <morozov@cs.duke.edu>
Mon, 12 Jun 2006 14:43:47 -0400
changeset 2 f3516f064329
parent 1 9ac2dc8d1e24
child 3 981001e95e19
permissions -rw-r--r--
Triangles stored explicitly, selection, normals computed explicitly, lights under control

#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; }
		
		void updateMinMax(float x, float y, float z);
		void init();
		
	protected:
		virtual void draw();
		virtual QString helpString() const;
		virtual void keyPressEvent(QKeyEvent *e);

		virtual void drawWithNames();
		virtual void postSelection(const QPoint& point);

	private:
		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 draw() const =0;
		virtual void drawWithNames(int offset) const =0;
		virtual int numElements() const =0;
		virtual void highlight(int selected) =0;

	protected:
		GLuint display_list;
				
};

class QVerticesLWI: public QDisplayListLWI
{
	public:
		QVerticesLWI(QString name, FILE* file, VEFViewer* v);
		virtual void draw() const;
		virtual void drawWithNames(int offset) const;
		virtual int numElements() const;
		virtual void highlight(int selected);
};

class Triangle;
class QFacesLWI: public QDisplayListLWI
{
	public:
		QFacesLWI(QString name, FILE* file, VEFViewer* v);
		virtual void draw() const;
		virtual void drawWithNames(int offset) const;
		virtual int numElements() const;
		virtual void highlight(int selected);

	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);
		qglviewer::Vec computeNormal(qglviewer::Vec v0, qglviewer::Vec v1, qglviewer::Vec v2) const;

		typedef 	std::vector<Triangle>		TrianglesContainer;
		TrianglesContainer triangles;

		int highlighted;
};

class QEdgesLWI: public QDisplayListLWI
{
	public:
		QEdgesLWI(QString name, FILE* file, VEFViewer* v);
		virtual void draw() const;
		virtual void drawWithNames(int offset) const;
		virtual int numElements() const;
		virtual void highlight(int selected);

	private:
		int readEdge (FILE* file,
                      float& x0, float& y0, float& z0,
                      float& x1, float& y1, float& z1);
};

class Triangle
{
	public:
		Triangle(float nxx, float nyy, float nzz,
				 float xx0, float yy0, float zz0,
				 float xx1, float yy1, float zz1,
				 float xx2, float yy2, float zz2):
			nx(nxx), ny(nyy), nz(nzz),
			x0(xx0), y0(yy0), z0(zz0),
			x1(xx1), y1(yy1), z1(zz1),
			x2(xx2), y2(yy2), z2(zz2)
		{}

		Triangle(const Triangle& other):
			nx(other.nx), ny(other.ny), nz(other.nz),
			x0(other.x0), y0(other.y0), z0(other.z0),
			x1(other.x1), y1(other.y1), z1(other.z1),
			x2(other.x2), y2(other.y2), z2(other.z2)
		{}

		void operator=(const Triangle& other)
		{
			nx = other.nx; ny = other.ny; nz = other.nz;
			x0 = other.x0; y0 = other.y0; z0 = other.z0;
			x1 = other.x1; y1 = other.y1; z1 = other.z1;
			x2 = other.x2; y2 = other.y2; z2 = other.z2;
		}

		float nx, ny, nz;
		float x0, y0, z0;
		float x1, y1, z1;
		float x2, y2, z2;
};


#endif // __VEFVIEWER_H__