| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <stdarg.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <string.h> |
| 26 |
#include <math.h> |
| 27 |
#include "icc.h" |
| 28 |
#include "numlib.h" |
| 29 |
#include "gamut.h" |
| 30 |
|
| 31 |
|
| 32 |
static double tri_area( |
| 33 |
double v1[3], |
| 34 |
double v2[3], |
| 35 |
double v3[3]) { |
| 36 |
int i, j; |
| 37 |
double sp, ss[3]; |
| 38 |
double area; |
| 39 |
double *vv[3]; |
| 40 |
|
| 41 |
vv[0] = v1; |
| 42 |
vv[1] = v2; |
| 43 |
vv[2] = v3; |
| 44 |
|
| 45 |
for (i = 0; i < 3; i++) { |
| 46 |
for (ss[i] = 0.0, j = 0; j < 3; j++) { |
| 47 |
double dd = vv[i][j] - vv[(i+1) % 3][j]; |
| 48 |
ss[i] += dd * dd; |
| 49 |
} |
| 50 |
ss[i] = sqrt(ss[i]); |
| 51 |
} |
| 52 |
sp = 0.5 * (ss[0] + ss[1] + ss[2]); |
| 53 |
area = sqrt(fabs(sp * (sp - ss[0]) * (sp - ss[1]) * (sp - ss[2]))); |
| 54 |
|
| 55 |
return area; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static int edge_tri_isect( |
| 61 |
gamut *s, |
| 62 |
double *ip, |
| 63 |
gtri *t, |
| 64 |
gedge *e |
| 65 |
) { |
| 66 |
double rv; |
| 67 |
double gv[3]; |
| 68 |
double ival[3]; |
| 69 |
double den; |
| 70 |
int j; |
| 71 |
|
| 72 |
gv[0] = e->v[1]->p[0] - e->v[0]->p[0]; |
| 73 |
gv[1] = e->v[1]->p[1] - e->v[0]->p[1]; |
| 74 |
gv[2] = e->v[1]->p[2] - e->v[0]->p[2]; |
| 75 |
|
| 76 |
den = t->pe[0] * gv[0] + t->pe[1] * gv[1] + t->pe[2] * gv[2]; |
| 77 |
if (fabs(den) < 1e-10) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
rv = -(t->pe[0] * e->v[0]->p[0] |
| 83 |
+ t->pe[1] * e->v[0]->p[1] |
| 84 |
+ t->pe[2] * e->v[0]->p[2] |
| 85 |
+ t->pe[3])/den; |
| 86 |
|
| 87 |
|
| 88 |
ival[0] = e->v[0]->p[0] + rv * gv[0]; |
| 89 |
ival[1] = e->v[0]->p[1] + rv * gv[1]; |
| 90 |
ival[2] = e->v[0]->p[2] + rv * gv[2]; |
| 91 |
|
| 92 |
|
| 93 |
if (rv < 0.0 || rv > 1.0) |
| 94 |
return 0; |
| 95 |
|
| 96 |
|
| 97 |
for (j = 0; j < 3; j++) { |
| 98 |
double ds; |
| 99 |
ds = t->ee[j][0] * (ival[0] - s->cent[0]) |
| 100 |
+ t->ee[j][1] * (ival[1] - s->cent[1]) |
| 101 |
+ t->ee[j][2] * (ival[2] - s->cent[2]) |
| 102 |
+ t->ee[j][3]; |
| 103 |
if (ds > 1e-8) { |
| 104 |
return 0; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
ip[0] = ival[0]; |
| 110 |
ip[1] = ival[1]; |
| 111 |
ip[2] = ival[2]; |
| 112 |
|
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
double isect_volume( |
| 120 |
gamut *s1, |
| 121 |
gamut *s2 |
| 122 |
) { |
| 123 |
int i, j, k; |
| 124 |
gtri *tp1, *tp2; |
| 125 |
double vol; |
| 126 |
|
| 127 |
if (s1->compatible(s1, s2) == 0) |
| 128 |
return -1.0; |
| 129 |
|
| 130 |
if IS_LIST_EMPTY(s1->tris) |
| 131 |
s1->triangulate(s1); |
| 132 |
if IS_LIST_EMPTY(s2->tris) |
| 133 |
s2->triangulate(s2); |
| 134 |
|
| 135 |
vol = 0.0; |
| 136 |
|
| 137 |
|
| 138 |
for (k = 0; k < 2; k++) { |
| 139 |
|
| 140 |
if (k == 1) { |
| 141 |
gamut *st = s1; |
| 142 |
s1 = s2; |
| 143 |
s2 = st; |
| 144 |
printf("~1 doing second gamut inside first\n"); |
| 145 |
} else { |
| 146 |
printf("~1 doing first gamut inside second\n"); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
tp1 = s1->tris; |
| 152 |
FOR_ALL_ITEMS(gtri, tp1) { |
| 153 |
double sp, ss[3]; |
| 154 |
double area; |
| 155 |
double dp; |
| 156 |
int inout[3]; |
| 157 |
int nout; |
| 158 |
|
| 159 |
printf("~1 doing triangle %d from %s gamut\n",tp1->n,k == 0 ? "first" : "second"); |
| 160 |
|
| 161 |
|
| 162 |
nout = 0; |
| 163 |
for (i = 0; i < 3; i++) { |
| 164 |
double pl; |
| 165 |
pl = s2->nradial(s2, NULL, tp1->v[i]->p); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if ((k == 0 && pl > (1.0 + 1e-10)) |
| 170 |
|| (k == 1 && pl > (1.0 - 1e-10))) { |
| 171 |
nout++; |
| 172 |
inout[i] = 1; |
| 173 |
} else |
| 174 |
inout[i] = 0; |
| 175 |
} |
| 176 |
|
| 177 |
printf("~1 vertices outside = %d\n",nout); |
| 178 |
|
| 179 |
|
| 180 |
if (nout == 3) |
| 181 |
continue; |
| 182 |
|
| 183 |
|
| 184 |
area = tri_area(tp1->v[0]->p, tp1->v[1]->p, tp1->v[2]->p); |
| 185 |
printf("~1 full triangle area = %f\n",area); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if (nout != 0) { |
| 190 |
gvert *opv; |
| 191 |
double parea = 0.0; |
| 192 |
|
| 193 |
|
| 194 |
if (nout == 2) { |
| 195 |
for (j = 0; j < 3; j++) { |
| 196 |
if (inout[j] == 0) |
| 197 |
break; |
| 198 |
} |
| 199 |
} else { |
| 200 |
for (j = 0; j < 3; j++) { |
| 201 |
if (inout[j] == 1) |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
opv = tp1->v[j]; |
| 206 |
|
| 207 |
tp2 = s2->tris; |
| 208 |
FOR_ALL_ITEMS(gtri, tp2) { |
| 209 |
double isps[2][3]; |
| 210 |
int nisps; |
| 211 |
double isp[3]; |
| 212 |
int kk; |
| 213 |
|
| 214 |
|
| 215 |
for (i = 0; i < 3; i++) { |
| 216 |
if (tp2->mix[1][i] < tp1->mix[0][i] |
| 217 |
|| tp2->mix[0][i] > tp1->mix[1][i]) |
| 218 |
break; |
| 219 |
} |
| 220 |
if (i < 3) |
| 221 |
continue; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
nisps = 0; |
| 229 |
|
| 230 |
for (kk = 0; kk < 2; kk++) { |
| 231 |
gamut *ts; |
| 232 |
gtri *tpa; |
| 233 |
gtri *tpb; |
| 234 |
if (kk == 0) { |
| 235 |
ts = s1; |
| 236 |
tpa = tp1; |
| 237 |
tpb = tp2; |
| 238 |
} else { |
| 239 |
ts = s2; |
| 240 |
tpa = tp2; |
| 241 |
tpb = tp1; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
for (j = 0; j < 3; j++) { |
| 246 |
if (edge_tri_isect(ts, isp, tpa, tpb->e[j]) != 0) { |
| 247 |
|
| 248 |
if (nisps < 2) { |
| 249 |
|
| 250 |
icmAry2Ary(isps[nisps], isp); |
| 251 |
nisps++; |
| 252 |
} else { |
| 253 |
int xx; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
if (icmNorm33sq(isps[0], isp) < icmNorm33sq(isps[1], isp)) |
| 258 |
xx = 0; |
| 259 |
else |
| 260 |
xx = 1; |
| 261 |
|
| 262 |
if (icmNorm33sq(isps[xx ^ 1], isp) |
| 263 |
> icmNorm33sq(isps[xx ^ 1], isps[xx])) { |
| 264 |
|
| 265 |
icmAry2Ary(isps[xx], isp); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
if (nisps == 0) { |
| 272 |
|
| 273 |
continue; |
| 274 |
|
| 275 |
} else if (nisps == 2) { |
| 276 |
double sarea; |
| 277 |
|
| 278 |
printf("~1 sub triangle =\n"); |
| 279 |
printf("~1 com %f %f %f\n", opv->p[0], opv->p[1], opv->p[2]); |
| 280 |
printf("~1 1st %f %f %f\n", isps[0][0], isps[0][1], isps[0][2]); |
| 281 |
printf("~1 2nd %f %f %f\n", isps[1][0], isps[1][1], isps[1][2]); |
| 282 |
|
| 283 |
|
| 284 |
sarea = tri_area(opv->p, isps[0], isps[1]); |
| 285 |
printf("~1 located intersecting triangle %d\n",tp2->n); |
| 286 |
printf("~1 got sub area %f\n",sarea); |
| 287 |
parea += sarea; |
| 288 |
} else { |
| 289 |
printf("~1 unexpectedly got %d intersection points in triangle\n",nisps); |
| 290 |
} |
| 291 |
} END_FOR_ALL_ITEMS(tp2); |
| 292 |
|
| 293 |
if (nout == 2) { |
| 294 |
area = parea; |
| 295 |
} else { |
| 296 |
area = area - parea; |
| 297 |
} |
| 298 |
printf("~1 partial area = %f\n",area); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
dp = tp1->v[0]->p[0] * tp1->pe[0] |
| 303 |
+ tp1->v[0]->p[1] * tp1->pe[1] |
| 304 |
+ tp1->v[0]->p[2] * tp1->pe[2]; |
| 305 |
|
| 306 |
printf("~1 vector volume = %f\n",dp * area); |
| 307 |
|
| 308 |
vol += dp * area; |
| 309 |
|
| 310 |
} END_FOR_ALL_ITEMS(tp1); |
| 311 |
} |
| 312 |
|
| 313 |
printf("~1 volume sum = %f\n",vol); |
| 314 |
vol = fabs(vol)/3.0; |
| 315 |
|
| 316 |
printf("~1 final volume = %f\n",vol); |
| 317 |
return vol; |
| 318 |
} |
| 319 |
|
| 320 |
|