Skip to Content.
Sympa Menu

cgal-discuss - Re: [cgal-discuss] Re: Can CGAL deal with STL file format?

Subject: CGAL users discussion list

List archive

Re: [cgal-discuss] Re: Can CGAL deal with STL file format?


Chronological Thread 
  • From: Andreas Fabri <>
  • To:
  • Subject: Re: [cgal-discuss] Re: Can CGAL deal with STL file format?
  • Date: Wed, 05 Dec 2012 09:46:25 +0100
  • Organization: GeometryFactory

Hello,

For the definition of the STL have a look at
http://en.wikipedia.org/wiki/STL_%28file_format%29


Attached you find a small program that converts a binary STL file
to an ascii OFF file.

Note that it ignores the normals.
Note also that in the code you find a brute force snapping,
as the data I had to convert had little gaps and overlaps of
triangles because points where not identical.

It should be straightforward to combine the code with the
Incremental Builder

http://www.cgal.org/Manual/latest/doc_html/cgal_manual/Polyhedron/Chapter_main.html#Subsection_25.6.2

At least if your triangles represent a surface, and are
oriented correctly.

Best regards,

Andreas


On 05/12/2012 09:29, areslp wrote:
Hi,
You can convert stl file to off file by Trimesh(c++), OpenMesh(c++),
Graph toolbox(matlab), Meshlab(Gui) etc. It's really simple to do it.
------------------------------------------------------------------------
Best Regards,
Areslp
*From:* 魏华祎
<mailto:>
*Date:* 2012-12-05 15:38
*To:* cgal-discuss
<mailto:>
*Subject:* Re: [cgal-discuss] Re: Can CGAL deal with STL file format?
Hi, Xianyong,

Thanks for your reply.

STL (STereoLithography) is a file format native to the
stereolithography CAD software created by 3D Systems. it is widely used
for rapid prototyping and computer-aided manufacturing. STL files
describe only the surface geometry of a three dimensional object without
any representation of color, texture or other common CAD model attributes.

Yes, CGAL surport the IO of OFF file very well. Maybe CGAL
haven't implement such an interface for STL file.

Best

Huayi

2012/12/5 ayongwust_sjtu
<

<mailto:>>

What do you mean of STL file format.

You can refer to Polyheron_3 example to read/write .off model file.

CGAL is a computational geometry library, therefore, it does not
care about
read/write file matters.






-----
-------------

Regards,
Xianyong Liu
--
View this message in context:

http://cgal-discuss.949826.n4.nabble.com/Can-CGAL-deal-with-STL-file-format-tp4656337p4656338.html
Sent from the cgal-discuss mailing list archive at Nabble.com.

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





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

phone: +33.492.954.912 skype: andreas.fabri
#include <boost/cstdint.hpp> 
#include "boost/tuple/tuple.hpp"
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/compute_average_spacing.h>

typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_3 Point_3;

int main(int argc, char* argv[])
{

  std::ifstream obj(argv[1], std::ios::in | std::ios::binary);
  for(int i=0; i < 80; i++){
    boost::uint8_t c;
    obj.read(reinterpret_cast<char*>(&c), sizeof(c));
    std::cerr << c;
  }
  std::cerr << std::endl;
  
  boost::uint32_t N32;
  obj.read(reinterpret_cast<char*>(&N32), sizeof(N32));
  unsigned int N = N32;
  std::cerr << N << " triangles" << std::endl;
  
  std::vector<Point_3> points;
  std::map<Point_3,int> pmap;
  typedef boost::tuple<int,int,int> Face;
  std::vector<Face> faces;
  faces.reserve(N);


  int number_of_points = 0;
  int number_of_snapped_points = 0;

  for(int i=0; i < N; i++){
    float normal[3];
    obj.read(reinterpret_cast<char*>(&normal[0]), sizeof(normal[0]));
    obj.read(reinterpret_cast<char*>(&normal[1]), sizeof(normal[1]));
    obj.read(reinterpret_cast<char*>(&normal[2]), sizeof(normal[2]));


    int index[3];
    for(int j=0; j < 3; j++){
      float x,y,z;

      obj.read(reinterpret_cast<char*>(&x), sizeof(x));
      obj.read(reinterpret_cast<char*>(&y), sizeof(y));
      obj.read(reinterpret_cast<char*>(&z), sizeof(z));
      Point_3 p(x,y,z);
      if(pmap.find(p)== pmap.end()){
        // check brute force if there is a close point
        bool found_close_point = false;
        for(int k = 0; k < points.size(); k++){
          if(sqrt(CGAL::squared_distance(p,points[k])) < 0.00001){
            index[j] = k;
            found_close_point = true;
            number_of_snapped_points++;
          }
        }
        if(! found_close_point){
          points.push_back(p);
          index[j] = number_of_points;
          pmap[p]= number_of_points++;
        }
      } else {
        index[j] = pmap[p];
      }
    }
    faces.push_back(boost::make_tuple(index[0],index[1],index[2]));
    char c;
    obj.read(reinterpret_cast<char*>(&c), sizeof(c));
    obj.read(reinterpret_cast<char*>(&c), sizeof(c));
  }
  std::cerr << number_of_snapped_points << " snapped points" << std::endl;
  std::cout.precision(20);
  
#if 0
  CGAL::compute_average_spacing(points.begin(),points.end(),1);
  return 0;
#endif

  std::cout << "OFF\n" << points.size() << " " << faces.size()  << " 0" << std::endl;
  for(int i=0; i < points.size(); i++){
    std::cout << points[i] << std::endl;
  }

  for(int i=0; i < faces.size(); i++){
    std::cout << "3 " << boost::get<0>(faces[i]) << " " << boost::get<1>(faces[i]) << " " << boost::get<2>(faces[i]) << std::endl;
  }

  return 0;
}



Archive powered by MHonArc 2.6.18.

Top of Page