3

I am working on the university project of computation graphics, I have implemented the marching cubes algorithm following the guidelines of http://paulbourke.net/geometry/polygonise/. When I test the algorithm without linear interpolation i get an isosurface without holes, when I use interpolation for the edges I have some holes on the isosurface, for example:

here is the code of the linear interpolation

public PVector LinearInterp(PVector p1, PVector p2, int valp1, int valp2)
{
  
   double mu;
   PVector p= new PVector();
 
  if(compare(p2,p1))
   {
     PVector temp;
     temp=p1;
     p1=p2;
     p2=temp;
   
   }
   if(abs(valp1-valp2)>0.00001)
   {
      p.x=p1.x+((p2.x-p1.x)/(valp2-valp1)*(isovalue-valp1));
      p.y=p1.y+((p2.y-p1.y)/(valp2-valp1)*(isovalue-valp1));
      p.z=p1.z+((p2.z-p1.z)/(valp2-valp1)*(isovalue-valp1));
   }
   else 
     p=p1;
    return p;
}
 

public boolean compare(PVector p1, PVector p2)
{ // p1 < p
  if(p1.x<p2.x)
  return true;
  else if(p1.x > p2.x)
  return false;
   
  if(p1.y<p2.y)
  return true;
  else if(p1.y > p2.y)
  return false;
   
  if(p1.x<p2.x)
  return true;
  else if(p1.z > p2.z)
  return false;
 
  return false;
}

SOLUTION: The problem was the function to calculate the interpolation point, here is the correct function

 public PVector LinearInterp(PVector p1, PVector p2, int valp1, int valp2)
        {
          
            PVector p= new PVector();
            p.x = p1.x;
            p.y = p1.y;
            p.z = p1.z;
            float val=0;
            if(p1.x != p2.x)
            {
              val = p2.x + (p1.x - p2.x) * (isovalue-valp2)/(valp1-valp2);
              p.x=val;
            }else if(p1.y!=p2.y)
            {
                val = p2.y + (p1.y-p2.y)*(isovalue-valp2)/(valp1-valp2);
                p.y=val;
            }
            else
            {
                val=p2.z+(p1.z-p2.z)*(isovalue-valp2)/(valp1-valp2);
                p.z=val;
            }
            return p;
      }
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Unfortunately I don't have the resources to provide a detailed answer at the moment. FWIW double check you're not simply swapping references `if(compare(p2,1))` is true. (Try using [`set()`](https://processing.org/reference/PVector_set_.html) .e.g. ```PVector temp = p1.copy();p1.set(p2);p2.set(temp);```). Also double check the threshold condition (I would expect the absolute difference between 2 `int`s to also be an `int`). Also [toxclibs supports IsoSurfaces](https://github.com/postspectacular/toxiclibs/blob/6896bc17e85320e886bdf0819c66491ce484728e/src.volume/toxi/volume/HashIsoSurface.java). – George Profenza Apr 19 '21 at 00:48
  • The other things that comes mind is checking normals/vertex order winding to ensure the avoid the situations where faces may be accidentally flipped(and simply not rendering correctly, though the vertex positions might be valid) – George Profenza Apr 19 '21 at 00:50
  • Hi, thank you very much for your answer, I applied the changes you suggested and I got a jagged isosurface, as you suggested the problem could be the ordering of vertices and edges? http://paulbourke.net/geometry/polygonise/ and https://afni.nimh.nih.gov/pub/dist/src/SUMA/MarchingCubes/LookUpTable.h use the same configuration table but with different orderings. I used the first, now I try to change with the second. – Sergio Di Meglio Apr 19 '21 at 09:25
  • 1
    The problem has not been solved, now I add the photo of the new mesh obtained, perhaps it is easier for you to understand the problem. some triangles appear to be facing inwards. – Sergio Di Meglio Apr 19 '21 at 11:18
  • You should post your attempt. The progress sounds good: nicely done! Triangles facing inwards might be normals of the triangles pointing inwards: double check the order in which you add the vertices of a triangle (clockwise vs counter-clockwise): this will affect the triangle's normal) – George Profenza Apr 29 '21 at 23:28

0 Answers0