Geogram  Version 1.9.1
A programming library of geometric algorithms
PCK.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_NUMERICS_PCK
41 #define GEOGRAM_NUMERICS_PCK
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/numeric.h>
45 #include <geogram/basic/logger.h>
46 #include <functional>
47 #include <algorithm>
48 #include <atomic>
49 
55 // Uncomment to get full reporting on predicate statistics
56 // (but has a non-negligible impact on performance)
57 // For instance, Early Universe Reconstruction with 2M points:
58 // with PCK_STATS: 6'36 without PCK_STATS: 3'38
59 
60 //#define PCK_STATS
61 
62 namespace GEO {
63 
64  namespace PCK {
65 
98 #ifdef PCK_STATS
99  class GEOGRAM_API PredicateStats {
100  public:
101  PredicateStats(const char* name);
102  void log_invoke() {
103  ++invoke_count_;
104  }
105  void log_exact() {
106  ++exact_count_;
107  }
108  void log_SOS() {
109  ++SOS_count_;
110  }
111  void show_stats();
112  static void show_all_stats();
113  private:
114  static PredicateStats* first_;
115  PredicateStats* next_;
116  const char* name_;
117  std::atomic<Numeric::int64> invoke_count_;
118  std::atomic<Numeric::int64> exact_count_;
119  std::atomic<Numeric::int64> SOS_count_;
120  };
121 #else
123  public:
124  PredicateStats(const char* name) {
125  geo_argused(name);
126  }
127  void log_invoke() {
128  }
129  void log_exact() {
130  }
131  void log_SOS() {
132  }
133  static void show_all_stats() {
134  Logger::out("Stats") << "Compiled without PCK_STAT (no stats)"
135  << std::endl;
136  }
137  };
138 #endif
139 
140 
145 #define SOS_result(x) [&]()->Sign { return Sign(x); }
146 
169  template <
170  class POINT, class COMPARE,
171  class FUNC1, class FUNC2, class FUNC3, class FUNC4
172  > inline Sign SOS(
173  COMPARE compare,
174  const POINT& p1, FUNC1 sos_p1,
175  const POINT& p2, FUNC2 sos_p2,
176  const POINT& p3, FUNC3 sos_p3,
177  const POINT& p4, FUNC4 sos_p4
178  ) {
179  static constexpr int N = 4;
180  Sign result = ZERO;
181  const POINT* p[N] = {&p1, &p2, &p3, &p4};
182  std::sort(
183  p, p+N,
184  [compare](const POINT* A, const POINT* B)->bool{
185  return compare(*A,*B);
186  }
187  );
188  for(int i=0; i<N; ++i) {
189  if(p[i] == &p1) {
190  result = sos_p1();
191  if(result != ZERO) {
192  return result;
193  }
194  }
195  if(p[i] == &p2) {
196  result = sos_p2();
197  if(result != ZERO) {
198  return result;
199  }
200  }
201  if(p[i] == &p3) {
202  result = sos_p3();
203  if(result != ZERO) {
204  return result;
205  }
206  }
207  if(p[i] == &p4) {
208  result = sos_p4();
209  if(result != ZERO) {
210  return result;
211  }
212  }
213  }
215  }
216 
217  }
218 }
219 
220 #endif
#define geo_assert_not_reached
Sets a non reachable point in the program.
Definition: assert.h:177
static std::ostream & out(const std::string &feature)
Gets the stream to send information messages.
Logs statistics for predicates. The statistics are displayed on exit if the command line flag "sys:st...
Definition: PCK.h:122
Common include file, providing basic definitions. Should be included before anything else by all head...
Generic logging mechanism.
void show_stats()
Displays some statistics about predicates, including the number of calls, the number of exact arithme...
Sign SOS(COMPARE compare, const POINT &p1, FUNC1 sos_p1, const POINT &p2, FUNC2 sos_p2, const POINT &p3, FUNC3 sos_p3, const POINT &p4, FUNC4 sos_p4)
template for writing symbolic perturbation in predicates
Definition: PCK.h:172
Global Vorpaline namespace.
Definition: basic.h:55
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition: argused.h:60
Sign
Integer constants that represent the sign of a value.
Definition: numeric.h:68
@ ZERO
Definition: numeric.h:72
void sort(const ITERATOR &begin, const ITERATOR &end)
Sorts elements in parallel.
Definition: algorithm.h:90
Types and functions for numbers manipulation.