Buggy Artifacts at Slicing .stl Models

50 Views Asked by At

I’m creating a 3D slicer, and as part of my efforts I have made quite an advancement. I’m currently able to slice most .stl files pretty good, but unfortunately, I did encounter a few setbacks. I noticed I have some buggy artifacts as a part of my slicing (.stl to .png) – example below:

First Bug

I zoned in on the problem: there were 2 parts of my code that logically collided:

1.Generating the Outline:

  const auto result = intersection(line_seg, segment->segment());
  double length = line_seg.bbox().xmax() - line_seg.bbox().xmin();
  if (result) {
    Point_2 point;
    if (assign(point, result)) {
      int col = floor(((point.x() - x_min_) / length) * num_cols_);
     (*data).data[row * num_cols_ + col] = 255;

The logic here is straightforward: search for intersections and fill them 2. Filling the inside of the model:

  for (int y_index = 0; y_index < num_rows; ++y_index) {
    bool fill_pixels = false;
    int col_offset = num_cols * y_index;
    for (int x_index = 0; x_index < num_cols; ++x_index) {
      // If we enter material just paint the pixels until we find the exit
      if (data[col_offset + x_index] > 0) {
        fill_pixels = !fill_pixels;
        grayscale_mat.at<uchar>(y_index + LENGTHOFFSET, x_index + WIDTHOFFSET) = 255;
        continue;
      }
      if (fill_pixels) {
        grayscale_mat.at<uchar>(y_index + LENGTHOFFSET, x_index + WIDTHOFFSET) = 255;
      }
    }
  }

purpose: for each line, fill the gaps between the odd and even outlines.

This was, of course, a problem: what if there were an odd number of points – like the top vertex of a triangle, or the top point of a circle?

I tried to modify it like this:

      if((*data).data[row * num_cols_ + col] == 0)
        (*data).data[row * num_cols_ + col] = 255;
      else
        (*data).data[row * num_cols_ + col + 1] = 255;

Logic: if the point is already “occupied” – just use the next, thus insuring that each segment has its own start/end – and no pixels are used for 2 different segments.

This did solve most of the cases, but still has some pretty bad results when dealing with segments that are not aligned with the normal x-y axis:

Current Bug

I’d really appreciate it if someone has any idea on how to solve this problem, since after many weeks of dealing with it, I’m currently helpless. Also, this is one of my very first posts- so please, if there are any problems with my post – please don’t dislike it! Just let me know and I’ll fix it right away.

0

There are 0 best solutions below