1 |
/* Copyright (c) 2006 The University of Notre Dame. All Rights Reserved. |
2 |
* |
3 |
* The University of Notre Dame grants you ("Licensee") a |
4 |
* non-exclusive, royalty free, license to use, modify and |
5 |
* redistribute this software in source and binary code form, provided |
6 |
* that the following conditions are met: |
7 |
* |
8 |
* 1. Acknowledgement of the program authors must be made in any |
9 |
* publication of scientific results based in part on use of the |
10 |
* program. An acceptable form of acknowledgement is citation of |
11 |
* the article in which the program was described (Matthew |
12 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
13 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
14 |
* Parallel Simulation Engine for Molecular Dynamics," |
15 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
16 |
* |
17 |
* 2. Redistributions of source code must retain the above copyright |
18 |
* notice, this list of conditions and the following disclaimer. |
19 |
* |
20 |
* 3. Redistributions in binary form must reproduce the above copyright |
21 |
* notice, this list of conditions and the following disclaimer in the |
22 |
* documentation and/or other materials provided with the |
23 |
* distribution. |
24 |
* |
25 |
* This software is provided "AS IS," without a warranty of any |
26 |
* kind. All express or implied conditions, representations and |
27 |
* warranties, including any implied warranty of merchantability, |
28 |
* fitness for a particular purpose or non-infringement, are hereby |
29 |
* excluded. The University of Notre Dame and its licensors shall not |
30 |
* be liable for any damages suffered by licensee as a result of |
31 |
* using, modifying or distributing the software or its |
32 |
* derivatives. In no event will the University of Notre Dame or its |
33 |
* licensors be liable for any lost revenue, profit or data, or for |
34 |
* direct, indirect, special, consequential, incidental or punitive |
35 |
* damages, however caused and regardless of the theory of liability, |
36 |
* arising out of the use of or inability to use software, even if the |
37 |
* University of Notre Dame has been advised of the possibility of |
38 |
* such damages. |
39 |
* |
40 |
* |
41 |
* ConvexHull.cpp |
42 |
* |
43 |
* Purpose: To calculate convexhull, hull volume and radius |
44 |
* using the CGAL library. |
45 |
* |
46 |
* Created by Charles F. Vardeman II on 11 Dec 2006. |
47 |
* @author Charles F. Vardeman II |
48 |
* @version $Id: ConvexHull.cpp,v 1.1 2006-12-14 19:32:32 chuckv Exp $ |
49 |
* |
50 |
*/ |
51 |
|
52 |
#include "math/ConvexHull.hpp" |
53 |
#include <iostream> |
54 |
#include <fstream> |
55 |
|
56 |
|
57 |
using namespace oopse; |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
bool ConvexHull::genHull(std::vector<Vector3d> pos) |
63 |
{ |
64 |
|
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 |
} |
73 |
|
74 |
// compute convex hull |
75 |
CGAL::convex_hull_3(points.begin(), points.end(), ch_object); |
76 |
// Make sure hull is a polyhedron |
77 |
if ( CGAL::assign(ch_polyhedron, ch_object) ) |
78 |
{ |
79 |
return true; |
80 |
} |
81 |
else |
82 |
{ |
83 |
return false; |
84 |
} |
85 |
} |
86 |
|
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)); |
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; |
118 |
} |
119 |
|
120 |
void ConvexHull::geomviewHull(const std::string& geomFileName) |
121 |
{ |
122 |
|
123 |
std::ofstream newGeomFile; |
124 |
|
125 |
//create new .md file based on old .md file |
126 |
newGeomFile.open(geomFileName.c_str()); |
127 |
|
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 |
} |
147 |
|
148 |
newGeomFile.close(); |
149 |
|
150 |
|
151 |
} |