簡體   English   中英

將外部支撐物生成網格以進行3D打印

[英]Generating outside supporters into mesh for 3D printing

序幕

這是我試圖再次向封閉的3D打印生成支持者提出的請求,因為這是一個有趣的問題,但缺少重要的細節...這是作為問答環節 ,目前我正在為答案編寫代碼,但隨時可以回答(我接受最佳答案)。

問題描述

OK這里有關該問題的一些基本信息:

由於這是一個巨大的問題,因此我將集中討論通用的網格/支撐圖案合並幾何問題。

簡而言之,如果要打印任何網格,則只有在網格連接到起始平面的角度最大為〜45度(對於不同的打印技術為+/-)時,我們才能進行打印。 因此,如果我們有未連接到該平面的零件,則需要創建一個將其固定/連接到該平面的橋。 這樣的事情(圖片取自上面鏈接的頁面):

概觀

在粗糙的情況下,我們需要添加盡可能少的材料,並且仍然具有足夠的強度以將我們的網格保持在適當位置而不會彎曲。 最重要的是,我們需要削弱網格附近的支撐,以便在打印后輕松將其折斷。

不要忘記形狀和位置取決於許多因素,例如所使用的材料和技術,熱流。

題:

為了將這個龐大的主題縮小為可回答的問題,讓我們僅關注此問題:

如何將3D三角網格(邊界表示如STL )與預定義的支撐圖案(例如3邊棱鏡)垂直合並,將其與定義的平面相連接?

使用簡單的C ++

OK讓我們從絕對的基礎開始。

  1. 支撐形狀

    您可以使用任何形狀來滿足二手印刷技術的要求。 STL中最容易生成的是3邊棱柱狀的形狀,其中包含2個三角形的底部(頂部和底部)和3個側面,所有這些邊均具有2個三角形。 因此共有8個三角形。

    支撐圖案形狀

    該形狀將從某個基准平面( Z=0 )開始,一直上升直至撞到網格。 但是,要進行這項工作,支撐必須在網格和網格本身之間有一個很小的gap在該網格上添加網格后,我們將添加弱化的關節結構。

  2. 支持模式

    這里有很多選擇,所以我選擇了最簡單的方法(但是不是防污的),那就是將支撐放置在均勻的網格中, grid在支撐之間保持恆定的距離。

    因此,只需從基礎平面上的每個網格位置向上投射光線,然后檢查與網格的相交。 如果發現發生在具有高度只是位置的支撐gap交點下方。

  3. 接頭

    想法是將非常薄的支架的風扇連接成錐形,以小於45度的角度連接並覆蓋主支架棱鏡上方的支架表面(因此, gap應足夠大,以這種方式覆蓋grid距離)。

    這里的主要問題是,我們必須細分要連接的三角形,以便滿足STL網格屬性。 為了解決連接問題(避免STL出現孔或斷開連接要求),我們可以對支撐使用不同的實體,而對網格使用不同的實體。 這也將使我們能夠觸摸表面而無需重新對其進行三角測量,這使這項工作變得容易得多。

    為簡單起見,我選擇了四面體形狀,該形狀可以很容易地由三角形構造,並且在網格/支撐關節處也存在缺陷。

因此,讓我們進行一些測試STL網格並將其放置在基本平面上方:

放置

並提供我們的主要支持:

主要支持

以及關節:

關節

聯合基地細節

這是此STL3D.h VCL / C ++代碼:

