Exemple #1
0
/**
 * STLデータのロード
 * @param filename ファイルパス
 * @retval true 成功
 * @retval false 失敗
 */
bool STLLoader::Load(const char* filename){
	Clear();
	SimpleSTLB obj;
	bool r = obj.Load(filename);

    mesh.Create(obj.GetVertexNum(), obj.GetIndexNum());
    Vec3Buffer* pos = mesh.Position();
    Vec3Buffer* normal = mesh.Normal();
    FloatBuffer* mat = mesh.Material();
    UintBuffer* index = mesh.Index();
    Vec2Buffer* texcoord = mesh.Texcoord();

	memcpy(pos->GetBuffer(), obj.GetPositionBuffer(), sizeof(float) * 3 * pos->GetNum());
	normal->Create(obj.GetVertexNum());
	memcpy(normal->GetBuffer(), obj.GetNormalBuffer(), sizeof(float) * 3 * normal->GetNum());
	mat->Create(obj.GetVertexNum());
	memset(mat->GetBuffer(), 0, sizeof(float) * mat->GetNum());
	index->Create(obj.GetIndexNum());
	memcpy(index->GetBuffer(), obj.GetIndex(), sizeof(unsigned int) * index->GetNum());

    
	printf("%d\n", pos->GetNum()     );
	printf("%d\n", normal->GetNum()  );
	printf("%d\n", mat->GetNum()     );
	printf("%d\n", index->GetNum()   );
	return r;
}
/**
 * VolumeData XYZベクトルデータ生成
 * @param  volume  X軸分割数
 * @retval 0 Volumeが指定されていない
 * @retval >0 生成されたベクトルデータの頂点数
 */
int VolumeToVector::Create(BufferVolumeData *volume) {
    if (!volume) {
        return 0;
    }

    //todo custom
    const double  bmin[3]      = {-1, -1, -1};
    const double  bmax[3]      = { 1,  1,  1};
    const int     width        = volume->Width();
    const int     height       = volume->Height();
    const int     depth        = volume->Depth();
    const int     volume_num   = (width * height * depth);
    const int     indexnum     = 0;//obj.GetIndexNum()
    const float   offset_inc   = 1.0;
    const double  fwidth       = static_cast<double>(width );
    const double  fheight      = static_cast<double>(height);
    const double  fdepth       = static_cast<double>(depth );
    double        offset_incX  = m_pitchX;
    double        offset_incY  = m_pitchY;
    double        offset_incZ  = m_pitchZ;
    if(m_usePitchInt) {
        printf("Use PIch Int\n");
        offset_incX = fwidth  / static_cast<double>(m_pitchIntX);
        offset_incY = fheight / static_cast<double>(m_pitchIntY);
        offset_incZ = fdepth  / static_cast<double>(m_pitchIntZ);
    }
    printf("VolumeNum  = %d(%d %d %d)\n", volume_num, width, height, depth);
    printf("OFFSET_INC = %f %f %f\n", offset_incX, offset_incY, offset_incZ);

    //Create Line Data
    Vec3Buffer*     volumedata = reinterpret_cast<Vec3Buffer*>(volume->Buffer());
    VX::Math::vec3* volbuf     = reinterpret_cast<VX::Math::vec3*>(volumedata->GetBuffer());

    std::vector<VX::Math::vec3> vposbuf;  //need reserve?
    std::vector<VX::Math::vec3> vdirbuf;  //need reserve?

    float offsetZ    = static_cast<float>(-depth  / 2) + 0.5;
    for(double k = 0; k < fdepth; k += offset_incZ)
    {
        float offsetY    = static_cast<float>(-height / 2) - 0.5; //yUP
        for(double j = 0; j < fheight; j += offset_incY)
        {
            float offsetX    = static_cast<float>(-width / 2) + 0.5;
            for(double i = 0; i < fwidth; i += offset_incX)
            {
                int buf_offset  = static_cast<int>(i);                  //X
                buf_offset     += static_cast<int>(j) * width;          //Y
                buf_offset     += static_cast<int>(k) * width * height; //Z
                if(volumedata->GetNum() <= buf_offset) {
                    printf("ERROR : BOF .\n");
                }

                VX::Math::vec3 v0 = VX::Math::vec3(offsetX, offsetY, offsetZ);
                VX::Math::vec3 v1 = volbuf[buf_offset];

				// Skip zero value voxel.
				if (fabsf(v1[0]) < 1.0e-20f && fabsf(v1[1]) < 1.0e-20f && fabsf(v1[2]) < 1.0e-20f) {
                	offsetX += offset_incX;
					continue;
				}

                v1 = normalize(v1);


                vposbuf.push_back(v0);
                vdirbuf.push_back(v1);

                offsetX += offset_incX;
            }
            offsetY += offset_incY;
        }
        offsetZ += offset_incZ;
    }
    printf("Builder : Line Num = %zu\n", vposbuf.size());
    m_vector->Create(vposbuf.size());
    Vec3Buffer*     pos     = reinterpret_cast<Vec3Buffer*>(m_vector->Position());
    VX::Math::vec3* posbuf  = reinterpret_cast<VX::Math::vec3*>(pos->GetBuffer());
    Vec3Buffer*     dir     = reinterpret_cast<Vec3Buffer*>(m_vector->Direction());
    VX::Math::vec3* dirbuf  = reinterpret_cast<VX::Math::vec3*>(dir->GetBuffer());

    //setup vertex
    memcpy(posbuf, &vposbuf[0], sizeof(VX::Math::vec3) * vposbuf.size());
    memcpy(dirbuf, &vdirbuf[0], sizeof(VX::Math::vec3) * vdirbuf.size());

    return vposbuf.size();
}