Support for stl files

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Support for stl files

Riedel, Torge

Hi,

 

is there an api in cgal framework for reading / writing stl files? I have found api for reading / writing OFF data. My evaluation file is in stl format.

 

Kind regards

Torge


CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711 7825-30, Fax: +49 711 7825-4000, Internet: www.cenit.com
Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen, Oelsnitz, Ratingen, Saarbruecken
Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des Aufsichtsrats), Hubert Leypoldt, Andreas Karrer
Bankverbindungen:
Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 0166 1040 00 SWIFT-CODE : DEUTDESS,
Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 0532 0155 00 SWIFT-Code : COBADEFF600,
Registergericht: Amtsgericht Stuttgart
Handelsregister: HRB Nr. 19117
Umsatzsteuer: ID-Nr. DE 147 862 777
Reply | Threaded
Open this post in threaded view
|

Re: Support for stl files

majid
you can use openmesh very easily to convert between stl and off



On Thu, 8 Feb 2018 at 18:20, Riedel, Torge

Hi,

 

is there an api in cgal framework for reading / writing stl files? I have found api for reading / writing OFF data. My evaluation file is in stl format.

 

Kind regards

Torge


CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711 7825-30, Fax: +49 711 7825-4000, Internet: www.cenit.com
Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen, Oelsnitz, Ratingen, Saarbruecken
Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des Aufsichtsrats), Hubert Leypoldt, Andreas Karrer
Bankverbindungen:
Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 0166 1040 00 SWIFT-CODE : DEUTDESS,
Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 0532 0155 00 SWIFT-Code : COBADEFF600,
Registergericht: Amtsgericht Stuttgart
Handelsregister: HRB Nr. 19117
Umsatzsteuer: ID-Nr. DE 147 862 777
Reply | Threaded
Open this post in threaded view
|

Re: Support for stl files

Sean Hardesty
In reply to this post by Riedel, Torge
I'd be interested to hear from more knowledgeable users, but in case it is helpful to anyone, I'll share what I do for writing:

void write_stl_triangle(std::ofstream &stl_binary, const vec3d v[3]) {
    vec3d p1 = v[1] - v[0];
    vec3d p2 = v[2] - v[0];
    vec3d n = p1 % p2;
    n.normalize();

    float data[3];

    // normal vector
    for(unsigned k=0;k<3;k++) {
        data[k] = (float)n[k];
    }
    stl_binary.write((char*)data, 3*sizeof(float));

    // vertices
    for(unsigned i=0;i<3;i++) {
        for(unsigned k=0;k<3;k++) {
            data[k] = (float)v[i][k];
        }
        stl_binary.write((char*)data, 3*sizeof(float));
    }

    // attribute byte count (can be used to indicate, e.g., color in some interpretations of STL)
    uint16_t abc=0;
    stl_binary.write((char*)&abc, sizeof(abc));
}

void write_stl_header(std::ofstream &stl_binary, uint64_t ntri) {
    const int header_size = 80;
    char header[header_size];
    std::fill_n(header, header_size, 0);

    stl_binary.write(header, header_size);

    uint32_t ntri32 = (uint32_t)ntri;

    stl_binary.write((char*)&ntri32, sizeof(uint32_t));
}

void write_stl_from_cgal(std::ofstream &stl_binary, const Mesh &mesh) {
    for(auto ti : mesh.faces()) {
        vec3d v[3];

        assert(mesh.degree(ti) == 3);

        unsigned j=0;
        for(auto vi : CGAL::vertices_around_face(mesh.halfedge(ti),mesh)) {
            v[j][0] = CGAL::to_double(mesh.point(vi).x());
            v[j][1] = CGAL::to_double(mesh.point(vi).y());
            v[j][2] = CGAL::to_double(mesh.point(vi).z());
            j++;
        }

        write_stl_triangle(stl_binary, v);
    }
}

