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