~ubuntu-branches/ubuntu/wily/ctioga2/wily

« back to all changes in this revision

Viewing changes to .pc/fix-ugly-coding.diff/lib/ctioga2/graphics/elements/parametric2d.rb

  • Committer: Package Import Robot
  • Author(s): Vincent Fourmond
  • Date: 2013-07-08 20:58:17 UTC
  • mfrom: (6.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130708205817-cephnc6etndyxrrp
Tags: 0.4-2
* Upload to unstable
* Already conforms to newer standards

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: undecided -*-
2
 
# parametric2d.rb: a 2D curve whose parameters depend on Z values
3
 
# copyright (c) 2006, 2007, 2008, 2009, 2010 by Vincent Fourmond
4
 
 
5
 
# This program is free software; you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License as published by
7
 
# the Free Software Foundation; either version 2 of the License, or
8
 
# (at your option) any later version.
9
 
 
10
 
# This program is distributed in the hope that it will be useful, but
11
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 
# General Public License for more details (in the COPYING file).
14
 
 
15
 
require 'ctioga2/utils'
16
 
require 'ctioga2/log'
17
 
 
18
 
require 'Dobjects/Function'
19
 
 
20
 
 
21
 
module CTioga2
22
 
 
23
 
  Version::register_svn_info('$Revision: 284 $', '$Date: 2011-02-13 17:23:43 +0100 (Sun, 13 Feb 2011) $')
24
 
 
25
 
  module Graphics
26
 
 
27
 
    module Elements
28
 
 
29
 
      # This class represents a 3D (or more, to be seen later) dataset
30
 
      # as markers with various parameters parametrized (color,
31
 
      # transparency, marker scale, marker type (discrete), possibly
32
 
      # stroke and fill colors ?
33
 
      #
34
 
      # @todo Find a mechanism to really say what varies. Ideally, one
35
 
      # would want to say:
36
 
      # * Y2 is marker color
37
 
      # * Y3 is marker size
38
 
      # * Y4 only takes discrete values and represents markers
39
 
      # 
40
 
      # However, this is complex enough to be left out of the curve
41
 
      # factory, I think. Color maps can be used for colors, but for
42
 
      # the rest, things will have to be implemented as parameters to
43
 
      # the curve generator, or even separated commands.
44
 
      class Parametric2D  < TiogaElement
45
 
 
46
 
        include Log
47
 
        include Dobjects
48
 
 
49
 
        # The Data::Dataset object that should get plotted.
50
 
        attr_accessor :dataset
51
 
 
52
 
        # A Styles::CurveStyle object saying how the curve should be
53
 
        # drawn.
54
 
        #
55
 
        # Some of the elements will be overridden.
56
 
        attr_accessor :curve_style
57
 
 
58
 
        # For convenience only: xy functions
59
 
        attr_accessor :function
60
 
 
61
 
        # A hash Z value -> corresponding XY functions.
62
 
        attr_accessor :planes
63
 
 
64
 
 
65
 
 
66
 
        undef :location=, :location
67
 
        
68
 
        # Creates a new Curve2D object with the given _dataset_ and
69
 
        # _style_.
70
 
        def initialize(dataset, style = nil)
71
 
          @dataset = dataset
72
 
          @curve_style = style
73
 
          prepare_data
74
 
        end
75
 
 
76
 
        # Prepares the internal storage of the data, from the @dataset
77
 
        def prepare_data
78
 
          @function = Function.new(@dataset.x.values.dup, 
79
 
                                   @dataset.y.values.dup)
80
 
 
81
 
          ## @todo this should eventually use Dataset::index_on_cols.
82
 
          @planes = {}
83
 
          @dataset.each_values do |i, x,y,*zs|
84
 
            @planes[zs[0]] ||= Function.new(Dvector.new, Dvector.new)
85
 
            @planes[zs[0]].x << x
86
 
            @planes[zs[0]].y << y
87
 
          end
88
 
        end
89
 
        
90
 
        protected :prepare_data
91
 
 
92
 
        # Returns the LocationStyle object of the curve. Returns the
93
 
        # one from #curve_style.
94
 
        def location
95
 
          return @curve_style.location
96
 
        end
97
 
 
98
 
        # Returns the Types::Boundaries of this curve.
99
 
        def get_boundaries
100
 
          return Types::Boundaries.bounds(@function.x, @function.y)
101
 
        end
102
 
 
103
 
        # Draws the markers, if applicable.
104
 
        def draw_path(t)
105
 
          min = @dataset.z.values.min
106
 
          max = @dataset.z.values.max
107
 
          if @curve_style.has_line?
108
 
            # We use a default color map for the lines
109
 
            @curve_style.color_map ||= 
110
 
              Styles::ColorMap.from_text("Red--Green")
111
 
            cmap = @curve_style.color_map
112
 
 
113
 
            for zs in @planes.keys.sort ## \todo have the sort
114
 
                                        ## direction configurable.
115
 
              f = @planes[zs]
116
 
              color = cmap.z_color(zs, min, max)
117
 
              t.context do 
118
 
                @curve_style.line.set_stroke_style(t)
119
 
                t.stroke_color = color
120
 
                t.show_polyline(f.x, f.y)
121
 
              end
122
 
            end
123
 
          end
124
 
        end
125
 
 
126
 
 
127
 
        # Draws the markers, if applicable.
128
 
        def draw_markers(t)
129
 
          min = @dataset.z.values.min
130
 
          max = @dataset.z.values.max
131
 
          if @curve_style.has_marker?
132
 
            # We use a default color map for the markers
133
 
            @curve_style.marker_color_map ||= 
134
 
              Styles::ColorMap.from_text("Red--Green")
135
 
            cmap = @curve_style.marker_color_map
136
 
            for zs in @planes.keys.sort ## \todo have the sort
137
 
                                        ## direction configurable.
138
 
              f = @planes[zs]
139
 
              color = cmap.z_color(zs, min, max)
140
 
              @curve_style.marker.draw_markers_at(t, f.x, f.y, 
141
 
                                                  { 'color' => color})
142
 
            end
143
 
          end
144
 
        end
145
 
        
146
 
        ## Actually draws the curve
147
 
        def real_do(t)
148
 
          debug { "Plotting curve #{inspect}" }
149
 
          t.context do
150
 
            ## \todo allow customization of the order of drawing,
151
 
            ## using a simple user-specificable array of path,
152
 
            ## markers... and use the corresponding #draw_path or
153
 
            ## #draw_markers... Ideally, any string could be used, and
154
 
            ## warnings should be issued on missing symbols.
155
 
 
156
 
            # draw_fill(t)
157
 
            # draw_errorbars(t)
158
 
            draw_path(t)
159
 
            draw_markers(t)
160
 
 
161
 
            if @curve_style.zaxis
162
 
              begin
163
 
                @parent.style.get_axis_style(@curve_style.zaxis).
164
 
                  set_color_map(@curve_style.marker_color_map, 
165
 
                                @dataset.z.values.min,
166
 
                                @dataset.z.values.max)
167
 
              rescue
168
 
                error { "Could not set Z info to non-existent axis #{@curve_style.zaxis}" }
169
 
              end
170
 
            end
171
 
 
172
 
            # draw_error_bars(t) ??
173
 
          end
174
 
        end
175
 
        
176
 
      end
177
 
    end
178
 
  end
179
 
end