2
2
* The purpose of this tool is to allow JRubyArt users to use an alternative
3
3
* to processing.org map, lerp and norm methods in their sketches and to implement
4
4
* JRubyArt convenenience method grid(width, height, stepW, stepH) { |x, y| do stuff }
5
- * Copyright (c) 2015-21 Martin Prout. This tool is free software; you can
5
+ * Copyright (c) 2015-20 Martin Prout. This tool is free software; you can
6
6
* redistribute it and/or modify it under the terms of the GNU Lesser General
7
7
* Public License as published by the Free Software Foundation; either version
8
8
* 2.1 of the License, or (at your option) any later version.
@@ -37,22 +37,6 @@ public static void createMathToolModule(Ruby runtime) {
37
37
RubyModule mtModule = runtime .defineModule ("MathTool" );
38
38
mtModule .defineAnnotatedMethods (MathToolModule .class );
39
39
}
40
-
41
- /**
42
- * Utility method
43
- *
44
- * @param obj
45
- * @return parse float value of object or zero
46
- */
47
- private static double jvalue (IRubyObject obj ) {
48
- if (obj instanceof RubyFloat rubyFloat ) {
49
- return rubyFloat .getValue ();
50
- }
51
- if (obj instanceof RubyFixnum rubyFixnum ) {
52
- return rubyFixnum .getDoubleValue ();
53
- }
54
- return 0 ;
55
- }
56
40
57
41
/**
58
42
*
@@ -63,13 +47,18 @@ private static double jvalue(IRubyObject obj) {
63
47
*/
64
48
@ JRubyMethod (name = "map1d" , rest = true , module = true )
65
49
public static IRubyObject mapOneD (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
66
- double value = jvalue (args [0 ]);
50
+ double value = args [0 ] instanceof RubyFloat
51
+ ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
67
52
RubyRange r1 = (RubyRange ) args [1 ];
68
53
RubyRange r2 = (RubyRange ) args [2 ];
69
- double first1 = jvalue (r1 .first (context ));
70
- double first2 = jvalue (r2 .first (context ));
71
- double last1 = jvalue (r1 .last (context ));
72
- double last2 = jvalue (r2 .last (context ));
54
+ double first1 = r1 .first (context ) instanceof RubyFloat
55
+ ? ((RubyFloat ) r1 .first (context )).getValue () : ((RubyFixnum ) r1 .first (context )).getDoubleValue ();
56
+ double first2 = r2 .first (context ) instanceof RubyFloat
57
+ ? ((RubyFloat ) r2 .first (context )).getValue () : ((RubyFixnum ) r2 .first (context )).getDoubleValue ();
58
+ double last1 = r1 .last (context ) instanceof RubyFloat
59
+ ? ((RubyFloat ) r1 .last (context )).getValue () : ((RubyFixnum ) r1 .last (context )).getDoubleValue ();
60
+ double last2 = r2 .last (context ) instanceof RubyFloat
61
+ ? ((RubyFloat ) r2 .last (context )).getValue () : ((RubyFixnum ) r2 .last (context )).getDoubleValue ();
73
62
return mapMt (context , value , first1 , last1 , first2 , last2 );
74
63
}
75
64
@@ -82,13 +71,17 @@ public static IRubyObject mapOneD(ThreadContext context, IRubyObject recv, IRuby
82
71
*/
83
72
@ JRubyMethod (name = "constrained_map" , rest = true , module = true )
84
73
public static IRubyObject constrainedMap (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
85
- double value = jvalue ( args [0 ]);
74
+ double value = args [ 0 ] instanceof RubyFloat ? (( RubyFloat ) args [0 ]). getValue () : (( RubyFixnum ) args [ 0 ]). getDoubleValue ( );
86
75
RubyRange r1 = (RubyRange ) args [1 ];
87
76
RubyRange r2 = (RubyRange ) args [2 ];
88
- double first1 = jvalue (r1 .first (context ));
89
- double first2 = jvalue (r2 .first (context ));
90
- double last1 = jvalue (r1 .last (context ));
91
- double last2 = jvalue (r2 .last (context ));
77
+ double first1 = r1 .first (context ) instanceof RubyFloat
78
+ ? ((RubyFloat ) r1 .first (context )).getValue () : ((RubyFixnum ) r1 .first (context )).getDoubleValue ();
79
+ double first2 = r2 .first (context ) instanceof RubyFloat
80
+ ? ((RubyFloat ) r2 .first (context )).getValue () : ((RubyFixnum ) r2 .first (context )).getDoubleValue ();
81
+ double last1 = r1 .last (context ) instanceof RubyFloat
82
+ ? ((RubyFloat ) r1 .last (context )).getValue () : ((RubyFixnum ) r1 .last (context )).getDoubleValue ();
83
+ double last2 = r2 .last (context ) instanceof RubyFloat
84
+ ? ((RubyFloat ) r2 .last (context )).getValue () : ((RubyFixnum ) r2 .last (context )).getDoubleValue ();
92
85
double max = Math .max (first1 , last1 );
93
86
double min = Math .min (first1 , last1 );
94
87
if (value < min ) {
@@ -109,11 +102,11 @@ public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv
109
102
*/
110
103
@ JRubyMethod (name = "p5map" , rest = true , module = true )
111
104
public static IRubyObject mapProcessing (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
112
- double value = jvalue ( args [0 ]);
113
- double first1 = jvalue ( args [1 ]);
114
- double first2 = jvalue ( args [3 ]);
115
- double last1 = jvalue ( args [2 ]);
116
- double last2 = jvalue ( args [4 ]);
105
+ double value = args [ 0 ] instanceof RubyFloat ? (( RubyFloat ) args [0 ]). getValue () : (( RubyFixnum ) args [ 0 ]). getDoubleValue ( );
106
+ double first1 = args [ 1 ] instanceof RubyFloat ? (( RubyFloat ) args [1 ]). getValue () : (( RubyFixnum ) args [ 1 ]). getDoubleValue ( );
107
+ double first2 = args [ 3 ] instanceof RubyFloat ? (( RubyFloat ) args [3 ]). getValue () : (( RubyFixnum ) args [ 3 ]). getDoubleValue ( );
108
+ double last1 = args [ 2 ] instanceof RubyFloat ? (( RubyFloat ) args [2 ]). getValue () : (( RubyFixnum ) args [ 2 ]). getDoubleValue ( );
109
+ double last2 = args [ 4 ] instanceof RubyFloat ? (( RubyFloat ) args [4 ]). getValue () : (( RubyFixnum ) args [ 4 ]). getDoubleValue ( );
117
110
return mapMt (context , value , first1 , last1 , first2 , last2 );
118
111
}
119
112
@@ -128,9 +121,9 @@ public static IRubyObject mapProcessing(ThreadContext context, IRubyObject recv,
128
121
*/
129
122
@ JRubyMethod (name = "lerp" , rest = true , module = true )
130
123
public static IRubyObject lerpP (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
131
- double start = jvalue ( args [0 ]);
132
- double stop = jvalue ( args [1 ]);
133
- double amount = jvalue ( args [2 ]);
124
+ double start = args [ 0 ] instanceof RubyFloat ? (( RubyFloat ) args [0 ]). getValue () : (( RubyFixnum ) args [ 0 ]). getDoubleValue ( );
125
+ double stop = args [ 1 ] instanceof RubyFloat ? (( RubyFloat ) args [1 ]). getValue () : (( RubyFixnum ) args [ 1 ]). getDoubleValue ( );
126
+ double amount = args [ 2 ] instanceof RubyFloat ? (( RubyFloat ) args [2 ]). getValue () : (( RubyFixnum ) args [ 2 ]). getDoubleValue ( );
134
127
if (amount <= 0 ) {
135
128
return args [0 ];
136
129
}
@@ -152,9 +145,9 @@ public static IRubyObject lerpP(ThreadContext context, IRubyObject recv, IRubyOb
152
145
*/
153
146
@ JRubyMethod (name = "norm" , rest = true , module = true )
154
147
public static IRubyObject normP (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
155
- double value = jvalue ( args [0 ]);
156
- double start = jvalue ( args [1 ]);
157
- double stop = jvalue ( args [2 ]);
148
+ double value = args [ 0 ] instanceof RubyFloat ? (( RubyFloat ) args [0 ]). getValue () : (( RubyFixnum ) args [ 0 ]). getDoubleValue ( );
149
+ double start = args [ 1 ] instanceof RubyFloat ? (( RubyFloat ) args [1 ]). getValue () : (( RubyFixnum ) args [ 1 ]). getDoubleValue ( );
150
+ double stop = args [ 2 ] instanceof RubyFloat ? (( RubyFloat ) args [2 ]). getValue () : (( RubyFixnum ) args [ 2 ]). getDoubleValue ( );
158
151
return mapMt (context , value , start , stop , 0 , 1.0 );
159
152
}
160
153
@@ -169,9 +162,10 @@ public static IRubyObject normP(ThreadContext context, IRubyObject recv, IRubyOb
169
162
*/
170
163
@ JRubyMethod (name = "norm_strict" , rest = true , module = true )
171
164
public static IRubyObject norm_strict (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
172
- double value = jvalue (args [0 ]);
173
- double start = jvalue (args [1 ]);
174
- double stop = jvalue (args [2 ]);
165
+
166
+ double value = args [0 ] instanceof RubyFloat ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
167
+ double start = args [1 ] instanceof RubyFloat ? ((RubyFloat ) args [1 ]).getValue () : ((RubyFixnum ) args [1 ]).getDoubleValue ();
168
+ double stop = args [2 ] instanceof RubyFloat ? ((RubyFloat ) args [2 ]).getValue () : ((RubyFixnum ) args [2 ]).getDoubleValue ();
175
169
double max = Math .max (start , stop );
176
170
double min = Math .min (start , stop );
177
171
if (value < min ) {
0 commit comments