toxi.geom.Triangle3D Java Examples

The following examples show how to use toxi.geom.Triangle3D. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AlignBoxToTriFace.java    From haxademic with MIT License 6 votes vote down vote up
void randomize() {
	// create random triangle
	tri=new Triangle3D(
			Vec3D.randomVector().scale(100), 
			Vec3D.randomVector().scale(100), 
			Vec3D.randomVector().scale(100)
			);
	// create box mesh around origin
	mesh = (TriangleMesh)new AABB(50).toMesh();
	// get triangle normal
	Vec3D n=tri.computeNormal();
	// rotate mesh such that the +Z axis is aligned with the triangle normal
	mesh.pointTowards(n);
	// move box in the normal direction 100 units relative from the triangle center
	mesh.translate(tri.computeCentroid().add(n.scale(100)));
}
 
Example #2
Source File: Terrain.java    From toxiclibs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Computes the 3D position (with elevation) and normal vector at the given
 * 2D location in the terrain. The position is in scaled world coordinates
 * based on the given terrain scale. The returned data is encapsulated in a
 * {@link toxi.geom.IsectData3D} instance.
 * 
 * @param x
 * @param z
 * @return intersection data parcel
 */
public IsectData3D intersectAtPoint(float x, float z) {
    float xx = x / scale.x + width * 0.5f;
    float zz = z / scale.y + depth * 0.5f;
    IsectData3D isec = new IsectData3D();
    if (xx >= 0 && xx < width && zz >= 0 && zz < depth) {
        int x2 = (int) MathUtils.min(xx + 1, width - 1);
        int z2 = (int) MathUtils.min(zz + 1, depth - 1);
        Vec3D a = getVertexAtCell((int) xx, (int) zz);
        Vec3D b = getVertexAtCell(x2, (int) zz);
        Vec3D c = getVertexAtCell(x2, z2);
        Vec3D d = getVertexAtCell((int) xx, z2);
        Ray3D r = new Ray3D(new Vec3D(x, 10000, z), new Vec3D(0, -1, 0));
        TriangleIntersector i = new TriangleIntersector(new Triangle3D(a,
                b, d));
        if (i.intersectsRay(r)) {
            isec = i.getIntersectionData();
        } else {
            i.setTriangle(new Triangle3D(b, c, d));
            i.intersectsRay(r);
            isec = i.getIntersectionData();
        }
    }
    return isec;
}
 
Example #3
Source File: ToxiclibsSupport.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public final void triangle(Triangle3D tri, boolean isFullShape) {
    if (isFullShape) {
        gfx.beginShape(PConstants.TRIANGLES);
    }
    Vec3D n = tri.computeNormal();
    gfx.normal(n.x, n.y, n.z);
    gfx.vertex(tri.a.x, tri.a.y, tri.a.z);
    gfx.vertex(tri.b.x, tri.b.y, tri.b.z);
    gfx.vertex(tri.c.x, tri.c.y, tri.c.z);
    if (isFullShape) {
        gfx.endShape();
    }
}
 
Example #4
Source File: TriangleMesh.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public boolean intersectsRay(Ray3D ray) {
    Triangle3D tri = intersector.getTriangle();
    for (Face f : faces) {
        tri.set(f.a, f.b, f.c);
        if (intersector.intersectsRay(ray)) {
            return true;
        }
    }
    return false;
}
 
Example #5
Source File: TriangleMesh.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Triangle3D perforateFace(Face f, float size) {
    Vec3D centroid = f.getCentroid();
    float d = 1 - size;
    Vec3D a2 = f.a.interpolateTo(centroid, d);
    Vec3D b2 = f.b.interpolateTo(centroid, d);
    Vec3D c2 = f.c.interpolateTo(centroid, d);
    removeFace(f);
    addFace(f.a, b2, a2);
    addFace(f.a, f.b, b2);
    addFace(f.b, c2, b2);
    addFace(f.b, f.c, c2);
    addFace(f.c, a2, c2);
    addFace(f.c, f.a, a2);
    return new Triangle3D(a2, b2, c2);
}
 
