0026377: Passing Handle objects as arguments to functions as non-const reference...
[occt.git] / src / VrmlData / VrmlData_IndexedLineSet.cxx
1 // Created on: 2007-08-01
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <VrmlData_IndexedLineSet.hxx>
17 #include <VrmlData_Scene.hxx>
18 #include <VrmlData_InBuffer.hxx>
19 #include <VrmlData_UnknownNode.hxx>
20 #include <BRep_Builder.hxx>
21 #include <Poly_Polygon3D.hxx>
22 #include <TColStd_Array1OfReal.hxx>
23 #include <TColgp_Array1OfPnt.hxx>
24 #include <TopoDS_Edge.hxx>
25 #include <TopoDS_Wire.hxx>
26 #include <VrmlData_Color.hxx>
27 #include <VrmlData_Coordinate.hxx>
28
29 IMPLEMENT_STANDARD_RTTIEXT(VrmlData_IndexedLineSet,VrmlData_Geometry)
30
31 #ifdef _MSC_VER
32 #define _CRT_SECURE_NO_DEPRECATE
33 #pragma warning (disable:4996)
34 #endif
35
36
37 //=======================================================================
38 //function : GetColor
39 //purpose  : 
40 //=======================================================================
41
42 Quantity_Color VrmlData_IndexedLineSet::GetColor
43                                         (const Standard_Integer /*iFace*/,
44                                          const Standard_Integer /*iVertex*/)
45 {
46   //TODO
47   return Quantity_NOC_BLACK;
48 }
49
50 //=======================================================================
51 //function : TShape
52 //purpose  : Query the shape. This method checks the flag myIsModified;
53 //           if True it should rebuild the shape presentation.
54 //=======================================================================
55
56 const Handle(TopoDS_TShape)& VrmlData_IndexedLineSet::TShape ()
57 {
58   if (myNbPolygons == 0)
59     myTShape.Nullify();
60   else if (myIsModified) {
61     Standard_Integer i;
62     BRep_Builder aBuilder;
63     const gp_XYZ * arrNodes = myCoords->Values();
64
65     // Create the Wire
66     TopoDS_Wire aWire;
67     aBuilder.MakeWire(aWire);
68     for (i = 0; i < (int)myNbPolygons; i++) {
69       const Standard_Integer * arrIndice;
70       const Standard_Integer nNodes = Polygon(i, arrIndice);
71       TColgp_Array1OfPnt   arrPoint (1, nNodes);
72       TColStd_Array1OfReal arrParam (1, nNodes);
73       for (Standard_Integer j = 0; j < nNodes; j++) {
74         arrPoint(j+1).SetXYZ (arrNodes[arrIndice[j]]);
75         arrParam(j+1) = j;
76       }
77       const Handle(Poly_Polygon3D) aPolyPolygon =
78         new Poly_Polygon3D (arrPoint, arrParam);
79       TopoDS_Edge anEdge;
80       aBuilder.MakeEdge (anEdge, aPolyPolygon);
81       aBuilder.Add (aWire, anEdge);
82     }
83     myTShape = aWire.TShape();
84   }
85   return myTShape;
86 }
87
88 //=======================================================================
89 //function : Clone
90 //purpose  : Create a copy of this node
91 //=======================================================================
92
93 Handle(VrmlData_Node) VrmlData_IndexedLineSet::Clone
94                                 (const Handle(VrmlData_Node)& theOther) const
95 {
96   Handle(VrmlData_IndexedLineSet) aResult =
97     Handle(VrmlData_IndexedLineSet)::DownCast (VrmlData_Node::Clone(theOther));
98   if (aResult.IsNull())
99     aResult =
100       new VrmlData_IndexedLineSet(theOther.IsNull()? Scene(): theOther->Scene(),
101                                   Name());
102
103   if (&aResult->Scene() == &Scene()) {
104     aResult->SetCoordinates (myCoords);
105     aResult->SetColors      (myColors);
106     aResult->SetPolygons    (myNbPolygons, myArrPolygons);
107     aResult->SetColorInd    (myNbColors, myArrColorInd);
108   } else {
109     // Create a dummy node to pass the different Scene instance to methods Clone
110     const Handle(VrmlData_UnknownNode) aDummyNode =
111       new VrmlData_UnknownNode (aResult->Scene());
112     if (myCoords.IsNull() == Standard_False)
113       aResult->SetCoordinates (Handle(VrmlData_Coordinate)::DownCast
114                                (myCoords->Clone (aDummyNode)));
115     if (myColors.IsNull() == Standard_False)
116       aResult->SetColors (Handle(VrmlData_Color)::DownCast
117                           (myColors->Clone (aDummyNode)));
118     //TODO: Replace the following lines with the relevant copying
119     aResult->SetPolygons    (myNbPolygons, myArrPolygons);
120     aResult->SetColorInd    (myNbColors, myArrColorInd);
121   }
122   aResult->SetColorPerVertex  (myColorPerVertex);
123   return aResult;
124 }
125
126 //=======================================================================
127 //function : Read
128 //purpose  : Read the Node from input stream.
129 //=======================================================================
130
131 VrmlData_ErrorStatus VrmlData_IndexedLineSet::Read
132                                         (VrmlData_InBuffer& theBuffer)
133 {
134   VrmlData_ErrorStatus aStatus;
135   const VrmlData_Scene& aScene = Scene();
136   while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
137   {
138     if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "colorPerVertex"))
139       aStatus = ReadBoolean (theBuffer, myColorPerVertex);
140     else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "coordIndex"))
141       aStatus = aScene.ReadArrIndex (theBuffer, myArrPolygons, myNbPolygons);
142     else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "colorIndex"))
143       aStatus = aScene.ReadArrIndex (theBuffer, myArrColorInd, myNbColors);
144     // These two checks should be the last one to avoid their interference
145     // with the other tokens (e.g., coordIndex)
146     else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "color"))
147     {
148       Handle(VrmlData_Node) aNode;
149       aStatus = ReadNode (theBuffer, aNode,
150                           STANDARD_TYPE(VrmlData_Color));
151       myColors = Handle(VrmlData_Color)::DownCast (aNode);
152     }
153     else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "coord"))
154     {
155       Handle(VrmlData_Node) aNode;
156       aStatus = ReadNode (theBuffer, aNode,
157                           STANDARD_TYPE(VrmlData_Coordinate));
158       myCoords = Handle(VrmlData_Coordinate)::DownCast (aNode);
159     }
160     else
161       break;
162     if (!OK(aStatus))
163       break;
164   }
165   // Read the terminating (closing) brace
166   if (OK(aStatus) || aStatus == VrmlData_EmptyData)
167     if (OK(aStatus, readBrace (theBuffer))) {
168       // Post-processing
169       ;
170     }
171   return aStatus;
172 }
173
174 //=======================================================================
175 //function : Write
176 //purpose  : Write the Node to output stream
177 //=======================================================================
178
179 VrmlData_ErrorStatus VrmlData_IndexedLineSet::Write
180                                         (const char * thePrefix) const
181 {
182   static char header[] = "IndexedLineSet {";
183   const VrmlData_Scene& aScene = Scene();
184   VrmlData_ErrorStatus aStatus;
185   if (OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent()))) {
186
187     if (OK(aStatus) && myCoords.IsNull() == Standard_False)
188       aStatus = aScene.WriteNode ("coord", myCoords);
189     if (OK(aStatus))
190       aStatus = aScene.WriteArrIndex ("coordIndex", myArrPolygons,myNbPolygons);
191
192     if (OK(aStatus) && myColorPerVertex == Standard_False)
193       aStatus = aScene.WriteLine ("colorPerVertex  FALSE");
194     if (OK(aStatus) && myColors.IsNull() == Standard_False)
195       aStatus = aScene.WriteNode ("color", myColors);
196     if (OK(aStatus))
197       aStatus = aScene.WriteArrIndex ("colorIndex", myArrColorInd, myNbColors);
198
199     aStatus = WriteClosing();
200   }
201   return aStatus;
202 }
203
204 //=======================================================================
205 //function : IsDefault
206 //purpose  : Returns True if the node is default,
207 //           so that it should not be written.
208 //=======================================================================
209
210 Standard_Boolean VrmlData_IndexedLineSet::IsDefault () const
211 {
212   Standard_Boolean aResult (Standard_True);
213   if (myNbPolygons)
214     aResult = Standard_False;
215   else if (myCoords.IsNull() == Standard_False)
216     aResult = myCoords->IsDefault();
217   return aResult;
218 }
219