1 // File: VrmlData_Scene.cxx
2 // Created: 25.05.06 16:33:25
3 // Author: Alexander GRIGORIEV
4 // Copyright: Open Cascade 2006
6 #include <VrmlData_Scene.hxx>
7 #include <VrmlData_InBuffer.hxx>
8 #include <VrmlData_Appearance.hxx>
9 #include <VrmlData_Box.hxx>
10 #include <VrmlData_Color.hxx>
11 #include <VrmlData_Cone.hxx>
12 #include <VrmlData_Coordinate.hxx>
13 #include <VrmlData_Cylinder.hxx>
14 #include <VrmlData_DataMapOfShapeAppearance.hxx>
15 #include <VrmlData_Group.hxx>
16 #include <VrmlData_ImageTexture.hxx>
17 #include <VrmlData_InBuffer.hxx>
18 #include <VrmlData_IndexedFaceSet.hxx>
19 #include <VrmlData_IndexedLineSet.hxx>
20 #include <VrmlData_Material.hxx>
21 #include <VrmlData_Normal.hxx>
22 #include <VrmlData_Scene.hxx>
23 #include <VrmlData_ShapeNode.hxx>
24 #include <VrmlData_Sphere.hxx>
25 #include <VrmlData_TextureCoordinate.hxx>
26 #include <VrmlData_UnknownNode.hxx>
27 //#include <VrmlData_WorldInfo.hxx>
28 #include <NCollection_Vector.hxx>
29 #include <TopoDS_TFace.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <BRep_Builder.hxx>
34 #include <Precision.hxx>
37 #define _CRT_SECURE_NO_DEPRECATE
38 #pragma warning (disable:4996)
41 static void dumpNode (Standard_OStream& theStream,
42 const Handle(VrmlData_Node)& theNode,
43 const TCollection_AsciiString& theIndent);
45 static void dumpNodeHeader (Standard_OStream& theStream,
46 const TCollection_AsciiString& theIndent,
48 const char * theName);
50 //=======================================================================
51 //function : VrmlData_Scene
52 //purpose : Constructor
53 //=======================================================================
55 VrmlData_Scene::VrmlData_Scene
56 (const Handle(NCollection_IncAllocator)& theAlloc)
58 myStatus (VrmlData_StatusOK),
59 myAllocator (theAlloc.IsNull() ?
60 new NCollection_IncAllocator : theAlloc.operator->()),
67 myWorldInfo = new VrmlData_WorldInfo (* this);
68 myWorldInfo->AddInfo ("Created by OPEN CASCADE (tm) VrmlData API");
69 myLstNodes.Append (myWorldInfo);
70 myAllNodes.Append (myWorldInfo);
73 //=======================================================================
76 //=======================================================================
78 const Handle(VrmlData_Node)& VrmlData_Scene::AddNode
79 (const Handle(VrmlData_Node)& theN,
80 const Standard_Boolean isTopLevel)
82 if (theN.IsNull() == Standard_False)
83 if (theN->IsKind (STANDARD_TYPE(VrmlData_WorldInfo)) == Standard_False) {
85 const Handle(VrmlData_Node)& aNode =
86 myAllNodes.Append ((&theN->Scene()== this) ? theN : theN->Clone (NULL));
87 // Name is checked for uniqueness. If not, letter 'D' is appended until
88 // the name proves to be unique.
89 if (aNode->Name()[0] != '\0')
90 while (myNamedNodes.Add (aNode) == Standard_False)
91 aNode->setName (aNode->Name(), "D");
93 myLstNodes.Append (aNode);
97 static Handle(VrmlData_Node) aNullNode;
102 //=======================================================================
103 //function : operator <<
104 //purpose : Export to text stream (file or else)
105 //=======================================================================
107 Standard_OStream& operator << (Standard_OStream& theOutput,
108 const VrmlData_Scene& theScene)
110 VrmlData_Scene& aScene = const_cast <VrmlData_Scene&> (theScene);
111 aScene.myMutex.Lock();
112 aScene.myCurrentIndent = 0;
113 aScene.myLineError = 0;
114 aScene.myOutput = 0L;
115 aScene.myNamedNodesOut.Clear();
116 aScene.myUnnamedNodesOut.Clear();
117 aScene.myAutoNameCounter = 0;
121 VrmlData_Scene::Iterator anIterD(aScene.myLstNodes);
122 for (; anIterD.More(); anIterD.Next()) {
123 const Handle(VrmlData_Node)& aNode = anIterD.Value();
124 if (aNode.IsNull() == Standard_False) {
125 const VrmlData_ErrorStatus aStatus = aScene.WriteNode (0L, aNode);
126 if (aStatus != VrmlData_StatusOK &&
127 aStatus != VrmlData_NotImplemented)
132 aScene.myOutput = &theOutput;
133 aScene.myNamedNodesOut.Clear();
134 theOutput << "#VRML V2.0 utf8" << endl << endl;
138 VrmlData_Scene::Iterator anIter(aScene.myLstNodes);
139 for (; anIter.More(); anIter.Next()) {
140 const Handle(VrmlData_Node)& aNode = anIter.Value();
141 if (aNode.IsNull() == Standard_False) {
142 const VrmlData_ErrorStatus aStatus = aScene.WriteNode (0L, aNode);
143 if (aStatus != VrmlData_StatusOK &&
144 aStatus != VrmlData_NotImplemented)
148 aScene.myOutput = 0L;
149 aScene.myNamedNodesOut.Clear();
150 aScene.myUnnamedNodesOut.Clear();
151 aScene.myMutex.Unlock();
155 //=======================================================================
156 //function : SetVrmlDir
158 //=======================================================================
160 void VrmlData_Scene::SetVrmlDir (const TCollection_ExtendedString& theDir)
162 TCollection_ExtendedString& aDir = myVrmlDir.Append (theDir);
163 const Standard_ExtCharacter aTerminator = aDir.Value(aDir.Length());
164 if (aTerminator != Standard_ExtCharacter('\\') &&
165 aTerminator != Standard_ExtCharacter('/'))
167 aDir += TCollection_ExtendedString ("\\");
169 aDir += TCollection_ExtendedString ("/");
173 //=======================================================================
174 //function : WorldInfo
176 //=======================================================================
178 const Handle_VrmlData_WorldInfo& VrmlData_Scene::WorldInfo() const
183 //=======================================================================
184 //function : readLine
186 //=======================================================================
188 VrmlData_ErrorStatus VrmlData_Scene::readLine (VrmlData_InBuffer& theBuffer)
190 VrmlData_ErrorStatus aStatus = VrmlData_StatusOK;
191 if (theBuffer.Input.eof())
192 aStatus = VrmlData_EndOfFile;
194 theBuffer.Input.getline (theBuffer.Line, sizeof(theBuffer.Line));
195 theBuffer.LineCount++;
196 const int stat = theBuffer.Input.rdstate();
197 if (stat & ios::badbit)
198 aStatus = VrmlData_UnrecoverableError;
199 else if (stat & ios::failbit)
200 if (stat & ios::eofbit)
201 aStatus = VrmlData_EndOfFile;
203 aStatus = VrmlData_GeneralError;
204 theBuffer.LinePtr = &theBuffer.Line[0];
205 theBuffer.IsProcessed = Standard_False;
210 //=======================================================================
211 //function : ReadLine
213 //=======================================================================
215 VrmlData_ErrorStatus VrmlData_Scene::ReadLine (VrmlData_InBuffer& theBuffer)
217 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
219 while (aStatus == VrmlData_StatusOK) {
220 // Find the first significant character of the line
221 for (; * theBuffer.LinePtr != '\0'; theBuffer.LinePtr++) {
222 if (* theBuffer.LinePtr != ' ' && * theBuffer.LinePtr != '\t'
223 && * theBuffer.LinePtr != ',')
225 if (* theBuffer.LinePtr == '\n' || * theBuffer.LinePtr == '\r' ||
226 * theBuffer.LinePtr == '#')
227 // go requesting the next line
232 // the line is empty here (no significant characters). Read the next one.
233 aStatus = readLine (theBuffer);
236 // error or EOF detected
240 // Try to detect comment
241 if (theBuffer.IsProcessed == Standard_False) {
242 Standard_Boolean isQuoted (Standard_False);
243 Standard_Integer anOffset (0);
244 char * ptr = theBuffer.LinePtr;
245 for (; * ptr != '\0'; ptr++) {
247 * ptr = ptr[anOffset];
248 if (* ptr == '\n' || * ptr == '\r' || * ptr == '#') {
249 if (isQuoted == Standard_False) {
253 } else if (* ptr == '\\' && isQuoted)
254 ptr[0] = ptr[++anOffset];
255 else if (* ptr == '\"')
256 isQuoted = !isQuoted;
258 theBuffer.IsProcessed = Standard_True;
263 //=======================================================================
264 //function : readHeader
266 //=======================================================================
268 VrmlData_ErrorStatus VrmlData_Scene::readHeader (VrmlData_InBuffer& theBuffer)
270 VrmlData_ErrorStatus aStat = readLine (theBuffer);
271 if (aStat == VrmlData_StatusOK &&
272 !VRMLDATA_LCOMPARE(theBuffer.LinePtr, "#VRML V2.0"))
273 aStat = VrmlData_NotVrmlFile;
275 aStat = readLine(theBuffer);
279 //=======================================================================
280 //function : operator <<
281 //purpose : Import from text stream (file or else)
282 //=======================================================================
284 VrmlData_Scene& VrmlData_Scene::operator << (Standard_IStream& theInput)
286 VrmlData_InBuffer aBuffer (theInput);
288 // Read the VRML header
289 myStatus = readHeader (aBuffer);
290 const Handle(VrmlData_UnknownNode) aNullNode= new VrmlData_UnknownNode(*this);
291 // if (myStatus == StatusOK)
292 // myStatus = ReadLine (aBuffer);
293 // Read VRML data by nodes
295 if (!VrmlData_Node::OK(myStatus, ReadLine(aBuffer))) {
296 if (myStatus == VrmlData_EndOfFile)
297 myStatus = VrmlData_StatusOK;
300 // this line provides the method ReadNode in the present context
301 Handle(VrmlData_Node) aNode;
302 myStatus = aNullNode->ReadNode (aBuffer, aNode);
303 // Unknown nodes are not stored however they do not generate error
304 if (myStatus != VrmlData_StatusOK)
306 if (aNode.IsNull() == Standard_False /*&&
307 !aNode->IsKind (STANDARD_TYPE(VrmlData_UnknownNode))*/)
309 if (aNode->IsKind (STANDARD_TYPE(VrmlData_WorldInfo)) == Standard_False)
310 myLstNodes.Append (aNode);
311 else if (aNode->IsDefault() == Standard_False) {
312 const Handle(VrmlData_WorldInfo) aInfo =
313 Handle(VrmlData_WorldInfo)::DownCast (aNode);
314 myWorldInfo->SetTitle (aInfo->Title());
315 NCollection_List <const char *>::Iterator anIterInfo =
316 aInfo->InfoIterator();
317 for (; anIterInfo.More(); anIterInfo.Next())
318 myWorldInfo->AddInfo (anIterInfo.Value());
322 if (myStatus != VrmlData_StatusOK)
323 myLineError = aBuffer.LineCount;
328 //=======================================================================
329 //function : FindNode
331 //=======================================================================
333 Handle(VrmlData_Node) VrmlData_Scene::FindNode
334 (const char * theName,
335 const Handle(Standard_Type)& theType) const
337 Handle(VrmlData_Node) aResult;
339 Iterator anIter (myAllNodes);
340 for (; anIter.More(); anIter.Next())
341 if (!strcmp (anIter.Value()->Name(), theName)) {
342 aResult = anIter.Value();
343 if (theType.IsNull())
345 if (aResult->IsKind(theType))
350 const Handle(VrmlData_UnknownNode) aDummyNode = new VrmlData_UnknownNode;
351 aDummyNode->myName = theName;
352 if (myNamedNodes.Contains (aDummyNode))
353 aResult = const_cast<VrmlData_MapOfNode&>(myNamedNodes).Added(aDummyNode);
358 //=======================================================================
359 //function : FindNode
361 //=======================================================================
363 Handle(VrmlData_Node) VrmlData_Scene::FindNode
364 (const char * theName,
365 gp_Trsf& theLocation) const
368 Handle(VrmlData_Node) aResult;
369 Iterator anIter (myLstNodes);
370 for (; anIter.More(); anIter.Next()) {
371 const Handle(VrmlData_Node)& aNode = anIter.Value();
374 // Match a top-level node name
375 if (strcmp(aNode->Name(), theName) == 0) {
380 // Try a Group type of node
381 if (aNode->IsKind(STANDARD_TYPE(VrmlData_Group)))
383 const Handle(VrmlData_Group) aGroup =
384 Handle(VrmlData_Group)::DownCast (aNode);
385 if (aGroup.IsNull() == Standard_False) {
386 aResult = aGroup->FindNode(theName, theLocation);
387 if (aResult.IsNull() == Standard_False)
395 //=======================================================================
396 //function : ReadWord
398 //=======================================================================
400 VrmlData_ErrorStatus VrmlData_Scene::ReadWord
401 (VrmlData_InBuffer& theBuffer,
402 TCollection_AsciiString& theWord)
404 VrmlData_ErrorStatus aStatus = ReadLine(theBuffer);
405 if (aStatus == VrmlData_StatusOK) {
406 char * ptr = theBuffer.LinePtr;
407 while (* ptr != '\0' && * ptr != '\n' && * ptr != '\r' &&
408 * ptr != ' ' && * ptr != '\t' && * ptr != '{' && * ptr != '}' &&
409 * ptr != ',' && * ptr != '[' && * ptr != ']')
411 const Standard_Integer aLen = Standard_Integer(ptr - theBuffer.LinePtr);
413 aStatus = VrmlData_StringInputError;
415 theWord = TCollection_AsciiString ((Standard_CString)theBuffer.LinePtr,
417 theBuffer.LinePtr = ptr;
423 //=======================================================================
424 //function : createNode
426 //=======================================================================
428 VrmlData_ErrorStatus VrmlData_Scene::createNode
429 (VrmlData_InBuffer& theBuffer,
430 Handle(VrmlData_Node)& theNode,
431 const Handle(Standard_Type)& theType)
433 VrmlData_ErrorStatus aStatus;
434 Handle(VrmlData_Node) aNode;
435 TCollection_AsciiString aName;
436 Standard_Boolean isReused (Standard_False);
438 // Read the DEF token to assign the node name
439 if (VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
440 if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "DEF")) {
441 if (VrmlData_Node::OK(aStatus, ReadWord (theBuffer, aName)))
442 aStatus = ReadLine(theBuffer);
443 } else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "NULL")) {
448 const char * strName = aName.ToCString();
449 if (aStatus == VrmlData_StatusOK) {
450 // create the new node
451 if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Appearance"))
452 aNode = new VrmlData_Appearance (* this, strName);
453 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Shape"))
454 aNode = new VrmlData_ShapeNode (* this, strName);
455 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Box"))
456 aNode = new VrmlData_Box (* this, strName);
457 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Color"))
458 aNode = new VrmlData_Color (* this, strName);
459 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Cone"))
460 aNode = new VrmlData_Cone (* this, strName);
461 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Coordinate"))
462 aNode = new VrmlData_Coordinate (* this, strName);
463 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Cylinder"))
464 aNode = new VrmlData_Cylinder (* this, strName);
465 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Group"))
466 aNode = new VrmlData_Group (* this, strName,
468 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Transform"))
469 aNode = new VrmlData_Group (* this, strName,
471 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Inline"))
472 aNode = new VrmlData_Group (* this, strName,
474 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "ImageTexture"))
475 aNode = new VrmlData_ImageTexture (* this, strName);
476 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "IndexedFaceSet"))
477 aNode = new VrmlData_IndexedFaceSet (* this, strName);
478 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "IndexedLineSet"))
479 aNode = new VrmlData_IndexedLineSet (* this, strName);
480 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Material"))
481 aNode = new VrmlData_Material (* this, strName);
482 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Normal"))
483 aNode = new VrmlData_Normal (* this, strName);
484 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Sphere"))
485 aNode = new VrmlData_Sphere (* this, strName);
486 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "TextureCoordinate"))
487 aNode = new VrmlData_TextureCoordinate(* this, strName);
488 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "WorldInfo"))
489 aNode = new VrmlData_WorldInfo (* this, strName);
491 void * isProto = VRMLDATA_LCOMPARE(theBuffer.LinePtr, "PROTO");
492 TCollection_AsciiString aTitle;
493 aStatus = ReadWord (theBuffer, aTitle);
495 aStatus = ReadLine(theBuffer);
496 if (aStatus == VrmlData_StatusOK)
497 if (theBuffer.LinePtr[0] != '[')
498 aStatus = VrmlData_VrmlFormatError;
501 Standard_Integer aLevelCounter(0);
502 // This loop searches for any opening bracket '['.
503 // Such bracket increments the level counter. A closing bracket decrements
504 // the counter. The loop terminates when the counter becomes negative.
505 while (aLevelCounter >= 0 &&
506 (aStatus = ReadLine(theBuffer)) == VrmlData_StatusOK) {
508 while ((aChar = theBuffer.LinePtr[0]) != '\0') {
513 } else if (aChar == ']') {
521 if (aStatus == VrmlData_StatusOK)
522 aNode = new VrmlData_UnknownNode(* this,
527 aStatus = ReadLine(theBuffer);
528 if (aNode.IsNull() == Standard_False) {
529 if (aNode->Name()[0] != '\0')
530 myNamedNodes.Add (aNode);
531 if (theType.IsNull() == Standard_False)
532 if (aNode->IsKind(theType) == Standard_False)
533 aStatus = VrmlData_VrmlFormatError;
535 aNode->myLineCount = theBuffer.LineCount;
538 if (aStatus == VrmlData_StatusOK)
539 if (theBuffer.LinePtr[0] == '{') {
542 myAllNodes.Append(aNode);
544 aStatus = VrmlData_VrmlFormatError;
548 //=======================================================================
549 //function : operator TopoDS_Shape
551 //=======================================================================
553 VrmlData_Scene::operator TopoDS_Shape () const
556 VrmlData_Scene::createShape (aShape, myLstNodes, 0L);
560 //=======================================================================
561 //function : GetShape
563 //=======================================================================
565 TopoDS_Shape VrmlData_Scene::GetShape (VrmlData_DataMapOfShapeAppearance& aMap)
568 VrmlData_Scene::createShape (aShape, myLstNodes, &aMap);
572 //=======================================================================
573 //function : createShape
575 //=======================================================================
577 void VrmlData_Scene::createShape
578 (TopoDS_Shape& outShape,
579 const VrmlData_ListOfNode& lstNodes,
580 VrmlData_DataMapOfShapeAppearance* pMapShapeApp)
582 TopoDS_Shape aSingleShape; // used when there is a single ShapeNode
583 Standard_Boolean isSingleShape (Standard_True);
584 BRep_Builder aBuilder;
586 aBuilder.MakeCompound(TopoDS::Compound(outShape));
587 aSingleShape.Orientation(TopAbs_FORWARD);
589 Iterator anIter (lstNodes);
590 for (; anIter.More(); anIter.Next()) {
591 // Try a Shape type of node
592 const Handle(VrmlData_ShapeNode) aNodeShape =
593 Handle(VrmlData_ShapeNode)::DownCast (anIter.Value());
594 if (aNodeShape.IsNull() == Standard_False) {
595 const Handle(VrmlData_Geometry) aNodeGeom =
596 Handle(VrmlData_Geometry)::DownCast(aNodeShape->Geometry());
597 if (aNodeGeom.IsNull() == Standard_False) {
598 if (aSingleShape.IsNull() == Standard_False)
599 isSingleShape = Standard_False;
600 const Handle(TopoDS_TShape) aTShape = aNodeGeom->TShape();
601 aSingleShape.TShape(aTShape);
602 if (aSingleShape.IsNull() == Standard_False)
603 aBuilder.Add (outShape, aSingleShape);
604 if (pMapShapeApp != 0L) {
605 const Handle(VrmlData_Appearance)& anAppearance =
606 aNodeShape->Appearance();
607 if (anAppearance.IsNull() == Standard_False) {
608 // Check if the current topology is a single face
609 if (aTShape->IsKind(STANDARD_TYPE(TopoDS_TFace)))
610 pMapShapeApp->Bind(aTShape, anAppearance);
612 // This is not a face, explode it in faces and bind each face
613 TopoDS_Shape aCurShape;
614 aCurShape.TShape(aTShape);
615 TopExp_Explorer anExp(aCurShape, TopAbs_FACE);
616 for (; anExp.More(); anExp.Next()) {
617 const TopoDS_Face& aFace = TopoDS::Face(anExp.Current());
618 pMapShapeApp->Bind(aFace.TShape(), anAppearance);
626 // Try a Group type of node
627 const Handle(VrmlData_Group) aNodeGroup =
628 Handle(VrmlData_Group)::DownCast (anIter.Value());
629 if (aNodeGroup.IsNull() == Standard_False) {
631 aNodeGroup->Shape(aShape, pMapShapeApp);
632 if (aShape.IsNull() == Standard_False) {
633 aBuilder.Add (outShape, aShape);
634 isSingleShape = Standard_False;
639 outShape = aSingleShape;
642 //=======================================================================
643 //function : ReadReal
645 //=======================================================================
647 VrmlData_ErrorStatus VrmlData_Scene::ReadReal
648 (VrmlData_InBuffer& theBuffer,
649 Standard_Real& theResult,
650 Standard_Boolean isScale,
651 Standard_Boolean isOnlyPositive) const
653 Standard_Real aResult(0.);
654 VrmlData_ErrorStatus aStatus;
655 if (VrmlData_Node::OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
657 aResult = strtod (theBuffer.LinePtr, &endptr);
658 if (endptr == theBuffer.LinePtr)
659 aStatus = VrmlData_NumericInputError;
660 else if (isOnlyPositive && aResult < 0.001*Precision::Confusion())
661 aStatus = VrmlData_IrrelevantNumber;
663 theResult = isScale ? (aResult * myLinearScale) : aResult;
664 theBuffer.LinePtr = endptr;
670 //=======================================================================
673 //=======================================================================
675 VrmlData_ErrorStatus VrmlData_Scene::ReadXYZ
676 (VrmlData_InBuffer& theBuffer,
678 Standard_Boolean isScale,
679 Standard_Boolean isOnlyPos) const
681 Standard_Real aVal[3] = {0., 0., 0.};
682 VrmlData_ErrorStatus aStatus;
683 for (Standard_Integer i = 0; i < 3; i++) {
684 if (!VrmlData_Node::OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
687 aVal[i] = strtod (theBuffer.LinePtr, &endptr);
688 if (endptr == theBuffer.LinePtr) {
689 aStatus = VrmlData_NumericInputError;
692 if (isOnlyPos && aVal[i] < 0.001*Precision::Confusion()) {
693 aStatus = VrmlData_IrrelevantNumber;
696 theBuffer.LinePtr = endptr;
699 if (aStatus == VrmlData_StatusOK)
701 theXYZ.SetCoord (aVal[0] * myLinearScale,
702 aVal[1] * myLinearScale,
703 aVal[2] * myLinearScale);
705 theXYZ.SetCoord (aVal[0], aVal[1], aVal[2]);
709 //=======================================================================
712 //=======================================================================
714 VrmlData_ErrorStatus VrmlData_Scene::ReadXY
715 (VrmlData_InBuffer& theBuffer,
717 Standard_Boolean isScale,
718 Standard_Boolean isOnlyPos) const
720 Standard_Real aVal[2] = {0., 0.};
721 VrmlData_ErrorStatus aStatus;
722 for (Standard_Integer i = 0; i < 2; i++) {
723 if (!VrmlData_Node::OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
726 aVal[i] = strtod (theBuffer.LinePtr, &endptr);
727 if (endptr == theBuffer.LinePtr) {
728 aStatus = VrmlData_NumericInputError;
731 if (isOnlyPos && aVal[i] < 0.001*Precision::Confusion()) {
732 aStatus = VrmlData_IrrelevantNumber;
735 theBuffer.LinePtr = endptr;
738 if (aStatus == VrmlData_StatusOK)
740 theXY.SetCoord (aVal[0] * myLinearScale, aVal[1] * myLinearScale);
742 theXY.SetCoord (aVal[0], aVal[1]);
746 //=======================================================================
747 //function : ReadArrIndex
748 //purpose : Read the body of the data node (comma-separated list of int
750 //=======================================================================
752 VrmlData_ErrorStatus VrmlData_Scene::ReadArrIndex
753 (VrmlData_InBuffer& theBuffer,
754 const Standard_Integer **& theArray,
755 Standard_Size& theNBlocks) const
757 VrmlData_ErrorStatus aStatus;
759 if (VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
760 if (theBuffer.LinePtr[0] != '[') // opening bracket
761 aStatus = VrmlData_VrmlFormatError;
764 NCollection_Vector<const Standard_Integer *> vecIndice;
765 NCollection_Vector<Standard_Integer> vecInt;
766 Standard_Boolean isMore (Standard_True);
769 // Loop reading integers from the stream
770 while (isMore && VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
772 // closing bracket, in case that it follows a comma
773 if (theBuffer.LinePtr[0] == ']') {
777 if (!VrmlData_Node::OK(aStatus, VrmlData_Node::ReadInteger(theBuffer,
780 // Check for valid delimiter (']' or ',')
781 if (!VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
783 if (theBuffer.LinePtr[0] == ']') {
785 isMore = Standard_False;
788 // The input value is a node index, store it in the buffer vector
789 vecInt.Append (static_cast<Standard_Integer> (anIntValue));
790 if ((anIntValue < 0 || isMore == Standard_False)
791 && vecInt.Length() > 0)
793 const Standard_Integer aLen = vecInt.Length();
794 // The input is the end-of-face, store and close this face
795 Standard_Integer * bufFace = static_cast <Standard_Integer *>
796 (myAllocator->Allocate((aLen+1) * sizeof(Standard_Integer)));
798 aStatus = VrmlData_UnrecoverableError;
802 for (Standard_Integer i = 0; i < aLen; i++)
803 bufFace[i+1] = vecInt(i);
805 vecIndice.Append(bufFace);
808 if (aStatus == VrmlData_StatusOK) {
809 const Standard_Size aNbBlocks =
810 static_cast <Standard_Size> (vecIndice.Length());
812 const Standard_Integer ** anArray =
813 static_cast <const Standard_Integer **>
814 (myAllocator->Allocate (aNbBlocks * sizeof(Standard_Integer *)));
816 aStatus = VrmlData_UnrecoverableError;
818 for (size_t i = 0; i < aNbBlocks; i++)
819 anArray[i] = vecIndice(i);
820 theNBlocks = aNbBlocks;
829 //=======================================================================
830 //function : writeArrIndex
832 //=======================================================================
834 VrmlData_ErrorStatus VrmlData_Scene::WriteArrIndex
835 (const char * thePrefix,
836 const Standard_Integer ** theArrIndex,
837 const Standard_Size theNbBlocks) const
839 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
840 if (theNbBlocks && (IsDummyWrite() == Standard_False)) {
841 if (VrmlData_Node::OK (aStatus,
842 WriteLine (thePrefix, "[", 1)))
844 const size_t aLineLimit = (myCurrentIndent < 41) ? 36 : 100;
846 for (Standard_Size iBlock = 0; iBlock < theNbBlocks; iBlock++) {
847 const Standard_Integer nVal (* theArrIndex[iBlock]);
848 const Standard_Integer * arrVal = theArrIndex[iBlock]+1;
851 sprintf (buf, "%d,", arrVal[0]);
854 sprintf (buf, "%d,%d,", arrVal[0], arrVal[1]);
857 sprintf (buf, "%d,%d,%d,", arrVal[0], arrVal[1], arrVal[2]);
860 sprintf (buf, "%d,%d,%d,%d,",
861 arrVal[0], arrVal[1], arrVal[2], arrVal[3]);
865 char * ptr = &buf[0];
866 for (Standard_Integer i = 0; i < nVal; i++) {
867 sprintf (ptr, "%d,", arrVal[i]);
868 ptr = strchr (ptr, ',') + 1;
869 if ((ptr - &buf[0]) > (ptrdiff_t)aLineLimit) {
876 WriteLine (buf, iBlock < theNbBlocks-1 ? "-1," : "-1");
878 if (aStatus == VrmlData_StatusOK)
879 aStatus = WriteLine ("]", 0L, -1);
885 //=======================================================================
886 //function : WriteXYZ
888 //=======================================================================
890 VrmlData_ErrorStatus VrmlData_Scene::WriteXYZ
891 (const gp_XYZ& theXYZ,
892 const Standard_Boolean isApplyScale,
893 const char * thePostfix) const
896 if (IsDummyWrite() == Standard_False)
897 if (isApplyScale && myLinearScale > Precision::Confusion())
898 sprintf (buf, "%.12g %.12g %.12g%s", theXYZ.X() / myLinearScale,
899 theXYZ.Y() / myLinearScale, theXYZ.Z() / myLinearScale,
900 thePostfix ? thePostfix : "");
902 sprintf (buf, "%.12g %.12g %.12g%s", theXYZ.X(), theXYZ.Y(), theXYZ.Z(),
903 thePostfix ? thePostfix : "");
904 return WriteLine (buf);
907 //=======================================================================
908 //function : WriteLine
909 //purpose : write the given string prepending the current indentation
910 //=======================================================================
912 VrmlData_ErrorStatus VrmlData_Scene::WriteLine
913 (const char * theLin0,
914 const char * theLin1,
915 const Standard_Integer theIndent) const
917 static const char spaces[] = " "
919 VrmlData_ErrorStatus& aStatus =
920 const_cast <VrmlData_ErrorStatus&> (myStatus);
922 aStatus = VrmlData_StatusOK;
924 Standard_Integer& aCurrentIndent =
925 const_cast <Standard_Integer&> (myCurrentIndent);
927 aCurrentIndent -= myIndent;
928 if (aCurrentIndent < 0)
930 if (theLin0 == 0L && theLin1 == 0L)
931 (* myOutput) << endl;
933 const Standard_Integer nSpaces = Min (aCurrentIndent, sizeof(spaces)-1);
934 (* myOutput) << &spaces[sizeof(spaces)-1 - nSpaces];
936 (* myOutput) << theLin0;
938 (* myOutput) << ' ' << theLin1;
940 (* myOutput) << theLin1;
941 (* myOutput) << endl;
943 const int stat = myOutput->rdstate();
944 if (stat & ios::badbit)
945 aStatus = VrmlData_UnrecoverableError;
946 else if (stat & ios::failbit)
947 // if (stat & ios::eofbit)
948 // aStatus = VrmlData_EndOfFile;
950 aStatus = VrmlData_GeneralError;
952 aCurrentIndent += myIndent;
957 //=======================================================================
958 //function : WriteNode
960 //=======================================================================
962 VrmlData_ErrorStatus VrmlData_Scene::WriteNode
963 (const char * thePrefix,
964 const Handle(VrmlData_Node)& theNode) const
966 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
967 Standard_Boolean isNoName (Standard_False);
968 if (theNode->Name() == 0L)
969 isNoName = Standard_True;
970 else if (theNode->Name()[0] == '\0')
971 isNoName = Standard_True;
973 if (theNode.IsNull() == Standard_False)
974 if (theNode->IsDefault() == Standard_False) {
975 if (isNoName && IsDummyWrite()) {
976 // We are in a tentative 'write' session (nothing is written).
977 // The goal is to identify multiply referred nodes.
978 Standard_Address addrNode = theNode.operator->();
979 if (!const_cast<NCollection_Map<Standard_Address>&>(myUnnamedNodesOut)
982 Handle(VrmlData_UnknownNode) bidNode = new VrmlData_UnknownNode;
986 ++const_cast<Standard_Integer&>(myAutoNameCounter));
987 bidNode->myName = &buf[0];
988 } while (myNamedNodes.Contains (bidNode));
989 // We found the vacant automatic name, let us assign to it.
990 theNode->setName (&buf[0]);
991 const_cast<VrmlData_MapOfNode&>(myNamedNodes).Add (theNode);
992 return aStatus; // do not search under already duplicated node
996 aStatus = theNode->Write (thePrefix);
998 // If the node name consists of blank characters, we do not write it
999 const char * nptr = theNode->Name();
1000 for (; * nptr != '\0'; nptr++)
1001 if (* nptr != ' ' && * nptr != '\t')
1004 aStatus = theNode->Write (thePrefix);
1006 // Name is written under DEF clause
1007 char buf[1024], * ptr;
1008 if (myNamedNodesOut.Contains (theNode)) {
1009 memcpy (buf, "USE ", 4);
1010 strncpy (&buf[4], theNode->Name(), sizeof(buf)-5);
1011 aStatus = WriteLine (thePrefix, buf);
1014 strncpy (buf, thePrefix, sizeof(buf));
1015 ptr = strchr (buf, '\0');
1019 strcpy (ptr, "DEF ");
1020 strncpy (ptr+4, theNode->Name(), &buf[sizeof(buf)] - (ptr+5));
1021 aStatus = theNode->Write (buf);
1022 const_cast<VrmlData_MapOfNode&>(myNamedNodesOut).Add (theNode);
1030 //=======================================================================
1033 //=======================================================================
1035 void VrmlData_Scene::Dump (Standard_OStream& theStream) const
1037 theStream << " ===== Diagnostic Dump of a Scene (" << myAllNodes.Extent()
1038 << " nodes)" << endl;
1041 Iterator anIterA(myAllNodes);
1042 for (; anIterA.More(); anIterA.Next())
1043 dumpNode(theStream, anIterA.Value(), "");
1045 Iterator anIter(myLstNodes);
1046 for (; anIter.More(); anIter.Next())
1047 dumpNode(theStream, anIter.Value(), " ");
1050 //=======================================================================
1051 //function : dumpNode
1052 //purpose : static (local) function
1053 //=======================================================================
1055 void dumpNode (Standard_OStream& theStream,
1056 const Handle(VrmlData_Node)& theNode,
1057 const TCollection_AsciiString& theIndent)
1059 if (theNode.IsNull())
1061 TCollection_AsciiString aNewIndent =
1062 theIndent.IsEmpty() ? theIndent : theIndent + " ";
1063 if (theNode->IsKind(STANDARD_TYPE(VrmlData_Appearance))) {
1064 const Handle(VrmlData_Appearance) anAppearance =
1065 Handle(VrmlData_Appearance)::DownCast (theNode);
1066 dumpNodeHeader (theStream, theIndent, "Appearance", theNode->Name());
1067 if (theIndent.IsEmpty() == Standard_False) {
1068 dumpNode (theStream, anAppearance->Material(), aNewIndent);
1069 dumpNode (theStream, anAppearance->Texture(), aNewIndent);
1070 dumpNode (theStream, anAppearance->TextureTransform(), aNewIndent);
1072 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_ShapeNode))) {
1073 const Handle(VrmlData_ShapeNode) aShape =
1074 Handle(VrmlData_ShapeNode)::DownCast (theNode);
1075 dumpNodeHeader (theStream, theIndent, "Shape", theNode->Name());
1076 if (theIndent.IsEmpty() == Standard_False) {
1077 dumpNode (theStream, aShape->Appearance(), aNewIndent);
1078 dumpNode (theStream, aShape->Geometry(), aNewIndent);
1080 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Box)))
1081 dumpNodeHeader (theStream, theIndent, "Box", theNode->Name());
1082 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Cylinder)))
1083 dumpNodeHeader (theStream, theIndent, "Cylinder", theNode->Name());
1084 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Sphere)))
1085 dumpNodeHeader (theStream, theIndent, "Sphere", theNode->Name());
1086 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Cone)))
1087 dumpNodeHeader (theStream, theIndent, "Cone", theNode->Name());
1088 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Coordinate)))
1089 dumpNodeHeader (theStream, theIndent, "Coordinate", theNode->Name());
1090 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Group))) {
1091 const Handle(VrmlData_Group) aGroup =
1092 Handle(VrmlData_Group)::DownCast (theNode);
1094 sprintf (buf, "Group (%s)",
1095 aGroup->IsTransform() ? "Transform" : "Group");
1096 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1097 if (theIndent.IsEmpty() == Standard_False) {
1098 VrmlData_ListOfNode::Iterator anIter = aGroup->NodeIterator();
1099 for (; anIter.More(); anIter.Next())
1100 dumpNode (theStream, anIter.Value(), aNewIndent);
1102 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_ImageTexture)))
1103 dumpNodeHeader (theStream, theIndent, "ImageTexture", theNode->Name());
1104 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_IndexedFaceSet))) {
1105 const Handle(VrmlData_IndexedFaceSet) aNode =
1106 Handle(VrmlData_IndexedFaceSet)::DownCast(theNode);
1107 const Standard_Integer ** ppDummy;
1108 const Standard_Size nCoord = aNode->Coordinates()->Length();
1109 const Standard_Size nPoly = aNode->Polygons (ppDummy);
1111 sprintf (buf, "IndexedFaceSet (%d vertices, %d polygons)", nCoord, nPoly);
1112 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1113 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_IndexedLineSet))) {
1114 const Handle(VrmlData_IndexedLineSet) aNode =
1115 Handle(VrmlData_IndexedLineSet)::DownCast(theNode);
1116 const Standard_Integer ** ppDummy;
1117 const Standard_Size nCoord = aNode->Coordinates()->Length();
1118 const Standard_Size nPoly = aNode->Polygons (ppDummy);
1120 sprintf (buf, "IndexedLineSet (%d vertices, %d polygons)", nCoord, nPoly);
1121 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1122 } else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Material))) {
1123 // const Handle(VrmlData_Material) aMaterial =
1124 // Handle(VrmlData_Material)::DownCast (theNode);
1125 dumpNodeHeader (theStream, theIndent, "Material", theNode->Name());
1127 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_Normal)))
1128 dumpNodeHeader (theStream, theIndent, "Normal", theNode->Name());
1129 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_TextureCoordinate)))
1130 dumpNodeHeader (theStream, theIndent, "TextureCoordinate", theNode->Name());
1131 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_WorldInfo)))
1132 dumpNodeHeader (theStream, theIndent, "WorldInfo", theNode->Name());
1133 else if (theNode->IsKind(STANDARD_TYPE(VrmlData_UnknownNode))) {
1134 const Handle(VrmlData_UnknownNode) anUnknown =
1135 Handle(VrmlData_UnknownNode)::DownCast (theNode);
1137 sprintf (buf, "Unknown (%s)", anUnknown->GetTitle().ToCString());
1138 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1142 //=======================================================================
1143 //function : dumpNodeHeader
1145 //=======================================================================
1147 void dumpNodeHeader (Standard_OStream& theStream,
1148 const TCollection_AsciiString& theIndent,
1149 const char * theType,
1150 const char * theName)
1152 theStream << theIndent << theType <<" node";
1153 if (theName[0] == '\0')
1156 theStream << ": \"" << theName << '\"' << endl;