mydxfentities.hpp
Go to the documentation of this file.
00001 00005 /* Copyright (c) 2010-2011 Taneli Kalvas. All rights reserved. 00006 * 00007 * You can redistribute this software and/or modify it under the terms 00008 * of the GNU General Public License as published by the Free Software 00009 * Foundation; either version 2 of the License, or (at your option) 00010 * any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this library (file "COPYING" included in the package); 00019 * if not, write to the Free Software Foundation, Inc., 51 Franklin 00020 * Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 * 00022 * If you have questions about your rights to use or distribute this 00023 * software, please contact Berkeley Lab's Technology Transfer 00024 * Department at TTD@lbl.gov. Other questions, comments and bug 00025 * reports should be sent directly to the author via email at 00026 * taneli.kalvas@jyu.fi. 00027 * 00028 * NOTICE. This software was developed under partial funding from the 00029 * U.S. Department of Energy. As such, the U.S. Government has been 00030 * granted for itself and others acting on its behalf a paid-up, 00031 * nonexclusive, irrevocable, worldwide license in the Software to 00032 * reproduce, prepare derivative works, and perform publicly and 00033 * display publicly. Beginning five (5) years after the date 00034 * permission to assert copyright is obtained from the U.S. Department 00035 * of Energy, and subject to any subsequent five (5) year renewals, 00036 * the U.S. Government is granted for itself and others acting on its 00037 * behalf a paid-up, nonexclusive, irrevocable, worldwide license in 00038 * the Software to reproduce, prepare derivative works, distribute 00039 * copies to the public, perform publicly and display publicly, and to 00040 * permit others to do so. 00041 */ 00042 00043 #ifndef MY_DXF_ENTITIES_HPP 00044 #define MY_DXF_ENTITIES_HPP 1 00045 00046 00047 #include <stdint.h> 00048 #include <vector> 00049 #include <cairo.h> 00050 #include "mydxffile.hpp" 00051 #include "vec3d.hpp" 00052 #include "transformation.hpp" 00053 00054 00057 enum EntityType { 00058 ENTITY_UNKNOWN = 0, 00059 ENTITY_LINE, 00060 ENTITY_LWPOLYLINE, 00061 ENTITY_SPLINE, 00062 ENTITY_ARC, 00063 ENTITY_CIRCLE, 00064 ENTITY_MTEXT, 00065 ENTITY_INSERT 00066 }; 00067 00068 00074 class MyDXFEntity 00075 { 00076 00077 protected: 00078 00079 std::string _handle; 00080 std::string _layer; 00081 00082 MyDXFEntity(); 00083 00084 //MyDXFEntity( const MyDXFEntity &ent ); 00085 00091 static void bbox_ppoint( Vec3D &min, Vec3D &max, const Vec3D &p ); 00092 00093 void write_common( class MyDXFFile *dxf, std::ofstream &ostr ); 00094 void process_group( class MyDXFFile *dxf ); 00095 void constructor_debug_print( void ) const; 00096 void debug_print_base( std::ostream &os ) const; 00097 virtual void debug_print( std::ostream &os ) const = 0; 00098 00099 public: 00100 00103 virtual ~MyDXFEntity() {} 00104 00107 virtual MyDXFEntity *copy( void ) const = 0; 00108 00115 virtual void explode( class MyDXFEntities *ent, MyDXFFile *dxf, const Transformation *t ) const = 0; 00116 00119 virtual void write( class MyDXFFile *dxf, std::ofstream &ostr ) = 0; 00120 00123 virtual void scale( class MyDXFFile *dxf, double s ) = 0; 00124 00127 virtual void translate( class MyDXFFile *dxf, const Vec3D &dx ) = 0; 00128 00131 void set_layer( const std::string &layer ) { _layer = layer; } 00132 00135 std::string get_layer( void ) const { return( _layer ); } 00136 00139 virtual EntityType get_type( void ) const = 0; 00140 00143 void set_handle( const std::string &handle ) { _handle = handle; } 00144 00147 std::string get_handle( void ) const { return( _handle ); } 00148 00156 virtual void plot( const class MyDXFFile *dxf, cairo_t *cairo, 00157 const Transformation *t, const double range[4] ) const = 0; 00158 00161 virtual void get_bbox( Vec3D &min, Vec3D &max, 00162 const class MyDXFFile *dxf, const Transformation *t ) const = 0; 00163 00164 friend std::ostream &operator<<( std::ostream &os, const MyDXFEntity &ent ); 00165 }; 00166 00167 00174 class MyDXFPathEntity : public MyDXFEntity 00175 { 00176 00177 protected: 00178 00179 MyDXFPathEntity() {} 00180 00181 MyDXFPathEntity( const MyDXFEntity &ent ) : MyDXFEntity(ent) {} 00182 00183 public: 00184 00187 virtual ~MyDXFPathEntity() {} 00188 00191 virtual Vec3D start( void ) const = 0; 00192 00195 virtual Vec3D end( void ) const = 0; 00196 00199 virtual void set_start( const Vec3D &s ) = 0; 00200 00203 virtual void set_end( const Vec3D &e ) = 0; 00204 00213 virtual int ray_cross( double x, double y ) const = 0; 00214 }; 00215 00216 00217 00218 00224 class MyDXFEntitySelection 00225 { 00226 00227 std::vector<uint32_t> _selection; 00228 00229 public: 00230 00233 MyDXFEntitySelection() {} 00234 00237 ~MyDXFEntitySelection() {} 00238 00241 uint32_t size() const { return( _selection.size() ); } 00242 00245 void add_entity( uint32_t a ) { _selection.push_back( a ); } 00246 00249 const uint32_t &operator()( int a ) const { 00250 if( a < 0 || a >= (int)_selection.size() ) 00251 throw( Error( ERROR_LOCATION, "index out of range" ) ); 00252 return( _selection[a] ); 00253 } 00254 00257 uint32_t &operator()( int a ) { 00258 if( a < 0 || a >= (int)_selection.size() ) 00259 throw( Error( ERROR_LOCATION, "index out of range" ) ); 00260 return( _selection[a] ); 00261 } 00262 00263 friend std::ostream &operator<<( std::ostream &os, const MyDXFEntitySelection &sel ); 00264 }; 00265 00266 00273 class MyDXFEntities 00274 { 00275 00276 MyDXFFile *_dxf; 00277 std::vector<MyDXFEntity *> _entities; 00279 public: 00280 00281 00284 MyDXFEntities( class MyDXFFile *dxf ); 00285 00288 MyDXFEntities( class MyDXFFile *dxf, MyDXFEntities *ent, MyDXFEntitySelection *sel ); 00289 00295 MyDXFEntities( class MyDXFFile *dxf, bool reading_blocks ); 00296 00299 ~MyDXFEntities(); 00300 00301 00304 void write( class MyDXFFile *dxf, std::ofstream &ostr ); 00305 00311 void write_entities( class MyDXFFile *dxf, std::ofstream &ostr ); 00312 00315 uint32_t size() const { return( _entities.size() ); } 00316 00319 const MyDXFEntity *get_entity( uint32_t a ) const { return( _entities[a] ); } 00320 00323 MyDXFEntity *get_entity( uint32_t a ) { return( _entities[a] ); } 00324 00325 00326 00327 00332 void add_entity( MyDXFEntity *e ) { _entities.push_back( e ); } 00333 00334 00337 MyDXFEntitySelection *selection_all( void ) const; 00338 00341 MyDXFEntitySelection *selection_layer( const std::string &layername ) const; 00342 00345 MyDXFEntitySelection *selection_type( EntityType type ) const; 00346 00355 MyDXFEntitySelection *selection_path_loop( MyDXFEntitySelection *selection, 00356 double eps = 1.0e-6 ); 00357 00358 00359 00360 00361 00367 bool geom_same( uint32_t a, uint32_t b, double eps = 1.0e-6 ) const; 00368 00369 /* ! \brief Check if point is inside a loop defined by a selection 00370 * of entities. 00371 * 00372 * The check is done assuming a 2D drawing in xy-plane. The 00373 * check is done using ray shooting algorithm. If exact crossing 00374 * happens perturbation algorithm is used. New test is performed 00375 * at eps distance from the first. 00376 */ 00377 bool inside_loop( MyDXFEntitySelection *selection, double x, double y, double eps = 1.0e-6 ); 00378 00388 void plot( const MyDXFEntitySelection *selection, const class MyDXFFile *dxf, 00389 cairo_t *cairo, const Transformation *t, const double range[4] ) const; 00390 00393 void get_bbox( const MyDXFEntitySelection *selection, Vec3D &min, Vec3D &max, 00394 const class MyDXFFile *dxf, const Transformation *t ) const; 00395 00396 00397 00398 /* 00399 void translate( MyDXFEntitySelection *selection, double dx, double dy, double dz ); 00400 void rotate_x( MyDXFEntitySelection *selection, double y, double z, double ang ); 00401 void rotate_y( MyDXFEntitySelection *selection, double x, double z, double ang ); 00402 void rotate_z( MyDXFEntitySelection *selection, double x, double y, double ang ); 00403 */ 00404 00409 void scale( MyDXFEntitySelection *selection, class MyDXFFile *dxf, double s ); 00410 00413 void translate( MyDXFEntitySelection *selection, class MyDXFFile *dxf, const Vec3D &dx ); 00414 00415 00416 00424 void remove( MyDXFEntitySelection *selection ); 00425 00426 00433 void explode( MyDXFEntitySelection *selection, class MyDXFFile *dxf ); 00434 00439 void explode( MyDXFEntities *ent, class MyDXFFile *dxf, const Transformation *t ) const; 00440 00441 00444 void debug_print( std::ostream &os ) const; 00445 00446 00447 }; 00448 00449 00450 00451 00452 00453 #endif 00454 00455 00456