]> SALOME platform Git repositories - samples/sierpinsky.git/blob - src/Sierpinsky/SIERPINSKY_Gen_i.cxx
Salome HOME
Make gd library optional for Sierpinsky
[samples/sierpinsky.git] / src / Sierpinsky / SIERPINSKY_Gen_i.cxx
1 // Copyright (C) 2005-2013  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 "SIERPINSKY_version.h"
28 #include <MED_Factory.hxx>
29 #ifdef WITH_LIBGD
30 #include <gd.h>
31 #endif // WITH_LIBGD
32
33 /*!
34  * Engine factory
35  */
36 extern "C"
37 {
38   PortableServer::ObjectId * SIERPINSKYEngine_factory( CORBA::ORB_ptr            orb,
39                                                        PortableServer::POA_ptr   poa, 
40                                                        PortableServer::ObjectId* contId,
41                                                        const char*               instanceName,
42                                                        const char*               interfaceName )
43 {
44   SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
45   return anEngine->getId() ;
46 }
47 }
48
49 /*!
50  * Default constructor
51  */
52 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
53 {
54 }
55
56 /*!
57  * Constructor
58  */
59 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i( CORBA::ORB_ptr            orb,
60                                     PortableServer::POA_ptr   poa,
61                                     PortableServer::ObjectId* contId, 
62                                     const char*               instanceName, 
63                                     const char*               interfaceName ) 
64 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName ) 
65 {
66   // activate servant
67   _thisObj = this;
68   _id = poa->activate_object(_thisObj);
69   // set default values
70   Reset();
71 }
72
73 /*!
74  * Destructor
75  */
76 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
77 {
78   myPoints.clear();
79 }
80   
81 /*!
82  * Initializes engine with three reference points
83  */
84 void SIERPINSKY_Gen_i::Init( CORBA::Double theX1, CORBA::Double theY1, 
85                              CORBA::Double theX2, CORBA::Double theY2, 
86                              CORBA::Double theX3, CORBA::Double theY3 )
87 {
88   myRefPoints[0] = MyPoint( theX1, theY1 );
89   myRefPoints[1] = MyPoint( theX2, theY2 );
90   myRefPoints[2] = MyPoint( theX3, theY3 );
91   myPoints.clear();
92 }
93   
94 /*!
95  * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
96  */
97 void SIERPINSKY_Gen_i::Reset()
98 {
99   myRefPoints[0] = MyPoint( 0.5, 1.0 );
100   myRefPoints[1] = MyPoint( 0.0, 0.0 );
101   myRefPoints[2] = MyPoint( 1.0, 0.0 );
102   myPoints.clear();
103 }
104   
105 /*!
106  * Generates next iteration point
107  */
108 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double  theX,     CORBA::Double  theY, 
109                                   CORBA::Long    theIter, 
110                                   CORBA::Double& theNextX, CORBA::Double& theNextY )
111 {
112   double x = theIter < 1 || theIter > 3 ? theX : ( theX + myRefPoints[ theIter-1 ].myX ) / 2;
113   double y = theIter < 1 || theIter > 3 ? theY : ( theY + myRefPoints[ theIter-1 ].myY ) / 2;
114   myPoints.push_back( MyPoint( x, y ) );
115   theNextX = x;
116   theNextY = y;
117 }
118   
119 /*!
120  * Exports data to the JPEG image
121  */
122 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
123 {
124 #ifdef WITH_LIBGD
125   if ( theSize <= 0 ) return false;
126
127   // open file
128   FILE* fileDescriptor = fopen( theFileName, "wb" );
129   if ( !fileDescriptor ) return false;
130
131   // create an image
132   gdImagePtr image = gdImageCreate( theSize, theSize );
133   int white = gdImageColorAllocate( image, 255, 255, 255 );
134   int black = gdImageColorAllocate( image,   0,   0,   0 );
135
136   gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
137
138   // draw points
139   std::list<MyPoint>::const_iterator iter;
140   for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
141     gdImageSetPixel( image, (int)( (*iter).myX * theSize ), theSize - (int)( (*iter).myY * theSize ), black );
142   }
143
144   // export jpeg image
145   gdImageJpeg( image, fileDescriptor, 95 );
146   fclose( fileDescriptor );
147   gdImageDestroy( image );
148   
149   return true;
150 #else // WITH_LIBGD
151   printf("Warning: ExportToJPEG() is not supported (libgd is required)!");
152   return false;
153 #endif // WITH_LIBGD
154 }
155   
156 /*!
157  * Exports data to the MED file
158  */
159 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
160 {
161   MED::TErr anError;
162   
163   // if file already exists - remove it (MED cannot overwrite files correctly)
164   FILE* aFile = fopen( theFileName, "rb" );
165   if ( aFile ) {
166     fclose( aFile );
167     if ( remove( theFileName ) ) return false; // can't remove file
168   }
169   
170   // create MED 2.2 file
171   MED::PWrapper aMed = MED::CrWrapper( theFileName, MED::eV2_2 );
172
173   // create 2D mesh
174   MED::PMeshInfo aMesh = aMed->CrMeshInfo( 2, 2, "Sierpinsky" );
175   aMed->SetMeshInfo( aMesh, &anError );
176   if ( anError < 0 ) return false;
177
178   // create nodes
179   MED::TFloatVector nodes;
180   MED::TIntVector   connect;
181   std::list<MyPoint>::const_iterator iter;
182   int ind = 1;
183   for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
184     nodes.push_back( (*iter).myX * theSize );
185     nodes.push_back( (*iter).myY * theSize );
186     connect.push_back( ind++ );
187   }
188   MED::PNodeInfo aNodes = aMed->CrNodeInfo( aMesh, nodes, 
189                                             MED::eFULL_INTERLACE, MED::eCART, 
190                                             MED::TStringVector(2),
191                                             MED::TStringVector(2), 
192                                             MED::TIntVector( myPoints.size() ),
193                                             MED::TIntVector() );
194   aMed->SetNodeInfo( aNodes, &anError );
195   if ( anError < 0 ) return false;
196
197   MED::PCellInfo aCells = aMed->CrCellInfo( aMesh, MED::eMAILLE, MED::ePOINT1, 
198                                             connect, MED::eNOD, 
199                                             MED::TIntVector( myPoints.size() ), 
200                                             MED::TIntVector( myPoints.size() ) );
201   aMed->SetCellInfo( aCells, &anError );
202   if ( anError < 0 ) return false;
203
204   return true;
205 }
206
207 // Version information
208 char* SIERPINSKY_Gen_i::getVersion()
209 {
210 #if SIERPINSKY_DEVELOPMENT
211   return CORBA::string_dup(SIERPINSKY_VERSION_STR"dev");
212 #else
213   return CORBA::string_dup(SIERPINSKY_VERSION_STR);
214 #endif
215 }