Search in sources :

Example 11 with Point3D

use of org.fxyz.geometry.Point3D in project FXyzLib by Birdasaur.

the class CSGMesh method createCSGMesh.

private TriangleMesh createCSGMesh() {
    List<Vertex> vertices = new ArrayList<>();
    List<List<Integer>> indices = new ArrayList<>();
    listVertices.clear();
    primitive.getPolygons().forEach(p -> {
        List<Integer> polyIndices = new ArrayList<>();
        p.vertices.forEach(v -> {
            if (!vertices.contains(v)) {
                vertices.add(v);
                listVertices.add(new Point3D((float) v.pos.x, (float) v.pos.y, (float) v.pos.z));
                polyIndices.add(vertices.size());
            } else {
                polyIndices.add(vertices.indexOf(v) + 1);
            }
        });
        indices.add(polyIndices);
    });
    textureCoords = new float[] { 0f, 0f };
    listTextures.clear();
    listFaces.clear();
    indices.forEach(pVerts -> {
        int index1 = pVerts.get(0);
        for (int i = 0; i < pVerts.size() - 2; i++) {
            int index2 = pVerts.get(i + 1);
            int index3 = pVerts.get(i + 2);
            listTextures.add(new Face3(0, 0, 0));
            listFaces.add(new Face3(index1 - 1, index2 - 1, index3 - 1));
        }
    });
    int[] faceSmoothingGroups = new int[listFaces.size()];
    smoothingGroups = faceSmoothingGroups;
    return createMesh();
}
Also used : Vertex(eu.mihosoft.vrl.v3d.Vertex) Point3D(org.fxyz.geometry.Point3D) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Face3(org.fxyz.geometry.Face3)

Example 12 with Point3D

use of org.fxyz.geometry.Point3D in project FXyzLib by Birdasaur.

the class IcosahedronMesh method createSphere.

private TriangleMesh createSphere(int level, float diameter) {
    TriangleMesh m0 = null;
    if (level > 0) {
        m0 = createSphere(level - 1, diameter);
    }
    // read vertices from level-1
    if (level == 0) {
        points0 = baseVertices;
        numVertices = baseVertices.length / 3;
    } else if (m0 != null) {
        points0 = new float[numVertices * m0.getPointElementSize()];
        m0.getPoints().toArray(points0);
    }
    List<Point3D> points1 = IntStream.range(0, numVertices).mapToObj(i -> new Point3D(points0[3 * i], points0[3 * i + 1], points0[3 * i + 2])).collect(Collectors.toList());
    // read textures from level -1
    if (level == 0) {
        texCoord0 = baseTexCoords;
        numTexCoords = baseTexCoords.length / 2;
    } else if (m0 != null) {
        texCoord0 = new float[numTexCoords * m0.getTexCoordElementSize()];
        m0.getTexCoords().toArray(texCoord0);
    }
    texCoord1 = IntStream.range(0, numTexCoords).mapToObj(i -> new Point2D(texCoord0[2 * i], texCoord0[2 * i + 1])).collect(Collectors.toList());
    // read faces from level -1
    if (level == 0) {
        faces0 = IntStream.range(0, baseFaces.size() / 3).mapToObj(i -> IntStream.of(baseFaces.get(3 * i), baseTexture[3 * i], baseFaces.get(3 * i + 1), baseTexture[3 * i + 1], baseFaces.get(3 * i + 2), baseTexture[3 * i + 2])).flatMapToInt(i -> i).toArray();
        numFaces = baseFaces.size() / 3;
    } else if (m0 != null) {
        faces0 = new int[numFaces * m0.getFaceElementSize()];
        m0.getFaces().toArray(faces0);
    }
    List<Face3> faces1 = IntStream.range(0, numFaces).mapToObj(i -> new Face3(faces0[6 * i], faces0[6 * i + 2], faces0[6 * i + 4])).collect(Collectors.toList());
    index.set(points1.size());
    map.clear();
    listVertices.clear();
    listFaces.clear();
    listVertices.addAll(points1);
    faces1.forEach(face -> {
        int v1 = face.p0;
        int v2 = face.p1;
        int v3 = face.p2;
        if (level > 0) {
            int a = getMiddle(v1, points1.get(v1), v2, points1.get(v2));
            int b = getMiddle(v2, points1.get(v2), v3, points1.get(v3));
            int c = getMiddle(v3, points1.get(v3), v1, points1.get(v1));
            listFaces.add(new Face3(v1, a, c));
            listFaces.add(new Face3(v2, b, a));
            listFaces.add(new Face3(v3, c, b));
            listFaces.add(new Face3(a, b, c));
        } else {
            listFaces.add(new Face3(v1, v2, v3));
        }
    });
    map.clear();
    numVertices = listVertices.size();
    numFaces = listFaces.size();
    List<Face3> textures1;
    if (level == 0) {
        textures1 = IntStream.range(0, faces0.length / 6).mapToObj(i -> new Face3(faces0[6 * i + 1], faces0[6 * i + 3], faces0[6 * i + 5])).collect(Collectors.toList());
    } else {
        textures1 = listTextures.stream().map(t -> t).collect(Collectors.toList());
    }
    index.set(texCoord1.size());
    listTextures.clear();
    textures1.forEach(face -> {
        int v1 = face.p0;
        int v2 = face.p1;
        int v3 = face.p2;
        if (level > 0) {
            int a = getMiddle(v1, texCoord1.get(v1), v2, texCoord1.get(v2));
            int b = getMiddle(v2, texCoord1.get(v2), v3, texCoord1.get(v3));
            int c = getMiddle(v3, texCoord1.get(v3), v1, texCoord1.get(v1));
            listTextures.add(new Face3(v1, a, c));
            listTextures.add(new Face3(v2, b, a));
            listTextures.add(new Face3(v3, c, b));
            listTextures.add(new Face3(a, b, c));
        } else {
            listTextures.add(new Face3(v1, v2, v3));
        }
    });
    map.clear();
    texCoord0 = texCoord1.stream().flatMapToDouble(p -> DoubleStream.of(p.getX(), p.getY())).collect(() -> new FloatCollector(texCoord1.size() * 2), FloatCollector::add, FloatCollector::join).toArray();
    numTexCoords = texCoord0.length / 2;
    textureCoords = texCoord0;
    if (level == getLevel()) {
        areaMesh.setWidth(Math.PI * diameter);
        areaMesh.setHeight(Math.PI * diameter);
        rectMesh.setWidth((int) Math.sqrt(texCoord0.length));
        rectMesh.setHeight(texCoord0.length / ((int) Math.sqrt(texCoord0.length)));
    }
    return createMesh();
}
Also used : IntStream(java.util.stream.IntStream) SimpleFloatProperty(javafx.beans.property.SimpleFloatProperty) Arrays(java.util.Arrays) FloatCollector(org.fxyz.utils.FloatCollector) TextureType(org.fxyz.shapes.primitives.helper.TriangleMeshHelper.TextureType) HashMap(java.util.HashMap) DrawMode(javafx.scene.shape.DrawMode) Collectors(java.util.stream.Collectors) IntegerProperty(javafx.beans.property.IntegerProperty) CullFace(javafx.scene.shape.CullFace) TriangleMesh(javafx.scene.shape.TriangleMesh) DoubleStream(java.util.stream.DoubleStream) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DepthTest(javafx.scene.DepthTest) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) Face3(org.fxyz.geometry.Face3) Point2D(javafx.geometry.Point2D) FloatProperty(javafx.beans.property.FloatProperty) Point3D(org.fxyz.geometry.Point3D) TriangleMesh(javafx.scene.shape.TriangleMesh) Point2D(javafx.geometry.Point2D) Point3D(org.fxyz.geometry.Point3D) FloatCollector(org.fxyz.utils.FloatCollector) Face3(org.fxyz.geometry.Face3)

