2015-10-18 158 views
0

我正在嘗試創建一個軸對齊的邊界框(aabb),並希望獲取所有頂點座標的最小值和最大值。Java獲取幾個浮點數的最小值和最大值

我正在讀取一個obj文件以獲得頂點座標,打印輸出將是浮點數中的x,y,z座標列表。

float xVertices; 
float yVertices; 
float zVertices; 

private void getObjVertices(String fileName) 
{  
    BufferedReader meshReader = null; 

    try 
    { 
     meshReader = new BufferedReader(new FileReader(fileName)); 
     String line; 

     while((line = meshReader.readLine()) != null) 
     { 
      String[] tokens = line.split(" "); 
      tokens = Util.RemoveEmptyStrings(tokens); 

      if(tokens.length == 0 || tokens[0].equals("#") || 
        tokens[0].equals("vt") || tokens[0].equals("vn") || tokens[0].equals("f")) 
       continue; 
      else if(tokens[0].equals("v")) 
      {            
       xVertices = Float.parseFloat(tokens[1]); 
       yVertices = Float.parseFloat(tokens[2]); 
       zVertices = Float.parseFloat(tokens[3]); 

       System.out.println("xVertices:" + xVertices); 
       System.out.println("yVertices:" + yVertices); 
       System.out.println("zVertices:" + zVertices); 

     // get min/max x,y,z values, calculatre width, height, depth 
      } 
     }   
     meshReader.close(); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
} 

我想達成什麼是讓所有xVertices,yVertices,zVertices並找出每個軸數爲的greates並且是最小的。

有了這些信息,我就可以創建對撞機了。有人知道我如何在我的代碼中計算最大和最小的數字嗎?

感謝您提前提供任何幫助!

+0

爲什麼不只是保持最大和最小數量的記錄?並在閱讀新頂點時不斷更新它們? – Bon

+0

哦,是的,這是一個有趣的想法,有趣的是它沒有發生在我身上..你會有一個具體的建議嗎? – DisasterCoder

回答

2

您可以維護最大和最小編號的記錄,並在程序讀取頂點時更新它們。下面是一個例子。

float xMin, xMax, yMin, yMax, zMin, zMax; 
    xMin = yMin = zMin = Float.MAX_VALUE; 
    xMax = yMax = zMax = Float.MIN_VALUE; 

    while ((line = meshReader.readLine()) != null) { 
     String[] tokens = line.split(" "); 
     tokens = Util.RemoveEmptyStrings(tokens); 

     if (tokens.length == 0 || tokens[0].equals("#") || 
       tokens[0].equals("vt") || tokens[0].equals("vn") || tokens[0].equals("f")) 
      continue; 
     else if (tokens[0].equals("v")) { 
      xVertices = Float.parseFloat(tokens[1]); 
      yVertices = Float.parseFloat(tokens[2]); 
      zVertices = Float.parseFloat(tokens[3]); 

      if (xMin > xVertices) xMin = xVertices; 
      if (yMin > yVertices) yMin = yVertices; 
      if (zMin > zVertices) zMin = zVertices; 

      if (xMax < xVertices) xMax = xVertices; 
      if (yMax < yVertices) yMax = yVertices; 
      if (zMax < zVertices) zMax = zVertices; 

      System.out.println("xVertices:" + xVertices); 
      System.out.println("yVertices:" + yVertices); 
      System.out.println("zVertices:" + zVertices); 

      // get min/max x,y,z values, calculatre width, height, depth 
     } 
+0

非常感謝! – DisasterCoder

相關問題