Commit b2350837 authored by Jonathan Lambrechts's avatar Jonathan Lambrechts
Browse files

no std::

parent 02f5c70c
......@@ -23,5 +23,6 @@ project(seamsh C CXX)
add_library(seamsh SHARED
seamsh.c
polymesh.cc
sort.c
robustPredicates.cpp
)
......@@ -5,10 +5,9 @@
#include "vector.h"
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <limits>
#include <cfloat>
#include <cstdio>
#include "robustPredicates.h"
typedef struct HalfEdgeStruct HalfEdge;
......@@ -388,7 +387,7 @@ public:
inline int split_triangle(int index, double x, double y, Face *f,
int (*doSwap)(HalfEdge *, void *) = NULL,
void *data = NULL,
std::vector<HalfEdge *> *_t = NULL)
HalfEdge ***_t = NULL)
{
Vertex *v = vertex_new(x, y, -1); // one more vertex
*vector_push(&vertices) = v;
......@@ -436,15 +435,15 @@ public:
createFace(f2, v2, v0, v, he2, he0v, hev2);
if(doSwap) {
std::stack<HalfEdge *> _stack;
_stack.push(he0);
_stack.push(he1);
_stack.push(he2);
std::vector<HalfEdge *> _touched;
while(!_stack.empty()) {
HalfEdge *he = _stack.top();
_touched.push_back(he);
_stack.pop();
HalfEdge **_stack = NULL;
*vector_push(&_stack) = he0;
*vector_push(&_stack) = he1;
*vector_push(&_stack) = he2;
HalfEdge **touched = NULL;
while(vector_size(_stack) != 0) {
HalfEdge *he = _stack[vector_size(_stack)-1];
vector_pop(_stack);
*vector_push(&touched) = he;
// printf("do we swap %g %g --> %g %g ?\n",
// he->v->position.x(),he->v->position.y(),
// he->next->v->position.x(),he->next->v->position.y());
......@@ -458,39 +457,49 @@ public:
if(H[k] == NULL) continue;
HalfEdge *heb = H[k]->next;
HalfEdge *hebo = heb->opposite;
if(std::find(_touched.begin(), _touched.end(), heb) ==
_touched.end() &&
std::find(_touched.begin(), _touched.end(), hebo) ==
_touched.end()) {
_stack.push(heb);
}
HalfEdge *hec = heb->next;
HalfEdge *heco = hec->opposite;
if(std::find(_touched.begin(), _touched.end(), hec) ==
_touched.end() &&
std::find(_touched.begin(), _touched.end(), heco) ==
_touched.end()) {
_stack.push(hec);
int found_b_bo = 0;
int found_c_co = 0;
for (int i = 0; i < vector_size(touched); ++i) {
if (touched[i] == heb || touched[i] == hebo)
found_b_bo = 1;
if (touched[i] == hec || touched[i] == heco)
found_c_co = 1;
}
if(found_b_bo == 0) {
*vector_push(&_stack) = heb;
}
if (found_c_co == 0) {
*vector_push(&_stack) = hec;
}
}
}
}
if(_t) *_t = _touched;
if(_t) *_t = touched;
else vector_free(touched);
vector_free(_stack);
}
return 0;
}
};
inline Vertex *ptrmin(Vertex *p1, Vertex *p2) {
return p1 < p2 ? p1 : p2;
}
inline Vertex *ptrmax(Vertex *p1, Vertex *p2) {
return p1 > p2 ? p1 : p2;
}
struct HalfEdgePtrLessThan {
bool operator()(HalfEdge *l1, HalfEdge *l2) const
{
Vertex *l10 = std::min(l1->v, l1->next->v);
Vertex *l11 = std::max(l1->v, l1->next->v);
Vertex *l20 = std::min(l2->v, l2->next->v);
Vertex *l21 = std::max(l2->v, l2->next->v);
Vertex *l10 = ptrmin(l1->v, l1->next->v);
Vertex *l11 = ptrmax(l1->v, l1->next->v);
Vertex *l20 = ptrmin(l2->v, l2->next->v);
Vertex *l21 = ptrmax(l2->v, l2->next->v);
if(l10 < l20) return true;
if(l10 > l20) return false;
if(l11 > l21) return true;
......@@ -501,10 +510,10 @@ struct HalfEdgePtrLessThan {
struct HalfEdgePtrEqual {
bool operator()(HalfEdge *l1, HalfEdge *l2) const
{
Vertex *l10 = std::min(l1->v, l1->next->v);
Vertex *l11 = std::max(l1->v, l1->next->v);
Vertex *l20 = std::min(l2->v, l2->next->v);
Vertex *l21 = std::max(l2->v, l2->next->v);
Vertex *l10 = ptrmin(l1->v, l1->next->v);
Vertex *l11 = ptrmax(l1->v, l1->next->v);
Vertex *l20 = ptrmin(l2->v, l2->next->v);
Vertex *l21 = ptrmax(l2->v, l2->next->v);
if(l10 == l20 && l11 == l21) return true;
return false;
}
......@@ -650,12 +659,12 @@ void plymesh_delete(PolyMesh *pm) {
}
static void get_bounding_box(int n, double *x, double bbmin[2], double bbmax[2]) {
bbmin[0] = bbmin[1] = std::numeric_limits<double>::max();
bbmax[0] = bbmax[1] = -std::numeric_limits<double>::max();
bbmin[0] = bbmin[1] = DBL_MAX;
bbmax[0] = bbmax[1] = -DBL_MAX;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2; ++j) {
bbmin[j] = std::min(x[i*2+j], bbmin[j]);
bbmax[j] = std::max(x[i*2+j], bbmax[j]);
bbmin[j] = fmin(x[i*2+j], bbmin[j]);
bbmax[j] = fmax(x[i*2+j], bbmax[j]);
}
}
for (int j = 0; j < 3; ++j) {
......@@ -674,9 +683,17 @@ PolyMesh *polymesh_new(double xmin[2], double xmax[2]) {
}
int size_t_cmp(const void *p0, const void *p1, void *pdata) {
size_t *HC = (size_t*)pdata;
size_t i0 = *(size_t*)p0;
size_t i1 = *(size_t*)p1;
return HC[i0] > HC[i1] ? 1 : -1;
}
void polymesh_add_points(PolyMesh *pm, int n, double *x, int *tags)
{
std::vector<size_t> HC(n), IND(n);
size_t *HC = (size_t*)malloc(sizeof(size_t)*n);
size_t *IND = (size_t*)malloc(sizeof(size_t)*n);
Face *f = pm->faces[0];
double bbmin[2], bbmax[2];
get_bounding_box(n, x, bbmin, bbmax);
......@@ -687,13 +704,13 @@ void polymesh_add_points(PolyMesh *pm, int n, double *x, int *tags)
bbmax[1] - bbcenter[1]);
IND[i] = i;
}
std::sort(IND.begin(), IND.end(),
[&](size_t i, size_t j) { return HC[i] < HC[j]; });
quicksort(IND, n, sizeof(size_t), size_t_cmp, HC);
for(size_t i = 0; i < n; i++) {
size_t I = IND[i];
f = Walk(f, x[I*2], x[I*2+1]);
pm->split_triangle(i, x[I*2], x[I*2+1], f, delaunayEdgeCriterionPlaneIsotropic, nullptr);
pm->split_triangle(i, x[I*2], x[I*2+1], f, delaunayEdgeCriterionPlaneIsotropic, NULL);
pm->vertices[vector_size(pm->vertices) - 1]->data = tags[I];
}
free(HC);
free(IND);
}
/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
The GNU C Library is free software; 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; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
/* If you consider tuning this algorithm, you should consult first:
Engineering a sort function; Jon Bentley and M. Douglas McIlroy;
Software - Practice and Experience; Vol. 23 (11), 1249-1265, 1993. */
#include <alloca.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
/* Byte-wise swap two items of size SIZE. */
#define SWAP(a, b, size) \
do \
{ \
size_t __size = (size); \
char *__a = (a), *__b = (b); \
do \
{ \
char __tmp = *__a; \
*__a++ = *__b; \
*__b++ = __tmp; \
} while (--__size > 0); \
} while (0)
/* Discontinue quicksort algorithm when partition gets below this size.
This particular magic number was chosen to work best on a Sun 4/260. */
#define MAX_THRESH 4
/* Stack node declarations used to store unfulfilled partition obligations. */
typedef struct
{
char *lo;
char *hi;
} stack_node;
/* The next 4 #defines implement a very fast in-line stack abstraction. */
/* The stack needs log (total_elements) entries (we could even subtract
log(MAX_THRESH)). Since total_elements has type size_t, we get as
upper bound for log (total_elements):
bits per byte (CHAR_BIT) * sizeof(size_t). */
#define STACK_SIZE (CHAR_BIT * sizeof (size_t))
#define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
#define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi)))
#define STACK_NOT_EMPTY (stack < top)
/* Order size using quicksort. This implementation incorporates
four optimizations discussed in Sedgewick:
1. Non-recursive, using an explicit stack of pointer that store the
next array partition to sort. To save time, this maximum amount
of space required to store an array of SIZE_MAX is allocated on the
stack. Assuming a 32-bit (64 bit) integer for size_t, this needs
only 32 * sizeof(stack_node) == 256 bytes (for 64 bit: 1024 bytes).
Pretty cheap, actually.
2. Chose the pivot element using a median-of-three decision tree.
This reduces the probability of selecting a bad pivot value and
eliminates certain extraneous comparisons.
3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
insertion sort to order the MAX_THRESH items within each partition.
This is a big win, since insertion sort is faster for small, mostly
sorted array segments.
4. The larger of the two sub-partitions is always pushed onto the
stack first, with the algorithm then concentrating on the
smaller partition. This *guarantees* no more than log (total_elems)
stack size is needed (actually O(1) in this case)! */
void quicksort (void *const pbase, size_t total_elems, size_t size,
int (*cmp)(const void*, const void*, void*), void *arg)
{
char *base_ptr = (char *) pbase;
const size_t max_thresh = MAX_THRESH * size;
if (total_elems == 0)
/* Avoid lossage with unsigned arithmetic below. */
return;
if (total_elems > MAX_THRESH)
{
char *lo = base_ptr;
char *hi = &lo[size * (total_elems - 1)];
stack_node stack[STACK_SIZE];
stack_node *top = stack;
PUSH (NULL, NULL);
while (STACK_NOT_EMPTY)
{
char *left_ptr;
char *right_ptr;
/* Select median value from among LO, MID, and HI. Rearrange
LO and HI so the three values are sorted. This lowers the
probability of picking a pathological pivot value and
skips a comparison for both the LEFT_PTR and RIGHT_PTR in
the while loops. */
char *mid = lo + size * ((hi - lo) / size >> 1);
if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
SWAP (mid, lo, size);
if ((*cmp) ((void *) hi, (void *) mid, arg) < 0)
SWAP (mid, hi, size);
else
goto jump_over;
if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
SWAP (mid, lo, size);
jump_over:;
left_ptr = lo + size;
right_ptr = hi - size;
/* Here's the famous ``collapse the walls'' section of quicksort.
Gotta like those tight inner loops! They are the main reason
that this algorithm runs much faster than others. */
do
{
while ((*cmp) ((void *) left_ptr, (void *) mid, arg) < 0)
left_ptr += size;
while ((*cmp) ((void *) mid, (void *) right_ptr, arg) < 0)
right_ptr -= size;
if (left_ptr < right_ptr)
{
SWAP (left_ptr, right_ptr, size);
if (mid == left_ptr)
mid = right_ptr;
else if (mid == right_ptr)
mid = left_ptr;
left_ptr += size;
right_ptr -= size;
}
else if (left_ptr == right_ptr)
{
left_ptr += size;
right_ptr -= size;
break;
}
}
while (left_ptr <= right_ptr);
/* Set up pointers for next iteration. First determine whether
left and right partitions are below the threshold size. If so,
ignore one or both. Otherwise, push the larger partition's
bounds on the stack and continue sorting the smaller one. */
if ((size_t) (right_ptr - lo) <= max_thresh)
{
if ((size_t) (hi - left_ptr) <= max_thresh)
/* Ignore both small partitions. */
POP (lo, hi);
else
/* Ignore small left partition. */
lo = left_ptr;
}
else if ((size_t) (hi - left_ptr) <= max_thresh)
/* Ignore small right partition. */
hi = right_ptr;
else if ((right_ptr - lo) > (hi - left_ptr))
{
/* Push larger left partition indices. */
PUSH (lo, right_ptr);
lo = left_ptr;
}
else
{
/* Push larger right partition indices. */
PUSH (left_ptr, hi);
hi = right_ptr;
}
}
}
/* Once the BASE_PTR array is partially sorted by quicksort the rest
is completely sorted using insertion sort, since this is efficient
for partitions below MAX_THRESH size. BASE_PTR points to the beginning
of the array to sort, and END_PTR points at the very last element in
the array (*not* one beyond it!). */
#define min(x, y) ((x) < (y) ? (x) : (y))
{
char *const end_ptr = &base_ptr[size * (total_elems - 1)];
char *tmp_ptr = base_ptr;
char *thresh = min(end_ptr, base_ptr + max_thresh);
char *run_ptr;
/* Find smallest element in first threshold and place it at the
array's beginning. This is the smallest array element,
and the operation speeds up insertion sort's inner loop. */
for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
tmp_ptr = run_ptr;
if (tmp_ptr != base_ptr)
SWAP (tmp_ptr, base_ptr, size);
/* Insertion sort, running from left-hand-side up to right-hand-side. */
run_ptr = base_ptr + size;
while ((run_ptr += size) <= end_ptr)
{
tmp_ptr = run_ptr - size;
while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
tmp_ptr -= size;
tmp_ptr += size;
if (tmp_ptr != run_ptr)
{
char *trav;
trav = run_ptr + size;
while (--trav >= run_ptr)
{
char c = *trav;
char *hi, *lo;
for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)
*hi = *lo;
*hi = c;
}
}
}
}
}
/*
* MigFlow - Copyright (C) <2010-2020>
* <Universite catholique de Louvain (UCL), Belgium
* Universite de Montpellier, France>
*
* List of the contributors to the development of MigFlow: see AUTHORS file.
* Description and complete License: see LICENSE file.
*
* This program (MigFlow) is free software:
* 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, either version
* 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program (see COPYING and COPYING.LESSER files). If not,
* see <http://www.gnu.org/licenses/>.
*/
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include <string.h>
#include <stdlib.h>
static size_t _vectorSize(void *m) {
return m == NULL ? 0 : (*((size_t*)m - 1));
}
static void _vectorFree(void **m) {
if (*m != NULL)
free(((size_t *)*m) - 2);
*m = NULL;
}
static void *_vectorPush(void **m, size_t s) {
if (s == 0) return *m;
if (*m == NULL) {
size_t *n = (size_t*)malloc(s * 2 + 2 * sizeof(size_t));
n[0] = 2 * s;
n[1] = s;
*m = n + 2;
return *m;
}
size_t *n = (*(size_t**)m) - 2;
n[1] += s;
if (n[0] < n[1]) {
n[0] *= 2;
n = (size_t*)realloc(n, n[0] + 2 * sizeof(size_t));
*m= n + 2;
}
return ((char*) *m) + n[1] - s;
}
static void *_vectorInsert(void **m, size_t p, size_t s) {
_vectorPush(m, s);
memmove(((char*)*m) + p + s, ((char*)*m) + p, _vectorSize(*m) - s - p);
return ((char*)*m) + p;
}
static void *_vectorDup(void *m) {
if (m == NULL)
return NULL;
size_t *n = ((size_t*)m - 2);
size_t N = n[1];
size_t *a = (size_t*) malloc(sizeof(size_t) * 2 + N);
memcpy(a, n, sizeof(size_t) * 2 + N);
a[0] = a[1] = N;
return a + 2;
}
static void vectorClear(void *m) {
if (m != NULL)
*(((size_t*) m) - 1) = 0;
}
static void _vectorPop(void *m, size_t s) {
if (m != NULL) {
*((size_t*)m - 1)-= s;
}
}
static void _vectorRemoveFlag(void *m, const int *flag, int size) {
size_t r = 0;
for (size_t i = 0; i < _vectorSize(m)/size; ++i) {
if(flag[i]) {
if (r != 0) {
memcpy(((char*)m)+size*(i-r),((char*)m)+size*i,size);
}
}
else {
r += 1;
}
}
_vectorPop(m,size*r);
}
extern "C" {
void quicksort (void *const pbase, size_t total_elems, size_t size,
int (*cmp)(const void*, const void*, void*), void *arg);
}
#define vector_remove_flag(v,f,repeat) _vectorRemoveFlag((void*)v,f,repeat*sizeof(*v))
#define vector_size(v) (_vectorSize((void*)v)/sizeof(*v))
#define vector_push(v) ((__typeof__(*v))_vectorPush((void**)v, sizeof(**v)))
#define vector_push_n(v, x) ((__typeof__(*v))_vectorPush((void**)v, sizeof(**v) * (x)))
#define vector_insert(v, p) ((__typeof__(*v))_vectorInsert((void**)v, p * (sizeof(**v)), sizeof(**v)))
#define vector_pop(v) _vectorPop((void*)v, sizeof(*v))
#define vector_pop_n(v, x) _vectorPop((void*)v, sizeof(*v) * (x)))
#define vector_free(m) _vectorFree((void**)&m)
#define vector_dup(m) ((__typeof__(m))_vectorDup(m))
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment