Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:21:17

0001 // Copyright (c) 2026 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _OpenGl_ShaderGrid_HeaderFile
0015 #define _OpenGl_ShaderGrid_HeaderFile
0016 
0017 #include <Aspect_GridParams.hxx>
0018 #include <Bnd_Box.hxx>
0019 #include <Graphic3d_Camera.hxx>
0020 #include <Graphic3d_Vertex.hxx>
0021 #include <NCollection_Mat4.hxx>
0022 #include <NCollection_Vec2.hxx>
0023 #include <OpenGl_Context.hxx>
0024 #include <OpenGl_ShaderProgram.hxx>
0025 #include <gp_Ax3.hxx>
0026 
0027 //! State and geometry model of the OpenGl shader-rendered grid.
0028 class OpenGl_ShaderGrid
0029 {
0030 public:
0031   //! Return TRUE if the grid is currently shown.
0032   bool IsShown() const { return myIsShown; }
0033 
0034   //! Return TRUE if the grid is rendered as a background.
0035   bool IsBackground() const { return myParams.IsBackground(); }
0036 
0037   //! Return current parameters.
0038   const Aspect_GridParams& Params() const { return myParams; }
0039 
0040   //! Store grid state. Returns FALSE when parameters cannot produce a shader grid.
0041   bool Display(const Aspect_GridParams&             theParams,
0042                const gp_Ax3&                        thePlane,
0043                const occ::handle<Graphic3d_Camera>& theCamera,
0044                const occ::handle<OpenGl_Context>&   theContext);
0045 
0046   //! Clear grid state.
0047   void Erase();
0048 
0049   //! Add helper bounds required by camera Z fitting.
0050   void AddZFitBounds(Bnd_Box& theGraphicBox, const occ::handle<Graphic3d_Camera>& theCamera) const;
0051 
0052   //! Return snapped point for the grid under the window pixel.
0053   bool Echo(const occ::handle<Graphic3d_Camera>& theCamera,
0054             const int                            theWidth,
0055             const int                            theHeight,
0056             const int                            theX,
0057             const int                            theY,
0058             Graphic3d_Vertex&                    thePoint,
0059             Graphic3d_Vertex&                    theDisplayPoint) const;
0060 
0061   //! Return snapped point for an arbitrary world point.
0062   bool SnapPoint(const occ::handle<Graphic3d_Camera>& theCamera,
0063                  const Graphic3d_Vertex&              thePoint,
0064                  Graphic3d_Vertex&                    theGridPoint) const;
0065 
0066   //! Upload shader uniforms for current grid state.
0067   void SetUniforms(const occ::handle<OpenGl_Context>&       theContext,
0068                    const occ::handle<OpenGl_ShaderProgram>& theProgram,
0069                    const occ::handle<Graphic3d_Camera>&     theCamera,
0070                    const NCollection_Mat4<float>&           theWorldView) const;
0071 
0072   //! Return worldview matrix to use for drawing.
0073   NCollection_Mat4<float> DrawWorldView(const NCollection_Mat4<float>& theCurrentWorldView) const;
0074 
0075 private:
0076   //! Compute grid plane frame.
0077   void frame(gp_Pnt& theOrigin, gp_XYZ& theX, gp_XYZ& theY, gp_XYZ& theN) const;
0078 
0079   //! Compute effective scales for the current camera.
0080   void effectiveScale(const occ::handle<Graphic3d_Camera>& theCamera,
0081                       double&                              theScaleX,
0082                       double&                              theScaleY) const;
0083 
0084   //! Return TRUE if local coordinates are inside configured grid domain.
0085   bool isPointInBounds(const double theLocalX, const double theLocalY) const;
0086 
0087   //! Add local point to box.
0088   static void addLocalPoint(Bnd_Box&      theBox,
0089                             const gp_Pnt& theOrigin,
0090                             const gp_XYZ& theX,
0091                             const gp_XYZ& theY,
0092                             const double  theLocalX,
0093                             const double  theLocalY);
0094 
0095   //! Add configured finite bounds to the box.
0096   void addFiniteBounds(Bnd_Box& theBox) const;
0097 
0098   //! Add view footprint bounds to the box.
0099   void addViewFootprintBounds(Bnd_Box&                             theBox,
0100                               const occ::handle<Graphic3d_Camera>& theCamera) const;
0101 
0102   //! Intersect camera ray at NDC point with the grid plane.
0103   bool planeLocalHit(const occ::handle<Graphic3d_Camera>& theCamera,
0104                      const double                         theNdcX,
0105                      const double                         theNdcY,
0106                      double&                              theLocalX,
0107                      double&                              theLocalY,
0108                      gp_XYZ*                              theHit            = nullptr,
0109                      const bool                           theToRejectBehind = true) const;
0110 
0111   //! Return stable visible reference point in local coordinates.
0112   bool referenceLocal(const occ::handle<Graphic3d_Camera>& theCamera,
0113                       double&                              theLocalX,
0114                       double&                              theLocalY) const;
0115 
0116   //! Return local shift snapped to a grid phase.
0117   static double snappedLocalShift(const double theLocal, const double theScale);
0118 
0119   //! Return TRUE if previous and new grid definitions share the same background anchor frame.
0120   bool hasSameAnchorFrame(const Aspect_GridParams& theParams, const gp_Ax3& thePlane) const;
0121 
0122   //! Accept echo candidate if it is visible and closer to the requested pixel.
0123   bool acceptEchoCandidate(const occ::handle<Graphic3d_Camera>& theCamera,
0124                            const int                            theWidth,
0125                            const int                            theHeight,
0126                            const int                            theX,
0127                            const int                            theY,
0128                            const gp_Pnt&                        theGridOrigin,
0129                            const gp_XYZ&                        theGridX,
0130                            const gp_XYZ&                        theGridY,
0131                            const double                         theLocalX,
0132                            const double                         theLocalY,
0133                            gp_XYZ&                              theBestSnapped,
0134                            double&                              theBestDist2,
0135                            bool&                                theHasBestPoint) const;
0136 
0137   //! Convert point to current draw view coordinates.
0138   static NCollection_Vec3<float> viewPoint(const NCollection_Mat4<float>& theWorldView,
0139                                            const gp_Pnt&                  thePoint);
0140 
0141   //! Convert direction to current draw view coordinates.
0142   static NCollection_Vec3<float> viewDirection(const NCollection_Mat4<float>& theWorldView,
0143                                                const gp_XYZ&                  theDirection);
0144 
0145   //! Return display-safe echo marker point.
0146   static bool echoDisplayPoint(const occ::handle<Graphic3d_Camera>& theCamera,
0147                                const gp_XYZ&                        theSnapped,
0148                                gp_Pnt&                              theDisplayPoint);
0149 
0150   //! Return TRUE if angle belongs to arc.
0151   static bool angleInArc(const double theStart, const double theEnd, const double theAngle);
0152 
0153 private:
0154   Aspect_GridParams       myParams;
0155   gp_Ax3                  myPlane;
0156   NCollection_Mat4<float> myRefViewMatrix;
0157   bool                    myIsShown = false;
0158 };
0159 
0160 #endif // _OpenGl_ShaderGrid_HeaderFile