The class vec3d is a simple 3d vector with some overloaded operators. Mesh is a CGAL Surface_mesh. You have to create the binary header before writing the triangles. Reading obviously is more involved, as you have to determine the connectivity based on some tolerances.

    Sincerely,
    Sean Hardesty


2018-02-08 9:19 GMT-06:00 Riedel, Torge <[hidden email]>:

Hi,

 

is there an api in cgal framework for reading / writing stl files? I have found api for reading / writing OFF data. My evaluation file is in stl format.

 

Kind regards

Torge


CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: <a href="tel:0711%20782530" value="+49711782530" target="_blank">+49 711 7825-30, Fax: <a href="tel:0711%2078254000" value="+4971178254000" target="_blank">+49 711 7825-4000, Internet: www.cenit.com
Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen, Oelsnitz, Ratingen, Saarbruecken
Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des Aufsichtsrats), Hubert Leypoldt, Andreas Karrer
Bankverbindungen:
Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 0166 1040 00 SWIFT-CODE : DEUTDESS,
Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 0532 0155 00 SWIFT-Code : COBADEFF600,
Registergericht: Amtsgericht Stuttgart
Handelsregister: HRB Nr. 19117
Umsatzsteuer: ID-Nr. DE 147 862 777

Reply | Threaded
Open this post in threaded view
|

Re: Support for stl files

andreas.fabri
In reply to this post by Riedel, Torge
Hello,


You can call
   bool
   read_STL( std::istream& input,
             std::vector< cpp11::array<double,3> >& points,
             std::vector< cpp11::array<int,3> >& facets,
             bool verbose = false)
in

CGAL-4.11/include/CGAL/IO/STL_reader.h



  std::ifstream input(argv[1], std::ios::binary);
   std::vector< CGAL::cpp11::array<double,3> > points;
   std::vector< CGAL::cpp11::array<int,3> > triangles;

   CGAL::read_STL(input,
                  points,
                  triangles);

   std::cout << points.size() << "  "  << triangles.size() << std::endl;
   Mesh m;
   if
(CGAL::Polygon_mesh_processing::is_polygon_soup_a_polygon_mesh(triangles)){
     std::cout << "it is a polygon mesh" << std::endl;
     CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points,
triangles, m);
   }


Best regards,

Andreas

On 2/8/2018 4:19 PM, Riedel, Torge wrote:

> Hi,
>
> is there an api in cgal framework for reading / writing stl files? I
> have found api for reading / writing OFF data. My evaluation file is in
> stl format.
>
> Kind regards
>
> Torge
>
>
> CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711
> 7825-30, Fax: +49 711 7825-4000, Internet: www.cenit.com
> Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen,
> Oelsnitz, Ratingen, Saarbruecken
> Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
> Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des
> Aufsichtsrats), Hubert Leypoldt, Andreas Karrer
> Bankverbindungen:
> Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 0166
> 1040 00 SWIFT-CODE : DEUTDESS,
> Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 0532
> 0155 00 SWIFT-Code : COBADEFF600,
> Registergericht: Amtsgericht Stuttgart
> Handelsregister: HRB Nr. 19117
> Umsatzsteuer: ID-Nr. DE 147 862 777

--
Andreas Fabri, PhD
Chief Officer, GeometryFactory
Editor, The CGAL Project

phone: +33.492.954.912    skype: andreas.fabri

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss


Reply | Threaded
Open this post in threaded view
|

AW: [cgal-discuss] Support for stl files

Riedel, Torge
Hi,

Thank you very much. I think I searched in the wrong place.
Nevertheless I was not able to read my STL since it is in binary format. (File starts with "solid binary"). CGAL::read_STL continues reading it and does not stop. So maybe there is a check missing for binary format.

Any chance to read binary STL files with CGAL?

Kind regards
Torge

