Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (806)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: theagentd (posted 2017-02-18 13:35:16, viewed 1020 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141  
public int[] optimizePostTransformCacheTomForsyth(int[] indices){
      
      ArrayList<Integer> cache = new ArrayList<>();
      
      //calculate vertex valence
      int[] vertexValence = new int[vertices.size()];
      for(int i = 0; i < indices.length; i++){
         vertexValence[indices[i]]++;
      }
      
      int[] newIndices = new int[indices.length];
      float[] vertexScores = new float[vertices.size()];
      
      //Calculate initial scores
      for(int i = 0; i < vertexScores.length; i++){
         vertexScores[i] = calcScore(-1, vertexValence[i]); 
      }
      
      for(int i = 0; i < newIndices.length; i += 3){
         

         //Find highest scoring triangle
         int bestTriangle = -1;
         float bestScore = -1;
         for(int j = 0; j < indices.length; j += 3){
            if(indices[j] == -1){
               continue;
            }
            
            float score = vertexScores[indices[j+0]] + vertexScores[indices[j+1]] + vertexScores[indices[j+2]];
            if(score > bestScore){
               bestTriangle = j;
               bestScore = score;
            }
         }
         
         int i0 = indices[bestTriangle + 0];
         int i1 = indices[bestTriangle + 1];
         int i2 = indices[bestTriangle + 2];
         
         //Save best triangle to new index list
         newIndices[i + 0] = i0;
         newIndices[i + 1] = i1;
         newIndices[i + 2] = i2;

         //Update cache
         cache.remove((Integer)i0);
         cache.remove((Integer)i1);
         cache.remove((Integer)i2);
         cache.add(i0);
         cache.add(i1);
         cache.add(i2);

         
         //remove ejected items from cache, make sure to recalculate their score
         //used vertices are added to the end of the cache, so the oldest vertex is at position 0 in the cache
         while(cache.size() > CACHE_SIZE){
            int removed = cache.remove(0);
            vertexScores[removed] = calcScore(-1, vertexValence[removed]);
         }

         // reduce valence of vertices
         vertexValence[i0]--;
         vertexValence[i1]--;
         vertexValence[i2]--;
         
         //update the score of everything in the cache
         for(int j = 0; j < cache.size(); j++){
            int v = cache.get(j);
            vertexScores[v] = calcScore(j, vertexValence[v]);
         }
         
         //"remove" the triangle from the index buffer to get rid of it
         indices[bestTriangle + 0] = indices[bestTriangle + 1] = indices[bestTriangle + 2] = -1;
      }
      
      return newIndices;
   }
   
   //Old reference implementation, replaced by faster precomputed version below
   /*private float calcScore(int cachePosition, int valence){
      
      //Not used by any triangle, return -1
      if(valence == 0){
         return -1;
      }
      
      float score;
      if(cachePosition == -1){
         //not in cache, initial score is 0
         score = 0;
      }else{
         if(cachePosition < 3){
            //part of last triangle, special score used
            score = LAST_TRIANGLE_SCORE;
         }else{
            //calculate score based on how old the vertex is
            score = (float)Math.pow(1.0 - (cachePosition - 3) / (CACHE_SIZE - 3), CACHE_DECAY_POWER);
         }
      }
      
      //boost score for low valence vertices
      float valenceBoost = VALENCE_BOOST_SCALE * (float)Math.pow(valence, VALENCE_BOOST_POWER);
      score += valenceBoost;
      
      return score;
   }*/
   
   private static final float[] CACHE_POSITION_TABLE;
   
   private static final int VALENCE_BOOST_TABLE_SIZE = 64; //ever seen a vertex used by more than 64 triangles? >_>
   private static final float[] VALENCE_BOOST_TABLE;
   static{
      CACHE_POSITION_TABLE = new float[CACHE_SIZE];
      for(int i = 0; i < CACHE_SIZE; i++){
         if(i < 3){
            //part of last triangle, special score used
            CACHE_POSITION_TABLE[i] = (float)LAST_TRIANGLE_SCORE;
         }else{
            //calculate score based on how old the vertex is
            CACHE_POSITION_TABLE[i] = (float)Math.pow(1.0 - (i - 3) / (CACHE_SIZE - 3), CACHE_DECAY_POWER);
         }
      }
      
      VALENCE_BOOST_TABLE = new float[VALENCE_BOOST_TABLE_SIZE];
      VALENCE_BOOST_TABLE[0] = -10; //zero valence = not used by anything anymore, score is set to -1
      for(int i = 1; i < VALENCE_BOOST_TABLE_SIZE; i++){
         VALENCE_BOOST_TABLE[i] = (float)(VALENCE_BOOST_SCALE * Math.pow(i, VALENCE_BOOST_POWER));
      }
   }
   
   private float calcScore(int cachePosition, int valence){
      float score = 0;
      if(cachePosition != -1){
         score += CACHE_POSITION_TABLE[cachePosition];
      }
      
      score += VALENCE_BOOST_TABLE[valence];
      
      return score;
   }





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
Riven (587 views)
2019-09-04 15:33:17

hadezbladez (5533 views)
2018-11-16 13:46:03

hadezbladez (2411 views)
2018-11-16 13:41:33

hadezbladez (5794 views)
2018-11-16 13:35:35

hadezbladez (1233 views)
2018-11-16 13:32:03

EgonOlsen (4669 views)
2018-06-10 19:43:48

EgonOlsen (5688 views)
2018-06-10 19:43:44

EgonOlsen (3205 views)
2018-06-10 19:43:20

DesertCoockie (4104 views)
2018-05-13 18:23:11

nelsongames (5125 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!