1 /****************************************************************************************
2  * Copyright (c) 2008-2012 Soren Harward <stharward@gmail.com>                          *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #define DEBUG_PREFIX "Constraint::TrackSpreader"
18 
19 #include "TrackSpreader.h"
20 
21 #include "core/meta/Meta.h"
22 #include "playlistgenerator/Constraint.h"
23 
24 #include <QHash>
25 
26 #include <math.h>
27 #include <stdlib.h>
28 
29 Constraint*
createNew(ConstraintNode * p)30 ConstraintTypes::TrackSpreader::createNew( ConstraintNode* p )
31 {
32     if ( p )
33         return new TrackSpreader( p );
34     else
35         return 0;
36 }
37 
38 ConstraintFactoryEntry*
registerMe()39 ConstraintTypes::TrackSpreader::registerMe()
40 {
41     return 0;
42 }
43 
TrackSpreader(ConstraintNode * p)44 ConstraintTypes::TrackSpreader::TrackSpreader( ConstraintNode* p ) : Constraint( p ) {
45 }
46 
47 QWidget*
editWidget() const48 ConstraintTypes::TrackSpreader::editWidget() const
49 {
50     return 0;
51 }
52 
53 void
toXml(QDomDocument &,QDomElement &) const54 ConstraintTypes::TrackSpreader::toXml( QDomDocument&, QDomElement& ) const {}
55 
56 double
satisfaction(const Meta::TrackList & tl) const57 ConstraintTypes::TrackSpreader::satisfaction( const Meta::TrackList& tl ) const
58 {
59     QHash<Meta::TrackPtr, int> locations;
60     double dist = 0.0;
61     for ( int i = 0; i < tl.size(); i++ ) {
62         Meta::TrackPtr t = tl.value( i );
63         if ( locations.contains( t ) ) {
64             foreach( int j, locations.values( t ) ) {
65                 dist += distance( i, j );
66             }
67         }
68         locations.insertMulti( tl.value( i ), i );
69     }
70 
71     return 1.0 / exp( 0.1 * dist );
72 }
73 
74 double
distance(const int a,const int b) const75 ConstraintTypes::TrackSpreader::distance( const int a, const int b ) const
76 {
77     if ( a == b ) {
78         return 0.0;
79     }
80 
81     int d = qAbs( a - b ) - 1;
82     return exp( -0.05 * ( double )d );
83 }
84