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