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 <Precision.hxx>
17 #include <VrmlData_Appearance.hxx>
18 #include <VrmlData_ImageTexture.hxx>
19 #include <VrmlData_Material.hxx>
20 #include <VrmlData_ShapeNode.hxx>
21 #include <VrmlData_UnknownNode.hxx>
22 #include <VrmlData_Scene.hxx>
23 #include <VrmlData_InBuffer.hxx>
28 #define _CRT_SECURE_NO_DEPRECATE
29 #pragma warning (disable:4996)
32 IMPLEMENT_STANDARD_HANDLE (VrmlData_Node, Standard_Transient)
33 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_Node, Standard_Transient)
34 IMPLEMENT_STANDARD_HANDLE (VrmlData_ShapeNode, VrmlData_Node)
35 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_ShapeNode, VrmlData_Node)
36 IMPLEMENT_STANDARD_HANDLE (VrmlData_Texture, VrmlData_Node)
37 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_Texture, VrmlData_Node)
38 IMPLEMENT_STANDARD_HANDLE (VrmlData_TextureTransform, VrmlData_Node)
39 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_TextureTransform, VrmlData_Node)
40 IMPLEMENT_STANDARD_HANDLE (VrmlData_ImageTexture, VrmlData_Texture)
41 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_ImageTexture, VrmlData_Texture)
42 IMPLEMENT_STANDARD_HANDLE (VrmlData_Appearance, VrmlData_Node)
43 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_Appearance, VrmlData_Node)
44 IMPLEMENT_STANDARD_HANDLE (VrmlData_UnknownNode, VrmlData_Node)
45 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_UnknownNode, VrmlData_Node)
47 static VrmlData_Scene MyDefaultScene;
49 //=======================================================================
51 //purpose : Global method
52 //=======================================================================
54 Standard_Boolean IsEqual (const Handle(VrmlData_Node)& theOne,
55 const Handle(VrmlData_Node)& theTwo)
57 Standard_Boolean aResult (Standard_False);
58 if (theOne->Name() != 0L && theTwo->Name() != 0L)
59 aResult = (strcmp (theOne->Name(), theTwo->Name()) == 0);
63 //=======================================================================
65 //purpose : Global method
66 //=======================================================================
68 Standard_Integer HashCode(const Handle(VrmlData_Node)& theNode,
69 const Standard_Integer theUpper)
71 return (theNode->Name() == 0L ? 0
72 : HashCode((Standard_CString)theNode->Name(), theUpper));
75 //=======================================================================
76 //function : VrmlData_Node
78 //=======================================================================
80 VrmlData_Node::VrmlData_Node ()
81 : myScene (&MyDefaultScene),
84 //=======================================================================
85 //function : VrmlData_Node
86 //purpose : Constructor
87 //=======================================================================
89 VrmlData_Node::VrmlData_Node (const VrmlData_Scene& theScene,
98 //=======================================================================
100 //purpose : Create a copy of this node.
101 //=======================================================================
103 Handle(VrmlData_Node) VrmlData_Node::Clone
104 (const Handle(VrmlData_Node)& theOther) const
106 if (theOther.IsNull() == Standard_False) {
107 if (theOther->IsKind (DynamicType()) == Standard_False)
109 if (&theOther->Scene() == myScene)
110 theOther->myName = myName;
112 theOther->setName (myName);
117 //=======================================================================
120 //=======================================================================
122 void VrmlData_Node::setName (const char * theName, const char * theSuffix)
129 len[1] = strlen (theSuffix);
130 char * aName = (char *)Scene().Allocator()->Allocate(len[0]+len[1]);
132 memcpy (aName, theName, len[0]);
134 memcpy (&aName[len[0] - 1], theSuffix, len[1]+1);
137 //=======================================================================
138 //function : IsDefault
140 //=======================================================================
142 Standard_Boolean VrmlData_Node::IsDefault () const
144 return Standard_False;
148 //=======================================================================
151 //=======================================================================
153 VrmlData_ErrorStatus VrmlData_Node::Write (const char *) const
155 return VrmlData_NotImplemented;
158 //=======================================================================
159 //function : WriteClosing
161 //=======================================================================
163 VrmlData_ErrorStatus VrmlData_Node::WriteClosing () const
165 VrmlData_ErrorStatus aResult = Scene().Status();
166 if (aResult == VrmlData_StatusOK || aResult == VrmlData_NotImplemented)
167 aResult = Scene().WriteLine ("}", 0L, -GlobalIndent());
171 //=======================================================================
172 //function : readBrace
174 //=======================================================================
176 VrmlData_ErrorStatus VrmlData_Node::readBrace (VrmlData_InBuffer& theBuffer)
178 VrmlData_ErrorStatus aStatus;
179 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
180 if (theBuffer.LinePtr[0] == '}')
183 aStatus = VrmlData_VrmlFormatError;
188 //=======================================================================
189 //function : ReadBoolean
191 //=======================================================================
193 VrmlData_ErrorStatus VrmlData_Node::ReadBoolean (VrmlData_InBuffer& theBuffer,
194 Standard_Boolean& theResult)
196 VrmlData_ErrorStatus aStatus;
197 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
198 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "TRUE"))
199 theResult = Standard_True;
200 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "FALSE"))
201 theResult = Standard_False;
203 aStatus = VrmlData_BooleanInputError;
208 //=======================================================================
209 //function : ReadInteger
211 //=======================================================================
213 VrmlData_ErrorStatus VrmlData_Node::ReadInteger (VrmlData_InBuffer& theBuffer,
216 VrmlData_ErrorStatus aStatus;
217 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
220 aResult = strtol (theBuffer.LinePtr, &endptr, 10);
221 if (endptr == theBuffer.LinePtr)
222 aStatus = VrmlData_NumericInputError;
225 theBuffer.LinePtr = endptr;
231 //=======================================================================
232 //function : ReadString
234 //=======================================================================
236 VrmlData_ErrorStatus VrmlData_Node::ReadString
237 (VrmlData_InBuffer& theBuffer,
238 TCollection_AsciiString& theResult)
240 VrmlData_ErrorStatus aStatus;
241 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
242 if (theBuffer.LinePtr[0] != '\"')
243 aStatus = VrmlData_StringInputError;
245 char * ptr = &theBuffer.LinePtr[1];
246 while (*ptr != '\0' && *ptr != '\"')
249 aStatus = VrmlData_StringInputError;
252 theResult = (Standard_CString) &theBuffer.LinePtr[1];
253 theBuffer.LinePtr = ptr+1;
260 //=======================================================================
261 //function : ReadMultiString
263 //=======================================================================
265 VrmlData_ErrorStatus VrmlData_Node::ReadMultiString
266 (VrmlData_InBuffer& theBuffer,
267 NCollection_List<TCollection_AsciiString>& theResult)
269 VrmlData_ErrorStatus aStatus;
270 Standard_Boolean isBracketed (Standard_False);
271 // Read the list of URL
272 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
273 if (theBuffer.LinePtr[0] == '[') {
275 isBracketed = Standard_True;
277 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
278 if (isBracketed && theBuffer.LinePtr[0] == ']') { // closing bracket
282 TCollection_AsciiString aString;
283 if (!OK(aStatus, ReadString(theBuffer, aString)))
285 theResult.Append(aString);
286 if (isBracketed == Standard_False ||
287 !OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
289 if (theBuffer.LinePtr[0] == ',') {
292 } else if (theBuffer.LinePtr[0] == ']') // closing bracket
295 aStatus = VrmlData_VrmlFormatError;
302 //=======================================================================
303 //function : ReadNode
305 //=======================================================================
307 VrmlData_ErrorStatus VrmlData_Node::ReadNode
308 (VrmlData_InBuffer& theBuffer,
309 Handle(VrmlData_Node)& theNode,
310 const Handle(Standard_Type)& theType)
312 Handle(VrmlData_Node) aNode;
313 VrmlData_ErrorStatus aStatus;
314 // First line of a new node should identify this node type
315 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
316 if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "USE")) {
317 TCollection_AsciiString aName;
318 aStatus = VrmlData_Scene::ReadWord (theBuffer, aName);
319 if (aStatus == VrmlData_StatusOK) {
320 aNode = myScene->FindNode (aName.ToCString(), theType);
322 aStatus = VrmlData_NodeNameUnknown;
324 // aNode = aNode->Clone(0L);
328 // We create a relevant node using the line with the type ID
330 const_cast<VrmlData_Scene *>(myScene)->createNode (theBuffer,
333 if (aNode.IsNull() == Standard_False)
334 // The node data are read here, including the final closing brace
335 aStatus = aNode->Read(theBuffer);
337 if (aStatus == VrmlData_StatusOK)
343 //=======================================================================
344 //function : VrmlData_ShapeNode::Clone
346 //=======================================================================
348 Handle(VrmlData_Node) VrmlData_ShapeNode::Clone
349 (const Handle(VrmlData_Node)& theOther) const
351 Handle(VrmlData_ShapeNode) aResult =
352 Handle(VrmlData_ShapeNode)::DownCast (VrmlData_Node::Clone(theOther));
353 if (aResult.IsNull())
354 aResult= new VrmlData_ShapeNode(theOther.IsNull()?Scene():theOther->Scene(),
356 if (&aResult->Scene() == &Scene()) {
357 aResult->SetAppearance (myAppearance);
358 aResult->SetGeometry (myGeometry);
360 // Create a dummy node to pass the different Scene instance to methods Clone
361 const Handle(VrmlData_UnknownNode) aDummyNode =
362 new VrmlData_UnknownNode (aResult->Scene());
363 if (myAppearance.IsNull() == Standard_False)
364 aResult->SetAppearance(Handle(VrmlData_Appearance)::DownCast
365 (myAppearance->Clone (aDummyNode)));
366 if (myGeometry.IsNull() == Standard_False)
367 aResult->SetGeometry (Handle(VrmlData_Geometry)::DownCast
368 (myGeometry->Clone (aDummyNode)));
373 //=======================================================================
374 //function : VrmlData_ShapeNode::Read
376 //=======================================================================
378 VrmlData_ErrorStatus VrmlData_ShapeNode::Read (VrmlData_InBuffer& theBuffer)
380 VrmlData_ErrorStatus aStatus;
381 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
382 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "appearance"))
383 aStatus = ReadNode (theBuffer, myAppearance,
384 STANDARD_TYPE(VrmlData_Appearance));
385 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "geometry"))
386 aStatus = ReadNode (theBuffer, myGeometry);
387 // here we do not check for the Geometry type because unknown node types can
388 // occur (IndexedLineSet, etc.)
389 // STANDARD_TYPE(VrmlData_Geometry));
397 // Read the terminating (closing) brace
399 aStatus = readBrace (theBuffer);
403 //=======================================================================
404 //function : VrmlData_ShapeNode::Write
406 //=======================================================================
408 VrmlData_ErrorStatus VrmlData_ShapeNode::Write (const char * thePrefix) const
410 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
411 const VrmlData_Scene& aScene = Scene();
412 static char header[] = "Shape {";
413 if (OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent())))
415 if (myAppearance.IsNull() == Standard_False)
416 aStatus = aScene.WriteNode ("appearance", myAppearance);
417 if (myGeometry.IsNull() == Standard_False && OK(aStatus))
418 aStatus = aScene.WriteNode ("geometry", myGeometry);
420 aStatus = WriteClosing();
425 //=======================================================================
426 //function : VrmlData_ShapeNode::IsDefault
428 //=======================================================================
430 Standard_Boolean VrmlData_ShapeNode::IsDefault () const
432 Standard_Boolean aResult (Standard_True);
433 if (myGeometry.IsNull() == Standard_False)
434 aResult = myGeometry->IsDefault();
438 //=======================================================================
439 //function : VrmlData_UnknownNode::Read
441 //=======================================================================
443 VrmlData_ErrorStatus VrmlData_UnknownNode::Read (VrmlData_InBuffer& theBuffer)
445 VrmlData_ErrorStatus aStatus = VrmlData_StatusOK;
446 Standard_Integer aLevelCounter (0);
447 // This loop searches for any opening brace.
448 // Such brace increments the level counter. A closing brace decrements
449 // the counter. The loop terminates when the counter becomes negative.
450 while (aLevelCounter >= 0 &&
451 OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
454 while ((aChar = theBuffer.LinePtr[0]) != '\0') {
459 } else if (aChar == '}') {
468 //=======================================================================
469 //function : VrmlData_UnknownNode::IsDefault
471 //=======================================================================
473 Standard_Boolean VrmlData_UnknownNode::IsDefault () const
475 return Standard_True;
478 //=======================================================================
479 //function : VrmlData_Appearance::Clone
481 //=======================================================================
483 Handle(VrmlData_Node) VrmlData_Appearance::Clone
484 (const Handle(VrmlData_Node)& theOther) const
486 Handle(VrmlData_Appearance) aResult =
487 Handle(VrmlData_Appearance)::DownCast (VrmlData_Node::Clone(theOther));
488 if (aResult.IsNull())
489 aResult = new VrmlData_Appearance
490 (theOther.IsNull() ? Scene() : theOther->Scene(), Name());
491 if (&aResult->Scene() == &Scene()) {
492 aResult->SetMaterial (myMaterial);
493 aResult->SetTexture (myTexture);
494 aResult->SetTextureTransform (myTTransform);
496 // Create a dummy node to pass the different Scene instance to methods Clone
497 const Handle(VrmlData_UnknownNode) aDummyNode =
498 new VrmlData_UnknownNode (aResult->Scene());
499 if (myMaterial.IsNull() == Standard_False)
500 aResult->SetMaterial (Handle(VrmlData_Material)::DownCast
501 (myMaterial->Clone (aDummyNode)));
502 if (myTexture.IsNull() == Standard_False)
503 aResult->SetTexture(Handle(VrmlData_Texture)::DownCast
504 (myTexture->Clone (aDummyNode)));
505 if (myTTransform.IsNull() == Standard_False)
506 aResult->SetTextureTransform(Handle(VrmlData_TextureTransform)::DownCast
507 (myTTransform->Clone (aDummyNode)));
512 //=======================================================================
513 //function : VrmlData_Appearance::Read
515 //=======================================================================
517 VrmlData_ErrorStatus VrmlData_Appearance::Read (VrmlData_InBuffer& theBuffer)
519 VrmlData_ErrorStatus aStatus;
520 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
521 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "material"))
522 aStatus = ReadNode (theBuffer, myMaterial,
523 STANDARD_TYPE(VrmlData_Material));
524 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "textureTransform"))
525 aStatus = ReadNode (theBuffer, myTTransform
526 /*,STANDARD_TYPE(VrmlData_TextureTransform)*/);
527 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "texture"))
528 aStatus = ReadNode (theBuffer, myTexture,
529 STANDARD_TYPE(VrmlData_Texture));
537 // Read the terminating (closing) brace
539 aStatus = readBrace (theBuffer);
543 //=======================================================================
544 //function : VrmlData_Appearance::Write
546 //=======================================================================
548 VrmlData_ErrorStatus VrmlData_Appearance::Write (const char * thePrefix) const
550 static char header[] = "Appearance {";
551 VrmlData_ErrorStatus aStatus;
552 const VrmlData_Scene& aScene = Scene();
553 if (OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent())))
555 if (myMaterial.IsNull() == Standard_False)
556 aStatus = aScene.WriteNode ("material", myMaterial);
557 if (myTexture.IsNull() == Standard_False && OK(aStatus))
558 aStatus = aScene.WriteNode ("texture", myTexture);
559 if (myTTransform.IsNull() == Standard_False && OK(aStatus))
560 aStatus = aScene.WriteNode ("textureTransform", myTTransform);
562 aStatus = WriteClosing();
567 //=======================================================================
568 //function : IsDefault
570 //=======================================================================
572 Standard_Boolean VrmlData_Appearance::IsDefault () const
574 Standard_Boolean aResult (Standard_True);
575 if (myMaterial.IsNull() == Standard_False)
576 aResult = myMaterial->IsDefault();
577 if (aResult == Standard_False)
578 if (myTexture.IsNull() == Standard_False)
579 aResult = myTexture->IsDefault();
580 if (aResult == Standard_False)
581 if (myTTransform.IsNull() == Standard_False)
582 aResult = myTTransform->IsDefault();
586 //=======================================================================
587 //function : VrmlData_ImageTexture
588 //purpose : Constructor
589 //=======================================================================
591 VrmlData_ImageTexture::VrmlData_ImageTexture (const VrmlData_Scene& theScene,
592 const char * theName,
594 const Standard_Boolean theRepS,
595 const Standard_Boolean theRepT)
596 : VrmlData_Texture (theScene, theName, theRepS, theRepT),
597 myURL (theScene.Allocator())
599 myURL.Append (theURL ? (Standard_CString)theURL : "");
602 //=======================================================================
603 //function : VrmlData_ImageTexture::Clone
605 //=======================================================================
607 Handle(VrmlData_Node) VrmlData_ImageTexture::Clone
608 (const Handle(VrmlData_Node)& theOther) const
610 Handle(VrmlData_ImageTexture) aResult =
611 Handle(VrmlData_ImageTexture)::DownCast (VrmlData_Node::Clone(theOther));
612 if (aResult.IsNull())
614 new VrmlData_ImageTexture(theOther.IsNull() ? Scene() : theOther->Scene(),
616 aResult->myURL = myURL;
620 //=======================================================================
621 //function : VrmlData_ImageTexture::Read
623 //=======================================================================
625 VrmlData_ErrorStatus VrmlData_ImageTexture::Read (VrmlData_InBuffer& theBuffer)
627 VrmlData_ErrorStatus aStatus;
628 Standard_Boolean aRepeatS (Standard_True), aRepeatT (Standard_True);
630 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
631 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "url"))
632 aStatus = ReadMultiString (theBuffer, myURL);
633 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "repeatS"))
634 aStatus = ReadBoolean (theBuffer, aRepeatS);
635 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "repeatT"))
636 aStatus = ReadBoolean (theBuffer, aRepeatT);
643 if (OK(aStatus) && OK(aStatus, readBrace (theBuffer))) {
644 SetRepeatS (aRepeatS);
645 SetRepeatT (aRepeatT);