首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否可以在处理过程中使用jcsg库?

是否可以在处理过程中使用jcsg库?
EN

Stack Overflow用户
提问于 2019-07-12 03:27:13
回答 1查看 525关注 0票数 4

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-12 06:48:13

从单独的处理中可以跳过的循环很少,但是是的,您可以在处理过程中使用任何Java库。(只需将库.jar拖到保存的草图上即可)

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

要做到这一点,您需要Gradle。如果您使用过Android / Android / IntelliJ /等等,您可以从头开始安装它,也可以使用系统中的现有安装。

正如自述文件所提到的,在OSX/Linux/等上从每个库文件夹运行:

代码语言:javascript
运行
复制
bash gradlew assemble

在Windows上运行:

代码语言:javascript
运行
复制
gradlew assemble

在我的例子中,我使用了Android附带的Gradle安装,我在我的mac上安装了它:

代码语言:javascript
运行
复制
bash /Applications/IDEsAndEditors/Android\ Studio.app/Contents/plugins/android/lib/templates/gradle/wrapper/gradlew assemble

一旦编译了.jar文件,就可以简单地将它们放到保存的处理草图中。这将创建一个code文件夹。在这个阶段,您可以使用草图中的库。

(提示:转到Processing > Preferences,并使用Ctrl+Space启用代码完成功能,以便更容易地查看可用的方法和属性(IDE,如eclipse/IntelliJ/NetBeans/等)

我做了一个快速测试,但是JCSG保存的OBJ文件不能被处理的内置PShape OBJ加载程序解析。

代码语言:javascript
运行
复制
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格式和不同的处理库,否则将访问顶点并在处理过程中直接绘制它们,同时考虑到JSCG与处理之间的单位/比例不同:

代码语言:javascript
运行
复制
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);
}

您可以下载上面的草图(带有预编译库) 这里 (以及JCSG生成的文档这里)。一定要查看库的文档/源代码,以便进行更高级的使用。

Update:为了提高效率您可以使用createShape()在安装过程中创建一组PShape对象,然后简单地在draw()中呈现(与我之前反复遍历所有多边形和顶点的示例不同):

代码语言:javascript
运行
复制
// 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);
}

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56999816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档