Skip to content
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

Extrude shape along a 3D path #298

Closed
drom opened this issue Oct 14, 2017 · 4 comments
Closed

Extrude shape along a 3D path #298

drom opened this issue Oct 14, 2017 · 4 comments

Comments

@drom
Copy link

drom commented Oct 14, 2017

I am building coil like 3D object.

screenshot_20171014_092824

The best solution I found so far is union of rotated cylinders (see the code below), but:

  • it leaves the gaps between segments
  • it is slow

Wouldn't it be nice to have some sort of extrude_along_the_path API to do the trick?

'use strict';

const mag = vec => Math.sqrt(
    vec.reduce((res, e) => res + Math.pow(e, 2), 0));

const rotx = vec => -Math.atan2(
    Math.sqrt(Math.pow(vec[0], 2) + Math.pow(vec[1], 2)),
    vec[2]
  ) * 180 / Math.PI;

const rotz = vec => -Math.atan2(vec[0], vec[1]) * 180 / Math.PI;

const wireSegment = (pos, vec) =>
    translate(pos,
        rotate([rotx(vec), 0, rotz(vec)],
            cylinder({r: .5, h: mag(vec), fn: 8})
        )
    );

const turnPattern = [
    [ 0, 2,  10], [ .5, .1,  1 ], [ .5, .1,  .5], [ 1 , .1, .5 ],
    [ 6, 1,   0], [ 1,  .1, -.5], [ .5, .1, -.5], [ .5, .1, -1 ],
    [ 0, 2, -10], [-.5, .1, -1 ], [-.5, .1, -.5], [-1,  .1, -.5],
    [-6, 1,   0], [-1,  .1,  .5], [-.5, .1,  .5], [-.5, .1,  1 ]
];

const genTurns = (nTurns, pattern) => {
    const patternLength = pattern.length;
    const fullTurns = nTurns | 0;
    const tailTurns = (nTurns - fullTurns) * patternLength;
    let res = [];
    for (let i = 0; i < fullTurns; i++) {
        res = res.concat(pattern);
    }
    for (let i = 0; i < tailTurns; i++) {
        res = res.concat([pattern[i]]);
    }
    return res;
};

const coil = nTurns => {
    const turns = genTurns(nTurns, turnPattern);
    const res = turns.reduce((res, turn, i) => {
        const {root, pos} = res;
        return {
            root: root.union(wireSegment(pos, turn)),
            pos: pos.map((e, i) => e + turn[i])
        };
    }, {
        root: {union: el => el},
        pos: [0, 0, 0]
    });
    return res.root.setColor([1, .3, .1]);
};

const main = () => coil(5 + 9/16);
@kaosat-dev
Copy link
Contributor

Hi @drom !
There is something like that in the works :) I hope to get it done by the end of next week.
in the meantime , you can , with a bit of hacking around get close by using solidFromSlices

@kaosat-dev
Copy link
Contributor

Side note : I really like your coding style of small specialized functions, if you can help out, it would be awesome :)

@drom
Copy link
Author

drom commented Oct 17, 2017

@kaosat-dev thank you, I will try solidFromSlices and will test new API once available.

I would love to help with the project. I am new to solid geometry but using JS instead of some DSL language over OpenSCAD like approach is really powerful idea.

@kaosat-dev
Copy link
Contributor

@drom I hope you do not mind, I created a copy of this here jscad/csg.js#68 as it is part of the 'core' features (csg.js)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants