1 // Created on: 2006-05-25
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2006-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <VrmlData_Scene.hxx>
17 #include <VrmlData_InBuffer.hxx>
18 #include <VrmlData_Appearance.hxx>
19 #include <VrmlData_Box.hxx>
20 #include <VrmlData_Color.hxx>
21 #include <VrmlData_Cone.hxx>
22 #include <VrmlData_Coordinate.hxx>
23 #include <VrmlData_Cylinder.hxx>
24 #include <VrmlData_DataMapOfShapeAppearance.hxx>
25 #include <VrmlData_Group.hxx>
26 #include <VrmlData_ImageTexture.hxx>
27 #include <VrmlData_InBuffer.hxx>
28 #include <VrmlData_IndexedFaceSet.hxx>
29 #include <VrmlData_IndexedLineSet.hxx>
30 #include <VrmlData_Material.hxx>
31 #include <VrmlData_Normal.hxx>
32 #include <VrmlData_Scene.hxx>
33 #include <VrmlData_ShapeNode.hxx>
34 #include <VrmlData_Sphere.hxx>
35 #include <VrmlData_TextureCoordinate.hxx>
36 #include <VrmlData_UnknownNode.hxx>
37 //#include <VrmlData_WorldInfo.hxx>
38 #include <NCollection_Vector.hxx>
39 #include <TopoDS_TFace.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <BRep_Builder.hxx>
44 #include <Precision.hxx>
45 #include <Standard_Version.hxx>
46 #include <VrmlData_WorldInfo.hxx>
47 #include <VrmlData_Geometry.hxx>
50 #define _CRT_SECURE_NO_DEPRECATE
51 #pragma warning (disable:4996)
54 static void dumpNode (Standard_OStream& theStream,
55 const Handle(VrmlData_Node)& theNode,
56 const TCollection_AsciiString& theIndent);
58 static void dumpNodeHeader (Standard_OStream& theStream,
59 const TCollection_AsciiString& theIndent,
61 const char * theName);
63 //=======================================================================
64 //function : VrmlData_Scene
65 //purpose : Constructor
66 //=======================================================================
68 VrmlData_Scene::VrmlData_Scene
69 (const Handle(NCollection_IncAllocator)& theAlloc)
71 myStatus (VrmlData_StatusOK),
72 myAllocator (theAlloc.IsNull() ?
73 new NCollection_IncAllocator : theAlloc.operator->()),
80 myWorldInfo = new VrmlData_WorldInfo (* this);
81 Standard_CString anInfo = "Generated by Open CASCADE Technology " OCC_VERSION_STRING;
82 myWorldInfo->AddInfo (anInfo);
83 myLstNodes.Append (myWorldInfo);
84 myAllNodes.Append (myWorldInfo);
87 //=======================================================================
90 //=======================================================================
92 const Handle(VrmlData_Node)& VrmlData_Scene::AddNode
93 (const Handle(VrmlData_Node)& theN,
94 const Standard_Boolean isTopLevel)
96 if (theN.IsNull() == Standard_False)
97 if (theN->IsKind (STANDARD_TYPE(VrmlData_WorldInfo)) == Standard_False) {
99 const Handle(VrmlData_Node)& aNode =
100 myAllNodes.Append ((&theN->Scene()== this) ? theN : theN->Clone (NULL));
101 // Name is checked for uniqueness. If not, letter 'D' is appended until
102 // the name proves to be unique.
103 if (aNode->Name()[0] != '\0')
104 while (myNamedNodes.Add (aNode) == Standard_False)
105 aNode->setName (aNode->Name(), "D");
107 myLstNodes.Append (aNode);
111 static Handle(VrmlData_Node) aNullNode;
116 //=======================================================================
117 //function : operator <<
118 //purpose : Export to text stream (file or else)
119 //=======================================================================
121 Standard_OStream& operator << (Standard_OStream& theOutput,
122 const VrmlData_Scene& theScene)
124 VrmlData_Scene& aScene = const_cast <VrmlData_Scene&> (theScene);
125 aScene.myMutex.Lock();
126 aScene.myCurrentIndent = 0;
127 aScene.myLineError = 0;
128 aScene.myOutput = 0L;
129 aScene.myNamedNodesOut.Clear();
130 aScene.myUnnamedNodesOut.Clear();
131 aScene.myAutoNameCounter = 0;
135 VrmlData_Scene::Iterator anIterD(aScene.myLstNodes);
136 for (; anIterD.More(); anIterD.Next()) {
137 const Handle(VrmlData_Node)& aNode = anIterD.Value();
138 if (aNode.IsNull() == Standard_False) {
139 const VrmlData_ErrorStatus aStatus = aScene.WriteNode (0L, aNode);
140 if (aStatus != VrmlData_StatusOK &&
141 aStatus != VrmlData_NotImplemented)
146 aScene.myOutput = &theOutput;
147 aScene.myNamedNodesOut.Clear();
148 theOutput << "#VRML V2.0 utf8\n\n";
152 VrmlData_Scene::Iterator anIter(aScene.myLstNodes);
153 for (; anIter.More(); anIter.Next()) {
154 const Handle(VrmlData_Node)& aNode = anIter.Value();
155 if (aNode.IsNull() == Standard_False) {
156 const VrmlData_ErrorStatus aStatus = aScene.WriteNode (0L, aNode);
157 if (aStatus != VrmlData_StatusOK &&
158 aStatus != VrmlData_NotImplemented)
162 aScene.myOutput = 0L;
163 aScene.myNamedNodesOut.Clear();
164 aScene.myUnnamedNodesOut.Clear();
165 aScene.myMutex.Unlock();
169 //=======================================================================
170 //function : SetVrmlDir
172 //=======================================================================
174 void VrmlData_Scene::SetVrmlDir (const TCollection_ExtendedString& theDir)
176 TCollection_ExtendedString& aDir = myVrmlDir.Append (theDir);
177 const Standard_ExtCharacter aTerminator = aDir.Value(aDir.Length());
178 if (aTerminator != Standard_ExtCharacter('\\') &&
179 aTerminator != Standard_ExtCharacter('/'))
181 aDir += TCollection_ExtendedString ("\\");
183 aDir += TCollection_ExtendedString ("/");
187 //=======================================================================
188 //function : WorldInfo
190 //=======================================================================
192 const Handle(VrmlData_WorldInfo)& VrmlData_Scene::WorldInfo() const
197 //=======================================================================
198 //function : readLine
200 //=======================================================================
202 VrmlData_ErrorStatus VrmlData_Scene::readLine (VrmlData_InBuffer& theBuffer)
204 VrmlData_ErrorStatus aStatus = VrmlData_StatusOK;
205 if (theBuffer.Input.eof())
206 aStatus = VrmlData_EndOfFile;
208 theBuffer.Input.getline (theBuffer.Line, sizeof(theBuffer.Line));
209 theBuffer.LineCount++;
210 const int stat = theBuffer.Input.rdstate();
211 if (stat & ios::badbit) {
212 aStatus = VrmlData_UnrecoverableError;
214 else if (stat & ios::failbit) {
215 if (stat & ios::eofbit) {
216 aStatus = VrmlData_EndOfFile;
219 aStatus = VrmlData_GeneralError;
222 theBuffer.LinePtr = &theBuffer.Line[0];
223 theBuffer.IsProcessed = Standard_False;
228 //=======================================================================
229 //function : ReadLine
231 //=======================================================================
233 VrmlData_ErrorStatus VrmlData_Scene::ReadLine (VrmlData_InBuffer& theBuffer)
235 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
237 while (aStatus == VrmlData_StatusOK) {
238 // Find the first significant character of the line
239 for (; * theBuffer.LinePtr != '\0'; theBuffer.LinePtr++) {
240 if (* theBuffer.LinePtr != ' ' && * theBuffer.LinePtr != '\t'
241 && * theBuffer.LinePtr != ',')
243 if (* theBuffer.LinePtr == '\n' || * theBuffer.LinePtr == '\r' ||
244 * theBuffer.LinePtr == '#')
245 // go requesting the next line
250 // the line is empty here (no significant characters). Read the next one.
251 aStatus = readLine (theBuffer);
254 // error or EOF detected
258 // Try to detect comment
259 if (theBuffer.IsProcessed == Standard_False) {
260 Standard_Boolean isQuoted (Standard_False);
261 Standard_Integer anOffset (0);
262 char * ptr = theBuffer.LinePtr;
263 for (; * ptr != '\0'; ptr++) {
265 * ptr = ptr[anOffset];
266 if (* ptr == '\n' || * ptr == '\r' || * ptr == '#') {
267 if (isQuoted == Standard_False) {
271 } else if (* ptr == '\\' && isQuoted)
272 ptr[0] = ptr[++anOffset];
273 else if (* ptr == '\"')
274 isQuoted = !isQuoted;
276 theBuffer.IsProcessed = Standard_True;
281 //=======================================================================
282 //function : readHeader
284 //=======================================================================
286 VrmlData_ErrorStatus VrmlData_Scene::readHeader (VrmlData_InBuffer& theBuffer)
288 VrmlData_ErrorStatus aStat = readLine (theBuffer);
289 if (aStat == VrmlData_StatusOK &&
290 !VRMLDATA_LCOMPARE(theBuffer.LinePtr, "#VRML V2.0"))
291 aStat = VrmlData_NotVrmlFile;
293 aStat = readLine(theBuffer);
297 //=======================================================================
298 //function : operator <<
299 //purpose : Import from text stream (file or else)
300 //=======================================================================
302 VrmlData_Scene& VrmlData_Scene::operator << (Standard_IStream& theInput)
304 VrmlData_InBuffer aBuffer (theInput);
306 // Read the VRML header
307 myStatus = readHeader (aBuffer);
308 const Handle(VrmlData_UnknownNode) aNullNode= new VrmlData_UnknownNode(*this);
309 // if (myStatus == StatusOK)
310 // myStatus = ReadLine (aBuffer);
311 // Read VRML data by nodes
313 if (!VrmlData_Node::OK(myStatus, ReadLine(aBuffer))) {
314 if (myStatus == VrmlData_EndOfFile)
315 myStatus = VrmlData_StatusOK;
318 // this line provides the method ReadNode in the present context
319 Handle(VrmlData_Node) aNode;
320 myStatus = aNullNode->ReadNode (aBuffer, aNode);
321 // Unknown nodes are not stored however they do not generate error
322 if (myStatus != VrmlData_StatusOK)
324 if (aNode.IsNull() == Standard_False /*&&
325 !aNode->IsKind (STANDARD_TYPE(VrmlData_UnknownNode))*/)
327 if (aNode->IsKind (STANDARD_TYPE(VrmlData_WorldInfo)) == Standard_False)
328 myLstNodes.Append (aNode);
329 else if (aNode->IsDefault() == Standard_False) {
330 const Handle(VrmlData_WorldInfo) aInfo =
331 Handle(VrmlData_WorldInfo)::DownCast (aNode);
332 myWorldInfo->SetTitle (aInfo->Title());
333 NCollection_List <const char *>::Iterator anIterInfo =
334 aInfo->InfoIterator();
335 for (; anIterInfo.More(); anIterInfo.Next())
336 myWorldInfo->AddInfo (anIterInfo.Value());
340 if (myStatus != VrmlData_StatusOK)
341 myLineError = aBuffer.LineCount;
346 //=======================================================================
347 //function : FindNode
349 //=======================================================================
351 Handle(VrmlData_Node) VrmlData_Scene::FindNode
352 (const char * theName,
353 const Handle(Standard_Type)& /*theType*/) const
355 Handle(VrmlData_Node) aResult;
357 Iterator anIter (myAllNodes);
358 for (; anIter.More(); anIter.Next())
359 if (!strcmp (anIter.Value()->Name(), theName)) {
360 aResult = anIter.Value();
361 if (theType.IsNull())
363 if (aResult->IsKind(theType))
368 const Handle(VrmlData_UnknownNode) aDummyNode = new VrmlData_UnknownNode;
369 aDummyNode->myName = theName;
370 if (myNamedNodes.Contains (aDummyNode))
371 aResult = const_cast<VrmlData_MapOfNode&>(myNamedNodes).Added(aDummyNode);
376 //=======================================================================
377 //function : FindNode
379 //=======================================================================
381 Handle(VrmlData_Node) VrmlData_Scene::FindNode
382 (const char * theName,
383 gp_Trsf& theLocation) const
386 Handle(VrmlData_Node) aResult;
387 Iterator anIter (myLstNodes);
388 for (; anIter.More(); anIter.Next()) {
389 const Handle(VrmlData_Node)& aNode = anIter.Value();
392 // Match a top-level node name
393 if (strcmp(aNode->Name(), theName) == 0) {
398 // Try a Group type of node
399 if (aNode->IsKind(STANDARD_TYPE(VrmlData_Group)))
401 const Handle(VrmlData_Group) aGroup =
402 Handle(VrmlData_Group)::DownCast (aNode);
403 if (aGroup.IsNull() == Standard_False) {
404 aResult = aGroup->FindNode(theName, theLocation);
405 if (aResult.IsNull() == Standard_False)
413 //=======================================================================
414 //function : ReadWord
416 //=======================================================================
418 VrmlData_ErrorStatus VrmlData_Scene::ReadWord
419 (VrmlData_InBuffer& theBuffer,
420 TCollection_AsciiString& theWord)
422 VrmlData_ErrorStatus aStatus = ReadLine(theBuffer);
423 if (aStatus == VrmlData_StatusOK) {
424 char * ptr = theBuffer.LinePtr;
425 while (* ptr != '\0' && * ptr != '\n' && * ptr != '\r' &&
426 * ptr != ' ' && * ptr != '\t' && * ptr != '{' && * ptr != '}' &&
427 * ptr != ',' && * ptr != '[' && * ptr != ']')
429 const Standard_Integer aLen = Standard_Integer(ptr - theBuffer.LinePtr);
431 aStatus = VrmlData_StringInputError;
433 theWord = TCollection_AsciiString ((Standard_CString)theBuffer.LinePtr,
435 theBuffer.LinePtr = ptr;
441 //=======================================================================
442 //function : createNode
444 //=======================================================================
446 VrmlData_ErrorStatus VrmlData_Scene::createNode
447 (VrmlData_InBuffer& theBuffer,
448 Handle(VrmlData_Node)& theNode,
449 const Handle(Standard_Type)& theType)
451 VrmlData_ErrorStatus aStatus;
452 Handle(VrmlData_Node) aNode;
453 TCollection_AsciiString aName;
455 // Read the DEF token to assign the node name
456 if (VrmlData_Node::OK(aStatus, ReadLine(theBuffer))) {
457 if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "DEF")) {
458 if (VrmlData_Node::OK(aStatus, ReadWord (theBuffer, aName)))
459 aStatus = ReadLine(theBuffer);
460 } else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "NULL")) {
466 const char * strName = aName.ToCString();
467 if (aStatus == VrmlData_StatusOK) {
468 // create the new node
469 if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Appearance"))
470 aNode = new VrmlData_Appearance (* this, strName);
471 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Shape"))
472 aNode = new VrmlData_ShapeNode (* this, strName);
473 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Box"))
474 aNode = new VrmlData_Box (* this, strName);
475 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Color"))
476 aNode = new VrmlData_Color (* this, strName);
477 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Cone"))
478 aNode = new VrmlData_Cone (* this, strName);
479 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Coordinate")) {
480 aNode = new VrmlData_Coordinate (* this, strName);
482 // Check for "Coordinate3"
483 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "3"))
486 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Cylinder"))
487 aNode = new VrmlData_Cylinder (* this, strName);
488 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Group"))
489 aNode = new VrmlData_Group (* this, strName,
491 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Transform"))
492 aNode = new VrmlData_Group (* this, strName,
494 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Inline"))
495 aNode = new VrmlData_Group (* this, strName,
497 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Separator"))
498 aNode = new VrmlData_Group (* this, strName,
500 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Switch"))
501 aNode = new VrmlData_Group (* this, strName,
503 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "ImageTexture"))
504 aNode = new VrmlData_ImageTexture (* this, strName);
505 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "IndexedFaceSet"))
506 aNode = new VrmlData_IndexedFaceSet (* this, strName);
507 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "IndexedLineSet"))
508 aNode = new VrmlData_IndexedLineSet (* this, strName);
509 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Material"))
510 aNode = new VrmlData_Material (* this, strName);
511 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Normal"))
512 aNode = new VrmlData_Normal (* this, strName);
513 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Sphere"))
514 aNode = new VrmlData_Sphere (* this, strName);
515 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "TextureCoordinate"))
516 aNode = new VrmlData_TextureCoordinate(* this, strName);
517 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "WorldInfo"))
518 aNode = new VrmlData_WorldInfo (* this, strName);
520 void * isProto = VRMLDATA_LCOMPARE(theBuffer.LinePtr, "PROTO");
521 TCollection_AsciiString aTitle;
522 aStatus = ReadWord (theBuffer, aTitle);
524 aStatus = ReadLine(theBuffer);
525 if (aStatus == VrmlData_StatusOK) {
526 if (theBuffer.LinePtr[0] != '[')
527 aStatus = VrmlData_VrmlFormatError;
530 Standard_Integer aLevelCounter(0);
531 // This loop searches for any opening bracket '['.
532 // Such bracket increments the level counter. A closing bracket decrements
533 // the counter. The loop terminates when the counter becomes negative.
534 while (aLevelCounter >= 0 &&
535 (aStatus = ReadLine(theBuffer)) == VrmlData_StatusOK) {
537 while ((aChar = theBuffer.LinePtr[0]) != '\0') {
542 } else if (aChar == ']') {
551 if (aStatus == VrmlData_StatusOK)
552 aNode = new VrmlData_UnknownNode(* this,
557 aStatus = ReadLine(theBuffer);
558 if (aNode.IsNull() == Standard_False) {
559 if (aNode->Name()[0] != '\0')
560 myNamedNodes.Add (aNode);
561 if (theType.IsNull() == Standard_False)
562 if (aNode->IsKind(theType) == Standard_False)
563 aStatus = VrmlData_VrmlFormatError;
565 if (aStatus == VrmlData_StatusOK) {
566 if (theBuffer.LinePtr[0] == '{') {
569 myAllNodes.Append(aNode);
571 aStatus = VrmlData_VrmlFormatError;
577 //=======================================================================
578 //function : operator TopoDS_Shape
580 //=======================================================================
582 VrmlData_Scene::operator TopoDS_Shape () const
585 VrmlData_Scene::createShape (aShape, myLstNodes, 0L);
589 //=======================================================================
590 //function : GetShape
592 //=======================================================================
594 TopoDS_Shape VrmlData_Scene::GetShape (VrmlData_DataMapOfShapeAppearance& aMap)
597 VrmlData_Scene::createShape (aShape, myLstNodes, &aMap);
601 //=======================================================================
602 //function : createShape
604 //=======================================================================
606 void VrmlData_Scene::createShape
607 (TopoDS_Shape& outShape,
608 const VrmlData_ListOfNode& lstNodes,
609 VrmlData_DataMapOfShapeAppearance* pMapShapeApp)
611 TopoDS_Shape aSingleShape; // used when there is a single ShapeNode
612 Standard_Boolean isSingleShape (Standard_True);
613 BRep_Builder aBuilder;
615 aBuilder.MakeCompound(TopoDS::Compound(outShape));
616 aSingleShape.Orientation(TopAbs_FORWARD);
618 Iterator anIter (lstNodes);
619 for (; anIter.More(); anIter.Next()) {
620 // Try a Shape type of node
621 const Handle(VrmlData_ShapeNode) aNodeShape =
622 Handle(VrmlData_ShapeNode)::DownCast (anIter.Value());
623 if (aNodeShape.IsNull() == Standard_False) {
624 const Handle(VrmlData_Geometry) aNodeGeom =
625 Handle(VrmlData_Geometry)::DownCast(aNodeShape->Geometry());
626 if (aNodeGeom.IsNull() == Standard_False) {
627 if (aSingleShape.IsNull() == Standard_False)
628 isSingleShape = Standard_False;
629 const Handle(TopoDS_TShape) aTShape = aNodeGeom->TShape();
630 aSingleShape.TShape(aTShape);
631 if (aSingleShape.IsNull() == Standard_False) {
632 aBuilder.Add (outShape, aSingleShape);
633 if (pMapShapeApp != 0L) {
634 const Handle(VrmlData_Appearance)& anAppearance =
635 aNodeShape->Appearance();
636 if (anAppearance.IsNull() == Standard_False) {
637 // Check if the current topology is a single face
638 if (aTShape->IsKind(STANDARD_TYPE(TopoDS_TFace)))
639 pMapShapeApp->Bind(aTShape, anAppearance);
641 // This is not a face, explode it in faces and bind each face
642 TopoDS_Shape aCurShape;
643 aCurShape.TShape(aTShape);
644 TopExp_Explorer anExp(aCurShape, TopAbs_FACE);
645 for (; anExp.More(); anExp.Next()) {
646 const TopoDS_Face& aFace = TopoDS::Face(anExp.Current());
647 pMapShapeApp->Bind(aFace.TShape(), anAppearance);
656 // Try a Group type of node
657 const Handle(VrmlData_Group) aNodeGroup =
658 Handle(VrmlData_Group)::DownCast (anIter.Value());
659 if (aNodeGroup.IsNull() == Standard_False) {
661 aNodeGroup->Shape(aShape, pMapShapeApp);
662 if (aShape.IsNull() == Standard_False) {
663 aBuilder.Add (outShape, aShape);
664 isSingleShape = Standard_False;
669 outShape = aSingleShape;
672 //=======================================================================
673 //function : ReadReal
675 //=======================================================================
677 VrmlData_ErrorStatus VrmlData_Scene::ReadReal
678 (VrmlData_InBuffer& theBuffer,
679 Standard_Real& theResult,
680 Standard_Boolean isScale,
681 Standard_Boolean isOnlyPositive) const
683 Standard_Real aResult(0.);
684 VrmlData_ErrorStatus aStatus;
685 if (VrmlData_Node::OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
687 aResult = Strtod (theBuffer.LinePtr, &endptr);
688 if (endptr == theBuffer.LinePtr)
689 aStatus = VrmlData_NumericInputError;
690 else if (isOnlyPositive && aResult < 0.001*Precision::Confusion())
691 aStatus = VrmlData_IrrelevantNumber;
693 theResult = isScale ? (aResult * myLinearScale) : aResult;
694 theBuffer.LinePtr = endptr;
700 //=======================================================================
703 //=======================================================================
705 VrmlData_ErrorStatus VrmlData_Scene::ReadXYZ
706 (VrmlData_InBuffer& theBuffer,
708 Standard_Boolean isScale,
709 Standard_Boolean isOnlyPos) const
711 Standard_Real aVal[3] = {0., 0., 0.};
712 VrmlData_ErrorStatus aStatus = VrmlData_StatusOK;
713 for (Standard_Integer i = 0; i < 3; i++) {
714 if (!VrmlData_Node::OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
717 aVal[i] = Strtod (theBuffer.LinePtr, &endptr);
718 if (endptr == theBuffer.LinePtr) {
719 aStatus = VrmlData_NumericInputError;
722 if (isOnlyPos && aVal[i] < 0.001*Precision::Confusion()) {
723 aStatus = VrmlData_IrrelevantNumber;
726 theBuffer.LinePtr = endptr;
729 if (aStatus == VrmlData_StatusOK) {
731 theXYZ.SetCoord (aVal[0] * myLinearScale,
732 aVal[1] * myLinearScale,
733 aVal[2] * myLinearScale);
736 theXYZ.SetCoord (aVal[0], aVal[1], aVal[2]);
742 //=======================================================================
745 //=======================================================================
747 VrmlData_ErrorStatus VrmlData_Scene::ReadXY
748 (VrmlData_InBuffer& theBuffer,
750 Standard_Boolean isScale,
751 Standard_Boolean isOnlyPos) const
753 Standard_Real aVal[2] = {0., 0.};
754 VrmlData_ErrorStatus aStatus = VrmlData_StatusOK;
755 for (Standard_Integer i = 0; i < 2; i++) {
756 if (!VrmlData_Node::OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
759 aVal[i] = Strtod (theBuffer.LinePtr, &endptr);
760 if (endptr == theBuffer.LinePtr) {
761 aStatus = VrmlData_NumericInputError;
764 if (isOnlyPos && aVal[i] < 0.001*Precision::Confusion()) {
765 aStatus = VrmlData_IrrelevantNumber;
768 theBuffer.LinePtr = endptr;
771 if (aStatus == VrmlData_StatusOK) {
773 theXY.SetCoord (aVal[0] * myLinearScale, aVal[1] * myLinearScale);
775 theXY.SetCoord (aVal[0], aVal[1]);
780 //=======================================================================
781 //function : ReadArrIndex
782 //purpose : Read the body of the data node (comma-separated list of int
784 //=======================================================================
786 VrmlData_ErrorStatus VrmlData_Scene::ReadArrIndex
787 (VrmlData_InBuffer& theBuffer,
788 const Standard_Integer **& theArray,
789 Standard_Size& theNBlocks) const
791 VrmlData_ErrorStatus aStatus;
793 if (VrmlData_Node::OK(aStatus, ReadLine(theBuffer))) {
794 if (theBuffer.LinePtr[0] != '[') // opening bracket
795 aStatus = VrmlData_VrmlFormatError;
798 NCollection_Vector<const Standard_Integer *> vecIndice;
799 NCollection_Vector<Standard_Integer> vecInt;
800 Standard_Boolean isMore (Standard_True);
803 // Loop reading integers from the stream
804 while (isMore && VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
806 // closing bracket, in case that it follows a comma
807 if (theBuffer.LinePtr[0] == ']') {
811 if (!VrmlData_Node::OK(aStatus, VrmlData_Node::ReadInteger(theBuffer,
814 // Check for valid delimiter (']' or ',')
815 if (!VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
817 if (theBuffer.LinePtr[0] == ']') {
819 isMore = Standard_False;
822 // The input value is a node index, store it in the buffer vector
823 vecInt.Append (static_cast<Standard_Integer> (anIntValue));
824 if ((anIntValue < 0 || isMore == Standard_False)
825 && vecInt.Length() > 0)
827 const Standard_Integer aLen = vecInt.Length();
828 // The input is the end-of-face, store and close this face
829 Standard_Integer * bufFace = static_cast <Standard_Integer *>
830 (myAllocator->Allocate((aLen+1) * sizeof(Standard_Integer)));
832 aStatus = VrmlData_UnrecoverableError;
836 for (Standard_Integer i = 0; i < aLen; i++)
837 bufFace[i+1] = vecInt(i);
839 vecIndice.Append(bufFace);
842 if (aStatus == VrmlData_StatusOK) {
843 const Standard_Size aNbBlocks =
844 static_cast <Standard_Size> (vecIndice.Length());
846 const Standard_Integer ** anArray =
847 static_cast <const Standard_Integer **>
848 (myAllocator->Allocate (aNbBlocks * sizeof(Standard_Integer *)));
850 aStatus = VrmlData_UnrecoverableError;
852 for (size_t i = 0; i < aNbBlocks; i++)
853 anArray[i] = vecIndice((Standard_Integer)i);
854 theNBlocks = aNbBlocks;
864 //=======================================================================
865 //function : writeArrIndex
867 //=======================================================================
869 VrmlData_ErrorStatus VrmlData_Scene::WriteArrIndex
870 (const char * thePrefix,
871 const Standard_Integer ** theArrIndex,
872 const Standard_Size theNbBlocks) const
874 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
875 if (theNbBlocks && (IsDummyWrite() == Standard_False)) {
876 if (VrmlData_Node::OK (aStatus,
877 WriteLine (thePrefix, "[", 1)))
879 const size_t aLineLimit = (myCurrentIndent < 41) ? 36 : 100;
881 for (Standard_Size iBlock = 0; iBlock < theNbBlocks; iBlock++) {
882 const Standard_Integer nVal (* theArrIndex[iBlock]);
883 const Standard_Integer * arrVal = theArrIndex[iBlock]+1;
886 Sprintf (buf, "%d,", arrVal[0]);
889 Sprintf (buf, "%d,%d,", arrVal[0], arrVal[1]);
892 Sprintf (buf, "%d,%d,%d,", arrVal[0], arrVal[1], arrVal[2]);
895 Sprintf (buf, "%d,%d,%d,%d,",
896 arrVal[0], arrVal[1], arrVal[2], arrVal[3]);
900 char * ptr = &buf[0];
901 for (Standard_Integer i = 0; i < nVal; i++) {
902 Sprintf (ptr, "%d,", arrVal[i]);
905 ptr = strchr (ptr, ',') + 1;
906 if ((ptr - &buf[0]) > (ptrdiff_t)aLineLimit) {
913 WriteLine (buf, iBlock < theNbBlocks-1 ? "-1," : "-1");
915 if (aStatus == VrmlData_StatusOK)
916 aStatus = WriteLine ("]", 0L, -1);
922 //=======================================================================
923 //function : WriteXYZ
925 //=======================================================================
927 VrmlData_ErrorStatus VrmlData_Scene::WriteXYZ
928 (const gp_XYZ& theXYZ,
929 const Standard_Boolean isApplyScale,
930 const char * thePostfix) const
933 if (IsDummyWrite() == Standard_False) {
934 if (isApplyScale && myLinearScale > Precision::Confusion())
935 Sprintf (buf, "%.12g %.12g %.12g%s", theXYZ.X() / myLinearScale,
936 theXYZ.Y() / myLinearScale, theXYZ.Z() / myLinearScale,
937 thePostfix ? thePostfix : "");
939 Sprintf (buf, "%.12g %.12g %.12g%s", theXYZ.X(), theXYZ.Y(), theXYZ.Z(),
940 thePostfix ? thePostfix : "");
942 return WriteLine (buf);
945 //=======================================================================
946 //function : WriteLine
947 //purpose : write the given string prepending the current indentation
948 //=======================================================================
950 VrmlData_ErrorStatus VrmlData_Scene::WriteLine
951 (const char * theLin0,
952 const char * theLin1,
953 const Standard_Integer theIndent) const
955 static const char spaces[] = " "
957 VrmlData_ErrorStatus& aStatus =
958 const_cast <VrmlData_ErrorStatus&> (myStatus);
960 aStatus = VrmlData_StatusOK;
962 Standard_Integer& aCurrentIndent =
963 const_cast <Standard_Integer&> (myCurrentIndent);
965 aCurrentIndent -= myIndent;
966 if (aCurrentIndent < 0)
968 if (theLin0 == 0L && theLin1 == 0L)
969 (* myOutput) << "\n";
971 const Standard_Integer nSpaces = Min (aCurrentIndent, sizeof(spaces)-1);
972 (* myOutput) << &spaces[sizeof(spaces)-1 - nSpaces];
974 (* myOutput) << theLin0;
976 (* myOutput) << " " << theLin1;
978 (* myOutput) << theLin1;
979 (* myOutput) << "\n";
981 const int stat = myOutput->rdstate();
982 if (stat & ios::badbit)
983 aStatus = VrmlData_UnrecoverableError;
984 else if (stat & ios::failbit)
985 // if (stat & ios::eofbit)
986 // aStatus = VrmlData_EndOfFile;
988 aStatus = VrmlData_GeneralError;
990 aCurrentIndent += myIndent;
995 //=======================================================================
996 //function : WriteNode
998 //=======================================================================
1000 VrmlData_ErrorStatus VrmlData_Scene::WriteNode
1001 (const char * thePrefix,
1002 const Handle(VrmlData_Node)& theNode) const
1004 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
1005 Standard_Boolean isNoName (Standard_False);
1006 if (theNode->Name() == 0L)
1007 isNoName = Standard_True;
1008 else if (theNode->Name()[0] == '\0')
1009 isNoName = Standard_True;
1011 if (theNode.IsNull() == Standard_False)
1012 if (theNode->IsDefault() == Standard_False) {
1013 if (isNoName && IsDummyWrite()) {
1014 // We are in a tentative 'write' session (nothing is written).
1015 // The goal is to identify multiply referred nodes.
1016 Standard_Address addrNode = theNode.operator->();
1017 if (!const_cast<NCollection_Map<Standard_Address>&>(myUnnamedNodesOut)
1020 Handle(VrmlData_UnknownNode) bidNode = new VrmlData_UnknownNode;
1023 Sprintf (buf, "_%d",
1024 ++const_cast<Standard_Integer&>(myAutoNameCounter));
1025 bidNode->myName = &buf[0];
1026 } while (myNamedNodes.Contains (bidNode));
1027 // We found the vacant automatic name, let us assign to it.
1028 theNode->setName (&buf[0]);
1029 const_cast<VrmlData_MapOfNode&>(myNamedNodes).Add (theNode);
1030 return aStatus; // do not search under already duplicated node
1034 aStatus = theNode->Write (thePrefix);
1036 // If the node name consists of blank characters, we do not write it
1037 const char * nptr = theNode->Name();
1038 for (; * nptr != '\0'; nptr++)
1039 if (* nptr != ' ' && * nptr != '\t')
1042 aStatus = theNode->Write (thePrefix);
1044 // Name is written under DEF clause
1045 TCollection_AsciiString buf;
1046 if (myNamedNodesOut.Contains (theNode))
1049 buf += theNode->Name();
1050 aStatus = WriteLine (thePrefix, buf.ToCString());
1060 buf += theNode->Name();
1061 aStatus = theNode->Write (buf.ToCString());
1062 const_cast<VrmlData_MapOfNode&>(myNamedNodesOut).Add (theNode);
1070 //=======================================================================
1073 //=======================================================================
1075 void VrmlData_Scene::Dump (Standard_OStream& theStream) const
1077 theStream << " ===== Diagnostic Dump of a Scene (" << myAllNodes.Extent()
1081 Iterator anIterA(myAllNodes);
1082 for (; anIterA.More(); anIterA.Next())
1083 dumpNode(theStream, anIterA.Value(), "");
1085 Iterator anIter(myLstNodes);
1086 for (; anIter.More(); anIter.Next())
1087 dumpNode(theStream, anIter.Value(), " ");
1090 //=======================================================================
1091 //function : dumpNode
1092 //purpose : static (local) function
1093 //=======================================================================
1095 void dumpNode (Standard_OStream& theStream,
1096 const Handle(VrmlData_Node)& theNode,
1097 const TCollection_AsciiString& theIndent)
1099 if (theNode.IsNull())
1101 TCollection_AsciiString aNewIndent =
1102 theIndent.IsEmpty() ? theIndent : theIndent + " ";
1103 if (theNode->IsKind(STANDARD_TYPE(VrmlData_Appearance))) {
1104 const Handle(VrmlData_Appearance) anAppearance =
1105 Handle(VrmlData_Appearance)::DownCast (theNode);
1106 dumpNodeHeader (theStream, theIndent, "Appearance", theNode->Name());
1107 if (theIndent.IsEmpty() == Standard_False) {
1108 dumpNode (theStream, anAppearance->Material(), aNewIndent);
1109 dumpNode (theStream, anAppearance->Texture(), aNewIndent);
1110 dumpNode (theStream, anAppearance->TextureTransform(), aNewIndent);
1112 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_ShapeNode))) {
1113 const Handle(VrmlData_ShapeNode) aShape =
1114 Handle(VrmlData_ShapeNode)::DownCast (theNode);
1115 dumpNodeHeader (theStream, theIndent, "Shape", theNode->Name());
1116 if (theIndent.IsEmpty() == Standard_False) {
1117 dumpNode (theStream, aShape->Appearance(), aNewIndent);
1118 dumpNode (theStream, aShape->Geometry(), aNewIndent);
1120 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Box)))
1121 dumpNodeHeader (theStream, theIndent, "Box", theNode->Name());
1122 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Cylinder)))
1123 dumpNodeHeader (theStream, theIndent, "Cylinder", theNode->Name());
1124 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Sphere)))
1125 dumpNodeHeader (theStream, theIndent, "Sphere", theNode->Name());
1126 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Cone)))
1127 dumpNodeHeader (theStream, theIndent, "Cone", theNode->Name());
1128 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Coordinate)))
1129 dumpNodeHeader (theStream, theIndent, "Coordinate", theNode->Name());
1130 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Group))) {
1131 const Handle(VrmlData_Group) aGroup =
1132 Handle(VrmlData_Group)::DownCast (theNode);
1134 Sprintf (buf, "Group (%s)",
1135 aGroup->IsTransform() ? "Transform" : "Group");
1136 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1137 if (theIndent.IsEmpty() == Standard_False) {
1138 VrmlData_ListOfNode::Iterator anIter = aGroup->NodeIterator();
1139 for (; anIter.More(); anIter.Next())
1140 dumpNode (theStream, anIter.Value(), aNewIndent);
1142 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_ImageTexture)))
1143 dumpNodeHeader (theStream, theIndent, "ImageTexture", theNode->Name());
1144 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_IndexedFaceSet))) {
1145 const Handle(VrmlData_IndexedFaceSet) aNode =
1146 Handle(VrmlData_IndexedFaceSet)::DownCast(theNode);
1147 const Standard_Integer ** ppDummy;
1148 const Standard_Size nCoord = aNode->Coordinates()->Length();
1149 const Standard_Size nPoly = aNode->Polygons (ppDummy);
1151 Sprintf (buf, "IndexedFaceSet (%" PRIuPTR " vertices, %" PRIuPTR " polygons)",
1154 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1155 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_IndexedLineSet))) {
1156 const Handle(VrmlData_IndexedLineSet) aNode =
1157 Handle(VrmlData_IndexedLineSet)::DownCast(theNode);
1158 const Standard_Integer ** ppDummy;
1159 const Standard_Size nCoord = aNode->Coordinates()->Length();
1160 const Standard_Size nPoly = aNode->Polygons (ppDummy);
1163 Sprintf(buf, "IndexedLineSet (%" PRIuPTR " vertices, %" PRIuPTR " polygons)",
1166 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1167 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Material))) {
1168 // const Handle(VrmlData_Material) aMaterial =
1169 // Handle(VrmlData_Material)::DownCast (theNode);
1170 dumpNodeHeader (theStream, theIndent, "Material", theNode->Name());
1172 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Normal)))
1173 dumpNodeHeader (theStream, theIndent, "Normal", theNode->Name());
1174 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_TextureCoordinate)))
1175 dumpNodeHeader (theStream, theIndent, "TextureCoordinate", theNode->Name());
1176 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_WorldInfo)))
1177 dumpNodeHeader (theStream, theIndent, "WorldInfo", theNode->Name());
1178 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_UnknownNode))) {
1179 const Handle(VrmlData_UnknownNode) anUnknown =
1180 Handle(VrmlData_UnknownNode)::DownCast (theNode);
1182 Sprintf (buf, "Unknown (%s)", anUnknown->GetTitle().ToCString());
1183 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1187 //=======================================================================
1188 //function : dumpNodeHeader
1190 //=======================================================================
1192 void dumpNodeHeader (Standard_OStream& theStream,
1193 const TCollection_AsciiString& theIndent,
1194 const char * theType,
1195 const char * theName)
1197 theStream << theIndent << theType << " node";
1198 if (theName[0] == '\0')
1201 theStream << ": \"" << theName << "\"\n";