void testVec3Bits() { glam::vec3 vf1(1.0f, -1.0f, 0.5f); glam::ivec3 vi1 = glam::floatBitsToInt(vf1); TS_ASSERT_EQUALS(vi1[0], 0x3f800000); TS_ASSERT_EQUALS(vi1[1], 0xbf800000); TS_ASSERT_EQUALS(vi1[2], 0x3f000000); glam::ivec3 vi2(0x3f800000, 0xbf800000, 0x3f000000); glam::vec3 vf2 = glam::intBitsToFloat(vi2); TS_ASSERT_EQUALS(vf2[0], 1.0f); TS_ASSERT_EQUALS(vf2[1], -1.0f); TS_ASSERT_EQUALS(vf2[2], 0.5f); }
void run_move_only() { move_only_structure mo; boost::variant<int, move_only_structure > vi, vi2(static_cast<move_only_structure&&>(mo)); BOOST_CHECK(vi.which() == 0); BOOST_CHECK(vi2.which() == 1); vi = 10; vi2 = 10; BOOST_CHECK(vi.which() == 0); BOOST_CHECK(vi2.which() == 0); vi = static_cast<move_only_structure&&>(mo); vi2 = static_cast<move_only_structure&&>(mo); BOOST_CHECK(vi.which() == 1); }
int main() { std::vector<int> vi; for(int i = 0; i < 20; ++i) { vi.push_back(i); } for(auto i : vi) { std::cout << i << " "; } std::cout << std::endl; std::vector<int>::iterator it5, it17; it5 = std::find(vi.begin(), vi.end(), 5); std::cout << *it5 << std::endl; it17 = std::find(vi.begin(), vi.end(), 17); std::cout << *it17 << std::endl; std::vector<int> vi2(std::distance(it5, it17+1)); std::copy(it5, it17+1, vi2.begin()); for(auto i : vi2) { std::cout << i << " "; } return 0; }
PView *GMSH_CVTRemeshPlugin::execute(PView *v) { //TODO normalization GModel* m = GModel::current() ; std::vector<double> vertices ; std::vector<unsigned int> faces ; unsigned int offset = 0 ; for(GModel::fiter it = m->firstFace(); it != m->lastFace(); ++it) { (*it)->buildSTLTriangulation() ; for(unsigned int i = 0; i < (*it)->stl_vertices.size(); ++i) { GPoint p = (*it)->point((*it)->stl_vertices[i]) ; vertices.push_back(p.x()) ; vertices.push_back(p.y()) ; vertices.push_back(p.z()) ; } for(unsigned int i = 0; i < (*it)->stl_triangles.size(); ++i) { faces.push_back((*it)->stl_triangles[i]+offset) ; } offset += (*it)->stl_vertices.size() ; } Revoropt::MeshBuilder<3> mesh ; mesh.swap_vertices(vertices) ; mesh.swap_faces(faces) ; double mesh_center[3] ; double mesh_scale ; Revoropt::normalize_mesh(&mesh, mesh_center, &mesh_scale) ; double nradius = (double)CVTRemeshOptions_Number[5].def ; //normals std::vector<double> normals(3*mesh.vertices_size()) ; Revoropt::full_robust_vertex_normals(&mesh,nradius,normals.data()) ; //lifted vertices std::vector<double> lifted_vertices(6*mesh.vertices_size(), 0) ; for(unsigned int vertex = 0; vertex < mesh.vertices_size(); ++vertex) { std::copy( mesh.vertex(vertex), mesh.vertex(vertex)+3, lifted_vertices.data()+6*vertex ) ; std::copy( normals.data()+3*vertex, normals.data()+3*vertex+3, lifted_vertices.data()+6*vertex+3 ) ; } //setup lifted mesh Revoropt::ROMeshWrapper<3,6> lifted_mesh( lifted_vertices.data(), lifted_vertices.size()/6, &mesh ) ; //triangle weight factor double twfactor = (double)CVTRemeshOptions_Number[3].def ; //face ratios std::vector<double> triangle_weights(lifted_mesh.faces_size()) ; if(twfactor > 0) { for(unsigned int f = 0; f < lifted_mesh.faces_size(); ++f) { //vertices of the initial triangle const unsigned int* fverts = mesh.face(f) ; //positions const double* x[3] ; for(int i=0; i<3; ++i) { x[i] = lifted_mesh.vertex(fverts[i]) ; } //ratio double ratio = 1 ; //vectors typedef Eigen::Matrix<double,3,1> Vector3 ; Eigen::Map<const Vector3> v0(x[0]) ; Eigen::Map<const Vector3> v1(x[1]) ; Eigen::Map<const Vector3> v2(x[2]) ; //triangle frame Vector3 U = (v1-v0) ; const double U_len = U.norm() ; if(U_len > 0) { U /= U_len ; Vector3 H = (v2-v0) ; H = H - H.dot(U)*U ; const double H_len = H.norm() ; if(H_len > 0) { //we know that the triangle is not flat H /= H_len ; //gradient of the barycentric weights in the triangle Eigen::Matrix<double,3,2> bar_grads ; bar_grads(2,0) = 0 ; bar_grads(2,1) = 1/H_len ; //gradient norms of every normal component for(int i = 0; i < 2; ++i) { //reference frame for the vertex Eigen::Map<const Vector3> vi0(x[(i+1)%3]) ; Eigen::Map<const Vector3> vi1(x[(i+2)%3]) ; Eigen::Map<const Vector3> vi2(x[ i ]) ; Vector3 Ui = (vi1-vi0) ; Ui /= Ui.norm() ; Vector3 Hi = (vi2-vi0) ; Hi = Hi - Hi.dot(Ui)*Ui ; const double Hi_invlen = 1/Hi.norm() ; Hi *= Hi_invlen ; bar_grads(i,0) = Hi.dot(U)*Hi_invlen ; bar_grads(i,1) = Hi.dot(H)*Hi_invlen ; } //gradient of each component of the normal Eigen::Map<const Vector3> n0(x[0]+3) ; Eigen::Map<const Vector3> n1(x[1]+3) ; Eigen::Map<const Vector3> n2(x[2]+3) ; Eigen::Matrix<double,3,2> n_grads = Eigen::Matrix<double,3,2>::Zero() ; n_grads = n0*bar_grads.row(0) ; n_grads += n1*bar_grads.row(1) ; n_grads += n2*bar_grads.row(2) ; //maximal gradient norm double g_max = n_grads.row(0).dot(n_grads.row(0)) ; double g_other = n_grads.row(1).dot(n_grads.row(1)) ; g_max = g_max > g_other ? g_max : g_other ; g_other = n_grads.row(2).dot(n_grads.row(2)) ; g_max = g_max > g_other ? g_max : g_other ; if(g_max == g_max) { //prevent nan ratio += g_max ; } } } triangle_weights[f] = pow(ratio,twfactor) ; } } //normal factor double nfactor = (double)CVTRemeshOptions_Number[2].def ; ; //weight the normal component by the provided factor for(unsigned int i = 0; i<lifted_mesh.vertices_size(); ++i) { double* v = lifted_vertices.data() + 6*i ; v[3]*= nfactor ; v[4]*= nfactor ; v[5]*= nfactor ; } //number of sites unsigned int nsites = (unsigned int)CVTRemeshOptions_Number[0].def ; //lifted sites std::vector<double> lifted_sites(6*nsites) ; if(twfactor > 0) { Revoropt::generate_random_sites< Revoropt::ROMesh<3,6> >( &lifted_mesh, nsites, lifted_sites.data(), triangle_weights.data() ) ; } else { Revoropt::generate_random_sites< Revoropt::ROMesh<3,6> >( &lifted_mesh, nsites, lifted_sites.data() ) ; } //setup the cvt minimizer Revoropt::CVT::DirectMinimizer< Revoropt::ROMesh<3,6> > cvt ; cvt.set_sites(lifted_sites.data(), nsites) ; cvt.set_mesh(&lifted_mesh) ; if(twfactor > 0) { cvt.set_triangle_weights(triangle_weights.data()) ; } //setup the callback SolverCallback callback ; //number of iterations unsigned int niter = (unsigned int)CVTRemeshOptions_Number[1].def ; ; unsigned int aniso_niter = std::min<unsigned int>(10,niter) ; //solver status int status = 0 ; //isotropic iterations if(niter > 10) { aniso_niter = std::max(aniso_niter,niter*10/100) ; cvt.set_anisotropy(1) ; status = cvt.minimize<Revoropt::Solver::AlgLBFGS>(niter-aniso_niter, &callback) ; } //anisotropic iterations if(niter > 0) { //tangent space anisotropy double tanisotropy = (double)CVTRemeshOptions_Number[4].def ; ; //anisotropic iterations cvt.set_anisotropy(tanisotropy) ; status = cvt.minimize<Revoropt::Solver::AlgLBFGS>(aniso_niter, &callback) ; } //rdt std::vector<unsigned int> rdt_triangles ; Revoropt::RDTBuilder< Revoropt::ROMesh<3,6> > build_rdt(rdt_triangles) ; Revoropt::RVD< Revoropt::ROMesh<3,6> > rvd ; rvd.set_sites(lifted_sites.data(), nsites) ; rvd.set_mesh(&lifted_mesh) ; rvd.compute(build_rdt) ; GFace* res_face = new discreteFace(m, m->getMaxElementaryNumber(2)+1) ; m->add(res_face) ; //scale back and transfer to gmsh std::vector<MVertex*> m_verts(nsites) ; for(unsigned int i = 0; i < nsites; ++i) { m_verts[i] = new MVertex( lifted_sites[6*i ]*mesh_scale + mesh_center[0], lifted_sites[6*i+1]*mesh_scale + mesh_center[1], lifted_sites[6*i+2]*mesh_scale + mesh_center[2] ) ; res_face->addMeshVertex(m_verts[i]) ; } for(unsigned int i = 0; i < rdt_triangles.size()/3; ++i) { res_face->addTriangle( new MTriangle( m_verts[rdt_triangles[3*i ]], m_verts[rdt_triangles[3*i+1]], m_verts[rdt_triangles[3*i+2]] ) ) ; } res_face->setAllElementsVisible(true) ; return v ; }
TEST(SharedValueTest, value) { // bool Value vb = false; EXPECT_EQ(vb.as<bool>(), false); EXPECT_EQ(bool(vb), false); EXPECT_EQ(static_cast<bool>(vb), false); EXPECT_NO_THROW(bool b = vb); Value vb1(false); Value vb2(true); EXPECT_EQ(bool(vb1), false); EXPECT_EQ(bool(vb2), true); bool b = vb; EXPECT_EQ(b, false); EXPECT_NO_THROW(vb = true); EXPECT_EQ(vb.as<bool>(), true); EXPECT_EQ(bool(vb), true); EXPECT_EQ(static_cast<bool>(vb), true); b = vb; EXPECT_EQ(b, true); // float Value vf = 1 / 3.; EXPECT_EQ(vf.as<float>(), 1 / 3.f); EXPECT_EQ(float(vf), 1 / 3.f); EXPECT_EQ(static_cast<float>(vf), 1 / 3.f); EXPECT_EQ(vf.as<double>(), 1 / 3.f); EXPECT_EQ(double(vf), 1 / 3.f); EXPECT_EQ(static_cast<double>(vf), 1 / 3.f); Value vf2(1.0 / 7); Value vf3(1 / 8.f); Value vf4(vf3); EXPECT_EQ(float(vf2), 1.f / 7); EXPECT_EQ(float(vf3), 1.f / 8); EXPECT_EQ(float(vf4), 1.f / 8); EXPECT_NO_THROW(vf = 1 / 7.f); EXPECT_NO_THROW(float f = vf); EXPECT_NO_THROW(double d = vf); float f = vf; double d = vf; EXPECT_EQ(f, 1 / 7.f); EXPECT_EQ(d, 1 / 7.f); EXPECT_NO_THROW(vf = 14.245f); EXPECT_EQ(float(vf), 14.245f); EXPECT_EQ(double(vf), 14.245f); EXPECT_EQ(double(vf), double(14.245f)); EXPECT_NE(double(vf), double(14.245)); EXPECT_TRUE(float(vf) == float(14.245)); // int Value vi = -56; EXPECT_EQ(vi.as<int>(), -56); EXPECT_EQ(int(vi), -56); EXPECT_EQ(static_cast<int>(vi), -56); EXPECT_NO_THROW(int i = vi); Value vi2(32); Value vi3(3530454); Value vi4(0xFF1110u); EXPECT_EQ(int(vi2), 32); EXPECT_EQ(int(vi3), 3530454); EXPECT_EQ(unsigned(vi4), 0xFF1110u); int i = vi; EXPECT_EQ(i, -56); EXPECT_NO_THROW(vi = 17); EXPECT_EQ(int(vi), 17); EXPECT_EQ(char(vi), 17); EXPECT_EQ(short(vi), 17); EXPECT_EQ(unsigned(vi), 17u); // pointer int test = 1; int test2 = 2; EXPECT_NO_THROW(Value vp(&test)); Value vp(static_cast<void*>(&test)); EXPECT_NO_THROW(const void* p = vp); const void* p = vp; EXPECT_EQ(static_cast<const void*>(vp), &test); EXPECT_ANY_THROW(vp = &test2); EXPECT_NO_THROW(vp = static_cast<const void *>(&test2)); EXPECT_EQ(static_cast<const void*>(vp), &test2); }
TEST(SharedValueTest, ctorInt) { int i = -324523; unsigned u = 0x334234u; char c = -22; short s = 289; unsigned char uc = 244; unsigned short us = 3467; long long ll = -20398574395872907ll; unsigned long long ull = 985734209239847656ull; EXPECT_NO_THROW(Value v(i)); EXPECT_NO_THROW(Value v(u)); EXPECT_NO_THROW(Value v(c)); EXPECT_NO_THROW(Value v(s)); EXPECT_NO_THROW(Value v(uc)); EXPECT_NO_THROW(Value v(us)); EXPECT_NO_THROW(Value v(ll)); EXPECT_NO_THROW(Value v(ull)); Value vi(i); EXPECT_EQ(vi.as<int>(), i); Value vu(u); EXPECT_EQ(vu.as<unsigned>(), u); Value vc(c); EXPECT_EQ(vc.as<char>(), c); Value vs(s); EXPECT_EQ(vs.as<short>(), s); Value vuc(uc); EXPECT_EQ(vuc.as<unsigned char>(), uc); Value vus(us); EXPECT_EQ(vus.as<unsigned short>(), us); Value vll(ll); EXPECT_NE(vll.as<long long>(), ll); // 32 bit storage overfflow EXPECT_EQ(vll.as<long long>(), static_cast<long long>(int(ll))); Value vull(ull); EXPECT_NE(vull.as<unsigned long long>(), ull); // 32 bit storage overfflow EXPECT_EQ(vull.as<unsigned long long>(), static_cast<unsigned long long>(int(ull))); Value vllNoOverflow(-42424ll); // no 32 bit storage overflow EXPECT_EQ(vllNoOverflow.as<long long>(), -42424ll); Value vullNoOverflow(0x43424242ull); // no 32 bit storage overflow EXPECT_EQ(vullNoOverflow.as<unsigned long long>(), 0x43424242ull); EXPECT_EQ(vi.type(), typeid(int)); EXPECT_EQ(vu.type(), typeid(int)); EXPECT_EQ(vc.type(), typeid(int)); EXPECT_EQ(vs.type(), typeid(int)); EXPECT_EQ(vuc.type(), typeid(int)); EXPECT_EQ(vus.type(), typeid(int)); EXPECT_EQ(vll.type(), typeid(int)); EXPECT_EQ(vull.type(), typeid(int)); // copy EXPECT_NO_THROW(Value v(vi)); EXPECT_NO_THROW(Value v(vu)); EXPECT_NO_THROW(Value v(vc)); EXPECT_NO_THROW(Value v(vs)); EXPECT_NO_THROW(Value v(vuc)); EXPECT_NO_THROW(Value v(vus)); EXPECT_NO_THROW(Value v(vll)); EXPECT_NO_THROW(Value v(vull)); Value vi2(vi); EXPECT_EQ(vi2.as<int>(), i); Value vu2(vu); EXPECT_EQ(vu2.as<unsigned>(), u); Value vc2(vc); EXPECT_EQ(vc2.as<char>(), c); Value vs2(vs); EXPECT_EQ(vs2.as<short>(), s); Value vuc2(vuc); EXPECT_EQ(vuc2.as<unsigned char>(), uc); Value vus2(vus); EXPECT_EQ(vus2.as<unsigned short>(), us); Value vll2(vll); EXPECT_NE(vll.as<long long>(), ll); EXPECT_EQ(vll2.as<long long>(), static_cast<long long>(int(vll))); Value vull2(vull); EXPECT_NE(vull.as<unsigned long long>(), ull); EXPECT_EQ(vull2.as<unsigned long long>(), static_cast<unsigned long long>(int(vull))); EXPECT_EQ(vi2.type(), typeid(int)); EXPECT_EQ(vu2.type(), typeid(int)); EXPECT_EQ(vc2.type(), typeid(int)); EXPECT_EQ(vs2.type(), typeid(int)); EXPECT_EQ(vuc2.type(), typeid(int)); EXPECT_EQ(vus2.type(), typeid(int)); EXPECT_EQ(vll2.type(), typeid(int)); EXPECT_EQ(vull2.type(), typeid(int)); }
void FixedPlaneMesh::TrianglatePolygon(const double3& normal, std::vector<Point3D>& pts, ListOfvertices& results) { if (pts.size() < 3) return ; if (pts.size() ==3) { VertexInfo vi1(pts[0], normal, mColor); VertexInfo vi2(pts[1], normal, mColor); VertexInfo vi3(pts[2], normal, mColor); results.push_back(vi1); results.push_back(vi2); results.push_back(vi3); return ; } typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef CGAL::Triangulation_vertex_base_2<K> Vb; typedef CGAL::Constrained_triangulation_face_base_2<K> Fb; typedef CGAL::Triangulation_data_structure_2<Vb,Fb> TDS; //typedef CGAL::Exact_predicates_tag Itag; typedef CGAL::Constrained_Delaunay_triangulation_2<K, TDS, CGAL::No_intersection_tag> CDT; vec3<double> origin = pts[0]; vec3<double> N = normal; vec3<double> U = normalize(pts[1] - origin); vec3<double> V = cross(N, U); CDT cdt; CDT::Vertex_handle vh1, vh2, vh3; vec3<double> v0 = PosToLocal(U, V, N, origin, pts[0]); CDT::Point p0(v0.x, v0.y); vh1 = vh3 = cdt.insert(p0); for ( int i = 1; i< pts.size() ; i++) { vec3<double> v1 = PosToLocal(U, V, N, origin, pts[i]); CDT::Point p1(v1.x, v1.y); vh2 = cdt.insert(p1); cdt.insert_constraint(vh1, vh2); vh1 = vh2; } cdt.insert_constraint(vh2, vh3); int count = cdt.number_of_faces() ; results.reserve(count*3); for (CDT::Finite_faces_iterator fit = cdt.finite_faces_begin(); fit != cdt.finite_faces_end(); ++fit) { vec2<double> v0(fit->vertex(2)->point().x(),fit->vertex(2)->point().y() ); vec2<double> v1(fit->vertex(1)->point().x(),fit->vertex(1)->point().y() ); vec2<double> v2(fit->vertex(0)->point().x(),fit->vertex(0)->point().y() ); if (IsEqual(cross(v0- v2, v1-v2), (double)0., (double)EPSF )) continue; // vec3<double > p0(v0, 0.0); vec3<double > p1(v1, 0.0); vec3<double > p2(v2, 0.0); p0 = PosToGlobal(U, V, N, origin, p0); p1 = PosToGlobal(U, V, N, origin, p1); p2 = PosToGlobal(U, V, N, origin, p2); VertexInfo vi1(p0, N, mColor); VertexInfo vi2(p1, N, mColor); VertexInfo vi3(p2, N, mColor); results.push_back(vi1); results.push_back(vi2); results.push_back(vi3); } }