VEFViewer.h
author Dmitriy Morozov <morozov@cs.duke.edu>
Sat, 29 Jul 2006 23:25:10 -0400
changeset 6 8752e612ec07
parent 3 981001e95e19
child 10 240ec9bb3d92
permissions -rw-r--r--
Added color picker functionality to the context menu

#ifndef __VEFVIEWER_H__
#define __VEFVIEWER_H__

#include <QGLViewer/qglviewer.h>
#include <QFileDialog>
#include <QListWidgetItem>
#include <QMenu>
#include <QColorDialog>

class VEFViewer: public QGLViewer
{
	Q_OBJECT
		
	public:
		VEFViewer(QWidget* parent);
		void addFile(QString s);
		void setModelList(QListWidget* 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;

	public slots:
		void listItemDoubleClicked(QListWidgetItem* item);
		void customContextMenuRequested(const QPoint& pos);
};

class QDisplayListLWI: public QObject, public QListWidgetItem
{
	Q_OBJECT

	public:
		QDisplayListLWI(QString name, QColor color_):
			QListWidgetItem(name), color(color_), visible(false)
		{ toggleVisible(); }
		GLuint getDisplayList() const	{ return display_list; }
		~QDisplayListLWI()				{ glDeleteLists(display_list, 1); }

		virtual void draw() const;
		virtual void drawWithNames(int offset) const =0;
		virtual int numElements() const =0;
		virtual void highlight(int selected) =0;
		virtual void setupMenu(QMenu& m) const
		{
			QAction* visible = m.addAction("Visible");
			connect(visible, SIGNAL(triggered()), this, SLOT(toggleVisible()));
			visible->setCheckable(true);
			visible->setChecked(isVisible());
			
			QAction* color = m.addAction("Pick color");
			connect(color, SIGNAL(triggered()), this, SLOT(changeColor()));
		}
		
		bool isVisible() const			{ return visible; }

	public slots:
		void setVisible(bool vis = true)	{ visible = !vis; adjustFont(); }
		void toggleVisible()				{ visible = !visible; adjustFont(); }
		void changeColor()					
		{ QColor nw = QColorDialog::getColor(color); if (nw.isValid()) color = nw; }

	protected:
		GLuint display_list;
		bool visible;
		QColor color;

	private:
		void adjustFont()				{ QFont f; if (visible)	f.setBold(true); setFont(f); }
				
};

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

class Triangle;
class QFacesLWI: public QDisplayListLWI
{
	Q_OBJECT
		
	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);
		virtual void setupMenu(QMenu& m) const
		{
			QDisplayListLWI::setupMenu(m);
			
			QAction* wireframe_action = m.addAction("Wireframe");
			connect(wireframe_action, SIGNAL(triggered()), this, SLOT(toggleWireframe()));
			wireframe_action->setCheckable(true);
			wireframe_action->setChecked(isWireframe());
		}
		bool isWireframe() const	{ return isWireframe_; }

	public slots:
		void toggleWireframe()		{ isWireframe_ = !isWireframe_; }
		
	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;
		void drawTriangles(GLenum mode) const;

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

		bool isWireframe_;
		GLuint wireframe_display_list;
		int highlighted;
};

class QEdgesLWI: public QDisplayListLWI
{
	public:
		QEdgesLWI(QString name, FILE* file, VEFViewer* v);
		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__