glpane.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:15k
- /*
- * ===========================================================================
- * PRODUCTION $Log: glpane.hpp,v $
- * PRODUCTION Revision 1000.4 2004/06/01 19:50:09 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.11
- * PRODUCTION
- * ===========================================================================
- */
- #ifndef GUI_GRAPH___GLPANE__HPP
- #define GUI_GRAPH___GLPANE__HPP
- /* $Id: glpane.hpp,v 1000.4 2004/06/01 19:50:09 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- * File Description:
- *
- */
- #include <corelib/ncbistl.hpp>
- #include <corelib/ncbidiag.hpp>
- #include <gui/opengl.h>
- #include <gui/opengl/glcolor.hpp>
- #include <gui/opengl/glrect.hpp>
- /** @addtogroup GUI_OPENGL
- *
- * @{
- */
- BEGIN_NCBI_SCOPE
- // units for coordinates representation on the output device (pixels) and in the model space
- typedef int TVPUnit;
- typedef GLdouble TModelUnit;
- typedef CGlPoint<TVPUnit> TVPPoint;
- typedef CGlPoint<TModelUnit> TModelPoint;
- typedef CGlRect<TVPUnit> TVPRect;
- typedef CGlRect<TModelUnit> TModelRect;
- enum EScaleType {
- eDec,
- eLog10,
- eLog2,
- eLn
- };
- enum EOrientation {
- eHorz,
- eVert
- };
- ////////////////////////////////////////////////////////////////////////////////
- /// class CGlPane
- // CGlPane represents a rectangular area used for drawing graphs and decorations
- // (such as grid, axes, labels etc.) CGlPane provides means for using OpenGL
- // projections in order to draw graphs in coordinates of their model space.
- class NCBI_GUIOPENGL_EXPORT CGlPane
- {
- public:
- enum EProjectionMode {
- eNone = -1, // closed or invalid
- eOrtho, // orthographic projection, drawing in model space coords
- ePixels // orthographic projection, drawing in pixels
- };
- /// Specifies what point should be preserved during various coordinate
- /// space adjustments.
- enum EOriginType {
- eOriginLeft,
- eOriginRight,
- eOriginBottom,
- eOriginTop,
- eOriginCenter
- };
- /// EProjMatrixPolicy controls CGlPane behavior in respect to obtaining
- /// projection matrices for future use in Project..(), UnProject...()
- /// functions.
- enum EProjMatrixPolicy
- {
- eNeverUpdate,
- eAlwaysUpdate
- };
- /// EAdjustmentPolicy specifies how visible area should be adjusted if
- /// some of the prjection parameters change
- enum EAdjustmentPolicy
- {
- fShiftToLimits = 0x1,
- fScaleToLimits = 0x2,
- fAdjustAll = fShiftToLimits | fScaleToLimits
- };
- /// EZoomOptions flags control behavior of Zoom operations.
- enum EZoomOptions
- {
- fZoomX = 0x1, // zoom by X coordinate
- fZoomY = 0x2, // zoom by Y coordinate
- fForce = 0x4, // overrides enable/disable settings in the pane
- fZoomXY = fZoomX | fZoomY
- };
- public:
- CGlPane(EProjMatrixPolicy policy = eAlwaysUpdate);
- virtual ~CGlPane();
- // Viewport manipulation
- void SetViewport(const TVPRect& R);
- TVPRect& GetViewport(void);
- const TVPRect& GetViewport(void) const;
- // Model Space manipulation
- void SetModelLimitsRect(const TModelRect& R);
- TModelRect& GetModelLimitsRect(void);
- const TModelRect& GetModelLimitsRect(void) const;
- // Ortho projection - visible ares
- void SetVisibleRect(const TModelRect& R);
- TModelRect& GetVisibleRect(void);
- const TModelRect& GetVisibleRect(void) const;
-
- /// set clipping rect that will be used by glScissor, NULL for reset
- void SetClipRect(const TVPRect* rc_clip = NULL);
- /// Offset is used as a workaround for OpenGL precision problems emerging
- /// when size of visible range is small in comparision to coordinates in
- /// this range.
- void EnableOffset(bool b_en = true);
- TModelUnit GetOffsetX() const;
- TModelUnit GetOffsetY() const;
- TModelPoint GetOffset() const;
- bool Open(EProjectionMode Mode);
- bool OpenOrtho();
- bool OpenPixels();
- void Close(void);
- EProjectionMode GetProjMode(void) const;
- // scale Model space units per pixel
- TModelUnit GetScaleX(void) const;
- TModelUnit GetScaleY(void) const;
-
- void SetMinScaleX(TModelUnit scale);
- void SetMinScaleY(TModelUnit scale);
- TModelUnit GetMinScaleX(void) const;
- TModelUnit GetMinScaleY(void) const;
-
- TModelUnit GetZoomAllScaleX(void) const;
- TModelUnit GetZoomAllScaleY(void) const;
- // in Proportional mode CGlPane will keep horz. and vert. scales equal
- void SetProportionalMode(bool set);
- bool IsProportionalMode(void) const;
- void SetOriginType(EOriginType type_x, EOriginType type_y);
- void SetAdjustmentPolicy(int adjust_x, int adjust_y);
- void SetAdjustToLimits(bool adjust_x, bool adjust_y);
- // Zoom functions
- void EnableZoom(bool en_x, bool en_y);
- bool IsZoomInAvaiable(void);
- bool IsZoomOutAvaiable(void);
- void SetZoomFactor(TModelUnit factor = 2.0);
- void ZoomAll(int options = fZoomXY);
-
- void ZoomInCenter(int options = fZoomXY);
- void ZoomIn(TModelUnit x, TModelUnit y, int options = fZoomXY);
- void ZoomIn(TModelPoint p, int options = fZoomXY);
-
- void ZoomOutCenter(int options = fZoomXY);
- void ZoomOut(TModelUnit x, TModelUnit y, int options = fZoomXY);
- void ZoomOut(TModelPoint p, int options = fZoomXY);
-
- void ZoomPoint(TModelUnit x, TModelUnit y, TModelUnit factor, int options = fZoomXY);
- void ZoomPoint(TModelPoint p, TModelUnit factor, int options = fZoomXY);
-
- void ZoomRect(const TModelRect& r);
- // Zooms so that p_center becomes a center of visible area, the size of
- // visible area is calculated based on given scales
- void SetScale(TModelUnit scale_x, TModelUnit scale_y, TModelPoint p_center);
- void SetScaleRefPoint(TModelUnit scale_x, TModelUnit scale_y,
- TModelPoint p_ref);
- // this function preserves current center point
- void SetScale(TModelUnit scale_x, TModelUnit scale_y);
- // Scroll functions
- bool NeedsScrollX(void) const;
- bool NeedsScrollY(void) const;
- void Scroll(TModelUnit dx, TModelUnit dy);
- void AdjustToLimits();
- // Coordinate transformations - now only for Ortho mode
- TVPUnit ProjectX(TModelUnit m_x) const;
- TVPUnit ProjectY(TModelUnit m_y) const;
- TVPPoint Project(TModelUnit m_x, TModelUnit m_y) const;
-
- TModelUnit UnProjectX(TVPUnit m_y) const;
- TModelUnit UnProjectY(TVPUnit m_y) const;
- TModelPoint UnProject(TVPUnit m_x, TVPUnit m_y) const;
- TModelUnit UnProjectWidth(TVPUnit vp_w);
- TModelUnit UnProjectHeight(TVPUnit vp_h);
- protected:
- bool x_OpenOrtho();
- bool x_OpenPixels();
- void x_Open_SetViewport();
-
- void x_UpdateProjectMatrices(void);
- // functions enforcing constraints in the display validity
- void x_CorrectScale(TModelPoint p_center);
- // shifts visible area so that it is located within limits
- void x_ShiftToFitLimits(void);
- void x_ScaleToFitLimits(void);
- void x_AdjustVisibleRect(void);
- static double s_GetShift(TModelUnit low_shift, TModelUnit high_shift, bool neg_range, EOriginType type);
- void x_AssertNotOpen(void) const;
- void x_AssertOrtho(void) const;
- protected:
- TVPRect m_rcVP;
- TModelRect m_rcLimits;
- TModelRect m_rcVisible;
- TModelRect m_rcOrigVisible; // used in ProportionalMode
- const TVPRect *m_prcClip;
- EProjectionMode m_Mode;
- EProjMatrixPolicy m_MatrixPolicy;
- int m_AdjustX;
- int m_AdjustY;
- bool m_bEnableOffset;
-
- bool m_bZoomEnX;
- bool m_bZoomEnY;
- TModelUnit m_ZoomFactor;
- bool m_bProportionalMode;
- EOriginType m_TypeX;
- EOriginType m_TypeY;
-
- TModelUnit m_MinScaleX;
- TModelUnit m_MinScaleY;
- // matrices for using with gluProject gluUnProject
- GLint m_mxVP[4];
- GLdouble m_mxProjection[16];
- GLdouble m_mxModelView[16];
- };
- class CGlPaneGuard
- {
- public:
- CGlPaneGuard(CGlPane& pane, CGlPane::EProjectionMode mode)
- : m_Pane(pane)
- {
- m_Pane.Open(mode);
- }
- ~CGlPaneGuard()
- {
- m_Pane.Close();
- }
- private:
- CGlPane& m_Pane;
- // forbidden
- CGlPaneGuard(const CGlPaneGuard&);
- CGlPaneGuard& operator=(const CGlPaneGuard&);
- };
- inline TVPRect& CGlPane::GetViewport(void)
- {
- return m_rcVP;
- }
- inline const TVPRect& CGlPane::GetViewport(void) const
- {
- return m_rcVP;
- }
- inline void CGlPane::SetModelLimitsRect(const TModelRect& R)
- {
- m_rcLimits = m_rcOrigVisible = R;
- }
- inline TModelRect& CGlPane::GetModelLimitsRect(void)
- {
- return m_rcLimits;
- }
- inline const TModelRect& CGlPane::GetModelLimitsRect(void) const
- {
- return m_rcLimits;
- }
- inline TModelRect& CGlPane::GetVisibleRect(void)
- {
- return m_rcVisible;
- }
- inline const TModelRect& CGlPane::GetVisibleRect(void) const
- {
- return m_rcVisible;
- }
- inline void CGlPane::SetProportionalMode(bool set)
- {
- m_bProportionalMode = set;
- }
- inline bool CGlPane::IsProportionalMode(void) const
- {
- return m_bProportionalMode;
- }
- inline void CGlPane::SetOriginType(EOriginType type_x, EOriginType type_y)
- {
- m_TypeX = type_x;
- m_TypeY = type_y;
- }
- inline void CGlPane::SetAdjustmentPolicy(int adjust_x, int adjust_y)
- {
- m_AdjustX = adjust_x;
- m_AdjustY = adjust_y;
- }
- inline void CGlPane::SetAdjustToLimits(bool adjust_x, bool adjust_y)
- {
- m_AdjustX = adjust_x ? fAdjustAll : 0;
- m_AdjustY = adjust_y ? fAdjustAll : 0;
- }
- inline void CGlPane::SetClipRect(const TVPRect* rc_clip)
- {
- m_prcClip = rc_clip;
- }
- inline void CGlPane::EnableOffset(bool b_en)
- {
- m_bEnableOffset = b_en;
- }
- inline TModelUnit CGlPane::GetOffsetX() const
- {
- return m_bEnableOffset ? m_rcVisible.Left() : 0;
- }
- inline TModelUnit CGlPane::GetOffsetY() const
- {
- return m_bEnableOffset ? m_rcVisible.Bottom() : 0;
- }
- inline TModelPoint CGlPane::GetOffset() const
- {
- if(m_bEnableOffset)
- return TModelPoint(m_rcVisible.Left(), m_rcVisible.Bottom());
- else return TModelPoint(0, 0);
- }
- inline bool CGlPane::OpenOrtho()
- {
- return Open(eOrtho);
- }
- inline bool CGlPane::OpenPixels()
- {
- return Open(ePixels);
- }
- inline CGlPane::EProjectionMode CGlPane::GetProjMode(void) const
- {
- return m_Mode;
- }
- inline void CGlPane::SetMinScaleX(TModelUnit scale)
- {
- m_MinScaleX = scale;
- }
- inline void CGlPane::SetMinScaleY(TModelUnit scale)
- {
- m_MinScaleY = scale;
- }
- inline TModelUnit CGlPane::GetMinScaleX(void) const
- {
- return m_MinScaleX;
- }
- inline TModelUnit CGlPane::GetMinScaleY(void) const
- {
- return m_MinScaleY;
- }
-
- inline void CGlPane::EnableZoom(bool en_x, bool en_y)
- {
- m_bZoomEnX = en_x;
- m_bZoomEnY = en_y;
- }
- inline void CGlPane::SetZoomFactor(TModelUnit factor)
- {
- _ASSERT(factor > 0);
- m_ZoomFactor = factor;
- }
- inline void CGlPane::ZoomIn(TModelPoint p, int options)
- {
- ZoomPoint(p.X(), p.Y(), m_ZoomFactor, options);
- }
- inline void CGlPane::ZoomIn(TModelUnit x, TModelUnit y, int options)
- {
- ZoomPoint(x, y, m_ZoomFactor);
- }
- inline void CGlPane::ZoomOut(TModelPoint p, int options)
- {
- ZoomPoint(p.X(), p.Y(), 1 / m_ZoomFactor, options);
- }
- inline void CGlPane::ZoomOut(TModelUnit x, TModelUnit y, int options)
- {
- ZoomPoint(x, y, 1 / m_ZoomFactor, options);
- }
- inline void CGlPane::ZoomPoint(TModelPoint p, TModelUnit factor, int options)
- {
- ZoomPoint(p.X(), p.Y(), factor, options);
- }
- inline void CGlPane::AdjustToLimits(void)
- {
- x_ShiftToFitLimits();
- }
- END_NCBI_SCOPE
- /* @} */
- /*
- * ===========================================================================
- * $Log: glpane.hpp,v $
- * Revision 1000.4 2004/06/01 19:50:09 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.11
- *
- * Revision 1.11 2004/05/11 18:54:22 dicuccio
- * Added doxygne modules info
- *
- * Revision 1.10 2004/04/20 14:08:37 rsmith
- * trailing commas at the ends of lists are non-standard and give warnings on some platforms.
- *
- * Revision 1.9 2004/04/07 12:38:26 dicuccio
- * Added CGlPaneGuard
- *
- * Revision 1.8 2003/12/08 15:07:36 yazhuk
- * Workaround for OpenGL rounding/precision problems
- *
- * Revision 1.7 2003/12/01 16:30:45 yazhuk
- * Added EOrientation enumeration.
- *
- * Revision 1.6 2003/11/17 20:27:57 yazhuk
- * Introduced zoom options - added EZoomOptions and modified zoom functions.
- *
- * Revision 1.5 2003/10/29 22:23:15 yazhuk
- * Added clipping capabilities
- *
- * Revision 1.4 2003/10/22 18:25:59 lebedev
- * SetScale method with reference point added
- *
- * Revision 1.3 2003/10/20 15:43:14 yazhuk
- * Replaced boolean flags with EAdjustmentPolicy enumeration.
- *
- * Revision 1.2 2003/10/15 21:12:45 yazhuk
- * Introduced projection matrix updating policy
- *
- * Revision 1.1 2003/10/08 12:53:02 dicuccio
- * Moved from gui/graph library
- *
- * Revision 1.10 2003/09/30 16:55:26 yazhuk
- * Added overloaded version of SetScale()
- *
- * Revision 1.9 2003/09/25 20:35:12 yazhuk
- * Added support for Visible rect adjustment
- *
- * Revision 1.8 2003/09/24 19:52:11 yazhuk
- * Enforced coding policy. Added eOriginCenter. Added "force" argument to
- * ZoomAll()
- *
- * Revision 1.7 2003/09/14 16:03:50 dicuccio
- * Restored export specifier
- *
- * Revision 1.6 2003/09/10 21:51:24 yazhuk
- * GCC compilation fixes
- *
- * Revision 1.5 2003/09/10 20:33:24 yazhuk
- * Added GetScale() functions
- *
- * Revision 1.4 2003/09/04 13:58:42 dicuccio
- * Added export specifiers. Inlined destructors of interface classes
- *
- * Revision 1.3 2003/08/28 19:19:15 yazhuk
- * Implemented specification of the port origin and autoadjustment of visible
- * rect
- *
- * Revision 1.2 2003/08/10 14:11:18 dicuccio
- * Compilation fixes for gcc
- *
- * Revision 1.1 2003/08/08 16:01:35 yazhuk
- * Initial revision.
- *
- * ===========================================================================
- */
- #endif