| 40 |  | * | 
| 41 |  | *  ConvexHull.cpp | 
| 42 |  | * | 
| 43 | < | *  Purpose: To calculate convexhull, hull volume and radius | 
| 44 | < | *  using the CGAL library. | 
| 43 | > | *  Purpose: To calculate convexhull, hull volume libqhull. | 
| 44 |  | * | 
| 45 |  | *  Created by Charles F. Vardeman II on 11 Dec 2006. | 
| 46 |  | *  @author  Charles F. Vardeman II | 
| 47 | < | *  @version $Id: ConvexHull.cpp,v 1.1 2006-12-14 19:32:32 chuckv Exp $ | 
| 47 | > | *  @version $Id: ConvexHull.cpp,v 1.2 2007-05-29 22:50:14 chuckv Exp $ | 
| 48 |  | * | 
| 49 |  | */ | 
| 50 |  |  | 
| 52 |  | #include <iostream> | 
| 53 |  | #include <fstream> | 
| 54 |  |  | 
| 55 | + | char options[] = "qhull Qt FA"; | 
| 56 | + | int dim_ = 3; | 
| 57 |  |  | 
| 58 |  | using namespace oopse; | 
| 59 |  |  | 
| 60 | + | ConvexHull::ConvexHull(){} | 
| 61 |  |  | 
| 62 |  |  | 
| 61 | – |  | 
| 63 |  | bool ConvexHull::genHull(std::vector<Vector3d> pos) | 
| 64 |  | { | 
| 65 | + | FILE *outfile = stdout; | 
| 66 | + | FILE *errfile = stderr; | 
| 67 | + | facetT *facet; | 
| 68 | + | int exitcode; | 
| 69 | + | boolT ismalloc = False; | 
| 70 | + | int curlong,totlong; | 
| 71 | + |  | 
| 72 | + | int numpoints = pos.size(); | 
| 73 | + |  | 
| 74 | + | coordT points[numpoints][dim_]; | 
| 75 | + |  | 
| 76 | + | for (int i=0; i<numpoints; i++) | 
| 77 | + | { | 
| 78 | + | points[i][0] = pos[i][0]; | 
| 79 | + | points[i][1] = pos[i][1]; | 
| 80 | + | points[i][2] = pos[i][2]; | 
| 81 | + | } | 
| 82 | + |  | 
| 83 |  |  | 
| 65 | – | std::vector<Point_3> points; | 
| 66 | – | points.reserve(pos.size()); | 
| 67 | – | // Copy the positon vector into a points vector for cgal. | 
| 68 | – | for (int i = 0; i < pos.size(); ++i) | 
| 69 | – | { | 
| 70 | – | Point_3 pt(pos[i][0],pos[i][1],pos[i][2]); | 
| 71 | – | points.push_back(pt); | 
| 72 | – | } | 
| 84 |  |  | 
| 85 | < | // compute convex hull | 
| 86 | < | CGAL::convex_hull_3(points.begin(), points.end(), ch_object); | 
| 87 | < | // Make sure hull is a polyhedron | 
| 88 | < | if ( CGAL::assign(ch_polyhedron, ch_object) ) | 
| 89 | < | { | 
| 90 | < | return true; | 
| 91 | < | } | 
| 92 | < | else | 
| 93 | < | { | 
| 94 | < | return false; | 
| 95 | < | } | 
| 85 | > | qh_initflags (options); | 
| 86 | > | qh_init_B (points[0], numpoints, dim_, ismalloc); | 
| 87 | > | qh_qhull(); | 
| 88 | > | qh_check_output(); | 
| 89 | > |  | 
| 90 | > |  | 
| 91 | > |  | 
| 92 | > | qh_getarea(qh facet_list); | 
| 93 | > | volume_ = qh totvol; | 
| 94 | > | area_ = qh totarea; | 
| 95 | > |  | 
| 96 | > |  | 
| 97 | > |  | 
| 98 | > | qh_freeqhull(!qh_ALL); | 
| 99 | > | qh_memfreeshort (&curlong, &totlong); | 
| 100 | > | if (curlong || totlong) | 
| 101 | > | fprintf (errfile, "qhull internal warning (main): did not free %d bytes of long memory (%d pieces)\n", | 
| 102 | > | totlong, curlong); | 
| 103 | > |  | 
| 104 | > |  | 
| 105 | > | return true; | 
| 106 |  | } | 
| 107 |  |  | 
| 108 | + |  | 
| 109 |  | RealType ConvexHull::getVolume() | 
| 110 |  | { | 
| 111 | < | /* | 
| 90 | < | std::list<Point> L; | 
| 91 | < | L.push_front(Point(0,0,0)); | 
| 92 | < | L.push_front(Point(1,0,0)); | 
| 93 | < | L.push_front(Point(0,1,0)); | 
| 94 | < |  | 
| 95 | < | Triangulation T(L.begin(), L.end()); | 
| 96 | < |  | 
| 97 | < | int n = T.number_of_vertices(); | 
| 98 | < |  | 
| 99 | < | // insertion from a vector : | 
| 100 | < | std::vector<Point> V(3); | 
| 101 | < | V[0] = Point(0,0,1); | 
| 102 | < | V[1] = Point(1,1,1); | 
| 103 | < | V[2] = Point(2,2,2); | 
| 104 | < |  | 
| 105 | < | n = n + T.insert(V.begin(), V.end()); | 
| 106 | < |  | 
| 107 | < | assert( n == 6 );       // 6 points have been inserted | 
| 108 | < | assert( T.is_valid() ); // checking validity of T | 
| 109 | < |  | 
| 110 | < | double sum_v = 0; | 
| 111 | < | for(Triangulation::Cell_iterator c_it = T.cells_begin(); c_it != T.cells_end(); ++c_it) | 
| 112 | < | { | 
| 113 | < | sum_v += T.tetrahedron(c_it).volume(); | 
| 114 | < | } | 
| 115 | < | std::cout << "sum_v " << sum_v << std::endl; | 
| 116 | < | */ | 
| 117 | < | return 0.0; | 
| 111 | > | return volume_; | 
| 112 |  | } | 
| 113 |  |  | 
| 114 |  | void ConvexHull::geomviewHull(const std::string& geomFileName) | 
| 119 |  | //create new .md file based on old .md file | 
| 120 |  | newGeomFile.open(geomFileName.c_str()); | 
| 121 |  |  | 
| 128 | – | // Write polyhedron in Object File Format (OFF). | 
| 129 | – | CGAL::set_ascii_mode( std::cout); | 
| 130 | – | newGeomFile << "OFF" << std::endl << ch_polyhedron.size_of_vertices() << ' ' | 
| 131 | – | << ch_polyhedron.size_of_facets() << " 0" << std::endl; | 
| 132 | – | std::copy( ch_polyhedron.points_begin(), ch_polyhedron.points_end(), | 
| 133 | – | std::ostream_iterator<Point_3>( newGeomFile, "\n")); | 
| 134 | – | for (  Facet_iterator i = ch_polyhedron.facets_begin(); i != ch_polyhedron.facets_end(); ++i) | 
| 135 | – | { | 
| 136 | – | Halfedge_facet_circulator j = i->facet_begin(); | 
| 137 | – | // Facets in polyhedral surfaces are at least triangles. | 
| 138 | – | CGAL_assertion( CGAL::circulator_size(j) >= 3); | 
| 139 | – | newGeomFile << CGAL::circulator_size(j) << ' '; | 
| 140 | – | do | 
| 141 | – | { | 
| 142 | – | newGeomFile << ' ' << std::distance(ch_polyhedron.vertices_begin(), j->vertex()); | 
| 143 | – | } | 
| 144 | – | while ( ++j != i->facet_begin()); | 
| 145 | – | newGeomFile << std::endl; | 
| 146 | – | } | 
| 122 |  |  | 
| 123 |  | newGeomFile.close(); | 
| 124 |  |  |