void Polynomial<int>::pseudo_div( const Polynomial<int>& f, const Polynomial<int>& g, Polynomial<int>& q, Polynomial<int>& r, int& D) { CGAL_NEF_TRACEN("pseudo_div "<<f<<" , "<< g); int fd=f.degree(), gd=g.degree(); if ( fd<gd ) { q = Polynomial<int>(0); r = f; D = 1; CGAL_postcondition(Polynomial<int>(D)*f==q*g+r); return; } // now we know fd >= gd and f>=g int qd=fd-gd, delta=qd+1, rd=fd; { q = Polynomial<int>( std::size_t(delta) ); }; // workaround for SUNPRO int G = g[gd]; // highest order coeff of g D = G; while (--delta) D*=G; // D = G^delta Polynomial<int> res = Polynomial<int>(D)*f; CGAL_NEF_TRACEN(" pseudo_div start "<<res<<" "<<qd<<" "<<q.degree()); while (qd >= 0) { int F = res[rd]; // highest order coeff of res int t = F/G; // ensured to be integer by multiplication of D q.coeff(qd) = t; // store q coeff res.minus_offsetmult(g,t,qd); if (res.is_zero()) break; rd = res.degree(); qd = rd - gd; } r = res; CGAL_postcondition(Polynomial<int>(D)*f==q*g+r); CGAL_NEF_TRACEN(" returning "<<q<<", "<<r<<", "<< D); }
void geometryUtils::subdivide(Polyhedron& P) { if (P.size_of_facets() == 0) return; // We use that new vertices/halfedges/facets are appended at the end. std::size_t nv = P.size_of_vertices(); Vertex_iterator last_v = P.vertices_end(); --last_v; // the last of the old vertices Edge_iterator last_e = P.edges_end(); --last_e; // the last of the old edges Facet_iterator last_f = P.facets_end(); --last_f; // the last of the old facets Facet_iterator f = P.facets_begin(); // create new center vertices do { geometryUtils::subdivide_create_center_vertex(P, f); } while (f++ != last_f); std::vector<Point_3> pts; // smooth the old vertices pts.reserve(nv); // get intermediate space for the new points ++last_v; // make it the past-the-end position again std::transform(P.vertices_begin(), last_v, std::back_inserter(pts), Smooth_old_vertex()); std::copy(pts.begin(), pts.end(), P.points_begin()); Edge_iterator e = P.edges_begin(); // flip the old edges ++last_e; // make it the past-the-end position again while (e != last_e) { Halfedge_handle h = e; ++e; // careful, incr. before flip since flip destroys current edge geometryUtils::subdivide_flip_edge(P, h); }; CGAL_postcondition(P.is_valid()); };
void Polynomial<int>::euclidean_div( const Polynomial<int>& f, const Polynomial<int>& g, Polynomial<int>& q, Polynomial<int>& r) { r = f; r.copy_on_write(); int rd=r.degree(), gd=g.degree(), qd; if ( rd < gd ) { q = Polynomial<int>(int(0)); } else { qd = rd-gd+1; q = Polynomial<int>(std::size_t(qd)); } while ( rd >= gd && !(r.is_zero())) { int S = r[rd] / g[gd]; qd = rd-gd; q.coeff(qd) += S; r.minus_offsetmult(g,S,qd); rd = r.degree(); } CGAL_postcondition( f==q*g+r ); }
// Subdivide each facet of the lcc by using pqq-subdivision. void subdivide_lcc_pqq (LCC & m) { if (m.number_of_darts () == 0) return; LCC::size_type old = m.get_new_mark (); LCC::size_type treated = m.get_new_mark (); m.negate_mark (old); // All the old darts are marked in O(1). // 1) We subdivide each edge. m.negate_mark (treated); // All the darts are marked in O(1). for (LCC::Dart_range::iterator it (m.darts().begin ()); m.number_of_marked_darts (treated) > 0; ++it) { if (m.is_marked (it, treated)) { // We unmark the darts of the facet to process only once dart/facet. CGAL::unmark_cell < LCC, 1 > (m, it, treated); // We insert barycenter in the middle of the edge. m.insert_barycenter_in_cell<1>(it); } } // 2) We create a barycenter point for each facets. Dart_handle dc; std::vector<Dart_handle> remove; remove.resize(0); m.negate_mark (treated); // All the darts are marked in O(1). for (LCC::Dart_range::iterator it (m.darts().begin ()); m.number_of_marked_darts (treated) > 0; ++it) { if (m.is_marked (it, treated)) { // We unmark the darts of the facet to process only once dart/facet. CGAL::unmark_cell < LCC, 2 > (m, it, treated); // We insert barycenter of the facet. dc = m.insert_barycenter_in_cell<2>(it); // We remove useless edges. for (LCC::One_dart_per_incident_cell_range<1,0>::iterator it2 = m.one_dart_per_incident_cell<1,0>(dc).begin(); it2 != m.one_dart_per_incident_cell<1,0>(dc).end(); ++it2) { // If the edge join the center and a corner. // We remove the edge. if( m.is_marked(m.beta(it2,1), old) ) { remove.push_back(it2); } } // Remove edges. for (std::vector <Dart_handle>::iterator dit = remove.begin (); dit != remove.end (); ++dit) { CGAL_assertion( (m.is_removable<1>(*dit)) ); m.remove_cell<1>(*dit); } remove.resize(0); // CGAL_assertion( m.is_valid() ); } } m.negate_mark (treated); CGAL_assertion (m.is_whole_map_marked (treated)); m.free_mark (treated); // 3) Smooth old points. std::vector < Vertex > old_vertices; // smooth the old vertices. old_vertices.reserve (m.number_of_attributes<0> ()); // get intermediate space. std::transform (m.vertex_attributes().begin (), m.vertex_attributes().end (), std::back_inserter (old_vertices), Smooth_vertex_pqq (m, old)); // Update. for (std::vector < Vertex >::iterator vit = old_vertices.begin (); vit != old_vertices.end (); ++vit) { m.point(vit->dart())=vit->point(); } // 4) Smooth new edges points. std::vector < Vertex > vertices; // smooth the old vertices. vertices.reserve (m.number_of_attributes<0> ()); // get intermediate space. std::transform (m.vertex_attributes().begin (), m.vertex_attributes().end (), std::back_inserter (vertices), Smooth_edge_pqq (m, old)); // Update. for (std::vector < Vertex >::iterator vit = vertices.begin (); vit != vertices.end (); ++vit) { m.point(vit->dart())=vit->point(); } m.unmark_all (old); m.free_mark (old); CGAL_postcondition ( m.is_valid ()); }
// a helper method for running different iterators void running_iterators( Polyhedron& P) { if ( P.size_of_facets() == 0) return; std::size_t nv = P.size_of_vertices(); std::cout << "The number of vertices in the Polyhedron: " << nv << std::endl; std::cout << "The number of facets in the Polyhedron: " << P.size_of_facets() << std::endl; std::cout << "The number of half edges in the Polyhedron: " << P.size_of_halfedges() << std::endl; std::cout << std:: endl; Polyhedron::Vertex_iterator last_v = P.vertices_end(); -- last_v; // the last of the old vertices Polyhedron::Edge_iterator last_e = P.edges_end(); -- last_e; // the last of the old edges Polyhedron::Facet_iterator last_f = P.facets_end(); -- last_f; // the last of the old facets int k = 0; Polyhedron::Facet_iterator f = P.facets_begin(); do { std::cout << "Printing a facet index: " << k++ << std::endl; f->halfedge(); } while ( f++ != last_f); std::cout << std::endl; // ------------------------------------------------- // traverse the vertices // ------------------------------------------------- std::cout << "Printing the vertex indices: " << std::endl; int n=0; for (Polyhedron::Vertex_iterator vi = P.vertices_begin(); vi != P.vertices_end(); ++vi) { Kernel::Point_3 p; p = vi->point(); std::cout << "Vertex index: " << n++ << std::endl; std::cout << "p.x() = " << p.x() << std::endl; std::cout << "p.y() = " << p.y() << std::endl; std::cout << "p.z() = " << p.z() << std::endl; } std::cout << std::endl; // ------------------------------------------------- // traverse the edges // ------------------------------------------------- std::cout << "Iterating over the edges.... " << std::endl; n=0; for (Polyhedron::Edge_iterator ei = P.edges_begin(); ei != P.edges_end(); ++ei) { ei->next(); Kernel::Point_3 p; p = ei->vertex()->point(); std::cout << "For edge index: " << n++ << std::endl; std::cout << "p.x() = " << p.x() << std::endl; std::cout << "p.y() = " << p.y() << std::endl; std::cout << "p.z() = " << p.z() << std::endl; } std::cout << std::endl; // ----------------------------------------------- // Do something else with the edge iterators // ----------------------------------------------- Polyhedron::Edge_iterator e = P.edges_begin(); ++ last_e; // make it the past-the-end position again while ( e != last_e) { Polyhedron::Halfedge_handle h = e; ++e; }; CGAL_postcondition( P.is_valid()); }
// Subdivide each facet of the lcc by using sqrt(3)-subdivision. void subdivide_lcc_3 (LCC & m) { if (m.number_of_darts () == 0) return; LCC::size_type mark = m.get_new_mark (); LCC::size_type treated = m.get_new_mark (); m.negate_mark (mark); // All the old darts are marked in O(1). // 1) We smoth the old vertices. std::vector <std::pair<Point_3, Dart_handle> > vertices; // smooth the old vertices vertices.reserve (m.number_of_attributes<0> ()); // get intermediate space std::transform (m.vertex_attributes().begin (), m.vertex_attributes().end (), std::back_inserter (vertices), Smooth_old_vertex (m, mark)); // 2) We subdivide each facet. m.negate_mark (treated); // All the darts are marked in O(1). unsigned int nb = 0; for (LCC::Dart_range::iterator it (m.darts().begin ()); m.number_of_marked_darts (treated) > 0; ++it) { ++nb; if (m.is_marked (it, treated)) { // We unmark the darts of the facet to process only once dart/facet. CGAL::unmark_cell < LCC, 2 > (m, it, treated); // We triangulate the facet. m.insert_barycenter_in_cell<2>(it); } } CGAL_assertion (m.is_whole_map_unmarked (treated)); CGAL_assertion (m.is_valid ()); m.free_mark (treated); // 3) We update the coordinates of old vertices. for (std::vector<std::pair<Point_3, Dart_handle> >::iterator vit=vertices.begin(); vit!=vertices.end(); ++vit) { m.point(vit->second)=vit->first; } // 4) We flip all the old edges. m.negate_mark (mark); // Now only new darts are marked. Dart_handle d2 =LCC::null_handle; for (LCC::Dart_range::iterator it (m.darts().begin ()); it != m.darts().end ();) { d2 = it++; if (!m.is_marked (d2, mark)) // This is an old dart. { // We process only the last dart of a same edge. if (!m.is_free(d2,2) && (m.beta(d2,2,3)==m.beta(d2,3,2))) { if (m.is_marked(m.beta(d2,2), mark) && (m.is_free(d2,3) || (m.is_marked(m.beta(d2,3), mark) && m.is_marked(m.beta(d2,2,3), mark)))) { flip_edge (m, d2); m.mark(d2, mark); } else m.mark (d2, mark); } else m.mark (d2, mark); } } CGAL_assertion (m.is_whole_map_marked (mark)); m.free_mark (mark); CGAL_postcondition ( m.is_valid ()); }