Salome HOME
Merge from V6_main_20120808 08Aug12
[samples/sierpinsky.git] / src / Sierpinsky / SIERPINSKY_Gen_i.cxx
1 // Copyright (C) 2005-2012  OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 ///////////////////////////////////////////////////////////
21 // File    : SIERPINSKY_Gen_i.cxx
22 // Author  : Vadim SANDLER (OCN)
23 // Created : 13/07/05
24 ///////////////////////////////////////////////////////////
25 //
26 #include "SIERPINSKY_Gen_i.hxx"
27 #include <MED_Factory.hxx>
28 #include <gd.h>
29
30 using namespace MED;
31
32 /*!
33  * Engine factory
34  */
35 extern "C"
36 {
37   PortableServer::ObjectId * SIERPINSKYEngine_factory( CORBA::ORB_ptr            orb,
38                                                        PortableServer::POA_ptr   poa, 
39                                                        PortableServer::ObjectId* contId,
40                                                        const char*               instanceName,
41                                                        const char*               interfaceName )
42 {
43   SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
44   return anEngine->getId() ;
45 }
46 }
47
48 /*!
49  * Default constructor
50  */
51 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
52 {
53 }
54
55 /*!
56  * Constructor
57  */
58 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i( CORBA::ORB_ptr            orb,
59                                     PortableServer::POA_ptr   poa,
60                                     PortableServer::ObjectId* contId, 
61                                     const char*               instanceName, 
62                                     const char*               interfaceName ) 
63 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName ) 
64 {
65   // activate servant
66   _thisObj = this;
67   _id = poa->activate_object(_thisObj);
68   // set default values
69   Reset();
70 }
71
72 /*!
73  * Destructor
74  */
75 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
76 {
77   myPoints.clear();
78 }
79   
80 /*!
81  * Initializes engine with three reference points
82  */
83 void SIERPINSKY_Gen_i::Init( CORBA::Double theX1, CORBA::Double theY1, 
84                              CORBA::Double theX2, CORBA::Double theY2, 
85                              CORBA::Double theX3, CORBA::Double theY3 )
86 {
87   myRefPoints[0] = MyPoint( theX1, theY1 );
88   myRefPoints[1] = MyPoint( theX2, theY2 );
89   myRefPoints[2] = MyPoint( theX3, theY3 );
90   myPoints.clear();
91 }
92   
93 /*!
94  * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
95  */
96 void SIERPINSKY_Gen_i::Reset()
97 {
98   myRefPoints[0] = MyPoint( 0.5, 1.0 );
99   myRefPoints[1] = MyPoint( 0.0, 0.0 );
100   myRefPoints[2] = MyPoint( 1.0, 0.0 );
101   myPoints.clear();
102 }
103   
104 /*!
105  * Generates next iteration point
106  */
107 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double  theX,     CORBA::Double  theY, 
108                                   CORBA::Long    theIter, 
109                                   CORBA::Double& theNextX, CORBA::Double& theNextY )
110 {
111   double x = theIter < 1 || theIter > 3 ? theX : ( theX + myRefPoints[ theIter-1 ].myX ) / 2;
112   double y = theIter < 1 || theIter > 3 ? theY : ( theY + myRefPoints[ theIter-1 ].myY ) / 2;
113   myPoints.push_back( MyPoint( x, y ) );
114   theNextX = x;
115   theNextY = y;
116 }
117   
118 /*!
119  * Exports data to the JPEG image
120  */
121 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
122 {
123   if ( theSize <= 0 ) return false;
124
125   // open file
126   FILE* fileDescriptor = fopen( theFileName, "wb" );
127   if ( !fileDescriptor ) return false;
128
129   // create an image
130   gdImagePtr image = gdImageCreate( theSize, theSize );
131   int white = gdImageColorAllocate( image, 255, 255, 255 );
132   int black = gdImageColorAllocate( image,   0,   0,   0 );
133
134   gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
135
136   // draw points
137   std::list<MyPoint>::const_iterator iter;
138   for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
139     gdImageSetPixel( image, (int)( (*iter).myX * theSize ), theSize - (int)( (*iter).myY * theSize ), black );
140   }
141
142   // export jpeg image
143   gdImageJpeg( image, fileDescriptor, 95 );
144   fclose( fileDescriptor );
145   gdImageDestroy( image );
146   
147   return true;
148 }
149   
150 /*!
151  * Exports data to the MED file
152  */
153 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
154 {
155   TErr anError;
156   
157   // if file already exists - remove it (MED cannot overwrite files correctly)
158   FILE* aFile = fopen( theFileName, "rb" );
159   if ( aFile ) {
160     fclose( aFile );
161     if ( remove( theFileName ) ) return false; // can't remove file
162   }
163   
164   // create MED 2.2 file
165   PWrapper aMed = CrWrapper( theFileName, MED::eV2_2 );
166
167   // create 2D mesh
168   PMeshInfo aMesh = aMed->CrMeshInfo( 2, 2, "Sierpinsky" );
169   aMed->SetMeshInfo( aMesh, &anError );
170   if ( anError < 0 ) return false;
171
172   // create nodes
173   TFloatVector nodes;
174   TIntVector   connect;
175   std::list<MyPoint>::const_iterator iter;
176   int ind = 1;
177   for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
178     nodes.push_back( (*iter).myX * theSize );
179     nodes.push_back( (*iter).myY * theSize );
180     connect.push_back( ind++ );
181   }
182   PNodeInfo aNodes = aMed->CrNodeInfo( aMesh, nodes, MED::eFULL_INTERLACE, MED::eCART, TStringVector(2), TStringVector(2), TIntVector( myPoints.size() ), TIntVector() );
183   aMed->SetNodeInfo( aNodes, &anError );
184   if ( anError < 0 ) return false;
185
186   PCellInfo aCells = aMed->CrCellInfo( aMesh, MED::eMAILLE, MED::ePOINT1, connect, eNOD, TIntVector( myPoints.size() ), TIntVector( myPoints.size() ) );
187   aMed->SetCellInfo( aCells, &anError );
188   if ( anError < 0 ) return false;
189
190   return true;
191 }