//---------------------------------------------------------------------------
//--- simple STL 3D mesh ----------------------------------------------------
//---------------------------------------------------------------------------
#ifndef _STL3D_h
#define _STL3D_h
//---------------------------------------------------------------------------
#ifdef ComctrlsHPP
TProgressBar *progress=NULL;        // loading progress bar for realy big STL files
#endif
void _progress_init(int n);
void _progress     (int ix);
void _progress_done();
//---------------------------------------------------------------------------
class STL3D                         // STL 3D mesh
    {                                                                      
public:
    double center[3],size[3],rmax;  // bbox center,half sizes, max(size[])
    struct _fac
        {
        float p[3][3];              // triangle vertexes CCW order
        float n[3];                 // triangle unit normal pointing out
        WORD attr;
        _fac()  {}
        _fac(_fac& a)   { *this=a; }
        ~_fac() {}
        _fac* operator = (const _fac *a) { *this=*a; return this; }
        //_fac* operator = (const _fac &a) { ...copy... return this; }
        void compute()                                  // compute normal
            {
            float a[3],b[3];
            vectorf_sub(a,p[1],p[0]);
            vectorf_sub(b,p[2],p[1]);
            vectorf_mul(n,a,b);
            vectorf_one(n,n);
            }
        double intersect_ray(double *pos,double *dir)   // return -1 or distance to triangle and unit ray intersection
            {
            double p0[3],p1[3],p2[3];                   // input triangle vertexes
            double e1[3],e2[3],pp[3],qq[3],rr[3];       // dir must be unit vector !!!
            double t,u,v,det,idet;
            // get points
            vector_ld(p0,p[0][0],p[0][1],p[0][2]);
            vector_ld(p1,p[1][0],p[1][1],p[1][2]);
            vector_ld(p2,p[2][0],p[2][1],p[2][2]);
            //compute ray triangle intersection
            vector_sub(e1,p1,p0);
            vector_sub(e2,p2,p0);
            // Calculate planes normal vector
            vector_mul(pp,dir,e2);
            det=vector_mul(e1,pp);
            // Ray is parallel to plane
            if (fabs(det)<1e-8) return -1.0;
            idet=1.0/det;
            vector_sub(rr,pos,p0);
            u=vector_mul(rr,pp)*idet;
            if ((u<0.0)||(u>1.0)) return -1.0;
            vector_mul(qq,rr,e1);
            v=vector_mul(dir,qq)*idet;
            if ((v<0.0)||(u+v>1.0)) return -1.0;
            // distance
            t=vector_mul(e2,qq)*idet;
            if (t<0.0) t=-1.0;
            return t;
            }
        };
    List<_fac> fac;                         // faces

    STL3D() { reset(); }
    STL3D(STL3D& a) { *this=a; }
    ~STL3D() {}
    STL3D* operator = (const STL3D *a) { *this=*a; return this; }
    //STL3D* operator = (const STL3D &a) { ...copy... return this; }

    void reset(){ fac.num=0; compute(); }   // clear STL
    void draw();                            // render STL mesh (OpenGL)
    void draw_normals(float size);          // render STL normals (OpenGL)
    void compute();                         // compute bbox
    void compute_normals();                 // recompute normals from points
    void supports(reper &obj);              // compute supports with obj placement above base plane z=0
    void load(AnsiString name);
    void save(AnsiString name);
    };
//---------------------------------------------------------------------------
void STL3D::draw()
    {
    _fac *f; int i,j; BYTE r,g,b;
    glBegin(GL_TRIANGLES);
    for (f=fac.dat,i=0;i<fac.num;i++,f++)
        {
        glNormal3fv(f->n);
        if (f->attr<32768)
            {
            r= f->attr     &31; r<<=3;
            g=(f->attr>> 5)&31; g<<=3;
            b=(f->attr>>10)&31; b<<=3;
            glColor3ub(r,g,b);
            }
        for (j=0;j<3;j++) glVertex3fv(f->p[j]);
        }
    glEnd();
    }
//---------------------------------------------------------------------------
void STL3D::draw_normals(float size)
    {
    _fac *f;
    int i; float a[3],b[3];
    glBegin(GL_LINES);
    for (f=fac.dat,i=0;i<fac.num;i++,f++)
        {
        vectorf_add(a,f->p[0],f->p[1]);
        vectorf_add(a,a      ,f->p[2]);
        vectorf_mul(a,a,1.0/3.0);
        vectorf_mul(b,f->n,size); glVertex3fv(a);
        vectorf_add(b,b,a);       glVertex3fv(b);
        }
    glEnd();
    }
//---------------------------------------------------------------------------
void STL3D::compute()
    {
    _fac *f;
    int i,j,k;
    double p0[3],p1[3];
    vector_ld(center,0.0,0.0,0.0);
    vector_ld(size,0.0,0.0,0.0);
    rmax=0.0;
    if (fac.num==0) return;
    // bbox
    for (k=0;k<3;k++) p0[k]=fac.dat[0].p[0][k];
    for (k=0;k<3;k++) p1[k]=fac.dat[0].p[0][k];
    for (f=fac.dat,i=0;i<fac.num;i++,f++)
     for (j=0;j<3;j++)
      for (k=0;k<3;k++)
        {
        if (p0[k]>f->p[j][k]) p0[k]=f->p[j][k];
        if (p1[k]<f->p[j][k]) p1[k]=f->p[j][k];
        }
    vector_add(center,p0,p1); vector_mul(center,center,0.5);
    vector_sub(size  ,p1,p0); vector_mul(size  ,size  ,0.5);
                      rmax=size[0];
    if (rmax<size[1]) rmax=size[1];
    if (rmax<size[2]) rmax=size[2];
    // attr repair
    for (f=fac.dat,i=0;i<fac.num;i++,f++)
     if (f->attr==0) f->attr=32768;
    }
