d39b3b62a5cebfc4252e06ca2962e479f6a3ba7e
[occt.git] / src / VrmlAPI / VrmlAPI_Writer.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <BRep_Tool.hxx>
16 #include <OSD_OpenFile.hxx>
17 #include <OSD_Path.hxx>
18 #include <Poly_Triangulation.hxx>
19 #include <Quantity_HArray1OfColor.hxx>
20 #include <Standard_Stream.hxx>
21 #include <TColStd_HArray1OfReal.hxx>
22 #include <TopAbs_ShapeEnum.hxx>
23 #include <TopExp_Explorer.hxx>
24 #include <TopoDS.hxx>
25 #include <TopoDS_Face.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <TopTools_Array1OfShape.hxx>
28 #include <Vrml.hxx>
29 #include <Vrml_Group.hxx>
30 #include <Vrml_Instancing.hxx>
31 #include <Vrml_Material.hxx>
32 #include <Vrml_Separator.hxx>
33 #include <VrmlAPI_Writer.hxx>
34 #include <VrmlConverter_Drawer.hxx>
35 #include <VrmlConverter_IsoAspect.hxx>
36 #include <VrmlConverter_LineAspect.hxx>
37 #include <VrmlConverter_PointAspect.hxx>
38 #include <VrmlConverter_Projector.hxx>
39 #include <VrmlConverter_ShadedShape.hxx>
40 #include <VrmlConverter_ShadingAspect.hxx>
41 #include <VrmlConverter_WFDeflectionShape.hxx>
42 #include <VrmlData_Scene.hxx>
43 #include <VrmlData_ShapeConvert.hxx>
44
45 VrmlAPI_Writer::VrmlAPI_Writer()
46 {
47   myDrawer = new VrmlConverter_Drawer;
48   myDeflection = -1;
49   Quantity_Color color;
50   color.SetValues(0, 0, 0, Quantity_TOC_RGB);
51   Handle(Quantity_HArray1OfColor) Col1 = new Quantity_HArray1OfColor(1,1);
52   Col1->SetValue(1,color);
53   Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,0.0);
54   Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,0.1);
55   myFrontMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
56   myPointsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
57   myUisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
58   myVisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
59   myLineMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
60   myWireMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
61   myFreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
62   myUnfreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
63   DX = 1;      
64   DY = -1;  
65   DZ = 1;
66   XUp = 0;
67   YUp = 0;
68   ZUp = 1;
69   Focus = 6;
70   ResetToDefaults();
71 }
72
73 void VrmlAPI_Writer::ResetToDefaults() 
74 {
75   myTransparency = 0.0;
76   myShininess = 0.1;
77   Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,myTransparency);
78   Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,myShininess);
79   Handle(Quantity_HArray1OfColor) Col = new Quantity_HArray1OfColor(1,1);
80   Quantity_Color color; 
81   color.SetValues(0, 0, 0, Quantity_TOC_RGB);
82   Col->SetValue(1,color);
83   //
84   myFrontMaterial->SetAmbientColor(Col); myFrontMaterial->SetTransparency(kik1);myFrontMaterial->SetShininess(kik2);
85   myPointsMaterial->SetAmbientColor(Col); myPointsMaterial->SetTransparency(kik1);myPointsMaterial->SetShininess(kik2);
86   myUisoMaterial->SetAmbientColor(Col); myUisoMaterial->SetTransparency(kik1);myUisoMaterial->SetShininess(kik2);
87   myVisoMaterial->SetAmbientColor(Col); myVisoMaterial->SetTransparency(kik1);myVisoMaterial->SetShininess(kik2);
88   myLineMaterial->SetAmbientColor(Col); myLineMaterial->SetTransparency(kik1);myLineMaterial->SetShininess(kik2);
89   myWireMaterial->SetAmbientColor(Col); myWireMaterial->SetTransparency(kik1); myWireMaterial->SetShininess(kik2);
90   myFreeBoundsMaterial->SetAmbientColor(Col); myFreeBoundsMaterial->SetTransparency(kik1);myFreeBoundsMaterial->SetShininess(kik2);
91   myUnfreeBoundsMaterial->SetAmbientColor(Col); myUnfreeBoundsMaterial->SetTransparency(kik1);myUnfreeBoundsMaterial->SetShininess(kik2);
92   //
93   //
94   Handle(Quantity_HArray1OfColor) Col2 = new Quantity_HArray1OfColor(1,1);
95   color.SetValues(0.75, 0.75, 0.75, Quantity_TOC_RGB);
96   Col2->SetValue(1,color);
97   Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
98   color.SetValues(0.82, 0.79, 0.42, Quantity_TOC_RGB);
99   Col3->SetValue(1,color);
100   
101   myUisoMaterial->SetDiffuseColor(Col2);
102   myVisoMaterial->SetDiffuseColor(Col2);
103   myFreeBoundsMaterial->SetDiffuseColor(Col2);
104   myUnfreeBoundsMaterial->SetDiffuseColor(Col2);
105   
106   //
107 //  Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
108 //  color.SetValues(Quantity_NOC_GOLD);
109 //  Col3->SetValue(1,color);
110   myLineMaterial->SetDiffuseColor(Col2);
111   myWireMaterial->SetDiffuseColor(Col2);
112   //
113 //  Handle(Quantity_HArray1OfColor) Col4 = new Quantity_HArray1OfColor(1,1);
114 //  color.SetValues(Quantity_NOC_GOLD);
115 //  Col4->SetValue(1,color);
116   myFrontMaterial->SetDiffuseColor(Col2);
117   myPointsMaterial->SetDiffuseColor(Col2);
118   //
119   
120   myUisoMaterial->SetSpecularColor(Col3);
121   myVisoMaterial->SetSpecularColor(Col3);
122   myFreeBoundsMaterial->SetSpecularColor(Col3);
123   myUnfreeBoundsMaterial->SetSpecularColor(Col3);
124   myLineMaterial->SetSpecularColor(Col3);
125   myWireMaterial->SetSpecularColor(Col3);
126   myFrontMaterial->SetSpecularColor(Col3);
127   myPointsMaterial->SetSpecularColor(Col3);
128   
129   myRepresentation = VrmlAPI_BothRepresentation;
130 }
131 Handle(VrmlConverter_Drawer) VrmlAPI_Writer::Drawer() const
132 {
133   return myDrawer;
134 }
135 void VrmlAPI_Writer::SetDeflection(const Standard_Real aDef)
136 {
137   myDeflection = aDef;
138   if (myDeflection > 0) {
139     myDrawer->SetMaximalChordialDeviation(myDeflection);
140     myDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
141   }
142   else myDrawer->SetTypeOfDeflection(Aspect_TOD_RELATIVE);
143 }
144 void VrmlAPI_Writer::SetRepresentation(const VrmlAPI_RepresentationOfShape aRep)
145 {
146   myRepresentation = aRep;
147 }
148 void VrmlAPI_Writer::SetTransparencyToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aTransparency) 
149 {
150   Handle(TColStd_HArray1OfReal) t = new TColStd_HArray1OfReal(1,1,aTransparency);
151   aMaterial->SetTransparency(t);
152 }
153
154 void VrmlAPI_Writer::SetShininessToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aShininess) 
155 {  
156   Handle(TColStd_HArray1OfReal) s = new TColStd_HArray1OfReal(1,1,aShininess);
157   aMaterial->SetShininess(s);
158 }
159
160 void VrmlAPI_Writer::SetAmbientColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
161 {
162   aMaterial->SetAmbientColor(Color);
163 }
164
165 void VrmlAPI_Writer::SetDiffuseColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
166 {
167   aMaterial->SetDiffuseColor(Color);
168 }
169
170 void VrmlAPI_Writer::SetSpecularColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
171 {
172   aMaterial->SetSpecularColor(Color);
173 }
174
175 void VrmlAPI_Writer::SetEmissiveColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
176 {
177   aMaterial->SetEmissiveColor(Color);
178 }
179
180 VrmlAPI_RepresentationOfShape VrmlAPI_Writer::GetRepresentation() const
181 {
182   return myRepresentation;  
183 }
184
185 Handle(Vrml_Material) VrmlAPI_Writer::GetFrontMaterial() const
186 {
187   return myFrontMaterial;
188 }
189
190 Handle(Vrml_Material) VrmlAPI_Writer::GetPointsMaterial() const
191 {
192   return myPointsMaterial;
193 }
194
195 Handle(Vrml_Material) VrmlAPI_Writer::GetUisoMaterial() const
196 {
197   return myUisoMaterial;
198 }
199
200 Handle(Vrml_Material) VrmlAPI_Writer::GetVisoMaterial() const
201 {
202   return myVisoMaterial;
203 }
204
205 Handle(Vrml_Material) VrmlAPI_Writer::GetLineMaterial() const
206 {
207   return myLineMaterial;
208 }
209
210 Handle(Vrml_Material) VrmlAPI_Writer::GetWireMaterial() const
211 {
212   return myWireMaterial;
213 }
214
215 Handle(Vrml_Material) VrmlAPI_Writer::GetFreeBoundsMaterial() const
216 {
217   return myFreeBoundsMaterial;
218 }
219
220 Handle(Vrml_Material) VrmlAPI_Writer::GetUnfreeBoundsMaterial() const
221 {
222   return myUnfreeBoundsMaterial;
223 }
224
225 void VrmlAPI_Writer::Write(const TopoDS_Shape& aShape,const Standard_CString aFile, const Standard_Integer aVersion) const
226 {
227   if (aVersion == 1)
228     write_v1(aShape, aFile);
229   else if (aVersion == 2)
230     write_v2(aShape, aFile);
231 }
232
233 void VrmlAPI_Writer::write_v1(const TopoDS_Shape& aShape,const Standard_CString aFile) const
234 {
235   OSD_Path thePath(aFile);
236   TCollection_AsciiString theFile;thePath.SystemName(theFile);
237   ofstream outfile;
238   OSD_OpenStream(outfile, theFile.ToCString(), ios::out);
239   Handle(VrmlConverter_IsoAspect) ia = new VrmlConverter_IsoAspect;  // UIso
240   Handle(VrmlConverter_IsoAspect) ia1 = new VrmlConverter_IsoAspect; //VIso
241   ia->SetMaterial(myUisoMaterial);
242   ia->SetHasMaterial(Standard_True);
243   myDrawer->SetUIsoAspect(ia);
244   ia1->SetMaterial(myVisoMaterial);
245   ia1->SetHasMaterial(Standard_True);
246   myDrawer->SetVIsoAspect(ia1);
247 // default Number of iso lines is 10
248 //----  Definition of LineAspect (default - without own material)
249   Handle(VrmlConverter_LineAspect) la = new VrmlConverter_LineAspect;
250   la->SetMaterial(myLineMaterial);
251   la->SetHasMaterial(Standard_True);
252   myDrawer->SetLineAspect(la);
253 //----  Definition of Wire (without any neighbour)
254   Handle(VrmlConverter_LineAspect) lw = new VrmlConverter_LineAspect;
255   lw->SetMaterial(myWireMaterial);
256   lw->SetHasMaterial(Standard_True);
257   myDrawer->SetWireAspect(lw);
258 //----  Definition of Free boundaries
259   Handle(VrmlConverter_LineAspect) lf = new VrmlConverter_LineAspect;
260   lf->SetMaterial(myFreeBoundsMaterial);
261   lf->SetHasMaterial(Standard_True);
262   myDrawer->SetFreeBoundaryAspect(lf);
263 //----  Definition of Unfree boundaries
264   Handle(VrmlConverter_LineAspect) lun = new VrmlConverter_LineAspect;
265   lun->SetMaterial(myUnfreeBoundsMaterial);
266   lun->SetHasMaterial(Standard_True);
267   myDrawer->SetUnFreeBoundaryAspect(lun);
268 //----  Definition of Points (default - without own material)
269   Handle(VrmlConverter_PointAspect) pa = new VrmlConverter_PointAspect;
270   pa->SetMaterial(myPointsMaterial);
271   pa->SetHasMaterial(Standard_True);
272   myDrawer->SetPointAspect(pa);
273 //-----------------------------------------
274   Handle(VrmlConverter_ShadingAspect) sa = new VrmlConverter_ShadingAspect;
275   sa->SetFrontMaterial(myFrontMaterial);
276   sa->SetHasMaterial(Standard_True);
277   Vrml_ShapeHints  sh;
278   sa->SetShapeHints(sh);
279   myDrawer->SetShadingAspect(sa);
280 //-------- Shape --------------------------
281   TopTools_Array1OfShape Shapes(1,1);
282   Shapes.SetValue(1,aShape);
283
284   // Check shape tesselation
285   TopExp_Explorer anExp (aShape, TopAbs_FACE);
286   TopLoc_Location aLoc;
287   Standard_Boolean hasTriangles = Standard_False;
288   for (; anExp.More(); anExp.Next())
289   {
290     const TopoDS_Face& aFace = TopoDS::Face (anExp.Current());
291     if (!aFace.IsNull()) 
292     {
293       Handle(Poly_Triangulation) aTri =
294         BRep_Tool::Triangulation (aFace, aLoc);
295
296       if (!aTri.IsNull())
297       {
298         hasTriangles = Standard_True;
299         break;
300       }
301     }
302   }
303
304 //=========================================
305 //----  Definition of data for Projector
306 //=========================================
307
308   VrmlConverter_TypeOfLight Light = VrmlConverter_NoLight;
309   VrmlConverter_TypeOfCamera Camera = VrmlConverter_PerspectiveCamera;
310   Handle(VrmlConverter_Projector) projector = new VrmlConverter_Projector (Shapes, 
311                                                                            Focus, 
312                                                                            DX,  DY,  DZ, 
313                                                                            XUp, YUp, ZUp,
314                                                                            Camera,
315                                                                            Light);
316   Vrml::VrmlHeaderWriter(outfile);
317   if (myRepresentation == VrmlAPI_BothRepresentation)
318     Vrml::CommentWriter(" This file contents both Shaded and Wire Frame representation of selected Shape ",outfile);   
319   if (myRepresentation == VrmlAPI_ShadedRepresentation)
320     Vrml::CommentWriter(" This file contents only Shaded representation of selected Shape ",outfile);   
321   if (myRepresentation == VrmlAPI_WireFrameRepresentation)
322     Vrml::CommentWriter(" This file contents only Wire Frame representation of selected Shape ",outfile);   
323   Vrml_Separator S1;
324   S1.Print(outfile); 
325   projector->Add(outfile);
326   Light = VrmlConverter_DirectionLight;
327   Camera = VrmlConverter_OrthographicCamera;
328   Handle(VrmlConverter_Projector) projector1 = new VrmlConverter_Projector (Shapes, 
329                                                                            Focus, 
330                                                                            DX,  DY,  DZ, 
331                                                                            XUp, YUp, ZUp,
332                                                                            Camera,
333                                                                            Light);
334   projector1->Add(outfile);
335   Vrml_Separator S2;
336   S2.Print(outfile); 
337   if ( (myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation) && hasTriangles)
338     {
339       Vrml_Group Group1;
340       Group1.Print(outfile);
341       Vrml_Instancing I2 ("Shaded representation of shape");
342       I2.DEF(outfile);
343       VrmlConverter_ShadedShape::Add(outfile,aShape,myDrawer);
344       Group1.Print(outfile);
345     }
346   if (myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
347     {
348       Vrml_Group Group2;
349       Group2.Print(outfile);
350       Vrml_Instancing I3 ("Wire Frame representation of shape");
351       I3.DEF(outfile);
352       VrmlConverter_WFDeflectionShape::Add(outfile,aShape,myDrawer);
353       Group2.Print(outfile);
354     }
355   S2.Print(outfile); 
356   S1.Print(outfile); 
357 }
358
359 void VrmlAPI_Writer::write_v2(const TopoDS_Shape& aShape,const Standard_CString aFile) const
360 {
361   Standard_Boolean anExtFace = Standard_False;
362   if(myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation) 
363     anExtFace = Standard_True;
364
365   Standard_Boolean anExtEdge = Standard_False;
366   if(myRepresentation == VrmlAPI_WireFrameRepresentation|| myRepresentation == VrmlAPI_BothRepresentation)
367     anExtEdge = Standard_True;
368
369   VrmlData_Scene aScene;
370   VrmlData_ShapeConvert aConv(aScene);
371   aConv.AddShape(aShape);
372   aConv.Convert(anExtFace, anExtEdge);
373
374   filebuf aFoc;
375   ostream outStream (&aFoc);
376   if (aFoc.open (aFile, ios::out))
377     outStream << aScene;
378 }