1 | #include <Singular/mod2.h> |
---|
2 | #include <Singular/ipid.h> |
---|
3 | #include <Singular/blackbox.h> |
---|
4 | #include <omalloc/omalloc.h> |
---|
5 | #include <kernel/febase.h> |
---|
6 | #include <kernel/longrat.h> |
---|
7 | #include <Singular/subexpr.h> |
---|
8 | #include <gfanlib/gfanlib.h> |
---|
9 | #include <Singular/bbcone.h> |
---|
10 | #include <ipshell.h> |
---|
11 | #include <kernel/intvec.h> |
---|
12 | #include <sstream> |
---|
13 | |
---|
14 | #ifdef HAVE_FANS |
---|
15 | |
---|
16 | int coneID; |
---|
17 | |
---|
18 | int integerToInt(gfan::Integer const &V, bool &ok) |
---|
19 | { |
---|
20 | mpz_t v; |
---|
21 | mpz_init(v); |
---|
22 | V.setGmp(v); |
---|
23 | int ret=0; |
---|
24 | if(mpz_fits_sint_p(v)) |
---|
25 | ret=mpz_get_si(v); |
---|
26 | else |
---|
27 | ok=false; |
---|
28 | mpz_clear(v); |
---|
29 | return ret; |
---|
30 | } |
---|
31 | |
---|
32 | intvec* zVector2Intvec(const gfan::ZVector zv) |
---|
33 | { |
---|
34 | int d=zv.size(); |
---|
35 | intvec* iv = new intvec(1, d, 0); |
---|
36 | bool ok = true; |
---|
37 | for(int i=1;i<=d;i++) |
---|
38 | IMATELEM(*iv, 1, i) = integerToInt(zv[i-1], ok); |
---|
39 | if (!ok) WerrorS("overflow while converting a gfan::ZVector to an intvec"); |
---|
40 | return iv; |
---|
41 | } |
---|
42 | |
---|
43 | intvec* zMatrix2Intvec(const gfan::ZMatrix zm) |
---|
44 | { |
---|
45 | int d=zm.getHeight(); |
---|
46 | int n=zm.getWidth(); |
---|
47 | intvec* iv = new intvec(d, n, 0); |
---|
48 | bool ok = true; |
---|
49 | for(int i=1;i<=d;i++) |
---|
50 | for(int j=1;j<=n;j++) |
---|
51 | IMATELEM(*iv, i, j) = integerToInt(zm[i-1][j-1], ok); |
---|
52 | if (!ok) WerrorS("overflow while converting a gfan::ZMatrix to an intmat"); |
---|
53 | return iv; |
---|
54 | } |
---|
55 | |
---|
56 | gfan::ZMatrix intmat2ZMatrix(const intvec* iMat) |
---|
57 | { |
---|
58 | int d=iMat->rows(); |
---|
59 | int n=iMat->cols(); |
---|
60 | gfan::ZMatrix ret(d,n); |
---|
61 | for(int i=0;i<d;i++) |
---|
62 | for(int j=0;j<n;j++) |
---|
63 | ret[i][j]=IMATELEM(*iMat, i+1, j+1); |
---|
64 | return ret; |
---|
65 | } |
---|
66 | |
---|
67 | /* expects iMat to have just one row */ |
---|
68 | gfan::ZVector intvec2ZVector(const intvec* iVec) |
---|
69 | { |
---|
70 | int n =iVec->rows(); |
---|
71 | gfan::ZVector ret(n); |
---|
72 | for(int j=0;j<n;j++) |
---|
73 | ret[j]=IMATELEM(*iVec, j+1, 1); |
---|
74 | return ret; |
---|
75 | } |
---|
76 | |
---|
77 | std::string toString(gfan::ZMatrix const &m, char *tab=0) |
---|
78 | { |
---|
79 | std::stringstream s; |
---|
80 | |
---|
81 | for(int i=0;i<m.getHeight();i++) |
---|
82 | { |
---|
83 | if(tab)s<<tab; |
---|
84 | for(int j=0;j<m.getWidth();j++) |
---|
85 | { |
---|
86 | s<<m[i][j]; |
---|
87 | if(i+1!=m.getHeight() || j+1!=m.getWidth()) |
---|
88 | { |
---|
89 | s<<","; |
---|
90 | } |
---|
91 | } |
---|
92 | s<<std::endl; |
---|
93 | } |
---|
94 | return s.str(); |
---|
95 | } |
---|
96 | |
---|
97 | std::string toString(gfan::ZCone const &c) |
---|
98 | { |
---|
99 | std::stringstream s; |
---|
100 | gfan::ZMatrix i=c.getInequalities(); |
---|
101 | gfan::ZMatrix e=c.getEquations(); |
---|
102 | s<<"AMBIENT_DIM"<<std::endl; |
---|
103 | s<<c.ambientDimension()<<std::endl; |
---|
104 | s<<"INEQUALITIES"<<std::endl; |
---|
105 | s<<toString(i); |
---|
106 | s<<"EQUATIONS"<<std::endl; |
---|
107 | s<<toString(e); |
---|
108 | return s.str(); |
---|
109 | } |
---|
110 | |
---|
111 | void *bbcone_Init(blackbox *b) |
---|
112 | { |
---|
113 | return (void*)(new gfan::ZCone()); |
---|
114 | } |
---|
115 | |
---|
116 | BOOLEAN bbcone_Assign(leftv l, leftv r) |
---|
117 | { |
---|
118 | gfan::ZCone* newZc; |
---|
119 | if (r==NULL) |
---|
120 | { |
---|
121 | if (l->Data()!=NULL) |
---|
122 | { |
---|
123 | gfan::ZCone* zd = (gfan::ZCone*)l->Data(); |
---|
124 | delete zd; |
---|
125 | } |
---|
126 | newZc = new gfan::ZCone(); |
---|
127 | } |
---|
128 | else if (r->Typ()==l->Typ()) |
---|
129 | { |
---|
130 | if (l->Data()!=NULL) |
---|
131 | { |
---|
132 | gfan::ZCone* zd = (gfan::ZCone*)l->Data(); |
---|
133 | delete zd; |
---|
134 | } |
---|
135 | gfan::ZCone* zc = (gfan::ZCone*)r->Data(); |
---|
136 | newZc = new gfan::ZCone(*zc); |
---|
137 | } |
---|
138 | else if (r->Typ()==INT_CMD) |
---|
139 | { |
---|
140 | int ambientDim = (int)(long)r->Data(); |
---|
141 | if (ambientDim < 0) |
---|
142 | { |
---|
143 | Werror("expected an int >= 0, but got %d", ambientDim); |
---|
144 | return TRUE; |
---|
145 | } |
---|
146 | if (l->Data()!=NULL) |
---|
147 | { |
---|
148 | gfan::ZCone* zd = (gfan::ZCone*)l->Data(); |
---|
149 | delete zd; |
---|
150 | } |
---|
151 | newZc = new gfan::ZCone(ambientDim); |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | Werror("assign Type(%d) = Type(%d) not implemented",l->Typ(),r->Typ()); |
---|
156 | return TRUE; |
---|
157 | } |
---|
158 | |
---|
159 | if (l->rtyp==IDHDL) |
---|
160 | { |
---|
161 | IDDATA((idhdl)l->data)=(char *)newZc; |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | l->data=(void *)newZc; |
---|
166 | } |
---|
167 | return FALSE; |
---|
168 | } |
---|
169 | |
---|
170 | char * bbcone_String(blackbox *b, void *d) |
---|
171 | { if (d==NULL) return omStrDup("invalid object"); |
---|
172 | else |
---|
173 | { |
---|
174 | std::string s=toString(*((gfan::ZCone*)d)); |
---|
175 | char* ns = (char*) omAlloc(strlen(s.c_str()) + 10); |
---|
176 | sprintf(ns, "%s", s.c_str()); |
---|
177 | omCheckAddr(ns); |
---|
178 | return ns; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | void bbcone_destroy(blackbox *b, void *d) |
---|
183 | { |
---|
184 | if (d!=NULL) |
---|
185 | { |
---|
186 | gfan::ZCone* zc = (gfan::ZCone*) d; |
---|
187 | delete zc; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | void * bbcone_Copy(blackbox*b, void *d) |
---|
192 | { |
---|
193 | gfan::ZCone* zc = (gfan::ZCone*)d; |
---|
194 | gfan::ZCone* newZc = new gfan::ZCone(*zc); |
---|
195 | return newZc; |
---|
196 | } |
---|
197 | |
---|
198 | static BOOLEAN jjCONERAYS1(leftv res, leftv v) |
---|
199 | { |
---|
200 | /* method for generating a cone object from half-lines |
---|
201 | (cone = convex hull of the half-lines; note: there may be |
---|
202 | entire lines in the cone); |
---|
203 | valid parametrizations: (intmat) */ |
---|
204 | intvec* rays = (intvec *)v->CopyD(INTVEC_CMD); |
---|
205 | gfan::ZMatrix zm = intmat2ZMatrix(rays); |
---|
206 | gfan::ZCone* zc = new gfan::ZCone(); |
---|
207 | *zc = gfan::ZCone::givenByRays(zm, gfan::ZMatrix(0, zm.getWidth())); |
---|
208 | res->rtyp = coneID; |
---|
209 | res->data = (char *)zc; |
---|
210 | return FALSE; |
---|
211 | } |
---|
212 | |
---|
213 | static BOOLEAN jjCONERAYS2(leftv res, leftv u, leftv v) |
---|
214 | { |
---|
215 | /* method for generating a cone object from half-lines, |
---|
216 | and lines (any point in the cone being the sum of a point |
---|
217 | in the convex hull of the half-lines and a point in the span |
---|
218 | of the lines; the second argument may contain or entirely consist |
---|
219 | of zero rows); |
---|
220 | valid parametrizations: (intmat, intmat) |
---|
221 | Errors will be invoked in the following cases: |
---|
222 | - u and v have different numbers of columns */ |
---|
223 | intvec* rays = (intvec *)u->CopyD(INTVEC_CMD); |
---|
224 | intvec* linSpace = (intvec *)v->CopyD(INTVEC_CMD); |
---|
225 | if (rays->cols() != linSpace->cols()) |
---|
226 | { |
---|
227 | Werror("expected same number of columns but got %d vs. %d", |
---|
228 | rays->cols(), linSpace->cols()); |
---|
229 | return TRUE; |
---|
230 | } |
---|
231 | gfan::ZMatrix zm1 = intmat2ZMatrix(rays); |
---|
232 | gfan::ZMatrix zm2 = intmat2ZMatrix(linSpace); |
---|
233 | gfan::ZCone* zc = new gfan::ZCone(); |
---|
234 | *zc = gfan::ZCone::givenByRays(zm1, zm2); |
---|
235 | res->rtyp = coneID; |
---|
236 | res->data = (char *)zc; |
---|
237 | return FALSE; |
---|
238 | } |
---|
239 | |
---|
240 | static BOOLEAN jjCONERAYS3(leftv res, leftv u, leftv v, leftv w) |
---|
241 | { |
---|
242 | /* method for generating a cone object from half-lines, |
---|
243 | and lines (any point in the cone being the sum of a point |
---|
244 | in the convex hull of the half-lines and a point in the span |
---|
245 | of the lines), and an integer k; |
---|
246 | valid parametrizations: (intmat, intmat, int); |
---|
247 | Errors will be invoked in the following cases: |
---|
248 | - u and v have different numbers of columns, |
---|
249 | - k not in [0..3]; |
---|
250 | if the 2^0-bit of k is set, then the lineality space is known |
---|
251 | to be the span of the provided lines; |
---|
252 | if the 2^1-bit of k is set, then the extreme rays are known: |
---|
253 | each half-line spans a (different) extreme ray */ |
---|
254 | intvec* rays = (intvec *)u->CopyD(INTVEC_CMD); |
---|
255 | intvec* linSpace = (intvec *)v->CopyD(INTVEC_CMD); |
---|
256 | if (rays->cols() != linSpace->cols()) |
---|
257 | { |
---|
258 | Werror("expected same number of columns but got %d vs. %d", |
---|
259 | rays->cols(), linSpace->cols()); |
---|
260 | return TRUE; |
---|
261 | } |
---|
262 | int k = (int)(long)w->Data(); |
---|
263 | if ((k < 0) || (k > 3)) |
---|
264 | { |
---|
265 | WerrorS("expected int argument in [0..3]"); |
---|
266 | return TRUE; |
---|
267 | } |
---|
268 | gfan::ZMatrix zm1 = intmat2ZMatrix(rays); |
---|
269 | gfan::ZMatrix zm2 = intmat2ZMatrix(linSpace); |
---|
270 | gfan::ZCone* zc = new gfan::ZCone(); |
---|
271 | *zc = gfan::ZCone::givenByRays(zm1, zm2); |
---|
272 | //k should be passed on to zc; not available yet |
---|
273 | res->rtyp = coneID; |
---|
274 | res->data = (char *)zc; |
---|
275 | return FALSE; |
---|
276 | } |
---|
277 | |
---|
278 | BOOLEAN cone_via_rays(leftv res, leftv args) |
---|
279 | { |
---|
280 | leftv u = args; |
---|
281 | if ((u != NULL) && (u->Typ() == INTMAT_CMD)) |
---|
282 | { |
---|
283 | if (u->next == NULL) return jjCONERAYS1(res, u); |
---|
284 | } |
---|
285 | leftv v = u->next; |
---|
286 | if ((v != NULL) && (v->Typ() == INTMAT_CMD)) |
---|
287 | { |
---|
288 | if (v->next == NULL) return jjCONERAYS2(res, u, v); |
---|
289 | } |
---|
290 | leftv w = v->next; |
---|
291 | if ((w != NULL) && (w->Typ() == INT_CMD)) |
---|
292 | { |
---|
293 | if (w->next == NULL) return jjCONERAYS3(res, u, v, w); |
---|
294 | } |
---|
295 | WerrorS("cone_via_rays: unexpected parameters"); |
---|
296 | return TRUE; |
---|
297 | } |
---|
298 | |
---|
299 | static BOOLEAN jjCONENORMALS1(leftv res, leftv v) |
---|
300 | { |
---|
301 | /* method for generating a cone object from inequalities; |
---|
302 | valid parametrizations: (intmat) */ |
---|
303 | intvec* inequs = (intvec *)v->CopyD(INTVEC_CMD); |
---|
304 | gfan::ZMatrix zm = intmat2ZMatrix(inequs); |
---|
305 | gfan::ZCone* zc = new gfan::ZCone(zm, gfan::ZMatrix(0, zm.getWidth())); |
---|
306 | res->rtyp = coneID; |
---|
307 | res->data = (char *)zc; |
---|
308 | return FALSE; |
---|
309 | } |
---|
310 | |
---|
311 | static BOOLEAN jjCONENORMALS2(leftv res, leftv u, leftv v) |
---|
312 | { |
---|
313 | /* method for generating a cone object from iequalities, |
---|
314 | and equations (...) |
---|
315 | valid parametrizations: (intmat, intmat) |
---|
316 | Errors will be invoked in the following cases: |
---|
317 | - u and v have different numbers of columns */ |
---|
318 | intvec* inequs = (intvec *)u->CopyD(INTVEC_CMD); |
---|
319 | intvec* equs = (intvec *)v->CopyD(INTVEC_CMD); |
---|
320 | if (inequs->cols() != equs->cols()) |
---|
321 | { |
---|
322 | Werror("expected same number of columns but got %d vs. %d", |
---|
323 | inequs->cols(), equs->cols()); |
---|
324 | return TRUE; |
---|
325 | } |
---|
326 | gfan::ZMatrix zm1 = intmat2ZMatrix(inequs); |
---|
327 | gfan::ZMatrix zm2 = intmat2ZMatrix(equs); |
---|
328 | gfan::ZCone* zc = new gfan::ZCone(zm1, zm2); |
---|
329 | res->rtyp = coneID; |
---|
330 | res->data = (char *)zc; |
---|
331 | return FALSE; |
---|
332 | } |
---|
333 | |
---|
334 | static BOOLEAN jjCONENORMALS3(leftv res, leftv u, leftv v, leftv w) |
---|
335 | { |
---|
336 | /* method for generating a cone object from inequalities, equations, |
---|
337 | and an integer k; |
---|
338 | valid parametrizations: (intmat, intmat, int); |
---|
339 | Errors will be invoked in the following cases: |
---|
340 | - u and v have different numbers of columns, |
---|
341 | - k not in [0..3]; |
---|
342 | if the 2^0-bit of k is set, then ... */ |
---|
343 | intvec* inequs = (intvec *)u->CopyD(INTVEC_CMD); |
---|
344 | intvec* equs = (intvec *)v->CopyD(INTVEC_CMD); |
---|
345 | if (inequs->cols() != equs->cols()) |
---|
346 | { |
---|
347 | Werror("expected same number of columns but got %d vs. %d", |
---|
348 | inequs->cols(), equs->cols()); |
---|
349 | return TRUE; |
---|
350 | } |
---|
351 | int k = (int)(long)w->Data(); |
---|
352 | if ((k < 0) || (k > 3)) |
---|
353 | { |
---|
354 | WerrorS("expected int argument in [0..3]"); |
---|
355 | return TRUE; |
---|
356 | } |
---|
357 | gfan::ZMatrix zm1 = intmat2ZMatrix(inequs); |
---|
358 | gfan::ZMatrix zm2 = intmat2ZMatrix(equs); |
---|
359 | gfan::ZCone* zc = new gfan::ZCone(zm1, zm2, k); |
---|
360 | res->rtyp = coneID; |
---|
361 | res->data = (char *)zc; |
---|
362 | return FALSE; |
---|
363 | } |
---|
364 | |
---|
365 | BOOLEAN cone_via_normals(leftv res, leftv args) |
---|
366 | { |
---|
367 | leftv u = args; |
---|
368 | if ((u != NULL) && (u->Typ() == INTMAT_CMD)) |
---|
369 | { |
---|
370 | if (u->next == NULL) return jjCONENORMALS1(res, u); |
---|
371 | } |
---|
372 | leftv v = u->next; |
---|
373 | if ((v != NULL) && (v->Typ() == INTMAT_CMD)) |
---|
374 | { |
---|
375 | if (v->next == NULL) return jjCONENORMALS2(res, u, v); |
---|
376 | } |
---|
377 | leftv w = v->next; |
---|
378 | if ((w != NULL) && (w->Typ() == INT_CMD)) |
---|
379 | { |
---|
380 | if (w->next == NULL) return jjCONENORMALS3(res, u, v, w); |
---|
381 | } |
---|
382 | WerrorS("cone_via_normals: unexpected parameters"); |
---|
383 | return TRUE; |
---|
384 | } |
---|
385 | |
---|
386 | static BOOLEAN jjGETPROPC(leftv res, leftv u, leftv v) |
---|
387 | { |
---|
388 | /* method for retrieving cone properties; |
---|
389 | valid parametrizations: (cone, string), |
---|
390 | Errors will be invoked in the following cases: |
---|
391 | - invalid property string (see below for valid ones) */ |
---|
392 | gfan::ZCone* zc = (gfan::ZCone*)u->Data(); |
---|
393 | char* prop = (char*)v->Data(); |
---|
394 | gfan::ZMatrix retMat; |
---|
395 | gfan::ZCone retCone; |
---|
396 | int retInt; |
---|
397 | gfan::ZVector retVec; |
---|
398 | int typeInfo; |
---|
399 | |
---|
400 | /* ################ properties with return type intmat: ################## */ |
---|
401 | if (strcmp(prop, "INEQUALITIES") == 0) |
---|
402 | { |
---|
403 | retMat = zc->getInequalities(); |
---|
404 | typeInfo = INTMAT_CMD; |
---|
405 | } |
---|
406 | else if (strcmp(prop, "EQUATIONS") == 0) |
---|
407 | { |
---|
408 | retMat = zc->getEquations(); |
---|
409 | typeInfo = INTMAT_CMD; |
---|
410 | } |
---|
411 | else if (strcmp(prop, "FACETS") == 0) |
---|
412 | { |
---|
413 | retMat = zc->getFacets(); |
---|
414 | typeInfo = INTMAT_CMD; |
---|
415 | } |
---|
416 | else if (strcmp(prop, "IMPLIED_EQUATIONS") == 0) |
---|
417 | { |
---|
418 | retMat = zc->getImpliedEquations(); |
---|
419 | typeInfo = INTMAT_CMD; |
---|
420 | } |
---|
421 | else if (strcmp(prop, "GENERATORS_OF_SPAN") == 0) |
---|
422 | { |
---|
423 | retMat = zc->generatorsOfSpan(); |
---|
424 | typeInfo = INTMAT_CMD; |
---|
425 | } |
---|
426 | else if (strcmp(prop, "GENERATORS_OF_LINEALITY_SPACE") == 0) |
---|
427 | { |
---|
428 | retMat = zc->generatorsOfLinealitySpace(); |
---|
429 | typeInfo = INTMAT_CMD; |
---|
430 | } |
---|
431 | else if (strcmp(prop, "RAYS") == 0) |
---|
432 | { |
---|
433 | retMat = zc->extremeRays(); |
---|
434 | typeInfo = INTMAT_CMD; |
---|
435 | } |
---|
436 | else if (strcmp(prop, "QUOTIENT_LATTICE_BASIS") == 0) |
---|
437 | { |
---|
438 | retMat = zc->quotientLatticeBasis(); |
---|
439 | typeInfo = INTMAT_CMD; |
---|
440 | } |
---|
441 | else if (strcmp(prop, "LINEAR_FORMS") == 0) |
---|
442 | { |
---|
443 | retMat = zc->getLinearForms(); |
---|
444 | typeInfo = INTMAT_CMD; |
---|
445 | } |
---|
446 | /* ################ properties with return type int: ################## */ |
---|
447 | else if (strcmp(prop, "AMBIENT_DIM") == 0) |
---|
448 | { |
---|
449 | retInt = zc->ambientDimension(); |
---|
450 | typeInfo = INT_CMD; |
---|
451 | } |
---|
452 | else if (strcmp(prop, "DIM") == 0) |
---|
453 | { |
---|
454 | retInt = zc->dimension(); |
---|
455 | typeInfo = INT_CMD; |
---|
456 | } |
---|
457 | else if (strcmp(prop, "LINEALITY_DIM") == 0) |
---|
458 | { |
---|
459 | retInt = zc->dimensionOfLinealitySpace(); |
---|
460 | typeInfo = INT_CMD; |
---|
461 | } |
---|
462 | else if (strcmp(prop, "MULTIPLICITY") == 0) |
---|
463 | { |
---|
464 | bool ok = true; |
---|
465 | retInt = integerToInt(zc->getMultiplicity(), ok); |
---|
466 | if (!ok) |
---|
467 | WerrorS("overflow while converting a gfan::Integer to an int"); |
---|
468 | typeInfo = INT_CMD; |
---|
469 | } |
---|
470 | else if (strcmp(prop, "IS_ORIGIN") == 0) |
---|
471 | { |
---|
472 | retInt = zc->isOrigin() ? 1 : 0; |
---|
473 | typeInfo = INT_CMD; |
---|
474 | } |
---|
475 | else if (strcmp(prop, "IS_FULL_SPACE") == 0) |
---|
476 | { |
---|
477 | retInt = zc->isFullSpace() ? 1 : 0; |
---|
478 | typeInfo = INT_CMD; |
---|
479 | } |
---|
480 | else if (strcmp(prop, "SIMPLICIAL") == 0) |
---|
481 | { |
---|
482 | retInt = zc->isSimplicial() ? 1 : 0; |
---|
483 | typeInfo = INT_CMD; |
---|
484 | } |
---|
485 | else if (strcmp(prop, "CONTAINS_POSITIVE_VECTOR") == 0) |
---|
486 | { |
---|
487 | retInt = zc->containsPositiveVector() ? 1 : 0; |
---|
488 | typeInfo = INT_CMD; |
---|
489 | } |
---|
490 | /* ################ properties with return type ZCone: ################## */ |
---|
491 | else if (strcmp(prop, "LINEALITY_SPACE") == 0) |
---|
492 | { |
---|
493 | retCone = zc->linealitySpace(); |
---|
494 | typeInfo = coneID; |
---|
495 | } |
---|
496 | else if (strcmp(prop, "DUAL_CONE") == 0) |
---|
497 | { |
---|
498 | retCone = zc->dualCone(); |
---|
499 | typeInfo = coneID; |
---|
500 | } |
---|
501 | else if (strcmp(prop, "NEGATED") == 0) |
---|
502 | { |
---|
503 | retCone = zc->negated(); |
---|
504 | typeInfo = coneID; |
---|
505 | } |
---|
506 | /* ################ properties with return type intvec: ################## */ |
---|
507 | else if (strcmp(prop, "SEMI_GROUP_GENERATOR") == 0) |
---|
508 | { |
---|
509 | /* test whether the cone's dim = dim of lin space + 1: */ |
---|
510 | int d = zc->dimension(); |
---|
511 | int dLS = zc->dimensionOfLinealitySpace(); |
---|
512 | if (d == dLS + 1) |
---|
513 | retVec = zc->semiGroupGeneratorOfRay(); |
---|
514 | else |
---|
515 | { |
---|
516 | Werror("expected dim of cone one larger than dim of lin space\n" |
---|
517 | "but got dimensions %d and %d", d, dLS); |
---|
518 | } |
---|
519 | typeInfo = INTVEC_CMD; |
---|
520 | } |
---|
521 | else if (strcmp(prop, "RELATIVE_INTERIOR_POINT") == 0) |
---|
522 | { |
---|
523 | retVec = zc->getRelativeInteriorPoint(); |
---|
524 | typeInfo = INTVEC_CMD; |
---|
525 | } |
---|
526 | else if (strcmp(prop, "UNIQUE_POINT") == 0) |
---|
527 | { |
---|
528 | retVec = zc->getUniquePoint(); |
---|
529 | typeInfo = INTVEC_CMD; |
---|
530 | } |
---|
531 | else |
---|
532 | { |
---|
533 | Werror("unexpected cone property '%s'", prop); |
---|
534 | return TRUE; |
---|
535 | } |
---|
536 | |
---|
537 | res->rtyp = typeInfo; |
---|
538 | if (typeInfo == INT_CMD) |
---|
539 | res->data = (void*)retInt; |
---|
540 | else if (typeInfo == INTMAT_CMD) |
---|
541 | res->data = (void*)zMatrix2Intvec(retMat); |
---|
542 | else if (typeInfo == coneID) |
---|
543 | res->data = (void*)new gfan::ZCone(retCone); |
---|
544 | else if (typeInfo == INTVEC_CMD) |
---|
545 | res->data = (void*)zVector2Intvec(retVec); |
---|
546 | else |
---|
547 | { |
---|
548 | WerrorS("implementation error in bbcone.cc::jjGETPROPC"); |
---|
549 | return TRUE; |
---|
550 | } |
---|
551 | return FALSE; |
---|
552 | } |
---|
553 | |
---|
554 | BOOLEAN getprop(leftv res, leftv args) |
---|
555 | { |
---|
556 | leftv u = args; |
---|
557 | if ((u != NULL) && (u->Typ() == coneID)) |
---|
558 | { |
---|
559 | leftv v = u->next; |
---|
560 | if ((v != NULL) && (v->Typ() == STRING_CMD)) |
---|
561 | { |
---|
562 | if (v->next == NULL) return jjGETPROPC(res, u, v); |
---|
563 | } |
---|
564 | } |
---|
565 | WerrorS("getprop: unexpected parameters"); |
---|
566 | return TRUE; |
---|
567 | } |
---|
568 | |
---|
569 | BOOLEAN cone_intersect(leftv res, leftv args) |
---|
570 | { |
---|
571 | leftv u = args; |
---|
572 | if ((u != NULL) && (u->Typ() == coneID)) |
---|
573 | { |
---|
574 | leftv v = u->next; |
---|
575 | if ((v != NULL) && (v->Typ() == coneID)) |
---|
576 | { |
---|
577 | gfan::ZCone* zc1 = (gfan::ZCone*)u->Data(); |
---|
578 | gfan::ZCone* zc2 = (gfan::ZCone*)v->Data(); |
---|
579 | int d1 = zc1->ambientDimension(); |
---|
580 | int d2 = zc2->ambientDimension(); |
---|
581 | if (d1 != d2) |
---|
582 | Werror("expected ambient dims of both cones to coincide\n" |
---|
583 | "but got %d and %d", d1, d2); |
---|
584 | gfan::ZCone zc3 = gfan::intersection(*zc1, *zc2); |
---|
585 | res->rtyp = coneID; |
---|
586 | res->data = (void *)new gfan::ZCone(zc3); |
---|
587 | return FALSE; |
---|
588 | } |
---|
589 | } |
---|
590 | WerrorS("cone_intersect: unexpected parameters"); |
---|
591 | return TRUE; |
---|
592 | } |
---|
593 | |
---|
594 | BOOLEAN cone_link(leftv res, leftv args) |
---|
595 | { |
---|
596 | leftv u = args; |
---|
597 | if ((u != NULL) && (u->Typ() == coneID)) |
---|
598 | { |
---|
599 | leftv v = u->next; |
---|
600 | if ((v != NULL) && (v->Typ() == INTVEC_CMD)) |
---|
601 | { |
---|
602 | gfan::ZCone* zc = (gfan::ZCone*)u->Data(); |
---|
603 | intvec* iv = (intvec*)v->Data(); |
---|
604 | gfan::ZVector zv= intvec2ZVector(iv); |
---|
605 | int d1 = zc->ambientDimension(); |
---|
606 | int d2 = zv.size(); |
---|
607 | if (d1 != d2) |
---|
608 | Werror("expected ambient dim of cone and size of vector\n" |
---|
609 | "to be equal but got %d and %d", d1, d2); |
---|
610 | if(!zc->contains(zv)) |
---|
611 | { |
---|
612 | WerrorS("the provided intvec does not lie in the cone"); |
---|
613 | } |
---|
614 | res->rtyp = coneID; |
---|
615 | res->data = (void *)new gfan::ZCone(zc->link(zv)); |
---|
616 | return FALSE; |
---|
617 | } |
---|
618 | } |
---|
619 | WerrorS("cone_link: unexpected parameters"); |
---|
620 | return TRUE; |
---|
621 | } |
---|
622 | |
---|
623 | static BOOLEAN jjCONTAINS2(leftv res, leftv u, leftv v) |
---|
624 | { |
---|
625 | gfan::ZCone* zc1 = (gfan::ZCone*)u->Data(); |
---|
626 | gfan::ZCone* zc2 = (gfan::ZCone*)v->Data(); |
---|
627 | int d1 = zc1->ambientDimension(); |
---|
628 | int d2 = zc2->ambientDimension(); |
---|
629 | if (d1 != d2) |
---|
630 | Werror("expected cones with same ambient dimensions\n but got" |
---|
631 | " dimensions %d and %d", d1, d2); |
---|
632 | res->rtyp = INT_CMD; |
---|
633 | res->data = (void *)(zc1->contains(*zc2) ? 1 : 0); |
---|
634 | return FALSE; |
---|
635 | } |
---|
636 | |
---|
637 | static BOOLEAN jjCONTAINS3(leftv res, leftv u, leftv v, leftv w) |
---|
638 | { |
---|
639 | gfan::ZCone* zc = (gfan::ZCone*)u->Data(); |
---|
640 | intvec* vec = (intvec*)v->Data(); |
---|
641 | int flag = (int)(long)w->Data(); |
---|
642 | gfan::ZVector zv = intvec2ZVector(vec); |
---|
643 | int d1 = zc->ambientDimension(); |
---|
644 | int d2 = zv.size(); |
---|
645 | if (d1 != d2) |
---|
646 | Werror("expected ambient dim of cone and size of vector\n" |
---|
647 | "to be equal but got %d and %d", d1, d2); |
---|
648 | res->rtyp = INT_CMD; |
---|
649 | if (flag) |
---|
650 | res->data = (void *)(zc->containsRelatively(zv) ? 1 : 0); |
---|
651 | else |
---|
652 | res->data = (void *)(zc->contains(zv) ? 1 : 0);; |
---|
653 | return FALSE; |
---|
654 | } |
---|
655 | |
---|
656 | BOOLEAN contains(leftv res, leftv args) |
---|
657 | { |
---|
658 | |
---|
659 | leftv u = args; |
---|
660 | if ((u != NULL) && (u->Typ() == coneID)) |
---|
661 | { |
---|
662 | leftv v = u->next; |
---|
663 | if ((v != NULL) && (v->Typ() == coneID)) return jjCONTAINS2(res, u, v); |
---|
664 | if ((v != NULL) && (v->Typ() == INTVEC_CMD)) |
---|
665 | { |
---|
666 | leftv w = v->next; |
---|
667 | if ((w != NULL) && (w->Typ() == INT_CMD)) return jjCONTAINS3(res, u, v, w); |
---|
668 | } |
---|
669 | } |
---|
670 | WerrorS("contains: unexpected parameters"); |
---|
671 | return TRUE; |
---|
672 | } |
---|
673 | |
---|
674 | static BOOLEAN jjSETPROPC3A(leftv res, leftv u, leftv v, leftv w) |
---|
675 | { |
---|
676 | gfan::ZCone* zc = (gfan::ZCone*)u->Data(); |
---|
677 | char* prop = (char*)v->Data(); |
---|
678 | int val = (int)(long)w->Data(); |
---|
679 | |
---|
680 | if (strcmp(prop, "MULTIPLICITY") == 0) |
---|
681 | { |
---|
682 | zc->setMultiplicity(gfan::Integer(val)); |
---|
683 | } |
---|
684 | else |
---|
685 | { |
---|
686 | Werror("unexpected cone property '%s'", prop); |
---|
687 | return TRUE; |
---|
688 | } |
---|
689 | res->rtyp = NONE; |
---|
690 | res->data = NULL; |
---|
691 | return FALSE; |
---|
692 | } |
---|
693 | static BOOLEAN jjSETPROPC3B(leftv res, leftv u, leftv v, leftv w) |
---|
694 | { |
---|
695 | gfan::ZCone* zc = (gfan::ZCone*)u->Data(); |
---|
696 | char* prop = (char*)v->Data(); |
---|
697 | intvec* mat = (intvec*)w->Data(); |
---|
698 | gfan::ZMatrix zm = intmat2ZMatrix(mat); |
---|
699 | |
---|
700 | if (strcmp(prop, "LINEAR_FORMS") == 0) |
---|
701 | { |
---|
702 | zc->setLinearForms(zm); |
---|
703 | } |
---|
704 | else |
---|
705 | { |
---|
706 | Werror("unexpected cone property '%s'", prop); |
---|
707 | return TRUE; |
---|
708 | } |
---|
709 | res->rtyp = NONE; |
---|
710 | res->data = NULL; |
---|
711 | return FALSE; |
---|
712 | } |
---|
713 | |
---|
714 | BOOLEAN setprop(leftv res, leftv args) |
---|
715 | { |
---|
716 | |
---|
717 | leftv u = args; |
---|
718 | if ((u != NULL) && (u->Typ() == coneID)) |
---|
719 | { |
---|
720 | leftv v = u->next; |
---|
721 | if ((v != NULL) && (v->Typ() == STRING_CMD)) |
---|
722 | { |
---|
723 | leftv w = v->next; |
---|
724 | if ((w != NULL) && (w->Typ() == INT_CMD)) return jjSETPROPC3A(res, u, v, w); |
---|
725 | if ((w != NULL) && (w->Typ() == INTMAT_CMD)) return jjSETPROPC3B(res, u, v, w); |
---|
726 | } |
---|
727 | } |
---|
728 | WerrorS("setprop: unexpected parameters"); |
---|
729 | return TRUE; |
---|
730 | } |
---|
731 | |
---|
732 | void bbcone_setup() |
---|
733 | { |
---|
734 | blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox)); |
---|
735 | // all undefined entries will be set to default in setBlackboxStuff |
---|
736 | // the default Print is quite usefule, |
---|
737 | // all other are simply error messages |
---|
738 | b->blackbox_destroy=bbcone_destroy; |
---|
739 | b->blackbox_String=bbcone_String; |
---|
740 | //b->blackbox_Print=blackbox_default_Print; |
---|
741 | b->blackbox_Init=bbcone_Init; |
---|
742 | b->blackbox_Copy=bbcone_Copy; |
---|
743 | b->blackbox_Assign=bbcone_Assign; |
---|
744 | iiAddCproc("","cone_via_rays",FALSE,cone_via_rays); |
---|
745 | iiAddCproc("","cone_via_normals",FALSE,cone_via_normals); |
---|
746 | iiAddCproc("","getprop",FALSE,getprop); |
---|
747 | iiAddCproc("","cone_intersect",FALSE,cone_intersect); |
---|
748 | iiAddCproc("","cone_link",FALSE,cone_link); |
---|
749 | iiAddCproc("","contains",FALSE,contains); |
---|
750 | iiAddCproc("","setprop",FALSE,setprop); |
---|
751 | coneID=setBlackboxStuff(b,"cone"); |
---|
752 | Print("created type %d (cone)\n",coneID); |
---|
753 | } |
---|
754 | |
---|
755 | #endif |
---|
756 | /* HAVE_FANS */ |
---|