Example #6
Source File: Demo_KinectV1_2dConnections.java    From haxademic with MIT License 5 votes vote down vote up
public void draw3PointsTriangle( Vec3D point1, Vec3D point2, Vec3D point3 ) {
		// draw a line - currently disabled from noStroke()
		if( point1 == null || point2 == null || point3 == null ) return; 
		
		// draw triangles
		Triangle3D tri = new Triangle3D(point1, point2, point3); 
		Toxiclibs.instance(p).toxi.triangle( tri );

		// draw mesh
//		WETriangleMesh mesh = new WETriangleMesh();
//		mesh.addFace( point1, point2, point3 );
//		
//		SubdivisionStrategy subdiv = new TriSubdivision();
//		mesh.subdivide( subdiv );
//		mesh.subdivide( subdiv );
//		toxi.mesh( mesh );
		
		// draw lines
//		toxi.line( new Line3D( point1, point2 ) );
//		toxi.line( new Line3D( point2, point3 ) );
//		toxi.line( new Line3D( point3, point1 ) );		
		
		// draw lines into texture
//		_texture.beginDraw();
//		_texture.beginShape(P.TRIANGLES);
//		_texture.vertex( point1.x, point1.y, point1.z );
//		_texture.vertex( point2.x, point2.y, point2.z );
//		_texture.vertex( point3.x, point3.y, point3.z );
//		_texture.endShape();
//		_texture.endDraw();
	}
 
