0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / BinMDataXtd / BinMDataXtd_TriangulationDriver.cxx
CommitLineData
624f732f 1// Created on: 2016-11-10
2// Created by: Anton KOZULIN
3// Copyright (c) 2016 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 <BinMDataXtd_TriangulationDriver.hxx>
17#include <BinObjMgt_Persistent.hxx>
83ae3591 18#include <Message_Messenger.hxx>
624f732f 19#include <Standard_Type.hxx>
20#include <TDataXtd_Triangulation.hxx>
21#include <TDF_Attribute.hxx>
22
23IMPLEMENT_STANDARD_RTTIEXT(BinMDataXtd_TriangulationDriver,BinMDF_ADriver)
24
25//=======================================================================
26//function : BinMDataXtd_TriangulationDriver
27//purpose : Constructor
28//=======================================================================
83ae3591 29BinMDataXtd_TriangulationDriver::BinMDataXtd_TriangulationDriver(const Handle(Message_Messenger)& theMsgDriver)
624f732f 30 : BinMDF_ADriver (theMsgDriver, STANDARD_TYPE(TDataXtd_Triangulation)->Name())
31{
32
33}
34
35//=======================================================================
36//function : NewEmpty
37//purpose :
38//=======================================================================
39Handle(TDF_Attribute) BinMDataXtd_TriangulationDriver::NewEmpty() const
40{
41 return new TDataXtd_Triangulation();
42}
43
44//=======================================================================
45//function : Paste
46//purpose : persistent -> transient (retrieve)
47//=======================================================================
48Standard_Boolean BinMDataXtd_TriangulationDriver::Paste(const BinObjMgt_Persistent& theSource,
49 const Handle(TDF_Attribute)& theTarget,
50 BinObjMgt_RRelocationTable& ) const
51{
52 Handle(TDataXtd_Triangulation) attrubute = Handle(TDataXtd_Triangulation)::DownCast(theTarget);
53
54 Standard_Integer i;
55 Standard_Real deflection, x, y, z;
56 Standard_Integer n1, n2, n3;
57 Standard_Integer nbNodes(0), nbTriangles(0);
58 Standard_Boolean hasUV(Standard_False);
59 gp_Pnt p;
60
61 theSource >> nbNodes;
62 theSource >> nbTriangles;
63 theSource >> hasUV;
64 theSource >> deflection;
65
b271edb0 66 if (!nbNodes || !nbTriangles)
67 {
68 return Standard_False;
69 }
70
624f732f 71 // allocate the mesh
72 Handle(Poly_Triangulation) PT = new Poly_Triangulation(nbNodes, nbTriangles, hasUV);
73
74 // deflection
75 PT->Deflection(deflection);
76
77 // read nodes
78 for (i = 1; i <= nbNodes; i++)
79 {
80 theSource >> x;
81 theSource >> y;
82 theSource >> z;
83 PT->ChangeNode(i).SetCoord(x, y, z);
84 }
85
86 // read 2d nodes
87 if (hasUV)
88 {
89 for (i = 1; i <= nbNodes; i++)
90 {
91 theSource >> x;
92 theSource >> y;
93 PT->ChangeUVNode(i).SetCoord(x,y);
94 }
95 }
96
97 // read triangles
98 for (i = 1; i <= nbTriangles; i++)
99 {
100 theSource >> n1;
101 theSource >> n2;
102 theSource >> n3;
103 PT->ChangeTriangle(i).Set(n1, n2, n3);
104 }
105
106 // set triangulation to Ocaf attribute
107 attrubute->Set(PT);
108 return !PT.IsNull();
109}
110
111//=======================================================================
112//function : Paste
113//purpose : transient -> persistent (store)
114//=======================================================================
115void BinMDataXtd_TriangulationDriver::Paste(const Handle(TDF_Attribute)& theSource,
116 BinObjMgt_Persistent& theTarget,
117 BinObjMgt_SRelocationTable& ) const
118{
119 const Handle(TDataXtd_Triangulation) attribute = Handle(TDataXtd_Triangulation)::DownCast(theSource);
120 const Handle(Poly_Triangulation)& PT = attribute->Get();
121 if (!PT.IsNull())
122 {
123 Standard_Integer nbNodes = PT->NbNodes();
124 Standard_Integer nbTriangles = PT->NbTriangles();
125 Standard_Integer n1, n2, n3;
126
127 // write number of elements
128 theTarget << nbNodes;
129 theTarget << nbTriangles;
130 theTarget << (PT->HasUVNodes() ? 1 : 0);
131 // write the deflection
132 theTarget << PT->Deflection();
133
134 // write 3d nodes
135 Standard_Integer i;
136 for (i = 1; i <= nbNodes; i++)
137 {
138 theTarget << PT->Node(i).X();
139 theTarget << PT->Node(i).Y();
140 theTarget << PT->Node(i).Z();
141 }
142
143 // write 2d nodes
144 if (PT->HasUVNodes())
145 {
146 for (i = 1; i <= nbNodes; i++)
147 {
148 theTarget << PT->UVNode(i).X();
149 theTarget << PT->UVNode(i).Y();
150 }
151 }
152
153 // Write triangles
154 const Poly_Array1OfTriangle& Triangles = PT->Triangles();
06bc0a59 155 for (i = 1; i <= nbTriangles; i++)
624f732f 156 {
157 Triangles(i).Get(n1, n2, n3);
158 theTarget << n1;
159 theTarget << n2;
160 theTarget << n3;
161 }
162 }
163}