00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <freecloth/geom/geMeshBuilder.h>
00020 #include <freecloth/base/algorithm>
00021
00022
00023
00024
00025 FREECLOTH_NAMESPACE_START
00026
00027
00028
00029
00030
00031
00032 GeMeshBuilder::GeMeshBuilder()
00033 : _meshPtr( RCShdPtr<GeMesh>( new GeMesh ) )
00034 {
00035 }
00036
00037
00038
00039 void GeMeshBuilder::preallocVertices( UInt32 nb )
00040 {
00041 _meshPtr->_vertices.reserve( nb );
00042 }
00043
00044
00045
00046 void GeMeshBuilder::preallocTextureVertices( UInt32 nb )
00047 {
00048 _meshPtr->_textureVertices.reserve( nb );
00049 }
00050
00051
00052
00053 void GeMeshBuilder::preallocFaces( UInt32 nb )
00054 {
00055 _meshPtr->_faces.reserve( nb );
00056 }
00057
00058
00059
00060 GeMeshBuilder::VertexId GeMeshBuilder::addVertex( const VertexType& vertex )
00061 {
00062 VertexId result = _meshPtr->getNbVertices();
00063 _meshPtr->_vertices.push_back( vertex );
00064 return result;
00065 }
00066
00067
00068
00069 GeMeshBuilder::VertexId GeMeshBuilder::addVertices(
00070 VertexConstIterator const& beginIt,
00071 VertexConstIterator const& endIt
00072 ) {
00073 VertexId result = _meshPtr->getNbVertices();
00074 _meshPtr->_vertices.insert( _meshPtr->_vertices.end(), beginIt, endIt );
00075 return result;
00076 }
00077
00078
00079
00080 UInt32 GeMeshBuilder::getNbVertices() const
00081 {
00082 return _meshPtr->getNbVertices();
00083 }
00084
00085
00086
00087 const GeMeshBuilder::VertexType& GeMeshBuilder::getVertex( VertexId vid ) const
00088 {
00089 return _meshPtr->getVertex( vid );
00090 }
00091
00092
00093
00094 GeMeshBuilder::TextureVertexId GeMeshBuilder::addTextureVertex(
00095 const TextureVertexType& textureVertex
00096 )
00097 {
00098 TextureVertexId result = _meshPtr->getNbTextureVertices();
00099 _meshPtr->_textureVertices.push_back( textureVertex );
00100 return result;
00101 }
00102
00103
00104
00105 GeMeshBuilder::TextureVertexId GeMeshBuilder::addTextureVertices(
00106 TextureVertexConstIterator const& beginIt,
00107 TextureVertexConstIterator const& endIt
00108 ) {
00109 TextureVertexId result = _meshPtr->getNbTextureVertices();
00110 _meshPtr->_textureVertices.insert(
00111 _meshPtr->_textureVertices.end(),
00112 beginIt, endIt
00113 );
00114 return result;
00115 }
00116
00117
00118
00119 UInt32 GeMeshBuilder::getNbTextureVertices() const
00120 {
00121 return _meshPtr->getNbTextureVertices();
00122 }
00123
00124
00125
00126 const GeMeshBuilder::TextureVertexType& GeMeshBuilder::getTextureVertex(
00127 TextureVertexId vid
00128 ) const {
00129 return _meshPtr->getTextureVertex( vid );
00130 }
00131
00132
00133
00134 GeMeshBuilder::FaceId GeMeshBuilder::addFace(
00135 VertexId v1, VertexId v2, VertexId v3,
00136 TextureVertexId tv1, TextureVertexId tv2, TextureVertexId tv3
00137 ) {
00138 FaceId result = _meshPtr->getNbFaces();
00139 _meshPtr->_faces.push_back(
00140 GeMesh::Face( v1, v2, v3, tv1, tv2, tv3 )
00141 );
00142 return result;
00143 }
00144
00145
00146
00147 GeMeshBuilder::FaceId GeMeshBuilder::addFace(
00148 VertexId vids[ GeMesh::FaceType::NB_VERTICES ],
00149 TextureVertexId tvids[ GeMesh::FaceType::NB_VERTICES ]
00150 ) {
00151 FaceId result = _meshPtr->getNbFaces();
00152 _meshPtr->_faces.push_back(
00153 GeMesh::Face(
00154 vids[ 0 ], vids[ 1 ], vids[ 2 ],
00155 tvids[ 0 ], tvids[ 1 ], tvids[ 2 ]
00156 )
00157 );
00158 return result;
00159 }
00160
00161
00162
00163 GeMeshBuilder::FaceId GeMeshBuilder::addFaces(
00164 FaceConstIterator const& beginIt,
00165 FaceConstIterator const& endIt
00166 ) {
00167 FaceId result = _meshPtr->getNbFaces();
00168 std::vector<GeMesh::Face>::const_iterator realBeginIt, realEndIt;
00169 realBeginIt = beginIt._wrapper._mesh->_faces.begin()
00170 + beginIt._wrapper._faceId;
00171 realEndIt = endIt._wrapper._mesh->_faces.begin()
00172 + endIt._wrapper._faceId;
00173
00174 _meshPtr->_faces.insert(
00175 _meshPtr->_faces.end(),
00176 realBeginIt,
00177 realEndIt
00178 );
00179 return result;
00180 }
00181
00182
00183
00184 UInt32 GeMeshBuilder::getNbFaces() const
00185 {
00186 return _meshPtr->getNbFaces();
00187 }
00188
00189
00190
00191 RCShdPtr<GeMesh> GeMeshBuilder::createMesh()
00192 {
00193 VertexId maxvid = 0;
00194 TextureVertexId maxtvid = 0;
00195 FaceConstIterator fi;
00196
00197 for ( fi = _meshPtr->beginFace(); fi != _meshPtr->endFace(); ++fi ) {
00198 for ( FaceVertexId fvid = 0; fvid < fi->getNbVertices(); ++fvid ) {
00199 maxvid = std::max( maxvid, fi->getVertexId( fvid ) );
00200 maxtvid = std::max( maxtvid, fi->getTextureVertexId( fvid ) );
00201 }
00202 }
00203
00204 DGFX_ASSERT( _meshPtr->getNbFaces() > 0 );
00205 DGFX_ASSERT( maxvid < _meshPtr->getNbVertices() );
00206 DGFX_ASSERT(
00207 ! _meshPtr->hasTexture() || maxtvid < _meshPtr->getNbTextureVertices()
00208 );
00209
00210 RCShdPtr<GeMesh> result( _meshPtr );
00211
00212 _meshPtr = RCShdPtr<GeMesh>( new GeMesh );
00213 return result;
00214 }
00215
00216 FREECLOTH_NAMESPACE_END