File indexing completed on 2024-05-19 04:49:58

0001 /****************************************************************************************
0002  * Copyright (c) 2008-2012 Soren Harward <stharward@gmail.com>                          *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #define DEBUG_PREFIX "Constraint::TrackSpreader"
0018 
0019 #include "TrackSpreader.h"
0020 
0021 #include "core/meta/Meta.h"
0022 #include "playlistgenerator/Constraint.h"
0023 
0024 #include <QHash>
0025 
0026 #include <cmath>
0027 #include <cstdlib>
0028 
0029 Constraint*
0030 ConstraintTypes::TrackSpreader::createNew( ConstraintNode* p )
0031 {
0032     if ( p )
0033         return new TrackSpreader( p );
0034     else
0035         return nullptr;
0036 }
0037 
0038 ConstraintFactoryEntry*
0039 ConstraintTypes::TrackSpreader::registerMe()
0040 {
0041     return nullptr;
0042 }
0043 
0044 ConstraintTypes::TrackSpreader::TrackSpreader( ConstraintNode* p ) : Constraint( p ) {
0045 }
0046 
0047 QWidget*
0048 ConstraintTypes::TrackSpreader::editWidget() const
0049 {
0050     return nullptr;
0051 }
0052 
0053 void
0054 ConstraintTypes::TrackSpreader::toXml( QDomDocument&, QDomElement& ) const {}
0055 
0056 double
0057 ConstraintTypes::TrackSpreader::satisfaction( const Meta::TrackList& tl ) const
0058 {
0059     QHash<Meta::TrackPtr, int> locations;
0060     double dist = 0.0;
0061     for ( int i = 0; i < tl.size(); i++ ) {
0062         Meta::TrackPtr t = tl.value( i );
0063         if ( locations.contains( t ) ) {
0064             foreach( int j, locations.values( t ) ) {
0065                 dist += distance( i, j );
0066             }
0067         }
0068         locations.insertMulti( tl.value( i ), i );
0069     }
0070 
0071     return 1.0 / exp( 0.1 * dist );
0072 }
0073 
0074 double
0075 ConstraintTypes::TrackSpreader::distance( const int a, const int b ) const
0076 {
0077     if ( a == b ) {
0078         return 0.0;
0079     }
0080 
0081     int d = qAbs( a - b ) - 1;
0082     return exp( -0.05 * ( double )d );
0083 }