> -----Ursprüngliche Nachricht-----
> Von: [hidden email] [mailto:[hidden email]] Im
> Auftrag von Andreas Fabri
> Gesendet: Freitag, 9. Februar 2018 08:04
> An: [hidden email]
> Betreff: Re: [cgal-discuss] Support for stl files
>
> Hello,
>
>
> You can call
>    bool
>    read_STL( std::istream& input,
>              std::vector< cpp11::array<double,3> >& points,
>              std::vector< cpp11::array<int,3> >& facets,
>              bool verbose = false)
> in
>
> CGAL-4.11/include/CGAL/IO/STL_reader.h
>
>
>
>   std::ifstream input(argv[1], std::ios::binary);
>    std::vector< CGAL::cpp11::array<double,3> > points;
>    std::vector< CGAL::cpp11::array<int,3> > triangles;
>
>    CGAL::read_STL(input,
>                   points,
>                   triangles);
>
>    std::cout << points.size() << "  "  << triangles.size() << std::endl;
>    Mesh m;
>    if
> (CGAL::Polygon_mesh_processing::is_polygon_soup_a_polygon_mesh(trian
> gles)){
>      std::cout << "it is a polygon mesh" << std::endl;
>
> CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points,
> triangles, m);
>    }
>
>
> Best regards,
>
> Andreas
>
> On 2/8/2018 4:19 PM, Riedel, Torge wrote:
> > Hi,
> >
> > is there an api in cgal framework for reading / writing stl files? I
> > have found api for reading / writing OFF data. My evaluation file is
> > in stl format.
> >
> > Kind regards
> >
> > Torge
> >
> >
> > CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711
> > 7825-30, Fax: +49 711 7825-4000, Internet: www.cenit.com
> > Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen,
> > Oelsnitz, Ratingen, Saarbruecken
> > Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
> > Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des
> > Aufsichtsrats), Hubert Leypoldt, Andreas Karrer
> > Bankverbindungen:
> > Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070
> > 0166
> > 1040 00 SWIFT-CODE : DEUTDESS,
> > Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071
> > 0532
> > 0155 00 SWIFT-Code : COBADEFF600,
> > Registergericht: Amtsgericht Stuttgart
> > Handelsregister: HRB Nr. 19117
> > Umsatzsteuer: ID-Nr. DE 147 862 777
>
> --
> Andreas Fabri, PhD
> Chief Officer, GeometryFactory
> Editor, The CGAL Project
>
> phone: +33.492.954.912    skype: andreas.fabri
>
> --
> You are currently subscribed to cgal-discuss.
> To unsubscribe or access the archives, go to
> https://sympa.inria.fr/sympa/info/cgal-discuss
>


CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711 7825-30, Fax: +49 711 7825-4000, Internet: www.cenit.com
Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen, Oelsnitz, Ratingen, Saarbruecken
Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des Aufsichtsrats), Hubert Leypoldt, Andreas Karrer
Bankverbindungen:
Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 0166 1040 00 SWIFT-CODE : DEUTDESS,
Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 0532 0155 00 SWIFT-Code : COBADEFF600,
Registergericht: Amtsgericht Stuttgart
Handelsregister: HRB Nr. 19117
Umsatzsteuer: ID-Nr. DE 147 862 777


--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss


Reply | Threaded
Open this post in threaded view
|

Re: Support for stl files

andreas.fabri

In fact it can read binary, but when we implemented it we read:

