390 lines
27 KiB
HTML
390 lines
27 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<link rel="stylesheet" href="../coverage.css"/> <title> mw_engine/heightmap.clj </title>
|
|
</head>
|
|
<body>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
001 (ns ^{:doc "Functions to apply a heightmap to a world.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
002
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
003 Heightmaps are considered only as greyscale images, so colour is redundent
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
004 (will be ignored). Darker shades are higher."
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
005 :author "Simon Brooke"}
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
006 mw-engine.heightmap
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
007 (:require [mikera.image.core :refer [load-image filter-image]]
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
008 [mikera.image.filters :as filters]
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
009 [mw-engine.utils :refer [get-int get-neighbours map-world]]
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
010 [mw-engine.world :refer [make-world]]))
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
011
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
012 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
013 ;;;;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
014 ;;;; mw-engine: the state/transition engine of MicroWorld.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
015 ;;;;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
016 ;;;; This program is free software; you can redistribute it and/or
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
017 ;;;; modify it under the terms of the GNU General Public License
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
018 ;;;; as published by the Free Software Foundation; either version 2
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
019 ;;;; of the License, or (at your option) any later version.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
020 ;;;;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
021 ;;;; This program is distributed in the hope that it will be useful,
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
022 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
023 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
024 ;;;; GNU General Public License for more details.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
025 ;;;;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
026 ;;;; You should have received a copy of the GNU General Public License
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
027 ;;;; along with this program; if not, write to the Free Software
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
028 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
029 ;;;; USA.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
030 ;;;;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
031 ;;;; Copyright (C) 2014 Simon Brooke
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
032 ;;;;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
033 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
034
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
035 (defn tag-property
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
036 "Set the value of this `property` of this cell from the corresponding pixel of this `heightmap`.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
037 If the heightmap you supply is smaller than the world, this will break.
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
038
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
039 * `world` not actually used, but present to enable this function to be
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
040 passed as an argument to `mw-engine.utils/map-world`, q.v.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
041 * `cell` a cell, as discussed in world.clj, q.v. Alternatively, a map;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
042 * `property` the property (normally a keyword) whose value will be set on the cell.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
043 * `heightmap` an (ideally) greyscale image, whose x and y dimensions should
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
044 exceed those of the world of which the `cell` forms part."
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
045 ([_ cell property heightmap]
|
|
</span><br/>
|
|
<span class="covered" title="5 out of 5 forms covered">
|
|
046 (tag-property cell property heightmap))
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
047 ([cell property heightmap]
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
048 (merge cell
|
|
</span><br/>
|
|
<span class="covered" title="2 out of 2 forms covered">
|
|
049 {property
|
|
</span><br/>
|
|
<span class="covered" title="5 out of 5 forms covered">
|
|
050 (+ (get-int cell property)
|
|
</span><br/>
|
|
<span class="covered" title="2 out of 2 forms covered">
|
|
051 (- 256
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
052 (abs
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
053 (mod
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
054 (.getRGB heightmap
|
|
</span><br/>
|
|
<span class="covered" title="4 out of 4 forms covered">
|
|
055 (get-int cell :x)
|
|
</span><br/>
|
|
<span class="covered" title="4 out of 4 forms covered">
|
|
056 (get-int cell :y)) 256))))})))
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
057
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
058 (defn tag-gradient
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
059 "Set the `gradient` property of this `cell` of this `world` to the difference in
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
060 altitude between its highest and lowest neghbours."
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
061 [world cell]
|
|
</span><br/>
|
|
<span class="covered" title="11 out of 11 forms covered">
|
|
062 (let [heights (remove nil? (map :altitude (get-neighbours world cell)))
|
|
</span><br/>
|
|
<span class="partial" title="5 out of 6 forms covered">
|
|
063 highest (cond (empty? heights) 0 ;; shouldn't happen
|
|
</span><br/>
|
|
<span class="covered" title="4 out of 4 forms covered">
|
|
064 :else (apply max heights))
|
|
</span><br/>
|
|
<span class="partial" title="5 out of 6 forms covered">
|
|
065 lowest (cond (empty? heights) 0 ;; shouldn't
|
|
</span><br/>
|
|
<span class="covered" title="4 out of 4 forms covered">
|
|
066 :else (apply min heights))
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
067 gradient (- highest lowest)]
|
|
</span><br/>
|
|
<span class="covered" title="6 out of 6 forms covered">
|
|
068 (merge cell {:gradient gradient})))
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
069
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
070 (defn tag-gradients
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
071 "Set the `gradient` property of each cell in this `world` to the difference in
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
072 altitude between its highest and lowest neghbours."
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
073 [world]
|
|
</span><br/>
|
|
<span class="not-covered" title="0 out of 4 forms covered">
|
|
074 (map-world world tag-gradient))
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
075
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
076 (defn tag-altitude
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
077 "Set the altitude of this cell from the corresponding pixel of this heightmap.
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
078 If the heightmap you supply is smaller than the world, this will break.
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
079
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
080 * `world` not actually used, but present to enable this function to be
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
081 passed as an argument to `mw-engine.utils/map-world`, q.v.;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
082 * `cell` a cell, as discussed in world.clj, q.v. Alternatively, a map;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
083 * `heightmap` an (ideally) greyscale image, whose x and y dimensions should
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
084 exceed those of the world of which the `cell` forms part."
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
085 ([_ cell heightmap]
|
|
</span><br/>
|
|
<span class="covered" title="5 out of 5 forms covered">
|
|
086 (tag-property cell :altitude heightmap))
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
087 ([cell heightmap]
|
|
</span><br/>
|
|
<span class="not-covered" title="0 out of 5 forms covered">
|
|
088 (tag-property cell :altitude heightmap)))
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
089
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
090 (defn apply-heightmap
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
091 "Apply the image file loaded from this path to this world, and return a world whose
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
092 altitudes are modified (added to) by the altitudes in the heightmap. It is assumed that
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
093 the heightmap is at least as large in x and y dimensions as the world. Note that, in
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
094 addition to setting the `:altitude` of each cell, this function also sets the `:gradient`.
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
095
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
096 * `world` a world, as defined in `world.clj`, q.v.; if world is not supplied,
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
097 a world the size of the heightmap will be created;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
098 * `imagepath` a file path or URL which indicates an (ideally greyscale) image file."
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
099 ([world imagepath]
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
100 (let [heightmap (filter-image
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
101 (load-image imagepath)
|
|
</span><br/>
|
|
<span class="covered" title="2 out of 2 forms covered">
|
|
102 (filters/grayscale))]
|
|
</span><br/>
|
|
<span class="covered" title="2 out of 2 forms covered">
|
|
103 (map-world
|
|
</span><br/>
|
|
<span class="covered" title="7 out of 7 forms covered">
|
|
104 (map-world world tag-altitude (list heightmap))
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
105 tag-gradient)))
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
106 ([imagepath]
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
107 (let [heightmap (filter-image
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
108 (load-image imagepath)
|
|
</span><br/>
|
|
<span class="covered" title="2 out of 2 forms covered">
|
|
109 (filters/grayscale))
|
|
</span><br/>
|
|
<span class="covered" title="4 out of 4 forms covered">
|
|
110 world (make-world (.getWidth heightmap) (.getHeight heightmap))]
|
|
</span><br/>
|
|
<span class="covered" title="2 out of 2 forms covered">
|
|
111 (map-world
|
|
</span><br/>
|
|
<span class="covered" title="7 out of 7 forms covered">
|
|
112 (map-world world tag-altitude (list heightmap))
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
113 tag-gradient))))
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
114
|
|
</span><br/>
|
|
<span class="covered" title="1 out of 1 forms covered">
|
|
115 (defn apply-valuemap
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
116 "Generalised from apply-heightmap, set an arbitrary property on each cell
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
117 of this `world` from the values in this (ideally greyscale) heightmap.
|
|
</span><br/>
|
|
<span class="blank" title="0 out of 0 forms covered">
|
|
118
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
119 * `world` a world, as defined in `world.clj`, q.v.;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
120 * `imagepath` a file path or URL which indicates an (ideally greyscale) image file;
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
121 * `property` the property of each cell whose value should be added to from the
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
122 intensity of the corresponding cell of the image."
|
|
</span><br/>
|
|
<span class="not-tracked" title="0 out of 0 forms covered">
|
|
123 [world imagepath property]
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
124 (let [heightmap (filter-image
|
|
</span><br/>
|
|
<span class="covered" title="3 out of 3 forms covered">
|
|
125 (load-image imagepath)
|
|
</span><br/>
|
|
<span class="covered" title="2 out of 2 forms covered">
|
|
126 (filters/grayscale))]
|
|
</span><br/>
|
|
<span class="covered" title="8 out of 8 forms covered">
|
|
127 (map-world world tag-property (list property heightmap))))
|
|
</span><br/>
|
|
</body>
|
|
</html>
|