Debian Codegraph @

src/Diagrams/ThreeD/Shapes.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE RankNTypes #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE TypeOperators #-}
8 {-# LANGUAGE UndecidableInstances #-}
9 -----------------------------------------------------------------------------
10 -- |
11 -- Module : Diagrams.ThreeD.Shapes
12 -- Copyright : (c) 2011 diagrams-lib team (see LICENSE)
13 -- License : BSD-style (see LICENSE)
14 -- Maintainer : [email protected]
15 --
16 -- Various three-dimensional shapes.
17 --
18 -----------------------------------------------------------------------------
19
20 module Diagrams.ThreeD.Shapes
21 (
22 -- * Skinned class
23 Skinned(..)
24
25 -- * Basic 3D shapes
26 , Ellipsoid(..)
27 , sphere
28
29 , Box(..)
30 , cube
31
32 , Frustum(..)
33 , frustum
34 , cone
35 , cylinder
36
37 -- * Constructive solid geometry
38 , CSG(..)
39 , union
40 , intersection
41 , difference
42 ) where
43
44 import Control.Lens (review, (^.), _1)
45 import Data.Typeable
46
47 import Data.Semigroup
48 import Diagrams.Angle
49 import Diagrams.Core
50 import Diagrams.Core.Trace
51 import Diagrams.Points
52 import Diagrams.Query
53 import Diagrams.Solve.Polynomial
54 import Diagrams.ThreeD.Types
55 import Diagrams.ThreeD.Vector
56
57 import Linear.Affine
58 import Linear.Metric
59 import Linear.Vector
60
61 data Ellipsoid n = Ellipsoid (Transformation V3 n)
62 deriving Typeable
63
64 type instance V (Ellipsoid n) = V3
65 type instance N (Ellipsoid n) = n
66
67 instance Fractional n => Transformable (Ellipsoid n) where
68 transform t1 (Ellipsoid t2) = Ellipsoid (t1 <> t2)
69
70 instance Fractional n => Renderable (Ellipsoid n) NullBackend where
71 render _ _ = mempty
72
73 instance OrderedField n => Enveloped (Ellipsoid n) where
74 getEnvelope (Ellipsoid tr) = transform tr . mkEnvelope $ \v -> 1 / norm v
75
76 instance OrderedField n => Traced (Ellipsoid n) where
77 getTrace (Ellipsoid tr) = transform tr . mkTrace $ \(P p) v -> let
78 a = v `dot` v
79 b = 2 * (p `dot` v)
80 c = (p `dot` p) - 1
81 in
82 mkSortedList $ quadForm a b c
83
84 -- | A sphere of radius 1 with its center at the origin.
85 sphere :: Num n => Ellipsoid n
86 sphere = Ellipsoid mempty
87
88 data Box n = Box (Transformation V3 n)
89 deriving Typeable
90
91 type instance V (Box n) = V3
92 type instance N (Box n) = n
93
94 instance Fractional n => Transformable (Box n) where
95 transform t1 (Box t2) = Box (t1 <> t2)
96
97 instance Fractional n => Renderable (Box n) NullBackend where
98 render _ _ = mempty
99
100 instance OrderedField n => Enveloped (Box n) where
101 getEnvelope (Box tr) = transform tr . mkEnvelope $ \v ->
102 maximum (map (v `dot`) corners) / quadrance v where
103 corners = mkR3 <$> [0,1] <*> [0,1] <*> [0,1]
104
105 instance (Fractional n, Ord n) => Traced (Box n) where
106 getTrace (Box tr) = transform tr . mkTrace $ \p v -> let
107 (x0, y0, z0) = unp3 p
108 (vx, vy, vz) = unr3 v
109 intersections f d = case d of
110 0 -> []
111 _ -> [-f/d, (1-f)/d]
112 ts = concat $ zipWith intersections [x0,y0,z0] [vx,vy,vz]
113 atT t = p .+^ (t*^v)
114 range u = and [x >= 0, x <= 1, y >= 0, y <= 1, z >= 0, z <= 1] where
115 (x, y, z) = unp3 u
116 in
117 -- ts gives all intersections with the planes forming the box
118 -- filter keeps only those actually on the box surface
119 mkSortedList . filter (range . atT) $ ts where
120
121 -- | A cube with side length 1, in the positive octant, with one
122 -- vertex at the origin.
123 cube :: Num n => Box n
124 cube = Box mempty
125
126 data Frustum n = Frustum n n (Transformation V3 n)
127 deriving Typeable
128
129 type instance V (Frustum n) = V3
130 type instance N (Frustum n) = n
131
132 instance Fractional n => Transformable (Frustum n) where
133 transform t1 (Frustum r0 r1 t2) = Frustum r0 r1 (t1 <> t2)
134
135 instance Fractional n => Renderable (Frustum n) NullBackend where
136 render _ _ = mempty
137
138 instance (OrderedField n, RealFloat n) => Enveloped (Frustum n) where
139 -- The plane containing v and the z axis intersects the frustum in a trapezoid
140 -- Test the four corners of this trapezoid; one must determine the Envelope
141 getEnvelope (Frustum r0 r1 tr) = transform tr . mkEnvelope $ \v ->let
142 θ = v ^. _theta
143 corners = [(r1,θ,1), (-r1,θ,1), (r0,θ,0), (-r0,θ,0)]
144 in
145 maximum . map (norm . project v . review r3CylindricalIso) $ corners
146
147 instance (RealFloat n, Ord n) => Traced (Frustum n) where
148 -- The trace can intersect the sides of the cone or one of the end
149 -- caps The sides are described by a quadric equation; substitute
150 -- in the parametric form of the ray but disregard any
151 -- intersections outside z = [0,1] Similarly, find intersections
152 -- with the planes z=0, z=1, but disregard any r>r0, r>r1
153 getTrace (Frustum r0 r1 tr) = transform tr . mkTrace $ \p v -> let
154 (px, py, pz) = unp3 p
155 (vx, vy, vz) = unr3 v
156 ray t = p .+^ t *^ v
157 dr = r1 - r0
158 a = vx**2 + vy**2 - vz**2 * dr**2
159 b = 2 * (px * vx + py * vy - (r0+pz*dr) * dr * vz)
160 c = px**2 + py**2 - (r0 + dr*pz)**2
161 zbounds t = ray t ^. _z >= 0
162 && ray t ^. _z <= 1
163 ends = concatMap cap [0,1]
164 cap z = [ t | ray t ^. lensP . r3CylindricalIso . _1 < r0 + z * dr ]
165 where
166 t = (z - pz) / vz
167 in
168 mkSortedList $ filter zbounds (quadForm a b c) ++ ends
169
170 -- | A frustum of a right circular cone. It has height 1 oriented
171 -- along the positive z axis, and radii r0 and r1 at Z=0 and Z=1.
172 -- 'cone' and 'cylinder' are special cases.
173 frustum :: Num n => n -> n -> Frustum n
174 frustum r0 r1 = Frustum r0 r1 mempty
175
176 -- | A cone with its base centered on the origin, with radius 1 at the
177 -- base, height 1, and it's apex on the positive Z axis.
178 cone :: Num n => Frustum n
179 cone = frustum 1 0
180
181 -- | A circular cylinder of radius 1 with one end cap centered on the
182 -- origin, and extending to Z=1.
183 cylinder :: Num n => Frustum n
184 cylinder = frustum 1 1
185
186 -- | Types which can be rendered as 3D Diagrams.
187 class Skinned t where
188 skin :: (Renderable t b, N t ~ n, TypeableFloat n) => t -> QDiagram b V3 n Any
189
190 instance (Num n, Ord n) => HasQuery (Ellipsoid n) Any where
191 getQuery (Ellipsoid tr) = transform tr $
192 Query $ \v -> Any $ quadrance (v .-. origin) <= 1
193
194 instance OrderedField n => Skinned (Ellipsoid n) where
195 skin s = mkQD (Prim s) (getEnvelope s) (getTrace s) mempty (getQuery s)
196
197 instance (Num n, Ord n) => HasQuery (Box n) Any where
198 getQuery (Box tr) = transform tr . Query $ Any . range where
199 range u = and [x >= 0, x <= 1, y >= 0, y <= 1, z >= 0, z <= 1] where
200 (x, y, z) = unp3 u
201
202 instance OrderedField n => Skinned (Box n) where
203 skin s = mkQD (Prim s) (getEnvelope s) (getTrace s) mempty (getQuery s)
204
205 instance (OrderedField n) => HasQuery (Frustum n) Any where
206 getQuery (Frustum r0 r1 tr)= transform tr $
207 Query $ \p -> let
208 z = p^._z
209 r = r0 + (r1 - r0)*z
210 v = p .-. origin
211 a = norm $ projectXY v
212 projectXY u = u ^-^ project unitZ u
213 in
214 Any $ z >= 0 && z <= 1 && a <= r
215
216 instance Skinned (Frustum n) where
217 skin s = mkQD (Prim s) (getEnvelope s) (getTrace s) mempty (getQuery s)
218
219 -- The CSG type needs to form a tree to be useful. This
220 -- implementation requires Backends to support all the included
221 -- primitives. If that turns out to be a problem, we have several
222 -- options:
223 -- a) accept runtime errors for unsupported primitives
224 -- b) carry the set of primitives in a row type in the CSG type
225 -- c) implement CSG in Haskell, so Backends supporting triangle meshes
226 -- can fall back to those.
227 -- (c) is worth doing anyway; I'm ambivalent about the others. -DMB
228
229 -- | A tree of Constructive Solid Geometry operations and the primitives that
230 -- can be used in them.
231 data CSG n
232 = CsgEllipsoid (Ellipsoid n)
233 | CsgBox (Box n)
234 | CsgFrustum (Frustum n)
235 | CsgUnion [CSG n]
236 | CsgIntersection [CSG n]
237 | CsgDifference (CSG n) (CSG n)
238 deriving Typeable
239
240 type instance V (CSG n) = V3
241 type instance N (CSG n) = n
242
243 instance Fractional n => Transformable (CSG n) where
244 transform t (CsgEllipsoid p) = CsgEllipsoid $ transform t p
245 transform t (CsgBox p) = CsgBox $ transform t p
246 transform t (CsgFrustum p) = CsgFrustum $ transform t p
247 transform t (CsgUnion ps) = CsgUnion $ map (transform t) ps
248 transform t (CsgIntersection ps) = CsgIntersection $ map (transform t) ps
249 transform t (CsgDifference p1 p2) = CsgDifference (transform t p1) (transform t p2)
250
251 -- | The Envelope for an Intersection or Difference is simply the
252 -- Envelope of the Union. This is wrong but easy to implement.
253 instance RealFloat n => Enveloped (CSG n) where
254 getEnvelope (CsgEllipsoid p) = getEnvelope p
255 getEnvelope (CsgBox p) = getEnvelope p
256 getEnvelope (CsgFrustum p) = getEnvelope p
257 getEnvelope (CsgUnion ps) = foldMap getEnvelope ps
258 getEnvelope (CsgIntersection ps) = foldMap getEnvelope ps
259 getEnvelope (CsgDifference p1 p2) = getEnvelope p1 <> getEnvelope p2
260 -- TODO after implementing some approximation scheme, calculate
261 -- correct (approximate) envelopes for intersections and difference.
262
263 instance (Floating n, Ord n) => HasQuery (CSG n) Any where
264 getQuery (CsgEllipsoid prim) = getQuery prim
265 getQuery (CsgBox prim) = getQuery prim
266 getQuery (CsgFrustum prim) = getQuery prim
267 getQuery (CsgUnion ps) = foldMap getQuery ps
268 getQuery (CsgIntersection ps) =
269 Any . getAll <$> foldMap (fmap (All . getAny) . getQuery) ps
270 getQuery (CsgDifference p1 p2) = inOut <$> getQuery p1 <*> getQuery p2 where
271 inOut (Any a) (Any b) = Any $ a && not b
272
273 instance (RealFloat n, Ord n) => Traced (CSG n) where
274 getTrace (CsgEllipsoid p) = getTrace p
275 getTrace (CsgBox p) = getTrace p
276 getTrace (CsgFrustum p) = getTrace p
277 -- on surface of some p, and not inside any of the others
278 getTrace (CsgUnion []) = mempty
279 getTrace (CsgUnion (s:ss)) = mkTrace t where
280 t pt v = onSortedList (filter $ without s) (appTrace (getTrace (CsgUnion ss)) pt v)
281 <> onSortedList (filter $ without (CsgUnion ss)) (appTrace (getTrace s) pt v) where
282 newPt dist = pt .+^ v ^* dist
283 without prim = not . inquire prim . newPt
284 -- on surface of some p, and inside all the others
285 getTrace (CsgIntersection []) = mempty
286 getTrace (CsgIntersection (s:ss)) = mkTrace t where
287 t pt v = onSortedList (filter $ within s) (appTrace (getTrace (CsgIntersection ss)) pt v)
288 <> onSortedList (filter $ within (CsgIntersection ss)) (appTrace (getTrace s) pt v) where
289 newPt dist = pt .+^ v ^* dist
290 within prim = inquire prim . newPt
291 -- on surface of p1, outside p2, or on surface of p2, inside p1
292 getTrace (CsgDifference s1 s2) = mkTrace t where
293 t pt v = onSortedList (filter $ not . within s2) (appTrace (getTrace s1) pt v)
294 <> onSortedList (filter $ within s1) (appTrace (getTrace s2) pt v) where
295 newPt dist = pt .+^ v ^* dist
296 within prim = inquire prim . newPt
297
298 instance (RealFloat n, Ord n) => Skinned (CSG n) where
299 skin s = mkQD (Prim s) (getEnvelope s) (getTrace s) mempty (getQuery s)
300
301 -- | Types which can be included in CSG trees.
302 class CsgPrim a where
303 toCsg :: a n -> CSG n
304
305 instance CsgPrim Ellipsoid where
306 toCsg = CsgEllipsoid
307
308 instance CsgPrim Box where
309 toCsg = CsgBox
310
311 instance CsgPrim Frustum where
312 toCsg = CsgFrustum
313
314 instance CsgPrim CSG where
315 toCsg = id
316
317 union :: (CsgPrim a, CsgPrim b) => a n -> b n -> CSG n
318 union a b = CsgUnion [toCsg a, toCsg b]
319
320 intersection :: (CsgPrim a, CsgPrim b) => a n -> b n -> CSG n
321 intersection a b = CsgIntersection [toCsg a, toCsg b]
322
323 difference :: (CsgPrim a, CsgPrim b) => a n -> b n -> CSG n
324 difference a b = CsgDifference (toCsg a) (toCsg b)