1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
// Copyright (c) 1997 Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
// INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
// (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
// and Tel-Aviv University (Israel). All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; version 2.1 of the License.
// See the file LICENSE.LGPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Generator/include/CGAL/point_generators_3.h $
// $Id: point_generators_3.h 39778 2007-08-08 15:59:25Z spion $
//
//
// Author(s) : Lutz Kettner <kettner@inf.ethz.ch>
#ifndef CGAL_POINT_GENERATORS_3_H
#define CGAL_POINT_GENERATORS_3_H 1
#include <CGAL/generators.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/number_type_basic.h>
CGAL_BEGIN_NAMESPACE
template < class P, class Creator =
Creator_uniform_3<typename Kernel_traits<P>::Kernel::RT,P> >
class Random_points_in_sphere_3 : public Random_generator_base<P> {
void generate_point();
public:
typedef Random_points_in_sphere_3<P,Creator> This;
Random_points_in_sphere_3( double r = 1, Random& rnd = default_random)
// g is an input iterator creating points of type `P' uniformly
// distributed in the open sphere with radius r, i.e. |`*g'| < r .
// Three random numbers are needed from `rnd' for each point.
: Random_generator_base<P>( r, rnd) { generate_point(); }
This& operator++() {
generate_point();
return *this;
}
This operator++(int) {
This tmp = *this;
++(*this);
return tmp;
}
};
template < class P, class Creator >
void
Random_points_in_sphere_3<P,Creator>::
generate_point() {
typedef typename Creator::argument_type T;
do {
Creator creator;
this->d_item =
creator( T(this->d_range * ( 2 * this->_rnd.get_double() - 1.0)),
T(this->d_range * ( 2 * this->_rnd.get_double() - 1.0)),
T(this->d_range * ( 2 * this->_rnd.get_double() - 1.0)));
}
while (CGAL::to_double(this->d_item.x() * this->d_item.x() +
this->d_item.y() * this->d_item.y() +
this->d_item.z() * this->d_item.z()) >=
this->d_range * this->d_range);
}
template < class P, class Creator =
Creator_uniform_3<typename Kernel_traits<P>::Kernel::RT,P> >
class Random_points_on_sphere_3 : public Random_generator_base<P> {
void generate_point();
public:
typedef Random_points_on_sphere_3<P,Creator> This;
Random_points_on_sphere_3( double r = 1, Random& rnd = default_random)
// g is an input iterator creating points of type `P' uniformly
// distributed on the circle with radius r, i.e. |`*g'| == r . A
// single random number is needed from `rnd' for each point.
: Random_generator_base<P>( r, rnd) { generate_point(); }
This& operator++() {
generate_point();
return *this;
}
This operator++(int) {
This tmp = *this;
++(*this);
return tmp;
}
};
template < class P, class Creator >
void
Random_points_on_sphere_3<P,Creator>::
generate_point() {
typedef typename Creator::argument_type T;
double alpha = this->_rnd.get_double() * 2.0 * CGAL_PI;
double z = 2 * this->_rnd.get_double() - 1.0;
double r = std::sqrt( 1 - z * z);
Creator creator;
this->d_item = creator( T(this->d_range * r * std::cos(alpha)),
T(this->d_range * r * std::sin(alpha)),
T(this->d_range * z));
}
template < class P, class Creator =
Creator_uniform_3<typename Kernel_traits<P>::Kernel::RT,P> >
class Random_points_in_cube_3 : public Random_generator_base<P>{
void generate_point();
public:
typedef Random_points_in_cube_3<P,Creator> This;
Random_points_in_cube_3( double a = 1, Random& rnd = default_random)
: Random_generator_base<P>( a, rnd) { generate_point(); }
This& operator++() {
generate_point();
return *this;
}
This operator++(int) {
This tmp = *this;
++(*this);
return tmp;
}
};
template < class P, class Creator >
void
Random_points_in_cube_3<P,Creator>::
generate_point() {
typedef typename Creator::argument_type T;
Creator creator;
this->d_item =
creator( T(this->d_range * ( 2 * this->_rnd.get_double() - 1.0)),
T(this->d_range * ( 2 * this->_rnd.get_double() - 1.0)),
T(this->d_range * ( 2 * this->_rnd.get_double() - 1.0)));
}
template <class OutputIterator, class Creator>
OutputIterator
points_on_cube_grid_3( double a, std::size_t n,
OutputIterator o, Creator creator)
{
if (n == 0)
return o;
int m = int(std::ceil(
std::sqrt(std::sqrt(static_cast<double>(n)))));
while (m*m*m < int(n)) m++;
double base = -a; // Left and bottom boundary.
double step = 2*a/(m-1);
int j = 0;
int k = 0;
double px = base;
double py = base;
double pz = base;
*o++ = creator( px, py, pz);
for (std::size_t i = 1; i < n; i++) {
j++;
if ( j == m) {
k++;
if ( k == m) {
py = base;
px = base;
pz = pz + step;
k = 0;
}
else {
px = base;
py = py + step;
}
j = 0;
} else {
px = px + step;
}
*o++ = creator( px, py, pz);
}
return o;
}
template <class OutputIterator>
OutputIterator
points_on_cube_grid_3( double a, std::size_t n, OutputIterator o)
{
typedef std::iterator_traits<OutputIterator> ITraits;
typedef typename ITraits::value_type P;
return points_on_square_grid_3(a, n, o,
Creator_uniform_3<typename Kernel_traits<P>::Kernel::RT,P>());
}
CGAL_END_NAMESPACE
#endif // CGAL_POINT_GENERATORS_3_H //
// EOF //
|