Example 13 with Point3D

use of org.fxyz.geometry.Point3D in project FXyzLib by Birdasaur.

the class CurvedSpringHelper method getKappa.

public double getKappa(double t) {
    // r'[t]
    Point3D dR = new Point3D((float) (-((R + r * Math.cos(h * t)) * Math.sin(t)) - h * r * Math.cos(t) * Math.sin(h * t)), (float) (Math.cos(t) * (R + r * Math.cos(h * t)) - h * r * Math.sin(t) * Math.sin(h * t)), (float) (h * r * Math.cos(h * t)));
    // || r'[t] ||
    float nT = dR.magnitude();
    // r''[t]
    Point3D ddR = new Point3D((float) (-(Math.cos(t) * (R + (1 + h * h) * r * Math.cos(h * t))) + 2 * h * r * Math.sin(t) * Math.sin(h * t)), (float) (-((R + (1 + h * h) * r * Math.cos(h * t)) * Math.sin(t)) - 2 * h * r * Math.cos(t) * Math.sin(h * t)), (float) (-(h * h * r * Math.sin(h * t))));
    // || r''[t]xr'[t] ||
    float nddRxdR = ddR.crossProduct(dR).magnitude();
    // kappa[t] = || r''[t]xr'[t] || / || r'[t] ||^3
    return nddRxdR / (float) Math.pow(nT, 3d);
}
Also used : Point3D(org.fxyz.geometry.Point3D)

Example 14 with Point3D

use of org.fxyz.geometry.Point3D in project FXyzLib by Birdasaur.

the class CurvedSpringHelper method getTrihedron.

/*
    t [0,<=2Pi]
    */
private Point3D[] getTrihedron(double t) {
    // r[t]
    Point3D vR = new Point3D((float) (Math.cos(t) * (R + r * Math.cos(h * t))), (float) ((R + r * Math.cos(h * t)) * Math.sin(t)), (float) (r * Math.sin(h * t)));
    // r'[t]
    Point3D dR = new Point3D((float) (-((R + r * Math.cos(h * t)) * Math.sin(t)) - h * r * Math.cos(t) * Math.sin(h * t)), (float) (Math.cos(t) * (R + r * Math.cos(h * t)) - h * r * Math.sin(t) * Math.sin(h * t)), (float) (h * r * Math.cos(h * t)));
    // || r'[t] ||
    float nT = dR.magnitude();
    // r''[t]
    Point3D ddR = new Point3D((float) (-(Math.cos(t) * (R + (1 + h * h) * r * Math.cos(h * t))) + 2 * h * r * Math.sin(t) * Math.sin(h * t)), (float) (-((R + (1 + h * h) * r * Math.cos(h * t)) * Math.sin(t)) - 2 * h * r * Math.cos(t) * Math.sin(h * t)), (float) (-(h * h * r * Math.sin(h * t))));
    // (|| r'[t] ||^2)'[t]
    float dn = (float) (-2 * h * r * (R + r * Math.cos(h * t)) * Math.sin(h * t));
    // T'[t]=r''[t]/||r't||-r'[t]*(|| r'[t] ||^2)'[t]/2/|| r'[t] ||^3
    Point3D dT = ddR.multiply(1f / nT).substract(dR.multiply(dn / ((float) (Math.pow(nT, 3d) * 2d))));
    // T[t]=r'[t]/||r'[t]||
    Point3D T = dR.normalize();
    // N[t]=T'[t]/||T'[t]||
    Point3D N = dT.normalize();
    // B[t]=T[t]xN[t]/||T[t]xN[t]||
    Point3D B = T.crossProduct(N).normalize();
    // R,N,B
    return new Point3D[] { vR, N, B };
}
Also used : Point3D(org.fxyz.geometry.Point3D)

Example 15 with Point3D

use of org.fxyz.geometry.Point3D in project FXyzLib by Birdasaur.

the class BezierHelper method getTrihedron.

/*
    t [0,1]
    */
private Point3D[] getTrihedron(double t) {
    if (ab == null || bc == null || cd == null) {
        preProcess();
    }
    // r[t]
    Point3D R = points.get(0).multiply((float) Math.pow(1d - t, 3)).add(points.get(1).multiply((float) (3d * Math.pow(1d - t, 2) * t)).add(points.get(2).multiply((float) (3d * (1d - t) * Math.pow(t, 2))).add(points.get(3).multiply((float) (Math.pow(t, 3))))));
    // r'[t]
    Point3D dR = ab.multiply((float) (3d * Math.pow(1d - t, 2))).add(bc.multiply((float) (6d * (1d - t) * t)).add(cd.multiply((float) (3d * Math.pow(t, 2)))));
    // || r'[t] ||
    float nT = dR.magnitude();
    // r''[t]
    Point3D ddR = abc.multiply((float) (6d * (1d - t))).add(bcd.multiply((float) (6d * t)));
    // (|| r'[t] ||^2)'[t]
    float dn = (float) (2 * (6 * bc.x * (1 - 2 * t) - 6 * ab.x * (1 - t) + 6 * cd.x * t) * (3 * ab.x * Math.pow(1 - t, 2) + 6 * bc.x * (1 - t) * t + 3 * cd.x * Math.pow(t, 2)) + 2 * (6 * bc.y * (1 - 2 * t) - 6 * ab.y * (1 - t) + 6 * cd.y * t) * (3 * ab.y * Math.pow(1 - t, 2) + 6 * bc.y * (1 - t) * t + 3 * cd.y * Math.pow(t, 2)) + 2 * (6 * bc.z * (1 - 2 * t) - 6 * ab.z * (1 - t) + 6 * cd.z * t) * (3 * ab.z * Math.pow(1 - t, 2) + 6 * bc.z * (1 - t) * t + 3 * cd.z * Math.pow(t, 2)));
    // T'[t]=r''[t]/||r't||-r'[t]*(|| r'[t] ||^2)'[t]/2/|| r'[t] ||^3
    Point3D dT = ddR.multiply(1f / nT).substract(dR.multiply(dn / ((float) (Math.pow(nT, 3d) * 2d))));
    // T[t]=r'[t]/||r'[t]||
    Point3D T = dR.normalize();
    // N[t]=T'[t]/||T'[t]||
    Point3D N = dT.normalize();
    // B[t]=T[t]xN[t]/||T[t]xN[t]||
    Point3D B = T.crossProduct(N).normalize();
    // R,N,B
    return new Point3D[] { R, N, B };
}
Also used : Point3D(org.fxyz.geometry.Point3D)

Aggregations

Point3D (org.fxyz.geometry.Point3D)48 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 List (java.util.List)12 Face3 (org.fxyz.geometry.Face3)12 Collectors (java.util.stream.Collectors)10 IntStream (java.util.stream.IntStream)10 Group (javafx.scene.Group)10 Translate (javafx.scene.transform.Translate)10 ArrayList (java.util.ArrayList)9 Point2D (javafx.geometry.Point2D)9 PerspectiveCamera (javafx.scene.PerspectiveCamera)9 Scene (javafx.scene.Scene)9 KeyCode (javafx.scene.input.KeyCode)9 TriangleMesh (javafx.scene.shape.TriangleMesh)9 Rotate (javafx.scene.transform.Rotate)9 PointLight (javafx.scene.PointLight)8 MouseEvent (javafx.scene.input.MouseEvent)8 DoubleProperty (javafx.beans.property.DoubleProperty)7 IntegerProperty (javafx.beans.property.IntegerProperty)7 SimpleDoubleProperty (javafx.beans.property.SimpleDoubleProperty)7