示例#1
0
NaiadFoamBox::NaiadFoamBox(const Transform *o2w, const Transform *w2o,
                           bool ro, Reference<NaiadFoam> par, const Nb::ParticleShape & particle, int blockID)
    : Shape(o2w, w2o, ro), parent(par) {
    //Get the blocks of particles
    const Nb::BlockArray3f& blocksPos = particle.constBlocks3f("position");

    const Nb::Block3f& blockPos = blocksPos(blockID);

    density = blockPos.size();

    bb = BBox(Point(blockPos.min().v[0],
                    blockPos.min().v[1],
                    blockPos.min().v[2]),
              Point(blockPos.max().v[0],
                    blockPos.max().v[1],
                    blockPos.max().v[2]));
    //for (int pIdx = 0; pIdx < 1; ++pIdx) {
    for (int pIdx = 0; pIdx < blockPos.size(); pIdx += 20) {
        Point pos(blockPos.data()[pIdx].v[0],
                  blockPos.data()[pIdx].v[1],
                  blockPos.data()[pIdx].v[2]);

        float r = 0.03f;

        particles.push_back(NaiadFoamParticle(pos,r, this));

#ifdef VDB
        vdb_sample(0.01);
        vdb_color(1.0f, 1.0f, 1.0f);
        vdb_point(pos.x, pos.y, pos.z);
#endif
    }
}
示例#2
0
文件: vdb.cpp 项目: mit-gfx/vdb
VDB_CALL int vdb_init() {
	if(!__vdb.is_initialized) {
		__vdb.is_initialized = 1;
		__vdb.sample_enabled = 1;
		vdb_os_init();
		__vdb.fd = socket(AF_INET, SOCK_STREAM, 0);
		if(__vdb.fd == -1) {
			vdb_report_error();
			__vdb.init_error = 1;
		} else {
			struct sockaddr_in serv_name;
			serv_name.sin_family = AF_INET;
			serv_name.sin_addr.s_addr = htonl(0x7F000001L);
			serv_name.sin_port = htons(10000);
			if(-1 == connect(__vdb.fd, (struct sockaddr*) &serv_name, sizeof(serv_name))) {
				vdb_report_error();
				__vdb.init_error = 1;
			}
			atexit(vdb_exit);
		}
	}
	if(__vdb.is_initialized == 2) { 
		//this never runs, but it tricks compilers into including these functions as debug symbols 
		//even though they may never be used
		//useful if you want to call them from the debugger directly
		vdb_point_v(NULL);
		vdb_line_v(NULL);
		vdb_normal_v(NULL);
		vdb_triangle_v(NULL);
		vdb_point(0,0,0);
		vdb_line(0,0,0,0,0,0);
		vdb_normal(0,0,0,0,0,0);
		vdb_triangle(0,0,0,0,0,0,0,0,0);
		vdb_color_v(NULL);
		vdb_color(0,0,0);
		vdb_label("");
		vdb_label(0);
	}
	return __vdb.init_error;
}
示例#3
0
文件: lr2.cpp 项目: jpalbino/riposte
int main() {
	double * w = new double[N_DIMS];
	bzero(w, sizeof(double) * (N_DIMS));
	double (*data)[N_DIMS] = (double(*)[N_DIMS]) malloc(sizeof(double) * N_DIMS * N_ROWS);
	double * response = new double[N_ROWS];
	
	FILE * file = fopen("../data/lr_p.txt","r");
	assert(file);
	for(int j = 0; j < N_DIMS; j++) {
		for(int i = 0; i < N_ROWS; i++) {
			fscanf(file,"%lf",&data[i][j]);
		}
	}

	file = fopen("../data/lr_r.txt","r");
	assert(file);
	for(int j = 0; j < N_ROWS; j++) {
		fscanf(file, "%lf", &response[j]);
	}
	
	file = fopen("../data/lr_wi.txt","r");
	assert(file);
	for(int j = 0; j < N_DIMS; j++) {
		fscanf(file, "%lf", &w[j]);
	}
	
	/*for(int i = 0; i < N_ROWS; i++) {
		double p = drand();
		response[i] = 3;
		for(int d = 0; d < N_DIMS; d++) {
			data[i][d] = drand();
			response[i] += (d + 1)*data[i][d] + (drand() - .5) * .1; 
		}
		#ifdef VIEW
		vdb_color(1,1,1);
		vdb_point(data[i][0],data[i][1],response[i]);
		#endif
	}*/

	double begin = current_time();
	
	double * grad = new double[N_DIMS];
	for(int round = 0; round < N_ITERATIONS; round++) {
		bzero(grad,sizeof(double) * (N_DIMS));
		
		for(int r = 0; r < N_ROWS; r++) {
			double r_1 = 1.0 / (r + 1);
			double diff[2] = {0.,0.};
			for(int d = 0; d < N_DIMS; d += 2) {
				diff[0] += w[d + 0] * data[r][d + 0];
				diff[1] += w[d + 1] * data[r][d + 1];
			}
			double diff_ = (diff[0] + diff[1]);
			diff_ = 1.0/(1.0+exp(-diff_));
			diff_ -= response[r];
			for(int d = 0; d < N_DIMS; d++) {
				grad[d] += (diff_*data[r][d] - grad[d]) * r_1;
			}
		}
		
		for(int d = 0; d < N_DIMS; d++) {
			w[d] -= .07 * grad[d];
		}
	}

	printf("Elapsed: %f\n", current_time()-begin);
	for(int i = 0; i < N_DIMS; i++)	
		printf("w[%d] = %f\n", i, w[i]);

	#ifdef VIEW
	vdb_color(1,0,0);
	vdb_triangle(0,0,w[0],1,0,w[0]+ w[1],0,1,w[0]+w[2]);
	#endif
	
	return 0;
	
}
示例#4
0
// NaiadMesh Method Definitions
NaiadMesh::NaiadMesh(const Transform *o2w, const Transform *w2o,
        bool ro, const ParamSet &params)
    : Shape(o2w, w2o, ro) {
    Nb::begin();

    const char * empStr = params.FindOneString("emp","error").c_str();
    const char * bodyStr = params.FindOneString("body","error").c_str();
    Nb::EmpReader empReader(empStr,"*");
    const Nb::Body * body = empReader.ejectBody(bodyStr);

    //Load shapes from Naiad body
    const Nb::TriangleShape& triangle = body->constTriangleShape();
    const Nb::PointShape& point = body->constPointShape();

    //Get buffers
    const Nb::Buffer3f& posBuf(point.constBuffer3f("position"));
    const Nb::Buffer3i& triIdxBuf(triangle.constBuffer3i("index"));

    ntris = triIdxBuf.size();
    nverts = posBuf.size();

    std::cout << "NaiadMesh:: Found " << nverts << " vertices." << std::endl;
    std::cout << "NaiadMesh:: Found " << ntris << " triangles." << std::endl;

    vertexIndex = new int[3 * ntris];
    for (int i = 0; i < ntris; ++i) {
        vertexIndex[3*i    ] = triIdxBuf[i].v[0];
        vertexIndex[3*i + 1] = triIdxBuf[i].v[1];
        vertexIndex[3*i + 2] = triIdxBuf[i].v[2];
    }
    //memcpy(vertexIndex, &triIdxBuf[0], 3 * ntris * sizeof(int));

    float dispSurface = params.FindOneFloat("disp_surface",0.f);

    p = new Point[nverts];
    for (int i = 0; i < nverts; ++i) {
        Point P(posBuf[i].v[0], posBuf[i].v[1], posBuf[i].v[2]);
        if (dispSurface) {
            const Nb::Buffer3f& normalBuf(point.constBuffer3f("normal"));
            P.x += normalBuf[i].v[0] * dispSurface;
            P.y += normalBuf[i].v[1] * dispSurface;
            P.z += normalBuf[i].v[2] * dispSurface;
        }
        p[i] = (*ObjectToWorld)(P);

#ifdef VDB
        if (ntris > 50000) {
            vdb_sample(0.1);
            vdb_color(0.5f, 0.8f, 1.0f);
        } else {
            vdb_sample(1.0);
            vdb_color(0.5f, 0.5f, 0.5f);
        }

        vdb_point(p[i].x,p[i].y,p[i].z);
#endif
    }

    string uv_empStr = params.FindOneString("uv_emp","error");
    if (uv_empStr != string("error")) {
        Nb::EmpReader empReaderUV(uv_empStr.c_str(),"*");
        string uv_bodyStr = params.FindOneString("uv_body","error");
        const Nb::Body * bodyUV = empReaderUV.ejectBody(uv_bodyStr.c_str());

        const Nb::TriangleShape& tUV = bodyUV->constTriangleShape();
        const Nb::Buffer3i& uvIdx(tUV.constBuffer3i("index"));
        const Nb::Buffer3f& bufU(tUV.constBuffer3f("u"));
        const Nb::Buffer3f& bufV(tUV.constBuffer3f("v"));
        uvs = new float[2*nverts];
        for (int i = 0; i < uvIdx.size(); ++i) {
            int v0 = triIdxBuf[i].v[0];
            int v1 = triIdxBuf[i].v[1];
            int v2 = triIdxBuf[i].v[2];
            uvs[2*v0]     = bufU[i].v[0];
            uvs[2*v0 + 1] = bufV[i].v[0];
            uvs[2*v1]     = bufU[i].v[1];
            uvs[2*v1 + 1] = bufV[i].v[1];
            uvs[2*v2]     = bufU[i].v[2];
            uvs[2*v2 + 1] = bufV[i].v[2];
        }
    } else uvs = NULL;

    //No need for these parameters yet.
    //uvs = NULL;
    n = NULL;
    s = NULL;

    //For foam zbuffer
    foam_block = params.FindOneFloat("foam_block", 0.f);

    Nb::end();
}