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-20 Martin Prout. This tool is free software; you can
5
+ * Copyright (c) 2015-21 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,6 +37,22 @@ 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
+ }
40
56
41
57
/**
42
58
*
@@ -47,18 +63,13 @@ public static void createMathToolModule(Ruby runtime) {
47
63
*/
48
64
@ JRubyMethod (name = "map1d" , rest = true , module = true )
49
65
public static IRubyObject mapOneD (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
50
- double value = args [0 ] instanceof RubyFloat
51
- ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
66
+ double value = jvalue (args [0 ]);
52
67
RubyRange r1 = (RubyRange ) args [1 ];
53
68
RubyRange r2 = (RubyRange ) args [2 ];
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 ();
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 ));
62
73
return mapMt (context , value , first1 , last1 , first2 , last2 );
63
74
}
64
75
@@ -71,17 +82,13 @@ public static IRubyObject mapOneD(ThreadContext context, IRubyObject recv, IRuby
71
82
*/
72
83
@ JRubyMethod (name = "constrained_map" , rest = true , module = true )
73
84
public static IRubyObject constrainedMap (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
74
- double value = args [ 0 ] instanceof RubyFloat ? (( RubyFloat ) args [0 ]). getValue () : (( RubyFixnum ) args [ 0 ]). getDoubleValue ( );
85
+ double value = jvalue ( args [0 ]);
75
86
RubyRange r1 = (RubyRange ) args [1 ];
76
87
RubyRange r2 = (RubyRange ) args [2 ];
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 ();
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 ));
85
92
double max = Math .max (first1 , last1 );
86
93
double min = Math .min (first1 , last1 );
87
94
if (value < min ) {
@@ -102,11 +109,11 @@ public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv
102
109
*/
103
110
@ JRubyMethod (name = "p5map" , rest = true , module = true )
104
111
public static IRubyObject mapProcessing (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
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 ( );
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 ]);
110
117
return mapMt (context , value , first1 , last1 , first2 , last2 );
111
118
}
112
119
@@ -121,9 +128,9 @@ public static IRubyObject mapProcessing(ThreadContext context, IRubyObject recv,
121
128
*/
122
129
@ JRubyMethod (name = "lerp" , rest = true , module = true )
123
130
public static IRubyObject lerpP (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
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 ( );
131
+ double start = jvalue ( args [0 ]);
132
+ double stop = jvalue ( args [1 ]);
133
+ double amount = jvalue ( args [2 ]);
127
134
if (amount <= 0 ) {
128
135
return args [0 ];
129
136
}
@@ -145,9 +152,9 @@ public static IRubyObject lerpP(ThreadContext context, IRubyObject recv, IRubyOb
145
152
*/
146
153
@ JRubyMethod (name = "norm" , rest = true , module = true )
147
154
public static IRubyObject normP (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
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 ( );
155
+ double value = jvalue ( args [0 ]);
156
+ double start = jvalue ( args [1 ]);
157
+ double stop = jvalue ( args [2 ]);
151
158
return mapMt (context , value , start , stop , 0 , 1.0 );
152
159
}
153
160
@@ -162,10 +169,9 @@ public static IRubyObject normP(ThreadContext context, IRubyObject recv, IRubyOb
162
169
*/
163
170
@ JRubyMethod (name = "norm_strict" , rest = true , module = true )
164
171
public static IRubyObject norm_strict (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
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 ();
172
+ double value = jvalue (args [0 ]);
173
+ double start = jvalue (args [1 ]);
174
+ double stop = jvalue (args [2 ]);
169
175
double max = Math .max (start , stop );
170
176
double min = Math .min (start , stop );
171
177
if (value < min ) {
0 commit comments