~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/graphics/types/bijection.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# bijection.rb: a bijection representing a reversible coordinate transformation
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/log'
 
16
 
 
17
module CTioga2
 
18
 
 
19
  Version::register_svn_info('$Revision: 155 $', '$Date: 2010-06-21 21:41:32 +0200 (Mon, 21 Jun 2010) $')
 
20
 
 
21
  module Graphics
 
22
 
 
23
    module Types
 
24
 
 
25
      # This class represents a *reversible* arbitrary coordinate
 
26
      # transformation, such as the ones that could be desirable for
 
27
      # alternative axes. Characterized by two Block objects, #from
 
28
      # and #to, that converts respectively from and to the target
 
29
      # coordinates.
 
30
      class Bijection
 
31
 
 
32
        # A Block converting from the target coordinates
 
33
        attr_accessor :from
 
34
 
 
35
        # A Block converting to the target coordinates
 
36
        attr_accessor :to
 
37
 
 
38
        # Creates a new Bijection with the given blocks.
 
39
        def initialize(from, to = nil)
 
40
          @from = from
 
41
          @to = to || @from
 
42
        end
 
43
 
 
44
        # Converts a vector to the target coordinates
 
45
        def convert_to(vect)
 
46
          return vect.map do |x|
 
47
            self.to.call(x)
 
48
          end
 
49
        end
 
50
 
 
51
        # Converts a vector from the target coordinates
 
52
        def convert_from(vect)
 
53
          return vect.map do |x|
 
54
            self.from.call(x)
 
55
          end
 
56
        end
 
57
 
 
58
        # Creates a Bijection from a text representation.
 
59
        #
 
60
        # Takes functions of _x_. Takes two blocks _from_ _to_
 
61
        # separated by :: -- or only one block in the case of an
 
62
        # involution (very common, actually, all 1/x transforms).
 
63
        #
 
64
        # \todo few things around here to change... in particular,
 
65
        # I should try to find a way to include Math... 
 
66
        #
 
67
        # \todo add very common cases ?
 
68
        def self.from_text(spec)
 
69
          blocks = spec.split(/::/).map do |code|
 
70
            eval("proc do |x|\n#{code}\nend")
 
71
          end
 
72
          return Bijection.new(*blocks)
 
73
        end
 
74
      end
 
75
 
 
76
    end
 
77
  end
 
78
end
 
79