1 // Copyright (C) 2005-2013 OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 ///////////////////////////////////////////////////////////
21 // File : SIERPINSKY_Gen_i.cxx
22 // Author : Vadim SANDLER (OCN)
24 ///////////////////////////////////////////////////////////
26 #include "SIERPINSKY_Gen_i.hxx"
27 #include "SIERPINSKY_version.h"
28 #include <MED_Factory.hxx>
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 )
44 SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
45 return anEngine->getId() ;
52 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
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 )
68 _id = poa->activate_object(_thisObj);
76 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
82 * Initializes engine with three reference points
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 )
88 myRefPoints[0] = MyPoint( theX1, theY1 );
89 myRefPoints[1] = MyPoint( theX2, theY2 );
90 myRefPoints[2] = MyPoint( theX3, theY3 );
95 * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
97 void SIERPINSKY_Gen_i::Reset()
99 myRefPoints[0] = MyPoint( 0.5, 1.0 );
100 myRefPoints[1] = MyPoint( 0.0, 0.0 );
101 myRefPoints[2] = MyPoint( 1.0, 0.0 );
106 * Generates next iteration point
108 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double theX, CORBA::Double theY,
110 CORBA::Double& theNextX, CORBA::Double& theNextY )
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 ) );
120 * Exports data to the JPEG image
122 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
125 if ( theSize <= 0 ) return false;
128 FILE* fileDescriptor = fopen( theFileName, "wb" );
129 if ( !fileDescriptor ) return false;
132 gdImagePtr image = gdImageCreate( theSize, theSize );
133 int white = gdImageColorAllocate( image, 255, 255, 255 );
134 int black = gdImageColorAllocate( image, 0, 0, 0 );
136 gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
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 );
145 gdImageJpeg( image, fileDescriptor, 95 );
146 fclose( fileDescriptor );
147 gdImageDestroy( image );
151 printf("Warning: ExportToJPEG() is not supported (libgd is required)!");
157 * Exports data to the MED file
159 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
163 // if file already exists - remove it (MED cannot overwrite files correctly)
164 FILE* aFile = fopen( theFileName, "rb" );
167 if ( remove( theFileName ) ) return false; // can't remove file
170 // create MED 2.2 file
171 MED::PWrapper aMed = MED::CrWrapper( theFileName, MED::eV2_2 );
174 MED::PMeshInfo aMesh = aMed->CrMeshInfo( 2, 2, "Sierpinsky" );
175 aMed->SetMeshInfo( aMesh, &anError );
176 if ( anError < 0 ) return false;
179 MED::TFloatVector nodes;
180 MED::TIntVector connect;
181 std::list<MyPoint>::const_iterator iter;
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++ );
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() ),
194 aMed->SetNodeInfo( aNodes, &anError );
195 if ( anError < 0 ) return false;
197 MED::PCellInfo aCells = aMed->CrCellInfo( aMesh, MED::eMAILLE, MED::ePOINT1,
199 MED::TIntVector( myPoints.size() ),
200 MED::TIntVector( myPoints.size() ) );
201 aMed->SetCellInfo( aCells, &anError );
202 if ( anError < 0 ) return false;
207 // Version information
208 char* SIERPINSKY_Gen_i::getVersion()
210 #if SIERPINSKY_DEVELOPMENT
211 return CORBA::string_dup(SIERPINSKY_VERSION_STR"dev");
213 return CORBA::string_dup(SIERPINSKY_VERSION_STR);