Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:32

0001 // Created on: 2006-10-08
0002 // Created by: Alexander GRIGORIEV
0003 // Copyright (c) 2006-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 VrmlData_Scene_HeaderFile
0017 #define VrmlData_Scene_HeaderFile
0018 
0019 #include <VrmlData_ListOfNode.hxx>
0020 #include <VrmlData_MapOfNode.hxx>
0021 #include <VrmlData_ErrorStatus.hxx>
0022 #include <VrmlData_WorldInfo.hxx>
0023 #include <TopoDS_Shape.hxx>
0024 #include <Standard_OStream.hxx>
0025 #include <Standard_IStream.hxx>
0026 #include <TCollection_ExtendedString.hxx>
0027 #include <NCollection_IncAllocator.hxx>
0028 #include <Standard_Mutex.hxx>
0029 #include <VrmlData_DataMapOfShapeAppearance.hxx>
0030 
0031 // resolve name collisions with X11 headers
0032 #ifdef Status
0033   #undef Status
0034 #endif
0035 
0036 struct VrmlData_InBuffer;
0037 
0038 /**
0039  * Block of comments describing class VrmlData_Scene
0040  */
0041 
0042 class VrmlData_Scene 
0043 {
0044  public:
0045   /**
0046    * Iterator type to get all contained Nodes one-by-one.
0047    */
0048   typedef VrmlData_ListOfNode::Iterator Iterator;
0049 
0050   // ---------- PUBLIC METHODS ----------
0051 
0052   /**
0053    * Constructor.
0054    */
0055   Standard_EXPORT VrmlData_Scene (const Handle(NCollection_IncAllocator)& = 0L);
0056 
0057   /**
0058    * Query the status of the previous operation.
0059    * Normally it should be equal to VrmlData_StatusOK (no error).
0060    */
0061   inline VrmlData_ErrorStatus   Status      () const
0062   { return myStatus; }
0063 
0064   /**
0065    * Add the given directory path to the list of VRML file search directories.
0066    * This method forms the list of directories ordered according to the
0067    * sequence of this method calls. When an Inline node is found, the URLs
0068    * in that node are matched with these directories.
0069    * The last (implicit) search directory is the current process directory
0070    * ("."). It takes effect if the list is empty or if there is no match with
0071    * exisiting directories.
0072    */
0073   Standard_EXPORT void          SetVrmlDir  (const TCollection_ExtendedString&);
0074 
0075   /**
0076    * Set the scale factor that would be further used in methods
0077    * ReadReal, ReadXYZ and ReadXY. All coordinates, distances and sized are
0078    * multiplied by this factor during reading the data.
0079    */
0080   inline void                   SetLinearScale (const Standard_Real theScale)
0081   { myLinearScale = theScale; }
0082 
0083   /**
0084    * Returns the directory iterator, to check the presence of requested VRML
0085    * file in each iterated directory.
0086    */
0087   inline NCollection_List<TCollection_ExtendedString>::Iterator
0088                                 VrmlDirIterator () const
0089   { return NCollection_List<TCollection_ExtendedString>::Iterator(myVrmlDir); }
0090 
0091   /**
0092    * Iterator of Nodes
0093    */
0094   inline Iterator               GetIterator () const
0095   { return Iterator (myLstNodes); }
0096 
0097   /**
0098    * Get the iterator of named nodes.
0099    */
0100   inline VrmlData_MapOfNode::Iterator
0101                                 NamedNodesIterator() const
0102   { return myNamedNodes; }
0103 
0104   /**
0105    * Allocator used by all nodes contained in the Scene.
0106    */
0107   inline const Handle(NCollection_IncAllocator)&
0108                                 Allocator   () const
0109   { return myAllocator; }
0110 
0111   /**
0112    * Add a Node. If theN belongs to another Scene, it is cloned.
0113    * <p>VrmlData_WorldInfo cannot be added, in this case the method
0114    * returns a NULL handle.
0115    */
0116   Standard_EXPORT const Handle(VrmlData_Node)&
0117                                 AddNode     (const Handle(VrmlData_Node)& theN,
0118                                              const Standard_Boolean isTopLevel
0119                                              = Standard_True);
0120 
0121   /**
0122    * Find a node by its name.
0123    * @param theName
0124    *   Name of the node to find.
0125    * @param theType
0126    *   Type to match. If this value is NULL, the first found node with the
0127    *   given name is returned. If theType is given, only the node that has
0128    *   that type is returned.
0129    */
0130   Standard_EXPORT Handle(VrmlData_Node)
0131                                 FindNode    (const char * theName,
0132                                              const Handle(Standard_Type)&
0133                                                            theType = 0L) const;
0134 
0135   /**
0136    * Find a node by its name.
0137    * @param theName
0138    *   Name of the node to search for.
0139    * @param theLocation
0140    *   Location of the found node with respect to the whole VRML shape.
0141    */
0142   Standard_EXPORT Handle(VrmlData_Node)
0143                                 FindNode(const char * theName,
0144                                          gp_Trsf&     theLocation) const;
0145 
0146   /**
0147    * Export to text stream (file or else).
0148    * This method is protected by Mutex, it is not allowed to read/write
0149    * two VRML streams concurrently.
0150    * The stream should give as the first line the VRML header:
0151    * <code>
0152    *   #VRML V2.0 <encoding type> [optional comment] <line terminator>
0153    * </code>
0154    *  
0155    */
0156   friend Standard_EXPORT Standard_OStream&
0157                                 operator << (Standard_OStream&      theOutput,
0158                                              const VrmlData_Scene&  theScene);
0159 
0160   /**
0161    * Import from text stream (file or else).
0162    * This method is protected by Mutex, it is not allowed to read/write
0163    * two VRML streams concurrently.
0164    */
0165   Standard_EXPORT VrmlData_Scene& operator<<(Standard_IStream& theInput);
0166 
0167   /**
0168    * Convert the scene to a Shape.
0169    */
0170   Standard_EXPORT               operator TopoDS_Shape () const;
0171 
0172   /**
0173    * Convert the scene to a Shape, with the information on materials defined
0174    * for each sub-shape. This method should be used instead of TopoDS_Shape
0175    * explicit conversion operator when you need to retrieve the material
0176    * aspect for each face or edge in the returned topological object.
0177    * @param M
0178    *   Data Map that binds an Appearance instance to each created TFace or
0179    *   TEdge if the Appearance node is defined in VRML scene for that geometry.
0180    * @return
0181    *   TopoDS_Shape (Compound) holding all the scene, similar to the result of
0182    *   explicit TopoDS_Shape conversion operator.
0183    */
0184   Standard_EXPORT TopoDS_Shape  GetShape (VrmlData_DataMapOfShapeAppearance& M);
0185 
0186   /**
0187    * Query the WorldInfo member.
0188    */
0189   Standard_EXPORT const Handle(VrmlData_WorldInfo)&
0190                                 WorldInfo() const;
0191 
0192   /**
0193    * Read a VRML line. Empty lines and comments are skipped.
0194    * The processing starts here from theBuffer.LinePtr; if there is at least
0195    * one non-empty character (neither space nor comment), this line is used
0196    * without reading the next one.
0197    * @param theLine
0198    *   Buffer receiving the input line
0199    * @param theInput
0200    *   Input stream
0201    * @param theLen
0202    *   Length of the input buffer (maximal line length)
0203    */
0204   Standard_EXPORT static VrmlData_ErrorStatus
0205                                 ReadLine    (VrmlData_InBuffer& theBuffer);
0206 
0207   /**
0208    * Read a singel word from the input stream, delimited by whitespace.
0209    */
0210   Standard_EXPORT static VrmlData_ErrorStatus
0211                                 ReadWord    (VrmlData_InBuffer&       theBuffer,
0212                                              TCollection_AsciiString& theStr);
0213 
0214   /**
0215    * Diagnostic dump of the contents
0216    */
0217   Standard_EXPORT void          Dump        (Standard_OStream& theStream) const;
0218 
0219   /**
0220    * Read one real value.
0221    */
0222   Standard_EXPORT VrmlData_ErrorStatus
0223                                 ReadReal    (VrmlData_InBuffer& theBuffer,
0224                                              Standard_Real&     theResult,
0225                                              Standard_Boolean   isApplyScale,
0226                                              Standard_Boolean   isOnlyPositive)
0227                                                                         const;
0228 
0229   /**
0230    * Read one triplet of real values.
0231    */
0232   Standard_EXPORT VrmlData_ErrorStatus
0233                                 ReadXYZ     (VrmlData_InBuffer& theBuffer,
0234                                              gp_XYZ&            theXYZ,
0235                                              Standard_Boolean   isApplyScale,
0236                                              Standard_Boolean   isOnlyPositive)
0237                                                                         const;
0238 
0239   /**
0240    * Read one doublet of real values.
0241    */
0242   Standard_EXPORT VrmlData_ErrorStatus
0243                                 ReadXY      (VrmlData_InBuffer& theBuffer,
0244                                              gp_XY&             theXYZ,
0245                                              Standard_Boolean   isApplyScale,
0246                                              Standard_Boolean   isOnlyPositive)
0247                                                                         const;
0248   /**
0249    * Read an array of integer indices, for IndexedfaceSet and IndexedLineSet.
0250    */ 
0251   Standard_EXPORT VrmlData_ErrorStatus
0252                                 ReadArrIndex(VrmlData_InBuffer& theBuffer,
0253                                              const Standard_Integer **& theArr,
0254                                              Standard_Size&             theNBl)
0255                                                                         const;
0256 
0257   /**
0258    * Query the line where the error occurred (if the status is not OK)
0259    */
0260   inline Standard_Integer       GetLineError() const { return myLineError; }
0261 
0262   /**
0263    * Store the indentation for VRML output.
0264    * @param nSpc
0265    *   number of spaces to insert at every indentation level
0266    */
0267   inline void                   SetIndent   (const Standard_Integer nSpc)
0268   { myIndent = nSpc; }
0269 
0270   /**
0271    * Write a triplet of real values on a separate line.
0272    * @param theXYZ
0273    *   The value to be output.
0274    * @param isScale
0275    *   If True, then each component is divided by myLinearScale.
0276    * @param thePostfix
0277    *   Optional string that is added before the end of the line.
0278    */
0279   Standard_EXPORT VrmlData_ErrorStatus
0280                                 WriteXYZ    (const  gp_XYZ&         theXYZ,
0281                                              const Standard_Boolean isScale,
0282                                              const char           * thePostfix
0283                                                                     = 0L) const;
0284   /**
0285    * Write an array of integer indices, for IndexedFaceSet and IndexedLineSet.
0286    */
0287   Standard_EXPORT VrmlData_ErrorStatus
0288                                 WriteArrIndex(const char *          thePrefix,
0289                                               const Standard_Integer ** theArr,
0290                                               const Standard_Size       theNbBl)
0291                                                                         const;
0292 
0293 
0294   /**
0295    * Write a string to the output stream respecting the indentation. The string
0296    * can be defined as two substrings that will be separated by a space.
0297    * Each of the substrings can be NULL, then it is ignored. If both
0298    * are NULL, then a single newline is output (without indent).
0299    * @param theLine0
0300    *   The first part of string to output
0301    * @param theLine1
0302    *   The second part of string to output
0303    * @param theIndent
0304    *   - 0 value ignored.
0305    *   - negative decreases the current indent and then outputs.
0306    *   - positive outputs and then increases the current indent. 
0307    * @return
0308    *   Error status of the stream, or a special error if myOutput == NULL.
0309    */
0310   Standard_EXPORT VrmlData_ErrorStatus
0311                                 WriteLine   (const char           * theLine0,
0312                                              const char           * theLine1=0L,
0313                                              const Standard_Integer theIndent
0314                                                                      = 0) const;
0315 
0316   /**
0317    * Write the given node to output stream 'myOutput'.
0318    */
0319   Standard_EXPORT VrmlData_ErrorStatus
0320                                 WriteNode   (const char * thePrefix,
0321                                              const Handle(VrmlData_Node)&) const;
0322 
0323   /**
0324    * Query if the current write operation is dummy, i.e., for the purpose of
0325    * collecting information before the real write is commenced.
0326    */
0327   inline Standard_Boolean       IsDummyWrite() const
0328   { return myOutput == 0L; }
0329 
0330  private:
0331   // ---------- PRIVATE METHODS (PROHIBITED) ----------
0332   VrmlData_Scene (const VrmlData_Scene&);
0333   VrmlData_Scene& operator = (const VrmlData_Scene&);
0334 
0335  protected:
0336   /**
0337    * Read whatever line from the input checking the std::istream flags.
0338    */ 
0339   Standard_EXPORT static VrmlData_ErrorStatus
0340                                 readLine    (VrmlData_InBuffer&     theBuffer);
0341 
0342   /**
0343    * Read and verify the VRML header (the 1st line of the file)
0344    */ 
0345   Standard_EXPORT static VrmlData_ErrorStatus
0346                                 readHeader  (VrmlData_InBuffer&     theBuffer);
0347 
0348   /**
0349    * Create the node.
0350    * @param theBuffer
0351    *   Input buffer from where the node is created
0352    * @param theNode
0353    *   Output parameter, contains the created node on exit
0354    * @param Type
0355    *   Node type to be checked. If it is NULL no type checking is done.
0356    *   Otherwise the created node is matched and an error is returned if
0357    *   no match detected.
0358    */
0359   Standard_EXPORT VrmlData_ErrorStatus 
0360                                 createNode  (VrmlData_InBuffer&     theBuffer,
0361                                              Handle(VrmlData_Node)& theNode,
0362                                              const Handle(Standard_Type)& Type);
0363 
0364   /**
0365    * Create a single Shape object from all geometric nodes in the list.
0366    */
0367   Standard_EXPORT static void   createShape (TopoDS_Shape&          outShape,
0368                                              const VrmlData_ListOfNode&,
0369                                              VrmlData_DataMapOfShapeAppearance*);
0370 
0371 
0372  private:
0373   // ---------- PRIVATE FIELDS ----------
0374   Standard_Real                                 myLinearScale;
0375   VrmlData_ListOfNode                           myLstNodes; ///! top-level nodes
0376   VrmlData_ListOfNode                           myAllNodes; ///! all nodes
0377   VrmlData_ErrorStatus                          myStatus;
0378   Handle(NCollection_IncAllocator)               myAllocator;
0379   Handle(VrmlData_WorldInfo)                     myWorldInfo;
0380   VrmlData_MapOfNode                            myNamedNodes;
0381 
0382   // read from stream
0383   NCollection_List<TCollection_ExtendedString>  myVrmlDir;
0384   Standard_Mutex                                myMutex;
0385   Standard_Integer                              myLineError;///! #0 if error
0386 
0387   // write to stream
0388   Standard_OStream                              * myOutput;
0389   Standard_Integer                              myIndent;
0390   Standard_Integer                              myCurrentIndent;
0391   /**
0392    * This map is used to avoid multiple storage of the same named node: each
0393    * named node is added here when it is written the first time.
0394    */
0395   VrmlData_MapOfNode                            myNamedNodesOut;
0396   /**
0397    * This map allows to resolve multiple reference to any unnamed node. It
0398    * is used during the dummy write (myOutput == 0L). When a node is processed
0399    * the first time it is added to this map, the second time it is automatically
0400    * assigned a name.
0401    */
0402   NCollection_Map<Standard_Address>             myUnnamedNodesOut;
0403   Standard_Integer                              myAutoNameCounter;
0404   friend class VrmlData_Group;
0405   friend class VrmlData_Node;
0406 };
0407 
0408 #endif