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.4 2007-05-30 19:51:07 chuckv Exp $ |
48 |
|
* |
49 |
|
*/ |
50 |
|
|
52 |
– |
#include "math/ConvexHull.hpp" |
51 |
|
#include <iostream> |
52 |
|
#include <fstream> |
53 |
+ |
#include "math/ConvexHull.hpp" |
54 |
|
|
56 |
– |
|
55 |
|
using namespace oopse; |
56 |
|
|
57 |
+ |
ConvexHull::ConvexHull() : dim_(3), options_("qhull Qt FA") { |
58 |
+ |
} |
59 |
|
|
60 |
+ |
bool ConvexHull::genHull(std::vector<Vector3d> pos) { |
61 |
+ |
FILE *outfile = stdout; |
62 |
+ |
FILE *errfile = stderr; |
63 |
+ |
facetT *facet; |
64 |
+ |
int exitcode; |
65 |
+ |
boolT ismalloc = False; |
66 |
+ |
int curlong,totlong; |
67 |
+ |
|
68 |
+ |
int numpoints = pos.size(); |
69 |
+ |
|
70 |
+ |
coordT* points; |
71 |
+ |
points = (coordT*) malloc(sizeof(coordT) * (numpoints*dim_)); |
72 |
|
|
73 |
< |
|
74 |
< |
bool ConvexHull::genHull(std::vector<Vector3d> pos) |
75 |
< |
{ |
76 |
< |
|
77 |
< |
std::vector<Point_3> points; |
78 |
< |
points.reserve(pos.size()); |
79 |
< |
// Copy the positon vector into a points vector for cgal. |
80 |
< |
for (int i = 0; i < pos.size(); ++i) |
81 |
< |
{ |
82 |
< |
Point_3 pt(pos[i][0],pos[i][1],pos[i][2]); |
83 |
< |
points.push_back(pt); |
84 |
< |
} |
85 |
< |
|
86 |
< |
// compute convex hull |
87 |
< |
CGAL::convex_hull_3(points.begin(), points.end(), ch_object); |
88 |
< |
// Make sure hull is a polyhedron |
89 |
< |
if ( CGAL::assign(ch_polyhedron, ch_object) ) |
90 |
< |
{ |
91 |
< |
return true; |
92 |
< |
} |
93 |
< |
else |
94 |
< |
{ |
95 |
< |
return false; |
96 |
< |
} |
73 |
> |
for (int i=0; i<numpoints; i++) { |
74 |
> |
points[dim_ * i] = pos[i][0]; |
75 |
> |
points[dim_ * i + 1] = pos[i][1]; |
76 |
> |
points[dim_ * i + 2] = pos[i][2]; |
77 |
> |
} |
78 |
> |
|
79 |
> |
|
80 |
> |
qh_initflags (const_cast<char *>(options_.c_str())); |
81 |
> |
qh_init_B (points, numpoints, dim_, ismalloc); |
82 |
> |
qh_qhull(); |
83 |
> |
qh_check_output(); |
84 |
> |
|
85 |
> |
|
86 |
> |
|
87 |
> |
qh_getarea(qh facet_list); |
88 |
> |
volume_ = qh totvol; |
89 |
> |
area_ = qh totarea; |
90 |
> |
|
91 |
> |
|
92 |
> |
|
93 |
> |
qh_freeqhull(!qh_ALL); |
94 |
> |
qh_memfreeshort (&curlong, &totlong); |
95 |
> |
if (curlong || totlong) |
96 |
> |
fprintf (errfile, "qhull internal warning (main): did not free %d bytes of long memory (%d pieces)\n", |
97 |
> |
totlong, curlong); |
98 |
> |
|
99 |
> |
|
100 |
> |
return true; |
101 |
|
} |
102 |
|
|
87 |
– |
RealType ConvexHull::getVolume() |
88 |
– |
{ |
89 |
– |
/* |
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)); |
103 |
|
|
104 |
< |
Triangulation T(L.begin(), L.end()); |
105 |
< |
|
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; |
104 |
> |
RealType ConvexHull::getVolume() { |
105 |
> |
return volume_; |
106 |
|
} |
107 |
|
|
108 |
< |
void ConvexHull::geomviewHull(const std::string& geomFileName) |
109 |
< |
{ |
122 |
< |
|
108 |
> |
void ConvexHull::geomviewHull(const std::string& geomFileName) { |
109 |
> |
|
110 |
|
std::ofstream newGeomFile; |
111 |
< |
|
111 |
> |
|
112 |
|
//create new .md file based on old .md file |
113 |
|
newGeomFile.open(geomFileName.c_str()); |
114 |
< |
|
115 |
< |
// 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 |
< |
} |
147 |
< |
|
114 |
> |
|
115 |
> |
|
116 |
|
newGeomFile.close(); |
149 |
– |
|
150 |
– |
|
117 |
|
} |