Changeset 7862:0d488ade72fc


Ignore:
Timestamp:
12/18/07 22:12:27 (5 years ago)
Author:
Robert Bradshaw <robertwb@…>
Branch:
default
Message:

3d plotting for graphs using plot3d (rather than tachyon directly)

Location:
sage/graphs
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sage/graphs/graph.py

    r7860 r7862  
    28042804                  color_by_label=color_by_label, 
    28052805                  heights=heights).show(**kwds) 
     2806 
     2807 
     2808    def plot3d_new(self, bgcolor=(1,1,1), 
     2809                         vertex_colors=None, vertex_size=0.06,  
     2810                         edge_colors=None, edge_size=0.02, 
     2811                         pos3d=None, 
     2812                         iterations=50, color_by_label=False, **kwds): 
     2813        from sage.plot.plot3d.all import Sphere, Line, Arrow 
     2814        line = Arrow if self.is_directed() else Line 
     2815         
     2816        verts = self.vertices() 
     2817 
     2818        if vertex_colors is None: 
     2819            vertex_colors = { (1,0,0) : verts } 
     2820        if pos3d is None: 
     2821            pos3d = graph_fast.spring_layout_fast(self, dim=3, iterations=iterations) 
     2822 
     2823        if color_by_label: 
     2824            if edge_colors is  None: 
     2825                    # do the coloring 
     2826                    edge_colors = self._color_by_label(format='rgbtuple') 
     2827        elif edge_colors is None: 
     2828            edge_colors = { (0,0,0) : self.edges() } 
     2829 
     2830        try: 
     2831            graphic = 0 
     2832            for color in vertex_colors: 
     2833                for v in vertex_colors[color]: 
     2834                    graphic += Sphere(vertex_size, color=color).translate(*pos3d[v]) 
     2835 
     2836            for color in edge_colors: 
     2837                for u, v, l in edge_colors[color]: 
     2838                    graphic += line(pos3d[u], pos3d[v], edge_size, color=color, closed=False) 
     2839 
     2840            return graphic 
     2841 
     2842        except KeyError: 
     2843            raise KeyError, "Oops! You haven't specified positions for all the vertices." 
     2844         
     2845 
    28062846 
    28072847    def transitive_closure(self): 
     
    48124852        f.close() 
    48134853         
    4814     def plot3d_new(self, bgcolor=(1,1,1), 
    4815                          vertex_colors=None, vertex_size=0.06,  
    4816                          edge_colors=None, edge_size=0.02, 
    4817                          pos3d=None, 
    4818                          iterations=50, color_by_label=False, **kwds): 
    4819         from math import sqrt 
    4820         from sage.plot.plot3d.all import Sphere, Line 
    4821          
    4822         c = [0,0,0] 
    4823         r = [] 
    4824  
    4825         verts = self.vertices() 
    4826  
    4827         if vertex_colors is None: 
    4828             vertex_colors = { (1,0,0) : verts } 
    4829         if pos3d is None: 
    4830             pos3d = graph_fast.spring_layout_fast(self, dim=3, iterations=iterations) 
    4831         try: 
    4832             for v in verts: 
    4833                 c[0] += pos3d[v][0] 
    4834                 c[1] += pos3d[v][1] 
    4835                 c[2] += pos3d[v][2] 
    4836         except KeyError: 
    4837             raise KeyError, "Oops! You haven't specified positions for all the vertices." 
    4838  
    4839         order = self.order() 
    4840         c[0] = c[0]/order 
    4841         c[1] = c[1]/order 
    4842         c[2] = c[2]/order 
    4843         for v in verts: 
    4844             pos3d[v][0] = pos3d[v][0] - c[0] 
    4845             pos3d[v][1] = pos3d[v][1] - c[1] 
    4846             pos3d[v][2] = pos3d[v][2] - c[2] 
    4847             r.append(abs(sqrt((pos3d[v][0])**2 + (pos3d[v][1])**2 + (pos3d[v][2])**2))) 
    4848         r = max(r) 
    4849         if r == 0: 
    4850             r = 1 
    4851         for v in verts: 
    4852             pos3d[v][0] = pos3d[v][0]/r 
    4853             pos3d[v][1] = pos3d[v][1]/r 
    4854             pos3d[v][2] = pos3d[v][2]/r 
    4855  
    4856         graphic = 0 
    4857         for color in vertex_colors: 
    4858             for v in vertex_colors[color]: 
    4859                 graphic += Sphere(vertex_size, color=color).translate(*pos3d[v]) 
    4860  
    4861         if color_by_label: 
    4862             if edge_colors is  None: 
    4863                     # do the coloring 
    4864                     edge_colors = self._color_by_label(format='rgbtuple') 
    4865         elif edge_colors is None: 
    4866             edge_colors = { (0,0,0) : self.edges() } 
    4867  
    4868         for color in edge_colors: 
    4869             for u, v, l in edge_colors[color]: 
    4870                 graphic += Line(pos3d[u], pos3d[v], edge_size, color=color) 
    4871  
    4872         return graphic 
    48734854         
    48744855    def plot3d(self, bgcolor=(1,1,1), 
  • sage/graphs/graph_fast.pyx

    r7161 r7862  
    6060    return pos 
    6161 
    62 def spring_layout_fast(G, iterations=50, dim=2, vpos=None): 
     62def spring_layout_fast(G, iterations=50, int dim=2, vpos=None, bint rescale=True): 
    6363    """ 
    6464    Spring force model layout 
     
    117117     
    118118    run_spring(iterations, dim, pos, elist, n) 
     119     
     120    # recenter 
     121    cdef double* cen  
     122    cdef double r, r2, max_r2 = 0 
     123    if rescale: 
     124        cen = <double *>sage_malloc(sizeof(double) * dim) 
     125        for x from 0 <= x < dim: cen[x] = 0 
     126        for i from 0 <= i < n: 
     127            for x from 0 <= x < dim: 
     128                cen[x] += pos[i*dim + x] 
     129        for x from 0 <= x < dim: cen[x] /= n 
     130        for i from 0 <= i < n: 
     131            r2 = 0 
     132            for x from 0 <= x < dim: 
     133                pos[i*dim + x] -= cen[x] 
     134                r2 += pos[i*dim + x] * pos[i*dim + x] 
     135            if r2 > max_r2: 
     136                max_r2 = r2 
     137        r = 1 if max_r2 == 0 else sqrt(max_r2) 
     138        for i from 0 <= i < n: 
     139            for x from 0 <= x < dim: 
     140                pos[i*dim + x] /= r 
     141        sage_free(cen) 
    119142     
    120143    # put the data back into a position dictionary 
Note: See TracChangeset for help on using the changeset viewer.