A binary STL file has an 80-character header (which is generally
ignored, but should never begin with "solid" because that will lead most
software to assume that this is an ASCII STL file
source: https://en.wikipedia.org/wiki/STL_(file_format)#Binary_STL

It is fixed in the master branch on github.


https://github.com/CGAL/cgal/blob/master/Polyhedron_IO/include/CGAL/IO/STL_reader.h

Alternatively, replace the word solid in your binary file .

andreas


On 2/9/2018 9:57 AM, Riedel, Torge wrote:

> Hi,
>
> Thank you very much. I think I searched in the wrong place.
> Nevertheless I was not able to read my STL since it is in binary format. (File starts with "solid binary"). CGAL::read_STL continues reading it and does not stop. So maybe there is a check missing for binary format.
>
> Any chance to read binary STL files with CGAL?
>
> Kind regards
> Torge
>
>> -----Ursprüngliche Nachricht-----
>> Von: [hidden email] [mailto:[hidden email]] Im
>> Auftrag von Andreas Fabri
>> Gesendet: Freitag, 9. Februar 2018 08:04
>> An: [hidden email]
>> Betreff: Re: [cgal-discuss] Support for stl files
>>
>> Hello,
>>
>>
>> You can call
>>     bool
>>     read_STL( std::istream& input,
>>               std::vector< cpp11::array<double,3> >& points,
>>               std::vector< cpp11::array<int,3> >& facets,
>>               bool verbose = false)
>> in
>>
>> CGAL-4.11/include/CGAL/IO/STL_reader.h
>>
>>
>>
>>    std::ifstream input(argv[1], std::ios::binary);
>>     std::vector< CGAL::cpp11::array<double,3> > points;
>>     std::vector< CGAL::cpp11::array<int,3> > triangles;
>>
>>     CGAL::read_STL(input,
>>                    points,
>>                    triangles);
>>
>>     std::cout << points.size() << "  "  << triangles.size() << std::endl;
>>     Mesh m;
>>     if
>> (CGAL::Polygon_mesh_processing::is_polygon_soup_a_polygon_mesh(trian
>> gles)){
>>       std::cout << "it is a polygon mesh" << std::endl;
>>
>> CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points,
>> triangles, m);
>>     }
>>
>>
>> Best regards,
>>
>> Andreas
>>
>> On 2/8/2018 4:19 PM, Riedel, Torge wrote:
>>> Hi,
>>>
>>> is there an api in cgal framework for reading / writing stl files? I
>>> have found api for reading / writing OFF data. My evaluation file is
>>> in stl format.
>>>
>>> Kind regards
>>>
>>> Torge
>>>
>>>
>>> CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711
>>> 7825-30, Fax: +49 711 7825-4000, Internet: www.cenit.com
>>> Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen,
>>> Oelsnitz, Ratingen, Saarbruecken
>>> Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
>>> Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des
>>> Aufsichtsrats), Hubert Leypoldt, Andreas Karrer
>>> Bankverbindungen:
>>> Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070
>>> 0166
>>> 1040 00 SWIFT-CODE : DEUTDESS,
>>> Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071
>>> 0532
>>> 0155 00 SWIFT-Code : COBADEFF600,
>>> Registergericht: Amtsgericht Stuttgart
>>> Handelsregister: HRB Nr. 19117
>>> Umsatzsteuer: ID-Nr. DE 147 862 777
>>
>> --
>> Andreas Fabri, PhD
>> Chief Officer, GeometryFactory
>> Editor, The CGAL Project
>>
>> phone: +33.492.954.912    skype: andreas.fabri
>>
>> --
>> You are currently subscribed to cgal-discuss.
>> To unsubscribe or access the archives, go to
>> https://sympa.inria.fr/sympa/info/cgal-discuss
>>
>
>
> CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711 7825-30, Fax: +49 711 7825-4000, Internet: www.cenit.com
> Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen, Oelsnitz, Ratingen, Saarbruecken
> Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
> Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des Aufsichtsrats), Hubert Leypoldt, Andreas Karrer
> Bankverbindungen:
> Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 0166 1040 00 SWIFT-CODE : DEUTDESS,
> Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 0532 0155 00 SWIFT-Code : COBADEFF600,
> Registergericht: Amtsgericht Stuttgart
> Handelsregister: HRB Nr. 19117
> Umsatzsteuer: ID-Nr. DE 147 862 777
>
>

--
Andreas Fabri, PhD
Chief Officer, GeometryFactory
Editor, The CGAL Project

phone: +33.492.954.912    skype: andreas.fabri

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss