ruby-gr 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -0
- data/README.md +12 -10
- data/lib/gr.rb +339 -292
- data/lib/gr/ffi.rb +188 -180
- data/lib/gr/grbase.rb +1 -0
- data/lib/gr/plot.rb +1344 -0
- data/lib/gr/plot.rb.md +207 -0
- data/lib/gr3.rb +99 -81
- data/lib/gr3/ffi.rb +114 -78
- data/lib/gr3/gr3base.rb +5 -0
- data/lib/gr_commons.rb +1 -1
- data/lib/gr_commons/define_methods.rb +3 -1
- data/lib/gr_commons/extern.rb +16 -0
- data/lib/gr_commons/fiddley.rb +419 -0
- data/lib/gr_commons/gr_common_utils.rb +19 -48
- data/lib/gr_commons/jupyter_support.rb +5 -2
- data/lib/gr_commons/version.rb +1 -1
- metadata +37 -20
- data/lib/gr_commons/attach_function.rb +0 -15
data/lib/gr3/ffi.rb
CHANGED
@@ -1,95 +1,131 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'fiddle/import'
|
4
4
|
|
5
5
|
module GR3
|
6
6
|
# FFI Wrapper module for GR3
|
7
7
|
module FFI
|
8
|
-
extend ::
|
8
|
+
extend Fiddle::Importer
|
9
9
|
|
10
10
|
begin
|
11
|
-
|
11
|
+
dlload GR3.ffi_lib
|
12
12
|
rescue LoadError
|
13
13
|
raise LoadError, 'Could not find GR Framework'
|
14
14
|
end
|
15
15
|
|
16
|
-
extend GRCommons::
|
16
|
+
extend GRCommons::Extern
|
17
17
|
|
18
18
|
# https://github.com/sciapp/gr/blob/master/lib/gr3/gr3.h
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
20
|
+
extern 'int gr3_init(int *attrib_list)'
|
21
|
+
extern 'void gr3_free(void *pointer)'
|
22
|
+
extern 'void gr3_terminate(void)'
|
23
|
+
extern 'int gr3_geterror(int clear, int *line, const char **file)'
|
24
|
+
extern 'const char *gr3_getrenderpathstring(void)'
|
25
|
+
extern 'const char *gr3_geterrorstring(int error)'
|
26
|
+
# extern 'void gr3_setlogcallback(void (*gr3_log_func)(const char *log_message))'
|
27
|
+
extern 'int gr3_clear(void)'
|
28
|
+
extern 'void gr3_usecurrentframebuffer()'
|
29
|
+
extern 'void gr3_useframebuffer(unsigned int framebuffer)'
|
30
|
+
extern 'int gr3_setquality(int quality)'
|
31
|
+
extern 'int gr3_getimage(int width, int height, int use_alpha, char *pixels)'
|
32
|
+
extern 'int gr3_export(const char *filename, int width, int height)'
|
33
|
+
extern 'int gr3_drawimage(float xmin, float xmax, float ymin, float ymax, ' \
|
34
|
+
'int width, int height, int drawable_type)'
|
35
|
+
extern 'int gr3_createmesh_nocopy(int *mesh, int n, ' \
|
36
|
+
'float *vertices, float *normals, float *colors)'
|
37
|
+
extern 'int gr3_createmesh(int *mesh, int n, ' \
|
38
|
+
'const float *vertices, const float *normals, const float *colors)'
|
39
|
+
extern 'int gr3_createindexedmesh_nocopy(int *mesh, int number_of_vertices, ' \
|
40
|
+
'float *vertices, float *normals, float *colors, int number_of_indices, int *indices)'
|
41
|
+
extern 'int gr3_createindexedmesh(int *mesh, int number_of_vertices, ' \
|
42
|
+
'const float *vertices, const float *normals, const float *colors, ' \
|
43
|
+
'int number_of_indices, const int *indices)'
|
44
|
+
extern 'void gr3_drawmesh(int mesh, int n, ' \
|
45
|
+
'const float *positions, const float *directions, const float *ups, ' \
|
46
|
+
'const float *colors, const float *scales)'
|
47
|
+
extern 'void gr3_deletemesh(int mesh)'
|
48
|
+
extern 'void gr3_cameralookat(float camera_x, float camera_y, float camera_z, ' \
|
49
|
+
'float center_x, float center_y, float center_z, ' \
|
50
|
+
'float up_x, float up_y, float up_z)'
|
51
|
+
extern 'int gr3_setcameraprojectionparameters(float vertical_field_of_view, ' \
|
52
|
+
'float zNear, float zFar)'
|
53
|
+
extern 'int gr3_getcameraprojectionparameters(float *vfov, float *znear, float *zfar)'
|
54
|
+
extern 'void gr3_setlightdirection(float x, float y, float z)'
|
55
|
+
extern 'void gr3_setbackgroundcolor(float red, float green, float blue, float alpha)'
|
56
|
+
extern 'int gr3_createheightmapmesh(const float *heightmap, int num_columns, int num_rows)'
|
57
|
+
extern 'void gr3_drawheightmap(const float *heightmap, ' \
|
58
|
+
'int num_columns, int num_rows, const float *positions, const float *scales)'
|
59
|
+
extern 'void gr3_drawconemesh(int n, const float *positions, const float *directions, ' \
|
60
|
+
'const float *colors, const float *radii, const float *lengths)'
|
61
|
+
extern 'void gr3_drawcylindermesh(int n, const float *positions, const float *directions, ' \
|
62
|
+
'const float *colors, const float *radii, const float *lengths)'
|
63
|
+
extern 'void gr3_drawspheremesh(int n, const float *positions, ' \
|
64
|
+
'const float *colors, const float *radii)'
|
65
|
+
extern 'void gr3_drawcubemesh(int n, ' \
|
66
|
+
'const float *positions, const float *directions, const float *ups, ' \
|
67
|
+
'const float *colors, const float *scales)'
|
68
|
+
extern 'void gr3_setobjectid(int id)'
|
69
|
+
extern 'int gr3_selectid(int x, int y, int width, int height, int *selection_id)'
|
70
|
+
extern 'void gr3_getviewmatrix(float *m)'
|
71
|
+
extern 'void gr3_setviewmatrix(const float *m)'
|
72
|
+
extern 'int gr3_getprojectiontype(void)'
|
73
|
+
extern 'void gr3_setprojectiontype(int type)'
|
74
|
+
# extern 'unsigned int gr3_triangulate(const unsigned short *data, unsigned short isolevel, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, double step_x, double step_y, double step_z, double offset_x, double offset_y, double offset_z, gr3_triangle_t **triangles_p)'
|
75
|
+
# extern 'void gr3_triangulateindexed(const unsigned short *data, unsigned short isolevel, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, double step_x, double step_y, double step_z, double offset_x, double offset_y, double offset_z, unsigned int *num_vertices, gr3_coord_t **vertices, gr3_coord_t **normals, unsigned int *num_indices, unsigned int **indices)'
|
76
|
+
extern 'int gr3_createisosurfacemesh(int *mesh, unsigned short *data, ' \
|
77
|
+
'unsigned short isolevel, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
|
78
|
+
'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
|
79
|
+
'double step_x, double step_y, double step_z, ' \
|
80
|
+
'double offset_x, double offset_y, double offset_z)'
|
81
|
+
extern 'int gr3_createsurfacemesh(int *mesh, int nx, int ny, ' \
|
82
|
+
'float *px, float *py, float *pz, int option)'
|
83
|
+
extern 'void gr3_drawmesh_grlike(int mesh, int n, ' \
|
84
|
+
'const float *positions, const float *directions, const float *ups, ' \
|
85
|
+
'const float *colors, const float *scales)'
|
86
|
+
extern 'void gr3_drawsurface(int mesh)'
|
87
|
+
extern 'void gr3_surface(int nx, int ny, float *px, float *py, float *pz, int option)'
|
88
|
+
extern 'int gr3_drawtubemesh(int n, float *points, float *colors, float *radii, ' \
|
89
|
+
'int num_steps, int num_segments)'
|
90
|
+
extern 'int gr3_createtubemesh(int *mesh, int n, ' \
|
91
|
+
'const float *points, const float *colors, const float *radii, ' \
|
92
|
+
'int num_steps, int num_segments)'
|
93
|
+
extern 'void gr3_drawspins(int n, const float *positions, const float *directions, const float *colors, ' \
|
94
|
+
'float cone_radius, float cylinder_radius, float cone_height, float cylinder_height)'
|
95
|
+
extern 'void gr3_drawmolecule(int n, const float *positions, const float *colors, const float *radii, ' \
|
96
|
+
'float bond_radius, const float bond_color[3], float bond_delta)'
|
97
|
+
extern 'void gr3_createxslicemesh(int *mesh, const unsigned short *data, ' \
|
98
|
+
'unsigned int ix, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
|
99
|
+
'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
|
100
|
+
'double step_x, double step_y, double step_z, ' \
|
101
|
+
'double offset_x, double offset_y, double offset_z)'
|
102
|
+
extern 'void gr3_createyslicemesh(int *mesh, const unsigned short *data, ' \
|
103
|
+
'unsigned int iy, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
|
104
|
+
'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
|
105
|
+
'double step_x, double step_y, double step_z, ' \
|
106
|
+
'double offset_x, double offset_y, double offset_z)'
|
107
|
+
extern 'void gr3_createzslicemesh(int *mesh, const unsigned short *data, ' \
|
108
|
+
'unsigned int iz, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
|
109
|
+
'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
|
110
|
+
'double step_x, double step_y, double step_z, ' \
|
111
|
+
'double offset_x, double offset_y, double offset_z)'
|
112
|
+
extern 'void gr3_drawxslicemesh(const unsigned short *data, unsigned int ix,' \
|
113
|
+
'unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
|
114
|
+
'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
|
115
|
+
'double step_x, double step_y, double step_z, ' \
|
116
|
+
'double offset_x, double offset_y, double offset_z)'
|
117
|
+
extern 'void gr3_drawyslicemesh(const unsigned short *data, unsigned int iy, ' \
|
118
|
+
'unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
|
119
|
+
'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
|
120
|
+
'double step_x, double step_y, double step_z, ' \
|
121
|
+
'double offset_x, double offset_y, double offset_z)'
|
122
|
+
extern 'void gr3_drawzslicemesh(const unsigned short *data, unsigned int iz, ' \
|
123
|
+
'unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
|
124
|
+
'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
|
125
|
+
'double step_x, double step_y, double step_z, ' \
|
126
|
+
'double offset_x, double offset_y, double offset_z)'
|
127
|
+
# extern 'void gr3_drawtrianglesurface(int n, const float *triangles)'
|
128
|
+
extern 'void gr_volume(int nx, int ny, int nz, double *data, ' \
|
129
|
+
'int algorithm, double *dmin_ptr, double *dmax_ptr)'
|
94
130
|
end
|
95
131
|
end
|
data/lib/gr3/gr3base.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module GR3
|
4
|
+
# This module automatically converts Ruby arrays and Numo::Narray into pointers.
|
4
5
|
module GR3Base
|
5
6
|
extend GRCommons::DefineMethods
|
6
7
|
define_ffi_methods(FFI,
|
7
8
|
prefix: 'gr3_',
|
8
9
|
default_type: :float)
|
10
|
+
# workaround for `gr_volume`
|
11
|
+
define_ffi_methods(FFI,
|
12
|
+
prefix: 'gr_',
|
13
|
+
default_type: :double)
|
9
14
|
end
|
10
15
|
private_constant :GR3Base
|
11
16
|
|
data/lib/gr_commons.rb
CHANGED
@@ -10,12 +10,14 @@ module GRCommons
|
|
10
10
|
# delete_prefix (Ruby >= 2.5)
|
11
11
|
method_name = method.to_s.sub(/^#{prefix}/, '')
|
12
12
|
|
13
|
+
# FIXME: Refactoring required
|
14
|
+
|
13
15
|
define_method(method_name) do |*args|
|
14
16
|
args.map! do |arg|
|
15
17
|
case arg
|
16
18
|
when Array
|
17
19
|
send(default_type, arg)
|
18
|
-
when ->(x) {
|
20
|
+
when ->(x) { defined?(Numo::NArray) && x.is_a?(Numo::NArray) }
|
19
21
|
send(default_type, arg)
|
20
22
|
else
|
21
23
|
arg
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GRCommons
|
4
|
+
# This module records the names of the methods defined by Fiddle::Importer.
|
5
|
+
module Extern
|
6
|
+
attr_reader :ffi_methods
|
7
|
+
|
8
|
+
# Remember added method name
|
9
|
+
def extern(*args)
|
10
|
+
@ffi_methods ||= []
|
11
|
+
func = super(*args)
|
12
|
+
@ffi_methods << func.name
|
13
|
+
func
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,419 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Fiddley v0.0.8
|
4
|
+
# https://github.com/unak/fiddley
|
5
|
+
#
|
6
|
+
# Copyright (c) 2017 NAKAMURA Usaku usa@garbagecollect.jp
|
7
|
+
#
|
8
|
+
# Released under the 2-Clause BSD License.
|
9
|
+
|
10
|
+
# NOTE: kojix2 added, removed, and modified several methods.
|
11
|
+
|
12
|
+
require 'fiddle/import'
|
13
|
+
|
14
|
+
module GRCommons
|
15
|
+
# Ruby-FFI compatible API layer for Fiddle.
|
16
|
+
module Fiddley
|
17
|
+
# NOTE: GR.rb supports 2.4 +. Unpack 1 does not work under 2.3.
|
18
|
+
|
19
|
+
module Utils
|
20
|
+
# assumes short = 16bit, int = 32bit, long long = 64bit
|
21
|
+
SIZET_FORMAT = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG ? 'l!' : 'q'
|
22
|
+
SIZET_TYPE = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG ? 'unsigned long' : 'unsigned long long'
|
23
|
+
|
24
|
+
module_function
|
25
|
+
|
26
|
+
def type2size(type)
|
27
|
+
case type
|
28
|
+
when :char, :uchar, :int8, :uint8
|
29
|
+
Fiddle::SIZEOF_CHAR
|
30
|
+
when :short, :ushort, :int16, :uint16
|
31
|
+
Fiddle::SIZEOF_SHORT
|
32
|
+
when :int, :uint, :int32, :uint32, :bool
|
33
|
+
Fiddle::SIZEOF_INT
|
34
|
+
when :long, :ulong
|
35
|
+
Fiddle::SIZEOF_LONG
|
36
|
+
when :int64, :uint64, :long_long, :ulong_long
|
37
|
+
Fiddle::SIZEOF_LONG_LONG
|
38
|
+
when :float
|
39
|
+
Fiddle::SIZEOF_FLOAT
|
40
|
+
when :double
|
41
|
+
Fiddle::SIZEOF_DOUBLE
|
42
|
+
when :size_t
|
43
|
+
Fiddle::SIZEOF_SIZE_T
|
44
|
+
when :string, :pointer
|
45
|
+
Fiddle::SIZEOF_VOIDP
|
46
|
+
else
|
47
|
+
raise "unknown type #{type}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# `type2offset_size` is not used in GR.rb, so deleted.
|
52
|
+
|
53
|
+
# `str2value` is not used in GR.rb, so deleted.
|
54
|
+
|
55
|
+
# added
|
56
|
+
def str2array(type, str)
|
57
|
+
case type
|
58
|
+
when :char, :int8
|
59
|
+
str.unpack('c*')
|
60
|
+
when :uchar, :uint8
|
61
|
+
str.unpack('C*')
|
62
|
+
when :short, :int16
|
63
|
+
str.unpack('s*')
|
64
|
+
when :ushort, :uint16
|
65
|
+
str.unpack('S*')
|
66
|
+
when :int32
|
67
|
+
str.unpack('l*')
|
68
|
+
when :uint32
|
69
|
+
str.unpack('L*')
|
70
|
+
when :int
|
71
|
+
str.unpack('i!*')
|
72
|
+
when :uint
|
73
|
+
str.unpack('I!*')
|
74
|
+
when :bool
|
75
|
+
str.unpack('i!*') != 0
|
76
|
+
when :long
|
77
|
+
str.unpack('l!*')
|
78
|
+
when :ulong
|
79
|
+
str.unpack('L!*')
|
80
|
+
when :long_long, :int64
|
81
|
+
str.unpack('q*')
|
82
|
+
when :ulong_long, :uint64
|
83
|
+
str.unpack('Q*')
|
84
|
+
when :size_t
|
85
|
+
str.unpack(SIZET_FORMAT)
|
86
|
+
when :float
|
87
|
+
str.unpack('f*')
|
88
|
+
when :double
|
89
|
+
str.unpack('d*')
|
90
|
+
when :string, :pointer
|
91
|
+
str.unpack('p*')
|
92
|
+
else
|
93
|
+
raise "unknown type #{type}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# `value2str` is not used in GR.rb, so deleted.
|
98
|
+
|
99
|
+
# added
|
100
|
+
def array2str(type, arr)
|
101
|
+
case type
|
102
|
+
when :char, :int8
|
103
|
+
arr.pack('c*')
|
104
|
+
when :uchar, :uint8
|
105
|
+
arr.pack('C*')
|
106
|
+
when :short, :int16
|
107
|
+
arr.pack('s*')
|
108
|
+
when :ushort, :uint16
|
109
|
+
arr.pack('S*')
|
110
|
+
when :int32
|
111
|
+
arr.pack('l*')
|
112
|
+
when :uint32
|
113
|
+
arr.pack('L*')
|
114
|
+
when :int
|
115
|
+
arr.pack('i!*')
|
116
|
+
when :uint
|
117
|
+
arr.pack('I!*')
|
118
|
+
when :bool
|
119
|
+
[arr ? 1 : 0].pack('i!*')
|
120
|
+
when :long
|
121
|
+
arr.pack('l!*')
|
122
|
+
when :ulong
|
123
|
+
arr.pack('L!*')
|
124
|
+
when :long_long, :int64
|
125
|
+
arr.pack('q*')
|
126
|
+
when :ulong_long, :uint64
|
127
|
+
arr.pack('Q*')
|
128
|
+
when :size_t
|
129
|
+
arr.pack(SIZET_FORMAT)
|
130
|
+
when :float
|
131
|
+
arr.pack('f*')
|
132
|
+
when :double
|
133
|
+
arr.pack('d*')
|
134
|
+
when :string, :pointer
|
135
|
+
arr.pack('p*')
|
136
|
+
else
|
137
|
+
raise "unknown type #{type}"
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# `type2str` is not used in GR.rb, so deleted.
|
142
|
+
|
143
|
+
def type2type(type)
|
144
|
+
case type
|
145
|
+
when :char, :int8
|
146
|
+
Fiddle::TYPE_CHAR
|
147
|
+
when :uchar, :uint8
|
148
|
+
-Fiddle::TYPE_CHAR
|
149
|
+
when :short, :int16
|
150
|
+
Fiddle::TYPE_SHORT
|
151
|
+
when :ushort, :uint16
|
152
|
+
-Fiddle::TYPE_SHORT
|
153
|
+
when :int, :int32
|
154
|
+
Fiddle::TYPE_INT
|
155
|
+
when :uint, :uint32
|
156
|
+
-Fiddle::TYPE_INT
|
157
|
+
when :bool
|
158
|
+
Fiddle::TYPE_INT
|
159
|
+
when :long
|
160
|
+
Fiddle::TYPE_LONG
|
161
|
+
when :ulong
|
162
|
+
-Fiddle::TYPE_LONG
|
163
|
+
when :long_long, :int64
|
164
|
+
Fiddle::TYPE_LONG_LONG
|
165
|
+
when :ulong_long, :uint64
|
166
|
+
-Fiddle::TYPE_LONG_LONG
|
167
|
+
when :float
|
168
|
+
Fiddle::TYPE_FLOAT
|
169
|
+
when :double
|
170
|
+
Fiddle::TYPE_DOUBLE
|
171
|
+
when :size_t
|
172
|
+
Fiddle::TYPE_SIZE_T
|
173
|
+
when :string, :pointer
|
174
|
+
Fiddle::TYPE_VOIDP
|
175
|
+
when :void
|
176
|
+
Fiddle::TYPE_VOID
|
177
|
+
else
|
178
|
+
raise "unknown type #{type}"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
class MemoryPointer
|
184
|
+
using Fiddley::RefineStringUnpack1 if defined?(Fiddley::RefineStringUnpack1)
|
185
|
+
|
186
|
+
include Fiddley::Utils
|
187
|
+
|
188
|
+
def initialize(type, num = 1, _clear = true)
|
189
|
+
if num.is_a?(Fiddle::Pointer)
|
190
|
+
@ptr = num
|
191
|
+
@size = @ptr.size
|
192
|
+
else
|
193
|
+
@size = type2size(type) * num
|
194
|
+
@ptr = Fiddle::Pointer.malloc(@size)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
attr_reader :size
|
199
|
+
|
200
|
+
def to_ptr
|
201
|
+
@ptr
|
202
|
+
end
|
203
|
+
|
204
|
+
{ 8 => 'c', 16 => 's', 32 => 'l', 64 => 'q' }.each do |bits, form|
|
205
|
+
bytes = bits / 8
|
206
|
+
|
207
|
+
define_method("put_int#{bits}") do |offset, val|
|
208
|
+
@ptr[offset, bytes] = [val].pack(form)
|
209
|
+
end
|
210
|
+
|
211
|
+
define_method("write_int#{bits}") do |val|
|
212
|
+
__send__("put_int#{bits}", 0, val)
|
213
|
+
end
|
214
|
+
|
215
|
+
define_method("put_array_of_int#{bits}") do |offset, ary|
|
216
|
+
put_bytes(offset, ary.pack(form + '*'))
|
217
|
+
end
|
218
|
+
|
219
|
+
define_method("write_array_of_int#{bits}") do |ary|
|
220
|
+
__send__("put_array_of_int#{bits}", 0, ary)
|
221
|
+
end
|
222
|
+
|
223
|
+
define_method("get_int#{bits}") do |offset|
|
224
|
+
@ptr[offset, bytes].unpack1(form)
|
225
|
+
end
|
226
|
+
|
227
|
+
define_method("read_int#{bits}") do
|
228
|
+
__send__("get_int#{bits}", 0)
|
229
|
+
end
|
230
|
+
|
231
|
+
define_method("get_array_of_int#{bits}") do |offset, num|
|
232
|
+
@ptr[offset, bytes * num].unpack(form + '*')
|
233
|
+
end
|
234
|
+
|
235
|
+
define_method("read_array_of_int#{bits}") do |num|
|
236
|
+
__send__("get_array_of_int#{bits}", 0, num)
|
237
|
+
end
|
238
|
+
|
239
|
+
form2 = form.upcase
|
240
|
+
|
241
|
+
define_method("put_uint#{bits}") do |offset, val|
|
242
|
+
@ptr[offset, bytes] = [val].pack(form2)
|
243
|
+
end
|
244
|
+
|
245
|
+
define_method("write_uint#{bits}") do |val|
|
246
|
+
__send__("put_uint#{bits}", 0, val)
|
247
|
+
end
|
248
|
+
|
249
|
+
define_method("put_array_of_uint#{bits}") do |offset, ary|
|
250
|
+
put_bytes(offset, ary.pack(form2 + '*'))
|
251
|
+
end
|
252
|
+
|
253
|
+
define_method("write_array_of_uint#{bits}") do |ary|
|
254
|
+
__send__("put_array_of_uint#{bits}", 0, ary)
|
255
|
+
end
|
256
|
+
|
257
|
+
define_method("get_uint#{bits}") do |offset|
|
258
|
+
@ptr[offset, bytes].unpack1(form2)
|
259
|
+
end
|
260
|
+
|
261
|
+
define_method("read_uint#{bits}") do
|
262
|
+
__send__("get_uint#{bits}", 0)
|
263
|
+
end
|
264
|
+
|
265
|
+
define_method("get_array_of_uint#{bits}") do |offset, num|
|
266
|
+
@ptr[offset, bytes * num].unpack(form2 + '*')
|
267
|
+
end
|
268
|
+
|
269
|
+
define_method("read_array_of_uint#{bits}") do |num|
|
270
|
+
__send__("get_array_of_uint#{bits}", 0, num)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
# added
|
275
|
+
define_method('put_double') do |offset, val|
|
276
|
+
@ptr[offset, 8] = [val].pack('d')
|
277
|
+
end
|
278
|
+
|
279
|
+
# added
|
280
|
+
define_method('put_float') do |offset, val|
|
281
|
+
@ptr[offset, 4] = [val].pack('f')
|
282
|
+
end
|
283
|
+
|
284
|
+
# added
|
285
|
+
define_method('write_double') do |val|
|
286
|
+
__send__('put_double', 0, val)
|
287
|
+
end
|
288
|
+
|
289
|
+
# added
|
290
|
+
define_method('write_float') do |val|
|
291
|
+
__send__('put_float', 0, val)
|
292
|
+
end
|
293
|
+
|
294
|
+
# added
|
295
|
+
define_method('put_array_of_double') do |offset, ary|
|
296
|
+
put_bytes(offset, ary.pack('d*'))
|
297
|
+
end
|
298
|
+
|
299
|
+
# added
|
300
|
+
define_method('write_array_of_double') do |ary|
|
301
|
+
__send__('put_array_of_double', 0, ary)
|
302
|
+
end
|
303
|
+
|
304
|
+
# added
|
305
|
+
define_method('put_array_of_float') do |offset, ary|
|
306
|
+
put_bytes(offset, ary.pack('f*'))
|
307
|
+
end
|
308
|
+
|
309
|
+
# added
|
310
|
+
define_method('write_array_of_float') do |ary|
|
311
|
+
__send__('put_array_of_float', 0, ary)
|
312
|
+
end
|
313
|
+
|
314
|
+
# added
|
315
|
+
define_method('get_double') do |offset|
|
316
|
+
@ptr[offset, 8].unpack1('d')
|
317
|
+
end
|
318
|
+
|
319
|
+
# added
|
320
|
+
define_method('read_double') do
|
321
|
+
__send__('get_double', 0)
|
322
|
+
end
|
323
|
+
|
324
|
+
# added
|
325
|
+
define_method('get_float') do |offset|
|
326
|
+
@ptr[offset, 4].unpack1('f')
|
327
|
+
end
|
328
|
+
|
329
|
+
# added
|
330
|
+
define_method('read_float') do
|
331
|
+
__send__('get_double', 0)
|
332
|
+
end
|
333
|
+
|
334
|
+
# added
|
335
|
+
define_method('get_array_of_double') do |offset, num|
|
336
|
+
@ptr[offset, 8 * num].unpack('d' + '*')
|
337
|
+
end
|
338
|
+
|
339
|
+
# added
|
340
|
+
define_method('get_array_of_float') do |offset, num|
|
341
|
+
@ptr[offset, 4 * num].unpack('f' + '*')
|
342
|
+
end
|
343
|
+
|
344
|
+
# added
|
345
|
+
define_method('read_array_of_double') do |num|
|
346
|
+
__send__('get_array_of_double', 0, num)
|
347
|
+
end
|
348
|
+
|
349
|
+
# added
|
350
|
+
define_method('read_array_of_float') do |num|
|
351
|
+
__send__('get_array_of_float', 0, num)
|
352
|
+
end
|
353
|
+
|
354
|
+
def put_bytes(offset, str, idx = 0, len = str.bytesize - idx)
|
355
|
+
@ptr[offset, len] = str[idx, len]
|
356
|
+
end
|
357
|
+
|
358
|
+
def write_bytes(str, idx = 0, len = nil)
|
359
|
+
put_bytes(0, str, idx, len)
|
360
|
+
end
|
361
|
+
|
362
|
+
def get_bytes(offset, len)
|
363
|
+
@ptr[offset, len]
|
364
|
+
end
|
365
|
+
|
366
|
+
def read_bytes(len)
|
367
|
+
get_bytes(0, len)
|
368
|
+
end
|
369
|
+
|
370
|
+
def put_string(offset, str)
|
371
|
+
@ptr[offset, str.bytesize] = str
|
372
|
+
end
|
373
|
+
|
374
|
+
def write_string(str, len = nil)
|
375
|
+
put_string(0, len ? str[0, len] : str)
|
376
|
+
end
|
377
|
+
|
378
|
+
def write_string_length(str, len)
|
379
|
+
put_string(0, str[0, len])
|
380
|
+
end
|
381
|
+
|
382
|
+
def get_string(offset, len = nil)
|
383
|
+
@ptr[offset, len || @size - offset]
|
384
|
+
end
|
385
|
+
|
386
|
+
def read_string(len = nil)
|
387
|
+
get_string(0, len)
|
388
|
+
end
|
389
|
+
|
390
|
+
# added
|
391
|
+
# NOTE: Return Fiddle::Pointer. Not MemoryPointer.
|
392
|
+
def read_pointer
|
393
|
+
to_ptr.ptr
|
394
|
+
end
|
395
|
+
|
396
|
+
alias put_int put_int32
|
397
|
+
alias write_int write_int32
|
398
|
+
alias get_int get_int32
|
399
|
+
alias read_int read_int32
|
400
|
+
alias write_array_of_int write_array_of_int32 # added
|
401
|
+
alias read_array_of_int read_array_of_int32 # added
|
402
|
+
|
403
|
+
alias put_uint put_uint32
|
404
|
+
alias write_uint write_uint32
|
405
|
+
alias get_uint get_uint32
|
406
|
+
alias read_uint read_uint32
|
407
|
+
alias write_array_of_uint write_array_of_uint32 # added
|
408
|
+
alias read_array_of_uint read_array_of_uint32 # added
|
409
|
+
end
|
410
|
+
|
411
|
+
class Function < Fiddle::Closure::BlockCaller
|
412
|
+
include Fiddley::Utils
|
413
|
+
|
414
|
+
def initialize(ret, params, &blk)
|
415
|
+
super(type2type(ret), params.map { |e| type2type(e) }, &blk)
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end
|
419
|
+
end
|