void GLGPUDataset::SerializeDataInfoToString(std::string& buf) const { #if WITH_PROTOBUF PBDataInfo pb; pb.set_model(PBDataInfo::GLGPU); pb.set_name(_data_name); if (Lengths()[0]>0) { pb.set_ox(Origins()[0]); pb.set_oy(Origins()[1]); pb.set_oz(Origins()[2]); pb.set_lx(Lengths()[0]); pb.set_ly(Lengths()[1]); pb.set_lz(Lengths()[2]); } pb.set_bx(B()[0]); pb.set_by(B()[1]); pb.set_bz(B()[2]); pb.set_kex(Kex()); pb.set_dx(dims()[0]); pb.set_dy(dims()[1]); pb.set_dz(dims()[2]); pb.set_pbc_x(pbc()[0]); pb.set_pbc_y(pbc()[0]); pb.set_pbc_z(pbc()[0]); pb.SerializeToString(&buf); #endif }
////////////////////////////////////////////////////////////////////////// //! build a box ////////////////////////////////////////////////////////////////////////// void buildBox(void) { Vec3f Lengths((Real32)(rand()%2)+1.0, (Real32)(rand()%2)+1.0, (Real32)(rand()%2)+1.0); Matrix m; //create OpenSG mesh GeometryRefPtr box; NodeRefPtr boxNode = makeBox(Lengths.x(), Lengths.y(), Lengths.z(), 1, 1, 1); box = dynamic_cast<Geometry*>(boxNode->getCore()); SimpleMaterialRefPtr box_mat = SimpleMaterial::create(); box_mat->setAmbient(Color3f(0.0,0.0,0.0)); box_mat->setDiffuse(Color3f(0.0,1.0 ,0.0)); box->setMaterial(box_mat); TransformRefPtr boxTrans; NodeRefPtr boxTransNode = makeCoredNode<Transform>(&boxTrans); m.setIdentity(); Real32 randX = (Real32)(rand()%10)-5.0; Real32 randY = (Real32)(rand()%10)-5.0; m.setTranslate(randX, randY, 10.0); boxTrans->setMatrix(m); //create ODE data PhysicsBodyRefPtr boxBody = PhysicsBody::create(physicsWorld); boxBody->setPosition(Vec3f(randX, randY, 10.0)); boxBody->setBoxMass(1.0, Lengths.x(), Lengths.y(), Lengths.z()); //std::cout << "mass: " << boxBody->getMass() << std::endl //<< "massCenterOfGravity: " << boxBody->getMassCenterOfGravity().x() << ", " << boxBody->getMassCenterOfGravity().y() << ", " << boxBody->getMassCenterOfGravity().z() << std::endl //<< "massInertiaTensor: " << std::endl //<< boxBody->getMassInertiaTensor()[0][0] << " "<< boxBody->getMassInertiaTensor()[0][1] << " "<< boxBody->getMassInertiaTensor()[0][2] << " " << boxBody->getMassInertiaTensor()[0][3] << std::endl //<< boxBody->getMassInertiaTensor()[1][0] << " "<< boxBody->getMassInertiaTensor()[1][1] << " "<< boxBody->getMassInertiaTensor()[1][2] << " " << boxBody->getMassInertiaTensor()[1][3] << std::endl //<< boxBody->getMassInertiaTensor()[2][0] << " "<< boxBody->getMassInertiaTensor()[2][1] << " "<< boxBody->getMassInertiaTensor()[2][2] << " " << boxBody->getMassInertiaTensor()[2][3] << std::endl //<< boxBody->getMassInertiaTensor()[3][0] << " "<< boxBody->getMassInertiaTensor()[3][1] << " "<< boxBody->getMassInertiaTensor()[3][2] << " " << boxBody->getMassInertiaTensor()[3][3] << std::endl //<< std::endl; PhysicsBoxGeomRefPtr boxGeom = PhysicsBoxGeom::create(); boxGeom->setBody(boxBody); boxGeom->setSpace(physicsSpace); boxGeom->setLengths(Lengths); //add attachments boxNode->addAttachment(boxGeom); boxTransNode->addAttachment(boxBody); boxTransNode->addChild(boxNode); //add to SceneGraph spaceGroupNode->addChild(boxTransNode); commitChanges(); }
////////////////////////////////////////////////////////////////////////// //! build a box ////////////////////////////////////////////////////////////////////////// void buildBox(void) { Vec3f Lengths(frand()*2.0+0.5, frand()*2.0+0.5, frand()*2.0+0.5); Matrix m; //create OpenSG mesh GeometryPtr box; NodePtr boxNode = makeBox(Lengths.x(), Lengths.y(), Lengths.z(), 1, 1, 1); box = GeometryPtr::dcast(boxNode->getCore()); SimpleMaterialPtr box_mat = SimpleMaterial::create(); beginEditCP(box_mat); box_mat->setAmbient(Color3f(0.0,0.0,0.0)); box_mat->setDiffuse(Color3f(0.0,1.0 ,0.0)); endEditCP(box_mat); beginEditCP(box, Geometry::MaterialFieldMask); box->setMaterial(box_mat); endEditCP(box, Geometry::MaterialFieldMask); TransformPtr boxTrans; NodePtr boxTransNode = makeCoredNode<Transform>(&boxTrans); m.setIdentity(); Real32 randX = frand()*10.0-5.0; Real32 randY = frand()*10.0-5.0; m.setTranslate(randX, randY, 10.0); beginEditCP(boxTrans, Transform::MatrixFieldMask); boxTrans->setMatrix(m); endEditCP(boxTrans, Transform::MatrixFieldMask); //create ODE data PhysicsBodyPtr boxBody = PhysicsBody::create(physicsWorld); beginEditCP(boxBody, PhysicsBody::PositionFieldMask); boxBody->setPosition(Vec3f(randX, randY, 10.0)); endEditCP(boxBody, PhysicsBody::PositionFieldMask); boxBody->setBoxMass(1.0, Lengths.x(), Lengths.y(), Lengths.z()); PhysicsBoxGeomPtr boxGeom = PhysicsBoxGeom::create(); beginEditCP(boxGeom, PhysicsBoxGeom::BodyFieldMask | PhysicsBoxGeom::SpaceFieldMask | PhysicsBoxGeom::LengthsFieldMask | PhysicsBoxGeom::CategoryBitsFieldMask); boxGeom->setBody(boxBody); boxGeom->setSpace(physicsSpace); boxGeom->setLengths(Lengths); boxGeom->setCategoryBits(BoxCategory); endEditCP(boxGeom, PhysicsBoxGeom::BodyFieldMask | PhysicsBoxGeom::SpaceFieldMask | PhysicsBoxGeom::LengthsFieldMask | PhysicsBoxGeom::CategoryBitsFieldMask); //add attachments beginEditCP(boxNode, Node::AttachmentsFieldMask); boxNode->addAttachment(boxGeom); endEditCP(boxNode, Node::AttachmentsFieldMask); beginEditCP(boxTransNode, Node::AttachmentsFieldMask | Node::ChildrenFieldMask); boxTransNode->addAttachment(boxBody); boxTransNode->addChild(boxNode); endEditCP(boxTransNode, Node::AttachmentsFieldMask | Node::ChildrenFieldMask); //add to SceneGraph beginEditCP(spaceGroupNode, Node::ChildrenFieldMask); spaceGroupNode->addChild(boxTransNode); endEditCP(spaceGroupNode, Node::ChildrenFieldMask); }
float GLGPUDataset::QP(const float X0_[], const float X1_[], int slot) const { float X0[3], X1[3]; float N[3]; for (int i=0; i<3; i++) { X0[i] = (X0_[i] - Origins()[i]) / CellLengths()[i]; X1[i] = (X1_[i] - Origins()[i]) / CellLengths()[i]; N[i] = dims()[i]; } if (B(slot)[1]>0 && fabs(X1[0]-X0[0])>N[0]/2) { // TODO assert(false); return 0.0; } else if (fabs(X1[1]-X0[1])>N[1]/2) { // pbc j float dj = X1[1] - X0[1]; if (dj > N[1]/2) dj = dj - N[1]; else if (dj < -N[1]/2) dj = dj + N[1]; float dist = fabs(dj); float dist1 = fabs(fmod1(X0[1] + N[1]/2, N[1]) - N[1]); float f = dist1/dist; // pbc k float dk = X1[2] - X0[2]; if (dk > N[2]/2) dk = dk - N[2]; else if (dk < -N[2]/2) dk = dk + N[2]; float k = fmod1(X0[2] + f*dk, N[2]); // pbc i float di = X1[0] - X0[0]; if (di > N[0]/2) di = di - N[0]; else if (di < -N[0]/2) di = di + N[0]; float i = fmod1(X0[0] + f*dk, N[0]); float sign = dj>0 ? 1 : -1; float qp = sign * (k*CellLengths()[2]*B(slot)[0]*Lengths()[1] - i*CellLengths()[0]*B(slot)[2]*Lengths()[1]); return qp; } return 0.0; }
////////////////////////////////////////////////////////////////////////// //! build a box ////////////////////////////////////////////////////////////////////////// PhysicsBodyUnrecPtr buildBox(void) { Vec3f Lengths((Real32)(rand()%2)+1.0, (Real32)(rand()%2)+1.0, (Real32)(rand()%2)+1.0); Matrix m; //create OpenSG mesh GeometryUnrecPtr box; NodeUnrecPtr boxNode = makeBox(Lengths.x(), Lengths.y(), Lengths.z(), 1, 1, 1); box = dynamic_cast<Geometry*>(boxNode->getCore()); SimpleMaterialUnrecPtr box_mat = SimpleMaterial::create(); box_mat->setAmbient(Color3f(0.0,0.0,0.0)); box_mat->setDiffuse(Color3f(0.0,1.0 ,0.0)); box->setMaterial(box_mat); TransformUnrecPtr boxTrans; NodeUnrecPtr boxTransNode = makeCoredNode<Transform>(&boxTrans); m.setIdentity(); Real32 randX = (Real32)(rand()%10)-5.0; Real32 randY = (Real32)(rand()%10)-5.0; m.setTranslate(randX, randY, 10.0); boxTrans->setMatrix(m); //create ODE data PhysicsBodyUnrecPtr boxBody = PhysicsBody::create(physicsWorld); boxBody->setPosition(Vec3f(randX, randY, 10.0)); boxBody->setBoxMass(1.0, Lengths.x(), Lengths.y(), Lengths.z()); PhysicsBoxGeomUnrecPtr boxGeom = PhysicsBoxGeom::create(); boxGeom->setBody(boxBody); boxGeom->setSpace(physicsSpace); boxGeom->setLengths(Lengths); //add attachments boxNode->addAttachment(boxGeom); boxTransNode->addAttachment(boxBody); boxTransNode->addChild(boxNode); //add to SceneGraph spaceGroupNode->addChild(boxTransNode); commitChanges(); return boxBody; }
float GLGPUDataset::QP(const float X0[], const float X1[]) const { const float *L = Lengths(), *O = Origins(); float d[3] = {X1[0] - X0[0], X1[1] - X0[1], X1[2] - X0[2]}; int p[3] = {0}; // 0: not crossed; 1: positive; -1: negative for (int i=0; i<3; i++) { d[i] = X1[i] - X0[i]; if (d[i]>L[i]/2) {d[i] -= L[i]; p[i] = 1;} else if (d[i]<-L[i]/2) {d[i] += L[i]; p[i] = -1;} } const float X[3] = {X0[0] - O[0], X0[1] - O[1], X0[2] - O[2]}; if (By()>0 && p[0]!=0) { // By>0 return p[0] * L[0] * (Bz()*X[1] - By()*X[2]); } else if (p[1]!=0) { return p[1] * L[1] * (Bx()*X[2] - Bz()*X[0]); } else return 0.0; }