mathgl 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +39 -0
- data/COPYING_LGPL +165 -0
- data/Gemfile +4 -0
- data/README.md +64 -0
- data/Rakefile +1 -0
- data/ext/mathgl/depend +2 -0
- data/ext/mathgl/extconf.rb +17 -0
- data/ext/mathgl/fltk/depend +2 -0
- data/ext/mathgl/fltk/extconf.rb +20 -0
- data/ext/mathgl/glut/depend +2 -0
- data/ext/mathgl/glut/extconf.rb +20 -0
- data/ext/mathgl/mathgl.cxx +130660 -0
- data/ext/mathgl/mathgl.i +123 -0
- data/ext/mathgl/qt/depend +2 -0
- data/ext/mathgl/qt/extconf.rb +20 -0
- data/ext/mathgl/rubymgl.i +172 -0
- data/ext/mathgl/tmpl/mkwin.rb +9 -0
- data/lib/mathgl/version.rb +3 -0
- data/mathgl.gemspec +24 -0
- data/sample/conv_sample.rb +310 -0
- data/sample/prepare.rb +229 -0
- data/sample/sample.rb +5325 -0
- data/setup.rb +1585 -0
- metadata +101 -0
data/ext/mathgl/mathgl.i
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
/***************************************************************************
|
2
|
+
* mgl.i is part of Math Graphic Library
|
3
|
+
* Copyright (C) 2007 Alexey Balakin <balakin@appl.sci-nnov.ru>,
|
4
|
+
* Xavier Delacour <xavier.delacour@gmail.com>,
|
5
|
+
* Alexander Filov <alexander.filov@gmail.com> *
|
6
|
+
* *
|
7
|
+
* This program is free software; you can redistribute it and/or modify *
|
8
|
+
* it under the terms of the GNU General Public License as published by *
|
9
|
+
* the Free Software Foundation; either version 2 of the License, or *
|
10
|
+
* (at your option) any later version. *
|
11
|
+
* *
|
12
|
+
* This program is distributed in the hope that it will be useful, *
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
15
|
+
* GNU General Public License for more details. *
|
16
|
+
* *
|
17
|
+
* You should have received a copy of the GNU General Public License *
|
18
|
+
* along with this program; if not, write to the *
|
19
|
+
* Free Software Foundation, Inc., *
|
20
|
+
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
21
|
+
***************************************************************************/
|
22
|
+
|
23
|
+
#ifdef SWIGRUBY
|
24
|
+
%module MathGL
|
25
|
+
#else
|
26
|
+
%module mathgl
|
27
|
+
#endif
|
28
|
+
|
29
|
+
#ifdef SWIGOCTAVE
|
30
|
+
%feature("autodoc", 1);
|
31
|
+
#endif // SWIGOCTAVE
|
32
|
+
|
33
|
+
#ifdef SWIGRUBY
|
34
|
+
%feature("autodoc", 2);
|
35
|
+
#endif
|
36
|
+
|
37
|
+
%ignore operator!;
|
38
|
+
%ignore operator=;
|
39
|
+
%ignore operator!=;
|
40
|
+
%ignore *::operator=;
|
41
|
+
%ignore *::operator+=;
|
42
|
+
%ignore *::operator-=;
|
43
|
+
%ignore *::operator*=;
|
44
|
+
%ignore *::operator/=;
|
45
|
+
//%ignore mglDataA;
|
46
|
+
|
47
|
+
%{
|
48
|
+
#define SWIG_FILE_WITH_INIT
|
49
|
+
//#include "mgl2/config.h"
|
50
|
+
#include "mgl2/type.h"
|
51
|
+
#include "mgl2/data.h"
|
52
|
+
#include "mgl2/mgl.h"
|
53
|
+
//#include "mgl2/window.h"
|
54
|
+
%}
|
55
|
+
|
56
|
+
#if MGL_USE_DOUBLE
|
57
|
+
typedef double mreal;
|
58
|
+
#else
|
59
|
+
typedef float mreal;
|
60
|
+
#endif
|
61
|
+
|
62
|
+
|
63
|
+
#ifdef SWIGOCTAVE
|
64
|
+
%rename(__add) operator+;
|
65
|
+
%rename(__sub) operator-;
|
66
|
+
%rename(__mul) operator*;
|
67
|
+
%rename(__div) operator/;
|
68
|
+
%rename(__eq) operator==;
|
69
|
+
%rename(__ne) operator!=;
|
70
|
+
%typemap(in,noblock=1) (double* d, int rows, int cols) (Matrix tmp) {
|
71
|
+
if (!$input.is_matrix_type()) { error("A must be a matrix"); SWIG_fail; }
|
72
|
+
tmp=$input.matrix_value();
|
73
|
+
$1=tmp.data();
|
74
|
+
$2=tmp.rows();
|
75
|
+
$3=tmp.columns();
|
76
|
+
}
|
77
|
+
#endif
|
78
|
+
|
79
|
+
#ifdef SWIGPYTHON
|
80
|
+
%rename(__add__) *::operator+(const mglData&, const mglData&);
|
81
|
+
%rename(__sub__) *::operator-(const mglData&, const mglData &);
|
82
|
+
%rename(__mul__) *::operator*(const mglData &, float);
|
83
|
+
%rename(__div__) *::operator/(const mglData &, float);
|
84
|
+
|
85
|
+
// Get the NumPy typemaps
|
86
|
+
%include "numpy.i"
|
87
|
+
%init %{
|
88
|
+
import_array();
|
89
|
+
%}
|
90
|
+
%apply (double* IN_ARRAY1, int DIM1) {(const double* d, int size)};
|
91
|
+
%apply (int DIM1, double* IN_ARRAY1) {(int size, const double* d)};
|
92
|
+
%apply (double* IN_ARRAY2, int DIM1, int DIM2) {(const double* d, int rows, int cols)};
|
93
|
+
%apply (int DIM1, int DIM2, double* IN_ARRAY2) {(int rows, int cols, const double* d)};
|
94
|
+
%apply (double* IN_ARRAY3, int DIM1, int DIM2, int DIM3) {(const double* d, int rows, int cols, int slc)};
|
95
|
+
%apply (int DIM1, int DIM2, int DIM3, double* IN_ARRAY3) {(int rows, int cols, int slc, const double* d)};
|
96
|
+
#endif
|
97
|
+
|
98
|
+
#ifdef SWIGRUBY
|
99
|
+
%include "rubymgl.i"
|
100
|
+
#endif
|
101
|
+
|
102
|
+
%include "mgl2/config.h"
|
103
|
+
//%include "mgl2/define.h"
|
104
|
+
%include "mgl2/type.h"
|
105
|
+
%include "mgl2/data.h"
|
106
|
+
%include "mgl2/mgl.h"
|
107
|
+
//%include "mgl2/window.h"
|
108
|
+
%extend mglData
|
109
|
+
{
|
110
|
+
float __getitem__( int i) { return self->GetVal(i); };
|
111
|
+
float __paren( int i) { return self->GetVal(i); };
|
112
|
+
void __setitem__( int i, float y) { self->SetVal(y,i); };
|
113
|
+
void __paren_asgn( int i, float y) { self->SetVal(y,i); };
|
114
|
+
};
|
115
|
+
|
116
|
+
#ifdef SWIGRUBY
|
117
|
+
%{
|
118
|
+
extern "C" {
|
119
|
+
void Init_MathGL();
|
120
|
+
void Init_mathgl(){ Init_MathGL(); }
|
121
|
+
}
|
122
|
+
%}
|
123
|
+
#endif
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "mkmf"
|
2
|
+
|
3
|
+
# configure options:
|
4
|
+
# --with-mathgl-dir=path
|
5
|
+
# --with-mathgl-include=path
|
6
|
+
# --with-mathgl-lib=path
|
7
|
+
|
8
|
+
dir_config("mathgl")
|
9
|
+
|
10
|
+
#with_cppflags "-g -O0 -Wall"
|
11
|
+
|
12
|
+
exit unless have_header("mgl2/type.h")
|
13
|
+
exit unless have_header("mgl2/data.h")
|
14
|
+
exit unless have_header("mgl2/mgl.h")
|
15
|
+
exit unless have_header("mgl2/qt.h")
|
16
|
+
exit unless have_library("mgl")
|
17
|
+
exit unless have_library("mgl-qt")
|
18
|
+
$objs = ["qt.o"]
|
19
|
+
$CPPFLAGS.split.each{|x| $INCFLAGS+=" "+x if /^-I\S+$/=~x}
|
20
|
+
create_makefile("qt")
|
@@ -0,0 +1,172 @@
|
|
1
|
+
%include "typemaps.i"
|
2
|
+
%include "wchar.i"
|
3
|
+
|
4
|
+
//-- mglPoint --
|
5
|
+
%{
|
6
|
+
static mglPoint GetMglPoint(VALUE arg)
|
7
|
+
{
|
8
|
+
if (TYPE(arg)==T_ARRAY) {
|
9
|
+
mreal v[4] = {0,0,0,0};
|
10
|
+
long len = RARRAY_LEN(arg);
|
11
|
+
if (len < 1 || len > 4) {
|
12
|
+
rb_raise(rb_eArgError, "wrong size array");
|
13
|
+
}
|
14
|
+
for (long i=0; i<len; i++) {
|
15
|
+
VALUE x = rb_ary_entry(arg, i);
|
16
|
+
v[i] = NUM2DBL(x);
|
17
|
+
}
|
18
|
+
return mglPoint(v[0],v[1],v[2],v[3]);
|
19
|
+
} else {
|
20
|
+
void *vptr = 0;
|
21
|
+
SWIG_ConvertPtr(arg, &vptr, SWIGTYPE_p_mglPoint, 1);
|
22
|
+
return *reinterpret_cast< mglPoint * >(vptr);
|
23
|
+
}
|
24
|
+
}
|
25
|
+
%}
|
26
|
+
|
27
|
+
%typemap(typecheck, precedence=SWIG_TYPECHECK_DOUBLE_ARRAY)
|
28
|
+
mglPoint
|
29
|
+
{
|
30
|
+
$1 = (TYPE($input)==T_ARRAY);
|
31
|
+
if (!($1)) {
|
32
|
+
void *vptr = 0;
|
33
|
+
int res = SWIG_ConvertPtr($input, &vptr, SWIGTYPE_p_mglPoint, 0);
|
34
|
+
$1 = SWIG_CheckState(res);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
%typemap(in) mglPoint {
|
39
|
+
$1 = GetMglPoint($input);
|
40
|
+
}
|
41
|
+
|
42
|
+
|
43
|
+
//-- mglData --
|
44
|
+
%{
|
45
|
+
static mglData *GetMglData(VALUE arg)
|
46
|
+
{
|
47
|
+
if (TYPE(arg)==T_ARRAY) {
|
48
|
+
long len = RARRAY_LEN(arg);
|
49
|
+
mglData *d = new mglData(len);
|
50
|
+
for (long i=0; i<len; i++) {
|
51
|
+
VALUE x = rb_ary_entry(arg, i);
|
52
|
+
d->SetVal(NUM2DBL(x), i);
|
53
|
+
}
|
54
|
+
return d;
|
55
|
+
} else {
|
56
|
+
void *vptr = 0;
|
57
|
+
SWIG_ConvertPtr(arg, &vptr, SWIGTYPE_p_mglData, 1);
|
58
|
+
mglData *src = reinterpret_cast< mglData * >(vptr);
|
59
|
+
mglData *link = new mglData;
|
60
|
+
link->Link(*src);
|
61
|
+
return link;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
%}
|
65
|
+
|
66
|
+
%typemap(typecheck, precedence=SWIG_TYPECHECK_DOUBLE_ARRAY)
|
67
|
+
mglData&, mglDataA&
|
68
|
+
{
|
69
|
+
$1 = (TYPE($input)==T_ARRAY);
|
70
|
+
if (!($1)) {
|
71
|
+
void *vptr = 0;
|
72
|
+
int res = SWIG_ConvertPtr($input, &vptr, $1_descriptor, 0);
|
73
|
+
$1 = SWIG_CheckState(res);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
%typemap(in) mglData&, mglDataA& {
|
78
|
+
$1 = GetMglData($input);
|
79
|
+
}
|
80
|
+
|
81
|
+
%typemap(freearg) mglData&, mglDataA& {
|
82
|
+
delete $1;
|
83
|
+
}
|
84
|
+
|
85
|
+
%extend mglData {
|
86
|
+
mglData(const double *d, int size, int rows, int cols)
|
87
|
+
{
|
88
|
+
if (size != rows*cols) {
|
89
|
+
rb_raise(rb_eArgError,"array size(%d) mismatches (rows=%d)*(cols=%d)",
|
90
|
+
size, rows, cols);
|
91
|
+
}
|
92
|
+
return new mglData(d, rows, cols);
|
93
|
+
}
|
94
|
+
|
95
|
+
void Set(const double *A, long size)
|
96
|
+
{ $self->Set(A, size); }
|
97
|
+
|
98
|
+
void Set(const double *A, long size, long NX, long NY)
|
99
|
+
{
|
100
|
+
if (size != NX*NY) {
|
101
|
+
rb_raise(rb_eArgError,"array size(%ld) mismatches NX(%ld)*NY(%ld)",
|
102
|
+
size, NX, NY);
|
103
|
+
}
|
104
|
+
$self->Set(A, NX, NY);
|
105
|
+
}
|
106
|
+
|
107
|
+
void Set(const double *A, long size, long NX, long NY, long NZ)
|
108
|
+
{
|
109
|
+
if (size != NX*NY*NZ) {
|
110
|
+
rb_raise(rb_eArgError,"array size(%ld) mismatches (NX=%ld)*(NY=%ld)*(NZ=%ld)",
|
111
|
+
size, NX, NY, NZ);
|
112
|
+
}
|
113
|
+
$self->Set(A, NX, NY, NZ);
|
114
|
+
}
|
115
|
+
|
116
|
+
inline mglData operator*(const mglDataA &d)
|
117
|
+
{ mglData a(self); a*=d; return a; }
|
118
|
+
inline mglData operator*(double b)
|
119
|
+
{ mglData a(self); a*=b; return a; }
|
120
|
+
inline mglData operator-(const mglDataA &d)
|
121
|
+
{ mglData a(self); a-=d; return a; }
|
122
|
+
inline mglData operator-(double b)
|
123
|
+
{ mglData a(self); a-=b; return a; }
|
124
|
+
inline mglData operator+(const mglDataA &d)
|
125
|
+
{ mglData a(self); a+=d; return a; }
|
126
|
+
inline mglData operator+(double b)
|
127
|
+
{ mglData a(self); a+=b; return a; }
|
128
|
+
inline mglData operator/(const mglDataA &d)
|
129
|
+
{ mglData a(self); a/=d; return a; }
|
130
|
+
inline mglData operator/(double b)
|
131
|
+
{ mglData a(self); a/=b; return a; }
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
%apply (double* INPUT, int) {(const double* d, int size)};
|
136
|
+
%apply (double* INPUT, long) {(const double *A, long size)};
|
137
|
+
|
138
|
+
%typemap(typecheck, precedence=SWIG_TYPECHECK_DOUBLE_ARRAY)
|
139
|
+
(const double* d, int size), (const double *A, long size)
|
140
|
+
{
|
141
|
+
$1 = (TYPE($input)==T_ARRAY);
|
142
|
+
}
|
143
|
+
|
144
|
+
// Get a list of double
|
145
|
+
%typemap(in)
|
146
|
+
(const double* d, int size), (const double *A, long size)
|
147
|
+
{
|
148
|
+
if (TYPE($input)!=T_ARRAY) {
|
149
|
+
rb_raise(rb_eArgError,"argument is not Array");
|
150
|
+
}
|
151
|
+
//Check_Type($input, T_ARRAY);
|
152
|
+
$2 = RARRAY_LEN($input);
|
153
|
+
$1 = ALLOC_N(double,$2);
|
154
|
+
for (long i=0; i<$2; i++) {
|
155
|
+
$1[i] = NUM2DBL(RARRAY_PTR($input)[i]);
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
// Free the list
|
160
|
+
%typemap(freearg) (const double* d, int size), (const double *A, long size)
|
161
|
+
{
|
162
|
+
xfree($1);
|
163
|
+
}
|
164
|
+
|
165
|
+
%extend mglGraph {
|
166
|
+
inline void SetFunc(const char *EqX, int EqY=0, int EqZ=0, int EqA=0)
|
167
|
+
{ self->SetFunc(EqX, 0, 0, 0); }
|
168
|
+
inline void SetFunc(const char *EqX, const char *EqY, int EqZ=0, int EqA=0)
|
169
|
+
{ self->SetFunc(EqX, EqY, 0, 0); }
|
170
|
+
inline void SetFunc(const char *EqX, const char *EqY, const char *EqZ=NULL, int EqA=0)
|
171
|
+
{ self->SetFunc(EqX, EqY, EqZ, 0); }
|
172
|
+
}
|
data/mathgl.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mathgl/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mathgl"
|
8
|
+
spec.version = Mathgl::VERSION
|
9
|
+
spec.authors = ["Masahiro TANAKA"]
|
10
|
+
spec.email = ["masa16.tanaka@gmail.com"]
|
11
|
+
spec.description = %q{Ruby wrapper for MathGL - library for scientific data visualization}
|
12
|
+
spec.summary = %q{Ruby wrapper for MathGL - library for scientific data visualization}
|
13
|
+
spec.homepage = "https://github.com/masa16/ruby-mathgl"
|
14
|
+
spec.license = "LGPL"
|
15
|
+
spec.extensions = `git ls-files ext`.split($/).grep(/extconf/)
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,310 @@
|
|
1
|
+
class ConvSample
|
2
|
+
def initialize
|
3
|
+
@a = []
|
4
|
+
@n_indent = 0
|
5
|
+
end
|
6
|
+
|
7
|
+
def parse
|
8
|
+
@s = ARGF.read
|
9
|
+
@s.gsub!(/\/\/.*?$/m, "")
|
10
|
+
@i = -1
|
11
|
+
|
12
|
+
print "
|
13
|
+
libdir = File.expand_path(File.dirname(__FILE__))
|
14
|
+
require libdir+"/../ext/mathgl/mathgl.so"
|
15
|
+
require libdir+"/prepare"
|
16
|
+
|
17
|
+
def mgl_rnd
|
18
|
+
rand
|
19
|
+
end
|
20
|
+
|
21
|
+
def pow(a,b)
|
22
|
+
a**b
|
23
|
+
end
|
24
|
+
|
25
|
+
M_PI = Math::PI
|
26
|
+
NULL = nil
|
27
|
+
NAN = 0.0/0.0
|
28
|
+
|
29
|
+
procs = {}
|
30
|
+
"
|
31
|
+
@s.scan(/^(void (\w+)\(([^)]*)\)\s*^{(.*?)^})/m) do |f|
|
32
|
+
parse_func($1,$2,$3,$4)
|
33
|
+
end
|
34
|
+
print "
|
35
|
+
kind = 'png'
|
36
|
+
gr = MathGL::MglGraph.new
|
37
|
+
procs.each do |key,blk|
|
38
|
+
file = 'smpl_'+key+'.'+kind
|
39
|
+
if !File.exist?(file)
|
40
|
+
puts key
|
41
|
+
gr.set_size(960,720)
|
42
|
+
gr.vertex_color(false)
|
43
|
+
gr.compression(false)
|
44
|
+
gr.default_plot_param()
|
45
|
+
gr.clf
|
46
|
+
gr.instance_eval(&blk)
|
47
|
+
case kind
|
48
|
+
when 'eps'
|
49
|
+
gr.write_eps(file)
|
50
|
+
when 'png'
|
51
|
+
gr.write_png(file,'',false)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
"
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_func(all,name,args,body)
|
59
|
+
if /^smgl_(\w+)/ =~ name
|
60
|
+
name = $1
|
61
|
+
end
|
62
|
+
return if name == "parser"
|
63
|
+
|
64
|
+
all.split(/^/).each do |line|
|
65
|
+
print "# "+line
|
66
|
+
end
|
67
|
+
print "\n"
|
68
|
+
|
69
|
+
body.gsub!(/\bsplot1\(b\);/m, "b.Norm(-1,1,true);gr->Rotate(70,60);gr->Box();gr->Surf3(b);")
|
70
|
+
body.gsub!(/\bsplot2\(b\);/m, "b.Norm(-1,1,true);gr->Rotate(70,60);gr->Box();gr->Surf3(0.5,b);gr->Surf3(-0.5,b);")
|
71
|
+
body.gsub!(/'0'\+i/m,"'%d'%i")
|
72
|
+
|
73
|
+
re = /(?<pare>\(([^\(\)]++|\g<pare>)*+\))/
|
74
|
+
body.gsub!(re){ @i+=1; @a[@i]=$1; "@#{@i}@" }
|
75
|
+
re = /=\s*\{([^{}]*)\}/
|
76
|
+
body.gsub!(re){ @i+=1; @a[@i]="[#{$1}]"; "=@#{@i}@" }
|
77
|
+
|
78
|
+
if /mgls_/=~name
|
79
|
+
a = args.split(/,/).map{|x| /(\w+)$/=~x; "#{$1}=nil"}
|
80
|
+
print "def "+name+"("+a.join(",")+")\n"
|
81
|
+
else
|
82
|
+
print "procs['#{name}'] = proc do\n"
|
83
|
+
end
|
84
|
+
indent_up
|
85
|
+
parse_body(body)
|
86
|
+
indent_down
|
87
|
+
print "end\n\n"
|
88
|
+
end
|
89
|
+
|
90
|
+
def restore_fix(s)
|
91
|
+
s = s.gsub(/(\w+)?@(\d+)@/){ ($1||"")+fix_arg($1,@a[$2.to_i]) }
|
92
|
+
s.gsub!(/\bmgl([A-Z]\w*\()/){ 'MathGL.'+fix_name('Mgl'+$1) }
|
93
|
+
s.gsub!(/\bin\b/,"inp")
|
94
|
+
s.gsub!(/\bL"/,'"')
|
95
|
+
s.gsub!(/\b(\d+\.)(?![\w.])/){|x| x+'0'}
|
96
|
+
s.sub!(/\.a\[([^\]]+)\]\s*=\s*(.*)/, '.set_val(\2,\1)')
|
97
|
+
s.sub!(/putsw/,"#putsw")
|
98
|
+
s.gsub!(/\bgr\./,'')
|
99
|
+
s += "='%d'%i" if /\s*txt\[1\]=stl\[1\]/=~s
|
100
|
+
s
|
101
|
+
end
|
102
|
+
|
103
|
+
def restore(s)
|
104
|
+
s.gsub(/@(\d+)@/){@a[$1.to_i]}
|
105
|
+
end
|
106
|
+
|
107
|
+
def fix_arg(func,a)
|
108
|
+
a = a.gsub(/"\s*\n\s*"/,"")
|
109
|
+
para = a.sub!(/^\((.*)\)$/,'\1')
|
110
|
+
re = /mglPoint(?<pare>\(([^\(\)]++|\g<pare>)*+\))/
|
111
|
+
a.gsub!(re){|x| x.sub(/^mglPoint\((.*)\)$/,'[\1]')}
|
112
|
+
re = /mglData(?<pare>\(([^\(\)]++|\g<pare>)*+\))/
|
113
|
+
a.gsub!(re){|x|
|
114
|
+
x.sub(/^mglData\((.*)\)$/){|x|
|
115
|
+
c = $1.split(/,/)
|
116
|
+
if c.size == 2
|
117
|
+
c = [c[-1]]
|
118
|
+
else
|
119
|
+
c = [c[-1]]+c[0..-2]
|
120
|
+
end
|
121
|
+
c = c.join(",")
|
122
|
+
"MathGL::MglData.new(#{c})"
|
123
|
+
}
|
124
|
+
}
|
125
|
+
a.gsub!(/&(\w+)/, '\1')
|
126
|
+
a.gsub!(/\b([a-z]\w*\.)(\w+)\(/) do
|
127
|
+
var,meth = $1,$2
|
128
|
+
var+fix_name(meth)+'('
|
129
|
+
end
|
130
|
+
b = a.split(",")
|
131
|
+
case func
|
132
|
+
when "set_func"
|
133
|
+
b = b.map{|y| y=="0" ? '""' : y}
|
134
|
+
when /dens_?/
|
135
|
+
b[1] = '""' if b[1]=="0"
|
136
|
+
when /^mgls_prepare/
|
137
|
+
b = b.map{|y| y=="0" ? 'nil' : y}
|
138
|
+
end
|
139
|
+
a = b.join(",")
|
140
|
+
a = "("+a+")" if para
|
141
|
+
a
|
142
|
+
end
|
143
|
+
|
144
|
+
def indent
|
145
|
+
" " * @n_indent
|
146
|
+
end
|
147
|
+
|
148
|
+
def indent_up
|
149
|
+
@n_indent += 2
|
150
|
+
end
|
151
|
+
|
152
|
+
def indent_down
|
153
|
+
@n_indent -= 2
|
154
|
+
end
|
155
|
+
|
156
|
+
def puts_asis(a)
|
157
|
+
Kernel.puts indent+a
|
158
|
+
end
|
159
|
+
|
160
|
+
def puts(a)
|
161
|
+
Kernel.puts restore_fix(indent+a)
|
162
|
+
end
|
163
|
+
|
164
|
+
def fix_name(x)
|
165
|
+
x = x.gsub(/(?<=[a-z])(?=[A-Z])/,"_").downcase
|
166
|
+
x.gsub!(/(?<=[a-z])(?=[0-9]+[a-z])/i,"_")
|
167
|
+
x
|
168
|
+
end
|
169
|
+
|
170
|
+
def parse_arg(x)
|
171
|
+
x
|
172
|
+
end
|
173
|
+
|
174
|
+
def parse_body(body)
|
175
|
+
body.scan(/([^;{}]*?)(;|\{|\})/m) do |x|
|
176
|
+
line, term = x
|
177
|
+
line.strip!
|
178
|
+
#p [line,term]
|
179
|
+
case line
|
180
|
+
when /^for@.*d\.a\[i\] = hypot@/
|
181
|
+
puts_asis "for j in 0...a.ny"
|
182
|
+
puts_asis " for i in 0...a.nx"
|
183
|
+
puts_asis " d.set_val(Math.hypot(a.get_val(i,j),b.get_val(i,j)), i,j)"
|
184
|
+
puts_asis " end"
|
185
|
+
puts_asis "end"
|
186
|
+
when /^if\s*@(\d+)@\s*(.*)/
|
187
|
+
i,s = $1.to_i,$2
|
188
|
+
@a[i].gsub!(/\bmini\b/,"$mini")
|
189
|
+
@a[i].gsub!(/^\((.*)\)$/,'\1')
|
190
|
+
puts "if @#{i}@"
|
191
|
+
s.strip!
|
192
|
+
if s != ""
|
193
|
+
indent_up
|
194
|
+
parse_body(s+";")
|
195
|
+
indent_down
|
196
|
+
puts "end"
|
197
|
+
end
|
198
|
+
when /^for\s*@(\d+)@\s*(.*)/m
|
199
|
+
j,s = $1,$2||""
|
200
|
+
if /(int )?(\w+)=([^;]+);(\w+)<([^;]+);(\w+)\+\+/ =~ @a[j.to_i]
|
201
|
+
i,b,n = $2,$3,$5
|
202
|
+
print indent+"for #{i} in #{b}...#{n}\n"
|
203
|
+
s.strip!
|
204
|
+
case s
|
205
|
+
when /^for\b/
|
206
|
+
#indent_up
|
207
|
+
parse_body(s+"{")
|
208
|
+
indent_down
|
209
|
+
puts "end"
|
210
|
+
when ""
|
211
|
+
else
|
212
|
+
indent_up
|
213
|
+
parse_line(s)
|
214
|
+
indent_down
|
215
|
+
puts "end"
|
216
|
+
end
|
217
|
+
else
|
218
|
+
puts line
|
219
|
+
end
|
220
|
+
when /^\s*$/
|
221
|
+
if term != "}" and term != ""
|
222
|
+
puts
|
223
|
+
end
|
224
|
+
else
|
225
|
+
parse_line(line)
|
226
|
+
end
|
227
|
+
#
|
228
|
+
case term
|
229
|
+
when /\{/
|
230
|
+
indent_up
|
231
|
+
when /\}/
|
232
|
+
indent_down
|
233
|
+
puts "end"
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def parse_line(line)
|
239
|
+
line.gsub!(/(a?sinh?|a?cosh?|a?tanh?|hypot|exp|log)@/, 'Math.\1@')
|
240
|
+
line.gsub!(/(\w+)(?:\.|->)(\w+)(@(\d+)@)?/) do |x|
|
241
|
+
v,m,a = $1,$2,$3||""
|
242
|
+
"#{v}.#{fix_name(m)}#{a}"
|
243
|
+
end
|
244
|
+
case line
|
245
|
+
when /^mgl(\w+)\s+([^;]+)/
|
246
|
+
klass, vars = "Mgl"+$1, $2
|
247
|
+
vars.scan(/([^,]+)/) do |vardef|
|
248
|
+
vd = vardef[0].strip
|
249
|
+
case vd
|
250
|
+
when /^\*\s*(\w+)\s*=\s*new\s+mgl(\w+)(@\d+@)?/
|
251
|
+
v,c,a = $1, "MathGL::Mgl"+$2, $3||""
|
252
|
+
puts "#{v} = #{c}.new#{a}"
|
253
|
+
when /^\*?\s*(\w+)\s*=\s*(.*)/
|
254
|
+
v,d = $1, $2
|
255
|
+
puts "#{v} = #{d}"
|
256
|
+
when /^\s*(\w+)(@(\d+)@)?/
|
257
|
+
v,a,i = $1,$2,$3
|
258
|
+
if a
|
259
|
+
i = i.to_i
|
260
|
+
if klass == "MglData"
|
261
|
+
if /\(((\d+),)?(\d+),([a-z]+)\)/ =~ @a[i]
|
262
|
+
if $1
|
263
|
+
@a[i] = "(#{$4},#{$2},#{$3})"
|
264
|
+
else
|
265
|
+
@a[i] = "(#{$4})"
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
puts "#{v} = MathGL::#{klass}.new#{a}"
|
270
|
+
else
|
271
|
+
puts "#{v} = MathGL::#{klass}.new"
|
272
|
+
end
|
273
|
+
else
|
274
|
+
puts vd
|
275
|
+
end
|
276
|
+
end
|
277
|
+
when /^\s*(int|register long|double) ([^;]+)/
|
278
|
+
$2.split(',').each do |v|
|
279
|
+
case v
|
280
|
+
when /(\w+)(\[\d*\])?\s*(=\s*([^,]+))/
|
281
|
+
if $3
|
282
|
+
puts "#{$1} = #{$4}"
|
283
|
+
elsif $2
|
284
|
+
puts "#{$1} = []"
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
when /^(const)?\s*char ([^;]+)/
|
289
|
+
args = $2+","
|
290
|
+
args.scan(/(\*)?(\w+)(\[\d*\])?\s*=\s*([^,]+?),/) do
|
291
|
+
puts "#{$2} = #{$4}"
|
292
|
+
end
|
293
|
+
when /\bgr.(\w+)(@\d+@)/
|
294
|
+
meth,args = $1,$2
|
295
|
+
puts meth+args
|
296
|
+
when /,/
|
297
|
+
line.split(',').each do |v|
|
298
|
+
puts v.strip
|
299
|
+
end
|
300
|
+
when /([^=]+)=([^=]+)/
|
301
|
+
x,y = $1,$2
|
302
|
+
puts parse_arg(x)+"="+parse_arg(y)
|
303
|
+
else
|
304
|
+
puts line
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
end
|
309
|
+
|
310
|
+
ConvSample.new.parse
|