Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:19:43

0001 // Created on: 2017-04-21
0002 // Created by: Alexander Bobkov
0003 // Copyright (c) 2017 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 _BRepTools_History_HeaderFile
0017 #define _BRepTools_History_HeaderFile
0018 
0019 #include <NCollection_Handle.hxx>
0020 #include <TopExp.hxx>
0021 #include <TopoDS_Shape.hxx>
0022 #include <NCollection_List.hxx>
0023 #include <TopTools_ShapeMapHasher.hxx>
0024 #include <NCollection_DataMap.hxx>
0025 #include <NCollection_Map.hxx>
0026 
0027 //! The history keeps the following relations between the input shapes
0028 //! (S1, ..., Sm) and output shapes (T1, ..., Tn):
0029 //! 1) an output shape Tj is generated from an input shape Si: Tj <= G(Si);
0030 //! 2) a output shape Tj is modified from an input shape Si: Tj <= M(Si);
0031 //! 3) an input shape (Si) is removed: R(Si) == 1.
0032 //!
0033 //! The relations are kept only for shapes of types vertex, edge, face, and
0034 //! solid.
0035 //!
0036 //! The last relation means that:
0037 //! 1) shape Si is not an output shape and
0038 //! 2) no any shape is modified (produced) from shape Si:
0039 //! R(Si) == 1 ==> Si != Tj, M(Si) == 0.
0040 //!
0041 //! It means that the input shape cannot be removed and modified
0042 //! simultaneously. However, the shapes may be generated from the
0043 //! removed shape. For instance, in Fillet operation the edges
0044 //! generate faces and then are removed.
0045 //!
0046 //! No any shape could be generated and modified from the same shape
0047 //! simultaneously: sets G(Si) and M(Si) are not intersected
0048 //! (G(Si) ^ M(Si) == 0).
0049 //!
0050 //! Each output shape should be:
0051 //! 1) an input shape or
0052 //! 2) generated or modified from an input shape (even generated from the
0053 //!   implicit null shape if necessary):
0054 //!   Tj == Si V (exists Si that Tj <= G(Si) U M(Si)).
0055 //!
0056 //! Recommendations to choose between relations 'generated' and 'modified':
0057 //! 1) a shape is generated from input shapes if it dimension is greater or
0058 //!   smaller than the dimensions of the input shapes;
0059 //! 2) a shape is generated from input shapes if these shapes are also output
0060 //!   shapes;
0061 //! 3) a shape is generated from input shapes of the same dimension if it is
0062 //!   produced by joining shapes generated from these shapes;
0063 //! 4) a shape is modified from an input shape if it replaces the input shape by
0064 //!   changes of the location, the tolerance, the bounds of the parametric
0065 //!   space (the faces for a solid), the parametrization and/or by applying of
0066 //!   an approximation;
0067 //! 5) a shape is modified from input shapes of the same dimension if it is
0068 //!   produced by joining shapes modified from these shapes.
0069 //!
0070 //! Two sequential histories:
0071 //! - one history (H12) of shapes S1, ..., Sm to shapes T1, ..., Tn and
0072 //! - another history (H23) of shapes T1, ..., Tn to shapes Q1, ..., Ql
0073 //! could be merged to the single history (H13) of shapes S1, ..., Sm to shapes
0074 //! Q1, ..., Ql.
0075 //!
0076 //! During the merge:
0077 //! 1) if shape Tj is generated from shape Si then each shape generated or
0078 //!   modified from shape Tj is considered as a shape generated from shape Si
0079 //!   among shapes Q1, ..., Ql:
0080 //!   Tj <= G12(Si), Qk <= G23(Tj) U M23(Tj) ==> Qk <= G13(Si).
0081 //! 2) if shape Tj is modified from shape Si, shape Qk is generated from shape
0082 //!   Tj then shape Qk is considered as a shape generated from shape Si among
0083 //!   shapes Q1, ..., Ql:
0084 //!   Tj <= M12(Si), Qk <= G23(Tj) ==> Qk <= G13(Si);
0085 //! 3) if shape Tj is modified from shape Si, shape Qk is modified from shape
0086 //!   Tj then shape Qk is considered as a shape modified from shape Si among
0087 //!   shapes Q1, ..., Ql:
0088 //!   Tj <= M12(Si), Qk <= M23(Tj) ==> Qk <= M13(Si);
0089 class BRepTools_History : public Standard_Transient
0090 {
0091 public: //! @name Constructors for History creation
0092   //! Empty constructor
0093   BRepTools_History() = default;
0094 
0095   //! Template constructor for History creation from the algorithm having
0096   //! standard history methods such as IsDeleted(), Modified() and Generated().
0097   //! @param[in] theArguments  Arguments of the algorithm;
0098   //! @param[in] theAlgo  The algorithm.
0099   template <class TheAlgo>
0100   BRepTools_History(const NCollection_List<TopoDS_Shape>& theArguments, TheAlgo& theAlgo)
0101   {
0102     // Map all argument shapes to save them in history
0103     NCollection_IndexedMap<TopoDS_Shape, TopTools_ShapeMapHasher> anArgsMap;
0104     NCollection_List<TopoDS_Shape>::Iterator                      aIt(theArguments);
0105     for (; aIt.More(); aIt.Next())
0106     {
0107       if (!aIt.Value().IsNull())
0108         TopExp::MapShapes(aIt.Value(), anArgsMap);
0109     }
0110 
0111     // Copy the history for all supported shapes from the algorithm
0112     int i, aNb = anArgsMap.Extent();
0113     for (i = 1; i <= aNb; ++i)
0114     {
0115       const TopoDS_Shape& aS = anArgsMap(i);
0116       if (!IsSupportedType(aS))
0117         continue;
0118 
0119       if (theAlgo.IsDeleted(aS))
0120         Remove(aS);
0121 
0122       // Check Modified
0123       const NCollection_List<TopoDS_Shape>& aModified = theAlgo.Modified(aS);
0124       for (aIt.Initialize(aModified); aIt.More(); aIt.Next())
0125         AddModified(aS, aIt.Value());
0126 
0127       // Check Generated
0128       const NCollection_List<TopoDS_Shape>& aGenerated = theAlgo.Generated(aS);
0129       for (aIt.Initialize(aGenerated); aIt.More(); aIt.Next())
0130         AddGenerated(aS, aIt.Value());
0131     }
0132   }
0133 
0134 public:
0135   //! The types of the historical relations.
0136   enum TRelationType
0137   {
0138     TRelationType_Removed,
0139     TRelationType_Generated,
0140     TRelationType_Modified
0141   };
0142 
0143 public:
0144   //! Returns 'true' if the type of the shape is supported by the history.
0145   static bool IsSupportedType(const TopoDS_Shape& theShape)
0146   {
0147     const TopAbs_ShapeEnum aType = theShape.ShapeType();
0148     return aType == TopAbs_VERTEX || aType == TopAbs_EDGE || aType == TopAbs_FACE
0149            || aType == TopAbs_SOLID;
0150   }
0151 
0152 public: //! Methods to set the history.
0153   //! Set the second shape as generated one from the first shape.
0154   Standard_EXPORT void AddGenerated(const TopoDS_Shape& theInitial,
0155                                     const TopoDS_Shape& theGenerated);
0156 
0157   //! Set the second shape as modified one from the first shape.
0158   Standard_EXPORT void AddModified(const TopoDS_Shape& theInitial, const TopoDS_Shape& theModified);
0159 
0160   //! Set the shape as removed one.
0161   Standard_EXPORT void Remove(const TopoDS_Shape& theRemoved);
0162 
0163   //! Set the second shape as the only generated one from the first one.
0164   Standard_EXPORT void ReplaceGenerated(const TopoDS_Shape& theInitial,
0165                                         const TopoDS_Shape& theGenerated);
0166 
0167   //! Set the second shape as the only modified one from the first one.
0168   Standard_EXPORT void ReplaceModified(const TopoDS_Shape& theInitial,
0169                                        const TopoDS_Shape& theModified);
0170 
0171   //! Clears the history.
0172   void Clear()
0173   {
0174     myShapeToModified.Clear();
0175     myShapeToGenerated.Clear();
0176     myRemoved.Clear();
0177   }
0178 
0179 public: //! Methods to read the history.
0180   //! Returns all shapes generated from the shape.
0181   Standard_EXPORT const NCollection_List<TopoDS_Shape>& Generated(
0182     const TopoDS_Shape& theInitial) const;
0183 
0184   //! Returns all shapes modified from the shape.
0185   Standard_EXPORT const NCollection_List<TopoDS_Shape>& Modified(
0186     const TopoDS_Shape& theInitial) const;
0187 
0188   //! Returns 'true' if the shape is removed.
0189   Standard_EXPORT bool IsRemoved(const TopoDS_Shape& theInitial) const;
0190 
0191   //! Returns 'true' if there any shapes with Generated elements present
0192   bool HasGenerated() const { return !myShapeToGenerated.IsEmpty(); }
0193 
0194   //! Returns 'true' if there any Modified shapes present
0195   bool HasModified() const { return !myShapeToModified.IsEmpty(); }
0196 
0197   //! Returns 'true' if there any removed shapes present
0198   bool HasRemoved() const { return !myRemoved.IsEmpty(); }
0199 
0200 public: //! A method to merge a next history to this history.
0201   //! Merges the next history to this history.
0202   Standard_EXPORT void Merge(const occ::handle<BRepTools_History>& theHistory23);
0203 
0204   //! Merges the next history to this history.
0205   Standard_EXPORT void Merge(const BRepTools_History& theHistory23);
0206 
0207   //! Template method for merging history of the algorithm having standard
0208   //! history methods such as IsDeleted(), Modified() and Generated()
0209   //! into current history object.
0210   //! @param[in] theArguments  Arguments of the algorithm;
0211   //! @param[in] theAlgo  The algorithm.
0212   template <class TheAlgo>
0213   void Merge(const NCollection_List<TopoDS_Shape>& theArguments, TheAlgo& theAlgo)
0214   {
0215     // Create new history object from the given algorithm and merge it into this.
0216     Merge(BRepTools_History(theArguments, theAlgo));
0217   }
0218 
0219 public: //! A method to dump a history
0220   //! Prints the brief description of the history into a stream
0221   void Dump(Standard_OStream& theS)
0222   {
0223     theS << "History contains:\n";
0224     theS << " - " << myRemoved.Extent() << " Deleted shapes;\n";
0225     theS << " - " << myShapeToModified.Extent() << " Modified shapes;\n";
0226     theS << " - " << myShapeToGenerated.Extent() << " Generated shapes.\n";
0227   }
0228 
0229 public:
0230   //! Define the OCCT RTTI for the type.
0231   DEFINE_STANDARD_RTTIEXT(BRepTools_History, Standard_Transient)
0232 
0233 private:
0234   //! Prepares the shapes generated from the first shape to set the second one
0235   //! as generated one from the first one by the addition or the replacement.
0236   //! Returns 'true' on success.
0237   bool prepareGenerated(const TopoDS_Shape& theInitial, const TopoDS_Shape& theGenerated);
0238 
0239   //! Prepares the shapes modified from the first shape to set the second one
0240   //! as modified one from the first one by the addition or the replacement.
0241   //! Returns 'true' on success.
0242   bool prepareModified(const TopoDS_Shape& theInitial, const TopoDS_Shape& theModified);
0243 
0244 private: //! Data to keep the history.
0245   //! Maps each input shape to all shapes modified from it.
0246   //! If an input shape is not bound to the map then
0247   //! there is no shapes modified from the shape.
0248   //! No any shape should be mapped to an empty list.
0249   NCollection_DataMap<TopoDS_Shape, NCollection_List<TopoDS_Shape>, TopTools_ShapeMapHasher>
0250     myShapeToModified;
0251 
0252   //! Maps each input shape to all shapes generated from it.
0253   //! If an input shape is not bound to the map then
0254   //! there is no shapes generated from the shape.
0255   //! No any shape should be mapped to an empty list.
0256   NCollection_DataMap<TopoDS_Shape, NCollection_List<TopoDS_Shape>, TopTools_ShapeMapHasher>
0257     myShapeToGenerated;
0258 
0259   NCollection_Map<TopoDS_Shape, TopTools_ShapeMapHasher> myRemoved; //!< The removed shapes.
0260 
0261 private: //! Auxiliary members to read the history.
0262   //! An auxiliary empty list.
0263   static const NCollection_List<TopoDS_Shape> myEmptyList;
0264 
0265   //! A method to export the auxiliary list.
0266   Standard_EXPORT static const NCollection_List<TopoDS_Shape>& emptyList();
0267 
0268 private:
0269   //! Auxiliary messages.
0270   static const char* myMsgUnsupportedType;
0271   static const char* myMsgGeneratedAndRemoved;
0272   static const char* myMsgModifiedAndRemoved;
0273   static const char* myMsgGeneratedAndModified;
0274 };
0275 
0276 #endif // _BRepTools_History_HeaderFile