Carna  Version 3.0.1
MeshFactory.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef MESHFACTORY_H_6014714286
13 #define MESHFACTORY_H_6014714286
14 
17 #include <Carna/base/IndexBuffer.h>
18 #include <Carna/base/ManagedMesh.h>
19 #include <Carna/base/Vertex.h>
20 #include <Carna/base/math.h>
21 
26 namespace Carna
27 {
28 
29 namespace base
30 {
31 
32 
33 
34 // ----------------------------------------------------------------------------------
35 // MeshFactory
36 // ----------------------------------------------------------------------------------
37 
48 template< typename VertexType >
50 {
51 
52  template< typename VectorType >
53  static VertexType vertex( const VectorType& );
54 
55 public:
56 
61  static ManagedMesh< VertexType, uint8_t >& createBox( float width, float height, float depth );
62 
66 
71 
72 }; // MeshFactory
73 
74 
75 template< typename VertexType >
76 template< typename VectorType >
77 VertexType MeshFactory< VertexType >::vertex( const VectorType& v )
78 {
79  VertexType vertex;
80  vertex.x = v.x();
81  vertex.y = v.y();
82  vertex.z = v.z();
83  return vertex;
84 }
85 
86 
87 template< typename VertexType >
89 {
90  return createBox( size.x(), size.y(), size.z() );
91 }
92 
93 
94 template< typename VertexType >
96 {
97  const math::Matrix4f baseTransform = math::scaling4f( sizeX / 2, sizeY / 2, sizeZ / 2 );
98 
99  /* Define faces.
100  */
101  math::Matrix4f transforms[ 6 ];
102  transforms[ 0 ] = math::basis4f( math::Vector3f( 0, 0, +1 ), math::Vector3f( 0, +1, 0 ), math::Vector3f( -1, 0, 0 ) ); // left
103  transforms[ 1 ] = math::basis4f( math::Vector3f( 0, 0, -1 ), math::Vector3f( 0, +1, 0 ), math::Vector3f( +1, 0, 0 ) ); // right
104  transforms[ 2 ] = math::basis4f( math::Vector3f( +1, 0, 0 ), math::Vector3f( 0, +1, 0 ), math::Vector3f( 0, 0, +1 ) ); // front
105  transforms[ 3 ] = math::basis4f( math::Vector3f( -1, 0, 0 ), math::Vector3f( 0, +1, 0 ), math::Vector3f( 0, 0, -1 ) ); // back
106  transforms[ 4 ] = math::basis4f( math::Vector3f( +1, 0, 0 ), math::Vector3f( 0, 0, -1 ), math::Vector3f( 0, +1, 0 ) ); // top
107  transforms[ 5 ] = math::basis4f( math::Vector3f( +1, 0, 0 ), math::Vector3f( 0, 0, +1 ), math::Vector3f( 0, -1, 0 ) ); // bottom
108 
109  const std::size_t verticesCount = 6 * 4;
110  const std::size_t indicesCount = 6 * 2 * 3;
111 
112  typedef ManagedMesh< VertexType, uint8_t > MeshInstance;
113  typedef typename MeshInstance::Vertex Vertex;
114  typedef typename MeshInstance:: Index Index;
115  Vertex vertices[ verticesCount ];
116  Index indices[ indicesCount ];
117 
118  int lastVertex = -1;
119  int lastIndex = -1;
120 
121  /* Create vertices and indices.
122  */
123  for( unsigned int faceIndex = 0; faceIndex < 6; ++faceIndex )
124  {
125  const math::Matrix4f transform = baseTransform * transforms[ faceIndex ];
126  vertices[ ++lastVertex ] = vertex( transform * math::Vector4f( -1, -1, 1, 1 ) );
127  vertices[ ++lastVertex ] = vertex( transform * math::Vector4f( +1, -1, 1, 1 ) );
128  vertices[ ++lastVertex ] = vertex( transform * math::Vector4f( +1, +1, 1, 1 ) );
129  vertices[ ++lastVertex ] = vertex( transform * math::Vector4f( -1, +1, 1, 1 ) );
130 
131  indices[ ++lastIndex ] = lastVertex - 3;
132  indices[ ++lastIndex ] = lastVertex - 2;
133  indices[ ++lastIndex ] = lastVertex;
134 
135  indices[ ++lastIndex ] = lastVertex;
136  indices[ ++lastIndex ] = lastVertex - 2;
137  indices[ ++lastIndex ] = lastVertex - 1;
138  }
139 
140  return MeshInstance::create
142  , vertices, verticesCount
143  , indices, indicesCount );
144 }
145 
146 
147 template< typename VertexType >
149 {
150  typedef ManagedMesh< VertexType, uint8_t > MeshInstance;
151  typedef typename MeshInstance::Vertex Vertex;
152  typedef typename MeshInstance:: Index Index;
153 
154  Vertex vertex;
155  Index index = 0;
156 
157  return MeshInstance::create( IndexBufferBase::PRIMITIVE_TYPE_POINTS, &vertex, 1, &index, 1 );
158 }
159 
160 
161 
162 } // namespace Carna :: base
163 
164 } // namespace Carna
165 
166 #endif // MESHFACTORY_H_6014714286
Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
Matrix4f scaling4f(float x, float y, float z)
Creates scaling matrix for homogeneous coordinates.
Definition: math.h:292
static const unsigned int PRIMITIVE_TYPE_POINTS
Draws points. Indicates that each index makes up a single point.
Definition: IndexBuffer.h:102
static ManagedMesh< VertexType, uint8_t > & createBox(float width, float height, float depth)
Creates box with width, height and depth. The box is centered in .
Definition: MeshFactory.h:95
Eigen::Matrix< float, 4, 1 > Vector4f
Defines vector.
Definition: math.h:195
Defines Carna::base::VertexAttributes.
static const unsigned int PRIMITIVE_TYPE_TRIANGLES
Draws triangles. Indicates that the indices make up the th triangle with .
Definition: IndexBuffer.h:65
Matrix4f basis4f(const Vector4f &x, const Vector4f &y, const Vector4f &z, const Vector4f &t=Vector4f(0, 0, 0, 0))
Creates basis embedded into a homogenous coordinates matrix.
Definition: math.h:242
Eigen::Matrix< float, 3, 1 > Vector3f
Defines vector.
Definition: math.h:196
Defines Carna::base::IndexBuffer.
Creates simple predefined ManagedMesh instances.
Definition: MeshFactory.h:49
Defines Carna::base::ManagedMesh.
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > Matrix4f
Defines matrix.
Definition: math.h:193
Defines Carna::base::VertexBase.
Implements MeshBase class for particular VertexType and IndexType.
Definition: ManagedMesh.h:147
static ManagedMesh< VertexType, uint8_t > & createPoint()
Creates mesh that consists of a single point.
Definition: MeshFactory.h:148
Defines Carna::base::VertexBuffer.