//=========================================== // Polygon::restructure // // Test for convexity, and subdivide if necessary //=========================================== void Polygon::restructure() { if (m_children.size() == 0) { if (!isConvex()) subdivide(); // We've found a non-convex leaf } else { for (uint_t i = 0; i < m_children.size(); ++i) m_children[i].restructure(); if (isConvex()) absorbChildren(); } }
void Collider::_copyFrom(const ComponentBase *model) { auto m = (Collider*)model; if (m->getColliderType() == Physics::ColliderType::Mesh) { init(m->getColliderType(), m->getMesh()); } else { init(m->getColliderType(), ""); } #ifdef EDITOR_ENABLED editorUpdate(); if (editorStruct) { editorStruct->copyDatas(m); } Link *link = entity.getLinkPtr(); auto p = scaleFromMat4(link->getGlobalTransform()); //scale(p / getSize()); #else setMaterial(m->getMaterial()->getName()); setAsTrigger(m->isATrigger()); setFilterGroup(m->getFilterGroup()); setCenter(m->getCenter()); setHeight(m->getHeight()); setRadius(m->getRadius()); setSize(m->getSize()); setMesh(m->getMesh()); if (m->isConvex()) setAsConvex(); else if (m->isConcave()) setAsConcave(); #endif }
// Inherited Methods void PhysXMeshCollider::scale(const glm::vec3 &scaling) { for (physx::PxShape *shape : getShapes()) { const physx::PxVec3 realScaling(scaling.x, scaling.y, scaling.z); physx::PxTransform localPose = shape->getLocalPose(); const physx::PxMat33 scalingMatrix = physx::PxMat33::createDiagonal(realScaling) * physx::PxMat33(localPose.q); if (isConvex()) { physx::PxConvexMeshGeometry &convexMesh = shape->getGeometry().convexMesh(); convexMesh.scale.scale.x *= scalingMatrix.column0.magnitude(); convexMesh.scale.scale.y *= scalingMatrix.column1.magnitude(); convexMesh.scale.scale.z *= scalingMatrix.column2.magnitude(); shape->setGeometry(convexMesh); } else { physx::PxTriangleMeshGeometry &triangleMesh = shape->getGeometry().triangleMesh(); triangleMesh.scale.scale.x *= scalingMatrix.column0.magnitude(); triangleMesh.scale.scale.y *= scalingMatrix.column1.magnitude(); triangleMesh.scale.scale.z *= scalingMatrix.column2.magnitude(); shape->setGeometry(triangleMesh); } localPose.p = localPose.p.multiply(realScaling); shape->setLocalPose(localPose); } }
void Planar::reloadFile(string configFile) throw(FileNotFound) { ifstream fin(configFile); if (!fin.is_open()) { throw FileNotFound(configFile + " is missing and cannot be loaded."); } //Determine the number of vertices fin >> total; //Gather each of the vertices for(int i = 0; i < total; ++i) { glm::vec3 pt; fin >> pt.x; fin >> pt.y; fin >> pt.z; vertices.push_back(pt); } //Check for convexity convex = isConvex(); //Check for planarity planar = isPlanar(); //Build the geometry buildGeometry(); //Put all of the vertices into an array mesh.dump(); //Allocate the buffer space Geometry::buildBuffer(mesh.total, mesh.colors, mesh.normals, mesh.points); }
bool QCanvas::razor_Cyrus_Beck(bool time_sleep) { if(!isConvex()) return false; for(size_t i = 0; i< obj_lines.size(); ++i) razor_CB(obj_lines.value(i).S,obj_lines.value(i).F,time_sleep); return true; }
bool QCanvas::razor_Cazerlend_Hodzhmen(bool time_sleep) { if(obj_lines.isEmpty()) return false; ssize_t direction_value; if(!(direction_value = isConvex())) return false; else if(direction_value < 0) { size_t size = obj_Polyhedrons.size(); for(size_t i = 0; i< size/2. ; ++i) { line_t line_tmp_1 = obj_Polyhedrons.value(i); line_t line_tmp_2 = obj_Polyhedrons.value(size-1-i); std::swap(line_tmp_1.S,line_tmp_1.F); std::swap(line_tmp_2.S,line_tmp_2.F); obj_Polyhedrons.replace(i,line_tmp_2); obj_Polyhedrons.replace(size-1-i,line_tmp_1); } } QVector< QVector<QPointF> > arr_poli; QPointF start = obj_lines.first().S; QVector<QPointF> tmp; tmp.push_back(start); for(size_t i = 0; i < obj_lines.size(); ++i) if(obj_lines.value(i).F != tmp.first()) tmp.push_back(obj_lines.value(i).F); else { arr_poli.push_back(tmp); tmp.clear(); if(i+1 != obj_lines.size()) tmp.push_back(obj_lines.value(i+1).S); else break; } for(size_t i = 0; i< arr_poli.size(); ++i) { QVector<QPointF> tmp = arr_poli.value(i); razor_CH(obj_Polyhedrons,tmp,false); print_poly(tmp); } }
void Collider::setAsConcave(void) { assert(collider != nullptr && "Invalid Collider"); switch (getColliderType()) { case Physics::ColliderType::Mesh: if (isConvex()) { collider->getAsMeshColliderInterface()->setAsConcave(); scale(entity->getLink().getScale()); } break; default: break; } }
void ConvexPolygon::copyPoint(const Point * argPointList) // Algorithme // a l'initialisation, copie le tableau de point // en parametre et verifie si le polygone est convexe { pointList = new Point[size]; for(int i(0); i<size; i++) { pointList[i] = argPointList[i]; } if(!isConvex()) { string e = "ERR"; e += "\r\n"; e += "#Non convex Polygon"; throw e; } }
void tst_QTessellator::testConvex() { int failures = 0; for (int i = 4; i < 10; ++i) { QVector<QPointF> vec(i); int k = 5000; while (k--) { fillRandomVec(vec); if (!isConvex(vec)) continue; if (!test(vec.data(), vec.size(), false, test_tessellate_polygon_convex)) { simplifyTestFailure(vec, false); ++failures; } if (!test(vec.data(), vec.size(), true, test_tessellate_polygon_convex)) { simplifyTestFailure(vec, true); ++failures; } } } QVERIFY(failures == 0); }
Mesh* Extrude::createMesh(){ cout<<isConvex()<<" - True = "<<true<<endl; Mesh* m = new Mesh(isConvex()); //empty mesh vector<vec3> polygon_top(polygon); //a copy of polygon //inserts polygon to mesh //so for polygon the index is k for(int j = 0; j< sides; j++){ m->addVertex(polygon[j]); } //add y coordinate to polygon_top and inserts it to mesh //so for top polygon the index is k+sides for(int i = 0; i<sides; i++){ polygon_top[i][1] = distance; m->addVertex(polygon_top[i]); } for(int k = 0; k<sides; k++){ if(k<(sides-1)){ //finds normal for face that is going to be inserted to the mesh vec4 vert1(polygon[k][0],polygon[k][1],polygon[k][2],1); vec4 vert2(polygon[k+1][0],polygon[k+1][1],polygon[k+1][2],1); vec4 vert3(polygon_top[k+1][0],polygon_top[k+1][1],polygon_top[k+1][2],1); vec4 n = getNormal(vert1,vert2,vert3); vec3 normal(n[0],n[1],n[2]); /*std::cout<<"--------\nNormal x = "<<normal[0]<<std::endl; std::cout<<"Normal y = "<<normal[1]<<std::endl; std::cout<<"Normal z = "<<normal[2]<<std::endl;*/ m->addNormal(normal); //creates the faces, normal index is = k, always have 4 vertices for a face vec2 f1((double)k,(double)k); vec2 f2((double)k+1,(double)k); vec2 f3((double)sides+k+1,(double)k); vec2 f4((double)sides+k,(double)k); std::vector<vec2> face; face.push_back(f1); face.push_back(f2); face.push_back(f3); face.push_back(f4); m->addFace(face); } //case: need to do last one too if(k == (sides-1)){ vec4 vert1L(polygon[k][0],polygon[k][1],polygon[k][2],1); vec4 vert2L(polygon[0][0],polygon[0][1],polygon[0][2],1); vec4 vert3L(polygon_top[0][0],polygon_top[0][1],polygon_top[0][2],1); vec4 nL = getNormal(vert1L,vert2L,vert3L); vec3 normalL(nL[0],nL[1],nL[2]); m->addNormal(normalL); //creates the faces, normal index is = k, always have 4 vertices for a face vec2 f1L((double)k,(double)k); vec2 f2L((double)0,(double)k); vec2 f3L((double)sides,(double)k); vec2 f4L((double)sides+k,(double)k); std::vector<vec2> faceL; faceL.push_back(f1L); faceL.push_back(f2L); faceL.push_back(f3L); faceL.push_back(f4L); m->addFace(faceL); } } //check if convex to cap or not if(isConvex()){ std::vector<vec2> cap; std::vector<vec2> cap_top; //add the two normals at side and side+1 vec4 vert1(polygon[0][0],polygon[0][1],polygon[0][2],1); vec4 vert2(polygon[1][0],polygon[1][1],polygon[1][2],1); vec4 vert3(polygon[2][0],polygon[2][1],polygon[2][2],1); vec4 n = getNormal(vert1,vert2,vert3); n = n*(-1); vec3 normal(n[0],n[1],n[2]); m->addNormal(normal); vec4 vert1_top(polygon_top[0][0],polygon_top[0][1],polygon_top[0][2],1); vec4 vert2_top(polygon_top[1][0],polygon_top[1][1],polygon_top[1][2],1); vec4 vert3_top(polygon_top[2][0],polygon_top[2][1],polygon_top[2][2],1); vec4 n_top = getNormal(vert1_top,vert2_top,vert3_top); vec3 normal_top(n_top[0],n_top[1],n_top[2]); m->addNormal(normal_top); //adds the faces, vertices for bottom cap = polygon(i) //for top cap = polygon_top(i+side) for(int i = 0; i<sides; i++){ vec2 f((double)i,(double)sides); cap.push_back(f); vec2 f_top((double)i+sides,(double)sides+1); cap_top.push_back(f_top); } m->addFace(cap); m->addFace(cap_top); } return m; }
void Mesh::generateMesh() { if (mesh_type == EXTRUSION) { if (isConvex()) { for (int ii = 1; ii < num_of_point - 1; ii++) { // Bottom endcap Triangle *tri_base = new Triangle(); tri_base->points[0] = 0; tri_base->points[1] = ii; tri_base->points[2] = ii + 1; tri_base->normal = (points[tri_base->points[1]] - points[tri_base->points[0]])^ (points[tri_base->points[2]] - points[tri_base->points[1]]).normalize(); triangles.push_back(tri_base); // Top endcap Triangle *tri_top = new Triangle(); tri_top->points[0] = num_of_point; tri_top->points[1] = num_of_point + ii + 1; tri_top->points[2] = num_of_point + ii; tri_top->normal = (points[tri_top->points[1]] - points[tri_top->points[0]])^ (points[tri_top->points[2]] - points[tri_top->points[1]]).normalize(); triangles.push_back(tri_top); } } for(int ii = 0; ii < num_of_point; ii++) { // Sides Triangle *tri_side0 = new Triangle(); tri_side0->points[0] = ii; tri_side0->points[1] = ii + num_of_point; tri_side0->points[2] = (ii + 1)%num_of_point + num_of_point; tri_side0->normal = (points[tri_side0->points[1]] - points[tri_side0->points[0]])^ (points[tri_side0->points[2]] - points[tri_side0->points[1]]).normalize(); triangles.push_back(tri_side0); Triangle *tri_side1 = new Triangle(); tri_side1->points[0] = ii; tri_side1->points[1] = (ii + 1)%num_of_point + num_of_point; tri_side1->points[2] = (ii + 1)%num_of_point; tri_side1->normal = (points[tri_side0->points[1]] - points[tri_side0->points[0]])^ (points[tri_side0->points[2]] - points[tri_side0->points[1]]).normalize(); triangles.push_back(tri_side1); } } else if (mesh_type == SURFREV) { if (points[points.size() - 1][0] != 0.0) { for(int ii = num_of_point - 1 + num_of_point*2; ii < points.size(); ii += num_of_point) { // Top endcap Triangle *tri_top = new Triangle(); tri_top->points[0] = num_of_point - 1; tri_top->points[1] = ii; tri_top->points[2] = ii - num_of_point; tri_top->normal = (points[tri_top->points[1]] - points[tri_top->points[0]])^ (points[tri_top->points[2]] - points[tri_top->points[1]]).normalize(); triangles.push_back(tri_top); } } if (points[0][0] != 0.0) { for (int ii = num_of_point*2; ii < points.size(); ii += num_of_point) { // Bottom endcap Triangle *tri_base = new Triangle(); tri_base->points[0] = 0; tri_base->points[1] = ii - num_of_point; tri_base->points[2] = ii; tri_base->normal = (points[tri_base->points[1]] - points[tri_base->points[0]])^ (points[tri_base->points[2]] - points[tri_base->points[1]]).normalize(); triangles.push_back(tri_base); } } for(int ii = 0; ii < radial_slice; ii++) { for(int jj = 0; jj < num_of_point - 1; jj++) { // Sides Triangle *tri_side0 = new Triangle(); tri_side0->points[0] = jj + ii*num_of_point; tri_side0->points[1] = jj + ((ii + 1)%radial_slice)*num_of_point + 1; tri_side0->points[2] = jj + ((ii + 1)%radial_slice)*num_of_point; tri_side0->normal = (points[tri_side0->points[1]] - points[tri_side0->points[0]])^ (points[tri_side0->points[2]] - points[tri_side0->points[1]]).normalize(); triangles.push_back(tri_side0); Triangle *tri_side1 = new Triangle(); tri_side1->points[0] = jj + ii*num_of_point; tri_side1->points[1] = jj + ii*num_of_point + 1; tri_side1->points[2] = jj + ((ii + 1)%radial_slice)*num_of_point + 1; tri_side1->normal = (points[tri_side0->points[1]] - points[tri_side0->points[0]])^ (points[tri_side0->points[2]] - points[tri_side0->points[1]]).normalize(); triangles.push_back(tri_side1); } } } }
void PhysXMeshCollider::updateShape(void) { static_cast<PhysXCollider *>(this)->updateShape(static_cast<PhysXWorld *>(getWorld())->getCollisionShapes(getMesh(), isConvex())); for (physx::PxShape *shape : getShapes()) { shape->setMaterials(&static_cast<PhysXMaterial *>(getMaterial())->material, 1); } }
// Constructors PhysXMeshCollider::PhysXMeshCollider(WorldInterface *world, const std::string &mesh, Private::GenericData *data) : ColliderInterface(world, data), MeshColliderInterface(world, mesh, IsConvexByDefault(), data), PhysXCollider(world, data, static_cast<PhysXWorld *>(world)->getCollisionShapes(mesh, isConvex())) { for (physx::PxShape *shape : getShapes()) { shape->setMaterials(&static_cast<PhysXMaterial *>(getMaterial())->material, 1); } }
CRCode *CRChainCodeBlob::code() { CRCode *code = NULL; CRDiffVectorInfo seed[4]; CRHomogeneousVec3 *line1 = NULL, *line2 = NULL, *line3 = NULL, *line4 = NULL; CRHomogeneousVec3 *firstCorner = NULL, *secondCorner = NULL, *thirdCorner = NULL, *fourthCorner = NULL; int step = 2; int diffListSize = (int)(this->elements->size() - step); CRDiffVectorInfo *diffList = (CRDiffVectorInfo*)malloc(sizeof(CRDiffVectorInfo) * diffListSize); std::list<CRChainCodeElement*>::iterator it = elements->begin(); std::list<CRChainCodeElement*>::iterator it_stepped = elements->begin(); ++it_stepped; ++it_stepped; for (int i = 0; i < diffListSize; i++) { CRChainCodeElement* p = (CRChainCodeElement*)*it; CRChainCodeElement* p_next = (CRChainCodeElement*)*it_stepped; diffList[i].x = p->x; diffList[i].y = p->y; diffList[i].dx = p_next->x - p->x; diffList[i].dy = p_next->y - p->y; diffList[i].label = 0; ++it_stepped; ++it; if (it_stepped == elements->end()) break; } // make seed for k-means int seedOffset = diffListSize / 8; int seedSampling = diffListSize / 4; seed[0] = diffList[seedOffset + 0 * seedSampling]; seed[1] = diffList[seedOffset + 1 * seedSampling]; seed[2] = diffList[seedOffset + 2 * seedSampling]; seed[3] = diffList[seedOffset + 3 * seedSampling]; if (this->elements->size() < MINIMUM_CHAINCODE_LENGTH) goto CODE_NOT_FOUND_EXCEPTION; for (int j = 0; j < 10; j++) { int labelUpdateCounter = 0; for (int i = 0; i < diffListSize; i++) { int newLabel = 0; float length = (diffList[i].dx - seed[0].dx) * (diffList[i].dx - seed[0].dx) + (diffList[i].dy - seed[0].dy) * (diffList[i].dy - seed[0].dy); for (int k = 1; k < 4; k++) { int temp = (diffList[i].dx - seed[k].dx) * (diffList[i].dx - seed[k].dx) + (diffList[i].dy - seed[k].dy) * (diffList[i].dy - seed[k].dy); if (temp < length) { length = temp; newLabel = k; } } // update label if (diffList[i].label != newLabel) { labelUpdateCounter++; diffList[i].label = newLabel; } } // update centroid int seedHistogram[4] = {0, 0, 0, 0}; for (int k = 0; k < 4; k++) { seed[k].dx = 0; seed[k].dy = 0; } for (int i = 0; i < diffListSize; i++) { int k = diffList[i].label; seedHistogram[k]++; seed[k].dx += diffList[i].dx; seed[k].dy += diffList[i].dy; } // check number of elements in clusters if (seedHistogram[0] == 0 || seedHistogram[1] == 0 || seedHistogram[2] == 0 || seedHistogram[3] == 0) goto CODE_NOT_FOUND_EXCEPTION; for (int k = 0; k < 4; k++) { seed[k].dx /= seedHistogram[k]; seed[k].dy /= seedHistogram[k]; } if (labelUpdateCounter == 0) break; } line1 = CreateLineFromCRDiffVectorInfoList(diffList, diffListSize, 0); line2 = CreateLineFromCRDiffVectorInfoList(diffList, diffListSize, 1); line3 = CreateLineFromCRDiffVectorInfoList(diffList, diffListSize, 2); line4 = CreateLineFromCRDiffVectorInfoList(diffList, diffListSize, 3); firstCorner = CRHomogeneousVec3::outerProduct(line4, line1); firstCorner->normalize(); secondCorner = CRHomogeneousVec3::outerProduct(line1, line2); secondCorner->normalize(); thirdCorner = CRHomogeneousVec3::outerProduct(line2, line3); thirdCorner->normalize(); fourthCorner = CRHomogeneousVec3::outerProduct(line3, line4); fourthCorner->normalize(); if (isConvex(firstCorner, secondCorner, thirdCorner, fourthCorner) == CR_FALSE) goto CODE_NOT_FOUND_EXCEPTION; // chaincode search algorithm is reverse order. code = new CRCode(firstCorner, fourthCorner, thirdCorner, secondCorner); code->left = this->left; code->right= this->right; code->top = this->top; code->bottom = this->bottom; CODE_NOT_FOUND_EXCEPTION: SAFE_DELETE(line1); SAFE_DELETE(line2); SAFE_DELETE(line3); SAFE_DELETE(line4); SAFE_DELETE(firstCorner); SAFE_DELETE(secondCorner); SAFE_DELETE(thirdCorner); SAFE_DELETE(fourthCorner); SAFE_FREE(diffList); return code; }