VCG Library
trimesh_isosurface.cpp
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #include <vcg/complex/complex.h>
24 #include <vcg/math/perlin_noise.h>
25 #include <vcg/complex/algorithms/create/marching_cubes.h>
26 #include <vcg/complex/algorithms/create/mc_trivial_walker.h>
27 #include <wrap/io_trimesh/export_ply.h>
28 
29 using namespace std;
30 using namespace vcg;
31 
32 typedef float ScalarType;
33 
34 class MyFace;
35 class MyVertex;
36 
37 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
38  Use<MyFace> ::AsFaceType>{};
39 
40 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags>{};
41 class MyFace : public Face< MyUsedTypes, face::VertexRef, face::BitFlags> {};
42 
43 class MyMesh : public vcg::tri::TriMesh< std::vector< MyVertex>, std::vector< MyFace > > {};
44 
45 
46 
47 typedef SimpleVolume<SimpleVoxel<float> > MyVolume;
48 
49 int main(int /*argc*/ , char **/*argv*/)
50 {
51  MyVolume volume;
52 
53  typedef vcg::tri::TrivialWalker<MyMesh,MyVolume> MyWalker;
54  typedef vcg::tri::MarchingCubes<MyMesh, MyWalker> MyMarchingCubes;
55  MyWalker walker;
56 
57 
58  // Simple initialization of the volume with some cool perlin noise
59  vcg::Box3f bb(vcg::Point3f(-1,-1,-1),vcg::Point3f(1,1,1));
60  volume.Init(Point3i(64,64,64),bb);
61  for(int i=0;i<64;i++)
62  for(int j=0;j<64;j++)
63  for(int k=0;k<64;k++)
64  volume.Val(i,j,k)=(j-32)*(j-32)+(k-32)*(k-32) + i*10*(float)math::Perlin::Noise(i*.2,j*.2,k*.2);
65 
66 
67  // MARCHING CUBES
68  MyMesh mc_mesh;
69  printf("[MARCHING CUBES] Building mesh...");
70  MyMarchingCubes mc(mc_mesh, walker);
71  walker.BuildMesh<MyMarchingCubes>(mc_mesh, volume, mc, 20*20);
72  vcg::tri::io::ExporterPLY<MyMesh>::Save( mc_mesh, "marching_cubes.ply");
73 
74  printf("OK!\n");
75 };