aboutsummaryrefslogtreecommitdiff
path: root/src/mtx_qhull
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2012-08-27 15:04:59 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2012-08-27 15:04:59 +0000
commite4f56a32d01350b09212172a509d34d9411a0d01 (patch)
treef7e5bf130a0cc181d15c368b831a71202af3c64e /src/mtx_qhull
parentaf9edd913bbb5ef56eded61030ce46c48cf4e90b (diff)
read list 'entry' type
svn path=/trunk/externals/iem/iemmatrix/; revision=16177
Diffstat (limited to 'src/mtx_qhull')
-rw-r--r--src/mtx_qhull/entry.h89
-rw-r--r--src/mtx_qhull/list.c136
-rw-r--r--src/mtx_qhull/list.h13
-rw-r--r--src/mtx_qhull/vectors.c22
4 files changed, 198 insertions, 62 deletions
diff --git a/src/mtx_qhull/entry.h b/src/mtx_qhull/entry.h
new file mode 100644
index 0000000..bc22f35
--- /dev/null
+++ b/src/mtx_qhull/entry.h
@@ -0,0 +1,89 @@
+#ifndef QHULL_ENTRY_H
+#define QHULL_ENTRY_H
+
+#include <sys/types.h>
+#include <stdio.h>
+
+typedef size_t index_t;
+
+typedef union {
+ index_t i;
+ void*p;
+} entryvalu_t;
+
+typedef enum {
+ INDEX,
+ POINTER,
+ INVALID
+} entrytype_t;
+
+typedef struct entry_ {
+ entrytype_t typ;
+ entryvalu_t val;
+} entry_t;
+
+static
+void entry_setIndex(entry_t*e, index_t i) {
+ e->typ=INDEX;
+ e->val.i=i;
+}
+static
+void entry_setPointer(entry_t*e, void*p) {
+ e->typ=POINTER;
+ e->val.p=p;
+}
+static
+entry_t entry_makeIndex(index_t i) {
+ entry_t result;
+ entry_setIndex(&result, i);
+ return result;
+}
+static
+entry_t entry_makePointer(void*p) {
+ entry_t result;
+ entry_setPointer(&result, p);
+ return result;
+}
+
+static
+index_t entry_getIndex(const entry_t*e) {
+ return (INDEX==e->typ)?e->val.i:0;
+}
+static
+void*entry_getPointer(const entry_t*e) {
+ return (POINTER==e->typ)?e->val.p:0;
+}
+static
+int entry_equals(const entry_t*e1, const entry_t*e2) {
+ if(e1->typ!=e2->typ)return 0;
+ switch(e1->typ) {
+ case INDEX:
+ return (e1->val.i == e2->val.i);
+ case POINTER:
+ return (e1->val.p == e2->val.p);
+ default:
+ return 0;
+ }
+ return 0;
+}
+
+static
+void print_entry(const entry_t e) {
+ switch(e.typ) {
+ case INDEX:
+ printf("%d", e.val.i);
+ return;
+ case POINTER:
+ printf("0x%p", e.val.p);
+ return;
+ default:
+ printf("<unkonwn>");
+ return;
+ }
+}
+
+
+
+
+
+#endif
diff --git a/src/mtx_qhull/list.c b/src/mtx_qhull/list.c
index 1f8f58b..6f3b416 100644
--- a/src/mtx_qhull/list.c
+++ b/src/mtx_qhull/list.c
@@ -66,8 +66,10 @@ size_t getLength(const list_t list) {
entry_t getEntry(const list_t list, const index_t index) {
if ((index>=0)&&(index<getLength(list)))
return list.entries[index];
- else
- return 0;
+ else {
+ entry_t result={0};
+ return result;
+ }
}
void setEntry(const list_t list, const index_t index, const entry_t entry) {
@@ -76,7 +78,7 @@ void setEntry(const list_t list, const index_t index, const entry_t entry) {
}
-list_t initList(entry_t *entries, const size_t length) {
+list_t initList(const entry_t *entries, const size_t length) {
index_t i;
list_t l = allocateList(length);
if (l.entries!=0)
@@ -84,28 +86,42 @@ list_t initList(entry_t *entries, const size_t length) {
setEntry(l,i,entries[i]);
return l;
}
+list_t initListIndex(const index_t *entries, const size_t length) {
+ index_t i;
+ list_t l = allocateList(length);
+ if (l.entries!=0)
+ for (i=0; i<(index_t)length; i++)
+ setEntry(l,i,entry_makeIndex(entries[i]));
+ return l;
+}
+
-list_t initListFromTo(const entry_t start, const entry_t stop) {
+
+list_t initListFromTo(const index_t start, const index_t stop) {
+ entry_t e;
index_t i;
size_t length;
- entry_t c;
+ index_t c;
int incr;
if (stop>=start) {
- length=(size_t) (stop-start+1);
+ length=(size_t) (stop-start+1);
incr=1;
} else {
- length=(size_t) (start-stop+1);
- incr=-1;
+ length=(size_t) (start-stop+1);
+ incr=-1;
}
list_t l = allocateList(length);
- if (l.entries!=0)
- for (i=0,c=start; i<length; i++, c+=incr)
- setEntry(l,i,c);
+ if (l.entries!=0) {
+ for (i=0,c=start; i<length; i++, c+=incr) {
+ entry_setIndex(&e, c);
+ setEntry(l,i,e);
+ }
+ }
return l;
}
list_t initConstantList(const entry_t c, const size_t length){
- entry_t i;
+ index_t i;
list_t l = allocateList(length);
if (l.entries!=0)
for (i=0; i<length; i++)
@@ -115,7 +131,7 @@ list_t initConstantList(const entry_t c, const size_t length){
list_t duplicateList(const list_t list_in) {
- entry_t i;
+ index_t i;
list_t list_out=emptyList();
list_out = allocateList(getLength(list_in));
for (i=0; i<getLength(list_out); i++)
@@ -125,7 +141,7 @@ list_t duplicateList(const list_t list_in) {
list_t mergeLists(const list_t list1, const list_t list2) {
list_t list_out;
- entry_t i,j;
+ index_t i,j;
list_out = allocateList(getLength(list1)+ getLength(list2));
if (list_out.entries!=0) {
for (i=0; i<getLength(list1); i++)
@@ -140,7 +156,10 @@ list_t getSubList(const list_t list, const list_t indices) {
index_t i;
list_t new_list = allocateList(getLength(indices));
for (i=0; i<getLength(new_list); i++) {
- setEntry(new_list,i,getEntry(list,getEntry(indices,i)));
+ entry_t e1=getEntry(indices,i);
+ setEntry(new_list,i,
+ getEntry(list,entry_getIndex(&e1))
+ );
}
}
@@ -172,7 +191,7 @@ void appendToList(list_t *list, const entry_t entry) {
}
}
-void removeEntryFromList(list_t *list, const index_t index) {
+void removeIndexFromList(list_t *list, const index_t index) {
index_t i,j;
for (i=j=0; i<getLength(*list); i++) {
if (i!=index)
@@ -185,8 +204,9 @@ void removeEntryFromList(list_t *list, const index_t index) {
void removeValueFromList(list_t *list, const entry_t entry) {
index_t i,j;
for (j=i=0; i<getLength(*list); i++) {
- if (getEntry(*list,i)!=entry)
- setEntry(*list, j++, getEntry(*list,i));
+ entry_t e1=getEntry(*list,i);
+ if (!entry_equals(&e1, &entry))
+ setEntry(*list, j++, getEntry(*list,i));
}
reallocateList(list,j);
}
@@ -204,9 +224,11 @@ void appendListToList(list_t *list1, const list_t list2) {
void removeEntryListFromList(list_t *list, const list_t indices) {
index_t i,j;
- for (i=j=0; i<getLength(*list); i++)
- if (notInList(i,indices))
- setEntry(*list, j++, getEntry(*list,i));
+ for (i=j=0; i<getLength(*list); i++) {
+ entry_t e={i};
+ if (notInList(e,indices))
+ setEntry(*list, j++, getEntry(*list,i));
+ }
reallocateList(list,j);
}
@@ -215,8 +237,11 @@ void removeValueListFromList(list_t *list, const list_t excl_list) {
int keep;
for (j=i=0; i<getLength(*list); i++) {
keep=1;
- for (k=0; k<getLength(excl_list); k++)
- keep=(keep)&&(getEntry(*list,i)!=getEntry(excl_list,k));
+ for (k=0; k<getLength(excl_list); k++) {
+ entry_t e1=getEntry(*list, i);
+ entry_t e2=getEntry( excl_list, k);
+ keep=(keep)&&(!entry_equals(&e1, &e2));
+ }
if (keep)
setEntry(*list, j++, getEntry(*list,i));
}
@@ -237,29 +262,35 @@ void reverseList(list_t * const list) {
int inList(const entry_t entry, const list_t list) {
index_t i;
- for (i=0; i<getLength(list); i++)
- if (getEntry(list,i)==entry)
- return 1;
+ for (i=0; i<getLength(list); i++) {
+ entry_t e1=getEntry(list,i);
+ if(entry_equals(&e1, &entry))
+ return 1;
+ }
return 0;
}
-entry_t findValueInList(const entry_t entry, const list_t list) {
+index_t findValueInList(const entry_t entry, const list_t list) {
index_t i;
- for (i=0; i<getLength(list); i++)
- if (entry==getEntry(list,i))
- return i;
- return getLength(list);
+ for (i=0; i<getLength(list); i++) {
+ entry_t e1=getEntry(list,i);
+ if(entry_equals(&e1, &entry))
+ return i;
+ }
+ return i;
}
void uniquefyListEntries(list_t *list) {
index_t i,j,k;
- int keep;
k=0;
for (j=0; j<getLength(*list); j++) {
- keep=1;
- for (i=0; (i<k)&&(keep); i++)
- keep = (keep)&&(list->entries[j]!=list->entries[i]);
- if (keep) {
+ for (i=0; i<k; i++) {
+ entry_t e1=list->entries[j];
+ entry_t e2=list->entries[i];
+ if(entry_equals(&e1, &e2))
+ break;
+ }
+ if (i==k) {
list->entries[i++]=list->entries[j];
k++;
}
@@ -271,27 +302,36 @@ list_t findValueListInList(const list_t value_list,
const list_t list) {
list_t l=emptyList();
index_t i,j;
- for (i=0; i<getLength(value_list); i++)
- appendToList(&l,findValueInList(
- getEntry(value_list,i),list));
+ for (i=0; i<getLength(value_list); i++) {
+ index_t idx=findValueInList(getEntry(value_list,i),list);
+ entry_t e;
+ entry_setIndex(&e, idx);
+ appendToList(&l, e);
+ }
return l;
}
int notInList(const entry_t entry, const list_t list) {
index_t i;
- for (i=0; i<getLength(list); i++)
- if (getEntry(list,i)==entry)
- return 0;
+ for (i=0; i<getLength(list); i++) {
+ entry_t e=getEntry(list, i);
+ if (entry_equals(&e, &entry))
+ return 0;
+ }
return 1;
}
void printList(list_t const list) {
- entry_t i;
- printf("[list]_%d=[",list.length);
- if (getLength(list)>0)
- printf("%d",getEntry(list,0));
- for (i=1; i<list.length; i++)
- printf(", %d",getEntry(list,i));
+ index_t i;
+ const size_t len=getLength(list);
+ printf("[list]_%d=[",len);
+ if (len>0) {
+ print_entry(getEntry(list,0));
+ }
+ for (i=1; i<len; i++) {
+ printf(", ");
+ print_entry(getEntry(list,i));
+ }
printf("]\n");
}
diff --git a/src/mtx_qhull/list.h b/src/mtx_qhull/list.h
index 1a98256..6b05531 100644
--- a/src/mtx_qhull/list.h
+++ b/src/mtx_qhull/list.h
@@ -5,8 +5,8 @@
#include <sys/types.h>
-typedef long int entry_t;
-typedef long int index_t;
+#include "entry.h"
+
typedef struct list_ {
entry_t *entries;
size_t length;
@@ -21,8 +21,9 @@ void freeList(list_t *list);
size_t getLength(const list_t list);
entry_t getEntry(const list_t list, const index_t index);
void setEntry(const list_t list, const index_t index, const entry_t entry);
-list_t initList(entry_t *entries, const size_t length);
-list_t initListFromTo(const entry_t start, const entry_t stop);
+list_t initList(const entry_t *entries, const size_t length);
+list_t initListIndex(const index_t *entries, const size_t length);
+list_t initListFromTo(const index_t start, const index_t stop);
list_t initConstantList(const entry_t c, const size_t length);
list_t duplicateList(const list_t list_in);
list_t mergeLists(const list_t list1, const list_t list2);
@@ -31,7 +32,7 @@ list_t getSubListFromTo(const list_t list, const index_t start,
const index_t stop);
void appendToList(list_t *list, const entry_t entry);
void removeValueFromList(list_t *list, const entry_t entry);
-void removeEntryFromList(list_t *list, const index_t index);
+void removeIndexFromList(list_t *list, const index_t index);
void appendListToList(list_t *list1, const list_t list2);
void removeValueListFromList(list_t *list, const list_t excl_list);
void removeEntryListFromList(list_t *list, const list_t indices);
@@ -39,7 +40,7 @@ void reverseList(list_t * const list);
int inList(const entry_t entry, const list_t list);
int notInList(const entry_t entry, const list_t list);
list_t findValueListInList(const list_t value_list, const list_t list);
-entry_t findValueInList(const entry_t entry, const list_t list);
+index_t findValueInList(const entry_t entry, const list_t list);
void uniquefyListEntries(list_t *list);
void printList(const list_t list);
diff --git a/src/mtx_qhull/vectors.c b/src/mtx_qhull/vectors.c
index d9d3bd0..655ed94 100644
--- a/src/mtx_qhull/vectors.c
+++ b/src/mtx_qhull/vectors.c
@@ -172,7 +172,8 @@ vector_t averageListedPoints(const points_t points, const list_t list) {
index_t i;
vector_t m = initVector(0.0f, 0.0f, 0.0f);
for (i=0; i<getLength(list); i++) {
- m=addVectors(getPoint(points,getEntry(list,i)),m);
+ entry_t e=getEntry(list,i);
+ m=addVectors(getPoint(points,entry_getIndex(&e)),m);
}
m=scaleVector(m,1.0f/((float)getLength(list)));
return m;
@@ -184,10 +185,12 @@ vector_t normalOfListedPoints(const points_t points, const list_t list) {
vector_t d1,d2,c;
index_t i;
for (i=1; i<=getLength(list); i++) {
- d1=subtractVectors(getPoint(points,getEntry(list,i-1)),m);
- d2=subtractVectors(getPoint(points,getEntry(list,i%getLength(list))),m);
- c=crossProduct(d1,d2);
- n=addVectors(c,n);
+ entry_t e=getEntry(list,i-1);
+ d1=subtractVectors(getPoint(points,entry_getIndex(&e)),m);
+ e=getEntry(list,i%getLength(list));
+ d2=subtractVectors(getPoint(points,entry_getIndex(&e)),m);
+ c=crossProduct(d1,d2);
+ n=addVectors(c,n);
}
return n;
}
@@ -198,9 +201,12 @@ vector_t directionOfListedPoints(const points_t points, const list_t list) {
vector_t d,c;
index_t i;
for (i=1; i<getLength(list); i++) {
- d=subtractVectors(getPoint(points,getEntry(list,i-1)),
- getPoint(points,getEntry(list,i)));
- dir=addVectors(d,dir);
+ entry_t e1=getEntry(list,i-1);
+ entry_t e2=getEntry(list,i );
+
+ d=subtractVectors(getPoint(points,entry_getIndex(&e1)),
+ getPoint(points,entry_getIndex(&e2)));
+ dir=addVectors(d,dir);
}
return dir;
}