1 | /* |
2 | Open Asset Import Library (assimp) |
3 | ---------------------------------------------------------------------- |
4 | |
5 | Copyright (c) 2006-2019, assimp team |
6 | |
7 | |
8 | All rights reserved. |
9 | |
10 | Redistribution and use of this software in source and binary forms, |
11 | with or without modification, are permitted provided that the |
12 | following conditions are met: |
13 | |
14 | * Redistributions of source code must retain the above |
15 | copyright notice, this list of conditions and the |
16 | following disclaimer. |
17 | |
18 | * Redistributions in binary form must reproduce the above |
19 | copyright notice, this list of conditions and the |
20 | following disclaimer in the documentation and/or other |
21 | materials provided with the distribution. |
22 | |
23 | * Neither the name of the assimp team, nor the names of its |
24 | contributors may be used to endorse or promote products |
25 | derived from this software without specific prior |
26 | written permission of the assimp team. |
27 | |
28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
29 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
31 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
32 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
33 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
34 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
35 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
36 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
37 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
38 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39 | |
40 | ---------------------------------------------------------------------- |
41 | */ |
42 | |
43 | /** @file BlenderTessellator.h |
44 | * @brief A simple tessellation wrapper |
45 | */ |
46 | #ifndef INCLUDED_AI_BLEND_TESSELLATOR_H |
47 | #define INCLUDED_AI_BLEND_TESSELLATOR_H |
48 | |
49 | // Use these to toggle between GLU Tessellate or poly2tri |
50 | // Note (acg) keep GLU Tessellate disabled by default - if it is turned on, |
51 | // assimp needs to be linked against GLU, which is currently not yet |
52 | // made configurable in CMake and potentially not wanted by most users |
53 | // as it requires a Gl environment. |
54 | #ifndef ASSIMP_BLEND_WITH_GLU_TESSELLATE |
55 | # define ASSIMP_BLEND_WITH_GLU_TESSELLATE 0 |
56 | #endif |
57 | |
58 | #ifndef ASSIMP_BLEND_WITH_POLY_2_TRI |
59 | # define ASSIMP_BLEND_WITH_POLY_2_TRI 1 |
60 | #endif |
61 | |
62 | #include <assimp/LogAux.h> |
63 | |
64 | #if ASSIMP_BLEND_WITH_GLU_TESSELLATE |
65 | |
66 | #if defined( WIN32 ) || defined( _WIN32 ) || defined( _MSC_VER ) |
67 | #include <windows.h> |
68 | #endif |
69 | #include <GL/glu.h> |
70 | |
71 | namespace Assimp |
72 | { |
73 | class BlenderBMeshConverter; |
74 | |
75 | // TinyFormatter.h |
76 | namespace Formatter |
77 | { |
78 | template < typename T,typename TR, typename A > class basic_formatter; |
79 | typedef class basic_formatter< char, std::char_traits< char >, std::allocator< char > > format; |
80 | } |
81 | |
82 | // BlenderScene.h |
83 | namespace Blender |
84 | { |
85 | struct MLoop; |
86 | struct MVert; |
87 | |
88 | struct VertexGL |
89 | { |
90 | GLdouble X; |
91 | GLdouble Y; |
92 | GLdouble Z; |
93 | int index; |
94 | int magic; |
95 | |
96 | VertexGL( GLdouble X, GLdouble Y, GLdouble Z, int index, int magic ): X( X ), Y( Y ), Z( Z ), index( index ), magic( magic ) { } |
97 | }; |
98 | |
99 | struct DrawCallGL |
100 | { |
101 | GLenum drawMode; |
102 | int baseVertex; |
103 | int vertexCount; |
104 | |
105 | DrawCallGL( GLenum drawMode, int baseVertex ): drawMode( drawMode ), baseVertex( baseVertex ), vertexCount( 0 ) { } |
106 | }; |
107 | |
108 | struct TessDataGL |
109 | { |
110 | std::vector< DrawCallGL > drawCalls; |
111 | std::vector< VertexGL > vertices; |
112 | }; |
113 | } |
114 | |
115 | class BlenderTessellatorGL: public LogFunctions< BlenderTessellatorGL > |
116 | { |
117 | public: |
118 | BlenderTessellatorGL( BlenderBMeshConverter& converter ); |
119 | ~BlenderTessellatorGL( ); |
120 | |
121 | void Tessellate( const Blender::MLoop* polyLoop, int vertexCount, const std::vector< Blender::MVert >& vertices ); |
122 | |
123 | private: |
124 | void AssertVertexCount( int vertexCount ); |
125 | void GenerateLoopVerts( std::vector< Blender::VertexGL >& polyLoopGL, const Blender::MLoop* polyLoop, int vertexCount, const std::vector< Blender::MVert >& vertices ); |
126 | void Tesssellate( std::vector< Blender::VertexGL >& polyLoopGL, Blender::TessDataGL& tessData ); |
127 | void TriangulateDrawCalls( const Blender::TessDataGL& tessData ); |
128 | void MakeFacesFromTris( const Blender::VertexGL* vertices, int vertexCount ); |
129 | void MakeFacesFromTriStrip( const Blender::VertexGL* vertices, int vertexCount ); |
130 | void MakeFacesFromTriFan( const Blender::VertexGL* vertices, int vertexCount ); |
131 | |
132 | static void TessellateBegin( GLenum drawModeGL, void* userData ); |
133 | static void TessellateEnd( void* userData ); |
134 | static void TessellateVertex( const void* vtxData, void* userData ); |
135 | static void TessellateCombine( const GLdouble intersection[ 3 ], const GLdouble* [ 4 ], const GLfloat [ 4 ], GLdouble** out, void* userData ); |
136 | static void TessellateEdgeFlag( GLboolean edgeFlag, void* userData ); |
137 | static void TessellateError( GLenum errorCode, void* userData ); |
138 | |
139 | BlenderBMeshConverter* converter; |
140 | }; |
141 | } // end of namespace Assimp |
142 | |
143 | #endif // ASSIMP_BLEND_WITH_GLU_TESSELLATE |
144 | |
145 | #if ASSIMP_BLEND_WITH_POLY_2_TRI |
146 | |
147 | #ifdef ASSIMP_USE_HUNTER |
148 | # include <poly2tri/poly2tri.h> |
149 | #else |
150 | # include "../contrib/poly2tri/poly2tri/poly2tri.h" |
151 | #endif |
152 | |
153 | namespace Assimp |
154 | { |
155 | class BlenderBMeshConverter; |
156 | |
157 | // TinyFormatter.h |
158 | namespace Formatter |
159 | { |
160 | template < typename T,typename TR, typename A > class basic_formatter; |
161 | typedef class basic_formatter< char, std::char_traits< char >, std::allocator< char > > format; |
162 | } |
163 | |
164 | // BlenderScene.h |
165 | namespace Blender |
166 | { |
167 | struct MLoop; |
168 | struct MVert; |
169 | |
170 | struct PointP2T |
171 | { |
172 | aiVector3D point3D; |
173 | p2t::Point point2D; |
174 | int magic; |
175 | int index; |
176 | }; |
177 | |
178 | struct PlaneP2T |
179 | { |
180 | aiVector3D centre; |
181 | aiVector3D normal; |
182 | }; |
183 | } |
184 | |
185 | class BlenderTessellatorP2T: public LogFunctions< BlenderTessellatorP2T > |
186 | { |
187 | public: |
188 | BlenderTessellatorP2T( BlenderBMeshConverter& converter ); |
189 | ~BlenderTessellatorP2T( ); |
190 | |
191 | void Tessellate( const Blender::MLoop* polyLoop, int vertexCount, const std::vector< Blender::MVert >& vertices ); |
192 | |
193 | private: |
194 | void AssertVertexCount( int vertexCount ); |
195 | void Copy3DVertices( const Blender::MLoop* polyLoop, int vertexCount, const std::vector< Blender::MVert >& vertices, std::vector< Blender::PointP2T >& targetVertices ) const; |
196 | aiMatrix4x4 GeneratePointTransformMatrix( const Blender::PlaneP2T& plane ) const; |
197 | void TransformAndFlattenVectices( const aiMatrix4x4& transform, std::vector< Blender::PointP2T >& vertices ) const; |
198 | void ReferencePoints( std::vector< Blender::PointP2T >& points, std::vector< p2t::Point* >& pointRefs ) const; |
199 | inline Blender::PointP2T& GetActualPointStructure( p2t::Point& point ) const; |
200 | void MakeFacesFromTriangles( std::vector< p2t::Triangle* >& triangles ) const; |
201 | |
202 | // Adapted from: http://missingbytes.blogspot.co.uk/2012/06/fitting-plane-to-point-cloud.html |
203 | float FindLargestMatrixElem( const aiMatrix3x3& mtx ) const; |
204 | aiMatrix3x3 ScaleMatrix( const aiMatrix3x3& mtx, float scale ) const; |
205 | aiVector3D GetEigenVectorFromLargestEigenValue( const aiMatrix3x3& mtx ) const; |
206 | Blender::PlaneP2T FindLLSQPlane( const std::vector< Blender::PointP2T >& points ) const; |
207 | |
208 | BlenderBMeshConverter* converter; |
209 | }; |
210 | } // end of namespace Assimp |
211 | |
212 | #endif // ASSIMP_BLEND_WITH_POLY_2_TRI |
213 | |
214 | #endif // INCLUDED_AI_BLEND_TESSELLATOR_H |
215 | |