mydxfentities.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2010-2011 Taneli Kalvas. All rights reserved.
6  *
7  * You can redistribute this software and/or modify it under the terms
8  * of the GNU General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this library (file "COPYING" included in the package);
19  * if not, write to the Free Software Foundation, Inc., 51 Franklin
20  * Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * If you have questions about your rights to use or distribute this
23  * software, please contact Berkeley Lab's Technology Transfer
24  * Department at TTD@lbl.gov. Other questions, comments and bug
25  * reports should be sent directly to the author via email at
26  * taneli.kalvas@jyu.fi.
27  *
28  * NOTICE. This software was developed under partial funding from the
29  * U.S. Department of Energy. As such, the U.S. Government has been
30  * granted for itself and others acting on its behalf a paid-up,
31  * nonexclusive, irrevocable, worldwide license in the Software to
32  * reproduce, prepare derivative works, and perform publicly and
33  * display publicly. Beginning five (5) years after the date
34  * permission to assert copyright is obtained from the U.S. Department
35  * of Energy, and subject to any subsequent five (5) year renewals,
36  * the U.S. Government is granted for itself and others acting on its
37  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
38  * the Software to reproduce, prepare derivative works, distribute
39  * copies to the public, perform publicly and display publicly, and to
40  * permit others to do so.
41  */
42 
43 #ifndef MY_DXF_ENTITIES_HPP
44 #define MY_DXF_ENTITIES_HPP 1
45 
46 
47 #include <stdint.h>
48 #include <vector>
49 #include <cairo.h>
50 #include "mydxffile.hpp"
51 #include "vec3d.hpp"
52 #include "transformation.hpp"
53 
54 
55 #define MYDXF_PERT_EPS 1.1e-12
56 
57 
60 enum EntityType {
69 };
70 
71 
78 {
79 
80 protected:
81 
82  std::string _handle;
83  std::string _layer;
84 
85  MyDXFEntity();
86 
87  //MyDXFEntity( const MyDXFEntity &ent );
88 
94  static void bbox_ppoint( Vec3D &min, Vec3D &max, const Vec3D &p );
95 
96  void write_common( class MyDXFFile *dxf, std::ofstream &ostr );
97  void process_group( class MyDXFFile *dxf );
98  void constructor_debug_print( void ) const;
99  void debug_print_base( std::ostream &os ) const;
100 
101 public:
102 
105  virtual ~MyDXFEntity() {}
106 
109  virtual MyDXFEntity *copy( void ) const = 0;
110 
117  virtual void explode( class MyDXFEntities *ent, MyDXFFile *dxf, const Transformation *t ) const = 0;
118 
121  virtual void write( class MyDXFFile *dxf, std::ofstream &ostr ) = 0;
122 
125  virtual void scale( class MyDXFFile *dxf, double s ) = 0;
126 
129  virtual void translate( class MyDXFFile *dxf, const Vec3D &dx ) = 0;
130 
133  void set_layer( const std::string &layer ) { _layer = layer; }
134 
137  std::string get_layer( void ) const { return( _layer ); }
138 
141  virtual EntityType get_type( void ) const = 0;
142 
145  void set_handle( const std::string &handle ) { _handle = handle; }
146 
149  std::string get_handle( void ) const { return( _handle ); }
150 
158  virtual void plot( const class MyDXFFile *dxf, cairo_t *cairo,
159  const Transformation *t, const double range[4] ) const = 0;
160 
163  virtual void get_bbox( Vec3D &min, Vec3D &max,
164  const class MyDXFFile *dxf, const Transformation *t ) const = 0;
165 
168  virtual void debug_print( std::ostream &os ) const = 0;
169 
170  friend std::ostream &operator<<( std::ostream &os, const MyDXFEntity &ent );
171 };
172 
173 
181 {
182 
183 protected:
184 
186 
187  MyDXFPathEntity( const MyDXFEntity &ent ) : MyDXFEntity(ent) {}
188 
189 public:
190 
193  virtual ~MyDXFPathEntity() {}
194 
197  virtual Vec3D start( void ) const = 0;
198 
201  virtual Vec3D end( void ) const = 0;
202 
205  virtual void set_start( const Vec3D &s ) = 0;
206 
209  virtual void set_end( const Vec3D &e ) = 0;
210 
219  virtual int ray_cross( double x, double y ) const = 0;
220 };
221 
222 
223 
224 
231 {
232 
233  std::vector<uint32_t> _selection;
234 
235 public:
236 
240 
244 
247  uint32_t size() const { return( _selection.size() ); }
248 
251  void add_entity( uint32_t a ) { _selection.push_back( a ); }
252 
255  const uint32_t &operator()( int a ) const {
256  if( a < 0 || a >= (int)_selection.size() )
257  throw( Error( ERROR_LOCATION, "index out of range" ) );
258  return( _selection[a] );
259  }
260 
263  uint32_t &operator()( int a ) {
264  if( a < 0 || a >= (int)_selection.size() )
265  throw( Error( ERROR_LOCATION, "index out of range" ) );
266  return( _selection[a] );
267  }
268 
269  friend std::ostream &operator<<( std::ostream &os, const MyDXFEntitySelection &sel );
270 };
271 
272 
280 {
281 
282  MyDXFFile *_dxf;
283  std::vector<MyDXFEntity *> _entities;
285 public:
286 
287 
290  MyDXFEntities( class MyDXFFile *dxf );
291 
294  MyDXFEntities( class MyDXFFile *dxf, MyDXFEntities *ent, MyDXFEntitySelection *sel );
295 
301  MyDXFEntities( class MyDXFFile *dxf, bool reading_blocks );
302 
305  ~MyDXFEntities();
306 
307 
310  void write( class MyDXFFile *dxf, std::ofstream &ostr );
311 
317  void write_entities( class MyDXFFile *dxf, std::ofstream &ostr );
318 
321  uint32_t size() const { return( _entities.size() ); }
322 
325  const MyDXFEntity *get_entity( uint32_t a ) const { return( _entities[a] ); }
326 
329  MyDXFEntity *get_entity( uint32_t a ) { return( _entities[a] ); }
330 
331 
332 
333 
338  void add_entity( MyDXFEntity *e ) { _entities.push_back( e ); }
339 
340 
343  MyDXFEntitySelection *selection_all( void ) const;
344 
347  MyDXFEntitySelection *selection_layer( const std::string &layername ) const;
348 
352 
362  double eps = 1.0e-6 );
363 
364 
365 
366 
367 
373  bool geom_same( uint32_t a, uint32_t b, double eps = 1.0e-6 ) const;
374 
375  /* ! \brief Check if point is inside a loop defined by a selection
376  * of entities.
377  *
378  * The check is done assuming a 2D drawing in xy-plane. The
379  * check is done using ray shooting algorithm. If exact crossing
380  * happens perturbation algorithm is used. New test is performed
381  * at eps distance from the first.
382  */
383  bool inside_loop( MyDXFEntitySelection *selection, double x, double y, double eps = 1.0e-6 );
384 
394  void plot( const MyDXFEntitySelection *selection, const class MyDXFFile *dxf,
395  cairo_t *cairo, const Transformation *t, const double range[4] ) const;
396 
399  void get_bbox( const MyDXFEntitySelection *selection, Vec3D &min, Vec3D &max,
400  const class MyDXFFile *dxf, const Transformation *t ) const;
401 
402 
403 
404  /*
405  void translate( MyDXFEntitySelection *selection, double dx, double dy, double dz );
406  void rotate_x( MyDXFEntitySelection *selection, double y, double z, double ang );
407  void rotate_y( MyDXFEntitySelection *selection, double x, double z, double ang );
408  void rotate_z( MyDXFEntitySelection *selection, double x, double y, double ang );
409  */
410 
415  void scale( MyDXFEntitySelection *selection, class MyDXFFile *dxf, double s );
416 
419  void translate( MyDXFEntitySelection *selection, class MyDXFFile *dxf, const Vec3D &dx );
420 
421 
422 
430  void remove( MyDXFEntitySelection *selection );
431 
432 
439  void explode( MyDXFEntitySelection *selection, class MyDXFFile *dxf );
440 
445  void explode( MyDXFEntities *ent, class MyDXFFile *dxf, const Transformation *t ) const;
446 
447 
450  void debug_print( std::ostream &os ) const;
451 
452 
453 };
454 
455 
456 
457 
458 
459 #endif
460 
461 
462 
463 
uint32_t size() const
Return number of entities.
Definition: mydxfentities.hpp:321
MyDXFPathEntity()
Definition: mydxfentities.hpp:185
bool geom_same(uint32_t a, uint32_t b, double eps=1.0e-6) const
Check if two entities are geometrically same.
std::string _handle
Definition: mydxfentities.hpp:82
Basic error class.
Definition: error.hpp:142
virtual void explode(class MyDXFEntities *ent, MyDXFFile *dxf, const Transformation *t) const =0
Explode into entities.
static void bbox_ppoint(Vec3D &min, Vec3D &max, const Vec3D &p)
Propose a point to bounding box.
void constructor_debug_print(void) const
Definition: mydxfentities.hpp:68
virtual Vec3D end(void) const =0
Get end point of path entity.
void debug_print(std::ostream &os) const
Print debugging information to os.
std::string get_handle(void) const
Get entity handle.
Definition: mydxfentities.hpp:149
MyDXFEntitySelection * selection_all(void) const
Make a new selection containg all entities from database.
void translate(MyDXFEntitySelection *selection, class MyDXFFile *dxf, const Vec3D &dx)
Translate selected entities by dx.
void plot(const MyDXFEntitySelection *selection, const class MyDXFFile *dxf, cairo_t *cairo, const Transformation *t, const double range[4]) const
Plot selected entities with cairo.
const MyDXFEntity * get_entity(uint32_t a) const
Return const pointer to entity a.
Definition: mydxfentities.hpp:325
friend std::ostream & operator<<(std::ostream &os, const MyDXFEntitySelection &sel)
~MyDXFEntitySelection()
Destructor.
Definition: mydxfentities.hpp:243
virtual MyDXFEntity * copy(void) const =0
Get a new copy of entity.
Three dimensional vectors.
virtual void set_start(const Vec3D &s)=0
Set start point of path entity.
virtual void scale(class MyDXFFile *dxf, double s)=0
Scale entity by factor s.
void write(class MyDXFFile *dxf, std::ofstream &ostr)
Write entities section of dxf file to stream.
const uint32_t & operator()(int a) const
Get a const reference to entity number in selection at location a.
Definition: mydxfentities.hpp:255
void set_handle(const std::string &handle)
Set entity handle.
Definition: mydxfentities.hpp:145
uint32_t size() const
Return number of entities in selection.
Definition: mydxfentities.hpp:247
DXF File.
DXF file class.
Definition: mydxffile.hpp:68
virtual void write(class MyDXFFile *dxf, std::ofstream &ostr)=0
Write dxf file to stream.
MyDXFEntities(class MyDXFFile *dxf)
Construct empty entities database.
void write_entities(class MyDXFFile *dxf, std::ofstream &ostr)
Write a list of entities to stream.
DXF path entity base class.
Definition: mydxfentities.hpp:180
#define ERROR_LOCATION
Macro for setting error location when throwing errors.
Definition: error.hpp:72
MyDXFEntitySelection()
Construct empty selection.
Definition: mydxfentities.hpp:239
virtual void get_bbox(Vec3D &min, Vec3D &max, const class MyDXFFile *dxf, const Transformation *t) const =0
Return bounding box of entity.
Definition: mydxfentities.hpp:65
bool inside_loop(MyDXFEntitySelection *selection, double x, double y, double eps=1.0e-6)
Transformation for homogenous three dimensional space.
Definition: transformation.hpp:59
void get_bbox(const MyDXFEntitySelection *selection, Vec3D &min, Vec3D &max, const class MyDXFFile *dxf, const Transformation *t) const
Get bounding box containing all entities in selection.
void process_group(class MyDXFFile *dxf)
void debug_print_base(std::ostream &os) const
virtual int ray_cross(double x, double y) const =0
Check for ray crossing.
MyDXFEntity * get_entity(uint32_t a)
Return pointer to entity a.
Definition: mydxfentities.hpp:329
~MyDXFEntities()
Destructor.
uint32_t & operator()(int a)
Get reference to entity number in selection at location a.
Definition: mydxfentities.hpp:263
virtual ~MyDXFEntity()
Virtual destructor.
Definition: mydxfentities.hpp:105
MyDXFEntitySelection * selection_layer(const std::string &layername) const
Make a new selection containg entities from named layer.
virtual Vec3D start(void) const =0
Get start point of path entity.
virtual EntityType get_type(void) const =0
Get entity type.
virtual void translate(class MyDXFFile *dxf, const Vec3D &dx)=0
Translate entity by dx.
void write_common(class MyDXFFile *dxf, std::ofstream &ostr)
friend std::ostream & operator<<(std::ostream &os, const MyDXFEntity &ent)
Definition: mydxfentities.hpp:62
void scale(MyDXFEntitySelection *selection, class MyDXFFile *dxf, double s)
Scale selected entities by factor s.
virtual void debug_print(std::ostream &os) const =0
Print debugging information to stream os.
DXF entity selection.
Definition: mydxfentities.hpp:230
MyDXFPathEntity(const MyDXFEntity &ent)
Definition: mydxfentities.hpp:187
Definition: mydxfentities.hpp:63
virtual ~MyDXFPathEntity()
Virtual destructor.
Definition: mydxfentities.hpp:193
Definition: mydxfentities.hpp:61
void add_entity(uint32_t a)
Add entity number a in selection.
Definition: mydxfentities.hpp:251
DXF entity database.
Definition: mydxfentities.hpp:279
Definition: mydxfentities.hpp:64
MyDXFEntitySelection * selection_path_loop(MyDXFEntitySelection *selection, double eps=1.0e-6)
Build complete loops.
virtual void plot(const class MyDXFFile *dxf, cairo_t *cairo, const Transformation *t, const double range[4]) const =0
Plot entity with cairo.
Definition: mydxfentities.hpp:67
std::string get_layer(void) const
Get layer.
Definition: mydxfentities.hpp:137
virtual void set_end(const Vec3D &e)=0
Set end point of path entity.
MyDXFEntitySelection * selection_type(EntityType type) const
Make a new selection containg entities of given type.
Three dimensional vector.
Definition: vec3d.hpp:58
void set_layer(const std::string &layer)
Set layer.
Definition: mydxfentities.hpp:133
Full transformation for three dimensional homogenous space.
std::string _layer
Definition: mydxfentities.hpp:83
Definition: mydxfentities.hpp:66
DXF entity base class.
Definition: mydxfentities.hpp:77
void explode(MyDXFEntitySelection *selection, class MyDXFFile *dxf)
Explode selected insert entities.
void add_entity(MyDXFEntity *e)
Add entity to list.
Definition: mydxfentities.hpp:338
EntityType
Entity type.
Definition: mydxfentities.hpp:60