是否可以将 jcsg 库与处理一起使用?
Is it possible to use jcsg library with Processing?
Processing 是一个创意编码平台 - 语言、IDE 和生态系统 - 由 Processing 社区在 Processing Foundation https://processing.org 的支持下维护。处理 Java 模式通常可以受益于 Java 库中的代码。
JCSG 是 Java 基于 BSP 的 CSG(构造立体几何)实现 https://github.com/miho/JCSG。
仅从 Processing 就可以跳过几个环节,但是是的,您可以在 Processing 中使用任何 Java 库。 (只需将库 .jar 拖到保存的草图之上)
首先,您需要将 JCSG .jar library along with the VVecMath .jar 库编译为 运行 示例代码。
要做到这一点,您需要 Gradle。如果您使用了 Android SDK / Android Studio / IntelliJ / etc.
,则可以从头开始安装它或使用系统中的现有安装
正如 readme 提到的那样,在 OSX/Linux/etc 上。来自每个库文件夹 运行:
bash gradlew assemble
在 Windows 运行:
gradlew assemble
在我的例子中,我使用了 Android Studio 附带的 Gradle 安装,我的 mac:
bash /Applications/IDEsAndEditors/Android\ Studio.app/Contents/plugins/android/lib/templates/gradle/wrapper/gradlew assemble
编译 .jar 文件后,您只需将它们放入保存的 Processing sketch 中即可。这将创建一个 code
文件夹。在此阶段,您可以使用该草图中的库。
(提示:转到 Processing > Preferences 并使用 Ctrl+Space 启用 Code completion 以便更容易地查看可用的方法和属性(像 eclipse/IntelliJ/NetBeans/etc 这样的 IDE。默认情况下这样做))
我做了一个快速测试,但是 JCSG 保存的 OBJ 文件无法被 Processing 的 built-in PShape OBJ 加载器解析。
import java.nio.file.Paths;
PShape csgResult;
void setup(){
size(900,900,P3D);
// we use cube and sphere as base geometries
CSG cube = new Cube(2).toCSG();
CSG sphere = new Sphere(1.25).toCSG();
// perform union, difference and intersection
CSG cubePlusSphere = cube.union(sphere);
CSG cubeMinusSphere = cube.difference(sphere);
CSG cubeIntersectSphere = cube.intersect(sphere);
// translate geometries to prevent overlapping
CSG union = cube.
union(sphere.transformed(Transform.unity().translateX(3))).
union(cubePlusSphere.transformed(Transform.unity().translateX(6))).
union(cubeMinusSphere.transformed(Transform.unity().translateX(9))).
union(cubeIntersectSphere.transformed(Transform.unity().translateX(12)));
// save union as stl
try {
FileUtil.write(
Paths.get(sketchPath("sample.obj")),
union.toObjString()
);
} catch (IOException ex) {
ex.printStackTrace();
}
//load shape into Processing
csgResult = loadShape(sketchPath("sample.obj"));
}
void draw(){
background(0);
translate(width * 0.5, height * 0.5,0);
scale(sin(frameCount * 0.1) * 100);
if(csgResult != null){
shape(csgResult);
}
}
我做了测试,顶点在那里,面丢失了:
感觉可以试试STL格式和不同的Processing库加载,
否则访问顶点并直接在 Processing 中绘制它们,考虑到 units/scales JSCG 和 Processing 之间的不同:
import java.nio.file.Paths;
CSG union;
void setup(){
size(900,900,P3D);
stroke(255);
//strokeWeight(3);
// we use cube and sphere as base geometries
CSG cube = new Cube(2).toCSG();
CSG sphere = new Sphere(1.25).toCSG();
// perform union, difference and intersection
CSG cubePlusSphere = cube.union(sphere);
CSG cubeMinusSphere = cube.difference(sphere);
CSG cubeIntersectSphere = cube.intersect(sphere);
// translate geometries to prevent overlapping
union = cube.
union(sphere.transformed(Transform.unity().translateX(3))).
union(cubePlusSphere.transformed(Transform.unity().translateX(6))).
union(cubeMinusSphere.transformed(Transform.unity().translateX(9))).
union(cubeIntersectSphere.transformed(Transform.unity().translateX(12)));
}
void drawCSG(CSG mesh,float scale){
beginShape(POINTS);
for(Polygon p : mesh.getPolygons()){
for(Vertex v : p.vertices){
vertex((float)v.pos.getX() * scale,(float)v.pos.getY() * scale,(float)v.pos.getZ() * scale);
}
}
endShape();
}
void draw(){
background(0);
translate(width * 0.5, height * 0.5,0);
rotateY(map(mouseX,0,width,-PI,PI));
rotateX(map(mouseY,0,height,PI,-PI));
drawCSG(union,sin(frameCount * 0.01) * 100);
}
您可以下载上面的草图(带有 pre-compiled 库)here (and the JCSG generated documentation here)。请务必阅读库的 documentation/source 代码以获得更高级的用法。
更新: 为了提高效率,您可以在设置中使用 createShape()
to create a group of PShape
对象一次,然后简单地在 draw()
中渲染(与我之前的示例相反遍历所有的多边形和顶点):
// the PShape reference which will contain the converted
PShape csgResult;
void setup(){
size(900,900,P3D);
noStroke();
// JCSG sample code:
// we use cube and sphere as base geometries
CSG cube = new Cube(2).toCSG();
CSG sphere = new Sphere(1.25).toCSG();
// perform union, difference and intersection
CSG cubePlusSphere = cube.union(sphere);
CSG cubeMinusSphere = cube.difference(sphere);
CSG cubeIntersectSphere = cube.intersect(sphere);
// translate geometries to prevent overlapping
CSG union = cube.
union(sphere.transformed(Transform.unity().translateX(3))).
union(cubePlusSphere.transformed(Transform.unity().translateX(6))).
union(cubeMinusSphere.transformed(Transform.unity().translateX(9))).
union(cubeIntersectSphere.transformed(Transform.unity().translateX(12)));
// translate merged geometry back by half the total translation to pivot around centre
union = union.transformed(Transform.unity().translateX(-6));
// Convert CSG to PShape -> Note: CSG units are small so we scale them up so the shapes are visible in Processing
csgResult = CSGToPShape(union,45);
}
// re-usable function to convert a CSG mesh to a Processing PShape
PShape CSGToPShape(CSG mesh,float scale){
// allocate a PShape group
PShape csgResult = createShape(GROUP);
// for each CSG polygon (Note: these can have 3,4 or more vertices)
for(Polygon p : mesh.getPolygons()){
// make a child PShape
PShape polyShape = createShape();
// begin setting vertices to it
polyShape.beginShape();
// for each vertex in the polygon
for(Vertex v : p.vertices){
// add each (scaled) polygon vertex
polyShape.vertex((float)v.pos.getX() * scale,(float)v.pos.getY() * scale,(float)v.pos.getZ() * scale);
}
// finish this polygon
polyShape.endShape();
//append the child PShape to the parent
csgResult.addChild(polyShape);
}
return csgResult;
}
void draw(){
background(0);
lights();
translate(width * 0.5, height * 0.5,0);
rotateY(map(mouseX,0,width,-PI,PI));
rotateX(map(mouseY,0,height,PI,-PI));
shape(csgResult);
}
Processing 是一个创意编码平台 - 语言、IDE 和生态系统 - 由 Processing 社区在 Processing Foundation https://processing.org 的支持下维护。处理 Java 模式通常可以受益于 Java 库中的代码。
JCSG 是 Java 基于 BSP 的 CSG(构造立体几何)实现 https://github.com/miho/JCSG。
仅从 Processing 就可以跳过几个环节,但是是的,您可以在 Processing 中使用任何 Java 库。 (只需将库 .jar 拖到保存的草图之上)
首先,您需要将 JCSG .jar library along with the VVecMath .jar 库编译为 运行 示例代码。
要做到这一点,您需要 Gradle。如果您使用了 Android SDK / Android Studio / IntelliJ / etc.
,则可以从头开始安装它或使用系统中的现有安装正如 readme 提到的那样,在 OSX/Linux/etc 上。来自每个库文件夹 运行:
bash gradlew assemble
在 Windows 运行:
gradlew assemble
在我的例子中,我使用了 Android Studio 附带的 Gradle 安装,我的 mac:
bash /Applications/IDEsAndEditors/Android\ Studio.app/Contents/plugins/android/lib/templates/gradle/wrapper/gradlew assemble
编译 .jar 文件后,您只需将它们放入保存的 Processing sketch 中即可。这将创建一个 code
文件夹。在此阶段,您可以使用该草图中的库。
(提示:转到 Processing > Preferences 并使用 Ctrl+Space 启用 Code completion 以便更容易地查看可用的方法和属性(像 eclipse/IntelliJ/NetBeans/etc 这样的 IDE。默认情况下这样做))
我做了一个快速测试,但是 JCSG 保存的 OBJ 文件无法被 Processing 的 built-in PShape OBJ 加载器解析。
import java.nio.file.Paths;
PShape csgResult;
void setup(){
size(900,900,P3D);
// we use cube and sphere as base geometries
CSG cube = new Cube(2).toCSG();
CSG sphere = new Sphere(1.25).toCSG();
// perform union, difference and intersection
CSG cubePlusSphere = cube.union(sphere);
CSG cubeMinusSphere = cube.difference(sphere);
CSG cubeIntersectSphere = cube.intersect(sphere);
// translate geometries to prevent overlapping
CSG union = cube.
union(sphere.transformed(Transform.unity().translateX(3))).
union(cubePlusSphere.transformed(Transform.unity().translateX(6))).
union(cubeMinusSphere.transformed(Transform.unity().translateX(9))).
union(cubeIntersectSphere.transformed(Transform.unity().translateX(12)));
// save union as stl
try {
FileUtil.write(
Paths.get(sketchPath("sample.obj")),
union.toObjString()
);
} catch (IOException ex) {
ex.printStackTrace();
}
//load shape into Processing
csgResult = loadShape(sketchPath("sample.obj"));
}
void draw(){
background(0);
translate(width * 0.5, height * 0.5,0);
scale(sin(frameCount * 0.1) * 100);
if(csgResult != null){
shape(csgResult);
}
}
我做了测试,顶点在那里,面丢失了:
感觉可以试试STL格式和不同的Processing库加载, 否则访问顶点并直接在 Processing 中绘制它们,考虑到 units/scales JSCG 和 Processing 之间的不同:
import java.nio.file.Paths;
CSG union;
void setup(){
size(900,900,P3D);
stroke(255);
//strokeWeight(3);
// we use cube and sphere as base geometries
CSG cube = new Cube(2).toCSG();
CSG sphere = new Sphere(1.25).toCSG();
// perform union, difference and intersection
CSG cubePlusSphere = cube.union(sphere);
CSG cubeMinusSphere = cube.difference(sphere);
CSG cubeIntersectSphere = cube.intersect(sphere);
// translate geometries to prevent overlapping
union = cube.
union(sphere.transformed(Transform.unity().translateX(3))).
union(cubePlusSphere.transformed(Transform.unity().translateX(6))).
union(cubeMinusSphere.transformed(Transform.unity().translateX(9))).
union(cubeIntersectSphere.transformed(Transform.unity().translateX(12)));
}
void drawCSG(CSG mesh,float scale){
beginShape(POINTS);
for(Polygon p : mesh.getPolygons()){
for(Vertex v : p.vertices){
vertex((float)v.pos.getX() * scale,(float)v.pos.getY() * scale,(float)v.pos.getZ() * scale);
}
}
endShape();
}
void draw(){
background(0);
translate(width * 0.5, height * 0.5,0);
rotateY(map(mouseX,0,width,-PI,PI));
rotateX(map(mouseY,0,height,PI,-PI));
drawCSG(union,sin(frameCount * 0.01) * 100);
}
您可以下载上面的草图(带有 pre-compiled 库)here (and the JCSG generated documentation here)。请务必阅读库的 documentation/source 代码以获得更高级的用法。
更新: 为了提高效率,您可以在设置中使用 createShape()
to create a group of PShape
对象一次,然后简单地在 draw()
中渲染(与我之前的示例相反遍历所有的多边形和顶点):
// the PShape reference which will contain the converted
PShape csgResult;
void setup(){
size(900,900,P3D);
noStroke();
// JCSG sample code:
// we use cube and sphere as base geometries
CSG cube = new Cube(2).toCSG();
CSG sphere = new Sphere(1.25).toCSG();
// perform union, difference and intersection
CSG cubePlusSphere = cube.union(sphere);
CSG cubeMinusSphere = cube.difference(sphere);
CSG cubeIntersectSphere = cube.intersect(sphere);
// translate geometries to prevent overlapping
CSG union = cube.
union(sphere.transformed(Transform.unity().translateX(3))).
union(cubePlusSphere.transformed(Transform.unity().translateX(6))).
union(cubeMinusSphere.transformed(Transform.unity().translateX(9))).
union(cubeIntersectSphere.transformed(Transform.unity().translateX(12)));
// translate merged geometry back by half the total translation to pivot around centre
union = union.transformed(Transform.unity().translateX(-6));
// Convert CSG to PShape -> Note: CSG units are small so we scale them up so the shapes are visible in Processing
csgResult = CSGToPShape(union,45);
}
// re-usable function to convert a CSG mesh to a Processing PShape
PShape CSGToPShape(CSG mesh,float scale){
// allocate a PShape group
PShape csgResult = createShape(GROUP);
// for each CSG polygon (Note: these can have 3,4 or more vertices)
for(Polygon p : mesh.getPolygons()){
// make a child PShape
PShape polyShape = createShape();
// begin setting vertices to it
polyShape.beginShape();
// for each vertex in the polygon
for(Vertex v : p.vertices){
// add each (scaled) polygon vertex
polyShape.vertex((float)v.pos.getX() * scale,(float)v.pos.getY() * scale,(float)v.pos.getZ() * scale);
}
// finish this polygon
polyShape.endShape();
//append the child PShape to the parent
csgResult.addChild(polyShape);
}
return csgResult;
}
void draw(){
background(0);
lights();
translate(width * 0.5, height * 0.5,0);
rotateY(map(mouseX,0,width,-PI,PI));
rotateX(map(mouseY,0,height,PI,-PI));
shape(csgResult);
}