1 /****************************************************************************
2  * MeshLab                                                           o o     *
3  * A versatile mesh processing toolbox                             o     o   *
4  *                                                                _   O  _   *
5  * Copyright(C) 2008                                                \/)\/    *
6  * Visual Computing Lab                                            /\/|      *
7  * ISTI - Italian National Research Council                           |      *
8  *                                                                    \      *
9  * All rights reserved.                                                      *
10  *                                                                           *
11  * This program is free software; you can redistribute it and/or modify      *
12  * it under the terms of the GNU General Public License as published by      *
13  * the Free Software Foundation; either version 2 of the License, or         *
14  * (at your option) any later version.                                       *
15  *                                                                           *
16  * This program is distributed in the hope that it will be useful,           *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19  * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20  * for more details.                                                         *
21  *                                                                           *
22  ****************************************************************************/
23 /****************************************************************************
24   History
25 $Log: edit_topo.h,v $
26 ****************************************************************************/
27 
28 #ifndef edit_topo_H
29 #define edit_topo_H
30 
31 #include <common/interfaces.h>
32 #include <meshlab/glarea.h>
33 
34 #include <stdlib.h>
35 
36 #include <wrap/gl/pick.h>
37 #include <wrap/gl/picking.h>
38 
39 #include "edit_topodialog.h"
40 #include "edit_topomeshbuilder.h"
41 
42 
43 //**************************************************************
44 //	class edit_topo
45 //		retopology main class
46 //
47 class edit_topo : public QObject, public MeshEditInterface
48 {
49 	Q_OBJECT
50 	Q_INTERFACES(MeshEditInterface)
51 
52 public:
53 	edit_topo();
54 	virtual ~edit_topo();
55 
56 	static const QString Info();
57 	virtual bool StartEdit(MeshModel &, GLArea *);
58 	virtual void EndEdit(MeshModel &, GLArea *);
59 	virtual void Decorate(MeshModel &, GLArea *);
60 	virtual void mousePressEvent(QMouseEvent *, MeshModel &, GLArea * );
61 	virtual void mouseMoveEvent(QMouseEvent *, MeshModel &, GLArea * );
62 	virtual void mouseReleaseEvent(QMouseEvent *event, MeshModel &, GLArea * );
63 
64 	// Mouse position tracking
65 	QPoint mousePos;
66 	int mouseRealY;
67 
68 	// retopology model builder object
69 	RetopMeshBuilder rm;
70 
71 	// simple counter for vertex naming
72 	int nameVtxCount;
73 
74 	// used to reDraw the model (force gla->update)
75 	bool reDraw;
76 	// to manage on click
77 	bool click;
78 	// to manage d&d
79 	bool drag_click;
80 	// to prevent new meshmodel generation after first algorithm use
81 	bool first_model_generated;
82 
83 	CMeshO::FacePointer currentFacePointer;
84 
85 	// user defined vertices stack
86 	// this simple stack contains all user selected
87 	// vertices. It's dinamically in the edit process
88 	QList<Vtx> stack;
89 	// user defined edges stack
90 	// this simple stack contains all user defined
91 	// edges. It's dinamically in the edit process
92 	QList<Edg> Estack;
93 	// user defined faces stack
94 	// this simple stack contains all user selected
95 	// faces. It's dinamically in the edit process
96 	QList<Fce> Fstack;
97 
98 	// temporary stack used in the d&d process
99 	QList<Fce> drag_stack;
100 	Vtx drag_vtx;
101 
102 	// used to draw "not found" closest vertices
103 	QList<Point3f> out;
104 
105 	Vtx lastPoint;
106 	Point3f cursorPoint;
107 
108 	Vtx connectStart;
109 	Vtx connectEnd;
110 	double _md;
111 
112 	// Topology definition methods
113 	bool isVertexVisible(Point3f v);
114 	bool getFaceAtMouse(MeshModel &m,CMeshO::FacePointer& val);
115 	bool getVertexAtMouse(MeshModel &m,CMeshO::VertexPointer& value);
116 	int  getNearest(QPointF center, QPointF *points,int num);
117 	float distancePointSegment(QPointF p, QPointF segmentP1,QPointF segmentP2);
118 	float distancePointPoint(QPointF P1, QPointF P2);
119 	bool pointInTriangle(const QPointF &p, const QPointF &a, const QPointF &b, const QPointF &c);
120 	bool getVisibleVertexNearestToMouse(QList<Vtx> list, Vtx &out);
121 	bool getVisibleEdgeNearestToMouse(QList<Edg> listE, Edg &ret);
122 
123 	// Decoration methods
124 	void drawFace(CMeshO::FacePointer fp);
125 	void drawPoint(MeshModel &m, float pSize, Color4b colorFront, Point3f p);
126 	void drawPoint(MeshModel &m, float pSize, Color4b colorFront, QList<Vtx> list);
127 	void drawLine(Color4b colorBack, Color4b colorFront, Point3f p1, Point3f p2);
128 	void drawLabel(QList<Vtx> list);
129 	void drawLabel(Vtx v);
130 
updateMatrixes()131 	inline void updateMatrixes()
132 	{
133 		glGetIntegerv(GL_VIEWPORT, viewport);
134 		glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
135 		glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
136 	}
137 
138 	// Ui managed functions (events)
139 public slots:
140 	void on_mesh_create();
141 	void on_update_request();
142 
143 private:
144 	GLArea *parentGla;
145 
146 	double mvmatrix[16];
147 	double projmatrix[16];
148 	GLint viewport[4];
149 
150 	// Edit modes
151 	void editAddVertex(MeshModel &m);
152 	void editAddVertexFree();
153 	void editDeleteVertex();
154 	void editConnectVertex();
155 	void editSelectFace();
156 	void editDeconnectEdge();
157 	void editDragAndDropVertex();
158 	void editEdgeSplit();
159 	void editEdgeCollapse();
160 
161 	// Edit decoration modes
162 	void editDecoStandard(MeshModel &m);
163 	void editDecoOnlyVertex(MeshModel &m);
164 	void editDecoDragAndDropVertex(MeshModel &m);
165 	void editDecoFaceSelect(MeshModel &m);
166 	void editDecoVertexSelect(MeshModel &m);
167 	void editDecoDeleteVertexConnect(MeshModel &m);
168 	void editDecoDeleteVertexSelect(MeshModel &m);
169 	void editDecoDeleteVertexDeconnect(MeshModel &m);
170 	void editDecoCollapse(MeshModel &m);
171 	void editDecoSplit(MeshModel &m);
172 
173 	edit_topodialog *edit_topodialogobj;
174 
175 	// Gui
176 	QDockWidget* dock;
177 };
178 
179 #endif
180