Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:47

0001 // Created on: 2013-07-12
0002 // Created by: Anton POLETAEV
0003 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Graphic3d_ClipPlane_HeaderFile
0017 #define _Graphic3d_ClipPlane_HeaderFile
0018 
0019 #include <gp_Pln.hxx>
0020 #include <Graphic3d_AspectFillArea3d.hxx>
0021 #include <Graphic3d_BndBox3d.hxx>
0022 #include <Graphic3d_CappingFlags.hxx>
0023 #include <Graphic3d_TextureMap.hxx>
0024 #include <NCollection_Vec4.hxx>
0025 #include <Standard_Macro.hxx>
0026 #include <Standard_TypeDef.hxx>
0027 #include <Standard_Transient.hxx>
0028 
0029 //! Clipping state.
0030 enum Graphic3d_ClipState
0031 {
0032   Graphic3d_ClipState_Out, //!< fully outside (clipped) - should be discarded
0033   Graphic3d_ClipState_In,  //!< fully inside  (NOT clipped) - should NOT be discarded
0034   Graphic3d_ClipState_On,  //!< on (not clipped / partially clipped) - should NOT be discarded
0035 };
0036 
0037 //! Container for properties describing either a Clipping halfspace (single Clipping Plane),
0038 //! or a chain of Clipping Planes defining logical AND (conjunction) operation.
0039 //! The plane equation is specified in "world" coordinate system.
0040 class Graphic3d_ClipPlane : public Standard_Transient
0041 {
0042   DEFINE_STANDARD_RTTIEXT(Graphic3d_ClipPlane,Standard_Transient)
0043 public:
0044 
0045   //! Type defining XYZW (ABCD) plane equation - left for compatibility with old code using Graphic3d_ClipPlane::Equation type.
0046   typedef Graphic3d_Vec4d Equation;
0047 
0048 public:
0049 
0050   //! Default constructor.
0051   //! Initializes clip plane container with the following properties:
0052   //! - Equation (0.0, 0.0, 1.0, 0)
0053   //! - IsOn (True),
0054   //! - IsCapping (False),
0055   //! - Material (Graphic3d_NameOfMaterial_DEFAULT),
0056   //! - Texture (NULL),
0057   //! - HatchStyle (Aspect_HS_HORIZONTAL),
0058   //! - IsHatchOn (False)
0059   Standard_EXPORT Graphic3d_ClipPlane();
0060 
0061   //! Copy constructor.
0062   //! @param theOther [in] the copied plane.
0063   Standard_EXPORT Graphic3d_ClipPlane(const Graphic3d_ClipPlane& theOther);
0064 
0065   //! Construct clip plane for the passed equation.
0066   //! By default the plane is on, capping is turned off.
0067   //! @param theEquation [in] the plane equation.
0068   Standard_EXPORT Graphic3d_ClipPlane (const Graphic3d_Vec4d& theEquation);
0069 
0070   //! Construct clip plane from the passed geometrical definition.
0071   //! By default the plane is on, capping is turned off.
0072   //! @param thePlane [in] the plane.
0073   Standard_EXPORT Graphic3d_ClipPlane (const gp_Pln& thePlane);
0074 
0075   //! Set plane equation by its geometrical definition.
0076   //! The equation is specified in "world" coordinate system.
0077   //! @param thePlane [in] the plane.
0078   Standard_EXPORT void SetEquation (const gp_Pln& thePlane);
0079 
0080   //! Set 4-component equation vector for clipping plane.
0081   //! The equation is specified in "world" coordinate system.
0082   //! @param theEquation [in] the XYZW (or "ABCD") equation vector.
0083   Standard_EXPORT void SetEquation (const Graphic3d_Vec4d& theEquation);
0084 
0085   //! Get 4-component equation vector for clipping plane.
0086   //! @return clipping plane equation vector.
0087   const Graphic3d_Vec4d& GetEquation() const { return myEquation; }
0088 
0089   //! Get 4-component equation vector for clipping plane.
0090   //! @return clipping plane equation vector.
0091   const Graphic3d_Vec4d& ReversedEquation() const { return myEquationRev; }
0092 
0093   //! Check that the clipping plane is turned on.
0094   //! @return boolean flag indicating whether the plane is in on or off state.
0095   Standard_Boolean IsOn() const
0096   {
0097     return myIsOn;
0098   }
0099 
0100   //! Change state of the clipping plane.
0101   //! @param theIsOn [in] the flag specifying whether the graphic driver
0102   //! clipping by this plane should be turned on or off.
0103   Standard_EXPORT void SetOn(const Standard_Boolean theIsOn);
0104 
0105   //! Change state of capping surface rendering.
0106   //! @param theIsOn [in] the flag specifying whether the graphic driver should
0107   //! perform rendering of capping surface produced by this plane. The graphic
0108   //! driver produces this surface for convex graphics by means of stencil-test
0109   //! and multi-pass rendering.
0110   Standard_EXPORT void SetCapping(const Standard_Boolean theIsOn);
0111 
0112   //! Check state of capping surface rendering.
0113   //! @return true (turned on) or false depending on the state.
0114   Standard_Boolean IsCapping() const
0115   {
0116     return myIsCapping;
0117   }
0118 
0119   //! Get geometrical definition.
0120   //! @return geometrical definition of clipping plane
0121   const gp_Pln& ToPlane() const { return myPlane; }
0122 
0123   //! Clone plane. Virtual method to simplify copying procedure if plane
0124   //! class is redefined at application level to add specific fields to it
0125   //! e.g. id, name, etc.
0126   //! @return new instance of clipping plane with same properties and attributes.
0127   Standard_EXPORT virtual Handle(Graphic3d_ClipPlane) Clone() const;
0128 
0129 public:
0130 
0131   //! Return TRUE if this item defines a conjunction (logical AND) between a set of Planes.
0132   //! Graphic3d_ClipPlane item defines either a Clipping halfspace (single Clipping Plane)
0133   //! or a Clipping volume defined by a logical AND (conjunction) operation between a set of Planes defined as a Chain
0134   //! (so that the volume cuts a space only in case if check fails for ALL Planes in the Chain).
0135   //!
0136   //! Note that Graphic3d_ClipPlane item cannot:
0137   //! - Define a Chain with logical OR (disjunction) operation;
0138   //!   this should be done through Graphic3d_SequenceOfHClipPlane.
0139   //! - Define nested Chains.
0140   //! - Disable Chain items; only entire Chain can be disabled (by disabled a head of Chain).
0141   //!
0142   //! The head of a Chain defines all visual properties of the Chain,
0143   //! so that Graphic3d_ClipPlane of next items in a Chain merely defines only geometrical definition of the plane.
0144   Standard_Boolean IsChain() const { return !myNextInChain.IsNull(); }
0145 
0146   //! Return the previous plane in a Chain of Planes defining logical AND operation,
0147   //! or NULL if there is no Chain or it is a first element in Chain.
0148   //! When clipping is defined by a Chain of Planes,
0149   //! it cuts a space only in case if check fails for all Planes in Chain.
0150   Handle(Graphic3d_ClipPlane) ChainPreviousPlane() const { return myPrevInChain; }
0151 
0152   //! Return the next plane in a Chain of Planes defining logical AND operation,
0153   //! or NULL if there is no chain or it is a last element in chain.
0154 
0155   const Handle(Graphic3d_ClipPlane)& ChainNextPlane() const { return myNextInChain; }
0156 
0157   //! Return the number of chains in forward direction (including this item, so it is always >= 1).
0158   //! For a head of Chain - returns the length of entire Chain.
0159   Standard_Integer NbChainNextPlanes() const { return myChainLenFwd; }
0160 
0161   //! Set the next plane in a Chain of Planes.
0162   //! This operation also updates relationship between chains (Previous/Next items),
0163   //! so that the previously set Next plane is cut off.
0164   Standard_EXPORT void SetChainNextPlane (const Handle(Graphic3d_ClipPlane)& thePlane);
0165 
0166 public: // @name user-defined graphical attributes
0167 
0168   //! Return color for rendering capping surface.
0169   Quantity_Color CappingColor() const { return myAspect->FrontMaterial().MaterialType() == Graphic3d_MATERIAL_ASPECT ? myAspect->FrontMaterial().Color() : myAspect->InteriorColor(); }
0170 
0171   //! Set color for rendering capping surface.
0172   Standard_EXPORT void SetCappingColor (const Quantity_Color& theColor);
0173 
0174   //! Set material for rendering capping surface.
0175   //! @param theMat [in] the material.
0176   Standard_EXPORT void SetCappingMaterial (const Graphic3d_MaterialAspect& theMat);
0177 
0178   //! @return capping material.
0179   const Graphic3d_MaterialAspect& CappingMaterial() const { return myAspect->FrontMaterial(); }
0180 
0181   //! Set texture to be applied on capping surface.
0182   //! @param theTexture [in] the texture.
0183   Standard_EXPORT void SetCappingTexture (const Handle(Graphic3d_TextureMap)& theTexture);
0184 
0185   //! @return capping texture map.
0186   Handle(Graphic3d_TextureMap) CappingTexture() const { return !myAspect->TextureSet().IsNull() && !myAspect->TextureSet()->IsEmpty()
0187                                                               ? myAspect->TextureSet()->First()
0188                                                               : Handle(Graphic3d_TextureMap)(); }
0189 
0190   //! Set hatch style (stipple) and turn hatching on.
0191   //! @param theStyle [in] the hatch style.
0192   Standard_EXPORT void SetCappingHatch (const Aspect_HatchStyle theStyle);
0193 
0194   //! @return hatching style.
0195   Aspect_HatchStyle CappingHatch() const { return (Aspect_HatchStyle)myAspect->HatchStyle()->HatchType(); }
0196 
0197   //! Set custom hatch style (stipple) and turn hatching on.
0198   //! @param theStyle [in] the hatch pattern.
0199   Standard_EXPORT void SetCappingCustomHatch (const Handle(Graphic3d_HatchStyle)& theStyle);
0200 
0201   //! @return hatching style.
0202   const Handle(Graphic3d_HatchStyle)& CappingCustomHatch() const { return myAspect->HatchStyle(); }
0203 
0204   //! Turn on hatching.
0205   Standard_EXPORT void SetCappingHatchOn();
0206 
0207   //! Turn off hatching.
0208   Standard_EXPORT void SetCappingHatchOff();
0209 
0210   //! @return True if hatching mask is turned on.
0211   Standard_Boolean IsHatchOn() const { return myAspect->InteriorStyle() == Aspect_IS_HATCH; }
0212 
0213   //! This ID is used for managing associated resources in graphical driver.
0214   //! The clip plane can be assigned within a range of IO which can be
0215   //! displayed in separate OpenGl contexts. For each of the context an associated
0216   //! OpenGl resource for graphical aspects should be created and kept.
0217   //! The resources are stored in graphical driver for each of individual groups
0218   //! of shared context under the clip plane identifier.
0219   //! @return clip plane resource identifier string.
0220   const TCollection_AsciiString& GetId() const
0221   {
0222     return myId;
0223   }
0224 
0225 public:
0226 
0227   //! Return capping aspect.
0228   //! @return capping surface rendering aspect.
0229   const Handle(Graphic3d_AspectFillArea3d)& CappingAspect() const { return myAspect; }
0230 
0231   //! Assign capping aspect.
0232   Standard_EXPORT void SetCappingAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect);
0233 
0234   //! Flag indicating whether material for capping plane should be taken from object.
0235   //! Default value: FALSE (use dedicated capping plane material).
0236   bool ToUseObjectMaterial() const { return (myFlags & Graphic3d_CappingFlags_ObjectMaterial) != 0; }
0237 
0238   //! Set flag for controlling the source of capping plane material.
0239   void SetUseObjectMaterial (bool theToUse) { setCappingFlag (theToUse, Graphic3d_CappingFlags_ObjectMaterial); }
0240 
0241   //! Flag indicating whether texture for capping plane should be taken from object.
0242   //! Default value: FALSE.
0243   bool ToUseObjectTexture() const { return (myFlags & Graphic3d_CappingFlags_ObjectTexture) != 0; }
0244 
0245   //! Set flag for controlling the source of capping plane texture.
0246   void SetUseObjectTexture (bool theToUse) { setCappingFlag (theToUse, Graphic3d_CappingFlags_ObjectTexture); }
0247 
0248   //! Flag indicating whether shader program for capping plane should be taken from object.
0249   //! Default value: FALSE.
0250   bool ToUseObjectShader() const { return (myFlags & Graphic3d_CappingFlags_ObjectShader) != 0; }
0251 
0252   //! Set flag for controlling the source of capping plane shader program.
0253   void SetUseObjectShader(bool theToUse) { setCappingFlag (theToUse, Graphic3d_CappingFlags_ObjectShader); }
0254 
0255   //! Return true if some fill area aspect properties should be taken from object.
0256   bool ToUseObjectProperties() const { return myFlags != Graphic3d_CappingFlags_None; }
0257 
0258 public:
0259 
0260   //! Check if the given point is outside / inside / on section.
0261   Graphic3d_ClipState ProbePoint (const Graphic3d_Vec4d& thePoint) const
0262   {
0263     Graphic3d_ClipState aState = Graphic3d_ClipState_Out;
0264     for (const Graphic3d_ClipPlane* aPlaneIter = this; aPlaneIter != NULL; aPlaneIter = aPlaneIter->myNextInChain.get())
0265     {
0266       Graphic3d_ClipState aPlnState = aPlaneIter->ProbePointHalfspace (thePoint);
0267       if (aPlnState == Graphic3d_ClipState_In)
0268       {
0269         return Graphic3d_ClipState_In;
0270       }
0271       else if (aPlnState != Graphic3d_ClipState_Out)
0272       {
0273         aState = Graphic3d_ClipState_On;
0274       }
0275     }
0276     return aState;
0277   }
0278 
0279   //! Check if the given bounding box is fully outside / fully inside.
0280   Graphic3d_ClipState ProbeBox (const Graphic3d_BndBox3d& theBox) const
0281   {
0282     Graphic3d_ClipState aState = Graphic3d_ClipState_Out;
0283     for (const Graphic3d_ClipPlane* aPlaneIter = this; aPlaneIter != NULL; aPlaneIter = aPlaneIter->myNextInChain.get())
0284     {
0285       if (aPlaneIter->IsBoxFullInHalfspace (theBox))
0286       {
0287         // within union operation, if box is entirely inside at least one half-space, others can be ignored
0288         return Graphic3d_ClipState_In;
0289       }
0290       else if (!aPlaneIter->IsBoxFullOutHalfspace (theBox))
0291       {
0292         // if at least one full out test fail, clipping state is inconclusive (partially clipped)
0293         aState = Graphic3d_ClipState_On;
0294       }
0295     }
0296     return aState;
0297   }
0298 
0299   //! Check if the given bounding box is In and touch the clipping planes
0300   Standard_Boolean ProbeBoxTouch (const Graphic3d_BndBox3d& theBox) const
0301   {
0302     for (const Graphic3d_ClipPlane* aPlaneIter = this; aPlaneIter != NULL; aPlaneIter = aPlaneIter->myNextInChain.get())
0303     {
0304       if (aPlaneIter->IsBoxFullInHalfspace (theBox))
0305       {
0306         // within union operation, if box is entirely inside at least one half-space, others can be ignored
0307         return Standard_False;
0308       }
0309       else if (!aPlaneIter->IsBoxFullOutHalfspace (theBox))
0310       {
0311         // the box is not fully out, and not fully in, check is it on (but not intersect)
0312         if (ProbeBoxMaxPointHalfspace (theBox) != Graphic3d_ClipState_Out)
0313         {
0314           return Standard_True;
0315         }
0316       }
0317     }
0318     return Standard_False;
0319   }
0320 
0321 public:
0322 
0323   //! Check if the given point is outside of the half-space (e.g. should be discarded by clipping plane).
0324   Graphic3d_ClipState ProbePointHalfspace (const Graphic3d_Vec4d& thePoint) const
0325   {
0326     const Standard_Real aVal = myEquation.Dot (thePoint);
0327     return aVal < 0.0
0328          ? Graphic3d_ClipState_Out
0329          : (aVal == 0.0
0330           ? Graphic3d_ClipState_On
0331           : Graphic3d_ClipState_In);
0332   }
0333 
0334   //! Check if the given bounding box is fully outside / fully inside the half-space.
0335   Graphic3d_ClipState ProbeBoxHalfspace (const Graphic3d_BndBox3d& theBox) const
0336   {
0337     if (IsBoxFullOutHalfspace (theBox))
0338     {
0339       return Graphic3d_ClipState_Out;
0340     }
0341     return IsBoxFullInHalfspace (theBox)
0342          ? Graphic3d_ClipState_In
0343          : Graphic3d_ClipState_On;
0344   }
0345 
0346   //! Check if the given point is outside of the half-space (e.g. should be discarded by clipping plane).
0347   bool IsPointOutHalfspace (const Graphic3d_Vec4d& thePoint) const { return ProbePointHalfspace (thePoint) == Graphic3d_ClipState_Out; }
0348 
0349   //! Check if the given bounding box is fully outside of the half-space (e.g. should be discarded by clipping plane).
0350   bool IsBoxFullOutHalfspace (const Graphic3d_BndBox3d& theBox) const
0351   {
0352     const Graphic3d_Vec4d aMaxPnt (myEquation.x() > 0.0 ? theBox.CornerMax().x() : theBox.CornerMin().x(),
0353                                    myEquation.y() > 0.0 ? theBox.CornerMax().y() : theBox.CornerMin().y(),
0354                                    myEquation.z() > 0.0 ? theBox.CornerMax().z() : theBox.CornerMin().z(),
0355                                    1.0);
0356     return IsPointOutHalfspace (aMaxPnt);
0357   }
0358 
0359   //! Check if the given bounding box is fully outside of the half-space (e.g. should be discarded by clipping plane).
0360   Graphic3d_ClipState ProbeBoxMaxPointHalfspace (const Graphic3d_BndBox3d& theBox) const
0361   {
0362     const Graphic3d_Vec4d aMaxPnt (myEquation.x() > 0.0 ? theBox.CornerMax().x() : theBox.CornerMin().x(),
0363                                    myEquation.y() > 0.0 ? theBox.CornerMax().y() : theBox.CornerMin().y(),
0364                                    myEquation.z() > 0.0 ? theBox.CornerMax().z() : theBox.CornerMin().z(),
0365                                    1.0);
0366     return ProbePointHalfspace (aMaxPnt);
0367   }
0368 
0369   //! Check if the given bounding box is fully inside (or touches from inside) the half-space (e.g. NOT discarded by clipping plane).
0370   bool IsBoxFullInHalfspace (const Graphic3d_BndBox3d& theBox) const
0371   {
0372     const Graphic3d_Vec4d aMinPnt (myEquation.x() > 0.0 ? theBox.CornerMin().x() : theBox.CornerMax().x(),
0373                                    myEquation.y() > 0.0 ? theBox.CornerMin().y() : theBox.CornerMax().y(),
0374                                    myEquation.z() > 0.0 ? theBox.CornerMin().z() : theBox.CornerMax().z(),
0375                                    1.0);
0376     return !IsPointOutHalfspace (aMinPnt);
0377   }
0378 
0379   //! Dumps the content of me into the stream
0380   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0381 
0382 public: // @name modification counters
0383 
0384   //! @return modification counter for equation.
0385   unsigned int MCountEquation() const
0386   {
0387     return myEquationMod;
0388   }
0389 
0390   //! @return modification counter for aspect.
0391   unsigned int MCountAspect() const
0392   {
0393     return myAspectMod;
0394   }
0395 
0396 private:
0397 
0398   //! Generate unique object id for OpenGL graphic resource manager.
0399   void makeId();
0400 
0401   //! Set capping flag.
0402   Standard_EXPORT void setCappingFlag (bool theToUse, int theFlag);
0403 
0404   //! Update chain length in backward direction.
0405   void updateChainLen();
0406 
0407   //! Update inversed plane definition from main plane.
0408   void updateInversedPlane()
0409   {
0410     gp_Pln aPlane = myPlane;
0411     aPlane.SetAxis (aPlane.Axis().Reversed());
0412     aPlane.Coefficients (myEquationRev[0], myEquationRev[1], myEquationRev[2], myEquationRev[3]);
0413   }
0414 
0415 private:
0416 
0417   Handle(Graphic3d_AspectFillArea3d) myAspect;    //!< fill area aspect
0418   Handle(Graphic3d_ClipPlane)   myNextInChain;    //!< next     plane in a chain of planes defining logical AND operation
0419   Graphic3d_ClipPlane*          myPrevInChain;    //!< previous plane in a chain of planes defining logical AND operation
0420   TCollection_AsciiString myId;                   //!< resource id
0421   gp_Pln                  myPlane;                //!< plane definition
0422   Graphic3d_Vec4d         myEquation;             //!< plane equation vector
0423   Graphic3d_Vec4d         myEquationRev;          //!< reversed plane equation
0424   Standard_Integer        myChainLenFwd;          //!< chain length in forward direction (including this item)
0425   unsigned int            myFlags;                //!< capping flags
0426   unsigned int            myEquationMod;          //!< modification counter for equation
0427   unsigned int            myAspectMod;            //!< modification counter of aspect
0428   Standard_Boolean        myIsOn;                 //!< state of the clipping plane
0429   Standard_Boolean        myIsCapping;            //!< state of graphic driver capping
0430 
0431 };
0432 
0433 DEFINE_STANDARD_HANDLE (Graphic3d_ClipPlane, Standard_Transient)
0434 
0435 #endif