stlfile.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 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 STLFILE_HPP
44 #define STLFILE_HPP 1
45 
46 
47 #include <fstream>
48 #include <string>
49 #include <vector>
50 #include "vec3d.hpp"
51 
52 
53 class STLFile {
54 
55  class Triangle {
56 
57  Vec3D _normal;
58  Vec3D _p1;
59  Vec3D _p2;
60  Vec3D _p3;
61  uint16_t _attr;
62 
63  void static read_binary_float_vector( Vec3D &x, std::ifstream &ifstr );
64  static void bbox_ppoint( Vec3D &min, Vec3D &max, const Vec3D &p );
65 
66  public:
67 
68  Triangle( std::ifstream &ifstr );
69  ~Triangle();
70 
71  const Vec3D &normal( void ) const;
72  const Vec3D &p1( void ) const;
73  const Vec3D &p2( void ) const;
74  const Vec3D &p3( void ) const;
75  const Vec3D &operator[]( int i ) const;
76 
77  void update_bbox( Vec3D &min, Vec3D &max ) const;
78 
79  int ray_cross( const Vec3D &x, const Vec3D &l ) const;
80 
81  void debug_print( std::ostream &os ) const;
82  };
83 
84  class VTriangle {
85 
86  uint32_t _v[3];
87  Vec3D _normal;
88 
89  public:
90 
91  VTriangle( uint32_t v1, uint32_t v2, uint32_t v3, const Vec3D &normal );
92  VTriangle( const uint32_t v[3], const Vec3D &normal );
93  ~VTriangle();
94 
95  const Vec3D &normal( void ) const;
96  const uint32_t &operator[]( int i ) const;
97 
98  bool inside( const Vec3D &x ) const;
99 
100  void debug_print( std::ostream &os ) const;
101 
104  friend std::ostream &operator<<( std::ostream &os, const VTriangle &vtri );
105  };
106 
107  bool _ascii;
108  std::vector<Triangle> _triangle; // Original triangle data
109 
110  std::vector<Vec3D> _vertex; // Vertex list
111  std::vector<VTriangle> _vtri; // Vertex made triangles
112 
113  void read_binary( std::ifstream &ifstr );
114  void build_vtriangle_data( void );
115 
116 public:
117 
118  STLFile( const std::string &filename );
119 
120  ~STLFile();
121 
122  size_t size( void );
123 
124  bool inside( const Vec3D &x, double eps = 1.0e-6 ) const;
125 
128  void get_bbox( Vec3D &min, Vec3D &max ) const;
129 
132  void debug_print( std::ostream &os ) const;
133 
136  friend std::ostream &operator<<( std::ostream &os, const VTriangle &vtri );
137 };
138 
139 
140 #endif
141 
142 
void debug_print(std::ostream &os) const
Print debugging information to os.
friend std::ostream & operator<<(std::ostream &os, const VTriangle &vtri)
Outputting to stream.
STLFile(const std::string &filename)
Three dimensional vectors.
Definition: stlfile.hpp:53
size_t size(void)
void get_bbox(Vec3D &min, Vec3D &max) const
Return bounding box of entity.
bool inside(const Vec3D &x, double eps=1.0e-6) const
Three dimensional vector.
Definition: vec3d.hpp:58