//---------------------------------------------------------------------------
void STL3D::compute_normals()
    {
    _fac *f; int i;
    for (f=fac.dat,i=0;i<fac.num;i++,f++) f->compute();
    }
//---------------------------------------------------------------------------
void STL3D::supports(reper &obj)
    {
    _fac *f,ff;
    int i,j,k;
    double p[3],dp[3],x0,y0,h0,x1,y1,x2,y2,h1,t;
    // some config values first
    const WORD   attr0=31<<10;              // support attr should be different than joint
    const WORD   attr1=31<<5;               // joint attr should be different than mesh,support
    const double grid0=8.0;                 // distance between supports
    const double grid1=2.0;                 // distance between joints
    const double gap=grid0/tan(45.0*deg);// distance between main support and mesh (joint size)
    const double ha=1.0;                    // main support side size
    // do not mess with these
    const double hx=    ha*cos(60.0*deg);   // half size of main support in x
    const double hy=0.5*ha*sin(60.0*deg);   // half size of main support in y
    const double grid2=0.4*hy;              // distance between joints bases
    const double ga=2.0*grid2*grid1/grid0;  // main support side size
    const double gx=hx*grid2/grid0;         // half size of joint support in x
    const double gy=hy*grid2/grid0;         // half size of joint support in y

    // apply placement obj (may lose some accuracy) not needed if matrices are not used
    for (f=fac.dat,i=0;i<fac.num;i++,f++)
        {
        for (j=0;j<3;j++)
            {
            for (k=0;k<3;k++) p[k]=f->p[j][k];  // float->double
            obj.l2g(p,p);
            for (k=0;k<3;k++) f->p[j][k]=p[k];  // double->float
            }
        for (k=0;k<3;k++) p[k]=f->n[k]; // float->double
        obj.l2g_dir(p,p);
        for (k=0;k<3;k++) f->n[k]=p[k]; // double->float
        } compute();

    // create supports
    for (x0=center[0]-size[0]+(0.5*grid0);x0<=center[0]+size[0]-(0.5*grid0);x0+=grid0)
     for (y0=center[1]-size[1]+(0.5*grid0);y0<=center[1]+size[1]-(0.5*grid0);y0+=grid0)
        {
        // cast ray x0,y0,0 in Z+ direction to check for mesh intersection to compute the support height h0
        h0=center[2]+size[2]+1e6;
        vector_ld(p,x0,y0,0.0);
        vector_ld(dp,0.0,0.0,+1.0);
        for (f=fac.dat,i=0;i<fac.num;i++,f++)
            {
            t=f->intersect_ray(p,dp);
            if ((t>=0.0)&&(t<h0)) h0=t;
            }
        if (h0>center[2]+size[2]+1e5) continue; // skip non intersected rays
        h0-=gap; if (h0<0.0) h0=0.0;
        // main suport prism
        ff.attr=attr0;
        // sides
        ff.attr=attr0;
        vectorf_ld(ff.p[0],x0-hx,y0-hy,0.0);
        vectorf_ld(ff.p[1],x0+hx,y0-hy,0.0);
        vectorf_ld(ff.p[2],x0-hx,y0-hy, h0); ff.compute(); fac.add(ff);
        vectorf_ld(ff.p[0],x0+hx,y0-hy,0.0);
        vectorf_ld(ff.p[1],x0+hx,y0-hy, h0);
        vectorf_ld(ff.p[2],x0-hx,y0-hy, h0); ff.compute(); fac.add(ff);

        vectorf_ld(ff.p[0],x0-hx,y0-hy, h0);
        vectorf_ld(ff.p[1],x0   ,y0+hy,0.0);
        vectorf_ld(ff.p[2],x0-hx,y0-hy,0.0); ff.compute(); fac.add(ff);
        vectorf_ld(ff.p[0],x0-hx,y0-hy, h0);
        vectorf_ld(ff.p[1],x0   ,y0+hy, h0);
        vectorf_ld(ff.p[2],x0   ,y0+hy,0.0); ff.compute(); fac.add(ff);

        vectorf_ld(ff.p[0],x0   ,y0+hy, h0);
        vectorf_ld(ff.p[1],x0+hx,y0-hy,0.0);
        vectorf_ld(ff.p[2],x0   ,y0+hy,0.0); ff.compute(); fac.add(ff);
        vectorf_ld(ff.p[0],x0   ,y0+hy, h0);
        vectorf_ld(ff.p[1],x0+hx,y0-hy, h0);
        vectorf_ld(ff.p[2],x0+hx,y0-hy,0.0); ff.compute(); fac.add(ff);
        // base triangles
        vectorf_ld(ff.p[0],x0   ,y0+hy,0.0);
        vectorf_ld(ff.p[1],x0+hx,y0-hy,0.0);
        vectorf_ld(ff.p[2],x0-hx,y0-hy,0.0); ff.compute(); fac.add(ff);
        vectorf_ld(ff.p[0],x0-hx,y0-hy, h0);
        vectorf_ld(ff.p[1],x0+hx,y0-hy, h0);
        vectorf_ld(ff.p[2],x0   ,y0+hy, h0); ff.compute(); fac.add(ff);

        // joints
        for (x1=x0-(0.5*grid0),x2=x0-(0.5*grid2);x1<=x0+(0.5*grid0);x1+=grid1,x2+=ga)
         for (y1=y0-(0.5*grid0),y2=y0-(1.9*grid2);y1<=y0+(0.5*grid0);y1+=grid1,y2+=ga)
            {
            // cast ray x1,y1,0 in Z+ direction to check for mesh intersection to compute the joint height h1
            h1=h0+gap+1e6;
            vector_ld(p,x1,y1,0.0);
            vector_ld(dp,0.0,0.0,+1.0);
            for (f=fac.dat,i=0;i<fac.num;i++,f++)
                {
                t=f->intersect_ray(p,dp);
                if ((t>=0.0)&&(t<h1)) h1=t;
                }
            if (h1>h0+gap+1e5) continue; // skip non intersected rays
            // tetrahedron joints
            ff.attr=attr1;
            // base triangle
            vectorf_ld(ff.p[0],x2   ,y2+gy,h0);
            vectorf_ld(ff.p[1],x2+gx,y2-gy,h0);
            vectorf_ld(ff.p[2],x2-gx,y2-gy,h0); ff.compute(); fac.add(ff);
            // sides
            vectorf_ld(ff.p[0],x2+gx,y2-gy,h0);
            vectorf_ld(ff.p[1],x2   ,y2+gy,h0);
            vectorf_ld(ff.p[2],x1   ,y1   ,h1); ff.compute(); fac.add(ff);
            vectorf_ld(ff.p[0],x2   ,y2+gy,h0);
            vectorf_ld(ff.p[1],x2-gx,y2-gy,h0);
            vectorf_ld(ff.p[2],x1   ,y1   ,h1); ff.compute(); fac.add(ff);
            vectorf_ld(ff.p[0],x2+gx,y2+gy,h0);
            vectorf_ld(ff.p[1],x2-gx,y2-gy,h0);
            vectorf_ld(ff.p[2],x1   ,y1   ,h1); ff.compute(); fac.add(ff);
            }
        }

    // reverse placement obj (may lose some accuracy) not needed if matrices are not used
    for (f=fac.dat,i=0;i<fac.num;i++,f++)
        {
        for (j=0;j<3;j++)
            {
            for (k=0;k<3;k++) p[k]=f->p[j][k];  // float->double
            obj.g2l(p,p);
            for (k=0;k<3;k++) f->p[j][k]=p[k];  // double->float
            }
        for (k=0;k<3;k++) p[k]=f->n[k]; // float->double
        obj.g2l_dir(p,p);
        for (k=0;k<3;k++) f->n[k]=p[k]; // double->float
        } compute();
    }
