vrshoot

diff libs/assimp/ObjTools.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/ObjTools.h	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,263 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +
    1.44 +/**	@file	ObjTools.h
    1.45 + *	@brief	Some helpful templates for text parsing
    1.46 + */
    1.47 +#ifndef OBJ_TOOLS_H_INC
    1.48 +#define OBJ_TOOLS_H_INC
    1.49 +
    1.50 +#include "fast_atof.h"
    1.51 +
    1.52 +namespace Assimp
    1.53 +{
    1.54 +
    1.55 +/**	@brief	Returns true, if the last entry of the buffer is reached.
    1.56 + *	@param	it	Iterator of current position.
    1.57 + *	@param	end	Iterator with end of buffer.
    1.58 + *	@return	true, if the end of the buffer is reached.
    1.59 + */
    1.60 +template<class char_t>
    1.61 +inline bool isEndOfBuffer(  char_t it, char_t end )
    1.62 +{
    1.63 +	if ( it == end )
    1.64 +	{
    1.65 +		return true;
    1.66 +	}
    1.67 +	else
    1.68 +	{
    1.69 +		end--;
    1.70 +	}
    1.71 +	return ( it == end );	
    1.72 +}
    1.73 +
    1.74 +/** @brief	Returns true, if token is a space on any supported platform
    1.75 +*	@param	token	Token to search in
    1.76 +*	@return	true, if token is a space			
    1.77 +*/
    1.78 +inline bool isSeparator( char token )
    1.79 +{
    1.80 +	return ( token == ' ' || 
    1.81 +			token == '\n' || 
    1.82 +			token == '\f' || 
    1.83 +			token == '\r' ||
    1.84 +			token == '\t' );
    1.85 +}
    1.86 +
    1.87 +/**	@brief	Returns true, fi token id a new line marking token.
    1.88 + *	@param	token	Token to search in
    1.89 + *	@return	true, if token is a newline token.
    1.90 + */
    1.91 +inline bool isNewLine( char token )
    1.92 +{
    1.93 +	return ( token == '\n' || token == '\f' || token == '\r' );
    1.94 +}
    1.95 +
    1.96 +/**	@brief	Returns next word separated by a space
    1.97 + *	@param	pBuffer	Pointer to data buffer
    1.98 + *	@param	pEnd	Pointer to end of buffer
    1.99 + *	@return	Pointer to next space
   1.100 + */
   1.101 +template<class Char_T>
   1.102 +inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd )
   1.103 +{
   1.104 +	while ( !isEndOfBuffer( pBuffer, pEnd ) )
   1.105 +	{
   1.106 +		if ( !isSeparator( *pBuffer ) || isNewLine( *pBuffer ) )
   1.107 +			break;
   1.108 +		pBuffer++;
   1.109 +	}
   1.110 +	return pBuffer;
   1.111 +}
   1.112 +
   1.113 +/**	@brief	Returns ponter a next token
   1.114 + *	@param	pBuffer	Pointer to data buffer
   1.115 + *	@param	pEnd	Pointer to end of buffer
   1.116 + *	@return	Pointer to next token
   1.117 + */
   1.118 +template<class Char_T>
   1.119 +inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd )
   1.120 +{
   1.121 +	while ( !isEndOfBuffer( pBuffer, pEnd ) )
   1.122 +	{
   1.123 +		if ( isSeparator( *pBuffer ) )
   1.124 +			break;
   1.125 +		pBuffer++;
   1.126 +	}
   1.127 +	return getNextWord( pBuffer, pEnd );
   1.128 +}
   1.129 +
   1.130 +/**	@brief	Skips a line
   1.131 + *	@param	it		Iterator set to current position
   1.132 + *	@param	end		Iterator set to end of scratch buffer for readout
   1.133 + *	@param	uiLine	Current linenumber in format
   1.134 + *	@return	Current-iterator with new position
   1.135 + */
   1.136 +template<class char_t>
   1.137 +inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine )
   1.138 +{
   1.139 +	while ( !isEndOfBuffer( it, end ) && !isNewLine( *it ) )
   1.140 +		++it;
   1.141 +	if ( it != end )
   1.142 +	{
   1.143 +		++it;
   1.144 +		++uiLine;
   1.145 +	}
   1.146 +	// fix .. from time to time there are spaces at the beginning of a material line
   1.147 +	while ( it != end && (*it == '\t' || *it == ' ') )
   1.148 +		++it;
   1.149 +	return it;
   1.150 +}
   1.151 +
   1.152 +/**	@brief	Get a name from the current line. Preserve space in the middle,
   1.153 + *    but trim it at the end.
   1.154 + *	@param	it		set to current position
   1.155 + *	@param	end		set to end of scratch buffer for readout
   1.156 + *	@param	name	Separated name
   1.157 + *	@return	Current-iterator with new position
   1.158 + */
   1.159 +template<class char_t>
   1.160 +inline char_t getName( char_t it, char_t end, std::string &name )
   1.161 +{
   1.162 +	name = "";
   1.163 +	it = getNextToken<char_t>( it, end );
   1.164 +	if ( isEndOfBuffer( it, end ) )
   1.165 +		return end;
   1.166 +	
   1.167 +	char *pStart = &( *it );
   1.168 +	while ( !isEndOfBuffer( it, end ) && !isNewLine( *it ) ) {
   1.169 +		++it;
   1.170 +	}
   1.171 +
   1.172 +	while(isEndOfBuffer( it, end ) || isNewLine( *it ) || isSeparator(*it)) {
   1.173 +		--it;
   1.174 +	}
   1.175 +	++it;
   1.176 +
   1.177 +	// Get name
   1.178 +	// if there is no name, and the previous char is a separator, come back to start
   1.179 +	while (&(*it) < pStart) {
   1.180 +		++it;
   1.181 +	}
   1.182 +	std::string strName( pStart, &(*it) );
   1.183 +	if ( strName.empty() )
   1.184 +		return it;
   1.185 +	else
   1.186 +		name = strName;
   1.187 +	
   1.188 +	return it;
   1.189 +}
   1.190 +
   1.191 +/**	@brief	Get next word from given line
   1.192 + *	@param	it		set to current position
   1.193 + *	@param	end		set to end of scratch buffer for readout
   1.194 + *	@param	pBuffer	Buffer for next word
   1.195 + *	@param	length	Buffer length
   1.196 + *	@return	Current-iterator with new position
   1.197 + */
   1.198 +template<class char_t>
   1.199 +inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length )
   1.200 +{
   1.201 +	size_t index = 0;
   1.202 +	it = getNextWord<char_t>( it, end );
   1.203 +	while ( !isSeparator( *it ) && !isEndOfBuffer( it, end ) )
   1.204 +	{
   1.205 +		pBuffer[index] = *it ;
   1.206 +		index++;
   1.207 +		if (index == length-1)
   1.208 +			break;
   1.209 +		++it;
   1.210 +	}
   1.211 +	pBuffer[ index ] = '\0';
   1.212 +	return it;
   1.213 +}
   1.214 +
   1.215 +/**	@brief	Get next float from given line
   1.216 + *	@param	it		set to current position
   1.217 + *	@param	end		set to end of scratch buffer for readout
   1.218 + *	@param	value	Separated float value.
   1.219 + *	@return	Current-iterator with new position
   1.220 + */
   1.221 +template<class char_t>
   1.222 +inline char_t getFloat( char_t it, char_t end, float &value )
   1.223 +{
   1.224 +	static const size_t BUFFERSIZE = 1024;
   1.225 +	char buffer[ BUFFERSIZE ];
   1.226 +	it = CopyNextWord<char_t>( it, end, buffer, BUFFERSIZE );
   1.227 +	value = (float) fast_atof( buffer );
   1.228 +
   1.229 +	return it;
   1.230 +}
   1.231 +
   1.232 +/**	@brief	Will perform a simple tokenize.
   1.233 + *	@param	str			String to tokenize.
   1.234 + *	@param	tokens		Array with tokens, will be empty if no token was found.
   1.235 + *	@param	delimiters	Delimiter for tokenize.
   1.236 + *	@return	Number of found token.
   1.237 + */
   1.238 +template<class string_type>
   1.239 +unsigned int tokenize( const string_type& str, std::vector<string_type>& tokens, 
   1.240 +						 const string_type& delimiters ) 
   1.241 +{
   1.242 +	// Skip delimiters at beginning.
   1.243 +	typename string_type::size_type lastPos = str.find_first_not_of( delimiters, 0 );
   1.244 +
   1.245 +	// Find first "non-delimiter".
   1.246 +	typename string_type::size_type pos = str.find_first_of( delimiters, lastPos );
   1.247 +	while ( string_type::npos != pos || string_type::npos != lastPos )
   1.248 +	{
   1.249 +		// Found a token, add it to the vector.
   1.250 +		string_type tmp = str.substr(lastPos, pos - lastPos);
   1.251 +		if ( !tmp.empty() && ' ' != tmp[ 0 ] )
   1.252 +			tokens.push_back( tmp );
   1.253 +
   1.254 +		// Skip delimiters.  Note the "not_of"
   1.255 +		lastPos = str.find_first_not_of( delimiters, pos );
   1.256 +
   1.257 +		// Find next "non-delimiter"
   1.258 +		pos = str.find_first_of( delimiters, lastPos );
   1.259 +	}
   1.260 +
   1.261 +	return static_cast<unsigned int>( tokens.size() );
   1.262 +}
   1.263 +
   1.264 +} // Namespace Assimp
   1.265 +
   1.266 +#endif