3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
31 /*! \page page_module_selection_insolidangle Selection method: insolidangle
33 * This method selects a subset of particles that are located in a solid
34 * angle defined by a center and a set of points.
35 * The solid angle is constructed as a union of small cones whose axis
36 * goes through the center and a point.
37 * So there's such a cone for each position, and a
38 * point is in the solid angle if it lies within any of these cones.
39 * The width of the cones can be adjusted.
43 * The method is implemented by partitioning the surface of the unit sphere
44 * into bins using the polar coordinates \f$(\theta, \phi)\f$.
45 * The partitioning is always uniform in the zenith angle \f$\theta\f$,
46 * while the partitioning in the azimuthal angle \f$\phi\f$ varies.
47 * For each reference point, the unit vector from the center to the point
48 * is constructed, and it is stored in all the bins that overlap with the
49 * cone defined by the point.
50 * Bins that are completely covered by a single cone are marked as such.
51 * Checking whether a point is in the solid angle is then straightforward
52 * with this data structure: one finds the bin that corresponds to the point,
53 * and checks whether the bin is completely covered. If it is not, one
54 * additionally needs to check whether it is within the specified cutoff of
55 * any of the stored points.
57 * The above construction gives quite a lot of flexibility for constructing
58 * the bins without modifying the rest of the code.
59 * The current (quite inefficient) implementation is discussed below, but
60 * it should be optimized to get the most out of the code.
62 * The current way of constructing the bins constructs the boundaries
63 * statically: the bin size in the zenith direction is set to approximately
64 * half the angle cutoff, and the bins in the azimuthal direction have
65 * sizes such that the shortest edge of the bin is approximately equal to
66 * half the angle cutoff (for the regions close to the poles, a single bin
68 * Each reference point is then added to the bins as follows:
69 * -# Find the zenith angle range that is spanned by the cone centered at the
70 * point (this is simple addition/subtraction).
71 * -# Calculate the maximal span of the cone in the azimuthal direction using
73 * \f[\sin \Delta \phi_{max} = \frac{\sin \alpha}{\sin \theta}\f]
74 * (a sine formula in spherical coordinates),
75 * where \f$\alpha\f$ is the width of the cone and \f$\theta\f$ is the
76 * zenith angle of the cone center.
77 * Similarly, the zenith angle at which this extent is achieved is
79 * \f[\cos \theta_{max} = \frac{\cos \theta}{\cos \alpha}\f]
80 * (Pythagoras's theorem in spherical coordinates).
81 * -# For each zenith angle bin that is at least partially covered by the
82 * cone, calculate the span of the cone at the edges using
83 * \f[\sin^2 \frac{\Delta \phi}{2} = \frac{\sin^2 \frac{\alpha}{2} - \sin^2 \frac{\theta - \theta'}{2}}{\sin \theta \sin \theta'}\f]
84 * (distance in spherical geometry),
85 * where \f$\theta'\f$ is the zenith angle of the bin edge.
86 * Treat zenith angle bins that are completely covered by the cone (in the
87 * case that the cone is centered close to the pole) as a special case.
88 * -# Using the values calculated above, loop through the azimuthal bins that
89 * are partially or completely covered by the cone and update them.
91 * The total solid angle (for covered fraction calculations) is estimated by
92 * taking the total area of completely covered bins plus
93 * half the area of partially covered bins.
94 * The second one is an approximation, but should give reasonable estimates
95 * for the averages as well as in cases where the bin size is small.
99 * Implements the \ref sm_insolidangle "insolidangle" selection method.
102 * The implementation could be optimized quite a bit.
105 * Move the covered fraction stuff somewhere else and make it more generic
106 * (along the lines it is handled in selection.h and trajana.h in the old C
109 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
110 * \ingroup module_selection
116 #include "gromacs/legacyheaders/macros.h"
117 #include "gromacs/legacyheaders/maths.h"
118 #include "gromacs/legacyheaders/pbc.h"
119 #include "gromacs/legacyheaders/physics.h"
120 #include "gromacs/legacyheaders/smalloc.h"
121 #include "gromacs/legacyheaders/vec.h"
123 #include "gromacs/selection/indexutil.h"
124 #include "gromacs/selection/position.h"
125 #include "gromacs/selection/selection.h"
126 #include "gromacs/selection/selmethod.h"
127 #include "gromacs/utility/exceptions.h"
135 * Internal data structure for the \p insolidangle selection method.
137 * \see \c t_partition
141 /** Left edge of the partition. */
143 /** Bin index corresponding to this partition. */
148 * Internal data structure for the \p insolidangle selection method.
150 * Describes the surface partitioning within one slice along the zenith angle.
151 * The slice from azimuthal angle \p p[i].left to \p p[i+1].left belongs to
156 /** Number of partition items (\p p contains \p n+1 items). */
158 /** Array of partition edges and corresponding bins. */
163 * Internal data structure for the \p insolidangle selection method.
165 * Contains the reference points that partially cover a certain region on the
166 * surface of the unit sphere.
167 * If \p n is -1, the whole region described by the bin is covered.
171 /** Number of points in the array \p x, -1 if whole bin covered. */
173 /** Number of elements allocated for \p x. */
175 /** Array of points that partially cover the bin. */
177 } t_spheresurfacebin;
180 * Data structure for the \p insolidangle selection method.
182 * All angle values are in the units of radians.
186 /** Center of the solid angle. */
187 gmx_ana_pos_t center;
188 /** Positions that span the solid angle. */
192 /** Estimate of the covered fraction. */
195 /** Cutoff for the cosine (equals cos(angcut)). */
197 /** Bin size to be used as the target bin size when constructing the bins. */
200 /** Number of bins in the \p tbin array. */
202 /** Size of one bin in the zenith angle direction. */
204 /** Array of zenith angle slices. */
206 /** Number of elements allocated for the \p bin array. */
208 /** Number of elements used in the \p bin array. */
210 /** Array of individual bins. */
211 t_spheresurfacebin *bin;
212 } t_methoddata_insolidangle;
214 /** Allocates data for the \p insolidangle selection method. */
216 init_data_insolidangle(int npar, gmx_ana_selparam_t *param);
217 /** Initializes the \p insolidangle selection method. */
219 init_insolidangle(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
220 /** Frees the data allocated for the \p insolidangle selection method. */
222 free_data_insolidangle(void *data);
223 /** Initializes the evaluation of the \p insolidangle selection method for a frame. */
225 init_frame_insolidangle(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data);
226 /** Internal helper function for evaluate_insolidangle(). */
228 accept_insolidangle(rvec x, t_pbc *pbc, void *data);
229 /** Evaluates the \p insolidangle selection method. */
231 evaluate_insolidangle(t_topology *top, t_trxframe *fr, t_pbc *pbc,
232 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
234 /** Calculates the distance between unit vectors. */
236 sph_distc(rvec x1, rvec x2);
237 /** Does a binary search on a \p t_partition to find a bin for a value. */
239 find_partition_bin(t_partition *p, real value);
240 /** Finds a bin that corresponds to a location on the unit sphere surface. */
242 find_surface_bin(t_methoddata_insolidangle *surf, rvec x);
243 /** Clears/initializes the bins on the unit sphere surface. */
245 clear_surface_points(t_methoddata_insolidangle *surf);
246 /** Frees memory allocated for storing the reference points in the surface bins. */
248 free_surface_points(t_methoddata_insolidangle *surf);
249 /** Adds a reference point to a given bin. */
251 add_surface_point(t_methoddata_insolidangle *surf, int tbin, int pbin, rvec x);
252 /** Marks a bin as completely covered. */
254 mark_surface_covered(t_methoddata_insolidangle *surf, int tbin, int pbin);
255 /** Helper function for store_surface_point() to update a single zenith angle bin. */
257 update_surface_bin(t_methoddata_insolidangle *surf, int tbin,
258 real phi, real pdelta1, real pdelta2, real pdeltamax,
260 /** Adds a single reference point and updates the surface bins. */
262 store_surface_point(t_methoddata_insolidangle *surf, rvec x);
263 /** Optimizes the surface bins for faster searching. */
265 optimize_surface_points(t_methoddata_insolidangle *surf);
266 /** Estimates the area covered by the reference cones. */
268 estimate_covered_fraction(t_methoddata_insolidangle *surf);
269 /** Checks whether a point lies within a solid angle. */
271 is_surface_covered(t_methoddata_insolidangle *surf, rvec x);
273 /** Parameters for the \p insolidangle selection method. */
274 static gmx_ana_selparam_t smparams_insolidangle[] = {
275 {"center", {POS_VALUE, 1, {NULL}}, NULL, SPAR_DYNAMIC},
276 {"span", {POS_VALUE, -1, {NULL}}, NULL, SPAR_DYNAMIC | SPAR_VARNUM},
277 {"cutoff", {REAL_VALUE, 1, {NULL}}, NULL, SPAR_OPTIONAL},
280 /** Help text for the \p insolidangle selection method. */
281 static const char *help_insolidangle[] = {
282 "SELECTING ATOMS IN A SOLID ANGLE[PAR]",
284 "[TT]insolidangle center POS span POS_EXPR [cutoff REAL][tt][PAR]",
286 "This keyword selects atoms that are within [TT]REAL[tt] degrees",
287 "(default=5) of any position in [TT]POS_EXPR[tt] as seen from [TT]POS[tt]",
288 "a position expression that evaluates to a single position), i.e., atoms",
289 "in the solid angle spanned by the positions in [TT]POS_EXPR[tt] and",
290 "centered at [TT]POS[tt].[PAR]"
292 "Technically, the solid angle is constructed as a union of small cones",
293 "whose tip is at [TT]POS[tt] and the axis goes through a point in",
294 "[TT]POS_EXPR[tt]. There is such a cone for each position in",
295 "[TT]POS_EXPR[tt], and point is in the solid angle if it lies within any",
296 "of these cones. The cutoff determines the width of the cones.",
299 /** \internal Selection method data for the \p insolidangle method. */
300 gmx_ana_selmethod_t sm_insolidangle = {
301 "insolidangle", GROUP_VALUE, SMETH_DYNAMIC,
302 asize(smparams_insolidangle), smparams_insolidangle,
303 &init_data_insolidangle,
307 &free_data_insolidangle,
308 &init_frame_insolidangle,
310 &evaluate_insolidangle,
311 {"insolidangle center POS span POS_EXPR [cutoff REAL]",
312 asize(help_insolidangle), help_insolidangle},
316 * \param[in] npar Not used (should be 3).
317 * \param[in,out] param Method parameters (should point to
318 * \ref smparams_insolidangle).
319 * \returns Pointer to the allocated data (\ref t_methoddata_insolidangle).
321 * Allocates memory for a \ref t_methoddata_insolidangle structure and
322 * initializes the parameter as follows:
323 * - \p center defines the value for t_methoddata_insolidangle::center.
324 * - \p span defines the value for t_methoddata_insolidangle::span.
325 * - \p cutoff defines the value for t_methoddata_insolidangle::angcut.
328 init_data_insolidangle(int npar, gmx_ana_selparam_t *param)
330 t_methoddata_insolidangle *data;
334 param[0].val.u.p = &data->center;
335 param[1].val.u.p = &data->span;
336 param[2].val.u.r = &data->angcut;
341 * \param top Not used.
342 * \param npar Not used.
343 * \param param Not used.
344 * \param data Pointer to \ref t_methoddata_insolidangle to initialize.
345 * \returns 0 on success, -1 on failure.
347 * Converts t_methoddata_insolidangle::angcut to radians and allocates
348 * and allocates memory for the bins used during the evaluation.
351 init_insolidangle(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
353 t_methoddata_insolidangle *surf = (t_methoddata_insolidangle *)data;
356 if (surf->angcut <= 0)
358 GMX_THROW(gmx::InvalidInputError("Angle cutoff should be > 0"));
361 surf->angcut *= DEG2RAD;
363 surf->distccut = -cos(surf->angcut);
364 surf->targetbinsize = surf->angcut / 2;
365 surf->ntbins = static_cast<int>(M_PI / surf->targetbinsize);
366 surf->tbinsize = (180.0 / surf->ntbins)*DEG2RAD;
368 snew(surf->tbin, static_cast<int>(M_PI / surf->tbinsize) + 1);
370 for (i = 0; i < surf->ntbins; ++i)
372 c = static_cast<int>(max(sin(surf->tbinsize*i),
373 sin(surf->tbinsize*(i+1)))
374 * M_2PI / surf->targetbinsize) + 1;
375 snew(surf->tbin[i].p, c+1);
379 snew(surf->bin, surf->maxbins);
383 * \param data Data to free (should point to a \ref t_methoddata_insolidangle).
385 * Frees the memory allocated for \c t_methoddata_insolidangle::center and
386 * \c t_methoddata_insolidangle::span, as well as the memory for the internal
390 free_data_insolidangle(void *data)
392 t_methoddata_insolidangle *d = (t_methoddata_insolidangle *)data;
397 for (i = 0; i < d->ntbins; ++i)
403 free_surface_points(d);
409 * \param[in] top Not used.
410 * \param[in] fr Current frame.
411 * \param[in] pbc PBC structure.
412 * \param data Should point to a \ref t_methoddata_insolidangle.
414 * Creates a lookup structure that enables fast queries of whether a point
415 * is within the solid angle or not.
418 init_frame_insolidangle(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
420 t_methoddata_insolidangle *d = (t_methoddata_insolidangle *)data;
424 free_surface_points(d);
425 clear_surface_points(d);
426 for (i = 0; i < d->span.nr; ++i)
430 pbc_dx(pbc, d->span.x[i], d->center.x[0], dx);
434 rvec_sub(d->span.x[i], d->center.x[0], dx);
437 store_surface_point(d, dx);
439 optimize_surface_points(d);
444 * \param[in] x Test point.
445 * \param[in] pbc PBC data (if NULL, no PBC are used).
446 * \param[in] data Pointer to a \c t_methoddata_insolidangle data structure.
447 * \returns true if \p x is within the solid angle, false otherwise.
450 accept_insolidangle(rvec x, t_pbc *pbc, void *data)
452 t_methoddata_insolidangle *d = (t_methoddata_insolidangle *)data;
457 pbc_dx(pbc, x, d->center.x[0], dx);
461 rvec_sub(x, d->center.x[0], dx);
464 return is_surface_covered(d, dx);
468 * See sel_updatefunc() for description of the parameters.
469 * \p data should point to a \c t_methoddata_insolidangle.
471 * Calculates which atoms in \p g are within the solid angle spanned by
472 * \c t_methoddata_insolidangle::span and centered at
473 * \c t_methoddata_insolidangle::center, and stores the result in \p out->u.g.
476 evaluate_insolidangle(t_topology *top, t_trxframe *fr, t_pbc *pbc,
477 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data)
482 for (b = 0; b < pos->nr; ++b)
484 if (accept_insolidangle(pos->x[b], pbc, data))
486 gmx_ana_pos_append(NULL, out->u.g, pos, b, 0);
492 * \param[in] sel Selection element to query.
493 * \returns true if the covered fraction can be estimated for \p sel with
494 * _gmx_selelem_estimate_coverfrac(), false otherwise.
497 _gmx_selelem_can_estimate_cover(const gmx::SelectionTreeElement &sel)
499 if (sel.type == SEL_BOOLEAN && sel.u.boolt == BOOL_OR)
504 bool bDynFound = false;
505 gmx::SelectionTreeElementPointer child = sel.child;
508 if (child->type == SEL_EXPRESSION)
510 if (child->u.expr.method->name == sm_insolidangle.name)
512 if (bFound || bDynFound)
518 else if (child->u.expr.method
519 && (child->u.expr.method->flags & SMETH_DYNAMIC))
528 else if (!_gmx_selelem_can_estimate_cover(*child))
538 * \param[in] sel Selection for which the fraction should be calculated.
539 * \returns Fraction of angles covered by the selection (between zero and one).
541 * The return value is undefined if _gmx_selelem_can_estimate_cover() returns
543 * Should be called after gmx_ana_evaluate_selections() has been called for the
547 _gmx_selelem_estimate_coverfrac(const gmx::SelectionTreeElement &sel)
551 if (sel.type == SEL_EXPRESSION && sel.u.expr.method->name == sm_insolidangle.name)
553 t_methoddata_insolidangle *d = (t_methoddata_insolidangle *)sel.u.expr.mdata;
556 d->cfrac = estimate_covered_fraction(d);
560 if (sel.type == SEL_BOOLEAN && sel.u.boolt == BOOL_NOT)
562 cfrac = _gmx_selelem_estimate_coverfrac(*sel.child);
570 /* Here, we assume that the selection is simple enough */
571 gmx::SelectionTreeElementPointer child = sel.child;
574 cfrac = _gmx_selelem_estimate_coverfrac(*child);
585 * \param[in] x1 Unit vector 1.
586 * \param[in] x2 Unit vector 2.
587 * \returns Minus the dot product of \p x1 and \p x2.
589 * This function is used internally to calculate the distance between the
590 * unit vectors \p x1 and \p x2 to find out whether \p x2 is within the
591 * cone centered at \p x1. Currently, the cosine of the angle is used
592 * for efficiency, and the minus is there to make it behave like a normal
593 * distance (larger values mean longer distances).
596 sph_distc(rvec x1, rvec x2)
598 return -iprod(x1, x2);
602 * \param[in] p Partition to search.
603 * \param[in] value Value to search for.
604 * \returns The partition index in \p p that contains \p value.
606 * If \p value is outside the range of \p p, the first/last index is returned.
607 * Otherwise, the return value \c i satisfies \c p->p[i].left<=value and
608 * \c p->p[i+1].left>value
611 find_partition_bin(t_partition *p, real value)
613 int pmin, pmax, pbin;
615 /* Binary search the partition */
616 pmin = 0; pmax = p->n;
617 while (pmax > pmin + 1)
619 pbin = pmin + (pmax - pmin) / 2;
620 if (p->p[pbin].left <= value)
634 * \param[in] surf Surface data structure to search.
635 * \param[in] x Unit vector to find.
636 * \returns The bin index that contains \p x.
638 * The return value is an index to the \p surf->bin array.
641 find_surface_bin(t_methoddata_insolidangle *surf, rvec x)
647 phi = atan2(x[YY], x[XX]);
648 tbin = static_cast<int>(floor(theta / surf->tbinsize));
649 if (tbin >= surf->ntbins)
651 tbin = surf->ntbins - 1;
653 pbin = find_partition_bin(&surf->tbin[tbin], phi);
654 return surf->tbin[tbin].p[pbin].bin;
658 * \param[in,out] surf Surface data structure.
660 * Clears the reference points from the bins and (re)initializes the edges
661 * of the azimuthal bins.
664 clear_surface_points(t_methoddata_insolidangle *surf)
669 for (i = 0; i < surf->ntbins; ++i)
671 c = static_cast<int>(min(sin(surf->tbinsize*i),
672 sin(surf->tbinsize*(i+1)))
673 * M_2PI / surf->targetbinsize) + 1;
679 for (j = 0; j < c; ++j)
681 surf->tbin[i].p[j].left = -M_PI + j*M_2PI/c - 0.0001;
682 surf->tbin[i].p[j].bin = surf->nbins;
683 surf->bin[surf->nbins].n = 0;
686 surf->tbin[i].p[c].left = M_PI + 0.0001;
687 surf->tbin[i].p[c].bin = -1;
692 * \param[in,out] surf Surface data structure.
695 free_surface_points(t_methoddata_insolidangle *surf)
699 for (i = 0; i < surf->nbins; ++i)
703 sfree(surf->bin[i].x);
705 surf->bin[i].n_alloc = 0;
706 surf->bin[i].x = NULL;
711 * \param[in,out] surf Surface data structure.
712 * \param[in] tbin Bin number in the zenith angle direction.
713 * \param[in] pbin Bin number in the azimuthal angle direction.
714 * \param[in] x Point to store.
717 add_surface_point(t_methoddata_insolidangle *surf, int tbin, int pbin, rvec x)
721 bin = surf->tbin[tbin].p[pbin].bin;
722 /* Return if bin is already completely covered */
723 if (surf->bin[bin].n == -1)
725 /* Allocate more space if necessary */
726 if (surf->bin[bin].n == surf->bin[bin].n_alloc) {
727 surf->bin[bin].n_alloc += 10;
728 srenew(surf->bin[bin].x, surf->bin[bin].n_alloc);
730 /* Add the point to the bin */
731 copy_rvec(x, surf->bin[bin].x[surf->bin[bin].n]);
736 * \param[in,out] surf Surface data structure.
737 * \param[in] tbin Bin number in the zenith angle direction.
738 * \param[in] pbin Bin number in the azimuthal angle direction.
741 mark_surface_covered(t_methoddata_insolidangle *surf, int tbin, int pbin)
745 bin = surf->tbin[tbin].p[pbin].bin;
746 surf->bin[bin].n = -1;
750 * \param[in,out] surf Surface data structure.
751 * \param[in] tbin Bin number in the zenith angle direction.
752 * \param[in] phi Azimuthal angle of \p x.
753 * \param[in] pdelta1 Width of the cone at the lower edge of \p tbin.
754 * \param[in] pdelta2 Width of the cone at the uppper edge of \p tbin.
755 * \param[in] pdeltamax Max. width of the cone inside \p tbin.
756 * \param[in] x Point to store (should have unit length).
759 update_surface_bin(t_methoddata_insolidangle *surf, int tbin,
760 real phi, real pdelta1, real pdelta2, real pdeltamax,
763 real pdelta, phi1, phi2;
764 int pbin1, pbin2, pbiniter, pbin;
766 /* Find the edges of the bins affected */
767 pdelta = max(max(pdelta1, pdelta2), pdeltamax);
771 pbin = find_partition_bin(&surf->tbin[tbin], phi1);
776 pbin = find_partition_bin(&surf->tbin[tbin], phi1 + M_2PI);
777 pbin1 = pbin - surf->tbin[tbin].n;
782 pbin2 = find_partition_bin(&surf->tbin[tbin], phi2);
786 pbin2 = find_partition_bin(&surf->tbin[tbin], phi2 - M_2PI);
787 pbin2 += surf->tbin[tbin].n;
790 if (pbin2 - pbin1 > surf->tbin[tbin].n)
792 pbin2 = pbin1 + surf->tbin[tbin].n;
794 /* Find the edges of completely covered region */
795 pdelta = min(pdelta1, pdelta2);
802 /* Loop over all affected bins */
803 for (pbiniter = pbin1; pbiniter != pbin2; ++pbiniter, ++pbin)
805 /* Wrap bin around if end reached */
806 if (pbin == surf->tbin[tbin].n)
812 /* Check if bin is completely covered and update */
813 if (surf->tbin[tbin].p[pbin].left >= phi1
814 && surf->tbin[tbin].p[pbin+1].left <= phi2)
816 mark_surface_covered(surf, tbin, pbin);
820 add_surface_point(surf, tbin, pbin, x);
826 * \param[in,out] surf Surface data structure.
827 * \param[in] x Point to store (should have unit length).
829 * Finds all the bins covered by the cone centered at \p x and calls
830 * update_surface_bin() to update them.
833 store_surface_point(t_methoddata_insolidangle *surf, rvec x)
836 real pdeltamax, tmax;
837 real theta1, theta2, pdelta1, pdelta2;
841 phi = atan2(x[YY], x[XX]);
842 /* Find the maximum extent in the phi direction */
843 if (theta <= surf->angcut)
848 else if (theta >= M_PI - surf->angcut)
855 pdeltamax = asin(sin(surf->angcut) / sin(theta));
856 tmax = acos(cos(theta) / cos(surf->angcut));
858 /* Find the first affected bin */
859 tbin = max(static_cast<int>(floor((theta - surf->angcut) / surf->tbinsize)), 0);
860 theta1 = tbin * surf->tbinsize;
861 if (theta1 < theta - surf->angcut)
869 /* Loop through all affected bins */
870 while (tbin < ceil((theta + surf->angcut) / surf->tbinsize)
871 && tbin < surf->ntbins)
873 /* Calculate the next boundaries */
874 theta2 = (tbin+1) * surf->tbinsize;
875 if (theta2 > theta + surf->angcut)
877 /* The circle is completely outside the cone */
880 else if (theta2 <= -(theta - surf->angcut)
881 || theta2 >= M_2PI - (theta + surf->angcut)
882 || tbin == surf->ntbins - 1)
884 /* The circle is completely inside the cone, or we are in the
885 * 360 degree bin covering the pole. */
890 /* TODO: This formula is numerically unstable if theta is very
891 * close to the pole. In practice, it probably does not matter
892 * much, but it would be nicer to adjust the theta bin boundaries
893 * such that the case above catches this instead of falling through
895 pdelta2 = 2*asin(sqrt(
896 (sqr(sin(surf->angcut/2)) - sqr(sin((theta2-theta)/2))) /
897 (sin(theta) * sin(theta2))));
900 if (tmax >= theta1 && tmax <= theta2)
902 update_surface_bin(surf, tbin, phi, pdelta1, pdelta2, pdeltamax, x);
906 update_surface_bin(surf, tbin, phi, pdelta1, pdelta2, 0, x);
916 * \param[in,out] surf Surface data structure.
918 * Currently, this function does nothing.
921 optimize_surface_points(t_methoddata_insolidangle *surf)
923 /* TODO: Implement */
927 * \param[in] surf Surface data structure.
928 * \returns An estimate for the area covered by the reference points.
931 estimate_covered_fraction(t_methoddata_insolidangle *surf)
934 real cfrac, tfrac, pfrac;
937 for (t = 0; t < surf->ntbins; ++t)
939 tfrac = cos(t * surf->tbinsize) - cos((t+1) * surf->tbinsize);
940 for (p = 0; p < surf->tbin[t].n; ++p)
942 pfrac = surf->tbin[t].p[p+1].left - surf->tbin[t].p[p].left;
943 n = surf->bin[surf->tbin[t].p[p].bin].n;
944 if (n == -1) /* Bin completely covered */
946 cfrac += tfrac * pfrac;
948 else if (n > 0) /* Bin partially covered */
950 cfrac += tfrac * pfrac / 2; /* A rough estimate */
954 return cfrac / (4*M_PI);
958 * \param[in] surf Surface data structure to search.
959 * \param[in] x Unit vector to check.
960 * \returns true if \p x is within the solid angle, false otherwise.
963 is_surface_covered(t_methoddata_insolidangle *surf, rvec x)
967 bin = find_surface_bin(surf, x);
968 /* Check for completely covered bin */
969 if (surf->bin[bin].n == -1)
973 /* Check each point that partially covers the bin */
974 for (i = 0; i < surf->bin[bin].n; ++i)
976 if (sph_distc(x, surf->bin[bin].x[i]) < surf->distccut)