Salome HOME
Merge branch V7_3_1_BR
[modules/smesh.git] / src / DriverUNV / UNV2420_Structure.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 "UNV2420_Structure.hxx"
24 #include "UNV_Utilities.hxx"
25
26 #include <fstream>
27 #include <cstdio>
28 #include <cmath>
29
30 using namespace std;
31 using namespace UNV;
32 using namespace UNV2420;
33
34 static string _label_dataset = "2420";
35
36 void UNV2420::Read(std::ifstream& in_stream,
37                    std::string&   part_name, // can re-store a mesh name
38                    TDataSet&      theDataSet)
39 {
40   if(!in_stream.good())
41     EXCEPTION(runtime_error,"ERROR: Input file not good.");
42
43   /*
44    * adjust the \p istream to our
45    * position
46    */
47   if(!beginning_of_dataset(in_stream,_label_dataset))
48     return;
49
50   string num_buf;
51   int part_uid;
52
53   in_stream >> part_uid; // Record 1
54   part_name = read_line( in_stream );  // Record 2
55
56   while ( !in_stream.eof() )
57   {
58     TRecord aRec;
59
60     // Record 3
61     in_stream >> aRec.coord_sys_label;
62     if ( aRec.coord_sys_label == -1 ) // end of dataset is reached
63       break;
64     in_stream >> aRec.coord_sys_type;
65     in_stream >> aRec.coord_sys_color;
66
67     aRec.coord_sys_name = read_line( in_stream ); // Record 4
68
69     // Records 5-8: rows of Transformation Matrix
70     for ( int row = 0; row < 4; ++row )
71       for ( int i = 0; i < 3; i++ )
72       {
73         in_stream >> num_buf;
74         aRec.matrix[row][i] = D_to_e(num_buf);
75       }
76     // Store a CS data only if it requires conversion into the global Cartesian CS
77     if ( aRec.coord_sys_type != 0 || !aRec.isIdentityMatrix() ) // 0 - Cartesian CS
78       theDataSet.push_back( aRec );
79   }
80 }
81
82
83 void UNV2420::Write(std::ofstream&     out_stream,
84                     const std::string& part_name)
85 //                    const TDataSet& theDataSet)
86 {
87   if(!out_stream.good())
88     EXCEPTION(runtime_error,"ERROR: Output file not good.");
89   
90   out_stream<<"    -1"  << endl;
91   out_stream<<"  "<<_label_dataset << endl;
92
93   out_stream<<"         1"                     << endl; // R1: Part UID
94   if ( part_name.empty() )
95     out_stream<<"SMESH_Mesh"                   << endl; // R2: Part Name
96   else
97     out_stream<< part_name                     << endl;
98   out_stream<<"         1         0         0" << endl; // R3: Label, Type, Color
99
100   out_stream<<"Global Cartesian Coordinate System" << endl; // R4: Name
101   out_stream<<"    1.0000000000000000E+0    0.0000000000000000E+0    0.0000000000000000E+0" << endl;
102   out_stream<<"    0.0000000000000000E+0    1.0000000000000000E+0    0.0000000000000000E+0" << endl;
103   out_stream<<"    0.0000000000000000E+0    0.0000000000000000E+0    1.0000000000000000E+0" << endl;
104   out_stream<<"    0.0000000000000000E+0    0.0000000000000000E+0    0.0000000000000000E+0" << endl;
105
106   out_stream<<"    -1"  << endl;
107 }
108
109
110 bool UNV2420::TRecord::isIdentityMatrix() const
111 {
112   bool isIdentity = true;
113   for ( int row = 0; row < 4 && isIdentity; ++row )
114     for ( int i = 0; i < 3; i++ )
115     {
116       if ( matrix[row][i] != ( row==i ? 1. : 0. ))
117       {
118         isIdentity = false;
119         break;
120       }
121     }
122   return isIdentity;
123 }
124
125 void UNV2420::TRecord::ApplyMatrix( double* c ) const
126 {
127   const double x = matrix[0][0] * c[0] + matrix[0][1] * c[1] + matrix[0][2] * c[2];
128   const double y = matrix[1][0] * c[0] + matrix[1][1] * c[1] + matrix[1][2] * c[2];
129   const double z = matrix[2][0] * c[0] + matrix[2][1] * c[1] + matrix[2][2] * c[2];
130   c[0] = x + matrix[3][0];
131   c[1] = y + matrix[3][1];
132   c[2] = z + matrix[3][2];
133 }
134
135 void UNV2420::TRecord::FromCylindricalCS( double* coords )
136 {
137   const double x = coords[0] * cos( coords[1] );
138   const double y = coords[0] * sin( coords[1] );
139   coords[0] = x;
140   coords[1] = y;
141 }
142
143 void UNV2420::TRecord::FromSphericalCS  ( double* coords )
144 {
145   const double sin2 = sin( coords[2] );
146   const double x = coords[0] * cos( coords[1] ) * sin2;
147   const double y = coords[0] * sin( coords[1] ) * sin2;
148   const double z = coords[0] * cos( coords[2] );
149   coords[0] = x;
150   coords[1] = y;
151   coords[2] = z;
152 }