Debian Codegraph @

gamut/isecvol.c

raw symbols SCIP
1
2 /*
3 * Compute the intersection volume of two gamuts.
4 *
5 * Author: Graeme W. Gill
6 * Date: 2008/1/7
7 * Version: 1.00
8 *
9 * Copyright 2008 Graeme W. Gill
10 * All rights reserved.
11 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
12 * see the License.txt file for licencing details.
13 */
14
15 /*
16 * TTBD:
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 /* Compute a triangles area */
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]; /* Triangle side lengths */
38 double area; /* Area of this triangle */
39 double *vv[3]; /* Pointers to vertexes */
40
41 vv[0] = v1;
42 vv[1] = v2;
43 vv[2] = v3;
44 /* Compute the full triangles area */
45 for (i = 0; i < 3; i++) { /* For each edge */
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]); /* semi-perimeter */
53 area = sqrt(fabs(sp * (sp - ss[0]) * (sp - ss[1]) * (sp - ss[2]))); /* Area of triangle */
54
55 return area;
56 }
57
58 /* See if the given edge intersects a given triangle. */
59 /* Return 1 if it does, 0 if it doesn't */
60 static int edge_tri_isect(
61 gamut *s, /* Gamut the triangle is in */
62 double *ip, /* return intersection point */
63 gtri *t, /* Triangle in question */
64 gedge *e /* edge to test (may be from another gamut) */
65 ) {
66 double rv; /* Axis parameter value */
67 double gv[3]; /* Grey axis vector */
68 double ival[3]; /* Intersection value */
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 /* Compute the intersection of the grey axis vector with the triangle plane */
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 /* Compute the actual intersection point */
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 /* Check if the intersection is on the edge */
93 if (rv < 0.0 || rv > 1.0)
94 return 0;
95
96 /* Check if the intersection point is within the triangle */
97 for (j = 0; j < 3; j++) {
98 double ds;
99 ds = t->ee[j][0] * (ival[0] - s->cent[0]) /* Convert to relative for edge check */
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; /* Not within triangle */
105 }
106 }
107
108 /* Got an intersection point */
109 ip[0] = ival[0];
110 ip[1] = ival[1];
111 ip[2] = ival[2];
112
113 return 1;
114 }
115
116
117 /* Return the total volume of the gamut */
118 /* Return -1.0 if incompatible gamuts */
119 double isect_volume(
120 gamut *s1,
121 gamut *s2
122 ) {
123 int i, j, k;
124 gtri *tp1, *tp2; /* Triangle pointer */
125 double vol; /* Gamut volume */
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 /* For first gamut then second gamut */
138 for (k = 0; k < 2; k++) {
139
140 if (k == 1) { /* Swap the two gamuts roles */
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 /* Compute the area of each triangle in the list that is within, */
150 /* the other gamut, and accumulate the gamut volume. */
151 tp1 = s1->tris;
152 FOR_ALL_ITEMS(gtri, tp1) {
153 double sp, ss[3]; /* Triangle side lengths */
154 double area; /* Area of this triangle */
155 double dp; /* Dot product of point in triangle and normal */
156 int inout[3]; /* 0 = inside, 1 = outside */
157 int nout; /* Number that are out */
158
159 printf("~1 doing triangle %d from %s gamut\n",tp1->n,k == 0 ? "first" : "second");
160 /* See how many vertices in the triangle are contained within */
161 /* the other gamut. */
162 nout = 0;
163 for (i = 0; i < 3; i++) { /* For each vertex */
164 double pl;
165 pl = s2->nradial(s2, NULL, tp1->v[i]->p);
166
167 /* We add a slight hysterysis to avoid issues */
168 /* with identical triangles in two gamuts. */
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 /* If none are in, skip this triangle */
180 if (nout == 3)
181 continue;
182
183 /* Compute the full triangles area */
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 /* If the triangle is not completely in, locate all the intersections */
188 /* between it and triangles in the other gamut */
189 if (nout != 0) {
190 gvert *opv; /* Pointer to the one "in" or "out" vertex */
191 double parea = 0.0; /* Total partial area */
192
193 /* Locate the odd point out of the three */
194 if (nout == 2) { /* Look for the one "in" point */
195 for (j = 0; j < 3; j++) {
196 if (inout[j] == 0)
197 break;
198 }
199 } else { /* Look for the one "out" point */
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) { /* Other gamut triangles */
209 double isps[2][3]; /* Intersection npoints */
210 int nisps; /* Number of intersection points */
211 double isp[3]; /* New intersection point */
212 int kk;
213
214 /* Do a min/max intersection elimination test */
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; /* min/max don't overlap */
219 }
220 if (i < 3)
221 continue; /* Skip this triangle, it can't intersect */
222
223 //printf("~1 located possible intersecting triangle %d\n",tp2->n);
224
225 /* Locate intersection of all sides of one triangle with */
226 /* the plane of the other. Keep the two points of */
227 /* intersection that lie within the triangles. */
228 nisps = 0;
229 //printf("~1 initial nisps = %d\n",nisps);
230 for (kk = 0; kk < 2; kk++) {
231 gamut *ts; /* Triangle gamut */
232 gtri *tpa; /* Triangle pointer */
233 gtri *tpb; /* Other triangle pointer */
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 /* For each edge */
245 for (j = 0; j < 3; j++) {
246 if (edge_tri_isect(ts, isp, tpa, tpb->e[j]) != 0) {
247 //printf("~1 isect %f %f %f\n", isp[0],isp[1],isp[2]);
248 if (nisps < 2) {
249 //printf("~1 added at %d\n",nisps);
250 icmAry2Ary(isps[nisps], isp);
251 nisps++;
252 } else { /* Figure which one to replace */
253 int xx;
254 /* Replace the one closest to the new one, */
255 /* if the new one is further from the other one */
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 //printf("~1 replaced %d\n",xx);
265 icmAry2Ary(isps[xx], isp);
266 }
267 }
268 }
269 }
270 }
271 if (nisps == 0) {
272 //printf("~1 no intersection\n");
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 /* Accumulate area of these two points + odd point */
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 { /* Hmm */
289 printf("~1 unexpectedly got %d intersection points in triangle\n",nisps);
290 }
291 } END_FOR_ALL_ITEMS(tp2);
292
293 if (nout == 2) { /* One "in" point */
294 area = parea;
295 } else { /* One "out" point */
296 area = area - parea;
297 }
298 printf("~1 partial area = %f\n",area);
299 }
300
301 /* Dot product between first vertex in triangle and the unit normal vector */
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 /* Accumulate gamut volume */
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