1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include "cone.h"
list<vector<bigrat> >::iterator last(list<vector<bigrat> >& L)
{
list<vector<bigrat> >::iterator i = L.end();
i--;
return i;
}
vector<bigrat> cross(const vector<bigrat> A, const vector<bigrat> B)
{
vector<bigrat> C;
C.push_back(A[1]*B[2] - A[2]*B[1]);
C.push_back(A[2]*B[0]-A[0]*B[2]);
C.push_back(A[0]*B[1]-A[1]*B[0]);
return C;
}
bigrat dot(const vector<bigrat> V, const vector<bigrat> W)
{
bigrat sum(0);
for(unsigned int i = 0; i<V.size(); i++)
sum += V[i]*W[i];
return sum;
}
bool multiple(const vector<bigrat> V, const vector<bigrat> W)
{
return zerovec(cross(V, W));
}
bigrat det(const vector<bigrat> U, const vector<bigrat> V, const vector<bigrat> W)
{
return dot(cross(U, V), W);
}
bool in_fan(const vector<bigrat> U, const vector<bigrat> V, const vector<bigrat> W)
{
return (sgn(dot(cross(U, V),cross(U, W))) >0) && ( sgn(dot(cross(W, V), cross(W, U))) > 0);
}
list<vector<bigrat> >::iterator circ_inc(list<vector<bigrat> >& L, list<vector<bigrat> >::iterator i)
{
i++;
if(i == L.end())
i = L.begin();
return i;
}
list<vector<bigrat> >::iterator circ_dec(list<vector<bigrat> >& L, list<vector<bigrat> >::iterator j)
{
if(j == L.begin())
j = L.end();
j--;
return j;
}
list<vector<bigrat> >::iterator circ_erase(list<vector<bigrat> >& L, list<vector<bigrat> >::iterator i)
{
i = L.erase(i);
if(i == L.end())
i = L.begin();
return i;
}
char Cone::add(vector<bigrat> newvec)
{
if(multiple_in_hull(newvec))
return 'b';
switch(type)
{
case 'p':
hull.push_back(newvec);
type = 'f';
return 'e';
case 'f':
switch(sgn(det(*(hull.begin()), *last(hull), newvec)))
{
case 0: //newvec in plane of cone
if(in_fan(*hull.begin(), newvec,*last(hull)))
return 'i';
else if(in_fan(newvec, *(hull.begin()), *last(hull)))
{
*(hull.begin()) = newvec;
return 'e';
}
else if(in_fan(*(hull.begin()), *last(hull), newvec))
{
*last(hull) = newvec;
return 'e';
}
else
{
type = 's';
return 'e';
}
case 1:
hull.push_back(newvec);
type = 'c';
return 'e';
case -1:
hull.insert(last(hull), newvec);
type = 'c';
return 'e';
}
case 's':
switch(sgn(det(*(hull.begin()), *last(hull), newvec)))
{
case 0:
return 'i';
case 1:
type = 'h';
return 'e';
case -1:
hull.push_back(hull.front());
hull.erase(hull.begin());
type = 'h';
return 'e';
}
case 'h':
switch(sgn(det(*(hull.begin()), *last(hull), newvec)))
{
case -1:
type = 'a';
hull.clear();
return 'e';
case 0:
return 'b';
case 1:
return 'i';
}
case 'a':
return 'i';
case 'c':
list<vector<bigrat> >::iterator first_pos, first_neg;
switch(extreme_pts(first_pos, first_neg, newvec))
{
case 'a':
type = 'a';
hull.clear();
return 'e';
case 'i':
return 'i';
case 'e':
first_neg = circ_inc(hull, first_neg);
while(first_neg != first_pos)
first_neg = circ_erase(hull, first_neg);
hull.insert(first_pos, newvec);
return 'e';
case 'z':
list<vector<bigrat> >::iterator next = circ_inc(hull, first_pos);
if(in_fan(*first_pos, newvec, *next))
return 'i';
else if(in_fan(newvec, *first_pos, *next))
{
first_pos = circ_erase(hull, first_pos);
first_pos = circ_dec(hull, first_pos);
|
{
first_pos = circ_erase(hull, first_pos);
first_pos = circ_dec(hull, first_pos);
}
hull.insert(next, newvec);
return 'e';
}
else if(in_fan(*first_pos, *next, newvec))
{
next = circ_erase(hull, next);
while(sgn(det(newvec, *next, *circ_inc(hull, next))) <= 0)
next = circ_erase(hull, next);
hull.insert(next, newvec);
return 'e';
}
else
{
list<vector<bigrat> > newhull;
newhull.push_back(*first_pos);
newhull.push_back(*next);
hull = newhull;
type = 'h';
return 'e';
}
}
}
return 'X'; //unreachable
} |