//---------------------------------------------------------------------------
void STL3D::load(AnsiString name)
    {
    int   adr,siz,hnd;
    BYTE *dat;
    AnsiString lin,s;
    int i,j,l,n;
    _fac f;

    reset(); f.attr=0;
    siz=0;
    hnd=FileOpen(name,fmOpenRead);
    if (hnd<0) return;
    siz=FileSeek(hnd,0,2);
        FileSeek(hnd,0,0);
    dat=new BYTE[siz];
    if (dat==NULL) { FileClose(hnd); return; }
    FileRead(hnd,dat,siz);
    FileClose(hnd);

    adr=0; s=txt_load_str(dat,siz,adr,true);
    // ASCII
    if (s=="solid")
        {
        _progress_init(siz); int progress_cnt=0;
        for (adr=0;adr<siz;)
            {
            progress_cnt++; if (progress_cnt>=128) { progress_cnt=0; _progress(adr); }
            lin=txt_load_lin(dat,siz,adr,true);
            for (i=1,l=lin.Length();i<=l;)
                {
                s=str_load_str(lin,i,true);
                if (s=="solid") { name=str_load_str(lin,i,true); break; }
                if (s=="endsolid") break;
                if (s=="facet")
                    {
                    j=0;
                    s=str_load_str(lin,i,true);
                    f.n[0]=str2num(str_load_str(lin,i,true));
                    f.n[1]=str2num(str_load_str(lin,i,true));
                    f.n[2]=str2num(str_load_str(lin,i,true));
                    }
                if (s=="vertex")
                 if (j<3)
                    {
                    f.p[j][0]=str2num(str_load_str(lin,i,true));
                    f.p[j][1]=str2num(str_load_str(lin,i,true));
                    f.p[j][2]=str2num(str_load_str(lin,i,true));
                    j++;
                    if (j==3) fac.add(f);
                    }
                break;
                }
            }
        }
    // binary
    else{
        adr=80;
        n=((DWORD*)(dat+adr))[0]; adr+=4;
        fac.allocate(n); fac.num=0;
        _progress_init(n); int progress_cnt=0;
        for (i=0;i<n;i++)
            {
            if (adr+50>siz) break;  // error
            progress_cnt++; if (progress_cnt>=128) { progress_cnt=0; _progress(i); }
            f.n[0]=((float*)(dat+adr))[0]; adr+=4;
            f.n[1]=((float*)(dat+adr))[0]; adr+=4;
            f.n[2]=((float*)(dat+adr))[0]; adr+=4;
            for (j=0;j<3;j++)
                {
                f.p[j][0]=((float*)(dat+adr))[0]; adr+=4;
                f.p[j][1]=((float*)(dat+adr))[0]; adr+=4;
                f.p[j][2]=((float*)(dat+adr))[0]; adr+=4;
                }
            f.attr=((WORD*)(dat+adr))[0]; adr+=2;   // attributes
            fac.add(f);
            }
        }
    _progress_done();
    delete[] dat;
    compute();
    }
