Skip to content

Commit

Permalink
Generate Arbitrary PlaneGraphs (#254) + adding a basic PLYWriter library
Browse files Browse the repository at this point in the history
* 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
noinia authored Nov 17, 2024
1 parent 59657af commit 53897d5
Show file tree
Hide file tree
Showing 20 changed files with 723 additions and 259 deletions.
32 changes: 32 additions & 0 deletions hgeometry-examples/draw/Main.hs
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
25 changes: 25 additions & 0 deletions hgeometry-examples/hgeometry-examples.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ common setup
, hgeometry:svg
, hgeometry:miso
, hgeometry:geojson
, hgeometry:quickcheck
, hiraffe >= 0.1
, containers >= 0.6
, vector >= 0.13
Expand All @@ -76,6 +77,7 @@ common setup
, file-io
, filepath
, geojson >= 4.1.1
, QuickCheck >= 2.15.0

-- , dependent-map >= 0.4
-- , dependent-sum >= 0.7.1
Expand Down Expand Up @@ -269,3 +271,26 @@ executable hgeometry-skia
Base
GeometryStore
GeometryStore.Helper


--------------------------------------------------------------------------------
executable hgeometry-draw
import: setup, miso-setup
hs-source-dirs: draw
main-is: Main.hs
-- other-modules:
-- Options


--------------------------------------------------------------------------------

-- Renders a 3D model of the lower envelope of a set of planes
executable hgeometry-lowerEnv
import: setup, miso-setup
hs-source-dirs: lowerEnv
main-is: Main.hs
build-depends:
hgeometry:ply-writer

-- other-modules:
-- Options
47 changes: 47 additions & 0 deletions hgeometry-examples/lowerEnv/Main.hs
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)
19 changes: 16 additions & 3 deletions hgeometry/hgeometry.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ common all-setup
, hgeometry-combinatorial >= 1.0.0.0 && < 2
, hiraffe >= 0.1 && < 1
, containers >= 0.6 && < 1
, nonempty-containers >= 0.3.4.5 && < 0.4
, dlist >= 0.7 && < 1.1
, bytestring >= 0.11 && < 1
, vector:vector >= 0.13 && < 1
Expand Down Expand Up @@ -391,7 +392,7 @@ library
HGeometry.ConvexHull.Melkman

-- HGeometry.ConvexHull.R3.Naive
-- HGeometry.ConvexHull.R3.Naive.Dual
HGeometry.ConvexHull.R3.Naive.Dual

HGeometry.BezierSpline

Expand Down Expand Up @@ -419,7 +420,7 @@ library


HGeometry.VoronoiDiagram

HGeometry.VoronoiDiagram.ViaLowerEnvelope

HGeometry.SegmentTree
HGeometry.SegmentTree.Base
Expand Down Expand Up @@ -460,7 +461,6 @@ library
-- HGeometry.Plane.LowerEnvelope.DivideAndConquer
-- HGeometry.Plane.LowerEnvelope.EpsApproximation

HGeometry.VoronoiDiagram.ViaLowerEnvelope



Expand Down Expand Up @@ -492,6 +492,7 @@ library ipe

Ipe.IpeRender
Ipe.IpeToIpe
-- HGeometry.PlaneGraph.Draw


other-modules:
Expand Down Expand Up @@ -570,6 +571,7 @@ library quickcheck
exposed-modules:
HGeometry.Instances
HGeometry.Polygon.Instances
HGeometry.PlaneGraph.Instances
other-modules:
Paths_hgeometry
autogen-modules:
Expand Down Expand Up @@ -604,6 +606,16 @@ library vector-quickcheck
build-depends:
hgeometry:vector

library ply-writer
import: all-setup
visibility: public
hs-source-dirs: ply/src
exposed-modules:
PLY.Writer
build-depends:
hgeometry:point
, hgeometry:kernel

--------------------------------------------------------------------------------
-- * Test Suites
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -699,6 +711,7 @@ test-suite hspec
Spec
ConvexHull.ConvexHullSpec
ConvexHull.MelkmanSpec
ConvexHull.R3Spec
ClosestPair.ClosestPairSpec
IntervalTreeSpec
LowerEnvelope.RegionsSpec
Expand Down
69 changes: 69 additions & 0 deletions hgeometry/ply/src/PLY/Writer.hs
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
Loading

0 comments on commit 53897d5

Please sign in to comment.