Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Last active August 29, 2015 14:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lihaoyi/c2f57b0935689555e338 to your computer and use it in GitHub Desktop.
Save lihaoyi/c2f57b0935689555e338 to your computer and use it in GitHub Desktop.
// Input Scala sources
def lightIsVisible(l: Vec, p: Vec) = {
val ray = Ray(p, l - p)
val length = (l - p).magnitude
var visible = true
for (i <- 0 until objects.length){
val (o, s) = objects(i)
val t = o.intersectionTime(ray)
if (t > Epsilon && t < length - Epsilon){
visible = false
}
}
visible
}
// Output after first round of optimization
ScalaJS.c.LScene.prototype.lightIsVisible__LVec__LVec__Z = (function(l, p) {
var ray = new ScalaJS.c.LRay().init___LVec__LVec$Unit(p, ScalaJS.m.LVec$().normalizer__LVec__LVec$Unit(l.$$minus__LVec__LVec(p)));
var length = l.$$minus__LVec__LVec(p).magnitude__D();
var elem$1 = false;
elem$1 = true;
var end = this.Scene$$objects$f.u["length"];
var isEmpty$4 = (end <= 0);
var numRangeElements$4 = (isEmpty$4 ? 0 : end);
var lastElement$4 = (isEmpty$4 ? (-1) : (((-1) + end) | 0));
var terminalElement$4 = ((1 + lastElement$4) | 0);
if ((numRangeElements$4 < 0)) {
ScalaJS.m.sci_Range$().scala$collection$immutable$Range$$fail__I__I__I__Z__sr_Nothing$(0, end, 1, false)
};
var i = 0;
var count = 0;
while ((i !== terminalElement$4)) {
var v1 = i;
var x1 = this.Scene$$objects$f.u[v1];
if ((x1 !== null)) {
var o = ScalaJS.as.LForm(x1.$$und1$f);
var s = ScalaJS.as.LSurface(x1.$$und2$f);
var x$2_$_$$und1$f = o;
var x$2_$_$$und2$f = s
} else {
var x$2;
throw new ScalaJS.c.s_MatchError().init___O(x1)
};
var o$2 = ScalaJS.as.LForm(x$2_$_$$und1$f);
ScalaJS.as.LSurface(x$2_$_$$und2$f);
var t = o$2.intersectionTime__LRay__D(ray);
if (((t > ScalaJS.m.LScalaJSExample$().Epsilon$1) && (t < (length - ScalaJS.m.LScalaJSExample$().Epsilon$1)))) {
elem$1 = false
};
count = ((1 + count) | 0);
i = ((1 + i) | 0)
};
return elem$1
});
// Output after being fully optimized, including by Google Closure
// Names were manually re-symbolicated from their short
// compression-friendly identifiers to human-readable identifiers
function lightIsVisible(self, l, p) {
var ray = (new Ray).Init(p, normalize(Vec$(), subtract(l, p)));
length = magnitude(subtract(l, p));
visible = !1;
visible = !0;
var i = self.Ve.objects.length,
o = 0 >= i,
max = 1 + (o ? -1 : -1 + i | 0) | 0;
0 > (o ? 0 : i) && fail(Range$(), 0, i, 1, !1);
for (i = 0; i !== max;) {
var tuple = self.Ve.objects[i];
if (null !== tuple) o = tuple._1, s = tuple._2;
else throw (new MatchError).init(s);
s;
t = o.intersectionTime(ray);
t > Example$().Epsilon && t < length - Example$().Epsilon && (visible = !1);
i = 1 + i | 0
}
return visible
}
// Output after being compiled by Scala-JVM and put through a Java decompiler (http://jd.benow.ca/)
public boolean lightIsVisible(Vec l, Vec p){
Ray ray = new Ray(p, Vec$.MODULE$.normalizer(l.$minus(p)));
double length = l.$minus(p).magnitude();
BooleanRef visible = new BooleanRef(true);
RichInt$.MODULE$
.until$extension0(Predef$.MODULE$.intWrapper(0), objects().length)
.foreach$mVc$sp(new Serializable(ray, length, visible) {
public final void apply(int i){
apply$mcVI$sp(i);
}
public void apply$mcVI$sp(int i){
Tuple2 tuple2_1 = Scene$.MODULE$.objects()[i];
if(tuple2_1 != null){
Form o = (Form)tuple2_1._1();
Surface s = (Surface)tuple2_1._2();
Tuple2 tuple2_2 = new Tuple2(o, s);
Tuple2 tuple2 = tuple2_2;
Form o = (Form)tuple2._1();
Surface s = (Surface)tuple2._2();
double t = o.intersectionTime(ray$1);
if(t > Scene$.MODULE$.Epsilon() && t < length$1 - Scene$.MODULE$.Epsilon())
visible$1.elem = false;
return;
} else{
throw new MatchError(tuple2_1);
}
}
public final volatile Object apply(Object v1){
apply(BoxesRunTime.unboxToInt(v1));
return BoxedUnit.UNIT;
}
public static final long serialVersionUID = 0L;
private final Ray ray$1;
private final double length$1;
private final BooleanRef visible$1;
public{
this.ray$1 = ray$1;
this.length$1 = length$1;
this.visible$1 = visible$1;
super();
}
});
return visible.elem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment