Home | History | Annotate | Download | only in dynamics
      1 /*******************************************************************************
      2  * Copyright (c) 2013, Daniel Murphy
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without modification,
      6  * are permitted provided that the following conditions are met:
      7  * 	* Redistributions of source code must retain the above copyright notice,
      8  * 	  this list of conditions and the following disclaimer.
      9  * 	* Redistributions in binary form must reproduce the above copyright notice,
     10  * 	  this list of conditions and the following disclaimer in the documentation
     11  * 	  and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     16  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     17  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     19  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     20  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     21  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     22  * POSSIBILITY OF SUCH DAMAGE.
     23  ******************************************************************************/
     24 package org.jbox2d.dynamics;
     25 
     26 import java.util.List;
     27 
     28 import org.jbox2d.common.MathUtils;
     29 
     30 public class Profile {
     31   private static final int LONG_AVG_NUMS = 20;
     32   private static final float LONG_FRACTION = 1f / LONG_AVG_NUMS;
     33   private static final int SHORT_AVG_NUMS = 5;
     34   private static final float SHORT_FRACTION = 1f / SHORT_AVG_NUMS;
     35 
     36   public static class ProfileEntry {
     37     float longAvg;
     38     float shortAvg;
     39     float min;
     40     float max;
     41     float accum;
     42 
     43     public ProfileEntry() {
     44       min = Float.MAX_VALUE;
     45       max = -Float.MAX_VALUE;
     46     }
     47 
     48     public void record(float value) {
     49       longAvg = longAvg * (1 - LONG_FRACTION) + value * LONG_FRACTION;
     50       shortAvg = shortAvg * (1 - SHORT_FRACTION) + value * SHORT_FRACTION;
     51       min = MathUtils.min(value, min);
     52       max = MathUtils.max(value, max);
     53     }
     54 
     55     public void startAccum() {
     56       accum = 0;
     57     }
     58 
     59     public void accum(float value) {
     60       accum += value;
     61     }
     62 
     63     public void endAccum() {
     64       record(accum);
     65     }
     66 
     67     @Override
     68     public String toString() {
     69       return shortAvg + " (" + longAvg + ") [" + min + "," + max + "]";
     70     }
     71   }
     72 
     73   public final ProfileEntry step = new ProfileEntry();
     74   public final ProfileEntry stepInit = new ProfileEntry();
     75   public final ProfileEntry collide = new ProfileEntry();
     76   public final ProfileEntry solveParticleSystem = new ProfileEntry();
     77   public final ProfileEntry solve = new ProfileEntry();
     78   public final ProfileEntry solveInit = new ProfileEntry();
     79   public final ProfileEntry solveVelocity = new ProfileEntry();
     80   public final ProfileEntry solvePosition = new ProfileEntry();
     81   public final ProfileEntry broadphase = new ProfileEntry();
     82   public final ProfileEntry solveTOI = new ProfileEntry();
     83 
     84   public void toDebugStrings(List<String> strings) {
     85     strings.add("Profile:");
     86     strings.add(" step: " + step);
     87     strings.add("  init: " + stepInit);
     88     strings.add("  collide: " + collide);
     89     strings.add("  particles: " + solveParticleSystem);
     90     strings.add("  solve: " + solve);
     91     strings.add("   solveInit: " + solveInit);
     92     strings.add("   solveVelocity: " + solveVelocity);
     93     strings.add("   solvePosition: " + solvePosition);
     94     strings.add("   broadphase: " + broadphase);
     95     strings.add("  solveTOI: " + solveTOI);
     96   }
     97 }
     98