Blender V2.61 - r43446

AUD_AnimateableProperty.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * Copyright 2009-2011 Jörg Hermann Müller
00005  *
00006  * This file is part of AudaSpace.
00007  *
00008  * Audaspace is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * AudaSpace is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Audaspace; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 #include "AUD_AnimateableProperty.h"
00031 
00032 #include <cstring>
00033 #include <cmath>
00034 
00035 AUD_AnimateableProperty::AUD_AnimateableProperty(int count) :
00036     AUD_Buffer(count * sizeof(float)), m_count(count), m_isAnimated(false)
00037 {
00038     memset(getBuffer(), 0, count * sizeof(float));
00039 
00040     pthread_mutexattr_t attr;
00041     pthread_mutexattr_init(&attr);
00042     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
00043 
00044     pthread_mutex_init(&m_mutex, &attr);
00045 
00046     pthread_mutexattr_destroy(&attr);
00047 }
00048 
00049 AUD_AnimateableProperty::~AUD_AnimateableProperty()
00050 {
00051     pthread_mutex_destroy(&m_mutex);
00052 }
00053 
00054 void AUD_AnimateableProperty::lock()
00055 {
00056     pthread_mutex_lock(&m_mutex);
00057 }
00058 
00059 void AUD_AnimateableProperty::unlock()
00060 {
00061     pthread_mutex_unlock(&m_mutex);
00062 }
00063 
00064 void AUD_AnimateableProperty::write(const float* data)
00065 {
00066     lock();
00067 
00068     m_isAnimated = false;
00069     memcpy(getBuffer(), data, m_count * sizeof(float));
00070 
00071     unlock();
00072 }
00073 
00074 void AUD_AnimateableProperty::write(const float* data, int position, int count)
00075 {
00076     lock();
00077 
00078     m_isAnimated = true;
00079 
00080     int pos = getSize() / (sizeof(float) * m_count);
00081 
00082     assureSize((count + position) * m_count * sizeof(float), true);
00083 
00084     float* buf = getBuffer();
00085 
00086     memcpy(buf + position * m_count, data, count * m_count * sizeof(float));
00087 
00088     for(int i = pos; i < position; i++)
00089         memcpy(buf + i * m_count, buf + (pos - 1) * m_count, m_count * sizeof(float));
00090 
00091     unlock();
00092 }
00093 
00094 void AUD_AnimateableProperty::read(float position, float* out)
00095 {
00096     lock();
00097 
00098     if(!m_isAnimated)
00099     {
00100         memcpy(out, getBuffer(), m_count * sizeof(float));
00101         unlock();
00102         return;
00103     }
00104 
00105     int last = getSize() / (sizeof(float) * m_count) - 1;
00106     float t = position - floor(position);
00107 
00108     if(position >= last)
00109     {
00110         position = last;
00111         t = 0;
00112     }
00113 
00114     if(t == 0)
00115     {
00116         memcpy(out, getBuffer() + int(floor(position)) * m_count, m_count * sizeof(float));
00117     }
00118     else
00119     {
00120         int pos = int(floor(position)) * m_count;
00121         float t2 = t * t;
00122         float t3 = t2 * t;
00123         float m0, m1;
00124         float* p0;
00125         float* p1 = getBuffer() + pos;
00126         float* p2;
00127         float* p3;
00128         last *= m_count;
00129 
00130         if(pos == 0)
00131             p0 = p1;
00132         else
00133             p0 = p1 - m_count;
00134 
00135         p2 = p1 + m_count;
00136         if(pos + m_count == last)
00137             p3 = p2;
00138         else
00139             p3 = p2 + m_count;
00140 
00141         for(int i = 0; i < m_count; i++)
00142         {
00143             m0 = (p2[i] - p0[i]) / 2.0f;
00144             m1 = (p3[i] - p1[i]) / 2.0f;
00145 
00146             out[i] = (2 * t3 - 3 * t2 + 1) * p0[i] + (-2 * t3 + 3 * t2) * p1[i] +
00147                      (t3 - 2 * t2 + t) * m0 + (t3 - t2) * m1;
00148         }
00149     }
00150 
00151     unlock();
00152 }
00153 
00154 bool AUD_AnimateableProperty::isAnimated() const
00155 {
00156     return m_isAnimated;
00157 }