Racket - Final project PDF

Title Racket - Final project
Course Intro To Computer Science-1
Institution University of Chicago
Pages 10
File Size 62.9 KB
File Type PDF
Total Downloads 17
Total Views 125

Summary

Final project...


Description

#lang typed/racket ;; CMSC 15100 Winter 2019 ;; Project Code ;; camera.rkt module ;; Janet Gao ;; ;; This module implements the Camera abstraction ;; ;; load custom definitions ;; (require "../include/cs151-core.rkt") ;; load image library definitions ;; (require "../include/cs151-image.rkt") ;; load math module ;; (require "math-util.rkt") ;; load color module ;; (require "color.rkt") ;; the representation of a Camera for the ray tracer. The camera ;; is assumed to be at the origin looking down the -Z axis. (define-struct Camera [(wid : Natural) ;; width of image (ht : Natural) ;; height of image (n-samples : Natural) ;; number of samples per pixel (focal-len : Float)]) ;; distance to image plane (in -Z direction) ;; A Pixel-Renderer is a function that takes the row and column of a pixel ;; and produces a Racket Image-Library Color (define-type Pixel-Renderer (Natural Natural -> Color)) (: foreach-pixel : Camera Pixel-Renderer -> Image) ;; extracts the height and width of an image from the Camera struct ;; which uses the pixel renderer to compute a color ;; which are then assembled into a list and the resulting list into an image (define (foreach-pixel cam pr) (if (or (= (camera-wid cam) 0) (= (camera-ht cam) 0)) empty-image (color-list->bitmap (row-helper f1 (Camera-ht cam) (Camera-wid cam) '() '()

(Camera-wid cam) (Camera-ht cam))))) (: row-helper : Natural Natural (Listof Color) (Listof Color) -> (Listof Color)) ;; iterates over the rows (define (row-helper f3 row column acc-c acc-r) (if (> row 0) (row-helper f3 (- row 1) column acc-c (append (column-helper f3 row column acc-c) acc-r)) acc-r)) (: column-helper : Natural Natural (Listof Color) -> (Listof Color)) ;; iterates over the columns (define (column-helper f2 row column acc) (if (> column 0) (aux-column f2 row (- column 1) (cons (f2 row column) acc)) acc)) ;(: color-list->bitmap : (Listof Color) Natural Natural -> Image) ;; takes list of colors for pixels, image width, image height ;; returns an image ;(define (color-list->bitmap l n1 n2) ; (row-helper (- (Camera-ht cam) 1) '()) ; (Camera-wid cam) (Camera-ht cam)) ; Wow. This was given. I did not mean to try to write color-list->bitmap. (: make-pixel-renderer : (Natural Natural -> RGB) (RGB -> Color) -> Pixel-Renderer) ;; takes a function that computes an RGB value for a pixel and an rgb->color function ;; composes them to produce a pixel renderer function (define (make-pixel-renderer f g) (lambda ([X: Natural] [Y: Natural]) (g (f X Y)))) "== eyeball test foreach-pixel" (local {(define wid : Natural 400) (define ht : Natural 200)} (foreach-pixel (Camera wid ht 1 0.0) (make-pixel-renderer (lambda ([r : Natural] [c : Natural]) (RGB (/ (->fl c) (->fl wid)) (/ (->fl (- ht r)) (->fl ht)) 0.2)) rgb->color))) (: ray-for-pixel : Camera -> (Natural Natural -> Ray)) ;; takes a camera and returns a function for generating a ray for ;; a pixel specified by row and column

(define (ray-for-pixel cam) (local {(define x0 : Float 2) (define pw : Float (/ 2 cam-w)) (define y0 : Float (/ (* 2 cam-h) (cam-w)))} (lambda (make-ray (fl3-normalize (0.0 0.0 0.0) (+ x0 (*c pw)) (- (y0 (* (camera-flen) pw)))))))) (check-within ((ray-for-pixel (Camera 11 11 11 11)) 6 6) (Ray (fl3 0.10 -0.10 20) (fl3 0.00096 -0.0009 0.99)) 0.01) (: pixel->rgb : Camera (Ray -> RGB) -> (Natural Natural -> RGB)) ;; given a camera and a ray-tracing function, return a function that ;; traces the ray for the given pixel (define (pixel->rgb cam r) (lambda ([x0: Float] [y0: Float]) (r ((ray-for-pixel cam) x0 y0)))) (: ray->rgb : Ray -> RGB) ;; a function for testing ray generation. It maps a ray to a color in ;; the white-to-blue range based on the Y component of the ray's direction ;; vector. (define (ray->rgb ray) (match ray [(Ray _ dir) (local {(define t : Float (* 0.5 (+ 1.0 (Float3-y dir))))} (rgb-lerp rgb-white t (RGB 0.5 0.7 1.0)))]))

(: rays-for-pixel : Camera -> (Natural Natural -> (Listof Ray))) ;; ;; given a camera, return a function that maps pixel coordinates in ;; the image plane to a list of rays from the camera through the pixel. ;; The number of rays is determined by the n-samples field of the ;; Camera.

(: antialias-pixel->rgb : Camera (Ray -> RGB) -> Natural Natural -> RGB) ;; given a camera and a ray-tracing function, return a function that ;; traces a list of rays for the given pixel and returns their average

"== background test" (define cam-400x200x1 (Camera 400 200 1 0.25)) (foreach-pixel cam-400x200x1 (make-pixel-renderer (pixel->rgb cam-400x200x1 ray->rgb)

rgb->color)) "== background" (define cam-400x200x1 (Camera 400 200 1 0.25)) (foreach-pixel cam-400x200x1 (make-pixel-renderer (pixel->rgb cam-400x200x1 ray->rgb) rgb->color)) "== test ray-for-pixel" (foreach-pixel cam-400x200x1 (make-pixel-renderer (pixel->rgb cam-400x200x1 (lambda ([ray : Ray]) (match ray [(Ray _ dir) (RGB (* 0.5 (+ 1.0 (Float3-x dir))) (* 0.5 (+ 1.0 (Float3-y dir))) (* 0.5 (+ 1.0 (Float3-z dir))))]))) rgb->color)) "== eyeball test for background (16 samples)" (define cam-200x100x16 (Camera 200 100 16 0.25)) (foreach-pixel cam-200x100x16 (make-pixel-renderer (antialias-pixel->rgb cam-200x100x16 ray->rgb) rgb->color))

;;;;;;;;;; ;; Exports ;;;;;;;;;; (provide (struct-out Camera)) (provide foreach-pixel make-pixel-renderer pixel->rgb ray->rgb) #lang typed/racket ;; CMSC 15100 Winter 2019 ;; Project Code ;; trace.rkt module ;; Janet Gao ;; ;; Ray casting and recursive ray tracing ;; ;; load custom definitions

;; (require "../include/cs151-core.rkt") ;; load image library definitions ;; (require "../include/cs151-image.rkt") ;; load testing infrastructure ;; (require typed/test-engine/racket-tests) ;; project modules ;; (require "util.rkt") (require "math-util.rkt") (require "color.rkt") (require "camera.rkt") (require "material.rkt") (require "object.rkt") (require "sphere.rkt") (: cast-ray-in-world : Object -> Ray -> RGB) ;; takes an object and a ray ;; tests for intersection with the object ;; computes color as soon as ray hits an object (define (cast-ray-in-world o) (if (lambda ([ray : Ray]) (match ((Object-hit? o) ray 0.0 +inf.0) ['Miss (ray->rgb ray)] [ht (match (get-hit-info ht ray) ['None (ray->rgb ray)] [(Some h) (Hit-Info-aten)])]))))

"== eyeball test for ray cast: flat material" (define sphere-0 (make-sphere (Float3 0.0 0.0 -2.0) 0.5 (flat-material (RGB 1.0 0.0 0.0)))) (define cam-400x200x1 (Camera 400 200 1 1.0)) (foreach-pixel cam-400x200x1 (make-pixel-renderer (pixel->rgb cam-400x200x1 (cast-ray-in-world sphere-0)) rgb->color)) "== eyeball test for ray cast: normal material" (define sphere-1 (make-sphere (Float3 0.0 0.0 -2.0) 0.5 normal-material))

(foreach-pixel cam-400x200x1 (make-pixel-renderer (pixel->rgb cam-400x200x1 (cast-ray-in-world sphere-1)) rgb->color))

"== eyeball test for ray cast: normal material and two spheres" (define sphere-2 (make-sphere (Float3 0.0 -100.5 -2.0) 100.0 normal-material)) (define world-1 (list->object (list sphere-1 sphere-2))) (foreach-pixel cam-400x200x1 (make-pixel-renderer (pixel->rgb cam-400x200x1 (cast-ray-in-world world-1)) rgb->color))

"== eyeball test for ray cast (100 samples): normal material and two spheres" (define cam-400x200x100 (Camera 400 200 100 1.0)) (foreach-pixel cam-400x200x100 (make-pixel-renderer (antialias-pixel->rgb cam-400x200x100 (cast-ray-in-world world-1)) rgb->color))

(: trace-ray-in-world : Object Natural -> Ray -> RGB) ;; Given a world and a maximum tracing depth, this function returns ;; a function that will recursively trace a ray through the world ;; to compute a color (define (trace-ray-in-world o n) (lambda ([ray : Ray]) (trace Object ray )))

(: lambertian-material : RGB -> Material) ;; A Lambertian surface of the given color (define (lambertian-material rgb) (Material (lambda ([ray : Ray] [hit : Hit]) (local { (define P (Hit-pt hit)) (define N (Hit-norm hit)) (define Q (ray-point-at ray (random)) ) (define S (fl3+ N Q))

(define R (make-ray (Float3 0.0 0.0 0.0) (fl3-scale (* (Hit-t hit) (/ 1.0 (fl3-length S))) S)))} (Some (Hit-Info rgb1 R))))))

"== eyeball test for ray trace (100 samples): lambertian material and two spheres" (define sphere-3 (make-sphere (Float3 0.0 0.0 -2.0) 0.5 (lambertian-material (rgb-gray 0.5)))) (define sphere-4 (make-sphere (Float3 0.0 -100.5 -2.0) 100.0 (lambertian-material (rgb-gray 0.5)))) (define world-2 (list->object (list sphere-3 sphere-4))) (define cam-200x100x100 (Camera 200 100 100 1.0)) (foreach-pixel cam-200x100x100 (make-pixel-renderer (antialias-pixel->rgb cam-200x100x100 (trace-ray-in-world world-2 5)) gamma-rgb->color))

(: metal-material : RGB Float -> Material) ;; a metalic surface

"== eyeball test for ray trace (100 samples): lambertian and metal; four spheres" (define sphere-5 (make-sphere (Float3 0.0 0.0 -2.0) 0.5 (lambertian-material (RGB 0.8 0.3 0.3)))) (define sphere-6 (make-sphere (Float3 0.0 -100.5 -2.0) 100.0 (lambertian-material (RGB 0.8 0.8 0.0)))) (define sphere-7 (make-sphere (Float3 1.0 0.0 -2.0) 0.5 (metal-material (RGB 0.8 0.6 0.2) 1.0))) (define sphere-8 (make-sphere (Float3 -1.0 0.0 -2.0) 0.5 (metal-material (RGB 0.8 0.8 0.8) 0.1))) (define world-3 (list->object (list sphere-5 sphere-6 sphere-7 sphere-8))) (foreach-pixel cam-200x100x100 (make-pixel-renderer (antialias-pixel->rgb cam-200x100x100 (trace-ray-in-world world-3 20)) gamma-rgb->color))

(: ray-tracer : Camera Object -> Image) ;; Given a camera and world object, render a scene using a depth limit of 20.

"== eyeball test for ray-tracer (100 samples): lambertian and metal; four spheres" (ray-tracer cam-200x100x100 world-3)

;;;;;;;;;; ;; Exports ;;;;;;;;;; (provide cast-ray-in-world trace-ray-in-world ray-tracer) #lang typed/racket ;; CMSC 15100 Winter 2019 ;; Project Code ;; material.rkt module ;; Janet Gao ;; ;; This module contains the definition of the Hit and Material types ;; ;; load custom definitions ;; (require "../include/cs151-core.rkt") ;; load image library definitions ;; (require "../include/cs151-image.rkt") ;; load testing infrastructure ;; (require typed/test-engine/racket-tests) ;; project modules ;; (require "util.rkt") (require "math-util.rkt") (require "color.rkt") ;; A (Hit ...) records an intersection (aka "hit") between a Ray R ;; and an object. (define-struct Hit ([t : Float] ;; value of t such that R(t) is the point ;; where the ray R intersects the object [pt : Float3] ;; The point of intersection [norm : Float3] ;; Unit-length normal vector at pt [material : Material])) ;; The material of the object at pt ;; A (Hit-Info rgb vec) value represents the information about a ray's hit on the

;; surface of an object. The rgb value is the attenuation factor and the ray ;; is the reflection/scatter ray for the surface (define-struct Hit-Info ([aten : RGB] ;; attenuation factor [reflect-ray : Ray])) ;; ray that is scattered or reflected from surface ;; A Material is a struct containing a scatter function that takes a ;; Ray and a Hit record for the Ray and returns 'None if there is no ;; color contribution and returns some hit info otherwise ;; (define-struct Material ([scatter : (Ray Hit -> (Option Hit-Info))])) (: get-hit-info : Hit Ray -> (Option Hit-Info)) ;; give a hit record and a ray, get the optional hit info for the ;; hit record's material (define (get-hit-info hit ray) (match hit [(Hit _ _ _ (Material scat)) (scat ray hit)])) (: make-info : RGB Ray -> (Option Hit-Info)) ;; helper function that builds some hit info (define (make-info rgb ray) (Some (Hit-Info rgb ray))) (: flat-material : RGB -> Material) ;; flat shading with the given color (define (flat-material rgb) (Material (lambda ([ray : Ray] [hit : Hit]) (make-info rgb (Ray fl3-zero fl3-zero))))) (: normal-material : Material) ;; computes the surface color by mapping the (x y z) components ;; of normal vector to the red, green, and blue channels of an RGB value (define (normal-material rgb) (match rgb (make-info rgb float normal) ;[(Ray _ dir) ; (local ; {(define t : Float (* 0.5 (+ 1.0 (Float3-y dir))))} ; (rgb-lerp rgb-white t (RGB 0.5 0.7 1.0)))])) ;;;;;;;;;; ;; Exports ;;;;;;;;;; (provide (struct-out Hit) (struct-out Hit-Info)

(struct-out Material)) (provide get-hit-info flat-material)...


Similar Free PDFs