An Implementation of the Marching Cubes Algorithm  1.0
tst-stdmc.cpp File Reference

A driver to test the implementation of the MC algorithm. More...

#include <iostream>
#include <cstdlib>
#include "grid.hpp"
#include "surfbuilder.hpp"
#include "3dvector.hpp"
Include dependency graph for tst-stdmc.cpp:

Go to the source code of this file.

Functions

bool check_grid (const Grid &g)
 Decides whether a given 3D grid is consistent, i.e., whether all values assigned to the vertices of the grid are distinct from zero and the values assigned to the boundary points are greater than zero.
double torus (double x, double y, double z)
 Returns the value of an implicit function whose the inverse image of the value zero defines a torus with major radius 4 and minor radius 2.
int main ()
 A simple test for an implementation of the MC algorithm.

Detailed Description

A driver to test the implementation of the MC algorithm.

Author:
Marcelo Ferreira Siqueira Universidade Federal do Rio Grande do Norte,
Departamento de Informatica e Matematica Aplicada,
mfsiqueira at gmail (dot) com
Version:
1.0
Date:
August 2012
Attention:
This program is distributed WITHOUT ANY WARRANTY, and it may be freely redistributed under the condition that the copyright notices are not removed, and no compensation is received. Private, research, and institutional use is free. Distribution of this code as part of a commercial system is permissible ONLY BY DIRECT ARRANGEMENT WITH THE AUTHOR.

Definition in file tst-stdmc.cpp.


Function Documentation

bool check_grid ( const Grid g)

Decides whether a given 3D grid is consistent, i.e., whether all values assigned to the vertices of the grid are distinct from zero and the values assigned to the boundary points are greater than zero.

Parameters:
gA 3D grid
Returns:
The logic value true of the grid is consistent, and the logic value false otherwise.

Definition at line 56 of file tst-stdmc.cpp.

References mc::Grid::get_size_x(), mc::Grid::get_size_y(), mc::Grid::get_size_z(), and mc::Grid::get_value().

Referenced by main().

{
  unsigned sx = g.get_size_x() ;
  unsigned sy = g.get_size_y() ;
  unsigned sz = g.get_size_z() ;

  for ( unsigned j = 0 ; j < sy ; j++ ) {
    for ( unsigned i = 0 ; i < sx ; i++ ) {
      if ( 
          ( g.get_value( i , j ,      0 ) <= 0 ) ||
          ( g.get_value( i , j , sz - 1 ) <= 0 ) 
         ) 
      {
        return false ;
      }
    }
  }

  for ( unsigned k = 0 ; k < sz ; k++ ) {
    for ( unsigned i = 0 ; i < sx ; i++ ) {
      if ( 
          ( g.get_value( i ,      0 , k ) <= 0 ) || 
          ( g.get_value( i , sy - 1 , k ) <= 0 ) 
         ) 
      {
        return false ;
      }
    }
  }

  for ( unsigned k = 0 ; k < sz ; k++ ) {
    for ( unsigned j = 0 ; j < sy ; j++ ) {
      if ( 
          ( g.get_value(     0 , j , k ) <= 0 ) || 
          ( g.get_value( sx - 1, j , k ) <= 0 )
         ) 
      {
        return false ;
      }
    }
  }

  return true ;
}
int main ( )

A simple test for an implementation of the MC algorithm.

Returns:
An integer number.

Definition at line 149 of file tst-stdmc.cpp.

References common::t3DVector::_x, common::t3DVector::_y, common::t3DVector::_z, check_grid(), mc::Grid::get_origin(), mc::Grid::get_size_x(), mc::Grid::get_size_y(), mc::Grid::get_size_z(), mc::Grid::get_spacing_x(), mc::Grid::get_spacing_y(), mc::Grid::get_spacing_z(), mc::SurfBuilder::run(), mc::Grid::set_value(), and torus().

{
  //
  // Create a 3D grid.
  //

  std::cerr << "Create a 3D grid..." 
            << std::endl ;

  Grid g( 
         26 ,                                  // Number of points in the X direction.
         26 ,                                  // Number of points in the Y direction.
         26 ,                                  // Number of points in the Z direction.
         t3DVector( -7.00 , -7.00 , -7.00 ) ,  // Grid origin.
         t3DVector(  0.56 ,  0.56 ,  0.56 )    // Length of the edges in each direction.
        ) ;

  //
  // Assign value to each grid point.
  //

  std::cerr << "Compute function values at the grid vertices..." 
            << std::endl ;

  unsigned sx = g.get_size_x() ;
  unsigned sy = g.get_size_y() ;
  unsigned sz = g.get_size_z() ;

  for ( unsigned i = 0 ; i < sx ; i++ ) {
    for ( unsigned j = 0 ; j < sy ; j++ ) {
      for ( unsigned k = 0 ; k < sz ; k++ ) {
        t3DVector pt = g.get_origin() ;

        double x = pt._x + i * g.get_spacing_x() ;
        double y = pt._y + j * g.get_spacing_y() ;
        double z = pt._z + k * g.get_spacing_z() ;

        double f = torus( x , y , z ) ;

        g.set_value( i , j , k , f ) ;
      }
    }
  }


  //
  // Check grid consistency.
  //

  std::cerr << "Check the consistency of the grid..." 
            << std::endl ;

  if ( !check_grid( g ) ) {
    std::cerr << "The grid is not consistent" << std::endl ;
    return EXIT_FAILURE;
  }

  //
  // Run the MC algorithm.
  //

  std::cerr << "Run the Marching Cubes algorithm..." 
            << std::endl ;

  SurfBuilder sb( &g ) ;

  sb.run() ;

  std::cerr << "Done!" << std::endl ;

  return EXIT_SUCCESS ;
}
double torus ( double  x,
double  y,
double  z 
)

Returns the value of an implicit function whose the inverse image of the value zero defines a torus with major radius 4 and minor radius 2.

Parameters:
xThe first Cartesian coordinate of a point.
yThe second Cartesian coordinate of a point.
zThe third Cartesian coordinate of a point.
Returns:
The value of the function at the given point.

Definition at line 115 of file tst-stdmc.cpp.

Referenced by main().

{
  double x2 = x * x ;
  double y2 = y * y ;
  double z2 = z * z ;

  double ff = 4 - sqrt( x2 + y2 ) ;

  ff *= ff ;

  ff += ( z2 - 4 ) ; 

  return ff ;
}