From 6a0c35416ed7a96c468012f7e57f327f938584b0 Mon Sep 17 00:00:00 2001 From: Cyrille Valladeau <cyrille.valladeau@c-s.fr> Date: Wed, 7 Oct 2009 19:24:17 +0200 Subject: [PATCH] ENH : add cross gl component representation --- Code/Visualization/otbCrossGlComponent.cxx | 122 ++++++++++++++ Code/Visualization/otbCrossGlComponent.h | 184 +++++++++++++++++++++ 2 files changed, 306 insertions(+) create mode 100644 Code/Visualization/otbCrossGlComponent.cxx create mode 100644 Code/Visualization/otbCrossGlComponent.h diff --git a/Code/Visualization/otbCrossGlComponent.cxx b/Code/Visualization/otbCrossGlComponent.cxx new file mode 100644 index 0000000000..c41f6e8d58 --- /dev/null +++ b/Code/Visualization/otbCrossGlComponent.cxx @@ -0,0 +1,122 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbCrossGlComponent_cxx +#define __otbCrossGlComponent_cxx + +#include "otbCrossGlComponent.h" + + +namespace otb +{ +CrossGlComponent +::CrossGlComponent() : m_IndexList(),m_Spacing(), m_Origin(), m_GluTesselator(), + m_ColorList(), m_LineWidth(1.5),m_CrossWidth(10), m_RedColor() +{ + // Default color is red + m_RedColor.Fill(0); + m_RedColor[0]=1.; + m_RedColor[3]=0.75; + + + // Intialize origin and spacing + m_Origin.Fill(0.); + m_Spacing.Fill(1.); + + // Create the tesselator + m_GluTesselator = gluNewTess(); +} + +CrossGlComponent +::~CrossGlComponent() +{ + // Delete the tesselator + gluDeleteTess(m_GluTesselator); +} + + +void +CrossGlComponent +::Render(const RegionType& extent,const AffineTransformType * space2ScreenTransform) +{ + if(m_IndexList.size() == 0) + { + // nothing to render, return + return; + } + + // Set up blending and color + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glColor4d(m_RedColor[0],m_RedColor[1],m_RedColor[2],m_RedColor[3]); + + // Set up line width + double previousWidth = 0.; + glGetDoublev(GL_LINE_WIDTH,&previousWidth); + + // convert line width to screen line width + VectorType imageLineWidth; + imageLineWidth.Fill(m_LineWidth); + VectorType screenLineWidth = space2ScreenTransform->TransformVector(imageLineWidth); + glLineWidth(screenLineWidth[0]); + // Do we need to render boundaries only (for polygons) + gluTessProperty(m_GluTesselator,GLU_TESS_BOUNDARY_ONLY,false);//m_RenderPolygonBoundariesOnly); + + // Enabling line antialiasing + glEnable(GL_LINE_SMOOTH); + + for(unsigned int i=0; i<m_IndexList.size(); i++) + { + this->Render(i,extent,space2ScreenTransform); + } + + glDisable(GL_LINE_SMOOTH); + glDisable(GL_BLEND); + glLineWidth(previousWidth); +} + + +void +CrossGlComponent +::Render(unsigned int id, const RegionType & extent, const AffineTransformType * space2ScreenTransform) +{ + glColor4d(m_ColorList[id][0],m_ColorList[id][1],m_ColorList[id][2],m_ColorList[id][3]); + + // Take into account pixel spacing and origin + //PointType spacePoint = dataNode->GetPoint(); + PointType spacePoint; + spacePoint[0]*= m_Spacing[0]; + spacePoint[1]*= m_Spacing[1]; + spacePoint[0] = m_IndexList[id][0] + m_Origin[0]; + spacePoint[1] = m_IndexList[id][1] + m_Origin[1]; + + // Transform to a screen point + PointType screenPoint = space2ScreenTransform->TransformPoint(spacePoint); + + glBegin(GL_LINES); + // Draw a cross + glVertex2d(screenPoint[0]-m_CrossWidth,screenPoint[1]); + glVertex2d(screenPoint[0]+m_CrossWidth,screenPoint[1]); + glVertex2d(screenPoint[0],screenPoint[1]-m_CrossWidth); + glVertex2d(screenPoint[0],screenPoint[1]+m_CrossWidth); + glEnd(); +} + +} +#endif + + diff --git a/Code/Visualization/otbCrossGlComponent.h b/Code/Visualization/otbCrossGlComponent.h new file mode 100644 index 0000000000..1c11b6ec39 --- /dev/null +++ b/Code/Visualization/otbCrossGlComponent.h @@ -0,0 +1,184 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbCrossGlComponent_h +#define __otbCrossGlComponent_h + +#include "otbGlComponent.h" +#include "itkPreOrderTreeIterator.h" + +# ifdef __APPLE__ +# include <OpenGL/glu.h> +# else +# include <GL/glu.h> +# endif + +// There are function prototype conflits under cygwin between standard w32 API +// and standard C ones +#ifndef CALLBACK +#if defined(_WINDOWS) || defined(__CYGWIN__) +#define CALLBACK __stdcall +#else +#define CALLBACK +#endif +#endif + + +namespace otb +{ +/** \class CrossGlComponent +* \brief This Gl Component to render a Cross. +* No checking is done upon the adequation between the Cross +* projection and the underlying image projection. +* +* Origin and Spacing allows to fit to the image axis. +* \ingroup Visualization + */ + +class CrossGlComponent : public GlComponent +{ +public: + /** Standard class typedefs */ + typedef CrossGlComponent Self; + typedef GlComponent Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + typedef Superclass::RegionType RegionType; + + // affine transform + typedef Superclass::AffineTransformType AffineTransformType; + typedef AffineTransformType::InputPointType PointType; + typedef AffineTransformType::InputVectorType VectorType; + typedef Superclass::ColorType ColorType; + + typedef itk::Index<> IndexType; + typedef std::vector<IndexType> IndexListType; + typedef std::vector<ColorType> ColorListType; + + /** Runtime information */ + itkTypeMacro(CrossGlComponent,GlComponent); + + /** New macro */ + itkNewMacro(Self); + + /// Render the vector data + virtual void Render(const RegionType& extent,const AffineTransformType * space2ScreenTransform); + + /** Set/Get the grid spacing */ + itkSetMacro(Spacing,VectorType); + itkGetConstReferenceMacro(Spacing,VectorType); + + /** Set/Get the grid origin */ + itkSetMacro(Origin,PointType); + itkGetConstReferenceMacro(Origin,PointType); + + /** Set/Get the index to render */ + void SetIndexList(IndexListType idList) { m_IndexList = idList; }; + IndexListType GetIndexList() { return m_IndexList; }; + void AddIndex(IndexType id) { m_IndexList.push_back(id); m_ColorList.push_back(m_RedColor); }; + void RemoveIndex(unsigned int id) + { + if( id >= m_IndexList.size() ) + itkExceptionMacro(<<"Index out of size "); + + m_IndexList.erase(m_IndexList.begin()+id); + }; + + /** Set/Get the color */ + void SetColorList(ColorListType colorList) { m_ColorList = colorList; }; + ColorListType GetColorList() { return m_ColorList; }; + void ChangeColor(ColorType color, unsigned int id) + { + if( id >= m_ColorList.size() ) + itkExceptionMacro(<<"Index out of size "); + + m_ColorList[id] = color; + }; + void RemoveColor(unsigned int id) + { + if( id >= m_ColorList.size() ) + itkExceptionMacro(<<"Index out of size "); + + m_ColorList.erase(m_ColorList.begin()+id); + }; + + /** Clear all*/ + void Clear() { m_IndexList.clear(); m_ColorList.clear(); }; + void ClearIndex(unsigned int id) + { + this->RemoveIndex(id); + this->RemoveColor(id); + }; + + /** Set/Get the line width */ + itkSetMacro(LineWidth,double); + itkGetMacro(LineWidth,double); + + /** Set/Get the cross width */ + itkSetMacro(CrossWidth,double); + itkGetMacro(CrossWidth,double); + +protected: + /** Constructor */ + CrossGlComponent(); + /** Destructor */ + virtual ~CrossGlComponent(); + /** Printself method */ + virtual void PrintSelf(std::ostream& os, itk::Indent indent) const + { + Superclass::PrintSelf(os,indent); + } + + // Recursive rendering method + virtual void Render(unsigned int id, const RegionType & extent, const AffineTransformType * space2ScreenTransform); + + +private: + CrossGlComponent(const Self&); // purposely not implemented + void operator=(const Self&); // purposely not implemented + + /// Index point to render + IndexListType m_IndexList; + + /// Spacing of the image grid + VectorType m_Spacing; + + /// Origin of the image + PointType m_Origin; + + /// The GluTesselator object to render complex polygons + GLUtesselator * m_GluTesselator; + + /// Color of the vector layer + ColorListType m_ColorList; + + /** The line width */ + double m_LineWidth; + + /** The cross width for points */ + double m_CrossWidth; + + /** Default color : red*/ + ColorType m_RedColor; + +}; // end class +} // end namespace otb + +#endif + + + -- GitLab