1 // Copyright (C) 2005-2008 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
19 ///////////////////////////////////////////////////////////
20 // File : SIERPINSKY_Gen_i.cxx
21 // Author : Vadim SANDLER (OCN)
23 ///////////////////////////////////////////////////////////
25 #include "SIERPINSKY_Gen_i.hxx"
26 #include <MED_Factory.hxx>
36 PortableServer::ObjectId * SIERPINSKYEngine_factory( CORBA::ORB_ptr orb,
37 PortableServer::POA_ptr poa,
38 PortableServer::ObjectId* contId,
39 const char* instanceName,
40 const char* interfaceName )
42 SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
43 return anEngine->getId() ;
50 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
57 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i( CORBA::ORB_ptr orb,
58 PortableServer::POA_ptr poa,
59 PortableServer::ObjectId* contId,
60 const char* instanceName,
61 const char* interfaceName )
62 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
66 _id = poa->activate_object(_thisObj);
74 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
80 * Initializes engine with three reference points
82 void SIERPINSKY_Gen_i::Init( CORBA::Double theX1, CORBA::Double theY1,
83 CORBA::Double theX2, CORBA::Double theY2,
84 CORBA::Double theX3, CORBA::Double theY3 )
86 myRefPoints[0] = MyPoint( theX1, theY1 );
87 myRefPoints[1] = MyPoint( theX2, theY2 );
88 myRefPoints[2] = MyPoint( theX3, theY3 );
93 * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
95 void SIERPINSKY_Gen_i::Reset()
97 myRefPoints[0] = MyPoint( 0.5, 1.0 );
98 myRefPoints[1] = MyPoint( 0.0, 0.0 );
99 myRefPoints[2] = MyPoint( 1.0, 0.0 );
104 * Generates next iteration point
106 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double theX, CORBA::Double theY,
108 CORBA::Double& theNextX, CORBA::Double& theNextY )
110 double x = theIter < 1 || theIter > 3 ? theX : ( theX + myRefPoints[ theIter-1 ].myX ) / 2;
111 double y = theIter < 1 || theIter > 3 ? theY : ( theY + myRefPoints[ theIter-1 ].myY ) / 2;
112 myPoints.push_back( MyPoint( x, y ) );
118 * Exports data to the JPEG image
120 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
122 if ( theSize <= 0 ) return false;
125 FILE* fileDescriptor = fopen( theFileName, "wb" );
126 if ( !fileDescriptor ) return false;
129 gdImagePtr image = gdImageCreate( theSize, theSize );
130 int white = gdImageColorAllocate( image, 255, 255, 255 );
131 int black = gdImageColorAllocate( image, 0, 0, 0 );
133 gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
136 std::list<MyPoint>::const_iterator iter;
137 for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
138 gdImageSetPixel( image, (int)( (*iter).myX * theSize ), theSize - (int)( (*iter).myY * theSize ), black );
142 gdImageJpeg( image, fileDescriptor, 95 );
143 fclose( fileDescriptor );
144 gdImageDestroy( image );
150 * Exports data to the MED file
152 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
156 // if file already exists - remove it (MED cannot overwrite files correctly)
157 FILE* aFile = fopen( theFileName, "rb" );
160 if ( remove( theFileName ) ) return false; // can't remove file
163 // create MED 2.2 file
164 PWrapper aMed = CrWrapper( theFileName, MED::eV2_2 );
167 PMeshInfo aMesh = aMed->CrMeshInfo( 2, "Sierpinsky" );
168 aMed->SetMeshInfo( aMesh, &anError );
169 if ( anError < 0 ) return false;
174 std::list<MyPoint>::const_iterator iter;
176 for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
177 nodes.push_back( (*iter).myX * theSize );
178 nodes.push_back( (*iter).myY * theSize );
179 connect.push_back( ind++ );
181 PNodeInfo aNodes = aMed->CrNodeInfo( aMesh, nodes, MED::eFULL_INTERLACE, MED::eCART, TStringVector(2), TStringVector(2), TIntVector( myPoints.size() ), TIntVector() );
182 aMed->SetNodeInfo( aNodes, &anError );
183 if ( anError < 0 ) return false;
185 PCellInfo aCells = aMed->CrCellInfo( aMesh, MED::eMAILLE, MED::ePOINT1, connect, eNOD, TIntVector( myPoints.size() ), TIntVector( myPoints.size() ) );
186 aMed->SetCellInfo( aCells, &anError );
187 if ( anError < 0 ) return false;