Salome HOME
PR: merged from V5_1_4rc1
[modules/smesh.git] / src / DriverSTL / DriverSTL_W_SMDS_Mesh.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <stdio.h>
24
25 #include "DriverSTL_W_SMDS_Mesh.h"
26
27 #include "SMDS_Mesh.hxx"
28 #include "SMDS_MeshElement.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include <gp_XYZ.hxx>
31 #include <OSD_Path.hxx>
32 #include <OSD_File.hxx>
33 #include <OSD_FromWhere.hxx>
34 #include <OSD_Protection.hxx>
35 #include <OSD_SingleProtection.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <TColgp_Array1OfXYZ.hxx>
38
39 #include "utilities.h"
40
41 //using namespace std;
42
43 // definition des constantes 
44 static const int LABEL_SIZE = 80;
45
46 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
47 {
48   myIsAscii = false;
49 }
50
51 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
52 {
53   myIsAscii = theIsAscii;
54 }
55
56 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
57 {
58   Status aResult = DRS_OK;
59
60   if ( !myMesh ) {
61     fprintf(stderr, ">> ERROR : Mesh is null \n");
62     return DRS_FAIL;
63   }
64   if ( myIsAscii )
65     aResult = writeAscii();
66   else
67     aResult = writeBinary();
68
69   return aResult;
70 }
71
72 // static methods
73
74 static void writeInteger( const Standard_Integer& theVal,
75                          OSD_File& ofile )
76 {
77   union {
78     Standard_Integer i;
79     char c[4];
80   }u;
81
82   u.i = theVal;
83
84   Standard_Integer entier;
85   entier  =  u.c[0] & 0xFF;
86   entier |= (u.c[1] & 0xFF) << 0x08;
87   entier |= (u.c[2] & 0xFF) << 0x10;
88   entier |= (u.c[3] & 0xFF) << 0x18;
89
90   ofile.Write((char *)&entier,sizeof(u.c));
91 }
92
93 static void writeFloat  ( const Standard_ShortReal& theVal,
94                          OSD_File& ofile)
95 {
96   union {
97     Standard_ShortReal f;
98     char c[4]; 
99   }u;
100
101   u.f = theVal;
102
103   Standard_Integer entier;
104
105   entier  =  u.c[0] & 0xFF;
106   entier |= (u.c[1] & 0xFF) << 0x08;
107   entier |= (u.c[2] & 0xFF) << 0x10;
108   entier |= (u.c[3] & 0xFF) << 0x18;
109
110   ofile.Write((char *)&entier,sizeof(u.c));
111 }
112
113 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
114 {
115   gp_XYZ n;
116   int aNbNode = theFace->NbNodes();
117   TColgp_Array1OfXYZ anArrOfXYZ(1,4);
118   gp_XYZ p1, p2, p3, p4;
119   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
120   int i = 1;
121   for ( ; aNodeItr->more() && i <= 4; i++ )
122   {
123     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
124     anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
125   }
126   
127   gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
128   gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
129   n  = q1 ^ q2;
130   if ( aNbNode > 3 )
131   {
132     gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
133     n += q2 ^ q3;
134   }
135   double len = n.Modulus();
136   if ( len > 0 )
137     n /= len;
138
139   return n;
140 }
141
142 // private methods
143
144 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
145 {
146   Status aResult = DRS_OK;
147   TCollection_AsciiString aFileName( (char *)myFile.c_str() );
148   if ( aFileName.IsEmpty() ) {
149     fprintf(stderr, ">> ERREOR : invalid file name \n");
150     return DRS_FAIL;
151   }
152
153   OSD_File aFile = OSD_File(OSD_Path(aFileName));
154   aFile.Build(OSD_WriteOnly,OSD_Protection());
155   TCollection_AsciiString buf = TCollection_AsciiString ("solid\n");
156   aFile.Write (buf,buf.Length());buf.Clear();
157   char sval[16];
158
159   SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
160   
161   for (; itFaces->more() ;) {
162     SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
163     
164     if (aFace->NbNodes() == 3) {
165       gp_XYZ normale = getNormale( aFace );
166
167       buf += " facet normal "; 
168       sprintf (sval,"% 12e",normale.X());
169       buf += sval;
170       buf += " "; 
171       sprintf (sval,"% 12e",normale.Y());
172       buf += sval;
173       buf += " "; 
174       sprintf (sval,"% 12e",normale.Z());
175       buf += sval;
176       buf += '\n';
177       aFile.Write (buf,buf.Length());buf.Clear();
178       buf += "   outer loop\n"; 
179       aFile.Write (buf,buf.Length());buf.Clear();
180       
181       SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator();
182       for (; aNodeIter->more(); ) {
183         SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
184         buf += "     vertex "; 
185         sprintf (sval,"% 12e",node->X());
186         buf += sval;
187         buf += " "; 
188         sprintf (sval,"% 12e",node->Y());
189         buf += sval;
190         buf += " "; 
191         sprintf (sval,"% 12e",node->Z());
192         buf += sval;
193         buf += '\n';
194         aFile.Write (buf,buf.Length());buf.Clear();
195       }
196       buf += "   endloop\n"; 
197       aFile.Write (buf,buf.Length());buf.Clear();
198       buf += " endfacet\n"; 
199       aFile.Write (buf,buf.Length());buf.Clear();
200     } 
201   }
202   buf += "endsolid\n";
203   aFile.Write (buf,buf.Length());buf.Clear();
204   
205   aFile.Close ();
206
207   return aResult;
208 }
209
210 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
211 {
212   Status aResult = DRS_OK;
213   TCollection_AsciiString aFileName( (char *)myFile.c_str() );
214   if ( aFileName.IsEmpty() ) {
215     fprintf(stderr, ">> ERREOR : invalid filename \n");
216     return DRS_FAIL;
217   }
218
219   OSD_File aFile = OSD_File(OSD_Path(aFileName));
220   aFile.Build(OSD_WriteOnly,OSD_Protection());
221
222   char sval[80];
223   Standard_Integer nbTri = 0;
224   SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
225
226   // we first count the number of triangles
227   for (;itFaces->more();) {
228     SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
229     if (aFace->NbNodes() == 3)
230       nbTri++;
231   }
232
233   // write number of triangles
234   //unsigned int NBT = nbTri;
235   aFile.Write((Standard_Address)sval,LABEL_SIZE);
236   writeInteger(nbTri,aFile);  
237
238   // loop writing nodes. take face iterator again
239   int dum=0;
240   itFaces = myMesh->facesIterator();
241   
242   for (;itFaces->more();) {
243     SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
244     
245     if (aFace->NbNodes() == 3) {
246       gp_XYZ aNorm = getNormale( aFace );
247       writeFloat(aNorm.X(),aFile);
248       writeFloat(aNorm.Y(),aFile);
249       writeFloat(aNorm.Z(),aFile);
250
251       SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator();
252       for (; aNodeIter->more(); ) {
253         SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
254         writeFloat(node->X(),aFile);
255         writeFloat(node->Y(),aFile);
256         writeFloat(node->Z(),aFile);
257       }
258       aFile.Write (&dum,2);
259     } 
260   }
261   aFile.Close ();
262
263   return aResult;
264 }