Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
basic.h
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 H_HEXDOM_ALGO_BASIC_H
41 #define H_HEXDOM_ALGO_BASIC_H
42 
44 #include <geogram/basic/geometry.h>
45 #include <geogram/basic/string.h>
46 #include <cmath>
47 #include <algorithm>
48 #include <assert.h>
49 #include <time.h>
50 
51 #ifndef FOR
52 #define FOR(i,max) for (index_t i = 0; i<index_t(max); i++)
53 #endif
54 
55 namespace GEO {
56 
57  struct FF_param {
58  FF_param();
59  bool rigid_border;
60  };
61  struct HexdomParam {
62  static FF_param FF;
63  };
64 
65  template<class T> void min_equal(T& A, T B) { if (A > B) A = B; }
66  template<class T> void max_equal(T& A, T B) { if (A < B) A = B; }
67 
68  inline double nint(double x) { return floor(x + .5); }
69 
70  inline index_t next_mod(index_t i, index_t imax) {
71  return (i + 1) % imax;
72  }
73 
74  inline index_t prev_mod(index_t i, index_t imax) {
75  return (i + index_t(int(imax) - 1)) % imax;
76  }
77 
78  template <class T> T clamp(T in, T vmin, T vmax) {
79  if (in<vmin) return vmin;
80  if (in>vmax) return vmax;
81  return in;
82  }
83 
84  const index_t NOT_AN_ID = index_t(-1);
85 
86  struct EXPLORAGRAM_API IdPair : public std::pair < index_t, index_t > {
87  IdPair(index_t a = index_t(-1), index_t b = index_t(-1)) : std::pair < index_t, index_t >(a, b) {
88  }
89  };
90 
92 
93  inline void show(mat3 r) {
94  std::cerr << "\n";
95  FOR(i, 3) {
96  std::cerr << "\n";
97  FOR(j, 3) {
98  std::cerr << " " << r(i, j);
99  }
100  }
101  }
102 
103  inline vec3 col(const mat3& M, index_t j) {
104  return vec3(M(0, j), M(1, j), M(2, j));
105  }
106 
107  inline vec3 operator*(const mat3& M, const vec3& v) {
108  return vec3(
109  M(0, 0)*v[0] + M(0, 1)*v[1] + M(0, 2)*v[2],
110  M(1, 0)*v[0] + M(1, 1)*v[1] + M(1, 2)*v[2],
111  M(2, 0)*v[0] + M(2, 1)*v[1] + M(2, 2)*v[2]
112  );
113  }
114 
115  inline vec3i operator*(const mat3& M, const vec3i& v) {
116  return vec3i(
117  int(M(0, 0)*v[0] + M(0, 1)*v[1] + M(0, 2)*v[2]),
118  int(M(1, 0)*v[0] + M(1, 1)*v[1] + M(1, 2)*v[2]),
119  int(M(2, 0)*v[0] + M(2, 1)*v[1] + M(2, 2)*v[2])
120  );
121  }
122 
123  inline mat3 mat3_from_coeffs(double a00, double a01, double a02, double a10, double a11, double a12, double a20, double a21, double a22) {
124  mat3 res;
125  res(0, 0) = a00; res(0, 1) = a01; res(0, 2) = a02;
126  res(1, 0) = a10; res(1, 1) = a11; res(1, 2) = a12;
127  res(2, 0) = a20; res(2, 1) = a21; res(2, 2) = a22;
128  return res;
129  }
130 
131  inline mat3 mat3_from_coeffs(double* c) {
132  mat3 res;
133  FOR(i, 9) res.data()[i] = c[i];
134  return res;
135  }
136 
137  inline double trace(const mat3& m) { return m(0, 0) + m(1, 1) + m(2, 2); }
138 
139  inline double Frobenius_norm(const mat3& m) {return trace(m.transpose()*m);}// easy to optimmize
140 
141  inline vec2 operator*(const mat2& M, const vec2& v) {
142  return vec2(M(0, 0)*v[0] + M(0, 1)*v[1], M(1, 0)*v[0] + M(1, 1)*v[1]) ;
143  }
144 
145 
146  inline vec3i snap_to_integer(const vec3& f) {
147  return vec3i(int(round(f[0])), int(round(f[1])), int(round(f[2])));
148  }
149 
150  inline bool is_integer(double d) {
151  return d == floor(d);
152  }
153 
154 
155  // a une periode pres
156  template <class T> inline
157  T& aupp(int id, vector<T>& data){
158  while (id <0) id += int(data.size());
159  while (id >= int(data.size())) id -= int(data.size());
160  return data[id];
161  }
162 
163  // a une periode pres
164  template <class T> inline
165  T& aupp(index_t id, vector<T>& data){
166  while (id >= data.size()) id -= data.size();
167  return data[id];
168  }
169 
170 
171 
172 
174  BBox1() { min = 1e20; max = -1e20; }
175  double length() { return max - min; }
176  bool intersect(const BBox1& b) const { return contains(b.min) || contains(b.max) || b.contains(min) || b.contains(max); }
177  bool contains(const double& v) const { return v > min && v < max; }
178  bool is_null() const;
179  void add(const BBox1& b);
180  void add(const double& P) { min_equal(min, P); max_equal(max, P);}
181  void dilate(double eps) { min -= eps; max += eps; }
182  double bary() const { return (max + min) / 2.; }
183 
184  double min;
185  double max;
186  };
187 }
188 
189 
190 
191 
192 /**************** debugging and logging ***************************/
193 
194 #define reach(x){GEO::Logger::out("HexDom") <<"\n========= mark ===> "<<#x<<" =============\n"<< std::endl; }
195 
196 
197 #define check1 GEO::Logger::out("HexDom") <<"checkpoint 1 reached"<< std::endl;
198 #define check2 GEO::Logger::out("HexDom") <<"checkpoint 2 reached"<< std::endl;
199 #define check3 GEO::Logger::out("HexDom") <<"checkpoint 3 reached"<< std::endl;
200 
201 
202 std::string EXPLORAGRAM_API plop_file(const char* file, int line);
203 
204 template <class T> inline std::string plop_val(T val) {
205  return " => " + GEO::String::to_string(val);
206 }
207 
208 template <> inline std::string plop_val(const char*) {
209  return "";
210 }
211 
212 inline std::string plop_unquote(const char* str) {
213  std::string result(str);
214  if(result.length() > 2 && result[0] == '\"' && result[result.length()-1] == '\"') {
215  result = result.substr(1, result.length()-2);
216  }
217  return result;
218 }
219 
220 #define plop(x) GEO::Logger::out("HexDom") << " ->|plop|<- " << plop_file(__FILE__, __LINE__) << " : " << plop_unquote(#x) << plop_val(x) << std::endl
221 #define error(x) GEO::Logger::out("HexDom") << "ERROR " << plop_file(__FILE__, __LINE__) << " : " << plop_unquote(#x) << plop_val(x) << std::endl
222 
223 /***************** OS/multithreading *******************************/
224 
225 #ifdef GEO_OPENMP
226 #define get_thread_range(nb_tasks,istart,iend) \
227  index_t istart ; \
228  index_t iend ; \
229  {int thread_id = omp_get_thread_num(); \
230  int n_threads = omp_get_num_threads(); \
231  istart = index_t((thread_id*int(nb_tasks)) / n_threads); \
232  iend = index_t(((thread_id + 1)*int(nb_tasks)) / n_threads); \
233  if (thread_id == n_threads - 1) iend = nb_tasks; \
234  }
235 #else
236 #define get_thread_range(nb_tasks,istart,iend) \
237  index_t istart=0; \
238  index_t iend=nb_tasks;
239 #endif
240 
241 #endif
Assertion checking mechanism.
A matrix type.
Definition: matrix.h:66
Generic maths vector.
Definition: vecg.h:70
#define EXPLORAGRAM_API
Linkage declaration for exploragram symbols.
Definition: defs.h:18
Included by all headers in exploragram.
Geometric functions in 2d and 3d.
Global Vorpaline namespace.
Definition: basic.h:55
vecng< 3, Numeric::float64 > vec3
Represents points and vectors in 3d.
Definition: geometry.h:65
vecng< 3, Numeric::int32 > vec3i
Represents points and vectors in 3d with integer coordinates.
Definition: basic.h:91
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Matrix< 3, Numeric::float64 > mat3
Represents a 3x3 matrix.
Definition: geometry.h:127
vecng< 2, Numeric::float64 > vec2
Represents points and vectors in 2d.
Definition: geometry.h:59
double round(double x)
Definition: numeric.h:368
Matrix< 2, Numeric::float64 > mat2
Represents a 2x2 matrix.
Definition: geometry.h:121
Functions for string manipulation.