Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
GlComplexPolygon.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 #ifndef GLCOMPLEXPOLYGON_H
20 #define GLCOMPLEXPOLYGON_H
21 
22 #ifdef WIN32
23 #include <windows.h>
24 #endif
25 
26 #if defined(__APPLE__)
27 #include <OpenGL/gl.h>
28 #include <OpenGL/glu.h>
29 #else
30 #include <GL/gl.h>
31 #include <GL/glu.h>
32 #endif
33 
34 #ifndef CALLBACK
35 #define CALLBACK
36 #endif
37 
38 #include <vector>
39 #include <map>
40 #include <set>
41 
42 #include <tulip/Color.h>
43 #include <tulip/Coord.h>
44 #include <tulip/tulipconf.h>
45 #include <tulip/GlSimpleEntity.h>
46 
47 namespace tlp {
48 
49 typedef struct {
50  GLdouble x, y, z, r, g, b, a;
51 } VERTEX;
52 
53 void CALLBACK beginCallback(GLenum which, GLvoid *polygonData);
54 void CALLBACK errorCallback(GLenum errorCode);
55 void CALLBACK endCallback(GLvoid *polygonData);
56 void CALLBACK vertexCallback(GLvoid *vertex, GLvoid *polygonData);
57 void CALLBACK combineCallback(GLdouble coords[3], VERTEX *d[4], GLfloat w[4], VERTEX** dataOut, GLvoid *polygonData);
58 
59 /**
60  * @ingroup OpenGL
61  * @brief Class to create a complex polygon (concave polygon or polygon with hole)
62  * If you want to create a complex polygon you have 4 constructors :
63  * Constructors with vector of coords : to create a complex polygon without hole
64  * - In this case you have two constructor : with and without outline color
65  * - You can create a polygon like this :
66  * \code
67  * vector <Coord> coords;
68  * coords.push_back(Coord(0,0,0));
69  * coords.push_back(Coord(10,0,0));
70  * coords.push_back(Coord(10,10,0));
71  * coords.push_back(Coord(0,10,0));
72  * GlComplexPolygon *complexPolygon=new GlComplexPolygon(coords,Color(255,0,0,255));
73  * layer->addGlEntity(complexPolygon,"complexPolygon");
74  * \endcode
75  *
76  * Constructors with vector of vector of Coords : to create a complex polygon with hole
77  * - In this case you have two constructor : with and without outline color
78  * - The first vector of coords is the polygon and others vector are holes
79  * - You can create a polygon with hole like this :
80  * \code
81  * vector <vector <Coord> > coords;
82  * vector <Coord> polygon;
83  * vector <Coord> hole;
84  * polygon.push_back(Coord(0,0,0));
85  * polygon.push_back(Coord(10,0,0));
86  * polygon.push_back(Coord(10,10,0));
87  * polygon.push_back(Coord(0,10,0));
88  * hole.push_back(Coord(4,4,0));
89  * hole.push_back(Coord(6,4,0));
90  * hole.push_back(Coord(6,6,0));
91  * hole.push_back(Coord(4,6,0));
92  * coords.push_back(polygon);
93  * coords.push_back(hole);
94  * GlComplexPolygon *complexPolygon=new GlComplexPolygon(coords,Color(255,0,0,255));
95  * layer->addGlEntity(complexPolygon,"complexPolygon");
96  * \endcode
97  *
98  * In constructors you can specify the polygon border style : polygonEdgesType parameter (0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves)
99  * You can also specify the texture name if you want to create a textured complex polygon
100  *
101  * In complex polygon you can add a smooth border : see activateQuadBorder(..) function
102  * And you can specify the texture zoom : see setTextureZoom(...) function
103  */
104 class TLP_GL_SCOPE GlComplexPolygon : public GlSimpleEntity {
105 
106  friend void CALLBACK beginCallback(GLenum which, GLvoid *polygonData);
107  friend void CALLBACK errorCallback(GLenum errorCode);
108  friend void CALLBACK endCallback(GLvoid *polygonData);
109  friend void CALLBACK vertexCallback(GLvoid *vertex, GLvoid *polygonData);
110  friend void CALLBACK combineCallback(GLdouble coords[3], VERTEX *d[4], GLfloat w[4], VERTEX** dataOut, GLvoid *polygonData);
111 
112 public:
113  /**
114  * @brief Default constructor
115  * @warning don't use this constructor if you want to create a complex polygon, see others constructors
116  */
118  /**
119  * @brief Constructor with a vector of coords, a fill color, a polygon edges type(0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves) and a textureName if you want
120  */
121  GlComplexPolygon(const std::vector<Coord> &coords,Color fcolor,int polygonEdgesType=0,const std::string &textureName = "");
122  /**
123  * @brief Constructor with a vector of coords, a fill color, an outline color, a polygon edges type(0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves) and a textureName if you want
124  */
125  GlComplexPolygon(const std::vector<Coord> &coords,Color fcolor,Color ocolor,int polygonEdgesType=0,const std::string &textureName = "");
126  /**
127  * @brief Constructor with a vector of vector of coords (the first vector of coord is the polygon and others vectors are holes in polygon), a fill color, a polygon edges type(0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves) and a textureName if you want
128  */
129  GlComplexPolygon(const std::vector<std::vector<Coord> >&coords,Color fcolor,int polygonEdgesType=0,const std::string &textureName = "");
130  /**
131  * @brief Constructor with a vector of vector of coords (the first vector of coord is the polygon and others vectors are holes in polygon), a fill color, an outline color a polygon edges type(0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves) and a textureName if you want
132  */
133  GlComplexPolygon(const std::vector<std::vector<Coord> >&coords,Color fcolor,Color ocolor,int polygonEdgesType=0,const std::string &textureName = "");
134 
135  virtual ~GlComplexPolygon() {}
136 
137  /**
138  * @brief Draw the complex polygon
139  */
140  virtual void draw(float lod,Camera *camera);
141 
142  /**
143  * @brief Set if the polygon is outlined or not
144  */
145  void setOutlineMode(const bool);
146 
147  /**
148  * @brief Set size of outline
149  */
150  void setOutlineSize(double size);
151 
152  /**
153  * @brief Get fill color of GlComplexPolygon
154  */
155  Color getFillColor() const {
156  return fillColor;
157  }
158 
159  /**
160  * @brief Set fill color of GlComplexPolygon
161  */
162  void setFillColor(const Color &color) {
163  fillColor=color;
164  }
165 
166  /**
167  * @brief Get outline color of GlComplexPolygon
168  */
169  Color getOutlineColor() const {
170  return outlineColor;
171  }
172 
173  /**
174  * @brief Set outline color of GlComplexPolygon
175  */
176  void setOutlineColor(const Color &color) {
177  outlineColor=color;
178  }
179 
180  /**
181  * @brief Get the texture zoom factor
182  */
183  float getTextureZoom() {
184  return textureZoom;
185  }
186 
187  /**
188  * @brief Set the texture zoom factor
189  *
190  * By default if you have a polygon with a size bigger than (1,1,0) the texture will be repeated
191  * If you want to don't have this texture repeat you have to modify texture zoom
192  * For example if you have a polygon with coords ((0,0,0),(5,0,0),(5,5,0),(0,5,0)) you can set texture zoom to 5. to don't have texture repeat
193  */
194  void setTextureZoom(float zoom) {
195  textureZoom=zoom;
196  runTesselation();
197  }
198 
199  /**
200  * @brief Get the textureName
201  */
202  std::string getTextureName();
203 
204  /**
205  * @brief Set the textureName
206  */
207  void setTextureName(const std::string &name);
208 
209  /**
210  * @brief Draw a thick (textured) border around the polygon.
211  *
212  * The graphic card must support geometry shader to make this feature to work.
213  * The position parameter determines the way the border is drawn (depending on the polygon points ordering):
214  * - 0 : the border is drawn outside (or inside) the polygon
215  * - 1 : the border is centered on the polygon outline
216  * - 2 : the border is drawn inside (or outside) the polygon
217  *
218  * The texCoordFactor parameter determines the way the texture is applied : if < 1, the texture will be expanded and > 1, the texture will be compressed
219  * The polygonId parameter determines on which contour of the polygon, the border will be applied
220  */
221  void activateQuadBorder(const float borderWidth, const Color &color, const std::string &texture = "", const int position = 1,
222  const float texCoordFactor = 1.f, const int polygonId = 0);
223 
224  /**
225  * @brief Desactivate the textured quad border
226  */
227  void desactivateQuadBorder(const int polygonId = 0);
228 
229  QStringList propertiesNames() const;
230 
231  QVariantList propertiesQVariant() const;
232 
233  void setProperty(const QString &name, const QVariant &value);
234 
235  /**
236  * @brief Translate entity
237  */
238  virtual void translate(const Coord& mouvement);
239 
240  /**
241  * @brief Function to export data and type outString (in XML format)
242  */
243  virtual void getXML(std::string &outString);
244 
245  /**
246  * @brief Function to export data in outString (in XML format)
247  */
248  virtual void getXMLOnlyData(std::string &outString);
249 
250  /**
251  * @brief Function to set data with inString (in XML format)
252  */
253  virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
254 
255  const std::vector<std::vector<Coord> > &getPolygonSides() const {
256  return points;
257  }
258 
259 
260 protected:
261 
262  /**
263  * @brief Add a new point in polygon
264  */
265  virtual void addPoint(const Coord& point);
266  /**
267  * @brief Begin a new hole in the polygon
268  */
269  virtual void beginNewHole();
270 
271  void runTesselation();
272  void createPolygon(const std::vector<Coord> &coords,int polygonEdgesType);
273  void startPrimitive(GLenum primitive);
274  void endPrimitive();
275  void addVertex(const Coord &vertexCoord, const Vec2f &vertexTexCoord);
276  VERTEX *allocateNewVertex();
277 
278  std::vector<std::vector<Coord> > points;
279  std::vector<std::vector<GLfloat> > pointsIdx;
280  std::set<GLenum> primitivesSet;
281  std::map<GLenum, std::vector<Coord> > verticesMap;
282  std::map<GLenum, std::vector<Vec2f> > texCoordsMap;
283  std::map<GLenum, std::vector<int> >startIndicesMap;
284  std::map<GLenum, std::vector<int> >verticesCountMap;
285  std::vector<VERTEX *> allocatedVertices;
286  GLenum currentPrimitive;
287  int nbPrimitiveVertices;
288  int currentVector;
289  bool outlined;
290  Color fillColor;
291  Color outlineColor;
292  double outlineSize;
293  std::string textureName;
294  float textureZoom;
295  std::vector<bool> quadBorderActivated;
296  std::vector<float> quadBorderWidth;
297  std::vector<Color> quadBorderColor;
298  std::vector<std::string> quadBorderTexture;
299  std::vector<int> quadBorderPosition;
300  std::vector<float> quadBorderTexFactor;
301 };
302 
303 }
304 #endif