The Original CHomP Software
simplex.h
Go to the documentation of this file.
1
3
16
17// Copyright (C) 1997-2020 by Pawel Pilarczyk.
18//
19// This file is part of my research software package. This is free software:
20// you can redistribute it and/or modify it under the terms of the GNU
21// General Public License as published by the Free Software Foundation,
22// either version 3 of the License, or (at your option) any later version.
23//
24// This software is distributed in the hope that it will be useful,
25// but WITHOUT ANY WARRANTY; without even the implied warranty of
26// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27// GNU General Public License for more details.
28//
29// You should have received a copy of the GNU General Public License
30// along with this software; see the file "license.txt". If not,
31// please, see <https://www.gnu.org/licenses/>.
32
33// Started on March 12, 2003. Last revision: October 24, 2013.
34
35
36#ifndef _CHOMP_SIMPLICES_SIMPLEX_H_
37#define _CHOMP_SIMPLICES_SIMPLEX_H_
38
39#include "chomp/system/config.h"
43
44#include <iostream>
45#include <fstream>
46#include <cstdlib>
47
48namespace chomp {
49namespace homology {
50
51
52// classes defined within this header file (in this order)
53class Simplex;
54
56typedef gcomplex<Simplex,integer> SimplicialComplex;
57
59typedef hashedset<Simplex> SetOfSimplices;
60
63
66
69
70
71// --------------------------------------------------
72// -------------------- Simplex ---------------------
73// --------------------------------------------------
74
78{
79public:
82 static const int MaxDim = 2048;
83
85 Simplex ();
86
88 Simplex (const int *v, int dim);
89
91 Simplex (const Simplex &s, int n);
92
94 ~Simplex ();
95
97 Simplex (const Simplex &s);
98
100 Simplex &operator = (const Simplex &s);
101
104 int dim () const;
105
107 void vertices (int *table) const;
108
110 int_t hashkey1 () const;
111
113 int_t hashkey2 () const;
114
116 static const char *name ();
117
119 static const char *pluralname ();
120
121 // friends
122 friend int operator == (const Simplex &s, const Simplex &t);
123 friend std::ostream &operator << (std::ostream &out, const Simplex &s);
124
125private:
128 int *tab;
129
130}; /* class Simplex */
131
132// --------------------------------------------------
133
135{
136 tab = NULL;
137 return;
138} /* Simplex::Simplex */
139
141{
142 if (tab)
143 delete [] tab;
144 return;
145} /* Simplex::~Simplex */
146
147inline const char *simplex::name ()
148{
149 return "simplex";
150} /* simplex::name */
151
152inline const char *simplex::pluralname ()
153{
154 return "simplices";
155} /* simplex::pluralname */
156
157inline int Simplex::dim () const
158{
159 if (!tab)
160 return -1;
161 else
162 return (*tab);
163} /* Simplex::dim */
164
165inline void Simplex::vertices (int *table) const
166{
167 int d = dim ();
168 for (int i = 0; i <= d; ++ i)
169 table [i] = tab [i + 1];
170 return;
171} /* Simplex::dim */
172
173inline Simplex::Simplex (const int *v, int d)
174{
175 if (d < 0)
176 throw "Negative dimension of a simplex.";
177 tab = new int [d + 2];
178 if (!tab)
179 throw "Not enough memory for a simplex.";
180 tab [0] = d;
181 for (int i = 0; i <= d; ++ i)
182 tab [i + 1] = v [i];
183 return;
184} /* Simplex::Simplex */
185
186inline Simplex::Simplex (const Simplex &s, int n)
187{
188 int d = s. dim () - 1;
189 if (d < 0)
190 throw "Undefined boundary simplex.";
191 tab = new int [d + 2];
192 if (!tab)
193 throw "Not enough memory for a boundary simplex.";
194 tab [0] = d;
195 int i;
196 for (i = 1; i <= n; ++ i)
197 tab [i] = s. tab [i];
198 for (i = n + 1; i < d + 2; ++ i)
199 tab [i] = s. tab [i + 1];
200 return;
201} /* Simplex::Simplex */
202
203inline Simplex::Simplex (const Simplex &s)
204{
205 int d = s. dim ();
206 if (d < 0)
207 tab = NULL;
208 else
209 {
210 tab = new int [d + 2];
211 if (!tab)
212 throw "Not enough memory to copy a simplex.";
213 for (int i = 0; i < d + 2; ++ i)
214 tab [i] = s. tab [i];
215 }
216 return;
217} /* Simplex::Simplex */
218
220{
221 int d = s. dim ();
222 if (d < 0)
223 {
224 if (tab)
225 delete [] tab;
226 tab = NULL;
227 }
228 else if (d == dim ())
229 {
230 for (int i = 0; i < d + 2; ++ i)
231 tab [i] = s. tab [i];
232 }
233 else
234 {
235 if (tab)
236 delete [] tab;
237 tab = new int [d + 2];
238 if (!tab)
239 throw "Not enough memory to assign a simplex.";
240 for (int i = 0; i < d + 2; ++ i)
241 tab [i] = s. tab [i];
242 }
243 return *this;
244} /* Simplex::operator = */
245
247{
248 int d = dim ();
249 if (d < 0)
250 return 0;
251 else if (d == 0)
252 return static_cast<int_t> (tab [1]) << 2;
253 else if (d == 1)
254 {
255 return ((static_cast<int_t> (tab [1])
256 ^ 0x55555555u) << 16) ^
257 ((static_cast<int_t> (tab [2])
258 ^ 0xAAAA00AAu) << 4);
259 }
260 else
261 {
262 return ((static_cast<int_t> (tab [1]) ^
263 0x55555555u) << 16) ^
264 ((static_cast<int_t> (tab [2]) ^
265 0xAA00AAAAu) << 4) ^
266 ((static_cast<int_t> (tab [3]) ^
267 0xAA55AA55u) >> 6);
268 }
269} /* Simplex::hashkey1 */
270
271inline int_t hashkey1 (const Simplex &s)
272{
273 return s. hashkey1 ();
274} /* hashkey1 */
275
277{
278 int d = dim ();
279 if (d < 0)
280 return 0;
281 else if (d == 0)
282 return static_cast<int_t> (tab [1]) << 2;
283 else if (d == 1)
284 {
285 return ((static_cast<int_t> (tab [1]) ^
286 0xAAAAAAAAu) >> 1) ^
287 ((static_cast<int_t> (tab [2]) ^
288 0x55555555u) << 13);
289 }
290 else
291 {
292 return ((static_cast<int_t> (tab [d + 1]) ^
293 0x55555555u) << 13) ^
294 ((static_cast<int_t> (tab [d]) ^
295 0xAA00AA00u) >> 1) ^
296 ((static_cast<int_t> (tab [d - 1]) ^
297 0xAA0055AAu) << 7);
298 }
299} /* Simplex::hashkey2 */
300
301inline int_t hashkey2 (const Simplex &s)
302{
303 return s. hashkey2 ();
304} /* hashkey2 */
305
306// --------------------------------------------------
307
310inline int operator == (const Simplex &s, const Simplex &t)
311{
312 int sd = s. dim ();
313 int td = t. dim ();
314 if (sd != td)
315 return 0;
316 for (int i = 1; i < sd + 2; ++ i)
317 if (s. tab [i] != t. tab [i])
318 return 0;
319 return 1;
320} /* operator == */
321
323inline int operator != (const Simplex &s, const Simplex &t)
324{
325 return !(s == t);
326} /* operator != */
327
328// --------------------------------------------------
329
331inline int boundarylength (const Simplex &s)
332{
333 int d = s. dim ();
334 return (d ? (d + 1) : 0);
335} /* boundarylength */
336
338inline int boundarycoef (const Simplex &, int i)
339{
340 if (i & 1)
341 return -1;
342 else
343 return 1;
344} /* boundarycoef */
345
347inline Simplex boundarycell (const Simplex &s, int i)
348{
349 return Simplex (s, i);
350} /* boundarycell */
351
353inline Simplex boundarycell (const Simplex &s, int i, bool)
354{
355 return boundarycell (s, i);
356} /* boundarycell */
357
358// --------------------------------------------------
359
361inline std::ostream &operator << (std::ostream &out, const Simplex &s)
362{
363 out << '(';
364 if (s. tab)
365 {
366 int d = s. dim ();
367 out << s. tab [1];
368 for (int i = 2; i < d + 2; ++ i)
369 out << ',' << s. tab [i];
370 }
371 out << ')';
372 return out;
373} /* operator << */
374
377inline std::istream &operator >> (std::istream &in, Simplex &s)
378{
379 // check if an opening parenthesis is waiting at the input
380 ignorecomments (in);
381 int closing = closingparenthesis (in. peek ());
382 if (closing == EOF)
383 throw "Cannot read a simplex: No opening parenthesis.";
384
385 // read the opening parenthesis
386 in. get ();
387 ignorecomments (in);
388
389 // read the vertices of the simplex
390 int v [Simplex::MaxDim];
391 int dim = -1;
392 while (in && (in. peek () != closing))
393 {
394 // read the vertex
395 in >> v [++ dim];
396 if (!in)
397 throw "Unable to read a vertex of a simplex.";
398
399 // read the separating comma if any
400 ignorecomments (in);
401 if (in. peek () == ',')
402 {
403 in. get ();
404 ignorecomments (in);
405 }
406
407 // if there are too many vertices...
408 if (dim >= Simplex::MaxDim)
409 throw "Too many vertices of a simplex.";
410 }
411
412 // sort the numbers of the vertices of the simplex
413 if (sortelements (v, dim + 1) != dim + 1)
414 throw "A repeated vertex in a simplex detected.";
415
416 // read the closing parenthesis and define the simplex
417 in. get ();
418 s = Simplex (v, dim);
419
420 return in;
421} /* operator >> */
422
423
424} // namespace homology
425} // namespace chomp
426
427#endif // _CHOMP_SIMPLICES_SIMPLEX_H_
428
430
This class defines a simplex as a geometric cell that can be used as a member of a geometric complex.
Definition: simplex.h:78
static const int MaxDim
The maximal dimension of a simplex.
Definition: simplex.h:82
Simplex()
The default constructor of the empty simplex.
Definition: simplex.h:134
static const char * name()
The singular name of the objects represented by this class.
Definition: simplex.h:147
int * tab
The array that keeps the vertices of the simplex.
Definition: simplex.h:128
friend std::ostream & operator<<(std::ostream &out, const Simplex &s)
Writes a simplex to the output stream in the text format.
Definition: simplex.h:361
int dim() const
Returns the dimension of the simplex.
Definition: simplex.h:157
Simplex & operator=(const Simplex &s)
The assignment operator.
Definition: simplex.h:219
friend int operator==(const Simplex &s, const Simplex &t)
The operator == that compares whether the two simplices are the same, that is, have the same vertices...
Definition: simplex.h:310
int_t hashkey2() const
The second hashing key required by the hashing set template.
Definition: simplex.h:276
static const char * pluralname()
The plural name of the objects represented by this class.
Definition: simplex.h:152
~Simplex()
The destructor.
Definition: simplex.h:140
int_t hashkey1() const
The first hashing key required by the hashing set template.
Definition: simplex.h:246
void vertices(int *table) const
Retrieves the vertices of the simplex to the given array.
Definition: simplex.h:165
The class that defines a geometric complex - a set of cells (cubes, simplices, etc).
Definition: gcomplex.h:85
This is a template for a set of objects of the given type.
Definition: hashsets.h:185
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
This file contains a definition of a general geometric complex which represents a polyhedron.
int int_t
Index type for indexing arrays, counting cubes, etc.
Definition: config.h:115
This file defines a class "integer" which represents the ring of integers or the field of integers mo...
hashedset< simplex > SetOfSimplices
A class for representing a set of simplices.
Definition: homology.h:89
hashedset< simplex > simplices
An alternative name for a set of simplices [deprecated].
Definition: simplex.h:68
int closingparenthesis(int ch)
Returns the matching closing parenthesis for the given opening one or EOF if none.
Definition: textfile.h:411
gcomplex< simplex, integer > simplicialcomplex
A lower-case name for a simplicial complex [deprecated].
Definition: simplex.h:65
std::ostream & operator<<(std::ostream &out, const bincube< Dim, twoPower > &b)
Definition: bincube.h:907
int_t hashkey2(const hashNumber< Number > &n)
The second hashing key.
Definition: bincube.h:1036
int boundarylength(const tCellBase< coordtype > &q)
Returns the length of the boundary of a cell.
Definition: cellbase.h:501
tCellBase< coordtype > boundarycell(const tCellBase< coordtype > &q, int i, bool onlyexisting)
Computes the i-th boundary element of a cell.
Definition: cellbase.h:485
Simplex simplex
A lower-case name for a simplex [deprecated].
Definition: simplex.h:62
bool operator!=(const typename bincube< Dim, twoPower >::neighborhood_iterator &x1, const typename bincube< Dim, twoPower >::neighborhood_iterator &x2)
Definition: bincube.h:794
std::istream & operator>>(std::istream &in, bincube< Dim, twoPower > &b)
Definition: bincube.h:914
int_t hashkey1(const hashNumber< Number > &n)
The first hashing key.
Definition: bincube.h:1029
bool operator==(const typename bincube< Dim, twoPower >::neighborhood_iterator &x1, const typename bincube< Dim, twoPower >::neighborhood_iterator &x2)
Definition: bincube.h:785
void ignorecomments(std::istream &in)
Ignores white characters (spaces, tabulators, CRs and LFs), as well as comments from the input text f...
Definition: textfile.h:385
gcomplex< simplex, integer > SimplicialComplex
A class for representing a simplicial complex.
Definition: homology.h:92
int sortelements(type *tab, int n)
A simple template that sorts an array using the bubble sort method, removes repeated elements and ret...
Definition: textfile.h:340
int boundarycoef(const tCellBase< coordtype > &q, int i)
Returns the i-th coefficient in the boundary of a cell.
Definition: cellbase.h:508
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51
This file contains some useful functions related to the text input/output procedures.