te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - three.js calculate STL file mesh volume - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - three.js calculate STL file mesh volume - Stack Overflow

programmeradmin4浏览0评论

I have to calculate the volume of an STL file, I successfully got the sizes of the model with

var box = new THREE.Box3().setFromObject( mesh );
var sizes = box.getSize();

but I just can't wrap my head around the concept of calculating it. I load the model with

var loader = new THREE.STLLoader();
loader.load(stlFileURL, function ( geometry ) {});

Can someone help me out and point me in the right direction? I'm doing it with javascript.

I have to calculate the volume of an STL file, I successfully got the sizes of the model with

var box = new THREE.Box3().setFromObject( mesh );
var sizes = box.getSize();

but I just can't wrap my head around the concept of calculating it. I load the model with

var loader = new THREE.STLLoader();
loader.load(stlFileURL, function ( geometry ) {});

Can someone help me out and point me in the right direction? I'm doing it with javascript.

Share Improve this question asked Nov 24, 2018 at 14:37 JohnJohn 1633 silver badges11 bronze badges 3
  • Are you trying to estimate how much filament for 3d printing? – manthrax Commented Nov 24, 2018 at 17:03
  • Have a look at this SO thread. – prisoner849 Commented Nov 24, 2018 at 17:58
  • @manthrax exactly – John Commented Nov 24, 2018 at 20:22
Add a ment  | 

3 Answers 3

Reset to default 14

You can find it with the algorithm from my ment.

In the code snippet, the volume is puted without scaling.

Also, I've added a simple check that the algorithm calculates correctly by finding the volume of a hollow cylinder. As THREE.STLLoader() returns a non-indexed geometry, I've casted the geometry of the cylinder to non-indexed too.

Related forum topic

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 1000);
camera.position.setScalar(20);
var renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0x404040);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

var controls = new THREE.OrbitControls(camera, renderer.domElement);

var loader = new THREE.STLLoader();
loader.load('https://threejs/examples/models/stl/binary/pr2_head_pan.stl', function(geometry) {

  var mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({
    color: 0xff00ff,
    wireframe: true
  }));
  mesh.rotation.set(-Math.PI / 2, 0, 0);
  mesh.scale.setScalar(100);
  scene.add(mesh);

  console.log("stl volume is " + getVolume(geometry));
});

// check with known volume:
var hollowCylinderGeom = new THREE.LatheBufferGeometry([
  new THREE.Vector2(1, 0),
  new THREE.Vector2(2, 0),
  new THREE.Vector2(2, 2),
  new THREE.Vector2(1, 2),
  new THREE.Vector2(1, 0)
], 90).toNonIndexed();
console.log("pre-puted volume of a hollow cylinder (PI * (R^2 - r^2) * h): " + Math.PI * (Math.pow(2, 2) - Math.pow(1, 2)) * 2);
console.log("puted volume of a hollow cylinder: " + getVolume(hollowCylinderGeom));


function getVolume(geometry) {

  let position = geometry.attributes.position;
  let faces = position.count / 3;
  let sum = 0;
  let p1 = new THREE.Vector3(),
    p2 = new THREE.Vector3(),
    p3 = new THREE.Vector3();
  for (let i = 0; i < faces; i++) {
    p1.fromBufferAttribute(position, i * 3 + 0);
    p2.fromBufferAttribute(position, i * 3 + 1);
    p3.fromBufferAttribute(position, i * 3 + 2);
    sum += signedVolumeOfTriangle(p1, p2, p3);
  }
  return sum;

}

function signedVolumeOfTriangle(p1, p2, p3) {
  return p1.dot(p2.cross(p3)) / 6.0;
}

renderer.setAnimationLoop(() => {
  renderer.render(scene, camera);
});
body {
  overflow: hidden;
  margin: 0;
}
<script src="https://threejs/build/three.min.js"></script>
<script src="https://threejs/examples/js/loaders/STLLoader.js"></script>
<script src="https://threejs/examples/js/controls/OrbitControls.js"></script>

This is a pretty tricky problem. One way is to depose the object into a bunch of convex polyhedra and sum the volumes of those...

Another way is to voxelize it, and add up the voxels on the inside to get an estimate whos accuracy is limited by the resolution of your voxelization.

Edit: prisoner849 has a rad solution!

I'm also looking for a solution to this, And didn't have any implementation so far.

But extending from the voxelization idea like @manthrax mentioned.

I think we can voxelized into the octree structure.

If the cube still intersects with multiple triangles then voxelized deeper octree.

Until we reached the level of a single triangle cut through,
Then we calculate the volume of the cube using this method:

https://math.stackexchange./questions/454583/volume-of-cube-section-above-intersection-with-plane


After understood prisoner849's solution, This idea is no more valid pared to his solution.

发布评论

评论列表(0)

  1. 暂无评论