001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.ui.layout.springbox;
033
034import java.util.Iterator;
035
036import org.miv.pherd.Particle;
037import org.miv.pherd.ntree.BarycenterCellData;
038import org.miv.pherd.ntree.Cell;
039import org.miv.pherd.ntree.CellData;
040import org.miv.pherd.ntree.NTreeListener;
041
042/**
043 * A N-Tree cell data that both compute the barycenter of each cell (aggregate position),
044 * the aggregate weight of each cell (sum of all of the cell node weights) and the
045 * aggregate degree of each cell (sum of all of the cell node degree).
046 */
047public class GraphCellData extends BarycenterCellData {
048        /** 
049         * Aggregate degree. The sum of the degrees of each node aggregated
050         * in this barycenter.
051         */
052        public double degree;
053
054        /** 
055         * Aggregate degree. The sum of the degrees of each node aggregated
056         * in this barycenter.
057         */
058        public double getDegree() {
059                return degree;
060        }
061        
062        @Override
063        public CellData newCellData() {
064                return new GraphCellData();
065        }
066        
067        @Override
068        public void recompute() {
069                double x = 0;
070                double y = 0;
071                double z = 0;
072                double n = 0;
073                
074                weight = 0;
075                degree = 0;
076                
077                if( cell.isLeaf() ) {
078                        Iterator<? extends Particle> particles = cell.getParticles();
079                        
080                        while(particles.hasNext()) {
081                                NodeParticle particle = (NodeParticle)particles.next();
082                                
083                                x += particle.getPosition().x; 
084                                y += particle.getPosition().y; 
085                                z += particle.getPosition().z; 
086                                
087                                weight += particle.getWeight();
088                                degree += particle.getEdges().size(); 
089                                
090                                n++;
091                        }
092                        
093                        if(n > 0) {
094                                x /= n;
095                                y /= n;
096                                z /= n;
097                        }
098                        
099                        center.set( x, y, z );
100                } else {
101                        double subcnt = cell.getSpace().getDivisions();
102                        double totpop = cell.getPopulation();
103                        int   verif  = 0;
104                        
105                        if( totpop > 0 )
106                        {
107                                for( int i=0; i<subcnt; ++i )
108                                {
109                                        Cell          subcell = cell.getSub( i );
110                                        GraphCellData data    = (GraphCellData) subcell.getData();
111                                        double        pop     = subcell.getPopulation();
112                
113                                        verif += pop;
114                                        
115                                        x += data.center.x * pop; 
116                                        y += data.center.y * pop;
117                                        z += data.center.z * pop;
118                                        
119                                        weight += data.weight;
120                                        degree += data.degree;
121                                }
122                                
123                                assert verif == totpop : "Discrepancy in population counts ?";
124                                
125                                x /= totpop;
126                                y /= totpop;
127                                z /= totpop;
128                        }
129
130                        center.set( x, y, z );
131                }
132                
133                for( NTreeListener listener: cell.getTree().getListeners() ) {
134                        listener.cellData( cell.getId(), "barycenter", this );
135                }
136        }
137}