Search in sources :

Example 6 with RubyFloat

use of org.jruby.RubyFloat in project propane by ruby-processing.

the class Vec3 method op_mul.

/**
 * @param context ThreadContext
 * @param scalar IRubyObject
 * @return new Vec3 object (ruby)
 */
@JRubyMethod(name = "*", required = 1)
public IRubyObject op_mul(ThreadContext context, IRubyObject scalar) {
    Ruby runtime = context.runtime;
    double multi = scalar instanceof RubyFloat ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
    return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[] { runtime.newFloat(jx * multi), runtime.newFloat(jy * multi), runtime.newFloat(jz * multi) });
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 7 with RubyFloat

use of org.jruby.RubyFloat in project propane by ruby-processing.

the class Vec3 method toVertexUV.

/**
 * Sends this Vec3D as a processing vertex uv
 *
 * @param context ThreadContext
 * @param args IRubyObject[]
 */
@JRubyMethod(name = "to_vertex_uv", rest = true)
public void toVertexUV(ThreadContext context, IRubyObject... args) {
    int count = args.length;
    double u = 0;
    double v = 0;
    if (count == 3) {
        u = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
        v = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
    }
    if (count == 2) {
        Vec2 texture = (Vec2) args[1].toJava(Vec2.class);
        u = texture.javax();
        v = texture.javay();
    }
    JRender renderer = (JRender) args[0].toJava(JRender.class);
    renderer.vertex(jx, jy, jz, u, v);
}
Also used : Vec2(monkstone.vecmath.vec2.Vec2) RubyFloat(org.jruby.RubyFloat) JRender(monkstone.vecmath.JRender) RubyFixnum(org.jruby.RubyFixnum) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 8 with RubyFloat

use of org.jruby.RubyFloat in project propane by ruby-processing.

the class MathToolModule method constrainedMap.

/**
 * @param context ThreadContext
 * @param recv IRubyObject
 * @param args array of RubyRange (must be be numeric)
 * @return mapped value RubyFloat
 */
@JRubyMethod(name = "constrained_map", rest = true, module = true)
public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
    double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
    RubyRange r1 = (RubyRange) args[1];
    RubyRange r2 = (RubyRange) args[2];
    double first1 = r1.first(context) instanceof RubyFloat ? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue();
    double first2 = r2.first(context) instanceof RubyFloat ? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue();
    double last1 = r1.last(context) instanceof RubyFloat ? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue();
    double last2 = r2.last(context) instanceof RubyFloat ? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue();
    double max = Math.max(first1, last1);
    double min = Math.min(first1, last1);
    if (value < min) {
        return mapMt(context, min, first1, last1, first2, last2);
    }
    if (value > max) {
        return mapMt(context, max, first1, last1, first2, last2);
    }
    return mapMt(context, value, first1, last1, first2, last2);
}
Also used : RubyRange(org.jruby.RubyRange) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 9 with RubyFloat

use of org.jruby.RubyFloat in project propane by ruby-processing.

the class Vec2 method op_div.

/**
 * @param context ThreadContext
 * @param other IRubyObject scalar
 * @return new Vec2D object (ruby)
 */
@JRubyMethod(name = "/", required = 1)
public IRubyObject op_div(ThreadContext context, IRubyObject other) {
    Ruby runtime = context.runtime;
    double scalar = (other instanceof RubyFloat) ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue();
    if (Math.abs(scalar) < Vec2.EPSILON) {
        return this;
    }
    return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[] { runtime.newFloat(jx / scalar), runtime.newFloat(jy / scalar) });
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 10 with RubyFloat

use of org.jruby.RubyFloat in project propane by ruby-processing.

the class Vec2 method lerp.

/**
 * @param context ThreadContext
 * @param args IRubyObject[]
 * @return as a new Vec2 object (ruby)
 */
@JRubyMethod(name = "lerp", rest = true)
public IRubyObject lerp(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;
    if (args.length != 2) {
        throw runtime.newSyntaxError("Check syntax");
    }
    Vec2 vec = (Vec2) args[0].toJava(Vec2.class);
    double scalar = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
    assert (scalar >= 0 && scalar < 1.0) : "Lerp value " + scalar + " out of range 0..1.0";
    return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[] { runtime.newFloat(jx + (vec.jx - jx) * scalar), runtime.newFloat(jy + (vec.jy - jy) * scalar) });
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

RubyFloat (org.jruby.RubyFloat)13 JRubyMethod (org.jruby.anno.JRubyMethod)13 Ruby (org.jruby.Ruby)9 RubyRange (org.jruby.RubyRange)2 JRender (monkstone.vecmath.JRender)1 Vec2 (monkstone.vecmath.vec2.Vec2)1 RubyFixnum (org.jruby.RubyFixnum)1 IRubyObject (org.jruby.runtime.builtin.IRubyObject)1