Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization sine/cosine functions. #304

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions chunky/src/java/se/llbit/chunky/main/Chunky.java
Expand Up @@ -65,6 +65,7 @@
import se.llbit.chunky.world.World;
import se.llbit.chunky.world.listeners.ChunkTopographyListener;
import se.llbit.log.Log;
import se.llbit.math.QuickMath;
import se.llbit.math.Vector3d;
import se.llbit.util.OSDetector;
import se.llbit.util.OSDetector.OS;
Expand Down Expand Up @@ -134,12 +135,6 @@ public static final String getAppName() {
Version.getVersion();
}

/**
* Constructor
*/
public Chunky() {
}

/**
* Create a new instance of the application GUI.
* @param args
Expand Down Expand Up @@ -379,6 +374,7 @@ public synchronized void viewUpdated() {
* @param args
*/
public static void main(final String[] args) {
QuickMath.initSinCosTable();
Chunky chunky = new Chunky();
int exitVal = chunky.run(args);
if (exitVal != 0) {
Expand Down
Expand Up @@ -41,8 +41,8 @@ public void apply(double x, double y, Vector3d o, Vector3d d) {
double ax = QuickMath.degToRad(x * fov);
double avSquared = ay * ay + ax * ax;
double angleFromCenter = FastMath.sqrt(avSquared);
double dz = FastMath.cos(angleFromCenter);
double dv = FastMath.sin(angleFromCenter);
double dz = QuickMath.cos(angleFromCenter);
double dv = QuickMath.sin(angleFromCenter);
double dy, dx;
if (angleFromCenter == 0) {
dx = dy = 0;
Expand Down
Expand Up @@ -45,10 +45,10 @@ public void apply(double x, double y, Vector3d o, Vector3d d) {
double ay = QuickMath.degToRad(y * fov);
double ax = QuickMath.degToRad(x * fov);

double vv = FastMath.cos(ay);
double vv = QuickMath.cos(ay);

o.set(0, 0, 0);
d.set(vv * FastMath.sin(ax), FastMath.sin(ay), vv * FastMath.cos(ax));
d.set(vv * QuickMath.sin(ax), QuickMath.sin(ay), vv * QuickMath.cos(ax));
}

@Override
Expand Down
Expand Up @@ -47,8 +47,8 @@ public void apply(double x, double y, Random random, Vector3d o,
public void apply(double x, double y, Vector3d o,
Vector3d d) {
double ax = QuickMath.degToRad(x * fov);
double dz = FastMath.cos(ax);
double dx = FastMath.sin(ax);
double dz = QuickMath.cos(ax);
double dx = QuickMath.sin(ax);
double dy = fovTan * y;

o.set(0, 0, 0);
Expand Down
16 changes: 8 additions & 8 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java
Expand Up @@ -285,8 +285,8 @@ public void getSkyDiffuseColorInner(Ray ray) {
}
case SKYMAP_SPHERICAL:
{
double cos = FastMath.cos(-rotation);
double sin = FastMath.sin(-rotation);
double cos = QuickMath.cos(-rotation);
double sin = QuickMath.sin(-rotation);
double x = cos*ray.d.x + sin*ray.d.z;
double y = ray.d.y;
double z = -sin*ray.d.x + cos*ray.d.z;
Expand All @@ -299,8 +299,8 @@ public void getSkyDiffuseColorInner(Ray ray) {
}
case SKYBOX:
{
double cos = FastMath.cos(-rotation);
double sin = FastMath.sin(-rotation);
double cos = QuickMath.cos(-rotation);
double sin = QuickMath.sin(-rotation);
double x = cos*ray.d.x + sin*ray.d.z;
double y = ray.d.y;
double z = -sin*ray.d.x + cos*ray.d.z;
Expand Down Expand Up @@ -395,8 +395,8 @@ public void getSkyColorInterpolated(Ray ray) {
}
case SKYMAP_SPHERICAL:
{
double cos = FastMath.cos(-rotation);
double sin = FastMath.sin(-rotation);
double cos = QuickMath.cos(-rotation);
double sin = QuickMath.sin(-rotation);
double x = cos*ray.d.x + sin*ray.d.z;
double y = ray.d.y;
double z = -sin*ray.d.x + cos*ray.d.z;
Expand All @@ -409,8 +409,8 @@ public void getSkyColorInterpolated(Ray ray) {
}
case SKYBOX:
{
double cos = FastMath.cos(-rotation);
double sin = FastMath.sin(-rotation);
double cos = QuickMath.cos(-rotation);
double sin = QuickMath.sin(-rotation);
double x = cos*ray.d.x + sin*ray.d.z;
double y = ray.d.y;
double z = -sin*ray.d.x + cos*ray.d.z;
Expand Down
24 changes: 12 additions & 12 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Sun.java
Expand Up @@ -125,11 +125,11 @@ public class Sun implements JSONifiable {
*/
public static final double RADIUS = .03;
@SuppressWarnings("javadoc")
public static final double RADIUS_COS = FastMath.cos(RADIUS);
public static final double RADIUS_COS = QuickMath.cos(RADIUS);
@SuppressWarnings("javadoc")
public static final double RADIUS_COS_2 = FastMath.cos(RADIUS*2);
public static final double RADIUS_COS_2 = QuickMath.cos(RADIUS*2);
@SuppressWarnings("javadoc")
public static final double RADIUS_SIN = FastMath.sin(RADIUS);
public static final double RADIUS_SIN = QuickMath.sin(RADIUS);
@SuppressWarnings("javadoc")
public static final double RADIUS_COS_SQ = RADIUS_COS * RADIUS_COS;
@SuppressWarnings("javadoc")
Expand Down Expand Up @@ -237,12 +237,12 @@ private void initSun() {
double theta = azimuth;
double phi = altitude;

double r = QuickMath.abs(FastMath.cos(phi));
double r = QuickMath.abs(QuickMath.cos(phi));

sw.set(
FastMath.cos(theta) * r,
FastMath.sin(phi),
FastMath.sin(theta) * r
QuickMath.cos(theta) * r,
QuickMath.sin(phi),
QuickMath.sin(theta) * r
);

if (QuickMath.abs(sw.x) > .1) {
Expand Down Expand Up @@ -353,7 +353,7 @@ public void setColor(Vector3d newColor) {

private void updateSkylightValues() {
double sunTheta = Math.PI/2 - altitude;
double cosTheta = FastMath.cos(sunTheta);
double cosTheta = QuickMath.cos(sunTheta);
double cos2Theta = cosTheta*cosTheta;
double chi = (4.0/9.0 - turb/120.0)*(Math.PI - 2*sunTheta);
zenith_Y = (4.0453*turb - 4.9710)*Math.tan(chi) - 0.2155*turb + 2.4192;
Expand Down Expand Up @@ -398,8 +398,8 @@ public void getRandomSunDirection(Ray reflected, Random random) {
Vector3d v = new Vector3d(sv);
Vector3d w = new Vector3d(sw);

u.scale(FastMath.cos(phi)*sin_a);
v.scale(FastMath.sin(phi)*sin_a);
u.scale(QuickMath.cos(phi)*sin_a);
v.scale(QuickMath.sin(phi)*sin_a);
w.scale(cos_a);

reflected.d.add(u, v);
Expand All @@ -424,7 +424,7 @@ public void doAtmos(Ray ray, double s, double attenuation) {
ray.color.z *= Fex;
} else {
double theta = ray.d.dot(sw);
double cos_theta = FastMath.cos(theta);
double cos_theta = QuickMath.cos(theta);
double cos2_theta = cos_theta*cos_theta;
double Brt = (3 / (16*Math.PI)) * Br * (1 + cos2_theta);
double Bmt = (1 / (4*Math.PI)) * Bm * ((1-g)*(1-g)) / FastMath.pow(1 + g*g + 2*g*cos_theta, 3/2.);
Expand Down Expand Up @@ -461,7 +461,7 @@ public double extinction(double s) {
* @return Inscatter factor
*/
public double inscatter(double Fex, double theta) {
double cos_theta = FastMath.cos(theta);
double cos_theta = QuickMath.cos(theta);
double cos2_theta = cos_theta*cos_theta;
double Brt = (3 / (16*Math.PI)) * Br * (1 + cos2_theta);
double Bmt = (1 / (4*Math.PI)) * Bm * ((1-g)*(1-g)) / FastMath.pow(1 + g*g + 2*g*cos_theta, 3/2.);
Expand Down
12 changes: 6 additions & 6 deletions chunky/src/java/se/llbit/math/Matrix3d.java
Expand Up @@ -36,8 +36,8 @@ public class Matrix3d {
* @param theta
*/
public final void rotX(double theta) {
double cost = FastMath.cos(theta);
double sint = FastMath.sin(theta);
double cost = QuickMath.cos(theta);
double sint = QuickMath.sin(theta);
m11 = 1;
m12 = 0;
m13 = 0;
Expand All @@ -55,8 +55,8 @@ public final void rotX(double theta) {
* @param theta
*/
public final void rotY(double theta) {
double cost = FastMath.cos(theta);
double sint = FastMath.sin(theta);
double cost = QuickMath.cos(theta);
double sint = QuickMath.sin(theta);
m11 = cost;
m12 = 0;
m13 = sint;
Expand All @@ -74,8 +74,8 @@ public final void rotY(double theta) {
* @param theta
*/
public final void rotZ(double theta) {
double cost = FastMath.cos(theta);
double sint = FastMath.sin(theta);
double cost = QuickMath.cos(theta);
double sint = QuickMath.sin(theta);
m11 = cost;
m12 = -sint;
m13 = 0;
Expand Down
8 changes: 4 additions & 4 deletions chunky/src/java/se/llbit/math/Ray.java
Expand Up @@ -274,8 +274,8 @@ public final void diffuseReflection(Ray ray, Random random) {
double theta = 2 * Math.PI * x2;

// project to point on hemisphere in tangent space
double tx = r * FastMath.cos(theta);
double ty = r * FastMath.sin(theta);
double tx = r * QuickMath.cos(theta);
double ty = r * QuickMath.sin(theta);
double tz = FastMath.sqrt(1 - x1);

// transform from tangent space to world space
Expand Down Expand Up @@ -341,8 +341,8 @@ public final void scatterNormal(Random random) {
double theta = 2 * Math.PI * x2;

// project to point on hemisphere in tangent space
double tx = r * FastMath.cos(theta);
double ty = r * FastMath.sin(theta);
double tx = r * QuickMath.cos(theta);
double ty = r * QuickMath.sin(theta);
double tz = FastMath.sqrt(1 - x1);

// transform from tangent space to world space
Expand Down
25 changes: 25 additions & 0 deletions lib/src/se/llbit/math/QuickMath.java
Expand Up @@ -24,6 +24,31 @@ public class QuickMath {

public static final double HALF_PI = Math.PI/2;
public static final double TAU = Math.PI*2;
public static final double[] SINE_TABLE = new double[65536];

public static void initSinCosTable() {
for (int i = 0; i < 65536; ++i) {
SINE_TABLE[i] = Math.sin(i * Math.PI * 2.0D / 65536.0D);
}
}

/**
* @param d
* @return The sine of d
*/
public static double sin(double d)
{
return SINE_TABLE[(int) (d * 10430.378D) & 65535];
}

/**
* @param d
* @return The cosine of d
*/
public static double cos(double d)
{
return SINE_TABLE[(int) (d * 10430.378D + 16384.0D) & 65535];
}

/**
* @param d
Expand Down