//---------------------------------------------------------------------------
void STL3D::save(AnsiString name)
    {
    // ToDo
    }
//---------------------------------------------------------------------------
void _progress_init(int n)
    {
    #ifdef ComctrlsHPP
    if (progress==NULL) return;
    progress->Position=0;
    progress->Max=n;
    progress->Visible=true;
    #endif
    }
//---------------------------------------------------------------------------
void _progress     (int ix)
    {
    #ifdef ComctrlsHPP
    if (progress==NULL) return;
    progress->Position=ix;
    progress->Update();
    #endif
    }
//---------------------------------------------------------------------------
void _progress_done()
    {
    #ifdef ComctrlsHPP
    if (progress==NULL) return;
    progress->Visible=false;
    #endif
    }
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------

用法很簡單:

#include "STL3D.h"                  // STL mesh (this is the important stuff)
STL3D mesh;                         // point cloud and tetrahedronal mesh

mesh.load("space_invader_magnet.stl");
mesh.supports(obj); //  obj is object holding 4x4 uniform matrix of placement if you STL is already placed than it is not needed

我使用了我的OpenGL引擎中的很多東西,例如動態List<>模板:


List<double> xxx; double xxx[];相同double xxx[];
xxx.add(5); 在列表末尾加5
xxx[7]訪問數組元素(安全)
xxx.dat[7]訪問數組元素(不安全但快速的直接訪問)
xxx.num是數組的實際使用大小
xxx.reset()清除數組並設置xxx.num=0
xxx.allocate(100)100項目預分配空間

或向量和矩陣數學( vectorf_float*vector_double vector_使用)不是很重要。 如果需要數學運算,請參閱:

如果已經放置了STL (沒有矩陣),則根本不需要放置轉換,也不需要obj 該代碼反映了上面的項目符號。 我想盡可能地簡化它,因此尚無優化方法。

gapgrid常數在supports函數中進行了硬編碼,尚未設置為有效值。

[筆記]

現在,這僅覆蓋了問題的最基本部分,還有很多未解決的邊緣情況,以保持此“簡短”。 代碼本身不會檢查三角形的斜率是否在45度以上,但是可以通過簡單的法線角度檢查來完成,例如:

if (acos(dot(normal,(0.0,0.0,1.0))<45.0*deg) continue;

例如,如果對象的層數多於從基礎平面僅支持第一層的層,則需要在網格的各個部分之間添加支撐。 其余的必須使用其下方的層...並在支撐的兩側使用弱化的接縫。 類似於放置第一層支撐,您只需要在兩個方向上投射射線...或通過整個bbox投射連續射線並通過分析射線的法線方向(點的簡單符號)來檢查起始/結束表面產品)。 例如,這是網格放置,可能需要這樣做(對於某些技術):

星

在設計支撐架時,請記住您在打印過程中應遵守正確的繞線規則(CCW)和法線方向(向外)...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM