summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjonascarpay <>2017-12-07 00:29:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2017-12-07 00:29:00 (GMT)
commitfe796f976ed3bc0980012e4a6d49eff0406e077b (patch)
treea9bd322884f8f4ce8c195d75f551646f19a1d773
version 0.1.0.0HEAD0.1.0.0master
-rw-r--r--LICENSE30
-rw-r--r--README.md17
-rw-r--r--Setup.hs2
-rw-r--r--simplemesh.cabal23
-rw-r--r--src/Data/Mesh.hs40
5 files changed, 112 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..32cc34e
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,30 @@
+Copyright Jonas Carpay (c) 2017
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Jonas Carpay nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f3a5287
--- /dev/null
+++ b/README.md
@@ -0,0 +1,17 @@
+# simplemesh
+
+Utility functions for the generation of primitive meshes, similar to Rust's [genmesh](https://github.com/gfx-rs/genmesh).
+Ideally, I want to be able to unify shapes in gloss and apecs-physics, but for now, this is just a collection of functions that I find myself writing a lot.
+If you know of a more formal library (or have ideas on how to make this one more formal), let me know.
+If you _don't_ know of another library and want me to push this one to Hackage, also let me know.
+
+```
+> vertices (rectangle 2 3)
+[ V2 0.0 0.0, V2 0.0 3.0, V2 2.0 3.0, V2 2.0 0.0 ]
+
+> edges (centeredRectangle 2 3)
+[ (V2 (-1.0) (-1.5), V2 (-1.0) 1.5)
+, (V2 (-1.0) 1.5, V2 1.0 1.5)
+, (V2 1.0 1.5, V2 1.0 (-1.5))
+, (V2 1.0 (-1.5), V2 (-1.0) (-1.5)) ]
+```
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
index 0000000..9a994af
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/simplemesh.cabal b/simplemesh.cabal
new file mode 100644
index 0000000..be00a4c
--- /dev/null
+++ b/simplemesh.cabal
@@ -0,0 +1,23 @@
+name: simplemesh
+version: 0.1.0.0
+synopsis: Generators for primitive meshes
+description: Generators for primitive meshes
+homepage: https://github.com/jonascarpay/simplemesh#readme
+license: BSD3
+license-file: LICENSE
+author: Jonas Carpay
+maintainer: jonascarpay@gmail.com
+category: Web
+build-type: Simple
+extra-source-files: README.md
+cabal-version: >=1.10
+
+library
+ hs-source-dirs: src
+ exposed-modules: Data.Mesh
+ build-depends: base >= 4.7 && < 5, linear
+ default-language: Haskell2010
+
+source-repository head
+ type: git
+ location: https://github.com/jonascarpay/simplemesh
diff --git a/src/Data/Mesh.hs b/src/Data/Mesh.hs
new file mode 100644
index 0000000..c03e89e
--- /dev/null
+++ b/src/Data/Mesh.hs
@@ -0,0 +1,40 @@
+module Data.Mesh where
+
+import Linear
+
+type Vec2 = V2 Double
+
+data Mesh2D
+ = Rectangle Vec2 Vec2
+ | Line Vec2 Vec2
+ | Triangle Vec2 Vec2 Vec2
+
+vertices :: Mesh2D -> [Vec2]
+vertices (Rectangle (V2 x y) (V2 w h)) = [V2 x y, V2 x (y+h), V2 (x+w) (y+h), V2 (x+w) y]
+vertices (Line o s) = [o, o + s]
+vertices (Triangle o a b) = [o, o+a, o+b]
+
+vertexLoop :: Mesh2D -> [Vec2]
+vertexLoop m = verts ++ [head verts]
+ where verts = vertices m
+
+edges :: Mesh2D -> [(Vec2, Vec2)]
+edges m = let verts = vertices m
+ in zip verts (tail . cycle $ verts)
+
+centeredRectangle :: Double -> Double -> Mesh2D
+centeredRectangle w h = Rectangle (V2 x y) (V2 w h)
+ where x = negate (w/2)
+ y = negate (h/2)
+
+rectangle :: Double -> Double -> Mesh2D
+rectangle w h = Rectangle 0 (V2 w h)
+
+horizontalLine, verticalLine :: Double -> Mesh2D
+horizontalLine length = Line (V2 ((-length)/2) 0) (V2 (length/2) 0)
+verticalLine length = Line (V2 ((-length)/2) 0) (V2 (length/2) 0)
+
+translate :: Vec2 -> Mesh2D -> Mesh2D
+translate r (Rectangle o s) = Rectangle (o+r) s
+translate r (Line o s) = Line (o+r) s
+translate r (Triangle o a b) = Triangle (o+r) a b