i'm using jcsg library javafx.
i have meshview
objects want convert them csg
objects, there way achieve this?
the easiest way combine javafx.scene.shape.mesh
object csg one, providing have trianglemesh
converting triangular faces polygons (eu.mihosoft.vrl.v3d.polygon
).
once have csg object can perform regular operations on it, , can export meshview
instance.
the problem primitive shapes (box
, sphere
, ...) don't have access trianglemesh
. can go f(x)yz library , pick of available 3d shapes.
for example, let's use frustummesh
object.
you can create one:
frustummesh cone = new frustummesh(1,0.2,4,2);
and have access mesh: cone.getmesh()
.
now need convert trianglemesh
list<polygon>
. can create utility class:
public class mesh2csg { /** * loads csg trianglemesh. * @param mesh * @return csg * @throws ioexception if loading failed */ public static csg mesh2csg(meshview mesh) throws ioexception { return mesh2csg(mesh.getmesh()); } public static csg mesh2csg(mesh mesh) throws ioexception { list<polygon> polygons = new arraylist<>(); list<vector3d> vertices = new arraylist<>(); if(mesh instanceof trianglemesh){ // faces observablefacearray faces = ((trianglemesh)mesh).getfaces(); int[] f=new int[faces.size()]; faces.toarray(f); // vertices observablefloatarray points = ((trianglemesh)mesh).getpoints(); float[] p = new float[points.size()]; points.toarray(p); // convert faces polygons for(int i=0; i<faces.size()/6; i++){ int i0=f[6*i], i1=f[6*i+2], i2=f[6*i+4]; vertices.add(new vector3d(p[3*i0], p[3*i0+1], p[3*i0+2])); vertices.add(new vector3d(p[3*i1], p[3*i1+1], p[3*i1+2])); vertices.add(new vector3d(p[3*i2], p[3*i2+1], p[3*i2+2])); polygons.add(polygon.frompoints(vertices)); vertices = new arraylist<>(); } } return csg.frompolygons(new propertystorage(),polygons); } }
with method, can csg cone:
csg conecsg = mesh2csg.mesh2csg(cone.getmesh());
so can combine other csg forms:
csg cube = new cube(2).tocsg().color(color.red); csg union = cube.union(conecsg);
and javafx mesh view it:
meshview unionmesh = conecsg.tojavafxmesh().getasmeshviews().get(0);
this full sample class (providing have on classpath fxyzlib.jar , jcsg.jar dependencies):
public class fxyzjcsg extends application { private double mouseposx, mouseposy; private double mouseoldx, mouseoldy; private final rotate rotatex = new rotate(-20, rotate.x_axis); private final rotate rotatey = new rotate(-20, rotate.y_axis); @override public void start(stage primarystage) throws ioexception { frustummesh cone = new frustummesh(1,0.2,4,2); cone.setdrawmode(drawmode.line); cone.settexturemodenone(color.royalblue); csg conecsg = mesh2csg.mesh2csg(cone.getmesh()); csg cube = new cube(2).tocsg().color(color.red); csg union = cube.union(conecsg); meshview unionmesh = union.tojavafxmesh().getasmeshviews().get(0); // unionmesh.setdrawmode(drawmode.line); perspectivecamera camera = new perspectivecamera(true); camera.gettransforms().addall (rotatex, rotatey, new translate(0, 0, -10)); group root3d = new group(camera,unionmesh); subscene subscene = new subscene(root3d, 600, 400, true, sceneantialiasing.balanced); subscene.setfill(color.aquamarine); subscene.setcamera(camera); scene scene = new scene(new stackpane(subscene), 600, 400); scene.setonmousepressed(me -> { mouseoldx = me.getscenex(); mouseoldy = me.getsceney(); }); scene.setonmousedragged(me -> { mouseposx = me.getscenex(); mouseposy = me.getsceney(); rotatex.setangle(rotatex.getangle()-(mouseposy - mouseoldy)); rotatey.setangle(rotatey.getangle()+(mouseposx - mouseoldx)); mouseoldx = mouseposx; mouseoldy = mouseposy; }); primarystage.settitle("fxyz & jcsg - javafx 3d"); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }