可以在处理中使用jcsg库吗?

时间:2019-07-12 03:27:13

标签: java processing csg

Processing是一个创新的编码平台-语言,IDE和生态系统-由Processing社区在Processing Foundation https://processing.org的支持下维护。处理Java模式通常可以从Java库中的代码中受益。

JCSG是基于BSP的CSG(构造实体几何)https://github.com/miho/JCSG的Java实现。

1 个答案:

答案 0 :(得分:4)

仅来自Processing的过程很少,但是可以,您可以在Processing中使用任何Java库。 (只需将库.jar拖到保存的草图顶部即可)

首先,您需要编译JCSG .jar库和VVecMath .jar库以运行示例代码。

要这样做,您需要Gradle。如果您已使用Android SDK / Android Studio / IntelliJ / etc等,则可以从头开始安装它,也可以从系统中使用现有安装。

正如readme所述,在OSX / Linux / etc上。从每个库文件夹运行:

bash gradlew assemble

在Windows上运行:

gradlew assemble

在我的情况下,我使用了Mac上随附的Android Studio附带的Gradle安装:

bash /Applications/IDEsAndEditors/Android\ Studio.app/Contents/plugins/android/lib/templates/gradle/wrapper/gradlew assemble

一旦您编译了.jar文件,您只需将它们放入保存的Processing草图中即可。这将创建一个code文件夹。在此阶段,您可以使用该草图中的库。

(提示:转到 Processing> Preferences ,并使用Ctrl + Space启用代码完成,以便更轻松地查看可用的方法和属性(如eclipse / IntelliJ / NetBeans / etc会默认执行此操作))

我已经进行了快速测试,但是处理的内置PShape OBJ加载器无法解析JCSG保存的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);
  }
}

我确实进行了测试,并且顶点在那里,但由于以下原因缺少了面孔:

JCSG demo vertices

请尝试尝试使用STL格式以及要加载的其他处理库, 否则,请访问顶点并直接在“处理”中绘制它们,并考​​虑到JSCG和“处理”之间的单位/比例不同:

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);
}

JSCG vertex traversal

您可以下载以上草图(带有预编译的库)here(以及JCSG生成的文档here)。请务必阅读库的文档/源代码以获取更多高级用法。

更新:为了提高效率,您可以在设置时使用createShape()创建一组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);
}

JCSG rendered as PShape in Processing