0024157: Parallelization of assembly part of BO
[occt.git] / src / VrmlData / VrmlData_Scene.hxx
CommitLineData
b311480e 1// Created on: 2006-10-08
2// Created by: Alexander GRIGORIEV
3// Copyright (c) 2006-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
7fd59977 20
21
22#ifndef VrmlData_Scene_HeaderFile
23#define VrmlData_Scene_HeaderFile
24
25#include <VrmlData_ListOfNode.hxx>
26#include <VrmlData_MapOfNode.hxx>
27#include <VrmlData_ErrorStatus.hxx>
28#include <VrmlData_Geometry.hxx>
29#include <VrmlData_WorldInfo.hxx>
30#include <TopoDS_Shape.hxx>
31#include <Standard_OStream.hxx>
32#include <Standard_IStream.hxx>
33#include <TCollection_ExtendedString.hxx>
34#include <Standard_Mutex.hxx>
35
36struct VrmlData_InBuffer;
37class Handle_VrmlData_WorldInfo;
38class VrmlData_DataMapOfShapeAppearance;
39
40/**
41 * Block of comments describing class VrmlData_Scene
42 */
43
44class VrmlData_Scene
45{
46 public:
47 /**
48 * Iterator type to get all contained Nodes one-by-one.
49 */
50 typedef VrmlData_ListOfNode::Iterator Iterator;
51
52 // ---------- PUBLIC METHODS ----------
53
54 /**
55 * Constructor.
56 */
57 Standard_EXPORT VrmlData_Scene (const Handle(NCollection_IncAllocator)& = 0L);
58
59 /**
60 * Query the status of the previous operation.
61 * Normally it should be equal to VrmlData_StatusOK (no error).
62 */
63 inline VrmlData_ErrorStatus Status () const
64 { return myStatus; }
65
66 /**
67 * Add the given directory path to the list of VRML file search directories.
68 * This method forms the list of directories ordered according to the
69 * sequence of this method calls. When an Inline node is found, the URLs
70 * in that node are matched with these directories.
71 * The last (implicit) search directory is the current process directory
72 * ("."). It takes effect if the list is empty or if there is no match with
73 * exisiting directories.
74 */
75 Standard_EXPORT void SetVrmlDir (const TCollection_ExtendedString&);
76
77 /**
78 * Set the scale factor that would be further used in methods
79 * ReadReal, ReadXYZ and ReadXY. All coordinates, distances and sized are
80 * multiplied by this factor during reading the data.
81 */
82 inline void SetLinearScale (const Standard_Real theScale)
83 { myLinearScale = theScale; }
84
85 /**
86 * Returns the directory iterator, to check the presence of requested VRML
87 * file in each iterated directory.
88 */
89 inline NCollection_List<TCollection_ExtendedString>::Iterator
90 VrmlDirIterator () const
91 { return NCollection_List<TCollection_ExtendedString>::Iterator(myVrmlDir); }
92
93 /**
94 * Iterator of Nodes
95 */
96 inline Iterator GetIterator () const
97 { return Iterator (myLstNodes); }
98
99 /**
100 * Get the iterator of named nodes.
101 */
102 inline VrmlData_MapOfNode::Iterator
103 NamedNodesIterator() const
104 { return myNamedNodes; }
105
106 /**
107 * Allocator used by all nodes contained in the Scene.
108 */
109 inline const Handle(NCollection_IncAllocator)&
110 Allocator () const
111 { return myAllocator; }
112
113 /**
114 * Add a Node. If theN belongs to another Scene, it is cloned.
115 * <p>VrmlData_WorldInfo cannot be added, in this case the method
116 * returns a NULL handle.
117 */
118 Standard_EXPORT const Handle(VrmlData_Node)&
119 AddNode (const Handle(VrmlData_Node)& theN,
120 const Standard_Boolean isTopLevel
121 = Standard_True);
122
123 /**
124 * Find a node by its name.
125 * @param theName
126 * Name of the node to find.
127 * @param theType
128 * Type to match. If this value is NULL, the first found node with the
129 * given name is returned. If theType is given, only the node that has
130 * that type is returned.
131 */
132 Standard_EXPORT Handle(VrmlData_Node)
133 FindNode (const char * theName,
134 const Handle(Standard_Type)&
135 theType = 0L) const;
136
137 /**
138 * Find a node by its name.
139 * @param theName
140 * Name of the node to search for.
141 * @param theLocation
142 * Location of the found node with respect to the whole VRML shape.
143 */
144 Standard_EXPORT Handle(VrmlData_Node)
145 FindNode(const char * theName,
146 gp_Trsf& theLocation) const;
147
148 /**
149 * Export to text stream (file or else).
150 * This method is protected by Mutex, it is not allowed to read/write
151 * two VRML streams concurrently.
152 * The stream should give as the first line the VRML header:
153 * <code>
154 * #VRML V2.0 <encoding type> [optional comment] <line terminator>
155 * </code>
156 *
157 */
158 friend Standard_EXPORT Standard_OStream&
159 operator << (Standard_OStream& theOutput,
160 const VrmlData_Scene& theScene);
161
162 /**
163 * Import from text stream (file or else).
164 * This method is protected by Mutex, it is not allowed to read/write
165 * two VRML streams concurrently.
166 */
167 Standard_EXPORT VrmlData_Scene& operator<<(Standard_IStream& theInput);
168
169 /**
170 * Convert the scene to a Shape.
171 */
172 Standard_EXPORT operator TopoDS_Shape () const;
173
174 /**
175 * Convert the scene to a Shape, with the information on materials defined
176 * for each sub-shape. This method should be used instead of TopoDS_Shape
177 * explicit conversion operator when you need to retrieve the material
178 * aspect for each face or edge in the returned topological object.
179 * @param M
180 * Data Map that binds an Appearance instance to each created TFace or
181 * TEdge if the Appearance node is defined in VRML scene for that geometry.
182 * @return
183 * TopoDS_Shape (Compound) holding all the scene, similar to the result of
184 * explicit TopoDS_Shape conversion operator.
185 */
186 Standard_EXPORT TopoDS_Shape GetShape (VrmlData_DataMapOfShapeAppearance& M);
187
188 /**
189 * Query the WorldInfo member.
190 */
191 Standard_EXPORT const Handle_VrmlData_WorldInfo&
192 WorldInfo() const;
193
194 /**
195 * Read a VRML line. Empty lines and comments are skipped.
196 * The processing starts here from theBuffer.LinePtr; if there is at least
197 * one non-empty character (neither space nor comment), this line is used
198 * without reading the next one.
199 * @param theLine
200 * Buffer receiving the input line
201 * @param theInput
202 * Input stream
203 * @param theLen
204 * Length of the input buffer (maximal line length)
205 */
206 Standard_EXPORT static VrmlData_ErrorStatus
207 ReadLine (VrmlData_InBuffer& theBuffer);
208
209 /**
210 * Read a singel word from the input stream, delimited by whitespace.
211 */
212 Standard_EXPORT static VrmlData_ErrorStatus
213 ReadWord (VrmlData_InBuffer& theBuffer,
214 TCollection_AsciiString& theStr);
215
216 /**
217 * Diagnostic dump of the contents
218 */
219 Standard_EXPORT void Dump (Standard_OStream& theStream) const;
220
221 /**
222 * Read one real value.
223 */
224 Standard_EXPORT VrmlData_ErrorStatus
225 ReadReal (VrmlData_InBuffer& theBuffer,
226 Standard_Real& theResult,
227 Standard_Boolean isApplyScale,
228 Standard_Boolean isOnlyPositive)
229 const;
230
231 /**
232 * Read one triplet of real values.
233 */
234 Standard_EXPORT VrmlData_ErrorStatus
235 ReadXYZ (VrmlData_InBuffer& theBuffer,
236 gp_XYZ& theXYZ,
237 Standard_Boolean isApplyScale,
238 Standard_Boolean isOnlyPositive)
239 const;
240
241 /**
242 * Read one doublet of real values.
243 */
244 Standard_EXPORT VrmlData_ErrorStatus
245 ReadXY (VrmlData_InBuffer& theBuffer,
246 gp_XY& theXYZ,
247 Standard_Boolean isApplyScale,
248 Standard_Boolean isOnlyPositive)
249 const;
250 /**
251 * Read an array of integer indices, for IndexedfaceSet and IndexedLineSet.
252 */
253 Standard_EXPORT VrmlData_ErrorStatus
254 ReadArrIndex(VrmlData_InBuffer& theBuffer,
255 const Standard_Integer **& theArr,
256 Standard_Size& theNBl)
257 const;
258
259 /**
260 * Query the line where the error occurred (if the status is not OK)
261 */
262 inline Standard_Integer GetLineError() const { return myLineError; }
263
264 /**
265 * Store the indentation for VRML output.
266 * @param nSpc
267 * number of spaces to insert at every indentation level
268 */
269 inline void SetIndent (const Standard_Integer nSpc)
270 { myIndent = nSpc; }
271
272 /**
273 * Write a triplet of real values on a separate line.
274 * @param theXYZ
275 * The value to be output.
276 * @param isScale
277 * If True, then each component is divided by myLinearScale.
278 * @param thePostfix
279 * Optional string that is added before the end of the line.
280 */
281 Standard_EXPORT VrmlData_ErrorStatus
282 WriteXYZ (const gp_XYZ& theXYZ,
283 const Standard_Boolean isScale,
284 const char * thePostfix
285 = 0L) const;
286 /**
287 * Write an array of integer indices, for IndexedFaceSet and IndexedLineSet.
288 */
289 Standard_EXPORT VrmlData_ErrorStatus
290 WriteArrIndex(const char * thePrefix,
291 const Standard_Integer ** theArr,
292 const Standard_Size theNbBl)
293 const;
294
295
296 /**
297 * Write a string to the output stream respecting the indentation. The string
298 * can be defined as two substrings that will be separated by a space.
299 * Each of the substrings can be NULL, then it is ignored. If both
300 * are NULL, then a single newline is output (without indent).
301 * @param theLine0
302 * The first part of string to output
303 * @param theLine1
304 * The second part of string to output
305 * @param theIndent
306 * - 0 value ignored.
307 * - negative decreases the current indent and then outputs.
308 * - positive outputs and then increases the current indent.
309 * @return
310 * Error status of the stream, or a special error if myOutput == NULL.
311 */
312 Standard_EXPORT VrmlData_ErrorStatus
313 WriteLine (const char * theLine0,
314 const char * theLine1=0L,
315 const Standard_Integer theIndent
316 = 0) const;
317
318 /**
319 * Write the given node to output stream 'myOutput'.
320 */
321 Standard_EXPORT VrmlData_ErrorStatus
322 WriteNode (const char * thePrefix,
323 const Handle_VrmlData_Node&) const;
324
325 /**
326 * Query if the current write operation is dummy, i.e., for the purpose of
327 * collecting information before the real write is commenced.
328 */
329 inline Standard_Boolean IsDummyWrite() const
330 { return myOutput == 0L; }
331
332 private:
333 // ---------- PRIVATE METHODS (PROHIBITED) ----------
334 VrmlData_Scene (const VrmlData_Scene&);
335 VrmlData_Scene& operator = (const VrmlData_Scene&);
336
337 protected:
338 /**
339 * Read whatever line from the input checking the istream flags.
340 */
341 Standard_EXPORT static VrmlData_ErrorStatus
342 readLine (VrmlData_InBuffer& theBuffer);
343
344 /**
345 * Read and verify the VRML header (the 1st line of the file)
346 */
347 Standard_EXPORT static VrmlData_ErrorStatus
348 readHeader (VrmlData_InBuffer& theBuffer);
349
350 /**
351 * Create the node.
352 * @param theBuffer
353 * Input buffer from where the node is created
354 * @param theNode
355 * Output parameter, contains the created node on exit
356 * @param Type
357 * Node type to be checked. If it is NULL no type checking is done.
358 * Otherwise the created node is matched and an error is returned if
359 * no match detected.
360 */
361 Standard_EXPORT VrmlData_ErrorStatus
362 createNode (VrmlData_InBuffer& theBuffer,
363 Handle(VrmlData_Node)& theNode,
364 const Handle(Standard_Type)& Type);
365
366 /**
367 * Create a single Shape object from all geometric nodes in the list.
368 */
369 Standard_EXPORT static void createShape (TopoDS_Shape& outShape,
370 const VrmlData_ListOfNode&,
371 VrmlData_DataMapOfShapeAppearance*);
372
373
374 private:
375 // ---------- PRIVATE FIELDS ----------
376 Standard_Real myLinearScale;
377 VrmlData_ListOfNode myLstNodes; ///! top-level nodes
378 VrmlData_ListOfNode myAllNodes; ///! all nodes
379 VrmlData_ErrorStatus myStatus;
380 Handle_NCollection_IncAllocator myAllocator;
381 Handle_VrmlData_WorldInfo myWorldInfo;
382 VrmlData_MapOfNode myNamedNodes;
383
384 // read from stream
385 NCollection_List<TCollection_ExtendedString> myVrmlDir;
386 Standard_Mutex myMutex;
387 Standard_Integer myLineError;///! #0 if error
388
389 // write to stream
390 Standard_OStream * myOutput;
391 Standard_Integer myIndent;
392 Standard_Integer myCurrentIndent;
393 /**
394 * This map is used to avoid multiple storage of the same named node: each
395 * named node is added here when it is written the first time.
396 */
397 VrmlData_MapOfNode myNamedNodesOut;
398 /**
399 * This map allows to resolve multiple reference to any unnamed node. It
400 * is used during the dummy write (myOutput == 0L). When a node is processed
401 * the first time it is added to this map, the second time it is automatically
402 * assigned a name.
403 */
404 NCollection_Map<Standard_Address> myUnnamedNodesOut;
405 Standard_Integer myAutoNameCounter;
406 friend class VrmlData_Group;
407 friend class VrmlData_Node;
408};
409
410#endif