Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
209 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
JARS=$HOME/Library/Clojure/lib | ||
CP=.:./src:$JARS/clojure.jar:$JARS/clojure-contrib.jar | ||
TEST="src/mreid/minilight/test/all.clj" | ||
MAIN="(mreid.minilight.test.all/-main)" | ||
|
||
java -cp $CP clojure.main -i $TEST -e "$MAIN" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(ns mreid.minilight.test.all | ||
(:gen-class) | ||
(:use mreid.minilight.test.vec) | ||
(:use mreid.minilight.test.triangle) | ||
(:use clojure.contrib.test-is)) | ||
|
||
(defn -main [] | ||
(run-tests 'mreid.minilight.test.vec) | ||
(run-tests 'mreid.minilight.test.triangle)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
;; --- src/name/reid/mark/minilight/test/triangle.clj --- | ||
;; Tests for triangle.clj using the test-is library. | ||
(ns mreid.minilight.test.triangle | ||
(:use mreid.minilight.triangle) | ||
(:use clojure.contrib.test-is)) | ||
|
||
(def xytriangle | ||
(struct triangle | ||
[ [0 0 0] [1 0 0] [0 1 0] ] ; Triangle in xy-plane | ||
[0.5 0.5 0.5] ; Reflectivity | ||
[1 1 1] ; Emitivity | ||
)) | ||
|
||
(def y2ztriangle | ||
(struct triangle | ||
[ [0 0 0] [0 2 0] [0 0 1] ] ; Triangle in xy-plane | ||
[0.5 0.5 0.5] ; Reflectivity | ||
[1 1 1] ; Emitivity | ||
)) | ||
|
||
(deftest test-vertex | ||
(is (= [0 0 0] (vertex xytriangle 0))) | ||
(is (= [1 0 0] (vertex xytriangle 1))) | ||
(is (= [0 1 0] (vertex xytriangle 2))) | ||
|
||
(is (= [0 0 0] (vertex y2ztriangle 0))) | ||
(is (= [0 2 0] (vertex y2ztriangle 1))) | ||
(is (= [0 0 1] (vertex y2ztriangle 2)))) | ||
|
||
(deftest test-edge | ||
(is (= [1 0 0] (edge xytriangle 0 1))) | ||
(is (= [0 1 0] (edge xytriangle 0 2))) | ||
(is (= [-1 1 0] (edge xytriangle 1 2))) | ||
(is (= [1 -1 0] (edge xytriangle 2 1))) | ||
|
||
(is (= [0 2 0] (edge y2ztriangle 0 1))) | ||
(is (= [0 0 1] (edge y2ztriangle 0 2))) | ||
(is (= [0 -2 1] (edge y2ztriangle 1 2))) | ||
(is (= [0 2 -1] (edge y2ztriangle 2 1)))) | ||
|
||
(deftest test-tangent | ||
(is (= [1 0 0] (tangent xytriangle))) | ||
(is (= [0 1 0] (tangent y2ztriangle)))) | ||
|
||
(deftest test-normal | ||
(is (= [0 0 1] (normal xytriangle))) | ||
(is (= [2 0 0] (normal y2ztriangle)))) | ||
|
||
(deftest test-unit-normal | ||
(is (= [0 0 1] (unit-normal xytriangle))) | ||
(is (= [1 0 0] (unit-normal y2ztriangle)))) | ||
|
||
(deftest test-area | ||
(is (= 0.5 (area xytriangle))) | ||
(is (= 1 (area y2ztriangle)))) | ||
|
||
(deftest test-intersect) | ||
(is (= 1 (intersect xytriangle [0 0 1] [0 0 -1]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
;; ---------------------------------------------------------------------------- | ||
;; MiniLight in Clojure --- Mark Reid <http://mark.reid.name/> | ||
;; | ||
;; Based on the Ruby code and architecture by Harrison Ainsworth / HXA7241. | ||
;; <http://www.hxa7241.org/> | ||
;; ---------------------------------------------------------------------------- | ||
|
||
;; --- src/name/reid/mark/minilight/triangle.clj --- | ||
;; A structure and functions for defining and querying triangles. | ||
(ns mreid.minilight.triangle | ||
(:use mreid.minilight.vec)) | ||
|
||
(defstruct triangle | ||
:vertices ; Collection of 3 vectors | ||
:reflect ; Vector with all values in [0,1) | ||
:emit ; Vector with positive values | ||
) | ||
|
||
(defn vertex | ||
"Returns (the zero-indexed) vertex i in the triangle t" | ||
[t i] (nth (:vertices t) i)) | ||
|
||
(defn edge | ||
"Returns the edge in the triangle t from vertex i to vertex j" | ||
[t i j] (sub (vertex t j) (vertex t i))) | ||
|
||
(defn tangent | ||
"Returns a unit vector tangent to the given triangle t" | ||
[t] (normalise (edge t 0 1))) | ||
|
||
(defn normal | ||
"Returns a vector normal to the given triangle t (edge01 x edge12)" | ||
[t] (cross (edge t 0 1) (edge t 1 2))) | ||
|
||
(defn unit-normal | ||
"Returns a unit vector normal to the given triangle t" | ||
[t] (normalise (normal t))) | ||
|
||
(defn area | ||
"Returns the are of the given triangle t" | ||
[t] (/ (norm (normal t)) 2)) | ||
|
||
;(defn read-triangle [string] (map read-vec (re-seq #"\(.*?\)" string))) | ||
; | ||
;(defn create | ||
; "Creates a new triangle from a string of 5 3-tuples. | ||
; The first three are vertex vectors, the fourth the reflectivity vector | ||
; and the last the emitivity vector." | ||
; | ||
; [string] | ||
; (let [ [v0 v1 v2 reflect emit] (read-triangle string) | ||
; edge0 (sub v1 v0) | ||
; edge1 (sub v2 v1) | ||
; edge3 (sub v2 v0) | ||
; tangent (normalise edge0) | ||
; normal (normalise (cross tangent edge1)) | ||
; pa2 (cross edge0 edge1) | ||
; area (/ (norm pa2) 2) ] | ||
; | ||
; (struct triangle | ||
; [v0 v1 v2] edge0 edge3 | ||
; (clamp 0 (- 1 Float/MIN_VALUE) reflect) | ||
; (clamp 0 Float/MAX_VALUE emit) | ||
; tangent normal area))) | ||
|
||
; FIXME: This could be more efficient by only looping through vertices once. | ||
(def TOLERANCE (/ 1.0 1024.0)) | ||
(defn tweak | ||
"Returns a function that adds or subtracts a small amount" | ||
[add-or-sub] | ||
(fn [x] (add-or-sub x (* (+ (Math/abs x) 1.0) TOLERANCE)))) | ||
|
||
(defn bounding-box | ||
"Computes the bounding box for a triangle t, returning the result as | ||
a list of two vectors [lower-corner upper-corner]." | ||
[t] | ||
(let [vs (:vertices t)] | ||
[ (map (tweak -) (apply map min vs)) | ||
(map (tweak +) (apply map max vs)) ])) | ||
|
||
(defn intersect | ||
"Finds the intersection with the triangle t of the ray starting at r0 in | ||
direction rd. The returned value is a positive number a such that r0 + a.rd | ||
is contained within t, or nil if there is no such a." | ||
[t r0 rd] | ||
(let [ e01 (edge t 0 1) | ||
e20 (edge t 2 0) | ||
invdet (invdet e01 rd e20) ] | ||
(if (number? invdet) | ||
(let [ v0 (vertex t 0) | ||
tv (sub r0 v0) | ||
u (* (dot v0 tv) invdet) ] | ||
(if (and (>= u 0) (<= u 1)) | ||
(let [ q (cross tv e01) | ||
v (* (dot rd q) invdet) ] | ||
(if (and (>= v 0) (<= (+ u v) 1)) | ||
(let [a (* (dot e20 q) invdet)] | ||
(if (>= a 0) a))))))))) | ||
|
||
(def rnd (java.util.Random.)) | ||
(defn sample-point | ||
"Returns a random point as a vector from inside the given triangle t" | ||
[t] | ||
(let [ sqr1 (Math/sqrt (.nextFloat rnd)) | ||
r2 (.nextFloat rnd) | ||
a (- 1 sqr1) | ||
b (* (- 1 r2) sqr1) ] | ||
(add (vertex t 0) | ||
(add (scale a (edge t 0 1)) | ||
(scale b (edge t 2 0)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.