DGtal  0.9.2
Display3D.h
1 
17 #pragma once
18 
31 #if defined(Display3D_RECURSES)
32 #error Recursive header files inclusion detected in Display3D.h
33 #else // defined(Display3D_RECURSES)
34 
35 #define Display3D_RECURSES
36 
37 #if !defined Display3D_h
38 
39 #define Display3D_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <vector>
45 #include <algorithm>
46 #include <map>
47 #include "DGtal/kernel/domains/CDomain.h"
48 #include "DGtal/base/Common.h"
49 #include "DGtal/base/CountedPtr.h"
50 #include "DGtal/io/Color.h"
51 #include "DGtal/images/CImage.h"
52 #include "DGtal/images/CConstImage.h"
53 #include "DGtal/shapes/Mesh.h"
54 
56 #include "DGtal/topology/CanonicCellEmbedder.h"
57 #include "DGtal/topology/CanonicSCellEmbedder.h"
58 #include "DGtal/kernel/CanonicEmbedder.h"
59 #include "DGtal/helpers/StdDefs.h"
60 #include "DGtal/io/DrawWithDisplay3DModifier.h"
61 #include "DGtal/kernel/CSpace.h"
62 
63 
65 
66 
67 namespace DGtal
68 {
69 
71  // class Display3D
91  template < typename Space = Z3i::Space, typename KSpace = Z3i::KSpace>
92  class Display3D
93  {
94  public:
95 
96  BOOST_CONCEPT_ASSERT((concepts::CSpace<Space>));
97  public:
98 
101  typedef typename Space::RealPoint RealPoint;
103  typedef typename Space::RealVector RealVector;
107 
109  typedef int (*SelectCallbackFct)( void* viewer, DGtal::int32_t name, void* data );
110 
111  protected:
112 
118  SelectCallbackFctStore( SelectCallbackFct _fct,
119  void* _data,
120  DGtal::int32_t _min, DGtal::int32_t _max )
121  : fct( _fct ), data( _data ), min( _min ), max( _max ) {}
122  bool operator<( const SelectCallbackFctStore& other ) const
123  {
124  return ( min < other.min ); // simple since there is no overlap.
125  }
126  bool isSelected( DGtal::int32_t name ) const
127  { return ( min <= name ) && ( name <= max ); }
128 
129  SelectCallbackFct fct;
130  void* data;
133  };
134 
138  struct CommonD3D {
141 
142  protected:
143  ~CommonD3D() = default;
144  };
145 
149  struct LineD3D : public CommonD3D {
150  RealPoint point1;
151  RealPoint point2;
152  double width;
153  bool isSigned;
154  bool signPos;
155  };
156 
160  struct CubeD3D : public CommonD3D {
162  RealPoint center;
164  double width;
165  };
166 
173  struct ClippingPlaneD3D : public CommonD3D {
174  double a,b,c,d;
175  };
176 
177 
185  struct QuadD3D : public CommonD3D {
186  RealPoint point1;
187  RealPoint point2;
188  RealPoint point3;
189  RealPoint point4;
190  double nx, ny, nz;
191  };
192 
193 
194 
200  struct TriangleD3D : public CommonD3D {
201  RealPoint point1;
202  RealPoint point2;
203  RealPoint point3;
204  double nx, ny, nz;
205  };
206 
207 
208  public:
209 
210 
211 
215  //have to be public because of external functions
216  struct BallD3D : public CommonD3D
217  {
219  const double & operator[]( unsigned int i ) const
220  {
221  assert(i<3);
222  return center[i];
223  };
224  double & operator[]( unsigned int i )
225  {
226  assert(i<3);
227  return center[i];
228  };
229  RealPoint center;
230  bool isSigned;
231  bool signPos;
232  double radius;
233  unsigned int resolution;
234  };
235 
236 
241  struct PolygonD3D : public CommonD3D
242  {
243  std::vector<RealPoint> vertices;
244  double nx, ny, nz;
246  };
247 
248 
250 
251 
252  public:
254  typedef std::map<DGtal::int32_t, std::vector< QuadD3D > > QuadsMap;
255 
257  typedef std::map<DGtal::int32_t, std::vector< CubeD3D > > CubesMap;
258 
259 
260  protected:
262  KSpace myKSpace;
264  Embedder *myEmbedder;
266  CellEmbedder *myCellEmbedder;
268  SCellEmbedder *mySCellEmbedder;
269 
270 
271 
272 
273  //----end of private data
274 
275  // ----------------------- Standard services ------------------------------
276  public:
277 
281  virtual ~Display3D()
282  {
283  delete myEmbedder;
284  delete mySCellEmbedder;
285  delete myCellEmbedder;
286  }
287 
292  Display3D( const KSpace & KSEmb )
293  : myKSpace( KSEmb )
294  , myEmbedder( new Embedder() )
295  , myCellEmbedder( new CellEmbedder( myKSpace ) )
296  , mySCellEmbedder( new SCellEmbedder( myKSpace ) )
297  , myBoundingPtEmptyTag( true )
298  , myCurrentFillColor( 220, 220, 220 )
299  , myCurrentLineColor( 22, 22, 222, 50 )
300  {
301  }
302 
308  : Display3D( KSpace() )
309  {
310  }
311 
313  Display3D( const Display3D & ) = delete;
314 
316  Display3D( Display3D && ) = delete;
317 
319  Display3D & operator= ( const Display3D & ) = delete;
320 
322  Display3D & operator= ( Display3D && ) = delete;
323 
324  // ----------------------- Interface --------------------------------------
325  public:
326 
328  const Embedder& embedder() const
329  { return *myEmbedder; }
330 
332  const CellEmbedder& cellEmbedder() const
333  { return *myCellEmbedder; }
334 
336  const SCellEmbedder& sCellEmbedder() const
337  { return *mySCellEmbedder; }
338 
340  const KSpace& space() const
341  { return myKSpace; }
342 
347  virtual void setFillColor(DGtal::Color aColor);
348 
353  virtual void setFillTransparency(unsigned char alpha);
354 
355 
360  virtual void setLineColor(DGtal::Color aColor);
361 
362 
368  virtual DGtal::Color getFillColor();
369 
375  virtual DGtal::Color getLineColor();
376 
381  virtual void setKSpace( const KSpace & aKSpace );
382 
383 
388  void setName3d( DGtal::int32_t name = -1 );
389 
393  DGtal::int32_t name3d() const;
394 
406  void setSelectCallback3D( SelectCallbackFct fct, void* data,
407  DGtal::int32_t min_name, DGtal::int32_t max_name );
408 
415  SelectCallbackFct getSelectCallback3D( DGtal::int32_t aName, void*& data ) const;
416 
417  // ----------------------- Graphical directives ----------------------------------
418  public:
419 
420 
432  void addClippingPlane(double a, double b, double c, double d, bool drawPlane);
433 
434 
435 
436 
444  std::string getMode( const std::string & objectName ) const;
445 
451  void createNewLineList(std::string s= "");
452 
453 
459  void createNewBallList(std::string s= "");
460 
461 
469 
470 
477  bool deleteCubeList(const DGtal::int32_t name);
478 
485 
486 
493  bool deleteQuadList(const DGtal::int32_t name);
494 
500  void createNewTriangleList(std::string s= "");
501 
507  void createNewPolygonList(std::string s= "");
508 
509 
521  void addQuad(const RealPoint &p1, const RealPoint &p2,
522  const RealPoint &p3, const RealPoint &p4);
523 
542  void addQuadWithNormal(const RealPoint &p1, const RealPoint &p2,
543  const RealPoint &p3, const RealPoint &p4,
544  const RealPoint &n,
545  const bool enableReorientation,
546  const bool enableDoubleFace = false);
547 
557  void addQuadFromSurfelCenter(const RealPoint &baseQuadCenter,
558  bool xSurfel, bool ySurfel, bool zSurfel);
559 
560 
561 
584  void addQuadFromSurfelCenterWithNormal(const RealPoint &baseQuadCenter, bool xSurfel, bool ySurfel, bool zSurfel,
585  const RealVector &aNormal,
586  const bool enableReorientation,
587  const bool sign,
588  const bool enableDoubleFace = false);
589 
590 
597  void addTriangle(const RealPoint &p1, const RealPoint &p2, const RealPoint &p3);
598 
599 
604  void addPolygon(const std::vector<RealPoint> &vertices);
605 
606 
616  void addLine(const RealPoint &p1, const RealPoint &p2, const double width=0.03);
617 
618 
626  void addCube(const RealPoint &center, double width=1.0);
627 
628 
636  void addBall(const RealPoint &center ,
637  const double radius=0.5,
638  const unsigned int resolution = 30);
639 
640 
641 
655  void addPrism(const RealPoint &baseQuadCenter,
656  bool xSurfel, bool ySurfel, bool zSurfel, double sizeShiftFactor,
657  double sizeFactor=1.0, bool isSigned= false, bool aSign=true);
658 
659 
660 
669  void addBasicSurfel(const RealPoint &baseQuadCenter,
670  bool xSurfel, bool ySurfel, bool zSurfel);
671 
672 
680  void addCone(const RealPoint &p1, const RealPoint &p2,
681  double width=0.08);
682 
683 
690  void addCylinder(const RealPoint &p1, const RealPoint &p2,
691  const double width=0.02);
692 
693 
699  void updateBoundingBox(const RealPoint &point);
700 
701 
702 
703 
710  void exportToMesh(Mesh<RealPoint> & aMesh ) const;
711 
712 
721  template <typename TDrawableWithDisplay3D>
722  Display3D & operator<<( const TDrawableWithDisplay3D & object );
723 
724 
725 
730  void selfDisplay ( std::ostream & out ) const;
731 
732 
737  bool isValid() const;
738 
739 
743  void clear();
744 
745 
746 
752  RealPoint embed(const typename Space::Point & dp) const ;
753 
759  RealPoint embedKS( const typename KSpace::SCell & cell ) const;
760 
761 
767  RealPoint embedKS( const DGtal::TransformedPrism& aTrans ) const;
768 
769 
775  RealPoint embedK( const typename KSpace::Cell & cell ) const;
776 
777  //---end interface
778 
779  // ------------------------- Protected Datas ------------------------------
780  public:
781 
786  typedef std::map< std::string, std::string > ModeMapping;
787 
792  typedef std::map< std::string,CountedPtr<DrawableWithDisplay3D> > StyleMapping;
793 
794 
795  ModeMapping myModes;
806  StyleMapping myStyles;
807 
811  double myBoundingPtUp [3];
813  double myBoundingPtLow [3];
814 
815  protected:
816  //the current fill color of the display
818  //the current line color of the display
820 
824 
827  std::vector< std::vector<LineD3D> > myLineSetList;
828 
831  std::vector< std::vector<BallD3D> > myBallSetList;
832 
835  std::vector< ClippingPlaneD3D > myClippingPlaneList;
836 
839  std::vector< QuadD3D > myPrismList;
840 
845  QuadsMap myQuadsMap;
846 
848  std::vector<std::vector< TriangleD3D > > myTriangleSetList;
849 
851  std::vector<std::vector<PolygonD3D> > myPolygonSetList;
852 
853 
857  CubesMap myCubesMap;
858 
859 
862  std::vector<std::string> myCubeSetNameList;
863 
866  std::vector<std::string> myLineSetNameList;
867 
870  std::vector<std::string> myBallSetNameList;
873  std::vector<std::string> myClippingPlaneNameList;
874 
877  std::vector<std::string> myPrismNameList;
878 
881  std::vector<std::string> myQuadSetNameList;
882 
885  std::vector<std::string> myTriangleSetNameList;
886 
889  std::vector<std::string> myPolygonSetNameList;
890 
894 
897  std::set<SelectCallbackFctStore> mySelectCallBackFcts;
898 
899  //----end of protected datas
900 
901  // ------------------------- Internals ------------------------------------
902  protected:
903 
910  static void cross (double dst[3], double srcA[3], double srcB[3]);
911 
916  static void normalize (double vec[3]);
917 
918 
919  }; // end of class Display3D
920 
927  template <typename Space , typename KSpace >
928  std::ostream&
929  operator<< ( std::ostream & out, const DGtal::Display3D<Space , KSpace > & object );
930 
931 
932  template <typename Space , typename KSpace >
939  void
940  operator>> ( const Display3D<Space , KSpace > &aDisplay3D,
942 
943 
950  template < typename Space , typename KSpace >
951  void
952  operator>> ( const Display3D< Space , KSpace > &aDisplay3D, std::string aFilename);
953 
954 
955 } // namespace DGtal
956 
957 
959 // Includes inline functions.
960 #include "DGtal/io/Display3D.ih"
961 
962 
963 // //
965 
966 #endif // !defined Display3D_h
967 
968 #undef Display3D_RECURSES
969 #endif // else defined(Display3D_RECURSES)
virtual void setFillColor(DGtal::Color aColor)
bool deleteCubeList(const DGtal::int32_t name)
double myCurrentfShiftVisuPrisms
Definition: Display3D.h:823
DGtal::int32_t createNewCubeList()
virtual ~Display3D()
Definition: Display3D.h:281
virtual void setFillTransparency(unsigned char alpha)
virtual DGtal::Color getFillColor()
static const RealPoint::Dimension dimension
Definition: Display3D.h:218
std::vector< std::vector< LineD3D > > myLineSetList
Definition: Display3D.h:827
static const Dimension dimension
Copy of the static dimension of the Point/Vector.
Definition: PointVector.h:167
double myBoundingPtUp[3]
upper point of the bounding box
Definition: Display3D.h:811
const Embedder & embedder() const
Definition: Display3D.h:328
std::vector< std::vector< BallD3D > > myBallSetList
Definition: Display3D.h:831
void addBall(const RealPoint &center, const double radius=0.5, const unsigned int resolution=30)
KSpace myKSpace
The Khalimsky space.
Definition: Display3D.h:262
std::vector< std::string > myQuadSetNameList
Definition: Display3D.h:881
DGtal::int32_t myName3d
Definition: Display3D.h:893
void createNewTriangleList(std::string s="")
unsigned int resolution
Definition: Display3D.h:233
void addCone(const RealPoint &p1, const RealPoint &p2, double width=0.08)
static void cross(double dst[3], double srcA[3], double srcB[3])
void addPrism(const RealPoint &baseQuadCenter, bool xSurfel, bool ySurfel, bool zSurfel, double sizeShiftFactor, double sizeFactor=1.0, bool isSigned=false, bool aSign=true)
std::vector< std::vector< TriangleD3D > > myTriangleSetList
Represents all the triangles drawn in the Display3D.
Definition: Display3D.h:848
std::vector< std::string > myCubeSetNameList
Definition: Display3D.h:862
Embedder * myEmbedder
an embeder from a dgtal space point to a real space point
Definition: Display3D.h:264
void setSelectCallback3D(SelectCallbackFct fct, void *data, DGtal::int32_t min_name, DGtal::int32_t max_name)
Aim: This class is defined to represent a surface mesh through a set of vertices and faces...
Definition: Mesh.h:91
void addBasicSurfel(const RealPoint &baseQuadCenter, bool xSurfel, bool ySurfel, bool zSurfel)
void createNewPolygonList(std::string s="")
virtual void setLineColor(DGtal::Color aColor)
DGtal::Dimension Dimension
Copy of the dimension type.
Definition: PointVector.h:164
void setName3d(DGtal::int32_t name=-1)
SCellEmbedder * mySCellEmbedder
an embeder from a signed khalimsky space point to a real space point
Definition: Display3D.h:268
Space::RealPoint RealPoint
RealPoint type.
Definition: Display3D.h:101
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
DGtal::int32_t name
The "OpenGL name" associated with the graphical structure, used for selecting it (-1 is none)...
Definition: Display3D.h:140
CellEmbedder * myCellEmbedder
an embeder from a unsigned khalimsky space point to a real space point
Definition: Display3D.h:266
void addQuadFromSurfelCenter(const RealPoint &baseQuadCenter, bool xSurfel, bool ySurfel, bool zSurfel)
std::vector< QuadD3D > myPrismList
Definition: Display3D.h:839
void updateBoundingBox(const RealPoint &point)
SelectCallbackFctStore(SelectCallbackFct _fct, void *_data, DGtal::int32_t _min, DGtal::int32_t _max)
Definition: Display3D.h:118
DGtal::int32_t name3d() const
std::map< DGtal::int32_t, std::vector< QuadD3D > > QuadsMap
The type that maps identifier name -> vector of QuadD3D.
Definition: Display3D.h:254
const double & operator[](unsigned int i) const
Definition: Display3D.h:219
std::vector< RealPoint > vertices
Definition: Display3D.h:243
const KSpace & space() const
Definition: Display3D.h:340
virtual void setKSpace(const KSpace &aKSpace)
DGtal::int32_t createNewQuadList()
DGtal::Color myCurrentLineColor
Definition: Display3D.h:819
std::map< std::string, std::string > ModeMapping
Definition: Display3D.h:786
ModeMapping myModes
Definition: Display3D.h:795
bool myBoundingPtEmptyTag
True if the bounding box is empty (no objects added)
Definition: Display3D.h:809
void createNewLineList(std::string s="")
SelectCallbackFct getSelectCallback3D(DGtal::int32_t aName, void *&data) const
void selfDisplay(std::ostream &out) const
CubesMap myCubesMap
Definition: Display3D.h:857
CanonicEmbedder< Space > Embedder
Definition: Display3D.h:104
void exportToMesh(Mesh< RealPoint > &aMesh) const
Aim: A trivial embedder for signed and unsigned cell, which corresponds to the canonic injection of c...
Display3D & operator<<(const TDrawableWithDisplay3D &object)
RealPoint center
The center coordinate of the cube.
Definition: Display3D.h:162
std::map< DGtal::int32_t, std::vector< CubeD3D > > CubesMap
The type that maps identifier name -> vector of CubeD3D.
Definition: Display3D.h:257
Display3D(const KSpace &KSEmb)
Definition: Display3D.h:292
static void normalize(double vec[3])
bool isValid() const
virtual DGtal::Color getLineColor()
CanonicSCellEmbedder< KSpace > SCellEmbedder
Definition: Display3D.h:106
RealPoint embedK(const typename KSpace::Cell &cell) const
std::string getMode(const std::string &objectName) const
void addLine(const RealPoint &p1, const RealPoint &p2, const double width=0.03)
void operator>>(const Display3D< Space, KSpace > &aDisplay3D, DGtal::Mesh< typename Display3D< Space, KSpace >::RealPoint > &aMesh)
~CommonD3D()=default
Protected destructor to disallow polymorphism.
DGtal is the top-level namespace which contains all DGtal functions and types.
bool operator<(const SelectCallbackFctStore &other) const
Definition: Display3D.h:122
Aim: A trivial embedder for digital points, which corresponds to the canonic injection of Zn into Rn...
std::vector< std::string > myBallSetNameList
Definition: Display3D.h:870
Display3D & operator=(const Display3D &)=delete
Assignment operator. Deleted.
void addTriangle(const RealPoint &p1, const RealPoint &p2, const RealPoint &p3)
std::vector< ClippingPlaneD3D > myClippingPlaneList
Definition: Display3D.h:835
double width
The width of a cube face.
Definition: Display3D.h:164
const CellEmbedder & cellEmbedder() const
Definition: Display3D.h:332
double myBoundingPtLow[3]
lower point of the bouding box
Definition: Display3D.h:813
std::map< std::string, CountedPtr< DrawableWithDisplay3D > > StyleMapping
Definition: Display3D.h:792
std::vector< std::string > myLineSetNameList
Definition: Display3D.h:866
void addPolygon(const std::vector< RealPoint > &vertices)
BOOST_CONCEPT_ASSERT((concepts::CSpace< Space >))
QuadsMap myQuadsMap
Definition: Display3D.h:845
std::vector< std::string > myTriangleSetNameList
Definition: Display3D.h:885
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
std::vector< std::string > myPrismNameList
Definition: Display3D.h:877
RealPoint embed(const typename Space::Point &dp) const
DGtal::Color color
Color used for displaying the graphical structure.
Definition: Display3D.h:139
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
void addQuadWithNormal(const RealPoint &p1, const RealPoint &p2, const RealPoint &p3, const RealPoint &p4, const RealPoint &n, const bool enableReorientation, const bool enableDoubleFace=false)
bool deleteQuadList(const DGtal::int32_t name)
Space::RealVector RealVector
RealVector type.
Definition: Display3D.h:103
void addCylinder(const RealPoint &p1, const RealPoint &p2, const double width=0.02)
void createNewBallList(std::string s="")
std::set< SelectCallbackFctStore > mySelectCallBackFcts
Definition: Display3D.h:897
void addCube(const RealPoint &center, double width=1.0)
void addQuad(const RealPoint &p1, const RealPoint &p2, const RealPoint &p3, const RealPoint &p4)
bool isSelected(DGtal::int32_t name) const
Definition: Display3D.h:126
double & operator[](unsigned int i)
Definition: Display3D.h:224
const SCellEmbedder & sCellEmbedder() const
Definition: Display3D.h:336
DGtal::Color myCurrentFillColor
Definition: Display3D.h:817
StyleMapping myStyles
Definition: Display3D.h:806
std::vector< std::vector< PolygonD3D > > myPolygonSetList
Represents all the polygon drawn in the Display3D.
Definition: Display3D.h:851
std::vector< std::string > myClippingPlaneNameList
Definition: Display3D.h:873
RealPoint embedKS(const typename KSpace::SCell &cell) const
std::vector< std::string > myPolygonSetNameList
Definition: Display3D.h:889
Aim: This semi abstract class defines the stream mechanism to display 3d primitive (like BallVector...
Definition: Common.h:134
Display3D< Space, KSpace > Self
Definition: Display3D.h:99
class to modify the position and scale to construct better illustration mode.
void addQuadFromSurfelCenterWithNormal(const RealPoint &baseQuadCenter, bool xSurfel, bool ySurfel, bool zSurfel, const RealVector &aNormal, const bool enableReorientation, const bool sign, const bool enableDoubleFace=false)
CanonicCellEmbedder< KSpace > CellEmbedder
Definition: Display3D.h:105
void addClippingPlane(double a, double b, double c, double d, bool drawPlane)