Example #7
Source File: JAXBGeomTest.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void save() {
    try {
        JAXBGeomTest test = new JAXBGeomTest();
        test.box = new AABB();
        test.plane = new Plane();
        test.quat = new Quaternion(0, Vec3D.X_AXIS);
        test.ray = new Ray3D();
        test.rect = new Rect(0, 0, 100, 200);
        test.sphere = new Sphere();
        test.tri = new Triangle3D(new Vec3D(), new Vec3D(), new Vec3D());
        List<Vec2D> points2d = new ArrayList<Vec2D>();
        points2d.add(new Vec2D());
        points2d.add(new Vec2D());
        points2d.add(new Vec2D());
        points2d.add(new Vec2D());
        test.spline2d = new Spline2D(points2d);
        List<Vec3D> points = new ArrayList<Vec3D>();
        points.add(new Vec3D());
        points.add(new Vec3D());
        points.add(new Vec3D());
        points.add(new Vec3D());
        test.spline3d = new Spline3D(points);
        JAXBContext context = JAXBContext.newInstance(JAXBGeomTest.class);
        File file = new File(XML_FILE);
        context.createMarshaller().marshal(test, file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: IndexedTriangleMesh.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public IsectData3D intersectsRay(Ray3D ray) {
    TriangleIntersector intersector = new TriangleIntersector();
    Triangle3D tri = intersector.getTriangle();
    Vec3D[] v = null;
    for (AttributedFace f : faces) {
        v = getFaceVertices(f, v);
        tri.set(v[0], v[1], v[2]);
        if (intersector.intersectsRay(ray)) {
            return intersector.getIntersectionData();
        }
    }
    return null;
}
 
Example #9
Source File: AABBTest.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testAABBTri() {
    AABB box = new AABB(new Vec3D(), new Vec3D(100, 100, 100));
    Vec3D a = new Vec3D(-90, 0, 0);
    Vec3D b = new Vec3D(-110, -200, 0);
    Vec3D c = new Vec3D(-110, 200, 0);
    Triangle3D tri = new Triangle3D(a, b, c);
    System.out.println(box.intersectsTriangle(tri));
}
 
Example #10
Source File: TriangleTest.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testNormal() {
    Vec3D a = new Vec3D(0, 100, 0);
    Vec3D b = new Vec3D(100, 0, 0);
    Vec3D c = new Vec3D(-100, -100, 0);
    Triangle3D t = new Triangle3D(a, b, c);
    ReadonlyVec3D n = t.computeNormal();
    assertTrue("normal wrong", n.equals(new Vec3D(0, 0, 1)));
}
 
Example #11
Source File: WEMeshTest.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSplitEdge() {
    WingedEdge e = ((WEVertex) m.vertices.get(new Vec3D())).edges.get(1);
    m.splitEdge(e, new MidpointSubdivision());
    assertEquals(4, m.faces.size());
    assertEquals(8, m.edges.size());
    m.computeVertexNormals();
    for (Face f : m.faces) {
        System.out.println(Triangle3D.isClockwiseInXY(f.a, f.b, f.c) + " "
                + f);
    }
    assertEquals(3, ((WEVertex) m.faces.get(0).a).edges.size());
    assertEquals(3, ((WEVertex) m.faces.get(0).b).edges.size());
    assertEquals(4, ((WEVertex) m.faces.get(0).c).edges.size());
}
 
Example #12
Source File: TriangleTest.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testBarycentric() {
    Vec3D a = new Vec3D(-100, -100, 0);
    Vec3D c = new Vec3D(100, 0, 0);
    Vec3D b = new Vec3D(-100, 100, 0);
    Triangle3D t = new Triangle3D(a, b, c);
    assertTrue(a.equalsWithTolerance(t.fromBarycentric(t.toBarycentric(a)),
            0.01f));
    assertTrue(b.equalsWithTolerance(t.fromBarycentric(t.toBarycentric(b)),
            0.01f));
    assertTrue(c.equalsWithTolerance(t.fromBarycentric(t.toBarycentric(c)),
            0.01f));
}
 
Example #13
Source File: TriangleTest.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testCentroid() {
    Vec3D a = new Vec3D(100, 0, 0);
    Vec3D b = new Vec3D(0, 100, 0);
    Vec3D c = new Vec3D(0, 0, 100);
    Triangle3D t = new Triangle3D(a, b, c);
    ReadonlyVec3D centroid = t.computeCentroid();
    assertTrue("incorrect centroid",
            centroid.equals(new Vec3D(100, 100, 100).scaleSelf(1f / 3)));
}
 
Example #14
Source File: TriangleTest.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testClockwise() {
    Vec3D a = new Vec3D(0, 100, 0);
    Vec3D b = new Vec3D(100, 0, -50);
    Vec3D c = new Vec3D(-100, -100, 100);
    assertTrue("not clockwiseXY", Triangle3D.isClockwiseInXY(a, b, c));
    assertTrue("not clockwiseXZ", Triangle3D.isClockwiseInXY(a, b, c));
    assertTrue("not clockwiseYZ", Triangle3D.isClockwiseInXY(a, b, c));
}
 
Example #15
Source File: TriangleTest.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testContainment() {
    Vec3D a = new Vec3D(100, 0, 0);
    Vec3D b = new Vec3D(0, 100, 0);
    Vec3D c = new Vec3D(0, 0, 100);
    Triangle3D t = new Triangle3D(a, b, c);
    assertTrue(t.containsPoint(a));
    assertTrue(t.containsPoint(b));
    assertTrue(t.containsPoint(c));
    assertTrue(t.containsPoint(t.computeCentroid()));
    assertFalse(t.containsPoint(a.add(0.1f, 0, 0)));
    t.flipVertexOrder();
    assertTrue(t.containsPoint(t.computeCentroid()));
}
 
Example #16
Source File: PlaneTest.java    From toxiclibs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testContainment() {
    Triangle3D t = new Triangle3D(new Vec3D(-100, 0, 0), new Vec3D(0, 0,
            -100), new Vec3D(0, 0, 100));
    Plane pl = new Plane(t.computeCentroid(), t.computeNormal());
}
 
Example #17
Source File: TriangleTest.java    From toxiclibs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testEquilateral() {
    Triangle3D t = Triangle3D.createEquilateralFrom(new Vec3D(-100, 0, 0),
            new Vec3D(100, 0, 0));

}
 
Example #18
Source File: PlaneCreator.java    From PapARt with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void computePlane() {
    Triangle3D tri = new Triangle3D(points[0],
            points[1],
            points[2]);
    this.plane = new Plane(tri);
}
 
Example #19
Source File: IndexedTriangleMesh.java    From toxiclibs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Triangle3D getFaceAsTriangle(AttributedFace f) {
    Vec3D[] verts = getFaceVertices(f, null);
    return new Triangle3D(verts[0], verts[1], verts[2]);
}
 
Example #20
Source File: ToxiclibsSupport.java    From toxiclibs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public final void triangle(Triangle3D tri) {
    triangle(tri, true);
}
 
Example #21
Source File: Face.java    From toxiclibs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Creates a generic {@link Triangle3D} instance using this face's vertices.
 * The new instance is made up of copies of the original vertices and
 * manipulating them will not impact the originals.
 * 
 * @return triangle copy of this mesh face
 */
public Triangle3D toTriangle() {
    return new Triangle3D(a.copy(), b.copy(), c.copy());
}