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