Octopus
cgal_polyhedra.F90
Go to the documentation of this file.
1!! Copyright (C) 2014 Vladimir Fuka
2!! Copyright (C) 2020 Heiko Appel
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation, version 3
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17
18#include "global.h"
19
21 use debug_oct_m
22 use global_oct_m
23 use iso_c_binding
25
26 implicit none
27
28 private
29
30 public :: &
37
38 type, bind(C) :: d3
39 real(c_double) :: x, y, z
40 end type
41
43 private
44 type(c_ptr) :: AABB_tree
45 type(c_ptr) :: polyhedron
46 end type cgal_polyhedra_t
47
48#ifdef HAVE_CGAL
49 interface
50
51 subroutine polyhedron_from_file(ptree, fname, verbose, ierr) bind(C,name="polyhedron_from_file")
52 import
53 type(c_ptr), intent(out) :: ptree
54 character(kind=c_char), intent(in) :: fname(*)
55 integer(c_int), value :: verbose ! avoid bool in C++, not equal to c_bool
56 integer(c_int), intent(out) :: ierr
57 end subroutine
58
59 subroutine polyhedron_build_aabb_tree(tree, poly) bind(C,name="polyhedron_build_AABB_tree")
60 import
61 type(c_ptr), intent(out) :: tree
62 type(c_ptr), intent(in) :: poly
63 end subroutine
64
65 function polyhedron_point_inside(tree, vec) result(res) bind(C,name="polyhedron_point_inside")
66 import
67 logical(c_bool) :: res
68 type(c_ptr), intent(in) :: tree
69 type(d3), intent(in) :: vec
70 end function
71
72 subroutine polyhedron_finalize_aabb_tree(tree) bind(C,name="polyhedron_finalize_AABB_tree")
73 import
74 type(c_ptr), intent(inout) :: tree
75 end subroutine
76
77 subroutine polyhedron_finalize_polyhedron(polyhedron) bind(C,name="polyhedron_finalize_polyhedron")
78 import
79 type(c_ptr), intent(inout) :: polyhedron
80 end subroutine
81
82 end interface
83#endif
84
85
86contains
87
88 ! ---------------------------------------------------------
89 subroutine cgal_polyhedron_init(cgal_poly, fname, verbose)
90 type(cgal_polyhedra_t), intent(inout) :: cgal_poly
91 character(*), intent(in) :: fname
92 logical, intent(in) :: verbose
93
94 push_sub(cgal_polyhedron_init)
95
96 call cgal_polyhedron_read(cgal_poly, fname, verbose)
98
100 end subroutine cgal_polyhedron_init
101
102
103 ! ---------------------------------------------------------
104 subroutine cgal_polyhedron_read(cgal_poly, fname, verbose)
105 type(cgal_polyhedra_t), intent(inout) :: cgal_poly
106 character(*), intent(in) :: fname
107 logical, intent(in) :: verbose
108
109 integer(c_int) :: verb, ierr
110
111 push_sub(cgal_polyhedron_read)
112
113 verb = 0
114 ierr = 0
115
116 if (verbose) verb = 1
117#ifdef HAVE_CGAL
118 call polyhedron_from_file(cgal_poly%polyhedron, fname//c_null_char, verb, ierr)
119#endif
120#ifndef HAVE_CGAL
121 ierr = 3
122#endif
123
124 select case (ierr)
125 case (0)
126 message(1) = "Info: finished reading polyhedron from file " // fname
127 call messages_info(1)
128 case (1)
129 message(1) = "Error reading file " // fname // ", it appears to be empty."
130 call messages_fatal(1)
131 case (2)
132 message(1) = "Error reading file " // fname // "."
133 call messages_fatal(1)
134 case (3)
135 message(1) = "You are trying to read polyhedron data from " // fname
136 message(2) = "For this feature Octopus has to be linked with the CGAL library."
137 call messages_fatal(2)
138 case default
139 message(1) = "Error: Error status not implemented in CGAL Fortran interface."
140 call messages_fatal(1)
141 end select
142
143 pop_sub(cgal_polyhedron_read)
144 end subroutine cgal_polyhedron_read
145
146
147 ! ---------------------------------------------------------
148 subroutine cgal_polyhedron_build_aabb_tree(cgal_poly)
149 type(cgal_polyhedra_t), intent(inout) :: cgal_poly
150
153#ifdef HAVE_CGAL
154 call polyhedron_build_aabb_tree(cgal_poly%AABB_tree, cgal_poly%polyhedron)
155#endif
156
159
160
161 ! ---------------------------------------------------------
162 function cgal_polyhedron_point_inside(cgal_poly, xq, yq, zq) result(res)
163 logical :: res
164 type(cgal_polyhedra_t), intent(in) :: cgal_poly
165 real(c_double), intent(in) :: xq, yq, zq
166
167 type(d3) :: query
168
169 ! no push/pop here since this is called too frequently
170 res = .false.
171 query = d3(xq, yq, zq)
172#ifdef HAVE_CGAL
173 res = polyhedron_point_inside(cgal_poly%AABB_tree, query)
174#endif
175
177
178 ! ---------------------------------------------------------
179 subroutine cgal_polyhedron_end(cgal_poly)
180 type(cgal_polyhedra_t), intent(inout) :: cgal_poly
181
183
184#ifdef HAVE_CGAL
185 call polyhedron_finalize_aabb_tree(cgal_poly%AABB_tree)
186 call polyhedron_finalize_polyhedron(cgal_poly%polyhedron)
187#endif
188
189 pop_sub(cgal_polyhedron_end)
190 end subroutine cgal_polyhedron_end
191
192end module cgal_polyhedra_oct_m
subroutine, public cgal_polyhedron_build_aabb_tree(cgal_poly)
subroutine, public cgal_polyhedron_init(cgal_poly, fname, verbose)
logical function, public cgal_polyhedron_point_inside(cgal_poly, xq, yq, zq)
subroutine, public cgal_polyhedron_end(cgal_poly)
subroutine, public cgal_polyhedron_read(cgal_poly, fname, verbose)