~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to src/libcola/cola.cpp

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#include "conjugate_gradient.h"
3
3
#include "straightener.h"
4
4
#include "shortest_paths.h"
5
 
#include "2geom/isnan.h"
 
5
#include <2geom/math-utils.h>
 
6
 
 
7
using namespace std;
6
8
 
7
9
namespace cola {
8
10
 
16
18
}
17
19
ConstrainedMajorizationLayout
18
20
::ConstrainedMajorizationLayout(
19
 
        vector<Rectangle*>& rs,
20
 
        vector<Edge>& es,
 
21
        std::vector<Rectangle*>& rs,
 
22
        std::vector<Edge>& es,
21
23
        double* eweights,
22
24
        double idealLength,
23
25
        TestConvergence& done)
85
87
                for(Cluster::iterator vit=c->begin();
86
88
                        vit!=c->end(); ++vit) {
87
89
                    double pos = coords[k][*vit];
88
 
                    minPos=min(pos,minPos);
89
 
                    maxPos=max(pos,maxPos);
90
 
                    p->leftof.push_back(make_pair(*vit,0)); 
91
 
                    p->rightof.push_back(make_pair(*vit,0)); 
 
90
                    minPos = std::min(pos, minPos);
 
91
                    maxPos = std::max(pos, maxPos);
 
92
                    p->leftof.push_back(std::make_pair(*vit,0)); 
 
93
                    p->rightof.push_back(std::make_pair(*vit,0)); 
92
94
                }
93
95
                p->place_l = minPos;
94
96
                p->place_r = maxPos;
108
110
        double** Dij, GradientProjection* gp, double* coords) 
109
111
{
110
112
    double b[n];
111
 
    fill(b,b+n,0);
 
113
    std::fill(b,b+n,0);
112
114
    majlayout(Dij,gp,coords,b);
113
115
}
114
116
void ConstrainedMajorizationLayout::majlayout(
123
125
            for (unsigned j = 0; j < lapSize; j++) {
124
126
                if (j == i) continue;
125
127
                dist_ij = euclidean_distance(i, j);
126
 
                if (dist_ij > 1e-30 && Dij[i][j] > 1e-30) {     /* skip zero distances */
 
128
                if (dist_ij > 1e-30 && Dij[i][j] > 1e-30) {     /* skip zero distances */
127
129
                    /* calculate L_ij := w_{ij}*d_{ij}/dist_{ij} */
128
130
                    L_ij = 1.0 / (dist_ij * Dij[i][j]);
129
131
                    degree -= L_ij;
216
218
    return true;
217
219
}
218
220
static bool straightenToProjection=true;
219
 
void ConstrainedMajorizationLayout::straighten(vector<straightener::Edge*>& sedges, Dim dim) {
220
 
        vector<straightener::Node*> snodes;
221
 
        for (unsigned i=0;i<lapSize;i++) {
222
 
                snodes.push_back(new straightener::Node(i,boundingBoxes[i]));
223
 
        }
 
221
void ConstrainedMajorizationLayout::straighten(std::vector<straightener::Edge*>& sedges, Dim dim) {
 
222
    std::vector<straightener::Node*> snodes;
 
223
    for (unsigned i=0;i<lapSize;i++) {
 
224
        snodes.push_back(new straightener::Node(i,boundingBoxes[i]));
 
225
    }
224
226
    SimpleConstraints cs;
225
227
    straightener::generateConstraints(snodes,sedges,cs,dim);
226
228
    n=snodes.size();
244
246
    LinearConstraints linearConstraints;
245
247
    for(unsigned i=0;i<sedges.size();i++) {
246
248
        sedges[i]->nodePath(snodes);
247
 
        vector<unsigned>& path=sedges[i]->path;
 
249
        std::vector<unsigned>& path=sedges[i]->path;
248
250
        // take u and v as the ends of the line
249
251
        //unsigned u=path[0];
250
252
        //unsigned v=path[path.size()-1];
267
269
    //cout << "Generated "<<linearConstraints.size()<< " linear constraints"<<endl;
268
270
    assert(snodes.size()==lapSize+linearConstraints.size());
269
271
    double b[n],*coords=dim==HORIZONTAL?X:Y,dist_ub,dist_bv;
270
 
    fill(b,b+n,0);
 
272
    std::fill(b,b+n,0);
271
273
    for(LinearConstraints::iterator i=linearConstraints.begin();
272
274
           i!= linearConstraints.end();i++) {
273
275
        LinearConstraint* c=*i;
286
288
            double wbv=edge_length*c->frac_bv;
287
289
            dist_ub=euclidean_distance(c->u,c->b)*wub;
288
290
            dist_bv=euclidean_distance(c->b,c->v)*wbv;
289
 
            wub=max(wub,0.00001);
290
 
            wbv=max(wbv,0.00001);
291
 
            dist_ub=max(dist_ub,0.00001);
292
 
            dist_bv=max(dist_bv,0.00001);
 
291
            wub = std::max(wub,0.00001);
 
292
            wbv = std::max(wbv,0.00001);
 
293
            dist_ub = std::max(dist_ub,0.00001);
 
294
            dist_bv = std::max(dist_bv,0.00001);
293
295
            wub=1/(wub*wub);
294
296
            wbv=1/(wbv*wbv);
295
297
            Q[c->u][c->u]-=wub;
306
308
                   - coords[c->b] / dist_ub - coords[c->b] / dist_bv;
307
309
        }
308
310
    }
309
 
        GradientProjection gp(dim,n,Q,coords,tol,100,
310
 
            (AlignmentConstraints*)NULL,false,(vpsc::Rectangle**)NULL,(PageBoundaryConstraints*)NULL,&cs);
 
311
    GradientProjection gp(dim,n,Q,coords,tol,100,
 
312
                          (AlignmentConstraints*)NULL,false,(vpsc::Rectangle**)NULL,(PageBoundaryConstraints*)NULL,&cs);
311
313
    constrainedLayout = true;
312
314
    majlayout(Dij,&gp,coords,b);
313
315
    for(unsigned i=0;i<sedges.size();i++) {
337
339
        PageBoundaryConstraints* pbcx, PageBoundaryConstraints* pbcy,
338
340
        SimpleConstraints* scx, SimpleConstraints* scy,
339
341
        Clusters* cs,
340
 
        vector<straightener::Edge*>* straightenEdges) {
 
342
        std::vector<straightener::Edge*>* straightenEdges) {
341
343
    constrainedLayout = true;
342
344
    this->avoidOverlaps = avoidOverlaps;
343
345
    if(cs) {
344
346
        clusters=cs;
345
347
    }
346
 
        gpX=new GradientProjection(
347
 
            HORIZONTAL,n,Q,X,tol,100,acsx,avoidOverlaps,boundingBoxes,pbcx,scx);
348
 
        gpY=new GradientProjection(
349
 
            VERTICAL,n,Q,Y,tol,100,acsy,avoidOverlaps,boundingBoxes,pbcy,scy);
 
348
    gpX = new GradientProjection(HORIZONTAL,n,Q,X,tol,100,acsx,avoidOverlaps,boundingBoxes,pbcx,scx);
 
349
    gpY = new GradientProjection(VERTICAL,n,Q,Y,tol,100,acsy,avoidOverlaps,boundingBoxes,pbcy,scy);
350
350
    this->straightenEdges = straightenEdges;
351
351
}
352
352
} // namespace cola