-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShapeGraphicsItem.h
295 lines (234 loc) · 9.01 KB
/
ShapeGraphicsItem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* ShapeGraphicsItem.h
*
* (c) 2015 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2016 Dame Diongue -- baydamd(@)gmail(.)com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHAPE_GRAPHICS_ITEM_H_
#define SHAPE_GRAPHICS_ITEM_H_
#include <QtGlobal>
#if __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include "Shapes.h"
#include "Paint.h"
#include "Mapping.h"
#include "MapperGLCanvas.h"
namespace mmp {
class MapperGLCanvas;
class ShapeControlPainter;
/**
* Represents a shape as part of a scene graph in one of the GL canvas.
* A ShapeGraphicsItem contains a Mapping and is identified as either an
* input (source) or an output (destination).
*/
class ShapeGraphicsItem : public QGraphicsItem
{
Q_DECLARE_TR_FUNCTIONS(ShapeGraphicsItem)
public:
typedef QSharedPointer<ShapeGraphicsItem> ptr;
protected:
ShapeGraphicsItem(Mapping::ptr mapping, bool output=true);
public:
virtual ~ShapeGraphicsItem() {}
public:
/// Returns the MShape (depends on mapping and whether this is an input or output).
MShape::ptr getShape() const { return _shape.toStrongRef(); }
/// Returns the mapping.
Mapping::ptr getMapping() const { return _mapping.toStrongRef(); }
/// Returns the control painter (see below).
QSharedPointer<ShapeControlPainter> getControlPainter() { return _controlPainter; }
/// Return whether the item is in the destination/output (true) or source/input (false).
bool isOutput() const { return _output; }
/// Returns pointer to GL canvas.
MapperGLCanvas* getCanvas() const;
/// Returns whether the mapping this shape is associated with is currently selected.
bool isMappingCurrent() const;
/// Returns whether the mapping this shape is associated should be visible.
bool isMappingVisible() const;
/// Returns the bounding rectangle of this item.
virtual QRectF boundingRect() const { return shape().boundingRect(); }
// virtual QPainterPath shape() const;
// virtual void paint(QPainter *painter,
// const QStyleOptionGraphicsItem *option, QWidget *widget);
// virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
/// Built-in function: paints the shape.
virtual void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget);
protected:
/// Main paint function that needs to be overriden (called by paint()).
virtual void _doPaint(QPainter *painter, const QStyleOptionGraphicsItem *option) = 0;
/// Can be overriden to perform operations *before* _doPaint() is called.
virtual void _prePaint(QPainter *painter, const QStyleOptionGraphicsItem *option)
{ Q_UNUSED(painter); Q_UNUSED(option); }
/// Can be overriden to perform operations *after* _doPaint() is called.
virtual void _postPaint(QPainter *painter, const QStyleOptionGraphicsItem *option)
{ Q_UNUSED(painter); Q_UNUSED(option); }
protected:
QWeakPointer<Mapping> _mapping;
QWeakPointer<MShape> _shape;
QSharedPointer<ShapeControlPainter> _controlPainter;
bool _output;
};
class ColorGraphicsItem : public ShapeGraphicsItem
{
protected:
ColorGraphicsItem(Mapping::ptr mapping, bool output=true)
: ShapeGraphicsItem(mapping, output) {}
public:
virtual ~ColorGraphicsItem() {}
protected:
virtual void _prePaint(QPainter *painter,
const QStyleOptionGraphicsItem *option);
};
/// Graphics item for colored polygons (eg. quad, triangle).
class PolygonColorGraphicsItem : public ColorGraphicsItem
{
public:
PolygonColorGraphicsItem(Mapping::ptr mapping, bool output=true);
virtual ~PolygonColorGraphicsItem() {}
virtual QPainterPath shape() const;
protected:
virtual void _doPaint(QPainter *painter,
const QStyleOptionGraphicsItem *option);
};
/**
* Graphics item for textured color.
*/
class MeshColorGraphicsItem : public PolygonColorGraphicsItem
{
public:
MeshColorGraphicsItem(Mapping::ptr mapping, bool output=true);
virtual ~MeshColorGraphicsItem(){}
protected:
virtual void _doPaint(QPainter *painter,
const QStyleOptionGraphicsItem *option);
};
/// Graphics item for colored polygons (eg. quad, triangle).
class EllipseColorGraphicsItem : public ColorGraphicsItem
{
public:
EllipseColorGraphicsItem(Mapping::ptr mapping, bool output=true);
virtual ~EllipseColorGraphicsItem() {}
virtual QPainterPath shape() const;
protected:
virtual void _doPaint(QPainter *painter,
const QStyleOptionGraphicsItem *option);
};
/// Abstract class for texture graphics items.
class TextureGraphicsItem : public ShapeGraphicsItem
{
public:
TextureGraphicsItem(Mapping::ptr mapping, bool output=true);
virtual ~TextureGraphicsItem() {}
protected:
virtual void _doPaint(QPainter *painter, const QStyleOptionGraphicsItem *option);
void _prePaint(QPainter* painter, const QStyleOptionGraphicsItem *option);
void _postPaint(QPainter* painter, const QStyleOptionGraphicsItem *option);
virtual void _doDrawOutput(QPainter* painter) = 0;
virtual void _doDrawInput(QPainter* painter);
protected:
QWeakPointer<TextureMapping> _textureMapping;
QWeakPointer<Texture> _texture;
QWeakPointer<MShape> _inputShape;
};
/// Graphics item for textured polygons (eg. triangles).
class PolygonTextureGraphicsItem : public TextureGraphicsItem
{
public:
PolygonTextureGraphicsItem(Mapping::ptr mapping, bool output=true);
virtual ~PolygonTextureGraphicsItem(){}
public:
virtual QPainterPath shape() const;
virtual QRectF boundingRect() const;
};
/// Graphics item for textured triangles.
class TriangleTextureGraphicsItem : public PolygonTextureGraphicsItem
{
public:
TriangleTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : PolygonTextureGraphicsItem(mapping, output) {}
virtual ~TriangleTextureGraphicsItem(){}
virtual void _doDrawOutput(QPainter* painter);
};
/**
* Graphics item for textured mesh.
* The drawing technique recursively subdivides the quad to approximate projective mapping and thus
* avoiding artifacts on the diagonals. Subdivided structure is cached to increase performance.
* Source: Oliveira, M. "Correcting Texture Mapping Errors Introduced by Graphics Hardware"
*/
class MeshTextureGraphicsItem : public PolygonTextureGraphicsItem
{
// Internal use (cache). A structure consisting of the input and output quads of mapping.
struct CacheQuadMapping {
Quad::ptr input;
Quad::ptr output;
};
// Internal use (cache). Contains a parent mapping and all its sub-mappings.
struct CacheQuadItem {
CacheQuadMapping parent;
QList<CacheQuadMapping> subQuads;
};
public:
MeshTextureGraphicsItem(Mapping::ptr mapping, bool output=true);
virtual ~MeshTextureGraphicsItem(){}
virtual void _doDrawOutput(QPainter* painter);
private:
/**
* Builds cache item recursively using the technique described in
* Oliveira, M. "Correcting Texture Mapping Errors Introduced by Graphics Hardware"
*/
void _buildCacheQuadItem(CacheQuadItem& item, const Quad::ptr& inputQuad, const Quad::ptr& outputQuad,
float outputArea, float inputThreshold = 0.0001f, float outputThreshold = 0.001f,
int minArea=MM::MESH_SUBDIVISION_MIN_AREA, int maxDepth=-1);
// Help function that returns four equal-size sub-quads from a quad.
QList<Quad::ptr> _split(const Quad& quad);
// Contains the current cache.
QVector<QVector<CacheQuadItem> > _cachedQuadItems;
int _nHorizontalQuads;
int _nVerticalQuads;
// True iff shape was being grabbed last time _buildCacheQuadItem() was called.
bool _wasGrabbing;
};
/// Graphics item for textured mesh.
class EllipseTextureGraphicsItem : public TextureGraphicsItem
{
static const int N_QUARTERS = 4;
class DrawingData {
public:
QPointF center;
QPointF controlCenter;
float horizontalRadius;
float verticalRadius;
float rotation;
float quarterAngles[N_QUARTERS];
DrawingData(const QSharedPointer<Ellipse>& ellipse);
float getSpanInQuarter(int quarter) const;
void setPointOfEllipseAtAngle(QPointF& point, float circularAngle);
};
public:
EllipseTextureGraphicsItem(Mapping::ptr mapping, bool output=true);
virtual ~EllipseTextureGraphicsItem(){}
virtual QPainterPath shape() const;
virtual QRectF boundingRect() const;
virtual void _doDrawOutput(QPainter* painter);
static void _setPointOfEllipseAtAngle(QPointF& point, const QPointF& center, float hRadius, float vRadius, float rotation, float circularAngle);
};
}
#endif