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>
29 IMPLEMENT_STANDARD_RTTIEXT(VrmlData_IndexedLineSet,VrmlData_Geometry)
32 #define _CRT_SECURE_NO_DEPRECATE
33 #pragma warning (disable:4996)
37 //=======================================================================
40 //=======================================================================
42 Quantity_Color VrmlData_IndexedLineSet::GetColor
43 (const Standard_Integer /*iFace*/,
44 const Standard_Integer /*iVertex*/)
47 return Quantity_NOC_BLACK;
50 //=======================================================================
52 //purpose : Query the shape. This method checks the flag myIsModified;
53 // if True it should rebuild the shape presentation.
54 //=======================================================================
56 const Handle(TopoDS_TShape)& VrmlData_IndexedLineSet::TShape ()
58 if (myNbPolygons == 0)
60 else if (myIsModified) {
62 BRep_Builder aBuilder;
63 const gp_XYZ * arrNodes = myCoords->Values();
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]]);
77 const Handle(Poly_Polygon3D) aPolyPolygon =
78 new Poly_Polygon3D (arrPoint, arrParam);
80 aBuilder.MakeEdge (anEdge, aPolyPolygon);
81 aBuilder.Add (aWire, anEdge);
83 myTShape = aWire.TShape();
88 //=======================================================================
90 //purpose : Create a copy of this node
91 //=======================================================================
93 Handle(VrmlData_Node) VrmlData_IndexedLineSet::Clone
94 (const Handle(VrmlData_Node)& theOther) const
96 Handle(VrmlData_IndexedLineSet) aResult =
97 Handle(VrmlData_IndexedLineSet)::DownCast (VrmlData_Node::Clone(theOther));
100 new VrmlData_IndexedLineSet(theOther.IsNull()? Scene(): theOther->Scene(),
103 if (&aResult->Scene() == &Scene()) {
104 aResult->SetCoordinates (myCoords);
105 aResult->SetColors (myColors);
106 aResult->SetPolygons (myNbPolygons, myArrPolygons);
107 aResult->SetColorInd (myNbColors, myArrColorInd);
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);
122 aResult->SetColorPerVertex (myColorPerVertex);
126 //=======================================================================
128 //purpose : Read the Node from input stream.
129 //=======================================================================
131 VrmlData_ErrorStatus VrmlData_IndexedLineSet::Read
132 (VrmlData_InBuffer& theBuffer)
134 VrmlData_ErrorStatus aStatus;
135 const VrmlData_Scene& aScene = Scene();
136 while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
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"))
148 Handle(VrmlData_Node) aNode;
149 aStatus = ReadNode (theBuffer, aNode,
150 STANDARD_TYPE(VrmlData_Color));
151 myColors = Handle(VrmlData_Color)::DownCast (aNode);
153 else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "coord"))
155 Handle(VrmlData_Node) aNode;
156 aStatus = ReadNode (theBuffer, aNode,
157 STANDARD_TYPE(VrmlData_Coordinate));
158 myCoords = Handle(VrmlData_Coordinate)::DownCast (aNode);
165 // Read the terminating (closing) brace
166 if (OK(aStatus) || aStatus == VrmlData_EmptyData)
167 if (OK(aStatus, readBrace (theBuffer))) {
174 //=======================================================================
176 //purpose : Write the Node to output stream
177 //=======================================================================
179 VrmlData_ErrorStatus VrmlData_IndexedLineSet::Write
180 (const char * thePrefix) const
182 static char header[] = "IndexedLineSet {";
183 const VrmlData_Scene& aScene = Scene();
184 VrmlData_ErrorStatus aStatus;
185 if (OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent()))) {
187 if (OK(aStatus) && myCoords.IsNull() == Standard_False)
188 aStatus = aScene.WriteNode ("coord", myCoords);
190 aStatus = aScene.WriteArrIndex ("coordIndex", myArrPolygons,myNbPolygons);
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);
197 aStatus = aScene.WriteArrIndex ("colorIndex", myArrColorInd, myNbColors);
199 aStatus = WriteClosing();
204 //=======================================================================
205 //function : IsDefault
206 //purpose : Returns True if the node is default,
207 // so that it should not be written.
208 //=======================================================================
210 Standard_Boolean VrmlData_IndexedLineSet::IsDefault () const
212 Standard_Boolean aResult (Standard_True);
214 aResult = Standard_False;
215 else if (myCoords.IsNull() == Standard_False)
216 aResult = myCoords->IsDefault();