-
-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some basic provenance tracking for triangles #374
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ module Graphics.Implicit.Definitions ( | |
Triangle(Triangle), | ||
NormedTriangle(NormedTriangle), | ||
TriangleMesh(TriangleMesh), | ||
AnnotatedTriangleMesh(AnnotatedTriangleMesh, unAnnotatedTriangleMesh), | ||
TriangleProvenance(..), | ||
NormedTriangleMesh(NormedTriangleMesh), | ||
Obj2, | ||
Obj3, | ||
|
@@ -64,12 +66,13 @@ module Graphics.Implicit.Definitions ( | |
toScaleFn, | ||
isScaleID, | ||
quaternionToEuler, | ||
removeTriangleMeshAnnotations, | ||
) | ||
where | ||
|
||
import GHC.Generics (Generic) | ||
|
||
import Prelude (Ord, Eq, atan2, asin, pi, (>=), signum, abs, (+), (-), RealFloat, (==), ($), flip, Semigroup((<>)), Monoid (mempty), Double, Either(Left, Right), Bool(True, False), (*), (/), fromIntegral, Float, realToFrac) | ||
import Prelude (Ord, Eq, atan2, asin, pi, (>=), signum, abs, (+), (-), RealFloat, (==), ($), flip, Semigroup((<>)), Monoid (mempty), Double, Either(Left, Right), Bool(True, False), (*), (/), fromIntegral, Float, realToFrac, map, fst, Int, seq) | ||
|
||
import Graphics.Implicit.FastIntUtil as F (Fastℕ(Fastℕ), fromFastℕ, toFastℕ) | ||
|
||
|
@@ -162,6 +165,18 @@ newtype NormedTriangle = NormedTriangle ((ℝ3, ℝ3), (ℝ3, ℝ3), (ℝ3, ℝ3 | |
-- | A triangle mesh is a bunch of triangles, attempting to be a surface. | ||
newtype TriangleMesh = TriangleMesh [Triangle] | ||
|
||
newtype AnnotatedTriangleMesh a = AnnotatedTriangleMesh { unAnnotatedTriangleMesh :: [(Triangle, a)] } | ||
|
||
removeTriangleMeshAnnotations :: AnnotatedTriangleMesh a -> TriangleMesh | ||
removeTriangleMeshAnnotations (AnnotatedTriangleMesh l) = TriangleMesh $ map fst l | ||
|
||
data TriangleProvenance | ||
= TriangleProvenance_SquareToTri Bool TriangleProvenance | ||
| TriangleProvenance_JoinXAligned TriangleProvenance TriangleProvenance | ||
| TriangleProvenance_JoinYAligned TriangleProvenance TriangleProvenance | ||
| TriangleProvenance_TesselateLoop Int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this int? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, just an enum I haven't bothered writing yet. |
||
deriving (Show, Eq, Ord) | ||
|
||
-- | A normed triangle mesh is a mesh of normed triangles. | ||
newtype NormedTriangleMesh = NormedTriangleMesh [NormedTriangle] | ||
|
||
|
@@ -174,6 +189,15 @@ instance NFData Triangle where | |
instance NFData TriangleMesh where | ||
rnf (TriangleMesh xs) = rnf xs | ||
|
||
instance NFData a => NFData (AnnotatedTriangleMesh a) where | ||
rnf (AnnotatedTriangleMesh xs) = rnf xs | ||
|
||
instance NFData TriangleProvenance where | ||
rnf (TriangleProvenance_SquareToTri b p) = rnf b `seq` rnf p | ||
rnf (TriangleProvenance_JoinXAligned a b) = rnf a `seq` rnf b | ||
rnf (TriangleProvenance_JoinYAligned a b) = rnf a `seq` rnf b | ||
rnf (TriangleProvenance_TesselateLoop n) = rnf n | ||
|
||
instance NFData Polytri where | ||
rnf (Polytri (a,b,c)) = rnf (a,b,c) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,11 @@ | |
{-# LANGUAGE ParallelListComp #-} | ||
|
||
-- export getContour and getMesh, which returns the edge of a 2D object, or the surface of a 3D object, respectively. | ||
module Graphics.Implicit.Export.Render (getMesh, getContour) where | ||
module Graphics.Implicit.Export.Render (getMesh, getAnnotatedMesh, getContour) where | ||
|
||
import Prelude(error, (-), ceiling, ($), (+), (*), max, div, tail, fmap, reverse, (.), foldMap, min, Int, (<>), (<$>)) | ||
|
||
import Graphics.Implicit.Definitions (ℝ, ℕ, Fastℕ, ℝ2, ℝ3, TriangleMesh, Obj2, SymbolicObj2, Obj3, SymbolicObj3, Polyline(Polyline), (⋯/), fromℕtoℝ, fromℕ) | ||
import Graphics.Implicit.Definitions (ℝ, ℕ, Fastℕ, ℝ2, ℝ3, TriangleMesh, Obj2, SymbolicObj2, Obj3, SymbolicObj3, Polyline(Polyline), (⋯/), fromℕtoℝ, fromℕ, AnnotatedTriangleMesh, removeTriangleMeshAnnotations, TriangleProvenance(..)) | ||
|
||
import Graphics.Implicit.Export.Symbolic.Rebound2 (rebound2) | ||
|
||
|
@@ -21,6 +21,8 @@ import Graphics.Implicit.ObjectUtil (getBox2, getBox3) | |
import Data.Foldable(fold) | ||
import Linear ( V3(V3), V2(V2) ) | ||
|
||
import GHC.Stack | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's this imported for? |
||
|
||
-- Here's the plan for rendering a cube (the 2D case is trivial): | ||
|
||
-- (1) We calculate midpoints using interpolate. | ||
|
@@ -75,7 +77,10 @@ import Graphics.Implicit.Primitives (getImplicit) | |
default (ℕ, Fastℕ, ℝ) | ||
|
||
getMesh :: ℝ3 -> SymbolicObj3 -> TriangleMesh | ||
getMesh res@(V3 xres yres zres) symObj = | ||
getMesh res symObj = removeTriangleMeshAnnotations $ getAnnotatedMesh res symObj | ||
|
||
getAnnotatedMesh :: ℝ3 -> SymbolicObj3 -> AnnotatedTriangleMesh TriangleProvenance | ||
getAnnotatedMesh res@(V3 xres yres zres) symObj = | ||
let | ||
-- Grow bounds a little to avoid sampling at exact bounds | ||
(obj, (p1@(V3 x1 y1 z1), p2)) = rebound3 (getImplicit symObj, getBox3 symObj) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,26 @@ | |
-- The purpose of this function is to symbolicaly compute triangle meshes using the symbolic system where possible. | ||
-- Otherwise we coerce it into an implicit function and apply our modified marching cubes algorithm. | ||
|
||
module Graphics.Implicit.Export.SymbolicObj3 (symbolicGetMesh) where | ||
module Graphics.Implicit.Export.SymbolicObj3 (symbolicGetMesh, symbolicGetAnnotatedMesh) where | ||
|
||
import Prelude(pure, zip, length, filter, (>), ($), null, (<>), foldMap, (.), (<$>)) | ||
import Prelude(pure, zip, length, filter, (>), ($), null, (<>), foldMap, (.), (<$>), unlines, zipWith, show, map, snd) | ||
|
||
import Graphics.Implicit.Definitions (ℝ, ℝ3, SymbolicObj3(Shared3), SharedObj(UnionR), Triangle, TriangleMesh(TriangleMesh)) | ||
import Graphics.Implicit.Export.Render (getMesh) | ||
import Graphics.Implicit.Definitions (ℝ, ℝ3, SymbolicObj3(Shared3), SharedObj(UnionR), Triangle, TriangleMesh(TriangleMesh), AnnotatedTriangleMesh(AnnotatedTriangleMesh,unAnnotatedTriangleMesh), removeTriangleMeshAnnotations, TriangleProvenance) | ||
import Graphics.Implicit.Export.Render (getAnnotatedMesh) | ||
import Graphics.Implicit.ObjectUtil (getBox3) | ||
import Graphics.Implicit.MathUtil(box3sWithin) | ||
|
||
import Control.Arrow(first, second) | ||
|
||
import Debug.Trace | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this one. |
||
|
||
symbolicGetMesh :: ℝ -> SymbolicObj3 -> TriangleMesh | ||
symbolicGetMesh res inputObj@(Shared3 (UnionR r objs)) = TriangleMesh $ | ||
symbolicGetMesh res inputObj = removeTriangleMeshAnnotations $ trace annotations mesh | ||
where mesh = symbolicGetAnnotatedMesh res inputObj | ||
annotations = unlines $ zipWith (\n a -> show n <> "\t" <> show a) [1..] $ map snd $ unAnnotatedTriangleMesh mesh | ||
|
||
symbolicGetAnnotatedMesh :: ℝ -> SymbolicObj3 -> AnnotatedTriangleMesh TriangleProvenance | ||
symbolicGetAnnotatedMesh res inputObj@(Shared3 (UnionR r objs)) = AnnotatedTriangleMesh $ | ||
let | ||
boxes = getBox3 <$> objs | ||
boxedObjs = zip boxes objs | ||
|
@@ -31,14 +38,14 @@ symbolicGetMesh res inputObj@(Shared3 (UnionR r objs)) = TriangleMesh $ | |
|
||
(dependants, independents) = sepFree boxedObjs | ||
in if null independents | ||
then unmesh $ getMesh (pure res) inputObj | ||
then unAnnotatedTriangleMesh $ getAnnotatedMesh (pure res) inputObj | ||
else if null dependants | ||
then foldMap (unmesh . symbolicGetMesh res) independents | ||
else foldMap (unmesh . symbolicGetMesh res) independents | ||
<> unmesh (symbolicGetMesh res (Shared3 (UnionR r dependants))) | ||
then foldMap (unAnnotatedTriangleMesh . symbolicGetAnnotatedMesh res) independents | ||
else foldMap (unAnnotatedTriangleMesh . symbolicGetAnnotatedMesh res) independents | ||
<> unAnnotatedTriangleMesh (symbolicGetAnnotatedMesh res (Shared3 (UnionR r dependants))) | ||
|
||
-- | If all that fails, coerce and apply marching cubes :( | ||
symbolicGetMesh res obj = getMesh (pure res) obj | ||
symbolicGetAnnotatedMesh res obj = getAnnotatedMesh (pure res) obj | ||
|
||
unmesh :: TriangleMesh -> [Triangle] | ||
unmesh (TriangleMesh m) = m |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this bool?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are just two call sites :P I'll put an enum type in before we consider merging.