/** * 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; }
/** * BCMデータのロード * @param filename ファイルパス * @param generateBond 線primitiveとの接点作成 * @retval true 成功 * @retval false 失敗 */ bool PDBLoader::Load(const char* filename, bool generateBond){ Clear(); tinypdb::TinyPDB pdb(filename); if (pdb.Parse(/* isBondGenerated = */ generateBond)) { fprintf(stderr,"[PDBLoader] PDB parsing failed: %s \n", filename); return false; } m_atoms = pdb.GetAtoms(); // copy int numAtoms = static_cast<int>(pdb.GetAtoms().size()); { ball.Create(numAtoms); Vec3Buffer* pos = ball.Position(); FloatBuffer* mat = ball.Material(); FloatBuffer* radius = ball.Radius(); printf("[PDBLoader] # of atoms: %ld\n", numAtoms); float* pp = pos->GetBuffer(); for (size_t i = 0; i < numAtoms; i++) { pp[3*i+0] = pdb.GetAtoms()[i].GetX(); pp[3*i+1] = pdb.GetAtoms()[i].GetY(); pp[3*i+2] = pdb.GetAtoms()[i].GetZ(); } // @fixme float* rad = radius->GetBuffer(); for (int i = 0; i < numAtoms; ++i) { rad[i] = 0.25f; } // @todo memset(mat->GetBuffer(), 0, sizeof(float) * mat->GetNum()); } if (generateBond) { // We reprent Bond as line primitives. std::vector<float> bondLines; for (unsigned int i = 0; i < numAtoms; i++) { tinypdb::Atom& atom = pdb.GetAtoms()[i]; for (unsigned int j = 0; j < atom.GetBonds().size(); j++) { const tinypdb::Atom* dst = atom.GetBonds()[j]; if (dst->Visited()) { continue; } bondLines.push_back(atom.GetX()); bondLines.push_back(atom.GetY()); bondLines.push_back(atom.GetZ()); bondLines.push_back(dst->GetX()); bondLines.push_back(dst->GetY()); bondLines.push_back(dst->GetZ()); } atom.SetVisited(true); } size_t numBonds = bondLines.size() / 3 / 2; size_t numBondVertices = numBonds * 2; printf("[PDBLoader] # of bonds: %ld\n", numBonds); stick.Create(numBondVertices, /* index num = */0, /* use vrying radius */false); Vec3Buffer* pos = stick.Position(); FloatBuffer* mat = stick.Material(); //FloatBuffer* radius = stick.Radius(); //UintBuffer* index = stick.Index(); // not used. float* pp = pos->GetBuffer(); for (size_t i = 0; i < numBondVertices; i++) { pp[3*i+0] = bondLines[3*i+0]; pp[3*i+1] = bondLines[3*i+1]; pp[3*i+2] = bondLines[3*i+2]; } // Don't use varying radius. //float* rad = radius->GetBuffer(); //for (int i = 0; i < numBondVertices; ++i) { // rad[i] = 1.0f; //} // @todo memset(mat->GetBuffer(), 0, sizeof(float) * mat->GetNum()); } return true; }
/** * 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(); }