001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.plugins.graphgenerator.generators;
006    
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.VertexModel;
010    import graphlab.platform.lang.CommandAttitude;
011    import graphlab.platform.parameter.Parameter;
012    import graphlab.platform.parameter.Parametrizable;
013    import graphlab.platform.StaticUtils;
014    import graphlab.plugins.graphgenerator.GraphGenerator;
015    import graphlab.plugins.graphgenerator.core.PositionGenerators;
016    import graphlab.plugins.graphgenerator.core.SimpleGeneratorInterface;
017    import graphlab.plugins.graphgenerator.core.extension.GraphGeneratorExtension;
018    
019    import java.awt.*;
020    
021    /**
022     * Author: Mohsen Khaki
023     * 
024     */
025    @CommandAttitude(name = "generate_helmn", abbreviation = "_g_helmn", description = "generates a Helm graph of order n")
026    public class HelmGraph implements GraphGeneratorExtension, Parametrizable, SimpleGeneratorInterface {
027    
028            @Parameter(name = "n")
029            public static int n = 3;
030    
031            GraphModel g;
032    
033            public void setWorkingGraph(GraphModel g)
034            {
035                    this.g = g;
036            }
037    
038            public String getName()
039            {
040                    return "Helm Graph";
041            }
042    
043            public String getDescription()
044            {
045                    return "Generate Helm Graph";
046            }
047    
048            VertexModel[] v;
049    
050            public VertexModel[] getVertices()
051            {
052                    VertexModel[] result = new VertexModel[2*n+1];
053                    for (int i = 0; i < 2*n+1; i++)
054                            result[i] = new VertexModel();
055                    v = result;
056                    return result;
057            }
058    
059            public EdgeModel[] getEdges()
060            {
061                    EdgeModel[] result = new EdgeModel[3*n];
062                    for (int i = 0; i < n; i++)
063                    {
064                            result[i] = new EdgeModel(v[i], v[n+i]);
065                            result[n+i] = new EdgeModel(v[n+i], v[2*n]);
066                            result[2*n+i] = new EdgeModel(v[n+i],v[n+((i+1)%n)]);
067                    }
068                    return result;
069            }
070    
071            public Point[] getVertexPositions()
072            {
073                    int w = 1000;
074                    double mw = ((double)w)/2.0, qw = ((double)w)/4.0;
075                    Point result[] = new Point[2*n+1];
076                    result[2*n] = new Point((int)(w/2), (int)(w/2));
077                    double r1 = mw;
078                    double r2 = qw;
079                    double ang = Math.PI*2.0/n;
080                    double offset = 0.0;
081                    if ((n % 2) == 0)
082                            offset = ang/2.0; 
083                    for ( int i = 0 ; i < n ; i++ )
084                    {
085                            double angle = offset + i * ang;
086                            result[i] = new Point((int)(mw + Math.sin(angle)*r1), (int)(mw - Math.cos(angle)*r1));
087                            result[n+i] = new Point((int)(mw + Math.sin(angle)*r2), (int)(mw - Math.cos(angle)*r2));
088                    }
089                    return result;
090            }
091    
092            public String checkParameters()
093            {
094                    return null;
095            }
096    
097            public GraphModel generateGraph()
098            {
099                    return GraphGenerator.getGraph(false, this);
100            }
101    
102            /**
103             * generates a Helm Graph with given parameters
104             */
105            public static GraphModel generateHelm(int n)
106            {
107                    HelmGraph.n = n;
108                    return GraphGenerator.getGraph(false, new HelmGraph());
109        }
110    
111    public static void main(String[] args) {
112            graphlab.platform.Application.main(args);
113    //        StaticUtils.loadSingleExtension(graphlab.samples.extensions.HelmGraph.class);
114        }
115    }