1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 /*
9  * (C) COPYRIGHT International Business Machines Corp. 1992, 1997
10  * All Rights Reserved
11  * Licensed Materials - Property of IBM
12  *
13  * US Government Users Restricted Rights - Use, duplication or
14  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
15  */
16 
17 #include <dxconfig.h>
18 
19 #if defined(HAVE_CTYPE_H)
20 #include <ctype.h>
21 #endif
22 
23 #if defined(HAVE_STRING_H)
24 #include <string.h>
25 #endif
26 
27 #include <dx/dx.h>
28 #include <math.h>
29 
30 /* things to add - components w/ no dep/ref/der - copy first.
31  *  handle renumbering of ref components
32  */
33 
34 
35 
36 /* generic groups can be handled three different ways:
37  *  1. if group members are fields, create a 0 .. N set of series pos
38  *     and stack the members and output a single field.
39  *  2. if the members are different series, stack each one separately
40  *     and output a group of stacked fields.
41  *  3. since a single field coming into stack gets the positions up'd by 1D.
42  *     a group of fields could also be interpreted as a request to up each
43  *     field by 1d and output a group of fields.
44  * given my druthers i guess i'd either prefer always #1, or look at the
45  * members and do either 1 or 2 based on what the first member type is.
46  *
47  * and how should multigrids be treated?  for now the same as generic groups,
48  * whatever i decide to do with them.
49  */
50 
51 typedef struct stackparms { /* input from the user */
52     Object inobj;        /* original top level object */
53     int stackdim;        /* connection dimension to stack on */
54     int newposdim;       /* new dimension to add to pos if connD != posD */
55 } StackParms;
56 
57 typedef struct stackinfo {  /* stuff specific to each group/series/field */
58     Object thisobj;      /* the thing currently being stacked */
59     Class inclass;       /* kind (class) of input object */
60     int stackflags;      /* flags - set for handling special cases */
61     int posdim;          /* dimensionality of positions */
62     int conndim;         /* dimensionality of connections */
63     int membercnt;       /* number of members in the series */
64     Field f0;            /* first field, used to compare to rest of stack */
65     Array ap0;           /* first positions array */
66     Array ac0;           /* first connections array */
67     int *countlist;      /* connections counts in stacked obj */
68     char **compname;     /* list of component names in every member */
69     int *compflags;      /* flags per component name */
70     int compcount;       /* count of components in list */
71 /* should stackparms pointer be here as well? */
72 } StackInfo;
73 
74 /* bitmasks for stackflags - not all used */
75 #define PARTITIONED  0x1    /* input is partitioned */
76 #define PD_NE_CD     0x2    /* connD < positionD e.g. lines in 2space */
77 #define PARTIALCOMP  0x4    /* some fields have components missing in others */
78 #define DIFFDELTAS   0x8    /* inputs are regular but have different deltas */
79 #define JUSTPOS      0x10   /* input is a field; just stack positions */
80 #define CREATESERIES 0x20   /* input is group; create artificial series */
81 #define CONTAINER    0x40   /* top obj is container; needs traverse */
82 #define EMPTYOUT     0x80   /* entire input is empty fields */
83 #define INVALIDS     0x100  /* input has some sort of invalid component: */
84 #define INVPOS       0x200  /* invalid positions */
85 #define INVCON       0x400  /*    "    connections */
86 #define HASREFS      0x800  /* has at least one "ref" component */
87 #define POINTS       0x1000 /* input is a series of arrays */
88 
89 /* bitmasks for compflags */
90 #define SKIP         0x1    /* skip this component while stacking */
91 #define WARNEDDCON   0x2    /* warning already printed for dep conn */
92 #define WARNEDMISS   0x4    /* warning printed for comp not in all fields */
93 #define ISREF        0x8    /* is ref another component and needs renumbering */
94 #define DOFIRST      0x10   /* if set, copy only first occurance of component */
95 #define DONEONE      0x20   /* set when one copy has been done */
96 
97 
98 /* general bit operators */
99 #define SET(flag, mask)    (flag |= mask)
100 #define CLR(flag, mask)    (flag &= ~mask)
101 #define ISSET(flag, mask)  ((flag & mask) == mask)
102 #define ISCLR(flag, mask)  ((flag & mask) == 0)
103 
104 #define INTERNALERROR DXSetError(ERROR_INTERNAL, "at line %d, file %s", \
105 				 __LINE__, __FILE__)
106 
107 #define NORMFUZZ   (DXD_FLOAT_EPSILON*20.0)
108 				/* sort of a normalized fuzz.  on normalized
109 				 * numbers (around 1.0), accept a difference
110 				 * of 2 in the second least significant digit
111 				 * when taking the difference of two deltas.
112 			         */
113 
114 
115 
116 
117 static Error parseparms(Object *in, StackParms *sp);
118 static Object doStack(StackParms *sp, StackInfo *si);
119 static Error setupwork(StackParms *sp, StackInfo *si);
120 static Object dowork(StackParms *sp, StackInfo *si);
121 static Object stackpos(StackParms *sp, StackInfo *si, float where);
122 static Array add1posdim(Array oldpos, int newdim, float where);
123 static Array addNposdim(StackInfo *si, int nflag, int oflag, int newdim, Array addpos);
124 static Array addpositions(StackParms *sp, StackInfo *si);
125 static Array addcondim(StackInfo *si, Array oldcon, int newdim, int newcount);
126 static Array addarrays(StackParms *sp, StackInfo *si, char *component, int flags);
127 static Array addirregarrays(StackInfo *si, char *component, int stackdim);
128 static Array addrefarrays(StackInfo *si, char *component, int stackdim);
129 static Array makerefs(StackInfo *si, int stackdim);
130 static Error dorenumber(Array newarr, Array renumber);
131 static Array addcnstarrays(StackParms *sp, StackInfo *si, char *component);
132 static Array addseriesdim(StackInfo *si, Array oldpos, Array addpos, int stackdim);
133 static Error newdimregular(StackParms *sp, StackInfo *si, Array *newarr, int *regflag);
134 static Error allposregular(StackInfo *si, int *regflag);
135 static Error seriesregular(StackParms *sp, StackInfo *si, Array *newpos);
136 static Array seriesspacing(StackParms *sp, StackInfo *si);
137 static Object stackgroup(StackParms *sp, StackInfo *si);
138 static Field getfirstfield(StackInfo *si);
139 static Error checksample(StackParms *sp, StackInfo *si);
140 static Error checkall(StackParms *sp, StackInfo *si);
141 static Error arraymatch(Array a1, Array a2, int countflag, char *name);
142 static Error ispartitioned(StackInfo *si);
143 static Error isstackable(StackParms *sp, StackInfo *si);
144 static Object invertobj(StackInfo *si);
145 static Object convertseries(StackInfo *si);
146 static Field setinvalids(StackInfo *si, Field f);
147 static void freespace(StackInfo *si);
148 static double nonzeromax(double a, double b);
149 static Array foolInvalids(Array a, char *name, Field f, int seriesmember);
150 extern void _dxfPermute(int dim, Pointer out, int *ostrides, int *counts,  /* from libdx/permute.c */
151  		        int size, Pointer in, int *istrides);
152 
153 /*
154  *  inputs:  series, group or field
155  *           dimension in which to stack
156  *           position dim if stacking nD conns in (n+1)D pos space
157  *  output:  stacked data
158  */
159 Error
m_Stack(Object * in,Object * out)160 m_Stack(Object *in, Object *out)
161 {
162     StackParms sp;
163     StackInfo si;
164 
165     memset((Pointer)&sp, '\0', sizeof(StackParms));
166     memset((Pointer)&si, '\0', sizeof(StackInfo));
167 
168 
169     if (!parseparms(in, &sp))
170 	goto error;
171 
172 
173     si.thisobj = sp.inobj;
174 
175     out[0] = doStack(&sp, &si);
176     if (out[0] == NULL)
177 	goto error;
178 
179 
180     return OK;
181 
182   error:
183 
184     out[0] = NULL;
185     return ERROR;
186 }
187 
188 
189 /*
190  * check input parms and set defaults.  allow x, y, z strings as
191  * aliases for 0, 1, 2 as dim numbers.  there are two dimension
192  * numbers in the case that connD != posD.  you need to know
193  * what connD to stack in, and then the posD can be a different value.
194  * think about stacking 2D lines into 3D quads.  you can stack
195  * the lines horizonally or vertically, and then you can make the
196  * resulting sheet of quads be located parallel to the x, y or z axis.
197  * if connD == posD, the third input is ignored (actually is an error to be
198  * set to something different than the second input).
199  */
200 static Error
parseparms(Object * in,StackParms * sp)201 parseparms(Object *in, StackParms *sp)
202 {
203     char *cp;
204 
205     if (!in[0]) {
206 	DXSetError(ERROR_BAD_PARAMETER, "#10000", "input");
207 	goto error;
208     }
209     sp->inobj = in[0];
210 
211     if (!in[1])
212 	sp->stackdim = 0;
213     else {
214 	if (DXExtractInteger(in[1], &sp->stackdim)) {
215 	    if (sp->stackdim < 0) {
216 		DXSetError(ERROR_BAD_PARAMETER, "#10030", "dimension");
217 		goto error;
218 	    }
219 	} else if (DXExtractString(in[1], &cp)) {
220 	    if (isupper(*cp))
221 	        *cp = tolower(*cp);
222 	    if (*cp == 'x')
223 	        sp->stackdim = 0;
224 	    else if (*cp == 'y')
225 	        sp->stackdim = 1;
226 	    else if (*cp == 'z')
227 	        sp->stackdim = 2;
228 	    else {
229 		DXSetError(ERROR_BAD_PARAMETER, "#10211",
230 			   "dimension", "x, y, z");
231 		goto error;
232 	    }
233 	} else {
234 	    DXSetError(ERROR_BAD_PARAMETER, "#10650", "dimension");
235 	    goto error;
236 	}
237     }
238 
239 #if 1
240     sp->newposdim = sp->stackdim;
241 #else
242     if (!in[2])
243 	sp->newposdim = 0;
244     else {
245 	if (DXExtractInteger(in[2], &sp->newposdim)) {
246 	    if (sp->newposdim < 0) {
247 		DXSetError(ERROR_BAD_PARAMETER, "#10030", "positionsdim");
248 		goto error;
249 	    }
250 	} else if (DXExtractString(in[2], &cp)) {
251 	    if (strcmp(cp, "x"))
252 		sp->newposdim = 0;
253 	    else if (strcmp(cp, "y"))
254 		sp->newposdim = 1;
255 	    else if (strcmp(cp, "z"))
256 		sp->newposdim = 2;
257 	    else {
258 		DXSetError(ERROR_BAD_PARAMETER, "#10211",
259 			   "dimension", "x", "y", "z");
260 		goto error;
261 	    }
262 	} else {
263 	    DXSetError(ERROR_BAD_PARAMETER, "#10650", "positionsdim");
264 	    goto error;
265 	}
266     }
267 #endif
268 
269     return OK;
270 
271   error:
272     return ERROR;
273 }
274 
275 /* recursive routine.  stacks when it comes to a series, a
276  * group or a field.
277  */
278 
279 static Object
doStack(StackParms * sp,StackInfo * si)280 doStack(StackParms *sp, StackInfo *si)
281 {
282 
283     Object tostack, subo;
284     Object stacked = NULL;
285     Object returnobj = NULL;
286     int i;
287     Class inclass;
288 
289     tostack = si->thisobj;
290 
291     inclass = DXGetObjectClass(tostack);
292     if (inclass == CLASS_GROUP)
293 	inclass = DXGetGroupClass((Group)tostack);
294 
295     si->inclass = inclass;
296 
297     switch (inclass) {
298 
299       /* GOOD */
300 
301       case CLASS_SERIES:
302       case CLASS_FIELD:
303 
304 	/* "normal" cases */
305 
306 	/* free any pending space first before clearing this struct */
307 	freespace(si);
308 
309 	memset(si, '\0', sizeof(StackInfo));
310 	si->thisobj = tostack;
311 	si->inclass = inclass;
312 
313 	/* handle series of composite fields by restructuring the
314          * input into a compositefield of series of fields, then stack
315 	 * each one into a single field.
316 	 */
317 	if (ispartitioned(si)) {
318 	    returnobj = invertobj(si);
319 	    if (!returnobj)
320 		goto error;
321 
322 	    si->thisobj = returnobj;
323 
324 	    stacked = doStack(sp, si);
325 	    if (!stacked)
326 		goto error;
327 
328 	    DXDelete(returnobj);   /* delete temporary structures */
329 	    returnobj = stacked;
330 	    break;
331 	}
332 
333 	if (!setupwork(sp, si))
334 	    goto error;
335 
336 	if (!(returnobj = dowork(sp, si)))
337 	    goto error;
338 
339 	freespace(si);
340 	break;
341 
342       case CLASS_GROUP:
343 
344 	/* first check to see if the members are compatible fields.
345 	 * if so, stack them with 0 .. n series positions.  else
346 	 * assume they are separate objs and try to stack each one
347 	 * individually.
348 	 *
349 	 * given that it's now easy to convert a generic group into
350 	 * a series (ChangeGroupType) i'm not sure this code should
351 	 * be here - maybe we should force the user to make a series
352 	 * themselves before coming into stack.  if so, just have the
353 	 * class_group case be the same as mg and cf and skip this code.
354 	 */
355 
356 	if (isstackable(sp, si)) {
357 
358 	    returnobj = convertseries(si);
359 	    if (!returnobj)
360 		goto error;
361 
362 	    si->thisobj = returnobj;
363 
364 	    stacked = doStack(sp, si);
365 	    if (!stacked)
366 		goto error;
367 
368 	    DXDelete(returnobj);   /* delete temporary structures */
369 	    returnobj = stacked;
370 	    break;
371 	}
372 
373 	/* if not compatible objs, FALL THROUGH! */
374 
375       case CLASS_MULTIGRID:       /* ??? what should happen to one of these? */
376       case CLASS_COMPOSITEFIELD:
377 
378 	returnobj = DXCopy(tostack, COPY_STRUCTURE);
379 	if (!returnobj)
380 	    goto error;
381 
382 	for (i=0; (subo=DXGetEnumeratedMember((Group)returnobj, i, NULL)); i++) {
383 	    si->thisobj = subo;
384 
385 	    stacked = doStack(sp, si);
386 	    if (!stacked)
387 		goto error;
388 
389 	    if (!DXSetEnumeratedMember((Group)returnobj, i, stacked))
390 		goto error;
391 	}
392 
393 	break;
394 
395       /* container objs */
396       case CLASS_XFORM:
397 	DXGetXformInfo((Xform)tostack, (Object *)&subo, NULL);
398 	si->thisobj = subo;
399 
400 	stacked = doStack(sp, si);
401 	if (!stacked)
402 	    goto error;
403 
404 	returnobj = DXCopy(tostack, COPY_STRUCTURE);
405 	if (!returnobj)
406 	    goto error;
407 
408 	if (!DXSetXformObject((Xform)returnobj, stacked))
409 	    goto error;
410 
411 	break;
412 
413       case CLASS_SCREEN:
414 	DXGetScreenInfo((Screen)tostack, (Object *)&subo, NULL, NULL);
415 	si->thisobj = subo;
416 
417 	stacked = doStack(sp, si);
418 	if (!stacked)
419 	    goto error;
420 
421 	returnobj = DXCopy(tostack, COPY_STRUCTURE);
422 	if (!returnobj)
423 	    goto error;
424 
425 	if (!DXSetScreenObject((Screen)returnobj, stacked))
426 	    goto error;
427 
428 	break;
429 
430       case CLASS_CLIPPED:
431 	DXGetClippedInfo((Clipped)tostack, (Object *)&subo, NULL);
432 	si->thisobj = subo;
433 
434 	stacked = doStack(sp, si);
435 	if (!stacked)
436 	    goto error;
437 
438 	returnobj = DXCopy(tostack, COPY_STRUCTURE);
439 	if (!returnobj)
440 	    goto error;
441 
442 	if (!DXSetClippedObjects((Clipped)returnobj, stacked, NULL))
443 	    goto error;
444 
445 	break;
446 
447       /* BAD */
448       case CLASS_STRING:
449       case CLASS_ARRAY:
450       case CLASS_CAMERA:
451       case CLASS_LIGHT:
452       default:
453 	DXSetError(ERROR_BAD_PARAMETER, "#10190", "input");
454 	goto error;
455     }
456 
457 
458   /* done */
459     freespace(si);
460     return returnobj;
461 
462   error:
463     freespace(si);
464     DXDelete(returnobj);
465     return NULL;
466 }
467 
468 
469 /* things which need to be checked before the actual bytes
470  *  are moved around.
471  */
472 static Error
setupwork(StackParms * sp,StackInfo * si)473 setupwork(StackParms *sp, StackInfo *si)
474 {
475     if (!checksample(sp, si))
476 	return ERROR;
477 
478     if (ISSET(si->stackflags, JUSTPOS) || ISSET(si->stackflags, EMPTYOUT)
479 	|| ISSET(si->stackflags, POINTS))
480 	return OK;
481 
482     if (!checkall(sp, si))
483 	return ERROR;
484 
485     return OK;
486 }
487 
488 
489 
490 #define MAXRANK 8  /* largest inline rank supported.
491 		     * will handle larger by alloc'ing space for it
492 		     */
493 
494 
495 /* pull out the first member and check things we can tell from
496  * our selected victim.
497  */
498 static Error
checksample(StackParms * sp,StackInfo * si)499 checksample(StackParms *sp, StackInfo *si)
500 {
501     Field testf;
502     Array ap, ac, aa;
503     int i, j;
504     int invs;
505     int comps;
506     int count;
507     int thiscomp;
508     Type t;
509     Category c;
510     int rank, hereshape[MAXRANK], *genshape = NULL;
511     char *cp, *ccp, *acp;
512 
513 
514     invs = 0;
515     /* mark that we'll have to deal with these at some point.  you
516      * shouldn't have both, but it has been seen before.  can cause
517      * strange things to happen.
518      */
519     if (DXExists(si->thisobj, "invalid positions")) {
520 	SET(si->stackflags, INVALIDS);
521 	SET(si->stackflags, INVPOS);
522 	invs++;
523     }
524     if (DXExists(si->thisobj, "invalid connections")) {
525 	SET(si->stackflags, INVALIDS);
526 	SET(si->stackflags, INVCON);
527 	invs++;
528     }
529 
530 
531     /* get the first nonempty field in the input, if there is one */
532 
533     if (si->inclass == CLASS_FIELD) {
534 	SET(si->stackflags, JUSTPOS);
535 	si->membercnt = 1;
536 	testf = (Field)si->thisobj;
537     } else {
538 	if (!DXGetMemberCount((Group)si->thisobj, &si->membercnt))
539 	    goto error;
540 	testf = getfirstfield(si);
541     }
542 
543     if (!testf) {
544 	if (ISSET(si->stackflags, POINTS)) {
545 	    si->conndim = 0;
546 	    goto done;
547 	}
548 
549 	goto error;
550     }
551 
552     if (DXEmptyField(testf)) {
553 	SET(si->stackflags, EMPTYOUT);
554 	goto done;
555     }
556 
557 
558     /* now we have it; dissect it.
559      */
560     ap = (Array)DXGetComponentValue(testf, "positions");
561     if (!ap) {
562 	/* shouldn't happen - we've tested for empty fields above */
563 	INTERNALERROR;
564 	goto error;
565     }
566     if ((!DXTypeCheckV(ap, TYPE_FLOAT, CATEGORY_REAL, 0, NULL)) &&
567 	(!DXTypeCheckV(ap, TYPE_FLOAT, CATEGORY_REAL, 1, NULL))) {
568 	DXSetError(ERROR_DATA_INVALID, "#11383");
569 	goto error;
570     }
571 
572     if (!DXGetArrayInfo(ap, &count, &t, &c, &rank, hereshape))
573 	goto error;
574 
575     if (rank == 0)
576 	si->posdim = 0;
577     else
578 	si->posdim = hereshape[0];
579 
580     ac = (Array)DXGetComponentValue(testf, "connections");
581     if (!ac)
582 	si->conndim = 0;   /* no connections == scattered pts */
583     else {
584 	/* get the dimensionality and make sure it's regular */
585 	if ((!DXTypeCheckV(ac, TYPE_INT, CATEGORY_REAL, 0, NULL)) &&
586 	    (!DXTypeCheckV(ac, TYPE_INT, CATEGORY_REAL, 1, NULL))) {
587 	    DXSetError(ERROR_DATA_INVALID, "#11382");
588 	    goto error;
589 	}
590 
591 	if (!DXGetStringAttribute((Object)ac, "element type", &cp)) {
592 	    DXSetError(ERROR_DATA_INVALID, "#13070");
593 	    goto error;
594 	}
595 
596 	if (!DXQueryGridConnections(ac, &rank, NULL)) {
597 	    DXSetError(ERROR_DATA_INVALID, "connections must be fully regular");
598 	    goto error;
599 	}
600 
601 	/* think about tris here -> prisms */
602 	if (strcmp(cp, "points") == 0)
603 	    si->conndim = 0;
604 	else if (strcmp(cp, "lines") == 0)
605 	    si->conndim = 1;
606 	else if (strcmp(cp, "quads") == 0)
607 	    si->conndim = 2;
608 	else if (strcmp(cp, "cubes") == 0)
609 	    si->conndim = 3;
610 	else if (strncmp(cp, "cubes", 5) == 0) {
611 	    if (sscanf(cp+5, "%d", &si->conndim) != 1) {
612 		DXSetError(ERROR_DATA_INVALID, "#10610", "input");
613 		goto error;
614 	    }
615 	} else {
616 	    DXSetError(ERROR_DATA_INVALID, "#10610", "input");
617 	    goto error;
618 	}
619 
620     }
621 
622     if (sp->stackdim > si->conndim) {
623 	DXSetError(ERROR_BAD_PARAMETER, "#11360", sp->stackdim, "input",
624 		   si->conndim);
625 	goto error;
626     }
627     if (si->conndim != si->posdim) {
628 	if (sp->newposdim > si->posdim) {
629 	    DXSetError(ERROR_BAD_PARAMETER, "#11361", sp->newposdim, "input",
630 		       si->posdim);
631 	    goto error;
632 	}
633     } else if (sp->stackdim != sp->newposdim) {
634 	DXSetError(ERROR_BAD_PARAMETER, "positiondimension can only be specified for lines in 2D or 3D, or surfaces in 3D");
635 	goto error;
636     }
637 
638 
639     /* save information for comparison with other fields in the group.
640      */
641     si->f0 = testf;
642     si->ap0 = ap;
643     si->ac0 = ac;
644 
645     /* and save the component names in a list. we can only stack
646      * the ones found in all the fields.
647      */
648     comps = 0;
649     for (i=0; DXGetEnumeratedComponentValue(testf, i, &cp); i++) {
650 	/* skip any here for now */
651 	if (strncmp("invalid", cp, strlen("invalid")) == 0)
652 	    continue;
653 	comps++;
654     }
655 
656     si->compname = (char **)DXAllocateZero((comps + invs) * sizeof(char *));
657     if (!si->compname)
658 	goto error;
659     si->compflags = (int *)DXAllocateZero((comps + invs) * sizeof(int));
660     if (!si->compflags)
661 	goto error;
662 
663     si->compcount = 0;
664     for (i=0; (aa=(Array)DXGetEnumeratedComponentValue(testf, i, &ccp)); i++) {
665 	/* skip derived components */
666 	if (DXGetAttribute((Object)aa, "der"))
667 	    continue;
668 
669 	/* pos & conn will be handled separately */
670 	if (!strcmp("positions", ccp) || !strcmp("connections", ccp))
671 	    continue;
672 
673 	/* handle these separately below */
674 	if (strncmp("invalid", ccp, strlen("invalid")) == 0)
675 		continue;
676 
677 	si->compname[si->compcount] = (char *)DXAllocate(strlen(ccp)+1);
678 	if (!si->compname[si->compcount])
679 	    goto error;
680 	strcpy(si->compname[si->compcount], ccp);
681 	thiscomp = si->compcount;
682 	si->compcount++;
683 
684 
685 	/* rest of components */
686 	if (DXGetAttribute((Object)aa, "dep") ||
687 	    DXGetAttribute((Object)aa, "ref")) {
688 
689 	    /* fail if it's data, warn if any other comp */
690             if (DXGetStringAttribute((Object)aa, "dep", &acp)) {
691 
692 		if (!strcmp("connections", acp)) {
693 		    if (!strcmp("data", ccp)) {
694 			DXSetError(ERROR_NOT_IMPLEMENTED,
695 				   "cannot stack data which is dependent on connections");
696 			goto error;
697 		    } else {
698 			if (ISCLR(si->compflags[thiscomp], WARNEDDCON)) {
699 			    DXWarning("cannot stack components which are dependent on connections; skipping `%s' component", ccp);
700 			    SET(si->compflags[thiscomp], SKIP);
701 			    SET(si->compflags[thiscomp], WARNEDDCON);
702 			}
703 		    }
704 		}
705 	    }
706 
707 	    if (DXGetStringAttribute((Object)aa, "ref", &acp)) {
708 		SET(si->compflags[thiscomp], ISREF);
709 	    }
710 	} else {
711 	    /* if no dep, set a flag and only do the first */
712 	    SET(si->compflags[thiscomp], DOFIRST);
713 	}
714     }
715 
716     /* now look through list and if anything is marked ref, see if the
717      * target is skipped.  if so, we don't have to renumber the ref.
718      */
719     for (i=0; i<si->compcount; i++) {
720 	if (ISCLR(si->compflags[i], ISREF))
721 	    continue;
722 
723 	aa = (Array)DXGetComponentValue(testf, si->compname[i]);
724 	DXGetStringAttribute((Object)aa, "ref", &acp);
725 	if (!acp) {
726 	    CLR(si->compflags[i], ISREF);
727 	    continue;
728 	}
729 	for(j=0; j<si->compcount; j++) {
730 	    if (strcmp(acp, si->compname[j]))
731 		continue;
732 	    /* target of ref */
733 	    if (ISSET(si->compflags[j], SKIP))
734 		CLR(si->compflags[i], ISREF);
735 	}
736     }
737 
738     /* finally, if there are invalid pos or con, add in those components,
739      * but they will be treated specially since they can be either dep
740      * or ref.
741      */
742     for (i=0; i<invs; i++) {
743 	if (ISSET(si->stackflags, INVPOS)) {
744 	    si->compname[si->compcount] =
745 		(char *)DXAllocate(strlen("invalid positions")+1);
746 	    if (!si->compname[si->compcount])
747 		goto error;
748 	    strcpy(si->compname[si->compcount], "invalid positions");
749 	    si->compcount++;
750 	}
751 	if (ISSET(si->stackflags, INVCON)) {
752 	    si->compname[si->compcount] =
753 		(char *)DXAllocate(strlen("invalid connections")+1);
754 	    if (!si->compname[si->compcount])
755 		goto error;
756 	    strcpy(si->compname[si->compcount], "invalid connections");
757 	    si->compcount++;
758 	}
759     }
760 
761 
762   done:
763     if (genshape)
764 	DXFree((Pointer)genshape);
765     return OK;
766 
767   error:
768     if (genshape)
769 	DXFree((Pointer)genshape);
770     return ERROR;
771 }
772 
773 
774 
775 /* find the first non-empty field in the input.  the input is at this point
776  * only going to be a series or generic group.
777  */
778 static Field
getfirstfield(StackInfo * si)779 getfirstfield(StackInfo *si)
780 {
781     Object testf;
782     Field retval=NULL;
783     int i;
784     int empty;
785 
786 
787     if ((si->inclass != CLASS_SERIES) && (si->inclass != CLASS_GROUP)) {
788 	INTERNALERROR;
789 	goto error;
790     }
791 
792     for (i=0, empty=1;
793 	 empty && (testf=DXGetEnumeratedMember((Group)si->thisobj, i, NULL));
794 	 i++) {
795 
796 	/* series of points can be stacked */
797 	if (DXGetObjectClass(testf) == CLASS_ARRAY) {
798 	    SET(si->stackflags, POINTS);
799 	    return NULL;
800 	}
801 
802 	/* might get here with a CF --  check this XXX */
803 	if (DXGetObjectClass(testf) != CLASS_FIELD) {
804 	    DXSetError(ERROR_BAD_CLASS,
805 		       "members of the series must be fields");
806 	    goto error;
807 	}
808 
809 	retval = (Field)testf;
810 
811 	if (DXEmptyField((Field)testf))
812 	    continue;
813 
814 	empty = 0;
815     }
816 
817     return retval;
818 
819   error:
820     return NULL;
821 }
822 
823 
824 
825 
826 /* based on what the first member was, now look at the rest of the
827  * series members and complain about inconsistent things (e.g. quads
828  * in first field and cubes in second).
829  */
830 static Error
checkall(StackParms * sp,StackInfo * si)831 checkall(StackParms *sp, StackInfo *si)
832 {
833     Field testf;
834     Array ap, ac, aa;
835     int i, j, tmp;
836     char *cp;
837 
838 
839     if ((si->inclass != CLASS_SERIES) && (si->inclass != CLASS_GROUP)) {
840 	INTERNALERROR;
841 	goto error;
842     }
843 
844     for (i=0;
845 	 (testf=(Field)DXGetEnumeratedMember((Group)si->thisobj, i, NULL));
846 	 i++) {
847 
848 	if (DXGetObjectClass((Object)testf) != CLASS_FIELD) {
849 	    DXSetError(ERROR_BAD_CLASS, "members of the series must be fields");
850 	    goto error;
851 	}
852 
853 	if (DXEmptyField(testf)) {
854 	    DXSetError(ERROR_BAD_CLASS, "series cannot contain Empty Fields");
855 	    goto error;
856 	}
857 
858 
859 
860 	/* compare each to the first field
861 	 */
862 	ap = (Array)DXGetComponentValue(testf, "positions");
863 	if (!ap) {
864 	    /* shouldn't happen - we've tested for empty fields above */
865 	    INTERNALERROR;
866 	    goto error;
867 	}
868 	if ((!DXTypeCheckV(ap, TYPE_FLOAT, CATEGORY_REAL, 0, NULL)) &&
869 	    (!DXTypeCheckV(ap, TYPE_FLOAT, CATEGORY_REAL, 1, NULL))) {
870 	    DXSetError(ERROR_DATA_INVALID, "#11383");
871 	    goto error;
872 	}
873 
874 
875 	if (!arraymatch(ap, si->ap0, 1, "positions"))
876 	    goto error;
877 
878 
879 	ac = (Array)DXGetComponentValue(testf, "connections");
880 	if (!ac) {
881 	    if (si->conndim != 0)  /* all have to be scattered */
882 		goto error;
883 	} else {
884 	    /* get the dimensionality and make sure it's regular */
885 	    if ((!DXTypeCheckV(ac, TYPE_INT, CATEGORY_REAL, 0, NULL)) &&
886 		(!DXTypeCheckV(ac, TYPE_INT, CATEGORY_REAL, 1, NULL))) {
887 		DXSetError(ERROR_DATA_INVALID, "#11382");
888 		goto error;
889 	    }
890 
891 	    if (!DXGetStringAttribute((Object)ac, "element type", &cp)) {
892 		DXSetError(ERROR_DATA_INVALID, "#13070");
893 		goto error;
894 	    }
895 
896 	    if (!arraymatch(ac, si->ac0, 1, "connections"))
897 		goto error;
898 
899 
900 	    /* think about tris here -> prisms */
901 	    if ((si->conndim == 0) && (strcmp(cp, "points") == 0))
902 		;  /* ok */
903 	    else if ((si->conndim == 1) && (strcmp(cp, "lines") == 0))
904 		;
905 	    else if ((si->conndim == 2) && (strcmp(cp, "quads") == 0))
906 		;
907 	    else if ((si->conndim == 3) && (strcmp(cp, "cubes") == 0))
908 		;
909 	    else if ((si->conndim > 3) && (strncmp(cp, "cubes", 5) == 0) &&
910 		     (sscanf(cp+5, "%d", &tmp) == 1) && tmp == si->conndim)
911 		;
912 	    else {
913 		DXSetError(ERROR_DATA_INVALID, "#10610", "input");
914 		goto error;
915 	    }
916 
917 	}
918 
919 
920 	/* make sure components match.
921 	 */
922 
923 
924 	/* need to beware of invalids - some might be dep & some ref
925          * with different counts & types!
926 	 */
927 	for (j=0; j < si->compcount; j++) {
928 	    /* skip invalids here - they need to be on the list, but not
929 	     * checked here.
930 	     */
931 
932 	    if (strncmp("invalid", si->compname[j], strlen("invalid")) == 0)
933 		continue;
934 
935 	    /* make this a warning instead of error? */
936 	    if (!(aa=(Array)DXGetComponentValue(testf, si->compname[j]))) {
937 		DXSetError(ERROR_DATA_INVALID,
938 			   "at least one field is missing a %s component",
939 			   si->compname[j]);
940 		goto error;
941 	    }
942 
943 	    if (!arraymatch(aa,
944 			    (Array)DXGetComponentValue(si->f0, si->compname[j]),
945 			    1, si->compname[j]))
946 		goto error;
947 	}
948     }
949 
950 
951   /* done */
952     return OK;
953 
954   error:
955     return ERROR;
956 }
957 
958 /* return ok if arrays match identically (plus rank 0 matches
959  * r1, shape 1 and visa versa).  if countflag == 1, counts have
960  * to match.
961  */
962 static Error
arraymatch(Array a1,Array a2,int countflag,char * name)963 arraymatch(Array a1, Array a2, int countflag, char *name)
964 {
965     int i;
966     int count1, count2;
967     Type t1, t2;
968     Category c1, c2;
969     int rank1, rank2;
970     int hereshape1[MAXRANK], *genshape1 = NULL;
971     int hereshape2[MAXRANK], *genshape2 = NULL;
972 
973 
974     if (!DXGetArrayInfo(a1, &count1, &t1, &c1, &rank1, NULL))
975 	goto error;
976 
977     if (rank1 < MAXRANK) {
978 	if (!DXGetArrayInfo(a1, &count1, &t1, &c1, &rank1, hereshape1))
979 	    goto error;
980     } else {
981 	genshape1 = (int *)DXAllocate(rank1 * sizeof(int));
982 	if (!genshape1)
983 	    goto error;
984 	if (!DXGetArrayInfo(a1, &count1, &t1, &c1, &rank1, genshape1))
985 	    goto error;
986 
987     }
988 
989     if (!DXGetArrayInfo(a2, &count2, &t2, &c2, &rank2, NULL))
990 	goto error;
991 
992     if (rank2 < MAXRANK) {
993 	if (!DXGetArrayInfo(a2, &count2, &t2, &c2, &rank2, hereshape2))
994 	    goto error;
995     } else {
996 	genshape2 = (int *)DXAllocate(rank2 * sizeof(int));
997 	if (!genshape2)
998 	    goto error;
999 	if (!DXGetArrayInfo(a2, &count2, &t2, &c2, &rank2, genshape2))
1000 	    goto error;
1001 
1002     }
1003 
1004     if (countflag && (count1 != count2)) {
1005 	DXSetError(ERROR_DATA_INVALID,
1006 		   "%s arrays are not the same length", name);
1007 	goto error;
1008     }
1009 
1010     if (t1 != t2) {
1011 	DXSetError(ERROR_DATA_INVALID,
1012 		   "%s arrays are not the same type", name);
1013 	goto error;
1014     }
1015 
1016     if (c1 != c2) {
1017 	DXSetError(ERROR_DATA_INVALID,
1018 		   "%s arrays are not the same category", name);
1019 	goto error;
1020     }
1021 
1022     if (rank1 != rank2) {
1023 	if ((rank1 != 0 && rank1 != 1) ||
1024 	    (rank2 != 0 && rank2 != 1)) {
1025 	    DXSetError(ERROR_DATA_INVALID,
1026 		       "%s arrays are not the same rank", name);
1027 	    goto error;
1028 	}
1029 	if (rank1 == 1 && hereshape1[0] != 1) {
1030 	    DXSetError(ERROR_DATA_INVALID,
1031 		       "%s arrays are not the same rank", name);
1032 	    goto error;
1033 	}
1034 	if (rank2 == 1 && hereshape2[0] != 1) {
1035 	    DXSetError(ERROR_DATA_INVALID,
1036 		       "%s arrays are not the same rank", name);
1037 	    goto error;
1038 	}
1039     }
1040 
1041     if (rank1 < MAXRANK) {
1042 	for (i=0; i<rank1; i++)
1043 	    if (hereshape1[i] != hereshape2[i]) {
1044 		DXSetError(ERROR_DATA_INVALID,
1045 			   "%s arrays are not the same shape", name);
1046 		goto error;
1047 	    }
1048     } else {
1049 	for (i=0; i<rank1; i++)
1050 	    if (genshape1[i] != genshape2[i]) {
1051 		DXSetError(ERROR_DATA_INVALID,
1052 			   "%s arrays are not the same shape", name);
1053 		goto error;
1054 	    }
1055     }
1056 
1057     /* they match */
1058 
1059 
1060 
1061   /* done */
1062     if (genshape1)
1063 	DXFree((Pointer)genshape1);
1064     if (genshape2)
1065 	DXFree((Pointer)genshape2);
1066     return OK;
1067 
1068   error:
1069     if (genshape1)
1070 	DXFree((Pointer)genshape1);
1071     if (genshape2)
1072 	DXFree((Pointer)genshape2);
1073     return ERROR;
1074 }
1075 
1076 
1077 /* if input is a series of composite fields, return OK
1078  * otherwise return error but do NOT set an error code.
1079  */
1080 static Error
ispartitioned(StackInfo * si)1081 ispartitioned(StackInfo *si)
1082 {
1083     Object subo;
1084 
1085     if (si->inclass != CLASS_SERIES)
1086 	goto error;
1087 
1088     subo = DXGetSeriesMember((Series)si->thisobj, 0, NULL);
1089     if (!subo)
1090 	goto error;
1091 
1092     if (DXGetObjectClass(subo) == CLASS_GROUP &&
1093 	DXGetGroupClass((Group)subo) == CLASS_COMPOSITEFIELD)
1094 	return OK;
1095 
1096   error:
1097     /* set no error code */
1098     return ERROR;
1099 }
1100 
1101 
1102 /* if input is a generic group of fields which match, then
1103  * return ok, otherwise return error.  the error code MIGHT
1104  * be set - reset it here before returning.
1105  */
1106 static Error
isstackable(StackParms * sp,StackInfo * si)1107 isstackable(StackParms *sp, StackInfo *si)
1108 {
1109     if (setupwork(sp, si) != ERROR)
1110 	return OK;
1111 
1112     /* return with NO error code set */
1113     DXResetError();
1114     return ERROR;
1115 }
1116 
1117 /* input obj is series of composite fields.  output is a
1118  * composite field of series.  use meshoffsets to match up
1119  * corresponding members.
1120  */
1121 
1122 static Object
invertobj(StackInfo * si)1123 invertobj(StackInfo *si)
1124 {
1125     CompositeField newtop = NULL;
1126     CompositeField first, next;
1127     Series news = NULL;
1128     float seriespos;
1129     Object subo;
1130     int i, ii, j, k;
1131     int cfcount;
1132     Array conn;
1133     int *meshoffsets = NULL;
1134     int meshoffsets2[MAXRANK];
1135     int meshsize, meshsize2;
1136     Array meshattr;
1137 
1138     /* lots o loops here */
1139 
1140     /* how about:  count up the number of partitions in series member 0
1141      *  make a cf and add each member to the cf as the first member of
1142      *  a new series.  record the mesh offsets at the series level.
1143      *  now for each other series member, loop through each partition,
1144      *  find the cf # which matches the mesh offsets and add that partition
1145      *  as the next series member.  draw pictures here - this is confusing.
1146      */
1147 
1148     newtop = DXNewCompositeField();
1149     if (!newtop)
1150 	goto error;
1151 
1152     /* first composite field in the series */
1153     first = (CompositeField)DXGetSeriesMember((Series)si->thisobj, 0, &seriespos);
1154 
1155     if (!DXGetMemberCount((Group)first, &cfcount))
1156 	goto error;
1157 
1158     /* first field in the composite field */
1159     subo = DXGetEnumeratedMember((Group)first, 0, NULL);
1160     if (!subo)
1161 	goto done;
1162 
1163     /* figure out dimensionality of the connections and allocate space
1164      * for ALL the mesh offsets for all the partitions at once
1165      */
1166     conn = (Array)DXGetComponentValue((Field)subo, "connections");
1167     if (!conn) {
1168 	DXSetError(ERROR_DATA_INVALID, "cannot stack partitioned data if some fields do not have a 'connections' component");
1169 	goto error;
1170     }
1171 
1172     if (!DXGetMeshArrayInfo((MeshArray)conn, &meshsize, NULL)) {
1173 	DXSetError(ERROR_DATA_INVALID,
1174 		   "cannot stack partitioned data if all fields do not havea completely regular 'connections' component");
1175 	goto error;
1176     }
1177 
1178     meshoffsets = (int *)DXAllocate(sizeof(int) * meshsize * cfcount);
1179     if (!meshoffsets)
1180 	goto error;
1181 
1182 
1183     for (i=0; (subo=DXGetEnumeratedMember((Group)first, i, NULL)); i++) {
1184 	news = DXNewSeries();
1185 	if (!news)
1186 	    goto error;
1187 
1188 	if (!DXSetSeriesMember(news, 0, (double)seriespos, subo))
1189 	    goto error;
1190 
1191 	conn = (Array)DXGetComponentValue((Field)subo, "connections");
1192 	if (!conn) {
1193 	    DXSetError(ERROR_DATA_INVALID, "cannot stack partitioned data if some fields do not have a 'connections' component");
1194 	    goto error;
1195 	}
1196 
1197 	if (!DXGetMeshArrayInfo((MeshArray)conn, &meshsize, NULL)) {
1198 	    DXSetError(ERROR_DATA_INVALID,
1199 		       "cannot stack partitioned data if all fields do not havea completely regular 'connections' component");
1200 	    goto error;
1201 	}
1202 
1203 	if (meshsize >= MAXRANK) {
1204 	    INTERNALERROR;
1205 	    goto error;
1206 	}
1207 
1208 	if (!DXGetMeshOffsets((MeshArray)conn, meshoffsets+(i*meshsize))) {
1209 	    DXSetError(ERROR_DATA_INVALID,
1210 		       "cannot stack partitioned data if all fields do not havemesh offsets");
1211 	    goto error;
1212 	}
1213 
1214 	meshattr = DXNewArray(TYPE_INT, CATEGORY_REAL, 0);
1215 	if (!meshattr)
1216 	    goto error;
1217 
1218 	if (!DXAddArrayData(meshattr,
1219 			    0, meshsize,
1220 			    (Pointer)(meshoffsets+(i*meshsize))))
1221 	    goto error;
1222 
1223 	if (!DXSetAttribute((Object)news, "mesh offsets", (Object)meshattr))
1224 	    goto error;
1225 
1226 
1227 	if (!DXSetMember((Group)newtop, NULL, (Object)news))
1228 	    goto error;
1229 
1230 	news = NULL;
1231 
1232     }
1233 
1234 
1235     for (k=1;
1236 	 (next=(CompositeField)DXGetSeriesMember((Series)si->thisobj, k, &seriespos));
1237 	 k++) {
1238 	for (j=0; (subo=DXGetEnumeratedMember((Group)next, j, NULL)); j++) {
1239 
1240 
1241 	    /* get each partition, find the mesh offsets, match them
1242              * up with the right cf member, and set it as the next member
1243 	     * of that series.
1244 	     */
1245 
1246 	    conn = (Array)DXGetComponentValue((Field)subo, "connections");
1247 	    if (!conn) {
1248 		DXSetError(ERROR_DATA_INVALID, "cannot stack partitioned data if some fields do not have a 'connections' component");
1249 		goto error;
1250 	    }
1251 
1252 	    if (!DXGetMeshArrayInfo((MeshArray)conn, &meshsize2, NULL)) {
1253 		DXSetError(ERROR_DATA_INVALID,
1254 			   "cannot stack partitioned data if all fields do not havea completely regular 'connections' component");
1255 		goto error;
1256 	    }
1257 
1258 	    /* shouldn't happen if we've gotten this far */
1259 	    if (meshsize2 != meshsize) {
1260 		INTERNALERROR;
1261 		goto error;
1262 	    }
1263 
1264 	    if (!DXGetMeshOffsets((MeshArray)conn, meshoffsets2)) {
1265 		DXSetError(ERROR_DATA_INVALID,
1266 			   "cannot stack partitioned data if all fields do not havemesh offsets");
1267 		goto error;
1268 	    }
1269 
1270 	    for (i=0; i<cfcount; i++) {
1271 		for (ii=0; ii<meshsize; ii++) {
1272 		    if (meshoffsets2[ii] != meshoffsets[i*meshsize + ii])
1273 			break;
1274 		}
1275 		if (ii == meshsize)
1276 		    break;
1277 	    }
1278 	    if (i == cfcount) {
1279 		DXSetError(ERROR_DATA_INVALID,
1280 			   "cannot stack partitioned data if all fields do not havemesh offsets");
1281 		goto error;
1282 	    }
1283 
1284 	    news = (Series)DXGetEnumeratedMember((Group)newtop, i, NULL);
1285 	    if (!news) {
1286 		INTERNALERROR;
1287 		goto error;
1288 	    }
1289 
1290 	    if (!DXSetSeriesMember(news, k, (double)seriespos, subo))
1291 		goto error;
1292 	}
1293     }
1294 
1295 
1296   done:
1297     DXFree((Pointer)meshoffsets);
1298     return (Object)newtop;
1299 
1300   error:
1301     DXFree((Pointer)meshoffsets);
1302     DXDelete((Object)newtop);
1303     DXDelete((Object)news);
1304     return NULL;
1305 
1306 }
1307 
1308 /* take a generic group and convert the members into
1309  * a series with series pos 0 .. n
1310  */
1311 static Object
convertseries(StackInfo * si)1312 convertseries(StackInfo *si)
1313 {
1314     Series newtop = NULL;
1315     Object subo;
1316     int i;
1317     double seriespos = 0.0;
1318 
1319 
1320     newtop = DXNewSeries();
1321     if (!newtop)
1322 	goto error;
1323 
1324     for (i=0, seriespos = 0.0;
1325 	 (subo=DXGetEnumeratedMember((Group)si->thisobj, i, NULL));
1326 	 i++, seriespos += 1.0)
1327 
1328 	if (!DXSetSeriesMember(newtop, i, seriespos, subo))
1329 	    goto error;
1330 
1331 
1332 
1333   /* done */
1334     return (Object)newtop;
1335 
1336   error:
1337     DXDelete((Object)newtop);
1338     return NULL;
1339 
1340 }
1341 
1342 
1343 
1344 /* actually do the stack
1345  */
1346 static Object
dowork(StackParms * sp,StackInfo * si)1347 dowork(StackParms *sp, StackInfo *si)
1348 {
1349 
1350     /* if the input is entirely empty, return an empty field
1351      */
1352     if (ISSET(si->stackflags, EMPTYOUT))
1353 	return (Object)DXEndField(DXNewField());
1354 
1355     /* if the input is a single field, stack the positions only
1356      * and return.
1357      */
1358     if (DXGetObjectClass(si->thisobj) == CLASS_FIELD)
1359 	return (stackpos(sp, si, 0.0));
1360 
1361     /* normal case - input is a series.  stack into a single
1362      * field and return.
1363      */
1364     return (stackgroup(sp, si));
1365 }
1366 
1367 
1368 /* add the requested dim to the positions component
1369  */
1370 static Object
stackpos(StackParms * sp,StackInfo * si,float where)1371 stackpos(StackParms *sp, StackInfo *si, float where)
1372 {
1373     Field retval = NULL;
1374     Array oldpos;
1375     Array newpos = NULL;
1376 
1377     retval = (Field)DXCopy(si->thisobj, COPY_STRUCTURE);
1378     if (!retval)
1379 	return NULL;
1380 
1381     oldpos = (Array)DXGetComponentValue(retval, "positions");
1382     if (!oldpos)
1383 	goto error;
1384 
1385 
1386     newpos = add1posdim(oldpos, sp->newposdim, where);
1387     if (!newpos)
1388 	goto error;
1389 
1390 
1391     if (!DXSetComponentValue(retval, "positions", (Object)newpos))
1392 	goto error;
1393     newpos = NULL;
1394 
1395     if (!DXChangedComponentValues(retval, "positions"))
1396 	goto error;
1397 
1398     if (!DXEndField(retval))
1399 	goto error;
1400 
1401     return (Object)retval;
1402 
1403   error:
1404     DXDelete((Object)retval);
1405     DXDelete((Object)newpos);
1406     return NULL;
1407 }
1408 
1409 
1410 /* add a fixed dimension to a position array.  we know these
1411  * have to be rank 1 and float, so we can cut corners
1412  * in the array handling.
1413  */
1414 static Array
add1posdim(Array oldpos,int newdim,float where)1415 add1posdim(Array oldpos, int newdim, float where)
1416 {
1417     Array newpos = NULL;
1418     int i, j;
1419     Type t;
1420     Category c;
1421     int items, tcount;
1422     int rank, shape;
1423     Array terms[MAXRANK], *genterms = NULL;
1424     int counts[MAXRANK], *gencounts = NULL;
1425     float orig[MAXRANK], *genorig = NULL;
1426     float delta[MAXRANK*MAXRANK], *gendelta = NULL;
1427     float *ifp, *ofp;
1428 
1429 #define FREEPTR(alloc, static)  if (alloc && (alloc != static)) \
1430                                         DXFree((Pointer)alloc)
1431 
1432 
1433     /* fully regular case */
1434     if (DXQueryGridPositions(oldpos, &shape, NULL, NULL, NULL)) {
1435 	if (shape+1 < MAXRANK) {
1436 	    if (!DXQueryGridPositions(oldpos, NULL, counts, orig, delta))
1437 		goto error;
1438 	    gencounts = counts;
1439 	    genorig = orig;
1440 	    gendelta = delta;
1441 	} else {
1442 	    items = sizeof(float) * (shape+1);
1443 	    genorig = (float *)DXAllocate(items);
1444 	    gencounts = (int *)DXAllocate(items);
1445 
1446 	    items *= (shape+1);
1447 	    gendelta = (float *)DXAllocate(items);
1448 	    if (!gencounts || !genorig || !gendelta)
1449 		goto error;
1450 
1451 	    if (!DXQueryGridPositions(oldpos, NULL, gencounts, genorig, gendelta))
1452 		goto error;
1453 	}
1454 
1455 	for (i=shape; i > newdim; --i) {
1456 	    genorig[i] = genorig[i-1];
1457 	    gencounts[i] = gencounts[i-1];
1458 	}
1459 
1460 	genorig[newdim] = where;
1461 	gencounts[newdim] = 1;
1462 
1463 	ifp = gendelta + shape * shape;
1464 	ofp = gendelta + (shape+1) * (shape+1);
1465 
1466 	for (i=shape; i >= 0; --i)
1467 	    for (j=shape; j >= 0; --j) {
1468 		if (i == newdim)
1469 		    *--ofp = 0.0;
1470 		else if (j == newdim)
1471 		    *--ofp = 0.0;
1472 		else
1473 		    *--ofp = *--ifp;
1474 	    }
1475 
1476 	newpos = DXMakeGridPositionsV(shape+1, gencounts, genorig, gendelta);
1477 	if (!newpos)
1478 	    goto error;
1479 
1480 	goto done;
1481     }
1482 
1483 
1484     /* not fully regular.  handle the compact forms when we can */
1485     DXGetArrayInfo(oldpos, &items, &t, &c, &rank, &shape);
1486     if (rank == 0)
1487 	shape = 1;
1488 
1489 
1490     switch (DXGetArrayClass(oldpos)) {
1491       case CLASS_ARRAY:
1492       /* expanded */
1493 	if (!(ifp = DXGetArrayData(oldpos)))
1494 	    goto error;
1495 	if (!(newpos = DXNewArray(TYPE_FLOAT, CATEGORY_REAL, 1, shape+1)))
1496 	    goto error;
1497 	if (!DXCopyAttributes((Object)newpos, (Object)oldpos))
1498 	    goto error;
1499 	if (!DXAddArrayData(newpos, 0, items, NULL))
1500 	    goto error;
1501 	if (!(ofp = DXGetArrayData(newpos)))
1502 	    goto error;
1503 
1504 	for (i=0; i<items; i++)
1505 	    for (j=0; j<shape+1; j++) {
1506 		if (j == newdim)
1507 		    *ofp++ = where;
1508 		else
1509 		    *ofp++ = *ifp++;
1510 	    }
1511 
1512 	break;
1513 
1514 
1515       case CLASS_REGULARARRAY:
1516 	if (shape+1 < MAXRANK) {
1517 	    if (!DXGetRegularArrayInfo((RegularArray)oldpos, &items,
1518 				       (Pointer)orig, (Pointer)delta))
1519 		goto error;
1520 	    genorig = orig;
1521 	    gendelta = delta;
1522 	} else {
1523 	    genorig = (float *)DXAllocate(sizeof(float) * (shape+1));
1524 	    gendelta = (float *)DXAllocate(sizeof(float) * (shape+1));
1525 	    if (!genorig || !gendelta)
1526 		goto error;
1527 
1528 	    if (!DXGetRegularArrayInfo((RegularArray)oldpos, &items,
1529 				       (Pointer)genorig, (Pointer)gendelta))
1530 		goto error;
1531 	}
1532 
1533 	for (i=shape; i > newdim; --i) {
1534 	    genorig[i] = genorig[i-1];
1535 	    gendelta[i] = gendelta[i-1];
1536 	}
1537 
1538 	genorig[newdim] = where;
1539 	gendelta[newdim] = 0.0;
1540 
1541 	if (!(newpos = (Array)DXNewRegularArray(TYPE_FLOAT, shape+1, items,
1542 	 			        (Pointer)genorig, (Pointer)gendelta)))
1543 	    goto error;
1544 
1545 	break;
1546 
1547       case CLASS_PRODUCTARRAY:
1548 
1549 	if (!DXGetProductArrayInfo((ProductArray)oldpos, &tcount, NULL))
1550 	    goto error;
1551 
1552 	if (tcount+1 < MAXRANK) {
1553 	    if (!DXGetProductArrayInfo((ProductArray)oldpos, &tcount, terms))
1554 		goto error;
1555 	    genterms = terms;
1556 	} else {
1557 	    genterms = (Array *)DXAllocate(sizeof(Array *) * (tcount+1));
1558 	    if (!genterms)
1559 		goto error;
1560 	    if (!DXGetProductArrayInfo((ProductArray)oldpos, &tcount, genterms))
1561 		goto error;
1562 	}
1563 
1564 	for (i=0; i<tcount; i++)
1565 	    genterms[i] = add1posdim(genterms[i], newdim, where);
1566 
1567 	for (i=tcount; i>newdim; i--)
1568 	    genterms[i] = genterms[i-1];
1569 
1570 
1571 	if (shape+1 < MAXRANK) {
1572 	    genorig = orig;
1573 	    gendelta = delta;
1574 	} else {
1575 	    genorig = (float *)DXAllocate(sizeof(float) * (shape+1));
1576 	    gendelta = (float *)DXAllocate(sizeof(float) * (shape+1));
1577 	    if (!genorig || !gendelta)
1578 		goto error;
1579 	}
1580 
1581 	for (i=0; i<shape+1; i++) {
1582 	    if (i == newdim)
1583 		genorig[i] = where;
1584 	    else
1585 		genorig[i] = 0.0;
1586 	    gendelta[i] = 0.0;
1587 	}
1588 
1589 	if (!(terms[newdim] = (Array)DXNewRegularArray(TYPE_FLOAT, shape+1,
1590 						       items,
1591 						       (Pointer)genorig,
1592 						       (Pointer)gendelta)))
1593 	    goto error;
1594 
1595 
1596         if (!(newpos = (Array)DXNewProductArrayV(tcount+1, terms)))
1597 	    goto error;
1598 
1599 	break;
1600 
1601 
1602       /* degenerate, but perhaps useful after being computed on? */
1603       case CLASS_CONSTANTARRAY:
1604 	if (shape+1 < MAXRANK) {
1605 	    if (!DXQueryConstantArray(oldpos, &items, (Pointer)orig))
1606 		goto error;
1607 	    genorig = orig;
1608 	} else {
1609 	    genorig = (float *)DXAllocate(sizeof(float) * (shape+1));
1610 	    if (!genorig)
1611 		goto error;
1612 	    if (!DXQueryConstantArray(oldpos, &items, (Pointer)genorig))
1613 		goto error;
1614 	}
1615 
1616 	for (i=shape; i > newdim; --i)
1617 	    genorig[i] = genorig[i-1];
1618 
1619 	genorig[newdim] = where;
1620 
1621 	if (!(newpos = (Array)DXNewConstantArray(items, (Pointer)genorig,
1622 						 TYPE_FLOAT, CATEGORY_REAL,
1623 						 1, shape+1)))
1624 	    goto error;
1625 
1626 	break;
1627 
1628       /* these are integer types - can't be used as the pos component */
1629       case CLASS_MESHARRAY:
1630       case CLASS_PATHARRAY:
1631       default:
1632 	DXSetError(ERROR_BAD_CLASS, "Unrecognized array type in positions");
1633 	goto error;
1634     }
1635 
1636 
1637   done:
1638 
1639     FREEPTR(gencounts, counts);
1640     FREEPTR(genorig, orig);
1641     FREEPTR(gendelta, delta);
1642     FREEPTR(genterms, terms);
1643 
1644     return newpos;
1645 
1646   error:
1647     FREEPTR(gencounts, counts);
1648     FREEPTR(genorig, orig);
1649     FREEPTR(gendelta, delta);
1650     FREEPTR(genterms, terms);
1651 
1652     DXDelete((Object)newpos);
1653     return NULL;
1654 
1655 }
1656 
1657 /* add a new array to the positions array.  we know positions
1658  * have to be rank 1 and float, so this can be less general.
1659  * nflag is set if the new positions dimension is regular,
1660  * oflag is set if the entire series has a regular and compatible
1661  * set of series positions so the output object can be completely
1662  * regular in that dimension.
1663  */
1664 static Array
addNposdim(StackInfo * si,int nflag,int oflag,int newdim,Array addpos)1665 addNposdim(StackInfo *si, int nflag, int oflag, int newdim, Array addpos)
1666 {
1667     Array oldpos = NULL;
1668     Array newpos = NULL;
1669     Array newterm = NULL;
1670     int i, j;
1671     Type t;
1672     Category c;
1673     int items, tcount, rcount;
1674     int rank, shape;
1675     Array terms[MAXRANK], *genterms = NULL;
1676     int counts[MAXRANK], *gencounts = NULL;
1677     float orig[MAXRANK], *genorig = NULL;
1678     float delta[MAXRANK*MAXRANK], *gendelta = NULL;
1679     float rorig[MAXRANK], *genrorig = NULL;
1680     float rdelta[MAXRANK], *genrdelta = NULL;
1681     float *ifp, *ofp;
1682     Array tmppos = NULL;
1683 
1684 
1685 
1686     /* NEEDS CODE CHANGE HERE!!! XXX   the reg test should be:
1687      * are all the pos the same.  if so, then do the query on each
1688      * of the inputs and if not, go to the expanded case w/o the
1689      * error out.
1690      */
1691 
1692     /* fully regular case - both existing dim and new stacked dim */
1693     if (nflag && oflag) {
1694 	oldpos = si->ap0;
1695 	if (!DXQueryGridPositions(oldpos, &shape, NULL, NULL, NULL)) {
1696 	    INTERNALERROR;
1697 	    goto error;
1698 	}
1699 	if (!DXGetArrayClass(addpos) == CLASS_REGULARARRAY) {
1700 	    INTERNALERROR;
1701 	    goto error;
1702 	}
1703 
1704 	if (shape+1 < MAXRANK) {
1705 	    if (!DXQueryGridPositions(oldpos, NULL, counts, orig, delta))
1706 		goto error;
1707 	    gencounts = counts;
1708 	    genorig = orig;
1709 	    gendelta = delta;
1710 	    if (!DXGetRegularArrayInfo((RegularArray)addpos, &rcount,
1711 				       (Pointer)rorig, (Pointer)rdelta))
1712 		goto error;
1713 	    genrorig = rorig;
1714 	    genrdelta = rdelta;
1715 	} else {
1716 	    items = sizeof(float) * (shape+1);
1717 	    genorig = (float *)DXAllocate(items);
1718 	    gencounts = (int *)DXAllocate(items);
1719 	    genorig = (float *)DXAllocate(items);
1720 	    gendelta = (float *)DXAllocate(items);
1721 
1722 	    items *= (shape+1);
1723 	    gendelta = (float *)DXAllocate(items);
1724 	    if (!gencounts || !genorig || !gendelta || !genorig || !gendelta)
1725 		goto error;
1726 
1727 	    if (!DXQueryGridPositions(oldpos, NULL, gencounts, genorig, gendelta))
1728 		goto error;
1729 	    if (!DXGetRegularArrayInfo((RegularArray)addpos, &rcount,
1730 				       (Pointer)genrorig, (Pointer)genrdelta))
1731 		goto error;
1732 	}
1733 
1734 	for (i=shape; i > newdim; --i) {
1735 	    genorig[i] = genorig[i-1];
1736 	    gencounts[i] = gencounts[i-1];
1737 	}
1738 
1739 	genorig[newdim] = genrorig[newdim];
1740  	gencounts[newdim] = rcount;
1741 
1742 	ifp = gendelta + shape * shape;
1743 	ofp = gendelta + (shape+1) * (shape+1);
1744 
1745 	for (i=shape; i >= 0; --i)
1746 	    for (j=shape; j >= 0; --j) {
1747 		if (i == newdim)
1748 		    *--ofp = genrdelta[j];
1749 		else if (j == newdim)
1750 		    *--ofp = 0.0;
1751 		else
1752 		    *--ofp = *--ifp;
1753 	    }
1754 
1755 	newpos = DXMakeGridPositionsV(shape+1, gencounts, genorig, gendelta);
1756 	if (!newpos)
1757 	    goto error;
1758 
1759 	goto done;
1760     }
1761 
1762 
1763 
1764     /* if oflag is not set, the old dim does not have compatible position
1765      * components and has to be completely expanded - all the series members.
1766      */
1767     if (!oflag) {
1768 	oldpos = addirregarrays(si, "positions", newdim);
1769 	if (!oldpos)
1770 	    goto error;
1771 
1772 	tmppos = oldpos;  /* to make sure this gets deleted at the end */
1773     } else
1774 	oldpos = si->ap0;
1775 
1776 
1777 
1778     DXGetArrayInfo(oldpos, &items, &t, &c, &rank, &shape);
1779     if (rank == 0)
1780 	shape = 1;
1781 
1782 
1783     switch (DXGetArrayClass(oldpos)) {
1784       case CLASS_ARRAY:
1785       /* expanded */
1786 	newpos = addseriesdim(si, oldpos, addpos, newdim);
1787 	if (!newpos)
1788 	    goto error;
1789 
1790 	break;
1791 
1792 
1793       case CLASS_REGULARARRAY:
1794 	if (shape+1 < MAXRANK) {
1795 	    if (!DXGetRegularArrayInfo((RegularArray)oldpos, &items,
1796 				       (Pointer)orig, (Pointer)delta))
1797 		goto error;
1798 	    genorig = orig;
1799 	    gendelta = delta;
1800 	} else {
1801 	    genorig = (float *)DXAllocate(sizeof(float) * (shape+1));
1802 	    gendelta = (float *)DXAllocate(sizeof(float) * (shape+1));
1803 	    if (!genorig || !gendelta)
1804 		goto error;
1805 
1806 	    if (!DXGetRegularArrayInfo((RegularArray)oldpos, &items,
1807 				       (Pointer)genorig, (Pointer)gendelta))
1808 		goto error;
1809 	}
1810 
1811 	for (i=shape; i > newdim; --i) {
1812 	    genorig[i] = genorig[i-1];
1813 	    gendelta[i] = gendelta[i-1];
1814 	}
1815 
1816 	genorig[newdim] = 0.0;
1817 	gendelta[newdim] = 0.0;
1818 
1819 	if (!(newterm = (Array)DXNewRegularArray(TYPE_FLOAT, shape+1, items,
1820 					(Pointer)genorig, (Pointer)gendelta)))
1821 	    goto error;
1822 
1823 	/* if the old pos were only a reg array, the old conn had to be 1d.
1824          * the only reasonable options for stackdim here are 0 or 1.
1825 	 */
1826 	switch (newdim) {
1827 	  case 0:
1828 	    terms[0] = addpos;
1829 	    terms[1] = newterm;
1830 	    break;
1831 	  case 1:
1832 	    terms[0] = newterm;
1833 	    terms[1] = addpos;
1834 	    break;
1835 	  default:
1836 	    INTERNALERROR;
1837 	    goto error;
1838 	}
1839 
1840         if (!(newpos = (Array)DXNewProductArrayV(2, terms)))
1841 	    goto error;
1842 
1843 	break;
1844 
1845       case CLASS_PRODUCTARRAY:
1846 
1847 	if (!DXGetProductArrayInfo((ProductArray)oldpos, &tcount, NULL))
1848 	    goto error;
1849 
1850 	if (tcount+1 < MAXRANK) {
1851 	    if (!DXGetProductArrayInfo((ProductArray)oldpos, &tcount, terms))
1852 		goto error;
1853 	    genterms = terms;
1854 	} else {
1855 	    genterms = (Array *)DXAllocate(sizeof(Array *) * (tcount+1));
1856 	    if (!genterms)
1857 		goto error;
1858 	    if (!DXGetProductArrayInfo((ProductArray)oldpos, &tcount, genterms))
1859 		goto error;
1860 	}
1861 
1862 	for (i=0; i<tcount; i++)
1863 	    genterms[i] = add1posdim(genterms[i], newdim, 0.0);
1864 
1865 	for (i=tcount; i>newdim; i--)
1866 	    genterms[i] = genterms[i-1];
1867 
1868 	genterms[newdim] = addpos;
1869 
1870         if (!(newpos = (Array)DXNewProductArrayV(tcount+1, terms)))
1871 	    goto error;
1872 
1873 	break;
1874 
1875 
1876       /* degenerate, but perhaps useful after being computed on? */
1877       case CLASS_CONSTANTARRAY:
1878 
1879 	/* can only do this if the addpos array matches exactly.
1880          *
1881 	 * NEED CODE HERE !!! XXX
1882 	 */
1883 
1884 	if (shape+1 < MAXRANK) {
1885 	    if (!DXQueryConstantArray(oldpos, &items, (Pointer)orig))
1886 		goto error;
1887 	    genorig = orig;
1888 	} else {
1889 	    genorig = (float *)DXAllocate(sizeof(float) * (shape+1));
1890 	    if (!genorig)
1891 		goto error;
1892 	    if (!DXQueryConstantArray(oldpos, &items, (Pointer)genorig))
1893 		goto error;
1894 	}
1895 
1896 	for (i=shape; i > newdim; --i)
1897 	    genorig[i] = genorig[i-1];
1898 
1899 	genorig[newdim] = 1.0;
1900 
1901 	if (!(newpos = (Array)DXNewConstantArray(items, (Pointer)genorig,
1902 						 TYPE_FLOAT, CATEGORY_REAL,
1903 						 1, shape+1)))
1904 	    goto error;
1905 
1906 	break;
1907 
1908       /* these are integer types - can't be used as the pos component */
1909       case CLASS_MESHARRAY:
1910       case CLASS_PATHARRAY:
1911       default:
1912 	DXSetError(ERROR_BAD_CLASS, "Unrecognized array type in positions");
1913 	goto error;
1914     }
1915 
1916 
1917 
1918   done:
1919 
1920     FREEPTR(gencounts, counts);
1921     FREEPTR(genorig, orig);
1922     FREEPTR(gendelta, delta);
1923     FREEPTR(genrorig, rorig);
1924     FREEPTR(genrdelta, rdelta);
1925     FREEPTR(genterms, terms);
1926     DXDelete((Object)tmppos);
1927     return newpos;
1928 
1929   error:
1930     FREEPTR(gencounts, counts);
1931     FREEPTR(genorig, orig);
1932     FREEPTR(gendelta, delta);
1933     FREEPTR(genrorig, rorig);
1934     FREEPTR(genrdelta, rdelta);
1935     FREEPTR(genterms, terms);
1936     DXDelete((Object)tmppos);
1937     DXDelete((Object)newpos);
1938     return NULL;
1939 
1940 }
1941 
1942 
1943 static Array
addpositions(StackParms * sp,StackInfo * si)1944 addpositions(StackParms *sp, StackInfo *si)
1945 {
1946     Array newseriesdim = NULL;
1947     Array newpos = NULL;
1948     int newposreg = 0;
1949     int allserreg = 0;
1950 
1951     /* decide if posD is already > conD - if so, then just
1952      * concatinate the pos together like other data arrays
1953      * and return.  that lets 2d lines become 2d quads (normal)
1954      * and 3d lines becomes 3d quads (ok as well).  don't add
1955      * another dim to the positions in this case.
1956      */
1957 
1958     if (si->posdim > si->conndim)
1959 	return addarrays(sp, si, "positions", 0);
1960 
1961     /* there is a 2x2 decision matrix here.  the new series axis can
1962      * be regular or not.  the existing series position members can
1963      * be regular and consistent or not.  take advantage of any regularity
1964      * you can.  newposreg is the flag for former, allserreg for latter.
1965      */
1966 
1967     if (!newdimregular(sp, si, &newseriesdim, &newposreg))
1968 	goto error;
1969 
1970 
1971     /* decide if the positions in the series are regular or not.
1972      * if regular, be sure they all have identical origin/count/deltas.
1973      */
1974     if (!allposregular(si, &allserreg))
1975 	goto error;
1976 
1977 
1978     /* ok based on the flags, decide how to construct the new positions
1979      * component.
1980      */
1981     newpos = addNposdim(si, newposreg, allserreg, sp->stackdim, newseriesdim);
1982     if (!newpos)
1983 	goto error;
1984 
1985 
1986     return newpos;
1987 
1988   error:
1989     return NULL;
1990 }
1991 
1992 static Array
addcondim(StackInfo * si,Array oldcon,int newdim,int newcount)1993 addcondim(StackInfo *si, Array oldcon, int newdim, int newcount)
1994 {
1995     Array newcon = NULL;
1996     int shape;
1997     int i;
1998     int counts[MAXRANK], *gencounts = NULL;
1999 
2000     /* originally scattered pts?  connect into lines */
2001     if (!oldcon) {
2002 	newcon = DXMakeGridConnections(1, newcount);
2003 	if (!newcon)
2004 	    goto error;
2005 
2006 	si->countlist = (int *)DXAllocate(sizeof(int));
2007 	if (!si->countlist)
2008 	    goto error;
2009 
2010 	si->countlist[0] = newcount;
2011 	goto done;
2012     }
2013 
2014 
2015     /* normal case - get old connections array and up it by 1D */
2016     if (!DXQueryGridConnections(oldcon, &shape, NULL))
2017 	goto error;
2018 
2019     if (shape+1 < MAXRANK) {
2020 	if (!DXQueryGridConnections(oldcon, NULL, counts))
2021 	    goto error;
2022 	gencounts = counts;
2023 
2024     } else {
2025 	gencounts = (int *)DXAllocate(sizeof(int) * (shape+1));
2026 	if (!gencounts)
2027 	    goto error;
2028 
2029 	if (!DXQueryGridConnections(oldcon, NULL, gencounts))
2030 	    goto error;
2031     }
2032 
2033     for (i=shape; i>newdim; --i)
2034 	gencounts[i] = gencounts[i-1];
2035 
2036     gencounts[i] = newcount;
2037 
2038 
2039     newcon = DXMakeGridConnectionsV(shape+1, gencounts);
2040     if (!newcon)
2041 	goto error;
2042 
2043     si->countlist = (int *)DXAllocate(sizeof(int) * (shape+1));
2044     if (!si->countlist)
2045 	goto error;
2046 
2047     for (i=0; i<shape+1; i++)
2048 	si->countlist[i] = gencounts[i];
2049 
2050   done:
2051     FREEPTR(gencounts, counts);
2052     return newcon;
2053 
2054   error:
2055     FREEPTR(gencounts, counts);
2056 
2057     DXDelete((Object)newcon);
2058     return NULL;
2059 }
2060 
2061 /* try to keep some things compact - constant & reg arrays w/0 deltas.
2062  */
2063 static Array
addarrays(StackParms * sp,StackInfo * si,char * component,int flags)2064 addarrays(StackParms *sp, StackInfo *si, char *component, int flags)
2065 {
2066     Array newarr;
2067 
2068     /* if array is constant or regular w/ 0 deltas, try to keep
2069      * it compact.
2070      */
2071     if ((newarr=addcnstarrays(sp, si, component)))
2072 	return newarr;
2073 
2074     /* else fall into irreg case */
2075     if (ISSET(flags, ISREF))
2076 	return addrefarrays(si, component, sp->stackdim);
2077 
2078     return addirregarrays(si, component, sp->stackdim);
2079 }
2080 
2081 static Array
addcnstarrays(StackParms * sp,StackInfo * si,char * component)2082 addcnstarrays(StackParms *sp, StackInfo *si, char *component)
2083 {
2084     Array newarr = NULL;
2085     Array a;
2086     int j;
2087     Object o;
2088     Type t0;
2089     Category c0;
2090     int rank0;
2091     int shape0[MAXRANK];
2092     int size, items;
2093     Pointer *dvalue0 = NULL;
2094     Pointer *dvalue1 = NULL;
2095 
2096 
2097     /* for each member of the series, extract the array and
2098      * see if it's a constant value across the entire series.
2099      */
2100     for (j=0; (o=DXGetSeriesMember((Series)si->thisobj, j, NULL)); j++) {
2101 
2102 	a = (Array)DXGetComponentValue((Field)o, component);
2103 
2104 	if ((DXGetArrayClass(a) != CLASS_CONSTANTARRAY) &&
2105 	    (DXGetArrayClass(a) != CLASS_REGULARARRAY))
2106 	    return NULL;
2107 
2108     }
2109 
2110     /* ok, might be worthwhile to try */
2111 
2112     a = (Array)DXGetComponentValue(si->f0, component);
2113     if (DXGetArrayClass(a) == CLASS_CONSTANTARRAY) {
2114 
2115 	if (!DXGetArrayInfo(a, &items, &t0, &c0, &rank0, NULL))
2116 	    return NULL;
2117 
2118 	if (rank0 >= MAXRANK)
2119 	    return NULL;   /* bail here */
2120 
2121 	if (!DXGetArrayInfo(a, NULL, NULL, NULL, &rank0, shape0))
2122 	    return NULL;
2123 
2124 	size = DXGetItemSize(a);
2125 
2126 	dvalue0 = (Pointer)DXAllocate(size);
2127 	dvalue1 = (Pointer)DXAllocate(size);
2128 	if (!dvalue0 || !dvalue1)
2129 	    return NULL;
2130 
2131 	if (!DXQueryConstantArray(a, NULL, (Pointer)dvalue0))
2132 	    goto error;
2133 
2134 	for (j=1; (o=DXGetSeriesMember((Series)si->thisobj, j, NULL)); j++) {
2135 
2136 	    a = (Array)DXGetComponentValue((Field)o, component);
2137 
2138 	    if (DXGetArrayClass(a) != CLASS_CONSTANTARRAY)
2139 		goto error;
2140 
2141 	    if (!DXQueryConstantArray(a, NULL, (Pointer)dvalue1))
2142 		goto error;
2143 
2144 	    if (memcmp(dvalue0, dvalue1, size) != 0)
2145 		goto error;
2146 	}
2147 
2148 	/* bingo. (success) */
2149 	newarr = (Array)DXNewConstantArrayV(items * si->membercnt,
2150 					    (Pointer)dvalue0,
2151 					    t0, c0, rank0, shape0);
2152 	if (!newarr)
2153 	    goto error;
2154 
2155 	goto done;
2156     }
2157 
2158 
2159     if (DXGetArrayClass(a) == CLASS_REGULARARRAY) {
2160 	/* see if the deltas are 0, and if the values match. */
2161 
2162 	/* less likely of happening, but some old code might still
2163 	 * be using reg arrays instead of constants
2164 	 */
2165 
2166 	/* NEED CODE HERE, but ok for now w/o it */
2167 	return NULL;
2168     }
2169 
2170 
2171   done:
2172     return newarr;
2173 
2174   error:
2175     DXFree(dvalue0);
2176     DXFree(dvalue1);
2177     DXDelete((Object)newarr);
2178     return NULL;
2179 }
2180 
2181 /* this component refs another. call addirregarrays first to
2182  * make it an expanded array, and then set up the same size
2183  * array to see where the numbers go.  then renumber the array.
2184  * this probably needs cases for constant arrays (and a test in
2185  * addcnstarrays() because the code doesn't get here in that case)
2186  * and regular arrays.  for now just assume it's irregular.
2187  */
2188 static Array
addrefarrays(StackInfo * si,char * component,int stackdim)2189 addrefarrays(StackInfo *si, char *component, int stackdim)
2190 {
2191     Array newarr;
2192     Array renumber = NULL;
2193 
2194     /* have to renumber the original values first - add
2195      * offsets to each stack so all the numbers are unique.
2196      * !! need to know if original target was stacked!!  if
2197      * not (no dep attrs) then we don't have to renumber anything!!
2198      */
2199     newarr = addirregarrays(si, component, stackdim);
2200     if (!newarr)
2201 	goto error;
2202 
2203     renumber = makerefs(si, stackdim);
2204     if (!renumber)
2205 	goto error;
2206 
2207     if (!dorenumber(newarr, renumber))
2208 	goto error;
2209 
2210     DXDelete((Object)renumber);
2211     return newarr;
2212 
2213   error:
2214     DXDelete((Object)newarr);
2215     DXDelete((Object)renumber);
2216     return NULL;
2217 }
2218 
2219 
2220 static Array
makerefs(StackInfo * si,int stackdim)2221 makerefs(StackInfo *si, int stackdim)
2222 {
2223     int i, j;
2224     int items, totalcnt;
2225     int *ip;
2226     Series news = NULL;
2227     Field newf = NULL;
2228     Array newa = NULL;
2229     Array retval = NULL;
2230 
2231     totalcnt = 0;
2232 
2233     items = 1;
2234     for (i=0; i<si->conndim; i++)
2235 	items *= si->countlist[i];
2236 
2237     news = DXNewSeries();
2238     if (!news)
2239 	goto error;
2240 
2241     for (i=0; i<si->membercnt; i++) {
2242 	newf = DXNewField();
2243 	if (!newf)
2244 	    goto error;
2245 
2246 	newa = DXNewArray(TYPE_INT, CATEGORY_REAL, 0);
2247 	if (!newa)
2248 	    goto error;
2249 
2250 	if (!DXAddArrayData(newa, 0, items, NULL))
2251 	    goto error;
2252 
2253 	ip = (int *)DXGetArrayData(newa);
2254 	if (!ip)
2255 	    goto error;
2256 
2257 	for (j = 0; j < items; j++)
2258 	    *ip++ = totalcnt++;
2259 
2260 	if (!DXSetComponentValue(newf, "numbers", (Object)newa))
2261 	    goto error;
2262 	newa = NULL;
2263 
2264 	if (!DXSetSeriesMember(news, i, (double)i, (Object)newf))
2265 	    goto error;
2266 	news = NULL;
2267     }
2268 
2269     if (!(retval = addirregarrays(si, "numbers", stackdim)))
2270 	goto error;
2271 
2272     DXDelete((Object)news);
2273     return retval;
2274 
2275   error:
2276     DXDelete((Object)news);
2277     DXDelete((Object)newf);
2278     DXDelete((Object)newa);
2279     DXDelete((Object)retval);
2280     return NULL;
2281 
2282 }
2283 
2284 /* renumber the array in place - we just created it
2285  * so it's not a read only input.
2286  */
2287 static Error
dorenumber(Array newarr,Array renumber)2288 dorenumber(Array newarr, Array renumber)
2289 {
2290     int i;
2291     int items;
2292     int *ip, *rip;
2293     int *mip = NULL;
2294 
2295     /* ref arrays should be able to be ubyte or int.  for now
2296      * force them to be int.
2297      */
2298     if (!DXTypeCheckV(newarr, TYPE_INT, CATEGORY_REAL, 0, NULL)) {
2299 	DXSetError(ERROR_DATA_INVALID, "ref arrays must be type integer");
2300 	return ERROR;
2301     }
2302 
2303     if (!DXGetArrayInfo(newarr, &items, NULL, NULL, NULL, NULL))
2304 	goto error;
2305 
2306     ip = (int *)DXGetArrayData(newarr);
2307     rip = (int *)DXGetArrayData(renumber);
2308     mip = (int *)DXAllocate(sizeof(int) * items);
2309     if (!ip || !rip || !mip)
2310 	goto error;
2311 
2312     for(i=0; i<items; i++)
2313 	mip[i] = -1;
2314 
2315     for(i=0; i<items; i++)
2316 	mip[rip[i]] = i;
2317 
2318     for(i=0; i<items; i++)
2319 	ip[i] = mip[ip[i]];
2320 
2321     DXFree((Pointer)mip);
2322     return OK;
2323 
2324   error:
2325     DXFree((Pointer)mip);
2326     return ERROR;
2327 }
2328 
2329 /* this irregularizes everything.  we should at least look
2330  * for constant arrays or regular arrays w/ deltas == 0.
2331  * colors are often like this.  (constant arrays now handled;
2332  * reg w/ delta=0 is NOT yet.)
2333  * if component == null, input is series of arrays.  don't look
2334  * up the componentt, work directly on the series member.
2335  */
2336 static Array
addirregarrays(StackInfo * si,char * component,int stackdim)2337 addirregarrays(StackInfo *si, char *component, int stackdim)
2338 {
2339     int i, j, k;
2340     Object o;
2341     Type t;
2342     Category c;
2343     int rank, shape[MAXRANK];
2344     Array a, na = NULL;
2345     int ncounts[MAXRANK];
2346     int dims[MAXRANK], temp[MAXRANK];
2347     int nstride[MAXRANK], ostride[MAXRANK];
2348     int nitems, itemsize;
2349     Pointer np, op;
2350 
2351     /* for each member of the series, extract the array and
2352      * add it into one array.
2353      */
2354 
2355     if (component) {
2356 	a = (Array)DXGetComponentValue(si->f0, component);
2357 
2358 	a = foolInvalids(a, component, si->f0, 0);
2359 	if (!a)
2360 	    return NULL;
2361     } else
2362 	a = (Array)DXGetSeriesMember((Series)si->thisobj, 0, NULL);
2363 
2364     if (!DXGetArrayInfo(a, &nitems, &t, &c, &rank, shape))
2365 	return NULL;
2366 
2367     na = DXNewArrayV(t, c, rank, shape);
2368     if (!na)
2369 	goto error;
2370     if (!DXCopyAttributes((Object)na, (Object)a))
2371 	goto error;
2372     np = DXGetArrayData(DXAddArrayData(na, 0, nitems*si->membercnt, NULL));
2373     if (!np)
2374 	goto error;
2375 
2376     itemsize = DXGetItemSize(a);
2377 
2378     for (i=0; i<si->conndim+1; i++)
2379 	dims[i] = i;
2380 
2381     /* old strides - w/o stacked dim*/
2382     k = 1;
2383     for (i=si->conndim; i>stackdim; i--) {
2384 	temp[i] = k;
2385 	k *= si->countlist[i];
2386     }
2387     temp[stackdim] = 0;
2388     for (i=stackdim-1; i>=0; i--) {
2389 	temp[i] = k;
2390 	k *= si->countlist[i];
2391     }
2392 
2393     /* rearrange dims */
2394     for (i=si->conndim; i>=0; i--) {
2395 	ostride[i] = temp[dims[i]];
2396 	ncounts[i] = si->countlist[dims[i]];
2397     }
2398 
2399     /* new strides - w/ stacked dim */
2400     k = 1;
2401     for (i=si->conndim; i>=0; i--) {
2402 	nstride[i] = k;
2403 	k *= ncounts[i];
2404     }
2405 
2406     ncounts[stackdim] = 1;
2407 
2408 
2409     for (j=0; (o=DXGetSeriesMember((Series)si->thisobj, j, NULL)); j++) {
2410 
2411 	if (component) {
2412 	    a = (Array)DXGetComponentValue((Field)o, component);
2413 	    a = foolInvalids(a, component, (Field)o, j);
2414 	    if (!a)
2415 		goto error;
2416 	} else
2417 	    a = (Array)o;
2418 
2419 	op = DXGetArrayData(a);
2420 	np = (char *)DXGetArrayData(na) + j*nstride[stackdim]*itemsize;
2421 
2422 	_dxfPermute(si->conndim+1, np, nstride, ncounts, itemsize, op, ostride);
2423 
2424     }
2425 
2426     return na;
2427 
2428   error:
2429     DXDelete((Object)na);
2430     return NULL;
2431 }
2432 
2433 
2434 
2435 /* take a positions component with all the previous positions
2436  * stacked into one array, plus the new series array (both irregular)
2437  * and permute them into a single array.
2438  */
2439 static Array
addseriesdim(StackInfo * si,Array oldpos,Array addpos,int stackdim)2440 addseriesdim(StackInfo *si, Array oldpos, Array addpos, int stackdim)
2441 {
2442     int i, j, k;
2443     int reps, skips;
2444     Type t;
2445     Category c;
2446     int rank, shape[MAXRANK];
2447     Array newpos = NULL;
2448     int nitems;
2449     float *ofp, *nfp;
2450 
2451     /* oldpos is the old positions, combined into a single array
2452      * but without the new position.  addpos is the new positions
2453      * to add.  use permute to get them into the right order.
2454      */
2455 
2456 
2457     if (!DXGetArrayInfo(oldpos, &nitems, &t, &c, &rank, shape))
2458 	return NULL;
2459     ofp = (float *)DXGetArrayData(oldpos);
2460     if (!ofp)
2461 	goto error;
2462 
2463     if (rank == 0) {
2464 	rank = 1;
2465 	shape[0] = 1;
2466     }
2467     shape[0] += 1;
2468     newpos = DXNewArrayV(t, c, rank, shape);
2469     if (!newpos)
2470 	goto error;
2471     if (!DXCopyAttributes((Object)newpos, (Object)addpos))
2472 	goto error;
2473     nfp = (float *)DXGetArrayData(DXAddArrayData(newpos, 0, nitems, NULL));
2474     if (!nfp)
2475 	goto error;
2476 
2477     /* make space for new dim */
2478     for (i=0; i<nitems; i++)
2479 	for (j=0; j<shape[0]; j++) {
2480 	    if (j == stackdim)
2481 		*nfp++ = 0.0;
2482 	    else
2483 		*nfp++ = *ofp++;
2484 	}
2485 
2486 
2487     /* now permute the new array into the slot left in the pos array.
2488      */
2489 
2490     /* compute offset to first and skip between items.
2491      *  if stackdim = maxdim, skip = 1, offset = skip*j;  varies fastest
2492      *  if stackdim = max-1, skip = countlist[maxdim], offset = skip*j
2493      *  if stackdim = max-2, skip = countlist[max*max-1], etc
2494      */
2495     /* reps & skips */
2496     reps = 1;
2497     skips = nitems/si->membercnt;
2498     for (i=si->conndim; i>stackdim; --i) {
2499 	reps *= si->countlist[i];
2500 	skips /= si->countlist[i];
2501     }
2502 
2503     nfp = (float *)DXGetArrayData(newpos);
2504     ofp = (float *)DXGetArrayData(addpos);
2505     if (!nfp || !ofp)
2506 	goto error;
2507 
2508 
2509     /* outer loop - once per data item */
2510     for (i=0; i<si->membercnt; i++) {
2511 	nfp = (float *)DXGetArrayData(newpos);
2512 	nfp += (reps * i) * shape[0];
2513 	for (j=0; j<skips; j++) {
2514 	    for (k=0; k<reps; k++) {
2515 		nfp[k*shape[0] + stackdim] = ofp[i*shape[0] + stackdim];
2516 	    }
2517 	    nfp += shape[0] * si->membercnt * reps;
2518 	}
2519     }
2520 
2521    return newpos;
2522 
2523   error:
2524     DXDelete((Object)newpos);
2525     return NULL;
2526 }
2527 
2528 
2529 /*
2530  * check the array - to stack invalids i have to have a real instantiated
2531  * array even if there are no invalids in one or more of the individual
2532  * series members.  so if there isn't an invalid array here
2533  * create one filled with 0s (all valid).  if there is one but it's ref,
2534  * create one which is dep.  if this array has nothing to do with invalids,
2535  * just return the original incoming array.
2536  */
2537 static Array
foolInvalids(Array a,char * name,Field f,int seriesmember)2538 foolInvalids(Array a, char *name, Field f, int seriesmember)
2539 {
2540     int i;
2541     Type t;
2542     Category c;
2543     int rank, shape[MAXRANK];
2544     Array na;
2545     int nitems;
2546     int *ip;
2547     Pointer np;
2548     char *attr;
2549 
2550     if (!a) {
2551         if (strncmp(name, "invalid ", strlen("invalid ")) != 0) {
2552             DXSetError(ERROR_DATA_INVALID,
2553                        "series member %d does not contain component `%s'",
2554                        seriesmember, name);
2555             return NULL;
2556         }
2557 
2558         /* prepare an invalid array filled with all valid values.
2559          */
2560         a = (Array)DXGetComponentValue(f, name+strlen("invalid "));
2561         if (!a) {
2562             DXSetError(ERROR_DATA_INVALID,
2563                        "series member %d does not contain component `%s'",
2564                        seriesmember, name+strlen("invalid "));
2565             return NULL;
2566         }
2567         if (!DXGetArrayInfo(a, &nitems, &t, &c, &rank, shape))
2568             return NULL;
2569 
2570         a = DXNewArray(TYPE_UBYTE, CATEGORY_REAL, 0);
2571         if (!a)
2572             return NULL;
2573 
2574         if (!DXAddArrayData(a, 0, nitems, NULL)) {
2575             DXDelete((Object)a);
2576             return NULL;
2577         }
2578 
2579         np = DXGetArrayData(a);
2580         if (!np) {
2581             DXDelete((Object)a);
2582             return NULL;
2583         }
2584 
2585         memset(np, '\0', nitems);
2586 
2587         if (!DXSetStringAttribute((Object)a, "dep", "positions")) {
2588             DXDelete((Object)a);
2589             return NULL;
2590         }
2591 
2592         /* and now continue like there was an invalid array there
2593          * from the beginning...
2594          */
2595 
2596         return a;
2597     }
2598 
2599     if (strncmp(name, "invalid ", strlen("invalid ")) == 0) {
2600 
2601         if (DXGetStringAttribute((Object)a, "ref", &attr) != NULL) {
2602 
2603             /* if the `name' array did already exist, and it's a ref list and
2604              * not a dep list, then we have to expand it into a dep list.
2605              */
2606 
2607             /* prepare an invalid array filled with all valid values.
2608              */
2609             na = (Array)DXGetComponentValue(f, name+strlen("invalid "));
2610             if (!na) {
2611                 DXSetError(ERROR_DATA_INVALID,
2612                            "series member %d does not contain component `%s'",
2613                            seriesmember, name+strlen("invalid "));
2614                 return NULL;
2615             }
2616             if (!DXGetArrayInfo(na, &nitems, &t, &c, &rank, shape))
2617                 return NULL;
2618 
2619             na = DXNewArray(TYPE_UBYTE, CATEGORY_REAL, 0);
2620             if (!na)
2621                 return NULL;
2622 
2623             if (!DXAddArrayData(na, 0, nitems, NULL)) {
2624                 DXDelete((Object)na);
2625                 return NULL;
2626             }
2627 
2628             np = DXGetArrayData(na);
2629             if (!np) {
2630                 DXDelete((Object)na);
2631                 return NULL;
2632             }
2633 
2634             memset(np, '\0', nitems);
2635 
2636             /* and now go thru the ref list and fill in the invalids.
2637              * get the original count of the ref list
2638              */
2639             if (!DXGetArrayInfo(a, &nitems, &t, &c, &rank, shape))
2640                 return NULL;
2641 
2642             ip = (int *)DXGetArrayData(a);
2643 
2644             for (i=0; i<nitems; i++)
2645                 ((ubyte *)np)[ip[i]] = (ubyte)1;
2646 
2647             /* now pretend this new array is what we originally had */
2648             a = na;
2649 
2650             if (!DXSetStringAttribute((Object)a, "dep", "positions")) {
2651                 DXDelete((Object)a);
2652                 return NULL;
2653             }
2654 
2655         }
2656 
2657     }
2658 
2659     /* either the original, or the newly constructed one */
2660     return a;
2661 }
2662 
2663 
2664 
2665 /* decide if the series position is regular.  if not, return ok
2666  * but leave newpos set to NULL.  if a real error occurred, return
2667  * error and the value of newpos is ignored.
2668  */
2669 static Error
seriesregular(StackParms * sp,StackInfo * si,Array * newpos)2670 seriesregular(StackParms *sp, StackInfo *si, Array *newpos)
2671 {
2672     int i;
2673     float s0, sN, sL;
2674     double del0, delN, nmax;
2675     float orig[MAXRANK], *genorig = NULL;
2676     float delta[MAXRANK], *gendelta = NULL;
2677 
2678     if (si->inclass != CLASS_SERIES)
2679 	return OK;
2680 
2681     if (!DXGetSeriesMember((Series)si->thisobj, 0, &s0))
2682 	goto error;
2683 
2684     if (!DXGetSeriesMember((Series)si->thisobj, 1, &sN))
2685 	goto error;
2686 
2687     del0 = sN - s0;
2688     if (fabs(del0) < DXD_MIN_FLOAT) {
2689 	DXSetError(ERROR_DATA_INVALID, "#11075");
2690 	goto error;
2691     }
2692 
2693     sL = sN;
2694     for (i=2; DXGetSeriesMember((Series)si->thisobj, i, &sN); i++) {
2695 	delN = sN - sL;
2696 
2697 	/* fuzzy compare to see if del == ndel */
2698 	nmax = nonzeromax(sN, s0);
2699 	if (fabs(nmax) > DXD_MIN_FLOAT && fabs((delN-del0)/nmax) < NORMFUZZ)
2700 	    ; /* close enough to be equal */
2701 	else
2702 	    goto done;   /* no error, just not regularly spaced */
2703 
2704 	if ((del0 > 0 && delN < 0) || (del0 < 0 && delN > 0) || (delN == 0.0)) {
2705 	    DXSetError(ERROR_DATA_INVALID, "#11075");
2706 	    goto error;
2707 	}
2708 
2709 	sL = sN;
2710     }
2711 
2712     /* if the series spacing is regular, construct an N+1 D
2713      * regular array to combine with the other part of the positions.
2714      */
2715     if (si->posdim+1 < MAXRANK) {
2716 	genorig = orig;
2717 	gendelta = delta;
2718     } else {
2719 	genorig = (float *)DXAllocate(sizeof(float) * (si->posdim+1));
2720 	gendelta = (float *)DXAllocate(sizeof(float) * (si->posdim+1));
2721 	if (!genorig || !gendelta)
2722 	    goto error;
2723     }
2724 
2725     for (i=0; i<si->posdim+1; i++) {
2726 	if (i == sp->stackdim) {
2727 	    genorig[i] = s0;
2728 	    gendelta[i] = del0;
2729 	} else {
2730 	    genorig[i] = 0.0;
2731 	    gendelta[i] = 0.0;
2732 	}
2733     }
2734 
2735     *newpos = (Array)DXNewRegularArray(TYPE_FLOAT, si->posdim+1, si->membercnt,
2736 				      genorig, gendelta);
2737     if (!*newpos)
2738 	goto error;
2739 
2740 
2741   done:
2742     FREEPTR(genorig, orig);
2743     FREEPTR(gendelta, delta);
2744     return OK;
2745 
2746   error:
2747     FREEPTR(genorig, orig);
2748     FREEPTR(gendelta, delta);
2749     DXDelete((Object)*newpos);
2750     *newpos = NULL;
2751     return ERROR;
2752 }
2753 
2754 
2755 
2756 /* decide if the series spacing is regular.  if so, construct a
2757  * regular grid of the proper dim.  seriesregular() returns error only
2758  * for a real error.  if the series is regular, it returns a reg array
2759  * in newseriesdim.  if not, it returns OK but null for the new array
2760  * and you have to call seriesspacing() to construct an expanded array.
2761  */
2762 static Error
newdimregular(StackParms * sp,StackInfo * si,Array * newarr,int * regflag)2763 newdimregular(StackParms *sp, StackInfo *si, Array *newarr, int *regflag)
2764 {
2765 
2766     if (!seriesregular(sp, si, newarr))
2767 	goto error;
2768 
2769     if (*newarr)
2770 	(*regflag)++;
2771     else {
2772 	*newarr = seriesspacing(sp, si);
2773 	if (! *newarr)
2774 	    goto error;
2775     }
2776 
2777     return OK;
2778 
2779   error:
2780     return ERROR;
2781 }
2782 
2783 /* this really isn't the question.  the question is: is the
2784  * positions component on the all series members identical,
2785  * whatever it is?  if so, then we can use the first one as
2786  * a template and make a product array with the other dim.
2787  * if it's not, we have to expand it completely no matter
2788  * what the original form was.
2789  */
2790 static Error
allposregular(StackInfo * si,int * regflag)2791 allposregular(StackInfo *si, int *regflag)
2792 {
2793     Field subo;
2794     Array pos;
2795     int i, j;
2796     int items;
2797     int shape0, shape;
2798     int counts0[MAXRANK], *gencounts0 = NULL;
2799     float orig0[MAXRANK], *genorig0 = NULL;
2800     float delta0[MAXRANK*MAXRANK], *gendelta0 = NULL;
2801     int counts[MAXRANK], *gencounts = NULL;
2802     float orig[MAXRANK], *genorig = NULL;
2803     float delta[MAXRANK*MAXRANK], *gendelta = NULL;
2804 
2805 
2806     /* look at first member as a reference */
2807     if (!DXQueryGridPositions(si->ap0, &shape0, NULL, NULL, NULL)) {
2808 	*regflag = 0;
2809 	goto done;
2810     }
2811 
2812     if (shape0 < MAXRANK) {
2813 	if (!DXQueryGridPositions(si->ap0, NULL, counts0, orig0, delta0))
2814 	    goto error;
2815 	gencounts0 = counts0;
2816 	genorig0 = orig0;
2817 	gendelta0 = delta0;
2818 	gencounts = counts;
2819 	genorig = orig;
2820 	gendelta = delta;
2821     } else {
2822 	items = sizeof(float) * shape0;
2823 	genorig0 = (float *)DXAllocate(items);
2824 	gencounts0 = (int *)DXAllocate(items);
2825 	genorig = (float *)DXAllocate(items);
2826 	gencounts = (int *)DXAllocate(items);
2827 
2828 	items *= shape0;
2829 	gendelta0 = (float *)DXAllocate(items);
2830 	gendelta = (float *)DXAllocate(items);
2831 	if (!gencounts0 || !genorig0 || !gendelta0 || !gencounts || !genorig || !gendelta)
2832 	    goto error;
2833 
2834 	if (!DXQueryGridPositions(si->ap0, NULL, gencounts0, genorig0, gendelta0))
2835 	    goto error;
2836     }
2837 
2838 
2839 
2840     for (i=1; (subo=(Field)DXGetSeriesMember((Series)si->thisobj, i, NULL)); i++) {
2841 	if (DXGetObjectClass((Object)subo) != CLASS_FIELD)
2842 	    goto error;
2843 
2844 	pos = (Array)DXGetComponentValue(subo, "positions");
2845 	if (!pos)
2846 	    goto error;
2847 
2848 	if (!DXQueryGridPositions(pos, &shape, NULL, NULL, NULL)) {
2849 	    *regflag = 0;
2850 	    goto done;
2851 	}
2852 
2853 	if (shape != shape0) {
2854 	    *regflag = 0;
2855 	    goto done;
2856 	}
2857 
2858 	if (!DXQueryGridPositions(pos, NULL, gencounts, genorig, gendelta))
2859 	    goto error;
2860 
2861 	for (j=0; j<shape; j++) {
2862 	    if (gencounts[j] != gencounts0[j]) {
2863 		*regflag = 0;
2864 		goto done;
2865 	    }
2866 	    if (genorig[j] != genorig0[j]) {
2867 		*regflag = 0;
2868 		goto done;
2869 	    }
2870 	    if (gendelta[j] != gendelta0[j]) {
2871 		*regflag = 0;
2872 		goto done;
2873 	    }
2874 	}
2875 
2876 
2877     }
2878 
2879     *regflag = 1;
2880 
2881   done:
2882     FREEPTR(gencounts0, counts0);
2883     FREEPTR(genorig0, orig0);
2884     FREEPTR(gendelta0, delta0);
2885     FREEPTR(gencounts, counts);
2886     FREEPTR(genorig, orig);
2887     FREEPTR(gendelta, delta);
2888     return OK;
2889 
2890   error:
2891     FREEPTR(gencounts0, counts0);
2892     FREEPTR(genorig0, orig0);
2893     FREEPTR(gendelta0, delta0);
2894     FREEPTR(gencounts, counts);
2895     FREEPTR(genorig, orig);
2896     FREEPTR(gendelta, delta);
2897     return ERROR;
2898 
2899 }
2900 
2901 static Array
seriesspacing(StackParms * sp,StackInfo * si)2902 seriesspacing(StackParms *sp, StackInfo *si)
2903 {
2904     int i;
2905     int shape, index;
2906     float *fp = NULL;
2907     float pos;
2908     Array newarr = NULL;
2909 
2910 
2911     /* if the series positions are NOT regular, construct an
2912      * N+1 D generic positions array, which will either be used
2913      * as a product term, or combined into an expanded array
2914      * depending on the rest of the positions.
2915      */
2916 
2917     shape = si->posdim + 1;
2918     index = sp->stackdim;
2919 
2920     newarr = DXNewArray(TYPE_FLOAT, CATEGORY_REAL, 1, shape);
2921     if (!newarr)
2922 	goto error;
2923 
2924     if (!DXAddArrayData(newarr, 0, si->membercnt, NULL))
2925 	goto error;
2926 
2927     fp = (float *)DXGetArrayData(newarr);
2928     if (!fp)
2929 	goto error;
2930 
2931     memset((Pointer)fp, '\0', sizeof(float) * shape * si->membercnt);
2932 
2933 
2934 
2935     for (i=0; DXGetSeriesMember((Series)si->thisobj, i, &pos); i++)
2936 	fp[shape*i + index] = pos;
2937 
2938 
2939     return newarr;
2940 
2941   error:
2942     return NULL;
2943 }
2944 
2945 
2946 static Object
stackgroup(StackParms * sp,StackInfo * si)2947 stackgroup(StackParms *sp, StackInfo *si)
2948 {
2949     Field retval = NULL;
2950     Object subo;
2951     float seriespos;
2952     Class class;
2953     Array newpos = NULL;
2954     Array newcon = NULL;
2955     Array newarr = NULL;
2956     int i;
2957 
2958 
2959     /* if series only has 1 member and it is a field, stack it alone */
2960     if (si->membercnt == 1) {
2961 	if (si->inclass == CLASS_SERIES) {
2962 	    if (!(subo=DXGetSeriesMember((Series)si->thisobj, 0, &seriespos)))
2963 		goto error;
2964 
2965 	    class = DXGetObjectClass(subo);
2966 	    if (class == CLASS_FIELD) {
2967 
2968 		si->thisobj = subo;
2969 		si->inclass = class;
2970 
2971 		retval = (Field)stackpos(sp, si, seriespos);
2972 		if (!retval)
2973 		    goto error;
2974 
2975 		goto done;
2976 	    }
2977 	} else {
2978 	    if (!(subo = DXGetEnumeratedMember((Group)si->thisobj, 0, NULL)))
2979 		goto error;
2980 
2981 	    class = DXGetObjectClass(subo);
2982 	    if (class == CLASS_FIELD) {
2983 		si->thisobj = subo;
2984 		si->inclass = class;
2985 
2986 		retval = (Field)stackpos(sp, si, 0.0);
2987 		if (!retval)
2988 		    goto error;
2989 
2990 		goto done;
2991 	    }
2992 	}
2993     }
2994 
2995 
2996     /* return field */
2997     retval = DXNewField();
2998     if (!retval)
2999 	goto error;
3000 
3001 
3002     /* if the members of the series are arrays, stack them into lines */
3003     if (ISSET(si->stackflags, POINTS)) {
3004 	newcon = DXMakeGridConnections(1, si->membercnt);
3005 	if (!newcon)
3006 	    goto error;
3007 
3008 	if (!DXSetComponentValue(retval, "connections", (Object)newcon))
3009 	    goto error;
3010 	newcon = NULL;
3011 
3012 	newpos = addirregarrays(si, NULL, sp->stackdim);
3013 	if (!newpos)
3014 	    goto error;
3015 
3016 	if (!DXSetComponentValue(retval, "positions", (Object)newpos))
3017 	    goto error;
3018 	newpos = NULL;
3019 
3020 	if (!DXEndField(retval))
3021 	    goto error;
3022 
3023 	goto done;
3024     }
3025 
3026     /* "normal" case of a series of fields */
3027 
3028     /* stack the connections */
3029     newcon = addcondim(si, si->ac0, sp->stackdim, si->membercnt);
3030     if (!newcon)
3031 	goto error;
3032 
3033     if (!DXSetComponentValue(retval, "connections", (Object)newcon))
3034 	goto error;
3035     newcon = NULL;
3036 
3037 
3038     /* stack the positions */
3039     newpos = addpositions(sp, si);
3040     if (!newpos)
3041 	goto error;
3042 
3043     if (!DXSetComponentValue(retval, "positions", (Object)newpos))
3044 	goto error;
3045     newpos = NULL;
3046 
3047     /* stack each of the members */
3048     for (i=0; i<si->compcount; i++) {
3049 	if (ISSET(si->compflags[i], SKIP))
3050 	    continue;
3051 
3052 	if (strcmp("connections", si->compname[i]) == 0)
3053 	    continue;
3054 	if (strcmp("positions", si->compname[i]) == 0)
3055 	    continue;
3056 
3057 	if (ISSET(si->compflags[i], DOFIRST)) {
3058 	    /* loop thru members looking for a field w/ component? */
3059 	    newarr = (Array)DXGetComponentValue(si->f0, si->compname[i]);
3060 	    if (!newarr)
3061 		continue;
3062 	    SET(si->compflags[i], SKIP);
3063 
3064 	} else {
3065 
3066 	    newarr = addarrays(sp, si, si->compname[i], si->compflags[i]);
3067 	    if (!newarr)
3068 		goto error;
3069 	}
3070 
3071 	if (!DXSetComponentValue(retval, si->compname[i], (Object)newarr))
3072 	    goto error;
3073 	newarr = NULL;
3074 
3075     }
3076 
3077     if (ISSET(si->stackflags, INVALIDS) && !setinvalids(si, retval))
3078 	goto error;
3079 
3080     if (!DXEndField(retval))
3081 	goto error;
3082 
3083   done:
3084     return (Object)retval;
3085 
3086   error:
3087     DXDelete((Object)retval);
3088     DXDelete((Object)newpos);
3089     DXDelete((Object)newcon);
3090     DXDelete((Object)newarr);
3091     return NULL;
3092 }
3093 
setinvalids(StackInfo * si,Field f)3094 static Field setinvalids(StackInfo *si, Field f)
3095 {
3096     InvalidComponentHandle ich;
3097 
3098     if (ISSET(si->stackflags, INVPOS))
3099 	ich = DXCreateInvalidComponentHandle((Object)f,
3100 					     NULL, "invalid positions");
3101     else
3102 	ich = DXCreateInvalidComponentHandle((Object)f,
3103 					     NULL, "invalid connections");
3104 
3105     if (!ich)
3106 	return NULL;
3107 
3108     if (!DXSaveInvalidComponent(f, ich))
3109 	return NULL;
3110 
3111     if (!DXFreeInvalidComponentHandle(ich))
3112 	return NULL;
3113 
3114     return f;
3115 }
3116 
3117 static void
freespace(StackInfo * si)3118 freespace(StackInfo *si)
3119 {
3120     int i;
3121 
3122     if (!si)
3123 	return;
3124 
3125     if (si->countlist) {
3126 	DXFree((Pointer)si->countlist);
3127 	si->countlist = NULL;
3128     }
3129     if (si->compflags) {
3130 	DXFree((Pointer)si->compflags);
3131 	si->compflags = NULL;
3132     }
3133     if (si->compname) {
3134 	for (i=0; i<si->compcount; i++)
3135 	    DXFree((Pointer)si->compname[i]);
3136 	DXFree((Pointer)si->compname);
3137 	si->compname = NULL;
3138     }
3139     si->compcount = 0;
3140 }
3141 
3142 
3143 /* return the max of a and b unless the max is zero, then
3144  * return the other value.
3145  */
3146 static double
nonzeromax(double a,double b)3147 nonzeromax(double a, double b)
3148 {
3149     if (a == 0.0 && b != 0.0)
3150 	return b;
3151     if (b == 0.0 && a != 0.0)
3152 	return a;
3153 
3154     if (a != 0.0 && b != 0.0)
3155 	return MAX(a, b);
3156 
3157     return NORMFUZZ;
3158 }
3159 
3160 
3161 #if 0
3162 static int
3163 isZero(Type t, Pointer value)
3164 {
3165     switch (t) {
3166       case TYPE_BYTE:   return (byte)0     == *(byte *)value;
3167       case TYPE_UBYTE:  return (ubyte)0    == *(ubyte *)value;
3168       case TYPE_SHORT:  return (short)0    == *(short *)value;
3169       case TYPE_USHORT: return (ushort)0   == *(ushort *)value;
3170       case TYPE_INT:    return (int)0      == *(int *)value;
3171       case TYPE_UINT:   return (uint)0     == *(uint *)value;
3172       case TYPE_FLOAT:  return (float)0.0  == *(float *)value;
3173       case TYPE_DOUBLE: return (double)0.0 == *(double *)value;
3174       default:          return 0;
3175     }
3176 
3177     /* not reached */
3178 }
3179 #endif
3180 
3181