1 // Created on: 2006-05-25
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2006-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <Precision.hxx>
22 #include <VrmlData_Appearance.hxx>
23 #include <VrmlData_ImageTexture.hxx>
24 #include <VrmlData_Material.hxx>
25 #include <VrmlData_ShapeNode.hxx>
26 #include <VrmlData_UnknownNode.hxx>
27 #include <VrmlData_Scene.hxx>
28 #include <VrmlData_InBuffer.hxx>
33 #define _CRT_SECURE_NO_DEPRECATE
34 #pragma warning (disable:4996)
37 IMPLEMENT_STANDARD_HANDLE (VrmlData_Node, Standard_Transient)
38 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_Node, Standard_Transient)
39 IMPLEMENT_STANDARD_HANDLE (VrmlData_ShapeNode, VrmlData_Node)
40 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_ShapeNode, VrmlData_Node)
41 IMPLEMENT_STANDARD_HANDLE (VrmlData_Texture, VrmlData_Node)
42 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_Texture, VrmlData_Node)
43 IMPLEMENT_STANDARD_HANDLE (VrmlData_TextureTransform, VrmlData_Node)
44 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_TextureTransform, VrmlData_Node)
45 IMPLEMENT_STANDARD_HANDLE (VrmlData_ImageTexture, VrmlData_Texture)
46 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_ImageTexture, VrmlData_Texture)
47 IMPLEMENT_STANDARD_HANDLE (VrmlData_Appearance, VrmlData_Node)
48 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_Appearance, VrmlData_Node)
49 IMPLEMENT_STANDARD_HANDLE (VrmlData_UnknownNode, VrmlData_Node)
50 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_UnknownNode, VrmlData_Node)
52 static VrmlData_Scene MyDefaultScene;
54 //=======================================================================
56 //purpose : Global method
57 //=======================================================================
59 Standard_Boolean IsEqual (const Handle(VrmlData_Node)& theOne,
60 const Handle(VrmlData_Node)& theTwo)
62 Standard_Boolean aResult (Standard_False);
63 if (theOne->Name() != 0L && theTwo->Name() != 0L)
64 aResult = (strcmp (theOne->Name(), theTwo->Name()) == 0);
68 //=======================================================================
70 //purpose : Global method
71 //=======================================================================
73 Standard_Integer HashCode(const Handle(VrmlData_Node)& theNode,
74 const Standard_Integer theUpper)
76 return (theNode->Name() == 0L ? 0
77 : HashCode((Standard_CString)theNode->Name(), theUpper));
80 //=======================================================================
81 //function : VrmlData_Node
83 //=======================================================================
85 VrmlData_Node::VrmlData_Node ()
86 : myScene (&MyDefaultScene),
89 //=======================================================================
90 //function : VrmlData_Node
91 //purpose : Constructor
92 //=======================================================================
94 VrmlData_Node::VrmlData_Node (const VrmlData_Scene& theScene,
103 //=======================================================================
105 //purpose : Create a copy of this node.
106 //=======================================================================
108 Handle(VrmlData_Node) VrmlData_Node::Clone
109 (const Handle(VrmlData_Node)& theOther) const
111 if (theOther.IsNull() == Standard_False) {
112 if (theOther->IsKind (DynamicType()) == Standard_False)
114 if (&theOther->Scene() == myScene)
115 theOther->myName = myName;
117 theOther->setName (myName);
122 //=======================================================================
125 //=======================================================================
127 void VrmlData_Node::setName (const char * theName, const char * theSuffix)
134 len[1] = strlen (theSuffix);
135 char * aName = (char *)Scene().Allocator()->Allocate(len[0]+len[1]);
137 memcpy (aName, theName, len[0]);
139 memcpy (&aName[len[0] - 1], theSuffix, len[1]+1);
142 //=======================================================================
143 //function : IsDefault
145 //=======================================================================
147 Standard_Boolean VrmlData_Node::IsDefault () const
149 return Standard_False;
153 //=======================================================================
156 //=======================================================================
158 VrmlData_ErrorStatus VrmlData_Node::Write (const char *) const
160 return VrmlData_NotImplemented;
163 //=======================================================================
164 //function : WriteClosing
166 //=======================================================================
168 VrmlData_ErrorStatus VrmlData_Node::WriteClosing () const
170 VrmlData_ErrorStatus aResult = Scene().Status();
171 if (aResult == VrmlData_StatusOK || aResult == VrmlData_NotImplemented)
172 aResult = Scene().WriteLine ("}", 0L, -GlobalIndent());
176 //=======================================================================
177 //function : readBrace
179 //=======================================================================
181 VrmlData_ErrorStatus VrmlData_Node::readBrace (VrmlData_InBuffer& theBuffer)
183 VrmlData_ErrorStatus aStatus;
184 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
185 if (theBuffer.LinePtr[0] == '}')
188 aStatus = VrmlData_VrmlFormatError;
192 //=======================================================================
193 //function : ReadBoolean
195 //=======================================================================
197 VrmlData_ErrorStatus VrmlData_Node::ReadBoolean (VrmlData_InBuffer& theBuffer,
198 Standard_Boolean& theResult)
200 VrmlData_ErrorStatus aStatus;
201 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
202 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "TRUE"))
203 theResult = Standard_True;
204 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "FALSE"))
205 theResult = Standard_False;
207 aStatus = VrmlData_BooleanInputError;
212 //=======================================================================
213 //function : ReadInteger
215 //=======================================================================
217 VrmlData_ErrorStatus VrmlData_Node::ReadInteger (VrmlData_InBuffer& theBuffer,
220 VrmlData_ErrorStatus aStatus;
221 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
224 aResult = strtol (theBuffer.LinePtr, &endptr, 10);
225 if (endptr == theBuffer.LinePtr)
226 aStatus = VrmlData_NumericInputError;
229 theBuffer.LinePtr = endptr;
235 //=======================================================================
236 //function : ReadString
238 //=======================================================================
240 VrmlData_ErrorStatus VrmlData_Node::ReadString
241 (VrmlData_InBuffer& theBuffer,
242 TCollection_AsciiString& theResult)
244 VrmlData_ErrorStatus aStatus;
245 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
246 if (theBuffer.LinePtr[0] != '\"')
247 aStatus = VrmlData_StringInputError;
249 char * ptr = &theBuffer.LinePtr[1];
250 while (*ptr != '\0' && *ptr != '\"')
253 aStatus = VrmlData_StringInputError;
256 theResult = (Standard_CString) &theBuffer.LinePtr[1];
257 theBuffer.LinePtr = ptr+1;
264 //=======================================================================
265 //function : ReadMultiString
267 //=======================================================================
269 VrmlData_ErrorStatus VrmlData_Node::ReadMultiString
270 (VrmlData_InBuffer& theBuffer,
271 NCollection_List<TCollection_AsciiString>& theResult)
273 VrmlData_ErrorStatus aStatus;
274 Standard_Boolean isBracketed (Standard_False);
275 // Read the list of URL
276 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
277 if (theBuffer.LinePtr[0] == '[') {
279 isBracketed = Standard_True;
281 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
282 if (isBracketed && theBuffer.LinePtr[0] == ']') { // closing bracket
286 TCollection_AsciiString aString;
287 if (!OK(aStatus, ReadString(theBuffer, aString)))
289 theResult.Append(aString);
290 if (isBracketed == Standard_False ||
291 !OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
293 if (theBuffer.LinePtr[0] == ',') {
296 } else if (theBuffer.LinePtr[0] == ']') // closing bracket
299 aStatus = VrmlData_VrmlFormatError;
306 //=======================================================================
307 //function : ReadNode
309 //=======================================================================
311 VrmlData_ErrorStatus VrmlData_Node::ReadNode
312 (VrmlData_InBuffer& theBuffer,
313 Handle(VrmlData_Node)& theNode,
314 const Handle(Standard_Type)& theType)
316 Handle(VrmlData_Node) aNode;
317 VrmlData_ErrorStatus aStatus;
318 // First line of a new node should identify this node type
319 if (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
320 if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "USE")) {
321 TCollection_AsciiString aName;
322 aStatus = VrmlData_Scene::ReadWord (theBuffer, aName);
323 if (aStatus == VrmlData_StatusOK) {
324 aNode = myScene->FindNode (aName.ToCString(), theType);
326 aStatus = VrmlData_NodeNameUnknown;
328 // aNode = aNode->Clone(0L);
332 // We create a relevant node using the line with the type ID
334 const_cast<VrmlData_Scene *>(myScene)->createNode (theBuffer,
337 if (aNode.IsNull() == Standard_False)
338 // The node data are read here, including the final closing brace
339 aStatus = aNode->Read(theBuffer);
341 if (aStatus == VrmlData_StatusOK)
347 //=======================================================================
348 //function : VrmlData_ShapeNode::Clone
350 //=======================================================================
352 Handle(VrmlData_Node) VrmlData_ShapeNode::Clone
353 (const Handle(VrmlData_Node)& theOther) const
355 Handle(VrmlData_ShapeNode) aResult =
356 Handle(VrmlData_ShapeNode)::DownCast (VrmlData_Node::Clone(theOther));
357 if (aResult.IsNull())
358 aResult= new VrmlData_ShapeNode(theOther.IsNull()?Scene():theOther->Scene(),
360 if (&aResult->Scene() == &Scene()) {
361 aResult->SetAppearance (myAppearance);
362 aResult->SetGeometry (myGeometry);
364 // Create a dummy node to pass the different Scene instance to methods Clone
365 const Handle(VrmlData_UnknownNode) aDummyNode =
366 new VrmlData_UnknownNode (aResult->Scene());
367 if (myAppearance.IsNull() == Standard_False)
368 aResult->SetAppearance(Handle(VrmlData_Appearance)::DownCast
369 (myAppearance->Clone (aDummyNode)));
370 if (myGeometry.IsNull() == Standard_False)
371 aResult->SetGeometry (Handle(VrmlData_Geometry)::DownCast
372 (myGeometry->Clone (aDummyNode)));
377 //=======================================================================
378 //function : VrmlData_ShapeNode::Read
380 //=======================================================================
382 VrmlData_ErrorStatus VrmlData_ShapeNode::Read (VrmlData_InBuffer& theBuffer)
384 VrmlData_ErrorStatus aStatus;
385 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
386 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "appearance"))
387 aStatus = ReadNode (theBuffer, myAppearance,
388 STANDARD_TYPE(VrmlData_Appearance));
389 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "geometry"))
390 aStatus = ReadNode (theBuffer, myGeometry);
391 // here we do not check for the Geometry type because unknown node types can
392 // occur (IndexedLineSet, etc.)
393 // STANDARD_TYPE(VrmlData_Geometry));
401 // Read the terminating (closing) brace
403 aStatus = readBrace (theBuffer);
407 //=======================================================================
408 //function : VrmlData_ShapeNode::Write
410 //=======================================================================
412 VrmlData_ErrorStatus VrmlData_ShapeNode::Write (const char * thePrefix) const
414 VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
415 const VrmlData_Scene& aScene = Scene();
416 static char header[] = "Shape {";
417 if (OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent())))
419 if (myAppearance.IsNull() == Standard_False)
420 aStatus = aScene.WriteNode ("appearance", myAppearance);
421 if (myGeometry.IsNull() == Standard_False && OK(aStatus))
422 aStatus = aScene.WriteNode ("geometry", myGeometry);
424 aStatus = WriteClosing();
429 //=======================================================================
430 //function : VrmlData_ShapeNode::IsDefault
432 //=======================================================================
434 Standard_Boolean VrmlData_ShapeNode::IsDefault () const
436 Standard_Boolean aResult (Standard_True);
437 if (myGeometry.IsNull() == Standard_False)
438 aResult = myGeometry->IsDefault();
442 //=======================================================================
443 //function : VrmlData_UnknownNode::Read
445 //=======================================================================
447 VrmlData_ErrorStatus VrmlData_UnknownNode::Read (VrmlData_InBuffer& theBuffer)
449 VrmlData_ErrorStatus aStatus = VrmlData_StatusOK;
450 Standard_Integer aLevelCounter (0);
451 // This loop searches for any opening brace.
452 // Such brace increments the level counter. A closing brace decrements
453 // the counter. The loop terminates when the counter becomes negative.
454 while (aLevelCounter >= 0 &&
455 OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
458 while ((aChar = theBuffer.LinePtr[0]) != '\0') {
463 } else if (aChar == '}') {
472 //=======================================================================
473 //function : VrmlData_UnknownNode::IsDefault
475 //=======================================================================
477 Standard_Boolean VrmlData_UnknownNode::IsDefault () const
479 return Standard_True;
482 //=======================================================================
483 //function : VrmlData_Appearance::Clone
485 //=======================================================================
487 Handle(VrmlData_Node) VrmlData_Appearance::Clone
488 (const Handle(VrmlData_Node)& theOther) const
490 Handle(VrmlData_Appearance) aResult =
491 Handle(VrmlData_Appearance)::DownCast (VrmlData_Node::Clone(theOther));
492 if (aResult.IsNull())
493 aResult = new VrmlData_Appearance
494 (theOther.IsNull() ? Scene() : theOther->Scene(), Name());
495 if (&aResult->Scene() == &Scene()) {
496 aResult->SetMaterial (myMaterial);
497 aResult->SetTexture (myTexture);
498 aResult->SetTextureTransform (myTTransform);
500 // Create a dummy node to pass the different Scene instance to methods Clone
501 const Handle(VrmlData_UnknownNode) aDummyNode =
502 new VrmlData_UnknownNode (aResult->Scene());
503 if (myMaterial.IsNull() == Standard_False)
504 aResult->SetMaterial (Handle(VrmlData_Material)::DownCast
505 (myMaterial->Clone (aDummyNode)));
506 if (myTexture.IsNull() == Standard_False)
507 aResult->SetTexture(Handle(VrmlData_Texture)::DownCast
508 (myTexture->Clone (aDummyNode)));
509 if (myTTransform.IsNull() == Standard_False)
510 aResult->SetTextureTransform(Handle(VrmlData_TextureTransform)::DownCast
511 (myTTransform->Clone (aDummyNode)));
516 //=======================================================================
517 //function : VrmlData_Appearance::Read
519 //=======================================================================
521 VrmlData_ErrorStatus VrmlData_Appearance::Read (VrmlData_InBuffer& theBuffer)
523 VrmlData_ErrorStatus aStatus;
524 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
525 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "material"))
526 aStatus = ReadNode (theBuffer, myMaterial,
527 STANDARD_TYPE(VrmlData_Material));
528 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "textureTransform"))
529 aStatus = ReadNode (theBuffer, myTTransform
530 /*,STANDARD_TYPE(VrmlData_TextureTransform)*/);
531 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "texture"))
532 aStatus = ReadNode (theBuffer, myTexture,
533 STANDARD_TYPE(VrmlData_Texture));
541 // Read the terminating (closing) brace
543 aStatus = readBrace (theBuffer);
547 //=======================================================================
548 //function : VrmlData_Appearance::Write
550 //=======================================================================
552 VrmlData_ErrorStatus VrmlData_Appearance::Write (const char * thePrefix) const
554 static char header[] = "Appearance {";
555 VrmlData_ErrorStatus aStatus;
556 const VrmlData_Scene& aScene = Scene();
557 if (OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent())))
559 if (myMaterial.IsNull() == Standard_False)
560 aStatus = aScene.WriteNode ("material", myMaterial);
561 if (myTexture.IsNull() == Standard_False && OK(aStatus))
562 aStatus = aScene.WriteNode ("texture", myTexture);
563 if (myTTransform.IsNull() == Standard_False && OK(aStatus))
564 aStatus = aScene.WriteNode ("textureTransform", myTTransform);
566 aStatus = WriteClosing();
571 //=======================================================================
572 //function : IsDefault
574 //=======================================================================
576 Standard_Boolean VrmlData_Appearance::IsDefault () const
578 Standard_Boolean aResult (Standard_True);
579 if (myMaterial.IsNull() == Standard_False)
580 aResult = myMaterial->IsDefault();
581 if (aResult == Standard_False)
582 if (myTexture.IsNull() == Standard_False)
583 aResult = myTexture->IsDefault();
584 if (aResult == Standard_False)
585 if (myTTransform.IsNull() == Standard_False)
586 aResult = myTTransform->IsDefault();
590 //=======================================================================
591 //function : VrmlData_ImageTexture
592 //purpose : Constructor
593 //=======================================================================
595 VrmlData_ImageTexture::VrmlData_ImageTexture (const VrmlData_Scene& theScene,
596 const char * theName,
598 const Standard_Boolean theRepS,
599 const Standard_Boolean theRepT)
600 : VrmlData_Texture (theScene, theName, theRepS, theRepT),
601 myURL (theScene.Allocator())
603 myURL.Append (theURL ? (Standard_CString)theURL : "");
606 //=======================================================================
607 //function : VrmlData_ImageTexture::Clone
609 //=======================================================================
611 Handle(VrmlData_Node) VrmlData_ImageTexture::Clone
612 (const Handle(VrmlData_Node)& theOther) const
614 Handle(VrmlData_ImageTexture) aResult =
615 Handle(VrmlData_ImageTexture)::DownCast (VrmlData_Node::Clone(theOther));
616 if (aResult.IsNull())
618 new VrmlData_ImageTexture(theOther.IsNull() ? Scene() : theOther->Scene(),
620 aResult->myURL = myURL;
624 //=======================================================================
625 //function : VrmlData_ImageTexture::Read
627 //=======================================================================
629 VrmlData_ErrorStatus VrmlData_ImageTexture::Read (VrmlData_InBuffer& theBuffer)
631 VrmlData_ErrorStatus aStatus;
632 Standard_Boolean aRepeatS (Standard_True), aRepeatT (Standard_True);
634 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
635 if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "url"))
636 aStatus = ReadMultiString (theBuffer, myURL);
637 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "repeatS"))
638 aStatus = ReadBoolean (theBuffer, aRepeatS);
639 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "repeatT"))
640 aStatus = ReadBoolean (theBuffer, aRepeatT);
647 if (OK(aStatus) && OK(aStatus, readBrace (theBuffer))) {
648 SetRepeatS (aRepeatS);
649 SetRepeatT (aRepeatT);