1 // Created on: 2007-08-01
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2007-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 <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>
30 #define _CRT_SECURE_NO_DEPRECATE
31 #pragma warning (disable:4996)
35 //=======================================================================
38 //=======================================================================
40 Quantity_Color VrmlData_IndexedLineSet::GetColor
41 (const Standard_Integer /*iFace*/,
42 const Standard_Integer /*iVertex*/)
45 return Quantity_NOC_BLACK;
48 //=======================================================================
50 //purpose : Query the shape. This method checks the flag myIsModified;
51 // if True it should rebuild the shape presentation.
52 //=======================================================================
54 const Handle(TopoDS_TShape)& VrmlData_IndexedLineSet::TShape ()
56 if (myNbPolygons == 0)
58 else if (myIsModified) {
60 BRep_Builder aBuilder;
61 const gp_XYZ * arrNodes = myCoords->Values();
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]]);
75 const Handle(Poly_Polygon3D) aPolyPolygon =
76 new Poly_Polygon3D (arrPoint, arrParam);
78 aBuilder.MakeEdge (anEdge, aPolyPolygon);
79 aBuilder.Add (aWire, anEdge);
81 myTShape = aWire.TShape();
86 //=======================================================================
88 //purpose : Create a copy of this node
89 //=======================================================================
91 Handle(VrmlData_Node) VrmlData_IndexedLineSet::Clone
92 (const Handle(VrmlData_Node)& theOther) const
94 Handle(VrmlData_IndexedLineSet) aResult =
95 Handle(VrmlData_IndexedLineSet)::DownCast (VrmlData_Node::Clone(theOther));
98 new VrmlData_IndexedLineSet(theOther.IsNull()? Scene(): theOther->Scene(),
101 if (&aResult->Scene() == &Scene()) {
102 aResult->SetCoordinates (myCoords);
103 aResult->SetColors (myColors);
104 aResult->SetPolygons (myNbPolygons, myArrPolygons);
105 aResult->SetColorInd (myNbColors, myArrColorInd);
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);
120 aResult->SetColorPerVertex (myColorPerVertex);
124 //=======================================================================
126 //purpose : Read the Node from input stream.
127 //=======================================================================
129 VrmlData_ErrorStatus VrmlData_IndexedLineSet::Read
130 (VrmlData_InBuffer& theBuffer)
132 VrmlData_ErrorStatus aStatus;
133 const VrmlData_Scene& aScene = Scene();
134 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
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));
155 // Read the terminating (closing) brace
156 if (OK(aStatus) || aStatus == VrmlData_EmptyData)
157 if (OK(aStatus, readBrace (theBuffer))) {
164 //=======================================================================
166 //purpose : Write the Node to output stream
167 //=======================================================================
169 VrmlData_ErrorStatus VrmlData_IndexedLineSet::Write
170 (const char * thePrefix) const
172 static char header[] = "IndexedLineSet {";
173 const VrmlData_Scene& aScene = Scene();
174 VrmlData_ErrorStatus aStatus;
175 if (OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent()))) {
177 if (OK(aStatus) && myCoords.IsNull() == Standard_False)
178 aStatus = aScene.WriteNode ("coord", myCoords);
180 aStatus = aScene.WriteArrIndex ("coordIndex", myArrPolygons,myNbPolygons);
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);
187 aStatus = aScene.WriteArrIndex ("colorIndex", myArrColorInd, myNbColors);
189 aStatus = WriteClosing();
194 //=======================================================================
195 //function : IsDefault
196 //purpose : Returns True if the node is default,
197 // so that it should not be written.
198 //=======================================================================
200 Standard_Boolean VrmlData_IndexedLineSet::IsDefault () const
202 Standard_Boolean aResult (Standard_True);
204 aResult = Standard_False;
205 else if (myCoords.IsNull() == Standard_False)
206 aResult = myCoords->IsDefault();