DGtal  1.5.beta
Viewer3D.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESSpace FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file Viewer3D.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  * @date 2011/01/03
23  *
24  * Implementation of inline methods defined in Viewer3D.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #ifdef _MSC_VER
35 #define NOMINMAX
36 #include <windows.h>
37 #include <GL/gl.h>
38 #include "DGtal/io/viewers/windows/GL/glext.h"
39 #endif
40 
41 #include "DGtal/io/viewers/Viewer3D.h"
42 
43 #include <algorithm>
44 #include <limits>
45 #include <QColor>
46 #include <QTextEdit>
47 #include <QMessageBox>
48 #include <QTextStream>
49 #include <QDir>
50 
51 #include <cstdlib>
52 #include "DGtal/io/CDrawableWithDisplay3D.h"
53 #include "DGtal/io/viewers/CDrawableWithViewer3D.h"
54 #include "DGtal/io/viewers/Viewer3DFactory.h"
55 #include "QGLViewer/manipulatedFrame.h"
56 #include "QGLViewer/manipulatedCameraFrame.h"
57 //////////////////////////////////////////////////////////////////////////////
58 
59 using namespace qglviewer;
60 
61 ///////////////////////////////////////////////////////////////////////////////
62 // Implementation of inline methods //
63 
64 template <typename TSpace, typename TKSpace>
65 inline void DGtal::Viewer3D<TSpace, TKSpace>::rotateDomain(
66 Image2DDomainD3D & anDom, double angleRotation, ImageDirection rotationDir )
67 {
68  DGtal::PointVector<3, int> pt;
69  pt[0] = (int) (anDom.point1[0]+anDom.point2[0]+anDom.point3[0]+anDom.point4[0])/4.0;
70  pt[1] = (int) (anDom.point1[1]+anDom.point2[1]+anDom.point3[1]+anDom.point4[1])/4.0;
71  pt[2] = (int) (anDom.point1[2]+anDom.point2[2]+anDom.point3[2]+anDom.point4[2])/4.0;
72  rotateImageVertex(anDom, angleRotation, rotationDir);
73 
74  std::vector<typename DGtal::Display3D<TSpace, TKSpace>::LineD3D> & aVectLine =
75  Viewer3D<TSpace, TKSpace>::myLineSetList.at( anDom.myLineSetIndex );
76  for(unsigned int i = 0; i< aVectLine.size();i++){
77  typename DGtal::Display3D<TSpace, TKSpace>::LineD3D & aLine =
78  aVectLine.at( i );
79  rotateLineD3D(aLine, pt, angleRotation, rotationDir );
80  }
81 
82 }
83 
84 template <typename TSpace, typename TKSpace>
85 template <typename TValues>
86 inline void DGtal::Viewer3D<TSpace, TKSpace>::rotatePoint(
87 TValues & x, TValues & y, TValues & z, double cx, double cy, double cz,
88 double angleRotation, ImageDirection rotationDir )
89 {
90  double dx = x-cx; double dy = y-cy; double dz = z-cz;
91  if(rotationDir == zDirection){
92  x = cx+dx*cos(angleRotation)-dy*sin(angleRotation);
93  y = cy+dx*sin(angleRotation)+dy*cos(angleRotation);
94  }
95  if(rotationDir == yDirection){
96  x = cx+dx*cos(angleRotation)-dz*sin(angleRotation);
97  z = cz+dx*sin(angleRotation)+dz*cos(angleRotation);
98  }
99  if(rotationDir == xDirection){
100  y = cy+dy*cos(angleRotation)-dz*sin(angleRotation);
101  z = cz+dy*sin(angleRotation)+dz*cos(angleRotation);
102  }
103 }
104 
105 template <typename TSpace, typename TKSpace>
106 template <typename TContainer>
107 inline void DGtal::Viewer3D<TSpace, TKSpace>::rotateLineD3D(
108 typename DGtal::Display3D<TSpace, TKSpace>::LineD3D & aLine,
109 DGtal::PointVector<3, int, TContainer> pt, double angleRotation,
110 ImageDirection dirRotation )
111 {
112  double dx1 = aLine.point1[0] - pt[0]; double dy1 = aLine.point1[1] - pt[1]; double dz1 = aLine.point1[2] - pt[2];
113  double dx2 = aLine.point2[0] - pt[0]; double dy2 = aLine.point2[1] - pt[1]; double dz2 = aLine.point2[2] - pt[2];
114  if(dirRotation==zDirection){
115  aLine.point1[0]=pt[0]+dx1*std::cos(angleRotation)-dy1*std::sin(angleRotation);
116  aLine.point1[1]=pt[1]+dx1*std::sin(angleRotation)+dy1*std::cos(angleRotation);
117 
118  aLine.point2[0]=pt[0]+dx2*std::cos(angleRotation)-dy2*std::sin(angleRotation);
119  aLine.point2[1]=pt[1]+dx2*std::sin(angleRotation)+dy2*std::cos(angleRotation);
120 
121  }else if(dirRotation==xDirection){
122  aLine.point1[1]=pt[1]+dy1*std::cos(angleRotation)-dz1*std::sin(angleRotation);
123  aLine.point1[2]=pt[2]+dy1*std::sin(angleRotation)+dz1*std::cos(angleRotation);
124 
125  aLine.point2[1]=pt[1]+dy2*std::cos(angleRotation)-dz2*std::sin(angleRotation);
126  aLine.point2[2]=pt[2]+dy2*std::sin(angleRotation)+dz2*std::cos(angleRotation);
127  }else if(dirRotation==yDirection){
128  aLine.point1[0]=pt[0]+dx1*std::cos(angleRotation)-dz1*std::sin(angleRotation);
129  aLine.point1[2]=pt[2]+dx1*std::sin(angleRotation)+dz1*std::cos(angleRotation);
130 
131  aLine.point2[0]=pt[0]+dx2*std::cos(angleRotation)-dz2*std::sin(angleRotation);
132  aLine.point2[2]=pt[2]+dx2*std::sin(angleRotation)+dz2*std::cos(angleRotation);
133  }else{
134  trace.error() << "No direction!!"<< std::endl;
135  }
136 
137 }
138 
139 template <typename TSpace, typename TKSpace>
140 inline unsigned int DGtal::Viewer3D<TSpace, TKSpace>::getCurrentDomainNumber()
141 {
142  return static_cast<unsigned int>(myImageDomainList.size());
143 }
144 
145 template <typename TSpace, typename TKSpace>
146 inline unsigned int DGtal::Viewer3D<TSpace, TKSpace>::getCurrentGLImageNumber()
147 {
148  return static_cast<unsigned int>(myGSImageList.size());
149 }
150 
151 template <typename TSpace, typename TKSpace>
152 inline void DGtal::Viewer3D<TSpace, TKSpace>::addTextureImage(
153 const typename Viewer3D<TSpace, TKSpace>::TextureImage & image )
154 {
155  myGSImageList.push_back(image);
156  Display3D<TSpace, TKSpace>::updateBoundingBox( image.point1 );
157  Display3D<TSpace, TKSpace>::updateBoundingBox( image.point2 );
158  Display3D<TSpace, TKSpace>::updateBoundingBox( image.point3 );
159  Display3D<TSpace, TKSpace>::updateBoundingBox( image.point4 );
160 }
161 
162 template <typename TSpace, typename TKSpace>
163 template <typename TImageType, typename TFunctor>
164 inline void DGtal::Viewer3D<TSpace, TKSpace>::updateTextureImage(
165 unsigned int imageIndex, const TImageType & image, const TFunctor & aFunctor,
166 double xTranslation, double yTranslation, double zTranslation,
167 double rotationAngle, ImageDirection rotationDir )
168 {
169  BOOST_CONCEPT_ASSERT(( concepts::CConstImage < TImageType > ));
170  assert ( imageIndex< myGSImageList.size());
171  typename Viewer3D<TSpace, TKSpace>::TextureImage & anImage =
172  myGSImageList.at( imageIndex );
173  Display::updateBoundingBox(RealPoint(anImage.point1[0]+xTranslation,
174  anImage.point1[1]+yTranslation,
175  anImage.point1[2]+zTranslation));
176  Display::updateBoundingBox(RealPoint(anImage.point2[0]+xTranslation,
177  anImage.point2[1]+yTranslation,
178  anImage.point2[2]+zTranslation));
179  Display::updateBoundingBox(RealPoint(anImage.point3[0]+xTranslation,
180  anImage.point3[1]+yTranslation,
181  anImage.point3[2]+zTranslation));
182  Display::updateBoundingBox(RealPoint(anImage.point4[0]+xTranslation,
183  anImage.point4[1]+yTranslation,
184  anImage.point4[2]+zTranslation));
185  anImage.updateImageDataAndParam(image, aFunctor, xTranslation, yTranslation, zTranslation);
186  if(anImage.myDrawDomain)
187  {
188  *this << DGtal::Translate2DDomain(anImage.myIndexDomain, xTranslation, yTranslation, zTranslation);
189  }
190 
191  if(rotationAngle!=0.0){
192  rotateDomain(myImageDomainList.at(anImage.myIndexDomain), rotationAngle, rotationDir);
193  rotateImageVertex(anImage, rotationAngle, rotationDir);
194  }
195 }
196 
197 template <typename TSpace, typename TKSpace>
198 inline void DGtal::Viewer3D<TSpace, TKSpace>::updateOrientationTextureImage(
199 unsigned int imageIndex, double xPosition, double yPosition, double zPosition,
200 ImageDirection newDirection )
201 {
202  assert ( imageIndex< myGSImageList.size());
203  typename Viewer3D<TSpace, TKSpace>::TextureImage & anImage =
204  myGSImageList.at( imageIndex );
205  Display3D<TSpace, TKSpace>::updateBoundingBox( anImage.point1 );
206  Display3D<TSpace, TKSpace>::updateBoundingBox( anImage.point2 );
207  Display3D<TSpace, TKSpace>::updateBoundingBox( anImage.point3 );
208  Display3D<TSpace, TKSpace>::updateBoundingBox( anImage.point4 );
209  anImage.updateImageOrientation(newDirection, xPosition, yPosition, zPosition);
210  if(anImage.myDrawDomain)
211  {
212  *this << DGtal::Update2DDomainPosition<Space ,KSpace >(anImage.myIndexDomain,
213  newDirection,
214  xPosition,
215  yPosition, zPosition);
216  }
217 }
218 
219 template <typename TSpace, typename TKSpace>
220 inline void DGtal::Viewer3D<TSpace, TKSpace>::updateEmbeddingTextureImage(
221 unsigned int anImageIndex, typename Space::Point aPoint1,
222 typename Space::Point aPoint2, typename Space::Point aPoint3,
223 typename Space::Point aPoint4 )
224 {
225  assert ( anImageIndex< myGSImageList.size());
226  typename Viewer3D<TSpace, TKSpace>::TextureImage & anImage =
227  myGSImageList.at( anImageIndex );
228  Display3D<TSpace, TKSpace>::updateBoundingBox( aPoint1 );
229  Display3D<TSpace, TKSpace>::updateBoundingBox( aPoint2 );
230  Display3D<TSpace, TKSpace>::updateBoundingBox( aPoint3 );
231  Display3D<TSpace, TKSpace>::updateBoundingBox( aPoint4 );
232  anImage.updateImage3DEmbedding(aPoint1, aPoint2, aPoint3, aPoint4);
233 }
234 
235 template <typename TSpace, typename TKSpace>
236 inline void
237 DGtal::Viewer3D<TSpace, TKSpace>::TextureImage::updateImageOrientation(
238 ImageDirection normalDir, double xBottomLeft, double yBottomLeft,
239 double zBottomLeft )
240 {
241  if(normalDir==zDirection)
242  {
243  point1[0] = xBottomLeft-0.5; point1[1] = yBottomLeft-0.5; point1[2] = zBottomLeft;
244  point2[0] = xBottomLeft+myImageWidth-0.5; point2[1] = yBottomLeft-0.5; point2[2] = zBottomLeft;
245  point3[0] = xBottomLeft+myImageWidth-0.5; point3[1] = yBottomLeft+myImageHeight-0.5; point3[2] = zBottomLeft;
246  point4[0] = xBottomLeft-0.5; point4[1] = yBottomLeft+myImageHeight-0.5; point4[2] = zBottomLeft;
247  }else if(normalDir==yDirection)
248  {
249  point1[0] = xBottomLeft-0.5; point1[1] = yBottomLeft; point1[2] = zBottomLeft-0.5;
250  point2[0] = xBottomLeft+myImageWidth-0.5; point2[1] = yBottomLeft; point2[2] = zBottomLeft-0.5;
251  point3[0] = xBottomLeft+myImageWidth-0.5; point3[1] = yBottomLeft; point3[2] = zBottomLeft+myImageHeight-0.5;
252  point4[0] = xBottomLeft-0.5; point4[1] = yBottomLeft; point4[2] = zBottomLeft+myImageHeight-0.5;
253  }else if(normalDir==xDirection)
254  {
255  point1[0] = xBottomLeft; point1[1] = yBottomLeft-0.5; point1[2]= zBottomLeft-0.5;
256  point2[0] = xBottomLeft; point2[1] = yBottomLeft+myImageWidth-0.5; point2[2] = zBottomLeft-0.5;
257  point3[0] = xBottomLeft; point3[1] = yBottomLeft+myImageWidth-0.5; point3[2] = zBottomLeft+myImageHeight-0.5;
258  point4[0] = xBottomLeft; point4[1] = yBottomLeft-0.5; point4[2] = zBottomLeft+myImageHeight-0.5;
259  }
260  myDirection=normalDir;
261 }
262 
263 template <typename TSpace, typename TKSpace>
264 inline void
265 DGtal::Viewer3D<TSpace, TKSpace>::Image2DDomainD3D::updateDomainOrientation(
266 ImageDirection normalDir, double xBottomLeft, double yBottomLeft,
267 double zBottomLeft )
268 {
269  if(normalDir==zDirection)
270  {
271  point1[0] = xBottomLeft-0.5; point1[1] = yBottomLeft-0.5; point1[2] = zBottomLeft;
272  point2[0] = xBottomLeft+myDomainWidth-0.5; point2[1] = yBottomLeft-0.5; point2[2] = zBottomLeft;
273  point3[0] = xBottomLeft+myDomainWidth-0.5; point3[1] = yBottomLeft+myDomainHeight-0.5; point3[2] = zBottomLeft;
274  point4[0] = xBottomLeft-0.5; point4[1] = yBottomLeft+myDomainHeight-0.5; point4[2] = zBottomLeft;
275  }else if(normalDir==yDirection)
276  {
277  point1[0] = xBottomLeft-0.5; point1[1] = yBottomLeft; point1[2] = zBottomLeft-0.5;
278  point2[0] = xBottomLeft+myDomainWidth-0.5; point2[1] = yBottomLeft; point2[2] = zBottomLeft-0.5;
279  point3[0] = xBottomLeft+myDomainWidth-0.5; point3[1] = yBottomLeft; point3[2] = zBottomLeft+myDomainHeight-0.5;
280  point4[0] = xBottomLeft-0.5; point4[1] = yBottomLeft; point4[2] = zBottomLeft+myDomainHeight-0.5;
281  }else if(normalDir==xDirection)
282  {
283  point1[0] = xBottomLeft; point1[1] = yBottomLeft-0.5; point1[2]= zBottomLeft-0.5;
284  point2[0] = xBottomLeft; point2[1] = yBottomLeft+myDomainWidth-0.5; point2[2] = zBottomLeft-0.5;
285  point3[0] = xBottomLeft; point3[1] = yBottomLeft+myDomainWidth-0.5; point3[2] = zBottomLeft+myDomainHeight-0.5;
286  point4[0] = xBottomLeft; point4[1] = yBottomLeft-0.5; point4[2] = zBottomLeft+myDomainHeight-0.5;
287  }
288  myDirection=normalDir;
289 }
290 
291 template <typename TSpace, typename TKSpace>
292 inline void DGtal::Viewer3D<TSpace, TKSpace>::Image2DDomainD3D::translateDomain(
293 double xTranslation, double yTranslation, double zTranslation )
294 {
295  point1[0] += xTranslation; point1[1] += yTranslation; point1[2] += zTranslation;
296  point2[0] += xTranslation; point2[1] += yTranslation; point2[2] += zTranslation;
297  point3[0] += xTranslation; point3[1] += yTranslation; point3[2] += zTranslation;
298  point4[0] += xTranslation; point4[1] += yTranslation; point4[2] += zTranslation;
299 }
300 
301 template <typename TSpace, typename TKSpace>
302 template <typename TDomain>
303 void DGtal::Viewer3D<TSpace, TKSpace>::addImage2DDomainD3D(
304 const TDomain & aDomain, std::string mode, const DGtal::Color & aColor )
305 {
306  typename DGtal::Viewer3D<TSpace, TKSpace>::Image2DDomainD3D anImageDomain(
307  aDomain );
308  anImageDomain.color = aColor;
309  anImageDomain.myMode = mode;
310  anImageDomain.myLineSetIndex =
311  Viewer3D<TSpace, TKSpace>::myLineSetList.size();
312 
313  myImageDomainList.push_back(anImageDomain);
314  Display3D<TSpace, TKSpace>::updateBoundingBox( anImageDomain.point1 );
315  Display3D<TSpace, TKSpace>::updateBoundingBox( anImageDomain.point2 );
316  Display3D<TSpace, TKSpace>::updateBoundingBox( anImageDomain.point3 );
317  Display3D<TSpace, TKSpace>::updateBoundingBox( anImageDomain.point4 );
318 
319  std::vector<typename DGtal::Viewer3D<TSpace, TKSpace>::LineD3D> vectLines =
320  compute2DDomainLineRepresentation( anImageDomain );
321  Viewer3D<TSpace, TKSpace>::myLineSetList.push_back( vectLines );
322 }
323 
324 template <typename TSpace, typename TKSpace>
325 inline std::vector<typename DGtal::Viewer3D<TSpace, TKSpace>::LineD3D>
326 DGtal::Viewer3D<TSpace, TKSpace>::compute2DDomainLineRepresentation(
327 typename DGtal::Viewer3D<TSpace, TKSpace>::Image2DDomainD3D & anImageDomain )
328 {
329  std::vector<typename DGtal::Viewer3D<TSpace, TKSpace>::LineD3D>
330  vectLinesResu = compute2DDomainLineRepresentation( anImageDomain, 0.05 );
331  std::vector<typename DGtal::Viewer3D<TSpace, TKSpace>::LineD3D>
332  vectLinesVerso = compute2DDomainLineRepresentation( anImageDomain, -0.05 );
333  for(unsigned int i=0; i<vectLinesVerso.size(); i++)
334  {
335  vectLinesResu.push_back(vectLinesVerso.at(i));
336  }
337  return vectLinesResu;
338 }
339 
340 template <typename TSpace, typename TKSpace>
341 inline std::vector<typename DGtal::Viewer3D<TSpace, TKSpace>::LineD3D>
342 DGtal::Viewer3D<TSpace, TKSpace>::compute2DDomainLineRepresentation(
343 typename DGtal::Viewer3D<TSpace, TKSpace>::Image2DDomainD3D & anImageDomain,
344 double delta )
345 {
346  std::vector<typename DGtal::Viewer3D<TSpace, TKSpace>::LineD3D> aLineSet;
347  typename Viewer3D<TSpace, TKSpace>::LineD3D aLine;
348  aLine.color = anImageDomain.color;
349  aLine.width=0.1;
350 
351  if( anImageDomain.myMode=="BoundingBox")
352  {
353  aLine.point1[0]=anImageDomain.point1[0]; aLine.point1[1]=anImageDomain.point1[1]; aLine.point1[2]=anImageDomain.point1[2];
354  aLine.point2[0]=anImageDomain.point2[0]; aLine.point2[1]=anImageDomain.point2[1]; aLine.point2[2]=anImageDomain.point2[2];
355  aLineSet.push_back(aLine);
356  aLine.point1[0]=anImageDomain.point2[0]; aLine.point1[1]=anImageDomain.point2[1]; aLine.point1[2]=anImageDomain.point2[2];
357  aLine.point2[0]=anImageDomain.point3[0]; aLine.point2[1]=anImageDomain.point3[1]; aLine.point2[2]=anImageDomain.point3[2];
358  aLineSet.push_back(aLine);
359  aLine.point1[0]=anImageDomain.point3[0]; aLine.point1[1]=anImageDomain.point3[1]; aLine.point1[2]=anImageDomain.point3[2];
360  aLine.point2[0]=anImageDomain.point4[0]; aLine.point2[1]=anImageDomain.point4[1]; aLine.point2[2]=anImageDomain.point4[2];
361  aLineSet.push_back(aLine);
362  aLine.point1[0]=anImageDomain.point4[0]; aLine.point1[1]=anImageDomain.point4[1]; aLine.point1[2]=anImageDomain.point4[2];
363  aLine.point2[0]=anImageDomain.point1[0]; aLine.point2[1]=anImageDomain.point1[1]; aLine.point2[2]=anImageDomain.point1[2];
364  aLineSet.push_back(aLine);
365  }else if(anImageDomain.myMode=="InterGrid")
366  {
367  if(anImageDomain.myDirection==zDirection)
368  {
369  //lines align to the x direction
370  for(unsigned int i=0; i <= anImageDomain.myDomainHeight; i++)
371  {
372  aLine.point1[0] = anImageDomain.point1[0]; aLine.point1[1] = anImageDomain.point1[1]+i; aLine.point1[2] = anImageDomain.point1[2]+delta;
373  aLine.point2[0] = anImageDomain.point2[0]; aLine.point2[1] = anImageDomain.point2[1]+i; aLine.point2[2] = anImageDomain.point2[2]+delta;
374  aLineSet.push_back(aLine);
375  }
376  //lines align to the y direction
377  for(unsigned int i=0; i <= anImageDomain.myDomainWidth; i++)
378  {
379  aLine.point1[0] = anImageDomain.point1[0]+i; aLine.point1[1] = anImageDomain.point1[1]; aLine.point1[2] = anImageDomain.point1[2]+delta;
380  aLine.point2[0] = anImageDomain.point4[0]+i; aLine.point2[1] = anImageDomain.point4[1]; aLine.point2[2] = anImageDomain.point4[2]+delta;
381  aLineSet.push_back(aLine);
382  }
383  }else if(anImageDomain.myDirection==xDirection)
384  {
385  //lines align to the y direction
386  for(unsigned int i=0; i <= anImageDomain.myDomainHeight; i++)
387  {
388  aLine.point1[0] = anImageDomain.point1[0]+delta; aLine.point1[1] = anImageDomain.point1[1]; aLine.point1[2] = anImageDomain.point1[2]+i;
389  aLine.point2[0] = anImageDomain.point1[0]+delta; aLine.point2[1] = anImageDomain.point2[1]; aLine.point2[2] = anImageDomain.point1[2]+i;
390  aLineSet.push_back(aLine);
391  }
392 
393  //lines align to the z direction
394  for(unsigned int i=0; i <= anImageDomain.myDomainWidth; i++)
395  {
396  aLine.point1[0] = anImageDomain.point1[0]+delta; aLine.point1[1] = anImageDomain.point1[1]+i; aLine.point1[2] = anImageDomain.point1[2];
397  aLine.point2[0] = anImageDomain.point1[0]+delta; aLine.point2[1] = anImageDomain.point1[1]+i; aLine.point2[2] = anImageDomain.point4[2];
398  aLineSet.push_back(aLine);
399  }
400  }else if(anImageDomain.myDirection==yDirection)
401  {
402  //lines align to the x direction
403  for(unsigned int i=0; i <= anImageDomain.myDomainHeight; i++)
404  {
405  aLine.point1[0] = anImageDomain.point1[0]; aLine.point1[1] = anImageDomain.point1[1]+delta; aLine.point1[2] = anImageDomain.point1[2]+i;
406  aLine.point2[0] = anImageDomain.point2[0]; aLine.point2[1] = anImageDomain.point1[1]+delta; aLine.point2[2] = anImageDomain.point1[2]+i;
407  aLineSet.push_back(aLine);
408  }
409 
410  //lines align to the z direction
411  for(unsigned int i=0; i <= anImageDomain.myDomainWidth; i++)
412  {
413  aLine.point1[0] = anImageDomain.point1[0]+i; aLine.point1[1] = anImageDomain.point1[1]+delta; aLine.point1[2] = anImageDomain.point1[2];
414  aLine.point2[0] = anImageDomain.point1[0]+i; aLine.point2[1] = anImageDomain.point1[1]+delta; aLine.point2[2] = anImageDomain.point4[2];
415  aLineSet.push_back(aLine);
416  }
417  }
418  }else if(anImageDomain.myMode=="Grid")
419  {
420  if(anImageDomain.myDirection==zDirection)
421  {
422  //lines align to the x direction
423  for(unsigned int i=0; i < anImageDomain.myDomainHeight; i++)
424  {
425  aLine.point1[0] = anImageDomain.point1[0]; aLine.point1[1] = anImageDomain.point1[1]+i+0.5; aLine.point1[2] = anImageDomain.point1[2]+delta;
426  aLine.point2[0] = anImageDomain.point2[0]; aLine.point2[1] = anImageDomain.point2[1]+i+0.5; aLine.point2[2] = anImageDomain.point2[2]+delta;
427  aLineSet.push_back(aLine);
428  }
429  //lines align to the y direction
430  for(unsigned int i=0; i < anImageDomain.myDomainWidth; i++)
431  {
432  aLine.point1[0] = anImageDomain.point1[0]+i+0.5; aLine.point1[1] = anImageDomain.point1[1]; aLine.point1[2] = anImageDomain.point1[2]+delta;
433  aLine.point2[0] = anImageDomain.point4[0]+i+0.5; aLine.point2[1] = anImageDomain.point4[1]; aLine.point2[2] = anImageDomain.point4[2]+delta;
434  aLineSet.push_back(aLine);
435  }
436  }else if(anImageDomain.myDirection==xDirection)
437  {
438  //lines align to the y direction
439  for(unsigned int i=0; i < anImageDomain.myDomainHeight; i++)
440  {
441  aLine.point1[0] = anImageDomain.point1[0]+delta; aLine.point1[1] = anImageDomain.point1[1]; aLine.point1[2] = anImageDomain.point1[2]+i+0.5;
442  aLine.point2[0] = anImageDomain.point1[0]+delta; aLine.point2[1] = anImageDomain.point2[1]; aLine.point2[2] = anImageDomain.point1[2]+i+0.5;
443  aLineSet.push_back(aLine);
444  }
445 
446  //lines align to the z direction
447  for(unsigned int i=0; i < anImageDomain.myDomainWidth; i++)
448  {
449  aLine.point1[0] = anImageDomain.point1[0]+delta; aLine.point1[1] = anImageDomain.point1[1]+i+0.5; aLine.point1[2] = anImageDomain.point1[2];
450  aLine.point2[0] = anImageDomain.point1[0]+delta; aLine.point2[1] = anImageDomain.point1[1]+i+0.5; aLine.point2[2] = anImageDomain.point4[2];
451  aLineSet.push_back(aLine);
452  }
453  }else if(anImageDomain.myDirection==yDirection)
454  {
455  //lines align to the x direction
456  for(unsigned int i=0; i < anImageDomain.myDomainHeight; i++)
457  {
458  aLine.point1[0] = anImageDomain.point1[0]; aLine.point1[1] = anImageDomain.point1[1]+delta; aLine.point1[2] = anImageDomain.point1[2]+i+0.5;
459  aLine.point2[0] = anImageDomain.point2[0]; aLine.point2[1] = anImageDomain.point1[1]+delta; aLine.point2[2] = anImageDomain.point1[2]+i+0.5;
460  aLineSet.push_back(aLine);
461  }
462 
463  //lines align to the z direction
464  for(unsigned int i=0; i < anImageDomain.myDomainWidth; i++)
465  {
466  aLine.point1[0] = anImageDomain.point1[0]+i+0.5; aLine.point1[1] = anImageDomain.point1[1]+delta; aLine.point1[2] = anImageDomain.point1[2];
467  aLine.point2[0] = anImageDomain.point1[0]+i+0.5; aLine.point2[1] = anImageDomain.point1[1]+delta; aLine.point2[2] = anImageDomain.point4[2];
468  aLineSet.push_back(aLine);
469  }
470  }
471  }
472  return aLineSet;
473 }
474 
475 template <typename TSpace, typename TKSpace>
476 inline void DGtal::Viewer3D<TSpace, TKSpace>::updateAn2DDomainOrientation(
477 unsigned int domainIndex, double xPosition, double yPosition, double zPosition,
478 ImageDirection newDirection )
479 {
480  ASSERT( domainIndex < myImageDomainList.size());
481  typename Viewer3D<TSpace, TKSpace>::Image2DDomainD3D & aDomain =
482  myImageDomainList.at( domainIndex );
483 
484  Display3D<TSpace, TKSpace>::updateBoundingBox( aDomain.point1 );
485  Display3D<TSpace, TKSpace>::updateBoundingBox( aDomain.point2 );
486  Display3D<TSpace, TKSpace>::updateBoundingBox( aDomain.point3 );
487  Display3D<TSpace, TKSpace>::updateBoundingBox( aDomain.point4 );
488  aDomain.updateDomainOrientation(newDirection, xPosition, yPosition, zPosition);
489 
490  std::vector<typename DGtal::Viewer3D<TSpace, TKSpace>::LineD3D> vectNewLines =
491  compute2DDomainLineRepresentation( aDomain );
492  std::vector<typename DGtal::Viewer3D<TSpace, TKSpace>::LineD3D> & vectLines =
493  Viewer3D<TSpace, TKSpace>::myLineSetList.at( aDomain.myLineSetIndex );
494  vectLines.clear();
495  for(unsigned int i=0; i<vectNewLines.size(); i++)
496  {
497  vectLines.push_back(vectNewLines.at(i));
498  }
499 
500 }
501 
502 template <typename TSpace, typename TKSpace>
503 inline void DGtal::Viewer3D<TSpace, TKSpace>::translateAn2DDomain(
504 unsigned int domainIndex, double xTranslation, double yTranslation,
505 double zTranslation )
506 {
507  typename Viewer3D<TSpace, TKSpace>::Image2DDomainD3D & anDomain =
508  myImageDomainList.at( domainIndex );
509  anDomain.translateDomain(xTranslation, yTranslation, zTranslation);
510 
511  Display3D<TSpace, TKSpace>::updateBoundingBox( anDomain.point1 );
512  Display3D<TSpace, TKSpace>::updateBoundingBox( anDomain.point2 );
513  Display3D<TSpace, TKSpace>::updateBoundingBox( anDomain.point3 );
514  Display3D<TSpace, TKSpace>::updateBoundingBox( anDomain.point4 );
515 
516  std::vector<typename DGtal::Display3D<TSpace, TKSpace>::LineD3D> & vectLines =
517  Viewer3D<TSpace, TKSpace>::myLineSetList.at( anDomain.myLineSetIndex );
518  for(unsigned int i=0; i<vectLines.size(); i++){
519  typename DGtal::Display3D<TSpace, TKSpace>::LineD3D & aLine =
520  vectLines.at( i );
521  aLine.point1[0]=aLine.point1[0]+xTranslation; aLine.point1[1]=aLine.point1[1]+yTranslation; aLine.point1[2]=aLine.point1[2]+zTranslation;
522  aLine.point2[0]=aLine.point2[0]+xTranslation; aLine.point2[1]=aLine.point2[1]+yTranslation; aLine.point2[2]=aLine.point2[2]+zTranslation;
523  }
524 }
525 
526 template <typename TSpace, typename TKSpace>
527 inline std::string
528 DGtal::Viewer3D<TSpace, TKSpace>::TextureImage::className() const
529 {
530  return "TextureImage";
531 }
532 
533 template <typename TSpace, typename TKSpace>
534 inline DGtal::Viewer3D<TSpace, TKSpace> & DGtal::Viewer3D<TSpace, TKSpace>::
535 operator<<( const DGtal::Color & aColor )
536 {
537  myDefaultColor=aColor;
538  return *this;
539 }
540 
541 template <typename TSpace, typename TKSpace>
542 inline DGtal::Viewer3D<TSpace, TKSpace> & DGtal::Viewer3D<TSpace, TKSpace>::
543 operator<<( const typename Viewer3D<TSpace, TKSpace>::StreamKey & key )
544 {
545  switch (key)
546  {
547  case Viewer3D<TSpace, TKSpace>::updateDisplay:
548  Viewer3D<TSpace, TKSpace>::updateList();
549  break;
550 
551  case Viewer3D<TSpace, TKSpace>::addNewList:
552  Viewer3D<TSpace, TKSpace>::createNewCubeList();
553  break;
554 
555  case Viewer3D<TSpace, TKSpace>::shiftSurfelVisu:
556  Viewer3D<TSpace, TKSpace>::myCurrentfShiftVisuPrisms += 0.3;
557  break;
558  }
559  return *this;
560 }
561 
562 template <typename TSpace, typename TKSpace>
563 template <typename TDrawableWithViewer3D>
564 inline DGtal::Viewer3D<TSpace, TKSpace> & DGtal::Viewer3D<TSpace, TKSpace>::
565 operator<<( const TDrawableWithViewer3D & object )
566 {
567  BOOST_CONCEPT_ASSERT((concepts::CDrawableWithViewer3D< TDrawableWithViewer3D, Space, KSpace >));
568 
569  DGtal::Viewer3DFactory<TSpace, TKSpace>::draw( *this, object );
570  return *this;
571 }
572 
573 
574 ///////////////////////////////////////////////////////////////////////////////
575 // Implementation of inline functions and external operators //
576 
577 template <typename TSpace, typename TKSpace>
578 inline std::ostream & DGtal::
579 operator<<( std::ostream & out, const Viewer3D<TSpace, TKSpace> & object )
580 {
581  object.selfDisplay ( out );
582  return out;
583 }
584 
585 // //
586 ///////////////////////////////////////////////////////////////////////////////
587 
588 
589 ///////////////////////////////////////////////////////////////////////////////
590 
591 // heritage of parents templates methods //
592 
593 
594 // end of heritage //
595 ///////////////////////////////////////////////////////////////////////////////
596 
597 ///////////////////////////////////////////////////////////////////////////////
598 
599 // surcharge of parents methods //
600 
601 
602 // end of surcharge //
603 ///////////////////////////////////////////////////////////////////////////////
604 
605 template <typename TSpace, typename TKSpace>
606 inline void DGtal::Viewer3D<TSpace, TKSpace>::drawWithNames()
607 {
608  // JOL: 2014/10/15. This method is called only when the user tries
609  // to select some graphic object through QGLViewer. By default,
610  // selection is left clic + shift key.
611  // JOL: 2014/10/15. This is my addition for interacting with
612  // quads. Seems to work well.
613  if ( myExtension != 0 )
614  myExtension->drawWithNames( *this );
615 
616  glCallList ( myQuadsMapId );
617 
618  glCallList ( myCubesMapId );
619 
620  for ( unsigned int i = 0; i < Viewer3D<TSpace, TKSpace>::myLineSetList.size();
621  i++ )
622  {
623  glCallList( myLineSetListId + i );
624  }
625 
626  for ( unsigned int i = 0;
627  i < Viewer3D<TSpace, TKSpace>::myBallSetList.size(); i++ )
628  {
629  glCallList(myBallSetListId+i);
630  }
631 
632 }
633 
634 template <typename TSpace, typename TKSpace>
635 inline void DGtal::Viewer3D<TSpace, TKSpace>::draw()
636 {
637 
638  glUpdateBackground();
639 
640  if ( myExtension != 0 )
641  myExtension->draw( *this );
642 
643  glPushMatrix();
644  glMultMatrixd ( manipulatedFrame()->matrix() );
645  glPushMatrix();
646  glScalef(myGLScaleFactorX, myGLScaleFactorY, myGLScaleFactorZ);
647 
648  glLightfv(GL_LIGHT0, GL_SPECULAR, myLightSpecularCoeffs);
649  glLightfv(GL_LIGHT0, GL_DIFFUSE, myLightDiffuseCoeffs);
650  glLightfv(GL_LIGHT0, GL_AMBIENT, myLightAmbientCoeffs);
651  if( myLightPositionFixToCamera ){
652  updateLightCoordsFromCamera();
653  }else{
654  updateRelativeCameraFromLightPosition();
655  }
656  glLightfv(GL_LIGHT0, GL_POSITION, myLightPosition);
657 
658  unsigned int i = 0;
659  typename std::vector<
660  typename Viewer3D<TSpace, TKSpace>::ClippingPlaneD3D>::const_iterator it =
661  Viewer3D<TSpace, TKSpace>::myClippingPlaneList.begin();
662 
663  // OpenGL can't draw more than GL_MAX_CLIP_PLANES clipping plane
664  while ( i < GL_MAX_CLIP_PLANES &&
665  it != Viewer3D<TSpace, TKSpace>::myClippingPlaneList.end() )
666  {
667  double eq[ 4 ];
668  eq[ 0 ] = it->a;
669  eq[ 1 ] = it->b;
670  eq[ 2 ] = it->c;
671  eq[ 3 ] = it->d;
672  glEnable( GL_CLIP_PLANE0 + i );
673  glClipPlane( GL_CLIP_PLANE0 + i, eq );
674  i++;
675  it++;
676  }
677  if (i == GL_MAX_CLIP_PLANES)
678  {
679  std::cerr <<"Warning maximal clipping plane added" << std::endl;
680  }
681 
682  Vec centerS = sceneCenter();
683  Vec posCam = camera()->position();
684  double distCam =sqrt ( ( posCam.x-centerS.x ) * ( posCam.x-centerS.x ) +
685  ( posCam.y-centerS.y ) * ( posCam.y-centerS.y ) +
686  ( posCam.z-centerS.z ) * ( posCam.z-centerS.z ) );
687  for(unsigned int j=0; j< myVectTextureImage.size(); j++)
688  {
689  GLTextureImage &textureImg = myVectTextureImage.at(j);
690  glPushName ( textureImg.myTextureName );
691  glEnable(GL_TEXTURE_2D);
692  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
693  glBindTexture(GL_TEXTURE_2D, textureImg.myTextureName);
694  glBegin(GL_QUADS);
695  glColor4ub ( 255.0, 255.0, 255.0, 255.0 );
696  glNormal3d(textureImg.vectNormal[0], textureImg.vectNormal[1], textureImg.vectNormal[2]);
697 
698  glTexCoord2f(0, 0);
699  glVertex3f(textureImg.point1[0], textureImg.point1[1], textureImg.point1[2]);
700  glTexCoord2f(textureImg.myTextureFitX, 0.0);
701  glVertex3f(textureImg.point2[0], textureImg.point2[1], textureImg.point2[2]);
702  glTexCoord2f(textureImg.myTextureFitX, textureImg.myTextureFitY);
703  glVertex3f(textureImg.point3[0], textureImg.point3[1], textureImg.point3[2]);
704  glTexCoord2f(0.0, textureImg.myTextureFitY);
705  glVertex3f(textureImg.point4[0], textureImg.point4[1], textureImg.point4[2]);
706  glEnd();
707  glDisable(GL_TEXTURE_2D);
708 
709  }
710 
711  for ( unsigned int j = 0;
712  j < Viewer3D<TSpace, TKSpace>::myLineSetList.size(); j++ )
713  {
714  if ( Viewer3D<TSpace, TKSpace>::myLineSetList.at( j ).size() != 0 )
715  {
716  glLineWidth(
717  std::max( myGLLineMinWidth,
718  Viewer3D<TSpace, TKSpace>::myLineSetList.at( j ).at( 0 ).width ) );
719  }
720  glCallList(myLineSetListId+j);
721  }
722 
723  glCallList(myPrismListId);
724 
725  glCallList( myCubesMapId );
726 
727  for ( unsigned int j = 0; j < Viewer3D<TSpace, TKSpace>::myBallSetList.size();
728  j++ )
729  {
730  if ( myUseGLPointsForBalls )
731  {
732  if ( Viewer3D<TSpace, TKSpace>::myBallSetList.at( j ).size() != 0 )
733  {
734  glPointSize( std::max(
735  myGLPointMinWidth,
736  ( Viewer3D<TSpace, TKSpace>::myBallSetList.at( j ).at( 0 ).radius ) ) );
737  }
738  }
739  else
740  glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
741 
742  glCallList( myBallSetListId + j );
743  glUpdateLightRenderingMode();
744  }
745 
746  glDisable(GL_CULL_FACE);
747  glCallList(myQuadsMapId);
748  if(myViewWire)
749  {
750  glLineWidth(
751  std::max( myGLLineMinWidth,
752  Viewer3D<TSpace, TKSpace>::myMeshDefaultLineWidth / distCam ) );
753  glCallList(myQuadsMapWiredId);
754  }
755 
756  glDisable(GL_CULL_FACE);
757  glCallList(myTriangleSetListId);
758  if(myViewWire)
759  {
760  glLineWidth(
761  std::max( myGLLineMinWidth,
762  Viewer3D<TSpace, TKSpace>::myMeshDefaultLineWidth / distCam ) );
763  glCallList(myTriangleSetListWiredId);
764  }
765 
766  glDisable(GL_CULL_FACE);
767  glCallList(myPolygonSetListId);
768  if(myViewWire)
769  {
770  glLineWidth(
771  std::max( myGLLineMinWidth,
772  Viewer3D<TSpace, TKSpace>::myMeshDefaultLineWidth / distCam ) );
773  glCallList(myPolygonSetListWiredId);
774  }
775 
776  if(myIsMovingLight){
777  drawLight(GL_LIGHT0);
778  }
779 
780  glPopMatrix();
781  glPopMatrix();
782 }
783 
784 template <typename TSpace, typename TKSpace>
785 void DGtal::Viewer3D<TSpace, TKSpace>::selfDisplay( std::ostream & out ) const
786 {
787  out << "[Viewer3D]";
788 }
789 
790 template <typename TSpace, typename TKSpace>
791 bool DGtal::Viewer3D<TSpace, TKSpace>::isValid() const
792 {
793  return true;
794 }
795 
796 template <typename TSpace, typename TKSpace>
797 void DGtal::Viewer3D<TSpace, TKSpace>::init()
798 {
799  if ( myExtension != 0 )
800  myExtension->init( *this );
801  myAutoSaveState = false;
802  myIsMovingLight = false;
803  myLigthRotationStep = 0.01;
804  myRefMouseXPos = 0;
805  myRefMouseYPos = 0;
806 
807  myLightPositionRefCamera[0] = myLightPositionRefCameraDefault[0];
808  myLightPositionRefCamera[1] = myLightPositionRefCameraDefault[1];
809  myLightPositionRefCamera[2] = myLightPositionRefCameraDefault[2];
810  updateLightCoordsFromCamera();
811 
812  glClearColor (0.0, 0.0, 0.0, 0.0);
813  glShadeModel (GL_SMOOTH);
814 
815  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, myMaterialShininessCoeff);
816  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, myMaterialSpecularCoeffs);
817 
818 
819  glLightfv(GL_LIGHT0, GL_SPECULAR, myLightSpecularCoeffs);
820  glLightfv(GL_LIGHT0, GL_DIFFUSE, myLightDiffuseCoeffs);
821  glLightfv(GL_LIGHT0, GL_AMBIENT, myLightAmbientCoeffs);
822 
823  glLightfv(GL_LIGHT0, GL_POSITION, myLightPosition);
824 
825  glEnable(GL_LIGHTING);
826  glEnable(GL_LIGHT0);
827  glEnable(GL_DEPTH_TEST);
828 
829  Viewer3D<TSpace, TKSpace>::myMeshDefaultLineWidth = 10.0;
830  myNbListe=0;
831  myViewWire=false;
832  setBackgroundColor ( QColor ( 217, 228, 255, 255 ) );
833  setForegroundColor ( QColor ( 217, 22, 25, 255 ) );
834 
835  Viewer3D<TSpace, TKSpace>::createNewCubeList();
836  std::vector<typename Viewer3D<TSpace, TKSpace>::LineD3D> listeLine;
837  Viewer3D<TSpace, TKSpace>::myLineSetList.push_back( listeLine );
838  std::vector<typename Viewer3D<TSpace, TKSpace>::BallD3D> listeBall;
839  Viewer3D<TSpace, TKSpace>::myBallSetList.push_back( listeBall );
840  Viewer3D<TSpace, TKSpace>::myCurrentFillColor = Color( 220, 220, 220 );
841  Viewer3D<TSpace, TKSpace>::myCurrentLineColor = Color( 22, 22, 222, 50 );
842  myDefaultBackgroundColor = Color ( backgroundColor().red(), backgroundColor().green(),
843  backgroundColor().blue() );
844  myIsBackgroundDefault=true;
845  Viewer3D<TSpace, TKSpace>::myBoundingPtLow[ 0 ] =
846  -10.0; // numeric_limits<double>::max( );
847  Viewer3D<TSpace, TKSpace>::myBoundingPtLow[ 1 ] =
848  -10.0; // numeric_limits<double>::max( );
849  Viewer3D<TSpace, TKSpace>::myBoundingPtLow[ 2 ] =
850  -10.0; // numeric_limits<double>::max( );
851 
852  Viewer3D<TSpace, TKSpace>::myBoundingPtUp[ 0 ] =
853  -10.0; // numeric_limits<double>::min( );
854  Viewer3D<TSpace, TKSpace>::myBoundingPtUp[ 1 ] =
855  -10.0; // numeric_limits<double>::min( );
856  Viewer3D<TSpace, TKSpace>::myBoundingPtUp[ 2 ] =
857  -10.0; // numeric_limits<double>::min( );
858  Viewer3D<TSpace, TKSpace>::createNewCubeList();
859  typename std::vector<typename Viewer3D<TSpace, TKSpace>::CubeD3D> aKSCubeList;
860 
861  Viewer3D<TSpace, TKSpace>::myCurrentfShiftVisuPrisms = 0.0;
862  Viewer3D<TSpace, TKSpace>::myDefaultColor = Color( 255, 255, 255 );
863  camera()->showEntireScene();
864  setKeyDescription ( Qt::Key_E, "Export the current display into OFF file (just Cube, surfel and SurfelPrism for now)." );
865  setKeyDescription ( Qt::Key_W, "Switch display with and without wired view of triangle and quad faces." );
866  setKeyDescription ( Qt::Key_T, "Sort elements for display improvements." );
867  setKeyDescription ( Qt::Key_L, "Load last visualisation settings (from a .qglviewer.xml file generated by using SHIFT+L)");
868  setKeyDescription ( Qt::ShiftModifier+Qt::Key_L, "Save visualisation settings." );
869  setKeyDescription ( Qt::Key_B, "Switch background color with White/Black colors." );
870  setKeyDescription ( Qt::Key_C, "Show camera informations." );
871  setKeyDescription ( Qt::Key_R, "Reset default scale for 3 axes to 1.0f." );
872  setKeyDescription ( Qt::Key_D, "Enable/Disable the two side face rendering." );
873  setKeyDescription ( Qt::Key_O, "Switch the ball display mode (quad ball display (default) or OpenGL point)." );
874  setKeyDescription ( Qt::Key_M, "Switch the rendering mode bewteen Default, Metallic and Plastic mode." );
875  setKeyDescription ( Qt::Key_P, "Switch the light source position mode between the camera mode (default: the light source position is fixed according to the camera position) and scene mode (the light source position is fixed according the scene coordinate system)." );
876  setKeyDescription( Qt::Key_Z,
877  "Move away the light source according to the scence "
878  "center (move closer with SHIFT+Z)" );
879  setKeyDescription( Qt::ShiftModifier + Qt::Key_Z,
880  "Move closer the light source according to the scence "
881  "center (move awya with key Z)" );
882 
883 #if !defined (QGLVIEWER_VERSION) || QGLVIEWER_VERSION < 0x020500
884  setMouseBindingDescription((Qt::ControlModifier|Qt::ShiftModifier) + Qt::LeftButton, "move light source position defined in the main coordinate system (an x-axis (resp. y-axis) mouse move changes the azimuth (resp. inclination) angle of the light source). Note that light source is always looking at the center point (0,0,0).");
885 #else
886  setMouseBindingDescription(Qt::KeyboardModifiers( Qt::ControlModifier|Qt::ShiftModifier ), Qt::LeftButton, "move light source position defined in the main coordinate system (an x-axis (resp. y-axis) mouse move changes the azimuth (resp. inclination) angle of the light source). Note that light source is always looking at the center point (0,0,0).");
887 #endif
888 
889 
890  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
891 
892 #if !defined (QGLVIEWER_VERSION) || QGLVIEWER_VERSION < 0x020500
893  setMouseBindingDescription ( Qt::ShiftModifier+Qt::RightButton, "Delete the mouse selected list." );
894 #else
895  setMouseBindingDescription ( Qt::ShiftModifier, Qt::RightButton, "Delete the mouse selected list." );
896 #endif
897 
898  setManipulatedFrame ( new ManipulatedFrame() );
899 }
900 
901 template <typename TSpace, typename TKSpace>
902 void DGtal::Viewer3D<TSpace, TKSpace>::sortSurfelFromCamera()
903 {
904  CompFarthestVoxelFromCamera comp;
905  comp.posCam= camera()->position();
906 
907  for ( auto & mapElem : Viewer3D<TSpace, TKSpace>::myCubesMap )
908 
909  {
910  DGtal::trace.info() << "sort quad size" << mapElem.second.size()
911  << std::endl;
912  sort( mapElem.second.begin(), mapElem.second.end(), comp );
913  }
914  CompFarthestSurfelFromCamera compSurf;
915  DGtal::trace.info() << "sort surfel size"
916  << Viewer3D<TSpace, TKSpace>::myPrismList.size()
917  << std::endl;
918  sort( Viewer3D<TSpace, TKSpace>::myPrismList.begin(),
919  Viewer3D<TSpace, TKSpace>::myPrismList.end(), compSurf );
920 }
921 
922 template <typename TSpace, typename TKSpace>
923 void DGtal::Viewer3D<TSpace, TKSpace>::sortTriangleFromCamera()
924 {
925  CompFarthestTriangleFromCamera comp;
926  comp.posCam= camera()->position();
927  for ( auto & listElem : Viewer3D<TSpace, TKSpace>::myTriangleSetList )
928  {
929  sort( listElem.begin(), listElem.end(), comp );
930  }
931 }
932 
933 template <typename TSpace, typename TKSpace>
934 void DGtal::Viewer3D<TSpace, TKSpace>::sortQuadFromCamera()
935 {
936  CompFarthestSurfelFromCamera comp;
937  comp.posCam= camera()->position();
938 
939  for ( auto & listElem : Viewer3D<TSpace, TKSpace>::myQuadsMap )
940  {
941  DGtal::trace.info() << "sort quad size" << listElem.second.size()
942  << std::endl;
943  sort( listElem.second.begin(), listElem.second.end(), comp );
944  }
945 }
946 
947 template <typename TSpace, typename TKSpace>
948 void DGtal::Viewer3D<TSpace, TKSpace>::sortPolygonFromCamera()
949 {
950  CompFarthestPolygonFromCamera comp;
951  comp.posCam= camera()->position();
952 
953  for ( auto & listElem : Viewer3D<TSpace, TKSpace>::myPolygonSetList )
954  {
955  DGtal::trace.info() << "sort polygon size" << listElem.size() << std::endl;
956  sort( listElem.begin(), listElem.end(), comp );
957  }
958 
959 }
960 
961 template <typename TSpace, typename TKSpace>
962 void DGtal::Viewer3D<TSpace, TKSpace>::postSelection( const QPoint & point )
963 {
964  bool handled = false;
965  if ( myExtension != 0 )
966  handled = myExtension->postSelection( *this, point );
967  if ( handled )
968  return;
969 
970  camera()->convertClickToLine ( point, myOrig, myDir );
971  bool found;
972  this->myPosSelector= point;
973  mySelectedPoint = camera()->pointUnderPixel ( point, found );
974  if ( found )
975  {
976  DGtal::trace.info() << "Element of liste= " << selectedName() << "selected" << std::endl;
977  // JOL: 2014/10/15
978  mySelectedElementId = selectedName();
979  void* aData = 0;
980  SelectCallbackFct fct = getSelectCallback3D( selectedName(), aData );
981  if ( fct ) fct( this, selectedName(), aData );
982  // I leave the remaining code.
983  updateList(false);
984  }else if (mySelectedElementId != -1)
985  {
986  mySelectedElementId = -1;
987  updateList(false);
988  }
989 }
990 
991 template <typename TSpace, typename TKSpace>
992 void DGtal::Viewer3D<TSpace, TKSpace>::updateList(
993 bool needToUpdateBoundingBox )
994 {
995 
996  // glDeleteLists
997  glDeleteLists(myCubesMapId, 1);
998  glDeleteLists(myLineSetListId, myNbLineSetList);
999  glDeleteLists(myBallSetListId, myNbBallSetList);
1000  glDeleteLists(myTriangleSetListId, 1);
1001  glDeleteLists(myTriangleSetListWiredId, 1);
1002  glDeleteLists(myPrismListId, 1);
1003  glDeleteLists(myPolygonSetListId, 1);
1004  glDeleteLists(myPolygonSetListWiredId, 1);
1005  glDeleteLists(myQuadsMapId, 1);
1006  glDeleteLists(myQuadsMapWiredId, 1);
1007 
1008  // Storing ID for each list
1009  myCubesMapId = glGenLists(1);
1010  myLineSetListId =
1011  glGenLists( static_cast<GLsizei>(Viewer3D<TSpace, TKSpace>::myLineSetList.size() ));
1012  myNbLineSetList = static_cast<unsigned int>(Viewer3D<TSpace, TKSpace>::myLineSetList.size());
1013  myBallSetListId =
1014  glGenLists( static_cast<GLsizei>(Viewer3D<TSpace, TKSpace>::myBallSetList.size() ) );
1015  myNbBallSetList = static_cast<unsigned int>(Viewer3D<TSpace, TKSpace>::myBallSetList.size());
1016  myTriangleSetListId = glGenLists(1);
1017  myTriangleSetListWiredId = glGenLists(1);
1018  myCubeSetListWiredId = glGenLists(1);
1019  myPolygonSetListId = glGenLists(1);
1020  myPolygonSetListWiredId = glGenLists(1);
1021  myQuadsMapId = glGenLists(1);
1022  myQuadsMapWiredId = glGenLists(1);
1023  myPrismListId = glGenLists(1);
1024 
1025 
1026  myNbListe=0;
1027 
1028  glEnable ( GL_BLEND );
1029  glEnable ( GL_MULTISAMPLE_ARB );
1030  glEnable ( GL_SAMPLE_ALPHA_TO_COVERAGE_ARB );
1031  glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1032 
1033  glCreateListCubesMaps( Viewer3D<TSpace, TKSpace>::myCubesMap, myCubesMapId );
1034 
1035  glCreateListQuadD3D( Viewer3D<TSpace, TKSpace>::myPrismList, myPrismListId );
1036  myNbListe++;
1037 
1038  for ( unsigned int j = 0; j < Viewer3D<TSpace, TKSpace>::myLineSetList.size();
1039  j++ )
1040  {
1041  glCreateListLines( Viewer3D<TSpace, TKSpace>::myLineSetList.at( j ),
1042  myLineSetListId + j );
1043  myNbListe++;
1044  }
1045 
1046  for ( unsigned int j = 0;
1047  j < Viewer3D<TSpace, TKSpace>::myBallSetList.size(); j++ )
1048  {
1049  glCreateListBalls( Viewer3D<TSpace, TKSpace>::myBallSetList.at( j ),
1050  myBallSetListId + j );
1051  myNbListe++;
1052  }
1053 
1054 
1055  // First list: quad faces.
1056  glCreateListQuadMaps( Viewer3D<TSpace, TKSpace>::myQuadsMap, myQuadsMapId );
1057  myNbListe++;
1058 
1059  // Second list: Wired version of quad face.
1060  glCreateListQuadMapsWired( Viewer3D<TSpace, TKSpace>::myQuadsMap,
1061  myQuadsMapWiredId );
1062  myNbListe++;
1063 
1064  // Third list: Triangle faces.
1065  glCreateListTriangles( Viewer3D<TSpace, TKSpace>::myTriangleSetList,
1066  myTriangleSetListId );
1067  myNbListe++;
1068 
1069  // Fourth list: Wired version of triangle face.
1070  glCreateListTrianglesWired( Viewer3D<TSpace, TKSpace>::myTriangleSetList,
1071  myTriangleSetListWiredId );
1072  myNbListe++;
1073 
1074  // Fifth list: Polygonal faces.
1075  glCreateListPolygons( Viewer3D<TSpace, TKSpace>::myPolygonSetList,
1076  myPolygonSetListId );
1077  myNbListe++;
1078 
1079  // Sixth list: Wired version of polygonal face.
1080  glCreateListPolygonsWired( Viewer3D<TSpace, TKSpace>::myPolygonSetList,
1081  myPolygonSetListWiredId );
1082  myNbListe++;
1083 
1084  // Seventh list: Textured images.
1085  glUpdateTextureImages( myGSImageList );
1086 
1087  if ( needToUpdateBoundingBox &&
1088  Viewer3D<TSpace, TKSpace>::myBoundingPtChangedTag )
1089  {
1090  setSceneBoundingBox(
1091  qglviewer::Vec( Viewer3D<TSpace, TKSpace>::myBoundingPtLow[ 0 ],
1092  Viewer3D<TSpace, TKSpace>::myBoundingPtLow[ 1 ],
1093  Viewer3D<TSpace, TKSpace>::myBoundingPtLow[ 2 ] ),
1094  qglviewer::Vec( Viewer3D<TSpace, TKSpace>::myBoundingPtUp[ 0 ],
1095  Viewer3D<TSpace, TKSpace>::myBoundingPtUp[ 1 ],
1096  Viewer3D<TSpace, TKSpace>::myBoundingPtUp[ 2 ] ) );
1097  showEntireScene();
1098  Viewer3D<TSpace, TKSpace>::myBoundingPtChangedTag = false;
1099  }
1100  update();
1101  glPopMatrix();
1102 }
1103 
1104 template <typename TSpace, typename TKSpace>
1105 void DGtal::Viewer3D<TSpace, TKSpace>::glDrawGLBall(
1106 const typename Viewer3D<TSpace, TKSpace>::BallD3D & aBall )
1107 {
1108  double thetaResolution = aBall.resolution;
1109  double thetaStep= (2.0*M_PI)/thetaResolution;
1110  double phiResolution = aBall.resolution;
1111  double phiStep= M_PI/phiResolution;
1112 
1113  double radius = aBall.radius;
1114  double xCenter = aBall.center[0];
1115  double yCenter = aBall.center[1];
1116  double zCenter = aBall.center[2];
1117  glBegin(GL_QUAD_STRIP);
1118  for(unsigned int j =0; j < phiResolution; j++)
1119  {
1120  double phi0 = M_PI/2.0-j*phiStep;
1121  double phi1 = M_PI/2.0-(j+1)*phiStep;
1122  for(unsigned int i =0; i <= thetaResolution; i++)
1123  {
1124  double theta0 = i * thetaStep;
1125  glColor4ub ( aBall.color.red(), aBall.color.green(), aBall.color.blue(), aBall.color.alpha() );
1126  glNormal3f(cos(phi0)*cos(theta0), cos(phi0)*sin(theta0), sin(phi0));
1127  glVertex3f(xCenter+cos(phi0)*cos(theta0)*radius,yCenter+ cos(phi0)*sin(theta0)*radius, zCenter+ sin(phi0)*radius);
1128  glNormal3f(cos(phi1)*cos(theta0), cos(phi1)*sin(theta0), sin(phi1));
1129  glVertex3f(xCenter+cos(phi1)*cos(theta0)*radius,yCenter+ cos(phi1)*sin(theta0)*radius, zCenter+ sin(phi1)*radius);
1130  }
1131  }
1132  glEnd();
1133 
1134 }
1135 
1136 template <typename TSpace, typename TKSpace>
1137 void DGtal::Viewer3D<TSpace, TKSpace>::mousePressEvent( QMouseEvent * e )
1138 {
1139  bool handled = false;
1140  // Checks if an extension is present.
1141  if ( myExtension != 0 )
1142  {
1143  handled = myExtension->mousePressEvent( *this, e );
1144  if ( handled )
1145  return;
1146  }
1147 
1148  if(e->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier))
1149  {
1150  myIsMovingLight=true;
1151  myRefMouseXPos = e->x();
1152  myRefMouseYPos = e->y();
1153  if( myLightPositionFixToCamera )
1154  {
1155  updateLightCoordsFromCamera();
1156  }
1157  myLightR = sqrt( myLightPosition[0]* myLightPosition[0]+
1158  myLightPosition[1]* myLightPosition[1]+
1159  myLightPosition[2]* myLightPosition[2]);
1160  myLightTheta = asin( myLightPosition[2]/myLightR);
1161  myLightPhi = atan2( myLightPosition[1], myLightPosition[0]);
1162  }
1163  else
1164  {
1165  QGLViewer::mousePressEvent(e);
1166  }
1167 }
1168 
1169 template <typename TSpace, typename TKSpace>
1170 void DGtal::Viewer3D<TSpace, TKSpace>::mouseReleaseEvent( QMouseEvent * e )
1171 {
1172  bool handled = false;
1173  // Checks if an extension is present.
1174  if ( myExtension != 0 )
1175  {
1176  handled = myExtension->mouseReleaseEvent( *this, e );
1177  if ( handled )
1178  return;
1179  }
1180 
1181  if(e->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier) || myIsMovingLight){
1182  myIsMovingLight=false;
1183  update();
1184  }else{
1185  QGLViewer::mouseReleaseEvent(e);
1186  }
1187 }
1188 
1189 template <typename TSpace, typename TKSpace>
1190 void DGtal::Viewer3D<TSpace, TKSpace>::mouseMoveEvent( QMouseEvent * e )
1191 {
1192  bool handled = false;
1193  // Checks if an extension is present.
1194  if ( myExtension != 0 )
1195  {
1196  handled = myExtension->mouseMoveEvent( *this, e );
1197  if ( handled )
1198  return;
1199  }
1200 
1201  if(e->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier)){
1202  int varX = e->x() - myRefMouseXPos;
1203  int varY = e->y() - myRefMouseYPos;
1204  myLightPhi -= varY * myLigthRotationStep;
1205  myLightTheta -= varX * myLigthRotationStep / 2.0;
1206  myLightPosition[0] = myLightR*cos(myLightTheta)*cos(myLightPhi);
1207  myLightPosition[1] = myLightR*cos(myLightTheta)*sin(myLightPhi);
1208  myLightPosition[2] = myLightR*sin(myLightTheta);
1209  if(myLightPositionFixToCamera){
1210  updateRelativeCameraFromLightPosition();
1211  }
1212  glLightfv(GL_LIGHT0, GL_POSITION, myLightPosition);
1213  myRefMouseXPos = e->x();
1214  myRefMouseYPos = e->y();
1215  update();
1216  }else{
1217  QGLViewer::mouseMoveEvent(e);
1218  }
1219 }
1220 
1221 template <typename TSpace, typename TKSpace>
1222 void DGtal::Viewer3D<TSpace, TKSpace>::keyPressEvent( QKeyEvent * e )
1223 {
1224  bool handled = false;
1225  // Checks if an extension is present.
1226  if ( myExtension != 0 )
1227  {
1228  handled = myExtension->keyPressEvent( *this, e );
1229  if ( handled )
1230  return;
1231  }
1232 
1233  if( e->key() == Qt::Key_D)
1234  {
1235  myIsDoubleFaceRendering = !myIsDoubleFaceRendering;
1236  glUpdateLightRenderingMode();
1237  update();
1238 
1239  }
1240  if( e->key() == Qt::Key_E)
1241  {
1242  trace.info() << "Exporting mesh..." ;
1243  operator>><TSpace, TKSpace>( *this, "exportedMesh.off" );
1244  trace.info() << "[done]"<< std::endl ;
1245  }
1246  if( e->key() == Qt::Key_M)
1247  {
1248  switch (myRenderingMode)
1249  {
1250  case RenderingMode::RenderingDefault :
1251  myRenderingMode = RenderingMode::RenderingMetallic;
1252  break;
1253  case RenderingMode::RenderingMetallic :
1254  myRenderingMode = RenderingMode::RenderingPlastic;
1255  break;
1256  case RenderingMode::RenderingPlastic :
1257  myRenderingMode = RenderingMode::RenderingLambertian;
1258  break;
1259  case RenderingMode::RenderingLambertian :
1260  myRenderingMode = RenderingMode::RenderingDefault;
1261  break;
1262  }
1263  updateRenderingCoefficients(myRenderingMode);
1264  }
1265 
1266 
1267 
1268  if ( ( e->key() ==Qt::Key_W ) )
1269  {
1270  myViewWire=!myViewWire;
1271  updateList(false);
1272  update();
1273  }
1274  if ( e->key() == Qt::Key_Z )
1275  {
1276  if ( myLightPositionFixToCamera )
1277  {
1278  updateLightCoordsFromCamera();
1279  }
1280  myLightR = sqrt( myLightPosition[ 0 ] * myLightPosition[ 0 ] +
1281  myLightPosition[ 1 ] * myLightPosition[ 1 ] +
1282  myLightPosition[ 2 ] * myLightPosition[ 2 ] );
1283  myLightTheta = asin( myLightPosition[ 2 ] / myLightR );
1284  myLightPhi = atan2( myLightPosition[ 1 ], myLightPosition[ 0 ] );
1285 
1286  std::stringstream ss;
1287  if ( e->modifiers() == Qt::ShiftModifier )
1288  {
1289  myLightR += 5.0;
1290  ss << "Move away light source at distance: " << myLightR;
1291  }
1292  else
1293  {
1294  myLightR -= 5.0;
1295  ss << "Move closer light source at distance: " << myLightR;
1296  }
1297  displayMessage( QString( ss.str().c_str() ), 3000 );
1298  myLightPosition[ 0 ] = myLightR * cos( myLightTheta ) * cos( myLightPhi );
1299  myLightPosition[ 1 ] = myLightR * cos( myLightTheta ) * sin( myLightPhi );
1300  myLightPosition[ 2 ] = myLightR * sin( myLightTheta );
1301  if ( myLightPositionFixToCamera )
1302  {
1303  updateRelativeCameraFromLightPosition();
1304  }
1305  myIsMovingLight = true;
1306  glLightfv( GL_LIGHT0, GL_POSITION, myLightPosition );
1307  update();
1308  }
1309 
1310  if ( ( e->key() == Qt::Key_P ) )
1311  {
1312  myLightPositionFixToCamera = !myLightPositionFixToCamera;
1313  setLightModeFixToCamera(myLightPositionFixToCamera);
1314  }
1315 
1316  if ( ( e->key() ==Qt::Key_O ) )
1317  {
1318  myUseGLPointsForBalls = !myUseGLPointsForBalls;
1319  updateList(false);
1320  update();
1321  }
1322 
1323  if ( ( e->key() ==Qt::Key_R ) )
1324  {
1325  myGLScaleFactorX=1.0f;
1326  myGLScaleFactorY=1.0f;
1327  myGLScaleFactorZ=1.0f;
1328  update();
1329  }
1330 
1331  if ( ( e->key() ==Qt::Key_T ) )
1332  {
1333  handled=true;
1334  DGtal::trace.info() << "sorting surfel according camera position....";
1335  sortSurfelFromCamera();
1336  sortTriangleFromCamera();
1337  sortQuadFromCamera();
1338  sortPolygonFromCamera();
1339  DGtal::trace.info() << " [done]"<< std::endl;
1340  updateList(false);
1341  update();
1342  }
1343  if ( ( e->key() ==Qt::Key_B ) )
1344  {
1345  handled=true;
1346  myIsBackgroundDefault=!myIsBackgroundDefault;
1347  glUpdateBackground();
1348  update();
1349  }
1350 
1351  if ( ( e->key() ==Qt::Key_L ) )
1352  {
1353  if(e->modifiers()==Qt::ShiftModifier){
1354  saveStateToFile();
1355  }else{
1356  restoreStateFromFile();
1357  update();
1358  }
1359  }
1360  if ( ( e->key() ==Qt::Key_C ) )
1361  {
1362  handled=true;
1363  GLint Viewport[4];
1364  GLdouble Projection[16], Modelview[16];
1365 
1366  glGetIntegerv ( GL_VIEWPORT , Viewport );
1367  glGetDoublev ( GL_MODELVIEW_MATRIX , Modelview );
1368  glGetDoublev ( GL_PROJECTION_MATRIX, Projection );
1369 
1370  for ( unsigned short m=0; m<4; ++m )
1371  {
1372  for ( unsigned short l=0; l<4; ++l )
1373  {
1374  double sum = 0.0;
1375  for ( unsigned short k=0; k<4; ++k )
1376  sum += Projection[l+4*k]*Modelview[k+4*m];
1377  }
1378  }
1379 
1380  DGtal::trace.info() << "Viewport: ";
1381  for ( unsigned short l=0; l<4; ++l )
1382  DGtal::trace.info() << Viewport[l] << ", ";
1383  DGtal::trace.info() << std::endl;
1384 
1385  Vec cp = camera()->position();
1386  Vec cd = camera()->viewDirection();
1387  Vec cup = camera()->upVector();
1388 
1389  DGtal::trace.info() << "camera.position: " ;
1390  for ( unsigned short l=0; l<3; ++l )
1391  DGtal::trace.info() << cp[l] << ", ";
1392  DGtal::trace.info() << std::endl;
1393 
1394  DGtal::trace.info() << "camera.direction: ";
1395  for ( unsigned short l=0; l<3; ++l )
1396  DGtal::trace.info() << cd[l] << ", ";
1397  DGtal::trace.info() << std::endl;
1398 
1399  DGtal::trace.info() << "camera.upVector: ";
1400  for ( unsigned short l=0; l<3; ++l )
1401  DGtal::trace.info() << cup[l] << ", ";
1402  DGtal::trace.info() << std::endl;
1403 
1404  DGtal::trace.info() << "zNear: " << camera()->zNear() << " - zFar: " << camera()->zFar() << std::endl;
1405  }
1406 
1407  if ( !handled )
1408  QGLViewer::keyPressEvent ( e );
1409 }
1410 
1411 template <typename TSpace, typename TKSpace>
1412 QString DGtal::Viewer3D<TSpace, TKSpace>::helpString() const
1413 {
1414  QString text;
1415  if ( myExtension != 0 )
1416  text += myExtension->helpString( *this );
1417 
1418  text += "<h2> Viewer3D</h2>";
1419  text += "Use the mouse to move the camera around the object. ";
1420  text += "You can respectively revolve around, zoom and translate with the three mouse buttons. ";
1421  text += "Left and middle buttons pressed together rotate around the camera view direction axis<br><br>";
1422  text += "Pressing <b>Alt</b> and one of the function keys (<b>F1</b>..<b>F12</b>) defines a camera keyFrame. ";
1423  text += "Simply press the function key again to restore it-> Several keyFrames define a ";
1424  text += "camera path. Paths are saved when you quit the application and restored at next start.<br><br>";
1425  text += "Press <b>F</b> to display the frame rate, <b>A</b> for the world axis, ";
1426  text += "<b>Alt+Return</b> for full screen mode and <b>Control+S</b> to save a snapshot. ";
1427  text += "See the <b>Keyboard</b> tab in this window for a complete shortcut list.<br><br>";
1428  text += "Double clicks automates single click actions: A left button double click aligns the closer axis with the camera (if close enough). ";
1429  text += "A middle button double click fits the zoom of the camera and the right button re-centers the scene.<br><br>";
1430  text += "A left button double click while holding right button pressed defines the camera <i>Revolve Around Ball</i>. ";
1431  text += "See the <b>Mouse</b> tab and the documentation web pages for details.<br><br>";
1432  text += "Press <b>Escape</b> to exit the viewer.";
1433  return text;
1434 }
1435 
1436 template <typename TSpace, typename TKSpace>
1437 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListCubesMaps(
1438 const typename DGtal::Display3D<TSpace, TKSpace>::CubesMap & aCubeMap,
1439 unsigned int idList )
1440 {
1441  glNewList ( idList , GL_COMPILE );
1442 
1443  for (auto &mapElem: aCubeMap)
1444  {
1445  glPushName ( mapElem.first );
1446  glEnable ( GL_LIGHTING );
1447  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
1448  glBegin ( GL_QUADS );
1449  bool useColorSelection = false;
1450  if(mySelectedElementId == mapElem.first)
1451  useColorSelection = true;
1452 
1453  for (auto &cube: mapElem.second)
1454  {
1455  if(useColorSelection)
1456  {
1457  unsigned char m = (cube.color.red()+ cube.color.green()+ cube.color.blue())/3;
1458  if(m>128)
1459  {
1460  glColor4ub ( std::max((int)(cube.color.red())-mySelectionColorShift, 0),
1461  std::max((int)(cube.color.green())-mySelectionColorShift, 0),
1462  std::max((int)(cube.color.blue())-mySelectionColorShift, 0),
1463  cube.color.alpha());
1464  }
1465  else{
1466  glColor4ub ( std::min(cube.color.red()+mySelectionColorShift, 255),
1467  std::min(cube.color.green()+mySelectionColorShift, 255),
1468  std::min(cube.color.blue()+mySelectionColorShift, 255),
1469  cube.color.alpha());
1470  }
1471  }
1472  else
1473  {
1474  glColor4ub ( cube.color.red(), cube.color.green(), cube.color.blue(),cube.color.alpha());
1475  }
1476  double _width= cube.width;
1477  //z+
1478  glNormal3f ( 0.0, 0.0, 1.0 );
1479  glVertex3f ( cube.center[0]-_width, cube.center[1]+_width, cube.center[2]+_width );
1480  glVertex3f ( cube.center[0]-_width, cube.center[1]-_width, cube.center[2]+_width );
1481  glVertex3f ( cube.center[0]+_width, cube.center[1]-_width, cube.center[2]+_width );
1482  glVertex3f ( cube.center[0]+_width, cube.center[1]+_width, cube.center[2]+_width );
1483  //z-
1484  glNormal3f ( 0.0, 0.0, -1.0 );
1485  glVertex3f ( cube.center[0]-_width, cube.center[1]+_width, cube.center[2]-_width );
1486  glVertex3f ( cube.center[0]+_width, cube.center[1]+_width, cube.center[2]-_width );
1487  glVertex3f ( cube.center[0]+_width, cube.center[1]-_width, cube.center[2]-_width );
1488  glVertex3f ( cube.center[0]-_width, cube.center[1]-_width, cube.center[2]-_width );
1489  //x+
1490  glNormal3f ( 1.0, 0.0, 0.0 );
1491  glVertex3f ( cube.center[0]+_width, cube.center[1]-_width, cube.center[2]+_width );
1492  glVertex3f ( cube.center[0]+_width, cube.center[1]-_width, cube.center[2]-_width );
1493  glVertex3f ( cube.center[0]+_width, cube.center[1]+_width, cube.center[2]-_width );
1494  glVertex3f ( cube.center[0]+_width, cube.center[1]+_width, cube.center[2]+_width );
1495  //x-
1496  glNormal3f ( -1.0, 0.0, 0.0 );
1497  glVertex3f ( cube.center[0]-_width, cube.center[1]-_width, cube.center[2]+_width );
1498  glVertex3f ( cube.center[0]-_width, cube.center[1]+_width, cube.center[2]+_width );
1499  glVertex3f ( cube.center[0]-_width, cube.center[1]+_width, cube.center[2]-_width );
1500  glVertex3f ( cube.center[0]-_width, cube.center[1]-_width, cube.center[2]-_width );
1501  //y+
1502  glNormal3f ( 0.0, 1.0, 0.0 );
1503  glVertex3f ( cube.center[0]-_width, cube.center[1]+_width, cube.center[2]+_width );
1504  glVertex3f ( cube.center[0]+_width, cube.center[1]+_width, cube.center[2]+_width );
1505  glVertex3f ( cube.center[0]+_width, cube.center[1]+_width, cube.center[2]-_width );
1506  glVertex3f ( cube.center[0]-_width, cube.center[1]+_width, cube.center[2]-_width );
1507  //y-
1508  glNormal3f ( 0.0, -1.0, 0.0 );
1509  glVertex3f ( cube.center[0]-_width, cube.center[1]-_width, cube.center[2]+_width );
1510  glVertex3f ( cube.center[0]-_width, cube.center[1]-_width, cube.center[2]-_width );
1511  glVertex3f ( cube.center[0]+_width, cube.center[1]-_width, cube.center[2]-_width );
1512  glVertex3f ( cube.center[0]+_width, cube.center[1]-_width, cube.center[2]+_width );
1513  }
1514  glEnd();
1515  glPopName();
1516  }
1517  glEndList();
1518  glUpdateLightRenderingMode();
1519 
1520 }
1521 
1522 template <typename TSpace, typename TKSpace>
1523 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListQuadD3D(
1524 const VectorQuad & aVectQuad, unsigned int idList )
1525 {
1526  glNewList ( idList, GL_COMPILE );
1527  glPushName ( myNbListe );
1528  glEnable ( GL_DEPTH_TEST );
1529  glEnable ( GL_BLEND );
1530  glEnable ( GL_MULTISAMPLE_ARB );
1531  glEnable ( GL_SAMPLE_ALPHA_TO_COVERAGE_ARB );
1532  glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1533 
1534  glBegin ( GL_QUADS );
1535  for ( auto const &q: aVectQuad )
1536  {
1537  glColor4ub ( q.color.red(), q.color.green(),
1538  q.color.blue(), q.color.alpha() );
1539  glNormal3f ( q.nx, q.ny, q.nz );
1540  glVertex3f ( q.point1[0], q.point1[1] , q.point1[2] );
1541  glVertex3f ( q.point2[0], q.point2[1] , q.point2[2] );
1542  glVertex3f ( q.point3[0], q.point3[1] , q.point3[2] );
1543  glVertex3f ( q.point4[0], q.point4[1] , q.point4[2] );
1544  }
1545  glEnd();
1546  glEndList();
1547 }
1548 
1549 template <typename TSpace, typename TKSpace>
1550 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListLines(
1551 const VectorLine & aVectLine, unsigned int idList )
1552 {
1553  glNewList ( idList, GL_COMPILE );
1554  glDisable ( GL_LIGHTING );
1555  glPushName ( myNbListe );
1556  glBegin ( GL_LINES );
1557  for (auto const &l: aVectLine )
1558  {
1559  glColor4ub ( l.color.red(), l.color.green(),
1560  l.color.blue(), l.color.alpha() );
1561  glVertex3f ( l.point1[0], l.point1[1], l.point1[2] );
1562  glVertex3f ( l.point2[0], l.point2[1], l.point2[2] );
1563  }
1564  glEnd();
1565  glEnable ( GL_LIGHTING );
1566  glEndList();
1567 }
1568 
1569 template <typename TSpace, typename TKSpace>
1570 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListBalls(
1571 const VectorBall & aVectBall, unsigned int idList )
1572 {
1573  if(myUseGLPointsForBalls)
1574  {
1575  glNewList ( idList, GL_COMPILE );
1576  glDepthMask ( GL_TRUE );
1577  glDisable ( GL_TEXTURE_2D );
1578  glDisable ( GL_POINT_SMOOTH );
1579  glDisable ( GL_LIGHTING );
1580  glPushName ( myNbListe );
1581  glBegin ( GL_POINTS );
1582  for ( auto const &ball: aVectBall )
1583  {
1584  glColor4ub ( ball.color.red(), ball.color.green(),
1585  ball.color.blue(), ball.color.alpha() );
1586  glVertex3f ( ball.center[0], ball.center[1], ball.center[2] );
1587  }
1588  glEnd();
1589  glEnable ( GL_LIGHTING );
1590  glEndList();
1591  }
1592  else
1593  {
1594  glNewList ( idList, GL_COMPILE );
1595  glPushName ( myNbListe );
1596  glDepthMask ( GL_TRUE );
1597  glDisable ( GL_TEXTURE_2D );
1598  glDisable ( GL_POINT_SMOOTH );
1599  for ( auto const & b: aVectBall)
1600  {
1601  glDrawGLBall ( b );
1602  }
1603  glEnd();
1604  glEndList();
1605  }
1606 }
1607 
1608 template <typename TSpace, typename TKSpace>
1609 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListQuadMaps(
1610 const typename DGtal::Display3D<TSpace, TKSpace>::QuadsMap & aQuadMap,
1611 unsigned int idList )
1612 {
1613  glNewList ( idList, GL_COMPILE );
1614  for (auto & mapElem: aQuadMap)
1615  {
1616  glPushName ( mapElem.first );
1617  glEnable ( GL_LIGHTING );
1618  glBegin ( GL_QUADS );
1619  bool useColorSelection = false;
1620  if(mySelectedElementId == mapElem.first)
1621  useColorSelection = true;
1622 
1623  for (auto &q: mapElem.second)
1624  {
1625  if(useColorSelection)
1626  {
1627  unsigned char m = (q.color.red()+ q.color.green()+ q.color.blue())/3;
1628  if(m>128)
1629  {
1630  glColor4ub ( std::max((int)(q.color.red())-mySelectionColorShift, 0),
1631  std::max((int)(q.color.green())-mySelectionColorShift, 0),
1632  std::max((int)(q.color.blue())-mySelectionColorShift, 0),
1633  q.color.alpha());
1634  }
1635  else{
1636  glColor4ub ( std::min(q.color.red()+mySelectionColorShift, 255),
1637  std::min(q.color.green()+mySelectionColorShift, 255),
1638  std::min(q.color.blue()+mySelectionColorShift, 255),
1639  q.color.alpha());
1640  }
1641  }
1642  else
1643  {
1644  glColor4ub ( q.color.red(), q.color.green(), q.color.blue(), q.color.alpha());
1645 
1646  }
1647 
1648  glNormal3f ( q.nx, q.ny ,q.nz );
1649  glVertex3f ( q.point1[0], q.point1[1], q.point1[2] );
1650  glVertex3f ( q.point2[0], q.point2[1], q.point2[2] );
1651  glVertex3f ( q.point3[0], q.point3[1], q.point3[2] );
1652  glVertex3f ( q.point4[0], q.point4[1], q.point4[2] );
1653  }
1654  glEnd();
1655  glPopName();
1656  }
1657  glEndList();
1658 }
1659 
1660 template <typename TSpace, typename TKSpace>
1661 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListQuadMapsWired(
1662 const typename DGtal::Display3D<TSpace, TKSpace>::QuadsMap & aQuadMap,
1663 unsigned int idList )
1664 {
1665  glNewList ( idList, GL_COMPILE );
1666  glPushName ( myNbListe );
1667  glDisable ( GL_LIGHTING );
1668  glBegin ( GL_LINES );
1669 
1670  for (auto const &mapElem: aQuadMap)
1671  {
1672  for(auto const &q: mapElem.second)
1673  {
1674  glColor4ub ( 150.0,150.0,150.0,255.0 );
1675  glColor4ub( Viewer3D<TSpace, TKSpace>::myCurrentLineColor.red(),
1676  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.green(),
1677  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.blue(),
1678  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.alpha() );
1679  glVertex3f ( q.point1[0], q.point1[1], q.point1[2] );
1680  glVertex3f ( q.point2[0], q.point2[1], q.point2[2] );
1681  glVertex3f ( q.point2[0], q.point2[1], q.point2[2] );
1682  glColor4ub( Viewer3D<TSpace, TKSpace>::myCurrentLineColor.red(),
1683  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.green(),
1684  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.blue(),
1685  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.alpha() );
1686  glVertex3f ( q.point3[0], q.point3[1], q.point3[2] );
1687  glVertex3f ( q.point3[0], q.point3[1], q.point3[2] );
1688  glVertex3f ( q.point4[0], q.point4[1], q.point4[2] );
1689  glColor4ub( Viewer3D<TSpace, TKSpace>::myCurrentLineColor.red(),
1690  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.green(),
1691  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.blue(),
1692  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.alpha() );
1693  glVertex3f ( q.point4[0], q.point4[1], q.point4[2] );
1694  glVertex3f ( q.point1[0], q.point1[1], q.point1[2] );
1695  }
1696  }
1697  glEnd();
1698  glEndList();
1699 
1700 }
1701 
1702 template <typename TSpace, typename TKSpace>
1703 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListTriangles(
1704 const std::vector<VectorTriangle> & aVectTriangle, unsigned int idList )
1705 {
1706  glNewList ( idList, GL_COMPILE );
1707  glPushName ( myNbListe );
1708  glEnable ( GL_LIGHTING );
1709  glBegin ( GL_TRIANGLES );
1710  for(auto const &tList: aVectTriangle)
1711  {
1712  for (auto const &t: tList)
1713  {
1714  glColor4ub (t.color.red(),t.color.green(),t.color.blue(),t.color.alpha() );
1715  glNormal3f (t.nx,t.ny ,t.nz );
1716  glVertex3f (t.point1[0],t.point1[1],t.point1[2] );
1717  glVertex3f (t.point2[0],t.point2[1],t.point2[2] );
1718  glVertex3f (t.point3[0],t.point3[1],t.point3[2] );
1719  }
1720  }
1721  glEnd();
1722  glEndList();
1723 }
1724 
1725 template <typename TSpace, typename TKSpace>
1726 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListTrianglesWired(
1727 const std::vector<VectorTriangle> & aVectTriangle, unsigned int idList )
1728 {
1729  glNewList ( idList, GL_COMPILE );
1730  glPushName ( myNbListe );
1731  glDisable ( GL_LIGHTING );
1732  glBegin ( GL_LINES );
1733  for (auto const &tList: aVectTriangle)
1734  {
1735  for (auto const &t: tList)
1736  {
1737  glColor4ub( Viewer3D<TSpace, TKSpace>::myCurrentLineColor.red(),
1738  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.green(),
1739  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.blue(),
1740  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.alpha() );
1741  glVertex3f (t.point1[0],t.point1[1],t.point1[2] );
1742  glVertex3f (t.point2[0],t.point2[1],t.point2[2] );
1743  glVertex3f (t.point2[0],t.point2[1],t.point2[2] );
1744  glVertex3f (t.point3[0],t.point3[1],t.point3[2] );
1745  glVertex3f (t.point3[0],t.point3[1],t.point3[2] );
1746  glVertex3f (t.point1[0],t.point1[1],t.point1[2] );
1747  }
1748  }
1749  glEnd();
1750  glEndList();
1751 
1752 }
1753 
1754 template <typename TSpace, typename TKSpace>
1755 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListPolygons(
1756 const std::vector<VectorPolygon> & aVectPolygon, unsigned int idList )
1757 {
1758  glNewList ( idList, GL_COMPILE );
1759  glPushName ( myNbListe );
1760  glEnable ( GL_LIGHTING );
1761 
1762  for(auto const &pList: aVectPolygon)
1763  {
1764  for (auto const &p: pList)
1765  {
1766  glBegin ( GL_POLYGON );
1767  glColor4ub ( p.color.red(), p.color.green(), p.color.blue(), p.color.alpha() );
1768  glNormal3f ( p.nx, p.ny ,p.nz );
1769  for(unsigned int j=0;j < (p.vertices).size();j++)
1770  {
1771  glVertex3f ( (p.vertices).at(j)[0], (p.vertices).at(j)[1], (p.vertices).at ( j )[2] );
1772  }
1773  glEnd();
1774  }
1775  }
1776  glEndList();
1777 }
1778 
1779 template <typename TSpace, typename TKSpace>
1780 void DGtal::Viewer3D<TSpace, TKSpace>::glCreateListPolygonsWired(
1781 const std::vector<VectorPolygon> & aVectPolygon, unsigned int idList )
1782 {
1783  glNewList ( idList, GL_COMPILE );
1784  glPushName ( myNbListe );
1785  glDisable ( GL_LIGHTING );
1786  glBegin ( GL_LINES );
1787  for(auto const &pList: aVectPolygon)
1788  {
1789  for (auto const &p: pList)
1790  {
1791  glColor4ub( Viewer3D<TSpace, TKSpace>::myCurrentLineColor.red(),
1792  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.green(),
1793  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.blue(),
1794  Viewer3D<TSpace, TKSpace>::myCurrentLineColor.alpha() );
1795  for(unsigned int j=0;j < (p.vertices).size();j++)
1796  {
1797  glVertex3f ( (p.vertices).at(j)[0], (p.vertices).at(j)[1], (p.vertices).at ( j )[2] );
1798  glVertex3f ( (p.vertices).at((j+1)%(p.vertices).size())[0],
1799  (p.vertices).at((j+1)%(p.vertices).size())[1],
1800  (p.vertices).at ( (j+1)%(p.vertices).size() )[2] );
1801  }
1802  }
1803  }
1804  glEnd();
1805  glEndList();
1806 }
1807 
1808 template <typename TSpace, typename TKSpace>
1809 void DGtal::Viewer3D<TSpace, TKSpace>::glUpdateTextureImages(
1810 const VectorTextureImage & aVectImage )
1811 {
1812 
1813  for(unsigned int j=0; j<myVectTextureImage.size(); j++){
1814  glDeleteTextures(1,&(myVectTextureImage[j].myTextureName));
1815  }
1816  myVectTextureImage.clear();
1817  for(unsigned int j=0; j<aVectImage.size(); j++)
1818  {
1819  typename Viewer3D<TSpace, TKSpace>::TextureImage aGSImage =
1820  aVectImage.at( j );
1821  GLTextureImage textureImg(aGSImage);
1822 
1823  glGenTextures(1, &textureImg.myTextureName);
1824  glBindTexture(GL_TEXTURE_2D, textureImg.myTextureName);
1825 
1826  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1827  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1828  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1829  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1830  if ( textureImg.myMode == Viewer3D<TSpace, TKSpace>::GrayScaleMode )
1831  {
1832  glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE, textureImg.myBufferWidth,
1833  textureImg.myBufferHeight, 0, GL_LUMINANCE,
1834  GL_UNSIGNED_BYTE, textureImg.myTextureImageBufferGS );
1835  }
1836  else if ( textureImg.myMode == Viewer3D<TSpace, TKSpace>::RGBMode )
1837  {
1838  glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, textureImg.myBufferWidth,
1839  textureImg.myBufferHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
1840  textureImg.myTextureImageBufferRGB );
1841  }
1842  myVectTextureImage.push_back(textureImg);
1843  }
1844 }
1845 
1846 template <typename TSpace, typename TKSpace>
1847 void DGtal::Viewer3D<TSpace, TKSpace>::glUpdateLightRenderingMode() const
1848 {
1849  if(myIsDoubleFaceRendering)
1850  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
1851  else
1852  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
1853 }
1854 
1855 
1856 template <typename TSpace, typename TKSpace>
1857 void DGtal::Viewer3D<TSpace, TKSpace>::glUpdateBackground()
1858 {
1859 
1860  if ( !myIsBackgroundDefault )
1861  {
1862  setBackgroundColor ( QColor ( 255, 255,255, 255 ) );
1863  }
1864  else
1865  {
1866  setBackgroundColor ( QColor ( myDefaultBackgroundColor.red(),
1867  myDefaultBackgroundColor.green(),
1868  myDefaultBackgroundColor.blue(),
1869  myDefaultBackgroundColor.alpha()));
1870  }
1871 }
1872 
1873 
1874 template <typename TSpace, typename TKSpace>
1875 QDomElement
1876 DGtal::Viewer3D<TSpace, TKSpace>::domElement( const QString & name,
1877  QDomDocument & document ) const
1878 {
1879  // Creates a custom node for a light position
1880  QDomElement deRendering = document.createElement("Rendering");
1881  deRendering.setAttribute("mode", myRenderingMode);
1882  QDomElement de = document.createElement("Light");
1883  de.setAttribute("pos_light_x", myLightPosition[0]);
1884  de.setAttribute("pos_light_y", myLightPosition[1]);
1885  de.setAttribute("pos_light_z", myLightPosition[2]);
1886  // Get default state domElement and append custom node
1887  QDomElement res = QGLViewer::domElement(name, document);
1888  res.appendChild(de);
1889  res.appendChild(deRendering);
1890  return res;
1891 }
1892 
1893 template <typename TSpace, typename TKSpace>
1894 void DGtal::Viewer3D<TSpace, TKSpace>::initFromDOMElement(
1895 const QDomElement & element )
1896 {
1897  // Restore standard state
1898  QGLViewer::initFromDOMElement(element);
1899  QDomElement child=element.firstChild().toElement();
1900  while (!child.isNull())
1901  {
1902  if (child.tagName() == "Rendering")
1903  {
1904  myRenderingMode = (RenderingMode)(child.attribute("mode").toInt());
1905  }
1906  if (child.tagName() == "Light")
1907  {
1908  if (child.hasAttribute("pos_light_x"))
1909  {
1910  myLightPosition[0] = child.attribute("pos_light_x").toDouble();
1911  }
1912 
1913  if (child.hasAttribute("pos_light_y"))
1914  {
1915  myLightPosition[1] = child.attribute("pos_light_y").toDouble();
1916  }
1917  if (child.hasAttribute("pos_light_z"))
1918  {
1919  myLightPosition[2] = child.attribute("pos_light_z").toDouble();
1920  }
1921  }
1922  child = child.nextSibling().toElement();
1923  }
1924  if(myLightPositionFixToCamera){
1925  updateRelativeCameraFromLightPosition();
1926  }
1927  updateRenderingCoefficients(myRenderingMode, false);
1928  update();
1929 }
1930 
1931 template <typename TSpace, typename TKSpace>
1932 void DGtal::Viewer3D<TSpace, TKSpace>::closeEvent( QCloseEvent * e )
1933 {
1934  if (myAutoSaveState)
1935  {
1936  saveStateToFile();
1937  }
1938  QGLViewer::closeEvent(e);
1939 }
1940 
1941 template <typename TSpace, typename TKSpace>
1942 void DGtal::Viewer3D<TSpace, TKSpace>::setGLDoubleRenderingMode(
1943 bool doubleSidedRendering )
1944 {
1945  myIsDoubleFaceRendering = doubleSidedRendering;
1946  glUpdateLightRenderingMode();
1947 }
1948 
1949 template <typename TSpace, typename TKSpace>
1950 void DGtal::Viewer3D<TSpace, TKSpace>::setGLMaterialShininessCoefficient(
1951 const GLfloat matShininessCoeff )
1952 {
1953  myMaterialShininessCoeff = matShininessCoeff;
1954  update();
1955 }
1956 
1957 template <typename TSpace, typename TKSpace>
1958 void DGtal::Viewer3D<TSpace, TKSpace>::setGLLightAmbientCoefficients(
1959 const GLfloat lightAmbientCoeffs[ 4 ] )
1960 {
1961  myLightAmbientCoeffs[0] = lightAmbientCoeffs[0];
1962  myLightAmbientCoeffs[1] = lightAmbientCoeffs[1];
1963  myLightAmbientCoeffs[2] = lightAmbientCoeffs[2];
1964  myLightAmbientCoeffs[3] = lightAmbientCoeffs[3];
1965  update();
1966 }
1967 
1968 template <typename TSpace, typename TKSpace>
1969 void DGtal::Viewer3D<TSpace, TKSpace>::setGLLightDiffuseCoefficients(
1970 const GLfloat lightDiffuseCoeffs[ 4 ] )
1971 {
1972  myLightDiffuseCoeffs[0] = lightDiffuseCoeffs[0];
1973  myLightDiffuseCoeffs[1] = lightDiffuseCoeffs[1];
1974  myLightDiffuseCoeffs[2] = lightDiffuseCoeffs[2];
1975  myLightDiffuseCoeffs[3] = lightDiffuseCoeffs[3];
1976  update();
1977 }
1978 
1979 template <typename TSpace, typename TKSpace>
1980 void DGtal::Viewer3D<TSpace, TKSpace>::setUseGLPointForBalls(
1981 const bool useOpenGLPt )
1982 {
1983  myUseGLPointsForBalls = useOpenGLPt;
1984 }
1985 
1986 template <typename TSpace, typename TKSpace>
1987 void DGtal::Viewer3D<TSpace, TKSpace>::updateRenderingCoefficients(
1988 const RenderingMode aRenderMode, bool displayState )
1989 {
1990  std::stringstream ss;
1991  ss << "Rendering mode ";
1992 
1993  GLfloat newCoefDiff, newCoefSpec = 1.0f;
1994  switch (aRenderMode)
1995  {
1996  case RenderingMode::RenderingDefault :
1997  newCoefDiff = myDefaultRenderDiff;
1998  newCoefSpec = myDefaultRenderSpec;
1999  ss << "Default (diffuse with few specular)";
2000  break;
2001  case RenderingMode::RenderingMetallic :
2002  newCoefDiff = myMetallicRenderDiff;
2003  newCoefSpec = myMetallicRenderSpec;
2004  ss << "Metallic (diffuse with specular)";
2005  break;
2006  case RenderingMode::RenderingPlastic :
2007  newCoefDiff = myPlasticRenderDiff;
2008  newCoefSpec = myPlasticRenderSpec;
2009  ss << "Plastic (few diffuse with large specular)";
2010  break;
2011  case RenderingMode::RenderingLambertian :
2012  newCoefDiff = myLambertRenderDiff;
2013  newCoefSpec = myLambertRenderSpec;
2014  ss << "Lambertian (only diffuse)";
2015  break;
2016  }
2017  for (unsigned int i = 0; i<3; i++)
2018  myLightDiffuseCoeffs[i] = newCoefDiff;
2019  myLightDiffuseCoeffs[3] = 1.0;
2020  for (unsigned int i = 0; i<3; i++)
2021  myLightSpecularCoeffs[i] = newCoefSpec;
2022  myLightSpecularCoeffs[3] = 1.0;
2023  if (displayState)
2024  {
2025  displayMessage(QString(ss.str().c_str()), 3000);
2026  }
2027  update();
2028 }
2029 
2030 template <typename TSpace, typename TKSpace>
2031 void DGtal::Viewer3D<TSpace, TKSpace>::setGLLightSpecularCoefficients(
2032 const GLfloat lightSpecularCoeffs[ 4 ] )
2033 {
2034  myLightSpecularCoeffs[0] = lightSpecularCoeffs[0];
2035  myLightSpecularCoeffs[1] = lightSpecularCoeffs[1];
2036  myLightSpecularCoeffs[2] = lightSpecularCoeffs[2];
2037  myLightSpecularCoeffs[3] = lightSpecularCoeffs[3];
2038  update();
2039 }
2040 
2041 template <typename TSpace, typename TKSpace>
2042 inline void DGtal::Viewer3D<TSpace, TKSpace>::show()
2043 {
2044  QGLViewer::show();
2045  updateList(false);
2046 }
2047 
2048 template <typename TSpace, typename TKSpace>
2049 inline void DGtal::Viewer3D<TSpace, TKSpace>::paintGL()
2050 {
2051  if ( displaysInStereo() )
2052  {
2053  for ( int view = 1; view >= 0; --view )
2054  {
2055  // Clears screen, set model view matrix with shifted matrix for ith buffer
2056  preDrawStereo( view );
2057  postDraw();
2058  // Used defined method. Default is empty
2059  if ( camera()->frame()->isManipulated() )
2060  fastDraw();
2061  else
2062  draw();
2063  }
2064  }
2065  else
2066  {
2067  // Clears screen, set model view matrix...
2068  preDraw();
2069  postDraw();
2070  // Used defined method. Default calls draw()
2071  if ( camera()->frame()->isManipulated() )
2072  fastDraw();
2073  else
2074  draw();
2075  // Add visual hints: axis, camera, grid...
2076  }
2077  Q_EMIT drawFinished( true );
2078 }
2079 
2080 template <typename TSpace, typename TKSpace>
2081 void DGtal::Viewer3D<TSpace, TKSpace>::updateLightCoordsFromCamera()
2082 {
2083  Vec posLCam;
2084  posLCam[0] = myLightPositionRefCamera[0];
2085  posLCam[1] = myLightPositionRefCamera[1];
2086  posLCam[2] = myLightPositionRefCamera[2];
2087  Vec posL = camera()->worldCoordinatesOf(posLCam);
2088  myLightPosition[0] = static_cast<GLfloat>(posL[0]);
2089  myLightPosition[1] = static_cast<GLfloat>(posL[1]);
2090  myLightPosition[2] = static_cast<GLfloat>(posL[2]);
2091  myLightPosition[3] = 1.0f;
2092 }
2093 
2094 template <typename TSpace, typename TKSpace>
2095 void DGtal::Viewer3D<TSpace, TKSpace>::updateRelativeCameraFromLightPosition()
2096 {
2097  Vec posL;
2098  posL[0] = myLightPosition[0];
2099  posL[1] = myLightPosition[1];
2100  posL[2] = myLightPosition[2];
2101  Vec posLCam = camera()->cameraCoordinatesOf(posL);
2102  myLightPositionRefCamera[0] = static_cast<GLfloat>(posLCam[0]);
2103  myLightPositionRefCamera[1] = static_cast<GLfloat>(posLCam[1]);
2104  myLightPositionRefCamera[2] = static_cast<GLfloat>(posLCam[2]);
2105 }
2106 
2107 
2108