// NeL - MMORPG Framework // Copyright (C) 2010 Winch Gate Property Limited // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . #include "std3d.h" #include "nel/misc/quat.h" #include "nel/misc/common.h" #include "nel/3d/track_sampled_quat.h" #include "nel/3d/track_sampled_quat_small_header.h" using namespace NLMISC; using namespace std; namespace NL3D { // *************************************************************************** // *************************************************************************** // Quaternion compression // *************************************************************************** // *************************************************************************** const double NL3D_OO32767= 1.0f/32767; const double NL3D_OO65535= 1.0f/65535; #ifdef NL3D_TSQ_ALLOW_QUAT_COMPRESS // *************************************************************************** void CQuatPack::pack(const CQuat &quat) { /* This is the most precise/faster compression we can have. Some other tries have been made. - deducing w from x,y,z is possible with w= 1-sqrt(x^2+y^2+z^2) (with tradeoff of the W sign) but very not precise. - Transform the quaternion to an AxisAngle is possible, but slower (some cos/sin or LUT). Axis is encoded with sint16, and angle is encoded with uint16. - The same than above, but encode the axis as X/Y only, and deduce Z from them, is possible but precision problems arise. You can see that the operation "deduce a 3/4 member from unit lenght rule" is definetly not precise. Hence this simpler but workable way. */ // normalize the quaterion. CQuatD nquat= quat; nquat.normalize(); sint ax= (sint)floor(nquat.x * 32767 + 0.5); sint ay= (sint)floor(nquat.y * 32767 + 0.5); sint az= (sint)floor(nquat.z * 32767 + 0.5); sint aw= (sint)floor(nquat.w * 32767 + 0.5); clamp(ax, -32767, 32767); clamp(ay, -32767, 32767); clamp(az, -32767, 32767); clamp(aw, -32767, 32767); x= ax; y= ay; z= az; w= aw; } // *************************************************************************** void CQuatPack::unpack(CQuat &quat) { // unpack x/y/z. CQuatD quatD; quatD.x= x * NL3D_OO32767; quatD.y= y * NL3D_OO32767; quatD.z= z * NL3D_OO32767; quatD.w= w * NL3D_OO32767; quatD.normalize(); quat= quatD; } #endif // *************************************************************************** // *************************************************************************** // CTrackSampledQuat // *************************************************************************** // *************************************************************************** // *************************************************************************** CTrackSampledQuat::CTrackSampledQuat() { } // *************************************************************************** CTrackSampledQuat::~CTrackSampledQuat() { } // *************************************************************************** void CTrackSampledQuat::serial(NLMISC::IStream &f) { /* Version 1: - split class with base CTrackSampledCommon (must add a version in it). Version 0: - base version. */ sint ver= f.serialVersion(1); if( ver<=0 ) { // serial Time infos, directly in CTrackSampledCommon f.serial(_LoopMode); f.serial(_BeginTime); f.serial(_EndTime) ; f.serial(_TotalRange); f.serial(_OOTotalRange); f.serial(_DeltaTime); f.serial(_OODeltaTime); f.serial(_TimeBlocks); } else { // serial Time infos. CTrackSampledCommon::serialCommon(f); } // serial Keys. f.serial(_Keys); } // *************************************************************************** void CTrackSampledQuat::build(const std::vector &timeList, const std::vector &keyList, float beginTime, float endTime) { nlassert( endTime>beginTime || (beginTime==endTime && keyList.size()<=1) ); nlassert( keyList.size()==timeList.size() ); uint i; // reset. uint numKeys= (uint)keyList.size(); _Keys.clear(); _TimeBlocks.clear(); // Build Common time information CTrackSampledCommon::buildCommon(timeList, beginTime, endTime); // Compute All Key values. //=================== _Keys.resize(numKeys); for(i=0; i keepKeys; applySampleDivisorCommon(sampleDivisor, keepKeys); // **** rebuild the keys NLMISC::CObjectVector newKeys; newKeys.resize((uint32)keepKeys.size()); for(uint i=0;i1) return false; // Support only 255 keys and not 256!!! cause _NumKeys is encoded in 8 bits! if(_Keys.size()>=256) return false; // if the number of keys ovveride the uint16 limit, abort if(_Keys.size()+quatCounter.NumKeys > 65536) return false; // Search if the Track header is the same as one of the quatCounter. // NB: O(N*N) but quatCounter.TrackHeaders should be very small uint headerIndex; for(headerIndex=0;headerIndex1) return NULL; // Support only 255 keys and not 256!!! cause _NumKeys is encoded in 8 bits! if(_Keys.size()>=256) return NULL; // if the number of keys ovveride the uint16 limit, abort if(_Keys.size()+globalKeyOffset > 65536) return NULL; // Search if the Track header is the same as one of the quatPacker. // NB: O(N*N) but quatPacker.TrackHeaders should be very small uint headerIndex; for(headerIndex=0;headerIndex