Exemplo n.º 1
0
void SPHSystem::writeFrame(float* c = 0) {
    for (int i = 0; i < n; ++i) {
        Vec x = p[i];
        float ci = c ? c[i] : 0;
        //cout << p[i].x << ' ' << p[i].y << ' '<< p[i].z << ' ' << ci << endl;
        
        logstream.write(reinterpret_cast<const char*>(&x.x), sizeof(float));
        logstream.write(reinterpret_cast<const char*>(&x.y), sizeof(float));
        logstream.write(reinterpret_cast<const char*>(&x.z), sizeof(float));
        logstream.write(reinterpret_cast<const char*>(&ci), sizeof(float));
    }
    
    vector<float> voxels = voxelize(64, 64, 64);
    // write the voxels to a file
    stringstream ss;
    ss << "frame" << curframe << ".bin";
    cout << "writing file " << ss.str() << " with " << voxels.size() << " voxels." << endl;
    ofstream fout(ss.str(), ios::binary);
    fout.write(reinterpret_cast<const char*>(&voxels[0]), sizeof(float)*voxels.size());
    fout.close();
    
    CIsoSurface<float> mc;
    cout << "extracing mesh with marching cubes ..." << endl;
    mc.GenerateSurface(&voxels[0], 1.0f, 63, 63, 63, 1.0, 1.0, 1.0);
    cout << "done." << endl;
    stringstream ss2;
    ss2 << "frame" << curframe << ".obj";
    cout << "writing file " << ss2.str() << endl;
    mc.writeToFile(ss2.str());
}
Exemplo n.º 2
0
void mesher::generateMesh(	char ***A, sorter *sort, std::vector<particle *> &particles, FLOAT density, int mg, 
						  std::vector<double> &vertices, std::vector<double> &normals, std::vector<int> &faces ) {
	static float *dense_grid = new float[mg*mg*mg];
	
	// Create Density Field
	OPENMP_FOR FOR_EVERY_CELL(mg) {
		double h = 1.0/(double)(mg-1);
		double x = i*h;
		double y = j*h;
		double z = k*h;
		FLOAT p[3] = { x, y, z };
        double value = implicit::implicit_func( sort, p, density );	
        if( i==0 || i==mg-1 || j==0 || j==mg-1 || k==0 || k==mg-1 ) {
            value = fmax(value,0.01);
        }
		dense_grid[(mg*mg)*k+mg*j+i] = -value;	
	} END_FOR

	// Extract mesh
	vertices.clear();
	normals.clear();
	faces.clear();
	CIsoSurface<float> surface;
	surface.GenerateSurface(dense_grid, 0, mg-1, mg-1, mg-1, 1.0/(mg-1), 1.0/(mg-1), 1.0/(mg-1));
	
	for( int i=0; i<surface.m_nVertices; i++ ) {
		for( int n=0; n<3; n++ ) vertices.push_back(surface.m_ppt3dVertices[i][n]);
	}
	
	for (unsigned int i = 0; i < surface.m_nTriangles; i++) {
		unsigned int id0, id1, id2;
		id0 = surface.m_piTriangleIndices[i*3];
		id1 = surface.m_piTriangleIndices[i*3+1];
		id2 = surface.m_piTriangleIndices[i*3+2];
        
        FLOAT v0[3] = { vertices[3*id0+0], vertices[3*id0+1], vertices[3*id0+2] };
        FLOAT v1[3] = { vertices[3*id1+0], vertices[3*id1+1], vertices[3*id1+2] };
        FLOAT v2[3] = { vertices[3*id2+0], vertices[3*id2+1], vertices[3*id2+2] };
        
        double maxlen = 0.1;
        double maxlen2 = maxlen*maxlen;
        if( length2(v0,v1) < maxlen2 && length2(v1,v2) < maxlen2 && length2(v0,v2) < maxlen2 ) {
            faces.push_back(id0);
            faces.push_back(id1);
            faces.push_back(id2);
        }
	}
	
	for( int i=0; i<surface.m_nNormals; i++ ) {
		for( int n=0; n<3; n++ ) normals.push_back(surface.m_pvec3dNormals[i][n]);
	}
	
	// Smooth Vertices
	vector<vector<int> > connections = buildConnection( vertices, normals, faces );
	smoothMesh( sort, vertices, normals, connections, 2, 1, 1 );
	
	// Align Normal Directions
	// alignNormalToWalls( sort, vertices, normals );
}
void MarchingCubes::create_marching_cubes_from_array(const T* scalar_field, CVector mesh_dimensions, CVector box_length, double threshold, bool larger_than) {
    int nx = mesh_dimensions.x;
    int ny = mesh_dimensions.y;
    int nz = mesh_dimensions.z;
    initialize(nx*ny*nz);
    num_vertices = 0;
    num_triangles = 0;

    Random *rnd = new Random(-time(NULL), 0, 0);


    float r = 111/255.0;
    float g = 133/255.0;
    float b = 144/255.0;

//    float r = rnd->next_double();
//    float g = rnd->next_double();
//    float b = rnd->next_double();

    CIsoSurface<T> surf;
    surf.GenerateSurface(scalar_field,threshold,nx-1,ny-1,nz-1,box_length.x, box_length.y, box_length.z, larger_than);

    for(int triangle=0; triangle<surf.m_nTriangles; triangle++) {
        unsigned int p1_index = surf.m_piTriangleIndices[3*triangle+0];
        unsigned int p2_index = surf.m_piTriangleIndices[3*triangle+1];
        unsigned int p3_index = surf.m_piTriangleIndices[3*triangle+2];

        CVector p1(surf.m_ppt3dVertices[p1_index][0], surf.m_ppt3dVertices[p1_index][1], surf.m_ppt3dVertices[p1_index][2]);
        CVector p2(surf.m_ppt3dVertices[p2_index][0], surf.m_ppt3dVertices[p2_index][1], surf.m_ppt3dVertices[p2_index][2]);
        CVector p3(surf.m_ppt3dVertices[p3_index][0], surf.m_ppt3dVertices[p3_index][1], surf.m_ppt3dVertices[p3_index][2]);
        VECTOR3D &norm1 = surf.m_pvec3dNormals[p1_index];
        VECTOR3D &norm2 = surf.m_pvec3dNormals[p2_index];
        VECTOR3D &norm3 = surf.m_pvec3dNormals[p3_index];

//        CVector n1 = (p2 - p1).cross(p3 - p1).normalize();
//        CVector n2 = (p2 - p1).cross(p3 - p1).normalize();
//        CVector n3 = (p2 - p1).cross(p3 - p1).normalize();

        CVector n1(norm1[0], norm1[1], norm1[2]);
        CVector n2(norm2[0], norm2[1], norm2[2]);
        CVector n3(norm3[0], norm3[1], norm3[2]);

        CVector color(r,g,b);

//        n1 = n1*(-1);
//        n2 = n2*(-1);
//        n3 = n3*(-1);

        add_vertex(p1);
        add_normal(n1);
        add_color(color, 1.0);

        add_vertex(p2);
        add_normal(n2);
        add_color(color, 1.0);

        add_vertex(p3);
        add_normal(n3);
        add_color(color, 1.0);
        continue;
        n1 = n1*(-1);
        n2 = n2*(-1);
        n3 = n3*(-1);

        p1 = p1+n1*0.01;
        p2 = p2+n2*0.01;
        p3 = p3+n3*0.01;

        add_vertex(p1);
        add_normal(n1);
        add_color(color, 1.0);

        add_vertex(p2);
        add_normal(n2);
        add_color(color, 1.0);

        add_vertex(p3);
        add_normal(n3);
        add_color(color, 1.0);
    }

    cout << "Marching cubes created with " << surf.m_nTriangles << " triangles." << endl;
}