-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate Arbitrary PlaneGraphs (#254) + adding a basic PLYWriter library
* getting started on the random graph business * typo * using the random instances * working on generating random graphs * progress on makign it compile * generating random plane graphs * some cleaning up * remdering random plane graphs * creating a graph from connected segments * some cleaning up * trying to get the upper hull thing to work as well actually * uncommenting unused code for doctest * quick ply-writer * deleting the PlaneGraph drawing stuff * setting up some more tests * creating a demo to render a lower envelope * plywriter does something at least :)
- Loading branch information
Showing
20 changed files
with
723 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{-# LANGUAGE QuasiQuotes #-} | ||
module Main(main) where | ||
|
||
import Control.Lens | ||
import qualified Data.List.NonEmpty as NonEmpty | ||
import HGeometry.Instances () | ||
import HGeometry.Number.Real.Rational | ||
import HGeometry.PlaneGraph | ||
import HGeometry.PlaneGraph.Instances | ||
import HGeometry.Point | ||
import Ipe | ||
import System.OsPath | ||
import Test.QuickCheck | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
type R = RealNumber 5 | ||
|
||
renderGraph :: PlaneGraph QuickCheckWorld (Point 2 R) () () -> IpePage R | ||
renderGraph gr = fromContent $ | ||
concat [ [ iO $ defIO p | p <- gr^..vertices ] | ||
, [ iO $ defIO seg | seg <- gr^..edgeSegments ] | ||
] | ||
|
||
main :: IO () | ||
main = do | ||
grs <- NonEmpty.fromList <$> sample' arbitrary | ||
let outFp = [osp|foo.ipe|] | ||
writeIpeFile outFp $ ipeFile (renderGraph <$> grs) | ||
|
||
-- (grs :: [PlaneGraph QuickCheckWorld (Point 2 R) () ()]) <- sample' arbitrary | ||
-- mapM_ print grs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{-# LANGUAGE QuasiQuotes #-} | ||
module Main(main) where | ||
|
||
import Control.Lens | ||
import qualified Data.Foldable as F | ||
import qualified Data.List.NonEmpty as NonEmpty | ||
import HGeometry.Ext | ||
import HGeometry.Number.Real.Rational | ||
import HGeometry.Plane.LowerEnvelope | ||
import HGeometry.PlaneGraph | ||
import HGeometry.PlaneGraph.Instances | ||
import HGeometry.Point | ||
import HGeometry.Triangle | ||
import HGeometry.VoronoiDiagram.ViaLowerEnvelope | ||
import Ipe | ||
import PLY.Writer | ||
import System.OsPath | ||
import Test.QuickCheck | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
type R = RealNumber 5 | ||
|
||
|
||
myPlanes = NonEmpty.fromList $ zipWith (\i p -> pointToPlane p :+ (i,p)) [0..] | ||
[ Point2 16 80 | ||
, Point2 64 48 | ||
, Point2 208 128 | ||
, Point2 176 48 | ||
, Point2 96 112 | ||
, Point2 128 80 | ||
, Point2 48 144 | ||
] | ||
|
||
verticesOf = NonEmpty.fromList . foldMap F.toList . trianglesOf | ||
trianglesOf _ = [ Triangle (origin :+ 0) (Point3 10 0 1 :+ 1) (Point3 0 10 2 :+ 2) ] | ||
|
||
|
||
-- \case | ||
-- ParallelStrips _ -> undefined | ||
-- ConnectedEnvelope env -> undefined | ||
|
||
-- trianglesOf env = [] | ||
|
||
main :: IO () | ||
main = renderOutputToFile [osp|myLowerEnv.ply|] (verticesOf $ lowerEnvelope myPlanes) | ||
(trianglesOf $ lowerEnvelope myPlanes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE QuasiQuotes #-} | ||
-------------------------------------------------------------------------------- | ||
-- | | ||
-- Module : Ply.Writer | ||
-- Copyright : (C) Frank Staals | ||
-- License : see the LICENSE file | ||
-- Maintainer : Frank Staals | ||
-- | ||
-- Helper module to write PLY files; so that we can export simple 3D scenes. | ||
-- | ||
-------------------------------------------------------------------------------- | ||
module PLY.Writer | ||
( renderOutputToFile | ||
, renderOutput | ||
) where | ||
|
||
import Control.Lens | ||
import qualified Data.ByteString.Lazy.Char8 as Char8 | ||
import qualified Data.Foldable as F | ||
import Data.Foldable1 | ||
import HGeometry.Ext | ||
import HGeometry.Point | ||
import HGeometry.Triangle | ||
import qualified System.File.OsPath as File | ||
import System.OsPath | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
-- | Writes the the points and triangles to a file in PLY format. | ||
renderOutputToFile :: (Foldable1 f, Point_ point 3 r, Show r) | ||
=> OsPath -> f (point :+ Int) | ||
-> [Triangle (point :+ Int)] | ||
-> IO () | ||
renderOutputToFile fp pts ts = File.writeFile fp $ renderOutput pts ts | ||
|
||
-- | Generates the content of the PLY file for the given non-empty list of points and the | ||
-- list of triangles | ||
-- assumes points are 0 indexed. | ||
renderOutput :: (Foldable1 f, Point_ point 3 r, Show r) | ||
=> f (point :+ Int) -> [Triangle (point :+ Int)] | ||
-> Char8.ByteString | ||
renderOutput (F.toList -> pts) ts = | ||
Char8.unlines $ hdr <> map renderPt pts <> map renderTri ts | ||
where | ||
hdr = ["ply" | ||
, "format ascii 1.0" | ||
,"element vertex " <> (showT $ length pts) | ||
,"property float32 x" | ||
,"property float32 y" | ||
,"property float32 z" | ||
,"element face " <> (showT $ length ts) | ||
,"property list uchar int vertex_index" | ||
,"end_header" | ||
] | ||
|
||
-- | Writes a Point to ply format | ||
renderPt :: (Point_ point 3 r, Show r) => (point :+ extra) -> Char8.ByteString | ||
renderPt (p :+ _) = let Point3 x y z = over coordinates showT $ p^.asPoint | ||
in Char8.unwords [x,y,z] | ||
|
||
-- | Writes a triangle to ply format | ||
renderTri :: (Point_ point 3 r, Show r) | ||
=> Triangle (point :+ Int) -> Char8.ByteString | ||
renderTri (Triangle p q r) = let i a = showT $ a^.extra in Char8.unwords ["3",i p, i q, i r] | ||
|
||
-- | Helper to output stuff to bytestrings | ||
showT :: Show a => a -> Char8.ByteString | ||
showT = Char8.pack . show |
Oops, something went wrong.