0022683: Incorrect result of Select3D_Projector::Project() in the case of perspective...
[occt.git] / src / VrmlData / VrmlData_Scene.cxx
CommitLineData
7fd59977 1// File: VrmlData_Scene.cxx
2// Created: 25.05.06 16:33:25
3// Author: Alexander GRIGORIEV
4// Copyright: Open Cascade 2006
5
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>
30#include <TopoDS.hxx>
31#include <TopoDS_Face.hxx>
32#include <TopExp_Explorer.hxx>
33#include <BRep_Builder.hxx>
34#include <Precision.hxx>
35
36#ifdef WNT
37#define _CRT_SECURE_NO_DEPRECATE
38#pragma warning (disable:4996)
39#endif
40
41static void dumpNode (Standard_OStream& theStream,
42 const Handle(VrmlData_Node)& theNode,
43 const TCollection_AsciiString& theIndent);
44
45static void dumpNodeHeader (Standard_OStream& theStream,
46 const TCollection_AsciiString& theIndent,
47 const char * theType,
48 const char * theName);
49
50//=======================================================================
51//function : VrmlData_Scene
52//purpose : Constructor
53//=======================================================================
54
55VrmlData_Scene::VrmlData_Scene
56 (const Handle(NCollection_IncAllocator)& theAlloc)
57 : myLinearScale (1.),
58 myStatus (VrmlData_StatusOK),
59 myAllocator (theAlloc.IsNull() ?
60 new NCollection_IncAllocator : theAlloc.operator->()),
61 myLineError (0),
62 myOutput (0L),
63 myIndent (2),
64 myCurrentIndent (0),
65 myAutoNameCounter (0)
66{
67 myWorldInfo = new VrmlData_WorldInfo (* this);
68 myWorldInfo->AddInfo ("Created by OPEN CASCADE (tm) VrmlData API");
69 myLstNodes.Append (myWorldInfo);
70 myAllNodes.Append (myWorldInfo);
71}
72
73//=======================================================================
74//function : AddNode
75//purpose :
76//=======================================================================
77
78const Handle(VrmlData_Node)& VrmlData_Scene::AddNode
79 (const Handle(VrmlData_Node)& theN,
80 const Standard_Boolean isTopLevel)
81{
82 if (theN.IsNull() == Standard_False)
83 if (theN->IsKind (STANDARD_TYPE(VrmlData_WorldInfo)) == Standard_False) {
84 myMutex.Lock();
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");
92 if (isTopLevel)
93 myLstNodes.Append (aNode);
94 myMutex.Unlock();
95 return aNode;
96 }
97 static Handle(VrmlData_Node) aNullNode;
98 aNullNode.Nullify();
99 return aNullNode;
100}
101
102//=======================================================================
103//function : operator <<
104//purpose : Export to text stream (file or else)
105//=======================================================================
106
107Standard_OStream& operator << (Standard_OStream& theOutput,
108 const VrmlData_Scene& theScene)
109{
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;
118
119 // Dummy write
120
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)
128 break;
129 }
130 }
131
132 aScene.myOutput = &theOutput;
133 aScene.myNamedNodesOut.Clear();
134 theOutput << "#VRML V2.0 utf8" << endl << endl;
135
136 // Real write
137
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)
145 break;
146 }
147 }
148 aScene.myOutput = 0L;
149 aScene.myNamedNodesOut.Clear();
150 aScene.myUnnamedNodesOut.Clear();
151 aScene.myMutex.Unlock();
152 return theOutput;
153}
154
155//=======================================================================
156//function : SetVrmlDir
157//purpose :
158//=======================================================================
159
160void VrmlData_Scene::SetVrmlDir (const TCollection_ExtendedString& theDir)
161{
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('/'))
166#ifdef WNT
167 aDir += TCollection_ExtendedString ("\\");
168#else
169 aDir += TCollection_ExtendedString ("/");
170#endif
171}
172
173//=======================================================================
174//function : WorldInfo
175//purpose :
176//=======================================================================
177
178const Handle_VrmlData_WorldInfo& VrmlData_Scene::WorldInfo() const
179{
180 return myWorldInfo;
181}
182
183//=======================================================================
184//function : readLine
185//purpose :
186//=======================================================================
187
188VrmlData_ErrorStatus VrmlData_Scene::readLine (VrmlData_InBuffer& theBuffer)
189{
190 VrmlData_ErrorStatus aStatus = VrmlData_StatusOK;
191 if (theBuffer.Input.eof())
192 aStatus = VrmlData_EndOfFile;
193 else {
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;
202 else
203 aStatus = VrmlData_GeneralError;
204 theBuffer.LinePtr = &theBuffer.Line[0];
205 theBuffer.IsProcessed = Standard_False;
206 }
207 return aStatus;
208}
209
210//=======================================================================
211//function : ReadLine
212//purpose :
213//=======================================================================
214
215VrmlData_ErrorStatus VrmlData_Scene::ReadLine (VrmlData_InBuffer& theBuffer)
216{
217 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
218
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 != ',')
224 {
225 if (* theBuffer.LinePtr == '\n' || * theBuffer.LinePtr == '\r' ||
226 * theBuffer.LinePtr == '#')
227 // go requesting the next line
228 break;
229 goto nonempty_line;
230 }
231 }
232 // the line is empty here (no significant characters). Read the next one.
233 aStatus = readLine (theBuffer);
234 }
235
236 // error or EOF detected
237 return aStatus;
238
239 nonempty_line:
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++) {
246 if (anOffset)
247 * ptr = ptr[anOffset];
248 if (* ptr == '\n' || * ptr == '\r' || * ptr == '#') {
249 if (isQuoted == Standard_False) {
250 * ptr = '\0';
251 break;
252 }
253 } else if (* ptr == '\\' && isQuoted)
254 ptr[0] = ptr[++anOffset];
255 else if (* ptr == '\"')
256 isQuoted = !isQuoted;
257 }
258 theBuffer.IsProcessed = Standard_True;
259 }
260 return aStatus;
261}
262
263//=======================================================================
264//function : readHeader
265//purpose :
266//=======================================================================
267
268VrmlData_ErrorStatus VrmlData_Scene::readHeader (VrmlData_InBuffer& theBuffer)
269{
270 VrmlData_ErrorStatus aStat = readLine (theBuffer);
271 if (aStat == VrmlData_StatusOK &&
272 !VRMLDATA_LCOMPARE(theBuffer.LinePtr, "#VRML V2.0"))
273 aStat = VrmlData_NotVrmlFile;
274 else
275 aStat = readLine(theBuffer);
276 return aStat;
277}
278
279//=======================================================================
280//function : operator <<
281//purpose : Import from text stream (file or else)
282//=======================================================================
283
284VrmlData_Scene& VrmlData_Scene::operator << (Standard_IStream& theInput)
285{
286 VrmlData_InBuffer aBuffer (theInput);
287 myMutex.Lock();
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
294 while (~0) {
295 if (!VrmlData_Node::OK(myStatus, ReadLine(aBuffer))) {
296 if (myStatus == VrmlData_EndOfFile)
297 myStatus = VrmlData_StatusOK;
298 break;
299 }
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)
305 break;
306 if (aNode.IsNull() == Standard_False /*&&
307 !aNode->IsKind (STANDARD_TYPE(VrmlData_UnknownNode))*/)
308 {
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());
319 }
320 }
321 }
322 if (myStatus != VrmlData_StatusOK)
323 myLineError = aBuffer.LineCount;
324 myMutex.Unlock();
325 return * this;
326}
327
328//=======================================================================
329//function : FindNode
330//purpose :
331//=======================================================================
332
333Handle(VrmlData_Node) VrmlData_Scene::FindNode
334 (const char * theName,
335 const Handle(Standard_Type)& theType) const
336{
337 Handle(VrmlData_Node) aResult;
338#ifdef USE_LIST_API
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())
344 break;
345 if (aResult->IsKind(theType))
346 break;
347 aResult.Nullify();
348 }
349#else
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);
354#endif
355 return aResult;
356}
357
358//=======================================================================
359//function : FindNode
360//purpose :
361//=======================================================================
362
363Handle(VrmlData_Node) VrmlData_Scene::FindNode
364 (const char * theName,
365 gp_Trsf& theLocation) const
366{
367 gp_Trsf aLoc;
368 Handle(VrmlData_Node) aResult;
369 Iterator anIter (myLstNodes);
370 for (; anIter.More(); anIter.Next()) {
371 const Handle(VrmlData_Node)& aNode = anIter.Value();
372 if (aNode.IsNull())
373 continue;
374 // Match a top-level node name
375 if (strcmp(aNode->Name(), theName) == 0) {
376 aResult = aNode;
377 theLocation = aLoc;
378 break;
379 }
380 // Try a Group type of node
381 if (aNode->IsKind(STANDARD_TYPE(VrmlData_Group)))
382 {
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)
388 break;
389 }
390 }
391 }
392 return aResult;
393}
394
395//=======================================================================
396//function : ReadWord
397//purpose :
398//=======================================================================
399
400VrmlData_ErrorStatus VrmlData_Scene::ReadWord
401 (VrmlData_InBuffer& theBuffer,
402 TCollection_AsciiString& theWord)
403{
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 != ']')
410 ptr++;
411 const Standard_Integer aLen = Standard_Integer(ptr - theBuffer.LinePtr);
412 if (aLen <= 0)
413 aStatus = VrmlData_StringInputError;
414 else {
415 theWord = TCollection_AsciiString ((Standard_CString)theBuffer.LinePtr,
416 aLen);
417 theBuffer.LinePtr = ptr;
418 }
419 }
420 return aStatus;
421}
422
423//=======================================================================
424//function : createNode
425//purpose :
426//=======================================================================
427
428VrmlData_ErrorStatus VrmlData_Scene::createNode
429 (VrmlData_InBuffer& theBuffer,
430 Handle(VrmlData_Node)& theNode,
431 const Handle(Standard_Type)& theType)
432{
433 VrmlData_ErrorStatus aStatus;
434 Handle(VrmlData_Node) aNode;
435 TCollection_AsciiString aName;
436 Standard_Boolean isReused (Standard_False);
437
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")) {
444 theNode.Nullify();
445 return aStatus;
446 }
447
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,
467 Standard_False);
468 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Transform"))
469 aNode = new VrmlData_Group (* this, strName,
470 Standard_True);
471 else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "Inline"))
472 aNode = new VrmlData_Group (* this, strName,
473 Standard_False);
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);
490 else {
491 void * isProto = VRMLDATA_LCOMPARE(theBuffer.LinePtr, "PROTO");
492 TCollection_AsciiString aTitle;
493 aStatus = ReadWord (theBuffer, aTitle);
494 if (isProto) {
495 aStatus = ReadLine(theBuffer);
496 if (aStatus == VrmlData_StatusOK)
497 if (theBuffer.LinePtr[0] != '[')
498 aStatus = VrmlData_VrmlFormatError;
499 else {
500 theBuffer.LinePtr++;
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) {
507 int aChar;
508 while ((aChar = theBuffer.LinePtr[0]) != '\0') {
509 theBuffer.LinePtr++;
510 if (aChar == '[') {
511 aLevelCounter++;
512 break;
513 } else if (aChar == ']') {
514 aLevelCounter--;
515 break;
516 }
517 }
518 }
519 }
520 }
521 if (aStatus == VrmlData_StatusOK)
522 aNode = new VrmlData_UnknownNode(* this,
523 strName,
524 aTitle.ToCString());
525 }
526 }
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;
534#ifdef _DEBUG
535 aNode->myLineCount = theBuffer.LineCount;
536#endif
537 }
538 if (aStatus == VrmlData_StatusOK)
539 if (theBuffer.LinePtr[0] == '{') {
540 theBuffer.LinePtr++;
541 theNode = aNode;
542 myAllNodes.Append(aNode);
543 } else
544 aStatus = VrmlData_VrmlFormatError;
545 return aStatus;
546}
547
548//=======================================================================
549//function : operator TopoDS_Shape
550//purpose :
551//=======================================================================
552
553VrmlData_Scene::operator TopoDS_Shape () const
554{
555 TopoDS_Shape aShape;
556 VrmlData_Scene::createShape (aShape, myLstNodes, 0L);
557 return aShape;
558}
559
560//=======================================================================
561//function : GetShape
562//purpose :
563//=======================================================================
564
565TopoDS_Shape VrmlData_Scene::GetShape (VrmlData_DataMapOfShapeAppearance& aMap)
566{
567 TopoDS_Shape aShape;
568 VrmlData_Scene::createShape (aShape, myLstNodes, &aMap);
569 return aShape;
570}
571
572//=======================================================================
573//function : createShape
574//purpose :
575//=======================================================================
576
577void VrmlData_Scene::createShape
578 (TopoDS_Shape& outShape,
579 const VrmlData_ListOfNode& lstNodes,
580 VrmlData_DataMapOfShapeAppearance* pMapShapeApp)
581{
582 TopoDS_Shape aSingleShape; // used when there is a single ShapeNode
583 Standard_Boolean isSingleShape (Standard_True);
584 BRep_Builder aBuilder;
585 outShape.Nullify();
586 aBuilder.MakeCompound(TopoDS::Compound(outShape));
587 aSingleShape.Orientation(TopAbs_FORWARD);
588
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);
611 else {
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);
619 }
620 }
621 }
622 }
623 }
624 continue;
625 }
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) {
630 TopoDS_Shape aShape;
631 aNodeGroup->Shape(aShape, pMapShapeApp);
632 if (aShape.IsNull() == Standard_False) {
633 aBuilder.Add (outShape, aShape);
634 isSingleShape = Standard_False;
635 }
636 }
637 }
638 if (isSingleShape)
639 outShape = aSingleShape;
640}
641
642//=======================================================================
643//function : ReadReal
644//purpose :
645//=======================================================================
646
647VrmlData_ErrorStatus VrmlData_Scene::ReadReal
648 (VrmlData_InBuffer& theBuffer,
649 Standard_Real& theResult,
650 Standard_Boolean isScale,
651 Standard_Boolean isOnlyPositive) const
652{
653 Standard_Real aResult(0.);
654 VrmlData_ErrorStatus aStatus;
655 if (VrmlData_Node::OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
656 char * endptr;
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;
662 else {
663 theResult = isScale ? (aResult * myLinearScale) : aResult;
664 theBuffer.LinePtr = endptr;
665 }
666 }
667 return aStatus;
668}
669
670//=======================================================================
671//function : ReadXYZ
672//purpose :
673//=======================================================================
674
675VrmlData_ErrorStatus VrmlData_Scene::ReadXYZ
676 (VrmlData_InBuffer& theBuffer,
677 gp_XYZ& theXYZ,
678 Standard_Boolean isScale,
679 Standard_Boolean isOnlyPos) const
680{
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)))
685 break;
686 char * endptr;
687 aVal[i] = strtod (theBuffer.LinePtr, &endptr);
688 if (endptr == theBuffer.LinePtr) {
689 aStatus = VrmlData_NumericInputError;
690 break;
691 } else {
692 if (isOnlyPos && aVal[i] < 0.001*Precision::Confusion()) {
693 aStatus = VrmlData_IrrelevantNumber;
694 break;
695 }
696 theBuffer.LinePtr = endptr;
697 }
698 }
699 if (aStatus == VrmlData_StatusOK)
700 if (isScale)
701 theXYZ.SetCoord (aVal[0] * myLinearScale,
702 aVal[1] * myLinearScale,
703 aVal[2] * myLinearScale);
704 else
705 theXYZ.SetCoord (aVal[0], aVal[1], aVal[2]);
706 return aStatus;
707}
708
709//=======================================================================
710//function : ReadXY
711//purpose :
712//=======================================================================
713
714VrmlData_ErrorStatus VrmlData_Scene::ReadXY
715 (VrmlData_InBuffer& theBuffer,
716 gp_XY& theXY,
717 Standard_Boolean isScale,
718 Standard_Boolean isOnlyPos) const
719{
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)))
724 break;
725 char * endptr;
726 aVal[i] = strtod (theBuffer.LinePtr, &endptr);
727 if (endptr == theBuffer.LinePtr) {
728 aStatus = VrmlData_NumericInputError;
729 break;
730 } else {
731 if (isOnlyPos && aVal[i] < 0.001*Precision::Confusion()) {
732 aStatus = VrmlData_IrrelevantNumber;
733 break;
734 }
735 theBuffer.LinePtr = endptr;
736 }
737 }
738 if (aStatus == VrmlData_StatusOK)
739 if (isScale)
740 theXY.SetCoord (aVal[0] * myLinearScale, aVal[1] * myLinearScale);
741 else
742 theXY.SetCoord (aVal[0], aVal[1]);
743 return aStatus;
744}
745
746//=======================================================================
747//function : ReadArrIndex
748//purpose : Read the body of the data node (comma-separated list of int
749// multiplets)
750//=======================================================================
751
752VrmlData_ErrorStatus VrmlData_Scene::ReadArrIndex
753 (VrmlData_InBuffer& theBuffer,
754 const Standard_Integer **& theArray,
755 Standard_Size& theNBlocks) const
756{
757 VrmlData_ErrorStatus aStatus;
758 theNBlocks = 0;
759 if (VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
760 if (theBuffer.LinePtr[0] != '[') // opening bracket
761 aStatus = VrmlData_VrmlFormatError;
762 else {
763 theBuffer.LinePtr++;
764 NCollection_Vector<const Standard_Integer *> vecIndice;
765 NCollection_Vector<Standard_Integer> vecInt;
766 Standard_Boolean isMore (Standard_True);
767 long anIntValue;
768
769 // Loop reading integers from the stream
770 while (isMore && VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
771 {
772 // closing bracket, in case that it follows a comma
773 if (theBuffer.LinePtr[0] == ']') {
774 theBuffer.LinePtr++;
775 break;
776 }
777 if (!VrmlData_Node::OK(aStatus, VrmlData_Node::ReadInteger(theBuffer,
778 anIntValue)))
779 break;
780 // Check for valid delimiter (']' or ',')
781 if (!VrmlData_Node::OK(aStatus, ReadLine(theBuffer)))
782 break;
783 if (theBuffer.LinePtr[0] == ']') {
784 theBuffer.LinePtr++;
785 isMore = Standard_False;
786 }
787 if (anIntValue >= 0)
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)
792 {
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)));
797 if (bufFace == 0L) {
798 aStatus = VrmlData_UnrecoverableError;
799 break;
800 }
801 bufFace[0] = aLen;
802 for (Standard_Integer i = 0; i < aLen; i++)
803 bufFace[i+1] = vecInt(i);
804 vecInt.Clear();
805 vecIndice.Append(bufFace);
806 }
807 }
808 if (aStatus == VrmlData_StatusOK) {
809 const Standard_Size aNbBlocks =
810 static_cast <Standard_Size> (vecIndice.Length());
811 if (aNbBlocks) {
812 const Standard_Integer ** anArray =
813 static_cast <const Standard_Integer **>
814 (myAllocator->Allocate (aNbBlocks * sizeof(Standard_Integer *)));
815 if (anArray == 0L)
816 aStatus = VrmlData_UnrecoverableError;
817 else {
818 for (size_t i = 0; i < aNbBlocks; i++)
819 anArray[i] = vecIndice(i);
820 theNBlocks = aNbBlocks;
821 theArray = anArray;
822 }
823 }
824 }
825 }
826 return aStatus;
827}
828
829//=======================================================================
830//function : writeArrIndex
831//purpose :
832//=======================================================================
833
834VrmlData_ErrorStatus VrmlData_Scene::WriteArrIndex
835 (const char * thePrefix,
836 const Standard_Integer ** theArrIndex,
837 const Standard_Size theNbBlocks) const
838{
839 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
840 if (theNbBlocks && (IsDummyWrite() == Standard_False)) {
841 if (VrmlData_Node::OK (aStatus,
842 WriteLine (thePrefix, "[", 1)))
843 {
844 const size_t aLineLimit = (myCurrentIndent < 41) ? 36 : 100;
845 char buf[256];
846 for (Standard_Size iBlock = 0; iBlock < theNbBlocks; iBlock++) {
847 const Standard_Integer nVal (* theArrIndex[iBlock]);
848 const Standard_Integer * arrVal = theArrIndex[iBlock]+1;
849 switch (nVal) {
850 case 1:
851 sprintf (buf, "%d,", arrVal[0]);
852 break;
853 case 2:
854 sprintf (buf, "%d,%d,", arrVal[0], arrVal[1]);
855 break;
856 case 3:
857 sprintf (buf, "%d,%d,%d,", arrVal[0], arrVal[1], arrVal[2]);
858 break;
859 case 4:
860 sprintf (buf, "%d,%d,%d,%d,",
861 arrVal[0], arrVal[1], arrVal[2], arrVal[3]);
862 break;
863 default:
864 if (nVal > 0) {
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) {
870 WriteLine(buf);
871 ptr = &buf[0];
872 }
873 }
874 }
875 }
876 WriteLine (buf, iBlock < theNbBlocks-1 ? "-1," : "-1");
877 }
878 if (aStatus == VrmlData_StatusOK)
879 aStatus = WriteLine ("]", 0L, -1);
880 }
881 }
882 return aStatus;
883}
884
885//=======================================================================
886//function : WriteXYZ
887//purpose :
888//=======================================================================
889
890VrmlData_ErrorStatus VrmlData_Scene::WriteXYZ
891 (const gp_XYZ& theXYZ,
892 const Standard_Boolean isApplyScale,
893 const char * thePostfix) const
894{
895 char buf[240];
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 : "");
901 else
902 sprintf (buf, "%.12g %.12g %.12g%s", theXYZ.X(), theXYZ.Y(), theXYZ.Z(),
903 thePostfix ? thePostfix : "");
904 return WriteLine (buf);
905}
906
907//=======================================================================
908//function : WriteLine
909//purpose : write the given string prepending the current indentation
910//=======================================================================
911
912VrmlData_ErrorStatus VrmlData_Scene::WriteLine
913 (const char * theLin0,
914 const char * theLin1,
915 const Standard_Integer theIndent) const
916{
917 static const char spaces[] = " "
918 " ";
919 VrmlData_ErrorStatus& aStatus =
920 const_cast <VrmlData_ErrorStatus&> (myStatus);
921 if (IsDummyWrite())
922 aStatus = VrmlData_StatusOK;
923 else {
924 Standard_Integer& aCurrentIndent =
925 const_cast <Standard_Integer&> (myCurrentIndent);
926 if (theIndent < 0)
927 aCurrentIndent -= myIndent;
928 if (aCurrentIndent < 0)
929 aCurrentIndent = 0;
930 if (theLin0 == 0L && theLin1 == 0L)
931 (* myOutput) << endl;
932 else {
933 const Standard_Integer nSpaces = Min (aCurrentIndent, sizeof(spaces)-1);
934 (* myOutput) << &spaces[sizeof(spaces)-1 - nSpaces];
935 if (theLin0) {
936 (* myOutput) << theLin0;
937 if (theLin1)
938 (* myOutput) << ' ' << theLin1;
939 } else
940 (* myOutput) << theLin1;
941 (* myOutput) << endl;
942 }
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;
949// else
950 aStatus = VrmlData_GeneralError;
951 if (theIndent > 0)
952 aCurrentIndent += myIndent;
953 }
954 return myStatus;
955}
956
957//=======================================================================
958//function : WriteNode
959//purpose :
960//=======================================================================
961
962VrmlData_ErrorStatus VrmlData_Scene::WriteNode
963 (const char * thePrefix,
964 const Handle(VrmlData_Node)& theNode) const
965{
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;
972
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)
980 .Add (addrNode))
981 {
982 Handle(VrmlData_UnknownNode) bidNode = new VrmlData_UnknownNode;
983 char buf[32];
984 do {
985 sprintf (buf, "_%d",
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
993 }
994 }
995 if (isNoName)
996 aStatus = theNode->Write (thePrefix);
997 else {
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')
1002 break;
1003 if (* nptr == '\0')
1004 aStatus = theNode->Write (thePrefix);
1005 else {
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);
1012 } else {
1013 if (thePrefix) {
1014 strncpy (buf, thePrefix, sizeof(buf));
1015 ptr = strchr (buf, '\0');
1016 * ptr++ = ' ';
1017 } else
1018 ptr = &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);
1023 }
1024 }
1025 }
1026 }
1027 return aStatus;
1028}
1029
1030//=======================================================================
1031//function : Dump
1032//purpose :
1033//=======================================================================
1034
1035void VrmlData_Scene::Dump (Standard_OStream& theStream) const
1036{
1037 theStream << " ===== Diagnostic Dump of a Scene (" << myAllNodes.Extent()
1038 << " nodes)" << endl;
1039
1040 /*
1041 Iterator anIterA(myAllNodes);
1042 for (; anIterA.More(); anIterA.Next())
1043 dumpNode(theStream, anIterA.Value(), "");
1044 */
1045 Iterator anIter(myLstNodes);
1046 for (; anIter.More(); anIter.Next())
1047 dumpNode(theStream, anIter.Value(), " ");
1048}
1049
1050//=======================================================================
1051//function : dumpNode
1052//purpose : static (local) function
1053//=======================================================================
1054
1055void dumpNode (Standard_OStream& theStream,
1056 const Handle(VrmlData_Node)& theNode,
1057 const TCollection_AsciiString& theIndent)
1058{
1059 if (theNode.IsNull())
1060 return;
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);
1071 }
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);
1079 }
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);
1093 char buf[64];
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);
1101 }
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);
1110 char buf[64];
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);
1119 char buf[64];
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());
1126 }
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);
1136 char buf[64];
1137 sprintf (buf, "Unknown (%s)", anUnknown->GetTitle().ToCString());
1138 dumpNodeHeader (theStream, theIndent, buf, theNode->Name());
1139 }
1140}
1141
1142//=======================================================================
1143//function : dumpNodeHeader
1144//purpose :
1145//=======================================================================
1146
1147void dumpNodeHeader (Standard_OStream& theStream,
1148 const TCollection_AsciiString& theIndent,
1149 const char * theType,
1150 const char * theName)
1151{
1152 theStream << theIndent << theType <<" node";
1153 if (theName[0] == '\0')
1154 theStream << endl;
1155 else
1156 theStream << ": \"" << theName << '\"' << endl;
1157}