Loading files
f2929057eThis file is pinned to the selected release. Download the original to open it in your design tool.
// Created on: 2007-08-01
// Created by: Alexander GRIGORIEV
// Copyright (c) 2007-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <VrmlData_IndexedLineSet.hxx>
#include <VrmlData_Scene.hxx>
#include <VrmlData_InBuffer.hxx>
#include <VrmlData_UnknownNode.hxx>
#include <BRep_Builder.hxx>
#include <Poly_Polygon3D.hxx>
#include <NCollection_Array1.hxx>
#include <gp_Pnt.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Wire.hxx>
#include <VrmlData_Color.hxx>
#include <VrmlData_Coordinate.hxx>
IMPLEMENT_STANDARD_RTTIEXT(VrmlData_IndexedLineSet, VrmlData_Geometry)
#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable : 4996)
#endif
//=================================================================================================
Quantity_Color VrmlData_IndexedLineSet::GetColor(const int /*iFace*/, const int /*iVertex*/)
{
// TODO
return Quantity_NOC_BLACK;
}
//=======================================================================
// function : TShape
// purpose : Query the shape. This method checks the flag myIsModified;
// if True it should rebuild the shape presentation.
//=======================================================================
const occ::handle<TopoDS_TShape>& VrmlData_IndexedLineSet::TShape()
{
if (myNbPolygons == 0)
{
myTShape.Nullify();
}
else if (myIsModified)
{
if (myCoords.IsNull() || myArrPolygons == nullptr)
{
myTShape.Nullify();
return myTShape;
}
BRep_Builder aBuilder;
const size_t aNbCoords = myCoords->Length();
// Create the Wire
TopoDS_Wire aWire;
aBuilder.MakeWire(aWire);
bool hasPolyline = false;
for (size_t aPolylineIdx = 0; aPolylineIdx < myNbPolygons; ++aPolylineIdx)
{
if (myArrPolygons[aPolylineIdx] == nullptr)
{
continue;
}
const int* aCoordIndices;
const int aNbPolylineNodes = Polygon(aPolylineIdx, aCoordIndices);
if (aNbPolylineNodes < 2)
{
continue;
}
bool hasInvalidCoordIndex = false;
for (size_t aNodeIdx = 0; aNodeIdx < static_cast<size_t>(aNbPolylineNodes); ++aNodeIdx)
{
const int aCoordIdx = aCoordIndices[aNodeIdx];
if (aCoordIdx < 0 || static_cast<size_t>(aCoordIdx) >= aNbCoords)
{
hasInvalidCoordIndex = true;
break;
}
}
if (hasInvalidCoordIndex)
{
continue;
}
NCollection_Array1<gp_Pnt> aPoints(static_cast<size_t>(aNbPolylineNodes));
NCollection_Array1<double> aParameters(static_cast<size_t>(aNbPolylineNodes));
for (size_t aNodeIdx = 0; aNodeIdx < static_cast<size_t>(aNbPolylineNodes); ++aNodeIdx)
{
aPoints.ChangeAt(aNodeIdx).SetXYZ(
myCoords->Coordinate(static_cast<size_t>(aCoordIndices[aNodeIdx])));
aParameters.ChangeAt(aNodeIdx) = static_cast<double>(aNodeIdx);
}
const occ::handle<Poly_Polygon3D> aPolyPolygon = new Poly_Polygon3D(aPoints, aParameters);
TopoDS_Edge anEdge;
aBuilder.MakeEdge(anEdge, aPolyPolygon);
aBuilder.Add(aWire, anEdge);
hasPolyline = true;
}
if (hasPolyline)
{
myTShape = aWire.TShape();
}
else
{
myTShape.Nullify();
}
}
return myTShape;
}
//=================================================================================================
occ::handle<VrmlData_Node> VrmlData_IndexedLineSet::Clone(
const occ::handle<VrmlData_Node>& theOther) const
{
occ::handle<VrmlData_IndexedLineSet> aResult =
occ::down_cast<VrmlData_IndexedLineSet>(VrmlData_Node::Clone(theOther));
if (aResult.IsNull())
{
aResult = new VrmlData_IndexedLineSet(theOther.IsNull() ? Scene() : theOther->Scene(), Name());
}
if (&aResult->Scene() == &Scene())
{
aResult->SetCoordinates(myCoords);
aResult->SetColors(myColors);
aResult->SetPolygons(myNbPolygons, myArrPolygons);
aResult->SetColorInd(myNbColors, myArrColorInd);
}
else
{
// Create a dummy node to pass the different Scene instance to methods Clone
const occ::handle<VrmlData_UnknownNode> aDummyNode = new VrmlData_UnknownNode(aResult->Scene());
if (!myCoords.IsNull())
{
aResult->SetCoordinates(occ::down_cast<VrmlData_Coordinate>(myCoords->Clone(aDummyNode)));
}
if (!myColors.IsNull())
{
aResult->SetColors(occ::down_cast<VrmlData_Color>(myColors->Clone(aDummyNode)));
}
// TODO: Replace the following lines with the relevant copying
aResult->SetPolygons(myNbPolygons, myArrPolygons);
aResult->SetColorInd(myNbColors, myArrColorInd);
}
aResult->SetColorPerVertex(myColorPerVertex);
return aResult;
}
//=======================================================================
// function : Read
// purpose : Read the Node from input stream.
//=======================================================================
VrmlData_ErrorStatus VrmlData_IndexedLineSet::Read(VrmlData_InBuffer& theBuffer)
{
VrmlData_ErrorStatus aStatus;
const VrmlData_Scene& aScene = Scene();
while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer)))
{
if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "colorPerVertex"))
{
aStatus = ReadBoolean(theBuffer, myColorPerVertex);
}
else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "coordIndex"))
{
aStatus = aScene.ReadArrIndex(theBuffer, myArrPolygons, myNbPolygons);
}
else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "colorIndex"))
{
aStatus = aScene.ReadArrIndex(theBuffer, myArrColorInd, myNbColors);
// These two checks should be the last one to avoid their interference
// with the other tokens (e.g., coordIndex)
}
else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "color"))
{
occ::handle<VrmlData_Node> aNode;
aStatus = ReadNode(theBuffer, aNode, STANDARD_TYPE(VrmlData_Color));
myColors = occ::down_cast<VrmlData_Color>(aNode);
}
else if (VRMLDATA_LCOMPARE(theBuffer.LinePtr, "coord"))
{
occ::handle<VrmlData_Node> aNode;
aStatus = ReadNode(theBuffer, aNode, STANDARD_TYPE(VrmlData_Coordinate));
myCoords = occ::down_cast<VrmlData_Coordinate>(aNode);
}
else
{
break;
}
if (!OK(aStatus))
{
break;
}
}
// Read the terminating (closing) brace
if (OK(aStatus) || aStatus == VrmlData_EmptyData)
{
if (OK(aStatus, readBrace(theBuffer)))
{
// Post-processing
;
}
}
return aStatus;
}
//=======================================================================
// function : Write
// purpose : Write the Node to output stream
//=======================================================================
VrmlData_ErrorStatus VrmlData_IndexedLineSet::Write(const char* thePrefix) const
{
static char header[] = "IndexedLineSet {";
const VrmlData_Scene& aScene = Scene();
VrmlData_ErrorStatus aStatus;
if (OK(aStatus, aScene.WriteLine(thePrefix, header, GlobalIndent())))
{
if (OK(aStatus) && !myCoords.IsNull())
{
aStatus = aScene.WriteNode("coord", myCoords);
}
if (OK(aStatus))
{
aStatus = aScene.WriteArrIndex("coordIndex", myArrPolygons, myNbPolygons);
}
if (OK(aStatus) && !myColorPerVertex)
{
aStatus = aScene.WriteLine("colorPerVertex FALSE");
}
if (OK(aStatus) && !myColors.IsNull())
{
aStatus = aScene.WriteNode("color", myColors);
}
if (OK(aStatus))
{
aStatus = aScene.WriteArrIndex("colorIndex", myArrColorInd, myNbColors);
}
aStatus = WriteClosing();
}
return aStatus;
}
//=======================================================================
// function : IsDefault
// purpose : Returns True if the node is default,
// so that it should not be written.
//=======================================================================
bool VrmlData_IndexedLineSet::IsDefault() const
{
bool aResult(true);
if (myNbPolygons)
{
aResult = false;
}
else if (!myCoords.IsNull())
{
aResult = myCoords->IsDefault();
}
return aResult;
}