summaryrefslogtreecommitdiff
path: root/cgi.scala
blob: 11a7fd63915e103ca2ef0c7fb46d509f54d6ffe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import net.iximeow.raytrace._
import Objects._
import scala.util.Try
import java.time._

object cgi_main extends App {
  val params = System.getenv("QUERY_STRING")
  val parsedObjects = params.split("&").map(parse _)
  println("HTTP/1.1 200 OK\nContent-type: text/html\n\n")
  println("<html><head><style>.mono { font-family: monospace; }</style></head><body>")
  val content = if (!parsedObjects.exists(_.isInstanceOf[Succ])) {
    usage()
  } else {
    render(parsedObjects)
  }
  println(content)
  println("</body></html>")

  def render(objects: Seq[ParseResult]) = {
    val imgPath = renderImage(objects)
    s"""<img src="/projects/raycast/${imgPath}"></img>"""
  }

  def renderImage(objects: Seq[ParseResult]): String = {
    val sceneElems = objects.foldLeft(Seq.empty[Surface]) {
      case (elems: Seq[Surface], o: ParseResult) => o match {
        case Succ(Right(xs)) => elems ++ xs
        case _ => elems
      }
    }
    val rays = objects.foldLeft(Seq.empty[Ray]) {
      case (rays: Seq[Ray], o: ParseResult) => o match {
        case Succ(Left(xs)) => rays ++ xs
        case _ => rays
      }
    }
    val res = objects.foldLeft(Option[Res](null)) {
      case (r: Option[Res], o: ParseResult) => o match {
        case x: Res => Some(x)
        case _ => r
      }
    }.getOrElse(Res(800, 600))
    val version = objects.foldLeft(Option[Version](null)) {
      case (v: Option[Version], o: ParseResult) => o match {
        case x: Version => Some(x)
        case _ => v
      }
    }
    val fuccs = objects.foldLeft(Seq.empty[String]) {
      case (fuc: Seq[String], o: ParseResult) => o match {
        case Fucc(err) => fuc :+ err
        case _ => fuc
      }
    }

    val scene = Scene(sceneElems)

    println("<p>Rendering a scene with " + sceneElems.size + " surfaces and " + rays.size + " rays</p>")

    scene.render(scale = 20, color = 0xc0c0c0, normals = true)
    rays
      .flatMap(x => scene.cast(x, 63))
      .foreach(_.renderTo(scene.buffer, 20, res.resX / 2, res.resY / 2))

    /*
    val destFile = DateTimeFormatter.ofPattern("YYYYMMdd-HHmmss")
      .format(LocalDateTime.now())
      */
     // because shitty vps doesn't have java 8 atm
    val destFile = System.currentTimeMillis + ".bmp"
    scene.save(destFile)
    destFile
  }

  sealed trait ParseResult { }
  case class Succ(surface: Either[Seq[Ray], Seq[Surface]]) extends ParseResult
  case class Fucc(error: String) extends ParseResult
  case class Version(version: String) extends ParseResult
  case class Res(resX: Int, resY: Int) extends ParseResult

  def parse(str: String): ParseResult = {
    val parts = str.split(":").toList
    parts match {
      case "Ray" :: rest => parseRay(rest)
      case "Light" :: rest => parseLight(rest)
      case "Segment" :: rest => parseSegment(rest)
      case "ParabolaMirror" :: rest => parseParabolicMirror(rest)
      case "SphericalMirror" :: rest => parseSphericalMirror(rest)
      case "ParabolicLens" :: rest => parseParabolicLens(rest)
      case "res" :: rest => parseRes(rest)
      case "version" :: rest => parseVersion(rest)
      case x => Fucc("Unkown param: " + x)
    }
  }

  private def parseDoubles(params: Seq[String], count: Int, tpe: String): Either[Fucc, Seq[Double]] = {
    if (params.length != count) return Left(Fucc("Invalid parameters for " + tpe + ": " + params.mkString(":")))
    val parsed = params.map(x => Try(java.lang.Double.parseDouble(x)))
    if (parsed.exists(_.isFailure)) return Left(Fucc(
      parsed.zip(params).map { case (res, src) => if (res.isFailure) {
        "<span class=red>x</span>" // maybe escape and print the invalid value here one day...
      } else {
        "_"
      }}.mkString(":")
    ))

    val succ = parsed.flatMap(_.toOption)
    if (succ.length != count) return Left(Fucc("oops"))
    Right(succ)
  }

  private def parseLight(params: Seq[String]): ParseResult = {
    parseDoubles(params, 6, "light") match {
      case Left(fucc) => fucc
      case Right(parsed) =>
        Succ(Left(Scene.rays(parsed(4).toInt, parsed(5), Point(parsed(1), parsed(2)), Point(4 * Math.cos(parsed(0)), 4 * Math.sin(parsed(0))))))
    }
  }

  private def parseRay(params: Seq[String]): ParseResult = {
    parseDoubles(params, 4, "ray") match {
      case Left(fucc) => fucc
      case Right(parsed) =>
        Succ(Left(Seq(Ray(parsed(0), parsed(1), Point(parsed(2), parsed(3))))))
    }
  }

  private def parseSegment(params: Seq[String]): ParseResult = {
    parseDoubles(params, 4, "segment") match {
      case Left(fucc) => fucc
      case Right(parsed) =>
        Succ(Right(Seq(Segment(parsed(0), parsed(1), Point(parsed(2), parsed(3))))))
    }
  }

  private def parseParabolicMirror(params: Seq[String]) =
    parseDoubles(params, 7, "parabolic mirror") match {
      case Left(fucc) => fucc
      case Right(parsed) =>
        Succ(Right(Scene.generateParabola(parsed(0), parsed(1), parsed(2), 0, parsed(3).toInt, Point(parsed(4), parsed(5)), parsed(6))))
    }

  private def parseSphericalMirror(params: Seq[String]) =
    parseDoubles(params, 7, "spherical mirror") match {
      case Left(fucc) => fucc
      case Right(parsed) =>
        Succ(Right(Scene.generateMirror(parsed(0), parsed(1).toInt, parsed(2), Point(parsed(3), parsed(4)), parsed(5))))
    }

  private def parseParabolicLens(params: Seq[String]) =
    parseDoubles(params, 5, "parabolic lens") match {
      case Left(fucc) => fucc
      case Right(parsed) =>
        Succ(Right(Seq(ParabolicLens(Point(parsed(0), parsed(1)), parsed(2), parsed(3), parsed(4)))))
    }

  private def parseRes(params: Seq[String]) =
    parseDoubles(params, 2, "res") match {
      case Left(fucc) => fucc
      case Right(parsed) =>
        Res(parsed(0).toInt, parsed(1).toInt)
    }

  private def parseVersion(params: Seq[String]) =
    parseDoubles(params, 1, "version") match {
      case Left(fucc) => fucc
      case Right(parsed) =>
        Version(parsed(0).toString)
    }

  def usage() = {
    """|<h2>USAGE:</h2>
    |<p style="font-family:monospace">object:params&object2:params&object3:params&....</p>
    |<p>Version may be specified by beginning with <span class=mono>version:str</span>. By default, assumes the latest version of each object. Some object's parameters may change from version to version.</p>
    |<p>Additionally, you may specify desired resolutions by providing <span class=mono>res:x:y</span> where <span class=mono>x</span> and <span class=mono>y</span> are integers.</p>
    |<p>(by default, resolution is 800x600)</p>
    |</br>
    |<h3>OBJECTS:</h3>
    |<ul>
    |  <li><pre class=mono>Ray:dx:dy:xi:yi                                     </pre><p>Ray to trace.</p></li>
    |  <li><pre class=mono>Light:rotation:atx:aty:number:spacing               </pre><p>Bulk batch of light from some place.</p></li>
    |  <li><pre class=mono>Segment:dx:dy:xi:yi                                 </pre><p>Reflective surface. Careful of normals.</p></li>
    |  <li><pre class=mono>ParabolaMirror:a:b:w_i:segments:atx:aty:rotated   </pre><p>Parabola with coefficients like x(t) = a * t, y(t) = b * t^2, centered at <span class=mono>(atx, aty)</span>, rotated <span class=mono>rotated</span> radians with <span class=mono>segments</span> segments of approximation.</p></li>
    |  <li><pre class=mono>SphericalMirror:r:segments:arcSize:atx:aty:rotated  </pre><p>doc: TODO</p></li>
    |  <li><pre class=mono>ParabolicLens:atx:aty:rotation:radius:focal         </pre><p>doc: TODO. ALSO, BUGGY.</p></li>
    |</ul>""".stripMargin
  }
}