(ns cortex.test.vision (:use (cortex world sense util body vision)) (:use cortex.test.body) (:import java.awt.image.BufferedImage) (:import javax.swing.JPanel) (:import javax.swing.SwingUtilities) (:import java.awt.Dimension) (:import javax.swing.JFrame) (:import com.jme3.math.ColorRGBA) (:import com.jme3.scene.Node) (:import com.jme3.math.Vector3f) (:import java.io.File) (:import (com.aurellem.capture Capture RatchetTimer IsoTimer))) (in-ns 'cortex.test.vision) (defn test-pipeline "Testing vision: Tests the vision system by creating two views of the same rotating object from different angles and displaying both of those views in JFrames. You should see a rotating cube, and two windows, each displaying a different view of the cube." ([] (test-pipeline false)) ([record?] (let [candy (box 1 1 1 :physical? false :color ColorRGBA/Blue)] (world (doto (Node.) (.attachChild candy)) {} (fn [world] (let [cam (.clone (.getCamera world)) width (.getWidth cam) height (.getHeight cam)] (add-camera! world cam (comp (view-image (if record? (File. "/home/r/proj/cortex/render/vision/1"))) BufferedImage!)) (add-camera! world (doto (.clone cam) (.setLocation (Vector3f. -10 0 0)) (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)) (comp (view-image (if record? (File. "/home/r/proj/cortex/render/vision/2"))) BufferedImage!)) (let [timer (IsoTimer. 60)] (.setTimer world timer) (display-dilated-time world timer)) ;; This is here to restore the main view ;; after the other views have completed processing (add-camera! world (.getCamera world) no-op))) (fn [world tpf] (.rotate candy (* tpf 0.2) 0 0)))))) (in-ns 'cortex.test.vision) (defn change-color [obj color] ;;(println-repl obj) (if obj (.setColor (.getMaterial obj) "Color" color))) (defn colored-cannon-ball [color] (comp #(change-color % color) (fire-cannon-ball))) (defn gen-worm "create a creature acceptable for testing as a replacement for the worm." [] (nodify "worm" [(nodify "eyes" [(doto (Node. "eye1") (.setLocalTranslation (Vector3f. 0 -1.1 0)) (.setUserData "eye" "(let [retina \"Models/test-creature/retina-small.png\"] {:all retina :red retina :green retina :blue retina})"))]) (box 0.2 0.2 0.2 :name "worm-segment" :position (Vector3f. 0 0 0) :color ColorRGBA/Orange)])) (defn test-worm-vision "Testing vision: You should see the worm suspended in mid-air, looking down at a table. There are four small displays, one each for red, green blue, and gray channels. You can fire balls of various colors, and the four channels should react accordingly. Keys: r : fire red-ball b : fire blue-ball g : fire green-ball : fire white ball" ([] (test-worm-vision false)) ([record?] (let [the-worm (doto (worm)(body!)) vision (vision! the-worm) vision-display (view-vision) fix-display (gen-fix-display) me (sphere 0.5 :color ColorRGBA/Blue :physical? false) x-axis (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red :position (Vector3f. 0 -5 0)) y-axis (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green :position (Vector3f. 0 -5 0)) z-axis (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue :position (Vector3f. 0 -5 0)) ] (world (nodify [(floor) the-worm x-axis y-axis z-axis me]) (merge standard-debug-controls {"key-r" (colored-cannon-ball ColorRGBA/Red) "key-b" (colored-cannon-ball ColorRGBA/Blue) "key-g" (colored-cannon-ball ColorRGBA/Green)}) (fn [world] (light-up-everything world) (speed-up world) (let [timer (IsoTimer. 60)] (.setTimer world timer) (display-dilated-time world timer)) ;; add a view from the worm's perspective (if record? (Capture/captureVideo world (File. "/home/r/proj/cortex/render/worm-vision/main-view"))) (add-camera! world (add-eye! the-worm (first (eyes the-worm))) (comp (view-image (if record? (File. "/home/r/proj/cortex/render/worm-vision/worm-view"))) BufferedImage!)) (set-gravity world Vector3f/ZERO) (add-camera! world (.getCamera world) no-op)) (fn [world _] (.setLocalTranslation me (.getLocation (.getCamera world))) (vision-display (map #(% world) vision) (if record? (File. "/home/r/proj/cortex/render/worm-vision"))) (fix-display world) )))))