How to calculate wall volume of STL in Three.js












0














I have a big challenge, I'm developing a software to calculate the cost of an impression 3d. For that, I need to get the volume of the STL piece to calculate the weight. With the grams I could calculate the price easily.
The problem is that I want to get the volume of the walls and the infill's volume, the walls have approximately 1mm and the infill the rest of the volume. The walls are solid and the infill have a percentage of infill (e.x. 30%). There are figures what have more walls that infill and vice versa.
The volume of both is calculated in Three.js. Now this is what I'm trying. I calculated the total volume of the piece and the volume of the piece scaled in a lower scale without walls(scaling in x, y and z to have 1mm minus) and subtract both.



//x, y and z are the size of mesh
//scale is the percentaje of scaling(uniform on all axes)
function calculateVolume(mesh,scale,x,y,z){
//Scale the mesh
x= x*scale;
y= y*scale;
z= z*scale;
var volume = 0;
var volume_wall = 0;
var volume_infill = 0;
var volume_reduced = 0;
//Calculate the proportion to be reduced with walls of 1mm of each side
var proportion_x = (x-10)/x;
var proportion_y = (y-10)/y;
var proportion_z = (z-10)/z;

//I travel array of mesh triangles
mesh.traverse(function (child){
if (child instance of THREE.Mesh){
//I get array of vector positions
var positions = child.geometry.getAttribute("position").array;
for(var i=0;i<positions.length; i+=9){
//I calculate volume total of triangle
volume += volumeOfVectors(positions,i,scale,scale,scale);
//I calculate volume of triangle minus the walls
volume_reduced += volumeOfVectors(positions,i,scale*proportion_x,
scale*proportion_y,scale*proportion_z);

}
}
});
//I convert volume in mm^3 to cm^3
volume = volume / 1000;
volume_reduced = volume_reduced / 1000;

//I calculate the wall volume
volume_wall = volume - volume_reduced;
//I set the fill volume as 30%
volume_infill = volume_reduced *0.30;
// 1.24 is the density
console.log("Volume 100% infill:"+volume*1.24);
console.log("Volume whitout wall:"+volume_reduced*1.24);
console.log("Volume wall:"+volume_wall*1.24);
console.log("Volume infill:"+volume_infill*1.24);

return [volume_wall,volume_infill];
}

//Calculating volume with the vectors of the triangle
function volumeOfVectors(positions,i,scale_x,scale_y,scale_z){
//Point 1
var t1 = {};
t1.x = positions[i+0]*scale_x;
t1.y = positions[i+1]*scale_y;
t1.z = positions[i+2]*scale_z;
//Point 2
var t2 = {};
t2.x = positions[i+3]*scale_x;
t2.y = positions[i+4]*scale_y;
t2.z = positions[i+5]*scale_z;
//Point 3
var t3 = {};
t3.x = positions[i+6]*scale_x;
t3.y = positions[i+7]*scale_y;
t3.z = positions[i+8]*scale_z;
//Volumen calcule
return signedVolumeOfTriangle(t1,t2,t3);
}


function signedVolumeOfTriangle(p1,p2,p3){
var v321 = p3.x*p2.y*p1.z;
var v231 = p2.x*p3.y*p1.z;
var v312 = p3.x*p1.y*p2.z;
var v132 = p1.x*p3.y*p2.z;
var v213 = p2.x*p1.y*p3.z;
var v123 = p1.x*p2.y*p3.z;
return (-v321 + v231 + v312 - v132 - v213 + v123)/6;
}


But this way is not accurate. Because the object is scaled in all directions, getting a volume as the first image. The 3d red object have been scaled to remove walls and the transparent is the original 3d object. As you see the 3d object isn't adapt to the borders.



image problem 1
image problem 2



The volume should be is as the next image. This way keep 1mm of wall in all directions.
image good 1
image good 2



Can you think of an idea or a way forward?










share|improve this question



























    0














    I have a big challenge, I'm developing a software to calculate the cost of an impression 3d. For that, I need to get the volume of the STL piece to calculate the weight. With the grams I could calculate the price easily.
    The problem is that I want to get the volume of the walls and the infill's volume, the walls have approximately 1mm and the infill the rest of the volume. The walls are solid and the infill have a percentage of infill (e.x. 30%). There are figures what have more walls that infill and vice versa.
    The volume of both is calculated in Three.js. Now this is what I'm trying. I calculated the total volume of the piece and the volume of the piece scaled in a lower scale without walls(scaling in x, y and z to have 1mm minus) and subtract both.



    //x, y and z are the size of mesh
    //scale is the percentaje of scaling(uniform on all axes)
    function calculateVolume(mesh,scale,x,y,z){
    //Scale the mesh
    x= x*scale;
    y= y*scale;
    z= z*scale;
    var volume = 0;
    var volume_wall = 0;
    var volume_infill = 0;
    var volume_reduced = 0;
    //Calculate the proportion to be reduced with walls of 1mm of each side
    var proportion_x = (x-10)/x;
    var proportion_y = (y-10)/y;
    var proportion_z = (z-10)/z;

    //I travel array of mesh triangles
    mesh.traverse(function (child){
    if (child instance of THREE.Mesh){
    //I get array of vector positions
    var positions = child.geometry.getAttribute("position").array;
    for(var i=0;i<positions.length; i+=9){
    //I calculate volume total of triangle
    volume += volumeOfVectors(positions,i,scale,scale,scale);
    //I calculate volume of triangle minus the walls
    volume_reduced += volumeOfVectors(positions,i,scale*proportion_x,
    scale*proportion_y,scale*proportion_z);

    }
    }
    });
    //I convert volume in mm^3 to cm^3
    volume = volume / 1000;
    volume_reduced = volume_reduced / 1000;

    //I calculate the wall volume
    volume_wall = volume - volume_reduced;
    //I set the fill volume as 30%
    volume_infill = volume_reduced *0.30;
    // 1.24 is the density
    console.log("Volume 100% infill:"+volume*1.24);
    console.log("Volume whitout wall:"+volume_reduced*1.24);
    console.log("Volume wall:"+volume_wall*1.24);
    console.log("Volume infill:"+volume_infill*1.24);

    return [volume_wall,volume_infill];
    }

    //Calculating volume with the vectors of the triangle
    function volumeOfVectors(positions,i,scale_x,scale_y,scale_z){
    //Point 1
    var t1 = {};
    t1.x = positions[i+0]*scale_x;
    t1.y = positions[i+1]*scale_y;
    t1.z = positions[i+2]*scale_z;
    //Point 2
    var t2 = {};
    t2.x = positions[i+3]*scale_x;
    t2.y = positions[i+4]*scale_y;
    t2.z = positions[i+5]*scale_z;
    //Point 3
    var t3 = {};
    t3.x = positions[i+6]*scale_x;
    t3.y = positions[i+7]*scale_y;
    t3.z = positions[i+8]*scale_z;
    //Volumen calcule
    return signedVolumeOfTriangle(t1,t2,t3);
    }


    function signedVolumeOfTriangle(p1,p2,p3){
    var v321 = p3.x*p2.y*p1.z;
    var v231 = p2.x*p3.y*p1.z;
    var v312 = p3.x*p1.y*p2.z;
    var v132 = p1.x*p3.y*p2.z;
    var v213 = p2.x*p1.y*p3.z;
    var v123 = p1.x*p2.y*p3.z;
    return (-v321 + v231 + v312 - v132 - v213 + v123)/6;
    }


    But this way is not accurate. Because the object is scaled in all directions, getting a volume as the first image. The 3d red object have been scaled to remove walls and the transparent is the original 3d object. As you see the 3d object isn't adapt to the borders.



    image problem 1
    image problem 2



    The volume should be is as the next image. This way keep 1mm of wall in all directions.
    image good 1
    image good 2



    Can you think of an idea or a way forward?










    share|improve this question

























      0












      0








      0







      I have a big challenge, I'm developing a software to calculate the cost of an impression 3d. For that, I need to get the volume of the STL piece to calculate the weight. With the grams I could calculate the price easily.
      The problem is that I want to get the volume of the walls and the infill's volume, the walls have approximately 1mm and the infill the rest of the volume. The walls are solid and the infill have a percentage of infill (e.x. 30%). There are figures what have more walls that infill and vice versa.
      The volume of both is calculated in Three.js. Now this is what I'm trying. I calculated the total volume of the piece and the volume of the piece scaled in a lower scale without walls(scaling in x, y and z to have 1mm minus) and subtract both.



      //x, y and z are the size of mesh
      //scale is the percentaje of scaling(uniform on all axes)
      function calculateVolume(mesh,scale,x,y,z){
      //Scale the mesh
      x= x*scale;
      y= y*scale;
      z= z*scale;
      var volume = 0;
      var volume_wall = 0;
      var volume_infill = 0;
      var volume_reduced = 0;
      //Calculate the proportion to be reduced with walls of 1mm of each side
      var proportion_x = (x-10)/x;
      var proportion_y = (y-10)/y;
      var proportion_z = (z-10)/z;

      //I travel array of mesh triangles
      mesh.traverse(function (child){
      if (child instance of THREE.Mesh){
      //I get array of vector positions
      var positions = child.geometry.getAttribute("position").array;
      for(var i=0;i<positions.length; i+=9){
      //I calculate volume total of triangle
      volume += volumeOfVectors(positions,i,scale,scale,scale);
      //I calculate volume of triangle minus the walls
      volume_reduced += volumeOfVectors(positions,i,scale*proportion_x,
      scale*proportion_y,scale*proportion_z);

      }
      }
      });
      //I convert volume in mm^3 to cm^3
      volume = volume / 1000;
      volume_reduced = volume_reduced / 1000;

      //I calculate the wall volume
      volume_wall = volume - volume_reduced;
      //I set the fill volume as 30%
      volume_infill = volume_reduced *0.30;
      // 1.24 is the density
      console.log("Volume 100% infill:"+volume*1.24);
      console.log("Volume whitout wall:"+volume_reduced*1.24);
      console.log("Volume wall:"+volume_wall*1.24);
      console.log("Volume infill:"+volume_infill*1.24);

      return [volume_wall,volume_infill];
      }

      //Calculating volume with the vectors of the triangle
      function volumeOfVectors(positions,i,scale_x,scale_y,scale_z){
      //Point 1
      var t1 = {};
      t1.x = positions[i+0]*scale_x;
      t1.y = positions[i+1]*scale_y;
      t1.z = positions[i+2]*scale_z;
      //Point 2
      var t2 = {};
      t2.x = positions[i+3]*scale_x;
      t2.y = positions[i+4]*scale_y;
      t2.z = positions[i+5]*scale_z;
      //Point 3
      var t3 = {};
      t3.x = positions[i+6]*scale_x;
      t3.y = positions[i+7]*scale_y;
      t3.z = positions[i+8]*scale_z;
      //Volumen calcule
      return signedVolumeOfTriangle(t1,t2,t3);
      }


      function signedVolumeOfTriangle(p1,p2,p3){
      var v321 = p3.x*p2.y*p1.z;
      var v231 = p2.x*p3.y*p1.z;
      var v312 = p3.x*p1.y*p2.z;
      var v132 = p1.x*p3.y*p2.z;
      var v213 = p2.x*p1.y*p3.z;
      var v123 = p1.x*p2.y*p3.z;
      return (-v321 + v231 + v312 - v132 - v213 + v123)/6;
      }


      But this way is not accurate. Because the object is scaled in all directions, getting a volume as the first image. The 3d red object have been scaled to remove walls and the transparent is the original 3d object. As you see the 3d object isn't adapt to the borders.



      image problem 1
      image problem 2



      The volume should be is as the next image. This way keep 1mm of wall in all directions.
      image good 1
      image good 2



      Can you think of an idea or a way forward?










      share|improve this question













      I have a big challenge, I'm developing a software to calculate the cost of an impression 3d. For that, I need to get the volume of the STL piece to calculate the weight. With the grams I could calculate the price easily.
      The problem is that I want to get the volume of the walls and the infill's volume, the walls have approximately 1mm and the infill the rest of the volume. The walls are solid and the infill have a percentage of infill (e.x. 30%). There are figures what have more walls that infill and vice versa.
      The volume of both is calculated in Three.js. Now this is what I'm trying. I calculated the total volume of the piece and the volume of the piece scaled in a lower scale without walls(scaling in x, y and z to have 1mm minus) and subtract both.



      //x, y and z are the size of mesh
      //scale is the percentaje of scaling(uniform on all axes)
      function calculateVolume(mesh,scale,x,y,z){
      //Scale the mesh
      x= x*scale;
      y= y*scale;
      z= z*scale;
      var volume = 0;
      var volume_wall = 0;
      var volume_infill = 0;
      var volume_reduced = 0;
      //Calculate the proportion to be reduced with walls of 1mm of each side
      var proportion_x = (x-10)/x;
      var proportion_y = (y-10)/y;
      var proportion_z = (z-10)/z;

      //I travel array of mesh triangles
      mesh.traverse(function (child){
      if (child instance of THREE.Mesh){
      //I get array of vector positions
      var positions = child.geometry.getAttribute("position").array;
      for(var i=0;i<positions.length; i+=9){
      //I calculate volume total of triangle
      volume += volumeOfVectors(positions,i,scale,scale,scale);
      //I calculate volume of triangle minus the walls
      volume_reduced += volumeOfVectors(positions,i,scale*proportion_x,
      scale*proportion_y,scale*proportion_z);

      }
      }
      });
      //I convert volume in mm^3 to cm^3
      volume = volume / 1000;
      volume_reduced = volume_reduced / 1000;

      //I calculate the wall volume
      volume_wall = volume - volume_reduced;
      //I set the fill volume as 30%
      volume_infill = volume_reduced *0.30;
      // 1.24 is the density
      console.log("Volume 100% infill:"+volume*1.24);
      console.log("Volume whitout wall:"+volume_reduced*1.24);
      console.log("Volume wall:"+volume_wall*1.24);
      console.log("Volume infill:"+volume_infill*1.24);

      return [volume_wall,volume_infill];
      }

      //Calculating volume with the vectors of the triangle
      function volumeOfVectors(positions,i,scale_x,scale_y,scale_z){
      //Point 1
      var t1 = {};
      t1.x = positions[i+0]*scale_x;
      t1.y = positions[i+1]*scale_y;
      t1.z = positions[i+2]*scale_z;
      //Point 2
      var t2 = {};
      t2.x = positions[i+3]*scale_x;
      t2.y = positions[i+4]*scale_y;
      t2.z = positions[i+5]*scale_z;
      //Point 3
      var t3 = {};
      t3.x = positions[i+6]*scale_x;
      t3.y = positions[i+7]*scale_y;
      t3.z = positions[i+8]*scale_z;
      //Volumen calcule
      return signedVolumeOfTriangle(t1,t2,t3);
      }


      function signedVolumeOfTriangle(p1,p2,p3){
      var v321 = p3.x*p2.y*p1.z;
      var v231 = p2.x*p3.y*p1.z;
      var v312 = p3.x*p1.y*p2.z;
      var v132 = p1.x*p3.y*p2.z;
      var v213 = p2.x*p1.y*p3.z;
      var v123 = p1.x*p2.y*p3.z;
      return (-v321 + v231 + v312 - v132 - v213 + v123)/6;
      }


      But this way is not accurate. Because the object is scaled in all directions, getting a volume as the first image. The 3d red object have been scaled to remove walls and the transparent is the original 3d object. As you see the 3d object isn't adapt to the borders.



      image problem 1
      image problem 2



      The volume should be is as the next image. This way keep 1mm of wall in all directions.
      image good 1
      image good 2



      Can you think of an idea or a way forward?







      javascript three.js 3d stl volume-rendering






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 18:57









      Gaston Zarate

      11




      11
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343849%2fhow-to-calculate-wall-volume-of-stl-in-three-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343849%2fhow-to-calculate-wall-volume-of-stl-in-three-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents