1 // Created on: 2018-03-29
2 // Created by: Eugeny MALTCHIKOV
3 // Copyright (c) 2018 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 #ifndef _BOPAlgo_MakeConnected_HeaderFile
17 #define _BOPAlgo_MakeConnected_HeaderFile
18 
19 #include <Standard.hxx>
20 #include <Standard_DefineAlloc.hxx>
21 #include <Standard_Handle.hxx>
22 
23 #include <BOPAlgo_Options.hxx>
24 #include <BOPAlgo_MakePeriodic.hxx>
25 
26 #include <BRepTools_History.hxx>
27 
28 #include <NCollection_DataMap.hxx>
29 
30 #include <TopTools_OrientedShapeMapHasher.hxx>
31 
32 //! BOPAlgo_MakeConnected is the algorithm for making the touching
33 //! shapes connected or glued, i.e. for making the coinciding geometries
34 //! be topologically shared among the shapes.
35 //!
36 //! The input shapes should be of the same dimension, otherwise
37 //! the gluing will not make any sense.
38 //!
39 //! After the shapes are made connected, the border elements of input shapes
40 //! are associated with the shapes to which they belong. At that, the orientation of
41 //! the border element in the shape is taken into account.
42 //! The associations are made for the following types:
43 //! - For input SOLIDS, the resulting FACES are associated with the input solids;
44 //! - For input FACES, the resulting EDGES are associated with the input faces;
45 //! - For input EDGES, the resulting VERTICES are associated with the input edges.
46 //!
47 //! In frames of this algorithm the input shapes are called materials,
48 //! and the association process is called the material association.
49 //! The material association allows finding the coinciding elements for the opposite
50 //! input shapes. These elements will be associated to at least two materials.
51 //!
52 //! After making the shapes connected, it is possible to make the connected
53 //! shape periodic using the *BOPAlgo_MakePeriodic* tool.
54 //! After making the shape periodic, the material associations will be updated
55 //! to correspond to the actual state of the result shape.
56 //! Repetition of the periodic shape is also possible here. Material associations
57 //! are not going to be lost.
58 //!
59 //! The algorithm supports history of shapes modification, thus it is possible
60 //! to track the modification of the input shapes during the operations.
61 //! Additionally to standard history methods, the algorithm provides the
62 //! the method *GetOrigins()* which allows obtaining the input shapes from which
63 //! the resulting shape has been created.
64 //!
65 //! The algorithm supports the parallel processing mode, which allows faster
66 //! completion of the operations.
67 //!
68 //! The algorithm returns the following Error/Warning messages:
69 //! - *BOPAlgo_AlertTooFewArguments* - error alert is given on the attempt to run
70 //!     the algorithm without the arguments;
71 //! - *BOPAlgo_AlertMultiDimensionalArguments* - error alert is given on the attempt
72 //!     to run the algorithm on multi-dimensional arguments;
73 //! - *BOPAlgo_AlertUnableToGlue* - error alert is given if the gluer algorithm
74 //!     is unable to glue the given arguments;
75 //! - *BOPAlgo_AlertUnableToMakePeriodic* - warning alert is given if the periodicity
76 //!     maker is unable to make the connected shape periodic with given options;
77 //! - *BOPAlgo_AlertShapeIsNotPeriodic* - warning alert is given on the attempt to
78 //!     repeat the shape before making it periodic.
79 //!
80 //! Here is the example of usage of the algorithm:
81 //! ~~~~
82 //! TopTools_ListOfShape anArguments = ...;  // Shapes to make connected
83 //! Standard_Boolean bRunParallel = ...;     // Parallel processing mode
84 //!
85 //! BOPAlgo_MakeConnected aMC;               // Tool for making the shapes connected
86 //! aMC.SetArguments(anArguments);           // Set the shapes
87 //! aMC.SetRunParallel(bRunParallel);        // Set parallel processing mode
88 //! aMC.Perform();                           // Perform the operation
89 //!
90 //! if (aMC.HasErrors())                     // Check for the errors
91 //! {
92 //!   // errors treatment
93 //!   Standard_SStream aSStream;
94 //!   aMC.DumpErrors(aSStream);
95 //!   return;
96 //! }
97 //! if (aMC.HasWarnings())                   // Check for the warnings
98 //! {
99 //!   // warnings treatment
100 //!   Standard_SStream aSStream;
101 //!   aMC.DumpWarnings(aSStream);
102 //! }
103 //!
104 //! const TopoDS_Shape& aGluedShape = aMC.Shape(); // Connected shape
105 //!
106 //! // Checking material associations
107 //! TopAbs_ShapeEnum anElemType = ...;       // Type of border element
108 //! TopExp_Explorer anExp(anArguments.First(), anElemType);
109 //! for (; anExp.More(); anExp.Next())
110 //! {
111 //!   const TopoDS_Shape& anElement = anExp.Current();
112 //!   const TopTools_ListOfShape& aNegativeM = aMC.MaterialsOnNegativeSide(anElement);
113 //!   const TopTools_ListOfShape& aPositiveM = aMC.MaterialsOnPositiveSide(anElement);
114 //! }
115 //!
116 //! // Making the connected shape periodic
117 //! BOPAlgo_MakePeriodic::PeriodicityParams aParams = ...; // Options for periodicity of the connected shape
118 //! aMC.MakePeriodic(aParams);
119 //!
120 //! // Shape repetition after making it periodic
121 //! // Check if the shape has been made periodic successfully
122 //! if (aMC.PeriodicityTool().HasErrors())
123 //! {
124 //!   // Periodicity maker error treatment
125 //! }
126 //!
127 //! // Shape repetition in periodic directions
128 //! aMC.RepeatShape(0, 2);
129 //!
130 //! const TopoDS_Shape& aShape = aMC.PeriodicShape(); // Periodic and repeated shape
131 //! ~~~~
132 //!
133 class BOPAlgo_MakeConnected : public BOPAlgo_Options
134 {
135 public:
136 
137   DEFINE_STANDARD_ALLOC
138 
139 public: //! @name Constructor
140 
141   //! Empty constructor
BOPAlgo_MakeConnected()142   BOPAlgo_MakeConnected() : BOPAlgo_Options()
143   {
144   }
145 
146 
147 public: //! @name Setters for the shapes to make connected
148 
149   //! Sets the shape for making them connected.
150   //! @param theArgs [in] The arguments for the operation.
SetArguments(const TopTools_ListOfShape & theArgs)151   void SetArguments(const TopTools_ListOfShape& theArgs)
152   {
153     myArguments = theArgs;
154   }
155 
156   //! Adds the shape to the arguments.
157   //! @param theS [in] One of the argument shapes.
AddArgument(const TopoDS_Shape & theS)158   void AddArgument(const TopoDS_Shape& theS)
159   {
160     myArguments.Append(theS);
161   }
162 
163   //! Returns the list of arguments of the operation.
Arguments() const164   const TopTools_ListOfShape& Arguments() const
165   {
166     return myArguments;
167   }
168 
169 public: //! @name Performing the operations
170 
171   //! Performs the operation, i.e. makes the input shapes connected.
172   Standard_EXPORT void Perform();
173 
174 
175 public: //! @name Shape periodicity & repetition
176 
177   //! Makes the connected shape periodic.
178   //! Repeated calls of this method overwrite the previous calls
179   //! working with the basis connected shape.
180   //! @param theParams [in] Periodic options.
181   Standard_EXPORT void MakePeriodic(const BOPAlgo_MakePeriodic::PeriodicityParams& theParams);
182 
183   //! Performs repetition of the periodic shape in specified direction
184   //! required number of times.
185   //! @param theDirectionID [in] The direction's ID (0 for X, 1 for Y, 2 for Z);
186   //! @param theTimes [in] Requested number of repetitions (sign of the value defines
187   //!                      the side of the repetition direction (positive or negative)).
188   Standard_EXPORT void RepeatShape(const Standard_Integer theDirectionID,
189                                    const Standard_Integer theTimes);
190 
191   //! Clears the repetitions performed on the periodic shape,
192   //! keeping the shape periodic.
193   Standard_EXPORT void ClearRepetitions();
194 
195   //! Returns the periodicity tool.
PeriodicityTool() const196   const BOPAlgo_MakePeriodic& PeriodicityTool() const
197   {
198     return myPeriodicityMaker;
199   }
200 
201 
202 public: //! @name Material transitions
203 
204   //! Returns the original shapes which images contain the
205   //! the given shape with FORWARD orientation.
206   //! @param theS [in] The shape for which the materials are necessary.
MaterialsOnPositiveSide(const TopoDS_Shape & theS)207   const TopTools_ListOfShape& MaterialsOnPositiveSide(const TopoDS_Shape& theS)
208   {
209     const TopTools_ListOfShape* pLM = myMaterials.Seek(theS.Oriented(TopAbs_FORWARD));
210     return (pLM ? *pLM : EmptyList());
211   }
212 
213   //! Returns the original shapes which images contain the
214   //! the given shape with REVERSED orientation.
215   //! @param theS [in] The shape for which the materials are necessary.
MaterialsOnNegativeSide(const TopoDS_Shape & theS)216   const TopTools_ListOfShape& MaterialsOnNegativeSide(const TopoDS_Shape& theS)
217   {
218     const TopTools_ListOfShape* pLM = myMaterials.Seek(theS.Oriented(TopAbs_REVERSED));
219     return (pLM ? *pLM : EmptyList());
220   }
221 
222 
223 public: //! @name History methods
224 
225   //! Returns the history of operations
Handle(BRepTools_History)226   const Handle(BRepTools_History)& History() const
227   {
228     return myHistory;
229   }
230 
231   //! Returns the list of shapes modified from the given shape.
232   //! @param theS [in] The shape for which the modified shapes are necessary.
GetModified(const TopoDS_Shape & theS)233   const TopTools_ListOfShape& GetModified(const TopoDS_Shape& theS)
234   {
235     return (myHistory.IsNull() ? EmptyList() : myHistory->Modified(theS));
236   }
237 
238   //! Returns the list of original shapes from which the current shape has been created.
239   //! @param theS [in] The shape for which the origins are necessary.
GetOrigins(const TopoDS_Shape & theS)240   const TopTools_ListOfShape& GetOrigins(const TopoDS_Shape& theS)
241   {
242     const TopTools_ListOfShape* pLOr = myOrigins.Seek(theS);
243     return (pLOr ? *pLOr : EmptyList());
244   }
245 
246 
247 public: //! @name Getting the result shapes
248 
249   //! Returns the resulting connected shape
Shape() const250   const TopoDS_Shape& Shape() const
251   {
252     return myGlued;
253   }
254 
255   //! Returns the resulting periodic & repeated shape
PeriodicShape() const256   const TopoDS_Shape& PeriodicShape() const
257   {
258     return myShape;
259   }
260 
261 
262 public: //! @name Clearing the contents of the algorithm from previous runs
263 
264   //! Clears the contents of the algorithm.
Clear()265   void Clear()
266   {
267     BOPAlgo_Options::Clear();
268     myArguments.Clear();
269     myAllInputsMap.Clear();
270     myPeriodicityMaker.Clear();
271     myOrigins.Clear();
272     myMaterials.Clear();
273     if (!myGlueHistory.IsNull())
274       myGlueHistory->Clear();
275     if (!myHistory.IsNull())
276       myHistory->Clear();
277     myGlued.Nullify();
278     myShape.Nullify();
279   }
280 
281 
282 protected: //! @name Protected methods performing the operation
283 
284   //! Checks the validity of input data.
285   Standard_EXPORT void CheckData();
286 
287   //! Makes the argument shapes connected (or glued).
288   Standard_EXPORT void MakeConnected();
289 
290   //! Associates the materials transitions for the border elements:
291   //! - For input Solids, associates the Faces to Solids;
292   //! - For input Faces, associates the Edges to Faces;
293   //! - For input Edges, associates the Vertices to Edges.
294   Standard_EXPORT void AssociateMaterials();
295 
296   //! Fills the map of origins
297   Standard_EXPORT void FillOrigins();
298 
299   //! Updates the history, material associations, origins map
300   //! after periodicity operations.
301   Standard_EXPORT void Update();
302 
303 private:
304 
305   //! Returns an empty list.
EmptyList()306   const TopTools_ListOfShape& EmptyList()
307   {
308     static const TopTools_ListOfShape anEmptyList;
309     return anEmptyList;
310   }
311 
312 protected: //! @name Fields
313 
314   // Inputs
315   TopTools_ListOfShape myArguments;          //!< Input shapes for making them connected
316   TopTools_IndexedMapOfShape myAllInputsMap; //!< Map of all BRep sub-elements of the input shapes
317 
318   // Tools
319   BOPAlgo_MakePeriodic myPeriodicityMaker;   //!< Tool for making the shape periodic
320 
321   // Results
322   NCollection_DataMap
323     <TopoDS_Shape,
324      TopTools_ListOfShape,
325      TopTools_OrientedShapeMapHasher> myMaterials; //!< Map of the materials associations
326                                                    //! for the border elements
327   TopTools_DataMapOfShapeListOfShape myOrigins;    //!< Map of origins
328                                                    //! (allows tracking the shape's ancestors)
329 
330   Handle(BRepTools_History) myGlueHistory;         //!< Gluing History
331   Handle(BRepTools_History) myHistory;             //!< Final History of shapes modifications
332                                                    //! (including making the shape periodic and repetitions)
333 
334   TopoDS_Shape myGlued;                            //!< The resulting connected (glued) shape
335   TopoDS_Shape myShape;                            //!< The resulting shape
336 };
337 
338 #endif // _BOPAlgo_MakeConnected_HeaderFile
339