gss_generator 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4868e0b7c76222b5191410a95508ad4bbfedae3c
4
- data.tar.gz: 101d123737d65b5b2b7fdc72b6e302c45e53e987
3
+ metadata.gz: dcb0eb5d3947f39d7d397b800ecab0c7cec2de3a
4
+ data.tar.gz: 57a4f152cf75ecdd5b4fc699d1a4772ac7d85467
5
5
  SHA512:
6
- metadata.gz: 4b8bdf6afa55a7244d59e63d97d3c736d40bce949b7a90caa012c85847b24998d685f250a5b8c693d36b907e48792d9ae955756d28b6d846d3b123d780080634
7
- data.tar.gz: 80920acc43a63a223fe68ea43a7cf02a5404df5c74667d74a22e77a9d46e4198f2af8c00163ff081720a0bea1c97ca66ee5ba62f6a399e660cdb706734e30a07
6
+ metadata.gz: 8a5bf0fc925400d7eed9044ac3b0f6e052883bf7d59a54e58b13434b56cab265fd3112c52eab2b71b060f5c8700f82ff804af6353387a715d127ac37bb76e120
7
+ data.tar.gz: 926e844d43ef31040c1c8db4136f79dca04409c0232fa73cc0b7127452f5eb3a9dbd75d52899ac7bc0d3f9c8fdf908c0e6ae7847e2b971b2c859151d328df2da
@@ -12,6 +12,9 @@ opts.banner = "Generate points with uniform distribution on the sphere."
12
12
  opts.on("-c", "--cartesian", "Cartesian coordinate."){|v|
13
13
  options[:cartesian] = true
14
14
  }
15
+ opts.on("-r", "--relocate", "Relocate first and last point."){|v|
16
+ options[:relocation] = true
17
+ }
15
18
  opts.on_tail("-h", "--help", "Show this message."){|v|
16
19
  print opts.help
17
20
  exit
@@ -25,13 +28,13 @@ opts.parse!
25
28
  r = ARGV.shift.to_f
26
29
  n = ARGV.shift.to_i
27
30
 
28
- gss = GSS::GSS.new
29
- points = gss.generate(r, n)
31
+ gss = GSS::Generator.new
32
+ points = gss.generate(n, options[:relocation])
30
33
  points.each do |p|
31
34
  if options[:cartesian]
32
- coord = p.to_cartesian
35
+ coord = p.to_cartesian.map{|c| c * r }
33
36
  print coord.to_csv
34
37
  else
35
- print [p.r, p.theta, p.phi].to_csv
38
+ print [r, p.theta, p.phi].to_csv
36
39
  end
37
40
  end
data/lib/gss.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "gss/version"
2
- require "gss/gss"
2
+ require "gss/generator"
3
3
 
4
4
  module GSS
5
5
  # Your code goes here...
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Generate points that distributed on unit sphere with Generalized Spiral Set.
4
+ # cf. https://www.jstage.jst.go.jp/article/geoinformatics/12/1/12_1_3/_article/-char/ja/
5
+ #
6
+
7
+ require "gss/polar_point"
8
+
9
+
10
+ module GSS
11
+
12
+ class Generator
13
+ def generate(n, relocation = false)
14
+ theta_1 = Math::PI
15
+ phi_1 = 0.0
16
+
17
+ points = []
18
+ points << PolarPoint.new(theta_1, phi_1)
19
+ 2.upto(n) do |k|
20
+ h_k = -1.0 + 2.0 * (k - 1) / (n - 1)
21
+ theta_k = Math.acos(h_k)
22
+ phi_k = points.last.phi + 3.6 / Math.sqrt(n) * 1 / Math.sqrt(1 - h_k ** 2)
23
+ phi_k = phi_k.infinite? ? 0.0 : phi_k % (Math::PI * 2.0)
24
+ points << PolarPoint.new(theta_k, phi_k)
25
+ end
26
+
27
+ if relocation
28
+ points[0] = relocate(points[1], points[2], points[4], points[5], points[6])
29
+ points[-1] = relocate(points[-2], points[-3], points[-5], points[-6], points[-7])
30
+ end
31
+
32
+ points
33
+ end
34
+
35
+ private
36
+
37
+ def relocate(p1, p2, p3, p4, p5)
38
+ c = [p1, p2, p3, p4, p5].map{|p| p.to_cartesian }
39
+ c = c.transpose.map{|l| l.inject(&:+) / 5.0 }
40
+ PolarPoint.from_cartesian(c[0], c[1], c[2])
41
+ end
42
+
43
+ end # of class Generator
44
+
45
+ end # of module GSS
@@ -4,20 +4,25 @@
4
4
  module GSS
5
5
 
6
6
  class PolarPoint
7
- attr_reader :r, :theta, :phi
7
+ attr_reader :theta, :phi
8
8
 
9
- def initialize(r, theta, phi)
10
- @r = r
9
+ def initialize(theta, phi)
11
10
  @theta = theta
12
11
  @phi = phi
13
12
  end
14
13
 
15
14
  def to_cartesian
16
- x = @r * Math.sin(@theta) * Math.cos(@phi)
17
- y = @r * Math.sin(@theta) * Math.sin(@phi)
18
- z = @r * Math.cos(@theta)
15
+ x = Math.sin(@theta) * Math.cos(@phi)
16
+ y = Math.sin(@theta) * Math.sin(@phi)
17
+ z = Math.cos(@theta)
19
18
  [x, y, z]
20
19
  end
20
+
21
+ def self.from_cartesian(x, y, z)
22
+ theta = Math.acos(z / Math.sqrt(x * x + y * y + z * z))
23
+ phi = Math.atan2(y, x)
24
+ self.new(theta, phi)
25
+ end
21
26
  end # of class PolarPoint
22
27
 
23
28
  end # of module GSS
@@ -1,3 +1,3 @@
1
1
  module GSS
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gss_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takatoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-21 00:00:00.000000000 Z
11
+ date: 2017-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -57,7 +57,7 @@ files:
57
57
  - exe/gss_gen
58
58
  - gss_generator.gemspec
59
59
  - lib/gss.rb
60
- - lib/gss/gss.rb
60
+ - lib/gss/generator.rb
61
61
  - lib/gss/polar_point.rb
62
62
  - lib/gss/version.rb
63
63
  homepage: https://github.com/takatoh/gss_generator
@@ -1,27 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require "gss/polar_point"
4
-
5
-
6
- module GSS
7
-
8
- class GSS
9
- def generate(r, n)
10
- theta_1 = Math::PI
11
- phi_1 = 0.0
12
-
13
- points = []
14
- points << PolarPoint.new(r, theta_1, phi_1)
15
- 2.upto(n) do |k|
16
- h_k = -1.0 + 2.0 * (k - 1) / (n - 1)
17
- theta_k = Math.acos(h_k)
18
- phi_k = points.last.phi + 3.6 / Math.sqrt(n) * 1 / Math.sqrt(1 - h_k ** 2)
19
- phi_k = phi_k.infinite? ? 0.0 : phi_k % (Math::PI * 2.0)
20
- points << PolarPoint.new(r, theta_k, phi_k)
21
- end
22
-
23
- points
24
- end
25
- end # of class GSS
26
-
27
- end # of module GSS