Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package stl;
  2.  
  3. /**
  4.  *  This program shows the basic shapes for 3D print
  5.  * @author do_hoang
  6.  * @version 1.3 2020-02-11
  7.  */
  8.  
  9. public class StlImage {
  10.  
  11.     private double minThick;
  12.     private double thickScale;
  13.     private double scale;
  14.     private double maxValue;
  15.    
  16.     public StlImage (double min, double max){
  17.         this.minThick = min;
  18. //      this.maxValue = max;
  19.         this.thickScale = max - min;
  20.         this.scale = 0.1;
  21.     }
  22.     public Solid createSolid(int[][] matrix) {
  23.         Solid solid = new Solid();
  24.         //find the maximum value in the integer array, store it in maxValue
  25.        
  26.         for(int i = 0; i < matrix.length; i++) {
  27.             for(int j = 0; j < matrix[i].length; j++) {
  28.                 if(matrix[i][j] >= maxValue) {
  29.                     maxValue = matrix[i][j];
  30.                
  31.                 }
  32.             }
  33.         }
  34.         for(int i = 0; i < matrix.length-1; i++) {
  35.             for(int j = 0; j < matrix[i].length-1; j++) {
  36.                 //top
  37.                 solid.addFacet(
  38.                 createIfsVertex(i, j, matrix[i][j]),
  39.                 createIfsVertex(i+1, j,   matrix[i+1][j]),
  40.                 createIfsVertex(i+1, j+1, matrix[i+1][j+1]),
  41.                 createIfsVertex(i,   j+1, matrix[i][j+1]));
  42.             }
  43.         }
  44.         int width = matrix.length;
  45.         int length = matrix[0].length;
  46.         //side1
  47.         //add the four Vertex to Solid using addFacet
  48.         solid.addFacet(
  49.                 new Vertex(0, 0, 0),
  50.                 new Vertex(width * scale, 0, 0),
  51.                 new Vertex(width * scale, 0, minThick),
  52.                 new Vertex(0, 0, minThick));
  53.     //side2
  54.         //add the four Vertex to Solid using addFacet
  55.         solid.addFacet(
  56.                 new Vertex(0, length * scale, 0),
  57.                 new Vertex(0, 0, 0),
  58.                 new Vertex(0, 0, minThick),
  59.                 new Vertex(0, length * scale, minThick));
  60.     //side3
  61.         //add the four Vertex to Solid using addFacet  
  62.         solid.addFacet(
  63.             new Vertex(width * scale, 0, 0),
  64.             new Vertex(width * scale, length * scale, 0),
  65.             new Vertex(width * scale, length * scale, minThick),
  66.             new Vertex(width * scale, 0, minThick));
  67.     //side4
  68.         //add the four Vertex to Solid using addFacet
  69.         solid.addFacet(
  70.             new Vertex(width * scale, length * scale, 0),
  71.             new Vertex(0, length * scale, 0),
  72.             new Vertex(0, length * scale, minThick),
  73.             new Vertex(width * scale, length * scale, minThick));
  74.     //bottom
  75.         //add the four Vertex to Solid using addFacet
  76.         solid.addFacet(
  77.             new Vertex(0, 0, 0),
  78.             new Vertex(0, length * scale, 0),
  79.             new Vertex(width * scale, length * scale, 0),
  80.             new Vertex(width * scale, 0, 0));
  81.         return solid;
  82.        
  83.     }
  84.     private Vertex createIfsVertex(double x, double y, double z) {
  85.         //calculate the thickness based on the following formula
  86.         double thickness = minThick + thickScale * Math.sqrt(z/maxValue);
  87.         //create a new Vertex with the following value
  88.         return new Vertex(x * scale, y * scale, thickness);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement