1 // Copyright (C) 2005-2024 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, or (at your option) any later version.
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 SIERPINSKYENGINE_EXPORT
39 PortableServer::ObjectId * SIERPINSKYEngine_factory( CORBA::ORB_ptr orb,
40 PortableServer::POA_ptr poa,
41 PortableServer::ObjectId* contId,
42 const char* instanceName,
43 const char* interfaceName )
45 SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
46 return anEngine->getId() ;
53 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
60 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i( CORBA::ORB_ptr orb,
61 PortableServer::POA_ptr poa,
62 PortableServer::ObjectId* contId,
63 const char* instanceName,
64 const char* interfaceName )
65 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
69 _id = poa->activate_object(_thisObj);
77 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
83 * Initializes engine with three reference points
85 void SIERPINSKY_Gen_i::Init( CORBA::Double theX1, CORBA::Double theY1,
86 CORBA::Double theX2, CORBA::Double theY2,
87 CORBA::Double theX3, CORBA::Double theY3 )
89 myRefPoints[0] = MyPoint( theX1, theY1 );
90 myRefPoints[1] = MyPoint( theX2, theY2 );
91 myRefPoints[2] = MyPoint( theX3, theY3 );
96 * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
98 void SIERPINSKY_Gen_i::Reset()
100 myRefPoints[0] = MyPoint( 0.5, 1.0 );
101 myRefPoints[1] = MyPoint( 0.0, 0.0 );
102 myRefPoints[2] = MyPoint( 1.0, 0.0 );
107 * Generates next iteration point
109 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double theX, CORBA::Double theY,
111 CORBA::Double& theNextX, CORBA::Double& theNextY )
113 double x = theIter < 1 || theIter > 3 ? theX : ( theX + myRefPoints[ theIter-1 ].myX ) / 2;
114 double y = theIter < 1 || theIter > 3 ? theY : ( theY + myRefPoints[ theIter-1 ].myY ) / 2;
115 myPoints.push_back( MyPoint( x, y ) );
121 * Exports data to the JPEG image
123 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
126 if ( theSize <= 0 ) return false;
129 FILE* fileDescriptor = fopen( theFileName, "wb" );
130 if ( !fileDescriptor ) return false;
133 gdImagePtr image = gdImageCreate( theSize, theSize );
134 int white = gdImageColorAllocate( image, 255, 255, 255 );
135 int black = gdImageColorAllocate( image, 0, 0, 0 );
137 gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
140 std::list<MyPoint>::const_iterator iter;
141 for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
142 gdImageSetPixel( image, (int)( (*iter).myX * theSize ), theSize - (int)( (*iter).myY * theSize ), black );
146 gdImageJpeg( image, fileDescriptor, 95 );
147 fclose( fileDescriptor );
148 gdImageDestroy( image );
152 printf("Warning: ExportToJPEG() is not supported (libgd is required)!");
158 * Exports data to the MED file
160 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
164 // if file already exists - remove it (MED cannot overwrite files correctly)
165 FILE* aFile = fopen( theFileName, "rb" );
168 if ( remove( theFileName ) ) return false; // can't remove file
171 // create MED 2.2 file
172 MED::PWrapper aMed = MED::CrWrapperW( theFileName );
175 MED::PMeshInfo aMesh = aMed->CrMeshInfo( 2, 2, "Sierpinsky" );
176 aMed->SetMeshInfo( aMesh, &anError );
177 if ( anError < 0 ) return false;
180 MED::TFloatVector nodes;
181 MED::TIntVector connect;
182 std::list<MyPoint>::const_iterator iter;
184 for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
185 nodes.push_back( (*iter).myX * theSize );
186 nodes.push_back( (*iter).myY * theSize );
187 connect.push_back( ind++ );
189 MED::PNodeInfo aNodes = aMed->CrNodeInfo( aMesh, nodes,
190 MED::eFULL_INTERLACE, MED::eCART,
191 MED::TStringVector(2),
192 MED::TStringVector(2),
193 MED::TIntVector( myPoints.size() ),
195 aMed->SetNodeInfo( aNodes, &anError );
196 if ( anError < 0 ) return false;
198 MED::PCellInfo aCells = aMed->CrCellInfo( aMesh, MED::eMAILLE, MED::ePOINT1,
200 MED::TIntVector( myPoints.size() ),
201 MED::TIntVector( myPoints.size() ) );
202 aMed->SetCellInfo( aCells, &anError );
203 if ( anError < 0 ) return false;
208 // Version information
209 char* SIERPINSKY_Gen_i::getVersion()
211 #if SIERPINSKY_DEVELOPMENT
212 return CORBA::string_dup(SIERPINSKY_VERSION_STR"dev");
214 return CORBA::string_dup(SIERPINSKY_VERSION_STR);