Openscad: Warp 3d Surface To Fit Onto Another Shape

Discussion in 'Software and Applications' started by andrewthommo, May 18, 2017.

  1. andrewthommo
    andrewthommo Member
    Can OpenSCAD warp a (basically flat) 3D map to fit the outside or inside of a shape like a tube?

    To expand, say there is a surface based on a grayscale image of a landscape's topography. Is there a command (or easy way I missed) to curve the flat (bottom) side to the inside or outside of a tube?

    I guess it'd be possible to carve the surface into N pieces, translate each one to the internal or external radius of the tube, then create an union of the slices. For the outside of the tube, it would be necessary to expand the width of each slice slightly to avoid creating steep 'canyons' appearing between each pair of slices.

    But I'm hoping there is a more elegant approach I've not thought of, or an existing command I've missed.
  2. lkcl
    lkcl New Member
    hi andrew, openscad is basically a thin wrapper onto libcsg. anything that CSG can do, openscad can do... and CSG is *not* designed to do anything but the most absolute basic of shapes and transformations: cubes, cylinders and *CORRECT* polygons defined by points and a list of triangles is your lot.

    openscad's language is basically... not very good. it's a macro language, it's nowhere near NP-complete and i don't really understand why people don't understand that actually writing directly in openscad is... painful shall we say. that having been said the amount of people who *have* worked with it is so huge, and the advantages of using e.g. git revision control and standard software development and management practices has been a *huge* advantage, such that there are an enormous amount of libraries and work out there which you can take advantage of.

    i can't stand not having a proper programming language (i tried for example to pass parameters to openscad only to discover that it doesn't even have proper variable "scopes"!) so i now work with pyopenscad and have done for years, now.

    pyopenscad (and the associated pyopenscadobj that i wrote) basically has *all* the power of python as a proper modern OO programming language - objects, functions, modules, lambda calculus, exceptions, yield and way more - but outputs SCAD files in effect as "machine code".

    with that in mind it's funny because i *just* completed an improvement to a program that i wrote called - an improvement that i've been planning for six years now - which can be used to do exactly what you asked for: morph one shape into another.

    the key improvement that i made was that polysurface now takes just a "sheet thickness" (where it previously took a vector parameter) and it automatically calculates the orthogonal vectors at each and every point. run "python" and then view the resultant test.scad file to see what it can do. it's really cool, and complicated as hell :)

    that example starts from a circle then progressively goes through a series of rectangles: it's designed to demonstrate "morphing" for a fan nozzle for a 3D printer:


    now, please bear in mind: splines are not really designed to be circular, so this example by no means perfect, but it demonstrates the point and is "good enough" for what i need. if i *really* wanted it to be "perfect" then what i would do is create 4 separate sets of splines, one for each quadrant of the object (with a special focus on making sure that each spline covered the corner of the rectangles), then join them all back together afterwards

    [and, as it's all in python, that's really easy to do].

    but one of the simple things that i will definitely be doing is to add a couple of extra sets of points to the list.... *after* spline-processing... one at the top and one at the bottom in the "perfect" shapes that i want. the only tricky bit is making sure that the number of points in the "perfect" shapes is the same as in the auto-generated spline(s).

    in short: you're expecting a pre-stone-age flint axe to be able to create a.. .a... GT40 supercar :)

    anyway go grab these files and you can get started:

    that should do the trick.

    so, to do what you want, you'd basically start from where you should be starting: a proper programming language, one that can read in the images, process them, and then use pyopenscad and to auto-generate a scad file.
    andrewthommo likes this.
  3. andrewthommo
    andrewthommo Member
    Thanks for the info.! That OpenSCAD cannot do this task 'out of the box' completely ..fails to surprise me.

    Though I am a bit busy at the moment, but will definitely look more closely into Python etc. in the near future.
  4. lkcl
    lkcl New Member
    it can "do" it... but you're down to manipulating at the polygon level... and openscad (because it's not an NP-complete language) simply cannot read any file-format that the (specific and directly-CGAL-related and *only* CGAL-related) available functions support.

    so if you want to read say a bmp file or any other arbitrary greyscale file-format... you can flat-out forget it.

    openscad *really is* a simple "shim" onto libCGAL (not CSG, sorry).

    so if you *happen* to have a way to programmatically convert... saaay... a grayscale bmp into a 2D DXF file *THEN* you can *CONSIDER* using the "2D to 3D extrude" function.

    but note the caveat "programmatically convert"... because openscad *can't handle it*.

    but even if you did that, you could only extrude 2D DXF to 3D object as a *flat* object.

    basically what you're asking for is pretty esoteric for any 3D CAD/CAM program to do: it really does require some sort of programming, and if you were going to start from anywhere i'd start from python. you *might* be able to find a series of snippets of random code on the internet which you can piece together. start with a google search "python convert greyscale image to 3D raised coordinates" or something like that.

    i know for a fact that there do exist programs out there where people have done this sort of thing. i have a friend whose son got into trouble for telling "lies" at school about a "magic box" that his daddy had which could make pictures come to life. at the next parent's "show and tell" weekly meeting, the teacher got a reprimand as his father brought in his 3D printer and created bas-relief coloured 3D prints of the children's pictures....
  5. stonysmith
    stonysmith Well-Known Member Moderator
    Let me see if I can dig up my old code.. I built a routine years ago to map a jpg of the moon onto a sphere.
    I think there might also be a routine in Blender to do a "bump map offset" or something like that.
  6. mygadgetlife
    mygadgetlife Well-Known Member
    Blender has a 'Displace' modifier, where you can nominate a texture map to use for displacement, normally a greyscale image.

    If you create a plane subdivided into large quantity of vertices (to provide sufficient 3D print resolution), use the image to offset the vertices perpendicular to the plane with the Displace modifier, then project the UV vertices onto the same image, you would get the desired result. Once that's done, use the Bend modifier to roll the plane into a cylinder.

    The precise details are sketchy in my mind since it's been so long since I did it, but I used this technique to make colour bump/image models of the moon, so it is possible.