Search in sources :

Example 6 with R3

use of suite.math.R3 in project suite by stupidsing.

the class Sphere method c.

public static RtObject c(R3 center, double radius, Material material) {
    R3 radiusRange = new R3(radius, radius, radius);
    R3 min = R3.sub(center, radiusRange);
    R3 max = R3.add(center, radiusRange);
    return new BoundingBox(min, max, new Sphere(center, radius, material));
}
Also used : R3(suite.math.R3)

Example 7 with R3

use of suite.math.R3 in project suite by stupidsing.

the class Parallelogram method c.

public static RtObject c(R3 origin, R3 axis0, R3 axis1, Material material) {
    R3 v0 = R3.add(origin, axis0);
    R3 v1 = R3.add(origin, axis1);
    R3 v2 = R3.add(origin, R3.add(axis0, axis1));
    Parallelogram parallelogram = new Parallelogram(origin, axis0, axis1, material);
    return BoundingBox.bound(List.of(origin, v0, v1, v2), parallelogram);
}
Also used : R3(suite.math.R3)

Example 8 with R3

use of suite.math.R3 in project suite by stupidsing.

the class Plane method hit.

@Override
public List<RayHit> hit(Ray ray) {
    double denum = R3.dot(normal, ray.dir);
    double adv;
    if (MathUtil.epsilon < Math.abs(denum))
        adv = (originIndex - R3.dot(normal, ray.startPoint)) / denum;
    else
        // treats as not-hit
        adv = -1d;
    double advance = adv;
    if (RayTracer.negligibleAdvance < advance) {
        RayHit rayHit = new RayHit() {

            public double advance() {
                return advance;
            }

            public RayIntersection intersection() {
                R3 hitPoint = ray.hitPoint(advance);
                return new RayIntersection() {

                    public R3 hitPoint() {
                        return hitPoint;
                    }

                    public R3 normal() {
                        return normal;
                    }

                    public Material material() {
                        return material;
                    }
                };
            }
        };
        return List.of(rayHit);
    } else
        return List.of();
}
Also used : R3(suite.math.R3) RayHit(suite.rt.RayTracer.RayHit) RayIntersection(suite.rt.RayTracer.RayIntersection)

Example 9 with R3

use of suite.math.R3 in project suite by stupidsing.

the class Render method renderPixels.

public Image renderPixels(int width, int height, IntInt_Obj<R3> f) {
    int nThreads = Constants.nThreads;
    int[] txs = Ints_.toArray(nThreads + 1, i -> width * i / nThreads);
    R3[][] pixels = new R3[width][height];
    List<Thread> threads = // 
    Ints_.range(// 
    nThreads).map(t -> Thread_.newThread(() -> {
        for (int x = txs[t]; x < txs[t + 1]; x++) for (int y = 0; y < height; y++) pixels[x][y] = f.apply(x, y);
    })).toList();
    Thread_.startJoin(threads);
    Image image = new Image(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) {
        R3 pixel = limit(pixels[x][y]);
        image.setRGB(x, y, new Color(pixel.x, pixel.y, pixel.z).getRGB());
    }
    return image;
}
Also used : Color(java.awt.Color) Friends.min(suite.util.Friends.min) LogUtil(suite.os.LogUtil) BufferedImage(java.awt.image.BufferedImage) Constants(suite.Constants) R3(suite.math.R3) IntInt_Obj(suite.primitive.IntInt_Obj) Thread_(suite.util.Thread_) Friends.max(suite.util.Friends.max) List(java.util.List) Floats_(suite.primitive.Floats_) JLabel(javax.swing.JLabel) ImageIcon(javax.swing.ImageIcon) Ints_(suite.primitive.Ints_) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) BiFun(suite.util.FunUtil2.BiFun) R3(suite.math.R3) Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage)

Example 10 with R3

use of suite.math.R3 in project suite by stupidsing.

the class Render method render.

public Image render(int width, int height, BiFun<Float, R3> f) {
    float scale = 1f / max(width, height);
    int centerX = width / 2, centerY = height / 2;
    float[] xs = Floats_.toArray(width + 1, x -> (x - centerX) * scale);
    float[] ys = Floats_.toArray(height + 1, y -> (y - centerY) * scale);
    return renderPixels(width, height, (IntInt_Obj<R3>) (x, y) -> {
        R3 color;
        try {
            color = f.apply(xs[x], ys[y]);
        } catch (Exception ex) {
            LogUtil.error(new RuntimeException("at (" + x + ", " + y + ")", ex));
            color = new R3(1d, 1d, 1d);
        }
        return color;
    });
}
Also used : Color(java.awt.Color) Friends.min(suite.util.Friends.min) LogUtil(suite.os.LogUtil) BufferedImage(java.awt.image.BufferedImage) Constants(suite.Constants) R3(suite.math.R3) IntInt_Obj(suite.primitive.IntInt_Obj) Thread_(suite.util.Thread_) Friends.max(suite.util.Friends.max) List(java.util.List) Floats_(suite.primitive.Floats_) JLabel(javax.swing.JLabel) ImageIcon(javax.swing.ImageIcon) Ints_(suite.primitive.Ints_) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) BiFun(suite.util.FunUtil2.BiFun) R3(suite.math.R3)

Aggregations

R3 (suite.math.R3)10 RayHit (suite.rt.RayTracer.RayHit)3 BorderLayout (java.awt.BorderLayout)2 Color (java.awt.Color)2 BufferedImage (java.awt.image.BufferedImage)2 List (java.util.List)2 ImageIcon (javax.swing.ImageIcon)2 JFrame (javax.swing.JFrame)2 JLabel (javax.swing.JLabel)2 Constants (suite.Constants)2 LogUtil (suite.os.LogUtil)2 Floats_ (suite.primitive.Floats_)2 IntInt_Obj (suite.primitive.IntInt_Obj)2 Ints_ (suite.primitive.Ints_)2 Friends.max (suite.util.Friends.max)2 Friends.min (suite.util.Friends.min)2 BiFun (suite.util.FunUtil2.BiFun)2 Thread_ (suite.util.Thread_)2 ArrayList (java.util.ArrayList)1 RayIntersection (suite.rt.RayTracer.RayIntersection)1