elm-geometry icon indicating copy to clipboard operation
elm-geometry copied to clipboard

Add Torus3d type and module

Open ianmackenzie opened this issue 7 years ago • 4 comments

type Torus3d = Torus3d { axis : Axis3d, majorRadius : Float, minorRadius : Float }

ianmackenzie avatar Oct 24 '16 04:10 ianmackenzie

It would be easier to have on base function that can create a mesh from a formula, in the end the only different between a torus a sphere or a supershape is the formula that creates the points from the two angles and a bunch of predefine parameters. I've started to implement this in Elm, but at the moment its just a fun project. I would really like to port it over to opensolid and make a reusable library out of it.

eskimoblood avatar Feb 06 '17 07:02 eskimoblood

For the purposes of meshing, you are correct, but that is not the only possible use of a 3D geometry library! There are several functions that I could see adding to this module that would be specific to toruses:

  • Checking if a point is within a torus
  • Finding the intersection (if any) between a line segment and a torus
  • Determining the closest point on a torus to a given point

It's entirely possible that some meshing code could be shared between toruses and spheres, but to me that seems more like an implementation detail than a design decision. Thoughts?

ianmackenzie avatar Feb 07 '17 03:02 ianmackenzie

For me it looks like there are solids with its settings and then there is stuff you can do with solids like rendering or checking for intersection. So you can define a torus type with its parameters and put it into the createMeshFromSolid or the isPointInSolid function.

eskimoblood avatar Feb 07 '17 08:02 eskimoblood

Sorry for the delayed response! It's been a busy week at work...

At some point I think it is likely there will be a Body3d type, but I think it still makes sense to have the specific types like Sphere3d, Torus3d etc. I envision something like

module OpenSolid.Body3d exposing (Body3d, contains)

import OpenSolid.Torus3d as Torus3d exposing (Torus3d)
import OpenSolid.Sphere3d as Sphere3d exposing (Sphere3d)

type Body3d
    = Torus Torus3d
    | Sphere Sphere3d

contains : Point3d -> Body3d -> Bool
contains point body =
    case body of
        Torus torus ->
            Torus3d.contains point torus

        Sphere sphere ->
           Sphere3d.contains point sphere

That is, the polymorphic Body3d is a thin wrapper over the monomorphic Torus3d, Sphere3d etc. types and mostly just delegates to them. To me this is the best of both worlds - you can deal with bodies in a generic way if you want, but if you happen to know specifically that you have a torus then you can just work with a torus directly (and potentially do stuff that is too difficult or inefficient to do with an arbitrary body, like perhaps compute volume).

This is influenced by my thinking on curves, where I plan to do something similar - there will be Arc2d, QuadraticSpline2d, CubicSpline2d etc. types as well as an opaque Curve2d type that wraps those and provides a common interface. This means that you can both work directly with arcs if you want (and have access to their center point, radius etc. which don't make sense for a general curve) or wrap them in a Curve2d to pass to general rendering functions etc.

ianmackenzie avatar Feb 10 '17 22:02 ianmackenzie